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

Linux Cross Reference
Tina5/tina-libs/tina/geometry/geomSpline_kws.c

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

  1 /**********
  2  * 
  3  * This file is part of the TINA Open Source Image Analysis Environment
  4  * henceforth known as TINA
  5  *
  6  * TINA is free software; you can redistribute it and/or modify
  7  * it under the terms of the GNU Lesser General Public License as 
  8  * published by the Free Software Foundation.
  9  *
 10  * TINA is distributed in the hope that it will be useful,
 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  * GNU Lesser General Public License for more details.
 14  *
 15  * You should have received a copy of the GNU Lesser General Public License
 16  * along with TINA; if not, write to the Free Software Foundation, Inc., 
 17  * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 18  *
 19  **********
 20  * 
 21  * Program :    TINA
 22  * File    :  $Source: /home/tina/cvs/tina-libs/tina/geometry/geomSpline_kws.c,v $
 23  * Date    :  $Date: 2005/01/23 19:10:21 $
 24  * Version :  $Revision: 1.2 $
 25  * CVS Id  :  $Id: geomSpline_kws.c,v 1.2 2005/01/23 19:10:21 paul Exp $
 26  *
 27  * Notes :
 28  *
 29  *********
 30 */
 31 
 32 
 33 #include "geomSpline_kws.h"
 34 
 35 #if HAVE_CONFIG_H
 36   #include <config.h>
 37 #endif
 38 
 39 #include <stdio.h>
 40 #include <math.h>
 41 #include <tina/sys/sysDef.h>
 42 #include <tina/sys/sysPro.h>
 43 #include <tina/math/mathDef.h>
 44 #include <tina/math/mathPro.h>
 45 #include <tina/geometry/geom_CurveDef.h>
 46 #include <tina/geometry/geom_CurvePro.h>
 47 #include <tina/geometry/geom_SplineDef.h>
 48 #include <tina/geometry/geomSpline_ucbs2.h>
 49 #include <tina/geometry/geomSpline_knots.h>
 50 
 51 Vec2    kwsnake_eval(Kwsnake * kwsnake, double t)
 52 {
 53     int     i, i1;
 54     int     n = kwsnake->n;
 55     double *x = kwsnake->x;
 56     double *y = kwsnake->y;
 57     double  dt, dt1;
 58 
 59     i = (int)floor(t);
 60     dt = t - i;
 61     dt1 = 1.0 - dt;
 62     while (i < 0)
 63         i += n;
 64     i %= n;
 65     i1 = (i + 1) % n;
 66     return (vec2(dt1 * x[i] + dt * x[i1], dt1 * y[i] + dt * y[i1]));
 67 }
 68 
 69 /**
 70 make a spline with given points
 71 **/
 72 
 73 Kwsnake *kwsnake_make(int type, int n, double *x, double *y)
 74 {
 75     Kwsnake *kwsnake = ts_ralloc(Kwsnake);
 76 
 77     kwsnake->type = type;
 78     kwsnake->n = n;
 79     kwsnake->x = dvector_dcopy(x, 0, n);
 80     kwsnake->y = dvector_dcopy(y, 0, n);
 81     return (kwsnake);
 82 }
 83 
 84 Kwsnake *kwsnake_copy(Kwsnake * kwsnake)
 85 {
 86     if (kwsnake == NULL)
 87         return (NULL);
 88     return (kwsnake_make(kwsnake->type, kwsnake->n, kwsnake->x, kwsnake->y));
 89 }
 90 
 91 void    kwsnake_free(Kwsnake * kwsnake)
 92 {
 93     if (kwsnake == NULL)
 94         return;
 95     dvector_free(kwsnake->x, 0);
 96     dvector_free(kwsnake->y, 0);
 97     rfree((void *) kwsnake);
 98 }
 99 
100 Kwsnake *kwsnake_interpolate_list(int type, List * points)
101 {
102     double *x, *y;
103     Kwsnake *kwsnake;
104     List   *ptr;
105     int     i, n;
106 
107     n = list_length(points);
108     x = dvector_alloc(0, n);
109     y = dvector_alloc(0, n);
110     for (i = 0, ptr = points; i < n; i++, ptr = ptr->next)
111     {
112         Vec2   *v = (Vec2 *) ptr->to;
113 
114         x[i] = vec2_x(*v);
115         y[i] = vec2_y(*v);
116     }
117     kwsnake = kwsnake_make(type, n, x, y);
118     dvector_free(x, 0);
119     dvector_free(y, 0);
120     return (kwsnake);
121 }
122 
123 Kwsnake *kwsnake_from_ucbs2(Ucbs2 * ucbs, double sample, int *mask)
124 
125 /* approx sample spacing */
126 /* if not null set ks indices for control points */
127 {
128     double *x, *y;
129     double  s, d;
130     int     i, j, k, n, nmax, ntot, steps;
131     Vec2    v = {Vec2_id};
132     Vec2    v_next = {Vec2_id};
133 
134     if (ucbs == NULL)
135         return (NULL);
136 
137     n = ucbs->n;
138     nmax = (ucbs->type == LOOP) ? n : n - 1;
139 
140     v = ucbs2_eval(ucbs, 0.0);
141     for (ntot = 0, i = 0; i < nmax; ++i, v = v_next)
142     {
143         v_next = ucbs2_eval(ucbs, i + 1.0);
144         d = vec2_mod(vec2_diff(v_next, v));
145         steps = (int)(d / sample + 1);
146 
147         if (mask != NULL)
148             mask[i] = ntot;
149         ntot += steps;
150     }
151 
152     if (ucbs->type != LOOP)
153     {
154         if (mask != NULL)
155             mask[i] = ntot;
156         ntot++;
157     }
158     x = dvector_alloc(0, ntot);
159     y = dvector_alloc(0, ntot);
160 
161     v = ucbs2_eval(ucbs, 0.0);
162     for (i = 0, k = 0; i < nmax; ++i, v = v_next)
163     {
164         v_next = ucbs2_eval(ucbs, i + 1.0);
165         d = vec2_mod(vec2_diff(v_next, v));
166         steps = (int)(d / sample + 1);
167         s = 1.0 / steps;
168 
169         for (j = 0; j < steps; ++j, ++k)
170         {
171             v = ucbs2_eval(ucbs, i + j * s);
172             x[k] = vec2_x(v);
173             y[k] = vec2_y(v);
174         }
175     }
176 
177     if (ucbs->type != LOOP)     /* add the last point */
178     {
179         v = ucbs2_eval(ucbs, (double) nmax);
180         x[k] = vec2_x(v);
181         y[k] = vec2_y(v);
182     }
183     return (kwsnake_make(ucbs->type, ntot, x, y));
184 }
185 
186 Kwsnake *kwsnake_of_es(Tstring * es, int ds)
187 {
188     int     n;
189     Kwsnake *kwsnake;
190     double *x, *y;
191 
192     if (es == NULL)
193         return (NULL);
194 
195     n = MAX(5, es->count / ds);
196     x = dvector_alloc(0, n);
197     y = dvector_alloc(0, n);
198     str2_get_knots(es, n, x, y);
199     kwsnake = kwsnake_make(es->type, n, x, y);
200     dvector_free(x, 0);
201     dvector_free(y, 0);
202     return (kwsnake);
203 }
204 
205 /* Convert kwsnake to edge_string */
206 Tstring *es_of_kwsnake(Kwsnake * kwsnake)
207 {
208     double  t1 = 0.0;
209     double t2 = 0.0;
210 
211     if (kwsnake == NULL)
212         return (NULL);
213 
214     switch (kwsnake->type)
215     {
216     case LOOP:
217         t2 = kwsnake->n;
218         break;
219     case STRING:
220         t2 = kwsnake->n - 1;
221         break;
222     }
223     return (str2_of_curve2(kwsnake_eval, (void *) kwsnake, t1, t2, NULL));
224 }
225 
226 double  kws_length(Kwsnake * kwsnake)
227 {
228     int     i, n;
229     double *x, *y, l = 0.0;
230     Vec2    p1 = {Vec2_id};
231     Vec2    p2 = {Vec2_id};
232 
233     if (kwsnake == NULL)
234         return (-1.0);
235     n = kwsnake->n;
236     x = kwsnake->x;
237     y = kwsnake->y;
238     p2 = vec2(x[n - 1], y[n - 1]);
239     for (i = 0; i < n; i++)
240     {
241         p1 = p2;
242         p2 = vec2(x[i], y[i]);
243         l += vec2_dist(p1, p2);
244     }
245     return (l);
246 }
247 

~ [ 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.