Salome HOME
8e024bdf0607d9a65ff3ac4d8a56c0a50e980f22
[modules/gui.git] / src / GLViewer / GLViewer_CoordSystem.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  Author : OPEN CASCADE
24 // File:      GLViewer_Context.cxx
25 // Created:   November, 2004
26 //
27 #include "GLViewer_CoordSystem.h"
28 #include <math.h>
29
30 /*!
31   Constructor
32   \param aType - type of CS
33   \param X0 - X of origin in reference CS
34   \param Y0 - Y of origin in reference CS
35   \param XUnit - X unit in reference CS
36   \param YUnit - Y unit in reference CS
37   \param Rotation - rotation relative reference CS
38 */
39 GLViewer_CoordSystem::GLViewer_CoordSystem( CSType aType, double X0, double Y0, 
40                                             double XUnit, double YUnit, double Rotation )
41 {
42     setType( aType );
43     setOrigin( X0, Y0 );
44     setUnits( XUnit, YUnit );
45     setRotation( Rotation );
46 }
47
48 /*!
49   \return origin in reference CS
50 */
51 void GLViewer_CoordSystem::getOrigin( double& x, double& y ) const
52 {
53     x = myX0;
54     y = myY0;
55 }
56
57 /*!
58   Sets origin in reference CS
59 */
60 void GLViewer_CoordSystem::setOrigin( double x, double y )
61 {
62     myX0 = x;
63     myY0 = y;
64 }
65
66 /*!
67   \return units
68 */
69 void GLViewer_CoordSystem::getUnits( double& x, double& y ) const
70 {
71     x = myXUnit;
72     y = myYUnit;
73 }
74
75 /*!
76   Sets units
77 */
78 void GLViewer_CoordSystem::setUnits( double x, double y )
79 {
80     if( x>0 )
81         myXUnit = x;
82     else
83         myXUnit = 1.0;
84
85     if( y>0 )
86         myYUnit = y;
87     else
88         myYUnit = 1.0;
89 }
90
91 /*!
92   \return rotation
93 */
94 double GLViewer_CoordSystem::getRotation() const
95 {
96     return myRotation;
97 }
98
99 /*!
100   Sets rotation
101 */
102 void GLViewer_CoordSystem::setRotation( double rotation )
103 {
104     myRotation = rotation;
105 }
106
107 /*!
108   \return type
109 */
110 GLViewer_CoordSystem::CSType GLViewer_CoordSystem::getType() const
111 {
112     return myType;
113 }
114
115 /*!
116   Sets type
117 */
118 void GLViewer_CoordSystem::setType( CSType type )
119 {
120     myType = type;
121 }
122
123 /*!
124   Recalculate co-ordinates to reference co-ordinates
125   \param x, y - co-ordinates
126 */
127 void GLViewer_CoordSystem::toReference( double& x, double& y )
128 {
129     if( myType==Cartesian )
130     {
131         double newx = myX0 + myXUnit*x*cos(myRotation) - myYUnit*y*sin(myRotation),
132                newy = myY0 + myXUnit*x*sin(myRotation) + myYUnit*y*cos(myRotation);
133         x = newx;
134         y = newy;
135     }
136     else if( myType==Polar )
137     {
138         double r = x, phi = y;
139         x = myX0 + myXUnit*r*cos(phi+myRotation);
140         y = myY0 + myXUnit*r*sin(phi+myRotation);
141     }
142 }
143
144 /*!
145   Recalculate co-ordinates from reference co-ordinates
146   \param x, y - co-ordinates
147 */
148 void GLViewer_CoordSystem::fromReference( double& x, double& y )
149 {
150     x = (x - myX0) / myXUnit;
151     y = (y - myY0) / myYUnit;
152
153     if( myType==Cartesian )
154     {
155         double newx =  x*cos(myRotation) + y*sin(myRotation),
156                newy = -x*sin(myRotation) + y*cos(myRotation);
157         x = newx;
158         y = newy;
159     }
160     else if( myType==Polar )
161     {
162         double r = sqrt( x*x+y*y );
163         double phi = 0.0;
164         double eps = 1E-8, pi = 3.14159265;
165
166         if( r>eps )
167         {
168             if( fabs(x)>eps )
169             {
170                 double arg = y/x;
171                 phi = atan(arg);
172                 if( x<0 ) // 2-nd or 4-rd quarter
173                     phi+=pi;
174             }
175             else if( y>0 )
176                 phi = pi/2.0;
177             else
178                 phi = 3*pi/2.0;
179         }
180
181         x = r;
182         y = phi-myRotation;
183     }
184 }
185
186 /*!
187   Recalculate co-ordinates to co-ordinates of other CS
188   \param aSystem - other CS
189   \param x, y - co-ordinates
190 */
191 void GLViewer_CoordSystem::transform( GLViewer_CoordSystem& aSystem, double& x, double& y )
192 {
193     toReference( x, y );
194     aSystem.fromReference( x, y );
195 }
196
197 /*!
198   \return stretching of CS along X and Y axis
199 */
200 void GLViewer_CoordSystem::getStretching( GLViewer_CoordSystem& aSystem, double& theX, double& theY )
201 {
202     theX = myXUnit / aSystem.myXUnit;
203     theY = myYUnit / aSystem.myYUnit;
204 }