0026083: Coding Rules - Poor design of Graphic3d_GraduatedTrihedron
[occt.git] / src / OpenGl / OpenGl_GraduatedTrihedron.hxx
1 // Created on: 2011-09-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2013 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_GraduatedTrihedron_Header
17 #define _OpenGl_GraduatedTrihedron_Header
18
19 #include <Graphic3d_GraduatedTrihedron.hxx>
20 #include <gp_Ax1.hxx>
21 #include <gp_Pnt.hxx>
22 #include <gp_Dir.hxx>
23 #include <NCollection_Array1.hxx>
24 #include <OpenGl_Aspects.hxx>
25 #include <OpenGl_Element.hxx>
26 #include <OpenGl_PrimitiveArray.hxx>
27 #include <OpenGl_Text.hxx>
28
29 class OpenGl_View;
30
31 //! This class allows to render Graduated Trihedron, i.e. trihedron with grid.
32 //! it is based on Graphic3d_GraduatedTrihedron parameters and support its customization
33 //! on construction level only.
34 //! @sa Graphic3d_GraduatedTrihedron
35 class OpenGl_GraduatedTrihedron : public OpenGl_Element
36 {
37 public:
38
39   DEFINE_STANDARD_ALLOC
40
41 public:
42
43   //! Default constructor.
44   Standard_EXPORT OpenGl_GraduatedTrihedron();
45
46   //! Destructor.
47   Standard_EXPORT virtual ~OpenGl_GraduatedTrihedron();
48
49   //! Draw the element.
50   Standard_EXPORT virtual void Render  (const Handle(OpenGl_Workspace)& theWorkspace) const Standard_OVERRIDE;
51
52   //! Release OpenGL resources.
53   Standard_EXPORT virtual void Release (OpenGl_Context* theCtx) Standard_OVERRIDE;
54
55   //! Setup configuration.
56   Standard_EXPORT void SetValues (const Graphic3d_GraduatedTrihedron& theData);
57
58   //! Sets up-to-date values of scene bounding box.
59   //! Can be used in callback mechanism to get up-to-date values.
60   //! @sa Graphic3d_GraduatedTrihedron::CubicAxesCallback
61   Standard_EXPORT void SetMinMax (const OpenGl_Vec3& theMin,
62                                   const OpenGl_Vec3& theMax);
63
64   //! Dumps the content of me into the stream
65   Standard_EXPORT virtual void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const Standard_OVERRIDE;
66
67 private:
68
69   //! Axis of trihedron. It incapsulates geometry and style.
70   class Axis
71   {
72   public:
73
74     OpenGl_Vec3         Direction;
75     Quantity_Color      NameColor;
76     OpenGl_Aspects      LineAspect;
77     mutable OpenGl_Text Label;
78     mutable OpenGl_PrimitiveArray Tickmark;
79     mutable OpenGl_PrimitiveArray Line;
80     mutable OpenGl_PrimitiveArray Arrow;
81
82   public:
83
84     Axis (const Graphic3d_GraduatedTrihedron::AxisAspect& theAspect =
85                 Graphic3d_GraduatedTrihedron::AxisAspect(),
86           const OpenGl_Vec3& theDirection = OpenGl_Vec3 (1.0f, 0.0f, 0.0f));
87
88     ~Axis();
89
90     Axis& operator= (const Axis& theOther);
91
92     void InitArrow (const Handle(OpenGl_Context)& theContext,
93                     const Standard_ShortReal theLength,
94                     const OpenGl_Vec3& theNormal) const;
95
96     void InitTickmark (const Handle(OpenGl_Context)& theContext,
97                        const OpenGl_Vec3& theDir) const;
98
99     void InitLine (const Handle(OpenGl_Context)& theContext,
100                    const OpenGl_Vec3& theDir) const;
101
102     void Release (OpenGl_Context* theCtx);
103
104   };
105
106 private:
107
108   //! Struct for triple of orthonormal vectors
109   //! and origin point, and axes for tickmarks.
110   //! It may be not a right or left coordinate system.
111   struct GridAxes
112   {
113   public:
114     GridAxes()
115     : Origin (0, 0, 0)
116     {
117       Axes[0] = OpenGl_Vec3 (1.0f, 0.0f, 0.0f);
118       Axes[1] = OpenGl_Vec3 (0.0f, 1.0f, 0.0f);
119       Axes[2] = OpenGl_Vec3 (0.0f, 0.0f, 1.0f);
120
121       Ticks[0] = OpenGl_Vec3 (0.0f, 0.0f, 0.0f);
122       Ticks[1] = OpenGl_Vec3 (0.0f, 0.0f, 0.0f);
123       Ticks[2] = OpenGl_Vec3 (0.0f, 0.0f, 0.0f);
124     }
125
126   public: //! @name Main grid directions
127     OpenGl_Vec3 Origin;
128     OpenGl_Vec3 Axes[3];
129
130   public: //! @name Directions for tickmarks
131     OpenGl_Vec3 Ticks[3];
132   };
133
134 private:
135
136   //! Initialize or update GL resources for rendering trihedron.
137   //! @param theContext [in] the GL context.
138   void initGlResources (const Handle(OpenGl_Context)& theContext) const;
139
140   //! Gets normal of the view out of user.
141   //! @param theContext [in] OpenGL Context
142   //! @param theNormal [out] normal of the view out of user
143   //! @return distance corresponding to 1 pixel
144   Standard_ShortReal getNormal (const Handle(OpenGl_Context)& theContext,
145                                 OpenGl_Vec3& theNormal) const;
146
147   //! Gets distance to point (theX, theY, theZ) of bounding box along the normal
148   //! @param theNormal [in] normal of the view out of user
149   //! @param theCenter [in] geometry center of bounding box
150   //! @param theX [in] x of target point
151   //! @param theY [in] y of target point
152   //! @param theZ [in] z of terget point
153   Standard_ShortReal getDistanceToCorner (const OpenGl_Vec3& theNormal,
154                                           const OpenGl_Vec3& theCenter,
155                                           const Standard_ShortReal theX,
156                                           const Standard_ShortReal theY,
157                                           const Standard_ShortReal theZ) const;
158
159   //! Gets axes of grid
160   //! @param theCorners [in] the corners of grid
161   //! @param theGridAxes [out] grid axes, the base of graduated trihedron grid.
162   Standard_ExtCharacter getGridAxes (const Standard_ShortReal theCorners[8],
163                                      GridAxes& theGridAxes) const;
164
165   //! Render line from the transformed primitive array myLine
166   //! @param theWorkspace [in] the OpenGl Workspace
167   //! @param theMat [in] theMat that contains base transformation and is used for applying
168   //!        translation and rotation
169   //! @param thaTx the X for vector of translation
170   //! @param thaTy the Y for vector of translation
171   //! @param thaTz the Z for vector of translation
172   void renderLine (const OpenGl_PrimitiveArray&    theLine,
173                    const Handle(OpenGl_Workspace)& theWorkspace,
174                    const OpenGl_Mat4&              theMat,
175                    const Standard_ShortReal        theXt,
176                    const Standard_ShortReal        theYt,
177                    const Standard_ShortReal        theZt) const;
178
179   //! Render grid lines perpendecular the axis of input index
180   //! @param theWorkspace [in] the OpenGl Workspace
181   //! @param theIndex [in] index of axis
182   //! @param theGridAxes [in] grid axes
183   //! @param theMat [in] theMat that contains base transformation and is used for applying
184   //!        translation and rotation
185   void renderGridPlane (const Handle(OpenGl_Workspace)& theWorkspace,
186                         const Standard_Integer& theIndex,
187                         const GridAxes& theGridAxes,
188                         OpenGl_Mat4& theMat) const;
189
190
191   //! Render the axis of input index
192   //! @param theWorkspace [in] the OpenGl Workspace
193   //! @param theIndex [in] index of axis
194   //! @param theMat [in] theMat that contains base transformation and is used for applying
195   //!        translation and rotation
196   void renderAxis (const Handle(OpenGl_Workspace)& theWorkspace,
197                    const Standard_Integer& theIndex,
198                    const OpenGl_Mat4& theMat) const;
199
200   //! Render grid labels, tickmark lines and labels
201   //! @param theWorkspace [in] the OpenGl Workspace
202   //! @param theMat [in] theMat that contains base transformation and is used for applying
203   //!        translation and rotation
204   //! @param theIndex [in] index of axis
205   //! @param theGridAxes [in] grid axes
206   //! @param theDpix [in] distance corresponding to 1 pixel
207   void renderTickmarkLabels (const Handle(OpenGl_Workspace)& theWorkspace,
208                              const OpenGl_Mat4& theMat,
209                              const Standard_Integer theIndex,
210                              const GridAxes& theGridAxes,
211                              const Standard_ShortReal theDpix) const;
212
213 protected: //! @name Scene bounding box values
214
215   OpenGl_Vec3 myMin;
216   OpenGl_Vec3 myMax;
217
218 protected:
219
220   mutable Axis myAxes[3]; //!< Axes for trihedron
221   mutable Graphic3d_GraduatedTrihedron myData;
222   mutable OpenGl_Aspects   myGridLineAspect; //!< Color grid properties
223
224 protected: //! @name Labels properties
225
226   mutable OpenGl_Text    myLabelValues;
227   mutable OpenGl_Aspects myAspectLabels;
228   mutable OpenGl_Aspects myAspectValues;
229
230 private:
231
232   mutable Standard_Boolean myIsInitialized;
233
234   enum AxisFlags
235   {
236     XOO_XYO = 1 << 1,
237     XOO_XOZ = 1 << 2,
238     OYO_OYZ = 1 << 3,
239     OYO_XYO = 1 << 4,
240     OOZ_XOZ = 1 << 5,
241     OOZ_OYZ = 1 << 6,
242     OYZ_XYZ = 1 << 7,
243     XOZ_XYZ = 1 << 8,
244     XYO_XYZ = 1 << 9
245   };
246 };
247
248 #endif //_OpenGl_GraduatedTrihedron_Header