我使用ParaView进行数据可视化,因此我需要以支持的格式(如ASCII .vtk)编写自己的数据。为此,我写了一个传统的ASCII .vtk编写器,它工作正常。但是,由于我编写了一个并行运行的写密集型应用程序,我想尝试使用vtk XML编写器。
是否可以使用来自其他库的类重用编写器?
从vtkPolyDataWriter documentation看,我注意到VTK是面向对象的,编写者严重依赖于使用端口的“可视化管道”流程,例如:
353 vtkNew<vtkPolyDataWriter> writer0;
354 writer0->SetFileName( "./hyperTreeGridContour.vtk" );
355 writer0->SetInputConnection( contour->GetOutputPort() );
356 writer0->Write();
来自vtkPolyData Example的。
那么可以在VTK层次结构之外的类中重用the writers吗?
可写对象应该是vtkObject,但最后写出的vtkPolyData类依赖于VTK特定类型。
答案 0 :(得分:2)
无需做任何花哨的事情。只需要一个使用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");
}