1 #include <vtkVersion.h> 
    2 #include <vtkRendererCollection.h> 
    3 #include <vtkDataSetMapper.h> 
    4 #include <vtkUnstructuredGrid.h> 
    5 #include <vtkIdTypeArray.h> 
    6 #include <vtkTriangleFilter.h> 
    7 #include <vtkPolyDataMapper.h> 
    9 #include <vtkCommand.h> 
   10 #include <vtkRenderWindow.h> 
   11 #include <vtkRenderer.h> 
   12 #include <vtkRenderWindowInteractor.h> 
   13 #include <vtkPoints.h> 
   14 #include <vtkCellArray.h> 
   15 #include <vtkPlaneSource.h> 
   16 #include <vtkCellPicker.h> 
   17 #include <vtkInteractorStyleTrackballCamera.h> 
   18 #include <vtkProperty.h> 
   19 #include <vtkSelectionNode.h> 
   20 #include <vtkSelection.h> 
   21 #include <vtkExtractSelection.h> 
   22 #include <vtkObjectFactory.h> 
   28 #define VTKISRBP_ORIENT 0 
   29 #define VTKISRBP_SELECT 1 
   31 class axlInteractorCellPickingPrivate {
 
   34     vtkSmartPointer<vtkPolyData> Data;
 
   35     vtkSmartPointer<vtkDataSetMapper> selectedMapper;
 
   36     vtkSmartPointer<vtkActor> selectedActor;
 
   42     d->selectedMapper = vtkSmartPointer<vtkDataSetMapper>::New();
 
   43     d->selectedActor = vtkSmartPointer<vtkActor>::New();
 
   57     int* pos = this->GetInteractor()->GetEventPosition();
 
   59     vtkSmartPointer<vtkCellPicker> picker =
 
   60             vtkSmartPointer<vtkCellPicker>::New();
 
   61     picker->SetTolerance(0.0005);
 
   64     picker->Pick(pos[0], pos[1], 0, this->GetDefaultRenderer());
 
   66     double* worldPosition = picker->GetPickPosition();
 
   67     std::cout << 
"Cell id is: " << picker->GetCellId() << std::endl;
 
   69     if(picker->GetCellId() != -1)
 
   72         std::cout << 
"Pick position is: " << worldPosition[0] << 
" " << worldPosition[1]
 
   73                   << 
" " << worldPosition[2] << endl;
 
   75         vtkSmartPointer<vtkIdTypeArray> ids =
 
   76                 vtkSmartPointer<vtkIdTypeArray>::New();
 
   77         ids->SetNumberOfComponents(1);
 
   78         ids->InsertNextValue(picker->GetCellId());
 
   80         vtkSmartPointer<vtkSelectionNode> selectionNode =
 
   81                 vtkSmartPointer<vtkSelectionNode>::New();
 
   82         selectionNode->SetFieldType(vtkSelectionNode::CELL);
 
   83         selectionNode->SetContentType(vtkSelectionNode::INDICES);
 
   84         selectionNode->SetSelectionList(ids);
 
   86         vtkSmartPointer<vtkSelection> selection =
 
   87                 vtkSmartPointer<vtkSelection>::New();
 
   88         selection->AddNode(selectionNode);
 
   90         vtkSmartPointer<vtkExtractSelection> extractSelection =
 
   91                 vtkSmartPointer<vtkExtractSelection>::New();
 
   92 #if VTK_MAJOR_VERSION <= 5 
   93         extractSelection->SetInput(0, d->Data);
 
   94         extractSelection->SetInput(1, selection);
 
   96         extractSelection->SetInputData(0, d->Data);
 
   97         extractSelection->SetInputData(1, selection);
 
   99         extractSelection->Update();
 
  102         vtkSmartPointer<vtkUnstructuredGrid> selected =
 
  103                 vtkSmartPointer<vtkUnstructuredGrid>::New();
 
  104         selected->ShallowCopy(extractSelection->GetOutput());
 
  106         std::cout << 
"There are " << selected->GetNumberOfPoints()
 
  107                   << 
" points in the selection." << std::endl;
 
  108         std::cout << 
"There are " << selected->GetNumberOfCells()
 
  109                   << 
" cells in the selection." << std::endl;
 
  112 #if VTK_MAJOR_VERSION <= 5 
  113         d->selectedMapper->SetInputConnection(
 
  114                     selected->GetProducerPort());
 
  116         d->selectedMapper->SetInputData(selected);
 
  119         d->selectedActor->SetMapper(d->selectedMapper);
 
  120         d->selectedActor->GetProperty()->EdgeVisibilityOn();
 
  121         d->selectedActor->GetProperty()->SetEdgeColor(1,0,0);
 
  122         d->selectedActor->GetProperty()->SetLineWidth(3);
 
  124         this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(d->selectedActor);
 
  128     vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
 
vtkStandardNewMacro(axlInteractorCellPicking)
~axlInteractorCellPicking(void)
void setData(vtkSmartPointer< vtkPolyData > data)
axlInteractorCellPicking(QObject *parent=0)