Esempio n. 1
0
int main()
{
	lng i, j, k, cpt=0,NmbVer, NmbTri, NmbItm, MshIdx, ver, dim, ref, idx, (*TriTab)[3], buf[ BufSiz ], inc=50;
	long long OctIdx;
	double crd1[3] = {0,0,0}, crd2[3] = {0.002928, 0.079575, 0.006978}, crd3[3] = {-.0054, .1488, -.0067};
	double dis, (*VerTab)[3], MinCrd[3], MaxCrd[3], IncCrd[3], AvgDis=0;
	time_t t, t2, MinTim = INT_MAX, MaxTim = 0;


	/*---------------------------------------*/
	/* Open, allocate and read the mesh file */
	/*---------------------------------------*/

	t = clock();
	puts("\nRead mesh");

	if(!(MshIdx = GmfOpenMesh("test.meshb", GmfRead, &ver, &dim)))
	{
		puts("Cannot open test.meshb.");
		return(1);
	}

	NmbVer = GmfStatKwd(MshIdx, GmfVertices);
	NmbTri = GmfStatKwd(MshIdx, GmfTriangles);
	printf("vertices = %d, triangles = %d, dimension = %d, version = %d\n", NmbVer, NmbTri, dim, ver);

	if( !NmbVer || (dim != 3) )
	{
		puts("Incompatible mesh.");
		return(1);
	}

	VerTab = malloc((NmbVer+1) * 3 * sizeof(double));
	GmfGotoKwd(MshIdx, GmfVertices);
	GmfGetBlock(MshIdx, GmfVertices, GmfDouble, &VerTab[1][0], &VerTab[2][0], GmfDouble, &VerTab[1][1], &VerTab[2][1], \
				GmfDouble, &VerTab[1][2], &VerTab[2][2], GmfLong, &ref, &ref);

	if(NmbTri)
	{
		TriTab = malloc((NmbTri+1) * 3 * sizeof(lng));
		GmfGotoKwd(MshIdx, GmfTriangles);
		GmfGetBlock(MshIdx, GmfTriangles, GmfLong, &TriTab[1][0], &TriTab[2][0], GmfLong, &TriTab[1][1], &TriTab[2][1], \
					GmfLong, &TriTab[1][2], &TriTab[2][2], GmfLong, &ref, &ref);
	}

	GmfCloseMesh(MshIdx);
	printf(" %g s\n", (double)(clock() - t) / CLOCKS_PER_SEC);


	/*---------------------------------------------------------*/
	/* Build an octree from this mesh and perform some queries */
	/*---------------------------------------------------------*/

	puts("\nBuild the octree : ");
	t = clock();
	OctIdx = NewOctree(NmbVer, VerTab[1], VerTab[2], NmbTri, TriTab[1], TriTab[2]);
	printf(" %g s\n", (double)(clock() - t) / CLOCKS_PER_SEC);

	for(i=0;i<3;i++)
	{
		MinCrd[i] = crd2[i] - .0001;
		MaxCrd[i] = crd2[i] + .0001;
	}

	puts("\nSearch for vertices in a bounding box :");
	NmbItm = GetBoundingBox(OctIdx, TypVer, BufSiz, buf, MinCrd, MaxCrd);

	for(i=0;i<NmbItm;i++)
		printf(" vertex : %d\n", buf[i]);

	puts("\nSearch for the closest vertex from a given point :");
	idx = GetNearest(OctIdx, TypVer, crd1, &dis, 0.);
	printf(" closest vertex = %d, distance = %g\n", idx, dis);

	if(NmbTri)
	{
		puts("\nSearch for triangles in a bounding box :");

		NmbItm = GetBoundingBox(OctIdx, TypTri, BufSiz, buf, MinCrd, MaxCrd);

		for(i=0;i<NmbItm;i++)
			printf(" triangle : %d\n", buf[i]);

		puts("\nSearch for the closest triangle from a given point :");
		idx = GetNearest(OctIdx, TypTri, crd1, &dis, 0.);
		printf(" closest triangle = %d, distance = %g\n", idx, dis);

		for(i=1;i<=NmbVer;i++)
			for(j=0;j<3;j++)
				if(VerTab[i][j] < MinCrd[j])
					MinCrd[j] = VerTab[i][j];
				else if(VerTab[i][j] > MaxCrd[j])
					MaxCrd[j] = VerTab[i][j];

		for(i=0;i<3;i++)
			IncCrd[i] = (MaxCrd[i] - MinCrd[i]) / inc;

		crd1[0] = MinCrd[0];

		t = clock();

		for(i=0;i<inc;i++)
		{
			crd1[1] = MinCrd[1];

			for(j=0;j<inc;j++)
			{
				crd1[2] = MinCrd[2];

				for(k=0;k<inc;k++)
				{
					t2 = clock();
					idx = GetNearest(OctIdx, TypTri, crd1, &dis, .005);
					t2 = clock() - t2;
					MinTim = MIN(MinTim, t2);
					MaxTim = MAX(MaxTim, t2);
					AvgDis += dis;
					crd1[2] += IncCrd[2];
				}

				crd1[1] += IncCrd[1];
			}

			crd1[0] += IncCrd[0];
		}

		printf("nb samples = %d, mean dist = %g, total time = %g s, min time = %g s, max time = %g s\n", \
				inc*inc*inc, AvgDis / (inc*inc*inc), (double)(clock() - t) / CLOCKS_PER_SEC, \
				(double)MinTim / CLOCKS_PER_SEC, (double)MaxTim / CLOCKS_PER_SEC);
	}


	/*------------------*/
	/* Cleanup memories */ 
	/*------------------*/

	printf("\nFree octree %lld\n", OctIdx);
	printf(" memory used = %zd bytes\n", FreeOctree(OctIdx));
	free(VerTab);

	if(NmbTri)
		free(TriTab);

	return(0);
}
Esempio n. 2
0
int main()
{
    int i, NmbVer, NmbQad, ver, dim, *RefTab, (*QadTab)[5], (*TriTab)[4];
    long long InpMsh, OutMsh;
    float (*VerTab)[3];


    /*-----------------------------------*/
    /* Ouverture du maillage "quad.mesh" */
    /*-----------------------------------*/

    if(!(InpMsh = GmfOpenMesh("quad.meshb", GmfRead, &ver, &dim)))
        return(1);

    printf("InpMsh : idx = %d, version = %d, dimension = %d\n", InpMsh, ver, dim);

    if(dim != 3)
        exit(1);

    /* Lecture des nombres d'elements et de noeuds pour l'allocation de memoire */
    NmbVer = GmfStatKwd(InpMsh, GmfVertices);
    printf("InpMsh : nmb vertices = %d\n", NmbVer);
    VerTab = malloc((NmbVer+1) * 3 * sizeof(float));
    RefTab = malloc((NmbVer+1) * sizeof(int));

    NmbQad = GmfStatKwd(InpMsh, GmfQuadrilaterals);
    printf("InpMsh : nmb quads = %d\n", NmbQad);
    QadTab = malloc((NmbQad+1) * 5 * sizeof(int));
    TriTab = malloc((NmbQad+1) * 2 * 4 * sizeof(int));

    /* Lecture des noeuds */
    GmfGetBlock(    InpMsh, GmfVertices, NULL, \
                    GmfFloat, &VerTab[1][0], &VerTab[2][0], \
                    GmfFloat, &VerTab[1][1], &VerTab[2][1], \
                    GmfFloat, &VerTab[1][2], &VerTab[2][2], \
                    GmfInt, &RefTab[1], &RefTab[2] );

    /* Lecture des quadrangles */
    GmfGetBlock(    InpMsh, GmfQuadrilaterals, NULL, \
                    GmfInt, &QadTab[1][0], &QadTab[2][0], \
                    GmfInt, &QadTab[1][1], &QadTab[2][1], \
                    GmfInt, &QadTab[1][2], &QadTab[2][2], \
                    GmfInt, &QadTab[1][3], &QadTab[2][3], \
                    GmfInt, &QadTab[1][4], &QadTab[2][4] );

    /* Fermeture du maillage quad */
    GmfCloseMesh(InpMsh);


    /*-----------------------------------*/
    /* Creation du maillage en triangles */
    /*-----------------------------------*/

    if(!(OutMsh = GmfOpenMesh("tri.meshb", GmfWrite, ver, dim)))
        return(1);

    /* Ecriture du nombre de noeuds */
    GmfSetKwd(OutMsh, GmfVertices, NmbVer);

    /* Puis ecriture des noeuds */
    GmfSetBlock(OutMsh, GmfVertices, NULL, \
                    GmfFloat, &VerTab[1][0], &VerTab[2][0], \
                    GmfFloat, &VerTab[1][1], &VerTab[2][1], \
                    GmfFloat, &VerTab[1][2], &VerTab[2][2], \
                    GmfInt, &RefTab[1], &RefTab[2] );


    /*  Ecriture du nombre de triangles */
    GmfSetKwd(OutMsh, GmfTriangles, 2*NmbQad);
    printf("OutMsh : nmb triangles = %d\n", 2*NmbQad);

    /* Puis boucle de conversion des quads en deux triangles */

    for(i=1;i<=NmbQad;i++)
    {
        TriTab[i*2-1][0] = QadTab[i][0];
        TriTab[i*2-1][1] = QadTab[i][1];
        TriTab[i*2-1][2] = QadTab[i][2];
        TriTab[i*2-1][3] = QadTab[i][4];

        TriTab[i*2][0] = QadTab[i][0];
        TriTab[i*2][1] = QadTab[i][2];
        TriTab[i*2][2] = QadTab[i][3];
        TriTab[i*2][3] = QadTab[i][4];
    }

    /* Ecriture des triangles */
    GmfSetBlock(OutMsh, GmfTriangles, NULL, \
                    GmfInt, &TriTab[1][0], &TriTab[2][0], \
                    GmfInt, &TriTab[1][1], &TriTab[2][1], \
                    GmfInt, &TriTab[1][2], &TriTab[2][2], \
                    GmfInt, &TriTab[1][3], &TriTab[2][3] );

    /* Ne pas oublier de fermer le fichier */
    GmfCloseMesh(OutMsh);

    free(QadTab);
    free(TriTab);
    free(RefTab);
    free(VerTab);

    return(0);
}
Esempio n. 3
0
/* read mesh */
int loadMesh(pMesh mesh) {
    pPoint       ppt;
    pTria        pt1;
    pTetra       ptt;
    pEdge       pa;
    float        fp1,fp2,fp3;
    int          k,inm,i;
    char        *ptr,data[256];
  
    mesh->dim=0;
  
    strcpy(data,mesh->name);
    ptr = strstr(data,".mesh");
    if ( !ptr ) {
        strcat(data,".mesh");
        if ( !(inm = GmfOpenMesh(data,GmfRead,&mesh->ver,&mesh->dim)) ) {
            strcat(data,"b");
            if ( !(inm = GmfOpenMesh(data,GmfRead,&mesh->ver,&mesh->dim)) ) {
                fprintf(stderr,"  ** %s  NOT FOUND.\n",data);
                return(0);
            }
        }
    }
    else {
    	if ( !(inm = GmfOpenMesh(data,GmfRead,&mesh->ver,&mesh->dim)) ) {
           fprintf(stderr,"  ** %s  NOT FOUND.\n",data);
     	   return(0);
    	}
    }
    fprintf(stdout,"  %%%% %s OPENED\n",data);
    
    if ( abs(info.imprim) > 3 )
        fprintf(stdout,"  -- READING DATA FILE %s\n",data);
    
    mesh->np = GmfStatKwd(inm,GmfVertices);
    mesh->nt = GmfStatKwd(inm,GmfTriangles);
    mesh->ne = GmfStatKwd(inm,GmfTetrahedra);
    mesh->na = GmfStatKwd(inm,GmfEdges);
    
    if ( !mesh->np ) {
        fprintf(stdout,"  ** MISSING DATA\n");
        return(0);
    }
    
    /* memory alloc */
    mesh->point = (pPoint)calloc(mesh->np+1,sizeof(Point));
    assert(mesh->point);
    if ( mesh->ne ) {
        mesh->tetra  = (pTetra)calloc(mesh->ne+1,sizeof(Tetra));
        assert(mesh->tetra);
    }
    if ( mesh->nt ) {
        mesh->tria  = (pTria)calloc(mesh->nt+1,sizeof(Tria));
        assert(mesh->tria);
    }
    
    if ( mesh->dim == 2 ) {
        
        if ( mesh->na ) {
            mesh->edge  = (pEdge)calloc(mesh->na+1,sizeof(Edge));
            assert(mesh->edge);
        }
        /* read mesh vertices */
        GmfGotoKwd(inm,GmfVertices);
        for (k=1; k<=mesh->np; k++) {
            ppt = &mesh->point[k];
            if ( mesh->ver == GmfFloat ) {
                GmfGetLin(inm,GmfVertices,&fp1,&fp2,&ppt->ref);
                ppt->c[0] = fp1;
                ppt->c[1] = fp2;
            }
            else
                GmfGetLin(inm,GmfVertices,&ppt->c[0],&ppt->c[1],&ppt->ref);
        }
        /* read mesh triangles */
        GmfGotoKwd(inm,GmfTriangles);
        for (k=1; k<=mesh->nt; k++) {
            pt1 = &mesh->tria[k];
            GmfGetLin(inm,GmfTriangles,&pt1->v[0],&pt1->v[1],&pt1->v[2],&pt1->ref);
            for (i=0; i<3; i++) {
                ppt = &mesh->point[pt1->v[i]];
                pt1->g[0] += ppt->c[0];
                pt1->g[1] += ppt->c[1];
                if ( !ppt->s )  ppt->s = k;
            }
            pt1->g[0] /= 3.0;
            pt1->g[1] /= 3.0;
        }
        
        /* read mesh edges */
        GmfGotoKwd(inm,GmfEdges);
        for (k=1; k<=mesh->na; k++) {
            pa = &mesh->edge[k];
            if ( mesh->ver == GmfFloat ) {
                GmfGetLin(inm,GmfEdges,&pa->v[0],&pa->v[1],&pa->ref);
                
            }
            else
                GmfGetLin(inm,GmfEdges,&pa->v[0],&pa->v[1],&pa->ref);
        }

        if ( abs(info.imprim) > 4 ) {
            fprintf(stdout,"  %%%% NUMBER OF VERTICES  %8d\n",mesh->np);
            if ( mesh->na )  fprintf(stdout,"  %%%% NUMBER OF EDGES     %8d\n",mesh->na);
            if (mesh->nt)    fprintf(stdout,"  %%%% NUMBER OF TRIANGLES %8d\n",mesh->nt);
        }
    }
    
    else {
        GmfGotoKwd(inm,GmfVertices);
        for (k=1; k<=mesh->np; k++) {
            ppt = &mesh->point[k];
            if ( mesh->ver == GmfFloat ) {
                GmfGetLin(inm,GmfVertices,&fp1,&fp2,&fp3,&ppt->ref);
                ppt->c[0] = fp1;
                ppt->c[1] = fp2;
                ppt->c[2] = fp3;
            }
            else
                GmfGetLin(inm,GmfVertices,&ppt->c[0],&ppt->c[1],&ppt->c[2],&ppt->ref);
        }
        /* read triangles */
        GmfGotoKwd(inm,GmfTriangles);
        for (k=1; k<=mesh->nt; k++) {
            pt1 = &mesh->tria[k];
            GmfGetLin(inm,GmfTriangles,&pt1->v[0],&pt1->v[1],&pt1->v[2],&pt1->ref);
        }
        /* read tetrahedra */
        GmfGotoKwd(inm,GmfTetrahedra);
        for (k=1; k<=mesh->ne; k++) {
            ptt = &mesh->tetra[k];
            GmfGetLin(inm,GmfTetrahedra,&ptt->v[0],&ptt->v[1],&ptt->v[2],&ptt->v[3],&ptt->ref);
            for (i=0; i<4; i++) {
                ppt = &mesh->point[ptt->v[i]];
                ptt->g[0] += ppt->c[0];
                ptt->g[1] += ppt->c[1];
                ptt->g[2] += ppt->c[2];
                if ( !ppt->s )  ppt->s = k;
            }
            ptt->g[0] *= 0.25;
            ptt->g[1] *= 0.25;
            ptt->g[2] *= 0.25;
        }
        
        if ( abs(info.imprim) > 4 ) {
            fprintf(stdout,"  %%%% NUMBER OF VERTICES   %8d\n",mesh->np);
            if ( mesh->nt )  fprintf(stdout,"  %%%% NUMBER OF TRIANGLES  %8d\n",mesh->nt);
            if ( mesh->ne )  fprintf(stdout,"  %%%% NUMBER OF TETRAHEDRA %8d\n",mesh->ne);
        }
    }
    
    GmfCloseMesh(inm);
    return(1);
}
Esempio n. 4
0
/* load scalar solution field */
int loadSol(pSol sol) {
    float       buf[GmfMaxTyp];
    double      bufd[GmfMaxTyp];
    int         i,k,type,inm,typtab[GmfMaxTyp],offset;
    char       *ptr,data[128];
 
    if ( !sol->name )  return(-1);
    strcpy(data,sol->name);
    ptr = strstr(data,".mesh");

 	if ( ptr ) *ptr = '\0';
 	strcat(data,".sol");
        if ( !(inm = GmfOpenMesh(data,GmfRead,&sol->ver,&sol->dim)) ) {
            strcat(data,"b");
            if ( !(inm = GmfOpenMesh(data,GmfRead,&sol->ver,&sol->dim)) ) {
                fprintf(stderr,"  ** %s  NOT FOUND.\n",data);
                return(0);
            }
        }
    fprintf(stdout,"  %%%% %s OPENED\n",data);
    
    if ( abs(info.imprim) > 3 )
        fprintf(stdout,"  -- READING DATA FILE %s\n",data);
    
    sol->np = GmfStatKwd(inm,GmfSolAtVertices,&type,&offset,&typtab);
    fprintf(stdout,"  -- READING DATA FILE %d %d %d \n",type,offset,typtab[0]);
    if ( !sol->np  )  return(-1);
    sol->size[0] = offset;
    
    if (typtab[0]==2) {
        
        /* alloc */
        sol->u  = (double*)calloc(sol->dim*sol->np,sizeof(double));
        assert(sol->u);
        
        /* read mesh solutions */
        GmfGotoKwd(inm,GmfSolAtVertices);
        if ( sol->ver == GmfFloat ) {
            for (k=0; k<sol->np; k++) {
                GmfGetLin(inm,GmfSolAtVertices,&buf);
                for (i=0; i<sol->dim; i++)
                    sol->u[sol->dim*k+i] = buf[i];
            }
        }
        else {
            for (k=0; k<sol->np; k++) {
                GmfGetLin(inm,GmfSolAtVertices,bufd);
                for (i=0; i<sol->dim; i++)
                    sol->u[sol->dim*k+i] = bufd[i];
            }
        }
    }
    else if (typtab[0]==1) {
        sol->u  = (double*)calloc(sol->np,sizeof(double));
        assert(sol->u);
        sol->valp1  = (double*)calloc(sol->np,sizeof(double));
        assert(sol->valp1 );
        
        GmfGotoKwd(inm,GmfSolAtVertices);
        if ( sol->ver == GmfFloat ) {
            for (k=0; k<sol->np; k++) {
                GmfGetLin(inm,GmfSolAtVertices,&buf);
                //sol->u[k] = buf[0];
                sol->valp1[k] = buf[0];
            }
        }
        else {
            for (k=0; k<sol->np; k++) {
                GmfGetLin(inm,GmfSolAtVertices,bufd);
                // sol->u[k] = bufd[0];
                sol->valp1[k] = bufd[0];
            }
        }
    }
 
    GmfCloseMesh(inm);
    return(1);
}
Esempio n. 5
0
int Mesh2::load(const string & filename)
{
  
  int bin;
  int ver,inm,dim;
  int lf=filename.size()+20;
  KN<char>  fileb(lf),filef(lf);
  char * pfile;
  strcpy(filef,filename.c_str());
  strcpy(fileb,filef);
  strcat(filef,".mesh");
  strcat(fileb,".meshb");
  if( (inm=GmfOpenMesh(pfile=fileb, GmfRead,&ver,&dim)) ) 
    bin=true;
  else if( (inm=GmfOpenMesh(pfile=filef, GmfRead,&ver,&dim)) ) 
    bin=false;
  else
    { cerr << " Erreur ouverture file " << (char *) fileb << " " << (char *) filef << endl;
      return   1;
    }
  int nv,nt,neb;
  nv = GmfStatKwd(inm,GmfVertices);
  nt = GmfStatKwd(inm,GmfTriangles);
  neb=GmfStatKwd(inm,GmfEdges);
  this->set(nv,nt,neb);

  if(verbosity)  
  cout << pfile <<": ver " << ver << ", d "<< dim  << ", nt " << nt << ", nv " << nv << " nbe:  = " << nbe << endl;
  if(dim  != Rd::d) { 
    cerr << "Err dim == " << dim << " != " << Rd::d << endl;
    return 2; }
  if( nv<=0 && nt <=0 ) {
    cerr << " missing data "<< endl;
    return 3;
  }
  int iv[4],lab;
  float cr[3];
  // read vertices 
  GmfGotoKwd(inm,GmfVertices);
  int mxlab=0;
  int mnlab=0;
  for(int i=0;i<nv;++i)
    {  
      if(ver<2) {
	GmfGetLin(inm,GmfVertices,&cr[0],&cr[1],&lab);
	vertices[i].x=cr[0];
	vertices[i].y=cr[1];
	//	vertices[i].z=cr[2];
    }
      else
	GmfGetLin(inm,GmfVertices,&vertices[i].x,&vertices[i].y,&lab);	    
      vertices[i].lab=lab;
      
      mxlab= max(mxlab,lab);
      mnlab= min(mnlab,lab);
    }
  
  
  //    /* read mesh triangles */
  //if(mnlab==0 &&mxlab==0 )
    {
      mes=0;
      GmfGotoKwd(inm,GmfTriangles);
      for(int i=0;i<nt;++i)
	{  
	  GmfGetLin(inm,GmfTriangles,&iv[0],&iv[1],&iv[2],&lab);
	  for (int j=0;j<3;++j)  
	    iv[j]--;
	  this->elements[i].set(this->vertices,iv,lab);
	  mes += this->elements[i].mesure();
	}
      
    }
  
  
  /* read mesh segement */
  mesb=0;
  GmfGotoKwd(inm,GmfEdges);
  for(int i=0;i<nbe;++i)
    {
      GmfGetLin(inm,GmfEdges,&iv[0],&iv[1],&lab);
      assert( iv[0]>0 && iv[0]<=nv && iv[1]>0 && iv[1]<=nv);
      for (int j=0;j<2;j++) iv[j]--;
      this->borderelements[i].set(vertices,iv,lab); 
      mesb += this->borderelements[i].mesure();	    
    }
  
  GmfCloseMesh(inm);
    if(verbosity>1)  cout << "   mesure :  "<< mes << " border mesure : " << mesb<< endl;
  return(0); // OK
  
}
Esempio n. 6
0
CglGeometry::CglGeometry(GEOMETRY geom, char* file) {
    mBuffer = nBuffer = cBuffer = iBuffer = bbmBuffer = bbiBuffer = -1;

    glm::vec3 center(0,0,0);
    glm::vec3 scale(1,1,1);
    glm::vec3 color(1,1,1);
    glm::vec3 pt1(0,0,0);
    glm::vec3 pt2(1,1,1);

    //pMaterial->setColor(glm::vec3(R, G, B));
    //pos = glm::vec3(x,y,z);
    //center = glm::vec3(x,y,z);
    //MODEL[3] = glm::vec4(center,1);

    switch(geom) {

    case CGL_CUBE: {
        vertices = std::vector<float> {
            -1.0, -1.0,  1.0,
            1.0, -1.0,  1.0,
            1.0,  1.0,  1.0,
            -1.0,  1.0,  1.0,
            -1.0, -1.0, -1.0,
            1.0, -1.0, -1.0,
            1.0,  1.0, -1.0,
            -1.0,  1.0, -1.0,
        };
        for(int i = 0 ; i < vertices.size() ; i+=3) {
            vertices[i+0] = scale.x * vertices[i+0];
            vertices[i+1] = scale.y * vertices[i+1];
            vertices[i+2] = scale.z * vertices[i+2];
        }
        indices = std::vector<int> {
            0, 1, 2,
            2, 3, 0,
            3, 2, 6,
            6, 7, 3,
            7, 6, 5,
            5, 4, 7,
            4, 5, 1,
            1, 0, 4,
            4, 0, 3,
            3, 7, 4,
            1, 5, 6,
            6, 2, 1
        };
        break;
    }
    case CGL_SPHERE: {
        sphereGeom sphere(10);
        indices = sphere.indices;
        for(int i = 0 ; i < sphere.vertices.size() ; i++) {
            for(int j = 0 ; j < 3 ; j++)
                vertices.push_back(scale[j] * sphere.vertices[i].pos[j]);
        }
        normals = vertices;
        break;
    }
    case CGL_CYLINDER: {
        glm::vec3 direction = pt2-pt1;
        glm::vec3 ortho1    = glm::normalize(glm::cross(direction, glm::vec3(0,1,0)));
        glm::vec3 ortho2    = glm::normalize(glm::cross(direction, ortho1));
        //CIRCLES
        std::vector<glm::vec3> circle1, circle2;
        int nb = 20;
        float angleInc = 2*3.14159f / nb;
        float angle = 0;
        for(int i = 0 ; i < nb ; i++) {
            circle1.push_back( pt1 +  scale.x*cos(angle)*ortho1 + scale.y*sin(angle)*ortho2 );
            circle2.push_back( pt2 +  scale.x*cos(angle)*ortho1 + scale.y*sin(angle)*ortho2 );
            angle+=angleInc;
        }
        //INDICES
        for(int i = 0 ; i < nb ; i++) {
            indices.push_back(i);
            if(i==nb-1)
                indices.push_back(0);
            else
                indices.push_back(i+1);
            indices.push_back(nb + i);
        }
        for(int i = 0 ; i < nb ; i++) {
            indices.push_back(nb + i);
            if(i==circle1.size()-1)
                indices.push_back(0);
            else
                indices.push_back(i+1);
            if(i==circle1.size()-1)
                indices.push_back(nb);
            else
                indices.push_back(nb+i+1);
        }
        //VERTICES & NORMALS
        for(int i = 0 ; i < nb ; i++) {
            for(int j = 0 ; j < 3 ; j ++) {
                vertices.push_back(circle1[i][j]);
                normals.push_back(circle1[i][j] - pt1[j]);
            }
        }
        for(int i = 0 ; i < nb ; i++) {
            for(int j = 0 ; j < 3 ; j ++) {
                vertices.push_back(circle2[i][j]);
                normals.push_back(circle2[i][j] - pt2[j]);
            }
        }
        break;
    }
    case CGL_MESH: {
        meshFile    = std::string(file);
        cout << "Reading: " << file << " " << endl;

        //Initialisation
        int                     np,nt,nn,dim,ver, nNAtV;
        Point                   *ppt;
        Tria                    *pt;
        double                  *n,dd;
        float                   fp1,fp2,fp3;
        int                     k,inm;
        vector<Point>           point;
        vector<Tria>            tria;
        vector<Normal>          normal;
        vector<NormalAtVertex>  NormalAtVertices;

        //Lecture du .sol
        /*
        std::string solFile   = meshFile.substr(0, meshFile.size()-5) + ".sol";
        cout << solFile << endl << endl;
        int ver2, dim2;
        int inMeshSol = 0;
        inMeshSol = GmfOpenMesh((char*)(solFile.c_str()), GmfRead, &ver2, &dim2);
        if(inMeshSol == 0){cout << "Unable to open sol file" << endl; exit(143);}
        cout << "sol file opened " << (char*)(solFile.c_str()) << endl;
        int type, offset, typtab[GmfMaxTyp];
        int nSol      = GmfStatKwd(inMeshSol, GmfSolAtVertices, &type, &offset, &typtab);
        std::vector<float> values(nSol);
        GmfGotoKwd(inMeshSol, GmfSolAtVertices);
        for(int i = 0 ; i< nSol ; i++){
            double val;
            if ( ver2 == GmfFloat ){
                GmfGetLin(inMeshSol, GmfSolAtVertices, &values[i]);
            }
            else{
              GmfGetLin(inMeshSol, GmfSolAtVertices, &val);
              values[i] = val;
            }
        }
        GmfCloseMesh(inMeshSol);
        //COLORS
        float mini = 1e9;
        float maxi = -1e9;
        for(int i = 0 ; i < values.size() ; i++){
            mini = min(mini, values[i]);
            maxi = max(maxi, values[i]);
        }
        palette = new CglPalette( mini, maxi, CGL_PALETTE_BR );
        palette->setBoundaries(mini, maxi);
        for(int i = 0 ; i < values.size() ; i++){
            glm::vec3 col = palette->getColor(values[i]);
            colors.push_back(col.x);
            colors.push_back(col.y);
            colors.push_back(col.z);
        }
        */

        //Lecture du .mesh
        inm = GmfOpenMesh(file,GmfRead,&ver,&dim);
        cout << file << "/" << ver << "/" << dim << endl;
        if ( !inm ) {
            cout << "Unable to open mesh file" << endl;
            exit(0);
        }

        np    = GmfStatKwd(inm, GmfVertices);
        nt    = GmfStatKwd(inm, GmfTriangles);
        nn    = GmfStatKwd(inm, GmfNormals);
        nNAtV = GmfStatKwd(inm, GmfNormalAtVertices);
        if ( !np ) {
            cout << "  ** MISSING DATA" << endl;
            exit(0);
        }
        point.resize(np);
        tria.resize(nt);
        normal.resize(np);
        NormalAtVertices.resize(nNAtV + 1);

        GmfGotoKwd(inm,GmfVertices);
        for (k=0; k<np; k++) {
            ppt = &point[k];
            if ( ver == GmfFloat ) {
                GmfGetLin(inm,GmfVertices,&fp1,&fp2,&fp3,&ppt->ref);
                ppt->c[0] = fp1;
                ppt->c[1] = fp2;
                ppt->c[2] = fp3;
            }
            else
                GmfGetLin(inm,GmfVertices,&ppt->c[0],&ppt->c[1],&ppt->c[2],&ppt->ref);
        }
        GmfGotoKwd(inm,GmfTriangles);
        for (k=0; k<nt; k++) {
            pt = &tria[k];
            GmfGetLin(inm,GmfTriangles,&pt->v[0],&pt->v[1],&pt->v[2],&pt->ref);
        }
        if ( nn ) {
            GmfGotoKwd(inm,GmfNormals);
            for (k=0; k<nn; k++) {
                if ( ver == GmfFloat ) {
                    GmfGetLin(inm,GmfNormals,&fp1,&fp2,&fp3);
                    normal[k].n[0] = fp1;
                    normal[k].n[1] = fp2;
                    normal[k].n[2] = fp3;
                }
                else
                    GmfGetLin(inm,GmfNormals,&normal[k].n[0],&normal[k].n[1],&normal[k].n[2]);
                n  = normal[k].n;
                dd = 1.0 / sqrt(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]);
                n[0] *= dd;
                n[1] *= dd;
                n[2] *= dd;
            }
        }

        //VERTICES
        int inv = ((pcv->profile.invertVertical)?-1:1);
        for (int i = 0 ; i < point.size() ; i++) {
            vertices.push_back(      point[i].c[0]);
            vertices.push_back(inv * point[i].c[1]);
            vertices.push_back(inv * point[i].c[2]);
        }
        //INDICES
        for (int i = 0 ; i < tria.size() ; i++) {
            for(int j = 0 ; j < 3 ; j++)
                indices.push_back(tria[i].v[j]-1);
        }
        //NORMALS
        if ( nNAtV ) {
            GmfGotoKwd(inm,GmfNormalAtVertices);
            for (k=0; k<nNAtV; k++)
                GmfGetLin(inm,GmfNormalAtVertices,
                          &NormalAtVertices[k].inds[0],
                          &NormalAtVertices[k].inds[1]);
        }
        for(int i = 0 ; i < vertices.size() ; i++) {
            normals.push_back(0.0f);
        }
        for(int i = 0 ; i < NormalAtVertices.size() - 1 ; i++) {
            int indV = NormalAtVertices[i].inds[0] - 1;
            int indN = NormalAtVertices[i].inds[1] - 1;
            normals[3 * indV + 0] =       normal[indN].n[0];
            normals[3 * indV + 1] = inv * normal[indN].n[1];
            normals[3 * indV + 2] = inv * normal[indN].n[2];
        }

        GmfCloseMesh(inm);
        break;
    }
    case CGL_PLANE: {
        std::vector<float> vertices = {-1,0,1,
                                       1,0,1,
                                       1,0,-1,
                                       -1,0,-1
                                      };
        std::vector<int> indices = {0,1,2,
                                    1,2,3
                                   };
        std::vector<float> normals = {  0,1,0,
                                        0,1,0
                                     };
    }
    }

    getBBOX(vertices);
    computeBuffers();
    //A récupérer par l'objet lors de la création
    /*
    center = glm::vec3(x,y,z);
    MODEL[3] = glm::vec4(center,1);
    */
}
Esempio n. 7
0
int main(int ArgCnt, char **ArgVec)
{
   int i, NmbVer, NmbTet, ver=0, dim=0, ref, (*TetTab)[4]=NULL;
   int VerIdx, TetIdx, MidIdx, CalMid, GpuIdx=0;
   int64_t InpMsh;
   float (*VerTab)[3]=NULL, (*MidTab)[4], dummy, chk=0.;
   double GpuTim;


   // If no arguments are give, print the help
   if(ArgCnt == 1)
   {
      puts("\nTetrahedraBasicLoop GPU_index");
      puts(" Choose GPU_index from the following list:");
      GmlListGPU();
      exit(0);
   }
   else
      GpuIdx = atoi(ArgVec[1]);


   /*--------------*/
   /* MESH READING */
   /*--------------*/

   // Open the mesh
   if( !(InpMsh = GmfOpenMesh("tetrahedra.meshb", GmfRead, &ver, &dim))
   || (ver != GmfFloat) || (dim != 3) )
   {
      puts("Could not open tetrahedra.meshb");
      puts("Please run the command in the same directory this mesh is located");
      return(1);
   }

   // Read the number of vertices and elements and allocate the memory
   if( !(NmbVer = GmfStatKwd(InpMsh, GmfVertices)) \
   || !(VerTab = malloc((NmbVer+1) * 3 * sizeof(float))) )
   {
      return(1);
   }

   if( !(NmbTet = GmfStatKwd(InpMsh, GmfTetrahedra)) \
   || !(TetTab = malloc((NmbTet+1) * 4 * sizeof(int))) )
   {
      return(1);
   }

   // Read the vertices
   GmfGotoKwd(InpMsh, GmfVertices);
   for(i=1;i<=NmbVer;i++)
      GmfGetLin(InpMsh, GmfVertices, &VerTab[i][0], &VerTab[i][1], &VerTab[i][2], &ref);

   // Read the elements
   GmfGotoKwd(InpMsh, GmfTetrahedra);
   for(i=1;i<=NmbTet;i++)
      GmfGetLin(  InpMsh, GmfTetrahedra, &TetTab[i][0], &TetTab[i][1], \
                  &TetTab[i][2], &TetTab[i][3], &ref );

   // And close the mesh
   GmfCloseMesh(InpMsh);


   /*---------------*/
   /* GPU COMPUTING */
   /*---------------*/

   // Init the GMLIB and compile the OpenCL source code
   if(!GmlInit(GpuIdx))
      return(1);

   if(!(CalMid = GmlNewKernel(TetrahedraBasicLoop, "TetrahedraBasic")))
      return(1);

   // Create a vertices data type and transfer the data to the GPU
   if(!(VerIdx = GmlNewData(GmlVertices, NmbVer, 0, GmlInput)))
      return(1);

   for(i=1;i<=NmbVer;i++)
      GmlSetVertex(VerIdx, i-1, VerTab[i][0], VerTab[i][1], VerTab[i][2]);

   GmlUploadData(VerIdx);

   // Do the same with the elements
   if(!(TetIdx = GmlNewData(GmlTetrahedra, NmbTet, 0, GmlInput)))
      return(1);

   for(i=1;i<=NmbTet;i++)
      GmlSetTetrahedron(TetIdx, i-1, TetTab[i][0]-1, TetTab[i][1]-1, \
                        TetTab[i][2]-1, TetTab[i][3]-1);

   GmlUploadData(TetIdx);

   // Create a raw datatype to store the element middles.
   // It does not need to be tranfered to the GPU
   if(!(MidIdx = GmlNewData(GmlRawData, NmbTet, sizeof(cl_float4), GmlOutput)))
      return(1);

   if(!(MidTab = malloc((NmbTet+1)*4*sizeof(float))))
      return(1);

   // Launch the kernel on the GPU
   GpuTim = GmlLaunchKernel(CalMid, NmbTet, 3, TetIdx, MidIdx, VerIdx);

   if(GpuTim < 0)
      return(1);

   /* Get the results back and print some stats */
   GmlDownloadData(MidIdx);

   for(i=1;i<=NmbTet;i++)
   {
      GmlGetRawData(MidIdx, i-1, MidTab[i]);
      chk += sqrt(MidTab[i][0] * MidTab[i][0] \
               +  MidTab[i][1] * MidTab[i][1] \
               +  MidTab[i][2] * MidTab[i][2]);
   }

   printf("%d tet centers computed in %g seconds, %ld MB used, %ld MB transfered, checksum = %g\n",
      NmbTet, GpuTim, GmlGetMemoryUsage()/1048576, GmlGetMemoryTransfer()/1048576, chk / NmbTet );


   /*-----*/
   /* END */
   /*-----*/

   GmlFreeData(VerIdx);
   GmlFreeData(TetIdx);
   GmlFreeData(MidIdx);
   GmlStop();

   free(MidTab);
   free(TetTab);
   free(VerTab);

   return(0);
}