b7cd4ba7 |
1 | // Created on: 2013-12-25 |
2 | // Created by: Varvara POSKONINA |
3 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
4 | // |
5 | // This file is part of Open CASCADE Technology software library. |
6 | // |
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. |
12 | // |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
15 | |
14a35e5d |
16 | #include <limits> |
17 | |
b7cd4ba7 |
18 | #include <OpenGl_BVHTreeSelector.hxx> |
19 | #include <OpenGl_BVHClipPrimitiveSet.hxx> |
c04c30b3 |
20 | #include <Graphic3d_GraphicDriver.hxx> |
b7cd4ba7 |
21 | |
b7cd4ba7 |
22 | // ======================================================================= |
23 | // function : OpenGl_BVHTreeSelector |
24 | // purpose : |
25 | // ======================================================================= |
26 | OpenGl_BVHTreeSelector::OpenGl_BVHTreeSelector() |
825aa485 |
27 | : myIsProjectionParallel (Standard_True) |
b7cd4ba7 |
28 | { |
29 | // |
30 | } |
31 | |
32 | // ======================================================================= |
825aa485 |
33 | // function : SetViewVolume |
34 | // purpose : Retrieves view volume's planes equations and its vertices from projection and world-view matrices. |
b7cd4ba7 |
35 | // ======================================================================= |
36 | void OpenGl_BVHTreeSelector::SetViewVolume (const Handle(Graphic3d_Camera)& theCamera) |
37 | { |
97f937cc |
38 | if (!myWorldViewProjState.IsChanged (theCamera->WorldViewProjState())) |
825aa485 |
39 | return; |
825aa485 |
40 | |
b7cd4ba7 |
41 | myIsProjectionParallel = theCamera->IsOrthographic(); |
825aa485 |
42 | |
43 | myProjectionMat = theCamera->ProjectionMatrixF(); |
44 | myWorldViewMat = theCamera->OrientationMatrixF(); |
45 | myWorldViewProjState = theCamera->WorldViewProjState(); |
b7cd4ba7 |
46 | |
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) |
51 | { |
52 | // handle perspective projection |
825aa485 |
53 | aNear = myProjectionMat.GetValue (2, 3) / (- 1.0f + myProjectionMat.GetValue (2, 2)); |
54 | aFar = myProjectionMat.GetValue (2, 3) / ( 1.0f + myProjectionMat.GetValue (2, 2)); |
b7cd4ba7 |
55 | // Near plane |
825aa485 |
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); |
b7cd4ba7 |
60 | // Far plane |
825aa485 |
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); |
b7cd4ba7 |
65 | } |
66 | else |
67 | { |
68 | // handle orthographic projection |
825aa485 |
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); |
b7cd4ba7 |
71 | // Near plane |
825aa485 |
72 | nLeft = ( 1.0f + myProjectionMat.GetValue (0, 3)) / (-myProjectionMat.GetValue (0, 0)); |
b7cd4ba7 |
73 | fLeft = nLeft; |
825aa485 |
74 | nRight = ( 1.0f - myProjectionMat.GetValue (0, 3)) / myProjectionMat.GetValue (0, 0); |
b7cd4ba7 |
75 | fRight = nRight; |
825aa485 |
76 | nTop = ( 1.0f - myProjectionMat.GetValue (1, 3)) / myProjectionMat.GetValue (1, 1); |
b7cd4ba7 |
77 | fTop = nTop; |
825aa485 |
78 | nBottom = (-1.0f - myProjectionMat.GetValue (1, 3)) / myProjectionMat.GetValue (1, 1); |
b7cd4ba7 |
79 | fBottom = nBottom; |
80 | } |
81 | |
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); |
86 | |
825aa485 |
87 | const OpenGl_Mat4 aViewProj = myWorldViewMat * myProjectionMat; |
88 | OpenGl_Mat4 anInvWorldView; |
89 | myWorldViewMat.Inverted(anInvWorldView); |
b7cd4ba7 |
90 | |
825aa485 |
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; |
b7cd4ba7 |
99 | |
100 | // UNNORMALIZED! |
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); |
107 | |
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()), |
112 | 1.0f); |
113 | |
114 | for (Standard_Integer aPlaneIter = 0; aPlaneIter < PlanesNB; ++aPlaneIter) |
115 | { |
116 | OpenGl_Vec4 anEq = myClipPlanes[aPlaneIter]; |
117 | if (SignedPlanePointDistance (anEq, aCenter) > 0) |
118 | { |
119 | anEq *= -1.0f; |
120 | myClipPlanes[aPlaneIter] = anEq; |
121 | } |
122 | } |
123 | } |
124 | |
91d96372 |
125 | // ======================================================================= |
126 | // function : SetViewportSize |
127 | // purpose : |
128 | // ======================================================================= |
129 | void OpenGl_BVHTreeSelector::SetViewportSize (const Standard_Integer theViewportWidth, |
130 | const Standard_Integer theViewportHeight) |
131 | { |
132 | myViewportHeight = theViewportHeight; |
133 | myViewportWidth = theViewportWidth; |
134 | } |
135 | |
b7cd4ba7 |
136 | // ======================================================================= |
137 | // function : SignedPlanePointDistance |
138 | // purpose : |
139 | // ======================================================================= |
140 | Standard_ShortReal OpenGl_BVHTreeSelector::SignedPlanePointDistance (const OpenGl_Vec4& theNormal, |
141 | const OpenGl_Vec4& thePnt) |
142 | { |
143 | const Standard_ShortReal aNormLength = std::sqrt (theNormal.x() * theNormal.x() |
144 | + theNormal.y() * theNormal.y() |
145 | + theNormal.z() * theNormal.z()); |
3c648527 |
146 | |
147 | if (aNormLength < FLT_EPSILON) |
148 | return 0.0f; |
149 | |
b7cd4ba7 |
150 | const Standard_ShortReal anInvNormLength = 1.0f / aNormLength; |
151 | const Standard_ShortReal aD = theNormal.w() * anInvNormLength; |
152 | const Standard_ShortReal anA = theNormal.x() * anInvNormLength; |
153 | const Standard_ShortReal aB = theNormal.y() * anInvNormLength; |
154 | const Standard_ShortReal aC = theNormal.z() * anInvNormLength; |
155 | return aD + (anA * thePnt.x() + aB * thePnt.y() + aC * thePnt.z()); |
156 | } |
157 | |
158 | // ======================================================================= |
159 | // function : CacheClipPtsProjections |
160 | // purpose : Caches view volume's vertices projections along its normals and AABBs dimensions |
161 | // Must be called at the beginning of each BVH tree traverse loop |
162 | // ======================================================================= |
163 | void OpenGl_BVHTreeSelector::CacheClipPtsProjections() |
164 | { |
14a35e5d |
165 | const Standard_Integer anIncFactor = myIsProjectionParallel ? 2 : 1; |
166 | for (Standard_Integer aPlaneIter = 0; aPlaneIter < 5; aPlaneIter += anIncFactor) |
b7cd4ba7 |
167 | { |
168 | const OpenGl_Vec4 aPlane = myClipPlanes[aPlaneIter]; |
14a35e5d |
169 | Standard_ShortReal aMaxProj = -std::numeric_limits<Standard_ShortReal>::max(); |
170 | Standard_ShortReal aMinProj = std::numeric_limits<Standard_ShortReal>::max(); |
b7cd4ba7 |
171 | for (Standard_Integer aCornerIter = 0; aCornerIter < ClipVerticesNB; ++aCornerIter) |
172 | { |
14a35e5d |
173 | Standard_ShortReal aProjection = aPlane.x() * myClipVerts[aCornerIter].x() + |
174 | aPlane.y() * myClipVerts[aCornerIter].y() + |
175 | aPlane.z() * myClipVerts[aCornerIter].z(); |
176 | aMaxProj = Max (aProjection, aMaxProj); |
177 | aMinProj = Min (aProjection, aMinProj); |
b7cd4ba7 |
178 | } |
14a35e5d |
179 | myMaxClipProjectionPts[aPlaneIter] = aMaxProj; |
180 | myMinClipProjectionPts[aPlaneIter] = aMinProj; |
b7cd4ba7 |
181 | } |
182 | |
b7cd4ba7 |
183 | for (Standard_Integer aDim = 0; aDim < 3; ++aDim) |
184 | { |
14a35e5d |
185 | Standard_ShortReal aMaxProj = -std::numeric_limits<Standard_ShortReal>::max(); |
186 | Standard_ShortReal aMinProj = std::numeric_limits<Standard_ShortReal>::max(); |
b7cd4ba7 |
187 | for (Standard_Integer aCornerIter = 0; aCornerIter < ClipVerticesNB; ++aCornerIter) |
188 | { |
14a35e5d |
189 | Standard_ShortReal aProjection = aDim == 0 ? myClipVerts[aCornerIter].x() |
190 | : (aDim == 1 ? myClipVerts[aCornerIter].y() : myClipVerts[aCornerIter].z()); |
191 | aMaxProj = Max (aProjection, aMaxProj); |
192 | aMinProj = Min (aProjection, aMinProj); |
b7cd4ba7 |
193 | } |
14a35e5d |
194 | myMaxOrthoProjectionPts[aDim] = aMaxProj; |
195 | myMinOrthoProjectionPts[aDim] = aMinProj; |
b7cd4ba7 |
196 | } |
197 | } |
198 | |
199 | // ======================================================================= |
200 | // function : Intersect |
201 | // purpose : Detects if AABB overlaps view volume using separating axis theorem (SAT) |
202 | // ======================================================================= |
203 | Standard_Boolean OpenGl_BVHTreeSelector::Intersect (const OpenGl_Vec4& theMinPt, |
204 | const OpenGl_Vec4& theMaxPt) const |
205 | { |
206 | // E1 |
207 | // |_ E0 |
208 | // / |
209 | // E2 |
b7cd4ba7 |
210 | |
211 | // E0 test |
14a35e5d |
212 | if (theMinPt.x() > myMaxOrthoProjectionPts[0] |
213 | || theMaxPt.x() < myMinOrthoProjectionPts[0]) |
b7cd4ba7 |
214 | { |
215 | return Standard_False; |
216 | } |
217 | |
218 | // E1 test |
14a35e5d |
219 | if (theMinPt.y() > myMaxOrthoProjectionPts[1] |
220 | || theMaxPt.y() < myMinOrthoProjectionPts[1]) |
b7cd4ba7 |
221 | { |
222 | return Standard_False; |
223 | } |
224 | |
225 | // E2 test |
14a35e5d |
226 | if (theMinPt.z() > myMaxOrthoProjectionPts[2] |
227 | || theMaxPt.z() < myMinOrthoProjectionPts[2]) |
b7cd4ba7 |
228 | { |
229 | return Standard_False; |
230 | } |
231 | |
14a35e5d |
232 | Standard_ShortReal aBoxProjMax = 0.0f, aBoxProjMin = 0.0f; |
b7cd4ba7 |
233 | const Standard_Integer anIncFactor = myIsProjectionParallel ? 2 : 1; |
234 | for (Standard_Integer aPlaneIter = 0; aPlaneIter < 5; aPlaneIter += anIncFactor) |
235 | { |
236 | OpenGl_Vec4 aPlane = myClipPlanes[aPlaneIter]; |
14a35e5d |
237 | aBoxProjMax = (aPlane.x() > 0.f ? (aPlane.x() * theMaxPt.x()) : aPlane.x() * theMinPt.x()) + |
238 | (aPlane.y() > 0.f ? (aPlane.y() * theMaxPt.y()) : aPlane.y() * theMinPt.y()) + |
239 | (aPlane.z() > 0.f ? (aPlane.z() * theMaxPt.z()) : aPlane.z() * theMinPt.z()); |
240 | if (aBoxProjMax > myMinClipProjectionPts[aPlaneIter] |
241 | && aBoxProjMax < myMaxClipProjectionPts[aPlaneIter]) |
b7cd4ba7 |
242 | { |
243 | continue; |
244 | } |
245 | |
14a35e5d |
246 | aBoxProjMin = (aPlane.x() < 0.f ? aPlane.x() * theMaxPt.x() : aPlane.x() * theMinPt.x()) + |
247 | (aPlane.y() < 0.f ? aPlane.y() * theMaxPt.y() : aPlane.y() * theMinPt.y()) + |
248 | (aPlane.z() < 0.f ? aPlane.z() * theMaxPt.z() : aPlane.z() * theMinPt.z()); |
249 | if (aBoxProjMin > myMaxClipProjectionPts[aPlaneIter] |
250 | || aBoxProjMax < myMinClipProjectionPts[aPlaneIter]) |
b7cd4ba7 |
251 | { |
252 | return Standard_False; |
253 | } |
254 | } |
255 | |
256 | return Standard_True; |
257 | } |