1 // Created on: 2014-11-21
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 <SelectMgr_TriangularFrustum.hxx>
18 #include <SelectMgr_FrustumBuilder.hxx>
19 #include <SelectMgr_ViewClipRange.hxx>
21 IMPLEMENT_STANDARD_RTTIEXT(SelectMgr_TriangularFrustum, Standard_Transient)
25 void computeFrustumNormals (const gp_Vec* theEdges, gp_Vec* theNormals)
28 theNormals[0] = theEdges[0].Crossed (theEdges[3]);
30 theNormals[1] = theEdges[1].Crossed (theEdges[4]);
32 theNormals[2] = theEdges[0].Crossed (theEdges[5]);
34 theNormals[3] = theEdges[3].Crossed (theEdges[4]);
36 theNormals[4] = -theNormals[3];
40 // =======================================================================
41 // function : SelectMgr_TriangularFrustum
43 // =======================================================================
44 SelectMgr_TriangularFrustum::SelectMgr_TriangularFrustum()
49 // =======================================================================
50 // function : ~SelectMgr_TriangularFrustum
52 // =======================================================================
53 SelectMgr_TriangularFrustum::~SelectMgr_TriangularFrustum()
58 // =======================================================================
59 // function : cacheVertexProjections
60 // purpose : Caches projection of frustum's vertices onto its plane directions
62 // =======================================================================
63 void SelectMgr_TriangularFrustum::cacheVertexProjections (SelectMgr_TriangularFrustum* theFrustum) const
65 for (Standard_Integer aPlaneIdx = 0; aPlaneIdx < 5; ++aPlaneIdx)
67 Standard_Real aMax = -DBL_MAX;
68 Standard_Real aMin = DBL_MAX;
69 const gp_XYZ& aPlane = theFrustum->myPlanes[aPlaneIdx].XYZ();
70 for (Standard_Integer aVertIdx = 0; aVertIdx < 6; ++aVertIdx)
72 Standard_Real aProjection = aPlane.Dot (theFrustum->myVertices[aVertIdx].XYZ());
73 aMax = Max (aMax, aProjection);
74 aMin = Min (aMin, aProjection);
76 theFrustum->myMaxVertsProjections[aPlaneIdx] = aMax;
77 theFrustum->myMinVertsProjections[aPlaneIdx] = aMin;
80 for (Standard_Integer aDim = 0; aDim < 3; ++aDim)
82 Standard_Real aMax = -DBL_MAX;
83 Standard_Real aMin = DBL_MAX;
84 for (Standard_Integer aVertIdx = 0; aVertIdx < 6; ++aVertIdx)
86 Standard_Real aProjection = theFrustum->myVertices[aVertIdx].XYZ().GetData()[aDim];
87 aMax = Max (aMax, aProjection);
88 aMin = Min (aMin, aProjection);
90 theFrustum->myMaxOrthoVertsProjections[aDim] = aMax;
91 theFrustum->myMinOrthoVertsProjections[aDim] = aMin;
95 //=======================================================================
98 //=======================================================================
99 void SelectMgr_TriangularFrustum::Init (const gp_Pnt2d& theP1,
100 const gp_Pnt2d& theP2,
101 const gp_Pnt2d& theP3)
103 mySelTriangle.Points[0] = theP1;
104 mySelTriangle.Points[1] = theP2;
105 mySelTriangle.Points[2] = theP3;
108 //=======================================================================
111 //=======================================================================
112 void SelectMgr_TriangularFrustum::Build()
115 myVertices[0] = myBuilder->ProjectPntOnViewPlane (mySelTriangle.Points[0].X(), mySelTriangle.Points[0].Y(), 0.0);
117 myVertices[1] = myBuilder->ProjectPntOnViewPlane (mySelTriangle.Points[1].X(), mySelTriangle.Points[1].Y(), 0.0);
119 myVertices[2] = myBuilder->ProjectPntOnViewPlane (mySelTriangle.Points[2].X(), mySelTriangle.Points[2].Y(), 0.0);
121 myVertices[3] = myBuilder->ProjectPntOnViewPlane (mySelTriangle.Points[0].X(), mySelTriangle.Points[0].Y(), 1.0);
123 myVertices[4] = myBuilder->ProjectPntOnViewPlane (mySelTriangle.Points[1].X(), mySelTriangle.Points[1].Y(), 1.0);
125 myVertices[5] = myBuilder->ProjectPntOnViewPlane (mySelTriangle.Points[2].X(), mySelTriangle.Points[2].Y(), 1.0);
128 myEdgeDirs[0] = myVertices[0].XYZ() - myVertices[3].XYZ();
130 myEdgeDirs[1] = myVertices[1].XYZ() - myVertices[4].XYZ();
132 myEdgeDirs[2] = myVertices[2].XYZ() - myVertices[5].XYZ();
134 myEdgeDirs[3] = myVertices[1].XYZ() - myVertices[0].XYZ();
136 myEdgeDirs[4] = myVertices[2].XYZ() - myVertices[1].XYZ();
138 myEdgeDirs[5] = myVertices[2].XYZ() - myVertices[0].XYZ();
140 computeFrustumNormals (myEdgeDirs, myPlanes);
142 cacheVertexProjections (this);
145 //=======================================================================
146 // function : ScaleAndTransform
147 // purpose : IMPORTANT: Scaling makes sense only for frustum built on a single point!
148 // Note that this method does not perform any checks on type of the frustum.
149 // Returns a copy of the frustum resized according to the scale factor given
150 // and transforms it using the matrix given.
151 // There are no default parameters, but in case if:
152 // - transformation only is needed: @theScaleFactor must be initialized
153 // as any negative value;
154 // - scale only is needed: @theTrsf must be set to gp_Identity.
155 //=======================================================================
156 Handle(SelectMgr_BaseIntersector) SelectMgr_TriangularFrustum::ScaleAndTransform (const Standard_Integer,
157 const gp_GTrsf& theTrsf,
158 const Handle(SelectMgr_FrustumBuilder)&) const
160 Handle(SelectMgr_TriangularFrustum) aRes = new SelectMgr_TriangularFrustum();
161 aRes->SetCamera (myCamera);
163 for (Standard_Integer anIt = 0; anIt < 6; anIt++)
165 gp_Pnt aPoint = myVertices[anIt];
166 theTrsf.Transforms (aPoint.ChangeCoord());
167 aRes->myVertices[anIt] = aPoint;
171 aRes->myEdgeDirs[0] = aRes->myVertices[0].XYZ() - aRes->myVertices[3].XYZ();
173 aRes->myEdgeDirs[1] = aRes->myVertices[1].XYZ() - aRes->myVertices[4].XYZ();
175 aRes->myEdgeDirs[2] = aRes->myVertices[2].XYZ() - aRes->myVertices[5].XYZ();
177 aRes->myEdgeDirs[3] = aRes->myVertices[1].XYZ() - aRes->myVertices[0].XYZ();
179 aRes->myEdgeDirs[4] = aRes->myVertices[2].XYZ() - aRes->myVertices[1].XYZ();
181 aRes->myEdgeDirs[5] = aRes->myVertices[2].XYZ() - aRes->myVertices[0].XYZ();
183 computeFrustumNormals (aRes->myEdgeDirs, aRes->myPlanes);
185 cacheVertexProjections (aRes.get());
187 aRes->mySelTriangle = mySelTriangle;
192 //=======================================================================
193 // function : CopyWithBuilder
194 // purpose : Returns a copy of the frustum using the given frustum builder configuration.
195 // Returned frustum should be re-constructed before being used.
196 //=======================================================================
197 Handle(SelectMgr_BaseIntersector) SelectMgr_TriangularFrustum::CopyWithBuilder (const Handle(SelectMgr_FrustumBuilder)& theBuilder) const
199 Handle(SelectMgr_TriangularFrustum) aRes = new SelectMgr_TriangularFrustum();
200 aRes->mySelTriangle = mySelTriangle;
201 aRes->SetBuilder (theBuilder);
206 //=======================================================================
207 // function : OverlapsBox
208 // purpose : SAT intersection test between defined volume and
209 // given axis-aligned box
210 //=======================================================================
211 Standard_Boolean SelectMgr_TriangularFrustum::OverlapsBox (const SelectMgr_Vec3& theMinPt,
212 const SelectMgr_Vec3& theMaxPt,
213 const SelectMgr_ViewClipRange& /*theClipRange*/,
214 SelectBasics_PickResult& /*thePickResult*/) const
216 return hasBoxOverlap (theMinPt, theMaxPt);
219 // =======================================================================
220 // function : OverlapsBox
221 // purpose : Returns true if selecting volume is overlapped by
222 // axis-aligned bounding box with minimum corner at point
223 // theMinPt and maximum at point theMaxPt
224 // =======================================================================
225 Standard_Boolean SelectMgr_TriangularFrustum::OverlapsBox (const SelectMgr_Vec3& theMinPt,
226 const SelectMgr_Vec3& theMaxPt,
227 Standard_Boolean* /*theInside*/) const
229 return hasBoxOverlap (theMinPt, theMaxPt, NULL);
232 // =======================================================================
233 // function : OverlapsPoint
234 // purpose : Intersection test between defined volume and given point
235 // =======================================================================
236 Standard_Boolean SelectMgr_TriangularFrustum::OverlapsPoint (const gp_Pnt& thePnt,
237 const SelectMgr_ViewClipRange& /*theClipRange*/,
238 SelectBasics_PickResult& /*thePickResult*/) const
240 return hasPointOverlap (thePnt);
243 // =======================================================================
244 // function : OverlapsPolygon
245 // purpose : SAT intersection test between defined volume and given
246 // ordered set of points, representing line segments. The test
247 // may be considered of interior part or boundary line defined
248 // by segments depending on given sensitivity type
249 // =======================================================================
250 Standard_Boolean SelectMgr_TriangularFrustum::OverlapsPolygon (const TColgp_Array1OfPnt& theArrayOfPnts,
251 Select3D_TypeOfSensitivity theSensType,
252 const SelectMgr_ViewClipRange& /*theClipRange*/,
253 SelectBasics_PickResult& /*thePickResult*/) const
255 if (theSensType == Select3D_TOS_BOUNDARY)
257 const Standard_Integer aLower = theArrayOfPnts.Lower();
258 const Standard_Integer anUpper = theArrayOfPnts.Upper();
259 for (Standard_Integer aPtIdx = aLower; aPtIdx <= anUpper; ++aPtIdx)
261 const gp_Pnt& aStartPt = theArrayOfPnts.Value (aPtIdx);
262 const gp_Pnt& aEndPt = theArrayOfPnts.Value (aPtIdx == anUpper ? aLower : (aPtIdx + 1));
263 if (!hasSegmentOverlap (aStartPt, aEndPt))
265 return Standard_False;
269 else if (theSensType == Select3D_TOS_INTERIOR)
271 gp_Vec aNorm (gp_XYZ (RealLast(), RealLast(), RealLast()));
272 return hasPolygonOverlap (theArrayOfPnts, aNorm);
275 return Standard_False;
278 // =======================================================================
279 // function : OverlapsSegment
280 // purpose : Checks if line segment overlaps selecting frustum
281 // =======================================================================
282 Standard_Boolean SelectMgr_TriangularFrustum::OverlapsSegment (const gp_Pnt& thePnt1,
283 const gp_Pnt& thePnt2,
284 const SelectMgr_ViewClipRange& /*theClipRange*/,
285 SelectBasics_PickResult& /*thePickResult*/) const
287 return hasSegmentOverlap (thePnt1, thePnt2);
290 // =======================================================================
291 // function : OverlapsTriangle
292 // purpose : SAT intersection test between defined volume and given
293 // triangle. The test may be considered of interior part or
294 // boundary line defined by triangle vertices depending on
295 // given sensitivity type
296 // =======================================================================
297 Standard_Boolean SelectMgr_TriangularFrustum::OverlapsTriangle (const gp_Pnt& thePnt1,
298 const gp_Pnt& thePnt2,
299 const gp_Pnt& thePnt3,
300 Select3D_TypeOfSensitivity theSensType,
301 const SelectMgr_ViewClipRange& theClipRange,
302 SelectBasics_PickResult& thePickResult) const
304 if (theSensType == Select3D_TOS_BOUNDARY)
306 const gp_Pnt aPntsArrayBuf[3] = { thePnt1, thePnt2, thePnt3 };
307 const TColgp_Array1OfPnt aPntsArray (aPntsArrayBuf[0], 1, 3);
308 return OverlapsPolygon (aPntsArray, Select3D_TOS_BOUNDARY, theClipRange, thePickResult);
310 else if (theSensType == Select3D_TOS_INTERIOR)
312 gp_Vec aNorm (gp_XYZ (RealLast(), RealLast(), RealLast()));
313 return hasTriangleOverlap (thePnt1, thePnt2, thePnt3, aNorm);
316 return Standard_True;
319 //=======================================================================
320 // function : OverlapsSphere
322 //=======================================================================
323 Standard_Boolean SelectMgr_TriangularFrustum::OverlapsSphere (const gp_Pnt& theCenter,
324 const Standard_Real theRadius,
325 Standard_Boolean* theInside) const
328 return hasBoxOverlap (SelectMgr_Vec3 (theCenter.X() - theRadius, theCenter.Y() - theRadius, theCenter.Z() - theRadius),
329 SelectMgr_Vec3 (theCenter.X() + theRadius, theCenter.Y() + theRadius, theCenter.Z() + theRadius), NULL);
332 //=======================================================================
333 // function : OverlapsSphere
335 //=======================================================================
336 Standard_Boolean SelectMgr_TriangularFrustum::OverlapsSphere (const gp_Pnt& theCenter,
337 const Standard_Real theRadius,
338 const SelectMgr_ViewClipRange& theClipRange,
339 SelectBasics_PickResult& thePickResult) const
342 (void )thePickResult;
343 return hasSphereOverlap (theCenter, theRadius);
346 //=======================================================================
347 // function : OverlapsCylinder
349 //=======================================================================
350 Standard_Boolean SelectMgr_TriangularFrustum::OverlapsCylinder (const Standard_Real theBottomRad,
351 const Standard_Real theTopRad,
352 const Standard_Real theHeight,
353 const gp_Trsf& theTrsf,
354 const Standard_Boolean theIsHollow,
355 const SelectMgr_ViewClipRange& theClipRange,
356 SelectBasics_PickResult& thePickResult) const
360 return hasCylinderOverlap (theBottomRad, theTopRad, theHeight, theTrsf, theIsHollow);
363 //=======================================================================
364 // function : OverlapsCylinder
366 //=======================================================================
367 Standard_Boolean SelectMgr_TriangularFrustum::OverlapsCylinder (const Standard_Real theBottomRad,
368 const Standard_Real theTopRad,
369 const Standard_Real theHeight,
370 const gp_Trsf& theTrsf,
371 const Standard_Boolean theIsHollow,
372 Standard_Boolean* theInside) const
375 return hasCylinderOverlap (theBottomRad, theTopRad, theHeight, theTrsf, theIsHollow);
378 //=======================================================================
379 // function : OverlapsCircle
381 //=======================================================================
382 Standard_Boolean SelectMgr_TriangularFrustum::OverlapsCircle (const Standard_Real theRadius,
383 const gp_Trsf& theTrsf,
384 const Standard_Boolean theIsFilled,
385 const SelectMgr_ViewClipRange& theClipRange,
386 SelectBasics_PickResult& thePickResult) const
390 return hasCircleOverlap (theRadius, theTrsf, theIsFilled);
393 //=======================================================================
394 // function : OverlapsCircle
396 //=======================================================================
397 Standard_Boolean SelectMgr_TriangularFrustum::OverlapsCircle (const Standard_Real theRadius,
398 const gp_Trsf& theTrsf,
399 const Standard_Boolean theIsFilled,
400 Standard_Boolean* theInside) const
403 return hasCircleOverlap (theRadius, theTrsf, theIsFilled);
406 // =======================================================================
408 // purpose : Nullifies the handle for corresponding builder instance to prevent
410 // =======================================================================
411 void SelectMgr_TriangularFrustum::Clear()
416 // =======================================================================
417 // function : GetPlanes
419 // =======================================================================
420 void SelectMgr_TriangularFrustum::GetPlanes (NCollection_Vector<SelectMgr_Vec4>& thePlaneEquations) const
422 SelectMgr_Vec4 aPlaneEquation;
423 for (Standard_Integer aPlaneIdx = 0; aPlaneIdx < 5; ++aPlaneIdx)
425 const gp_Vec& aNorm = myPlanes[aPlaneIdx];
426 aPlaneEquation.x() = aNorm.X();
427 aPlaneEquation.y() = aNorm.Y();
428 aPlaneEquation.z() = aNorm.Z();
429 aPlaneEquation.w() = - (aNorm.XYZ().Dot (myVertices[aPlaneIdx % 2 == 0 ? aPlaneIdx : 1].XYZ()));
430 thePlaneEquations.Append (aPlaneEquation);
434 //=======================================================================
435 //function : DumpJson
437 //=======================================================================
438 void SelectMgr_TriangularFrustum::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
440 OCCT_DUMP_CLASS_BEGIN (theOStream, SelectMgr_TriangularFrustum)
441 OCCT_DUMP_BASE_CLASS (theOStream, theDepth, SelectMgr_Frustum)