0024307: TKOpenGl - efficient culling of large number of presentations
[occt.git] / src / OpenGl / OpenGl_BVHTreeSelector.hxx
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
16 #ifndef _OpenGl_BVHTreeSelector_HeaderFile
17 #define _OpenGl_BVHTreeSelector_HeaderFile
18
19 #include <Graphic3d_Camera.hxx>
20
21 #include <OpenGl_Vec.hxx>
22
23 //! BVHTreeSelector class provides a possibility to store parameters of view volume,
24 //! such as its vertices and equations, and contains methods detecting if given AABB overlaps
25 //! view volume.
26 class OpenGl_BVHTreeSelector
27 {
28 public:
29
30   //! Creates an empty selector object with parallel projection type by default.
31   Standard_EXPORT OpenGl_BVHTreeSelector();
32
33   //! Retrieves view volume's planes equations and its vertices from projection and modelview matrices.
34   Standard_EXPORT void SetViewVolume (const Handle(Graphic3d_Camera)& theCamera);
35
36   //! Detects if AABB overlaps view volume using separating axis theorem (SAT).
37   //! @param theMinPt [in] maximum point of AABB.
38   //! @param theMaxPt [in] minimum point of AABB.
39   //! @return Standard_True, if AABB is in viewing area, Standard_False otherwise.
40   Standard_EXPORT Standard_Boolean Intersect (const OpenGl_Vec4& theMinPt,
41                                               const OpenGl_Vec4& theMaxPt) const;
42
43   //! Caches view volume's vertices projections along its normals and AABBs dimensions.
44   //! Must be called at the beginning of each BVH tree traverse loop.
45   Standard_EXPORT void CacheClipPtsProjections();
46
47   //! Returnes the state of projection matrix previously saved in selector.
48   Standard_EXPORT const Standard_Size ProjectionState() { return myProjectionState; }
49
50   //! Returnes the link for changing the state of projection matrix.
51   Standard_EXPORT Standard_Size& ChangeProjectionState() { return myProjectionState; }
52
53   //! Returnes the state of model view matrix previously saved in selector.
54   Standard_EXPORT const Standard_Size ModelViewState() { return myModelViewState; }
55
56   //! Returnes the link for changing the state of model view matrix.
57   Standard_EXPORT Standard_Size& ChangeModelViewState() { return myModelViewState; }
58
59 protected:
60
61   //! Calculates signed distance from plane to point.
62   //! @param theNormal [in] the plane's normal.
63   //! @param thePnt    [in]
64   Standard_EXPORT Standard_ShortReal SignedPlanePointDistance (const OpenGl_Vec4& theNormal,
65                                                                const OpenGl_Vec4& thePnt);
66
67 protected:
68
69   //! Enumerates planes of view volume.
70   enum
71   {
72     Plane_Top,
73     Plane_Bottom,
74     Plane_Left,
75     Plane_Right,
76     Plane_Near,
77     Plane_Far,
78     PlanesNB
79   };
80
81   //! Enumerates vertices of view volume.
82   enum
83   {
84     ClipVert_LeftTopNear,
85     ClipVert_LeftBottomNear,
86     ClipVert_RightTopNear,
87     ClipVert_RightBottomNear,
88     ClipVert_LeftTopFar,
89     ClipVert_LeftBottomFar,
90     ClipVert_RightTopFar,
91     ClipVert_RightBottomFar,
92     ClipVerticesNB
93   };
94
95 protected:
96
97   OpenGl_Vec4        myClipPlanes[PlanesNB];           //!< Plane equations
98   OpenGl_Vec4        myClipVerts[ClipVerticesNB];      //!< Vertices
99
100   // for caching clip points projections onto viewing area normals once per traverse
101   // ORDER: TOP, BOTTOM, LEFT, RIGHT, NEAR, FAR
102   Standard_ShortReal myMaxClipProjectionPts[PlanesNB]; //!< Max view volume's vertices projections onto its normals
103   Standard_ShortReal myMinClipProjectionPts[PlanesNB]; //!< Min view volume's vertices projections onto its normals
104
105   // for caching clip points projections onto AABB normals once per traverse
106   // ORDER: E0, E1, E2
107   Standard_ShortReal myMaxOrthoProjectionPts[3];       //!< Max view volume's vertices projections onto normalized dimensions of AABB
108   Standard_ShortReal myMinOrthoProjectionPts[3];       //!< Min view volume's vertices projections onto normalized dimensions of AABB
109
110   Standard_Boolean   myIsProjectionParallel;
111
112   Standard_Size myProjectionState; //! Caches the state of projection matrix to prevent unnecessary updates.
113   Standard_Size myModelViewState;  //! Caches the state of model view matrix to prevent unnecessary updates.
114
115 };
116
117 #endif // _OpenGl_BVHTreeSelector_HeaderFile