一、概述
1、vtkPolyData是VTK(Visualization ToolKit)中用于表示多边行数据的核心类。它是VTK中最常用的数据类之一,广泛用于表示点、线、多表形等几何数据。
2、vtkPolyData数据由几何数据结构、拓扑结构数据和属性结构数据组成。几何结构数据主要是组成模型的点集(Points)。拓扑结构数据是按一定关系组成的单元数据(Cells)。属性数据与几何机构和拓扑结构数据相关联,可以标量、向量、张量,可以用来间接表示图形的颜色。
二、vtkPolyData核心组件
1、点(Points)
(1)存储几何数据,即点的坐标
(2)使用vtkPoints类来存储点数据
(3)每个店有一个唯一的索引(ID),从0开始
vtkNew<vtkPoints> points;
points->InsertNextPoint(0.0, 0.0, 0.0); // 点 0
points->InsertNextPoint(1.0, 0.0, 0.0); // 点 1
points->InsertNextPoint(0.0, 1.0, 0.0); // 点 2
points->InsertNextPoint(0.0, 0.0, 1.0); // 点 3
2、单元(cells)
(1)存储拓扑数据,即点如何连接成线、多边形等。
(2)使用vtkCellArray类来存储单元数据
(3)单元类型包括:顶点(Vertex)、线(Line)、三角形(Triangle)、四边形(Quad)、多边形(Polygon)
vtkNew<vtkCellArray> cells;
方式一:通过InsertPoint方法添加坐标点,然后创建四面体的四个三角形,通过GetPoints->SetId()方法设置点的索引,然后将四个三角形通过InsertNextCell方法添加进多变形单元中。
vtkNew<vtkTriangle> tran1;
tran1->GetPointIds()->SetId(0, 0);
tran1->GetPointIds()->SetId(1, 1);
tran1->GetPointIds()->SetId(2, 2);
vtkNew<vtkTriangle> tran2;
tran2->GetPointIds()->SetId(0, 0);
tran2->GetPointIds()->SetId(1, 1);
tran2->GetPointIds()->SetId(2, 3);
vtkNew<vtkTriangle> tran3;
tran3->GetPointIds()->SetId(0, 0);
tran3->GetPointIds()->SetId(1, 2);
tran3->GetPointIds()->SetId(2, 3);
vtkNew<vtkTriangle> tran4;
tran4->GetPointIds()->SetId(0, 1);
tran4->GetPointIds()->SetId(1, 2);
tran4->GetPointIds()->SetId(2, 3);
cells->InsertNextCell(tran1.Get());
cells->InsertNextCell(tran2.Get());
cells->InsertNextCell(tran3.Get());
cells->InsertNextCell(tran4.Get());
//方式二:一般情况下的3D模型都是由三角形组成的,且三角形非常渲染资源且渲染速度快。
cells->InsertNextCell(6);
cells->InsertCellPoint(1);
cells->InsertCellPoint(3);
cells->InsertCellPoint(0);
cells->InsertCellPoint(2);
cells->InsertCellPoint(1);
cells->InsertCellPoint(3);
3、属性数据(Attribute Data)
(1)存储与点或单元相关的属性数据,如标量、向量、法向量等
(2)分为两类:点数据(Point Data)与点相关的属性,单元数据(Cell Data)与单元相关的属性。
unsigned char red[3] = { 255, 0, 0 };
unsigned char green[3] = { 0, 255, 0 };
unsigned char blue[3] = { 0, 0, 255 };
vtkNew<vtkUnsignedCharArray> pointsColor;
pointsColor->SetNumberOfComponents(3);
pointsColor->InsertNextTypedTuple(red);
pointsColor->InsertNextTypedTuple(green);
pointsColor->InsertNextTypedTuple(blue);
pointsColor->InsertNextTypedTuple(red);
vtkNew<vtkPolyData> source;
source->SetPoints(points.Get());
source->SetPolys(cells.Get());
source->GetCellData()->SetScalars(pointsColor.Get());
三、主要功能
1、数据管理:设置和获取点、单元数据
vtkNew<vtkPolyData> source;
source->SetPoints(points); //设置点数据
source->SetPolys(cells); //设置单元数据
auto points = source->getPoints(); //获取点数据
auto cells = source->getPolys(): //获取单元数据
2、属性数据管理:设置和获取点、单元属性数据
vtkNew<vtkPolyData> source;
source->GetPointData()->SetScalars(pointsScalars); //设置点标量数据
source->GetPointData()->GetScalars(); //获取点标量数据
source->GetCellData()->SetScalars(cellScalars); //设置单元标量数据
source->GetCellData()->GetScalars(); //获取单元标量数据
3、数据查询 :获取点和单元数量
vtkNew<vtkPolyData> source;
source->GetNumberOfPoints();
source->GetNumberOfCells();
四、示例
#include <vtkActor.h>
#include <vtkCellArray.h>
#include <vtkDiskSource.h>
#include <vtkFeatureEdges.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkTriangle.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkUnsignedCharArray.h>
#include <vtkCellData.h>
#include <vtkPointData.h>
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
int main(int argc, char *argv[])
{
unsigned char red[3] = { 255, 0, 0 };
unsigned char green[3] = { 0, 255, 0 };
unsigned char blue[3] = { 0, 0, 255 };
vtkNew<vtkPoints> points;
points->InsertNextPoint(0.0, 0.0, 0.0);
points->InsertNextPoint(1.0, 0.0, 0.0);
points->InsertNextPoint(0.0, 1.0, 0.0);
points->InsertNextPoint(0.0, 0.0, 1.0);
vtkNew<vtkCellArray> cells;
cells->InsertNextCell(6);
cells->InsertCellPoint(1);
cells->InsertCellPoint(3);
cells->InsertCellPoint(0);
cells->InsertCellPoint(2);
cells->InsertCellPoint(1);
cells->InsertCellPoint(3);
vtkNew<vtkUnsignedCharArray> pointsColor;
pointsColor->SetNumberOfComponents(3);
pointsColor->InsertNextTypedTuple(red);
pointsColor->InsertNextTypedTuple(green);
pointsColor->InsertNextTypedTuple(blue);
pointsColor->InsertNextTypedTuple(red);
vtkNew<vtkPolyData> source;
source->SetPoints(points.Get());
source->SetStrips(cells.Get());
source->GetPointData()->SetScalars(pointsColor.Get());
vtkNew<vtkPolyDataMapper> coneMapper;
coneMapper->SetInputData(source.Get());
vtkNew<vtkActor> actor;
actor->SetMapper(coneMapper.Get());
vtkNew<vtkRenderer> render;
render->AddActor(actor.Get());
render->SetBackground(0.8, 0.4, 0.4);
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(render.Get());
vtkNew<vtkRenderWindowInteractor> iter;
iter->SetRenderWindow(renderWindow.Get());
vtkNew<vtkInteractorStyleTrackballCamera> camera;
iter->SetInteractorStyle(camera.Get());
iter->Initialize();
iter->Start();
return EXIT_SUCCESS;
}