1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/
19 #include <VTKViewer_CellRectPicker.h>
21 #include "vtkGenericCell.h"
22 #include "vtkAssemblyNode.h"
23 #include "vtkAssemblyPath.h"
24 #include "vtkObjectFactory.h"
25 #include "vtkImageData.h"
26 #include "vtkMapper.h"
27 #include "vtkVolumeMapper.h"
31 #include "vtkPoints.h"
35 vtkStandardNewMacro(VTKViewer_CellRectPicker);
38 * \li create instance of generic cell
39 * \li create two instances of Quad.
41 VTKViewer_CellRectPicker::VTKViewer_CellRectPicker()
43 this->Cell = vtkGenericCell::New();
44 this->Quad1 = vtkQuad::New();
45 this->Quad2 = vtkQuad::New();
49 * \li Delete generic cell
50 * \li Delete two quads.
52 VTKViewer_CellRectPicker::~VTKViewer_CellRectPicker()
55 this->Quad1->Delete();
56 this->Quad2->Delete();
59 /*!Clear \a ActorData and call VTKViewer_RectPicker::Initialize() method*/
60 void VTKViewer_CellRectPicker::Initialize()
63 this->VTKViewer_RectPicker::Initialize();
67 float VTKViewer_CellRectPicker::IntersectWithHex(float p1[4][4], float p2[4][4], float tol,
68 vtkAssemblyPath *path, vtkProp3D *prop3D,
69 vtkAbstractMapper3D *m)
73 vtkVolumeMapper *volumeMapper;
75 // Get the underlying dataset
76 if ( (mapper=vtkMapper::SafeDownCast(m)) != NULL ) {
77 input = mapper->GetInput();
79 else if ( (volumeMapper=vtkVolumeMapper::SafeDownCast(m)) != NULL ) {
80 input = volumeMapper->GetInput();
83 return VTK_LARGE_FLOAT;
87 if ( (numCells = input->GetNumberOfCells()) < 1 ) {
92 float bounds[6], center[3], cp1[3], cp2[3];
93 float pcoord[3], weight[4], dist;
94 float t1[2], t2[2], x[3], t;
96 for (k = 0; k < 4; k++) {
97 this->Quad1->PointIds->SetId(k, k);
98 this->Quad1->Points->SetPoint(k, p1[k]);
99 this->Quad2->PointIds->SetId(k, k);
100 this->Quad2->Points->SetPoint(k, p2[k]);
104 cout << "---> Selection area:" << endl;
105 for (k = 0; k < 4; k++)
106 cout << "\t(" << p1[k][0] << ", " << p1[k][1] << ", " << p1[k][2] << ")";
108 for (k = 0; k < 4; k++)
109 cout << "\t(" << p2[k][0] << ", " << p2[k][1] << ", " << p2[k][2] << ")";
114 VTKViewer_CellData cellData;
115 VTKViewer_CellDataSet dataList;
117 char inside; int n; float *point;
118 float tMin = VTK_LARGE_FLOAT, xMin[3];
119 for (cellId = 0; cellId < numCells; cellId++) {
120 input->GetCell(cellId, this->Cell);
122 this->Cell->GetBounds(bounds);
123 for (i = 0; i < 3; i++)
124 center[i] = (bounds[2*i]+bounds[2*i+1])/2;
127 if (!VTKViewer_RectPicker::PointInside(center, p1, p2, 0.01))
132 n = this->Cell->Points->GetNumberOfPoints();
133 //cout << "---> Info cell " << cellId << " (" << n << "): " << endl;
134 for (k = 0; k < n; k++) {
135 point = this->Cell->Points->GetPoint(k);
136 //cout << " P (" << point[0] << ", " << point[1] << ", " << point[2] << ")";
137 if (!VTKViewer_RectPicker::PointInside(point, p1, p2)) {
142 if (!inside) continue;
144 //cout << "---> Inside cell " << cellId << endl;
146 this->Quad1->EvaluatePosition(center, 0, subId, pcoord, dist, weight);
147 t1[0] = pcoord[0]; t1[1] = pcoord[1];
148 this->Quad2->EvaluatePosition(center, 0, subId, pcoord, dist, weight);
149 t2[0] = pcoord[0]; t2[1] = pcoord[1];
151 pcoord[0] = (t1[0]+t2[0])/2; pcoord[1] = (t1[1]+t2[1])/2; pcoord[2] = 0;
153 this->Quad1->EvaluateLocation(subId, pcoord, cp1, weight);
154 this->Quad2->EvaluateLocation(subId, pcoord, cp2, weight);
156 if (this->Cell->IntersectWithLine(cp1, cp2, tol, t, x, pcoord, subId)) {
157 cellData.cellId = cellId;
158 cellData.subId = subId;
160 for (i = 0; i < 3; i++) {
161 cellData.p1[i] = cp1[i];
162 cellData.p2[i] = cp2[i];
165 cout << "---> Include cell " << cellId << ", depth = " << t << endl;
166 cout << " P1 = (" << cp1[0] << ", " << cp1[1] << ", " << cp1[2] << ")" << endl;
167 cout << " P2 = (" << cp2[0] << ", " << cp2[1] << ", " << cp2[2] << ")" << endl;
169 IncludeCell(input, cellData, dataList);
172 for (i = 0; i < 3; i++)
178 if (!dataList.empty()) {
179 // compare against other actors
180 IncludeActor(prop3D, input, dataList);
181 if (tMin < this->GlobalTMin) {
182 this->MarkPicked(path, prop3D, m, tMin, xMin);
190 void VTKViewer_CellRectPicker::IncludeCell(vtkDataSet* input,
191 VTKViewer_CellData cellData,
192 VTKViewer_CellDataSet& dataList)
194 vtkGenericCell* cell1 = vtkGenericCell::New();
195 vtkGenericCell* cell2 = vtkGenericCell::New();
199 input->GetCell(cellData.cellId, cell1);
200 if (cell1->GetNumberOfFaces() > 0)
201 sub1 = cell1->GetFace(cellData.subId);
204 float p1[3], p2[3], dir[3];
207 VTKViewer_CellData curData;
208 VTKViewer_CellDataSet::iterator it;
209 for (it = dataList.begin(); it != dataList.end();) {
211 for (i = 0; i < 3; i++) {
212 p1[i] = (cellData.p1[i]+curData.p1[i])/2;
213 p2[i] = (cellData.p2[i]+curData.p2[i])/2;
214 dir[i] = p2[i] - p1[i];
217 input->GetCell(curData.cellId, cell2);
219 if (cell2->GetNumberOfFaces() > 0)
220 sub2 = cell2->GetFace(curData.subId);
224 result = IntersectCells(sub1, cellData.depth,
225 sub2, curData.depth, dir);
227 result = IntersectCells(sub1, cellData.depth,
228 cell2, curData.depth, dir);
232 result = IntersectCells(cell1, cellData.depth,
233 sub2, curData.depth, dir);
235 result = IntersectCells(cell1, cellData.depth,
236 cell2, curData.depth, dir);
239 cout << " Direction = (" << dir[0] << ", " << dir[1] << ", " << dir[2] << ")";
240 cout << " depth = " << cellData.depth << ", " << curData.depth << ", " << result << endl;
244 it = dataList.erase(it);
255 //cout << " add " << endl;
256 dataList.push_back(cellData);
264 void VTKViewer_CellRectPicker::IncludeActor(vtkProp3D* prop,
266 VTKViewer_CellDataSet& dataList)
268 vtkGenericCell* cell1 = vtkGenericCell::New();
269 vtkGenericCell* cell2 = vtkGenericCell::New();
272 float p1[3], p2[3], dir[3];
275 VTKViewer_CellDataSet actorData;
276 VTKViewer_CellData curData, cellData;
277 VTKViewer_CellDataSet::iterator it1, it2;
278 VTKViewer_ActorDataMap::iterator it;
280 for (it1 = dataList.begin(); it1 != dataList.end();) {
282 input->GetCell(cellData.cellId, cell1);
285 for (it = ActorData.begin(); it != ActorData.end(); ++it) {
286 //vtkActor* actor = (*it).first;
287 actorData = (*it).second;
289 for (it2 = actorData.begin(); it2 != actorData.end();) {
291 for (i = 0; i < 3; i++) {
292 p1[i] = (cellData.p1[i]+curData.p1[i])/2;
293 p2[i] = (cellData.p2[i]+curData.p2[i])/2;
294 dir[i] = p2[i] - p1[i];
297 input->GetCell(curData.cellId, cell2);
298 result = IntersectCells(cell1, cellData.depth,
299 cell2, curData.depth, dir);
302 it2 = actorData.erase(it2);
307 it1 = dataList.erase(it1);
319 if (!dataList.empty()) {
321 if ((actor = vtkActor::SafeDownCast(prop)) != NULL)
322 ActorData[actor] = dataList;
329 /*! The method returns a non-zero value, if the cells intersect each other
330 * in the direction dir[3].
331 * \param c1 - first vtkCell pointer
333 * \param c2 - second vtkCell pointer
335 * \param dir[3] - direction
336 * \retval integer - Returned value is 1 if the first cell is top of
337 * the second one according to direction and 2 if the second cell is top.
339 int VTKViewer_CellRectPicker::IntersectCells(vtkCell* c1, float d1, vtkCell* c2, float d2, float dir[3])
342 float *orig, cp[3], bounds[6];
346 // define the central point and cell for projection
347 if (fabs(d1-d2) < 1.e-7) return 0;
350 orig = c1->Points->GetPoint(0);
353 c2->GetBounds(bounds);
354 for (i = 0; i < 3; i++)
355 cp[i] = (bounds[2*i] + bounds[2*i+1])/2;
358 orig = c2->Points->GetPoint(0);
361 c1->GetBounds(bounds);
362 for (i = 0; i < 3; i++)
363 cp[i] = (bounds[2*i] + bounds[2*i+1])/2;
367 vtkPlane::GeneralizedProjectPoint(cp, orig, dir, proj);
370 vtkPoints* aPoints = vtkPoints::New();
371 for (k = 0; k < cell->Points->GetNumberOfPoints(); k++) {
372 p = cell->Points->GetPoint(k);
373 vtkPlane::GeneralizedProjectPoint(p, orig, dir, pp);
374 aPoints->InsertNextPoint(pp[0], pp[1], pp[2]);
378 if (PointInside(proj, aPoints)) {
379 result = (d1<d2) ? 1 : 2;
383 // test for boundary cells
384 for (k = 0; k < tcell->Points->GetNumberOfPoints(); k++) {
385 p = tcell->Points->GetPoint(k);
386 vtkPlane::GeneralizedProjectPoint(p, orig, dir, pp);
387 if (PointInside(pp, aPoints)) {
391 if (result && d2<d1) result = 2;
398 /*! Check point position.
399 * \param point[3] - point
400 * \param list - point data set
401 * \retval 0 or 1 - The method returns a non-zero value, if the point is inlide point data set
403 char VTKViewer_CellRectPicker::PointInside(float point[3], vtkPoints* list)
406 float x[3], *a1, *a2, v[3], p[3], q[3];
408 n = list->GetNumberOfPoints();
411 for (k = 0; k < n; k++) {
412 l = k+1; if (l == n) l = 0;
413 a1 = list->GetPoint(k);
414 a2 = list->GetPoint(l);
417 for (i = 0; i < 3; i++) {
418 x[i] = point[i] - a1[i];
422 // check if point coincides with vertex
423 if (x[0]*x[0] + x[1]*x[1] + x[2]*x[2] < 1.e-7) return 0;
426 vtkMath::Cross(x, v, p);
428 // check if point lays on edge
429 if (p[0]*p[0] + p[1]*p[1] + p[2]*p[2] < 1.e-7) return 0;
431 // check if normals are in the same direction
433 if (vtkMath::Dot(p, q) < 0.0) return 0;
436 // remember normal direction
437 for (i = 0; i < 3; i++) {