e351367c43a9cf09cc8013514a9ca8f76162d839
[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_ShortReal, 4>())
26 {
27   myBuilder = new BVH_LinearBuilder<Standard_ShortReal, 4> (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_BndBox4f 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_ShortReal OpenGl_BVHClipPrimitiveTrsfPersSet::Center (const Standard_Integer theIdx,
53                                                                const Standard_Integer theAxis) const
54 {
55   const Graphic3d_BndBox4f& aBndBox = *myStructBoxes (theIdx + 1);
56
57   return (aBndBox.CornerMin()[theAxis] + aBndBox.CornerMax()[theAxis]) * 0.5f;
58 }
59
60 // =======================================================================
61 // function : Swap
62 // purpose  :
63 // =======================================================================
64 void OpenGl_BVHClipPrimitiveTrsfPersSet::Swap (const Standard_Integer theIdx1,
65                                                const Standard_Integer theIdx2)
66 {
67   const Standard_Integer aStructIdx1 = theIdx1 + 1;
68   const Standard_Integer aStructIdx2 = theIdx2 + 1;
69
70   myStructs    .Swap (aStructIdx1, aStructIdx2);
71   myStructBoxes.Swap (aStructIdx1, aStructIdx2);
72 }
73
74 // =======================================================================
75 // function : Add
76 // purpose  :
77 // =======================================================================
78 Standard_Boolean OpenGl_BVHClipPrimitiveTrsfPersSet::Add (const OpenGl_Structure* theStruct)
79 {
80   const Standard_Integer aSize = myStructs.Size();
81
82   if (myStructs.Add (theStruct) > aSize) // new structure?
83   {
84     MarkDirty();
85
86     return Standard_True;
87   }
88
89   return Standard_False;
90 }
91
92 // =======================================================================
93 // function : Remove
94 // purpose  :
95 // =======================================================================
96 Standard_Boolean OpenGl_BVHClipPrimitiveTrsfPersSet::Remove (const OpenGl_Structure* theStruct)
97 {
98   const Standard_Integer anIndex = myStructs.FindIndex (theStruct);
99
100   if (anIndex != 0)
101   {
102     myStructs.Swap (Size(), anIndex);
103     myStructs.RemoveLast();
104     MarkDirty();
105
106     return Standard_True;
107   }
108
109   return Standard_False;
110 }
111
112 // =======================================================================
113 // function : Clear
114 // purpose  :
115 // =======================================================================
116 void OpenGl_BVHClipPrimitiveTrsfPersSet::Clear()
117 {
118   myStructs.Clear();
119   MarkDirty();
120 }
121
122 // =======================================================================
123 // function : GetStructureById
124 // purpose  :
125 // =======================================================================
126 const OpenGl_Structure* OpenGl_BVHClipPrimitiveTrsfPersSet::GetStructureById (Standard_Integer theId)
127 {
128   return myStructs.FindKey (theId + 1);
129 }
130
131 //=======================================================================
132 // function : BVH
133 // purpose  :
134 //=======================================================================
135 const NCollection_Handle<BVH_Tree<Standard_ShortReal, 4> >&
136   OpenGl_BVHClipPrimitiveTrsfPersSet::BVH (const OpenGl_Mat4& theProjectionMatrix,
137                                            const OpenGl_Mat4& 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     HBndBox4f aBoundingBox = new Graphic3d_BndBox4f;
156     *aBoundingBox = aStructure->BoundingBox();
157      aStructure->TransformPersistence.Apply (theProjectionMatrix, theWorldViewMatrix, theViewportWidth, theViewportHeight, *aBoundingBox);
158
159     myStructBoxes.Add (aBoundingBox);
160   }
161
162   myBuilder->Build (this, myBVH.operator->(), BVH_Set<Standard_ShortReal, 4>::Box());
163
164   myStructBoxesState = theWVPState;
165   myStructBoxes.Clear();
166   myIsDirty = Standard_False;
167
168   return myBVH;
169 }