VTK
图形vtkPolyData
数据生成与显示
引言
多变形数据集vtkPolyData
由顶点(Vertex)、多顶点(Polyvertex)、线(Line)、折线(Polyline)和三角形条带(Triangle Strip)等单元构成,多边形数据是不规则结构的,并且多边形数据集的单元再拓扑维度上有多种类型。
vtkPolyData
数据生成与显示
- 几何结构数据:组成模型的点集;
- 拓扑结构数据:这些点根据一定的连接关系组成的单元数据;表明集合点集之间的拓扑关系
- 属性数据:与几何结构数据和拓扑结构数据相关联,属性数据可以是标量、向量或者张量。
CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 3.3 FATAL_ERROR)
PROJECT(PolyDataDemo)
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})
ADD_EXECUTABLE(PolyDataDemo PolyDataDemo.cxx)
TARGET_LINK_LIBRARIES(PolyDataDemo ${VTK_LIBRARIES})
PolyDataDemo.cxx
#include<vtkSmartPointer.h>
#include<vtkConeSource.h>
#include<vtkPolyData.h>
#include<vtkCellArray.h>
#include<vtkCell.h>
#include<vtkPolyDataMapper.h>
#include<vtkActor.h>
#include<vtkRenderer.h>
#include<vtkRenderWindow.h>
#include<vtkRenderWindowInteractor.h>
int main(int argc, char const *argv[])
{
vtkSmartPointer<vtkConeSource> coneSource = vtkSmartPointer<vtkConeSource>::New();
coneSource->Update();
vtkSmartPointer<vtkPolyData> polyData = coneSource->GetOutput();
unsigned int nPoints = polyData->GetNumberOfPoints();
unsigned int nCells = polyData->GetNumberOfCells();
std::cout << "几何数据(点数):" << nPoints << std::endl;
std::cout << "拓扑数据(单元):" << nCells << std::endl;
//Test
vtkSmartPointer<vtkCellArray> polyAry = vtkSmartPointer<vtkCellArray>::New();
vtkSmartPointer<vtkCellArray> lineAry = vtkSmartPointer<vtkCellArray>::New();
vtkIdType iCells = polyData->GetNumberOfCells();
for (vtkIdType i = 0; i < iCells; ++i) {
vtkCell* cell = polyData->GetCell(i);
if (cell->GetNumberOfPoints() > 0) {
vtkIdType iId = cell->GetPointId(0);
vtkIdType iId1 = cell->GetPointId(cell->GetNumberOfPoints() - 1);
if (iId != iId1) {
lineAry->InsertNextCell(cell);
}
else {
polyAry->InsertNextCell(cell);
}
}
}
std::cout << "PolyLine 单元:" << lineAry->GetNumberOfCells() << std::endl;
std::cout << "封闭多边形单元:" << polyAry->GetNumberOfCells() << std::endl;
vtkSmartPointer<vtkPolyDataMapper> polyDataMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
polyDataMapper->SetInputData(polyData);
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(polyDataMapper);
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);
renderer->SetBackground(10, 0, 0);
vtkSmartPointer<vtkRenderWindow> renderWin = vtkSmartPointer<vtkRenderWindow>::New();
renderWin->AddRenderer(renderer);
renderWin->SetSize(640, 480);
renderWin->SetWindowName("PolyData Structure Learing");
vtkSmartPointer<vtkRenderWindowInteractor>rwi =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
rwi->SetRenderWindow(renderWin);
rwi->Initialize();
rwi->Start();
return 0;
}