vtkPlaneSource创建平面,只可以创建平行四边形的平面,根据一个起点和两个终点创建法向量创建平面。但是当有创建多个点围成不规则平面的需求时,该怎么创建显示呢?
在网上查了资料,可以使用vtkPolyData拓扑结构。
下面这篇文章很详细的说明了相关的知识理论,可以参考学习。
VTK 初步 (2) ----- 基本数据结构_vtkcellarray-CSDN博客
//点
void Widget::on_point_clicked()
{
vtkSmartPointer points = vtkSmartPointer::New();
vtkSmartPointer cellArray = vtkSmartPointer::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 polyData = vtkSmartPointer::New();
polyData->SetPoints(points);
polyData->SetVerts(cellArray);//点
vtkSmartPointer mapper = vtkSmartPointer::New();
mapper->SetInputData(polyData);
vtkSmartPointer actor = vtkSmartPointer::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();
}
//线
void Widget::on_line_clicked()
{
vtkSmartPointer points = vtkSmartPointer::New();
vtkSmartPointer cellArray = vtkSmartPointer::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 polyData = vtkSmartPointer::New();
polyData->SetPoints(points);
polyData->SetLines(cellArray);//线
vtkSmartPointer mapper = vtkSmartPointer::New();
mapper->SetInputData(polyData);
vtkSmartPointer actor = vtkSmartPointer::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();
}
//面
void Widget::on_poly_clicked()
{
vtkSmartPointer points = vtkSmartPointer::New();
vtkSmartPointer cellArray = vtkSmartPointer::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 polyData = vtkSmartPointer::New();
polyData->SetPoints(points);
polyData->SetPolys(cellArray);//面
vtkSmartPointer mapper = vtkSmartPointer::New();
mapper->SetInputData(polyData);
vtkSmartPointer actor = vtkSmartPointer::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();
}
//多个面
void Widget::on_cube_clicked()
{
vtkSmartPointer points = vtkSmartPointer::New();
vtkSmartPointer cellArray = vtkSmartPointer::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 polyData = vtkSmartPointer::New();
polyData->SetPoints(points);
polyData->SetPolys(cellArray);//面
vtkSmartPointer mapper = vtkSmartPointer::New();
mapper->SetInputData(polyData);
vtkSmartPointer actor = vtkSmartPointer::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();
}
//三角带
void Widget::on_strip_clicked()
{
vtkSmartPointer points = vtkSmartPointer::New();
vtkSmartPointer cellArray = vtkSmartPointer::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 polyData = vtkSmartPointer::New();
polyData->SetPoints(points);
polyData->SetStrips(cellArray);//三角带
vtkSmartPointer mapper = vtkSmartPointer::New();
mapper->SetInputData(polyData);
vtkSmartPointer actor = vtkSmartPointer::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();
}
总体