//	VERSION  2B).  //
TRIANGLE* MarchingCubesLinear(float mcMinX, float mcMaxX, float mcMinY, float mcMaxY, float mcMinZ, float mcMaxZ, 
								int ncellsX, int ncellsY, int ncellsZ, float minValue, 
								FORMULA formula, int &numTriangles)
{
	return MarchingCubes(mcMinX, mcMaxX, mcMinY, mcMaxY, mcMinZ, mcMaxZ, ncellsX, ncellsY, ncellsZ, minValue,
		formula, LinearInterp, numTriangles);
}
//	VERSION  2A).  //
TRIANGLE* MarchingCubes(float mcMinX, float mcMaxX, float mcMinY, float mcMaxY, float mcMinZ, float mcMaxZ, 
							int ncellsX, int ncellsY, int ncellsZ, float minValue, 
							FORMULA formula, INTERSECTION intersection, int &numTriangles)
{
	//space is already defined and subdivided, staring with step 3
	//first initialize the points
	mp4Vector * mcDataPoints = new mp4Vector[(ncellsX+1)*(ncellsY+1)*(ncellsZ+1)];
	mpVector stepSize((mcMaxX-mcMinX)/ncellsX, (mcMaxY-mcMinY)/ncellsY, (mcMaxZ-mcMinZ)/ncellsZ);
	
	int YtimesZ = (ncellsY+1)*(ncellsZ+1);	//for extra speed
	for(int i=0; i < ncellsX+1; i++) {
		int ni = i*YtimesZ;						//for speed
		float vertX = mcMinX + i*stepSize.x;
		for(int j=0; j < ncellsY+1; j++) {
			int nj = j*(ncellsZ+1);				//for speed
			float vertY = mcMinY + j*stepSize.y;
			for(int k=0; k < ncellsZ+1; k++) {
				mp4Vector vert(vertX, vertY, mcMinZ + k*stepSize.z, 0);
				vert.val = formula((mpVector)vert);
   /*(step 3)*/ mcDataPoints[ni + nj + k] = vert;
			}
		}
	}
	//then run Marching Cubes (version 1A) on the data
	return MarchingCubes(ncellsX, ncellsY, ncellsZ, minValue, mcDataPoints, intersection, numTriangles);
}
Beispiel #3
0
void MyWidget::paintEvent(QPaintEvent * e) {

    QPainter paint(this);
    memset(_bitsDest, 0, _PIC_MAX);

    initFigure();

    switch (_func_index) {
        case 0: ScalarField();
            break;
        case 1: MarchingCubes();
            break;
    }

    for (int x = 0; x < fR2; x++) {
        for (int y = 0; y < fR2; y++) {
            for (int z = 0; z < fR2; z++) {
                _figure2D[x][y][z].free();
                _figure3D[x][y][z].free();
            }
        }
    }
    paint.drawImage(QPoint(0, 0), *_imageDest);
    e->accept();
}
Beispiel #4
0
	//----------------------------------------------------------------------
    System::System(RTPSSettings* set, CL* c)
    {
	dout<<"settings "<<set<<endl;

        settings = set;
        cli = c;
        acquiredGL=false;
        max_num = settings->GetSettingAs<unsigned int>("max_num_particles");
        num = settings->GetSettingAs<unsigned int>("num_particles");
        maxGravSources=settings->GetSettingAs<unsigned int>("max_gravity_sources");
        activeParticle = 0;
		// I should be able to not specify this, but GPU restrictions ...
        //seed random
        srand ( time(NULL) );

        grid = Domain(settings->GetSettingAs<float4>("domain_min"),settings->GetSettingAs<float4>("domain_max"));

        setupTimers();
        //*** end Initialization
#ifdef CPU
        dout<<"RUNNING ON THE CPU"<<endl;
#endif
#ifdef GPU
        dout<<"RUNNING ON THE GPU"<<endl;
        prepareSorted();

        //dout<<"Here"<<endl;
        //should be more cross platform
        string common_source_dir = settings->GetSettingAs<string>("rtps_path") + "/" + std::string(COMMON_CL_SOURCE_DIR);
        cli->addIncludeDir(common_source_dir);
        //dout<<common_source_dir.c_str()<<endl;

        hash = Hash(common_source_dir, cli, timers["hash_gpu"]);
        gravity = Gravity(common_source_dir, cli);
        bitonic = Bitonic<unsigned int>(common_source_dir, cli );
        //radix = Radix<unsigned int>(common_source_dir, cli, max_num, 128);
        cellindices = CellIndices(common_source_dir, cli, timers["ci_gpu"] );
        permute = Permute( common_source_dir, cli, timers["perm_gpu"] );
        m2p = MeshToParticles(common_source_dir, cli, timers["meshtoparticles_gpu"]);
        marchingcubes = MarchingCubes(common_source_dir, cli, timers["marchingcubes_gpu"],settings->GetSettingAs<unsigned int>("color_field_res","2"));
#endif

    }
/*!
   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);
}
//	VERSION  1B).  //
TRIANGLE* MarchingCubesLinear(int ncellsX, int ncellsY, int ncellsZ, float minValue, 
									mp4Vector * points, int &numTriangles)
{
	return MarchingCubes(ncellsX, ncellsY, ncellsZ, minValue, points, LinearInterp, numTriangles);
}
Beispiel #7
0
void ElPoly::IsoSurf(int NSample,double *IsoLevel,int NIso){
  int NPairF = NFile[1]-NFile[0];
  double OldPos[3] = {pNanoPos(0,0),pNanoPos(0,1),pNanoPos(0,2)};
  double DensEl = CUB(NSample)/(pVol()*NPairF);
  double Dens  = 13.3;
  FILE *FNano = fopen("NanoPos.txt","w");
  //IsoLevel *= NPairF;
  for(int ff=NFile[0];ff<NFile[1];ff+=NPairF){
    double *Plot = (double *)calloc(CUBE(NSample),sizeof(double));
    double Min = 0.;
    double Max = 0.;
    VAR_TRIANGLE *Triang = NULL;
    double Pos[3];
    for(int f=ff;f<ff+NPairF&&f<NFile[1];f++){
      Processing(f);
      if(OpenRisk(cFile[f],BF_PART))return;
      for(int b=0;b<pNBlock();b++){
	for(int p=Block[b].InitIdx;p<Block[b].EndIdx;p++){
	  if(pType(p) != 0)continue;
	  for(int d=0;d<3;d++){
	    Pos[d] = pPos(p,d) - (pNanoPos(0,d)-.5*pEdge(d));
	    Pos[d] -= floor(Pos[d]*pInvEdge(d))*pEdge(d);
	  }
	  int sx = (int)(Pos[0]*pInvEdge(0)*NSample);
	  int sy = (int)(Pos[1]*pInvEdge(1)*NSample);
	  int sz = (int)(Pos[2]*pInvEdge(2)*NSample);
	  int sTot = (sx*NSample+sy)*NSample+sz;
	  Plot[sTot] += DensEl;
	  if(Max < Plot[sTot]) Max = Plot[sTot];
	  if(Min > Plot[sTot]) Min = Plot[sTot];
	}
      }
    }
    Matrice Mask(3,3,3);
    Mask.FillGaussian(.5,3.);
    Mask.Print();
    int NDim = 3;
    int IfMinImConv = 1;
    Mask.ConvoluteMatrix(Plot,NSample,NDim,IfMinImConv);
    Mask.ConvoluteMatrix(Plot,NSample,NDim,IfMinImConv);
    // ConvoluteMatrix(Plot,NSample,&Mask,3);
    // ConvoluteMatrix(Plot,NSample,&Mask,3);
    char FString[256];
    sprintf(FString,"IsoSurf%05d.dat",ff);
    FILE *F2Write = fopen(FString,"w");
    fprintf(F2Write,"#l(%lf %lf %lf) v[%d] d[polygon]\n",pEdge(0),pEdge(1),pEdge(2),NSample);
    HeaderNano(F2Write);
    int NTri = 0;
    for(int i=0;i<NIso;i++){
      printf("Min %lf Max %lf IsoLevel %lf DensEl %lf\n",Min,Max,IsoLevel[i],DensEl);
      Triang = MarchingCubes(Plot,NSample,IsoLevel[i],&NTri);
      for(int t=0;t<NTri;t++){
	for(int i=0;i<3;i++){
	  int l1 = t*3 + (i+1)%3;
	  int l2 = t*3 + (i+2)%3;
	  for(int d=0;d<3;d++) Pos[d] = Triang[t].p[i].x[d];
	  int sx = (int)(Pos[0]*pInvEdge(0)*NSample);
	  int sy = (int)(Pos[1]*pInvEdge(1)*NSample);
	  int sz = (int)(Pos[2]*pInvEdge(2)*NSample);
	  int sTot = (sx*NSample+sy)*NSample+sz;
	  fprintf(F2Write,"{t[%d %d %d] ",sTot,t,0);
	  fprintf(F2Write,"x(%lf %lf %lf) ",Pos[0],Pos[1],Pos[2]);
	  fprintf(F2Write,"v(%lf %lf %lf) ",Triang[t].n[i].x[0],Triang[t].n[i].x[1],Triang[t].n[i].x[2]);
	  fprintf(F2Write,"l[%d] l[%d]}\n",l1,l2);
	}
      }
    }
    fclose(F2Write);
    free(Plot);
    free(Triang);continue;
    int NVertex = CUBE(2*NSample-1);
    double *WeightL = (double *) calloc(NVertex,sizeof(double));
    NormalWeight(Triang,WeightL,NSample,NTri);
    double CmStalk[3] = {0.,0.,0.};//OldPos[0],OldPos[1],OldPos[2]};
    double CountStalk = 0.;
    for(int t=0;t<NTri;t++){
      for(int v=0;v<3;v++){
	int vRef = Triang[t].v[v];
	for(int d=0;d<3;d++){
	  CmStalk[d] = Triang[t].p[v].x[d]*WeightL[vRef];
	}
	CountStalk += WeightL[vRef];
      }
    }
    free(WeightL);
    free(Triang);
    if(CountStalk <= 0.) CountStalk = 1.;
    for(int d=0;d<3;d++){
      CmStalk[d] /= CountStalk;
    }
    pPos(CmStalk);
    SetNNano(1);
    for(int d=0;d<3;d++){
      Nano->Pos[d]   = CmStalk[d];
      OldPos[d] = Nano->Pos[d];
    }
    SetNanoBkf(0);
    Nano->Axis[0] = 0.;
    Nano->Axis[1] = 0.;
    Nano->Axis[2] = 1.;
    Nano->Rad     = .5;
    Nano->Height  = 4.;
    Nano->Hamaker = 1.;
    Nano->OffSet  = 1.;
    Nano->Shape = SHAPE_STALK;    
    for(int f=ff;f<ff+NPairF&&f<NFile[1];f++){
      char String[120];
      StringNano(String,0);
      fprintf(FNano,"sed '/Rigid/c\\%s' %s > Ciccia.dat\n",String,cFile[f]);
      fprintf(FNano,"mv Ciccia.dat %s\n",cFile[f]);
      //command("chmod u+x %s\n","NanoPos.txt");
      //SubNanoHeader(cFile[f]);
    }
    printf("\n");
  }
  fclose(FNano);
}