Back to index

salome-gui  6.5.0
GLViewer_CoordSystem.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  Author : OPEN CASCADE
00024 // File:      GLViewer_Context.cxx
00025 // Created:   November, 2004
00026 //
00027 #include "GLViewer_CoordSystem.h"
00028 #include <math.h>
00029 
00039 GLViewer_CoordSystem::GLViewer_CoordSystem( CSType aType, double X0, double Y0, 
00040                                             double XUnit, double YUnit, double Rotation )
00041 {
00042     setType( aType );
00043     setOrigin( X0, Y0 );
00044     setUnits( XUnit, YUnit );
00045     setRotation( Rotation );
00046 }
00047 
00051 void GLViewer_CoordSystem::getOrigin( double& x, double& y ) const
00052 {
00053     x = myX0;
00054     y = myY0;
00055 }
00056 
00060 void GLViewer_CoordSystem::setOrigin( double x, double y )
00061 {
00062     myX0 = x;
00063     myY0 = y;
00064 }
00065 
00069 void GLViewer_CoordSystem::getUnits( double& x, double& y ) const
00070 {
00071     x = myXUnit;
00072     y = myYUnit;
00073 }
00074 
00078 void GLViewer_CoordSystem::setUnits( double x, double y )
00079 {
00080     if( x>0 )
00081         myXUnit = x;
00082     else
00083         myXUnit = 1.0;
00084 
00085     if( y>0 )
00086         myYUnit = y;
00087     else
00088         myYUnit = 1.0;
00089 }
00090 
00094 double GLViewer_CoordSystem::getRotation() const
00095 {
00096     return myRotation;
00097 }
00098 
00102 void GLViewer_CoordSystem::setRotation( double rotation )
00103 {
00104     myRotation = rotation;
00105 }
00106 
00110 GLViewer_CoordSystem::CSType GLViewer_CoordSystem::getType() const
00111 {
00112     return myType;
00113 }
00114 
00118 void GLViewer_CoordSystem::setType( CSType type )
00119 {
00120     myType = type;
00121 }
00122 
00127 void GLViewer_CoordSystem::toReference( double& x, double& y )
00128 {
00129     if( myType==Cartesian )
00130     {
00131         double newx = myX0 + myXUnit*x*cos(myRotation) - myYUnit*y*sin(myRotation),
00132                newy = myY0 + myXUnit*x*sin(myRotation) + myYUnit*y*cos(myRotation);
00133         x = newx;
00134         y = newy;
00135     }
00136     else if( myType==Polar )
00137     {
00138         double r = x, phi = y;
00139         x = myX0 + myXUnit*r*cos(phi+myRotation);
00140         y = myY0 + myXUnit*r*sin(phi+myRotation);
00141     }
00142 }
00143 
00148 void GLViewer_CoordSystem::fromReference( double& x, double& y )
00149 {
00150     x = (x - myX0) / myXUnit;
00151     y = (y - myY0) / myYUnit;
00152 
00153     if( myType==Cartesian )
00154     {
00155         double newx =  x*cos(myRotation) + y*sin(myRotation),
00156                newy = -x*sin(myRotation) + y*cos(myRotation);
00157         x = newx;
00158         y = newy;
00159     }
00160     else if( myType==Polar )
00161     {
00162         double r = sqrt( x*x+y*y );
00163         double phi = 0.0;
00164         double eps = 1E-8, pi = 3.14159265;
00165 
00166         if( r>eps )
00167         {
00168             if( fabs(x)>eps )
00169             {
00170                 double arg = y/x;
00171                 phi = atan(arg);
00172                 if( x<0 ) // 2-nd or 4-rd quarter
00173                     phi+=pi;
00174             }
00175             else if( y>0 )
00176                 phi = pi/2.0;
00177             else
00178                 phi = 3*pi/2.0;
00179         }
00180 
00181         x = r;
00182         y = phi-myRotation;
00183     }
00184 }
00185 
00191 void GLViewer_CoordSystem::transform( GLViewer_CoordSystem& aSystem, double& x, double& y )
00192 {
00193     toReference( x, y );
00194     aSystem.fromReference( x, y );
00195 }
00196 
00200 void GLViewer_CoordSystem::getStretching( GLViewer_CoordSystem& aSystem, double& theX, double& theY )
00201 {
00202     theX = myXUnit / aSystem.myXUnit;
00203     theY = myYUnit / aSystem.myYUnit;
00204 }