不需要做任何花哨的事情。只需有一个使用 VTK 类的 WriteVTKPolyData() 方法。
#include <vtkXMLPolyDataWriter.h>
#include <vtkSmartPointer.h>
#include <vtkCellArray.h>
#include <vtkPolyData.h>
#include <vtkVersion.h>
#include <vtkPoints.h>
class MyClass {
/* ... */
public:
void WriteVTKPolyData(const char * file_name) const;
};
void MyClass::WriteVTKPolyData(const char * file_name) const {
vtkSmartPointer<vtkPolyData> polyData =
vtkSmartPointer<vtkPolyData>::New();
vtkSmartPointer<vtkPoints> points =
vtkSmartPointer<vtkPoints>::New();
// EXAMPLE CODE POPULATING VTKPOLYDATA
points->InsertNextPoint(+1, +1, +1);
points->InsertNextPoint(-1, -1, +1);
points->InsertNextPoint(-1, +1, -1);
points->InsertNextPoint(+1, -1, -1);
polyData->SetPoints(points);
vtkSmartPointer<vtkCellArray> polys =
vtkSmartPointer<vtkCellArray>::New();
vtkIdType cell[3];
cell[0] = 0; cell[1] = 2; cell[2] = 1;
polys->InsertNextCell(3,cell);
cell[0] = 1; cell[1] = 2; cell[2] = 3;
polys->InsertNextCell(3,cell);
cell[0] = 2; cell[1] = 0; cell[2] = 3;
polys->InsertNextCell(3,cell);
cell[0] = 3; cell[1] = 0; cell[2] = 1;
polys->InsertNextCell(3,cell);
polyData->SetPolys(polys);
// END EXAMPLE CODE POPULATING
vtkSmartPointer<vtkXMLPolyDataWriter> writer =
vtkSmartPointer<vtkXMLPolyDataWriter>::New();
writer->SetFileName(file_name);
#if VTK_MAJOR_VERSION <= 5
writer->SetInput(polyData);
#else
writer->SetInputData(polyData);
#endif
writer->Write();
}
int main(int argc, char ** argv) {
MyClass myObject;
myObject.WriteVTKPolyData("out.vtp");
}
如果您想在编辑点和多边形时将它们保留在内存中:
#include <vtkXMLPolyDataWriter.h>
#include <vtkSmartPointer.h>
#include <vtkCellArray.h>
#include <vtkPolyData.h>
#include <vtkVersion.h>
#include <vtkPoints.h>
class MyClass {
vtkSmartPointer<vtkPolyData> m_PolyData;
vtkSmartPointer<vtkPoints> m_Points;
vtkSmartPointer<vtkCellArray> m_Polygons;
/* ... */
public:
/* ... */
MyClass();
void WriteVTKPolyData(const char * file_name) const;
void f();
};
MyClass::MyClass() :
m_PolyData(vtkSmartPointer<vtkPolyData>::New()),
m_Points(vtkSmartPointer<vtkPoints>::New()),
m_Polygons(vtkSmartPointer<vtkCellArray>::New())
{
m_PolyData->SetPoints(m_Points);
m_PolyData->SetPolys(m_Polygons);
}
void MyClass::f() {
// EXAMPLE CODE POPULATING VTKPOLYDATA
m_Points->InsertNextPoint(+1, +1, +1);
m_Points->InsertNextPoint(-1, -1, +1);
m_Points->InsertNextPoint(-1, +1, -1);
m_Points->InsertNextPoint(+1, -1, -1);
vtkIdType cell[3];
cell[0] = 0; cell[1] = 2; cell[2] = 1;
m_Polygons->InsertNextCell(3,cell);
cell[0] = 1; cell[1] = 2; cell[2] = 3;
m_Polygons->InsertNextCell(3,cell);
cell[0] = 2; cell[1] = 0; cell[2] = 3;
m_Polygons->InsertNextCell(3,cell);
cell[0] = 3; cell[1] = 0; cell[2] = 1;
m_Polygons->InsertNextCell(3,cell);
// END EXAMPLE CODE POPULATING
}
void MyClass::WriteVTKPolyData(const char * file_name) const {
vtkSmartPointer<vtkXMLPolyDataWriter> writer =
vtkSmartPointer<vtkXMLPolyDataWriter>::New();
writer->SetFileName(file_name);
#if VTK_MAJOR_VERSION <= 5
writer->SetInput(m_PolyData);
#else
writer->SetInputData(m_PolyData);
#endif
writer->Write();
}
int main(int argc, char ** argv) {
MyClass myObject;
myObject.f();
myObject.WriteVTKPolyData("out.vtp");
}