1 // Created on: 2013-09-05
2 // Created by: Anton POLETAEV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <OpenGl_Clipping.hxx>
18 #include <OpenGl_ClippingIterator.hxx>
20 // =======================================================================
21 // function : OpenGl_Clipping
23 // =======================================================================
24 OpenGl_Clipping::OpenGl_Clipping()
25 : myCappedSubPlane (0),
32 // =======================================================================
35 // =======================================================================
36 void OpenGl_Clipping::Init()
38 myPlanesGlobal.Nullify();
39 myPlanesLocal.Nullify();
46 myCappedChain.Nullify();
49 // =======================================================================
52 // =======================================================================
53 void OpenGl_Clipping::Reset (const Handle(Graphic3d_SequenceOfHClipPlane)& thePlanes)
55 const Standard_Integer aStartIndex = myPlanesGlobal.IsNull() ? 1 : myPlanesGlobal->Size() + 1;
56 remove (myPlanesLocal, aStartIndex);
57 remove (myPlanesGlobal, 1);
59 myPlanesGlobal = thePlanes;
60 myPlanesLocal.Nullify();
65 myCappedChain.Nullify();
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.
73 // =======================================================================
74 // function : SetLocalPlanes
76 // =======================================================================
77 void OpenGl_Clipping::SetLocalPlanes (const Handle(Graphic3d_SequenceOfHClipPlane)& thePlanes)
79 const Standard_Integer aStartIndex = myPlanesGlobal.IsNull() ? 1 : myPlanesGlobal->Size() + 1;
80 remove (myPlanesLocal, aStartIndex);
82 myPlanesLocal = thePlanes;
84 add (thePlanes, aStartIndex);
87 // =======================================================================
90 // =======================================================================
91 void OpenGl_Clipping::add (const Handle(Graphic3d_SequenceOfHClipPlane)& thePlanes,
92 const Standard_Integer theStartIndex)
94 if (thePlanes.IsNull())
99 Standard_Integer aPlaneId = theStartIndex;
100 for (Graphic3d_SequenceOfHClipPlane::Iterator aPlaneIt (*thePlanes); aPlaneIt.More(); aPlaneIt.Next(), ++aPlaneId)
102 const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value();
103 myDisabledPlanes.SetValue (aPlaneId, Standard_False); // automatically resizes the vector
109 const Standard_Integer aNbSubPlanes = aPlane->NbChainNextPlanes();
111 if (aPlane->IsCapping())
113 myNbCapping += aNbSubPlanes;
117 myNbClipping += aNbSubPlanes;
122 // =======================================================================
125 // =======================================================================
126 void OpenGl_Clipping::remove (const Handle(Graphic3d_SequenceOfHClipPlane)& thePlanes,
127 const Standard_Integer theStartIndex)
129 if (thePlanes.IsNull())
134 Standard_Integer aPlaneIndex = theStartIndex;
135 for (Graphic3d_SequenceOfHClipPlane::Iterator aPlaneIt (*thePlanes); aPlaneIt.More(); aPlaneIt.Next(), ++aPlaneIndex)
137 const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value();
139 || myDisabledPlanes.Value (aPlaneIndex))
144 const Standard_Integer aNbSubPlanes = aPlane->NbChainNextPlanes();
146 if (aPlane->IsCapping())
148 myNbCapping -= aNbSubPlanes;
152 myNbClipping -= aNbSubPlanes;
157 // =======================================================================
158 // function : SetEnabled
160 // =======================================================================
161 Standard_Boolean OpenGl_Clipping::SetEnabled (const OpenGl_ClippingIterator& thePlane,
162 const Standard_Boolean theIsEnabled)
164 const Standard_Integer aPlaneIndex = thePlane.PlaneIndex();
165 Standard_Boolean& isDisabled = myDisabledPlanes.ChangeValue (aPlaneIndex);
166 if (isDisabled == !theIsEnabled)
168 return Standard_False;
171 isDisabled = !theIsEnabled;
172 const Standard_Integer aNbSubPlanes = thePlane.Value()->NbChainNextPlanes();
173 if (thePlane.Value()->IsCapping())
175 myNbCapping += (theIsEnabled ? aNbSubPlanes : -aNbSubPlanes);
179 myNbClipping += (theIsEnabled ? aNbSubPlanes : -aNbSubPlanes);
181 myNbChains += (theIsEnabled ? 1 : -1);
182 myNbDisabled += (theIsEnabled ? -aNbSubPlanes : aNbSubPlanes);
183 return Standard_True;
186 // =======================================================================
187 // function : RestoreDisabled
189 // =======================================================================
190 void OpenGl_Clipping::RestoreDisabled()
192 if (myNbDisabled == 0)
198 for (OpenGl_ClippingIterator aPlaneIter (*this); aPlaneIter.More(); aPlaneIter.Next())
200 Standard_Boolean& isDisabled = myDisabledPlanes.ChangeValue (aPlaneIter.PlaneIndex());
206 isDisabled = Standard_False;
207 const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIter.Value();
208 const Standard_Integer aNbSubPlanes = aPlane->NbChainNextPlanes();
210 if (aPlane->IsCapping())
212 myNbCapping += aNbSubPlanes;
216 myNbClipping += aNbSubPlanes;
221 // =======================================================================
222 // function : DisableGlobal
224 // =======================================================================
225 void OpenGl_Clipping::DisableGlobal()
227 for (OpenGl_ClippingIterator aPlaneIter (*this); aPlaneIter.More(); aPlaneIter.Next())
229 if (!aPlaneIter.IsGlobal())
231 // local planes always follow global ones in iterator
235 SetEnabled (aPlaneIter, Standard_False);
239 // =======================================================================
240 // function : DisableAllExcept
242 // =======================================================================
243 void OpenGl_Clipping::DisableAllExcept (const Handle(Graphic3d_ClipPlane)& theChain,
244 const Standard_Integer theSubPlaneIndex)
246 myCappedChain = theChain;
247 myCappedSubPlane = theSubPlaneIndex;
250 // =======================================================================
251 // function : EnableAllExcept
253 // =======================================================================
254 void OpenGl_Clipping::EnableAllExcept (const Handle(Graphic3d_ClipPlane)& theChain,
255 const Standard_Integer theSubPlaneIndex)
257 myCappedChain = theChain;
258 myCappedSubPlane = -theSubPlaneIndex;
261 // =======================================================================
262 // function : ResetCappingFilter
264 // =======================================================================
265 void OpenGl_Clipping::ResetCappingFilter()
267 myCappedChain.Nullify();
268 myCappedSubPlane = 0;