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