Salome HOME
PAL10125 - by double click on reference original object becomes selected
[modules/gui.git] / src / GLViewer / GLViewer_Geom.h
1 // File:      GLViewer_Geom.h
2 // Created:   November, 2004
3 // Author:    OCC team
4 // Copyright (C) CEA 2004
5
6 #ifndef GLVIEWER_GEOM_H
7 #define GLVIEWER_GEOM_H
8
9 #include "GLViewer.h"
10
11 #include <qrect.h>
12 #include <qvaluelist.h>
13 #include <qgl.h>
14 #include <math.h>
15
16
17 #ifdef WNT
18 #pragma warning( disable:4251 )
19 #endif
20
21 /*! Struct GLViewer_Pnt
22 * Substitution of QPoint for OpenGL
23 */
24
25 struct GLVIEWER_API GLViewer_Pnt
26 {
27 public:
28   GLViewer_Pnt() : myX( 0. ), myY( 0. ) {};
29   GLViewer_Pnt( GLfloat theX, GLfloat theY ) : myX( theX ), myY( theY ) {}
30   
31   GLfloat x() const { return myX; }
32   GLfloat y() const { return myY; }
33   void    setX( GLfloat theX ) { myX = theX; }
34   void    setY( GLfloat theY ) { myY = theY; }
35   void    setXY( GLfloat theX, GLfloat theY ) { myX = theX; myY = theY; }
36   void    move( GLfloat theDX, GLfloat theDY ) { myX += theDX; myY += theDY; }
37   
38 private:
39   GLfloat myX;
40   GLfloat myY;
41 };
42
43 typedef QValueList<GLViewer_Pnt> GLViewer_PntList;
44
45 /*! Class  GLViewer_Rect
46 *  Substitution of QRect for OpenGL
47 */
48
49 class GLVIEWER_API GLViewer_Rect
50 {
51 public:
52   GLViewer_Rect(): myLeft(0.0), myRight(0.0), myTop(0.0), myBottom(0.0){}
53   GLViewer_Rect( float theLeft, float theRight, float theTop, float theBottom )
54     : myLeft(theLeft), myRight(theRight), myTop(theTop), myBottom(theBottom) {}
55   GLViewer_Rect( QRect theRect ) {
56     myLeft = ( float )theRect.left(); myRight = ( float )theRect.right();
57     myTop = ( float )theRect.top(); myBottom = ( float )theRect.bottom(); }
58   
59   float       left() const { return myLeft; }
60   float       right() const { return myRight; }
61   float       top() const { return myTop; }
62   float       bottom() const { return myBottom; }
63
64   float       width() const { return fabs( myRight - myLeft ); }
65   float       height() const { return fabs( myTop - myBottom ); }
66   
67   void        setLeft( float theLeft ) { myLeft = theLeft; }
68   void        setRight( float theRight ) { myRight = theRight; }
69   void        setTop( float theTop ) { myTop = theTop; }
70   void        setBottom( float theBottom ) { myBottom = theBottom; }
71   
72   void        setCoords( float theLeft, float theRight, float theBottom, float theTop )
73   { myLeft = theLeft; myRight = theRight; myBottom = theBottom; myTop = theTop; }
74   
75   //! \warning This method translate only rect format
76   QRect       toQRect() { return QRect( ( int )myLeft, ( int )myBottom,
77                                         ( int )( myRight - myLeft ),
78                                         ( int )( myTop - myBottom ) ); }
79
80   //! On/off empty status
81   void        setIsEmpty( bool on ) { myIsEmpty = on; }
82   //! Checks empty status
83   bool        isEmpty() const { return myIsEmpty; }
84
85   //! Checks null status
86   bool        isNull() const { return myLeft == 0.0 && myRight == 0.0 && myBottom == 0.0 && myTop == 0.0; }
87   //! Checks valid status
88   bool        isValid() const { return ( myLeft < myRight && myBottom < myTop ); }
89
90   //! Checks staus of contains point 
91   bool        contains( GLViewer_Pnt pnt ) { return ( pnt.x() > left() &&
92                                                       pnt.x() < right() &&
93                                                       pnt.y() > bottom() &&
94                                                       pnt.y() < top() ); } 
95
96 protected:
97   float       myLeft;
98   float       myRight;
99   float       myTop;
100   float       myBottom;
101
102   bool        myIsEmpty;
103 };
104
105 /*! Class GLViewer_Segment
106 * Segment for 2d detection
107 */
108
109 class GLVIEWER_API GLViewer_Segment
110 {
111 public:
112   GLViewer_Segment( const GLViewer_Pnt& thePnt1, 
113                     const GLViewer_Pnt& thePnt2 );
114   
115   //! Ordinary segment construction
116   /*!Construction of a ray with given equation Ax + By + C = 0 */
117
118   GLViewer_Segment( const GLViewer_Pnt& thePnt, 
119                     const GLfloat theA, 
120                     const GLfloat theB,
121                     const GLfloat theC );
122   ~GLViewer_Segment();
123
124   bool              HasIntersection( const GLViewer_Segment& theOther ) const;
125   // Detects intersection with another segment or ray
126
127 private:
128   GLViewer_Pnt      myPnt1;
129   GLViewer_Pnt      myPnt2;
130   GLfloat           myA;
131   GLfloat           myB;
132   GLfloat           myC;
133 };
134
135 /*! Class  GLViewer_Poly
136 * Polygon for 2d detection
137 */
138
139 class GLVIEWER_API GLViewer_Poly 
140 {
141 public:
142   GLViewer_Poly( const GLViewer_PntList* thePoints );
143   virtual ~GLViewer_Poly();
144
145   //! Returns number of point
146   int               Count() const { return myPoints->count(); }
147
148   //! Returns true if a point lies inside this polygon
149   virtual bool      IsIn( const GLViewer_Pnt& thePnt ) const;
150
151   //! Returns true if a other polygon covers this polygon  
152   virtual bool      IsCovers( const GLViewer_Poly& thePoly ) const;
153
154   //! Likes the above function
155   virtual bool      IsCovers( const GLViewer_Rect& theRect ) const;
156   
157   // Returns true if intersection of this polygon with a segment or a ray not empty
158   virtual bool      HasIntersection( const GLViewer_Segment& theSegment ) const;
159
160
161 private:
162   GLViewer_PntList* myPoints;
163 };
164
165 #ifdef WNT
166 #pragma warning ( default:4251 )
167 #endif
168
169 #endif