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