4269bd1b |
1 | // Created on: 2013-09-05 |
2 | // Created by: Anton POLETAEV |
d5f74e42 |
3 | // Copyright (c) 2013-2014 OPEN CASCADE SAS |
4269bd1b |
4 | // |
973c2be1 |
5 | // This file is part of Open CASCADE Technology software library. |
4269bd1b |
6 | // |
d5f74e42 |
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 |
973c2be1 |
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. |
4269bd1b |
12 | // |
973c2be1 |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
4269bd1b |
15 | |
30f0ad28 |
16 | #include <OpenGl_Clipping.hxx> |
79f4f036 |
17 | |
25c35042 |
18 | #include <OpenGl_ClippingIterator.hxx> |
4269bd1b |
19 | |
3202bf1e |
20 | // ======================================================================= |
25c35042 |
21 | // function : OpenGl_Clipping |
3202bf1e |
22 | // purpose : |
23 | // ======================================================================= |
25c35042 |
24 | OpenGl_Clipping::OpenGl_Clipping() |
25 | : myCappedSubPlane (0), |
26 | myNbClipping (0), |
3202bf1e |
27 | myNbCapping (0), |
25c35042 |
28 | myNbChains (0), |
3202bf1e |
29 | myNbDisabled (0) |
4269bd1b |
30 | {} |
31 | |
32 | // ======================================================================= |
33 | // function : Init |
34 | // purpose : |
35 | // ======================================================================= |
25c35042 |
36 | void OpenGl_Clipping::Init() |
4269bd1b |
37 | { |
3202bf1e |
38 | myPlanesGlobal.Nullify(); |
39 | myPlanesLocal.Nullify(); |
40 | |
3b1817a9 |
41 | myNbClipping = 0; |
42 | myNbCapping = 0; |
25c35042 |
43 | myNbChains = 0; |
3202bf1e |
44 | myNbDisabled = 0; |
25c35042 |
45 | myCappedSubPlane = 0; |
46 | myCappedChain.Nullify(); |
4269bd1b |
47 | } |
48 | |
49 | // ======================================================================= |
3202bf1e |
50 | // function : Reset |
4269bd1b |
51 | // purpose : |
52 | // ======================================================================= |
25c35042 |
53 | void OpenGl_Clipping::Reset (const Handle(Graphic3d_SequenceOfHClipPlane)& thePlanes) |
4269bd1b |
54 | { |
3202bf1e |
55 | const Standard_Integer aStartIndex = myPlanesGlobal.IsNull() ? 1 : myPlanesGlobal->Size() + 1; |
25c35042 |
56 | remove (myPlanesLocal, aStartIndex); |
57 | remove (myPlanesGlobal, 1); |
4269bd1b |
58 | |
3202bf1e |
59 | myPlanesGlobal = thePlanes; |
60 | myPlanesLocal.Nullify(); |
4269bd1b |
61 | |
25c35042 |
62 | add (thePlanes, 1); |
3202bf1e |
63 | myNbDisabled = 0; |
25c35042 |
64 | myCappedSubPlane = 0; |
65 | myCappedChain.Nullify(); |
c827ea3a |
66 | |
3202bf1e |
67 | // Method ::add() implicitly extends myDisabledPlanes (NCollection_Vector::SetValue()), |
68 | // however we do not reset myDisabledPlanes and mySkipFilter beforehand to avoid redundant memory re-allocations. |
69 | // So once extended, they will never reduce their size to lower values. |
70 | // This should not be a problem since overall number of clipping planes is expected to be quite small. |
71 | } |
72 | |
73 | // ======================================================================= |
74 | // function : SetLocalPlanes |
75 | // purpose : |
76 | // ======================================================================= |
25c35042 |
77 | void OpenGl_Clipping::SetLocalPlanes (const Handle(Graphic3d_SequenceOfHClipPlane)& thePlanes) |
3202bf1e |
78 | { |
79 | const Standard_Integer aStartIndex = myPlanesGlobal.IsNull() ? 1 : myPlanesGlobal->Size() + 1; |
25c35042 |
80 | remove (myPlanesLocal, aStartIndex); |
3202bf1e |
81 | |
82 | myPlanesLocal = thePlanes; |
83 | |
25c35042 |
84 | add (thePlanes, aStartIndex); |
4269bd1b |
85 | } |
86 | |
87 | // ======================================================================= |
3202bf1e |
88 | // function : add |
4269bd1b |
89 | // purpose : |
90 | // ======================================================================= |
25c35042 |
91 | void OpenGl_Clipping::add (const Handle(Graphic3d_SequenceOfHClipPlane)& thePlanes, |
3202bf1e |
92 | const Standard_Integer theStartIndex) |
4269bd1b |
93 | { |
3202bf1e |
94 | if (thePlanes.IsNull()) |
95 | { |
96 | return; |
97 | } |
98 | |
3202bf1e |
99 | Standard_Integer aPlaneId = theStartIndex; |
100 | for (Graphic3d_SequenceOfHClipPlane::Iterator aPlaneIt (*thePlanes); aPlaneIt.More(); aPlaneIt.Next(), ++aPlaneId) |
4269bd1b |
101 | { |
102 | const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value(); |
3202bf1e |
103 | myDisabledPlanes.SetValue (aPlaneId, Standard_False); // automatically resizes the vector |
104 | if (!aPlane->IsOn()) |
b859a34d |
105 | { |
4269bd1b |
106 | continue; |
b859a34d |
107 | } |
4269bd1b |
108 | |
25c35042 |
109 | const Standard_Integer aNbSubPlanes = aPlane->NbChainNextPlanes(); |
110 | myNbChains += 1; |
3b1817a9 |
111 | if (aPlane->IsCapping()) |
112 | { |
25c35042 |
113 | myNbCapping += aNbSubPlanes; |
3b1817a9 |
114 | } |
115 | else |
116 | { |
25c35042 |
117 | myNbClipping += aNbSubPlanes; |
3b1817a9 |
118 | } |
b859a34d |
119 | } |
4269bd1b |
120 | } |
121 | |
4269bd1b |
122 | // ======================================================================= |
3202bf1e |
123 | // function : remove |
4269bd1b |
124 | // purpose : |
125 | // ======================================================================= |
25c35042 |
126 | void OpenGl_Clipping::remove (const Handle(Graphic3d_SequenceOfHClipPlane)& thePlanes, |
3202bf1e |
127 | const Standard_Integer theStartIndex) |
4269bd1b |
128 | { |
3202bf1e |
129 | if (thePlanes.IsNull()) |
130 | { |
131 | return; |
132 | } |
133 | |
3202bf1e |
134 | Standard_Integer aPlaneIndex = theStartIndex; |
135 | for (Graphic3d_SequenceOfHClipPlane::Iterator aPlaneIt (*thePlanes); aPlaneIt.More(); aPlaneIt.Next(), ++aPlaneIndex) |
b859a34d |
136 | { |
137 | const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value(); |
3202bf1e |
138 | if (!aPlane->IsOn() |
139 | || myDisabledPlanes.Value (aPlaneIndex)) |
b859a34d |
140 | { |
141 | continue; |
142 | } |
143 | |
25c35042 |
144 | const Standard_Integer aNbSubPlanes = aPlane->NbChainNextPlanes(); |
145 | myNbChains -= 1; |
3202bf1e |
146 | if (aPlane->IsCapping()) |
147 | { |
25c35042 |
148 | myNbCapping -= aNbSubPlanes; |
3202bf1e |
149 | } |
150 | else |
151 | { |
25c35042 |
152 | myNbClipping -= aNbSubPlanes; |
3202bf1e |
153 | } |
154 | } |
155 | } |
156 | |
157 | // ======================================================================= |
158 | // function : SetEnabled |
159 | // purpose : |
160 | // ======================================================================= |
25c35042 |
161 | Standard_Boolean OpenGl_Clipping::SetEnabled (const OpenGl_ClippingIterator& thePlane, |
3202bf1e |
162 | const Standard_Boolean theIsEnabled) |
163 | { |
164 | const Standard_Integer aPlaneIndex = thePlane.PlaneIndex(); |
165 | Standard_Boolean& isDisabled = myDisabledPlanes.ChangeValue (aPlaneIndex); |
166 | if (isDisabled == !theIsEnabled) |
167 | { |
168 | return Standard_False; |
169 | } |
170 | |
171 | isDisabled = !theIsEnabled; |
25c35042 |
172 | const Standard_Integer aNbSubPlanes = thePlane.Value()->NbChainNextPlanes(); |
3202bf1e |
173 | if (thePlane.Value()->IsCapping()) |
b859a34d |
174 | { |
25c35042 |
175 | myNbCapping += (theIsEnabled ? aNbSubPlanes : -aNbSubPlanes); |
3202bf1e |
176 | } |
177 | else |
178 | { |
25c35042 |
179 | myNbClipping += (theIsEnabled ? aNbSubPlanes : -aNbSubPlanes); |
b859a34d |
180 | } |
25c35042 |
181 | myNbChains += (theIsEnabled ? 1 : -1); |
182 | myNbDisabled += (theIsEnabled ? -aNbSubPlanes : aNbSubPlanes); |
3202bf1e |
183 | return Standard_True; |
4269bd1b |
184 | } |
185 | |
186 | // ======================================================================= |
3202bf1e |
187 | // function : RestoreDisabled |
4269bd1b |
188 | // purpose : |
189 | // ======================================================================= |
25c35042 |
190 | void OpenGl_Clipping::RestoreDisabled() |
4269bd1b |
191 | { |
3202bf1e |
192 | if (myNbDisabled == 0) |
b859a34d |
193 | { |
4269bd1b |
194 | return; |
b859a34d |
195 | } |
4269bd1b |
196 | |
3202bf1e |
197 | myNbDisabled = 0; |
3202bf1e |
198 | for (OpenGl_ClippingIterator aPlaneIter (*this); aPlaneIter.More(); aPlaneIter.Next()) |
b859a34d |
199 | { |
3202bf1e |
200 | Standard_Boolean& isDisabled = myDisabledPlanes.ChangeValue (aPlaneIter.PlaneIndex()); |
201 | if (!isDisabled) |
202 | { |
203 | continue; |
204 | } |
205 | |
206 | isDisabled = Standard_False; |
3202bf1e |
207 | const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIter.Value(); |
25c35042 |
208 | const Standard_Integer aNbSubPlanes = aPlane->NbChainNextPlanes(); |
209 | myNbChains += 1; |
3202bf1e |
210 | if (aPlane->IsCapping()) |
3b1817a9 |
211 | { |
25c35042 |
212 | myNbCapping += aNbSubPlanes; |
3b1817a9 |
213 | } |
214 | else |
215 | { |
25c35042 |
216 | myNbClipping += aNbSubPlanes; |
3b1817a9 |
217 | } |
b859a34d |
218 | } |
3202bf1e |
219 | } |
220 | |
221 | // ======================================================================= |
222 | // function : DisableGlobal |
223 | // purpose : |
224 | // ======================================================================= |
25c35042 |
225 | void OpenGl_Clipping::DisableGlobal() |
3202bf1e |
226 | { |
227 | for (OpenGl_ClippingIterator aPlaneIter (*this); aPlaneIter.More(); aPlaneIter.Next()) |
b859a34d |
228 | { |
3202bf1e |
229 | if (!aPlaneIter.IsGlobal()) |
79f4f036 |
230 | { |
3202bf1e |
231 | // local planes always follow global ones in iterator |
232 | return; |
79f4f036 |
233 | } |
3202bf1e |
234 | |
25c35042 |
235 | SetEnabled (aPlaneIter, Standard_False); |
3202bf1e |
236 | } |
237 | } |
238 | |
239 | // ======================================================================= |
240 | // function : DisableAllExcept |
241 | // purpose : |
242 | // ======================================================================= |
25c35042 |
243 | void OpenGl_Clipping::DisableAllExcept (const Handle(Graphic3d_ClipPlane)& theChain, |
244 | const Standard_Integer theSubPlaneIndex) |
3202bf1e |
245 | { |
25c35042 |
246 | myCappedChain = theChain; |
247 | myCappedSubPlane = theSubPlaneIndex; |
3202bf1e |
248 | } |
249 | |
250 | // ======================================================================= |
251 | // function : EnableAllExcept |
252 | // purpose : |
253 | // ======================================================================= |
25c35042 |
254 | void OpenGl_Clipping::EnableAllExcept (const Handle(Graphic3d_ClipPlane)& theChain, |
255 | const Standard_Integer theSubPlaneIndex) |
3202bf1e |
256 | { |
25c35042 |
257 | myCappedChain = theChain; |
258 | myCappedSubPlane = -theSubPlaneIndex; |
259 | } |
4269bd1b |
260 | |
25c35042 |
261 | // ======================================================================= |
262 | // function : ResetCappingFilter |
263 | // purpose : |
264 | // ======================================================================= |
265 | void OpenGl_Clipping::ResetCappingFilter() |
266 | { |
267 | myCappedChain.Nullify(); |
268 | myCappedSubPlane = 0; |
4269bd1b |
269 | } |