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

Linux Cross Reference
Tina6/tina-libs/tina/file/fileGeom_ffg_read.c

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

  1 /**********
  2  * 
  3  * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
  4  * University of Manchester, UK.  All rights reserved.
  5  * 
  6  * Redistribution and use in source and binary forms, with or without modification, 
  7  * are permitted provided that the following conditions are met:
  8  * 
  9  *   . Redistributions of source code must retain the above copyright notice, 
 10  *     this list of conditions and the following disclaimer.
 11  *    
 12  *   . Redistributions in binary form must reproduce the above copyright notice,
 13  *     this list of conditions and the following disclaimer in the documentation 
 14  *     and/or other materials provided with the distribution.
 15  * 
 16  *   . Neither the name of the University of Manchester nor the names of its
 17  *     contributors may be used to endorse or promote products derived from this 
 18  *     software without specific prior written permission.
 19  * 
 20  * 
 21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  * POSSIBILITY OF SUCH DAMAGE.
 32  *
 33  **********
 34  * 
 35  * Program :    TINA
 36  * File    :  $Source: /home/tina/cvs/tina-libs/tina/file/fileGeom_ffg_read.c,v $
 37  * Date    :  $Date: 2008/10/02 11:53:05 $
 38  * Version :  $Revision: 1.3 $
 39  * CVS Id  :  $Id: fileGeom_ffg_read.c,v 1.3 2008/10/02 11:53:05 neil Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes   :
 44  * 
 45   *
 46  *********
 47 */
 48 
 49 #include "fileGeom_ffg_read.h"
 50 
 51 #if HAVE_CONFIG_H
 52   #include <config.h>
 53 #endif
 54 
 55 #include <stdio.h>
 56 #include <tina/sys/sysPro.h>
 57 #include <tina/sys/sysDef.h>
 58 #include <tina/math/mathPro.h>
 59 #include <tina/math/mathDef.h>
 60 #include <tina/vision/visDef.h>
 61 #include <tina/vision/visPro.h>
 62 #include <tina/file/file_UtilPro.h>
 63 
 64 #include <tina/geometry/geomGen_free.h>
 65 
 66 #define CLICHE_ID               335
 67 #define CLICHE_MEM_LIST         336
 68 #define MAX_FOCUS_NO            12
 69 
 70 static List       *clicheMem_list, *global_clicheMem_list = NULL;
 71 
 72 static int c_limit = 5;
 73 static int reps=5;
 74 
 75 List   *ffg_read_file(char *pathname, List * list)
 76 {
 77    List   *cliche_list = NULL;
 78    FILE   *stream = fopen_2(pathname, "r");
 79 
 80    if (stream)
 81    {
 82         ffgn_match_cliche_read(stream, list);
 83         (void) fclose_2(stream, pathname);
 84    }
 85    return (cliche_list);
 86 }
 87 
 88 void free_clicheMem_list( List * list )
 89 {
 90 /* pecular free method to remove extra element of list (see below) NAT */
 91     list_rm( list->last, rfree );
 92 }
 93 
 94 void free_globalMem_list ()
 95 {
 96    if ( global_clicheMem_list != NULL )
 97    list_rm( global_clicheMem_list, free_clicheMem_list );
 98    global_clicheMem_list = NULL;
 99 }
100 
101 List   *ffgn_match_cliche_read(FILE * stream, List * list)
102 {
103    List   *cliche_list = NULL;
104    int     label, label2;
105    void   *geom_getbylabel();
106    int     i, ii, count, clength, *icopy;
107    char    geom;   
108    float   l1, l2, l3, l4;
109    float   f1, f2;
110    float   vx, vy, vz;
111    int     line_count = 0;
112 /*
113    int     counter;
114 */
115 
116    free_globalMem_list();
117 
118    if (fscanf(stream, "%d", &count) == EOF)
119    {
120         error("unexpected end of file", non_fatal);
121         return (NULL);
122    }
123 
124    for (i = 0; i < count; ++i)
125    {    
126        clicheMem_list = NULL;
127 /*
128        counter = 0;
129 */
130         
131        if (fscanf(stream, "\n%*c (%f %f %f) %d %*d", &vx, &vy, &vz, &clength ) == EOF)
132        {
133            error("unexpected end of file", non_fatal);
134            return (NULL);
135        }
136 
137        line_count+=2;    
138 
139        reps = c_limit;  // still need to remove reps from all models' cliche files
140 
141        for (ii = 0; ii < clength; ++ii)
142        {
143            Cliche_id *cliche_id = (Cliche_id *)ralloc( sizeof( Cliche_id ) );
144 
145            cliche_id->vx = vx;         
146            cliche_id->vy = vy; 
147            cliche_id->vz = vz; 
148 
149            line_count++;
150 
151            if (fscanf(stream, "\n%c", &geom) == EOF)
152            {
153                error("unexpected end of file", non_fatal);
154                return (NULL);
155            }
156            if (geom == 'S')
157            {
158                if (fscanf(stream, "%d (%f %f)[%*f]{%f %*d}", &label, &l1, &l2, &f1) == EOF)
159                {
160                     error("unexpected end of file", non_fatal);
161                     return (NULL);
162                }
163                cliche_id->type = geom;
164                cliche_id->feat1 = label;
165                cliche_id->feat2 =  -1;
166                cliche_id->latShift1 = l1;
167                cliche_id->latShift2 = l2;
168                cliche_id->latShift3 = -1;
169                cliche_id->latShift4 = -1;
170                if( f1 > 0.0 ) cliche_id->display1 = 1; else cliche_id->display1 = 0;
171                cliche_id->display2 =  0;
172            }
173            else if (geom == 'E')
174            {
175                if (fscanf(stream, "%d (%f %f %f %f)[%*f]{%f %f %*d}", 
176                           &label, &l1, &l2, &l3, &l4, &f1, &f2) == EOF)
177                {
178                     error("unexpected end of file", non_fatal);
179                     return (NULL);
180                }
181                cliche_id->type =geom;
182                cliche_id->feat1 = label;
183                cliche_id->feat2 = 0;
184                cliche_id->latShift1 =l1;
185                cliche_id->latShift2 = l2;
186                cliche_id->latShift3 = l3;
187                cliche_id->latShift4 =l4;
188                if( f1 > 0.0 ) cliche_id->display1 = 1; else cliche_id->display1 = 0;
189                if( f2 > 0.0 ) cliche_id->display2 = 1; else cliche_id->display2 = 0;
190            }
191            else if (geom == 'O')
192            {
193                 if (fscanf(stream, "%d %d (%f %f %f %f)[%*f %*f]{%f %f %*d %*d}", 
194                            &label, &label2, &l1, &l2, &l3, &l4, &f1, &f2) == EOF)
195                {
196                     error("unexpected end of file", non_fatal);
197                     return (NULL);
198                }
199                cliche_id->type = geom;
200                cliche_id->feat1 = label;
201                cliche_id->feat2 = label2;
202                cliche_id->latShift1 = l1;
203                cliche_id->latShift2 = l2;
204                cliche_id->latShift3 = l3;
205                cliche_id->latShift4 = l4;
206                if( f1 > 0.0 ) cliche_id->display1 = 1; else cliche_id->display1 = 0;
207                if( f2 > 0.0 ) cliche_id->display2 = 1; else cliche_id->display2 = 0;
208            }
209            else 
210            { 
211                error("unexpected cliche file format", non_fatal); 
212                printf("cliche file fault detected around line %d\n", line_count ); 
213                return(NULL);
214            }
215 
216            clicheMem_list = ref_addtostart( clicheMem_list, (void *) cliche_id, 0 ); 
217        }
218        global_clicheMem_list = ref_addtostart( global_clicheMem_list, (void *) clicheMem_list  , 0 );
219  
220 /* add peculiar extra element to fromt of list */
221        icopy = (int *)ralloc( sizeof(int) );
222        *icopy = i;
223        clicheMem_list = ref_addtostart( clicheMem_list, (void *) icopy, 0 );
224    }
225    return (list_reverse(cliche_list));
226 }
227 
228 List *cliches_from_ffg(List *model_list, int clength)
229 {
230     int i=0;
231     List *ptr, *ptr2;
232     List *cliche_list = NULL;
233 
234     c_limit = clength;
235 
236     for (ptr = global_clicheMem_list; ptr!=NULL; ptr=ptr->next)
237     {
238         i++;
239         ptr2 = ptr->to;
240         cliche_list = get_x_longest_cliches(cliche_list, clength, model_list, ptr2->last);
241     }
242 
243     return(list_reverse(cliche_list));
244 }
245 
246 List *get_global_cliche_mem_list( void )
247 {
248     return ( global_clicheMem_list );
249 }
250 
251 List *get_x_longest_cliches(List *cliche_list, int clength, List *model_list, List *clicheMem_list)
252 {
253     char     geom, *geom2=NULL, *focus, *feature;
254     int      label, geom_type, geom_num, counter, counter2;
255     List    *ptr, *ptrnext, *group=NULL, *length_list = NULL;
256     Generic *gen;
257     double   length;
258     float    disp1, disp2;
259     List   *pcliche;
260     Cliche_id *icliche;
261     Match_cliche *cliche;
262 
263     for (pcliche = clicheMem_list->next; pcliche!=NULL; pcliche=pcliche->next)
264     {
265         icliche = pcliche->to;
266         geom = icliche->type;
267         length = 0;
268         if (geom == 'S')
269         {
270            label = icliche->feat1;
271            for ( ptr = model_list; ptr != NULL; ptr = ptr->next )
272            {
273              geom2 = (void *) (ptr->to);  
274              geom_type = (int)(ptr->type);
275              geom_num = geom_label_get( geom2, geom_type );
276              if ( geom_num == label ) 
277              {  
278                 length = ((Line3 *)(geom2))->length; 
279                 break;
280              }
281            }
282            gen =  (Generic *)ralloc( sizeof(Generic));
283            gen->type = (int)length;
284            gen->label = label;
285            gen->to = geom2;
286            length_list = ref_addtostart( length_list, (void *) gen, 0 ); 
287         }
288         else if (geom == 'E')
289         {
290            label = icliche->feat1;
291            disp1 = icliche->display1;
292            disp2 = icliche->display2;
293            if ( disp1 != 0.0 || disp2 != 0.0 )
294            {
295                 for ( ptr = model_list; ptr != NULL; ptr = ptr->next )
296                 {
297                   geom2 = (void *) (ptr->to);  
298                   geom_type = (int)(ptr->type);
299                   geom_num = geom_label_get( geom2, geom_type );
300                   if ( geom_num == label ) 
301                   { 
302                       length =  conic_approx_length( ((Conic3 *)(geom2))->conic, 10 ); 
303                       break;
304                   }
305                 }
306                 if ( (disp1 == 0.0 && disp2 != 0) || (disp1 != 0 && disp2 == 0.0) ) length /= 2.0;
307 
308                 gen =  (Generic *)ralloc( sizeof(Generic));
309                 gen->type = (int)length;
310                 gen->label = label;
311                 gen->to = geom2;
312                 length_list = ref_addtostart( length_list, (void *) gen, 0 ); 
313             }
314         }
315     }   
316  
317     length_list = sort_list( length_list, length_sort, NULL );
318     for ( ptr = length_list, counter = 1; ptr != NULL; ptr = ptrnext, counter++ ) 
319     {
320           ptrnext = ptr->next;
321           if ( counter > c_limit ) link_rm( ptr, rfree );
322     }
323 
324     for (ptr = length_list; ptr!=NULL; ptr=ptr->next)
325     {
326          gen = ptr->to;
327          label = gen->label;
328          feature =  geom_getbylabel(model_list, label, &geom_type) ;
329          if (feature!=NULL) group = ref_addtostart(group, feature, geom_type);
330                else format("feature not found %d\n",label);
331          counter++;
332     }
333 
334     for( ptr = length_list, counter2 = 0; counter2 < reps; ptr = ptr->next, counter2++ )
335     {
336        gen = ptr->to;
337        label = gen->label;
338        focus = geom_getbylabel(model_list, label, &geom_type);
339        if (focus == NULL) 
340        {
341             format("problem building cliches for geom %d \n", label);
342             continue;
343        }
344        cliche = match_cliche_make(link_alloc(focus, geom_type), 1, list_copy(group,NULL,NULL), 5);
345        cliche->cliche_tag =  clicheMem_list;
346 
347        if (cliche == NULL)
348        {
349             format("problem building cliches for geom %d \n", label);
350             return (cliche_list);
351        }
352        cliche_list = ref_addtostart(cliche_list, (void *) cliche, 0);
353     }
354     return(cliche_list);
355 }
356 
357 double length_sort( Generic *gen )
358 {
359     return ( -(double)(gen->type) );
360 }
361 

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