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/geomCurve_con_fltr.c,v $
27 * Date : $Date: 2003/09/09 16:02:21 $
28 * Version : $Revision: 1.2 $
29 * CVS Id : $Id: geomCurve_con_fltr.c,v 1.2 2003/09/09 16:02:21 ipoole Exp $
30 *
31 * Author : Legacy TINA
32 *
33 * Notes : Avoids too many parameters in conic_filter & calling functions
34 *
35 *********
36 */
37
38 #include "geomCurve_con_fltr.h"
39
40 #if HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
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/geomDef.h>
50 #include <tina/geometry/geom_CurveDef.h>
51 #include <tina/geometry/geomCurve_con_util.h>
52 #include <tina/geometry/geomCurve_con_stat.h>
53 #include <tina/geometry/geomCurve_conic_5pt.h>
54
55
56 static double (*filter) () = NULL; /* Filter to be applied */ /* static data! */
57
58 double (*conic_filter_get()) ( void /* ??? */ )
59 {
60 return (filter);
61 }
62
63 void conic_filter_set(double (*newfilter) ( /* ??? */ ))
64 {
65 filter = newfilter;
66 }
67
68 /**
69 sampling frequency on fitted curve
70 **/
71
72 static int sample = 4; /* static data! */
73
74 int conic_sample_get(void)
75 {
76 return (sample);
77 }
78
79 void conic_sample_set(int newsample)
80 {
81 sample = newsample;
82 }
83
84 /**
85 nominal variance of fitted points
86 **/
87
88 static double var = 1.0; /* static data! */
89
90 double conic_var_get(void)
91 {
92 return (var);
93 }
94
95 void conic_var_set(double newvar)
96 {
97 var = newvar;
98 }
99
100 /**
101 number of filter iterations
102 **/
103
104 static int iter = 3; /* static data! */
105
106 int conic_iter_get(void)
107 {
108 return (iter);
109 }
110
111 void conic_iter_set(int newiter)
112 {
113 iter = newiter;
114 }
115
116 /* Apply chosen filter to point string */
117 void conic_filter(Conic * conic, List * start, List * end)
118 {
119 Conic_stat stats = {Conic_stat_id};
120 double scale;
121 List *dptr;
122 Vec2 p = {Vec2_id};
123 Vec2 p1 = {Vec2_id};
124 Vec2 p2 = {Vec2_id};
125 int i, j, len;
126
127 if (conic == NULL || filter == NULL)
128 return;
129
130 /** store copy of unfiltered conic **/
131 /* BUGFIX conic0 unused */
132 /* Conic conic0; conic0 = *conic; */
133
134 conic_normalise(conic);
135 scale = MAX(fabs(conic->alpha), fabs(conic->beta));
136
137 /** filter at least 5 points **/
138 len = ddstr_length(start, end);
139 if (len / sample < 4)
140 sample = len / 4;
141
142 for (i = 0; i < iter; ++i)
143 {
144 conic_stat_init(&stats, scale, 100.0);
145 for (dptr = start;;)
146 {
147 DD_GET_POS2(dptr, p);
148 filter(conic, &stats, p, var);
149 if (dptr == end)
150 break;
151 for (j = 0; dptr != end && j < sample; ++j)
152 dptr = dptr->next;
153 }
154 conic_correct(conic, stats.x);
155 }
156 conic_setup(conic);
157
158 DD_GET_POS2(start, p1);
159 DD_GET_POS2(end, p2);
160 DD_GET_POS2(ddstr_mid_point(start, end), p);
161 conic_set_ends(conic, p1, p2, p);
162 conic->props = proplist_add(conic->props,
163 (void *) conic_stat_copy(&stats),
164 STATS,
165 rfree);
166 }
167
168 /**
169 apply chosen filter to point string but limit result to an ellipse
170 **/
171 void conic_ellipse_filter(Conic * conic, List * start, List * end, double min_aratio)
172 {
173 Conic_stat stats = {Conic_stat_id};
174 double scale;
175 List *dptr;
176 Vec2 p = {Vec2_id};
177 Vec2 p1 = {Vec2_id};
178 Vec2 p2 = {Vec2_id};
179 int i, j, len;
180 Conic conic0 = {Conic_id};
181
182 if (conic == NULL || filter == NULL)
183 return;
184
185 /** store copy of unfiltered conic **/
186 conic0 = *conic;
187
188 conic_normalise(conic);
189 scale = MAX(fabs(conic->alpha), fabs(conic->beta));
190
191 /** filter at least 5 points **/
192 len = ddstr_length(start, end);
193 if (len / sample < 4)
194 sample = len / 4;
195
196 for (i = 0; i < iter; ++i)
197 {
198 conic_stat_init(&stats, scale, 100.0);
199 for (dptr = start;;)
200 {
201 DD_GET_POS2(dptr, p);
202 filter(conic, &stats, p, var);
203 if (dptr == end)
204 break;
205 for (j = 0; dptr != end && j < sample; ++j)
206 dptr = dptr->next;
207 }
208 conic_correct(conic, stats.x);
209 }
210 conic_setup(conic);
211
212 if (conic->type == HYPERBOLA || conic_aratio(conic) < min_aratio)
213 {
214 /** have made things worse, use saved copy **/
215 double t1 = conic0.t1;
216 double dt = conic0.t2 - conic0.t1;
217
218 *conic = conic0;
219
220 /** set up 5pt stats without changing conic **/
221 conic_stat_init(&stats, scale, 100.0);
222 filter(conic, &stats, conic_point(conic, t1), var);
223 filter(conic, &stats, conic_point(conic, t1 + 0.25 * dt), var);
224 filter(conic, &stats, conic_point(conic, t1 + 0.50 * dt), var);
225 filter(conic, &stats, conic_point(conic, t1 + 0.75 * dt), var);
226 filter(conic, &stats, conic_point(conic, t1 + 1.00 * dt), var);
227 }
228 DD_GET_POS2(start, p1);
229 DD_GET_POS2(end, p2);
230 DD_GET_POS2(ddstr_mid_point(start, end), p);
231 conic_set_ends(conic, p1, p2, p);
232 conic->props = proplist_add(conic->props,
233 (void *) conic_stat_copy(&stats),
234 STATS,
235 rfree);
236 }
237
238 Conic *conic_fit(List * start, List * end)
239 {
240 Conic *conic = ddstr_conic_5pt(start, end);
241
242 conic_filter(conic, start, end);
243 return (conic);
244 }
245
246 Conic *conic_ellipse_fit(List * start, List * end, double min_aratio)
247 {
248 Conic *conic = ddstr_conic_ellipse_5pt(start, end, min_aratio);
249
250 conic_ellipse_filter(conic, start, end, min_aratio);
251 return (conic);
252 }
253
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.