0027772: Foundation Classes - define Standard_Boolean using C++ type "bool" instead...
[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 <Aspect_GenId.hxx>
20 #include <Graphic3d_ClipPlane.hxx>
21 #include <Graphic3d_SequenceOfHClipPlane.hxx>
22 #include <NCollection_DataMap.hxx>
23 #include <NCollection_Handle.hxx>
24 #include <Standard_TypeDef.hxx>
25 #include <OpenGl_Matrix.hxx>
26
27 class OpenGl_Context;
28 class OpenGl_Workspace;
29
30 //! This class contains logics related to tracking and modification of clipping plane
31 //! state for particular OpenGl context. It contains information about enabled
32 //! clipping planes and provides method to change clippings in context. The methods
33 //! should be executed within OpenGl context associated with instance of this
34 //! class.
35 class OpenGl_Clipping
36 {
37 public: //! @name general methods
38
39   //! Default constructor.
40   Standard_EXPORT OpenGl_Clipping();
41
42   //! Initialize.
43   //! @param theMaxPlanes [in] number of clipping planes supported by OpenGl context.
44   Standard_EXPORT void Init (const Standard_Integer theMaxPlanes);
45
46 public: //! @name non-modifying getters
47
48   //! Check whether the clipping plane has been added to the current context state.
49   //! @param thePlane [in] the plane to check.
50   //! @return True if plane is set.
51   inline Standard_Boolean Contains (const Handle(Graphic3d_ClipPlane)& thePlane) const
52   {
53     return myPlaneStates.IsBound (thePlane);
54   }
55
56   //! Get clip planes defined for context.
57   //! @return sequence of set clipping planes.
58   inline const Graphic3d_SequenceOfHClipPlane& Planes() const
59   {
60     return myPlanes;
61   }
62
63   //! Check whether the clipping plane has been set and enabled for the current context state.
64   //! @param thePlane [in] the plane to check.
65   //! @return True if plane is enabled.
66   inline Standard_Boolean IsEnabled (const Handle(Graphic3d_ClipPlane)& thePlane) const
67   {
68     return myPlaneStates.Find (thePlane).IsEnabled;
69   }
70
71   //! @return true if there are enabled clipping planes (NOT capping)
72   inline Standard_Boolean IsClippingOn() const
73   {
74     return myNbClipping > 0;
75   }
76
77   //! @return true if there are enabled capping planes
78   inline Standard_Boolean IsCappingOn() const
79   {
80     return myNbCapping > 0;
81   }
82
83   //! @return true if there are enabled clipping or capping planes
84   inline Standard_Boolean IsClippingOrCappingOn() const
85   {
86     return (myNbClipping + myNbCapping) > 0;
87   }
88
89   //! @return number of enabled clipping + capping planes
90   Standard_Integer NbClippingOrCappingOn() const
91   {
92     return myNbClipping + myNbCapping;
93   }
94
95 public: //! @name clipping state modification commands
96
97   //! Add planes to the context clipping at the specified system of coordinates.
98   //! This methods loads appropriate transformation matrix from workspace to
99   //! to transform equation coordinates. The planes become enabled in the context.
100   //! If the number of the passed planes exceeds capabilities of OpenGl, the last planes
101   //! are simply ignored.
102   //!
103   //! Within FFP, method also temporarily resets ModelView matrix before calling glClipPlane().
104   //! Otherwise the method just redirects to addLazy().
105   //!
106   //! @param theGlCtx [in] context to access the matrices
107   //! @param thePlanes [in/out] the list of planes to be added
108   //! The list then provides information on which planes were really added to clipping state.
109   //! This list then can be used to fall back to previous state.
110   Standard_EXPORT void add (const Handle(OpenGl_Context)&   theGlCtx,
111                             Graphic3d_SequenceOfHClipPlane& thePlanes);
112
113   //! Add planes to the context clipping at the specified system of coordinates.
114   //! This method assumes that appropriate matrix is already set in context state.
115   //! If the number of the passed planes exceeds capabilities of OpenGl, the last planes
116   //! are simply ignored.
117   //! @param thePlanes [in/out] the list of planes to be added.
118   //! The list then provides information on which planes were really added to clipping state.
119   //! This list then can be used to fall back to previous state.
120   Standard_EXPORT void addLazy (const Handle(OpenGl_Context)&   theGlCtx,
121                                 Graphic3d_SequenceOfHClipPlane& thePlanes);
122
123   //! Remove the passed set of clipping planes from the context state.
124   //! @param thePlanes [in] the planes to remove from list.
125   Standard_EXPORT void Remove (const Handle(OpenGl_Context)&         theGlCtx,
126                                const Graphic3d_SequenceOfHClipPlane& thePlanes);
127
128   //! Enable or disable clipping plane in the OpenGl context.
129   //! @param thePlane [in] the plane to affect.
130   //! @param theIsEnabled [in] the state of the plane.
131   Standard_EXPORT void SetEnabled (const Handle(OpenGl_Context)&      theGlCtx,
132                                    const Handle(Graphic3d_ClipPlane)& thePlane,
133                                    const Standard_Boolean             theIsEnabled);
134
135 public: //! @name Short-cuts
136
137   //! Add planes to the context clipping at the world system of coordinates.
138   //! If the number of the passed planes exceeds capabilities of OpenGl, the last planes
139   //! are simply ignored.
140   //! @param theGlCtx [in] context to access the matrices
141   //! @param thePlanes [in/out] the list of planes to be added
142   //! The list then provides information on which planes were really added to clipping state.
143   //! This list then can be used to fall back to previous state.
144   inline void AddWorld (const Handle(OpenGl_Context)&   theGlCtx,
145                         Graphic3d_SequenceOfHClipPlane& thePlanes)
146   {
147     add (theGlCtx, thePlanes);
148   }
149
150   //! Remove all of the planes from context state.
151   inline void RemoveAll (const Handle(OpenGl_Context)& theGlCtx)
152   {
153     Remove (theGlCtx, Planes());
154   }
155
156 private:
157
158   struct PlaneProps
159   {
160     // declare default constructor
161     // to allow compilation of template collections
162     PlaneProps() {}
163     PlaneProps (const Standard_Integer theID,
164                 const Standard_Boolean theIsEnabled)
165     {
166       ContextID  = theID;
167       IsEnabled  = theIsEnabled;
168     }
169
170     Standard_Integer ContextID;
171     Standard_Boolean IsEnabled;
172   };
173
174 private:
175
176   typedef NCollection_DataMap<Handle(Graphic3d_ClipPlane), PlaneProps> OpenGl_MapOfPlaneStates;
177   typedef NCollection_Handle<Aspect_GenId> OpenGl_EmptyPlaneIds;
178
179   Graphic3d_SequenceOfHClipPlane myPlanes;        //!< defined clipping planes
180   OpenGl_MapOfPlaneStates        myPlaneStates;   //!< map of clip planes bound for the props
181   OpenGl_EmptyPlaneIds           myEmptyPlaneIds; //!< generator of empty ids
182   Standard_Integer               myNbClipping;    //!< number of enabled clipping-only planes (NOT capping)
183   Standard_Integer               myNbCapping;     //!< number of enabled capping  planes
184
185 private:
186
187   //! Copying allowed only within Handles
188   OpenGl_Clipping            (const OpenGl_Clipping& );
189   OpenGl_Clipping& operator= (const OpenGl_Clipping& );
190 };
191
192 #endif