我有一个软件,我从点云地图中选择 5 个点,如下所示:
void Visualizer::pointPickingEventOccurred (const pcl::visualization::PointPickingEvent &event)
{
std::cout << "[INOF] Point picking event occurred." << std::endl;
float x, y, z;
if (event.getPointIndex () == -1)
{
return;
}
event.getPoint(x, y, z);
std::cout << "[INOF] Point coordinate ( " << x << ", " << y << ", " << z << ")" << std::endl;
points->InsertNextPoint(x, y, z);
}
这样做之后,我通过按下按钮使用 vtkPolyLine 和 3DGlyph 在点之间绘制一条线:
void Visualizer::drawLine(){
// Create one sphere for all
sphere->SetPhiResolution(21);
sphere->SetThetaResolution(21);
sphere->SetRadius(.08);
vtkNew<vtkNamedColors> colors;
vtkNew<vtkPolyLine> polyLine;
polyLine->GetPointIds()->SetNumberOfIds(5);
for (unsigned int i = 0; i < 5; i++)
{
polyLine->GetPointIds()->SetId(i, i);
}
// Create a cell array to store the lines in and add the lines to it
vtkNew<vtkCellArray> cells;
cells->InsertNextCell(polyLine);
// Create a polydata to store everything in
vtkNew<vtkPolyData> polyData;
// Add the points to the dataset
polyData->SetPoints(points);
// Add the lines to the dataset
polyData->SetLines(cells);
// Create cells
vtkSmartPointer<vtkUnstructuredGrid> ug = vtkSmartPointer<vtkUnstructuredGrid>::New();
ug->SetPoints(points);
ug->InsertNextCell(polyLine->GetCellType(), polyLine->GetPointIds());
// Label the points
vtkSmartPointer<vtkLabeledDataMapper> labelMapper = vtkSmartPointer<vtkLabeledDataMapper>::New();
labelMapper->SetInputData(ug);
vtkSmartPointer<vtkActor2D> labelActor = vtkSmartPointer<vtkActor2D>::New();
labelActor->SetMapper(labelMapper);
viewer->addActorToRenderer(labelActor);
// Glyph the points
vtkSmartPointer<vtkGlyph3DMapper> pointMapper = vtkSmartPointer<vtkGlyph3DMapper>::New();
pointMapper->SetInputData(ug);
pointMapper->SetSourceConnection(sphere->GetOutputPort());
pointMapper->ScalingOff();
pointMapper->ScalarVisibilityOff();
vtkSmartPointer<vtkActor> pointActor = vtkSmartPointer<vtkActor>::New();
pointActor->SetMapper(pointMapper);
pointActor->GetProperty()->SetDiffuseColor(colors->GetColor3d("Banana").GetData());
pointActor->GetProperty()->SetSpecular(.6);
pointActor->GetProperty()->SetSpecularColor(1.0, 1.0, 1.0);
pointActor->GetProperty()->SetSpecularPower(100);
viewer->addActorToRenderer(pointActor);
// Setup actor and mapper
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputData(polyData);
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(colors->GetColor3d("Tomato").GetData());
actor->GetProperty()->EdgeVisibilityOn();
actor->GetProperty()->SetLineWidth(5);
actor->GetProperty()->SetOpacity(.5);
viewer->addActorToRenderer(actor);
}
创建线后,我显然无法创建另一条线,因为points
list 仍然具有先前点的值。
我试图清空“drawline”函数末尾的点列表:
vtkSmartPointer<vtkPoints> newPoints =
vtkSmartPointer<vtkPoints>::New();
for(vtkIdType i = 0; i < points->GetNumberOfPoints(); i++)
{
if(i != id)
{
double p[3];
points->GetPoint(i,p);
newPoints->InsertNextPoint(p);
}
}
points->ShallowCopy(newPoints);
但我仍然无法创建另一条线