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

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

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