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 printf("EED creaVtkCallbackPointPicker::Execute Start\n");
32 if (boxPointPicker!=NULL)
34 if (boxPointPicker->bbGetInputActive()==true)
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 )
42 picker->AddPickList( boxPointPicker->bbGetInputProp3D() );
43 picker->PickFromListOn();
45 picker->Pick(pos[0], pos[1], 0, iren->GetRenderWindow()->GetRenderers()->GetFirstRenderer() );
47 if(picker->GetCellId() != -1)
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() );
59 boxPointPicker->bbSetOutputCellId( picker->GetCellId() );
60 vtkDataSet *dataset = picker->GetDataSet();
61 if (strcmp(dataset->GetClassName(),"vtkPolyData")==0)
63 vtkPolyData *polydata = (vtkPolyData*)dataset;
64 vtkPointData *pointdata = polydata->GetPointData();
65 vtkDataArray *dataarray;
68 int i,size=pointdata->GetNumberOfArrays();
71 dataarray=pointdata->GetArray(i);
72 printf("EED creaVtkCallbackPointPicker::Execute dataarray=%s n=%ld p=%ld\n", dataarray->GetName(),dataarray->GetNumberOfValues() ,polydata->GetNumberOfPoints() );
75 dataarray = pointdata->GetNormals();
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 );
86 boxPointPicker->bbSignalOutputModification();
89 } // if boxPoiintPicker
91 printf("EED creaVtkCallbackPointPicker::Execute End\n");
97 std::cout << "Pick position is: " << worldPosition[0] << " " << worldPosition[1]
98 << " " << worldPosition[2] << endl;
100 vtkSmartPointer<vtkIdTypeArray> ids =
101 vtkSmartPointer<vtkIdTypeArray>::New()creaVtkCallbackPointPicker *callPicker;
102 ids->SetNumberOfComponents(1);
103 ids->InsertNextValue(picker->GetCellId());
105 vtkSmartPointer<vtkSelectionNode> selectionNode =
106 vtkSmartPointer<vtkSelectionNode>::New();
107 selectionNode->SetFieldType(vtkSelectionNode::CELL);
108 selectionNode->SetContentType(vtkSelectionNode::INDICES);
109 selectionNode->SetSelectionList(ids);
111 vtkSmartPointer<vtkSelection> selection =
112 vtkSmartPointer<vtkSelection>::New();
113 selection->AddNode(selectionNode);
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);
121 extractSelection->SetInputData(0, this->Data);
122 extractSelection->SetInputData(1, selection);
124 extractSelection->Update();
127 vtkSmartPointer<vtkUnstructuredGrid> selected =
128 vtkSmartPointer<vtkUnstructuredGrid>::New();
129 selected->ShallowCopy(extractSelection->GetOutput());
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;
137 #if VTK_MAJOR_VERSION <= 5
138 selectedMapper->SetInputConnection(
139 selected->GetProducerPort());
141 selectedMapper->SetInputData(selected);
144 selectedActor->SetMapper(selectedMapper);
145 selectedActor->GetProperty()->EdgeVisibilityOn();
146 selectedActor->GetProperty()->SetEdgeColor(1,0,0);
147 selectedActor->GetProperty()->SetLineWidth(3);
149 this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(selectedActor);
157 //--------------------------------------------------------
158 //--------------------------------------------------------
159 //--------------------------------------------------------
162 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,PointPicker)
163 BBTK_BLACK_BOX_IMPLEMENTATION(PointPicker,bbtk::AtomicBlackBox);
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)
167 void PointPicker::Process()
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
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')
182 // bbSetOutputOut( bbGetInputIn() );
183 // std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
188 // vtkCellPicker *picker = vtkCellPicker::New();
189 // vtkPropPicker *picker = vtkCellPicker::New();
190 // picker->SetTolerance(0.005);
192 // picker->AddObserver("EndPickEvent", annotatePick)
193 // iren = vtk.vtkRenderWindowInteractor()
194 // iren.SetPicker(picker)
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);
203 callPicker = creaVtkCallbackPointPicker::New();
204 callPicker->setBox( this );
208 if (bbGetInputRenderer()!=NULL)
210 vtkRenderWindowInteractor *vrwi = bbGetInputRenderer()->GetRenderWindow()->GetInteractor();
211 vrwi->RemoveObserver( callPicker );
213 if (bbGetInputTypeEvent()==0)
216 } else if (bbGetInputTypeEvent()==1)
218 vrwi->AddObserver( vtkCommand::MouseMoveEvent , callPicker );
219 } else if (bbGetInputTypeEvent()==2)
221 vrwi->AddObserver( vtkCommand::LeftButtonPressEvent , callPicker );
222 } else if (bbGetInputTypeEvent()==3)
224 #if VTK_MAJOR_VERSION >= 9
225 vrwi->AddObserver( vtkCommand::LeftButtonDoubleClickEvent , callPicker );
227 printf("EED Warnning! PointPicker LeftButtonPressEvent since vtk 9 \n");
230 // ....See vtkCommand EventIds
231 } // if bbGetInputRenderer()
234 // std::vector<double> lstPoint;
235 // lstPoint.push_back(0);
236 // lstPoint.push_back(0);
237 // lstPoint.push_back(1);
238 // bbSetOutputPoint( lstPoint );
240 // BORRAME bbSetOutputPoint( bbGetInputDefaultPoint() );
242 // bbSetOutputMesh( NULL );
243 // Borrame bbSetOutputPointId( bbGetInputDefaultPointId() );
244 // bbSetOutputCellId( -1 );
245 // bbSetOutputNormal( lstPoint );
247 printf("EED PointPicker::Process End \n");
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)
254 void PointPicker::bbUserSetDefaultValues()
256 // SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
257 // Here we initialize the input 'In' to 0
259 bbSetInputActive(true);
260 bbSetInputTypeEvent(0);
261 bbSetInputDefaultPointId( -1 );
262 bbSetOutputMesh(NULL);
263 bbSetOutputPointId(-1);
264 bbSetOutputCellId(-1);
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 );
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)
278 void PointPicker::bbUserInitializeProcessing()
280 // THE INITIALIZATION METHOD BODY :
282 // but this is where you should allocate the internal/output pointers
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)
289 void PointPicker::bbUserFinalizeProcessing()
291 // THE FINALIZATION METHOD BODY :
293 // but this is where you should desallocate the internal/output pointers
297 }// EO namespace bbcreaVtk