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>
20 #include <Graphic3d_GraphicDriver.hxx>
22 // =======================================================================
23 // function : OpenGl_BVHTreeSelector
25 // =======================================================================
26 OpenGl_BVHTreeSelector::OpenGl_BVHTreeSelector()
27 : myIsProjectionParallel (Standard_True)
32 // =======================================================================
33 // function : SetViewVolume
34 // purpose : Retrieves view volume's planes equations and its vertices from projection and world-view matrices.
35 // =======================================================================
36 void OpenGl_BVHTreeSelector::SetViewVolume (const Handle(Graphic3d_Camera)& theCamera)
38 if (!myWorldViewProjState.IsChanged (theCamera->WorldViewProjState()))
41 myIsProjectionParallel = theCamera->IsOrthographic();
43 myProjectionMat = theCamera->ProjectionMatrixF();
44 myWorldViewMat = theCamera->OrientationMatrixF();
45 myWorldViewProjState = theCamera->WorldViewProjState();
47 Standard_ShortReal nLeft = 0.0f, nRight = 0.0f, nTop = 0.0f, nBottom = 0.0f;
48 Standard_ShortReal fLeft = 0.0f, fRight = 0.0f, fTop = 0.0f, fBottom = 0.0f;
49 Standard_ShortReal aNear = 0.0f, aFar = 0.0f;
50 if (!myIsProjectionParallel)
52 // handle perspective projection
53 aNear = myProjectionMat.GetValue (2, 3) / (- 1.0f + myProjectionMat.GetValue (2, 2));
54 aFar = myProjectionMat.GetValue (2, 3) / ( 1.0f + myProjectionMat.GetValue (2, 2));
56 nLeft = aNear * (myProjectionMat.GetValue (0, 2) - 1.0f) / myProjectionMat.GetValue (0, 0);
57 nRight = aNear * (myProjectionMat.GetValue (0, 2) + 1.0f) / myProjectionMat.GetValue (0, 0);
58 nTop = aNear * (myProjectionMat.GetValue (1, 2) + 1.0f) / myProjectionMat.GetValue (1, 1);
59 nBottom = aNear * (myProjectionMat.GetValue (1, 2) - 1.0f) / myProjectionMat.GetValue (1, 1);
61 fLeft = aFar * (myProjectionMat.GetValue (0, 2) - 1.0f) / myProjectionMat.GetValue (0, 0);
62 fRight = aFar * (myProjectionMat.GetValue (0, 2) + 1.0f) / myProjectionMat.GetValue (0, 0);
63 fTop = aFar * (myProjectionMat.GetValue (1, 2) + 1.0f) / myProjectionMat.GetValue (1, 1);
64 fBottom = aFar * (myProjectionMat.GetValue (1, 2) - 1.0f) / myProjectionMat.GetValue (1, 1);
68 // handle orthographic projection
69 aNear = (1.0f / myProjectionMat.GetValue (2, 2)) * (myProjectionMat.GetValue (2, 3) + 1.0f);
70 aFar = (1.0f / myProjectionMat.GetValue (2, 2)) * (myProjectionMat.GetValue (2, 3) - 1.0f);
72 nLeft = ( 1.0f + myProjectionMat.GetValue (0, 3)) / (-myProjectionMat.GetValue (0, 0));
74 nRight = ( 1.0f - myProjectionMat.GetValue (0, 3)) / myProjectionMat.GetValue (0, 0);
76 nTop = ( 1.0f - myProjectionMat.GetValue (1, 3)) / myProjectionMat.GetValue (1, 1);
78 nBottom = (-1.0f - myProjectionMat.GetValue (1, 3)) / myProjectionMat.GetValue (1, 1);
82 OpenGl_Vec4 aLeftTopNear (nLeft, nTop, -aNear, 1.0f), aRightBottomFar (fRight, fBottom, -aFar, 1.0f);
83 OpenGl_Vec4 aLeftBottomNear (nLeft, nBottom, -aNear, 1.0f), aRightTopFar (fRight, fTop, -aFar, 1.0f);
84 OpenGl_Vec4 aRightBottomNear (nRight, nBottom, -aNear, 1.0f), aLeftTopFar (fLeft, fTop, -aFar, 1.0f);
85 OpenGl_Vec4 aRightTopNear (nRight, nTop, -aNear, 1.0f), aLeftBottomFar (fLeft, fBottom, -aFar, 1.0f);
87 const OpenGl_Mat4 aViewProj = myWorldViewMat * myProjectionMat;
88 OpenGl_Mat4 anInvWorldView;
89 myWorldViewMat.Inverted(anInvWorldView);
91 myClipVerts[ClipVert_LeftTopNear] = anInvWorldView * aLeftTopNear;
92 myClipVerts[ClipVert_RightBottomFar] = anInvWorldView * aRightBottomFar;
93 myClipVerts[ClipVert_LeftBottomNear] = anInvWorldView * aLeftBottomNear;
94 myClipVerts[ClipVert_RightTopFar] = anInvWorldView * aRightTopFar;
95 myClipVerts[ClipVert_RightBottomNear] = anInvWorldView * aRightBottomNear;
96 myClipVerts[ClipVert_LeftTopFar] = anInvWorldView * aLeftTopFar;
97 myClipVerts[ClipVert_RightTopNear] = anInvWorldView * aRightTopNear;
98 myClipVerts[ClipVert_LeftBottomFar] = anInvWorldView * aLeftBottomFar;
101 myClipPlanes[Plane_Left] = aViewProj.GetRow (3) + aViewProj.GetRow (0);
102 myClipPlanes[Plane_Right] = aViewProj.GetRow (3) - aViewProj.GetRow (0);
103 myClipPlanes[Plane_Top] = aViewProj.GetRow (3) - aViewProj.GetRow (1);
104 myClipPlanes[Plane_Bottom] = aViewProj.GetRow (3) + aViewProj.GetRow (1);
105 myClipPlanes[Plane_Near] = aViewProj.GetRow (3) + aViewProj.GetRow (2);
106 myClipPlanes[Plane_Far] = aViewProj.GetRow (3) - aViewProj.GetRow (2);
108 gp_Pnt aPtCenter = theCamera->Center();
109 OpenGl_Vec4 aCenter (static_cast<Standard_ShortReal> (aPtCenter.X()),
110 static_cast<Standard_ShortReal> (aPtCenter.Y()),
111 static_cast<Standard_ShortReal> (aPtCenter.Z()),
114 for (Standard_Integer aPlaneIter = 0; aPlaneIter < PlanesNB; ++aPlaneIter)
116 OpenGl_Vec4 anEq = myClipPlanes[aPlaneIter];
117 if (SignedPlanePointDistance (anEq, aCenter) > 0)
120 myClipPlanes[aPlaneIter] = anEq;
125 // =======================================================================
126 // function : SignedPlanePointDistance
128 // =======================================================================
129 Standard_ShortReal OpenGl_BVHTreeSelector::SignedPlanePointDistance (const OpenGl_Vec4& theNormal,
130 const OpenGl_Vec4& thePnt)
132 const Standard_ShortReal aNormLength = std::sqrt (theNormal.x() * theNormal.x()
133 + theNormal.y() * theNormal.y()
134 + theNormal.z() * theNormal.z());
136 if (aNormLength < FLT_EPSILON)
139 const Standard_ShortReal anInvNormLength = 1.0f / aNormLength;
140 const Standard_ShortReal aD = theNormal.w() * anInvNormLength;
141 const Standard_ShortReal anA = theNormal.x() * anInvNormLength;
142 const Standard_ShortReal aB = theNormal.y() * anInvNormLength;
143 const Standard_ShortReal aC = theNormal.z() * anInvNormLength;
144 return aD + (anA * thePnt.x() + aB * thePnt.y() + aC * thePnt.z());
147 // =======================================================================
148 // function : CacheClipPtsProjections
149 // purpose : Caches view volume's vertices projections along its normals and AABBs dimensions
150 // Must be called at the beginning of each BVH tree traverse loop
151 // =======================================================================
152 void OpenGl_BVHTreeSelector::CacheClipPtsProjections()
154 const Standard_Integer anIncFactor = myIsProjectionParallel ? 2 : 1;
155 for (Standard_Integer aPlaneIter = 0; aPlaneIter < 5; aPlaneIter += anIncFactor)
157 const OpenGl_Vec4 aPlane = myClipPlanes[aPlaneIter];
158 Standard_ShortReal aMaxProj = -std::numeric_limits<Standard_ShortReal>::max();
159 Standard_ShortReal aMinProj = std::numeric_limits<Standard_ShortReal>::max();
160 for (Standard_Integer aCornerIter = 0; aCornerIter < ClipVerticesNB; ++aCornerIter)
162 Standard_ShortReal aProjection = aPlane.x() * myClipVerts[aCornerIter].x() +
163 aPlane.y() * myClipVerts[aCornerIter].y() +
164 aPlane.z() * myClipVerts[aCornerIter].z();
165 aMaxProj = Max (aProjection, aMaxProj);
166 aMinProj = Min (aProjection, aMinProj);
168 myMaxClipProjectionPts[aPlaneIter] = aMaxProj;
169 myMinClipProjectionPts[aPlaneIter] = aMinProj;
172 for (Standard_Integer aDim = 0; aDim < 3; ++aDim)
174 Standard_ShortReal aMaxProj = -std::numeric_limits<Standard_ShortReal>::max();
175 Standard_ShortReal aMinProj = std::numeric_limits<Standard_ShortReal>::max();
176 for (Standard_Integer aCornerIter = 0; aCornerIter < ClipVerticesNB; ++aCornerIter)
178 Standard_ShortReal aProjection = aDim == 0 ? myClipVerts[aCornerIter].x()
179 : (aDim == 1 ? myClipVerts[aCornerIter].y() : myClipVerts[aCornerIter].z());
180 aMaxProj = Max (aProjection, aMaxProj);
181 aMinProj = Min (aProjection, aMinProj);
183 myMaxOrthoProjectionPts[aDim] = aMaxProj;
184 myMinOrthoProjectionPts[aDim] = aMinProj;
188 // =======================================================================
189 // function : Intersect
190 // purpose : Detects if AABB overlaps view volume using separating axis theorem (SAT)
191 // =======================================================================
192 Standard_Boolean OpenGl_BVHTreeSelector::Intersect (const OpenGl_Vec4& theMinPt,
193 const OpenGl_Vec4& theMaxPt) const
201 if (theMinPt.x() > myMaxOrthoProjectionPts[0]
202 || theMaxPt.x() < myMinOrthoProjectionPts[0])
204 return Standard_False;
208 if (theMinPt.y() > myMaxOrthoProjectionPts[1]
209 || theMaxPt.y() < myMinOrthoProjectionPts[1])
211 return Standard_False;
215 if (theMinPt.z() > myMaxOrthoProjectionPts[2]
216 || theMaxPt.z() < myMinOrthoProjectionPts[2])
218 return Standard_False;
221 Standard_ShortReal aBoxProjMax = 0.0f, aBoxProjMin = 0.0f;
222 const Standard_Integer anIncFactor = myIsProjectionParallel ? 2 : 1;
223 for (Standard_Integer aPlaneIter = 0; aPlaneIter < 5; aPlaneIter += anIncFactor)
225 OpenGl_Vec4 aPlane = myClipPlanes[aPlaneIter];
226 aBoxProjMax = (aPlane.x() > 0.f ? (aPlane.x() * theMaxPt.x()) : aPlane.x() * theMinPt.x()) +
227 (aPlane.y() > 0.f ? (aPlane.y() * theMaxPt.y()) : aPlane.y() * theMinPt.y()) +
228 (aPlane.z() > 0.f ? (aPlane.z() * theMaxPt.z()) : aPlane.z() * theMinPt.z());
229 if (aBoxProjMax > myMinClipProjectionPts[aPlaneIter]
230 && aBoxProjMax < myMaxClipProjectionPts[aPlaneIter])
235 aBoxProjMin = (aPlane.x() < 0.f ? aPlane.x() * theMaxPt.x() : aPlane.x() * theMinPt.x()) +
236 (aPlane.y() < 0.f ? aPlane.y() * theMaxPt.y() : aPlane.y() * theMinPt.y()) +
237 (aPlane.z() < 0.f ? aPlane.z() * theMaxPt.z() : aPlane.z() * theMinPt.z());
238 if (aBoxProjMin > myMaxClipProjectionPts[aPlaneIter]
239 || aBoxProjMax < myMinClipProjectionPts[aPlaneIter])
241 return Standard_False;
245 return Standard_True;