Salome HOME
cc55453bb57f8d8bf7e7065abd1a538b166e0bf1
[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 // Class:   GLViewer_Pnt
22 // Descr:   Substitution of QPoint for OpenGL
23
24 struct GLVIEWER_API GLViewer_Pnt
25 {
26 public:
27   GLViewer_Pnt() : myX( 0. ), myY( 0. ) {};
28   GLViewer_Pnt( GLfloat theX, GLfloat theY ) : myX( theX ), myY( theY ) {}
29   
30   GLfloat x() const { return myX; }
31   GLfloat y() const { return myY; }
32   void    setX( GLfloat theX ) { myX = theX; }
33   void    setY( GLfloat theY ) { myY = theY; }
34   void    setXY( GLfloat theX, GLfloat theY ) { myX = theX; myY = theY; }
35   void    move( GLfloat theDX, GLfloat theDY ) { myX += theDX; myY += theDY; }
36   
37 private:
38   GLfloat myX;
39   GLfloat myY;
40 };
41
42 typedef QValueList<GLViewer_Pnt> GLViewer_PntList;
43
44 // Class:   GLViewer_Rect
45 // Descr:   Substitution of QRect for OpenGL
46
47 class GLVIEWER_API GLViewer_Rect
48 {
49 public:
50   GLViewer_Rect(): myLeft(0.0), myRight(0.0), myTop(0.0), myBottom(0.0){}
51   GLViewer_Rect( float theLeft, float theRight, float theTop, float theBottom )
52     : myLeft(theLeft), myRight(theRight), myTop(theTop), myBottom(theBottom) {}
53   GLViewer_Rect( QRect theRect ) {
54     myLeft = ( float )theRect.left(); myRight = ( float )theRect.right();
55     myTop = ( float )theRect.top(); myBottom = ( float )theRect.bottom(); }
56   
57   float       left() const { return myLeft; }
58   float       right() const { return myRight; }
59   float       top() const { return myTop; }
60   float       bottom() const { return myBottom; }
61
62   float       width() const { return fabs( myRight - myLeft ); }
63   float       height() const { return fabs( myTop - myBottom ); }
64   
65   void        setLeft( float theLeft ) { myLeft = theLeft; }
66   void        setRight( float theRight ) { myRight = theRight; }
67   void        setTop( float theTop ) { myTop = theTop; }
68   void        setBottom( float theBottom ) { myBottom = theBottom; }
69   
70   void        setCoords( float theLeft, float theRight, float theBottom, float theTop )
71   { myLeft = theLeft; myRight = theRight; myBottom = theBottom; myTop = theTop; }
72   
73   QRect       toQRect() { return QRect( ( int )myLeft, ( int )myBottom,
74                                         ( int )( myRight - myLeft ),
75                                         ( int )( myTop - myBottom ) ); }
76
77   void        setIsEmpty( bool on ) { myIsEmpty = on; }
78   bool        isEmpty() const { return myIsEmpty; }
79
80   bool        isNull() const { return myLeft == 0.0 && myRight == 0.0 && myBottom == 0.0 && myTop == 0.0; }
81   bool        isValid() const { return ( myLeft < myRight && myBottom < myTop ); }
82
83   bool        contains( GLViewer_Pnt pnt ) { return ( pnt.x() > left() &&
84                                                       pnt.x() < right() &&
85                                                       pnt.y() > bottom() &&
86                                                       pnt.y() < top() ); } 
87
88 protected:
89   float       myLeft;
90   float       myRight;
91   float       myTop;
92   float       myBottom;
93
94   bool        myIsEmpty;
95 };
96
97 // Class:   GLViewer_Segment
98 // Descr:   Segment for 2d detection
99
100 class GLVIEWER_API GLViewer_Segment
101 {
102 public:
103   GLViewer_Segment( const GLViewer_Pnt& thePnt1, 
104                     const GLViewer_Pnt& thePnt2 );
105   // Ordinary segment construction
106
107   GLViewer_Segment( const GLViewer_Pnt& thePnt, 
108                     const GLfloat theA, 
109                     const GLfloat theB,
110                     const GLfloat theC );
111   // Construction of a ray with given equation Ax + By + C = 0
112
113   ~GLViewer_Segment();
114
115   bool              HasIntersection( const GLViewer_Segment& theOther ) const;
116   // Detects intersection with another segment or ray
117
118 private:
119   GLViewer_Pnt      myPnt1;
120   GLViewer_Pnt      myPnt2;
121   GLfloat           myA;
122   GLfloat           myB;
123   GLfloat           myC;
124 };
125
126 // Class:   GLViewer_Poly
127 // Descr:   Polygon for 2d detection
128
129 class GLVIEWER_API GLViewer_Poly 
130 {
131 public:
132   GLViewer_Poly( const GLViewer_PntList* thePoints );
133   virtual ~GLViewer_Poly();
134
135   int               Count() const { return myPoints->count(); }
136
137   virtual bool      IsIn( const GLViewer_Pnt& thePnt ) const;
138   //virtual bool      IsIn( const GLViewer_Pnt& thePnt, const float tolerance = 0 ) const;
139   // Detects if a point lies inside this polygon
140   
141   virtual bool      IsCovers( const GLViewer_Poly& thePoly ) const;
142   // Detect if a other polygon covers this polygon
143
144   virtual bool      IsCovers( const GLViewer_Rect& theRect ) const;
145   // likes the above function
146
147   virtual bool      HasIntersection( const GLViewer_Segment& theSegment ) const;
148   // Detects intersection of this polygon with a segment or a ray
149
150 private:
151   GLViewer_PntList* myPoints;
152 };
153
154 #ifdef WNT
155 #pragma warning ( default:4251 )
156 #endif
157
158 #endif