1

我正在使用 ParaView 进行数据可视化,因此我需要以支持的格式(如 ASCII .vtk)编写我自己的数据。为此,我编写了一个传统的 ASCII .vtk writer,它运行良好。但是,由于我编写了一个并行运行的写入密集型应用程序,因此我想尝试使用 vtk XML 编写器。

是否可以使用来自另一个库的类重新使用编写器?

通过查看vtkPolyDataWriter 文档,我注意到 VTK 是面向对象的,并且作者严重依赖于使用端口的“可视化管道”流程,例如:

 353     vtkNew<vtkPolyDataWriter> writer0;
 354     writer0->SetFileName( "./hyperTreeGridContour.vtk" );
 355     writer0->SetInputConnection( contour->GetOutputPort() );
 356     writer0->Write();

来自vtkPolyData 示例

那么是否可以重新使用具有 VTK 层次结构之外的类的编写器?

可写对象应该是一个 vtkObject,但最后被写出的vtkPolyData类更依赖于 VTK 特定类型。

4

1 回答 1

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");
}
于 2013-04-29T16:23:56.623 回答