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

Linux Cross Reference
Tina5/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, counter;
112 
113    free_globalMem_list();
114 
115    if (fscanf(stream, "%d", &count) == EOF)
116    {
117         error("unexpected end of file", non_fatal);
118         return (NULL);
119    }
120 
121    for (i = 0; i < count; ++i)
122    {    
123        clicheMem_list = NULL;
124        counter = 0;
125         
126        if (fscanf(stream, "\n%*c (%f %f %f) %d %*d", &vx, &vy, &vz, &clength ) == EOF)
127        {
128            error("unexpected end of file", non_fatal);
129            return (NULL);
130        }
131 
132        line_count+=2;    
133 
134        reps = c_limit;  // still need to remove reps from all models' cliche files
135 
136        for (ii = 0; ii < clength; ++ii)
137        {
138            Cliche_id *cliche_id = (Cliche_id *)ralloc( sizeof( Cliche_id ) );
139 
140            cliche_id->vx = vx;         
141            cliche_id->vy = vy; 
142            cliche_id->vz = vz; 
143 
144            line_count++;
145 
146            if (fscanf(stream, "\n%c", &geom) == EOF)
147            {
148                error("unexpected end of file", non_fatal);
149                return (NULL);
150            }
151            if (geom == 'S')
152            {
153                if (fscanf(stream, "%d (%f %f)[%*f]{%f %*d}", &label, &l1, &l2, &f1) == EOF)
154                {
155                     error("unexpected end of file", non_fatal);
156                     return (NULL);
157                }
158                cliche_id->type = geom;
159                cliche_id->feat1 = label;
160                cliche_id->feat2 =  -1;
161                cliche_id->latShift1 = l1;
162                cliche_id->latShift2 = l2;
163                cliche_id->latShift3 = -1;
164                cliche_id->latShift4 = -1;
165                if( f1 > 0.0 ) cliche_id->display1 = 1; else cliche_id->display1 = 0;
166                cliche_id->display2 =  0;
167            }
168            else if (geom == 'E')
169            {
170                if (fscanf(stream, "%d (%f %f %f %f)[%*f]{%f %f %*d}", 
171                           &label, &l1, &l2, &l3, &l4, &f1, &f2) == EOF)
172                {
173                     error("unexpected end of file", non_fatal);
174                     return (NULL);
175                }
176                cliche_id->type =geom;
177                cliche_id->feat1 = label;
178                cliche_id->feat2 = 0;
179                cliche_id->latShift1 =l1;
180                cliche_id->latShift2 = l2;
181                cliche_id->latShift3 = l3;
182                cliche_id->latShift4 =l4;
183                if( f1 > 0.0 ) cliche_id->display1 = 1; else cliche_id->display1 = 0;
184                if( f2 > 0.0 ) cliche_id->display2 = 1; else cliche_id->display2 = 0;
185            }
186            else if (geom == 'O')
187            {
188                 if (fscanf(stream, "%d %d (%f %f %f %f)[%*f %*f]{%f %f %*d %*d}", 
189                            &label, &label2, &l1, &l2, &l3, &l4, &f1, &f2) == EOF)
190                {
191                     error("unexpected end of file", non_fatal);
192                     return (NULL);
193                }
194                cliche_id->type = geom;
195                cliche_id->feat1 = label;
196                cliche_id->feat2 = label2;
197                cliche_id->latShift1 = l1;
198                cliche_id->latShift2 = l2;
199                cliche_id->latShift3 = l3;
200                cliche_id->latShift4 = l4;
201                if( f1 > 0.0 ) cliche_id->display1 = 1; else cliche_id->display1 = 0;
202                if( f2 > 0.0 ) cliche_id->display2 = 1; else cliche_id->display2 = 0;
203            }
204            else 
205            { 
206                error("unexpected cliche file format", non_fatal); 
207                printf("cliche file fault detected around line %d\n", line_count ); 
208                return(NULL);
209            }
210 
211            clicheMem_list = ref_addtostart( clicheMem_list, (void *) cliche_id, 0 ); 
212        }
213        global_clicheMem_list = ref_addtostart( global_clicheMem_list, (void *) clicheMem_list  , 0 );
214  
215 /* add peculiar extra element to fromt of list */
216        icopy = (int *)ralloc( sizeof(int) );
217        *icopy = i;
218        clicheMem_list = ref_addtostart( clicheMem_list, (void *) icopy, 0 );
219    }
220    return (list_reverse(cliche_list));
221 }
222 
223 List *cliches_from_ffg(List *model_list, int clength)
224 {
225     int i=0;
226     List *ptr, *ptr2;
227     List *cliche_list = NULL;
228 
229     c_limit = clength;
230 
231     for (ptr = global_clicheMem_list; ptr!=NULL; ptr=ptr->next)
232     {
233         i++;
234         ptr2 = ptr->to;
235         cliche_list = get_x_longest_cliches(cliche_list, clength, model_list, ptr2->last);
236     }
237 
238     return(list_reverse(cliche_list));
239 }
240 
241 List *get_global_cliche_mem_list( void )
242 {
243     return ( global_clicheMem_list );
244 }
245 
246 List *get_x_longest_cliches(List *cliche_list, int clength, List *model_list, List *clicheMem_list)
247 {
248     char     geom, *geom2=NULL, *focus, *feature;
249     int      label, geom_type, geom_num, counter, counter2;
250     List    *ptr, *group=NULL, *length_list = NULL;
251     Generic *gen;
252     double   length;
253     float    disp1, disp2;
254     List   *pcliche;
255     Cliche_id *icliche;
256     Match_cliche *cliche;
257 
258     for (pcliche = clicheMem_list->next; pcliche!=NULL; pcliche=pcliche->next)
259     {
260         icliche = pcliche->to;
261         geom = icliche->type;
262         length = 0;
263         if (geom == 'S')
264         {
265            label = icliche->feat1;
266            for ( ptr = model_list; ptr != NULL; ptr = ptr->next )
267            {
268              geom2 = (void *) (ptr->to);  
269              geom_type = (int)(ptr->type);
270              geom_num = geom_label_get( geom2, geom_type );
271              if ( geom_num == label ) 
272              {  
273                 length = ((Line3 *)(geom2))->length; 
274                 break;
275              }
276            }
277            gen =  (Generic *)ralloc( sizeof(Generic));
278            gen->type = (int)length;
279            gen->label = label;
280            gen->to = geom2;
281            length_list = ref_addtostart( length_list, (void *) gen, 0 ); 
282         }
283         else if (geom == 'E')
284         {
285            label = icliche->feat1;
286            disp1 = icliche->display1;
287            disp2 = icliche->display2;
288            if ( disp1 != 0.0 || disp2 != 0.0 )
289            {
290                 for ( ptr = model_list; ptr != NULL; ptr = ptr->next )
291                 {
292                   geom2 = (void *) (ptr->to);  
293                   geom_type = (int)(ptr->type);
294                   geom_num = geom_label_get( geom2, geom_type );
295                   if ( geom_num == label ) 
296                   { 
297                       length =  conic_approx_length( ((Conic3 *)(geom2))->conic, 10 ); 
298                       break;
299                   }
300                 }
301                 if ( (disp1 == 0.0 && disp2 != 0) || (disp1 != 0 && disp2 == 0.0) ) length /= 2.0;
302 
303                 gen =  (Generic *)ralloc( sizeof(Generic));
304                 gen->type = (int)length;
305                 gen->label = label;
306                 gen->to = geom2;
307                 length_list = ref_addtostart( length_list, (void *) gen, 0 ); 
308             }
309         }
310     }   
311  
312     length_list = sort_list( length_list, length_sort, NULL );
313     for ( ptr = length_list, counter = 1; ptr != NULL; ptr = ptr->next, counter++ ) 
314           if ( counter > c_limit ) link_rm( ptr, rfree );
315 
316     for (ptr = length_list; ptr!=NULL; ptr=ptr->next)
317     {
318          gen = ptr->to;
319          label = gen->label;
320          feature =  geom_getbylabel(model_list, label, &geom_type) ;
321          if (feature!=NULL) group = ref_addtostart(group, feature, geom_type);
322                else format("feature not found %d\n",label);
323          counter++;
324     }
325 
326     for( ptr = length_list, counter2 = 0; counter2 < reps; ptr = ptr->next, counter2++ )
327     {
328        gen = ptr->to;
329        label = gen->label;
330        focus = geom_getbylabel(model_list, label, &geom_type);
331        if (focus == NULL) 
332        {
333             format("problem building cliches for geom %d \n", label);
334             continue;
335        }
336        cliche = match_cliche_make(link_alloc(focus, geom_type), 1, list_copy(group,NULL,NULL), 5);
337        cliche->cliche_tag =  clicheMem_list;
338 
339        if (cliche == NULL)
340        {
341             format("problem building cliches for geom %d \n", label);
342             return (cliche_list);
343        }
344        cliche_list = ref_addtostart(cliche_list, (void *) cliche, 0);
345     }
346     return(cliche_list);
347 }
348 
349 double length_sort( Generic *gen )
350 {
351     return ( -(double)(gen->type) );
352 }
353 

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