Back to index

salome-gui  6.5.0
GLViewer_Geom.h
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_Geom.h
00025 // Created:   November, 2004
00026 //
00027 #ifndef GLVIEWER_GEOM_H
00028 #define GLVIEWER_GEOM_H
00029 
00030 #include "GLViewer.h"
00031 
00032 #include <QRect>
00033 #include <QtOpenGL>
00034 #include <math.h>
00035 
00036 #ifdef WIN32
00037 #pragma warning( disable:4251 )
00038 #endif
00039 
00044 struct GLVIEWER_API GLViewer_Pnt
00045 {
00046 public:
00047   GLViewer_Pnt() : myX( 0. ), myY( 0. ) {};
00048   GLViewer_Pnt( GLfloat theX, GLfloat theY ) : myX( theX ), myY( theY ) {}
00049   
00050   GLfloat x() const { return myX; }
00051   GLfloat y() const { return myY; }
00052   void    setX( GLfloat theX ) { myX = theX; }
00053   void    setY( GLfloat theY ) { myY = theY; }
00054   void    setXY( GLfloat theX, GLfloat theY ) { myX = theX; myY = theY; }
00055   void    move( GLfloat theDX, GLfloat theDY ) { myX += theDX; myY += theDY; }
00056   
00057 private:
00058   GLfloat myX;
00059   GLfloat myY;
00060 };
00061 
00062 typedef QList<GLViewer_Pnt> GLViewer_PntList;
00063 
00068 class GLVIEWER_API GLViewer_Rect
00069 {
00070 public:
00071   GLViewer_Rect(): myLeft(0.0), myRight(0.0), myTop(0.0), myBottom(0.0){}
00072   GLViewer_Rect( float theLeft, float theRight, float theTop, float theBottom )
00073     : myLeft(theLeft), myRight(theRight), myTop(theTop), myBottom(theBottom) {}
00074   GLViewer_Rect( QRect theRect ) {
00075     myLeft = ( float )theRect.left(); myRight = ( float )theRect.right();
00076     myTop = ( float )theRect.top(); myBottom = ( float )theRect.bottom(); }
00077   
00078   float       left() const { return myLeft; }
00079   float       right() const { return myRight; }
00080   float       top() const { return myTop; }
00081   float       bottom() const { return myBottom; }
00082 
00083   float       width() const { return fabs( myRight - myLeft ); }
00084   float       height() const { return fabs( myTop - myBottom ); }
00085   
00086   void        setLeft( float theLeft ) { myLeft = theLeft; }
00087   void        setRight( float theRight ) { myRight = theRight; }
00088   void        setTop( float theTop ) { myTop = theTop; }
00089   void        setBottom( float theBottom ) { myBottom = theBottom; }
00090   
00091   void        setCoords( float theLeft, float theRight, float theBottom, float theTop )
00092   { myLeft = theLeft; myRight = theRight; myBottom = theBottom; myTop = theTop; }
00093   
00095   QRect       toQRect() { return QRect( ( int )myLeft, ( int )myBottom,
00096                                         ( int )( myRight - myLeft ),
00097                                         ( int )( myTop - myBottom ) ); }
00098 
00100   void        setIsEmpty( bool on ) { myIsEmpty = on; }
00102   bool        isEmpty() const { return myIsEmpty; }
00103 
00105   bool        isNull() const { return myLeft == 0.0 && myRight == 0.0 && myBottom == 0.0 && myTop == 0.0; }
00107   bool        isValid() const { return ( myLeft < myRight && myBottom < myTop ); }
00108 
00110   bool        contains( GLViewer_Pnt pnt ) { return ( pnt.x() > left() &&
00111                                                       pnt.x() < right() &&
00112                                                       pnt.y() > bottom() &&
00113                                                       pnt.y() < top() ); }
00114   
00115   void        move( const float x, const float y )
00116                   {
00117                     myLeft   += x;
00118                     myRight  += x;
00119                     myTop    += y;
00120                     myBottom += y;
00121                   }
00122 
00123 protected:
00124   float       myLeft;
00125   float       myRight;
00126   float       myTop;
00127   float       myBottom;
00128 
00129   bool        myIsEmpty;
00130 };
00131 
00136 class GLVIEWER_API GLViewer_Segment
00137 {
00138 public:
00139   GLViewer_Segment( const GLViewer_Pnt& thePnt1, 
00140                     const GLViewer_Pnt& thePnt2 );
00141   
00143 
00145   GLViewer_Segment( const GLViewer_Pnt& thePnt, 
00146                     const GLfloat theA, 
00147                     const GLfloat theB,
00148                     const GLfloat theC );
00149   ~GLViewer_Segment();
00150 
00151   bool              HasIntersection( const GLViewer_Segment& theOther ) const;
00152   // Detects intersection with another segment or ray
00153 
00154 private:
00155   GLViewer_Pnt      myPnt1;
00156   GLViewer_Pnt      myPnt2;
00157   GLfloat           myA;
00158   GLfloat           myB;
00159   GLfloat           myC;
00160 };
00161 
00166 class GLVIEWER_API GLViewer_Poly 
00167 {
00168 public:
00169   GLViewer_Poly( const GLViewer_PntList* thePoints );
00170   virtual ~GLViewer_Poly();
00171 
00173   void              AddPoint( GLViewer_Pnt& pnt ) { myPoints->append( pnt ); }
00174 
00176   int               Count() const { return myPoints->count(); }
00177 
00179   virtual bool      IsIn( const GLViewer_Pnt& thePnt ) const;
00180 
00182   virtual bool      IsCovers( const GLViewer_Poly& thePoly ) const;
00183 
00185   virtual bool      IsCovers( const GLViewer_Rect& theRect ) const;
00186   
00187   // Returns true if intersection of this polygon with a segment or a ray not empty
00188   virtual bool      HasIntersection( const GLViewer_Segment& theSegment ) const;
00189 
00190 private:
00191   GLViewer_PntList* myPoints;
00192 };
00193 
00194 #ifdef WIN32
00195 #pragma warning ( default:4251 )
00196 #endif
00197 
00198 #endif