~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina4/src/vision/spline/kws.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /**@(#)
  2 **/
  3 #include <math.h>
  4 #include <tina/sys.h>
  5 #include <tina/sysfuncs.h>
  6 #include <tina/math.h>
  7 #include <tina/mathfuncs.h>
  8 #include <tina/vision.h>
  9 #include <tina/visionfuncs.h>
 10 
 11 Vec2    kwsnake_eval(Kwsnake * kwsnake, double t)
 12 {
 13     int     i, i1;
 14     int     n = kwsnake->n;
 15     double *x = kwsnake->x;
 16     double *y = kwsnake->y;
 17     double  dt, dt1;
 18 
 19     i = (int)floor(t);
 20     dt = t - i;
 21     dt1 = 1.0 - dt;
 22     while (i < 0)
 23         i += n;
 24     i %= n;
 25     i1 = (i + 1) % n;
 26     return (vec2(dt1 * x[i] + dt * x[i1], dt1 * y[i] + dt * y[i1]));
 27 }
 28 
 29 /**
 30 make a spline with given points
 31 **/
 32 
 33 Kwsnake *kwsnake_make(int type, int n, double *x, double *y)
 34 {
 35     Kwsnake *kwsnake = ts_ralloc(Kwsnake);
 36 
 37     kwsnake->type = type;
 38     kwsnake->n = n;
 39     kwsnake->x = dvector_dcopy(x, 0, n);
 40     kwsnake->y = dvector_dcopy(y, 0, n);
 41     return (kwsnake);
 42 }
 43 
 44 Kwsnake *kwsnake_copy(Kwsnake * kwsnake)
 45 {
 46     if (kwsnake == NULL)
 47         return (NULL);
 48     return (kwsnake_make(kwsnake->type, kwsnake->n, kwsnake->x, kwsnake->y));
 49 }
 50 
 51 void    kwsnake_free(Kwsnake * kwsnake)
 52 {
 53     if (kwsnake == NULL)
 54         return;
 55     dvector_free((void *) kwsnake->x, 0);
 56     dvector_free((void *) kwsnake->y, 0);
 57     rfree((void *) kwsnake);
 58 }
 59 
 60 Kwsnake *kwsnake_interpolate_list(int type, List * points)
 61 {
 62     double *x, *y;
 63     Kwsnake *kwsnake;
 64     List   *ptr;
 65     int     i, n;
 66 
 67     n = list_length(points);
 68     x = dvector_alloc(0, n);
 69     y = dvector_alloc(0, n);
 70     for (i = 0, ptr = points; i < n; i++, ptr = ptr->next)
 71     {
 72         Vec2   *v = (Vec2 *) ptr->to;
 73 
 74         x[i] = vec2_x(*v);
 75         y[i] = vec2_y(*v);
 76     }
 77     kwsnake = kwsnake_make(type, n, x, y);
 78     dvector_free((void *) x, 0);
 79     dvector_free((void *) y, 0);
 80     return (kwsnake);
 81 }
 82 
 83 Kwsnake *kwsnake_from_ucbs2(Ucbs2 * ucbs, double sample, int *mask)
 84 
 85 /* approx sample spacing */
 86 /* if not null set ks indices for control points */
 87 {
 88     double *x, *y;
 89     double  s, d;
 90     int     i, j, k, n, nmax, ntot, steps;
 91     Vec2    v = {Vec2_id};
 92     Vec2    v_next = {Vec2_id};
 93 
 94     if (ucbs == NULL)
 95         return (NULL);
 96 
 97     n = ucbs->n;
 98     nmax = (ucbs->type == LOOP) ? n : n - 1;
 99 
100     v = ucbs2_eval(ucbs, 0.0);
101     for (ntot = 0, i = 0; i < nmax; ++i, v = v_next)
102     {
103         v_next = ucbs2_eval(ucbs, i + 1.0);
104         d = vec2_mod(vec2_diff(v_next, v));
105         steps = (int)(d / sample + 1);
106 
107         if (mask != NULL)
108             mask[i] = ntot;
109         ntot += steps;
110     }
111 
112     if (ucbs->type != LOOP)
113     {
114         if (mask != NULL)
115             mask[i] = ntot;
116         ntot++;
117     }
118     x = dvector_alloc(0, ntot);
119     y = dvector_alloc(0, ntot);
120 
121     v = ucbs2_eval(ucbs, 0.0);
122     for (i = 0, k = 0; i < nmax; ++i, v = v_next)
123     {
124         v_next = ucbs2_eval(ucbs, i + 1.0);
125         d = vec2_mod(vec2_diff(v_next, v));
126         steps = (int)(d / sample + 1);
127         s = 1.0 / steps;
128 
129         for (j = 0; j < steps; ++j, ++k)
130         {
131             v = ucbs2_eval(ucbs, i + j * s);
132             x[k] = vec2_x(v);
133             y[k] = vec2_y(v);
134         }
135     }
136 
137     if (ucbs->type != LOOP)     /* add the last point */
138     {
139         v = ucbs2_eval(ucbs, (double) nmax);
140         x[k] = vec2_x(v);
141         y[k] = vec2_y(v);
142     }
143     return (kwsnake_make(ucbs->type, ntot, x, y));
144 }
145 
146 Kwsnake *kwsnake_of_es(Tstring * es, int ds)
147 {
148     int     n;
149     Kwsnake *kwsnake;
150     double *x, *y;
151 
152     if (es == NULL)
153         return (NULL);
154 
155     n = MAX(5, es->count / ds);
156     x = dvector_alloc(0, n);
157     y = dvector_alloc(0, n);
158     str2_get_knots(es, n, x, y);
159     kwsnake = kwsnake_make(es->type, n, x, y);
160     dvector_free((void *) x, 0);
161     dvector_free((void *) y, 0);
162     return (kwsnake);
163 }
164 
165 /* Convert kwsnake to edge_string */
166 Tstring *es_of_kwsnake(Kwsnake * kwsnake)
167 {
168     double  t1 = 0.0;
169     double t2 = 0.0;
170 
171     if (kwsnake == NULL)
172         return (NULL);
173 
174     switch (kwsnake->type)
175     {
176     case LOOP:
177         t2 = kwsnake->n;
178         break;
179     case STRING:
180         t2 = kwsnake->n - 1;
181         break;
182     }
183     return (str2_of_curve2(kwsnake_eval, (void *) kwsnake, t1, t2, NULL));
184 }
185 
186 double  kws_length(Kwsnake * kwsnake)
187 {
188     int     i, n;
189     double *x, *y, l = 0.0;
190     Vec2    p1 = {Vec2_id};
191     Vec2    p2 = {Vec2_id};
192 
193     if (kwsnake == NULL)
194         return (-1.0);
195     n = kwsnake->n;
196     x = kwsnake->x;
197     y = kwsnake->y;
198     p2 = vec2(x[n - 1], y[n - 1]);
199     for (i = 0; i < n; i++)
200     {
201         p1 = p2;
202         p2 = vec2(x[i], y[i]);
203         l += vec2_dist(p1, p2);
204     }
205     return (l);
206 }
207 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.