Salome HOME
New item (FontItem), allowing to show information about font setting and to select...
[modules/gui.git] / src / GLViewer / GLViewer_CoordSystem.cxx
1 // File:      GLViewer_Context.cxx
2 // Created:   November, 2004
3 // Author:    OCC team
4 // Copyright (C) CEA 2004
5
6 //================================================================
7 // Class       : GLViewer_CoordSystem
8 // Description : Class implementing mathematical model of 2D coordinate system 
9 //================================================================
10 #include "GLViewer_CoordSystem.h"
11 #include <math.h>
12
13 //=======================================================================
14 // Function: GLViewer_CoordSystem
15 // Purpose :
16 //=======================================================================
17 GLViewer_CoordSystem::GLViewer_CoordSystem( CSType aType, double X0, double Y0, 
18                                             double XUnit, double YUnit, double Rotation )
19 {
20     setType( aType );
21     setOrigin( X0, Y0 );
22     setUnits( XUnit, YUnit );
23     setRotation( Rotation );
24 }
25
26 //=======================================================================
27 // Function: getOrigin
28 // Purpose :
29 //=======================================================================
30 void GLViewer_CoordSystem::getOrigin( double& x, double& y ) const
31 {
32     x = myX0;
33     y = myY0;
34 }
35
36 //=======================================================================
37 // Function: setOrigin
38 // Purpose :
39 //=======================================================================
40 void GLViewer_CoordSystem::setOrigin( double x, double y )
41 {
42     myX0 = x;
43     myY0 = y;
44 }
45
46 //=======================================================================
47 // Function: getUnits
48 // Purpose :
49 //=======================================================================
50 void GLViewer_CoordSystem::getUnits( double& x, double& y ) const
51 {
52     x = myXUnit;
53     y = myYUnit;
54 }
55
56 //=======================================================================
57 // Function: setUnits
58 // Purpose :
59 //=======================================================================
60 void GLViewer_CoordSystem::setUnits( double x, double y )
61 {
62     if( x>0 )
63         myXUnit = x;
64     else
65         myXUnit = 1.0;
66
67     if( y>0 )
68         myYUnit = y;
69     else
70         myYUnit = 1.0;
71 }
72 //=======================================================================
73 // Function: getRotation
74 // Purpose :
75 //=======================================================================
76 double GLViewer_CoordSystem::getRotation() const
77 {
78     return myRotation;
79 }
80
81 //=======================================================================
82 // Function: setRotation
83 // Purpose :
84 //=======================================================================
85 void GLViewer_CoordSystem::setRotation( double rotation )
86 {
87     myRotation = rotation;
88 }
89
90 //=======================================================================
91 // Function: getType
92 // Purpose :
93 //=======================================================================
94 GLViewer_CoordSystem::CSType GLViewer_CoordSystem::getType() const
95 {
96     return myType;
97 }
98
99 //=======================================================================
100 // Function: setType
101 // Purpose :
102 //=======================================================================
103 void GLViewer_CoordSystem::setType( CSType type )
104 {
105     myType = type;
106 }
107
108 //=======================================================================
109 // Function: toReference
110 // Purpose :
111 //=======================================================================
112 void GLViewer_CoordSystem::toReference( double& x, double& y )
113 {
114     if( myType==Cartesian )
115     {
116         double newx = myX0 + myXUnit*x*cos(myRotation) - myYUnit*y*sin(myRotation),
117                newy = myY0 + myXUnit*x*sin(myRotation) + myYUnit*y*cos(myRotation);
118         x = newx;
119         y = newy;
120     }
121     else if( myType==Polar )
122     {
123         double r = x, phi = y;
124         x = myX0 + myXUnit*r*cos(phi+myRotation);
125         y = myY0 + myXUnit*r*sin(phi+myRotation);
126     }
127 }
128
129 //=======================================================================
130 // Function: fromReference
131 // Purpose :
132 //=======================================================================
133 void GLViewer_CoordSystem::fromReference( double& x, double& y )
134 {
135     x = (x - myX0) / myXUnit;
136     y = (y - myY0) / myYUnit;
137
138     if( myType==Cartesian )
139     {
140         double newx =  x*cos(myRotation) + y*sin(myRotation),
141                newy = -x*sin(myRotation) + y*cos(myRotation);
142         x = newx;
143         y = newy;
144     }
145     else if( myType==Polar )
146     {
147         double r = sqrt( x*x+y*y );
148         double phi = 0.0;
149         double eps = 1E-8, pi = 3.14159265;
150
151         if( r>eps )
152             if( fabs(x)>eps )
153             {
154                 double arg = y/x;
155                 phi = atan(arg);
156                 if( x<0 ) // 2-nd or 4-rd quarter
157                     phi+=pi;
158             }
159             else if( y>0 )
160                 phi = pi/2.0;
161             else
162                 phi = 3*pi/2.0;
163
164         x = r;
165         y = phi-myRotation;
166     }
167 }
168
169 //=======================================================================
170 // Function: transform
171 // Purpose :
172 //=======================================================================
173 void GLViewer_CoordSystem::transform( GLViewer_CoordSystem& aSystem, double& x, double& y )
174 {
175     toReference( x, y );
176     aSystem.fromReference( x, y );
177 }
178
179 //=======================================================================
180 // Function: getStretching
181 // Purpose :
182 //=======================================================================
183 void GLViewer_CoordSystem::getStretching( GLViewer_CoordSystem& aSystem, double& theX, double& theY )
184 {
185     theX = myXUnit / aSystem.myXUnit;
186     theY = myYUnit / aSystem.myYUnit;
187 }