]> Creatis software - creaVtk.git/blob - bbtk_creaVtk_PKG/src/bbcreaVtkPointPicker.cxx
69c4c02eb71c54d4044dbec818573c41f26b4271
[creaVtk.git] / bbtk_creaVtk_PKG / src / bbcreaVtkPointPicker.cxx
1 //===== 
2 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
3 //===== 
4 #include "bbcreaVtkPointPicker.h"
5 #include "bbcreaVtkPackage.h"
6
7 #include <vtkPropPicker.h>
8 #include <vtkCellPicker.h>
9 #include <vtkRenderWindow.h>
10 #include <vtkRendererCollection.h>
11
12 namespace bbcreaVtk
13 {
14
15 creaVtkCallbackPointPicker::creaVtkCallbackPointPicker(void)
16 {
17         boxPointPicker = NULL;
18 }
19
20 void creaVtkCallbackPointPicker::setBox(PointPicker *box)
21 {
22         boxPointPicker = box;
23 }
24
25 void creaVtkCallbackPointPicker::Execute(vtkObject *caller, unsigned long, void*)
26 {
27         if (boxPointPicker!=NULL)
28         {
29                 if (boxPointPicker->bbGetInputActive()==true)
30                 {
31 //                      printf("creaVtkCallbackPointPicker EED Picker\n");
32                         vtkRenderWindowInteractor *iren = static_cast<vtkRenderWindowInteractor*>(caller);
33                         int* pos = iren->GetEventPosition();
34                     vtkCellPicker *picker = vtkCellPicker::New();
35                     picker->SetTolerance(0.0005);
36                     
37                     if (boxPointPicker->bbGetInputProp3D()!=NULL ) 
38                     {
39                         picker->AddPickList( boxPointPicker->bbGetInputProp3D() );
40                         picker->PickFromListOn();
41                         }                   
42                     
43                     picker->Pick(pos[0], pos[1], 0, iren->GetRenderWindow()->GetRenderers()->GetFirstRenderer() );
44                         double point[3];
45
46                         if(picker->GetCellId() != -1)
47                     {
48 //                              printf("creaVtkCallbackPointPicker EED picker OK\n");
49                                 picker->GetPickPosition(point);
50                                 std::vector<double> vecPoint;
51                                 vecPoint.push_back( point[0] );
52                                 vecPoint.push_back( point[1] );
53                                 vecPoint.push_back( point[2] );
54                                         boxPointPicker->bbSetOutputPoint( vecPoint );
55                                         boxPointPicker->bbSetOutputMesh( picker->GetProp3D() );
56                                         boxPointPicker->bbSetOutputPointId( picker->GetPointId() );
57                                         boxPointPicker->bbSetOutputCellId( picker->GetCellId() );
58                                         boxPointPicker->bbSignalOutputModification();
59                         } else {
60 //                              printf("creaVtkCallbackPointPicker EED picker --\n");
61                         } // if
62                 }
63         } // if boxPoiintPicker
64
65 /*
66 {
67  
68         std::cout << "Pick position is: " << worldPosition[0] << " " << worldPosition[1]
69                   << " " << worldPosition[2] << endl;
70  
71         vtkSmartPointer<vtkIdTypeArray> ids =
72           vtkSmartPointer<vtkIdTypeArray>::New()creaVtkCallbackPointPicker *callPicker;
73         ids->SetNumberOfComponents(1);
74         ids->InsertNextValue(picker->GetCellId());
75  
76         vtkSmartPointer<vtkSelectionNode> selectionNode =
77           vtkSmartPointer<vtkSelectionNode>::New();
78         selectionNode->SetFieldType(vtkSelectionNode::CELL);
79         selectionNode->SetContentType(vtkSelectionNode::INDICES);
80         selectionNode->SetSelectionList(ids);
81  
82         vtkSmartPointer<vtkSelection> selection =
83           vtkSmartPointer<vtkSelection>::New();
84         selection->AddNode(selectionNode);
85  
86         vtkSmartPointer<vtkExtractSelection> extractSelection =
87           vtkSmartPointer<vtkExtractSelection>::New();
88 #if VTK_MAJOR_VERSION <= 5
89         extractSelection->SetInput(0, this->Data);
90         extractSelection->SetInput(1, selection);
91 #else
92         extractSelection->SetInputData(0, this->Data);
93         extractSelection->SetInputData(1, selection);
94 #endif
95         extractSelection->Update();
96  
97         // In selection
98         vtkSmartPointer<vtkUnstructuredGrid> selected =
99           vtkSmartPointer<vtkUnstructuredGrid>::New();
100         selected->ShallowCopy(extractSelection->GetOutput());
101  
102         std::cout << "There are " << selected->GetNumberOfPoints()
103                   << " points in the selection." << std::endl;
104         std::cout << "There are " << selected->GetNumberOfCells()
105                   << " cells in the selection." << std::endl;
106  
107  
108 #if VTK_MAJOR_VERSION <= 5
109         selectedMapper->SetInputConnection(
110           selected->GetProducerPort());
111 #else
112         selectedMapper->SetInputData(selected);
113 #endif
114  
115         selectedActor->SetMapper(selectedMapper);
116         selectedActor->GetProperty()->EdgeVisibilityOn();
117         selectedActor->GetProperty()->SetEdgeColor(1,0,0);
118         selectedActor->GetProperty()->SetLineWidth(3);
119  
120         this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(selectedActor);
121  
122         }
123 */
124
125
126
127 }
128
129
130 //--------------------------------------------------------
131 //--------------------------------------------------------
132 //--------------------------------------------------------
133
134
135 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,PointPicker)
136 BBTK_BLACK_BOX_IMPLEMENTATION(PointPicker,bbtk::AtomicBlackBox);
137 //===== 
138 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
139 //===== 
140 void PointPicker::Process()
141 {
142
143 // THE MAIN PROCESSING METHOD BODY
144 //   Here we simply set the input 'In' value to the output 'Out'
145 //   And print out the output value
146 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
147 //    void bbSet{Input|Output}NAME(const TYPE&)
148 //    const TYPE& bbGet{Input|Output}NAME() const 
149 //    Where :
150 //    * NAME is the name of the input/output
151 //      (the one provided in the attribute 'name' of the tag 'input')
152 //    * TYPE is the C++ type of the input/output
153 //      (the one provided in the attribute 'type' of the tag 'input')
154
155 //    bbSetOutputOut( bbGetInputIn() );
156 //    std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
157   
158         if (firsttime==true)
159         {
160                 firsttime = false;
161 //              vtkCellPicker *picker = vtkCellPicker::New();
162 //              vtkPropPicker *picker = vtkCellPicker::New();
163 //              picker->SetTolerance(0.005);
164
165 //              picker->AddObserver("EndPickEvent", annotatePick)
166 //              iren = vtk.vtkRenderWindowInteractor()
167 //              iren.SetPicker(picker)
168
169 /*
170                 creaVtkCallbackPointPicker *callPicker = creaVtkCallbackPointPicker::New();
171                 picker->AddObserver( vtkCommand::EndPickEvent , callPicker );
172                 picker->AddObserver( vtkCommand::StartPickEvent , callPicker );
173                 picker->AddObserver( vtkCommand::MouseMoveEvent , callPicker );
174                 bbGetInputRenderer()->GetRenderWindow()->GetInteractor()->SetPicker(picker);
175 */
176
177                 callPicker = creaVtkCallbackPointPicker::New();
178                 callPicker->setBox( this );
179
180         } // if firsttime
181
182
183         if (bbGetInputRenderer()!=NULL)
184         {
185                 vtkRenderWindowInteractor *vrwi = bbGetInputRenderer()->GetRenderWindow()->GetInteractor();
186                 vrwi->RemoveObserver( callPicker );
187
188                 if (bbGetInputTypeEvent()==0)
189                 {
190                         // Nothing
191                 } else if (bbGetInputTypeEvent()==1)
192                 {
193                         vrwi->AddObserver( vtkCommand::MouseMoveEvent , callPicker );
194                 } else if (bbGetInputTypeEvent()==2)
195                 {
196                         vrwi->AddObserver( vtkCommand::LeftButtonPressEvent , callPicker );
197                 } else if (bbGetInputTypeEvent()==3)
198                 {
199 #if VTK_MAJOR_VERSION >= 9
200                 vrwi->AddObserver( vtkCommand::LeftButtonDoubleClickEvent , callPicker );
201 #else
202                 printf("EED Warnning!  PointPicker LeftButtonPressEvent since vtk 9 \n");
203 #endif
204                 } // if TypeEvent       
205                 // ....See vtkCommand EventIds
206         } // if bbGetInputRenderer()
207
208                 
209
210
211 }
212 //===== 
213 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
214 //===== 
215 void PointPicker::bbUserSetDefaultValues()
216 {
217 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
218 //    Here we initialize the input 'In' to 0
219 //   bbSetInputIn(0);
220         bbSetInputActive(true);
221         bbSetInputTypeEvent(0);
222         bbSetOutputMesh(NULL);
223         bbSetOutputPointId(-1);
224         bbSetOutputCellId(-1);
225         firsttime=true;
226         callPicker=NULL;
227         bbSetInputProp3D(NULL);
228 }
229 //===== 
230 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
231 //===== 
232 void PointPicker::bbUserInitializeProcessing()
233 {
234
235 //  THE INITIALIZATION METHOD BODY :
236 //    Here does nothing 
237 //    but this is where you should allocate the internal/output pointers 
238 //    if any 
239
240   
241 }
242 //===== 
243 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
244 //===== 
245 void PointPicker::bbUserFinalizeProcessing()
246 {
247
248 //  THE FINALIZATION METHOD BODY :
249 //    Here does nothing 
250 //    but this is where you should desallocate the internal/output pointers 
251 //    if any
252   
253 }
254 }
255 // EO namespace bbcreaVtk
256
257