1 #include <VTKViewer_CellRectPicker.h>
3 #include "vtkGenericCell.h"
4 #include "vtkAssemblyNode.h"
5 #include "vtkAssemblyPath.h"
6 #include "vtkObjectFactory.h"
7 #include "vtkImageData.h"
9 #include "vtkVolumeMapper.h"
13 #include "vtkPoints.h"
17 vtkStandardNewMacro(VTKViewer_CellRectPicker);
20 * \li create instance of generic cell
21 * \li create two instances of Quad.
23 VTKViewer_CellRectPicker::VTKViewer_CellRectPicker()
25 this->Cell = vtkGenericCell::New();
26 this->Quad1 = vtkQuad::New();
27 this->Quad2 = vtkQuad::New();
31 * \li Delete generic cell
32 * \li Delete two quads.
34 VTKViewer_CellRectPicker::~VTKViewer_CellRectPicker()
37 this->Quad1->Delete();
38 this->Quad2->Delete();
41 /*!Clear \a ActorData and call VTKViewer_RectPicker::Initialize() method*/
42 void VTKViewer_CellRectPicker::Initialize()
45 this->VTKViewer_RectPicker::Initialize();
49 float VTKViewer_CellRectPicker::IntersectWithHex(float p1[4][4], float p2[4][4], float tol,
50 vtkAssemblyPath *path, vtkProp3D *prop3D,
51 vtkAbstractMapper3D *m)
55 vtkVolumeMapper *volumeMapper;
57 // Get the underlying dataset
58 if ( (mapper=vtkMapper::SafeDownCast(m)) != NULL ) {
59 input = mapper->GetInput();
61 else if ( (volumeMapper=vtkVolumeMapper::SafeDownCast(m)) != NULL ) {
62 input = volumeMapper->GetInput();
65 return VTK_LARGE_FLOAT;
69 if ( (numCells = input->GetNumberOfCells()) < 1 ) {
74 float bounds[6], center[3], cp1[3], cp2[3];
75 float pcoord[3], weight[4], dist;
76 float t1[2], t2[2], x[3], t;
78 for (k = 0; k < 4; k++) {
79 this->Quad1->PointIds->SetId(k, k);
80 this->Quad1->Points->SetPoint(k, p1[k]);
81 this->Quad2->PointIds->SetId(k, k);
82 this->Quad2->Points->SetPoint(k, p2[k]);
86 cout << "---> Selection area:" << endl;
87 for (k = 0; k < 4; k++)
88 cout << "\t(" << p1[k][0] << ", " << p1[k][1] << ", " << p1[k][2] << ")";
90 for (k = 0; k < 4; k++)
91 cout << "\t(" << p2[k][0] << ", " << p2[k][1] << ", " << p2[k][2] << ")";
96 VTKViewer_CellData cellData;
97 VTKViewer_CellDataSet dataList;
99 char inside; int n; float *point;
100 float tMin = VTK_LARGE_FLOAT, xMin[3];
101 for (cellId = 0; cellId < numCells; cellId++) {
102 input->GetCell(cellId, this->Cell);
104 this->Cell->GetBounds(bounds);
105 for (i = 0; i < 3; i++)
106 center[i] = (bounds[2*i]+bounds[2*i+1])/2;
109 if (!VTKViewer_RectPicker::PointInside(center, p1, p2, 0.01))
114 n = this->Cell->Points->GetNumberOfPoints();
115 //cout << "---> Info cell " << cellId << " (" << n << "): " << endl;
116 for (k = 0; k < n; k++) {
117 point = this->Cell->Points->GetPoint(k);
118 //cout << " P (" << point[0] << ", " << point[1] << ", " << point[2] << ")";
119 if (!VTKViewer_RectPicker::PointInside(point, p1, p2)) {
124 if (!inside) continue;
126 //cout << "---> Inside cell " << cellId << endl;
128 this->Quad1->EvaluatePosition(center, 0, subId, pcoord, dist, weight);
129 t1[0] = pcoord[0]; t1[1] = pcoord[1];
130 this->Quad2->EvaluatePosition(center, 0, subId, pcoord, dist, weight);
131 t2[0] = pcoord[0]; t2[1] = pcoord[1];
133 pcoord[0] = (t1[0]+t2[0])/2; pcoord[1] = (t1[1]+t2[1])/2; pcoord[2] = 0;
135 this->Quad1->EvaluateLocation(subId, pcoord, cp1, weight);
136 this->Quad2->EvaluateLocation(subId, pcoord, cp2, weight);
138 if (this->Cell->IntersectWithLine(cp1, cp2, tol, t, x, pcoord, subId)) {
139 cellData.cellId = cellId;
140 cellData.subId = subId;
142 for (i = 0; i < 3; i++) {
143 cellData.p1[i] = cp1[i];
144 cellData.p2[i] = cp2[i];
147 cout << "---> Include cell " << cellId << ", depth = " << t << endl;
148 cout << " P1 = (" << cp1[0] << ", " << cp1[1] << ", " << cp1[2] << ")" << endl;
149 cout << " P2 = (" << cp2[0] << ", " << cp2[1] << ", " << cp2[2] << ")" << endl;
151 IncludeCell(input, cellData, dataList);
154 for (i = 0; i < 3; i++)
160 if (!dataList.empty()) {
161 // compare against other actors
162 IncludeActor(prop3D, input, dataList);
163 if (tMin < this->GlobalTMin) {
164 this->MarkPicked(path, prop3D, m, tMin, xMin);
172 void VTKViewer_CellRectPicker::IncludeCell(vtkDataSet* input,
173 VTKViewer_CellData cellData,
174 VTKViewer_CellDataSet& dataList)
176 vtkGenericCell* cell1 = vtkGenericCell::New();
177 vtkGenericCell* cell2 = vtkGenericCell::New();
181 input->GetCell(cellData.cellId, cell1);
182 if (cell1->GetNumberOfFaces() > 0)
183 sub1 = cell1->GetFace(cellData.subId);
186 float p1[3], p2[3], dir[3];
189 VTKViewer_CellData curData;
190 VTKViewer_CellDataSet::iterator it;
191 for (it = dataList.begin(); it != dataList.end();) {
193 for (i = 0; i < 3; i++) {
194 p1[i] = (cellData.p1[i]+curData.p1[i])/2;
195 p2[i] = (cellData.p2[i]+curData.p2[i])/2;
196 dir[i] = p2[i] - p1[i];
199 input->GetCell(curData.cellId, cell2);
201 if (cell2->GetNumberOfFaces() > 0)
202 sub2 = cell2->GetFace(curData.subId);
206 result = IntersectCells(sub1, cellData.depth,
207 sub2, curData.depth, dir);
209 result = IntersectCells(sub1, cellData.depth,
210 cell2, curData.depth, dir);
214 result = IntersectCells(cell1, cellData.depth,
215 sub2, curData.depth, dir);
217 result = IntersectCells(cell1, cellData.depth,
218 cell2, curData.depth, dir);
221 cout << " Direction = (" << dir[0] << ", " << dir[1] << ", " << dir[2] << ")";
222 cout << " depth = " << cellData.depth << ", " << curData.depth << ", " << result << endl;
226 it = dataList.erase(it);
237 //cout << " add " << endl;
238 dataList.push_back(cellData);
246 void VTKViewer_CellRectPicker::IncludeActor(vtkProp3D* prop,
248 VTKViewer_CellDataSet& dataList)
250 vtkGenericCell* cell1 = vtkGenericCell::New();
251 vtkGenericCell* cell2 = vtkGenericCell::New();
254 float p1[3], p2[3], dir[3];
257 VTKViewer_CellDataSet actorData;
258 VTKViewer_CellData curData, cellData;
259 VTKViewer_CellDataSet::iterator it1, it2;
260 VTKViewer_ActorDataMap::iterator it;
262 for (it1 = dataList.begin(); it1 != dataList.end();) {
264 input->GetCell(cellData.cellId, cell1);
267 for (it = ActorData.begin(); it != ActorData.end(); ++it) {
268 //vtkActor* actor = (*it).first;
269 actorData = (*it).second;
271 for (it2 = actorData.begin(); it2 != actorData.end();) {
273 for (i = 0; i < 3; i++) {
274 p1[i] = (cellData.p1[i]+curData.p1[i])/2;
275 p2[i] = (cellData.p2[i]+curData.p2[i])/2;
276 dir[i] = p2[i] - p1[i];
279 input->GetCell(curData.cellId, cell2);
280 result = IntersectCells(cell1, cellData.depth,
281 cell2, curData.depth, dir);
284 it2 = actorData.erase(it2);
289 it1 = dataList.erase(it1);
301 if (!dataList.empty()) {
303 if ((actor = vtkActor::SafeDownCast(prop)) != NULL)
304 ActorData[actor] = dataList;
311 /*! The method returns a non-zero value, if the cells intersect each other
312 * in the direction dir[3].
313 * \param c1 - first vtkCell pointer
315 * \param c2 - second vtkCell pointer
317 * \param dir[3] - direction
318 * \retval integer - Returned value is 1 if the first cell is top of
319 * the second one according to direction and 2 if the second cell is top.
321 int VTKViewer_CellRectPicker::IntersectCells(vtkCell* c1, float d1, vtkCell* c2, float d2, float dir[3])
324 float *orig, cp[3], bounds[6];
328 // define the central point and cell for projection
329 if (fabs(d1-d2) < 1.e-7) return 0;
332 orig = c1->Points->GetPoint(0);
335 c2->GetBounds(bounds);
336 for (i = 0; i < 3; i++)
337 cp[i] = (bounds[2*i] + bounds[2*i+1])/2;
340 orig = c2->Points->GetPoint(0);
343 c1->GetBounds(bounds);
344 for (i = 0; i < 3; i++)
345 cp[i] = (bounds[2*i] + bounds[2*i+1])/2;
349 vtkPlane::GeneralizedProjectPoint(cp, orig, dir, proj);
352 vtkPoints* aPoints = vtkPoints::New();
353 for (k = 0; k < cell->Points->GetNumberOfPoints(); k++) {
354 p = cell->Points->GetPoint(k);
355 vtkPlane::GeneralizedProjectPoint(p, orig, dir, pp);
356 aPoints->InsertNextPoint(pp[0], pp[1], pp[2]);
360 if (PointInside(proj, aPoints)) {
361 result = (d1<d2) ? 1 : 2;
365 // test for boundary cells
366 for (k = 0; k < tcell->Points->GetNumberOfPoints(); k++) {
367 p = tcell->Points->GetPoint(k);
368 vtkPlane::GeneralizedProjectPoint(p, orig, dir, pp);
369 if (PointInside(pp, aPoints)) {
373 if (result && d2<d1) result = 2;
380 /*! Check point position.
381 * \param point[3] - point
382 * \param list - point data set
383 * \retval 0 or 1 - The method returns a non-zero value, if the point is inlide point data set
385 char VTKViewer_CellRectPicker::PointInside(float point[3], vtkPoints* list)
388 float x[3], *a1, *a2, v[3], p[3], q[3];
390 n = list->GetNumberOfPoints();
393 for (k = 0; k < n; k++) {
394 l = k+1; if (l == n) l = 0;
395 a1 = list->GetPoint(k);
396 a2 = list->GetPoint(l);
399 for (i = 0; i < 3; i++) {
400 x[i] = point[i] - a1[i];
404 // check if point coincides with vertex
405 if (x[0]*x[0] + x[1]*x[1] + x[2]*x[2] < 1.e-7) return 0;
408 vtkMath::Cross(x, v, p);
410 // check if point lays on edge
411 if (p[0]*p[0] + p[1]*p[1] + p[2]*p[2] < 1.e-7) return 0;
413 // check if normals are in the same direction
415 if (vtkMath::Dot(p, q) < 0.0) return 0;
418 // remember normal direction
419 for (i = 0; i < 3; i++) {