6 #include "cgmath/cgmath.h"
15 #define CPLERP(res, a, b, t) \
17 (res).x = (a).x + ((b).x - (a).x) * (t); \
18 (res).y = (a).y + ((b).y - (a).y) * (t); \
25 int init_logo(const char *fname)
31 #else /* !def NOLOAD */
32 int init_logo(const char *fname)
36 struct cpnode *cpn, *cplist = 0, *cptail = 0;
39 if(!(fp = fopen(fname, "rb"))) {
40 fprintf(stderr, "failed to load logo curve: %s\n", fname);
43 fgets(buf, sizeof buf, fp);
44 if(memcmp(buf, "GCURVES", 7) != 0) {
45 fprintf(stderr, "invalid logo file: %s\n", fname);
51 while(fgets(buf, sizeof buf, fp)) {
56 while(*ptr && isspace(*ptr)) ptr++;
58 if(sscanf(ptr, "cp %f %f", &v.x, &v.y) == 2) {
59 if(!(cpn = malloc(sizeof *cpn))) {
71 cplist = cptail = cpn;
79 fprintf(stderr, "invalid logo file, found %d control points\n", numcp);
83 if(!(cp = malloc(numcp * sizeof *cp))) {
84 fprintf(stderr, "failed to allocate curve of %d control points\n", numcp);
87 cplist = cplist->next;
96 cplist = cplist->next;
105 static void eval_seg(float *res, int a, int b, float t)
110 res[0] = cp[a].x + (cp[b].x - cp[a].x) * t;
111 res[1] = cp[a].y + (cp[b].y - cp[a].y) * t;
115 prev = a <= 0 ? a : a - 1;
116 next = b >= numcp - 1 ? b : b + 1;
118 res[0] = cgm_bspline(cp[prev].x, cp[a].x, cp[b].x, cp[next].x, t);
119 res[1] = cgm_bspline(cp[prev].y, cp[a].y, cp[b].y, cp[next].y, t);
122 void eval_logo(float *res, float t)
127 if(!cp || numcp <= 0) {
137 if(t < 0.0f) t = 0.0f;
138 if(t > 1.0f) t = 1.0f;
140 idx0 = (int)floor(t * (numcp - 1));
141 if(idx0 > numcp - 2) idx0 = numcp - 2;
144 dt = 1.0f / (float)(numcp - 1);
145 t0 = (float)idx0 * dt;
146 t1 = (float)idx1 * dt;
147 t = (t - t0) / (t1 - t0);
149 eval_seg(res, idx0, idx1, t);