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