Back to index

salome-gui  6.5.0
GLViewer_Grid.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_Grid.cxx
00025 // Created:   November, 2004
00026 //#include <GLViewerAfx.h>
00027 //
00028 #include "GLViewer_Grid.h"
00029 #include "GLViewer_Defs.h"
00030 
00031 #include <Precision.hxx>
00032 
00036 GLViewer_Grid::GLViewer_Grid() :
00037        myGridList( 0 ), myGridHeight( (GLfloat)0.0 ), myGridWidth( (GLfloat)0.0 ),
00038        myWinW( (GLfloat)0.0 ), myWinH( (GLfloat)0.0 ), myXSize( (GLfloat)0.0 ), myYSize( (GLfloat)0.0 ),
00039        myXPan( (GLfloat)0.0 ), myYPan( (GLfloat)0.0 ), myXScale( (GLfloat)1.0 ), myYScale( (GLfloat)1.0 ),
00040        myLineWidth( (GLfloat)0.05 ), myCenterWidth( (GLfloat)1.5 ), myCenterRadius( (GLfloat)5.0 ), 
00041        myScaleFactor( 10 ), myIsUpdate( GL_FALSE )
00042 {
00043   myGridColor[0] = 0.5;
00044   myGridColor[1] = 0.5;
00045   myGridColor[2] = 0.5;
00046   myAxisColor[0] = 0.75;
00047   myAxisColor[1] = 0.75;
00048   myAxisColor[2] = 0.75;
00049 }
00050 
00059 GLViewer_Grid::GLViewer_Grid( GLfloat width, GLfloat height,
00060                               GLfloat winW, GLfloat winH,
00061                               GLfloat xSize, GLfloat ySize,
00062                               GLfloat xPan, GLfloat yPan,
00063                               GLfloat xScale, GLfloat yScale ) :
00064        myGridList( 0 ), myGridHeight( (GLfloat)0.0 ), myGridWidth( (GLfloat)0.0 ),
00065        myWinW( (GLfloat)0.0 ), myWinH( (GLfloat)0.0 ), myXSize( (GLfloat)0.0 ), myYSize( (GLfloat)0.0 ),
00066        myXPan( (GLfloat)0.0 ), myYPan( (GLfloat)0.0 ), myXScale( (GLfloat)1.0 ), myYScale( (GLfloat)1.0 ),
00067        myLineWidth( (GLfloat)0.05 ), myCenterWidth( (GLfloat)1.5 ), myCenterRadius( (GLfloat)5.0 ), 
00068        myScaleFactor( 10 ), myIsUpdate( GL_FALSE )
00069 {
00070   myGridColor[0] = 0.5;
00071   myGridColor[1] = 0.5;
00072   myGridColor[2] = 0.5;
00073   myAxisColor[0] = 0.75;
00074   myAxisColor[1] = 0.75;
00075   myAxisColor[2] = 0.75;
00076 }
00077 
00081 GLViewer_Grid::~GLViewer_Grid()
00082 {
00083 }
00084 
00088 void GLViewer_Grid::draw()
00089 {
00090   if ( myGridList == 0 || myIsUpdate )
00091     initList();
00092 
00093   glCallList( myGridList );
00094 }
00095 
00100 void GLViewer_Grid::setGridColor( GLfloat r, GLfloat g, GLfloat b )
00101 {
00102   if( myGridColor[0] == r && myGridColor[1] == g && myGridColor[2] == b )
00103     return;
00104 
00105   myGridColor[0] = r;
00106   myGridColor[1] = g;
00107   myGridColor[2] = b;
00108   myIsUpdate = GL_TRUE;
00109 }
00110 
00115 void GLViewer_Grid::setAxisColor( GLfloat r, GLfloat g, GLfloat b )
00116 {
00117   if( myAxisColor[0] == r && myAxisColor[1] == g && myAxisColor[2] == b )
00118     return;
00119 
00120   myAxisColor[0] = r;
00121   myAxisColor[1] = g;
00122   myAxisColor[2] = b;
00123   myIsUpdate = GL_TRUE;
00124 }
00125 
00130 void GLViewer_Grid::setGridWidth( float w )
00131 {
00132   if( myGridWidth == w )
00133     return;
00134 
00135   myGridWidth = w;
00136   myIsUpdate = GL_TRUE;
00137 }
00138 
00143 void GLViewer_Grid::setCenterRadius( int r )
00144 {
00145   if( myCenterRadius == r )
00146     return;
00147 
00148   myCenterRadius = r;
00149   myIsUpdate = GL_TRUE;
00150 }
00151 
00157 void GLViewer_Grid::setSize( float xSize, float ySize )
00158 {
00159   if( myXSize == xSize && myYSize == ySize )
00160     return;
00161   
00162   myXSize = xSize;
00163   myYSize = ySize;
00164   myIsUpdate = GL_TRUE;
00165 }
00166 
00172 void GLViewer_Grid::setPan( float xPan, float yPan )
00173 {
00174   if( myXPan == xPan && myYPan == yPan )
00175     return;
00176  
00177   myXPan = xPan;
00178   myYPan = yPan;
00179   myIsUpdate = GL_TRUE; 
00180 }
00181 
00186 bool GLViewer_Grid::setZoom( float zoom )
00187 {
00188   if( zoom == 1.0 )
00189     return true;
00190   
00191   //backup values
00192   float bXScale = myXScale;
00193   float bYScale = myYScale;
00194 
00195   myXScale /= zoom; 
00196   myYScale /= zoom;
00197 
00198   if( fabs(myXScale) < Precision::Confusion() || fabs(myYScale) < Precision::Confusion() )
00199   { //undo
00200     myXScale = bXScale;
00201     myYScale = bYScale;
00202     return false;
00203   }
00204   
00205   myGridWidth /= zoom; 
00206   myGridHeight /= zoom;  
00207   myIsUpdate = GL_TRUE;
00208   return true;
00209 }
00210 
00217 void GLViewer_Grid::setResize( float WinW, float WinH, float zoom )
00218 {
00219   if( myWinW == WinW && myWinH == WinH && zoom == 1.0 )
00220     return;
00221 
00222   myGridWidth = myGridWidth + ( WinW - myWinW ) * myXScale; 
00223   myGridHeight = myGridHeight + ( WinH - myWinH ) * myYScale;
00224   myWinW = WinW;
00225   myWinH = WinH;
00226   setZoom( zoom );
00227   myIsUpdate = GL_TRUE;
00228 }
00229 
00235 void GLViewer_Grid::getSize( float& xSize, float& ySize ) const
00236 {
00237   xSize = myXSize;
00238   ySize = myYSize;
00239 }
00240 
00246 void GLViewer_Grid::getPan( float& xPan, float& yPan ) const
00247 {
00248   xPan = myXPan;
00249   yPan = myYPan;
00250 }
00251 
00257 void GLViewer_Grid::getScale( float& xScale, float& yScale ) const
00258 {
00259   xScale = myXScale;
00260   yScale = myYScale;
00261 }
00262 
00266 bool GLViewer_Grid::initList()
00267 {
00268   myIsUpdate = GL_FALSE;
00269    
00270     if( myXSize == (GLfloat)0.0 )
00271         myXSize = (GLfloat)0.1;
00272     if( myYSize == (GLfloat)0.0 )
00273         myYSize = (GLfloat)0.1;
00274 
00275 label:
00276   if( ( myXSize >= myGridWidth/5 ) && ( myYSize >= myGridHeight/5 ) )
00277   { //zoom in
00278     myXSize /= myScaleFactor;
00279     myYSize /= myScaleFactor;
00280     goto label;
00281   }
00282   else if( ( myXSize * myScaleFactor < myGridWidth/5 ) 
00283         || ( myYSize * myScaleFactor < myGridHeight/5 ) )
00284   { //zoom out
00285     myXSize *= myScaleFactor;
00286     myYSize *= myScaleFactor;
00287     goto label;
00288   }
00289 
00290   //int n = myGridWidth / myXSize;
00291   //int m = myGridHeight / myYSize;
00292   // do not initialise integer by float
00293   //if( ( n != 0 ) || ( m != 0 ) ) 
00294   if( ( myGridWidth > 0.5 * myXSize ) || ( myGridHeight > 0.5 * myYSize ) )
00295   { 
00296     if ( myGridList != 0 )  
00297     { 
00298       glDeleteLists( myGridList, 1 ); 
00299       if ( glGetError() != GL_NO_ERROR ) 
00300     return FALSE;
00301     } 
00302          
00303     float xLoc = (int)(myXPan / myXSize) * myXSize; 
00304     float yLoc = (int)(myYPan / myYSize) * myYSize; 
00305  
00306     myGridList = glGenLists( 1 ); 
00307     glNewList( myGridList, GL_COMPILE ); 
00308 
00309     glColor3f( myGridColor[0], myGridColor[1], myGridColor[2] );  
00310     glLineWidth( myLineWidth ); 
00311     
00312     glBegin( GL_LINES ); 
00313     for( int j = 0; ( j-1 ) * myXSize <= myGridWidth / 2 ; j++ )
00314     { 
00315       glVertex2d( -myXSize * j - xLoc, -myGridHeight / 2 - myYSize - yLoc );
00316       glVertex2d( -myXSize * j - xLoc,  myGridHeight / 2 + myYSize - yLoc ); 
00317       glVertex2d(  myXSize * j - xLoc, -myGridHeight / 2 - myYSize - yLoc );
00318       glVertex2d(  myXSize * j - xLoc,  myGridHeight / 2 + myYSize - yLoc );
00319     }
00320     for( int i = 0; ( i-1 ) * myYSize <= myGridHeight / 2 ; i++)  
00321     {
00322       glVertex2d( -myGridWidth / 2 - myXSize - xLoc, -myYSize * i - yLoc ); 
00323       glVertex2d(  myGridWidth / 2 + myXSize - xLoc, -myYSize * i - yLoc ); 
00324       glVertex2d( -myGridWidth / 2 - myXSize - xLoc,  myYSize * i - yLoc ); 
00325       glVertex2d(  myGridWidth / 2 + myXSize - xLoc,  myYSize * i - yLoc ); 
00326     } 
00327     glEnd();
00328 
00329     glColor3f( myAxisColor[0], myAxisColor[1], myAxisColor[2] );
00330     glLineWidth( myCenterWidth );
00331 
00332     glBegin( GL_LINES );
00333     glVertex2d(  myGridWidth / 2 + myXSize - xLoc, 0); 
00334     glVertex2d( -myGridWidth / 2 - myXSize - xLoc, 0); 
00335     glVertex2d( 0,  myGridHeight / 2 + myYSize - yLoc );
00336     glVertex2d( 0, -myGridHeight / 2 - myYSize - yLoc );    
00337     glEnd();
00338 
00339     glBegin( GL_LINE_LOOP ); 
00340     double angle = 0.0;
00341     for ( int k = 0; k < SEGMENTS; k++ )     
00342     { 
00343       glVertex2f( cos(angle) * myCenterRadius * myXScale,
00344           sin(angle) * myCenterRadius * myYScale ); 
00345       angle += STEP; 
00346     } 
00347     glEnd();
00348 
00349     glEndList();
00350   }
00351   return TRUE;
00352 }