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> |
825aa485 |
17 | |
18 | // ======================================================================= |
19 | // function : OpenGl_BVHClipPrimitiveTrsfPersSet |
20 | // purpose : |
21 | // ======================================================================= |
f5b72419 |
22 | OpenGl_BVHClipPrimitiveTrsfPersSet::OpenGl_BVHClipPrimitiveTrsfPersSet (const Handle(Select3D_BVHBuilder3d)& theBuilder) |
825aa485 |
23 | : myIsDirty (Standard_False), |
f5b72419 |
24 | myBVH (new BVH_Tree<Standard_Real, 3>()), |
25 | myBuilder (theBuilder) |
825aa485 |
26 | { |
f5b72419 |
27 | // |
825aa485 |
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 | // ======================================================================= |
7c3ef2f7 |
43 | Graphic3d_BndBox3d OpenGl_BVHClipPrimitiveTrsfPersSet::Box (const Standard_Integer theIdx) const |
825aa485 |
44 | { |
45 | return *myStructBoxes (theIdx + 1); |
46 | } |
47 | |
48 | // ======================================================================= |
49 | // function : Center |
50 | // purpose : |
51 | // ======================================================================= |
7c3ef2f7 |
52 | Standard_Real OpenGl_BVHClipPrimitiveTrsfPersSet::Center (const Standard_Integer theIdx, |
53 | const Standard_Integer theAxis) const |
825aa485 |
54 | { |
7c3ef2f7 |
55 | const Graphic3d_BndBox3d& aBndBox = *myStructBoxes (theIdx + 1); |
56 | return (aBndBox.CornerMin()[theAxis] + aBndBox.CornerMax()[theAxis]) * 0.5; |
825aa485 |
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 | |
97f937cc |
69 | myStructs .Swap (aStructIdx1, aStructIdx2); |
825aa485 |
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 | //======================================================================= |
f5b72419 |
134 | const opencascade::handle<BVH_Tree<Standard_Real, 3> >& |
3fe9ce0e |
135 | OpenGl_BVHClipPrimitiveTrsfPersSet::BVH (const Handle(Graphic3d_Camera)& theCamera, |
7c3ef2f7 |
136 | const OpenGl_Mat4d& theProjectionMatrix, |
137 | const OpenGl_Mat4d& theWorldViewMatrix, |
91d96372 |
138 | const Standard_Integer theViewportWidth, |
139 | const Standard_Integer theViewportHeight, |
825aa485 |
140 | const Graphic3d_WorldViewProjState& theWVPState) |
141 | { |
97f937cc |
142 | if (!myIsDirty |
143 | && (myStructBoxesState.IsValid() |
144 | && !myStructBoxesState.IsChanged (theWVPState))) |
825aa485 |
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 | |
7c3ef2f7 |
155 | Handle(HBndBox3d) aBoundingBox = new HBndBox3d(); |
97f937cc |
156 | *aBoundingBox = aStructure->BoundingBox(); |
778cd667 |
157 | if (!aStructure->TransformPersistence().IsNull()) |
158 | { |
159 | aStructure->TransformPersistence()->Apply (theCamera, theProjectionMatrix, theWorldViewMatrix, theViewportWidth, theViewportHeight, *aBoundingBox); |
160 | } |
825aa485 |
161 | |
162 | myStructBoxes.Add (aBoundingBox); |
163 | } |
164 | |
7c3ef2f7 |
165 | myBuilder->Build (this, myBVH.operator->(), BVH_Set<Standard_Real, 3>::Box()); |
825aa485 |
166 | |
167 | myStructBoxesState = theWVPState; |
168 | myStructBoxes.Clear(); |
169 | myIsDirty = Standard_False; |
170 | |
171 | return myBVH; |
172 | } |