I. Introduction
vtkPlaneSource creates a plane. It can only create a parallelogram plane. It creates a plane based on a starting point and two end points and creates a normal vector. But when there is a need to create multiple points forming an irregular plane, how to create a display?
I checked the information online and found that vtkPolyData topology can be used.
2. Topology
The following article explains the relevant knowledge theory in detail, you can refer to it for study.
VTK preliminary (2) ----- Basic data structure_vtkcellarray-CSDN blog
3. Code examples
1 o'clock
//点
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. Line
//线
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. Noodles
//面
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. Multiple sides
//多个面
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.Triangle belt
//三角带
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();
}
overall