Salome HOME
cc5d11820d35fdef7aef89c52d101aad24db66a8
[modules/gui.git] / src / GLViewer / GLViewer_CoordSystem.cxx
1 //  Copyright (C) 2007-2008  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.
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 //  Author : OPEN CASCADE
23 // File:      GLViewer_Context.cxx
24 // Created:   November, 2004
25 //
26 #include "GLViewer_CoordSystem.h"
27 #include <math.h>
28
29 /*!
30   Constructor
31   \param aType - type of CS
32   \param X0 - X of origin in reference CS
33   \param Y0 - Y of origin in reference CS
34   \param XUnit - X unit in reference CS
35   \param YUnit - Y unit in reference CS
36   \param Rotation - rotation relative reference CS
37 */
38 GLViewer_CoordSystem::GLViewer_CoordSystem( CSType aType, double X0, double Y0, 
39                                             double XUnit, double YUnit, double Rotation )
40 {
41     setType( aType );
42     setOrigin( X0, Y0 );
43     setUnits( XUnit, YUnit );
44     setRotation( Rotation );
45 }
46
47 /*!
48   \return origin in reference CS
49 */
50 void GLViewer_CoordSystem::getOrigin( double& x, double& y ) const
51 {
52     x = myX0;
53     y = myY0;
54 }
55
56 /*!
57   Sets origin in reference CS
58 */
59 void GLViewer_CoordSystem::setOrigin( double x, double y )
60 {
61     myX0 = x;
62     myY0 = y;
63 }
64
65 /*!
66   \return units
67 */
68 void GLViewer_CoordSystem::getUnits( double& x, double& y ) const
69 {
70     x = myXUnit;
71     y = myYUnit;
72 }
73
74 /*!
75   Sets units
76 */
77 void GLViewer_CoordSystem::setUnits( double x, double y )
78 {
79     if( x>0 )
80         myXUnit = x;
81     else
82         myXUnit = 1.0;
83
84     if( y>0 )
85         myYUnit = y;
86     else
87         myYUnit = 1.0;
88 }
89
90 /*!
91   \return rotation
92 */
93 double GLViewer_CoordSystem::getRotation() const
94 {
95     return myRotation;
96 }
97
98 /*!
99   Sets rotation
100 */
101 void GLViewer_CoordSystem::setRotation( double rotation )
102 {
103     myRotation = rotation;
104 }
105
106 /*!
107   \return type
108 */
109 GLViewer_CoordSystem::CSType GLViewer_CoordSystem::getType() const
110 {
111     return myType;
112 }
113
114 /*!
115   Sets type
116 */
117 void GLViewer_CoordSystem::setType( CSType type )
118 {
119     myType = type;
120 }
121
122 /*!
123   Recalculate co-ordinates to reference co-ordinates
124   \param x, y - co-ordinates
125 */
126 void GLViewer_CoordSystem::toReference( double& x, double& y )
127 {
128     if( myType==Cartesian )
129     {
130         double newx = myX0 + myXUnit*x*cos(myRotation) - myYUnit*y*sin(myRotation),
131                newy = myY0 + myXUnit*x*sin(myRotation) + myYUnit*y*cos(myRotation);
132         x = newx;
133         y = newy;
134     }
135     else if( myType==Polar )
136     {
137         double r = x, phi = y;
138         x = myX0 + myXUnit*r*cos(phi+myRotation);
139         y = myY0 + myXUnit*r*sin(phi+myRotation);
140     }
141 }
142
143 /*!
144   Recalculate co-ordinates from reference co-ordinates
145   \param x, y - co-ordinates
146 */
147 void GLViewer_CoordSystem::fromReference( double& x, double& y )
148 {
149     x = (x - myX0) / myXUnit;
150     y = (y - myY0) / myYUnit;
151
152     if( myType==Cartesian )
153     {
154         double newx =  x*cos(myRotation) + y*sin(myRotation),
155                newy = -x*sin(myRotation) + y*cos(myRotation);
156         x = newx;
157         y = newy;
158     }
159     else if( myType==Polar )
160     {
161         double r = sqrt( x*x+y*y );
162         double phi = 0.0;
163         double eps = 1E-8, pi = 3.14159265;
164
165         if( r>eps )
166             if( fabs(x)>eps )
167             {
168                 double arg = y/x;
169                 phi = atan(arg);
170                 if( x<0 ) // 2-nd or 4-rd quarter
171                     phi+=pi;
172             }
173             else if( y>0 )
174                 phi = pi/2.0;
175             else
176                 phi = 3*pi/2.0;
177
178         x = r;
179         y = phi-myRotation;
180     }
181 }
182
183 /*!
184   Recalculate co-ordinates to co-ordinates of other CS
185   \param aSystem - other CS
186   \param x, y - co-ordinates
187 */
188 void GLViewer_CoordSystem::transform( GLViewer_CoordSystem& aSystem, double& x, double& y )
189 {
190     toReference( x, y );
191     aSystem.fromReference( x, y );
192 }
193
194 /*!
195   \return stretching of CS along X and Y axis
196 */
197 void GLViewer_CoordSystem::getStretching( GLViewer_CoordSystem& aSystem, double& theX, double& theY )
198 {
199     theX = myXUnit / aSystem.myXUnit;
200     theY = myYUnit / aSystem.myYUnit;
201 }