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