1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // Author : OPEN CASCADE
23 //#include <GLViewerAfx.h>
25 #include "GLViewer_Geom.h"
27 #define FAR_POINT 1e10 // Value used as a "very distant" co-ordinate
28 #define TOLERANCE 1e-3
31 constructs a real segment bounded by two points
33 GLViewer_Segment::GLViewer_Segment( const GLViewer_Pnt& thePnt1,
34 const GLViewer_Pnt& thePnt2 )
38 myA = myPnt1.y() - myPnt2.y();
39 myB = myPnt2.x() - myPnt1.x();
40 myC = myPnt1.x() * myPnt2.y() - myPnt2.x() * myPnt1.y();
44 constructs a ray starting at <thePnt> and directed along positive X axis direction (or Y axis if vertical )
46 GLViewer_Segment::GLViewer_Segment( const GLViewer_Pnt& thePnt,
55 if ( fabs( myB ) < TOLERANCE )
56 myPnt2 = GLViewer_Pnt( myPnt1.x(), FAR_POINT );
58 myPnt2 = GLViewer_Pnt( FAR_POINT, - myA / myB * FAR_POINT - myC / myB );
62 destructor, does nothing
64 GLViewer_Segment::~GLViewer_Segment()
69 detects intersection with segment <theOther>
71 bool GLViewer_Segment::HasIntersection( const GLViewer_Segment& theOther ) const
74 GLfloat aDiv = myA * theOther.myB - myB * theOther.myA;
75 if ( fabs( aDiv ) > TOLERANCE )
77 GLfloat aX = ( myB * theOther.myC - theOther.myB * myC ) / aDiv;
78 GLfloat aX11 = myPnt1.x() > myPnt2.x() ? myPnt2.x() : myPnt1.x();
79 GLfloat aX12 = myPnt1.x() > myPnt2.x() ? myPnt1.x() : myPnt2.x();
80 GLfloat aX21 = theOther.myPnt1.x() > theOther.myPnt2.x() ? theOther.myPnt2.x() : theOther.myPnt1.x();
81 GLfloat aX22 = theOther.myPnt1.x() > theOther.myPnt2.x() ? theOther.myPnt1.x() : theOther.myPnt2.x();
83 GLfloat aY = ( myC * theOther.myA - theOther.myC * myA ) / aDiv;
84 GLfloat aY11 = myPnt1.y() > myPnt2.y() ? myPnt2.y() : myPnt1.y();
85 GLfloat aY12 = myPnt1.y() > myPnt2.y() ? myPnt1.y() : myPnt2.y();
86 GLfloat aY21 = theOther.myPnt1.y() > theOther.myPnt2.y() ? theOther.myPnt2.y() : theOther.myPnt1.y();
87 GLfloat aY22 = theOther.myPnt1.y() > theOther.myPnt2.y() ? theOther.myPnt1.y() : theOther.myPnt2.y();
89 if ( fabs( aX11 - aX12 ) > TOLERANCE )
90 aRes = aX11 < aX && aX < aX12;
92 aRes = aY11 < aY && aY < aY12;
96 if ( fabs( aX21 - aX22 ) > TOLERANCE )
97 aRes = aX21 < aX && aX < aX22;
99 aRes = aY21 < aY && aY < aY22;
107 constructs a closed polygon from the given ordered list of points
109 GLViewer_Poly::GLViewer_Poly( const GLViewer_PntList* thePoints )
110 : myPoints( (GLViewer_PntList*)thePoints )
115 destructor, <myPoints> mustn't be deleted here!
117 GLViewer_Poly::~GLViewer_Poly()
122 \return true if <thePnt> lies within this polygon
124 bool GLViewer_Poly::IsIn( const GLViewer_Pnt& thePnt ) const
129 //cout << thePnt.x() << endl;
130 //cout << thePnt.y() << endl << endl;
133 GLViewer_Segment aRay( thePnt, 0., 1., -thePnt.y() );
135 GLViewer_PntList::const_iterator it1 = myPoints->begin();
136 GLViewer_PntList::const_iterator it2 = myPoints->begin();
138 for ( ; it1 != myPoints->end(); ++it1, ++it2 )
140 if ( it2 == myPoints->end() )
141 it2 = myPoints->begin();
143 if ( aRay.HasIntersection( GLViewer_Segment( *it1, *it2 ) ) )
147 return ( aNbInter % 2 == 1 );
150 \return true if <thePnt> lies within this polygon
153 bool GLViewer_Poly::IsIn( const GLViewer_Pnt& thePnt, const float tolerance ) const
158 float x = thePnt.x();
159 float y = thePnt.y();
162 GLViewer_Pnt p1( x - tolerance, y - tolerance );
163 GLViewer_Pnt p2( x - tolerance, y + tolerance );
164 GLViewer_Pnt p3( x + tolerance, y - tolerance );
165 GLViewer_Pnt p4( x + tolerance, y + tolerance );
167 res = ( IsInPnt( thePnt ) ||
168 IsInPnt( p1 ) || IsInPnt( p2 ) || IsInPnt( p3 ) || IsInPnt( p4 ) );
174 \return true if <thePoly> covers this polygon
176 bool GLViewer_Poly::IsCovers( const GLViewer_Poly& thePoly ) const
178 if ( !myPoints || !thePoly.Count() )
181 GLViewer_PntList::const_iterator it = myPoints->begin();
183 for ( ; it != myPoints->end(); ++it )
185 if( !thePoly.IsIn( *it ) )
193 \return true if <theRect> covers this polygon
195 bool GLViewer_Poly::IsCovers( const GLViewer_Rect& theRect ) const
197 if ( !myPoints ) //needs check for <theRect>
200 GLViewer_PntList aList;
201 GLViewer_PntList::iterator it = aList.begin();
203 aList.insert( it, GLViewer_Pnt( theRect.left(), theRect.top() ) );
204 aList.insert( it, GLViewer_Pnt( theRect.right(), theRect.top() ) );
205 aList.insert( it, GLViewer_Pnt( theRect.right(), theRect.bottom() ) );
206 aList.insert( it, GLViewer_Pnt( theRect.left(), theRect.bottom() ) );
208 return IsCovers( GLViewer_Poly( &aList ) );
212 \return true if polygon has intersection with segment or ray
213 \param theSegment - segment to check intersection
215 bool GLViewer_Poly::HasIntersection( const GLViewer_Segment& theSegment ) const
221 GLViewer_PntList::const_iterator it1 = myPoints->begin();
222 GLViewer_PntList::const_iterator it2 = myPoints->begin();
224 for ( ; !aRes && it1 != myPoints->end(); ++it1, ++it2 )
226 if ( it2 == myPoints->end() )
227 it2 = myPoints->begin();
229 aRes = theSegment.HasIntersection( GLViewer_Segment( *it1, *it2 ) );