Warnings on vc14 were eliminated
[occt.git] / src / OpenGl / OpenGl_Clipping.hxx
1 // Created on: 2013-09-05
2 // Created by: Anton POLETAEV
3 // Copyright (c) 2013-2014 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 #ifndef _OpenGl_Clipping_H__
17 #define _OpenGl_Clipping_H__
18
19 #include <Graphic3d_SequenceOfHClipPlane.hxx>
20 #include <NCollection_Vector.hxx>
21 #include <Standard_TypeDef.hxx>
22
23 class OpenGl_Context;
24 class OpenGl_ClippingIterator;
25
26 //! This class contains logics related to tracking and modification of clipping plane
27 //! state for particular OpenGl context. It contains information about enabled
28 //! clipping planes and provides method to change clippings in context. The methods
29 //! should be executed within OpenGl context associated with instance of this
30 //! class.
31 class OpenGl_Clipping
32 {
33 public: //! @name general methods
34
35   //! Default constructor.
36   Standard_EXPORT OpenGl_Clipping();
37
38   //! Initialize.
39   //! @param theMaxPlanes [in] number of clipping planes supported by OpenGl context.
40   Standard_EXPORT void Init (const Standard_Integer theMaxPlanes);
41
42   //! Setup list of global (for entire view) clipping planes
43   //! and clears local plane list if it was not released before.
44   Standard_EXPORT void Reset (const Handle(OpenGl_Context)& theGlCtx,
45                               const Handle(Graphic3d_SequenceOfHClipPlane)& thePlanes);
46
47   //! Setup list of local (for current object) clipping planes.
48   Standard_EXPORT void SetLocalPlanes (const Handle(OpenGl_Context)& theGlCtx,
49                                        const Handle(Graphic3d_SequenceOfHClipPlane)& thePlanes);
50
51   //! @return true if there are enabled clipping planes (NOT capping)
52   Standard_Boolean IsClippingOn() const { return myNbClipping > 0; }
53
54   //! @return true if there are enabled capping planes
55   Standard_Boolean IsCappingOn() const { return myNbCapping > 0; }
56
57   //! @return true if there are enabled clipping or capping planes
58   Standard_Boolean IsClippingOrCappingOn() const { return (myNbClipping + myNbCapping) > 0; }
59
60   //! @return number of enabled clipping + capping planes
61   Standard_Integer NbClippingOrCappingOn() const { return myNbClipping + myNbCapping; }
62
63 public: //! @name advanced method for disabling defined planes
64
65   //! Return true if some clipping planes have been temporarily disabled.
66   Standard_Boolean HasDisabled() const { return myNbDisabled > 0; }
67
68   //! Disable plane temporarily.
69   Standard_EXPORT Standard_Boolean SetEnabled (const Handle(OpenGl_Context)&  theGlCtx,
70                                                const OpenGl_ClippingIterator& thePlane,
71                                                const Standard_Boolean         theIsEnabled);
72
73   //! Temporarily disable all planes from the global (view) list, keep only local (object) list.
74   Standard_EXPORT void DisableGlobal (const Handle(OpenGl_Context)& theGlCtx);
75
76   //! Restore all temporarily disabled planes.
77   //! Does NOT affect constantly disabled planes Graphic3d_ClipPlane::IsOn().
78   Standard_EXPORT void RestoreDisabled (const Handle(OpenGl_Context)& theGlCtx);
79
80   //! Temporarily disable all planes except specified one.
81   //! Does not affect already disabled planes.
82   Standard_EXPORT void DisableAllExcept (const Handle(OpenGl_Context)&  theGlCtx,
83                                          const OpenGl_ClippingIterator& thePlane);
84
85   //! Enable back planes disabled by ::DisableAllExcept().
86   //! Keeps only specified plane enabled.
87   Standard_EXPORT void EnableAllExcept (const Handle(OpenGl_Context)&  theGlCtx,
88                                         const OpenGl_ClippingIterator& thePlane);
89
90 protected: //! @name clipping state modification commands
91
92   //! Add planes to the context clipping at the specified system of coordinates.
93   //! This methods loads appropriate transformation matrix from workspace to
94   //! to transform equation coordinates. The planes become enabled in the context.
95   //! If the number of the passed planes exceeds capabilities of OpenGl, the last planes
96   //! are simply ignored.
97   //!
98   //! Within FFP, method also temporarily resets ModelView matrix before calling glClipPlane().
99   //! Otherwise the method just redirects to addLazy().
100   //!
101   //! @param theGlCtx [in] context to access the matrices
102   //! @param thePlanes [in/out] the list of planes to be added
103   //! The list then provides information on which planes were really added to clipping state.
104   //! This list then can be used to fall back to previous state.
105   Standard_EXPORT void add (const Handle(OpenGl_Context)& theGlCtx,
106                             const Handle(Graphic3d_SequenceOfHClipPlane)& thePlanes,
107                             const Standard_Integer theStartIndex);
108
109   //! Remove the passed set of clipping planes from the context state.
110   //! @param thePlanes [in] the planes to remove from list.
111   Standard_EXPORT void remove (const Handle(OpenGl_Context)& theGlCtx,
112                                const Handle(Graphic3d_SequenceOfHClipPlane)& thePlanes,
113                                const Standard_Integer theStartIndex);
114
115 private:
116
117   Handle(Graphic3d_SequenceOfHClipPlane)   myPlanesGlobal;   //!< global clipping planes
118   Handle(Graphic3d_SequenceOfHClipPlane)   myPlanesLocal;    //!< object clipping planes
119   NCollection_Vector<Standard_Boolean>     myDisabledPlanes; //!< ids of disabled planes
120   NCollection_Vector<Standard_Boolean>     mySkipFilter;     //!< ids of planes that were disabled before calling ::DisableAllExcept()
121   Standard_Integer                         myNbClipping;     //!< number of enabled clipping-only planes (NOT capping)
122   Standard_Integer                         myNbCapping;      //!< number of enabled capping  planes
123   Standard_Integer                         myNbDisabled;     //!< number of defined but disabled planes
124
125 private:
126
127   //! Copying allowed only within Handles
128   OpenGl_Clipping            (const OpenGl_Clipping& );
129   OpenGl_Clipping& operator= (const OpenGl_Clipping& );
130
131   friend class OpenGl_ClippingIterator;
132 };
133
134 //! The iterator through clipping planes.
135 class OpenGl_ClippingIterator
136 {
137 public:
138
139   //! Main constructor.
140   Standard_EXPORT OpenGl_ClippingIterator(const OpenGl_Clipping& theClipping);
141
142   //! Return true if iterator points to the valid clipping plane.
143   bool More() const { return myIter1.More() || myIter2.More(); }
144
145   //! Go to the next clipping plane.
146   void Next()
147   {
148     ++myCurrIndex;
149     if (myIter1.More())
150     {
151       myIter1.Next();
152     }
153     else
154     {
155       myIter2.Next();
156     }
157   }
158
159   //! Return true if plane has been temporarily disabled
160   //! either by Graphic3d_ClipPlane->IsOn() property or by temporary filter.
161   bool IsDisabled() const { return myDisabled->Value (myCurrIndex) || !Value()->IsOn(); }
162
163   //! Return the plane at current iterator position.
164   const Handle(Graphic3d_ClipPlane)& Value() const
165   {
166     return myIter1.More()
167          ? myIter1.Value()
168          : myIter2.Value();
169   }
170
171   //! Return true if plane from the global (view) list.
172   bool IsGlobal() const { return myIter1.More(); }
173
174   //! Return the plane index.
175   Standard_Integer PlaneIndex() const { return myCurrIndex; }
176
177 private:
178
179   Graphic3d_SequenceOfHClipPlane::Iterator myIter1;
180   Graphic3d_SequenceOfHClipPlane::Iterator myIter2;
181   const NCollection_Vector<Standard_Boolean>* myDisabled;
182   Standard_Integer myCurrIndex;
183
184 };
185
186 #endif