39fb68ad |
1 | // Created on: 2015-06-30 |
825aa485 |
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 | |
97f937cc |
70 | myStructs .Swap (aStructIdx1, aStructIdx2); |
825aa485 |
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 Graphic3d_WorldViewProjState& theWVPState) |
139 | { |
97f937cc |
140 | if (!myIsDirty |
141 | && (myStructBoxesState.IsValid() |
142 | && !myStructBoxesState.IsChanged (theWVPState))) |
825aa485 |
143 | { |
144 | return myBVH; |
145 | } |
146 | |
147 | myStructBoxes.ReSize (myStructs.Size()); |
148 | |
149 | for (Standard_Integer aStructIdx = 1; aStructIdx <= myStructs.Size(); ++aStructIdx) |
150 | { |
151 | const OpenGl_Structure* aStructure = myStructs (aStructIdx); |
152 | |
153 | HBndBox4f aBoundingBox = new Graphic3d_BndBox4f; |
97f937cc |
154 | *aBoundingBox = aStructure->BoundingBox(); |
155 | aStructure->TransformPersistence.Apply (theProjectionMatrix, theWorldViewMatrix, 0, 0, *aBoundingBox); |
825aa485 |
156 | |
157 | myStructBoxes.Add (aBoundingBox); |
158 | } |
159 | |
160 | myBuilder->Build (this, myBVH.operator->(), BVH_Set<Standard_ShortReal, 4>::Box()); |
161 | |
162 | myStructBoxesState = theWVPState; |
163 | myStructBoxes.Clear(); |
164 | myIsDirty = Standard_False; |
165 | |
166 | return myBVH; |
167 | } |