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