1 // Copyright (C) 2005 OPEN CASCADE
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.
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.
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
17 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
19 // Author : OPEN CASCADE
22 // File: GLViewer_Context.cxx
23 // Created: November, 2004
25 //================================================================
26 // Class : GLViewer_CoordSystem
27 // Description : Class implementing mathematical model of 2D coordinate system
28 //================================================================
29 #include "GLViewer_CoordSystem.h"
32 //=======================================================================
33 // Function: GLViewer_CoordSystem
35 //=======================================================================
36 GLViewer_CoordSystem::GLViewer_CoordSystem( CSType aType, double X0, double Y0,
37 double XUnit, double YUnit, double Rotation )
41 setUnits( XUnit, YUnit );
42 setRotation( Rotation );
45 //=======================================================================
46 // Function: getOrigin
48 //=======================================================================
49 void GLViewer_CoordSystem::getOrigin( double& x, double& y ) const
55 //=======================================================================
56 // Function: setOrigin
58 //=======================================================================
59 void GLViewer_CoordSystem::setOrigin( double x, double y )
65 //=======================================================================
68 //=======================================================================
69 void GLViewer_CoordSystem::getUnits( double& x, double& y ) const
75 //=======================================================================
78 //=======================================================================
79 void GLViewer_CoordSystem::setUnits( double x, double y )
91 //=======================================================================
92 // Function: getRotation
94 //=======================================================================
95 double GLViewer_CoordSystem::getRotation() const
100 //=======================================================================
101 // Function: setRotation
103 //=======================================================================
104 void GLViewer_CoordSystem::setRotation( double rotation )
106 myRotation = rotation;
109 //=======================================================================
112 //=======================================================================
113 GLViewer_CoordSystem::CSType GLViewer_CoordSystem::getType() const
118 //=======================================================================
121 //=======================================================================
122 void GLViewer_CoordSystem::setType( CSType type )
127 //=======================================================================
128 // Function: toReference
130 //=======================================================================
131 void GLViewer_CoordSystem::toReference( double& x, double& y )
133 if( myType==Cartesian )
135 double newx = myX0 + myXUnit*x*cos(myRotation) - myYUnit*y*sin(myRotation),
136 newy = myY0 + myXUnit*x*sin(myRotation) + myYUnit*y*cos(myRotation);
140 else if( myType==Polar )
142 double r = x, phi = y;
143 x = myX0 + myXUnit*r*cos(phi+myRotation);
144 y = myY0 + myXUnit*r*sin(phi+myRotation);
148 //=======================================================================
149 // Function: fromReference
151 //=======================================================================
152 void GLViewer_CoordSystem::fromReference( double& x, double& y )
154 x = (x - myX0) / myXUnit;
155 y = (y - myY0) / myYUnit;
157 if( myType==Cartesian )
159 double newx = x*cos(myRotation) + y*sin(myRotation),
160 newy = -x*sin(myRotation) + y*cos(myRotation);
164 else if( myType==Polar )
166 double r = sqrt( x*x+y*y );
168 double eps = 1E-8, pi = 3.14159265;
175 if( x<0 ) // 2-nd or 4-rd quarter
188 //=======================================================================
189 // Function: transform
191 //=======================================================================
192 void GLViewer_CoordSystem::transform( GLViewer_CoordSystem& aSystem, double& x, double& y )
195 aSystem.fromReference( x, y );
198 //=======================================================================
199 // Function: getStretching
201 //=======================================================================
202 void GLViewer_CoordSystem::getStretching( GLViewer_CoordSystem& aSystem, double& theX, double& theY )
204 theX = myXUnit / aSystem.myXUnit;
205 theY = myYUnit / aSystem.myYUnit;