0028487: Visualization, TKOpenGl - add option for rendering with lower resolution
[occt.git] / src / OpenGl / OpenGl_BVHClipPrimitiveTrsfPersSet.cxx
1 // Created on: 2015-06-30
2 // Created by: Anton POLETAEV
3 // Copyright (c) 2015 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 #include <OpenGl_BVHClipPrimitiveTrsfPersSet.hxx>
17 #include <BVH_LinearBuilder.hxx>
18
19 // =======================================================================
20 // function : OpenGl_BVHClipPrimitiveTrsfPersSet
21 // purpose  :
22 // =======================================================================
23 OpenGl_BVHClipPrimitiveTrsfPersSet::OpenGl_BVHClipPrimitiveTrsfPersSet()
24 : myIsDirty (Standard_False),
25   myBVH (new BVH_Tree<Standard_Real, 3>())
26 {
27   myBuilder = new BVH_LinearBuilder<Standard_Real, 3> (1, 32);
28 }
29
30 // =======================================================================
31 // function : Size
32 // purpose  :
33 // =======================================================================
34 Standard_Integer OpenGl_BVHClipPrimitiveTrsfPersSet::Size() const
35 {
36   return myStructs.Size();
37 }
38
39 // =======================================================================
40 // function : Box
41 // purpose  :
42 // =======================================================================
43 Graphic3d_BndBox3d OpenGl_BVHClipPrimitiveTrsfPersSet::Box (const Standard_Integer theIdx) const
44 {
45   return *myStructBoxes (theIdx + 1);
46 }
47
48 // =======================================================================
49 // function : Center
50 // purpose  :
51 // =======================================================================
52 Standard_Real OpenGl_BVHClipPrimitiveTrsfPersSet::Center (const Standard_Integer theIdx,
53                                                           const Standard_Integer theAxis) const
54 {
55   const Graphic3d_BndBox3d& aBndBox = *myStructBoxes (theIdx + 1);
56   return (aBndBox.CornerMin()[theAxis] + aBndBox.CornerMax()[theAxis]) * 0.5;
57 }
58
59 // =======================================================================
60 // function : Swap
61 // purpose  :
62 // =======================================================================
63 void OpenGl_BVHClipPrimitiveTrsfPersSet::Swap (const Standard_Integer theIdx1,
64                                                const Standard_Integer theIdx2)
65 {
66   const Standard_Integer aStructIdx1 = theIdx1 + 1;
67   const Standard_Integer aStructIdx2 = theIdx2 + 1;
68
69   myStructs    .Swap (aStructIdx1, aStructIdx2);
70   myStructBoxes.Swap (aStructIdx1, aStructIdx2);
71 }
72
73 // =======================================================================
74 // function : Add
75 // purpose  :
76 // =======================================================================
77 Standard_Boolean OpenGl_BVHClipPrimitiveTrsfPersSet::Add (const OpenGl_Structure* theStruct)
78 {
79   const Standard_Integer aSize = myStructs.Size();
80
81   if (myStructs.Add (theStruct) > aSize) // new structure?
82   {
83     MarkDirty();
84
85     return Standard_True;
86   }
87
88   return Standard_False;
89 }
90
91 // =======================================================================
92 // function : Remove
93 // purpose  :
94 // =======================================================================
95 Standard_Boolean OpenGl_BVHClipPrimitiveTrsfPersSet::Remove (const OpenGl_Structure* theStruct)
96 {
97   const Standard_Integer anIndex = myStructs.FindIndex (theStruct);
98
99   if (anIndex != 0)
100   {
101     myStructs.Swap (Size(), anIndex);
102     myStructs.RemoveLast();
103     MarkDirty();
104
105     return Standard_True;
106   }
107
108   return Standard_False;
109 }
110
111 // =======================================================================
112 // function : Clear
113 // purpose  :
114 // =======================================================================
115 void OpenGl_BVHClipPrimitiveTrsfPersSet::Clear()
116 {
117   myStructs.Clear();
118   MarkDirty();
119 }
120
121 // =======================================================================
122 // function : GetStructureById
123 // purpose  :
124 // =======================================================================
125 const OpenGl_Structure* OpenGl_BVHClipPrimitiveTrsfPersSet::GetStructureById (Standard_Integer theId)
126 {
127   return myStructs.FindKey (theId + 1);
128 }
129
130 //=======================================================================
131 // function : BVH
132 // purpose  :
133 //=======================================================================
134 const NCollection_Handle<BVH_Tree<Standard_Real, 3> >&
135   OpenGl_BVHClipPrimitiveTrsfPersSet::BVH (const Handle(Graphic3d_Camera)& theCamera,
136                                            const OpenGl_Mat4d& theProjectionMatrix,
137                                            const OpenGl_Mat4d& theWorldViewMatrix,
138                                            const Standard_Integer theViewportWidth,
139                                            const Standard_Integer theViewportHeight,
140                                            const Graphic3d_WorldViewProjState& theWVPState)
141 {
142   if (!myIsDirty
143     && (myStructBoxesState.IsValid()
144     && !myStructBoxesState.IsChanged (theWVPState)))
145   {
146     return myBVH;
147   }
148
149   myStructBoxes.ReSize (myStructs.Size());
150
151   for (Standard_Integer aStructIdx = 1; aStructIdx <= myStructs.Size(); ++aStructIdx)
152   {
153     const OpenGl_Structure* aStructure = myStructs (aStructIdx);
154
155     Handle(HBndBox3d) aBoundingBox = new HBndBox3d();
156     *aBoundingBox = aStructure->BoundingBox();
157     if (!aStructure->TransformPersistence().IsNull())
158     {
159       aStructure->TransformPersistence()->Apply (theCamera, theProjectionMatrix, theWorldViewMatrix, theViewportWidth, theViewportHeight, *aBoundingBox);
160     }
161
162     myStructBoxes.Add (aBoundingBox);
163   }
164
165   myBuilder->Build (this, myBVH.operator->(), BVH_Set<Standard_Real, 3>::Box());
166
167   myStructBoxesState = theWVPState;
168   myStructBoxes.Clear();
169   myIsDirty = Standard_False;
170
171   return myBVH;
172 }