小结:本博文主要讲解vtkPolyData接口及常用的方法实现原理。
vtkPolyData
1描述
vtkPolyData是一系列的数据集包括vertices,lines,polygons,triangle strips。
vtkPolyData是vtkDataSet的具体实现,代表了一系列的几何结构,包括vertices,lines,polygons,triangle strips,以及Point和Cell属性结构。
vtkPolyData支持的Cell类型为vtkVertex, vtkPolyVertex, vtkLine, vtkPolyLine, vtkTriangle, vtkQuad, vtkPolygon, and vtkTriangleStrip。
注意:
1. vtkPolyData实现了代表0D的vertices,1D的Lines,2D的Polygons和TriangleStrips;因此vtkPolyData可以创建由混合单元格类型组成的vtkPolyData实例。由于接口的设计特性,vtkPolyData在插值时必须按顺序进行处理:按顶点(vtkVertex和vtkPolyVertex)的顺序,线(vtkLine和vtkPolyLine),多边形(vtkTriangle,vtkQuad,vtkPolygon) 和 三角带(vtkTriangleStrip)。小伙伴可以参考vtkClipClosedSurface的具体实现来学习具体过程,附上相关博文链接:VTK中Clip/Trim总结_雪易的博客-CSDN博客
2. 在Filter的具体实现中,可能会将vtkTriangleStrip转换为vtkTriangles进行处理(vtkClipClosedSurface就是这么操作的);也可能会将vtkDecimatePro转换为vtkTriangles或vtkTriangleStrip;
2构建方法
#include <vtkActor.h>
#include <vtkCellArray.h>
#include <vtkNamedColors.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkPolygon.h>
#include <vtkProperty.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>
int main(int, char *[])
{
vtkSmartPointer<vtkNamedColors> colors =
vtkSmartPointer<vtkNamedColors>::New();
// Setup four points
vtkSmartPointer<vtkPoints> points =
vtkSmartPointer<vtkPoints>::New();
points->InsertNextPoint(0.0, 0.0, 0.0);
points->InsertNextPoint(1.0, 0.0, 0.0);
points->InsertNextPoint(1.0, 1.0, 0.0);
points->InsertNextPoint(0.0, 1.0, 0.0);
// Create the polygon
vtkSmartPointer<vtkPolygon> polygon =
vtkSmartPointer<vtkPolygon>::New();
polygon->GetPointIds()->SetNumberOfIds(4); //make a quad
polygon->GetPointIds()->SetId(0, 0);
polygon->GetPointIds()->SetId(1, 1);
polygon->GetPointIds()->SetId(2, 2);
polygon->GetPointIds()->SetId(3, 3);
// Add the polygon to a list of polygons
vtkSmartPointer<vtkCellArray> polygons =
vtkSmartPointer<vtkCellArray>::New();
polygons->InsertNextCell(polygon);
// Create a PolyData
vtkSmartPointer<vtkPolyData> polygonPolyData =
vtkSmartPointer<vtkPolyData>::New();
polygonPolyData->SetPoints(points);
polygonPolyData->SetPolys(polygons);
// Create a mapper and actor
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputData(polygonPolyData);
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(
colors->GetColor3d("Silver").GetData());
// Visualize
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetWindowName("Polygon");
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(actor);
renderer->SetBackground(colors->GetColor3d("Salmon").GetData());
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
3涉及的方法
1)GetCellNeighbors-获取指定Cell,指定点的相邻Cell
/**
* Get the neighbors at an edge. More efficient than the general
* GetCellNeighbors(). Assumes links have been built (with BuildLinks()),
* and looks specifically for edge neighbors.
*/
void GetCellEdgeNeighbors(vtkIdType cellId, vtkIdType p1, vtkIdType p2, vtkIdList* cellIds);
2)获取PolyData的全部内容
vtkPolyData* pd = ...
//获取pd中的点
vtkPoints* inPts = pd->GetPoints();
vtkIdType numPts = pd->GetNumberOfPoints();
//获取pd中所有的Cells,包括vertex,line,poly,strip
vtkCellArray* vertexs = pd->GetVerts();
vtkCellArray* lines = pd->GetLines();
vtkCellArray* polys = pd->GetPolys();
vtkCellArray* strips = pd->GetStrips();
vtkIdType numVertexs = pd->GetNumberOfVerts();
vtkIdType numLines = pd->GetNumberOfLines();
vtkIdType numPolys = pd->GetNumberOfPolys();
vtkIdType numStrips = pd->GetNumberOfStrips();
//numcells = numVertexs+numLines+numPolys+numStrips
vtkIdType numCells = pd->GetNumberOfCells();
vtkPolyData* pd = ...
vtkIdType cellId;
vtkIdType numCells = pd->GetNumberOfCells();
vtkGenericCell* cell;
vtkPoints* cellPts;
vtkIdList* cellIds;
for (cellId = 0; cellId < numCells; cellId++)
{
pd->GetCell(cellId, cell);
cellPts = cell->GetPoints();
cellIds = cell->GetPointIds();
}
3) vtkPolyData->vtkUnstructuredGrid
vtkUnstructuredGrid* uGrid = vtkUnstructuredGrid::New();
uGrid->SetPoints(inPts);
const vtkIdType* pts = nullptr;
vtkIdType npts;
for (vtkIdType id = 0; id < numCells; id++)
{
pd->GetCellPoints(id, npts, pts);
uGrid->InsertNextCell(pd->GetCellType(id), npts, pts);
}