static int rip_bad_vertices(MRI_SURFACE *mris, MRI *mri_profiles) { int unknown_index, bad, i, vno, index ; VERTEX *v ; CTABfindName(mris->ct, "Unknown", &unknown_index) ; printf("unknown index = %d\n", unknown_index) ; for (vno = 0 ; vno < mris->nvertices ; vno++) { v = &mris->vertices[vno] ; if (vno == Gdiag_no) DiagBreak() ; if (v->ripflag) continue ; CTABfindAnnotation(mris->ct, v->annotation, &index); if (index == unknown_index) { v->annotation = 0 ; v->ripflag = 1 ; } else { bad = 1 ; for (i = 0 ; i < mri_profiles->nframes ; i++) { if (!FZERO(MRIgetVoxVal(mri_profiles, vno, 0, 0, i))) bad = 0 ; } if (bad) { v->ripflag = 1 ; v->annotation = 0 ; } } } MRISripFaces(mris) ; MRIScomputeMetricProperties(mris) ; return(NO_ERROR) ; }
/* expects the two surfaces to have the CANONICAL_VERTICES field set to the sphere.reg positions. */ int MRISmapCuts(MRI_SURFACE *mris_in, MRI_SURFACE *mris_out) { MHT *mht_in, *mht_out ; int vno_out /*, vno_in, fno_in, fno_out, n, ripflag*/ ; VERTEX *v_out, *v_in ; #if 0 FACE *f_in, *f_out ; #endif MRISstoreRipFlags(mris_in) ; MRISunrip(mris_in) ; mht_in = MHTfillVertexTable(mris_in, NULL, CANONICAL_VERTICES) ; mht_out = MHTfillVertexTable(mris_out, NULL, CANONICAL_VERTICES) ; #if 0 for (vno_in = 0 ; vno_in < mris_in->nvertices ; vno_in++) { if (vno_in == Gdiag_no) DiagBreak() ; v_in = &mris_in->vertices[vno_in] ; if (v_in->oripflag == 0) continue ; v_out = MHTfindClosestVertexInTable(mht_out, mris_out, v_in->cx, v_in->cy, v_in->cz, 1) ; if (v_out == NULL) DiagBreak() ; else v_out->ripflag = 1 ; } for (vno_out = 0 ; vno_out < mris_out->nvertices ; vno_out++) { if (vno_out == Gdiag_no) DiagBreak() ; v_out = &mris_out->vertices[vno_out] ; v_in = MHTfindClosestVertexInTable(mht_in, mris_in, v_out->cx, v_out->cy, v_out->cz, 1) ; if (v_in == NULL) DiagBreak() ; else if (v_in->oripflag) v_out->ripflag = 1 ; } for (fno_in = 0 ; fno_in < mris_in->nfaces ; fno_in++) { f_in = &mris_in->faces[fno_in] ; if (f_in->ripflag == 0) continue ; for (n = 0 ; n < VERTICES_PER_FACE ; n++) { vno_in = f_in->v[n] ; if (vno_in == Gdiag_no) DiagBreak() ; v_in = &mris_in->vertices[vno_in] ; v_out = MHTfindClosestVertexInTable(mht_out, mris_out, v_in->cx, v_in->cy, v_in->cz, 1) ; if (v_out == NULL) DiagBreak() ; else v_out->ripflag = 1 ; } } for (fno_out = 0 ; fno_out < mris_out->nfaces ; fno_out++) { f_out = &mris_out->faces[fno_out] ; ripflag = 0 ; for (n = 0 ; n < VERTICES_PER_FACE ; n++) { vno_out = f_out->v[n] ; if (vno_out == Gdiag_no) DiagBreak() ; v_out = &mris_out->vertices[vno_out] ; v_in = MHTfindClosestVertexInTable(mht_in, mris_in, v_out->cx, v_out->cy, v_out->cz, 1) ; if (v_in == NULL) DiagBreak() ; else if (v_in->ripflag) ripflag = 1 ; } f_out->ripflag = ripflag ; } for (fno_out = 0 ; fno_out < mris_out->nfaces ; fno_out++) { double cx, cy, cz ; int n ; f_out = &mris_out->faces[fno_out] ; ripflag = 0 ; cx = cy = cz = 0.0 ; for (n = 0 ; n < VERTICES_PER_FACE ; n++) { vno_out = f_out->v[n] ; if (vno_out == Gdiag_no) DiagBreak() ; v_out = &mris_out->vertices[vno_out] ; cx += v_out->cx ; cy += v_out->cy ; cz += v_out->cz ; } cx /= VERTICES_PER_FACE ; cy /= VERTICES_PER_FACE ; cz /= VERTICES_PER_FACE ; v_in = MHTfindClosestVertexInTable(mht_in, mris_in, cx, cy, cz, 1) ; if (v_in == NULL) DiagBreak() ; else if (v_in->ripflag) { f_out->ripflag = 1 ; for (n = 0 ; n < VERTICES_PER_FACE ; n++) mris_out->vertices[f_out->v[n]].ripflag = 1 ; } } #endif #define STEP_SIZE 0.05 for (vno_out = 0 ; vno_out < mris_out->nvertices ; vno_out++) { double d, dx, dy, dz ; int n ; VERTEX *vn ; v_out = &mris_out->vertices[vno_out] ; for (n = 0 ; n < v_out->vnum ; n++) { vn = &mris_out->vertices[v_out->v[n]] ; dx = vn->cx - v_out->cx ; dy = vn->cy - v_out->cy ; dz = vn->cz - v_out->cz ; for (d = 0.0 ; d <= 1.0 ; d+= STEP_SIZE) { v_in = MHTfindClosestVertexInTable(mht_in, mris_in, v_out->cx+dx*d, v_out->cy+dy*d, v_out->cz+dz*d, 1) ; if (v_in == NULL) DiagBreak() ; else if (v_in->oripflag) { v_out->ripflag = 1 ; break ; } } } } MRISripFaces(mris_out) ; MRISrestoreRipFlags(mris_in) ; MHTfree(&mht_in) ; MHTfree(&mht_out) ; return(NO_ERROR) ; }
int main(int argc, char *argv[]) { int ac, nargs ; char **av, *cp, surf_name[100], *hemi, *subject_name, *label_name, *out_fname ; MRI_SURFACE *mris ; LABEL *label ; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: label2patch.c,v 1.3 2011/03/02 00:04:11 nicks Exp $", "$Name: stable5 $"); if (nargs && argc - nargs == 1) exit (0); argc -= nargs; Progname = argv[0] ; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; /* read in command-line options */ ac = argc ; av = argv ; for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++) { nargs = get_option(argc, argv) ; argc -= nargs ; argv += nargs ; } if (argc < 4) print_usage() ; subject_name = argv[1] ; hemi = argv[2] ; label_name = argv[3] ; out_fname = argv[4] ; if (strlen(subjects_dir) == 0) { cp = getenv("SUBJECTS_DIR") ; if (!cp) ErrorExit(ERROR_BADPARM, "no subjects directory in environment.\n") ; strcpy(subjects_dir, cp) ; } sprintf(surf_name,"%s/%s/surf/%s.inflated",subjects_dir,subject_name,hemi); fprintf(stderr, "reading %s...\n", surf_name) ; mris = MRISread(surf_name) ; if (!mris) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s\n", surf_name) ; MRIScomputeMetricProperties(mris) ; label = LabelRead(subject_name, label_name) ; if (ndilate) LabelDilate(label, mris, ndilate) ; if (nerode) LabelErode(label, mris, nerode) ; if (nclose) { LabelDilate(label, mris, nclose) ; LabelErode(label, mris, nclose) ; } LabelRipRestOfSurface(label, mris) ; MRISripFaces(mris) ; MRISwritePatch(mris, out_fname) ; if (verbose) fprintf(stderr, "done.\n") ; exit(0) ; return(0) ; /* ansi */ }
int main(int argc, char *argv[]) { char **av, in_surf_fname[STRLEN], *in_patch_fname, *out_patch_fname, fname[STRLEN], path[STRLEN], *cp, hemi[10] ; int ac, nargs ; MRI_SURFACE *mris ; MRI *mri_vertices ; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, "$Id: mris_flatten.c,v 1.42 2016/12/10 22:57:46 fischl Exp $", "$Name: $"); if (nargs && argc - nargs == 1) exit (0); argc -= nargs; Gdiag |= DIAG_SHOW ; Progname = argv[0] ; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; Gdiag |= (DIAG_SHOW | DIAG_WRITE) ; memset(&parms, 0, sizeof(parms)) ; parms.dt = .1 ; parms.projection = PROJECT_PLANE ; parms.tol = 0.2 ; parms.n_averages = 1024 ; parms.l_dist = 1.0 ; parms.l_nlarea = 1.0 ; parms.niterations = 40 ; parms.area_coef_scale = 1.0 ; parms.dt_increase = 1.01 /* DT_INCREASE */; parms.dt_decrease = 0.98 /* DT_DECREASE*/ ; parms.error_ratio = 1.03 /*ERROR_RATIO */; parms.integration_type = INTEGRATE_LINE_MINIMIZE ; parms.momentum = 0.9 ; parms.desired_rms_height = -1.0 ; parms.base_name[0] = 0 ; parms.nbhd_size = 7 ; /* out to 7-connected neighbors */ parms.max_nbrs = 12 ; /* 12 at each distance */ ac = argc ; av = argv ; for ( ; argc > 1 && ISOPTION(*argv[1]) ; argc--, argv++) { nargs = get_option(argc, argv) ; argc -= nargs ; argv += nargs ; } if (argc < 3) print_help() ; parms.base_dt = base_dt_scale * parms.dt ; in_patch_fname = argv[1] ; out_patch_fname = argv[2] ; FileNamePath(in_patch_fname, path) ; cp = strrchr(in_patch_fname, '/') ; if (!cp) cp = in_patch_fname ; cp = strchr(cp, '.') ; if (cp) { strncpy(hemi, cp-2, 2) ; hemi[2] = 0 ; } else strcpy(hemi, "lh") ; if (one_surf_flag) sprintf(in_surf_fname, "%s", in_patch_fname) ; else sprintf(in_surf_fname, "%s/%s.%s", path, hemi, original_surf_name) ; if (parms.base_name[0] == 0) { FileNameOnly(out_patch_fname, fname) ; cp = strchr(fname, '.') ; if (cp) strcpy(parms.base_name, cp+1) ; else strcpy(parms.base_name, "flattened") ; } mris = MRISread(in_surf_fname) ; if (!mris) ErrorExit(ERROR_NOFILE, "%s: could not read surface file %s", Progname, in_surf_fname) ; if (sphere_flag) { MRIScenter(mris, mris) ; mris->radius = MRISaverageRadius(mris) ; MRISstoreMetricProperties(mris) ; MRISsaveVertexPositions(mris, ORIGINAL_VERTICES) ; } if (Gdiag_no >= 0) { int n ; printf("vertex %d has %d nbrs before patch:\n", Gdiag_no, mris->vertices[Gdiag_no].vnum) ; for (n = 0 ; n < mris->vertices[Gdiag_no].vnum ; n++) printf("\t%d\n", mris->vertices[Gdiag_no].v[n]) ; } if (one_surf_flag) /* only have the 1 surface - no patch file */ { mris->patch = 1 ; mris->status = MRIS_PATCH ; if (!FEQUAL(rescale,1)) { MRISscaleBrain(mris, mris, rescale) ; MRIScomputeMetricProperties(mris) ; } MRISstoreMetricProperties(mris) ; MRISsaveVertexPositions(mris, ORIGINAL_VERTICES) ; } else { MRISresetNeighborhoodSize(mris, mris->vertices[0].nsize) ; // set back to max if (label_fname) // read in a label instead of a patch { LABEL *area ; area = LabelRead(NULL, label_fname) ; if (area == NULL) ErrorExit(ERROR_BADPARM, "%s: could not read label file %s", Progname, label_fname) ; LabelDilate(area, mris, dilate_label, CURRENT_VERTICES) ; MRISclearMarks(mris) ; LabelMark(area, mris) ; MRISripUnmarked(mris) ; MRISripFaces(mris); mris->patch = 1 ; mris->status = MRIS_CUT ; LabelFree(&area) ; printf("%d valid vertices (%2.1f %% of total)\n", MRISvalidVertices(mris), 100.0*MRISvalidVertices(mris)/mris->nvertices) ; } else { if (MRISreadPatch(mris, in_patch_fname) != NO_ERROR) ErrorExit(ERROR_BADPARM, "%s: could not read patch file %s", Progname, in_patch_fname) ; if (dilate) { printf("dilating patch %d times\n", dilate) ; MRISdilateRipped(mris, dilate) ; printf("%d valid vertices (%2.1f %% of total)\n", MRISvalidVertices(mris), 100.0*MRISvalidVertices(mris)/mris->nvertices) ; } } MRISremoveRipped(mris) ; MRISupdateSurface(mris) ; #if 0 mris->nsize = 1 ; // before recalculation of 2 and 3-nbrs { int vno ; VERTEX *v ; for (vno= 0 ; vno < mris->nvertices ; vno++) { v = &mris->vertices[vno] ; v->vtotal = v->vnum ; v->nsize = 1 ; } } MRISsetNeighborhoodSize(mris, nbrs) ; #endif } if (Gdiag_no >= 0) printf("vno %d is %sin patch\n", Gdiag_no, mris->vertices[Gdiag_no].ripflag ? "NOT " : "") ; if (Gdiag_no >= 0 && mris->vertices[Gdiag_no].ripflag == 0) { int n ; printf("vertex %d has %d nbrs after patch:\n", Gdiag_no, mris->vertices[Gdiag_no].vnum) ; for (n = 0 ; n < mris->vertices[Gdiag_no].vnum ; n++) printf("\t%d\n", mris->vertices[Gdiag_no].v[n]) ; } fprintf(stderr, "reading original vertex positions...\n") ; if (!FZERO(disturb)) mrisDisturbVertices(mris, disturb) ; if (parms.niterations > 0) { MRISresetNeighborhoodSize(mris, nbrs) ; if (!FZERO(parms.l_unfold) || !FZERO(parms.l_expand)) { static INTEGRATION_PARMS p2 ; sprintf(in_surf_fname, "%s/%s.%s", path, hemi, original_surf_name) ; if (stricmp(original_unfold_surf_name,"none") == 0) { printf("using current position of patch as initial position\n") ; MRISstoreMetricProperties(mris) ; /* use current positions */ } else if (!sphere_flag && !one_surf_flag) MRISreadOriginalProperties(mris, original_unfold_surf_name) ; *(&p2) = *(&parms) ; p2.l_dist = 0 ; p2.niterations = 100 ; p2.nbhd_size = p2.max_nbrs = 1 ; p2.n_averages = 0 ; p2.write_iterations = parms.write_iterations > 0 ? 25 : 0 ; p2.tol = -1 ; p2.dt = 0.5 ; p2.l_area = 0.0 ; p2.l_spring = 0.9 ; p2.l_convex = 0.9 ; p2.momentum = 0 ; p2.integration_type = INTEGRATE_MOMENTUM ; MRISrestoreVertexPositions(mris, ORIGINAL_VERTICES) ; #if 0 p2.flags |= IPFLAG_NO_SELF_INT_TEST ; printf("expanding surface....\n") ; MRISexpandSurface(mris, 4.0, &p2) ; // push it away from fissure #endif p2.niterations = 100 ; MRISunfold(mris, &p2, 1) ; p2.niterations = 300 ; p2.l_unfold *= 0.25 ; MRISunfold(mris, &p2, 1) ; p2.l_unfold *= 0.25 ; MRISunfold(mris, &p2, 1) ; #if 0 printf("smoothing unfolded surface..\n"); p2.niterations = 200 ; p2.l_unfold = 0 ; // just smooth it MRISunfold(mris, &p2, max_passes) ; #endif parms.start_t = p2.start_t ; parms.l_unfold = parms.l_convex = parms.l_boundary = parms.l_expand=0 ; MRIfree(&parms.mri_dist) ; } sprintf(in_surf_fname, "%s/%s.%s", path, hemi, original_surf_name) ; if (!sphere_flag && !one_surf_flag) MRISreadOriginalProperties(mris, original_surf_name) ; if (randomly_flatten) MRISflattenPatchRandomly(mris) ; else MRISflattenPatch(mris) ; /* optimize metric properties of flat map */ fprintf(stderr,"minimizing metric distortion induced by projection...\n"); MRISscaleBrain(mris, mris, scale) ; MRIScomputeMetricProperties(mris) ; MRISunfold(mris, &parms, max_passes) ; MRIScenter(mris, mris) ; fprintf(stderr, "writing flattened patch to %s\n", out_patch_fname) ; MRISwritePatch(mris, out_patch_fname) ; } if (plane_flag || sphere_flag) { char fname[STRLEN] ; FILE *fp ; #if 0 sprintf(fname, "%s.%s.out", mris->hemisphere == RIGHT_HEMISPHERE ? "rh" : "lh", parms.base_name); #else sprintf(fname, "flatten.log") ; #endif fp = fopen(fname, "a") ; if (plane_flag) MRIScomputeAnalyticDistanceError(mris, MRIS_PLANE, fp) ; else if (sphere_flag) MRIScomputeAnalyticDistanceError(mris, MRIS_SPHERE, fp) ; fclose(fp) ; } if (mri_overlay) { MRI *mri_flattened ; char fname[STRLEN] ; // if it is NxNx1x1 reshape it to be Nx1x1xN if ( mri_overlay->width == mri_overlay->height && mri_overlay->depth == 1 && mri_overlay->nframes == 1) { MRI *mri_tmp ; printf("reshaping to move 2nd dimension to time\n") ; mri_tmp = mri_reshape( mri_overlay, mri_overlay->width, 1, 1, mri_overlay->height); MRIfree( &mri_overlay ); mri_overlay = mri_tmp; } // put in some special code that knows about icosahedra if (mris->nvertices == 163842 || // ic7 mris->nvertices == 40962 || // ic6 mris->nvertices == 10242 || // ic5 mris->nvertices == 2562) // ic4 { int nvals, start_index, end_index ; MRI *mri_tmp ; printf("cross-hemispheric correlation matrix detected, reshaping...\n") ; nvals = mri_overlay->width * mri_overlay->height * mri_overlay->depth ; if (nvals == 2*mris->nvertices) // it's a corr matrix for both hemis { if (mris->hemisphere == LEFT_HEMISPHERE || mris->hemisphere == RIGHT_HEMISPHERE) { if (mris->hemisphere == LEFT_HEMISPHERE) { start_index = 0 ; end_index = mris->nvertices-1 ; } else { start_index = mris->nvertices ; end_index = 2*mris->nvertices-1 ; } mri_tmp = MRIextract(mri_overlay, NULL, start_index, 0, 0, mris->nvertices, 1, 1) ; MRIfree(&mri_overlay) ; mri_overlay = mri_tmp; } else // both hemis { } } } printf("resampling overlay (%d x %d x %d x %d) into flattened coordinates..\n", mri_overlay->width, mri_overlay->height, mri_overlay->depth, mri_overlay->nframes) ; if (synth_name) { LABEL *area_lh, *area_rh ; char fname[STRLEN], path[STRLEN], fname_no_path[STRLEN] ; int vno, n, vno2, n2 ; MRIsetValues(mri_overlay, 0) ; FileNameOnly(synth_name, fname_no_path) ; FileNamePath(synth_name, path) ; sprintf(fname, "%s/lh.%s", path, fname_no_path) ; area_lh = LabelRead(NULL, fname) ; if (area_lh == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read label from %s", Progname,fname) ; sprintf(fname, "%s/rh.%s", path, fname_no_path) ; area_rh = LabelRead(NULL, fname) ; if (area_rh == NULL) ErrorExit(ERROR_NOFILE, "%s: could not read label from %s", Progname,fname) ; #if 0 for (n = 0 ; n < area_lh->n_points ; n++) { vno = area_lh->lv[n].vno ; MRIsetVoxVal(mri_overlay, vno, 0, 0, vno, 1) ; printf("synthesizing map with vno %d: (%2.1f, %2.1f)\n", vno, mris->vertices[vno].x, mris->vertices[vno].y) ; break ; } #else for (n = 0 ; n < area_lh->n_points ; n++) { vno = area_lh->lv[n].vno ; if (vno >= 0) { for (n2 = 0 ; n2 < area_lh->n_points ; n2++) { vno2 = area_lh->lv[n2].vno ; if (vno2 >= 0) MRIsetVoxVal(mri_overlay, vno, 0, 0, vno2, 1) ; } for (n2 = 0 ; n2 < area_rh->n_points ; n2++) { vno2 = area_rh->lv[n2].vno ; if (vno2 >= 0) MRIsetVoxVal(mri_overlay, vno, 0, 0, mris->nvertices+vno2, 1) ; } } } #endif } mri_flattened = MRIflattenOverlay(mris, mri_overlay, NULL, 1.0, label_overlay, &mri_vertices) ; printf("writing flattened overlay to %s\n", out_patch_fname) ; MRIwrite(mri_flattened, out_patch_fname) ; MRIfree(&mri_flattened) ; FileNameRemoveExtension(out_patch_fname, fname) ; strcat(fname, ".vnos.mgz") ; printf("writing flattened vertex #s to %s\n", fname) ; MRIwrite(mri_vertices, fname) ; MRIfree(&mri_vertices) ; } #if 0 sprintf(fname, "%s.area_error", out_fname) ; printf("writing area errors to %s\n", fname) ; MRISwriteAreaError(mris, fname) ; sprintf(fname, "%s.angle_error", out_fname) ; printf("writing angle errors to %s\n", fname) ; MRISwriteAngleError(mris, fname) ; MRISfree(&mris) ; #endif exit(0) ; return(0) ; /* for ansi */ }