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_XYZextract.c

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

Powered by Plone

This site conforms to the following standards: