0027562: Coding - avoid exporting of inline methods
[occt.git] / src / OpenGl / OpenGl_BVHTreeSelector.cxx
CommitLineData
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// =======================================================================
26OpenGl_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// =======================================================================
36void 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
125// =======================================================================
126// function : SignedPlanePointDistance
127// purpose :
128// =======================================================================
129Standard_ShortReal OpenGl_BVHTreeSelector::SignedPlanePointDistance (const OpenGl_Vec4& theNormal,
130 const OpenGl_Vec4& thePnt)
131{
132 const Standard_ShortReal aNormLength = std::sqrt (theNormal.x() * theNormal.x()
133 + theNormal.y() * theNormal.y()
134 + theNormal.z() * theNormal.z());
3c648527 135
136 if (aNormLength < FLT_EPSILON)
137 return 0.0f;
138
b7cd4ba7 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());
145}
146
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// =======================================================================
152void OpenGl_BVHTreeSelector::CacheClipPtsProjections()
153{
14a35e5d 154 const Standard_Integer anIncFactor = myIsProjectionParallel ? 2 : 1;
155 for (Standard_Integer aPlaneIter = 0; aPlaneIter < 5; aPlaneIter += anIncFactor)
b7cd4ba7 156 {
157 const OpenGl_Vec4 aPlane = myClipPlanes[aPlaneIter];
14a35e5d 158 Standard_ShortReal aMaxProj = -std::numeric_limits<Standard_ShortReal>::max();
159 Standard_ShortReal aMinProj = std::numeric_limits<Standard_ShortReal>::max();
b7cd4ba7 160 for (Standard_Integer aCornerIter = 0; aCornerIter < ClipVerticesNB; ++aCornerIter)
161 {
14a35e5d 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);
b7cd4ba7 167 }
14a35e5d 168 myMaxClipProjectionPts[aPlaneIter] = aMaxProj;
169 myMinClipProjectionPts[aPlaneIter] = aMinProj;
b7cd4ba7 170 }
171
b7cd4ba7 172 for (Standard_Integer aDim = 0; aDim < 3; ++aDim)
173 {
14a35e5d 174 Standard_ShortReal aMaxProj = -std::numeric_limits<Standard_ShortReal>::max();
175 Standard_ShortReal aMinProj = std::numeric_limits<Standard_ShortReal>::max();
b7cd4ba7 176 for (Standard_Integer aCornerIter = 0; aCornerIter < ClipVerticesNB; ++aCornerIter)
177 {
14a35e5d 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);
b7cd4ba7 182 }
14a35e5d 183 myMaxOrthoProjectionPts[aDim] = aMaxProj;
184 myMinOrthoProjectionPts[aDim] = aMinProj;
b7cd4ba7 185 }
186}
187
188// =======================================================================
189// function : Intersect
190// purpose : Detects if AABB overlaps view volume using separating axis theorem (SAT)
191// =======================================================================
192Standard_Boolean OpenGl_BVHTreeSelector::Intersect (const OpenGl_Vec4& theMinPt,
193 const OpenGl_Vec4& theMaxPt) const
194{
195 // E1
196 // |_ E0
197 // /
198 // E2
b7cd4ba7 199
200 // E0 test
14a35e5d 201 if (theMinPt.x() > myMaxOrthoProjectionPts[0]
202 || theMaxPt.x() < myMinOrthoProjectionPts[0])
b7cd4ba7 203 {
204 return Standard_False;
205 }
206
207 // E1 test
14a35e5d 208 if (theMinPt.y() > myMaxOrthoProjectionPts[1]
209 || theMaxPt.y() < myMinOrthoProjectionPts[1])
b7cd4ba7 210 {
211 return Standard_False;
212 }
213
214 // E2 test
14a35e5d 215 if (theMinPt.z() > myMaxOrthoProjectionPts[2]
216 || theMaxPt.z() < myMinOrthoProjectionPts[2])
b7cd4ba7 217 {
218 return Standard_False;
219 }
220
14a35e5d 221 Standard_ShortReal aBoxProjMax = 0.0f, aBoxProjMin = 0.0f;
b7cd4ba7 222 const Standard_Integer anIncFactor = myIsProjectionParallel ? 2 : 1;
223 for (Standard_Integer aPlaneIter = 0; aPlaneIter < 5; aPlaneIter += anIncFactor)
224 {
225 OpenGl_Vec4 aPlane = myClipPlanes[aPlaneIter];
14a35e5d 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])
b7cd4ba7 231 {
232 continue;
233 }
234
14a35e5d 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])
b7cd4ba7 240 {
241 return Standard_False;
242 }
243 }
244
245 return Standard_True;
246}