]> Creatis software - creaVtk.git/blob - bbtk_creaVtk_PKG/src/bbcreaVtkPointPicker.cxx
7bcdd934d91304ab5a235c6f1e0c61638d2a9713
[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     printf("EED PointPicker::Process  %s\n", bbGetFullName().c_str()  );
181     
182         if (firsttime==true)
183         {
184                 firsttime = false;
185 //              vtkCellPicker *picker = vtkCellPicker::New();
186 //              vtkPropPicker *picker = vtkCellPicker::New();
187 //              picker->SetTolerance(0.005);
188
189 //              picker->AddObserver("EndPickEvent", annotatePick)
190 //              iren = vtk.vtkRenderWindowInteractor()
191 //              iren.SetPicker(picker)
192
193 /*
194                 creaVtkCallbackPointPicker *callPicker = creaVtkCallbackPointPicker::New();
195                 picker->AddObserver( vtkCommand::EndPickEvent , callPicker );
196                 picker->AddObserver( vtkCommand::StartPickEvent , callPicker );
197                 picker->AddObserver( vtkCommand::MouseMoveEvent , callPicker );
198                 bbGetInputRenderer()->GetRenderWindow()->GetInteractor()->SetPicker(picker);
199 */
200                 callPicker = creaVtkCallbackPointPicker::New();
201                 callPicker->setBox( this );
202         } // if firsttime
203
204
205         if (bbGetInputRenderer()!=NULL)
206         {
207                 vtkRenderWindowInteractor *vrwi = bbGetInputRenderer()->GetRenderWindow()->GetInteractor();
208                 vrwi->RemoveObserver( callPicker );
209
210                 if (bbGetInputTypeEvent()==0)
211                 {
212                         // Nothing
213                 } else if (bbGetInputTypeEvent()==1)
214                 {
215                         vrwi->AddObserver( vtkCommand::MouseMoveEvent , callPicker );
216                 } else if (bbGetInputTypeEvent()==2)
217                 {
218                         vrwi->AddObserver( vtkCommand::LeftButtonPressEvent , callPicker );
219                 } else if (bbGetInputTypeEvent()==3)
220                 {
221 #if VTK_MAJOR_VERSION >= 9
222                 vrwi->AddObserver( vtkCommand::LeftButtonDoubleClickEvent , callPicker );
223 #else
224                 printf("EED Warnning!  PointPicker LeftButtonPressEvent since vtk 9 \n");
225 #endif
226                 } // if TypeEvent       
227                 // ....See vtkCommand EventIds
228         } // if bbGetInputRenderer()
229
230                 
231 //  std::vector<double> lstPoint;
232 //  lstPoint.push_back(0);
233 //  lstPoint.push_back(0);
234 //  lstPoint.push_back(1);
235 //  bbSetOutputPoint( lstPoint );
236     
237 // BORRAME    bbSetOutputPoint( bbGetInputDefaultPoint() );
238     
239 //  bbSetOutputMesh( NULL );
240 // Borrame    bbSetOutputPointId( bbGetInputDefaultPointId() );
241 //  bbSetOutputCellId( -1 );
242 //  bbSetOutputNormal( lstPoint );
243     
244 }
245
246 //===== 
247 // 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)
248 //===== 
249 void PointPicker::bbUserSetDefaultValues()
250 {
251 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
252 //    Here we initialize the input 'In' to 0
253 //   bbSetInputIn(0);
254         bbSetInputActive(true);
255         bbSetInputTypeEvent(0);
256     bbSetInputDefaultPointId( -1 );
257         bbSetOutputMesh(NULL);
258         bbSetOutputPointId(-1);
259         bbSetOutputCellId(-1);
260         firsttime=true;
261         callPicker=NULL;
262         bbSetInputProp3D(NULL);
263     std::vector<double> lstNormal;
264     lstNormal.push_back(0);
265     lstNormal.push_back(0);
266     lstNormal.push_back(1);
267     bbSetOutputNormal( lstNormal );
268     
269     std::vector<double> point;
270     point.push_back(20);
271     point.push_back(20);
272     point.push_back(20);
273     bbSetOutputPoint( point );
274 }
275
276 //===== 
277 // 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)
278 //===== 
279 void PointPicker::bbUserInitializeProcessing()
280 {
281 //  THE INITIALIZATION METHOD BODY :
282 //    Here does nothing 
283 //    but this is where you should allocate the internal/output pointers 
284 //    if any
285 }
286
287 //===== 
288 // 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)
289 //===== 
290 void PointPicker::bbUserFinalizeProcessing()
291 {
292 //  THE FINALIZATION METHOD BODY :
293 //    Here does nothing 
294 //    but this is where you should desallocate the internal/output pointers 
295 //    if any
296 }
297
298 }// EO namespace bbcreaVtk
299
300