Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

SUMA_IV_FaceSetsextract.c

Go to the documentation of this file.
00001 /*#define TEST*/
00002 /*#define DEBUG_3*/
00003 #ifdef DEBUG_1
00004         #define DEBUG_2
00005         #define DEBUG_3
00006 #endif
00007 
00008 /* Header FILES */
00009 #include "SUMA_suma.h"
00010  
00011 /*!
00012  
00013 File : Taken from IV_FaceSetsextract.c
00014 Author : Ziad Saad
00015 Date : Tue Nov 17 19:02:16 CST 1998
00016  
00017 Purpose : 
00018  
00019         Extracts the FaceSets coordinates of the nodes in an IV file so that we can manipulate the 
00020         data to our liking. The program looks for a sequence of characters that seem to indicate
00021         the start of the FaceSets list. Once the sequence is found, the series of triplets is read and
00022         written out either to a file or to the screen. The sequence of triplets must be terminated 
00023         by an ending sequence.
00024         The starting and ending sequences are hard coded into the program.
00025         
00026         The program outputs an error message if :
00027         If the starting or ending sequences are not found
00028         If the number of points read is not a multiple of three
00029         If the first number of the first FaceSets triplet and the last character in the starting sequence 
00030                 are not spearated by a space (or tab). You can fix this by manually adding a space.
00031          
00032         
00033  
00034 Input paramters : 
00035  
00036         IV_filename (char *) a string specifying the name of the inventor file
00037         N_FaceSetList (int *) will give the number of rows in FaceSetList
00038  
00039 Usage : 
00040                 FaceSetList = SUMA_IV_FaceSetsextract (char *IV_filename, int *N_FaceSetList)
00041  
00042  
00043 Returns : 
00044  
00045         FaceSetList (int *) the facesetlist in the inventor file, an  Mx3 integer vector (used to be a matrix before SUMA 1.2)
00046                 
00047  
00048 Support : 
00049  
00050  
00051  
00052 Side effects : 
00053  
00054  
00055  
00056 ***/
00057  
00058 
00059 int *SUMA_IV_FaceSetsextract (char *IV_filename, int *N_FaceSetList)
00060 {/* SUMA_IV_FaceSetsextract */
00061         char s[500],serr[500];
00062         char seq_strt[5][30], seq_end[5][30];
00063         int i, f, ex, si, si_exit, evl, se, se_exit;
00064         int ip, NP, cnt, nospacing, MaxAlloc = 100, *linv, *FaceSetList;
00065         div_t cnt4;
00066         FILE*iv_file;
00067    static char FuncName[]={"SUMA_IV_FaceSetsextract"};
00068    
00069 
00070         /* intialize the number of points read to 0 */
00071         *N_FaceSetList = 0;
00072         
00073         linv = (int *)SUMA_malloc (MaxAlloc*sizeof(int));
00074         if (!linv)
00075                 {
00076                                 SUMA_alloc_problem ("Allocation error in IV-FaceSetExtract");
00077                                 return (NULL);
00078                 }
00079                 
00080         /*This is the sequence to trigger reading the numbers*/
00081         sprintf (seq_strt[0],"IndexedFaceSet");
00082         sprintf (seq_strt[1],"{");
00083         sprintf (seq_strt[2],"coordIndex");     
00084         sprintf (seq_strt[3],"[");
00085 
00086         si_exit = 4; /* set equal to the number of strings to be matched */
00087 
00088         /*This is a sequence to mark the end of the number list*/
00089         sprintf (seq_end[0],"]");
00090         sprintf (seq_end[1],"}");  /* you do not need that one, it is possible to have other objects following coordIndex */
00091 
00092         se_exit = 1; /* set equal to the number of strings to be matched */
00093 
00094         iv_file = fopen (IV_filename,"r");
00095         if (iv_file == NULL)
00096                 {
00097                         SUMA_error_message ("SUMA_IV_FaceSetsextract","Could not open input file ",1);
00098                         return (NULL);
00099                 }
00100 
00101 
00102         si = 0;
00103         ex = 1;
00104         cnt = 0;
00105 
00106         nospacing = 0; /* this flag is used to when the last number is read and it has the first seq_end*/
00107                                                         /* character attached to it, ie no space ! in between*/
00108         while (ex != EOF && si < si_exit)
00109         {
00110                 ex = fscanf (iv_file,"%s",s);
00111 
00112                 /*evl = equal_strings (s,seq_strt[si]);*/
00113 
00114                 if (strlen (seq_strt[si]) >= strlen (s)) 
00115                         {
00116                                 evl = SUMA_iswordin (seq_strt[si],s);
00117                                 if (evl == 1)
00118                                         nospacing = 0; /* There is a space between character in starting sequence and first number*/ 
00119                         }
00120                 else
00121                         {
00122                                 evl = SUMA_iswordin (s,seq_strt[si]);
00123                                 if (evl == 1)
00124                                                 nospacing = 1;
00125                         }
00126 
00127                 switch (evl)
00128                         {
00129                                 case 0:
00130                                         si = 0;         /* No match, reset the sequence counter */
00131                                         break;
00132                                 case 1:
00133                                         if (nospacing == 1 && si == si_exit-1) /* it has to be the last character in the sequence*/
00134                                                 {
00135                                                         sprintf (serr,"Must have a space character between first number and last character in start sequence ( %s )",s);
00136                                                         SUMA_error_message ("SUMA_IV_FaceSetsextract",serr,1);
00137                                                         exit (1);
00138 
00139                                                 }
00140                                         si = si +1;     /* increment the start sequence counter */
00141                                         #ifdef DEBUG_3
00142                                                 printf ("found %s  ---  si = %d\n",s,si);
00143                                         #endif
00144                                         break;
00145                                 default:
00146                                         break;
00147                         }
00148         }
00149 
00150         /* Now, read the series of numbers until you encounter the first string of the ending sequence*/
00151         se = 0;
00152         nospacing = 0; 
00153 
00154         while (ex != EOF && se < se_exit )
00155                 {
00156                         ex = fscanf (iv_file,"%s",s);
00157                         /*evl = equal_strings (s,seq_end[se]);*/
00158 
00159                         if (strlen (seq_end[se]) >= strlen (s)) 
00160                                 {
00161                                         evl = SUMA_iswordin (seq_end[se],s);
00162                                         if (evl == 1)
00163                                                 nospacing = 0; /* There is a space between last number and fisrt character in ending sequence */ 
00164                                 }
00165                         else
00166                                 { 
00167                                         evl = SUMA_iswordin (s,seq_end[se]);
00168                                         if (evl == 1)
00169                                                 nospacing = 1;
00170                                 }
00171 
00172                         switch (evl)
00173                                 {
00174                                         case 0:
00175                                                 f = atoi (s);
00176                                                 linv[cnt] = f;
00177                                                         #ifdef DEBUG_3
00178                                                                 printf ("\nNumber (%d): %d is appended to end sequence !\n",cnt,linv[cnt]);     
00179                                                         #endif
00180                 
00181                                                 ++cnt;
00182                                                 if (cnt >= MaxAlloc - 1)
00183                                                         {
00184                                                                 MaxAlloc = MaxAlloc + 100;
00185                                                                 linv = (int *)SUMA_realloc ((void*) linv, MaxAlloc * sizeof(int));
00186                                                                 if (!linv)
00187                                                                         {
00188                                                                                 SUMA_alloc_problem ("Re-Allocation error in IV-FaceSetExtract");
00189                                                                                 return (NULL);
00190                                                                         }
00191                                                                 
00192                                                         }  
00193                                                 se = 0;  /* no match for ending sequence, reset the counter */
00194                                                 break;
00195                                         case 1:         /* found the first character in the end sequence */
00196                                                 if (nospacing == 1 && se == 0) /* it has to be the first character in the sequence*/
00197                                                 {
00198                                                         f = atoi (s);
00199                                                         linv[cnt] = f;
00200                                                         
00201                                                         ++cnt;
00202                                                         #ifdef DEBUG_3
00203                                                                 printf ("\nLast number (%d): %f is appended to end sequence !\n",cnt,f);        
00204                                                         #endif
00205                                                 }
00206                                                 #ifdef DEBUG_3
00207                                                         printf ("\nfound %s  ---  se = %d\n",s,se);
00208                                                 #endif
00209                                                 se = se + 1;
00210                                                 break;
00211                                         default:
00212                                                 break;
00213                                 }
00214 
00215                 }
00216         if (si < si_exit)
00217                 {
00218                         SUMA_error_message ("IV_FaceSetextract","Could not find specified starting sequence",0);
00219                         for (i=0;i<si_exit;++i)
00220                                         printf ("%s \t",seq_strt[i]);
00221                 }
00222         else
00223                 { 
00224                         if (se < se_exit)
00225                                 {
00226                                         SUMA_error_message ("IV_FaceSetextract","Could not find specified ending sequence",0);
00227                                         for (i=0;i<se_exit;++i)
00228                                                         printf ("%s \t",seq_end[i]);
00229                                 }
00230                         else
00231                                 {
00232                                         /* check that the number of points read is multiple of 4 */
00233                                         cnt4 = div (cnt,4);
00234                                         if (cnt4.rem != 0)
00235                                                 {
00236                                                         SUMA_error_message ("IV_FaceSetextract","number of points read is not multiple of 4 !",0);
00237                                                         #ifdef DEBUG_3
00238                                                                 printf ("%f FaceSets sets read !!!\n",(float)cnt/4);
00239                                                         #endif
00240                                                 }
00241                                         
00242                                 }
00243                 }
00244 
00245         *N_FaceSetList = cnt4.quot ;
00246         
00247         /* Now allocate space for SUMA_IV_FaceSetsextract and fill it up */
00248         NP = 3;
00249         FaceSetList = (int *) SUMA_calloc (*N_FaceSetList * NP, sizeof(int));
00250         if (!FaceSetList)
00251                 {
00252                         SUMA_alloc_problem("IV_FaceSetextract : Could not allocate");
00253                         return(NULL);
00254                 }
00255         
00256         i = 0;
00257         ip = 0;
00258         while (i < cnt) {
00259                 FaceSetList[ip] = linv[i]; ++ip; ++i;
00260                 FaceSetList[ip] = linv[i]; ++ip; ++i;
00261                 FaceSetList[ip] = linv[i]; ++ip; ++i;
00262                 ++i; /* skip the 4th value */
00263         }
00264         
00265         fclose (iv_file);
00266         
00267         SUMA_free(linv);
00268         
00269         return (FaceSetList);
00270 
00271 }/* SUMA_IV_FaceSetsextract */
00272 
00273 #ifdef TEST 
00274 void usage ()
00275  
00276   {/*Usage*/
00277                 printf ("\nUsage:  SUMA_IV_FaceSetsextract <IV_filename> [-o output filename] [-c]\n\n");
00278                 printf ("\t     Extracts the FaceSets coordinates of the nodes in an IV file so that we can manipulate the \n");
00279                 printf ("\t data to our liking. The program looks for a sequence of characters that seem to indicate\n");
00280                 printf ("\t the start of the FaceSets list. Once the sequence is found, the series of triplets is read and\n");
00281                 printf ("\t written out either to a file or to the screen. The sequence of triplets must be terminated \n");
00282                 printf ("\t by an ending sequence.\n");
00283                 printf ("\t The starting and ending sequences are hard coded into the program.\n");
00284                 printf ("\t \n");
00285                 printf ("\t The program outputs an error message if :\n");
00286                 printf ("\t If the starting or ending sequences are not found\n");
00287                 printf ("\t If the number of points read is not a multiple of three\n");
00288                 printf ("\t If the first number of the first FaceSets triplet and the last character in the starting sequence \n");
00289                 printf ("\t     are not spearated by a space (or tab). You can fix this by manually adding a space.\n");
00290                 printf ("\t \n");
00291                 printf ("\t IV_filename : Filename of the ascii inventor file \n");
00292                 printf ("\t [-o output-filename] : output file name containing the : NodeNumber X Y Z  \n");
00293                 printf ("\t                   of each node in the iv file. This parameter is optional\n");
00294                 printf ("\t                   if no filename is specified, the output goes to the screen\n");
00295                 printf ("\t [-co] : Count the number of nodes, do not output results\n");
00296                 printf ("\t The format of the output on each line is tab delimited:\n");
00297                 printf ("\t NodeNumber  X       Y       Z\n\n");
00298                 printf ("\t The older version of this code is called IV-FaceSetsextract\n");
00299                 printf ("\t  it is left around for backward compatibilty\n\n");
00300                 printf ("\t\t\t Ziad Saad \t Tue Nov 17 19:00:42 CST 1998\n");
00301                 exit (0);
00302   }/*Usage*/
00303  
00304 main (int argc,char *argv[])
00305 {/* Main */
00306 char outfile[300];
00307 int N_FaceSetList, *FaceSetList ,writeout,CountOnly,paramnum;
00308  
00309 if (argc < 2)
00310     {
00311       usage ();
00312       exit (1);
00313      }
00314 
00315 writeout = 0;                   /* check out second option */
00316 CountOnly = 0;                  /* Set to 1 if you want to Count the number of triplets only */
00317 paramnum = 2;
00318 while (paramnum < argc)
00319         {
00320                 if (equal_strings (argv[paramnum],"-o") == 1)
00321                         {
00322                                 if ((paramnum + 1) >= argc) 
00323                                         {
00324                                                 SUMA_error_message ("SUMA_IV_FaceSetsextract","No Output file name specified with -o option",1);
00325                                                 exit (1);
00326                                         }
00327                                 if (filexists (argv[paramnum+1]) == 1)
00328                                         {
00329                                                 SUMA_error_message ("SUMA_IV_FaceSetsextract","Output file exists, will not overwrite",1);
00330                                                 exit (1);
00331                                         }
00332                                 else
00333                                         {
00334                                                 sprintf(outfile,"%s",argv[paramnum+1]);
00335                                                 writeout = 1;
00336                                         }
00337                                 ++ paramnum;
00338                         }
00339                 if (equal_strings (argv[paramnum],"-co") == 1)
00340                         {
00341                                 CountOnly = 1;
00342                         }
00343         ++ paramnum;
00344         }
00345 
00346 FaceSetList = SUMA_IV_FaceSetsextract (argv[1], &N_FaceSetList);
00347 
00348 if (CountOnly)
00349         printf ("%d FaceSets sets read\n",N_FaceSetList);
00350         
00351 if (writeout == 1) {
00352                 FILE *outfid; 
00353         
00354         outfid = fopen (outfile, "w");
00355         if (outfid == NULL) {
00356                 fprintf (SUMA_STDERR, "Error %s: Could not open %s for writing.\n", FuncName, outfile);
00357         }else {
00358                 i = 0;
00359                 cntlim = N_FaceSetList*3;
00360                 while (i < cntlim) {
00361                         j = 0;
00362                         while (j < 3) {
00363                                 fprintf (outfid, "%d\t", FaceSetList[i]);
00364                                 ++i;
00365                                 ++j;
00366                         }
00367                         fprintf (outfid, "\n");
00368                 }
00369                 fclose (outfid);
00370         }
00371 } else if (!CountOnly) {
00372         i = 0;
00373         cntlim = N_FaceSetList*3;
00374         while (i < cntlim) {
00375                 j = 0;
00376                 while (j < 3) {
00377                         fprintf (SUMA_STDOUT, "%d\t", FaceSetList[i]);
00378                         ++i;
00379                         ++j;
00380                 }
00381                 fprintf (SUMA_STDOUT, "\n");
00382         }
00383 }
00384         
00385 SUMA_free(FaceSetList);
00386         
00387 }/* Main */
00388 #endif
 

Powered by Plone

This site conforms to the following standards: