// From http://www.vtk.org/Wiki/VTK/Examples/Cxx/Picking/CellPicking void VtkCustomInteractorStyle::OnLeftButtonDown() { if (!_data) return vtkInteractorStyleTrackballCamera::OnLeftButtonDown(); if (_alternateMouseActions) { // Get the location of the click (in window coordinates) int* pos = this->GetInteractor()->GetEventPosition(); vtkSmartPointer<vtkCellPicker> picker = vtkSmartPointer<vtkCellPicker>::New(); picker->SetTolerance(0.0005); // Pick from this location. picker->Pick(pos[0], pos[1], 0, this->GetDefaultRenderer()); double* worldPosition = picker->GetPickPosition(); INFO("Cell id is: %d", picker->GetCellId()); if(picker->GetCellId() != -1) { INFO("Pick position is: %f %f %f", worldPosition[0], worldPosition[1], worldPosition[2]); vtkSmartPointer<vtkIdTypeArray> ids = vtkSmartPointer<vtkIdTypeArray>::New(); ids->SetNumberOfValues(1); ids->SetValue(0, picker->GetCellId()); vtkSmartPointer<vtkSelectionNode> selectionNode = vtkSmartPointer<vtkSelectionNode>::New(); selectionNode->SetFieldType(vtkSelectionNode::CELL); selectionNode->SetContentType(vtkSelectionNode::INDICES); selectionNode->SetSelectionList(ids); vtkSmartPointer<vtkSelection> selection = vtkSmartPointer<vtkSelection>::New(); selection->AddNode(selectionNode); vtkSmartPointer<vtkExtractSelection> extractSelection = vtkSmartPointer<vtkExtractSelection>::New(); extractSelection->SetInputData(0, _data); extractSelection->SetInputData(1, selection); extractSelection->Update(); // In selection vtkSmartPointer<vtkUnstructuredGrid> selected = vtkSmartPointer<vtkUnstructuredGrid>::New(); selected->ShallowCopy(extractSelection->GetOutput()); INFO("There are %d points in the selection.", selected->GetNumberOfPoints()); INFO("There are %d cells in the selection.", selected->GetNumberOfCells()); // check if the underlying object is a mesh and if so, send a signal to the element model for display of information about the picked element. vtkAlgorithm* data_set = picker->GetActor()->GetMapper()->GetInputConnection(0, 0)->GetProducer()->GetInputConnection(0,0)->GetProducer(); vtkUnstructuredGridAlgorithm* source = dynamic_cast<vtkUnstructuredGridAlgorithm*>(data_set); if (source) emit elementPicked(source, static_cast<unsigned>(picker->GetCellId())); else emit clearElementView(); _selectedMapper->SetInputData(selected); this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()-> AddActor(_selectedActor); //_highlightActor = true; } else this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()-> RemoveActor(_selectedActor); emit requestViewUpdate(); } else // Forward events vtkInteractorStyleTrackballCamera::OnLeftButtonDown(); }
// From http://www.vtk.org/Wiki/VTK/Examples/Cxx/Picking/CellPicking void VtkCustomInteractorStyle::OnLeftButtonDown() { if (!Data) return vtkInteractorStyleTrackballCamera::OnLeftButtonDown(); if (_alternateMouseActions) { // Get the location of the click (in window coordinates) int* pos = this->GetInteractor()->GetEventPosition(); vtkSmartPointer<vtkCellPicker> picker = vtkSmartPointer<vtkCellPicker>::New(); picker->SetTolerance(0.0005); // Pick from this location. picker->Pick(pos[0], pos[1], 0, this->GetDefaultRenderer()); double* worldPosition = picker->GetPickPosition(); std::cout << "Cell id is: " << picker->GetCellId() << std::endl; if(picker->GetCellId() != -1) { std::cout << "Pick position is: " << worldPosition[0] << " " << worldPosition[1] << " " << worldPosition[2] << endl; vtkSmartPointer<vtkIdTypeArray> ids = vtkSmartPointer<vtkIdTypeArray>::New(); ids->SetNumberOfComponents(1); ids->InsertNextValue(picker->GetCellId()); vtkSmartPointer<vtkSelectionNode> selectionNode = vtkSmartPointer<vtkSelectionNode>::New(); selectionNode->SetFieldType(vtkSelectionNode::CELL); selectionNode->SetContentType(vtkSelectionNode::INDICES); selectionNode->SetSelectionList(ids); vtkSmartPointer<vtkSelection> selection = vtkSmartPointer<vtkSelection>::New(); selection->AddNode(selectionNode); vtkSmartPointer<vtkExtractSelection> extractSelection = vtkSmartPointer<vtkExtractSelection>::New(); extractSelection->SetInput(0, this->Data); extractSelection->SetInput(1, selection); extractSelection->Update(); // In selection vtkSmartPointer<vtkUnstructuredGrid> selected = vtkSmartPointer<vtkUnstructuredGrid>::New(); selected->ShallowCopy(extractSelection->GetOutput()); std::cout << "There are " << selected->GetNumberOfPoints() << " points in the selection." << std::endl; std::cout << "There are " << selected->GetNumberOfCells() << " cells in the selection." << std::endl; // check if the underlying object is a mesh and if so, send a signal to the element model for display of information about the picked element. vtkAlgorithm* data_set = picker->GetActor()->GetMapper()->GetInputConnection(0, 0)->GetProducer() ->GetInputConnection(0,0)->GetProducer(); VtkMeshSource* source = dynamic_cast<VtkMeshSource*>(data_set); if (source) emit elementPicked(source->GetMesh(), picker->GetCellId()); selectedMapper->SetInputConnection(selected->GetProducerPort()); this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()-> AddActor(selectedActor); } else this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()-> RemoveActor(selectedActor); emit requestViewUpdate(); } else // Forward events vtkInteractorStyleTrackballCamera::OnLeftButtonDown(); }