std::vector<double> Histogram::compute_data(const std::vector<double> *sorted_data, int n_bins, const it_dbl begin, const it_dbl end, double min_value, double step ) { if(n_bins == 1) { std::vector<double> result; result.push_back(end - begin); return result; } int offset = n_bins % 2; n_bins /= 2; double mid_value = min_value + n_bins * step; it_dbl mid = std::upper_bound(begin, end, mid_value); std::vector<double> left_data = compute_data(sorted_data, n_bins, begin, mid, min_value, step); std::vector<double> right_data = compute_data(sorted_data, n_bins + offset, mid, end, min_value + n_bins * step, step); left_data.insert(left_data.end(), right_data.begin(), right_data.end()); return left_data; }
/* Return a subfield, gen_0 [ change p ] or NULL [ not a subfield ] */ static GEN subfield(GEN A, blockdata *B) { long N, i, j, d, lf, m = lg(A)-1; GEN M, pe, pol, fhk, g, e, d_1_term, delta, listdelta, whichdelta; GEN T = B->S->T, p = B->S->p, firstroot = B->S->firstroot; pol= (GEN)B->DATA[1]; N = degpol(pol); d = N/m; /* m | N */ pe = (GEN)B->DATA[2]; fhk= (GEN)B->DATA[3]; M = (GEN)B->DATA[8]; delta = cgetg(m+1,t_VEC); whichdelta = cgetg(N+1, t_VECSMALL); d_1_term = gen_0; for (i=1; i<=m; i++) { GEN Ai = gel(A,i), p1 = (GEN)fhk[Ai[1]]; for (j=2; j<=d; j++) p1 = Fq_mul(p1, (GEN)fhk[Ai[j]], T, pe); gel(delta,i) = p1; if (DEBUGLEVEL>2) fprintferr("delta[%ld] = %Z\n",i,p1); /* g = prod (X - delta[i]) * if g o h = 0 (pol), we'll have h(Ai[j]) = delta[i] for all j */ /* fk[k] belongs to block number whichdelta[k] */ for (j=1; j<=d; j++) whichdelta[Ai[j]] = i; if (typ(p1) == t_POL) p1 = constant_term(p1); d_1_term = addii(d_1_term, p1); } d_1_term = centermod(d_1_term, pe); /* Tr(g) */ if (absi_cmp(d_1_term, gel(M,3)) > 0) { if (DEBUGLEVEL>1) fprintferr("d-1 test failed\n"); return NULL; } g = FqV_roots_to_pol(delta, T, pe, 0); g = centermod(polsimplify(g), pe); /* assume g in Z[X] */ if (DEBUGLEVEL>2) fprintferr("pol. found = %Z\n",g); if (!ok_coeffs(g,M)) { if (DEBUGLEVEL>1) fprintferr("coeff too big for pol g(x)\n"); return NULL; } if (!FpX_is_squarefree(g, p)) { if (DEBUGLEVEL>1) fprintferr("changing f(x): p divides disc(g)\n"); compute_data(B); return subfield(A, B); } lf = lg(firstroot); listdelta = cgetg(lf, t_VEC); for (i=1; i<lf; i++) listdelta[i] = delta[whichdelta[firstroot[i]]]; if (DEBUGLEVEL) fprintferr("candidate = %Z\n", g); e = embedding(g, B->DATA, B->S, B->PD->den, listdelta); if (!e) return NULL; if (DEBUGLEVEL) fprintferr("embedding = %Z\n", e); return _subfield(g, e); }
Histogram::Histogram(std::vector<double> *sorted_data, int n_bins, bool normalize) { double min_value, max_value, step; data = new std::vector<double>(); if(sorted_data->empty()) { data->push_back(0); return; } max_value = sorted_data->at(sorted_data->size() - 1); min_value = sorted_data->at(0); step = (max_value - min_value) / n_bins; *data = compute_data(sorted_data, n_bins, sorted_data->begin(), sorted_data->end(), min_value, step); }
/* subfields of degree d */ static GEN subfields_of_given_degree(blockdata *B) { pari_sp av = avma; GEN L; if (DEBUGLEVEL) fprintferr("\n* Look for subfields of degree %ld\n\n", B->d); B->DATA = NULL; compute_data(B); L = calc_block(B, B->S->Z, cgetg(1,t_VEC), NULL); if (DEBUGLEVEL) fprintferr("\nSubfields of degree %ld: %Z\n", B->d, L); if (isclone(B->DATA)) gunclone(B->DATA); avma = av; return L; }
//_____________________________________________________________________________ // main function int main (int argc, char **argv) //----------------------------------------------------------------------------- { MarchingCubes mc ; mc.set_resolution( 60,60,60 ) ; mc.init_all() ; compute_data( mc ) ; mc.run() ; mc.clean_temps() ; mc.writePLY("test.ply") ; mc.clean_all() ; return 0 ; }
Histogram::Histogram(std::vector<double> *sorted_data, int n_bins, double min_value, double max_value) { double step = (max_value - min_value) / n_bins; data = new std::vector<double>(); *data = compute_data(sorted_data, n_bins, sorted_data->begin(), sorted_data->end(), min_value, step); }
/*! A function version of the program mc by Thomas Lewiner see main.c in ./MarchingCubes */ SUMA_SurfaceObject *SUMA_MarchingCubesSurface( SUMA_GENERIC_PROG_OPTIONS_STRUCT * Opt) { static char FuncName[]={"SUMA_MarchingCubesSurface"}; SUMA_SurfaceObject *SO=NULL; int nxx, nyy, nzz, cnt, i, j, k, *FaceSetList=NULL; float *NodeList=NULL; SUMA_NEW_SO_OPT *nsoopt = NULL; THD_fvec3 fv, iv; MCB *mcp ; SUMA_ENTRY; if (Opt->obj_type < 0) { nxx = DSET_NX(Opt->in_vol); nyy = DSET_NY(Opt->in_vol); nzz = DSET_NZ(Opt->in_vol); if (Opt->debug) { fprintf(SUMA_STDERR, "%s:\nNxx=%d\tNyy=%d\tNzz=%d\n", FuncName, nxx, nyy, nzz); } mcp = MarchingCubes(-1, -1, -1); set_resolution( mcp, nxx, nyy, nzz ) ; init_all(mcp) ; if (Opt->debug) fprintf(SUMA_STDERR,"%s:\nSetting data...\n", FuncName); cnt = 0; for( k = 0 ; k < mcp->size_z ; k++ ) { for( j = 0 ; j < mcp->size_y ; j++ ) { for( i = 0 ; i < mcp->size_x ; i++ ) { SUMA_SET_MC_DATA ( mcp, Opt->mcdatav[cnt], i, j, k); ++cnt; } } } } else { /* built in */ nxx = nyy = nzz = Opt->obj_type_res; mcp = MarchingCubes(-1, -1, -1); set_resolution( mcp, nxx, nyy, nzz) ; init_all(mcp) ; compute_data( *mcp , Opt->obj_type) ; } if (Opt->debug) fprintf(SUMA_STDERR,"%s:\nrunning MarchingCubes...\n", FuncName); run(mcp) ; clean_temps(mcp) ; if (Opt->debug > 1) { fprintf(SUMA_STDERR,"%s:\nwriting out NodeList and FaceSetList...\n", FuncName); write1Dmcb(mcp); } if (Opt->debug) { fprintf(SUMA_STDERR,"%s:\nNow creating SO...\n", FuncName); } NodeList = (float *)SUMA_malloc(sizeof(float)*3*mcp->nverts); FaceSetList = (int *)SUMA_malloc(sizeof(int)*3*mcp->ntrigs); if (!NodeList || !FaceSetList) { SUMA_SL_Crit("Failed to allocate!"); SUMA_RETURN(SO); } nsoopt = SUMA_NewNewSOOpt(); if (Opt->obj_type < 0) { nsoopt->LargestBoxSize = -1; if (Opt->debug) { fprintf(SUMA_STDERR, "%s:\nCopying vertices, changing to DICOM \n" "Orig:(%f %f %f) \nD:(%f %f %f)...\n", FuncName, DSET_XORG(Opt->in_vol), DSET_YORG(Opt->in_vol), DSET_ZORG(Opt->in_vol), DSET_DX(Opt->in_vol), DSET_DY(Opt->in_vol), DSET_DZ(Opt->in_vol)); } for ( i = 0; i < mcp->nverts; i++ ) { j = 3*i; /* change from index coordinates to mm DICOM, next three lines are equivalent of SUMA_THD_3dfind_to_3dmm*/ fv.xyz[0] = DSET_XORG(Opt->in_vol) + mcp->vertices[i].x * DSET_DX(Opt->in_vol); fv.xyz[1] = DSET_YORG(Opt->in_vol) + mcp->vertices[i].y * DSET_DY(Opt->in_vol); fv.xyz[2] = DSET_ZORG(Opt->in_vol) + mcp->vertices[i].z * DSET_DZ(Opt->in_vol); /* change mm to RAI coords */ iv = SUMA_THD_3dmm_to_dicomm( Opt->in_vol->daxes->xxorient, Opt->in_vol->daxes->yyorient, Opt->in_vol->daxes->zzorient, fv ); NodeList[j ] = iv.xyz[0]; NodeList[j+1] = iv.xyz[1]; NodeList[j+2] = iv.xyz[2]; } for ( i = 0; i < mcp->ntrigs; i++ ) { j = 3*i; FaceSetList[j ] = mcp->triangles[i].v3; FaceSetList[j+1] = mcp->triangles[i].v2; FaceSetList[j+2] = mcp->triangles[i].v1; } } else { nsoopt->LargestBoxSize = 100; /* built in */ for ( i = 0; i < mcp->nverts; i++ ) { j = 3*i; NodeList[j ] = mcp->vertices[i].x; NodeList[j+1] = mcp->vertices[i].y; NodeList[j+2] = mcp->vertices[i].z; } for ( i = 0; i < mcp->ntrigs; i++ ) { j = 3*i; FaceSetList[j ] = mcp->triangles[i].v3; FaceSetList[j+1] = mcp->triangles[i].v2; FaceSetList[j+2] = mcp->triangles[i].v1; } } SO = SUMA_NewSO(&NodeList, mcp->nverts, &FaceSetList, mcp->ntrigs, nsoopt); if (Opt->obj_type < 0) { /* not sure if anything needs to be done here ...*/ } else { if (Opt->obj_type == 0) SO->normdir = 1; else SO->normdir = -1; } if (Opt->debug) { fprintf(SUMA_STDERR,"%s:\nCleaning mcp...\n", FuncName); } clean_all(mcp) ; free(mcp); nsoopt=SUMA_FreeNewSOOpt(nsoopt); SUMA_RETURN(SO); }