]> Creatis software - creaVtk.git/blob - bbtk_creaVtk_PKG/src/bbcreaVtkPointPicker.cxx
3511 bbcreaVtkPointPicker bug init point
[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     std::vector<double> point;
229     bbSetOutputPoint( point );
230
231                 
232 //  std::vector<double> lstPoint;
233 //  lstPoint.push_back(0);
234 //  lstPoint.push_back(0);
235 //  lstPoint.push_back(1);
236 //  bbSetOutputPoint( lstPoint );
237     
238 // BORRAME    bbSetOutputPoint( bbGetInputDefaultPoint() );
239     
240 //  bbSetOutputMesh( NULL );
241 // Borrame    bbSetOutputPointId( bbGetInputDefaultPointId() );
242 //  bbSetOutputCellId( -1 );
243 //  bbSetOutputNormal( lstPoint );
244     
245 }
246
247 //===== 
248 // 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)
249 //===== 
250 void PointPicker::bbUserSetDefaultValues()
251 {
252 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
253 //    Here we initialize the input 'In' to 0
254 //   bbSetInputIn(0);
255         bbSetInputActive(true);
256         bbSetInputTypeEvent(0);
257     bbSetInputDefaultPointId( -1 );
258         bbSetOutputMesh(NULL);
259         bbSetOutputPointId(-1);
260         bbSetOutputCellId(-1);
261         firsttime=true;
262         callPicker=NULL;
263         bbSetInputProp3D(NULL);
264     std::vector<double> lstNormal;
265     lstNormal.push_back(0);
266     lstNormal.push_back(0);
267     lstNormal.push_back(1);
268     bbSetOutputNormal( lstNormal );
269     
270 //    std::vector<double> point;
271 //    point.push_back(-1);
272 //    point.push_back(-1);
273 //    point.push_back(-1);
274 //    bbSetOutputPoint( point );
275 }
276
277 //===== 
278 // 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)
279 //===== 
280 void PointPicker::bbUserInitializeProcessing()
281 {
282 //  THE INITIALIZATION METHOD BODY :
283 //    Here does nothing 
284 //    but this is where you should allocate the internal/output pointers 
285 //    if any
286 }
287
288 //===== 
289 // 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)
290 //===== 
291 void PointPicker::bbUserFinalizeProcessing()
292 {
293 //  THE FINALIZATION METHOD BODY :
294 //    Here does nothing 
295 //    but this is where you should desallocate the internal/output pointers 
296 //    if any
297 }
298
299 }// EO namespace bbcreaVtk
300
301