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

Linux Cross Reference
Tina4/src/pgh/pgh_model.c

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

  1 /* APA 3/2/94 */
  2 /* Modified by APA and CIB 3/2/94 */
  3 
  4 #include <string.h>
  5 #include <math.h>
  6 #include <stdio.h>
  7 #include <values.h>
  8 #include <sys/param.h>
  9 #include <tina/sys.h>
 10 #include <tina/sysfuncs.h>
 11 #include <tina/math.h>
 12 #include <tina/mathfuncs.h>
 13 #include <tina/vision.h>
 14 #include <tina/visionfuncs.h>
 15 #include <tina/tv.h>
 16 #include <tina/tvfuncs.h>
 17 #include <tina/draw.h>
 18 #include <tina/drawfuncs.h>
 19 #include <tina/toolsfuncs.h>
 20 
 21 #include <tina/file.h>
 22 #include <tina/filefuncs.h>
 23 #include <tina/file_gen.h>
 24 #include <tina/image.h>
 25 #include <pgh/pgh_defs.h>
 26 #include <pgh/pgh_types.h>
 27 #include <pgh/pgh_funcs.h>
 28 
 29 #include <pgh/extra.h>
 30 
 31 /* ---------- Functions ---------- */
 32 
 33 void add_model_poly_set(char *base_name, char *directory_name)
 34 {
 35         char model_set_path[MAXPATHLEN];
 36         char model_dir[MAXPATHLEN];
 37         FILE *ifp;
 38         char buffer[MAXPATHLEN];
 39 
 40         string_append(model_set_path, directory_name, "/", base_name, ".msd", NULL);
 41 
 42         ifp = fopen(model_set_path, "r");
 43 
 44         if (ifp==NULL)
 45         {
 46                 printf("Error: Cannot find '%s'.\n", model_set_path);
 47                 return;
 48         }
 49 
 50 
 51         while(fscanf(ifp, "%s", buffer) != EOF)
 52     {
 53                 if (strcmp(buffer, "SETDIR")==0)
 54                         fscanf(ifp, "%s", model_dir);
 55                 else
 56                 {
 57                         printf("Adding Model: %s\n", buffer);
 58                         add_model_poly(buffer, model_dir);
 59                 }
 60         }
 61 
 62         fclose(ifp);
 63 }
 64 
 65 /* ---------- */
 66 
 67 void add_model_poly(char *base_name, char *dir_name)
 68 {
 69 
 70         Pairs_scale_def   *scale_def;
 71 
 72         scale_def = pairs_scale_def_get();
 73 
 74         if ( (scale_def->scale_min != 1.0) || 
 75                  (scale_def->scale_max != 1.0 ))
 76                 add_model_scaled(base_name, dir_name);
 77         else
 78                 add_model_unscaled(base_name, dir_name);
 79 }
 80 
 81 /* ---------- */
 82 
 83 void add_model_unscaled(char *base_name, char *dir_name)
 84 {
 85 
 86     List              *geom, *ptr;
 87     Line2             *lptr;
 88     Imrect            *im;
 89     Model_poly_header *polyh;
 90     List              *model_poly_list;
 91     List              *model_pairs_list;
 92     Pairs_hist_def    *hist_def;
 93     int                pairs_model_present();
 94 
 95     model_poly_list = pairs_model_poly_list_get();
 96     model_pairs_list = pairs_model_pairs_list_get();
 97     hist_def = pairs_hist_def_get();
 98 
 99         if (pairs_model_present(base_name))
100         {
101         format("Requested model already in database.\n");
102         return;
103     }
104 
105         polyh = pairs_load_model_poly(base_name, dir_name);
106     if (polyh==NULL) return;
107         geom = polyh->model_poly_data;
108 
109 /* Generate a pairwise histogram for each of the new model lines and then
110    add these to the model_pairwise_list after normalizing. */
111 
112     init_pairs_entry(hist_def->pairs_type,
113                      hist_def->dbin_max, 
114                      hist_def->dbin_size, 
115                      hist_def->num_abin,
116                      hist_def->angle_sigma,
117                      hist_def->dist_ramp);
118 
119     for(ptr=geom;ptr!=NULL;ptr=ptr->next)
120     {
121         lptr = ptr->to;
122         if (lptr->length>hist_def->min_length)
123         {
124            im = build_normalized_pairwise(lptr, polyh, geom, hist_def->dbin_max);
125 
126            model_pairs_list =
127              list_addtoend(model_pairs_list, link_alloc(im, IMRECT));
128         }
129     }
130 
131     pairs_model_pairs_list_set(model_pairs_list);
132 }
133 
134 /* ---------- */
135 
136 /* Adds sufficient numbers of model histograms to represent the
137    specified range of scale and precision */
138 
139 void add_model_scaled(char *base_name, char *dir_name)
140 {
141         Pairs_scale_def         *scale_def;
142         double                          scale_min, scale_max;
143         Model_poly_header       *polyh;
144         List                            *geom, *ptr;
145         double                          marker, next_guess, upper_bound, lower_bound, last_cross;
146         double                          dist_to_thres;
147         int                                     count;
148         int                                     mode;
149         Line2                           *lptr;
150         Pairs_hist_def          *hist_def;
151         Imrect                          *marker_hist, *guess_hist;
152         List                            *model_pairs_list;
153         Pgh_serr *serr;
154 
155         scale_def = pairs_scale_def_get();
156         scale_min = scale_def->scale_min;
157         scale_max = scale_def->scale_max;
158         hist_def = pairs_hist_def_get();
159     model_pairs_list = pairs_model_pairs_list_get();
160 
161         if (pairs_model_present(base_name))
162         {
163         format("Requested model already in database.\n");
164         return;
165     }
166 
167     init_pairs_entry(hist_def->pairs_type,
168                      hist_def->dbin_max, 
169                      hist_def->dbin_size, 
170                      hist_def->num_abin,
171                      hist_def->angle_sigma,
172                      hist_def->dist_ramp);
173 
174         polyh = pairs_load_model_poly(base_name, dir_name);
175     if (polyh==NULL) return;
176         geom = polyh->model_poly_data;
177 
178         for(ptr=geom;ptr!=NULL;ptr=ptr->next)
179         {
180                 lptr = ptr->to;
181                 
182                 if (lptr->length>hist_def->min_length)
183                 {
184                 
185                         mode = STORE;
186                         marker = scale_min;
187                         marker_hist = pairs_build_norm_hist_scale(lptr, polyh, geom, hist_def->dbin_max, marker);
188                         upper_bound = scale_max;
189                         lower_bound = scale_min;
190                         count = 0;
191                         last_cross = scale_min;
192 
193                         while(last_cross<scale_max)
194                         {       
195 
196                                 /* Find the value of scale above marker which lies on the theshold */
197                                 next_guess = upper_bound;
198                                 guess_hist = pairs_build_norm_hist_scale(lptr, polyh, geom, hist_def->dbin_max, next_guess);
199 
200                                 dist_to_thres = dot_product(marker_hist, guess_hist, 
201                                                                 hist_ref_get(marker_hist)->type, hist_ref_get(guess_hist)->type)
202                                                                 - scale_def->match_score_thres;
203 
204                                 while(fabs(dist_to_thres) > scale_def->precision)
205                                 {
206                                         if (dist_to_thres>0.0) /* Above threshold */
207                                         {
208                                                 if (next_guess==upper_bound)
209                                                 {
210                                                         upper_bound*=2.0;
211                                                         next_guess = upper_bound;
212                                                 }
213                                                 else
214                                                 {
215                                                         lower_bound = next_guess;
216                                                         next_guess = next_guess+(upper_bound-next_guess)/2.0;
217                                                 }
218                                         }
219                                         else
220                                         {
221                                                 upper_bound = next_guess;
222                                                 next_guess = lower_bound+(next_guess-lower_bound)/2.0;
223                                         }
224 
225                                         im_free(guess_hist);
226                                         guess_hist = pairs_build_norm_hist_scale(lptr, polyh, geom, hist_def->dbin_max, next_guess);
227 
228                                         dist_to_thres = dot_product(marker_hist, guess_hist, 
229                                                                         hist_ref_get(marker_hist)->type, hist_ref_get(guess_hist)->type)
230                                                                         - scale_def->match_score_thres;
231                                 }
232 
233 
234                                 if (mode==STORE)
235                                 {
236                                     /* Add scale error data to the stored hist */
237                                     serr = ralloc(sizeof(Pgh_serr));
238                                     serr->smin = marker;
239                                     serr->smax = next_guess; /* For now at least */
240                                     guess_hist->props = proplist_add(guess_hist->props, serr,
241                                                                        PGH_SERR_TYPE, rfree);
242                                 
243                                         im_free(marker_hist);
244                                         marker = next_guess;
245                                         marker_hist = guess_hist;
246                                         model_pairs_list = ref_addtostart(model_pairs_list, marker_hist, IMRECT);
247 
248                                         count+=1;
249                                         mode = DONT_STORE;
250                                 }
251                                 else
252                                 {
253                                     serr = prop_get(marker_hist->props, PGH_SERR_TYPE);
254                                     serr->smax = next_guess;
255                                     
256                                         marker = next_guess;
257                                         marker_hist = guess_hist;
258 
259                                         mode = STORE;
260                                         last_cross = marker;
261                                 }
262 
263                         }
264 
265                 /*printf("Stored hists = %d\n", count);*/
266                 }
267         }
268 
269     pairs_model_pairs_list_set(model_pairs_list);
270 }
271 
272 /* ---------- */
273 
274 void free_model_list(void)
275 {
276     List *model_poly_list;
277     List *model_pairs_list;
278 
279     model_poly_list = pairs_model_poly_list_get();
280     model_pairs_list = pairs_model_pairs_list_get();
281 
282     list_rm(model_poly_list, polyh_free);
283     model_poly_list = NULL;
284 
285     list_rm(model_pairs_list, im_free);
286     model_pairs_list = NULL;
287 
288     pairs_model_poly_list_set(model_poly_list);
289     pairs_model_pairs_list_set(model_pairs_list);
290 }
291 
292 /* ---------- */
293 
294 Model_poly_header *polyh_alloc(List *geom, char *basename)
295 {
296      Model_poly_header *polyh;
297 
298     polyh = ralloc(sizeof(Model_poly_header));
299     strcpy(polyh->name, basename);
300     polyh->ref = 0; /* this may be used later */
301     polyh->model_poly_data = geom;
302     return(polyh);
303 }
304 
305 /* ---------- */
306 
307 void polyh_free(Model_poly_header *polyh)
308 {
309     if (polyh==NULL) return;
310 
311     if (polyh->model_poly_data!=NULL);
312         reclist_list_free(polyh->model_poly_data, geom_free, LINE2, NULL);
313     rfree(polyh);
314 }
315 
316 /* ---------- */
317 
318 /* descends the model_poly_list to see if the requested model has already 
319    been loaded. */
320 
321 int pairs_model_present(char *name)
322 {
323     Model_poly_header   *polyh;
324         List                            *ptr;
325     List                        *model_poly_list;
326 
327         List                            *pairs_model_poly_list_get();
328 
329     model_poly_list = pairs_model_poly_list_get();
330 
331 
332     strip_spaces(name);
333 
334     for(ptr=model_poly_list;ptr!=NULL;ptr=ptr->next)
335     {
336         polyh = ptr->to;
337         if (strcmp(name,polyh->name) == 0) 
338           return TRUE;
339     }
340         return FALSE;
341 }
342 
343 /* ---------- */
344 
345 /* load the poly data and add this to a Model_poly_header which is added
346    to the model_poly_list */
347 
348 Model_poly_header *pairs_load_model_poly(char *base_name, char *dir_name)
349 {
350     char                                pathname[MAXPATHLEN];
351         List                            *geom, *ptr, *model_poly_list;
352         Line2                           *lptr;
353     Model_poly_header   *polyh;
354 
355         Model_poly_header       *polyh_alloc();
356 
357     model_poly_list = pairs_model_poly_list_get();
358 
359     strip_spaces(base_name);
360     strip_spaces(dir_name);
361     string_append(pathname, dir_name, "/", base_name, NULL);
362     strcat(pathname, ".poly");
363 
364     geom = geom2_read(pathname);
365 
366     if (geom==NULL)   
367         return NULL;
368 
369         /* set the p-field of each line to the lines mid-point */
370     for(ptr=geom; ptr!=NULL;ptr=ptr->next)
371     {
372         lptr = ptr->to;
373         lptr->p = vec2_midpoint(lptr->p1, lptr->p2);
374     }
375  
376     polyh = polyh_alloc(geom,base_name);
377     model_poly_list = 
378        list_addtoend(model_poly_list, link_alloc(polyh, MODEL_POLY_TYPE));
379 
380     pairs_model_poly_list_set(model_poly_list);
381 
382         /* Move the model data axis to the model's centre */
383     centre_poly_axis(geom);
384 
385         return polyh;
386 }
387 
388 /* ---------- */
389 
390 

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