6 #include "cgmath/cgmath.h"
14 #define CPLERP(res, a, b, t) \
16 (res).x = (a).x + ((b).x - (a).x) * (t); \
17 (res).y = (a).y + ((b).y - (a).y) * (t); \
23 int init_logo(const char *fname)
27 struct cpnode *cpn, *cplist = 0, *cptail = 0;
30 if(!(fp = fopen(fname, "rb"))) {
31 fprintf(stderr, "failed to load logo curve: %s\n", fname);
34 fgets(buf, sizeof buf, fp);
35 if(memcmp(buf, "GCURVES", 7) != 0) {
36 fprintf(stderr, "invalid logo file: %s\n", fname);
42 while(fgets(buf, sizeof buf, fp)) {
47 while(*ptr && isspace(*ptr)) ptr++;
49 if(sscanf(ptr, "cp %f %f", &v.x, &v.y) == 2) {
50 if(!(cpn = malloc(sizeof *cpn))) {
62 cplist = cptail = cpn;
70 fprintf(stderr, "invalid logo file, found %d control points\n", numcp);
74 if(!(cp = malloc(numcp * sizeof *cp))) {
75 fprintf(stderr, "failed to allocate curve of %d control points\n", numcp);
78 cplist = cplist->next;
87 cplist = cplist->next;
95 static void eval_seg(float *res, int a, int b, float t)
100 res[0] = cp[a].x + (cp[b].x - cp[a].x) * t;
101 res[1] = cp[a].y + (cp[b].y - cp[a].y) * t;
105 prev = a <= 0 ? a : a - 1;
106 next = b >= numcp - 1 ? b : b + 1;
108 res[0] = cgm_bspline(cp[prev].x, cp[a].x, cp[b].x, cp[next].x, t);
109 res[1] = cgm_bspline(cp[prev].y, cp[a].y, cp[b].y, cp[next].y, t);
112 void eval_logo(float *res, float t)
117 if(!cp || numcp <= 0) {
127 if(t < 0.0f) t = 0.0f;
128 if(t > 1.0f) t = 1.0f;
130 idx0 = (int)floor(t * (numcp - 1));
131 if(idx0 > numcp - 2) idx0 = numcp - 2;
134 dt = 1.0f / (float)(numcp - 1);
135 t0 = (float)idx0 * dt;
136 t1 = (float)idx1 * dt;
137 t = (t - t0) / (t1 - t0);
139 eval_seg(res, idx0, idx1, t);