Developer documentation | Axl-2.5.1

axlInteractorCellPicking.cpp
Go to the documentation of this file.
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>
8 #include <vtkActor.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>
23 
25 
27 
28 #define VTKISRBP_ORIENT 0
29 #define VTKISRBP_SELECT 1
30 
31 class axlInteractorCellPickingPrivate {
32 public :
33 
34  vtkSmartPointer<vtkPolyData> Data;
35  vtkSmartPointer<vtkDataSetMapper> selectedMapper;
36  vtkSmartPointer<vtkActor> selectedActor;
37 
38 };
39 
40 
41 axlInteractorCellPicking::axlInteractorCellPicking(QObject *parent) : QObject(parent),d(new axlInteractorCellPickingPrivate){
42  d->selectedMapper = vtkSmartPointer<vtkDataSetMapper>::New();
43  d->selectedActor = vtkSmartPointer<vtkActor>::New();
44 }
45 
47  delete d;
48  d = NULL;
49 }
50 
51 void axlInteractorCellPicking::setData(vtkSmartPointer<vtkPolyData> data){
52  d->Data = data;
53 }
54 
56  // Get the location of the click (in window coordinates)
57  int* pos = this->GetInteractor()->GetEventPosition();
58 
59  vtkSmartPointer<vtkCellPicker> picker =
60  vtkSmartPointer<vtkCellPicker>::New();
61  picker->SetTolerance(0.0005);
62 
63  // Pick from this location.
64  picker->Pick(pos[0], pos[1], 0, this->GetDefaultRenderer());
65 
66  double* worldPosition = picker->GetPickPosition();
67  std::cout << "Cell id is: " << picker->GetCellId() << std::endl;
68 
69  if(picker->GetCellId() != -1)
70  {
71 
72  std::cout << "Pick position is: " << worldPosition[0] << " " << worldPosition[1]
73  << " " << worldPosition[2] << endl;
74 
75  vtkSmartPointer<vtkIdTypeArray> ids =
76  vtkSmartPointer<vtkIdTypeArray>::New();
77  ids->SetNumberOfComponents(1);
78  ids->InsertNextValue(picker->GetCellId());
79 
80  vtkSmartPointer<vtkSelectionNode> selectionNode =
81  vtkSmartPointer<vtkSelectionNode>::New();
82  selectionNode->SetFieldType(vtkSelectionNode::CELL);
83  selectionNode->SetContentType(vtkSelectionNode::INDICES);
84  selectionNode->SetSelectionList(ids);
85 
86  vtkSmartPointer<vtkSelection> selection =
87  vtkSmartPointer<vtkSelection>::New();
88  selection->AddNode(selectionNode);
89 
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);
95 #else
96  extractSelection->SetInputData(0, d->Data);
97  extractSelection->SetInputData(1, selection);
98 #endif
99  extractSelection->Update();
100 
101  // In selection
102  vtkSmartPointer<vtkUnstructuredGrid> selected =
103  vtkSmartPointer<vtkUnstructuredGrid>::New();
104  selected->ShallowCopy(extractSelection->GetOutput());
105 
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;
110 
111 
112 #if VTK_MAJOR_VERSION <= 5
113  d->selectedMapper->SetInputConnection(
114  selected->GetProducerPort());
115 #else
116  d->selectedMapper->SetInputData(selected);
117 #endif
118 
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);
123 
124  this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(d->selectedActor);
125 
126  }
127  // Forward events
128  vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
129 }
vtkStandardNewMacro(axlInteractorCellPicking)
void setData(vtkSmartPointer< vtkPolyData > data)
axlInteractorCellPicking(QObject *parent=0)