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