一、前言
vtkPlaneSource创建平面,只可以创建平行四边形的平面,根据一个起点和两个终点创建法向量创建平面。但是当有创建多个点围成不规则平面的需求时,该怎么创建显示呢?
在网上查了资料,可以使用vtkPolyData拓扑结构。
二、拓扑结构
下面这篇文章很详细的说明了相关的知识理论,可以参考学习。
VTK 初步 (2) ----- 基本数据结构_vtkcellarray-CSDN博客
三、代码示例
1.点
//点
void Widget::on_point_clicked()
{
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkCellArray> cellArray = vtkSmartPointer<vtkCellArray>::New();
vtkIdType pid[12] = {0,1,2,3,4,5,6,7,8,9,10,11};
points->InsertNextPoint(1, 1, 1);
points->InsertNextPoint(1, 2, 0);
points->InsertNextPoint(1, 1, -1);
points->InsertNextPoint(1, -1, -1);
points->InsertNextPoint(1, -2, 0);
points->InsertNextPoint(1, -1, 1);
points->InsertNextPoint(-1, 1, 1);
points->InsertNextPoint(-1, 2, 0);
points->InsertNextPoint(-1, 1, -1);
points->InsertNextPoint(-1, -1, -1);
points->InsertNextPoint(-1, -2, 0);
points->InsertNextPoint(-1, -1, 1);
cellArray->InsertNextCell(12,pid);
vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
polyData->SetPoints(points);
polyData->SetVerts(cellArray);//点
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputData(polyData);
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->GetProperty()->SetColor((float)255/255, (float)255/255, (float)0/255);
actor->GetProperty()->SetPointSize(5);
actor->SetMapper(mapper);
renderer->AddActor(actor);
renderer->ResetCamera();
ui->vtk_widget->GetRenderWindow()->Render();
}
2.线
//线
void Widget::on_line_clicked()
{
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkCellArray> cellArray = vtkSmartPointer<vtkCellArray>::New();
vtkIdType pid[13] = {0,1,2,3,4,5,6,7,8,9,10,11,12};
points->InsertNextPoint(1, 1, 1);
points->InsertNextPoint(1, 2, 0);
points->InsertNextPoint(1, 1, -1);
points->InsertNextPoint(1, -1, -1);
points->InsertNextPoint(1, -2, 0);
points->InsertNextPoint(1, -1, 1);
points->InsertNextPoint(-1, -1, 1);
points->InsertNextPoint(-1, -2, 0);
points->InsertNextPoint(-1, -1, -1);
points->InsertNextPoint(-1, 1, -1);
points->InsertNextPoint(-1, 2, 0);
points->InsertNextPoint(-1, 1, 1);
points->InsertNextPoint(1, 1, 1);
cellArray->InsertNextCell(13,pid);
vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
polyData->SetPoints(points);
polyData->SetLines(cellArray);//线
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputData(polyData);
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->GetProperty()->SetColor((float)255/255, (float)0/255, (float)255/255);
actor->GetProperty()->SetLineWidth(3);
actor->SetMapper(mapper);
renderer->AddActor(actor);
renderer->ResetCamera();
ui->vtk_widget->GetRenderWindow()->Render();
}
3.面
//面
void Widget::on_poly_clicked()
{
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkCellArray> cellArray = vtkSmartPointer<vtkCellArray>::New();
vtkIdType pid[6] = {0,1,2,3,4,5};
points->InsertNextPoint(1, 1, 1);
points->InsertNextPoint(1, 2, 0);
points->InsertNextPoint(1, 1, -1);
points->InsertNextPoint(1, -1, -1);
points->InsertNextPoint(1, -2, 0);
points->InsertNextPoint(1, -1, 1);
cellArray->InsertNextCell(6,pid);
vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
polyData->SetPoints(points);
polyData->SetPolys(cellArray);//面
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputData(polyData);
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->GetProperty()->SetColor((float)0/255, (float)255/255, (float)255/255);
actor->SetMapper(mapper);
renderer->AddActor(actor);
renderer->ResetCamera();
ui->vtk_widget->GetRenderWindow()->Render();
}
4.多个面
//多个面
void Widget::on_cube_clicked()
{
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkCellArray> cellArray = vtkSmartPointer<vtkCellArray>::New();
vtkIdType pid[18] = {0,1,2,
0,2,3,
0,3,4,
0,4,5,
0,5,6,
0,6,1
};
points->InsertNextPoint(0, 0, 0);
points->InsertNextPoint(-1, -1, 1);
points->InsertNextPoint(-1, -2, 0);
points->InsertNextPoint(-1, -1, -1);
points->InsertNextPoint(-1, 1, -1);
points->InsertNextPoint(-1, 2, 0);
points->InsertNextPoint(-1, 1, 1);
cellArray->InsertNextCell(18,pid);
vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
polyData->SetPoints(points);
polyData->SetPolys(cellArray);//面
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputData(polyData);
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->GetProperty()->SetColor((float)128/255, (float)128/255, (float)128/255);
actor->SetMapper(mapper);
renderer->AddActor(actor);
renderer->ResetCamera();
ui->vtk_widget->GetRenderWindow()->Render();
}
5.三角带
//三角带
void Widget::on_strip_clicked()
{
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkCellArray> cellArray = vtkSmartPointer<vtkCellArray>::New();
vtkIdType pid[12] = {0,1,2,3,4,5,6,7,8,9,10,11};
points->InsertNextPoint(1, 1, 1);
points->InsertNextPoint(1, 2, 0);
points->InsertNextPoint(1, 1, -1);
points->InsertNextPoint(1, -1, -1);
points->InsertNextPoint(1, -2, 0);
points->InsertNextPoint(1, -1, 1);
points->InsertNextPoint(-1, 1, 1);
points->InsertNextPoint(-1, 2, 0);
points->InsertNextPoint(-1, 1, -1);
points->InsertNextPoint(-1, -1, -1);
points->InsertNextPoint(-1, -2, 0);
points->InsertNextPoint(-1, -1, 1);
cellArray->InsertNextCell(12,pid);
vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
polyData->SetPoints(points);
polyData->SetStrips(cellArray);//三角带
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputData(polyData);
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->GetProperty()->SetColor((float)255/255, (float)128/255, (float)128/255);
actor->GetProperty()->SetPointSize(3);
actor->SetMapper(mapper);
renderer->AddActor(actor);
renderer->ResetCamera();
ui->vtk_widget->GetRenderWindow()->Render();
}
总体