1 // Created on: 2014-05-22
2 // Created by: Varvara POSKONINA
3 // Copyright (c) 2005-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <NCollection_Vector.hxx>
17 #include <Poly_Array1OfTriangle.hxx>
19 #include <SelectMgr_RectangularFrustum.hxx>
21 // =======================================================================
22 // function : segmentSegmentDistance
24 // =======================================================================
25 void SelectMgr_RectangularFrustum::segmentSegmentDistance (const gp_Pnt& theSegPnt1,
26 const gp_Pnt& theSegPnt2,
27 Standard_Real& theDepth)
29 gp_XYZ anU = theSegPnt2.XYZ() - theSegPnt1.XYZ();
30 gp_XYZ aV = myViewRayDir.XYZ();
31 gp_XYZ aW = theSegPnt1.XYZ() - myNearPickedPnt.XYZ();
33 Standard_Real anA = anU.Dot (anU);
34 Standard_Real aB = anU.Dot (aV);
35 Standard_Real aC = aV.Dot (aV);
36 Standard_Real aD = anU.Dot (aW);
37 Standard_Real anE = aV.Dot (aW);
38 Standard_Real aCoef = anA * aC - aB * aB;
39 Standard_Real aSn = aCoef;
40 Standard_Real aTc, aTn, aTd = aCoef;
42 if (aCoef < gp::Resolution())
49 aSn = (aB * anE - aC * aD);
50 aTn = (anA * anE - aB * aD);
71 aTc = (Abs (aTd) < gp::Resolution() ? 0.0 : aTn / aTd);
73 gp_Pnt aClosestPnt = myNearPickedPnt.XYZ() + myViewRayDir.XYZ() * aTc;
74 theDepth = myNearPickedPnt.Distance (aClosestPnt) * myScale;
77 // =======================================================================
78 // function : segmentPlaneIntersection
80 // =======================================================================
81 void SelectMgr_RectangularFrustum::segmentPlaneIntersection (const gp_Vec& thePlane,
82 const gp_Pnt& thePntOnPlane,
83 Standard_Real& theDepth)
85 gp_XYZ anU = myViewRayDir.XYZ();
86 gp_XYZ aW = myNearPickedPnt.XYZ() - thePntOnPlane.XYZ();
87 Standard_Real aD = thePlane.Dot (anU);
88 Standard_Real aN = -thePlane.Dot (aW);
90 if (Abs (aD) < Precision::Confusion())
92 if (Abs (aN) < Precision::Angular())
104 Standard_Real aParam = aN / aD;
105 if (aParam < 0.0 || aParam > 1.0)
111 gp_Pnt aClosestPnt = myNearPickedPnt.XYZ() + anU * aParam;
112 theDepth = myNearPickedPnt.Distance (aClosestPnt) * myScale;
117 // =======================================================================
118 // function : computeFrustum
119 // purpose : Computes base frustum data: its vertices and edge directions
120 // =======================================================================
121 void computeFrustum (const gp_Pnt2d theMinPnt, const gp_Pnt2d& theMaxPnt,
122 const Handle(SelectMgr_FrustumBuilder)& theBuilder,
123 gp_Pnt* theVertices, gp_Vec* theEdges)
126 theVertices[0] = theBuilder->ProjectPntOnViewPlane (theMinPnt.X(),
130 theVertices[1] = theBuilder->ProjectPntOnViewPlane (theMinPnt.X(),
134 theVertices[2] = theBuilder->ProjectPntOnViewPlane (theMinPnt.X(),
138 theVertices[3] = theBuilder->ProjectPntOnViewPlane (theMinPnt.X(),
142 theVertices[4] = theBuilder->ProjectPntOnViewPlane (theMaxPnt.X(),
146 theVertices[5] = theBuilder->ProjectPntOnViewPlane (theMaxPnt.X(),
150 theVertices[6] = theBuilder->ProjectPntOnViewPlane (theMaxPnt.X(),
154 theVertices[7] = theBuilder->ProjectPntOnViewPlane (theMaxPnt.X(),
159 theEdges[0] = theVertices[4].XYZ() - theVertices[0].XYZ();
161 theEdges[1] = theVertices[2].XYZ() - theVertices[0].XYZ();
163 theEdges[2] = theVertices[2].XYZ() - theVertices[3].XYZ();
165 theEdges[3] = theVertices[6].XYZ() - theVertices[7].XYZ();
167 theEdges[4] = theVertices[0].XYZ() - theVertices[1].XYZ();
169 theEdges[5] = theVertices[4].XYZ() - theVertices[5].XYZ();
172 // =======================================================================
173 // function : computeNormals
174 // purpose : Computes normals to frustum faces
175 // =======================================================================
176 void computeNormals (const gp_Vec* theEdges, gp_Vec* theNormals)
179 theNormals[0] = theEdges[0].Crossed (theEdges[4]);
181 theNormals[1] = theEdges[2].Crossed (theEdges[0]);
183 theNormals[2] = theEdges[4].Crossed (theEdges[1]);
185 theNormals[3] = theEdges[1].Crossed (theEdges[5]);
187 theNormals[4] = theEdges[0].Crossed (theEdges[1]);
189 theNormals[5] = -theNormals[4];
193 // =======================================================================
194 // function : cacheVertexProjections
195 // purpose : Caches projection of frustum's vertices onto its plane directions
197 // =======================================================================
198 void SelectMgr_RectangularFrustum::cacheVertexProjections (SelectMgr_RectangularFrustum* theFrustum) const
200 if (theFrustum->myIsOrthographic)
202 // project vertices onto frustum normals
203 // Since orthographic view volume's faces are always a pairwise translation of
204 // one another, only 2 vertices that belong to opposite faces can be projected
205 // to simplify calculations.
206 Standard_Integer aVertIdxs[6] = { LeftTopNear, LeftBottomNear, // opposite planes in height direction
207 LeftBottomNear, RightBottomNear, // opposite planes in width direcion
208 LeftBottomFar, RightBottomNear }; // opposite planes in depth direction
209 for (Standard_Integer aPlaneIdx = 0; aPlaneIdx < 5; aPlaneIdx += 2)
211 Standard_Real aProj1 = theFrustum->myPlanes[aPlaneIdx].XYZ().Dot (theFrustum->myVertices[aVertIdxs[aPlaneIdx]].XYZ());
212 Standard_Real aProj2 = theFrustum->myPlanes[aPlaneIdx].XYZ().Dot (theFrustum->myVertices[aVertIdxs[aPlaneIdx + 1]].XYZ());
213 theFrustum->myMinVertsProjections[aPlaneIdx] = Min (aProj1, aProj2);
214 theFrustum->myMaxVertsProjections[aPlaneIdx] = Max (aProj1, aProj2);
219 // project all vertices onto frustum normals
220 for (Standard_Integer aPlaneIdx = 0; aPlaneIdx < 6; ++aPlaneIdx)
222 Standard_Real aMax = -DBL_MAX;
223 Standard_Real aMin = DBL_MAX;
224 const gp_XYZ& aPlane = theFrustum->myPlanes[aPlaneIdx].XYZ();
225 for (Standard_Integer aVertIdx = 0; aVertIdx < 8; ++aVertIdx)
227 Standard_Real aProjection = aPlane.Dot (theFrustum->myVertices[aVertIdx].XYZ());
228 aMin = Min (aMin, aProjection);
229 aMax = Max (aMax, aProjection);
231 theFrustum->myMinVertsProjections[aPlaneIdx] = aMin;
232 theFrustum->myMaxVertsProjections[aPlaneIdx] = aMax;
236 // project vertices onto {i, j, k}
237 for (Standard_Integer aDim = 0; aDim < 3; ++aDim)
239 Standard_Real aMax = -DBL_MAX;
240 Standard_Real aMin = DBL_MAX;
241 for (Standard_Integer aVertIdx = 0; aVertIdx < 8; ++aVertIdx)
243 const gp_XYZ& aVert = theFrustum->myVertices[aVertIdx].XYZ();
244 aMax = Max (aVert.GetData()[aDim], aMax);
245 aMin = Min (aVert.GetData()[aDim], aMin);
247 theFrustum->myMaxOrthoVertsProjections[aDim] = aMax;
248 theFrustum->myMinOrthoVertsProjections[aDim] = aMin;
252 // =======================================================================
254 // purpose : Build volume according to the point and given pixel
256 // =======================================================================
257 void SelectMgr_RectangularFrustum::Build (const gp_Pnt2d &thePoint)
259 myNearPickedPnt = myBuilder->ProjectPntOnViewPlane (thePoint.X(), thePoint.Y(), 0.0);
260 myFarPickedPnt = myBuilder->ProjectPntOnViewPlane (thePoint.X(), thePoint.Y(), 1.0);
261 myViewRayDir = myFarPickedPnt.XYZ() - myNearPickedPnt.XYZ();
262 myMousePos = thePoint;
264 gp_Pnt2d aMinPnt (thePoint.X() - myPixelTolerance * 0.5,
265 thePoint.Y() - myPixelTolerance * 0.5);
266 gp_Pnt2d aMaxPnt (thePoint.X() + myPixelTolerance * 0.5,
267 thePoint.Y() + myPixelTolerance * 0.5);
269 // calculate base frustum characteristics: vertices and edge directions
270 computeFrustum (aMinPnt, aMaxPnt, myBuilder, myVertices, myEdgeDirs);
272 // compute frustum normals
273 computeNormals (myEdgeDirs, myPlanes);
275 // compute vertices projections onto frustum normals and
276 // {i, j, k} vectors and store them to corresponding class fields
277 cacheVertexProjections (this);
279 myViewClipRange.SetVoid();
284 // =======================================================================
286 // purpose : Build volume according to the selected rectangle
287 // =======================================================================
288 void SelectMgr_RectangularFrustum::Build (const gp_Pnt2d& theMinPnt,
289 const gp_Pnt2d& theMaxPnt)
291 myNearPickedPnt = myBuilder->ProjectPntOnViewPlane ((theMinPnt.X() + theMaxPnt.X()) * 0.5,
292 (theMinPnt.Y() + theMaxPnt.Y()) * 0.5,
294 myFarPickedPnt = myBuilder->ProjectPntOnViewPlane ((theMinPnt.X() + theMaxPnt.X()) * 0.5,
295 (theMinPnt.Y() + theMaxPnt.Y()) * 0.5,
297 myViewRayDir = myFarPickedPnt.XYZ() - myNearPickedPnt.XYZ();
299 // calculate base frustum characteristics: vertices and edge directions
300 computeFrustum (theMinPnt, theMaxPnt, myBuilder, myVertices, myEdgeDirs);
302 // compute frustum normals
303 computeNormals (myEdgeDirs, myPlanes);
305 // compute vertices projections onto frustum normals and
306 // {i, j, k} vectors and store them to corresponding class fields
307 cacheVertexProjections (this);
309 myViewClipRange.SetVoid();
314 // =======================================================================
315 // function : ScaleAndTransform
316 // purpose : IMPORTANT: Scaling makes sense only for frustum built on a single point!
317 // Note that this method does not perform any checks on type of the frustum.
318 // Returns a copy of the frustum resized according to the scale factor given
319 // and transforms it using the matrix given.
320 // There are no default parameters, but in case if:
321 // - transformation only is needed: @theScaleFactor must be initialized
322 // as any negative value;
323 // - scale only is needed: @theTrsf must be set to gp_Identity.
324 // =======================================================================
325 Handle(SelectMgr_BaseFrustum) SelectMgr_RectangularFrustum::ScaleAndTransform (const Standard_Integer theScaleFactor,
326 const gp_GTrsf& theTrsf) const
328 Standard_ASSERT_RAISE (theScaleFactor > 0,
329 "Error! Pixel tolerance for selection should be greater than zero");
331 Handle(SelectMgr_RectangularFrustum) aRes = new SelectMgr_RectangularFrustum();
332 const Standard_Boolean isToScale = theScaleFactor != 1;
333 const Standard_Boolean isToTrsf = theTrsf.Form() != gp_Identity;
335 if (!isToScale && !isToTrsf)
338 aRes->myIsOrthographic = myIsOrthographic;
339 const SelectMgr_RectangularFrustum* aRef = this;
343 aRes->myNearPickedPnt = myNearPickedPnt;
344 aRes->myFarPickedPnt = myFarPickedPnt;
345 aRes->myViewRayDir = myViewRayDir;
347 const gp_Pnt2d aMinPnt (myMousePos.X() - theScaleFactor * 0.5,
348 myMousePos.Y() - theScaleFactor * 0.5);
349 const gp_Pnt2d aMaxPnt (myMousePos.X() + theScaleFactor * 0.5,
350 myMousePos.Y() + theScaleFactor * 0.5);
352 // recompute base frustum characteristics from scratch
353 computeFrustum (aMinPnt, aMaxPnt, myBuilder, aRes->myVertices, aRes->myEdgeDirs);
360 const Standard_Real aRefScale = aRef->myFarPickedPnt.SquareDistance (aRef->myNearPickedPnt);
362 gp_Pnt aPoint = aRef->myNearPickedPnt;
363 theTrsf.Transforms (aPoint.ChangeCoord());
364 aRes->myNearPickedPnt = aPoint;
366 aPoint.SetXYZ (aRef->myFarPickedPnt.XYZ());
367 theTrsf.Transforms (aPoint.ChangeCoord());
368 aRes->myFarPickedPnt = aPoint;
370 aRes->myViewRayDir = aRes->myFarPickedPnt.XYZ() - aRes->myNearPickedPnt.XYZ();
372 for (Standard_Integer anIt = 0; anIt < 8; anIt++)
374 aPoint = aRef->myVertices[anIt];
375 theTrsf.Transforms (aPoint.ChangeCoord());
376 aRes->myVertices[anIt] = aPoint;
380 aRes->myEdgeDirs[0] = aRes->myVertices[4].XYZ() - aRes->myVertices[0].XYZ();
382 aRes->myEdgeDirs[1] = aRes->myVertices[2].XYZ() - aRes->myVertices[0].XYZ();
384 aRes->myEdgeDirs[2] = aRes->myVertices[2].XYZ() - aRes->myVertices[3].XYZ();
386 aRes->myEdgeDirs[3] = aRes->myVertices[6].XYZ() - aRes->myVertices[7].XYZ();
388 aRes->myEdgeDirs[4] = aRes->myVertices[0].XYZ() - aRes->myVertices[1].XYZ();
390 aRes->myEdgeDirs[5] = aRes->myVertices[4].XYZ() - aRes->myVertices[5].XYZ();
392 // Compute scale to transform depth from local coordinate system to world coordinate system
393 aRes->myScale = Sqrt (aRefScale / aRes->myFarPickedPnt.SquareDistance (aRes->myNearPickedPnt));
396 // compute frustum normals
397 computeNormals (aRes->myEdgeDirs, aRes->myPlanes);
399 cacheVertexProjections (aRes.get());
401 aRes->myViewClipRange = myViewClipRange;
402 aRes->myIsViewClipEnabled = myIsViewClipEnabled;
403 aRes->myMousePos = myMousePos;
408 // =======================================================================
409 // function : Overlaps
410 // purpose : Returns true if selecting volume is overlapped by
411 // axis-aligned bounding box with minimum corner at point
412 // theMinPnt and maximum at point theMaxPnt
413 // =======================================================================
414 Standard_Boolean SelectMgr_RectangularFrustum::Overlaps (const SelectMgr_Vec3& theBoxMin,
415 const SelectMgr_Vec3& theBoxMax,
416 Standard_Boolean* theInside)
418 return hasOverlap (theBoxMin, theBoxMax, theInside);
421 // =======================================================================
422 // function : Overlaps
423 // purpose : SAT intersection test between defined volume and
424 // given axis-aligned box
425 // =======================================================================
426 Standard_Boolean SelectMgr_RectangularFrustum::Overlaps (const SelectMgr_Vec3& theBoxMin,
427 const SelectMgr_Vec3& theBoxMax,
428 Standard_Real& theDepth)
430 if (!hasOverlap (theBoxMin, theBoxMax))
431 return Standard_False;
433 gp_Pnt aNearestPnt (RealLast(), RealLast(), RealLast());
434 aNearestPnt.SetX (Max (Min (myNearPickedPnt.X(), theBoxMax.x()), theBoxMin.x()));
435 aNearestPnt.SetY (Max (Min (myNearPickedPnt.Y(), theBoxMax.y()), theBoxMin.y()));
436 aNearestPnt.SetZ (Max (Min (myNearPickedPnt.Z(), theBoxMax.z()), theBoxMin.z()));
438 theDepth = aNearestPnt.Distance (myNearPickedPnt);
440 return isViewClippingOk (theDepth);
443 // =======================================================================
444 // function : Overlaps
445 // purpose : Intersection test between defined volume and given point
446 // =======================================================================
447 Standard_Boolean SelectMgr_RectangularFrustum::Overlaps (const gp_Pnt& thePnt,
448 Standard_Real& theDepth)
450 if (!hasOverlap (thePnt))
451 return Standard_False;
453 gp_XYZ aV = thePnt.XYZ() - myNearPickedPnt.XYZ();
454 gp_Pnt aDetectedPnt =
455 myNearPickedPnt.XYZ() + myViewRayDir.XYZ() * (aV.Dot (myViewRayDir.XYZ()) / myViewRayDir.Dot (myViewRayDir));
457 theDepth = aDetectedPnt.Distance (myNearPickedPnt) * myScale;
459 return isViewClippingOk (theDepth);
462 // =======================================================================
463 // function : Overlaps
464 // purpose : Intersection test between defined volume and given point
465 // =======================================================================
466 Standard_Boolean SelectMgr_RectangularFrustum::Overlaps (const gp_Pnt& thePnt)
468 return hasOverlap (thePnt);
471 // =======================================================================
472 // function : Overlaps
473 // purpose : Checks if line segment overlaps selecting frustum
474 // =======================================================================
475 Standard_Boolean SelectMgr_RectangularFrustum::Overlaps (const gp_Pnt& thePnt1,
476 const gp_Pnt& thePnt2,
477 Standard_Real& theDepth)
480 if (!hasOverlap (thePnt1, thePnt2))
481 return Standard_False;
483 segmentSegmentDistance (thePnt1, thePnt2, theDepth);
485 return isViewClippingOk (theDepth);
488 // =======================================================================
489 // function : Overlaps
490 // purpose : SAT intersection test between defined volume and given
491 // ordered set of points, representing line segments. The test
492 // may be considered of interior part or boundary line defined
493 // by segments depending on given sensitivity type
494 // =======================================================================
495 Standard_Boolean SelectMgr_RectangularFrustum::Overlaps (const TColgp_Array1OfPnt& theArrayOfPnts,
496 Select3D_TypeOfSensitivity theSensType,
497 Standard_Real& theDepth)
499 if (theSensType == Select3D_TOS_BOUNDARY)
501 Standard_Integer aMatchingSegmentsNb = -1;
503 const Standard_Integer aLower = theArrayOfPnts.Lower();
504 const Standard_Integer anUpper = theArrayOfPnts.Upper();
505 for (Standard_Integer aPntIter = aLower; aPntIter <= anUpper; ++aPntIter)
507 const gp_Pnt& aStartPnt = theArrayOfPnts.Value (aPntIter);
508 const gp_Pnt& aEndPnt = theArrayOfPnts.Value (aPntIter == anUpper ? aLower : (aPntIter + 1));
509 if (hasOverlap (aStartPnt, aEndPnt))
511 aMatchingSegmentsNb++;
512 Standard_Real aSegmentDepth = RealLast();
513 segmentSegmentDistance (aStartPnt, aEndPnt, aSegmentDepth);
514 theDepth = Min (theDepth, aSegmentDepth);
518 if (aMatchingSegmentsNb == -1)
519 return Standard_False;
521 else if (theSensType == Select3D_TOS_INTERIOR)
523 gp_Vec aPolyNorm (gp_XYZ (RealLast(), RealLast(), RealLast()));
524 if (!hasOverlap (theArrayOfPnts, aPolyNorm))
525 return Standard_False;
527 segmentPlaneIntersection (aPolyNorm,
528 theArrayOfPnts.Value (theArrayOfPnts.Lower()),
532 return isViewClippingOk (theDepth);
535 // =======================================================================
536 // function : Overlaps
537 // purpose : SAT intersection test between defined volume and given
538 // triangle. The test may be considered of interior part or
539 // boundary line defined by triangle vertices depending on
540 // given sensitivity type
541 // =======================================================================
542 Standard_Boolean SelectMgr_RectangularFrustum::Overlaps (const gp_Pnt& thePnt1,
543 const gp_Pnt& thePnt2,
544 const gp_Pnt& thePnt3,
545 Select3D_TypeOfSensitivity theSensType,
546 Standard_Real& theDepth)
548 if (theSensType == Select3D_TOS_BOUNDARY)
550 const gp_Pnt aPntsArrayBuf[4] = { thePnt1, thePnt2, thePnt3, thePnt1 };
551 const TColgp_Array1OfPnt aPntsArray (aPntsArrayBuf[0], 1, 4);
552 return Overlaps (aPntsArray, Select3D_TOS_BOUNDARY, theDepth);
554 else if (theSensType == Select3D_TOS_INTERIOR)
556 gp_Vec aTriangleNormal (gp_XYZ (RealLast(), RealLast(), RealLast()));
557 if (!hasOverlap (thePnt1, thePnt2, thePnt3, aTriangleNormal))
558 return Standard_False;
560 // check if intersection point belongs to triangle's interior part
561 gp_XYZ aTrEdges[3] = { thePnt2.XYZ() - thePnt1.XYZ(),
562 thePnt3.XYZ() - thePnt2.XYZ(),
563 thePnt1.XYZ() - thePnt3.XYZ() };
565 Standard_Real anAlpha = aTriangleNormal.Dot (myViewRayDir);
566 if (Abs (anAlpha) < gp::Resolution())
568 // handle degenerated triangles: in this case, there is no possible way to detect overlap correctly.
569 if (aTriangleNormal.SquareMagnitude() < gp::Resolution())
571 theDepth = std::numeric_limits<Standard_Real>::max();
572 return Standard_False;
575 // handle the case when triangle normal and selecting frustum direction are orthogonal: for this case, overlap
576 // is detected correctly, and distance to triangle's plane can be measured as distance to its arbitrary vertex.
577 const gp_XYZ aDiff = myNearPickedPnt.XYZ() - thePnt1.XYZ();
578 theDepth = aTriangleNormal.Dot (aDiff) * myScale;
580 return isViewClippingOk (theDepth);
583 gp_XYZ anEdge = (thePnt1.XYZ() - myNearPickedPnt.XYZ()) * (1.0 / anAlpha);
585 Standard_Real aTime = aTriangleNormal.Dot (anEdge);
587 gp_XYZ aVec = myViewRayDir.XYZ().Crossed (anEdge);
589 Standard_Real anU = aVec.Dot (aTrEdges[2]);
590 Standard_Real aV = aVec.Dot (aTrEdges[0]);
592 Standard_Boolean isInterior = (aTime >= 0.0) && (anU >= 0.0) && (aV >= 0.0) && (anU + aV <= 1.0);
596 gp_Pnt aDetectedPnt = myNearPickedPnt.XYZ() + myViewRayDir.XYZ() * aTime;
597 theDepth = myNearPickedPnt.Distance (aDetectedPnt) * myScale;
599 return isViewClippingOk (theDepth);
602 gp_Pnt aPnts[3] = {thePnt1, thePnt2, thePnt3};
603 Standard_Real aMinDist = RealLast();
604 Standard_Integer aNearestEdgeIdx = -1;
605 gp_Pnt aPtOnPlane = myNearPickedPnt.XYZ() + myViewRayDir.XYZ() * aTime;
606 for (Standard_Integer anEdgeIdx = 0; anEdgeIdx < 3; ++anEdgeIdx)
608 gp_XYZ aW = aPtOnPlane.XYZ() - aPnts[anEdgeIdx].XYZ();
609 Standard_Real aCoef = aTrEdges[anEdgeIdx].Dot (aW) / aTrEdges[anEdgeIdx].Dot (aTrEdges[anEdgeIdx]);
610 Standard_Real aDist = aPtOnPlane.Distance (aPnts[anEdgeIdx].XYZ() + aCoef * aTrEdges[anEdgeIdx]);
611 if (aMinDist > aDist)
614 aNearestEdgeIdx = anEdgeIdx;
617 segmentSegmentDistance (aPnts[aNearestEdgeIdx], aPnts[(aNearestEdgeIdx + 1) % 3], theDepth);
620 return isViewClippingOk (theDepth);
623 // =======================================================================
624 // function : DistToGeometryCenter
625 // purpose : Measures distance between 3d projection of user-picked
626 // screen point and given point theCOG
627 // =======================================================================
628 Standard_Real SelectMgr_RectangularFrustum::DistToGeometryCenter (const gp_Pnt& theCOG)
630 return theCOG.Distance (myNearPickedPnt) * myScale;
633 // =======================================================================
634 // function : DetectedPoint
635 // purpose : Calculates the point on a view ray that was detected during
636 // the run of selection algo by given depth
637 // =======================================================================
638 gp_Pnt SelectMgr_RectangularFrustum::DetectedPoint (const Standard_Real theDepth) const
640 return myNearPickedPnt.XYZ() + myViewRayDir.Normalized().XYZ() * theDepth / myScale;
643 // =======================================================================
644 // function : computeClippingRange
646 // =======================================================================
647 void SelectMgr_RectangularFrustum::computeClippingRange (const Graphic3d_SequenceOfHClipPlane& thePlanes,
648 SelectMgr_ViewClipRange& theRange)
650 Standard_Real aPlaneA, aPlaneB, aPlaneC, aPlaneD;
651 for (Graphic3d_SequenceOfHClipPlane::Iterator aPlaneIt (thePlanes); aPlaneIt.More(); aPlaneIt.Next())
653 const Handle(Graphic3d_ClipPlane)& aClipPlane = aPlaneIt.Value();
654 if (!aClipPlane->IsOn())
659 Bnd_Range aSubRange (RealFirst(), RealLast());
660 for (const Graphic3d_ClipPlane* aSubPlaneIter = aClipPlane.get(); aSubPlaneIter != NULL; aSubPlaneIter = aSubPlaneIter->ChainNextPlane().get())
662 const gp_Pln aGeomPlane = aSubPlaneIter->ToPlane();
663 aGeomPlane.Coefficients (aPlaneA, aPlaneB, aPlaneC, aPlaneD);
665 const gp_XYZ& aPlaneDirXYZ = aGeomPlane.Axis().Direction().XYZ();
666 Standard_Real aDotProduct = myViewRayDir.XYZ().Dot (aPlaneDirXYZ);
667 Standard_Real aDistance = -myNearPickedPnt.XYZ().Dot (aPlaneDirXYZ) - aPlaneD;
668 Standard_Real aDistToPln = 0.0;
670 // check whether the pick line is parallel to clip plane
671 if (Abs (aDotProduct) < Precision::Angular())
677 aDistToPln = RealLast();
682 // compute distance to point of pick line intersection with the plane
683 const Standard_Real aParam = aDistance / aDotProduct;
685 const gp_Pnt anIntersectionPnt = myNearPickedPnt.XYZ() + myViewRayDir.XYZ() * aParam;
686 aDistToPln = anIntersectionPnt.Distance (myNearPickedPnt);
689 // the plane is "behind" the ray
690 aDistToPln = -aDistToPln;
694 // change depth limits for case of opposite and directed planes
695 if (!aClipPlane->IsChain())
697 if (aDotProduct < 0.0)
699 theRange.ChangeMain().Add (Bnd_Range (aDistToPln, RealLast()));
703 theRange.ChangeMain().Add (Bnd_Range (RealFirst(), aDistToPln));
708 if (aDotProduct < 0.0)
710 aSubRange.TrimFrom (aDistToPln);
714 aSubRange.TrimTo (aDistToPln);
719 if (!aSubRange.IsVoid()
720 && aClipPlane->IsChain())
722 theRange.AddSubRange (aSubRange);
727 // =======================================================================
728 // function : IsClipped
729 // purpose : Checks if the point of sensitive in which selection was
730 // detected belongs to the region defined by clipping planes
731 // =======================================================================
732 Standard_Boolean SelectMgr_RectangularFrustum::IsClipped (const Graphic3d_SequenceOfHClipPlane& thePlanes,
733 const Standard_Real theDepth)
735 SelectMgr_ViewClipRange aRange;
736 computeClippingRange (thePlanes, aRange);
737 return aRange.IsClipped (theDepth);
740 // =======================================================================
741 // function : SetViewClipping
743 // =======================================================================
744 void SelectMgr_RectangularFrustum::SetViewClipping (const Handle(Graphic3d_SequenceOfHClipPlane)& thePlanes)
746 if (thePlanes.IsNull()
747 || thePlanes->IsEmpty())
749 myViewClipRange.SetVoid();
753 computeClippingRange (*thePlanes, myViewClipRange);
756 // =======================================================================
757 // function : isViewClippingOk
759 // =======================================================================
760 Standard_Boolean SelectMgr_RectangularFrustum::isViewClippingOk (const Standard_Real theDepth) const
762 return !myIsViewClipEnabled
763 || !myViewClipRange.IsClipped (theDepth);
766 // =======================================================================
767 // function : GetPlanes
769 // =======================================================================
770 void SelectMgr_RectangularFrustum::GetPlanes (NCollection_Vector<SelectMgr_Vec4>& thePlaneEquations) const
772 thePlaneEquations.Clear();
774 SelectMgr_Vec4 anEquation;
775 for (Standard_Integer aPlaneIdx = 0; aPlaneIdx < 6; ++aPlaneIdx)
777 const gp_Vec& aPlaneNorm = myIsOrthographic && aPlaneIdx % 2 == 1 ?
778 myPlanes[aPlaneIdx - 1].Reversed() : myPlanes[aPlaneIdx];
779 anEquation.x() = aPlaneNorm.X();
780 anEquation.y() = aPlaneNorm.Y();
781 anEquation.z() = aPlaneNorm.Z();
782 anEquation.w() = - (aPlaneNorm.XYZ().Dot (myVertices[aPlaneIdx % 2 == 0 ? aPlaneIdx : aPlaneIdx + 2].XYZ()));
783 thePlaneEquations.Append (anEquation);