[vtkusers] Display mesh with attributes

agatte agatakrason at gmail.com
Tue Aug 14 11:18:01 EDT 2012


Hi ;) 

I have a question. 
I have already red mesh with attributes (in file).
But I can't display corectly  mesh with attributes in VTK.
I receive only one color. But When I open file in paraview, I can display
well mesh & attributes  with colors.
Could anyone help me please ? What is it wrong in this code  ? What should I
add ?
I would appreciate for any help please.

Here I put my code :

int main(int argc, char* argv[])
{

	// file with mesh
    const char* filename = "heartmesh.txt";
	std::ifstream infile(filename);
    vtkIdType number_of_points, number_of_triangles;
    infile >> number_of_points >> number_of_triangles;
    vtkPoints* points = vtkPoints::New();
    points->SetNumberOfPoints(number_of_points);

	//std::cout<<"number_of_points: "<<number_of_points&lt;&lt;std::endl;
	//std::cout&lt;&lt;&quot;number_of_triangles: &quot;
&lt;&lt;number_of_triangles&lt;&lt;std::endl;

    for (vtkIdType i = 0; i &lt; number_of_points; i++)
     {
        double x, y, z;
        infile >> x >> y >> z;
        points->SetPoint(i,x, y, z);
	  
     }
 
    vtkCellArray* polys = vtkCellArray::New();

   for (vtkIdType i = 0; i < number_of_triangles; i++)
    {
      vtkIdType a, b, c;
      infile >> a >> b >> c;
      polys->InsertNextCell(3);
      polys->InsertCellPoint(a-1);
      polys->InsertCellPoint(b-1);
      polys->InsertCellPoint(c-1);
	 
    }


   vtkUnstructuredGrid* grid = vtkUnstructuredGrid::New();
   grid->SetPoints(points);
   grid->SetCells(5, polys);
   grid->Update();
 
   // Here I read a file with attributes scalars
   vtkDelimitedTextReader *dread = vtkDelimitedTextReader::New();
   dread->SetFileName("CSVFile.csv");
   dread->Update();
 
   vtkTable *tdata = dread->GetOutput();

   vtkFloatArray *fd = vtkFloatArray::New();

   vtkIdType nr = tdata->GetNumberOfRows();

   fd->SetNumberOfTuples(nr);

   for (vtkIdType i=0; i<nr; i++) {
    std::cout &lt;&lt;  tdata->GetValue(i, 0).ToFloat() << "\n"; 
    fd->SetTuple1(i, tdata->GetValue(i, 0).ToFloat());
   }

  vtkPointData *pd = grid->GetPointData();
  pd->SetScalars(fd);
  pd->Update();

  // Save in file mesh& attributes
  vtkUnstructuredGridWriter *writer = vtkUnstructuredGridWriter::New();
  writer->SetInput(grid);
  writer->SetFileName("outputMeshWithAttributes.vtk");
  writer->Write();

  vtkGeometryFilter* geometryFilter = vtkGeometryFilter::New();
  geometryFilter->SetInput(grid);
  geometryFilter->Update();

  vtkPolyData* polydata = geometryFilter->GetOutput();

  // Here I need to display mesh with attributes !
  // Visualization & Display 
  vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
  mapper->SetInput(polydata);

  //mapper->SetLookupTable(lut);
  mapper->SetColorModeToMapScalars();
  vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
  actor->SetMapper(mapper);
 

  //Create a renderer, render window, and interactor
  vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);

  renderer->AddActor(actor);
  renderer->SetBackground(0, 0, 0); 
  renderWindow->Render();
  renderWindowInteractor->Start();


  /*polydata->Delete();
  geometryFilter->Delete();
  writer->Delete();
  pd->Delete();
  tdata->Delete();
  fd->Delete();
  dread->Delete();
  polys->Delete();
  points->Delete();*/
 
 
 
  return EXIT_SUCCESS;
}


http://vtk.1045678.n5.nabble.com/file/n5715247/meshinParaview.png 
http://vtk.1045678.n5.nabble.com/file/n5715247/meshInVTK.png 






--
View this message in context: http://vtk.1045678.n5.nabble.com/Display-mesh-with-attributes-tp5715247.html
Sent from the VTK - Users mailing list archive at Nabble.com.



More information about the vtkusers mailing list