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

Linux Cross Reference
Tina6/tina-libs/tina/vision/visCalib_grid.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/vision/visCalib_grid.c,v $
 27  * Date    :  $Date: 2005/01/09 17:49:25 $
 28  * Version :  $Revision: 1.4 $
 29  * CVS Id  :  $Id: visCalib_grid.c,v 1.4 2005/01/09 17:49:25 paul Exp $
 30  *
 31  * Author  : Legacy TINA
 32  *
 33  * Notes :
 34  *
 35  *********
 36 */
 37 
 38 #include "visCalib_grid.h"
 39 
 40 #if HAVE_CONFIG_H
 41   #include <config.h>
 42 #endif
 43 
 44 #include <stdio.h>
 45 #include <math.h>
 46 #include <float.h>
 47 
 48 #include <tina/sys/sysDef.h>
 49 #include <tina/sys/sysPro.h>
 50 #include <tina/math/mathDef.h>
 51 #include <tina/math/mathPro.h>
 52 #include <tina/geometry/geomDef.h>
 53 #include <tina/geometry/geomPro.h>
 54 
 55 /* FORWARD REFS  */
 56 static void label_grid_lines(List * lines, List ** inter_lines, int num, double grid_thres, double vert_thres, int *label);
 57 
 58 static float invariant[9] =
 59 {(float)0.416, (float)0.400, (float)0.375, (float)0.333, (float)0.250,
 60 (float)0.750, (float)0.666, (float)0.625, (float)0.600};
 61 
 62 void    grid_match(List * world, List * horiz_lines, List * vert_lines, double grid_thres, double vert_thres, void (*store_vertex) ( /* ??? */
 63 ))
 64 {
 65     List   *co_lines;
 66     List   *co_list;
 67     List  **horiz_grid;
 68     List  **vert_grid;
 69     Line2  *line1;
 70     Line2  *line2;
 71     Vec2   *vec2_inter_par_test();
 72     double  min_interx(List * ptr), min_intery(List * ptr);
 73     int    *horiz_label, *vert_label;
 74     int     horiz_num, vert_num;
 75     int     i, j, k, r, c, label;
 76     int     max_rec, max_num, repeat, num_rec;
 77 
 78     for (co_list = horiz_lines, horiz_num = 0; co_list != NULL; co_list = co_list->next)
 79         horiz_num++;
 80     for (co_list = vert_lines, vert_num = 0; co_list != NULL; co_list = co_list->next)
 81         vert_num++;
 82     if (horiz_num < 4 || vert_num < 4)
 83     {
 84         format("grid matcher failed:  insufficient co_linear lines\n");
 85         return;
 86     }
 87     horiz_grid = (List **) ralloc((unsigned) horiz_num * sizeof(List *));
 88     vert_grid = (List **) ralloc((unsigned) vert_num * sizeof(List *));
 89     horiz_label = (int *) ralloc((unsigned) horiz_num * sizeof(int));
 90     vert_label = (int *) ralloc((unsigned) vert_num * sizeof(int));
 91 
 92     for (i = 0; i < horiz_num; i++)
 93         horiz_grid[i] = NULL;
 94     for (i = 0; i < vert_num; i++)
 95         vert_grid[i] = NULL;
 96 
 97     for (co_list = horiz_lines, i = 0, j = 0; co_list != NULL; co_list = co_list->next)
 98     {
 99         co_lines = co_list->to;
100         horiz_label[i] = -1;
101         horiz_grid[i++] = co_lines;
102     }
103 
104     for (co_list = vert_lines, i = 0, j = 0; co_list != NULL; co_list = co_list->next)
105     {
106         co_lines = co_list->to;
107         vert_label[i] = -1;
108         vert_grid[i++] = co_lines;
109     }
110 
111     compute_horiz_invariants(world);
112     for (i = 0, max_rec = 0, max_num = -1; i < horiz_num; i++)
113     {
114         label_grid_lines(horiz_grid[i], vert_grid, vert_num, grid_thres, vert_thres, vert_label);
115         for (j = 0, repeat = 1, num_rec = 0; j < vert_num; j++)
116         {
117             for (k = 0; k < vert_num; k++)
118                 if (j != k && vert_label[j] >= 0 && vert_label[j] == vert_label[k])
119                     repeat = 0;
120             if (vert_label[j] >= 0)
121                 num_rec++;
122         }
123         if (num_rec > max_rec && repeat)
124         {
125             max_rec = num_rec;
126             max_num = i;
127         }
128         if (num_rec == 8 && repeat)
129             break;
130     }
131 
132     if (max_num >= 0 && i != max_num)
133         label_grid_lines(horiz_grid[max_num], vert_grid, vert_num, grid_thres, vert_thres, vert_label);
134     if (max_num < 0)
135     {
136         format("grid matcher failed to label vertical lines\n");
137         rfree((void *) horiz_label);
138         rfree((void *) horiz_grid);
139         rfree((void *) vert_label);
140         rfree((void *) vert_grid);
141         return;
142     }
143     compute_vert_invariants(world);
144     for (i = 0, max_rec = 0, max_num = -1; i < vert_num; i++)
145     {
146         label_grid_lines(vert_grid[i], horiz_grid, horiz_num, grid_thres, vert_thres, horiz_label);
147         for (j = 0, repeat = 1, num_rec = 0; j < horiz_num; j++)
148         {
149             for (k = 0; k < horiz_num; k++)
150                 if (j != k && horiz_label[j] >= 0 && horiz_label[j] == horiz_label[k])
151                     repeat = 0;
152             if (horiz_label[j] >= 0)
153                 num_rec++;
154         }
155         if (num_rec > max_rec && repeat)
156         {
157             max_rec = num_rec;
158             max_num = i;
159         }
160         if (num_rec == 8 && repeat)
161             break;;
162     }
163     if (max_num >= 0 && i != max_num)
164         label_grid_lines(vert_grid[max_num], horiz_grid, horiz_num, grid_thres, vert_thres, horiz_label);
165     if (max_num < 0)
166     {
167         format("grid matcher failed to label horizontal lines\n");
168         rfree((void *) horiz_label);
169         rfree((void *) horiz_grid);
170         rfree((void *) vert_label);
171         rfree((void *) vert_grid);
172         return;
173     }
174     for (i = 0, max_rec = 0, max_num = -1; i < horiz_num; i++)
175     {
176         if ((r = horiz_label[i]) >= 0)
177         {
178             for (j = 0; j < vert_num; j++)
179             {
180                 if ((c = vert_label[j]) >= 0)
181                 {
182                     get_inter_lines(horiz_grid[i], vert_grid[j], vert_thres, &line1, &line2);
183                     if (line1 != NULL && line2 != NULL)
184                     {
185                         label = 8 * (7 - r) + c;
186                         store_vertex(world, line1, line2, label);
187                     }
188                 }
189             }
190         }
191     }
192     rfree((void *) horiz_label);
193     rfree((void *) horiz_grid);
194     rfree((void *) vert_label);
195     rfree((void *) vert_grid);
196 }
197 
198 static void label_grid_lines(List * lines, List ** inter_lines, int num, double grid_thres, double vert_thres, int *label)
199 {
200     int     j, k, n, min_label;
201     double  da=0.0, db=0.0, dc = 0.0, dd = 0.0;
202     Vec2   *inter[64];
203     Line2  *line1;
204     Line2  *line2;
205 
206     for (j = 0; j < num; j++)
207         label[j] = -1;
208     for (j = 0; j < 64; j++)
209         inter[j] = NULL;
210     for (j = 0, n = 0; j < num; j++)
211     {
212         get_inter_lines(lines, inter_lines[j], vert_thres, &line1, &line2);
213         if (line1 != NULL && line2 != NULL && n < 62)
214         {
215             if ((inter[n] = vec2_inter_par_test(line1->p, line1->v, line2->p, line2->v, 0.97)))
216             {
217                 if (n >= 3)
218                 {
219                     da = vec2_mod(vec2_diff(*(inter[n - 3]), *(inter[n - 2])));
220                     db = vec2_mod(vec2_diff(*(inter[n - 1]), *(inter[n])));
221                     dc = vec2_mod(vec2_diff(*(inter[n - 2]), *(inter[n])));
222                     dd = vec2_mod(vec2_diff(*(inter[n - 3]), *(inter[n - 1])));
223                     if (fabs(da * db - invariant[4] * dc * dd) < grid_thres * (da + dc))
224                         break;
225                 }
226                 n++;
227             }
228         }
229     }
230     if ((fabs(da * db - invariant[4] * dc * dd) >= grid_thres * (da + dc))
231         || n < 3)
232     {
233         for (j = 0; j < 64; j++)
234             if (inter[j] != NULL)
235                 vec2_free((void *) inter[j]);
236         return;
237     }
238     for (k = 0; k < num; k++)
239     {
240         get_inter_lines(lines, inter_lines[k], vert_thres, &line1, &line2);
241         if (line1 != NULL && line2 != NULL)
242         {
243             if ((inter[n + 1] = vec2_inter_par_test(line1->p, line1->v, line2->p, line2->v, 0.97)))
244             {
245                 if (vec2_mod(vec2_diff(*(inter[n - 3]), *(inter[n + 1]))) < 0.001)
246                     label[k] = 6;
247                 else if (vec2_mod(vec2_diff(*(inter[n - 2]), *(inter[n + 1]))) < 0.001)
248                     label[k] = 7;
249                 else if (vec2_mod(vec2_diff(*(inter[n - 1]), *(inter[n + 1]))) < 0.001)
250                     label[k] = 8;
251                 else if (vec2_mod(vec2_diff(*(inter[n]), *(inter[n + 1]))) < 0.001)
252                     label[k] = 9;
253                 else
254                 {
255                     da = vec2_mod(vec2_diff(*(inter[n + 1]), *(inter[n - 2])));
256                     db = vec2_mod(vec2_diff(*(inter[n - 1]), *(inter[n])));
257                     dc = vec2_mod(vec2_diff(*(inter[n - 2]), *(inter[n])));
258                     dd = vec2_mod(vec2_diff(*(inter[n + 1]), *(inter[n - 1])));
259                     if (fabs(da * db - invariant[0] * dc * dd) < grid_thres * (da + db))
260                         label[k] = 2;
261                     else if (fabs(da * db - invariant[1] * dc * dd) < grid_thres * (da + db))
262                         label[k] = 3;
263                     else if (fabs(da * db - invariant[2] * dc * dd) < grid_thres * (da + db))
264                         label[k] = 4;
265                     else if (fabs(da * db - invariant[3] * dc * dd) < grid_thres * (da + db))
266                         label[k] = 5;
267                     else if (fabs(da * db - invariant[5] * dc * dd) < grid_thres * (da + db))
268                         label[k] = 10;
269                     else if (fabs(da * db - invariant[6] * dc * dd) < grid_thres * (da + db))
270                         label[k] = 11;
271                     else if (fabs(da * db - invariant[7] * dc * dd) < grid_thres * (da + db))
272                         label[k] = 12;
273                     else if (fabs(da * db - invariant[8] * dc * dd) < grid_thres * (da + db))
274                         label[k] = 13;
275                 }
276             }
277         }
278     }
279     for (k = 0, min_label = 14; k < num; k++)
280     {
281         if (label[k] > 0 && label[k] < min_label)
282             min_label = label[k];
283     }
284     for (k = 0; k < num; k++)
285         if (label[k] > 0)
286             label[k] -= min_label;
287 
288     for (j = 0; j < 64; j++)
289         if (inter[j] != NULL)
290             vec2_free((void *) inter[j]);
291 }
292 
293 void    get_inter_lines(List * horiz, List * vert, double thres, Line2 ** hline, Line2 ** vline)
294 {
295     List   *hptr;
296     List   *vptr;
297 
298     for (hptr = horiz; hptr != NULL; hptr = hptr->next)
299     {
300         for (vptr = vert; vptr != NULL; vptr = vptr->next)
301         {
302             *hline = hptr->to;
303             *vline = vptr->to;
304             if ((*hline)->length > 10.0 && (*vline)->length > 10.0)
305                 if (vec2_mod(vec2_diff((*hline)->p1, (*vline)->p1)) < thres
306                     || vec2_mod(vec2_diff((*hline)->p1, (*vline)->p2)) < thres
307                     || vec2_mod(vec2_diff((*hline)->p2, (*vline)->p1)) < thres
308                     || vec2_mod(vec2_diff((*hline)->p2, (*vline)->p2)) < thres)
309                 {
310                     return;
311                 }
312         }
313     }
314     *hline = NULL;
315     *vline = NULL;
316 }
317 
318 double  min_interx(List * ptr)
319 {
320     Line2  *line;
321     Vec2    xaxis = {Vec2_id};
322     Vec2    origin = {Vec2_id};
323     Vec2   *vec2_inter_par_test();
324     Vec2   *inters;
325     double  inter = DBL_MAX;
326 
327     xaxis.el[0] = (float)1.0;
328     xaxis.el[1] = (float)0.0;
329     origin.el[0] = (float)0.0;
330     origin.el[1] = (float)0.0;
331 
332     line = ptr->to;
333     inters = vec2_inter_par_test(line->p, line->v, origin, xaxis, 0.97);
334     if (inters != NULL)
335     {
336         inter = inters->el[0];
337         rfree((void *) inters);
338     }
339     return (inter);
340 }
341 
342 double  min_intery(List * ptr)
343 {
344     Line2  *line;
345     Vec2    yaxis = {Vec2_id};
346     Vec2    origin = {Vec2_id};
347     Vec2   *inters;
348     Vec2   *vec2_inter_par_test();
349     double  inter = DBL_MAX;
350 
351     yaxis.el[0] = (float)0.0;
352     yaxis.el[1] = (float)1.0;
353     origin.el[0] = (float)0.0;
354     origin.el[1] = (float)0.0;
355 
356     line = ptr->to;
357     inters = vec2_inter_par_test(line->p, line->v, origin, yaxis, 0.97);
358     if (inters != NULL)
359     {
360         inter = inters->el[1];
361         rfree((void *) inters);
362     }
363     return (inter);
364 }
365 
366 void    compute_vert_invariants(List * world)
367 {
368     List   *ptr;
369     int     i;
370     Match  *match_to_3d;
371     Vec3   *vertex[8];
372     float   da, db, dc, dd;
373 
374     for (i = 0; i < 64; i += 8)
375     {
376         for (ptr = world; ptr != NULL; ptr = ptr->next)
377         {
378             match_to_3d = ptr->to;
379             if (match_to_3d->label == i)
380             {
381                 vertex[i / 8] = match_to_3d->to1;
382                 break;
383             }
384         }
385     }
386     for (i = 4; i < 8; i++)
387     {
388         da = (float)vec3_mod(vec3_diff(*(vertex[i]), *(vertex[1])));
389         db = (float)vec3_mod(vec3_diff(*(vertex[2]), *(vertex[3])));
390         dc = (float)vec3_mod(vec3_diff(*(vertex[1]), *(vertex[3])));
391         dd = (float)vec3_mod(vec3_diff(*(vertex[i]), *(vertex[2])));
392         invariant[i + 1] = da * db / (dc * dd);
393     }
394     for (i = 0; i < 5; i++)
395     {
396         da = (float)vec3_mod(vec3_diff(*(vertex[i]), *(vertex[5])));
397         db = (float)vec3_mod(vec3_diff(*(vertex[6]), *(vertex[7])));
398         dc = (float)vec3_mod(vec3_diff(*(vertex[5]), *(vertex[7])));
399         dd = (float)vec3_mod(vec3_diff(*(vertex[i]), *(vertex[6])));
400         invariant[i] = da * db / (dc * dd);
401     }
402 }
403 
404 void    compute_horiz_invariants(List * world)
405 {
406     List   *ptr;
407     int     i;
408     Match  *match_to_3d;
409     Vec3   *vertex[8];
410     float   da, db, dc, dd;
411 
412     for (i = 0; i < 8; i++)
413     {
414         for (ptr = world; ptr != NULL; ptr = ptr->next)
415         {
416             match_to_3d = ptr->to;
417             if (match_to_3d->label == i)
418             {
419                 vertex[i] = match_to_3d->to1;
420                 break;
421             }
422         }
423     }
424     for (i = 4; i < 8; i++)
425     {
426         da = (float)vec3_mod(vec3_diff(*(vertex[i]), *(vertex[1])));
427         db = (float)vec3_mod(vec3_diff(*(vertex[2]), *(vertex[3])));
428         dc = (float)vec3_mod(vec3_diff(*(vertex[1]), *(vertex[3])));
429         dd = (float)vec3_mod(vec3_diff(*(vertex[i]), *(vertex[2])));
430         invariant[i + 1] = da * db / (dc * dd);
431     }
432     for (i = 0; i < 5; i++)
433     {
434         da = (float)vec3_mod(vec3_diff(*(vertex[i]), *(vertex[5])));
435         db = (float)vec3_mod(vec3_diff(*(vertex[6]), *(vertex[7])));
436         dc = (float)vec3_mod(vec3_diff(*(vertex[5]), *(vertex[7])));
437         dd = (float)vec3_mod(vec3_diff(*(vertex[i]), *(vertex[6])));
438         invariant[i] = da * db / (dc * dd);
439     }
440 }
441 

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