不需要做任何事情。只需要一個使用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");
}
我想你可以編寫自己的類,它從vtkPolyData(和你正在使用其他可能的數據類型)繼承並實現必要的功能? – Dan 2013-04-29 14:42:44
@Dan謝謝,但像PointList這樣的數據結構(一般來說)已經在我正在使用的庫中設置和修復(儘管是通用的)。我期望編寫者是通用的,我只需要適應我的接口或迭代器到數據類型元素。在VTK中有明確的抽象概念:多邊形,點等...對於我來說,這種類型是固定的,有點令人吃驚。 – tmaric 2013-04-29 15:54:29
如果我理解正確,您正嘗試使用其中一個VTK編寫器類從您自己的內部存儲佈局寫出數據。這不是一個明智的方法,因爲VTK編寫者被設計爲在整個VTK框架內工作。如果你堅持使用VTK作家類,那麼實現這一目標的唯一明智方式就在於Hal的答案。如果你堅持只是堅持自己的數據存儲框架,那麼最好的解決方案就是從頭開始編寫自己的作者。正如哈爾所說,在嘗試之前不要預先判斷速度。 – 2013-04-29 20:12:38