int main(int argc, char *argv[]) { char **av, surf_fname[100], *template_fname, *out_fname, *surf_dir, *hemi, *sphere_name ; int ac, nargs ; MRI_SURFACE *mris ; MRI_SP *mrisp, *mrisp_template ; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_add_template.c,v 1.8 2011/03/02 00:04:26 nicks Exp $", "$Name: $"); if (nargs && argc - nargs == 1) exit (0); argc -= nargs; Progname = argv[0] ; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; ac = argc ; av = argv ; for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++) { nargs = get_option(argc, argv) ; argc -= nargs ; argv += nargs ; } if (argc < 5) usage_exit() ; surf_dir = argv[1] ; hemi = argv[2] ; sphere_name = argv[3] ; out_fname = template_fname = argv[4] ; if (argc > 5) out_fname = argv[5] ; sprintf(surf_fname, "%s/%s.%s", surf_dir, hemi, sphere_name) ; fprintf(stderr, "reading new surface %s...\n", surf_fname) ; mris = MRISread(surf_fname) ; if (!mris) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, surf_fname) ; MRISsaveVertexPositions(mris, ORIGINAL_VERTICES) ; if (!FileExists(template_fname)) /* first time - create it */ { fprintf(stderr, "creating new parameterization...\n") ; mrisp_template = MRISPalloc(scale, PARAM_IMAGES); } else { fprintf(stderr, "reading template parameterization from %s...\n", template_fname) ; mrisp_template = MRISPread(template_fname) ; if (!mrisp_template) ErrorExit(ERROR_NOFILE, "%s: could not open template file %s", Progname, template_fname) ; } /* first read in inflated surface and use it to build the first template set. */ sprintf(surf_fname, "%s/%s.%s", surf_dir, hemi, INFLATED_NAME) ; if (MRISreadVertexPositions(mris, surf_fname) != NO_ERROR) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, surf_fname) ; MRISsetNeighborhoodSize(mris, nbrs) ; MRIScomputeMetricProperties(mris) ; MRIScomputeSecondFundamentalForm(mris) ; MRISuseMeanCurvature(mris) ; MRISaverageCurvatures(mris, navgs) ; MRISrestoreVertexPositions(mris, ORIGINAL_VERTICES) ; MRISnormalizeCurvature(mris, which_norm) ; fprintf(stderr, "computing parameterization for surface %s...\n",surf_fname); mrisp = MRIStoParameterization(mris, NULL, scale, 0) ; MRISPcombine(mrisp, mrisp_template, 0) ; MRISPfree(&mrisp) ; /* now do the same thing with the smoothwm curvatures. */ sprintf(surf_fname, "%s/%s.%s", surf_dir, hemi, SMOOTH_NAME) ; if (MRISreadVertexPositions(mris, surf_fname) != NO_ERROR) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, surf_fname) ; MRIScomputeMetricProperties(mris) ; if (curvature_fname[0]) MRISreadCurvatureFile(mris, curvature_fname) ; else { MRIScomputeSecondFundamentalForm(mris) ; MRISuseMeanCurvature(mris) ; } MRISaverageCurvatures(mris, navgs) ; MRISrestoreVertexPositions(mris, ORIGINAL_VERTICES) ; if (curvature_fname[0]) fprintf(stderr, "computing parameterization for surface %s (%s)...\n", surf_fname, curvature_fname); else fprintf(stderr, "computing parameterization for surface %s...\n", surf_fname); MRISnormalizeCurvature(mris, which_norm) ; mrisp = MRIStoParameterization(mris, NULL, scale, 0) ; MRISPcombine(mrisp, mrisp_template, 3) ; fprintf(stderr, "writing updated template to %s...\n", out_fname) ; MRISPwrite(mrisp_template, out_fname) ; MRISPfree(&mrisp) ; MRISPfree(&mrisp_template) ; MRISfree(&mris) ; exit(0) ; return(0) ; /* for ansi */ }
int main(int argc, char *argv[]) { char **av, surf_fname[STRLEN], *template_fname, *hemi, *sphere_name, *cp, *subject, fname[STRLEN] ; int ac, nargs, ino, sno, nbad = 0, failed, n,nfields; VERTEX *v; VALS_VP *vp; MRI_SURFACE *mris ; MRI_SP *mrisp, /* *mrisp_aligned,*/ *mrisp_template ; INTEGRATION_PARMS parms ; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_make_template.c,v 1.27 2011/03/02 00:04:33 nicks Exp $", "$Name: stable5 $"); if (nargs && argc - nargs == 1) exit (0); argc -= nargs; memset(&parms, 0, sizeof(parms)) ; Progname = argv[0] ; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; /* setting default values for vectorial registration */ setParms(&parms); ac = argc ; av = argv ; for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++) { nargs = get_option(argc, argv,&parms) ; argc -= nargs ; argv += nargs ; } if (argc < 5) usage_exit() ; /* multiframe registration */ if (multiframes) parms.flags |= IP_USE_MULTIFRAMES; if (!strlen(subjects_dir)) /* not specified on command line*/ { cp = getenv("SUBJECTS_DIR") ; if (!cp) ErrorExit(ERROR_BADPARM, "%s: SUBJECTS_DIR not defined in environment.\n", Progname) ; strcpy(subjects_dir, cp) ; } hemi = argv[1] ; sphere_name = argv[2] ; template_fname = argv[argc-1] ; if (1 || !FileExists(template_fname)) /* first time - create it */ { fprintf(stderr, "creating new parameterization...\n") ; if (multiframes) { mrisp_template = MRISPalloc(scale, atlas_size * IMAGES_PER_SURFACE ); /* if (no_rot) /\* don't do rigid alignment *\/ */ /* mrisp_aligned = NULL ; */ /* else */ /* mrisp_aligned = MRISPalloc(scale, PARAM_FRAMES); */ } else { mrisp_template = MRISPalloc(scale, PARAM_IMAGES); /* if (no_rot) /\* don't do rigid alignment *\/ */ /* mrisp_aligned = NULL ; */ /* else */ /* mrisp_aligned = MRISPalloc(scale, PARAM_IMAGES); */ } } else { fprintf(stderr, "reading template parameterization from %s...\n", template_fname) ; /* mrisp_aligned = NULL ; */ mrisp_template = MRISPread(template_fname) ; if (!mrisp_template) ErrorExit(ERROR_NOFILE, "%s: could not open template file %s", Progname, template_fname) ; } argv += 3 ; argc -= 3 ; for (ino = 0 ; ino < argc-1 ; ino++) { failed = 0 ; subject = argv[ino] ; fprintf(stderr, "\nprocessing subject %s (%d of %d)\n", subject, ino+1, argc-1) ; sprintf(surf_fname, "%s/%s/surf/%s.%s", subjects_dir, subject, hemi, sphere_name) ; fprintf(stderr, "reading spherical surface %s...\n", surf_fname) ; mris = MRISread(surf_fname) ; if (!mris) { nbad++ ; ErrorPrintf(ERROR_NOFILE, "%s: could not read surface file %s", Progname, surf_fname) ; exit(1) ; } if (annot_name) { if (MRISreadAnnotation(mris, annot_name) != NO_ERROR) ErrorExit(ERROR_BADPARM, "%s: could not read annot file %s", Progname, annot_name) ; MRISripMedialWall(mris) ; } MRISsaveVertexPositions(mris, CANONICAL_VERTICES) ; MRIScomputeMetricProperties(mris) ; MRISstoreMetricProperties(mris) ; if (Gdiag & DIAG_WRITE) { char *cp1 ; FileNameOnly(template_fname, fname) ; cp = strchr(fname, '.') ; if (cp) { cp1 = strrchr(fname, '.') ; if (cp1 && cp1 != cp) strncpy(parms.base_name, cp+1, cp1-cp-1) ; else strcpy(parms.base_name, cp+1) ; } else strcpy(parms.base_name, "template") ; sprintf(fname, "%s.%s.out", hemi, parms.base_name); parms.fp = fopen(fname, "w") ; printf("writing output to '%s'\n", fname) ; } /* multiframe registration */ if (multiframes) { nfields=parms.nfields; for ( n = 0; n < mris->nvertices ; n++) /* allocate the VALS_VP structure */ { v=&mris->vertices[n]; vp=calloc(1,sizeof(VALS_VP)); vp->nvals=nfields; vp->orig_vals=(float*)malloc(nfields*sizeof(float)); /* before blurring */ vp->vals=(float*)malloc(nfields*sizeof(float)); /* values used by MRISintegrate */ v->vp=(void*)vp; } /* load the different fields */ for (n = 0 ; n < parms.nfields ; n++) { if (parms.fields[n].name != NULL) { sprintf(surf_fname, "%s/%s/%s/%s.%s", subjects_dir, subject, overlay_dir, hemi, parms.fields[n].name) ; printf("reading overlay file %s...\n", surf_fname) ; if (MRISreadValues(mris, surf_fname) != NO_ERROR) ErrorExit(ERROR_BADPARM, "%s: could not read overlay file %s", Progname, surf_fname) ; MRIScopyValuesToCurvature(mris) ; } else if (ReturnFieldName(parms.fields[n].field)) { /* read in precomputed curvature file */ sprintf(surf_fname, "%s/%s/surf/%s.%s", subjects_dir, subject, hemi, ReturnFieldName(parms.fields[n].field)) ; // fprintf(stderr,"\nreading field %d from %s(type=%d,frame=%d)\n",parms.fields[n].field,surf_fname,parms.fields[n].type,parms.fields[n].frame); if (MRISreadCurvatureFile(mris, surf_fname) != NO_ERROR) { fprintf(stderr,"\n\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); fprintf(stderr, "%s: could not read curvature file '%s'\n", Progname, surf_fname) ; failed = 1; break; } } else { /* compute curvature of surface */ sprintf(surf_fname, "%s/%s/surf/%s.%s", subjects_dir, subject, hemi, surface_names[parms.fields[n].field]) ; /*if(parms.fields[n].field==0) sprintf(fname, "inflated") ; else sprintf(fname, "smoothwm") ;*/ //fprintf(stderr,"\ngenerating field %d(type=%d,frame=%d) (from %s)\n",parms.fields[n].field,parms.fields[n].type,parms.fields[n].frame,surf_fname); // MRISsaveVertexPositions(mris, TMP_VERTICES) ; if (MRISreadVertexPositions(mris, surf_fname) != NO_ERROR) { fprintf(stderr,"\n\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); ErrorPrintf(ERROR_NOFILE, "%s: could not read surface file %s", Progname, surf_fname) ; fprintf(stderr,"setting up correlation coefficient to zero\n"); parms.fields[n].l_corr=parms.fields[n].l_pcorr=0.0; failed=1; break; } if (nbrs > 1) MRISsetNeighborhoodSize(mris, nbrs) ; MRIScomputeMetricProperties(mris) ; MRIScomputeSecondFundamentalForm(mris) ; MRISuseMeanCurvature(mris) ; MRISaverageCurvatures(mris, navgs) ; MRISrestoreVertexPositions(mris, CANONICAL_VERTICES) ; } /* if(parms.fields[n].field!=SULC_CORR_FRAME)*/ MRISnormalizeField(mris,parms.fields[n].type, parms.fields[n].which_norm); /* normalize values */ MRISsetCurvaturesToOrigValues(mris,n); MRISsetCurvaturesToValues(mris,n); } if (failed) { fprintf(stderr,"\n\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); fprintf(stderr,"Subject %s Failed",subject); fprintf(stderr,"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n\n"); /* free cal structure*/ for ( n = 0; n < mris->nvertices ; n++) { v=&mris->vertices[n]; vp=(VALS_VP*)v->vp; free(vp->orig_vals); free(vp->vals); free(vp); v->vp=NULL; } /* free surface */ MRISfree(&mris); /* go onto the next subject */ continue; } } if (multiframes && (!no_rot)) { /* rigid body alignment */ parms.frame_no = 3 ; /* don't use single field correlation functions */ parms.l_corr = parms.l_pcorr = 0.0f ; parms.mrisp = MRIStoParameterization(mris, NULL, scale, 0) ; parms.mrisp_template = mrisp_template ; MRISrigidBodyAlignVectorGlobal(mris, &parms, 1.0, 64.0, 8) ; if (Gdiag & DIAG_WRITE) MRISwrite(mris, "sphere.rot.global") ; MRISrigidBodyAlignVectorLocal(mris, &parms) ; if (Gdiag & DIAG_WRITE) MRISwrite(mris, "sphere.rot.local") ; MRISPfree(&parms.mrisp) ; MRISsaveVertexPositions(mris, CANONICAL_VERTICES) ; }; if ((!multiframes) && (!no_rot) && ino > 0) { /* rigid body alignment */ sprintf(surf_fname, "%s/%s/surf/%s.%s", subjects_dir, subject, hemi, "sulc") ; if (MRISreadCurvatureFile(mris, surf_fname) != NO_ERROR) { ErrorPrintf(Gerror, "%s: could not read curvature file '%s'\n", Progname, surf_fname) ; nbad++ ; MRISfree(&mris) ; continue ; } parms.frame_no = 3 ; /* use sulc for rigid registration */ parms.mrisp = MRIStoParameterization(mris, NULL, scale, 0) ; parms.mrisp_template = mrisp_template ; parms.l_corr = 1.0f ; MRISrigidBodyAlignGlobal(mris, &parms, 1.0, 64.0, 8) ; if (Gdiag & DIAG_WRITE) MRISwrite(mris, "sphere.rot.global") ; MRISrigidBodyAlignLocal(mris, &parms) ; if (Gdiag & DIAG_WRITE) MRISwrite(mris, "sphere.rot.local") ; MRISPfree(&parms.mrisp) ; MRISsaveVertexPositions(mris, CANONICAL_VERTICES) ; } if (multiframes) { for (n = 0; n < parms.nfields ; n++) { MRISsetOrigValuesToCurvatures(mris,n); MRISaverageCurvatures(mris, parms.fields[n].navgs) ; mrisp = MRIStoParameterization(mris, NULL, scale, 0) ; MRISPcombine(mrisp, mrisp_template, parms.fields[n].frame * IMAGES_PER_SURFACE) ; MRISPfree(&mrisp) ; } /* free the VALS_VP structure */ for ( n = 0; n < mris->nvertices ; n++) { v=&mris->vertices[n]; vp=(VALS_VP*)v->vp; free(vp->orig_vals); free(vp->vals); free(vp); v->vp=NULL; } MRISfree(&mris) ; } else { for (sno = 0; sno < SURFACES ; sno++) { if (curvature_names[sno]) /* read in precomputed curvature file */ { sprintf(surf_fname, "%s/%s/surf/%s.%s", subjects_dir, subject, hemi, curvature_names[sno]) ; if (MRISreadCurvatureFile(mris, surf_fname) != NO_ERROR) { nbad++ ; ErrorPrintf(Gerror, "%s: could not read curvature file '%s'\n", Progname, surf_fname) ; failed = 1 ; break ; } /* the two next lines were not in the original code */ MRISaverageCurvatures(mris, navgs) ; MRISnormalizeCurvature(mris, which_norm) ; } else /* compute curvature of surface */ { sprintf(surf_fname, "%s/%s/surf/%s.%s", subjects_dir, subject, hemi, surface_names[sno]) ; if (MRISreadVertexPositions(mris, surf_fname) != NO_ERROR) { ErrorPrintf(ERROR_NOFILE, "%s: could not read surface file %s", Progname, surf_fname) ; nbad++ ; failed = 1 ; break ; } if (nbrs > 1) MRISsetNeighborhoodSize(mris, nbrs) ; MRIScomputeMetricProperties(mris) ; MRIScomputeSecondFundamentalForm(mris) ; MRISuseMeanCurvature(mris) ; MRISaverageCurvatures(mris, navgs) ; MRISrestoreVertexPositions(mris, CANONICAL_VERTICES) ; MRISnormalizeCurvature(mris, which_norm) ; } fprintf(stderr, "computing parameterization for surface %s...\n", surf_fname); if (failed) { continue ; MRISfree(&mris) ; } mrisp = MRIStoParameterization(mris, NULL, scale, 0) ; MRISPcombine(mrisp, mrisp_template, sno*3) ; MRISPfree(&mrisp) ; } MRISfree(&mris) ; } } #if 0 if (mrisp_aligned) /* new parameterization - use rigid alignment */ { MRI_SP *mrisp_tmp ; if (Gdiag & DIAG_WRITE) { char *cp1 ; FileNameOnly(template_fname, fname) ; cp = strchr(fname, '.') ; if (cp) { cp1 = strrchr(fname, '.') ; if (cp1 && cp1 != cp) strncpy(parms.base_name, cp+1, cp1-cp-1) ; else strcpy(parms.base_name, cp+1) ; } else strcpy(parms.base_name, "template") ; sprintf(fname, "%s.%s.out", hemi, parms.base_name); parms.fp = fopen(fname, "w") ; printf("writing output to '%s'\n", fname) ; } for (ino = 0 ; ino < argc-1 ; ino++) { subject = argv[ino] ; if (Gdiag & DIAG_WRITE) fprintf(parms.fp, "processing subject %s\n", subject) ; fprintf(stderr, "processing subject %s\n", subject) ; sprintf(surf_fname, "%s/%s/surf/%s.%s", subjects_dir, subject, hemi, sphere_name) ; fprintf(stderr, "reading spherical surface %s...\n", surf_fname) ; mris = MRISread(surf_fname) ; if (!mris) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, surf_fname) ; MRIScomputeMetricProperties(mris) ; MRISstoreMetricProperties(mris) ; MRISsaveVertexPositions(mris, ORIGINAL_VERTICES) ; sprintf(surf_fname, "%s/%s/surf/%s.%s", subjects_dir, subject, hemi, "sulc") ; if (MRISreadCurvatureFile(mris, surf_fname) != NO_ERROR) ErrorExit(Gerror, "%s: could not read curvature file '%s'\n", Progname, surf_fname) ; parms.frame_no = 3 ; parms.mrisp = MRIStoParameterization(mris, NULL, scale, 0) ; parms.mrisp_template = mrisp_template ; parms.l_corr = 1.0f ; MRISrigidBodyAlignGlobal(mris, &parms, 1.0, 32.0, 8) ; if (Gdiag & DIAG_WRITE) MRISwrite(mris, "sphere.rot.global") ; MRISrigidBodyAlignLocal(mris, &parms) ; if (Gdiag & DIAG_WRITE) MRISwrite(mris, "sphere.rot.local") ; MRISPfree(&parms.mrisp) ; #if 0 /* write out rotated surface */ sprintf(surf_fname, "%s.rot", mris->fname) ; fprintf(stderr, "writing out rigidly aligned surface to '%s'\n", surf_fname) ; MRISwrite(mris, surf_fname) ; #endif /* now generate new parameterization using the optimal alignment */ for (sno = 0; sno < SURFACES ; sno++) { if (curvature_names[sno]) /* read in precomputed curvature file */ { sprintf(surf_fname, "%s/%s/surf/%s.%s", subjects_dir, subject, hemi, curvature_names[sno]) ; if (MRISreadCurvatureFile(mris, surf_fname) != NO_ERROR) ErrorExit(Gerror, "%s: could not read curvature file '%s'\n", Progname, surf_fname) ; } else /* compute curvature of surface */ { sprintf(surf_fname, "%s/%s/surf/%s.%s", subjects_dir, subject, hemi, surface_names[sno]) ; if (MRISreadVertexPositions(mris, surf_fname) != NO_ERROR) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, surf_fname) ; if (nbrs > 1) MRISsetNeighborhoodSize(mris, nbrs) ; MRIScomputeMetricProperties(mris) ; MRIScomputeSecondFundamentalForm(mris) ; MRISuseMeanCurvature(mris) ; MRISaverageCurvatures(mris, navgs) ; MRISrestoreVertexPositions(mris, ORIGINAL_VERTICES) ; MRISnormalizeCurvature(mris) ; } fprintf(stderr, "computing parameterization for surface %s...\n", surf_fname); mrisp = MRIStoParameterization(mris, NULL, scale, 0) ; MRISPcombine(mrisp, mrisp_aligned, sno*3) ; MRISPfree(&mrisp) ; } MRISfree(&mris) ; } if (Gdiag & DIAG_WRITE) fclose(parms.fp) ; mrisp_tmp = mrisp_aligned ; mrisp_aligned = mrisp_template ; mrisp_template = mrisp_tmp ; MRISPfree(&mrisp_aligned) ; } #endif fprintf(stderr, "writing updated template with %d subjects to %s...\n", argc-1-nbad, template_fname) ; MRISPwrite(mrisp_template, template_fname) ; MRISPfree(&mrisp_template) ; exit(0) ; return(0) ; /* for ansi */ }