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