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();
44 myProjectionMat = theCamera->ProjectionMatrix();
45 myWorldViewMat = theCamera->OrientationMatrix();
46 myWorldViewProjState = theCamera->WorldViewProjState();
48 Standard_Real nLeft = 0.0, nRight = 0.0, nTop = 0.0, nBottom = 0.0;
49 Standard_Real fLeft = 0.0, fRight = 0.0, fTop = 0.0, fBottom = 0.0;
50 Standard_Real aNear = 0.0, aFar = 0.0;
51 if (!myIsProjectionParallel)
53 // handle perspective projection
54 aNear = myProjectionMat.GetValue (2, 3) / (- 1.0 + myProjectionMat.GetValue (2, 2));
55 aFar = myProjectionMat.GetValue (2, 3) / ( 1.0 + myProjectionMat.GetValue (2, 2));
57 nLeft = aNear * (myProjectionMat.GetValue (0, 2) - 1.0) / myProjectionMat.GetValue (0, 0);
58 nRight = aNear * (myProjectionMat.GetValue (0, 2) + 1.0) / myProjectionMat.GetValue (0, 0);
59 nTop = aNear * (myProjectionMat.GetValue (1, 2) + 1.0) / myProjectionMat.GetValue (1, 1);
60 nBottom = aNear * (myProjectionMat.GetValue (1, 2) - 1.0) / myProjectionMat.GetValue (1, 1);
62 fLeft = aFar * (myProjectionMat.GetValue (0, 2) - 1.0) / myProjectionMat.GetValue (0, 0);
63 fRight = aFar * (myProjectionMat.GetValue (0, 2) + 1.0) / myProjectionMat.GetValue (0, 0);
64 fTop = aFar * (myProjectionMat.GetValue (1, 2) + 1.0) / myProjectionMat.GetValue (1, 1);
65 fBottom = aFar * (myProjectionMat.GetValue (1, 2) - 1.0) / myProjectionMat.GetValue (1, 1);
69 // handle orthographic projection
70 aNear = (1.0 / myProjectionMat.GetValue (2, 2)) * (myProjectionMat.GetValue (2, 3) + 1.0);
71 aFar = (1.0 / myProjectionMat.GetValue (2, 2)) * (myProjectionMat.GetValue (2, 3) - 1.0);
73 nLeft = ( 1.0 + myProjectionMat.GetValue (0, 3)) / (-myProjectionMat.GetValue (0, 0));
75 nRight = ( 1.0 - myProjectionMat.GetValue (0, 3)) / myProjectionMat.GetValue (0, 0);
77 nTop = ( 1.0 - myProjectionMat.GetValue (1, 3)) / myProjectionMat.GetValue (1, 1);
79 nBottom = (-1.0 - myProjectionMat.GetValue (1, 3)) / myProjectionMat.GetValue (1, 1);
83 OpenGl_Vec4d aLeftTopNear (nLeft, nTop, -aNear, 1.0), aRightBottomFar (fRight, fBottom, -aFar, 1.0);
84 OpenGl_Vec4d aLeftBottomNear (nLeft, nBottom, -aNear, 1.0), aRightTopFar (fRight, fTop, -aFar, 1.0);
85 OpenGl_Vec4d aRightBottomNear (nRight, nBottom, -aNear, 1.0), aLeftTopFar (fLeft, fTop, -aFar, 1.0);
86 OpenGl_Vec4d aRightTopNear (nRight, nTop, -aNear, 1.0), aLeftBottomFar (fLeft, fBottom, -aFar, 1.0);
88 const OpenGl_Mat4d aViewProj = myWorldViewMat * myProjectionMat;
89 OpenGl_Mat4d anInvWorldView;
90 myWorldViewMat.Inverted (anInvWorldView);
92 myClipVerts[ClipVert_LeftTopNear] = anInvWorldView * aLeftTopNear;
93 myClipVerts[ClipVert_RightBottomFar] = anInvWorldView * aRightBottomFar;
94 myClipVerts[ClipVert_LeftBottomNear] = anInvWorldView * aLeftBottomNear;
95 myClipVerts[ClipVert_RightTopFar] = anInvWorldView * aRightTopFar;
96 myClipVerts[ClipVert_RightBottomNear] = anInvWorldView * aRightBottomNear;
97 myClipVerts[ClipVert_LeftTopFar] = anInvWorldView * aLeftTopFar;
98 myClipVerts[ClipVert_RightTopNear] = anInvWorldView * aRightTopNear;
99 myClipVerts[ClipVert_LeftBottomFar] = anInvWorldView * aLeftBottomFar;
102 myClipPlanes[Plane_Left] = aViewProj.GetRow (3) + aViewProj.GetRow (0);
103 myClipPlanes[Plane_Right] = aViewProj.GetRow (3) - aViewProj.GetRow (0);
104 myClipPlanes[Plane_Top] = aViewProj.GetRow (3) - aViewProj.GetRow (1);
105 myClipPlanes[Plane_Bottom] = aViewProj.GetRow (3) + aViewProj.GetRow (1);
106 myClipPlanes[Plane_Near] = aViewProj.GetRow (3) + aViewProj.GetRow (2);
107 myClipPlanes[Plane_Far] = aViewProj.GetRow (3) - aViewProj.GetRow (2);
109 gp_Pnt aPtCenter = theCamera->Center();
110 OpenGl_Vec4d aCenter (aPtCenter.X(), aPtCenter.Y(), aPtCenter.Z(), 1.0);
112 for (Standard_Integer aPlaneIter = 0; aPlaneIter < PlanesNB; ++aPlaneIter)
114 OpenGl_Vec4d anEq = myClipPlanes[aPlaneIter];
115 if (SignedPlanePointDistance (anEq, aCenter) > 0)
118 myClipPlanes[aPlaneIter] = anEq;
123 // =======================================================================
124 // function : SetViewportSize
126 // =======================================================================
127 void OpenGl_BVHTreeSelector::SetViewportSize (const Standard_Integer theViewportWidth,
128 const Standard_Integer theViewportHeight)
130 myViewportHeight = theViewportHeight;
131 myViewportWidth = theViewportWidth;
134 // =======================================================================
135 // function : SignedPlanePointDistance
137 // =======================================================================
138 Standard_Real OpenGl_BVHTreeSelector::SignedPlanePointDistance (const OpenGl_Vec4d& theNormal,
139 const OpenGl_Vec4d& thePnt)
141 const Standard_Real aNormLength = std::sqrt (theNormal.x() * theNormal.x()
142 + theNormal.y() * theNormal.y()
143 + theNormal.z() * theNormal.z());
145 if (aNormLength < gp::Resolution())
148 const Standard_Real anInvNormLength = 1.0 / aNormLength;
149 const Standard_Real aD = theNormal.w() * anInvNormLength;
150 const Standard_Real anA = theNormal.x() * anInvNormLength;
151 const Standard_Real aB = theNormal.y() * anInvNormLength;
152 const Standard_Real aC = theNormal.z() * anInvNormLength;
153 return aD + (anA * thePnt.x() + aB * thePnt.y() + aC * thePnt.z());
156 // =======================================================================
157 // function : CacheClipPtsProjections
158 // purpose : Caches view volume's vertices projections along its normals and AABBs dimensions
159 // Must be called at the beginning of each BVH tree traverse loop
160 // =======================================================================
161 void OpenGl_BVHTreeSelector::CacheClipPtsProjections()
163 const Standard_Integer anIncFactor = myIsProjectionParallel ? 2 : 1;
164 for (Standard_Integer aPlaneIter = 0; aPlaneIter < 5; aPlaneIter += anIncFactor)
166 const OpenGl_Vec4d aPlane = myClipPlanes[aPlaneIter];
167 Standard_Real aMaxProj = -std::numeric_limits<Standard_Real>::max();
168 Standard_Real aMinProj = std::numeric_limits<Standard_Real>::max();
169 for (Standard_Integer aCornerIter = 0; aCornerIter < ClipVerticesNB; ++aCornerIter)
171 Standard_Real aProjection = aPlane.x() * myClipVerts[aCornerIter].x()
172 + aPlane.y() * myClipVerts[aCornerIter].y()
173 + aPlane.z() * myClipVerts[aCornerIter].z();
174 aMaxProj = Max (aProjection, aMaxProj);
175 aMinProj = Min (aProjection, aMinProj);
177 myMaxClipProjectionPts[aPlaneIter] = aMaxProj;
178 myMinClipProjectionPts[aPlaneIter] = aMinProj;
181 for (Standard_Integer aDim = 0; aDim < 3; ++aDim)
183 Standard_Real aMaxProj = -std::numeric_limits<Standard_Real>::max();
184 Standard_Real aMinProj = std::numeric_limits<Standard_Real>::max();
185 for (Standard_Integer aCornerIter = 0; aCornerIter < ClipVerticesNB; ++aCornerIter)
187 Standard_Real aProjection = aDim == 0
188 ? myClipVerts[aCornerIter].x()
190 ? myClipVerts[aCornerIter].y()
191 : myClipVerts[aCornerIter].z());
192 aMaxProj = Max (aProjection, aMaxProj);
193 aMinProj = Min (aProjection, aMinProj);
195 myMaxOrthoProjectionPts[aDim] = aMaxProj;
196 myMinOrthoProjectionPts[aDim] = aMinProj;
200 // =======================================================================
201 // function : Intersect
202 // purpose : Detects if AABB overlaps view volume using separating axis theorem (SAT)
203 // =======================================================================
204 Standard_Boolean OpenGl_BVHTreeSelector::Intersect (const OpenGl_Vec3d& theMinPt,
205 const OpenGl_Vec3d& theMaxPt) const
213 if (theMinPt.x() > myMaxOrthoProjectionPts[0]
214 || theMaxPt.x() < myMinOrthoProjectionPts[0])
216 return Standard_False;
220 if (theMinPt.y() > myMaxOrthoProjectionPts[1]
221 || theMaxPt.y() < myMinOrthoProjectionPts[1])
223 return Standard_False;
227 if (theMinPt.z() > myMaxOrthoProjectionPts[2]
228 || theMaxPt.z() < myMinOrthoProjectionPts[2])
230 return Standard_False;
233 Standard_Real aBoxProjMax = 0.0, aBoxProjMin = 0.0;
234 const Standard_Integer anIncFactor = myIsProjectionParallel ? 2 : 1;
235 for (Standard_Integer aPlaneIter = 0; aPlaneIter < 5; aPlaneIter += anIncFactor)
237 OpenGl_Vec4d aPlane = myClipPlanes[aPlaneIter];
238 aBoxProjMax = (aPlane.x() > 0.0 ? (aPlane.x() * theMaxPt.x()) : aPlane.x() * theMinPt.x())
239 + (aPlane.y() > 0.0 ? (aPlane.y() * theMaxPt.y()) : aPlane.y() * theMinPt.y())
240 + (aPlane.z() > 0.0 ? (aPlane.z() * theMaxPt.z()) : aPlane.z() * theMinPt.z());
241 if (aBoxProjMax > myMinClipProjectionPts[aPlaneIter]
242 && aBoxProjMax < myMaxClipProjectionPts[aPlaneIter])
247 aBoxProjMin = (aPlane.x() < 0.0 ? aPlane.x() * theMaxPt.x() : aPlane.x() * theMinPt.x())
248 + (aPlane.y() < 0.0 ? aPlane.y() * theMaxPt.y() : aPlane.y() * theMinPt.y())
249 + (aPlane.z() < 0.0 ? aPlane.z() * theMaxPt.z() : aPlane.z() * theMinPt.z());
250 if (aBoxProjMin > myMaxClipProjectionPts[aPlaneIter]
251 || aBoxProjMax < myMinClipProjectionPts[aPlaneIter])
253 return Standard_False;
257 return Standard_True;