1 // Created on: 2013-12-25
2 // Created by: Varvara POSKONINA
3 // Copyright (c) 1999-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.
18 #include <OpenGl_BVHTreeSelector.hxx>
19 #include <OpenGl_BVHClipPrimitiveSet.hxx>
21 // =======================================================================
22 // function : OpenGl_BVHTreeSelector
24 // =======================================================================
25 OpenGl_BVHTreeSelector::OpenGl_BVHTreeSelector()
26 : myIsProjectionParallel (Standard_True),
27 myProjectionState (0),
33 // =======================================================================
34 // function : SetClipVolume
35 // purpose : Retrieves view volume's planes equations and its vertices from projection and modelview matrices.
36 // =======================================================================
37 void OpenGl_BVHTreeSelector::SetViewVolume (const Handle(Graphic3d_Camera)& theCamera)
39 myIsProjectionParallel = theCamera->IsOrthographic();
40 const OpenGl_Mat4& aProjMat = theCamera->ProjectionMatrixF();
41 const OpenGl_Mat4& aModelMat = theCamera->OrientationMatrixF();
43 Standard_ShortReal nLeft = 0.0f, nRight = 0.0f, nTop = 0.0f, nBottom = 0.0f;
44 Standard_ShortReal fLeft = 0.0f, fRight = 0.0f, fTop = 0.0f, fBottom = 0.0f;
45 Standard_ShortReal aNear = 0.0f, aFar = 0.0f;
46 if (!myIsProjectionParallel)
48 // handle perspective projection
49 aNear = aProjMat.GetValue (2, 3) / (- 1.0f + aProjMat.GetValue (2, 2));
50 aFar = aProjMat.GetValue (2, 3) / ( 1.0f + aProjMat.GetValue (2, 2));
52 nLeft = aNear * (aProjMat.GetValue (0, 2) - 1.0f) / aProjMat.GetValue (0, 0);
53 nRight = aNear * (aProjMat.GetValue (0, 2) + 1.0f) / aProjMat.GetValue (0, 0);
54 nTop = aNear * (aProjMat.GetValue (1, 2) + 1.0f) / aProjMat.GetValue (1, 1);
55 nBottom = aNear * (aProjMat.GetValue (1, 2) - 1.0f) / aProjMat.GetValue (1, 1);
57 fLeft = aFar * (aProjMat.GetValue (0, 2) - 1.0f) / aProjMat.GetValue (0, 0);
58 fRight = aFar * (aProjMat.GetValue (0, 2) + 1.0f) / aProjMat.GetValue (0, 0);
59 fTop = aFar * (aProjMat.GetValue (1, 2) + 1.0f) / aProjMat.GetValue (1, 1);
60 fBottom = aFar * (aProjMat.GetValue (1, 2) - 1.0f) / aProjMat.GetValue (1, 1);
64 // handle orthographic projection
65 aNear = (1.0f / aProjMat.GetValue (2, 2)) * (aProjMat.GetValue (2, 3) + 1.0f);
66 aFar = (1.0f / aProjMat.GetValue (2, 2)) * (aProjMat.GetValue (2, 3) - 1.0f);
68 nLeft = ( 1.0f + aProjMat.GetValue (0, 3)) / (-aProjMat.GetValue (0, 0));
70 nRight = ( 1.0f - aProjMat.GetValue (0, 3)) / aProjMat.GetValue (0, 0);
72 nTop = ( 1.0f - aProjMat.GetValue (1, 3)) / aProjMat.GetValue (1, 1);
74 nBottom = (-1.0f - aProjMat.GetValue (1, 3)) / aProjMat.GetValue (1, 1);
78 OpenGl_Vec4 aLeftTopNear (nLeft, nTop, -aNear, 1.0f), aRightBottomFar (fRight, fBottom, -aFar, 1.0f);
79 OpenGl_Vec4 aLeftBottomNear (nLeft, nBottom, -aNear, 1.0f), aRightTopFar (fRight, fTop, -aFar, 1.0f);
80 OpenGl_Vec4 aRightBottomNear (nRight, nBottom, -aNear, 1.0f), aLeftTopFar (fLeft, fTop, -aFar, 1.0f);
81 OpenGl_Vec4 aRightTopNear (nRight, nTop, -aNear, 1.0f), aLeftBottomFar (fLeft, fBottom, -aFar, 1.0f);
83 const OpenGl_Mat4 aViewProj = aModelMat * aProjMat;
84 OpenGl_Mat4 anInvModelView;
85 aModelMat.Inverted(anInvModelView);
87 myClipVerts[ClipVert_LeftTopNear] = anInvModelView * aLeftTopNear;
88 myClipVerts[ClipVert_RightBottomFar] = anInvModelView * aRightBottomFar;
89 myClipVerts[ClipVert_LeftBottomNear] = anInvModelView * aLeftBottomNear;
90 myClipVerts[ClipVert_RightTopFar] = anInvModelView * aRightTopFar;
91 myClipVerts[ClipVert_RightBottomNear] = anInvModelView * aRightBottomNear;
92 myClipVerts[ClipVert_LeftTopFar] = anInvModelView * aLeftTopFar;
93 myClipVerts[ClipVert_RightTopNear] = anInvModelView * aRightTopNear;
94 myClipVerts[ClipVert_LeftBottomFar] = anInvModelView * aLeftBottomFar;
97 myClipPlanes[Plane_Left] = aViewProj.GetRow (3) + aViewProj.GetRow (0);
98 myClipPlanes[Plane_Right] = aViewProj.GetRow (3) - aViewProj.GetRow (0);
99 myClipPlanes[Plane_Top] = aViewProj.GetRow (3) - aViewProj.GetRow (1);
100 myClipPlanes[Plane_Bottom] = aViewProj.GetRow (3) + aViewProj.GetRow (1);
101 myClipPlanes[Plane_Near] = aViewProj.GetRow (3) + aViewProj.GetRow (2);
102 myClipPlanes[Plane_Far] = aViewProj.GetRow (3) - aViewProj.GetRow (2);
104 gp_Pnt aPtCenter = theCamera->Center();
105 OpenGl_Vec4 aCenter (static_cast<Standard_ShortReal> (aPtCenter.X()),
106 static_cast<Standard_ShortReal> (aPtCenter.Y()),
107 static_cast<Standard_ShortReal> (aPtCenter.Z()),
110 for (Standard_Integer aPlaneIter = 0; aPlaneIter < PlanesNB; ++aPlaneIter)
112 OpenGl_Vec4 anEq = myClipPlanes[aPlaneIter];
113 if (SignedPlanePointDistance (anEq, aCenter) > 0)
116 myClipPlanes[aPlaneIter] = anEq;
121 // =======================================================================
122 // function : SignedPlanePointDistance
124 // =======================================================================
125 Standard_ShortReal OpenGl_BVHTreeSelector::SignedPlanePointDistance (const OpenGl_Vec4& theNormal,
126 const OpenGl_Vec4& thePnt)
128 const Standard_ShortReal aNormLength = std::sqrt (theNormal.x() * theNormal.x()
129 + theNormal.y() * theNormal.y()
130 + theNormal.z() * theNormal.z());
132 if (aNormLength < FLT_EPSILON)
135 const Standard_ShortReal anInvNormLength = 1.0f / aNormLength;
136 const Standard_ShortReal aD = theNormal.w() * anInvNormLength;
137 const Standard_ShortReal anA = theNormal.x() * anInvNormLength;
138 const Standard_ShortReal aB = theNormal.y() * anInvNormLength;
139 const Standard_ShortReal aC = theNormal.z() * anInvNormLength;
140 return aD + (anA * thePnt.x() + aB * thePnt.y() + aC * thePnt.z());
143 // =======================================================================
144 // function : CacheClipPtsProjections
145 // purpose : Caches view volume's vertices projections along its normals and AABBs dimensions
146 // Must be called at the beginning of each BVH tree traverse loop
147 // =======================================================================
148 void OpenGl_BVHTreeSelector::CacheClipPtsProjections()
150 const Standard_Integer anIncFactor = myIsProjectionParallel ? 2 : 1;
151 for (Standard_Integer aPlaneIter = 0; aPlaneIter < 5; aPlaneIter += anIncFactor)
153 const OpenGl_Vec4 aPlane = myClipPlanes[aPlaneIter];
154 Standard_ShortReal aMaxProj = -std::numeric_limits<Standard_ShortReal>::max();
155 Standard_ShortReal aMinProj = std::numeric_limits<Standard_ShortReal>::max();
156 for (Standard_Integer aCornerIter = 0; aCornerIter < ClipVerticesNB; ++aCornerIter)
158 Standard_ShortReal aProjection = aPlane.x() * myClipVerts[aCornerIter].x() +
159 aPlane.y() * myClipVerts[aCornerIter].y() +
160 aPlane.z() * myClipVerts[aCornerIter].z();
161 aMaxProj = Max (aProjection, aMaxProj);
162 aMinProj = Min (aProjection, aMinProj);
164 myMaxClipProjectionPts[aPlaneIter] = aMaxProj;
165 myMinClipProjectionPts[aPlaneIter] = aMinProj;
168 for (Standard_Integer aDim = 0; aDim < 3; ++aDim)
170 Standard_ShortReal aMaxProj = -std::numeric_limits<Standard_ShortReal>::max();
171 Standard_ShortReal aMinProj = std::numeric_limits<Standard_ShortReal>::max();
172 for (Standard_Integer aCornerIter = 0; aCornerIter < ClipVerticesNB; ++aCornerIter)
174 Standard_ShortReal aProjection = aDim == 0 ? myClipVerts[aCornerIter].x()
175 : (aDim == 1 ? myClipVerts[aCornerIter].y() : myClipVerts[aCornerIter].z());
176 aMaxProj = Max (aProjection, aMaxProj);
177 aMinProj = Min (aProjection, aMinProj);
179 myMaxOrthoProjectionPts[aDim] = aMaxProj;
180 myMinOrthoProjectionPts[aDim] = aMinProj;
184 // =======================================================================
185 // function : Intersect
186 // purpose : Detects if AABB overlaps view volume using separating axis theorem (SAT)
187 // =======================================================================
188 Standard_Boolean OpenGl_BVHTreeSelector::Intersect (const OpenGl_Vec4& theMinPt,
189 const OpenGl_Vec4& theMaxPt) const
197 if (theMinPt.x() > myMaxOrthoProjectionPts[0]
198 || theMaxPt.x() < myMinOrthoProjectionPts[0])
200 return Standard_False;
204 if (theMinPt.y() > myMaxOrthoProjectionPts[1]
205 || theMaxPt.y() < myMinOrthoProjectionPts[1])
207 return Standard_False;
211 if (theMinPt.z() > myMaxOrthoProjectionPts[2]
212 || theMaxPt.z() < myMinOrthoProjectionPts[2])
214 return Standard_False;
217 Standard_ShortReal aBoxProjMax = 0.0f, aBoxProjMin = 0.0f;
218 const Standard_Integer anIncFactor = myIsProjectionParallel ? 2 : 1;
219 for (Standard_Integer aPlaneIter = 0; aPlaneIter < 5; aPlaneIter += anIncFactor)
221 OpenGl_Vec4 aPlane = myClipPlanes[aPlaneIter];
222 aBoxProjMax = (aPlane.x() > 0.f ? (aPlane.x() * theMaxPt.x()) : aPlane.x() * theMinPt.x()) +
223 (aPlane.y() > 0.f ? (aPlane.y() * theMaxPt.y()) : aPlane.y() * theMinPt.y()) +
224 (aPlane.z() > 0.f ? (aPlane.z() * theMaxPt.z()) : aPlane.z() * theMinPt.z());
225 if (aBoxProjMax > myMinClipProjectionPts[aPlaneIter]
226 && aBoxProjMax < myMaxClipProjectionPts[aPlaneIter])
231 aBoxProjMin = (aPlane.x() < 0.f ? aPlane.x() * theMaxPt.x() : aPlane.x() * theMinPt.x()) +
232 (aPlane.y() < 0.f ? aPlane.y() * theMaxPt.y() : aPlane.y() * theMinPt.y()) +
233 (aPlane.z() < 0.f ? aPlane.z() * theMaxPt.z() : aPlane.z() * theMinPt.z());
234 if (aBoxProjMin > myMaxClipProjectionPts[aPlaneIter]
235 || aBoxProjMax < myMinClipProjectionPts[aPlaneIter])
237 return Standard_False;
241 return Standard_True;