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)
4 #include "bbcreaVtkPointPicker.h"
5 #include "bbcreaVtkPackage.h"
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>
19 creaVtkCallbackPointPicker::creaVtkCallbackPointPicker(void)
21 boxPointPicker = NULL;
24 void creaVtkCallbackPointPicker::setBox(PointPicker *box)
29 void creaVtkCallbackPointPicker::Execute(vtkObject *caller, unsigned long, void*)
31 if (boxPointPicker!=NULL)
33 if (boxPointPicker->bbGetInputActive()==true)
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 )
41 picker->AddPickList( boxPointPicker->bbGetInputProp3D() );
42 picker->PickFromListOn();
44 picker->Pick(pos[0], pos[1], 0, iren->GetRenderWindow()->GetRenderers()->GetFirstRenderer() );
46 if(picker->GetCellId() != -1)
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() );
57 boxPointPicker->bbSetOutputCellId( picker->GetCellId() );
58 vtkDataSet *dataset = picker->GetDataSet();
59 if (strcmp(dataset->GetClassName(),"vtkPolyData")==0)
61 vtkPolyData *polydata = (vtkPolyData*)dataset;
62 vtkPointData *pointdata = polydata->GetPointData();
63 vtkDataArray *dataarray;
66 int i,size=pointdata->GetNumberOfArrays();
69 dataarray=pointdata->GetArray(i);
70 printf("EED creaVtkCallbackPointPicker::Execute dataarray=%s n=%ld p=%ld\n", dataarray->GetName(),dataarray->GetNumberOfValues() ,polydata->GetNumberOfPoints() );
73 dataarray = pointdata->GetNormals();
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 );
84 boxPointPicker->bbSignalOutputModification();
87 } // if boxPoiintPicker
93 std::cout << "Pick position is: " << worldPosition[0] << " " << worldPosition[1]
94 << " " << worldPosition[2] << endl;
96 vtkSmartPointer<vtkIdTypeArray> ids =
97 vtkSmartPointer<vtkIdTypeArray>::New()creaVtkCallbackPointPicker *callPicker;
98 ids->SetNumberOfComponents(1);
99 ids->InsertNextValue(picker->GetCellId());
101 vtkSmartPointer<vtkSelectionNode> selectionNode =
102 vtkSmartPointer<vtkSelectionNode>::New();
103 selectionNode->SetFieldType(vtkSelectionNode::CELL);
104 selectionNode->SetContentType(vtkSelectionNode::INDICES);
105 selectionNode->SetSelectionList(ids);
107 vtkSmartPointer<vtkSelection> selection =
108 vtkSmartPointer<vtkSelection>::New();
109 selection->AddNode(selectionNode);
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);
117 extractSelection->SetInputData(0, this->Data);
118 extractSelection->SetInputData(1, selection);
120 extractSelection->Update();
123 vtkSmartPointer<vtkUnstructuredGrid> selected =
124 vtkSmartPointer<vtkUnstructuredGrid>::New();
125 selected->ShallowCopy(extractSelection->GetOutput());
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;
133 #if VTK_MAJOR_VERSION <= 5
134 selectedMapper->SetInputConnection(
135 selected->GetProducerPort());
137 selectedMapper->SetInputData(selected);
140 selectedActor->SetMapper(selectedMapper);
141 selectedActor->GetProperty()->EdgeVisibilityOn();
142 selectedActor->GetProperty()->SetEdgeColor(1,0,0);
143 selectedActor->GetProperty()->SetLineWidth(3);
145 this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(selectedActor);
153 //--------------------------------------------------------
154 //--------------------------------------------------------
155 //--------------------------------------------------------
158 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,PointPicker)
159 BBTK_BLACK_BOX_IMPLEMENTATION(PointPicker,bbtk::AtomicBlackBox);
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)
163 void PointPicker::Process()
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
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')
177 // bbSetOutputOut( bbGetInputIn() );
178 // std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
183 // vtkCellPicker *picker = vtkCellPicker::New();
184 // vtkPropPicker *picker = vtkCellPicker::New();
185 // picker->SetTolerance(0.005);
187 // picker->AddObserver("EndPickEvent", annotatePick)
188 // iren = vtk.vtkRenderWindowInteractor()
189 // iren.SetPicker(picker)
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);
198 callPicker = creaVtkCallbackPointPicker::New();
199 callPicker->setBox( this );
203 if (bbGetInputRenderer()!=NULL)
205 vtkRenderWindowInteractor *vrwi = bbGetInputRenderer()->GetRenderWindow()->GetInteractor();
206 vrwi->RemoveObserver( callPicker );
208 if (bbGetInputTypeEvent()==0)
211 } else if (bbGetInputTypeEvent()==1)
213 vrwi->AddObserver( vtkCommand::MouseMoveEvent , callPicker );
214 } else if (bbGetInputTypeEvent()==2)
216 vrwi->AddObserver( vtkCommand::LeftButtonPressEvent , callPicker );
217 } else if (bbGetInputTypeEvent()==3)
219 #if VTK_MAJOR_VERSION >= 9
220 vrwi->AddObserver( vtkCommand::LeftButtonDoubleClickEvent , callPicker );
222 printf("EED Warnning! PointPicker LeftButtonPressEvent since vtk 9 \n");
225 // ....See vtkCommand EventIds
226 } // if bbGetInputRenderer()
229 // std::vector<double> lstPoint;
230 // lstPoint.push_back(0);
231 // lstPoint.push_back(0);
232 // lstPoint.push_back(1);
233 // bbSetOutputPoint( lstPoint );
235 // BORRAME bbSetOutputPoint( bbGetInputDefaultPoint() );
237 // bbSetOutputMesh( NULL );
238 // Borrame bbSetOutputPointId( bbGetInputDefaultPointId() );
239 // bbSetOutputCellId( -1 );
240 // bbSetOutputNormal( lstPoint );
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)
247 void PointPicker::bbUserSetDefaultValues()
249 // SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
250 // Here we initialize the input 'In' to 0
252 bbSetInputActive(true);
253 bbSetInputTypeEvent(0);
254 bbSetInputDefaultPointId( -1 );
255 bbSetOutputMesh(NULL);
256 bbSetOutputPointId(-1);
257 bbSetOutputCellId(-1);
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 );
267 std::vector<double> point;
271 bbSetOutputPoint( point );
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)
277 void PointPicker::bbUserInitializeProcessing()
279 // THE INITIALIZATION METHOD BODY :
281 // but this is where you should allocate the internal/output pointers
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)
288 void PointPicker::bbUserFinalizeProcessing()
290 // THE FINALIZATION METHOD BODY :
292 // but this is where you should desallocate the internal/output pointers
296 }// EO namespace bbcreaVtk