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.
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( );
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With