0024321: Use List collection instead of NCollection_Set for collection of Handle...
[occt.git] / src / OpenGl / OpenGl_Clipping.hxx
1 // Created on: 2013-09-05
2 // Created by: Anton POLETAEV
3 // Copyright (c) 2013 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20 #ifndef _OpenGl_Clipping_H__
21 #define _OpenGl_Clipping_H__
22
23 #include <Aspect_GenId.hxx>
24 #include <Graphic3d_ClipPlane.hxx>
25 #include <Graphic3d_SequenceOfHClipPlane.hxx>
26 #include <NCollection_DataMap.hxx>
27 #include <NCollection_Handle.hxx>
28 #include <Standard_TypeDef.hxx>
29 #include <OpenGl_Matrix.hxx>
30 #include <Handle_OpenGl_Workspace.hxx>
31
32 //! This class contains logics related to tracking and modification of clipping plane
33 //! state for particular OpenGl context. It contains information about enabled
34 //! clipping planes and provides method to change clippings in context. The methods
35 //! should be executed within OpenGl context associated with instance of this
36 //! class.
37 class OpenGl_Clipping
38 {
39 public:
40
41   //! Enumerates supported equation coordinate spaces.
42   enum EquationCoords
43   {
44     EquationCoords_View,
45     EquationCoords_World
46   };
47
48 public: //! @name general methods
49
50   //! Default constructor.
51   Standard_EXPORT OpenGl_Clipping();
52
53   //! Initialize.
54   //! @param theMaxPlanes [in] number of clipping planes supported by OpenGl context.
55   Standard_EXPORT void Init (const Standard_Integer theMaxPlanes);
56
57 public: //! @name non-modifying getters
58
59   //! Check whether the clipping plane has been added to the current context state.
60   //! @param thePlane [in] the plane to check.
61   //! @return True if plane is set.
62   inline Standard_Boolean Contains (const Handle(Graphic3d_ClipPlane)& thePlane) const
63   {
64     return myPlaneStates.IsBound (thePlane);
65   }
66
67   //! Get clip planes defined for context.
68   //! @return sequence of set clipping planes.
69   inline const Graphic3d_SequenceOfHClipPlane& Planes() const
70   {
71     return myPlanes;
72   }
73
74   //! @return kind of equation coordinate space used for the clip plane.
75   inline EquationCoords GetEquationSpace (const Handle(Graphic3d_ClipPlane)& thePlane) const
76   {
77     return myPlaneStates.Find (thePlane).CoordSpace;
78   }
79
80   //! Check whether the clipping plane has been set and enabled for the current context state.
81   //! @param thePlane [in] the plane to check.
82   //! @return True if plane is enabled.
83   inline Standard_Boolean IsEnabled (const Handle(Graphic3d_ClipPlane)& thePlane) const
84   {
85     return myPlaneStates.Find (thePlane).IsEnabled;
86   }
87
88 public: //! @name clipping state modification commands
89
90   //! Add planes to the context clipping at the specified system of coordinates.
91   //! This methods loads appropriate transformation matrix from workspace to
92   //! to transform equation coordinates. The planes become enabled in the context.
93   //! If the number of the passed planes exceeds capabilities of OpenGl, the last planes
94   //! are simply ignored.
95   //! @param thePlanes [in/out] the list of planes to be added.
96   //! The list then provides information on which planes were really added to clipping state.
97   //! This list then can be used to fall back to previous state.
98   //! @param theCoordSpace [in] the equation definition space.
99   //! @param theWS [in] the workspace to access the matrices.
100   Standard_EXPORT void Add (Graphic3d_SequenceOfHClipPlane& thePlanes,
101                             const EquationCoords& theCoordSpace,
102                             const Handle(OpenGl_Workspace)& theWS);
103
104   //! Add planes to the context clipping at the specified system of coordinates.
105   //! This method assumes that appropriate matrix is already set in context state.
106   //! If the number of the passed planes exceeds capabilities of OpenGl, the last planes
107   //! are simply ignored.
108   //! @param thePlanes [in/out] the list of planes to be added.
109   //! The list then provides information on which planes were really added to clipping state.
110   //! This list then can be used to fall back to previous state.
111   //! @param theCoordSpace [in] the equation definition space.
112   Standard_EXPORT void Add (Graphic3d_SequenceOfHClipPlane& thePlanes,
113                             const EquationCoords& theCoordSpace);
114
115   //! Remove the passed set of clipping planes from the context state.
116   //! @param thePlanes [in] the planes to remove from list.
117   Standard_EXPORT void Remove (const Graphic3d_SequenceOfHClipPlane& thePlanes);
118
119   //! Enable or disable clipping plane in the OpenGl context.
120   //! @param thePlane [in] the plane to affect.
121   //! @param theIsEnabled [in] the state of the plane.
122   Standard_EXPORT void SetEnabled (const Handle(Graphic3d_ClipPlane)& thePlane,
123                                    const Standard_Boolean theIsEnabled);
124
125 public: //! @name Short-cuts
126
127   //! Add planes to the context clipping at the view system of coordinates.
128   //! If the number of the passed planes exceeds capabilities of OpenGl, the last planes
129   //! are simply ignored.
130   //! @param thePlanes [in/out] the list of planes to be added.
131   //! The list then provides information on which planes were really added to clipping state.
132   //! This list then can be used to fall back to previous state.
133   //! @param theWS [in] the workspace to access the matrices.
134   inline void AddView (Graphic3d_SequenceOfHClipPlane& thePlanes, const Handle(OpenGl_Workspace)& theWS)
135   {
136     Add (thePlanes, EquationCoords_View, theWS);
137   }
138
139   //! Add planes to the context clipping at the view system of coordinates.
140   //! If the number of the passed planes exceeds capabilities of OpenGl, the last planes
141   //! are simply ignored.
142   //! @param thePlanes [in/out] the list of planes to be added.
143   //! The list then provides information on which planes were really added to clipping state.
144   //! This list then can be used to fall back to previous state.
145   inline void AddView (Graphic3d_SequenceOfHClipPlane& thePlanes)
146   {
147     Add (thePlanes, EquationCoords_View);
148   }
149
150   //! Add planes to the context clipping at the world system of coordinates.
151   //! If the number of the passed planes exceeds capabilities of OpenGl, the last planes
152   //! are simply ignored.
153   //! @param thePlanes [in/out] the list of planes to be added.
154   //! The list then provides information on which planes were really added to clipping state.
155   //! This list then can be used to fall back to previous state.
156   //! @param theWS [in] the workspace to access the matrices.
157   inline void AddWorld (Graphic3d_SequenceOfHClipPlane& thePlanes, const Handle(OpenGl_Workspace)& theWS)
158   {
159     Add (thePlanes, EquationCoords_World, theWS);
160   }
161
162   //! Add planes to the context clipping at the world system of coordinates.
163   //! If the number of the passed planes exceeds capabilities of OpenGl, the last planes
164   //! are simply ignored.
165   //! @param thePlanes [in/out] the list of planes to be added.
166   //! The list then provides information on which planes were really added to clipping state.
167   //! This list then can be used to fall back to previous state.
168   inline void AddWorld (Graphic3d_SequenceOfHClipPlane& thePlanes)
169   {
170     Add (thePlanes, EquationCoords_World);
171   }
172
173   //! Remove all of the planes from context state.
174   inline void RemoveAll()
175   {
176     Remove (Planes());
177   }
178
179 private:
180
181   struct PlaneProps
182   {
183     // declare default constructor
184     // to allow compilation of template collections
185     PlaneProps() {}
186     PlaneProps (const EquationCoords theCoords,
187                 const Standard_Integer theID,
188                 const Standard_Boolean theIsEnabled)
189     {
190       CoordSpace = theCoords;
191       ContextID  = theID;
192       IsEnabled  = theIsEnabled;
193     }
194
195     EquationCoords   CoordSpace;
196     Standard_Integer ContextID;
197     Standard_Boolean IsEnabled;
198   };
199
200 private:
201
202   typedef NCollection_DataMap<Handle(Graphic3d_ClipPlane), PlaneProps> OpenGl_MapOfPlaneStates;
203   typedef NCollection_Handle<Aspect_GenId> OpenGl_EmptyPlaneIds;
204
205   Graphic3d_SequenceOfHClipPlane myPlanes;        //!< defined clipping planes.
206   OpenGl_MapOfPlaneStates        myPlaneStates;   //!< map of clip planes bound for the props.
207   OpenGl_EmptyPlaneIds           myEmptyPlaneIds; //!< generator of empty ids.
208
209 private:
210
211   //! Copying allowed only within Handles
212   OpenGl_Clipping            (const OpenGl_Clipping& );
213   OpenGl_Clipping& operator= (const OpenGl_Clipping& );
214 };
215
216 #endif