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() );
56 boxPointPicker->bbSetOutputCellId( picker->GetCellId() );
57 vtkDataSet *dataset = picker->GetDataSet();
58 if (strcmp(dataset->GetClassName(),"vtkPolyData")==0)
60 vtkPolyData *polydata = (vtkPolyData*)dataset;
61 vtkPointData *pointdata = polydata->GetPointData();
62 vtkDataArray *dataarray;
65 int i,size=pointdata->GetNumberOfArrays();
68 dataarray=pointdata->GetArray(i);
69 printf("EED creaVtkCallbackPointPicker::Execute dataarray=%s n=%ld p=%ld\n", dataarray->GetName(),dataarray->GetNumberOfValues() ,polydata->GetNumberOfPoints() );
72 dataarray = pointdata->GetNormals();
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 );
83 boxPointPicker->bbSignalOutputModification();
86 } // if boxPoiintPicker
91 std::cout << "Pick position is: " << worldPosition[0] << " " << worldPosition[1]
92 << " " << worldPosition[2] << endl;
94 vtkSmartPointer<vtkIdTypeArray> ids =
95 vtkSmartPointer<vtkIdTypeArray>::New()creaVtkCallbackPointPicker *callPicker;
96 ids->SetNumberOfComponents(1);
97 ids->InsertNextValue(picker->GetCellId());
99 vtkSmartPointer<vtkSelectionNode> selectionNode =
100 vtkSmartPointer<vtkSelectionNode>::New();
101 selectionNode->SetFieldType(vtkSelectionNode::CELL);
102 selectionNode->SetContentType(vtkSelectionNode::INDICES);
103 selectionNode->SetSelectionList(ids);
105 vtkSmartPointer<vtkSelection> selection =
106 vtkSmartPointer<vtkSelection>::New();
107 selection->AddNode(selectionNode);
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);
115 extractSelection->SetInputData(0, this->Data);
116 extractSelection->SetInputData(1, selection);
118 extractSelection->Update();
121 vtkSmartPointer<vtkUnstructuredGrid> selected =
122 vtkSmartPointer<vtkUnstructuredGrid>::New();
123 selected->ShallowCopy(extractSelection->GetOutput());
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;
131 #if VTK_MAJOR_VERSION <= 5
132 selectedMapper->SetInputConnection(
133 selected->GetProducerPort());
135 selectedMapper->SetInputData(selected);
138 selectedActor->SetMapper(selectedMapper);
139 selectedActor->GetProperty()->EdgeVisibilityOn();
140 selectedActor->GetProperty()->SetEdgeColor(1,0,0);
141 selectedActor->GetProperty()->SetLineWidth(3);
143 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()
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
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')
178 // bbSetOutputOut( bbGetInputIn() );
179 // std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
184 // vtkCellPicker *picker = vtkCellPicker::New();
185 // vtkPropPicker *picker = vtkCellPicker::New();
186 // picker->SetTolerance(0.005);
188 // picker->AddObserver("EndPickEvent", annotatePick)
189 // iren = vtk.vtkRenderWindowInteractor()
190 // iren.SetPicker(picker)
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);
200 callPicker = creaVtkCallbackPointPicker::New();
201 callPicker->setBox( this );
206 if (bbGetInputRenderer()!=NULL)
208 vtkRenderWindowInteractor *vrwi = bbGetInputRenderer()->GetRenderWindow()->GetInteractor();
209 vrwi->RemoveObserver( callPicker );
211 if (bbGetInputTypeEvent()==0)
214 } else if (bbGetInputTypeEvent()==1)
216 vrwi->AddObserver( vtkCommand::MouseMoveEvent , callPicker );
217 } else if (bbGetInputTypeEvent()==2)
219 vrwi->AddObserver( vtkCommand::LeftButtonPressEvent , callPicker );
220 } else if (bbGetInputTypeEvent()==3)
222 #if VTK_MAJOR_VERSION >= 9
223 vrwi->AddObserver( vtkCommand::LeftButtonDoubleClickEvent , callPicker );
225 printf("EED Warnning! PointPicker LeftButtonPressEvent since vtk 9 \n");
228 // ....See vtkCommand EventIds
229 } // if bbGetInputRenderer()
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)
238 void PointPicker::bbUserSetDefaultValues()
240 // SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
241 // Here we initialize the input 'In' to 0
243 bbSetInputActive(true);
244 bbSetInputTypeEvent(0);
245 bbSetOutputMesh(NULL);
246 bbSetOutputPointId(-1);
247 bbSetOutputCellId(-1);
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 );
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)
260 void PointPicker::bbUserInitializeProcessing()
263 // THE INITIALIZATION METHOD BODY :
265 // but this is where you should allocate the internal/output pointers
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)
273 void PointPicker::bbUserFinalizeProcessing()
276 // THE FINALIZATION METHOD BODY :
278 // but this is where you should desallocate the internal/output pointers
283 // EO namespace bbcreaVtk