Salome HOME
*** empty log message ***
[modules/gui.git] / src / GLViewer / GLViewer_CoordSystem.cxx
1 /***************************************************************************
2 **  Class:   GLViewer_CoordSystem
3 **  Descr:   
4 **  Module:  GLViewer
5 **  Created: UI team, 03.09.02
6 ****************************************************************************/
7
8 //#include <GLViewerAfx.h>
9 #include "GLViewer_CoordSystem.h"
10 #include <math.h>
11
12 GLViewer_CoordSystem::GLViewer_CoordSystem( CSType aType, double X0, double Y0, 
13                                             double XUnit, double YUnit, double Rotation )
14 {
15     setType( aType );
16     setOrigin( X0, Y0 );
17     setUnits( XUnit, YUnit );
18     setRotation( Rotation );
19 }
20
21 void GLViewer_CoordSystem::getOrigin( double& x, double& y ) const
22 {
23     x = myX0;
24     y = myY0;
25 }
26
27 void GLViewer_CoordSystem::setOrigin( double x, double y )
28 {
29     myX0 = x;
30     myY0 = y;
31 }
32
33 void GLViewer_CoordSystem::getUnits( double& x, double& y ) const
34 {
35     x = myXUnit;
36     y = myYUnit;
37 }
38
39 void GLViewer_CoordSystem::setUnits( double x, double y )
40 {
41     if( x>0 )
42         myXUnit = x;
43     else
44         myXUnit = 1.0;
45
46     if( y>0 )
47         myYUnit = y;
48     else
49         myYUnit = 1.0;
50 }
51
52 double GLViewer_CoordSystem::getRotation() const
53 {
54     return myRotation;
55 }
56
57 void GLViewer_CoordSystem::setRotation( double rotation )
58 {
59     myRotation = rotation;
60 }
61
62 GLViewer_CoordSystem::CSType GLViewer_CoordSystem::getType() const
63 {
64     return myType;
65 }
66
67 void GLViewer_CoordSystem::setType( CSType type )
68 {
69     myType = type;
70 }
71
72 void GLViewer_CoordSystem::toReference( double& x, double& y )
73 {
74     if( myType==Cartesian )
75     {
76         double newx = myX0 + myXUnit*x*cos(myRotation) - myYUnit*y*sin(myRotation),
77                newy = myY0 + myXUnit*x*sin(myRotation) + myYUnit*y*cos(myRotation);
78         x = newx;
79         y = newy;
80     }
81     else if( myType==Polar )
82     {
83         double r = x, phi = y;
84         x = myX0 + myXUnit*r*cos(phi+myRotation);
85         y = myY0 + myXUnit*r*sin(phi+myRotation);
86     }
87 }
88
89 void GLViewer_CoordSystem::fromReference( double& x, double& y )
90 {
91     x = (x - myX0) / myXUnit;
92     y = (y - myY0) / myYUnit;
93
94     if( myType==Cartesian )
95     {
96         double newx =  x*cos(myRotation) + y*sin(myRotation),
97                newy = -x*sin(myRotation) + y*cos(myRotation);
98         x = newx;
99         y = newy;
100     }
101     else if( myType==Polar )
102     {
103         double r = sqrt( x*x+y*y );
104         double phi = 0.0;
105         double eps = 1E-8, pi = 3.14159265;
106
107         if( r>eps )
108             if( fabs(x)>eps )
109             {
110                 double arg = y/x;
111                 phi = atan(arg);
112                 if( x<0 ) // 2-nd or 4-rd quarter
113                     phi+=pi;
114             }
115             else if( y>0 )
116                 phi = pi/2.0;
117             else
118                 phi = 3*pi/2.0;
119
120         x = r;
121         y = phi-myRotation;
122     }
123 }
124
125 void GLViewer_CoordSystem::transform( GLViewer_CoordSystem& aSystem, double& x, double& y )
126 {
127     toReference( x, y );
128     aSystem.fromReference( x, y );
129 }
130
131 void GLViewer_CoordSystem::getStretching( GLViewer_CoordSystem& aSystem, double& theX, double& theY )
132 {
133     theX = myXUnit / aSystem.myXUnit;
134     theY = myYUnit / aSystem.myYUnit;
135 }