开发环境:
- Windows 11 家庭中文版
- Microsoft Visual Studio Community 2019
- VTK-9.3.0.rc0
- vtk-example
- 参考代码
demo解决问题:不同阶段添加多个单元对象。 定义一个点集和一个单元集合,单元的类型可以是点、三角形、矩形、多边形等基本图形。只有定义了单元数据才能显示该图形数据。
关键类:vtkPoints、vtkCellArray、vtkPolyData
vtkPolyData可以看看知乎这位老兄的总结:https://zhuanlan.zhihu.com/p/336743251
prj name: AddCell
#include <vtkCellArray.h>
#include <vtkNew.h>
#include <vtkPolyData.h>
#include <vtkTriangle.h>int main(int, char*[])
{// Create a trianglevtkNew<vtkPoints> points;points->InsertNextPoint(1.0, 0.0, 0.0);points->InsertNextPoint(0.0, 0.0, 0.0);points->InsertNextPoint(0.0, 1.0, 0.0);vtkNew<vtkTriangle> triangle;triangle->GetPointIds()->SetId(0, 0);triangle->GetPointIds()->SetId(1, 1);triangle->GetPointIds()->SetId(2, 2);//构造cell array 并插入一个单元对象的索引,插入操作是深拷贝vtkNew<vtkCellArray> triangles;triangles->InsertNextCell(triangle);// Create a polydata objectvtkNew<vtkPolyData> polyData;// Add the geometry and topology to the polydatapolyData->SetPoints(points);polyData->SetPolys(triangles);std::cout << "There are " << polyData->GetNumberOfCells() << " cells."<< std::endl;polyData->GetPolys()->InsertNextCell(triangle);//获取到cell array后再插入一个单元对象std::cout << "There are " << polyData->GetNumberOfCells() << " cells."<< std::endl;return EXIT_SUCCESS;
}