BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Triangle_2_Triangle_2_do_intersect.h
Go to the documentation of this file.
00001 // Copyright (c) 2003  INRIA Sophia-Antipolis (France).
00002 // All rights reserved.
00003 //
00004 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public License as
00006 // published by the Free Software Foundation; version 2.1 of the License.
00007 // See the file LICENSE.LGPL distributed with CGAL.
00008 //
00009 // Licensees holding a valid commercial license may use this file in
00010 // accordance with the commercial license agreement provided with the software.
00011 //
00012 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00013 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00014 //
00015 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Intersections_2/include/CGAL/Triangle_2_Triangle_2_do_intersect.h $
00016 // $Id: Triangle_2_Triangle_2_do_intersect.h 39776 2007-08-08 15:15:20Z spion $
00017 // 
00018 //
00019 // Author(s)     : Philippe Guigue
00020 
00021 #ifndef CGAL_TRIANGLE_2_TRIANGLE_2_DO_INTERSECT_H
00022 #define CGAL_TRIANGLE_2_TRIANGLE_2_DO_INTERSECT_H
00023 
00024 #include <CGAL/Triangle_2.h>
00025 
00026 CGAL_BEGIN_NAMESPACE
00027 
00028 
00029 
00030 namespace CGALi {
00031 
00032 template <class K>
00033 bool intersection_test_vertex(const typename K::Point_2 *  P1, 
00034                               const typename K::Point_2 *  Q1, 
00035                               const typename K::Point_2 *  R1,
00036                               const typename K::Point_2 *  P2, 
00037                               const typename K::Point_2 *  Q2, 
00038                               const typename K::Point_2 *  R2,
00039                               const K & k ){
00040   
00041   
00042   CGAL_kernel_precondition( k.orientation_2_object() (*P1,*Q1,*R1)
00043                             == POSITIVE);
00044   CGAL_kernel_precondition( k.orientation_2_object() (*P2,*Q2,*R2)
00045                             == POSITIVE);
00046 
00047   typename K::Orientation_2 orientation = 
00048     k.orientation_2_object();
00049   
00050   if (orientation(*R2,*P2,*Q1) != NEGATIVE) {
00051     if (orientation(*R2,*Q2,*Q1) != POSITIVE) {
00052       if (orientation(*P1,*P2,*Q1) == POSITIVE)
00053         return orientation(*P1,*Q2,*Q1) != POSITIVE;
00054       return   orientation(*P1,*P2,*R1) != NEGATIVE
00055         &&  orientation(*Q1,*R1,*P2) != NEGATIVE  ;
00056     }
00057     return  orientation(*P1,*Q2,*Q1) != POSITIVE
00058       && orientation(*R2,*Q2,*R1) != POSITIVE
00059       &&  orientation(*Q1,*R1,*Q2) != NEGATIVE ;
00060   }
00061   
00062   if (orientation(*R2,*P2,*R1) != NEGATIVE) { 
00063     if (orientation(*Q1,*R1,*R2) != NEGATIVE)
00064       return orientation(*P1,*P2,*R1) != NEGATIVE;
00065     return orientation(*Q1,*R1,*Q2) != NEGATIVE
00066       &&  orientation(*R2,*R1,*Q2) != NEGATIVE;
00067   }
00068   
00069   return false; 
00070   
00071 }
00072 
00073 
00074 template <class K>
00075 bool intersection_test_edge(const typename K::Point_2 *  P1, 
00076                             const typename K::Point_2 *  Q1, 
00077                             const typename K::Point_2 *  R1,
00078                             const typename K::Point_2 *  P2, 
00079                             const typename K::Point_2 *  Q2, 
00080                             const typename K::Point_2 *  R2,
00081                             const K & k ){
00082   
00083   
00084   CGAL_kernel_precondition( k.orientation_2_object() (*P1,*Q1,*R1)
00085                             == POSITIVE);
00086   CGAL_kernel_precondition( k.orientation_2_object() (*P2,*Q2,*R2)
00087                             == POSITIVE);
00088   
00089   typename K::Orientation_2 orientation = 
00090     k.orientation_2_object();
00091   
00092   if (orientation(*R2,*P2,*Q1) != NEGATIVE) { 
00093     if (orientation(*P1,*P2,*Q1) != NEGATIVE) 
00094       return orientation(*P1,*Q1,*R2) != NEGATIVE;
00095     return orientation(*Q1,*R1,*P2) != NEGATIVE
00096       && orientation(*R1,*P1,*P2) != NEGATIVE;
00097   } 
00098 
00099   if (orientation(*R2,*P2,*R1) != NEGATIVE) 
00100     return  orientation(*P1,*P2,*R1) != NEGATIVE 
00101       && ( orientation(*P1,*R1,*R2) != NEGATIVE
00102            || orientation(*Q1,*R1,*R2) != NEGATIVE ) ;
00103   
00104   return false; 
00105   
00106 }
00107 
00108 
00109 template <class K>
00110 bool do_intersect(const typename K::Triangle_2 &t1, 
00111                   const typename K::Triangle_2 &t2,
00112                   const K & k ){
00113   
00114   CGAL_kernel_precondition( ! k.is_degenerate_2_object() (t1) );
00115   CGAL_kernel_precondition( ! k.is_degenerate_2_object() (t2) );
00116   
00117   typename K::Construct_vertex_2 vertex_on =
00118     k.construct_vertex_2_object();
00119 
00120   typename K::Orientation_2 orientation = 
00121     k.orientation_2_object();
00122 
00123 
00124   typedef typename K::Point_2 Point_2;
00125   
00126   const Point_2 & P1 = vertex_on(t1,0);
00127   const Point_2 & Q1 = vertex_on(t1,1);
00128   const Point_2 & R1 = vertex_on(t1,2);
00129   const Point_2 & P2 = vertex_on(t2,0);
00130   const Point_2 & Q2 = vertex_on(t2,1);
00131   const Point_2 & R2 = vertex_on(t2,2);
00132 
00133   const Point_2 * p1 = &P1;
00134   const Point_2 * q1 = &Q1;
00135   const Point_2 * r1 = &R1;
00136 
00137   const Point_2 * p2 = &P2;
00138   const Point_2 * q2 = &Q2;
00139   const Point_2 * r2 = &R2;
00140 
00141   if ( orientation(P1,Q1,R1) != POSITIVE ) {
00142     q1 = &R1;;
00143     r1 = &Q1;
00144   }
00145   
00146   if ( orientation(P2,Q2,R2) != POSITIVE ) {
00147     q2 = &R2;
00148     r2 = &Q2;
00149   }
00150 
00151   if ( orientation(*p2,*q2,*p1) != NEGATIVE ) {
00152     if ( orientation(*q2,*r2,*p1) != NEGATIVE ) { 
00153       if ( orientation(*r2,*p2,*p1) != NEGATIVE ) return true;
00154       return CGALi::intersection_test_edge(p1,q1,r1,p2,q2,r2,k);
00155     } 
00156     if ( orientation(*r2,*p2,*p1) != NEGATIVE ) 
00157       return CGALi::intersection_test_edge(p1,q1,r1,r2,p2,q2,k);
00158     return CGALi::intersection_test_vertex(p1,q1,r1,p2,q2,r2,k);
00159     
00160   }
00161   
00162   if ( orientation(*q2,*r2,*p1) != NEGATIVE ) {
00163     if ( orientation(*r2,*p2,*p1) != NEGATIVE ) 
00164       return CGALi::intersection_test_edge(p1,q1,r1,q2,r2,p2,k);
00165     return CGALi::intersection_test_vertex(p1,q1,r1,q2,r2,p2,k);
00166   }
00167   return CGALi::intersection_test_vertex(p1,q1,r1,r2,p2,q2,k);
00168   
00169 }
00170 
00171 } // namespace CGALi
00172 
00173 
00174 
00175 
00176 template <class K>
00177 inline bool do_intersect(const Triangle_2<K> &t1, 
00178                          const Triangle_2<K> &t2)
00179 {
00180   typedef typename K::Do_intersect_2 Do_intersect;
00181   return Do_intersect()(t1,t2);
00182 }
00183 
00184 CGAL_END_NAMESPACE
00185 
00186 #endif //CGAL_TRIANGLE_2_TRIANGLE_2_DO_INTERSECT_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines