Salome HOME
a07fa0ca0d1c4daa506193000a47078a47859bbb
[modules/gui.git] / src / GLViewer / GLViewer_Geom.h
1 // Copyright (C) 2007-2022  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, or (at your option) any later version.
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
23 //  Author : OPEN CASCADE
24 // File:      GLViewer_Geom.h
25 // Created:   November, 2004
26 //
27 #ifndef GLVIEWER_GEOM_H
28 #define GLVIEWER_GEOM_H
29
30 #include "GLViewer.h"
31
32 #include <QRect>
33 #include <QtOpenGL>
34 #include <math.h>
35
36 #ifdef WIN32
37 #pragma warning( disable:4251 )
38 #endif
39
40 /*! Struct GLViewer_Pnt
41 * Substitution of QPoint for OpenGL
42 */
43
44 struct GLVIEWER_API GLViewer_Pnt
45 {
46 public:
47   GLViewer_Pnt() : myX( 0. ), myY( 0. ) {};
48   GLViewer_Pnt( GLfloat theX, GLfloat theY ) : myX( theX ), myY( theY ) {}
49   
50   GLfloat x() const { return myX; }
51   GLfloat y() const { return myY; }
52   void    setX( GLfloat theX ) { myX = theX; }
53   void    setY( GLfloat theY ) { myY = theY; }
54   void    setXY( GLfloat theX, GLfloat theY ) { myX = theX; myY = theY; }
55   void    move( GLfloat theDX, GLfloat theDY ) { myX += theDX; myY += theDY; }
56   
57 private:
58   GLfloat myX;
59   GLfloat myY;
60 };
61
62 typedef QList<GLViewer_Pnt> GLViewer_PntList;
63
64 /*! Class  GLViewer_Rect
65 *  Substitution of QRect for OpenGL
66 */
67
68 class GLVIEWER_API GLViewer_Rect
69 {
70 public:
71   GLViewer_Rect(): myLeft(0.0), myRight(0.0), myTop(0.0), myBottom(0.0){}
72   GLViewer_Rect( float theLeft, float theRight, float theTop, float theBottom )
73     : myLeft(theLeft), myRight(theRight), myTop(theTop), myBottom(theBottom) {}
74   GLViewer_Rect( QRect theRect ) {
75     myLeft = ( float )theRect.left(); myRight = ( float )theRect.right();
76     myTop = ( float )theRect.top(); myBottom = ( float )theRect.bottom(); }
77   
78   float       left() const { return myLeft; }
79   float       right() const { return myRight; }
80   float       top() const { return myTop; }
81   float       bottom() const { return myBottom; }
82
83   float       width() const { return fabs( myRight - myLeft ); }
84   float       height() const { return fabs( myTop - myBottom ); }
85   
86   void        setLeft( float theLeft ) { myLeft = theLeft; }
87   void        setRight( float theRight ) { myRight = theRight; }
88   void        setTop( float theTop ) { myTop = theTop; }
89   void        setBottom( float theBottom ) { myBottom = theBottom; }
90   
91   void        setCoords( float theLeft, float theRight, float theBottom, float theTop )
92   { myLeft = theLeft; myRight = theRight; myBottom = theBottom; myTop = theTop; }
93   
94   //! \warning This method translate only rect format
95   QRect       toQRect() { return QRect( ( int )myLeft, ( int )myBottom,
96                                         ( int )( myRight - myLeft ),
97                                         ( int )( myTop - myBottom ) ); }
98
99   //! On/off empty status
100   void        setIsEmpty( bool on ) { myIsEmpty = on; }
101   //! Checks empty status
102   bool        isEmpty() const { return myIsEmpty; }
103
104   //! Checks null status
105   bool        isNull() const { return myLeft == 0.0 && myRight == 0.0 && myBottom == 0.0 && myTop == 0.0; }
106   //! Checks valid status
107   bool        isValid() const { return ( myLeft < myRight && myBottom < myTop ); }
108
109   //! Checks staus of contains point 
110   bool        contains( GLViewer_Pnt pnt ) { return ( pnt.x() > left() &&
111                                                       pnt.x() < right() &&
112                                                       pnt.y() > bottom() &&
113                                                       pnt.y() < top() ); }
114   
115   void        move( const float x, const float y )
116                   {
117                     myLeft   += x;
118                     myRight  += x;
119                     myTop    += y;
120                     myBottom += y;
121                   }
122
123 protected:
124   float       myLeft;
125   float       myRight;
126   float       myTop;
127   float       myBottom;
128
129   bool        myIsEmpty;
130 };
131
132 /*! Class GLViewer_Segment
133 * Segment for 2d detection
134 */
135
136 class GLVIEWER_API GLViewer_Segment
137 {
138 public:
139   GLViewer_Segment( const GLViewer_Pnt& thePnt1, 
140                     const GLViewer_Pnt& thePnt2 );
141   
142   //! Ordinary segment construction
143   /*!Construction of a ray with given equation Ax + By + C = 0 */
144
145   GLViewer_Segment( const GLViewer_Pnt& thePnt, 
146                     const GLfloat theA, 
147                     const GLfloat theB,
148                     const GLfloat theC );
149   ~GLViewer_Segment();
150
151   bool              HasIntersection( const GLViewer_Segment& theOther ) const;
152   // Detects intersection with another segment or ray
153
154 private:
155   GLViewer_Pnt      myPnt1;
156   GLViewer_Pnt      myPnt2;
157   GLfloat           myA;
158   GLfloat           myB;
159   GLfloat           myC;
160 };
161
162 /*! Class  GLViewer_Poly
163 * Polygon for 2d detection
164 */
165
166 class GLVIEWER_API GLViewer_Poly 
167 {
168 public:
169   GLViewer_Poly( const GLViewer_PntList* thePoints );
170   virtual ~GLViewer_Poly();
171
172   //! Adds point to polygon
173   void              AddPoint( GLViewer_Pnt& pnt ) { myPoints->append( pnt ); }
174
175   //! Returns number of point
176   int               Count() const { return myPoints->count(); }
177
178   //! Returns true if a point lies inside this polygon
179   virtual bool      IsIn( const GLViewer_Pnt& thePnt ) const;
180
181   //! Returns true if a other polygon covers this polygon  
182   virtual bool      IsCovers( const GLViewer_Poly& thePoly ) const;
183
184   //! Likes the above function
185   virtual bool      IsCovers( const GLViewer_Rect& theRect ) const;
186   
187   // Returns true if intersection of this polygon with a segment or a ray not empty
188   virtual bool      HasIntersection( const GLViewer_Segment& theSegment ) const;
189
190 private:
191   GLViewer_PntList* myPoints;
192 };
193
194 #ifdef WIN32
195 #pragma warning ( default:4251 )
196 #endif
197
198 #endif