[vtkusers] Extract Selection of Polydata
meakcey
meakcey at gmail.com
Thu Nov 22 16:21:09 EST 2018
Hi
In the example of ExtractSelectedIds, if I replace vtkUnstructuredGrid
template to vtkPolyData, and render the actor, nothing is seen.
https://lorensen.github.io/VTKExamples/site/Cxx/PolyData/ExtractSelectedIds/
I am working through PolyData and trying to delete selected points on the
render window like below.
It would be better to directly modify PolyData and assign back but could not
succeeded.
class interactorArea : public vtkInteractorStyleRubberBandPick{
public:
static interactorArea* New();
vtkTypeMacro(interactorArea, vtkInteractorStyleRubberBandPick);
interactorArea();
virtual void OnLeftButtonUp();
virtual void OnKeyPress();
virtual void OnKeyUp();
void SetPoints(vtkSmartPointer<vtkPolyData> points) {this->Points =
points;}
private:
bool areaSelected = false;
vtkSmartPointer<vtkIdTypeArray> selectedIds;
vtkSmartPointer<vtkPolyData> Points;
vtkSmartPointer<vtkActor> SelectedActor;
vtkSmartPointer<vtkDataSetMapper> SelectedMapper;
};
vtkStandardNewMacro(interactorArea);
interactorArea::interactorArea(){
this->SelectedMapper = vtkSmartPointer<vtkDataSetMapper>::New();
this->SelectedActor = vtkSmartPointer<vtkActor>::New();
this->SelectedActor->SetMapper(SelectedMapper);
}
void interactorArea::OnLeftButtonUp(){
qInfo() << "Area interactor left button up";
vtkInteractorStyleRubberBandPick::OnLeftButtonUp();
areaSelected = false;
if(this->CurrentMode == VTKISRBP_SELECT){
vtkPlanes* frustum =
static_cast<vtkAreaPicker*>(this->GetInteractor()->GetPicker())->GetFrustum();
vtkSmartPointer<vtkExtractGeometry> extractGeometry =
vtkSmartPointer<vtkExtractGeometry>::New();
extractGeometry->SetImplicitFunction(frustum);
extractGeometry->SetInputData(this->Points);
extractGeometry->Update();
vtkSmartPointer<vtkSelectVisiblePoints> visiblePoints =
vtkSmartPointer<vtkSelectVisiblePoints>::New();
visiblePoints->SetInputConnection(extractGeometry->GetOutputPort());
visiblePoints->SetRenderer(this->GetInteractor()->GetRenderWindow()->GetRenderers()->GetFirstRenderer());
visiblePoints->Update();
vtkSmartPointer<vtkVertexGlyphFilter> glyphFilter =
vtkSmartPointer<vtkVertexGlyphFilter>::New();
glyphFilter->SetInputConnection(extractGeometry->GetOutputPort());
glyphFilter->Update();
vtkPolyData* selected = glyphFilter->GetOutput();
vtkSmartPointer<vtkVertexGlyphFilter> visibleFilter =
vtkSmartPointer<vtkVertexGlyphFilter>::New();
visibleFilter->SetInputConnection(visiblePoints->GetOutputPort());
visibleFilter->Update();
vtkPolyData* visibles = visibleFilter->GetOutput();
std::cout << "Selected points " << selected->GetNumberOfPoints() <<
std::endl;
std::cout << "Visible points " << visibles->GetNumberOfPoints() <<
std::endl;
//std::cout << "Selected " << selected->GetNumberOfCells() << "
cells." << std::endl;
if(selected->GetNumberOfPoints() <= 0){
std::cout << "Nothing selected" << std::endl;
return;
}
areaSelected = true;
selectedIds = vtkSmartPointer<vtkIdTypeArray>::New();
selectedIds =
vtkIdTypeArray::SafeDownCast(visibles->GetPointData()->GetArray("pointIds"));
unsigned int idsize = selectedIds->GetNumberOfTuples();
this->SelectedMapper->SetInputData(visibles);
this->SelectedMapper->ScalarVisibilityOff();
areaSelected = true;
this->SelectedActor->GetProperty()->SetColor(1.0, 0.0, 0.0);
//(R,G,B)
this->SelectedActor->GetProperty()->SetPointSize(12);
//this->CurrentRenderer->AddActor(SelectedActor);
this->GetInteractor()->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(SelectedActor);
this->GetInteractor()->GetRenderWindow()->Render();
this->HighlightProp(NULL);
}
}
void interactorArea::OnKeyUp(){
vtkInteractorStyleTrackballCamera::OnKeyUp();
vtkRenderWindowInteractor *rwi = this->Interactor;
std::string key = rwi->GetKeySym();
if(key == "Delete" && areaSelected){
vtkSmartPointer<vtkActorCollection> actorCollection =
vtkSmartPointer<vtkActorCollection>::New();
actorCollection =
this->GetInteractor()->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->GetActors();
unsigned int actors = actorCollection->GetNumberOfItems();
for(unsigned int j=0; j<actors; j++)
this->GetInteractor()->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->RemoveActor(actorCollection->GetLastActor());
//this->GetInteractor()->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->RemoveAllViewProps();
//this->GetInteractor()->GetRenderWindow()->Render();
vtkSmartPointer<vtkPolyData> extractedPoints =
vtkSmartPointer<vtkPolyData>::New();
unsigned int selsize = selectedIds->GetNumberOfTuples();
unsigned int allsize = Points->GetNumberOfPoints();
vtkSmartPointer<vtkSelectionNode> selectionNode =
vtkSmartPointer<vtkSelectionNode>::New();
selectionNode->SetFieldType(vtkSelectionNode::POINT);
selectionNode->SetContentType(vtkSelectionNode::INDICES);
selectionNode->SetSelectionList(selectedIds);
vtkSmartPointer<vtkSelection> selection =
vtkSmartPointer<vtkSelection>::New();
selection->AddNode(selectionNode);
vtkSmartPointer<vtkExtractSelectedIds> extractSelectedIds =
vtkSmartPointer<vtkExtractSelectedIds>::New();
extractSelectedIds->SetInputData(0, Points);
extractSelectedIds->SetInputData(1, selection);
extractSelectedIds->Update();
vtkSmartPointer<vtkUnstructuredGrid> selected =
vtkSmartPointer<vtkUnstructuredGrid>::New();
//vtkSmartPointer<vtkPolyData> selected =
vtkSmartPointer<vtkPolyData>::New(); // This one does not getting rendered
selected->ShallowCopy(extractSelectedIds->GetOutput());
std::cout << "There are " << selected->GetNumberOfPoints() << "
points in the selection." << std::endl;
selectionNode->GetProperties()->Set(vtkSelectionNode::INVERSE(), 1);
//invert the selection
extractSelectedIds->Update();
vtkSmartPointer<vtkUnstructuredGrid> notSelected =
vtkSmartPointer<vtkUnstructuredGrid>::New();
//vtkSmartPointer<vtkPolyData> notSelected =
vtkSmartPointer<vtkPolyData>::New(); // This one does not getting rendered
notSelected->ShallowCopy(extractSelectedIds->GetOutput());
std::cout << "There are " << notSelected->GetNumberOfPoints() << "
points NOT in the selection." << std::endl;
vtkSmartPointer<vtkDataSetMapper> leftMapper =
vtkSmartPointer<vtkDataSetMapper>::New();
leftMapper->SetInputData(notSelected);
vtkSmartPointer<vtkActor> leftActor =
vtkSmartPointer<vtkActor>::New();
leftActor->SetMapper(leftMapper);
vtkSmartPointer<vtkGeometryFilter> geometryFilter =
vtkSmartPointer<vtkGeometryFilter>::New();
geometryFilter->SetInputData(notSelected);
geometryFilter->Update();
this->Points = geometryFilter->GetOutput();
leftActor->GetProperty()->SetPointSize(12);
leftActor->GetProperty()->EdgeVisibilityOff();
this->GetInteractor()->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(leftActor);
this->GetInteractor()->GetRenderWindow()->Render();
this->HighlightProp(NULL);
areaSelected = false;
}
}
Also another problem is, I could not delete after first deletion.
Selection is working but, when I press delete nothing is getting deleted.
I dont know is it related with vtkUnstructuredGrid and vtkPolyData
conversion.
--
Sent from: http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html
More information about the vtkusers
mailing list