Example #1
0
int main (int argc,char *argv[])
{/* Main */    
   static char FuncName[]={"quickspec"};
   int detail, kar, i, j, N_surf, N_name, idefstate;
   FILE *fid = NULL;
   char *spec_name, stmp[500], *Unique_st;
   SUMA_SO_File_Type TypeC[SUMA_MAX_N_SURFACE_SPEC];
   static char  
         *State[SUMA_MAX_N_SURFACE_SPEC],
         *Name_coord[SUMA_MAX_N_SURFACE_SPEC],
         *Name_topo[SUMA_MAX_N_SURFACE_SPEC],
         Anat[SUMA_MAX_N_SURFACE_SPEC],
         *LDP[SUMA_MAX_N_SURFACE_SPEC],
         *MARK[SUMA_MAX_N_SURFACE_SPEC],
         *LABEL[SUMA_MAX_N_SURFACE_SPEC];
   SUMA_GENERIC_ARGV_PARSE *ps;
   SUMA_Boolean brk;
   
   SUMA_mainENTRY;
   
	/* allocate space for CommonFields structure */
	SUMAg_CF = SUMA_Create_CommonFields ();
	if (SUMAg_CF == NULL) {
		fprintf( SUMA_STDERR,
               "Error %s: Failed in SUMA_Create_CommonFields\n", FuncName);
		exit(1);
	}
   
   ps = SUMA_Parse_IO_Args(argc, argv, "-t;");
   
   if (argc < 3)
       {
          usage_SUMA_quickspec (ps);
          exit (1);
       }
   
   kar = 1;
	brk = NOPE;
   detail = 1;
   N_surf = 0;
   N_name = 0;
   spec_name = NULL;
	while (kar < argc) { /* loop accross command ine options */
		/*fprintf(stdout, "%s verbose: Parsing command line...\n", FuncName);*/
		if (strcmp(argv[kar], "-h") == 0 || strcmp(argv[kar], "-help") == 0) {
			 usage_SUMA_quickspec(ps);
          exit (1);
		}
		if (!brk && (strcmp(argv[kar], "-spec") == 0)) {
         kar ++;
			if (kar >= argc)  {
		  		fprintf (SUMA_STDERR, "need argument after -spec \n");
				exit (1);
			}
         spec_name = argv[kar];
			if (!THD_ok_overwrite() && SUMA_filexists(spec_name)) {
            fprintf (SUMA_STDERR, 
               "File %s exists, choose another one.\n", spec_name);
            exit(1);
         }
			brk = YUP;
		}
      if (!brk && (strcmp(argv[kar], "-tn") == 0)) {
         if (N_surf >= SUMA_MAX_N_SURFACE_SPEC) {
            SUMA_SL_Err("Exceeding maximum number of allowed surfaces...");
            exit(1);   
         }
         /* get the type */
         kar ++;
			if (kar >= argc)  {
		  		fprintf (SUMA_STDERR, "Type argument must follow -tn \n");
				exit (1);
			}
         TypeC[N_surf] = SUMA_SurfaceTypeCode(argv[kar]);
         if (TypeC[N_surf] == SUMA_FT_ERROR || 
             TypeC[N_surf] == SUMA_FT_NOT_SPECIFIED) {
            fprintf (SUMA_STDERR, "%s is a bad file type.\n", argv[kar]);
            exit(1);
         }
         /* get the name */
         if (TypeC[N_surf] == SUMA_SUREFIT || TypeC[N_surf] == SUMA_VEC) 
            N_name = 2;
         else N_name = 1;
         if (kar+N_name >= argc)  {
		  		fprintf (SUMA_STDERR, "need %d elements for NAME \n", N_name);
				exit (1);
			}
         kar ++; Name_coord[N_surf] = argv[kar];
         if (N_name == 2) {
            kar ++; Name_topo[N_surf] = argv[kar];
         } else { 
            Name_topo[N_surf] = NULL;
         }
         State[N_surf] = NULL;
         Anat[N_surf] = 'Y';
         LDP[N_surf] = NULL;
         ++N_surf; 
			brk = YUP;
		}
      if (!brk && (strcmp(argv[kar], "-tsn") == 0)) {
         if (N_surf >= SUMA_MAX_N_SURFACE_SPEC) {
            SUMA_SL_Err("Exceeding maximum number of allowed surfaces...");
            exit(1);   
         }
         /* get the type */
         kar ++;
			if (kar >= argc)  {
		  		fprintf (SUMA_STDERR, "TYPE argument must follow -tsn \n");
				exit (1);
			}
         TypeC[N_surf] = SUMA_SurfaceTypeCode(argv[kar]);
         if (  TypeC[N_surf] == SUMA_FT_ERROR || 
               TypeC[N_surf] == SUMA_FT_NOT_SPECIFIED) {
            fprintf (SUMA_STDERR, "%s is a bad file TYPE.\n", argv[kar]);
            exit(1);
         }
         /* get the state */
         kar ++;
			if (kar >= argc)  {
		  		fprintf (SUMA_STDERR, 
                     "STATE argument must follow TYPE with -tsn \n");
				exit (1);
			}
         State[N_surf] = argv[kar];
         
         /* get the name */
         if (  TypeC[N_surf] == SUMA_SUREFIT || 
               TypeC[N_surf] == SUMA_VEC) N_name = 2;
         else N_name = 1;
         if (kar+N_name >= argc)  {
		  		fprintf (SUMA_STDERR, "need %d elements for NAME \n", N_name);
				exit (1);
			}
         kar ++; Name_coord[N_surf] = argv[kar];
         if (N_name == 2) {
            kar ++; Name_topo[N_surf] = argv[kar];
         } else { 
            Name_topo[N_surf] = NULL;
         }
         
         Anat[N_surf] = 'Y';
         LDP[N_surf] = NULL;
         ++N_surf; 
			brk = YUP;
		}
      
      if (!brk && (strcmp(argv[kar], "-tsnad") == 0)) {
         if (N_surf >= SUMA_MAX_N_SURFACE_SPEC) {
            SUMA_SL_Err("Exceeding maximum number of allowed surfaces...");
            exit(1);   
         }
         /* get the type */
         kar ++;
			if (kar >= argc)  {
		  		fprintf (SUMA_STDERR, "TYPE argument must follow -tsnad \n");
				exit (1);
			}
         TypeC[N_surf] = SUMA_SurfaceTypeCode(argv[kar]);
         if (  TypeC[N_surf] == SUMA_FT_ERROR || 
               TypeC[N_surf] == SUMA_FT_NOT_SPECIFIED) {
            fprintf (SUMA_STDERR, "%s is a bad file TYPE.\n", argv[kar]);
            exit(1);
         }
         /* get the state */
         kar ++;
			if (kar >= argc)  {
		  		fprintf (SUMA_STDERR, 
                     "STATE argument must follow TYPE with -tsnad \n");
				exit (1);
			}
         State[N_surf] = argv[kar];
         
         /* get the name */
         if (  TypeC[N_surf] == SUMA_SUREFIT || 
               TypeC[N_surf] == SUMA_VEC) N_name = 2;
         else N_name = 1;
         if (kar+N_name >= argc)  {
		  		fprintf (SUMA_STDERR, "need %d elements for NAME \n", N_name);
				exit (1);
			}
         kar ++; Name_coord[N_surf] = argv[kar];
         if (N_name == 2) {
            kar ++; Name_topo[N_surf] = argv[kar];
         } else { 
            Name_topo[N_surf] = NULL;
         }
         
         
         /* get the anatomical flag */
         kar ++;
			if (kar >= argc)  {
		  		fprintf (SUMA_STDERR, 
                     "Anatomical flag must follow NAME with -tsnad \n");
				exit (1);
			}
         Anat[N_surf] = SUMA_TO_UPPER_C(argv[kar][0]);
         if (Anat[N_surf] != 'Y' && Anat[N_surf] != 'N') {
            SUMA_S_Err("Anatomical flag must be either 'y' or 'n'");
            exit (1);
         }
         /* get the LDP */
         kar ++;
			if (kar >= argc)  {
		  		fprintf (SUMA_STDERR, 
                 "LocalDomainParent must follow Anatomical flag with -tsnad \n");
				exit (1);
			}
         LDP[N_surf] = argv[kar];
         
         ++N_surf; 
			brk = YUP;
		}
      
      if (!brk && (strcmp(argv[kar], "-tsnadm") == 0)) {
         if (N_surf >= SUMA_MAX_N_SURFACE_SPEC) {
            SUMA_SL_Err("Exceeding maximum number of allowed surfaces...");
            exit(1);   
         }
         /* get the type */
         kar ++;
			if (kar >= argc)  {
		  		fprintf (SUMA_STDERR, "TYPE argument must follow -tsnad \n");
				exit (1);
			}
         TypeC[N_surf] = SUMA_SurfaceTypeCode(argv[kar]);
         if (  TypeC[N_surf] == SUMA_FT_ERROR || 
               TypeC[N_surf] == SUMA_FT_NOT_SPECIFIED) {
            fprintf (SUMA_STDERR, "%s is a bad file TYPE.\n", argv[kar]);
            exit(1);
         }
         /* get the state */
         kar ++;
			if (kar >= argc)  {
		  		fprintf (SUMA_STDERR, 
                     "STATE argument must follow TYPE with -tsnad \n");
				exit (1);
			}
         State[N_surf] = argv[kar];
         
         /* get the name */
         if (  TypeC[N_surf] == SUMA_SUREFIT || 
               TypeC[N_surf] == SUMA_VEC) N_name = 2;
         else N_name = 1;
         if (kar+N_name >= argc)  {
		  		fprintf (SUMA_STDERR, "need %d elements for NAME \n", N_name);
				exit (1);
			}
         kar ++; Name_coord[N_surf] = argv[kar];
         if (N_name == 2) {
            kar ++; Name_topo[N_surf] = argv[kar];
         } else { 
            Name_topo[N_surf] = NULL;
         }
         
         
         /* get the anatomical flag */
         kar ++;
			if (kar >= argc)  {
		  		fprintf (SUMA_STDERR, 
                     "Anatomical flag must follow NAME with -tsnad \n");
				exit (1);
			}
         Anat[N_surf] = SUMA_TO_UPPER_C(argv[kar][0]);
         if (Anat[N_surf] != 'Y' && Anat[N_surf] != 'N') {
            SUMA_S_Err("Anatomical flag must be either 'y' or 'n'");
            exit (1);
         }
         /* get the LDP */
         kar ++;
			if (kar >= argc)  {
		  		fprintf (SUMA_STDERR, 
                 "LocalDomainParent must follow Anatomical flag with -tsnad \n");
				exit (1);
			}
         LDP[N_surf] = argv[kar];
         
         /* get the nodeMarker */
         kar ++;
			if (kar >= argc)  {
		  		fprintf (SUMA_STDERR, 
                 "LocalDomainParent must follow Anatomical flag with -tsnad \n");
				exit (1);
			}
         MARK[N_surf] = argv[kar];
         ++N_surf; 
			brk = YUP;
		}
      
      if (!brk && (strcmp(argv[kar], "-tsnadl") == 0)) {
         if (N_surf >= SUMA_MAX_N_SURFACE_SPEC) {
            SUMA_SL_Err("Exceeding maximum number of allowed surfaces...");
            exit(1);   
         }
         /* get the type */
         kar ++;
			if (kar >= argc)  {
		  		fprintf (SUMA_STDERR, "TYPE argument must follow -tsnad \n");
				exit (1);
			}
         TypeC[N_surf] = SUMA_SurfaceTypeCode(argv[kar]);
         if (  TypeC[N_surf] == SUMA_FT_ERROR || 
               TypeC[N_surf] == SUMA_FT_NOT_SPECIFIED) {
            fprintf (SUMA_STDERR, "%s is a bad file TYPE.\n", argv[kar]);
            exit(1);
         }
         /* get the state */
         kar ++;
			if (kar >= argc)  {
		  		fprintf (SUMA_STDERR, 
                     "STATE argument must follow TYPE with -tsnad \n");
				exit (1);
			}
         State[N_surf] = argv[kar];
         
         /* get the name */
         if (  TypeC[N_surf] == SUMA_SUREFIT || 
               TypeC[N_surf] == SUMA_VEC) N_name = 2;
         else N_name = 1;
         if (kar+N_name >= argc)  {
		  		fprintf (SUMA_STDERR, "need %d elements for NAME \n", N_name);
				exit (1);
			}
         kar ++; Name_coord[N_surf] = argv[kar];
         if (N_name == 2) {
            kar ++; Name_topo[N_surf] = argv[kar];
         } else { 
            Name_topo[N_surf] = NULL;
         }
         
         
         /* get the anatomical flag */
         kar ++;
			if (kar >= argc)  {
		  		fprintf (SUMA_STDERR, 
                     "Anatomical flag must follow NAME with -tsnad \n");
				exit (1);
			}
         Anat[N_surf] = SUMA_TO_UPPER_C(argv[kar][0]);
         if (Anat[N_surf] != 'Y' && Anat[N_surf] != 'N') {
            SUMA_S_Err("Anatomical flag must be either 'y' or 'n'");
            exit (1);
         }
         /* get the LDP */
         kar ++;
			if (kar >= argc)  {
		  		fprintf (SUMA_STDERR, 
                 "LocalDomainParent must follow Anatomical flag with -tsnad \n");
				exit (1);
			}
         LDP[N_surf] = argv[kar];
         
         /* get the nodeMarker */
         kar ++;
			if (kar >= argc)  {
		  		fprintf (SUMA_STDERR, 
                 "LocalDomainParent must follow Anatomical flag with -tsnad \n");
				exit (1);
			}
         LABEL[N_surf] = argv[kar];
         ++N_surf; 
			brk = YUP;
		}
      
      if (!brk) {
			fprintf (SUMA_STDERR,
                  "Error %s: Option %s not understood. Try -help for usage\n", 
                  FuncName, argv[kar]);
			exit (1);
		} else {	
			brk = NOPE;
			kar ++;
		}
   }
   
   /* write out the comments */
   if (!spec_name) {
      fid = fopen("quick.spec", "w");
   } else {
      fid = fopen(spec_name,"w");
   }
   if (!fid){
      SUMA_SL_Err("Failed to open file for output");
      exit(1);
   }
   fprintf(fid,"# define the group\n");
   fprintf(fid,"\tGroup = QuickSpec\n");
   
   
   /* now create a list of unique states */
   idefstate = 0;
   if (!State[0]) {
      Unique_st = SUMA_copy_string ("\tStateDef = S_1\n");
      idefstate = 1;
   } else {
      sprintf(stmp, "\tStateDef = %s\n", State[0]);
      Unique_st = SUMA_copy_string (stmp);
   }
   for (i=1; i < N_surf; ++i) {
      if (!State[i]) { 
         ++idefstate;
         sprintf(stmp,"\tStateDef = S_%d\n", idefstate);
         Unique_st = SUMA_append_replace_string (Unique_st, stmp, "", 1);
      } else { 
         if (SUMA_iswordin(Unique_st, State[i]) != 1) {
            sprintf(stmp, "\tStateDef = %s\n", State[i]);
            Unique_st = SUMA_append_replace_string(Unique_st, stmp, "", 1);
         }
      }
   }
   fprintf (fid, "# define the various States\n");
   fprintf (fid, "%s\n", Unique_st);
   
   /* check on LDP correctness */
   for (i=0; i < N_surf; ++i) {
      if (LDP[i]) {
         if (!strcmp(LDP[i],"same") || !strcmp(LDP[i],"Same")) 
            SUMA_TO_UPPER(LDP[i]);
         if (strcmp(LDP[i],"SAME")) {
            j= 0;
            while (j<N_surf && strcmp(LDP[i], Name_coord[j])) ++j;
            if (j == N_surf) {
               SUMA_S_Errv("Could not find a surface named %s\n"
                           "to be the local domain parent of %s\n",
                           LDP[i], Name_coord[i]);
               exit(1);
            }
            if (!strcmp(LDP[i], Name_coord[i])) {/* reset to SAME*/
               LDP[i] = NULL; /* this results is SAME below */
            }
         }
      }
   } 
   /* now loop accross surfaces and write out the results */
   idefstate = 0;
   for (i=0; i < N_surf; ++i) {
      fprintf(fid, "\nNewSurface\n");
      fprintf(fid, "\tSurfaceType = %s\n", SUMA_SurfaceTypeString(TypeC[i]));
      if (!State[i]) { 
         ++idefstate;
         fprintf(fid, "\tSurfaceState = S_%d\n", idefstate);
      } else fprintf(fid, "\tSurfaceState = %s\n", State[i]);
      if (Name_topo[i]) {
         fprintf(fid, "\tCoordFile = %s\n", Name_coord[i]);
         fprintf(fid, "\tTopoFile = %s\n", Name_topo[i]);
      } else {
         fprintf(fid, "\tSurfaceName = %s\n", Name_coord[i]); 
      }
      /* add LocalDomainParent */
      if (LDP[i]) fprintf(fid, "\tLocalDomainParent = %s\n", LDP[i]);
      else fprintf(fid, "\tLocalDomainParent = SAME\n");
      /* add Anatomical */
      if (Anat[i]) fprintf(fid, "\tAnatomical = %c\n", Anat[i]);
      else fprintf(fid, "\tAnatomical = Y\n");
      /* add nodeMarker */
      if (MARK[i]) fprintf(fid, "\tNodeMarker = %s\n", MARK[i]);
      if (LABEL[i]) fprintf(fid, "\tLabelDset = %s\n", LABEL[i]);
      
      /* binary ? */
      switch (TypeC[i]) {
         case SUMA_FREE_SURFER:
            if (!SUMA_isExtension(Name_coord[i], ".asc")) {
               fprintf(fid, "\tSurfaceFormat = BINARY\n");
            }
            break;
         default:
            break;
      }
   }
   
   fclose(fid); fid = NULL;
   
   if (Unique_st) SUMA_free(Unique_st); Unique_st = NULL;
   
   if (ps) SUMA_FreeGenericArgParse(ps); ps = NULL;
   if (!SUMA_Free_CommonFields(SUMAg_CF)) {
      fprintf(SUMA_STDERR,"Error %s: SUMAg_CF Cleanup Failed!\n", FuncName);
      exit(1);
   }

   SUMA_RETURN(0);
   
}/* main quickspec */
Example #2
0
int main (int argc,char *argv[])
{
    static char FuncName[]= {"SUMA_inflate_compare"};
    SUMA_SurfSpecFile Spec;
    char *specfilename = NULL;
    int kar, id;
    SUMA_Boolean brk;
    SUMA_Boolean SurfIn = NOPE;

    SUMA_SurfaceObject *Surf1 = NULL, *Surf2=NULL;
    char *Surf1_FileName = NULL;
    char *Surf2_FileName = NULL;
    char *Vol1Parent_FileName=NULL;
    char *Vol2Parent_FileName=NULL;

    /* for SureFit surface */
    SUMA_SFname *Surf1_SFName = NULL, *Surf2_SFName = NULL;

    /* other variables */
    int i;
    int num_nodes1;
    int num_nodes2;
    float P0[3];
    float delta_t;
    float P1[3];
    float N0[3];
    float maxdistance, mindistance;
    float *distance;
    float Points[2][3]= { {0.0, 0.0, 0.0}, {0.0, 0.0, 0.0}}, p1[3]= {0.0, 0.0, 0.0}, p2[3]= {0.0, 0.0, 0.0};
    SUMA_COLOR_MAP *MyColMap;
    SUMA_SCALE_TO_MAP_OPT *MyOpt;
    SUMA_COLOR_SCALED_VECT * MySV;
    SUMA_MT_INTERSECT_TRIANGLE *triangle;
    SUMA_SURF_NORM SN1;
    SUMA_SURF_NORM SN2;
    SUMA_SurfaceObject *SO1, *SO2;
    struct timeval tt;
    FILE *colorfile;
    FILE *distancefile;
    char colorfilename[1000];
    char distancefilename[1000];
    char *tag1 = NULL;
    char *tag2 = NULL;
    char *state1 = NULL;
    char *state2 = NULL;
    char *hemi = NULL;
    float B_dim[3];
    SUMA_ISINBOX isin;
    SUMA_PATCH *Patch=NULL;
    SUMA_Boolean TryFull = NOPE, FullOnly;
    SUMA_MEMBER_FACE_SETS *Memb = NULL;
    int *FaceSet_tmp;
    int N_FaceSet_tmp;
    char *fout = NULL;
    int inflation = 10; /* inflation in mm */

    /* allocate space for CommonFields structure */
    SUMAg_CF = SUMA_Create_CommonFields ();
    if (SUMAg_CF == NULL) {
        fprintf(SUMA_STDERR,"Error %s: Failed in SUMA_Create_CommonFields\n", FuncName);
        exit(1);
    }

    if (argc < 5) {
        cmp_surf_usage();
        exit (1);
    }

    /* read in the surfaces */
    kar = 1;
    brk = NOPE;
    SurfIn = NOPE;
    FullOnly = YUP;
    while (kar < argc) {
        /* loop accross command line options */
        if ((strcmp(argv[kar], "-h") == 0) || (strcmp(argv[kar], "-help") == 0)) {
            cmp_surf_usage ();
            exit (1);
        }
        if (!brk && (strcmp(argv[kar], "-hemi")) == 0) {
            kar ++;
            if (kar >= argc) {
                fprintf (SUMA_STDERR, "need argument after -hemi");
                exit (1);
            }
            hemi = argv[kar];
            brk = YUP;
        }
        if (!brk && (strcmp(argv[kar], "-prefix")) == 0) {
            kar ++;
            if (kar >= argc) {
                fprintf (SUMA_STDERR, "need argument after -prefix");
                exit (1);
            }
            fout = argv[kar];
            brk = YUP;
        }
        if (!brk && (strcmp(argv[kar], "-spec")) == 0) {
            kar ++;
            if (kar >= argc) {
                fprintf (SUMA_STDERR, "need argument after -spec ");
                exit (1);
            }
            specfilename = argv[kar];
            brk = YUP;
        }
        if (!brk && (strcmp(argv[kar], "-box")) == 0) {
            kar ++;
            if (kar+2 >= argc) {
                fprintf (SUMA_STDERR, "need 3 arguments after -box");
                exit (1);
            }
            B_dim[0] = atof(argv[kar]);
            kar ++;
            B_dim[1] = atof(argv[kar]);
            kar ++;
            B_dim[2] = atof(argv[kar]);

            FullOnly = NOPE;
            brk = YUP;
        }
        if (!brk) {
            fprintf (SUMA_STDERR,"Error %s: Option %s not understood. Try -help for usage\n", FuncName, argv[kar]);
            exit (1);
        }
        else {
            brk = NOPE;
            kar ++;
        }
    }/* loop across command line options */


    if (specfilename == NULL) {
        fprintf (SUMA_STDERR,"Error %s: No spec filename specified.\n", FuncName);
        exit(1);
    }
    if (!SUMA_AllocSpecFields(&Spec)) {
        SUMA_S_Err("Failed to allocate spec fields");
        exit(1);
    }
    if (!SUMA_Read_SpecFile (specfilename, &Spec)) {
        fprintf(SUMA_STDERR,"Error %s: Error in SUMA_Read_SpecFile\n", FuncName);
        exit(1);
    }

    /**** loading the first surface *****/
    if (SUMA_iswordin(Spec.SurfaceType[0], "FreeSurfer") == 1) {
        Surf1_FileName = Spec.SurfaceFile[0];
        Surf1 = SUMA_Load_Surface_Object(Surf1_FileName, SUMA_FREE_SURFER, SUMA_ASCII, Vol1Parent_FileName);
        tag1 =  "FS";
    }
    else if (SUMA_iswordin(Spec.SurfaceType[0], "SureFit") == 1) {
        Surf1_SFName = SUMA_malloc(sizeof(SUMA_SFname));
        strcpy(Surf1_SFName->name_coord,Spec.CoordFile[0]);
        strcpy(Surf1_SFName->name_topo, Spec.TopoFile[0]);
        strcpy(Surf1_SFName->name_param, Spec.SureFitVolParam[0]);
        Surf1 = SUMA_Load_Surface_Object(Surf1_SFName, SUMA_SUREFIT, SUMA_ASCII,Vol1Parent_FileName);
        tag1 =  "SF";
    }
    state1 = Spec.State[0];

    /**** loading the second surface *****/
    if (SUMA_iswordin(Spec.SurfaceType[0], "FreeSurfer") == 1) {
        Surf2_FileName = Spec.SurfaceFile[0];
        Surf2 = SUMA_Load_Surface_Object(Surf1_FileName, SUMA_FREE_SURFER, SUMA_ASCII, Vol1Parent_FileName);
        tag2 =  "FS";
    }
    else if (SUMA_iswordin(Spec.SurfaceType[0], "SureFit") == 1) {
        Surf2_SFName = SUMA_malloc(sizeof(SUMA_SFname));
        strcpy(Surf2_SFName->name_coord,Spec.CoordFile[0]);
        strcpy(Surf2_SFName->name_topo, Spec.TopoFile[0]);
        strcpy(Surf2_SFName->name_param, Spec.SureFitVolParam[0]);
        Surf2 = SUMA_Load_Surface_Object(Surf1_SFName, SUMA_SUREFIT, SUMA_ASCII,Vol1Parent_FileName);
        tag2 =  "SF";
    }
    state2 = Spec.State[0];

    if (fout == NULL) { /* form default name */
        sprintf(distancefilename, "dist_%s_%s_%s_%dmm.txt",hemi,tag1,state1,inflation);
        sprintf(colorfilename, "dist_%s_%s_%s_%dmm.col",hemi,tag1,state1,inflation);
    }
    else {
        sprintf (colorfilename, "%s.col", fout);
        sprintf (distancefilename, "%s.txt", fout);
    }

    if (SUMA_filexists(colorfilename) || SUMA_filexists(distancefilename)) {
        fprintf (SUMA_STDERR,"Error %s: One or both of output files %s, %s exists.\nWill not overwrite.\n", \
                 FuncName, distancefilename, colorfilename);
        exit(1);
    }

    /***********************************************************************************************/

    SO1 = Surf1;
    SO2 = Surf2;

    /*SUMA_Print_Surface_Object ( SO1, NULL);
    SUMA_Print_Surface_Object ( SO2, NULL);*/

    num_nodes1 = SO1->N_Node;
    num_nodes2 = SO2->N_Node;

    fprintf(SUMA_STDERR, "Number of nodes in surface 1: %d \n", num_nodes1);
    fprintf(SUMA_STDERR, "Number of nodes in surface 2: %d \n", num_nodes2);
    SN1 = SUMA_SurfNorm(SO1->NodeList,  SO1->N_Node, SO1->FaceSetList, SO1->N_FaceSet);

    /* Take each node in the SO1-> Nodelist and its corresponding SN1->NodeNormList.  This is the normalized normal vector to the node on the first surface.
       So each node is P0.  P1 is computed as some point along the normal vector to that node.  Lets say P1 is P0 + 10 mm along the normal.

     Write out P1 as a surface */
    /* for each node on the first surface do the following */
    SUMA_etime (&tt, 0);
    for (i = 0; i < num_nodes1; i++) {
        id = SO1->NodeDim * i;
        P0[0] = SO1->NodeList[id];
        P0[1] = SO1->NodeList[id+1];
        P0[2] = SO1->NodeList[id+2];
        N0[0] = SN1.NodeNormList[id];
        N0[1] = SN1.NodeNormList[id+1];
        N0[2] = SN1.NodeNormList[id+2];
        SUMA_POINT_AT_DISTANCE(N0, P0, 10, Points);
        P1[0] = Points[0][0];
        P1[1] = Points[0][1];
        P1[2] = Points[0][2];
        //    fprintf(SUMA_STDERR,"P0 values: %f\t%f\t%f, P1 values: %f\t%f\t%f\n", P0[0],P0[1],P0[2],P1[0],P1[1],P1[2]);

        /* Saving the new surfaces */
        SO2->NodeList[id] = P1[0];
        SO2->NodeList[id+1] = P1[1];
        SO2->NodeList[id+2] = P1[2];
    }

    /* Now use SO1 and SO2 as the two surfaces and compute the distance */

    SN1 = SUMA_SurfNorm(SO1->NodeList,  SO1->N_Node, SO1->FaceSetList, SO1->N_FaceSet);
    SN2 = SUMA_SurfNorm(SO2->NodeList,  SO2->N_Node, SO2->FaceSetList, SO2->N_FaceSet);
    if (!FullOnly) {
        /* get the Node member structure */
        fprintf(SUMA_STDOUT, "%s: Computing MemberFaceSets... \n", FuncName);
        Memb = SUMA_MemberFaceSets (SO2->N_Node, SO2->FaceSetList, SO2->N_FaceSet, 3, SO2->idcode_str);
        if (Memb->NodeMemberOfFaceSet == NULL) {
            fprintf(SUMA_STDERR, "Error %s: Failed in SUMA_MemberFaceSets. \n", FuncName);
            exit(1);
        }
    }

    /* Take each node in the SO1-> Nodelist and its corresponding SN1->NodeNormList.  This is the normalized normal vector to the node on the first surface.
       So each node is P0.  P1 is computed as some point along the normal vector to that node.  Lets say P1 is P0 + 10 mm along the normal.  */

    /* for each node on the first surface do the following */

    SUMA_etime (&tt, 0);
    distance = SUMA_malloc(num_nodes1*sizeof(float));
    for (i = 0; i < num_nodes1; i++) {
        //for (i = 0; i < 10; i++) {
        id = SO1->NodeDim * i;
        P0[0] = SO1->NodeList[id];
        P0[1] = SO1->NodeList[id+1];
        P0[2] = SO1->NodeList[id+2];
        N0[0] = SN1.NodeNormList[id];
        N0[1] = SN1.NodeNormList[id+1];
        N0[2] = SN1.NodeNormList[id+2];
        SUMA_POINT_AT_DISTANCE(N0, P0, 1000, Points);
        P1[0] = Points[0][0];
        P1[1] = Points[0][1];
        P1[2] = Points[0][2];
        if (!FullOnly) {
            /* trying to speed up intersection computations
                             by restricting it to nodes within a box */
            TryFull = NOPE;
            /* search for nodes on surface 2 within xx mm of P0 */
            isin = SUMA_isinbox (SO2->NodeList, SO2->N_Node, P0, B_dim, 0);
            if (isin.nIsIn) {
                /* find the patch of surface 2 that is formed by those intersection nodes */
                Patch = SUMA_getPatch ( isin.IsIn, isin.nIsIn,
                                        SO2->N_Node, SO2->FaceSetList, SO2->N_FaceSet,
                                        Memb, 1, 0, 1);
                if (Patch == NULL) {
                    fprintf(SUMA_STDERR,
                            "Error %s: Null returned from SUMA_getPatch.\n", FuncName);
                    exit (1);
                }

                /* Perform the intersection based on that patch using Shruti's version */
                FaceSet_tmp = Patch->FaceSetList;
                N_FaceSet_tmp = Patch->N_FaceSet;
            } else {
                fprintf (SUMA_STDOUT,
                         "%s: No nodes in box about node %d. \n"
                         "Trying for full surface intersection.\n", FuncName, i);
                TryFull = YUP; /* flag to send it to full intersection */
            }
        }

        if (FullOnly || TryFull) {
            Patch = NULL;
            FaceSet_tmp = SO2->FaceSetList;
            N_FaceSet_tmp = SO2->N_FaceSet;
        }

        /*now try with the segment from Points[0] to Points[1] returned above. */
        p1[0] = Points[0][0];
        p1[1] = Points[0][1];
        p1[2] = Points[0][2];
        p2[0] = Points[1][0];
        p2[1] = Points[1][1];
        p2[2] = Points[1][2];
        triangle = SUMA_MT_intersect_triangle(p1,p2, SO2->NodeList, SO2->N_Node, SO2->FaceSetList, SO2->N_FaceSet, NULL, 0);
        //SUMA_Show_MT_intersect_triangle(triangle, NULL);
        if (triangle->N_hits ==0) {
            distance[i] = -1;
            // fprintf(SUMA_STDERR, "Could not find hit for node %d in either direction.\n", i);
        }
        else {
            distance[i] = sqrtf( pow(triangle->P[0]-P0[0],2)+
                                 pow(triangle->P[1]-P0[1],2)+
                                 pow(triangle->P[2]-P0[2],2)   );
            /* fprintf(SUMA_STDERR, "distance is : %f\n", distance[i]); */

        }

        SUMA_Free_MT_intersect_triangle(triangle);
        if (Patch) SUMA_freePatch(Patch);

        /* calculating the time elapsed and remaining */
        if (!(i%100)) {
            delta_t = SUMA_etime(&tt, 1);
            fprintf (SUMA_STDERR,
                     " [%d]/[%d] %.2f/100%% completed. "
                     "Dt = %.2f min done of %.2f min total\r" ,
                     i, num_nodes1, (float)i / num_nodes1 * 100,
                     delta_t/60, delta_t/i * num_nodes1/60);
        }

    }

    /* write out the distance file */
    if((distancefile = fopen(distancefilename, "w"))==NULL) {
        fprintf(SUMA_STDERR, "Could not open file distance.txt.\n");
        exit(1);
    }
    else {
        for (i=0; i < num_nodes1; ++i) {
            fprintf (distancefile,"%d\t%f\n", i, distance[i]);
        }
        fclose (distancefile);
    }

    /* output this distance as a color file */
    MyColMap = SUMA_FindNamedColMap("byr64");
    MyOpt = SUMA_ScaleToMapOptInit();
    MySV = SUMA_Create_ColorScaledVect(num_nodes1, 0);
    mindistance = minimum(num_nodes1, distance);
    maxdistance = maximum(num_nodes1, distance);
    SUMA_ScaleToMap(distance,num_nodes1,mindistance, maxdistance,
                    MyColMap,MyOpt,MySV);


    /* write out the distance color file */
    if((colorfile = fopen(colorfilename, "w"))==NULL) {
        fprintf(SUMA_STDERR, "Could not open file distance.col.\n");
        exit(1);
    }
    else {
        for (i=0; i < num_nodes1; ++i) {
            fprintf (colorfile,
                     "%d\t%f\t%f\t%f\n",
                     i, MySV->cV[3*i  ], MySV->cV[3*i+1], MySV->cV[3*i+2]);
        }
        fclose (colorfile);
    }

    if (!SUMA_FreeSpecFields(&Spec)) {
        SUMA_S_Err("Failed to free spec fields");
    }
    if (!SUMA_Free_CommonFields(SUMAg_CF)) SUMA_error_message(FuncName,"SUMAg_CF Cleanup Failed!",1);

    return 1;
}
int *SUMA_IV_FaceSetsextract (char *IV_filename, int *N_FaceSetList)
{/* SUMA_IV_FaceSetsextract */
	char s[500],serr[500];
	char seq_strt[5][30], seq_end[5][30];
	int i, f, ex, si, si_exit, evl, se, se_exit;
	int ip, NP, cnt, nospacing, MaxAlloc = 100, *linv, *FaceSetList;
	div_t cnt4;
	FILE*iv_file;
   static char FuncName[]={"SUMA_IV_FaceSetsextract"};
   
   cnt4.quot = cnt4.rem = 0;

	/* intialize the number of points read to 0 */
	*N_FaceSetList = 0;
	
	linv = (int *)SUMA_malloc (MaxAlloc*sizeof(int));
	if (!linv)
		{
				SUMA_alloc_problem ("Allocation error in IV-FaceSetExtract");
				return (NULL);
		}
		
	/*This is the sequence to trigger reading the numbers*/
	sprintf (seq_strt[0],"IndexedFaceSet");
	sprintf (seq_strt[1],"{");
	sprintf (seq_strt[2],"coordIndex");	
	sprintf (seq_strt[3],"[");

	si_exit = 4; /* set equal to the number of strings to be matched */

	/*This is a sequence to mark the end of the number list*/
	sprintf (seq_end[0],"]");
	sprintf (seq_end[1],"}");  /* you do not need that one, it is possible to have other objects following coordIndex */

	se_exit = 1; /* set equal to the number of strings to be matched */

	iv_file = fopen (IV_filename,"r");
	if (iv_file == NULL)
		{
			SUMA_error_message ("SUMA_IV_FaceSetsextract","Could not open input file ",1);
			return (NULL);
		}


	si = 0;
	ex = 1;
	cnt = 0;

	nospacing = 0; /* this flag is used to when the last number is read and it has the first seq_end*/
							/* character attached to it, ie no space ! in between*/
	while (ex != EOF && si < si_exit)
	{
		ex = fscanf (iv_file,"%s",s);

		/*evl = equal_strings (s,seq_strt[si]);*/

		if (strlen (seq_strt[si]) >= strlen (s)) 
			{
				evl = SUMA_iswordin (seq_strt[si],s);
				if (evl == 1)
					nospacing = 0; /* There is a space between character in starting sequence and first number*/ 
			}
		else
			{
				evl = SUMA_iswordin (s,seq_strt[si]);
				if (evl == 1)
						nospacing = 1;
			}

		switch (evl)
			{
				case 0:
					si = 0;		/* No match, reset the sequence counter */
					break;
				case 1:
					if (nospacing == 1 && si == si_exit-1) /* it has to be the last character in the sequence*/
						{
							sprintf (serr,"Must have a space character between first number and last character in start sequence ( %s )",s);
							SUMA_error_message ("SUMA_IV_FaceSetsextract",serr,1);
							exit (1);

						}
					si = si +1;	/* increment the start sequence counter */
					#ifdef DEBUG_3
						printf ("found %s  ---  si = %d\n",s,si);
					#endif
					break;
				default:
					break;
			}
	}

	/* Now, read the series of numbers until you encounter the first string of the ending sequence*/
	se = 0;
	nospacing = 0; 

	while (ex != EOF && se < se_exit )
		{
			ex = fscanf (iv_file,"%s",s);
			/*evl = equal_strings (s,seq_end[se]);*/

			if (strlen (seq_end[se]) >= strlen (s)) 
				{
					evl = SUMA_iswordin (seq_end[se],s);
					if (evl == 1)
						nospacing = 0; /* There is a space between last number and fisrt character in ending sequence */ 
				}
			else
				{ 
					evl = SUMA_iswordin (s,seq_end[se]);
					if (evl == 1)
						nospacing = 1;
				}

			switch (evl)
				{
					case 0:
						f = atoi (s);
						linv[cnt] = f;
							#ifdef DEBUG_3
								printf ("\nNumber (%d): %d is appended to end sequence !\n",cnt,linv[cnt]);	
							#endif
		
						++cnt;
						if (cnt >= MaxAlloc - 1)
							{
								MaxAlloc = MaxAlloc + 100;
								linv = (int *)SUMA_realloc ((void*) linv, MaxAlloc * sizeof(int));
								if (!linv)
									{
										SUMA_alloc_problem ("Re-Allocation error in IV-FaceSetExtract");
										return (NULL);
									}
								
							}  
						se = 0;  /* no match for ending sequence, reset the counter */
						break;
					case 1:		/* found the first character in the end sequence */
						if (nospacing == 1 && se == 0) /* it has to be the first character in the sequence*/
						{
							f = atoi (s);
							linv[cnt] = f;
							
							++cnt;
							#ifdef DEBUG_3
								printf ("\nLast number (%d): %f is appended to end sequence !\n",cnt,f);	
							#endif
						}
						#ifdef DEBUG_3
							printf ("\nfound %s  ---  se = %d\n",s,se);
						#endif
						se = se + 1;
						break;
					default:
						break;
				}

		}
	if (si < si_exit)
		{
			SUMA_error_message ("IV_FaceSetextract","Could not find specified starting sequence",0);
			for (i=0;i<si_exit;++i)
					printf ("%s \t",seq_strt[i]);
		}
	else
		{ 
			if (se < se_exit)
				{
					SUMA_error_message ("IV_FaceSetextract","Could not find specified ending sequence",0);
					for (i=0;i<se_exit;++i)
							printf ("%s \t",seq_end[i]);
				}
			else
				{
					/* check that the number of points read is multiple of 4 */
					cnt4 = div (cnt,4);
					if (cnt4.rem != 0)
						{
							SUMA_error_message ("IV_FaceSetextract","number of points read is not multiple of 4 !",0);
							#ifdef DEBUG_3
								printf ("%f FaceSets sets read !!!\n",(float)cnt/4);
							#endif
						}
					
				}
		}

	*N_FaceSetList = cnt4.quot ;
	
	/* Now allocate space for SUMA_IV_FaceSetsextract and fill it up */
	NP = 3;
	FaceSetList = (int *) SUMA_calloc (*N_FaceSetList * NP, sizeof(int));
	if (!FaceSetList)
		{
			SUMA_alloc_problem("IV_FaceSetextract : Could not allocate");
			return(NULL);
		}
	
	i = 0;
	ip = 0;
	while (i < cnt) {
		FaceSetList[ip] = linv[i]; ++ip; ++i;
		FaceSetList[ip] = linv[i]; ++ip; ++i;
		FaceSetList[ip] = linv[i]; ++ip; ++i;
		++i; /* skip the 4th value */
	}
	
	fclose (iv_file);
	
	SUMA_free(linv);
	
	return (FaceSetList);

}/* SUMA_IV_FaceSetsextract */