[vtkusers] Drawing a mesh with vtk

Frédéric HUGUET fhuguet at scconsultants.com
Tue Aug 19 10:38:49 EDT 2003


Hi deras vtkusers,

i would like to read a mesh file (points coordinates and mesh caracteristics) and then draw it under a simple SDI MFC 
application.

So my idea was the following :

read the mesh file, and use the vtk Mesh structure to save data.

Mesh *m_pMaillage3D;

then here is the code of the function which read the mesh file :

int CvtkMaillage::ReadMesh(char *nom_fichier){
 FILE *ft;
 long i,nb_elt_cot;
 char buffer[256];
 int dim_noe, dim_elt,nb_elt;
 long fin;
 int nb_car_lu2=0;
 int n1,n2,n3,n4;
 vtkIdType Id, nb_pts_cell;
 vtkIdType *IdList;
 vtkTetra* Element_Courant = vtkTetra::New();

 if((ft=fopen(nom_fichier,"r"))!=NULL) {

  fgets(buffer,256,ft);
  sscanf(buffer,"%d %d %d %d", &this->m_pMaillage3D->vertices, &dim_noe, &this->m_pMaillage3D->faces, &dim_elt) ;
 nb_elt_cot = this->m_pMaillage3D->faces;

    //Initialisation du tableau des points
 this->m_pMaillage3D->vertex = new Vector[this->m_pMaillage3D->vertices];
    
    //Tableau de connectivité des éléments
    this->m_pMaillage3D->aCellArray = vtkCellArray::New();
    
    
    for(i = 0 ; i<this->m_pMaillage3D->vertices ; i++) {
      fgets(buffer,256,ft);
      traitement_texte_reel(buffer);
      sscanf(buffer,"%lf %lf %lf",&(this->m_pMaillage3D->vertex[i][0]),&(this->m_pMaillage3D->vertex[i][1]),&(this->m_pMaillage3D->vertex[i][2]));
    }  
    
 Element_Courant->PointIds->Initialize();
 Element_Courant->PointIds->Allocate(4);
    
 fin = - 1;

 for(i=0;i<nb_elt_cot;i++) {
      fgets(buffer,256,ft);
      sscanf(buffer,"%d %d %d %d",&n1,&n2,&n3,&n4);
   Element_Courant->PointIds->InsertId(0, n1);
   Element_Courant->PointIds->InsertId(1, n2);
   Element_Courant->PointIds->InsertId(2, n3);
   Element_Courant->PointIds->InsertId(3, n4);
      
   Id = this->m_pMaillage3D->aCellArray->InsertNextCell(Element_Courant); 
   /*maillage3D.topologie.tab_typeelt[i]=1;*/
      if(fin==-1 && n4==0) 
  fin=i;
    }
    
    if(fin!=-1) {
      nb_elt=fin;
      this->m_pMaillage3D->faces=nb_elt_cot-fin;
      this->m_pMaillage3D->face = new Face[this->m_pMaillage3D->faces];
      for(i = 0 ; i < this->m_pMaillage3D->faces; i++) {
  this->m_pMaillage3D->aCellArray->GetCell(fin + i, nb_pts_cell, IdList);
  this->m_pMaillage3D->face[i].a = IdList[0];
  this->m_pMaillage3D->face[i].b = IdList[1];
  this->m_pMaillage3D->face[i].c = IdList[2];
  //free(maillage3D.topologie.tab_elt[fin+i]);
      }
      
    }
    else {
      nb_elt=nb_elt_cot;
    }
  
   fclose(ft);
   return 1;
  }
  return 0;
}

as you can see, i first save number of vertices and number of faces. Then i first use  m_pMaillage3D->vertex to save points coordinates. Then i use m_pMaillage3D->aCellArray to save elements connectivity of the mesh and m_pMaillage3D->face to save face.

When I saved this data, i build a vtkActor using a vtk3DSImporter. I generate PolyData using vtk3DSImporter::GeneratePolyData. Then i build the actor as usual in vtk.

But now mesh cant be drawn although i call a CView::OnPaint overroden method which call the vtkRenderer. Could you tell me if the data structures and the general idea to save my mesh in VTK is good ? How could I do better this method ?

This project cant demonstrate my company that VTK is a useful library :)


Thanks a lot,

Frederic
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20030819/cccfd141/attachment.htm>


More information about the vtkusers mailing list