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
00002
00003 #ifdef DEBUG_1
00004 #define DEBUG_2
00005 #define DEBUG_3
00006 #endif
00007
00008
00009 #include "SUMA_suma.h"
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 int *SUMA_IV_FaceSetsextract (char *IV_filename, int *N_FaceSetList)
00060 {
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
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
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;
00087
00088
00089 sprintf (seq_end[0],"]");
00090 sprintf (seq_end[1],"}");
00091
00092 se_exit = 1;
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;
00107
00108 while (ex != EOF && si < si_exit)
00109 {
00110 ex = fscanf (iv_file,"%s",s);
00111
00112
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;
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;
00131 break;
00132 case 1:
00133 if (nospacing == 1 && si == si_exit-1)
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;
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
00151 se = 0;
00152 nospacing = 0;
00153
00154 while (ex != EOF && se < se_exit )
00155 {
00156 ex = fscanf (iv_file,"%s",s);
00157
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;
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;
00194 break;
00195 case 1:
00196 if (nospacing == 1 && se == 0)
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
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
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;
00263 }
00264
00265 fclose (iv_file);
00266
00267 SUMA_free(linv);
00268
00269 return (FaceSetList);
00270
00271 }
00272
00273 #ifdef TEST
00274 void usage ()
00275
00276 {
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 }
00303
00304 main (int argc,char *argv[])
00305 {
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;
00316 CountOnly = 0;
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 }
00388 #endif