1 /***************************************************************************
2 ** Class: GLViewer_Geom
5 ** Created: UI team, 16.11.04
6 ****************************************************************************/
8 //#include <GLViewerAfx.h>
9 #include <GLViewer_Geom.h>
11 #define FAR_POINT 1e10 // Value used as a "very distant" co-ordinate
12 #define TOLERANCE 1e-3
14 //================================================================
15 // Function : GLViewer_Segment
16 // Purpose : constructs a real segment bounded by two points
17 //================================================================
18 GLViewer_Segment::GLViewer_Segment( const GLViewer_Pnt& thePnt1,
19 const GLViewer_Pnt& thePnt2 )
23 myA = myPnt1.y() - myPnt2.y();
24 myB = myPnt2.x() - myPnt1.x();
25 myC = myPnt1.x() * myPnt2.y() - myPnt2.x() * myPnt1.y();
28 //================================================================
29 // Function : GLViewer_Segment
30 // Purpose : constructs a ray starting at <thePnt> and directed
31 // along positive X axis direction (or Y axis if vertical )
32 //================================================================
33 GLViewer_Segment::GLViewer_Segment( const GLViewer_Pnt& thePnt,
42 if ( fabs( myB ) < TOLERANCE )
43 myPnt2 = GLViewer_Pnt( myPnt1.x(), FAR_POINT );
45 myPnt2 = GLViewer_Pnt( FAR_POINT, - myA / myB * FAR_POINT - myC / myB );
48 //================================================================
49 // Function : GLViewer_Segment
50 // Purpose : destructor, does nothing
51 //================================================================
52 GLViewer_Segment::~GLViewer_Segment()
56 //================================================================
57 // Function : HasIntersection
58 // Purpose : detects intersection with segment <theOther>
59 //================================================================
60 bool GLViewer_Segment::HasIntersection( const GLViewer_Segment& theOther ) const
63 GLfloat aDiv = myA * theOther.myB - myB * theOther.myA;
64 if ( fabs( aDiv ) > TOLERANCE )
66 GLfloat aX = ( myB * theOther.myC - theOther.myB * myC ) / aDiv;
67 GLfloat aX11 = myPnt1.x() > myPnt2.x() ? myPnt2.x() : myPnt1.x();
68 GLfloat aX12 = myPnt1.x() > myPnt2.x() ? myPnt1.x() : myPnt2.x();
69 GLfloat aX21 = theOther.myPnt1.x() > theOther.myPnt2.x() ? theOther.myPnt2.x() : theOther.myPnt1.x();
70 GLfloat aX22 = theOther.myPnt1.x() > theOther.myPnt2.x() ? theOther.myPnt1.x() : theOther.myPnt2.x();
72 GLfloat aY = ( myC * theOther.myA - theOther.myC * myA ) / aDiv;
73 GLfloat aY11 = myPnt1.y() > myPnt2.y() ? myPnt2.y() : myPnt1.y();
74 GLfloat aY12 = myPnt1.y() > myPnt2.y() ? myPnt1.y() : myPnt2.y();
75 GLfloat aY21 = theOther.myPnt1.y() > theOther.myPnt2.y() ? theOther.myPnt2.y() : theOther.myPnt1.y();
76 GLfloat aY22 = theOther.myPnt1.y() > theOther.myPnt2.y() ? theOther.myPnt1.y() : theOther.myPnt2.y();
78 if ( fabs( aX11 - aX12 ) > TOLERANCE )
79 aRes = aX11 < aX && aX < aX12;
81 aRes = aY11 < aY && aY < aY12;
85 if ( fabs( aX21 - aX22 ) > TOLERANCE )
86 aRes = aX21 < aX && aX < aX22;
88 aRes = aY21 < aY && aY < aY22;
95 //================================================================
96 // Function : GLViewer_Poly
97 // Purpose : constructs a closed polygon from the given ordered list of points
98 //================================================================
99 GLViewer_Poly::GLViewer_Poly( const GLViewer_PntList* thePoints )
100 : myPoints( (GLViewer_PntList*)thePoints )
104 //================================================================
105 // Function : ~GLViewer_Poly
106 // Purpose : destructor, <myPoints> mustn't be deleted here!
107 //================================================================
108 GLViewer_Poly::~GLViewer_Poly()
112 //================================================================
114 // Purpose : returns true if <thePnt> lies within this polygon
115 //================================================================
116 bool GLViewer_Poly::IsIn( const GLViewer_Pnt& thePnt ) const
121 //cout << thePnt.x() << endl;
122 //cout << thePnt.y() << endl << endl;
125 GLViewer_Segment aRay( thePnt, 0., 1., -thePnt.y() );
127 GLViewer_PntList::const_iterator it1 = myPoints->begin();
128 GLViewer_PntList::const_iterator it2 = myPoints->begin();
130 for ( ; it1 != myPoints->end(); ++it1, ++it2 )
132 if ( it2 == myPoints->end() )
133 it2 = myPoints->begin();
135 if ( aRay.HasIntersection( GLViewer_Segment( *it1, *it2 ) ) )
139 return ( aNbInter % 2 == 1 );
142 //================================================================
144 // Purpose : returns true if <thePnt> lies within this polygon
145 //================================================================
146 bool GLViewer_Poly::IsIn( const GLViewer_Pnt& thePnt, const float tolerance ) const
151 float x = thePnt.x();
152 float y = thePnt.y();
155 GLViewer_Pnt p1( x - tolerance, y - tolerance );
156 GLViewer_Pnt p2( x - tolerance, y + tolerance );
157 GLViewer_Pnt p3( x + tolerance, y - tolerance );
158 GLViewer_Pnt p4( x + tolerance, y + tolerance );
160 res = ( IsInPnt( thePnt ) ||
161 IsInPnt( p1 ) || IsInPnt( p2 ) || IsInPnt( p3 ) || IsInPnt( p4 ) );
166 //================================================================
167 // Function : IsCovers
168 // Purpose : returns true if <thePoly> covers this polygon
169 //================================================================
170 bool GLViewer_Poly::IsCovers( const GLViewer_Poly& thePoly ) const
172 if ( !myPoints || !thePoly.Count() )
175 GLViewer_PntList::const_iterator it = myPoints->begin();
177 for ( ; it != myPoints->end(); ++it )
179 if( !thePoly.IsIn( *it ) )
186 //================================================================
187 // Function : IsCovers
188 // Purpose : returns true if <theRect> covers this polygon
189 //================================================================
190 bool GLViewer_Poly::IsCovers( const GLViewer_Rect& theRect ) const
192 if ( !myPoints ) //needs check for <theRect>
195 GLViewer_PntList aList;
196 GLViewer_PntList::iterator it = aList.begin();
198 aList.insert( it, GLViewer_Pnt( theRect.left(), theRect.top() ) );
199 aList.insert( it, GLViewer_Pnt( theRect.right(), theRect.top() ) );
200 aList.insert( it, GLViewer_Pnt( theRect.right(), theRect.bottom() ) );
201 aList.insert( it, GLViewer_Pnt( theRect.left(), theRect.bottom() ) );
203 return IsCovers( GLViewer_Poly( &aList ) );
206 //================================================================
207 // Function : HasIntersection
208 // Purpose : looks for any
209 //================================================================
210 bool GLViewer_Poly::HasIntersection( const GLViewer_Segment& theSegment ) const
216 GLViewer_PntList::const_iterator it1 = myPoints->begin();
217 GLViewer_PntList::const_iterator it2 = myPoints->begin();
219 for ( ; !aRes && it1 != myPoints->end(); ++it1, ++it2 )
221 if ( it2 == myPoints->end() )
222 it2 = myPoints->begin();
224 aRes = theSegment.HasIntersection( GLViewer_Segment( *it1, *it2 ) );