b88979a5a007c956ceb8733b7275a1e3c4df7aad
[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:
38
39   //! Enumerates supported equation coordinate spaces.
40   enum EquationCoords
41   {
42     EquationCoords_View,
43     EquationCoords_World
44   };
45
46 public: //! @name general methods
47
48   //! Default constructor.
49   Standard_EXPORT OpenGl_Clipping();
50
51   //! Initialize.
52   //! @param theMaxPlanes [in] number of clipping planes supported by OpenGl context.
53   Standard_EXPORT void Init (const Standard_Integer theMaxPlanes);
54
55 public: //! @name non-modifying getters
56
57   //! Check whether the clipping plane has been added to the current context state.
58   //! @param thePlane [in] the plane to check.
59   //! @return True if plane is set.
60   inline Standard_Boolean Contains (const Handle(Graphic3d_ClipPlane)& thePlane) const
61   {
62     return myPlaneStates.IsBound (thePlane);
63   }
64
65   //! Get clip planes defined for context.
66   //! @return sequence of set clipping planes.
67   inline const Graphic3d_SequenceOfHClipPlane& Planes() const
68   {
69     return myPlanes;
70   }
71
72   //! @return kind of equation coordinate space used for the clip plane.
73   inline EquationCoords GetEquationSpace (const Handle(Graphic3d_ClipPlane)& thePlane) const
74   {
75     return myPlaneStates.Find (thePlane).CoordSpace;
76   }
77
78   //! Check whether the clipping plane has been set and enabled for the current context state.
79   //! @param thePlane [in] the plane to check.
80   //! @return True if plane is enabled.
81   inline Standard_Boolean IsEnabled (const Handle(Graphic3d_ClipPlane)& thePlane) const
82   {
83     return myPlaneStates.Find (thePlane).IsEnabled;
84   }
85
86   //! @return true if there are enabled clipping planes (NOT capping)
87   inline Standard_Boolean IsClippingOn() const
88   {
89     return myNbClipping > 0;
90   }
91
92   //! @return true if there are enabled capping planes
93   inline Standard_Boolean IsCappingOn() const
94   {
95     return myNbCapping > 0;
96   }
97
98   //! @return true if there are enabled clipping or capping planes
99   inline Standard_Boolean IsClippingOrCappingOn() const
100   {
101     return (myNbClipping + myNbCapping) > 0;
102   }
103
104 public: //! @name clipping state modification commands
105
106   //! Add planes to the context clipping at the specified system of coordinates.
107   //! This methods loads appropriate transformation matrix from workspace to
108   //! to transform equation coordinates. The planes become enabled in the context.
109   //! If the number of the passed planes exceeds capabilities of OpenGl, the last planes
110   //! are simply ignored.
111   //!
112   //! Within FFP, method also temporarily resets ModelView matrix before calling glClipPlane().
113   //! Otherwise the method just redirects to addLazy().
114   //!
115   //! @param theGlCtx [in] context to access the matrices
116   //! @param theCoordSpace [in] the equation definition space
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 add (const Handle(OpenGl_Context)&   theGlCtx,
121                             const EquationCoords&           theCoordSpace,
122                             Graphic3d_SequenceOfHClipPlane& thePlanes);
123
124   //! Add planes to the context clipping at the specified system of coordinates.
125   //! This method assumes that appropriate matrix is already set in context state.
126   //! If the number of the passed planes exceeds capabilities of OpenGl, the last planes
127   //! are simply ignored.
128   //! @param thePlanes [in/out] the list of planes to be added.
129   //! The list then provides information on which planes were really added to clipping state.
130   //! This list then can be used to fall back to previous state.
131   //! @param theCoordSpace [in] the equation definition space.
132   Standard_EXPORT void addLazy (const Handle(OpenGl_Context)&   theGlCtx,
133                                 const EquationCoords&           theCoordSpace,
134                                 Graphic3d_SequenceOfHClipPlane& thePlanes);
135
136   //! Remove the passed set of clipping planes from the context state.
137   //! @param thePlanes [in] the planes to remove from list.
138   Standard_EXPORT void Remove (const Handle(OpenGl_Context)&         theGlCtx,
139                                const Graphic3d_SequenceOfHClipPlane& thePlanes);
140
141   //! Enable or disable clipping plane in the OpenGl context.
142   //! @param thePlane [in] the plane to affect.
143   //! @param theIsEnabled [in] the state of the plane.
144   Standard_EXPORT void SetEnabled (const Handle(OpenGl_Context)&      theGlCtx,
145                                    const Handle(Graphic3d_ClipPlane)& thePlane,
146                                    const Standard_Boolean             theIsEnabled);
147
148 public: //! @name Short-cuts
149
150   //! Add planes to the context clipping at the view system of coordinates.
151   //! If the number of the passed planes exceeds capabilities of OpenGl, the last planes
152   //! are simply ignored.
153   //! @param theGlCtx [in] context to access the matrices
154   //! @param thePlanes [in/out] the list of planes to be added
155   //! The list then provides information on which planes were really added to clipping state.
156   //! This list then can be used to fall back to previous state.
157   inline void AddView (const Handle(OpenGl_Context)&   theGlCtx,
158                        Graphic3d_SequenceOfHClipPlane& thePlanes)
159   {
160     add (theGlCtx, EquationCoords_View, thePlanes);
161   }
162
163   //! Add planes to the context clipping at the world system of coordinates.
164   //! If the number of the passed planes exceeds capabilities of OpenGl, the last planes
165   //! are simply ignored.
166   //! @param theGlCtx [in] context to access the matrices
167   //! @param thePlanes [in/out] the list of planes to be added
168   //! The list then provides information on which planes were really added to clipping state.
169   //! This list then can be used to fall back to previous state.
170   inline void AddWorld (const Handle(OpenGl_Context)&   theGlCtx,
171                         Graphic3d_SequenceOfHClipPlane& thePlanes)
172   {
173     add (theGlCtx, EquationCoords_World, thePlanes);
174   }
175
176   //! Remove all of the planes from context state.
177   inline void RemoveAll (const Handle(OpenGl_Context)& theGlCtx)
178   {
179     Remove (theGlCtx, Planes());
180   }
181
182 private:
183
184   struct PlaneProps
185   {
186     // declare default constructor
187     // to allow compilation of template collections
188     PlaneProps() {}
189     PlaneProps (const EquationCoords theCoords,
190                 const Standard_Integer theID,
191                 const Standard_Boolean theIsEnabled)
192     {
193       CoordSpace = theCoords;
194       ContextID  = theID;
195       IsEnabled  = theIsEnabled;
196     }
197
198     EquationCoords   CoordSpace;
199     Standard_Integer ContextID;
200     Standard_Boolean IsEnabled;
201   };
202
203 private:
204
205   typedef NCollection_DataMap<Handle(Graphic3d_ClipPlane), PlaneProps> OpenGl_MapOfPlaneStates;
206   typedef NCollection_Handle<Aspect_GenId> OpenGl_EmptyPlaneIds;
207
208   Graphic3d_SequenceOfHClipPlane myPlanes;        //!< defined clipping planes
209   OpenGl_MapOfPlaneStates        myPlaneStates;   //!< map of clip planes bound for the props
210   OpenGl_EmptyPlaneIds           myEmptyPlaneIds; //!< generator of empty ids
211   Standard_Boolean               myNbClipping;    //!< number of enabled clipping-only planes (NOT capping)
212   Standard_Boolean               myNbCapping;     //!< number of enabled capping  planes
213
214 private:
215
216   //! Copying allowed only within Handles
217   OpenGl_Clipping            (const OpenGl_Clipping& );
218   OpenGl_Clipping& operator= (const OpenGl_Clipping& );
219 };
220
221 #endif