Salome HOME
4b75d3e782b3767db196c8be114f421b5ce4fb44
[modules/gui.git] / src / GLViewer / GLViewer_Geom.h
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  Author : OPEN CASCADE
23 // File:      GLViewer_Geom.h
24 // Created:   November, 2004
25 //
26 #ifndef GLVIEWER_GEOM_H
27 #define GLVIEWER_GEOM_H
28
29 #include "GLViewer.h"
30
31 #include <QRect>
32 #include <QtOpenGL>
33 #include <math.h>
34
35 //using namespace QGL;
36
37 #ifdef WIN32
38 #pragma warning( disable:4251 )
39 #endif
40
41 /*! Struct GLViewer_Pnt
42 * Substitution of QPoint for OpenGL
43 */
44
45 struct GLVIEWER_API GLViewer_Pnt
46 {
47 public:
48   GLViewer_Pnt() : myX( 0. ), myY( 0. ) {};
49   GLViewer_Pnt( GLfloat theX, GLfloat theY ) : myX( theX ), myY( theY ) {}
50   
51   GLfloat x() const { return myX; }
52   GLfloat y() const { return myY; }
53   void    setX( GLfloat theX ) { myX = theX; }
54   void    setY( GLfloat theY ) { myY = theY; }
55   void    setXY( GLfloat theX, GLfloat theY ) { myX = theX; myY = theY; }
56   void    move( GLfloat theDX, GLfloat theDY ) { myX += theDX; myY += theDY; }
57   
58 private:
59   GLfloat myX;
60   GLfloat myY;
61 };
62
63 typedef QList<GLViewer_Pnt> GLViewer_PntList;
64
65 /*! Class  GLViewer_Rect
66 *  Substitution of QRect for OpenGL
67 */
68
69 class GLVIEWER_API GLViewer_Rect
70 {
71 public:
72   GLViewer_Rect(): myLeft(0.0), myRight(0.0), myTop(0.0), myBottom(0.0){}
73   GLViewer_Rect( float theLeft, float theRight, float theTop, float theBottom )
74     : myLeft(theLeft), myRight(theRight), myTop(theTop), myBottom(theBottom) {}
75   GLViewer_Rect( QRect theRect ) {
76     myLeft = ( float )theRect.left(); myRight = ( float )theRect.right();
77     myTop = ( float )theRect.top(); myBottom = ( float )theRect.bottom(); }
78   
79   float       left() const { return myLeft; }
80   float       right() const { return myRight; }
81   float       top() const { return myTop; }
82   float       bottom() const { return myBottom; }
83
84   float       width() const { return fabs( myRight - myLeft ); }
85   float       height() const { return fabs( myTop - myBottom ); }
86   
87   void        setLeft( float theLeft ) { myLeft = theLeft; }
88   void        setRight( float theRight ) { myRight = theRight; }
89   void        setTop( float theTop ) { myTop = theTop; }
90   void        setBottom( float theBottom ) { myBottom = theBottom; }
91   
92   void        setCoords( float theLeft, float theRight, float theBottom, float theTop )
93   { myLeft = theLeft; myRight = theRight; myBottom = theBottom; myTop = theTop; }
94   
95   //! \warning This method translate only rect format
96   QRect       toQRect() { return QRect( ( int )myLeft, ( int )myBottom,
97                                         ( int )( myRight - myLeft ),
98                                         ( int )( myTop - myBottom ) ); }
99
100   //! On/off empty status
101   void        setIsEmpty( bool on ) { myIsEmpty = on; }
102   //! Checks empty status
103   bool        isEmpty() const { return myIsEmpty; }
104
105   //! Checks null status
106   bool        isNull() const { return myLeft == 0.0 && myRight == 0.0 && myBottom == 0.0 && myTop == 0.0; }
107   //! Checks valid status
108   bool        isValid() const { return ( myLeft < myRight && myBottom < myTop ); }
109
110   //! Checks staus of contains point 
111   bool        contains( GLViewer_Pnt pnt ) { return ( pnt.x() > left() &&
112                                                       pnt.x() < right() &&
113                                                       pnt.y() > bottom() &&
114                                                       pnt.y() < top() ); }
115   
116   void        move( const float x, const float y )
117                   {
118                     myLeft   += x;
119                     myRight  += x;
120                     myTop    += y;
121                     myBottom += y;
122                   }
123
124 protected:
125   float       myLeft;
126   float       myRight;
127   float       myTop;
128   float       myBottom;
129
130   bool        myIsEmpty;
131 };
132
133 /*! Class GLViewer_Segment
134 * Segment for 2d detection
135 */
136
137 class GLVIEWER_API GLViewer_Segment
138 {
139 public:
140   GLViewer_Segment( const GLViewer_Pnt& thePnt1, 
141                     const GLViewer_Pnt& thePnt2 );
142   
143   //! Ordinary segment construction
144   /*!Construction of a ray with given equation Ax + By + C = 0 */
145
146   GLViewer_Segment( const GLViewer_Pnt& thePnt, 
147                     const GLfloat theA, 
148                     const GLfloat theB,
149                     const GLfloat theC );
150   ~GLViewer_Segment();
151
152   bool              HasIntersection( const GLViewer_Segment& theOther ) const;
153   // Detects intersection with another segment or ray
154
155 private:
156   GLViewer_Pnt      myPnt1;
157   GLViewer_Pnt      myPnt2;
158   GLfloat           myA;
159   GLfloat           myB;
160   GLfloat           myC;
161 };
162
163 /*! Class  GLViewer_Poly
164 * Polygon for 2d detection
165 */
166
167 class GLVIEWER_API GLViewer_Poly 
168 {
169 public:
170   GLViewer_Poly( const GLViewer_PntList* thePoints );
171   virtual ~GLViewer_Poly();
172
173   //! Adds point to polygon
174   void              AddPoint( GLViewer_Pnt& pnt ) { myPoints->append( pnt ); }
175
176   //! Returns number of point
177   int               Count() const { return myPoints->count(); }
178
179   //! Returns true if a point lies inside this polygon
180   virtual bool      IsIn( const GLViewer_Pnt& thePnt ) const;
181
182   //! Returns true if a other polygon covers this polygon  
183   virtual bool      IsCovers( const GLViewer_Poly& thePoly ) const;
184
185   //! Likes the above function
186   virtual bool      IsCovers( const GLViewer_Rect& theRect ) const;
187   
188   // Returns true if intersection of this polygon with a segment or a ray not empty
189   virtual bool      HasIntersection( const GLViewer_Segment& theSegment ) const;
190
191 private:
192   GLViewer_PntList* myPoints;
193 };
194
195 #ifdef WIN32
196 #pragma warning ( default:4251 )
197 #endif
198
199 #endif