4

I have a mesh in my own format and I want export it to a .vtk file. How do I do this?

The original mesh is in the following format: (Pseudocode)

class Mesh {

   List<Float[3]> _coordinates

   List<int[4]> _tetra //(elements, Faces, etc.)

}

I hope this clarify my problem. I just want load it with Paraview so I don't care if it is structured, poly, etc.

Alvaro Fuentes
  • 16,937
  • 4
  • 56
  • 68

2 Answers2

6

You may create a vtkUnstructuredGrid:

//vtkPolyData *data = vtkPolyData::New();
vtkUnstructuredGrid *mesh = vtkUnstructuredGrid::New();
vtkPoints *points = vtkPoints::New();
vtkCellArray *cells = vtkCellArray::New();

//vtkIdType pointIDs[4];

std::vector<point>::const_iterator iterator1 = fpoints.begin();

point aux;

if ( event_report != NULL ) { event_report->SetMaxTicks(fpoints.size() + fnodes.size()); }
points->SetNumberOfPoints(fpoints.size());
int pointId = 0;
for (;iterator1 != fpoints.end();iterator1++)
{
    aux = *iterator1;
    points->SetPoint(pointId, aux.coord);
    pointId++;
}

std::vector<node>::const_iterator iterator2 = fnodes.begin();

node aux1;

vtkSmartPointer<vtkIdTypeArray> idCells = 
  vtkSmartPointer<vtkIdTypeArray>::New();
idCells->SetNumberOfComponents(5);
idCells->SetNumberOfTuples(fnodes.size());

int cellIndex = 0;
for (;iterator2 != fnodes.end(); iterator2++)
{
    aux1 = *iterator2;
    vtkIdType * tuple = new vtkIdType[4];
    tuple[0] = 4;
    tuple[1] = aux1.indexs[0] - 1;
    tuple[2] = aux1.indexs[1] - 1;
    tuple[3] = aux1.indexs[2] - 1;
    tuple[4] = aux1.indexs[3] - 1;
    idCells->SetTupleValue(cellIndex, tuple);
    cellIndex++;
}
cells->SetCells(fnodes.size(), idCells);

mesh->SetPoints(points);
mesh->SetCells(VTK_TETRA, cells);

//vtkPolyDataWriter *writer = vtkPolyDataWriter::New();
vtkUnstructuredGridWriter *tetra_writer = vtkUnstructuredGridWriter::New();
tetra_writer->SetFileName( filename );

#if VTK_MAJOR_VERSION <= 5
    tetra_writer->SetInput(mesh);
#else
    tetra_writer->SetInputData(mesh);
#endif

tetra_writer->Write();
tetra_writer->Delete( );
El Marce
  • 3,144
  • 1
  • 26
  • 40
  • I do not have the mesh and want to create tetrahedron mesh from vtkPolyData, how can I do that (Is there any examples)? My the other question is that you have commented `data` (first line), what is the input here to create the mesh and then `unstructuredGrid`? – sc241 Nov 06 '19 at 14:05
6

Check out meshio (a project of mine), it'll help you convert meshes from many formats to many others (including VTK). The VTK interface code may give you a clue as to how to customize it.

Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
  • Thanks, I think it's very useful. – Alvaro Fuentes Jan 13 '17 at 10:27
  • Thanks, this will come in handy. I'm working with VTK on the backend and would like to generate the meshes. I need to incorporate a set of geological horizons representing bedding planes. Do you recommend any mesh generators? – skytaker Apr 21 '17 at 21:46
  • @NicoSchlömer Thanks for sharing your project, do you have C++ examples for ITK and VTK? I am struggling to extract a tetrahedron unstructured mesh from a vtkImageData binary label file. – sc241 Nov 06 '19 at 13:39