int main(int argc, char *argv[]) { char **av, *in_fname,fname[STRLEN],hemi[10], path[STRLEN], name[STRLEN],*cp ; int ac, nargs, nhandles ; MRI_SURFACE *mris ; double ici, fi, var ; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_curvature.c,v 1.31 2011/03/02 00:04:30 nicks Exp $", "$Name: stable5 $"); 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 < 2) { usage_exit() ; } in_fname = argv[1] ; FileNamePath(in_fname, path) ; FileNameOnly(in_fname, name) ; cp = strchr(name, '.') ; if (!cp) ErrorExit(ERROR_BADPARM, "%s: could not scan hemisphere from '%s'", Progname, fname) ; strncpy(hemi, cp-2, 2) ; hemi[2] = 0 ; if (patch_flag) /* read the orig surface, then the patch file */ { sprintf(fname, "%s/%s.orig", path, hemi) ; mris = MRISfastRead(fname) ; if (!mris) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, in_fname) ; if (Gdiag & DIAG_SHOW) { fprintf(stderr, "reading patch file %s...\n", in_fname) ; } if (MRISreadPatch(mris, in_fname) != NO_ERROR) ErrorExit(ERROR_NOFILE, "%s: could not read patch file %s", Progname, in_fname) ; } else /* just read the surface normally */ { mris = MRISread(in_fname) ; if (!mris) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, in_fname) ; } MRISsetNeighborhoodSize(mris, nbrs) ; if (nbhd_size > 0) { MRISsampleAtEachDistance(mris, nbhd_size, nbrs_per_distance) ; } if (max_mm > 0) { float ratio ; MRISstoreMetricProperties(mris) ; if (MRISreadCanonicalCoordinates(mris, "sphere") != NO_ERROR) { ErrorExit(ERROR_NOFILE, "%s: could not read canonical coordinates from ?h.sphere", Progname); } MRISsaveVertexPositions(mris, ORIGINAL_VERTICES) ; MRISrestoreVertexPositions(mris, CANONICAL_VERTICES) ; MRIScomputeMetricProperties(mris) ; ratio = mris->orig_area / M_PI * mris->radius * mris->radius * 4.0 ; ratio = mris->orig_area / mris->total_area ; MRISscaleBrain(mris, mris, sqrt(ratio)) ; MRISsaveVertexPositions(mris, CANONICAL_VERTICES) ; MRISrestoreVertexPositions(mris, ORIGINAL_VERTICES) ; MRIScomputeMetricProperties(mris) ; MRIScomputeNeighbors(mris, max_mm) ; } if (param_file) { MRI_SP *mrisp ; mrisp = MRISPread(param_file) ; if (normalize_param) { MRISnormalizeFromParameterization(mrisp, mris, param_no) ; } else { MRISfromParameterization(mrisp, mris, param_no) ; } MRISPfree(&mrisp) ; if (normalize) { MRISnormalizeCurvature(mris,which_norm) ; } sprintf(fname, "%s/%s%s.param", path,name,suffix) ; fprintf(stderr, "writing parameterized curvature to %s...", fname) ; MRISwriteCurvature(mris, fname) ; fprintf(stderr, "done.\n") ; } else { MRIScomputeSecondFundamentalFormThresholded(mris, cthresh) ; nhandles = nint(1.0 - mris->Ktotal / (4.0*M_PI)) ; fprintf(stderr, "total integrated curvature = %2.3f*4pi (%2.3f) --> " "%d handles\n", (float)(mris->Ktotal/(4.0f*M_PI)), (float)mris->Ktotal, nhandles) ; #if 0 fprintf(stderr, "0: k1 = %2.3f, k2 = %2.3f, H = %2.3f, K = %2.3f\n", mris->vertices[0].k1, mris->vertices[0].k2, mris->vertices[0].H, mris->vertices[0].K) ; fprintf(stderr, "0: vnum = %d, v2num = %d, total=%d, area=%2.3f\n", mris->vertices[0].vnum, mris->vertices[0].v2num, mris->vertices[0].vtotal,mris->vertices[0].area) ; #endif MRIScomputeCurvatureIndices(mris, &ici, &fi); var = MRIStotalVariation(mris) ; fprintf(stderr,"ICI = %2.1f, FI = %2.1f, variation=%2.3f\n", ici, fi, var); if (diff_flag) { MRISuseCurvatureDifference(mris) ; MRISaverageCurvatures(mris, navgs) ; sprintf(fname, "%s/%s%s.diff", path,name,suffix) ; fprintf(stderr, "writing curvature difference to %s...", fname) ; MRISwriteCurvature(mris, fname) ; fprintf(stderr, "done.\n") ; } if (ratio_flag) { MRISuseCurvatureRatio(mris) ; MRISaverageCurvatures(mris, navgs) ; if (normalize) { MRISnormalizeCurvature(mris,which_norm) ; } sprintf(fname, "%s/%s%s.ratio", path,name,suffix) ; fprintf(stderr, "writing curvature ratio to %s...", fname) ; MRISwriteCurvature(mris, fname) ; fprintf(stderr, "done.\n") ; } if (contrast_flag) { MRISuseCurvatureContrast(mris) ; MRISaverageCurvatures(mris, navgs) ; if (normalize) { MRISnormalizeCurvature(mris,which_norm) ; } sprintf(fname, "%s/%s%s.contrast", path,name,suffix) ; fprintf(stderr, "writing curvature contrast to %s...", fname) ; MRISwriteCurvature(mris, fname) ; fprintf(stderr, "done.\n") ; } if (neg_flag) { int neg ; if (mris->patch) { mris->status = MRIS_PLANE ; } MRIScomputeMetricProperties(mris) ; neg = MRIScountNegativeTriangles(mris) ; MRISuseNegCurvature(mris) ; MRISaverageCurvatures(mris, navgs) ; sprintf(fname, "%s/%s%s.neg", path,name,suffix) ; fprintf(stderr, "writing negative vertex curvature to %s...", fname) ; MRISwriteCurvature(mris, fname) ; fprintf(stderr, "%d negative triangles\n", neg) ; fprintf(stderr, "done.\n") ; { int vno, fno ; VERTEX *v ; FACE *f ; for (vno = 0 ; vno < mris->nvertices ; vno++) { v = &mris->vertices[vno] ; if (v->ripflag) { continue ; } neg = 0 ; for (fno = 0 ; fno < v->num ; fno++) { f = &mris->faces[v->f[fno]] ; if (f->area < 0.0f) { neg = 1 ; } } if (neg) { fprintf(stdout, "%d\n", vno) ; } } } } if (max_flag) { MRISuseCurvatureMax(mris) ; MRISaverageCurvatures(mris, navgs) ; if (normalize) { MRISnormalizeCurvature(mris,which_norm) ; } sprintf(fname, "%s/%s%s.max", path,name,suffix) ; fprintf(stderr, "writing curvature maxima to %s...", fname) ; MRISwriteCurvature(mris, fname) ; fprintf(stderr, "done.\n") ; } if (min_flag) { MRISuseCurvatureMin(mris) ; MRISaverageCurvatures(mris, navgs) ; if (normalize) { MRISnormalizeCurvature(mris,which_norm) ; } sprintf(fname, "%s/%s%s.min", path,name,suffix) ; fprintf(stderr, "writing curvature minima to %s...", fname) ; MRISwriteCurvature(mris, fname) ; fprintf(stderr, "done.\n") ; } if (stretch_flag) { MRISreadOriginalProperties(mris, NULL) ; MRISuseCurvatureStretch(mris) ; MRISaverageCurvatures(mris, navgs) ; if (normalize) { MRISnormalizeCurvature(mris,which_norm) ; } sprintf(fname, "%s/%s%s.stretch", path,name,suffix) ; fprintf(stderr, "writing curvature stretch to %s...", fname) ; MRISwriteCurvature(mris, fname) ; fprintf(stderr, "done.\n") ; } if (write_flag) { MRISuseGaussianCurvature(mris) ; if (cthresh > 0) { MRIShistoThresholdCurvature(mris, cthresh) ; } MRISaverageCurvatures(mris, navgs) ; sprintf(fname, "%s/%s%s.K", path,name, suffix) ; fprintf(stderr, "writing Gaussian curvature to %s...", fname) ; if (normalize) { MRISnormalizeCurvature(mris,which_norm) ; } MRISwriteCurvature(mris, fname) ; MRISuseMeanCurvature(mris) ; if (cthresh > 0) { MRIShistoThresholdCurvature(mris, cthresh) ; } MRISaverageCurvatures(mris, navgs) ; if (normalize) { MRISnormalizeCurvature(mris,which_norm) ; } sprintf(fname, "%s/%s%s.H", path,name, suffix) ; fprintf(stderr, "done.\nwriting mean curvature to %s...", fname) ; MRISwriteCurvature(mris, fname) ; fprintf(stderr, "done.\n") ; } } 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 */ }
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, *template_fname, *out_fname, fname[STRLEN],*cp; int ac, nargs,err, msec ; MRI_SURFACE *mris ; MRI_SP *mrisp_template ; char cmdline[CMD_LINE_LEN] ; struct timeb start ; make_cmd_version_string (argc, argv, "$Id: mris_register.c,v 1.59 2011/03/02 00:04:33 nicks Exp $", "$Name: stable5 $", cmdline); /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_register.c,v 1.59 2011/03/02 00:04:33 nicks Exp $", "$Name: stable5 $"); if (nargs && argc - nargs == 1) { exit (0); } argc -= nargs; TimerStart(&start) ; Progname = argv[0] ; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; memset(&parms, 0, sizeof(parms)) ; parms.projection = PROJECT_SPHERE ; parms.flags |= IP_USE_CURVATURE ; parms.tol = 0.5 ; // was 1e-0*2.5 parms.min_averages = 0 ; parms.l_area = 0.0 ; parms.l_parea = 0.1f ; // used to be 0.2 parms.l_dist = 5.0 ; // used to be 0.5, and before that 0.1 parms.l_corr = 1.0f ; parms.l_nlarea = 1 ; parms.l_pcorr = 0.0f ; parms.niterations = 25 ; parms.n_averages = 1024 ; // used to be 256 parms.write_iterations = 100 ; parms.dt_increase = 1.01 /* DT_INCREASE */; parms.dt_decrease = 0.99 /* DT_DECREASE*/ ; parms.error_ratio = 1.03 /*ERROR_RATIO */; parms.dt_increase = 1.0 ; parms.dt_decrease = 1.0 ; parms.l_external = 10000 ; /* in case manual label is specified */ parms.error_ratio = 1.1 /*ERROR_RATIO */; parms.integration_type = INTEGRATE_ADAPTIVE ; parms.integration_type = INTEGRATE_MOMENTUM /*INTEGRATE_LINE_MINIMIZE*/ ; parms.integration_type = INTEGRATE_LINE_MINIMIZE ; parms.dt = 0.9 ; parms.momentum = 0.95 ; parms.desired_rms_height = -1.0 ; parms.nbhd_size = -10 ; parms.max_nbrs = 10 ; ac = argc ; av = argv ; for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++) { nargs = get_option(argc, argv) ; argc -= nargs ; argv += nargs ; } if (nsigmas > 0) { MRISsetRegistrationSigmas(sigmas, nsigmas) ; } parms.which_norm = which_norm ; if (argc < 4) { usage_exit() ; } printf("%s\n", vcid) ; printf(" %s\n",MRISurfSrcVersion()); fflush(stdout); surf_fname = argv[1] ; template_fname = argv[2] ; out_fname = argv[3] ; if (parms.base_name[0] == 0) { FileNameOnly(out_fname, fname) ; cp = strchr(fname, '.') ; if (cp) { strcpy(parms.base_name, cp+1) ; } else { strcpy(parms.base_name, "sphere") ; } } fprintf(stderr, "reading surface from %s...\n", surf_fname) ; mris = MRISread(surf_fname) ; if (!mris) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, surf_fname) ; if (parms.var_smoothness) { parms.vsmoothness = (float *)calloc(mris->nvertices, sizeof(float)) ; if (parms.vsmoothness == NULL) { ErrorExit(ERROR_NOMEMORY, "%s: could not allocate vsmoothness array", Progname) ; } parms.dist_error = (float *)calloc(mris->nvertices, sizeof(float)) ; if (parms.dist_error == NULL) { ErrorExit(ERROR_NOMEMORY, "%s: could not allocate dist_error array", Progname) ; } parms.area_error = (float *)calloc(mris->nvertices, sizeof(float)) ; if (parms.area_error == NULL) { ErrorExit(ERROR_NOMEMORY, "%s: could not allocate area_error array", Progname) ; } parms.geometry_error = (float *)calloc(mris->nvertices, sizeof(float)) ; if (parms.geometry_error == NULL) { ErrorExit(ERROR_NOMEMORY, "%s: could not allocate geometry_error array", Progname) ; } } MRISresetNeighborhoodSize(mris, 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, TMP2_VERTICES) ; MRISaddCommandLine(mris, cmdline) ; if (!FZERO(dalpha) || !FZERO(dbeta) || !FZERO(dgamma)) MRISrotate(mris, mris, RADIANS(dalpha), RADIANS(dbeta), RADIANS(dgamma)) ; if (curvature_fname[0]) { fprintf(stderr, "reading source curvature from %s\n",curvature_fname) ; MRISreadCurvatureFile(mris, curvature_fname) ; } if (single_surf) { char fname[STRLEN], *cp, surf_dir[STRLEN], hemi[10] ; MRI_SURFACE *mris_template ; int sno, tnbrs=3 ; FileNamePath(template_fname, surf_dir) ; cp = strrchr(template_fname, '/') ; if (cp == NULL) // no path - start from beginning of file name { cp = template_fname ; } cp = strchr(cp, '.') ; if (cp == NULL) ErrorExit(ERROR_NOFILE, "%s: could no scan hemi from %s", Progname, template_fname) ; strncpy(hemi, cp-2, 2) ; hemi[2] = 0 ; fprintf(stderr, "reading spherical surface %s...\n", template_fname) ; mris_template = MRISread(template_fname) ; if (mris_template == NULL) { ErrorExit(ERROR_NOFILE, "") ; } #if 0 if (reverse_flag) { MRISreverse(mris_template, REVERSE_X, 1) ; } #endif MRISsaveVertexPositions(mris_template, CANONICAL_VERTICES) ; MRIScomputeMetricProperties(mris_template) ; MRISstoreMetricProperties(mris_template) ; if (noverlays > 0) { mrisp_template = MRISPalloc(scale, IMAGES_PER_SURFACE*noverlays); for (sno = 0; sno < noverlays ; sno++) { sprintf(fname, "%s/../label/%s.%s", surf_dir, hemi, overlays[sno]) ; if (MRISreadValues(mris_template, fname) != NO_ERROR) ErrorExit(ERROR_NOFILE, "%s: could not read overlay from %s", Progname, fname) ; MRIScopyValuesToCurvature(mris_template) ; MRISaverageCurvatures(mris_template, navgs) ; MRISnormalizeCurvature(mris_template, which_norm) ; fprintf(stderr, "computing parameterization for overlay %s...\n", fname); MRIStoParameterization(mris_template, mrisp_template, scale, sno*3) ; MRISPsetFrameVal(mrisp_template, sno*3+1, 1.0) ; } } else { mrisp_template = MRISPalloc(scale, PARAM_IMAGES); for (sno = 0; sno < SURFACES ; sno++) { if (curvature_names[sno]) /* read in precomputed curvature file */ { sprintf(fname, "%s/%s.%s", surf_dir, hemi, curvature_names[sno]) ; if (MRISreadCurvatureFile(mris_template, fname) != NO_ERROR) ErrorExit(Gerror, "%s: could not read curvature file '%s'\n", Progname, fname) ; /* the two next lines were not in the original code */ MRISaverageCurvatures(mris_template, navgs) ; MRISnormalizeCurvature(mris_template, which_norm) ; } else /* compute curvature of surface */ { sprintf(fname, "%s/%s.%s", surf_dir, hemi, surface_names[sno]) ; if (MRISreadVertexPositions(mris_template, fname) != NO_ERROR) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, fname) ; if (tnbrs > 1) { MRISresetNeighborhoodSize(mris_template, tnbrs) ; } MRIScomputeMetricProperties(mris_template) ; MRIScomputeSecondFundamentalForm(mris_template) ; MRISuseMeanCurvature(mris_template) ; MRISaverageCurvatures(mris_template, navgs) ; MRISrestoreVertexPositions(mris_template, CANONICAL_VERTICES) ; MRISnormalizeCurvature(mris_template, which_norm) ; } fprintf(stderr, "computing parameterization for surface %s...\n", fname); MRIStoParameterization(mris_template, mrisp_template, scale, sno*3) ; MRISPsetFrameVal(mrisp_template, sno*3+1, 1.0) ; } } } 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) ; if (noverlays > 0) { if (mrisp_template->Ip->num_frame != IMAGES_PER_SURFACE*noverlays) ErrorExit(ERROR_BADPARM, "template frames (%d) doesn't match input (%d x %d) = %d\n", mrisp_template->Ip->num_frame, IMAGES_PER_SURFACE,noverlays, IMAGES_PER_SURFACE*noverlays) ; } } if (use_defaults) { if (*IMAGEFseq_pix(mrisp_template->Ip, 0, 0, 2) <= 1.0) /* 1st time */ { parms.l_dist = 5.0 ; parms.l_corr = 1.0 ; parms.l_parea = 0.2 ; } else /* subsequent alignments */ { parms.l_dist = 5.0 ; parms.l_corr = 1.0 ; parms.l_parea = 0.2 ; } } if (nbrs > 1) { MRISresetNeighborhoodSize(mris, nbrs) ; } MRISprojectOntoSphere(mris, mris, DEFAULT_RADIUS) ; mris->status = MRIS_PARAMETERIZED_SPHERE ; MRIScomputeMetricProperties(mris) ; if (!FZERO(parms.l_dist)) { MRISscaleDistances(mris, scale) ; } #if 0 MRISsaveVertexPositions(mris, ORIGINAL_VERTICES) ; MRISzeroNegativeAreas(mris) ; MRISstoreMetricProperties(mris) ; #endif MRISstoreMeanCurvature(mris) ; /* use curvature from file */ MRISsetOriginalFileName(orig_name) ; if (inflated_name) { MRISsetInflatedFileName(inflated_name) ; } err = MRISreadOriginalProperties(mris, orig_name) ; if (err != 0) { printf("ERROR %d from MRISreadOriginalProperties().\n",err); exit(1); } if (MRISreadCanonicalCoordinates(mris, canon_name) != NO_ERROR) ErrorExit(ERROR_BADFILE, "%s: could not read canon surface %s", Progname, canon_name) ; if (reverse_flag) { MRISreverse(mris, REVERSE_X, 1) ; MRISsaveVertexPositions(mris, TMP_VERTICES) ; MRISrestoreVertexPositions(mris, CANONICAL_VERTICES) ; MRISreverse(mris, REVERSE_X, 0) ; MRISsaveVertexPositions(mris, CANONICAL_VERTICES) ; MRISrestoreVertexPositions(mris, TMP_VERTICES) ; MRIScomputeMetricProperties(mris) ; } #if 0 MRISsaveVertexPositions (mris, CANONICAL_VERTICES) ; // uniform spherical positions #endif if (starting_reg_fname) if (MRISreadVertexPositions(mris, starting_reg_fname) != NO_ERROR) { exit(Gerror) ; } if (multiframes) { if (use_initial_registration) MRISvectorRegister(mris, mrisp_template, &parms, max_passes, min_degrees, max_degrees, nangles) ; parms.l_corr=parms.l_pcorr=0.0f; #if 0 parms.l_dist = 0.0 ; parms.l_corr = 0.0 ; parms.l_parea = 0.0 ; parms.l_area = 0.0 ; parms.l_parea = 0.0f ; parms.l_dist = 0.0 ; parms.l_corr = 0.0f ; parms.l_nlarea = 0.0f ; parms.l_pcorr = 0.0f ; #endif MRISvectorRegister(mris, mrisp_template, &parms, max_passes, min_degrees, max_degrees, nangles) ; } else { double l_dist = parms.l_dist ; if (multi_scale > 0) { int i ; parms.l_dist = l_dist * pow(5.0, (multi_scale-1.0)) ; parms.flags |= IPFLAG_NOSCALE_TOL ; parms.flags &= ~IP_USE_CURVATURE ; for (i = 0 ; i < multi_scale ; i++) { printf("*************** round %d, l_dist = %2.3f **************\n", i, parms.l_dist) ; MRISregister(mris, mrisp_template, &parms, max_passes, min_degrees, max_degrees, nangles) ; parms.flags |= IP_NO_RIGID_ALIGN ; parms.flags &= ~IP_USE_INFLATED ; parms.l_dist /= 5 ; } if (parms.nbhd_size < 0) { parms.nbhd_size *= -1 ; printf("**** starting 2nd epoch, with long-range distances *****\n"); parms.l_dist = l_dist * pow(5.0, (multi_scale-2.0)) ; for (i = 1 ; i < multi_scale ; i++) { printf("*********** round %d, l_dist = %2.3f *************\n", i, parms.l_dist) ; MRISregister(mris, mrisp_template, &parms, max_passes, min_degrees, max_degrees, nangles) ; parms.l_dist /= 5 ; } } printf("****** final curvature registration ***************\n") ; if (parms.nbhd_size > 0) { parms.nbhd_size *= -1 ; // disable long-range stuff } parms.l_dist *= 5 ; parms.flags |= (IP_USE_CURVATURE | IP_NO_SULC); MRISregister(mris, mrisp_template, &parms, max_passes, min_degrees, max_degrees, nangles) ; } else MRISregister(mris, mrisp_template, &parms, max_passes, min_degrees, max_degrees, nangles) ; } if (remove_negative) { parms.niterations = 1000 ; MRISremoveOverlapWithSmoothing(mris,&parms) ; } fprintf(stderr, "writing registered surface to %s...\n", out_fname) ; MRISwrite(mris, out_fname) ; if (jacobian_fname) { MRIScomputeMetricProperties(mris) ; compute_area_ratios(mris) ; /* will put results in v->curv */ #if 0 MRISwriteArea(mris, jacobian_fname) ; #else MRISwriteCurvature(mris, jacobian_fname) ; #endif } msec = TimerStop(&start) ; if (Gdiag & DIAG_SHOW) printf("registration took %2.2f hours\n", (float)msec/(1000.0f*60.0f*60.0f)); MRISPfree(&mrisp_template) ; MRISfree(&mris) ; exit(0) ; return(0) ; /* for ansi */ }