BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Point_2_Triangle_2_intersection.h
Go to the documentation of this file.
00001 // Copyright (c) 2000  Utrecht University (The Netherlands),
00002 // ETH Zurich (Switzerland), Freie Universitaet Berlin (Germany),
00003 // INRIA Sophia-Antipolis (France), Martin-Luther-University Halle-Wittenberg
00004 // (Germany), Max-Planck-Institute Saarbruecken (Germany), RISC Linz (Austria),
00005 // and Tel-Aviv University (Israel).  All rights reserved.
00006 //
00007 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public License as
00009 // published by the Free Software Foundation; version 2.1 of the License.
00010 // See the file LICENSE.LGPL distributed with CGAL.
00011 //
00012 // Licensees holding a valid commercial license may use this file in
00013 // accordance with the commercial license agreement provided with the software.
00014 //
00015 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00016 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00017 //
00018 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Intersections_2/include/CGAL/Point_2_Triangle_2_intersection.h $
00019 // $Id: Point_2_Triangle_2_intersection.h 45046 2008-08-20 13:49:31Z spion $
00020 // 
00021 //
00022 // Author(s)     : Geert-Jan Giezeman
00023 
00024 
00025 #ifndef CGAL_POINT_2_TRIANGLE_2_INTERSECTION_H
00026 #define CGAL_POINT_2_TRIANGLE_2_INTERSECTION_H
00027 
00028 #include <CGAL/Point_2.h>
00029 #include <CGAL/Triangle_2.h>
00030 #include <CGAL/Line_2.h>
00031 #include <CGAL/kernel_assertions.h>
00032 #include <CGAL/number_utils.h>
00033 #include <CGAL/Straight_2.h>
00034 #include <CGAL/Object.h>
00035 
00036 CGAL_BEGIN_NAMESPACE
00037 
00038 namespace CGALi {
00039 
00040 template <class K>
00041 class Point_2_Triangle_2_pair {
00042 public:
00043     enum Intersection_results {NO_INTERSECTION, POINT};
00044     Point_2_Triangle_2_pair(typename K::Point_2 const *pt,
00045                             typename K::Triangle_2 const *trian)
00046             : _pt(pt), _trian(trian), _known(false) {}
00047 
00048     Intersection_results intersection_type() const;
00049 
00050     typename K::Point_2  intersection_point() const;
00051 protected:
00052     typename K::Point_2 const *    _pt;
00053     typename K::Triangle_2 const * _trian;
00054     mutable bool                   _known;
00055     mutable Intersection_results   _result;
00056     mutable typename K::Point_2    _intersection_point;
00057     mutable typename K::Point_2    _other_point;
00058 };
00059 
00060 template <class K>
00061 inline bool do_intersect(const typename K::Point_2 &p1,
00062                          const typename K::Triangle_2 &p2,
00063                          const K&)
00064 {
00065   typedef Point_2_Triangle_2_pair<K> pair_t;
00066   pair_t pair(&p1, &p2);
00067   return pair.intersection_type() != pair_t::NO_INTERSECTION;
00068 }
00069 
00070 template <class K>
00071 inline bool do_intersect(const typename K::Triangle_2 &p2,
00072                          const typename K::Point_2 &p1,
00073                          const K& k)
00074 {
00075   return CGALi::do_intersect(p1, p2, k);
00076 }
00077 
00078 
00079 template <class K>
00080 typename Point_2_Triangle_2_pair<K>::Intersection_results
00081 Point_2_Triangle_2_pair<K>::intersection_type() const
00082 {
00083     typedef typename K::Line_2 line_t;
00084     if (_known)
00085         return _result;
00086 // The non const this pointer is used to cast away const.
00087     _known = true;
00088     if (_trian->has_on_unbounded_side(*_pt)) {
00089         _result = NO_INTERSECTION;
00090     } else {
00091         _result = POINT;
00092     }
00093     return _result;
00094 /*
00095     line_t l(_trian->vertex(0), _trian->vertex(1));
00096     if (l.has_on_positive_side(_trian->vertex(2))) {
00097         for (int i=0; i<3; i++) {
00098             if (line_t(_trian->vertex(i), _trian->vertex(i+1)).
00099                                 has_on_negative_side(*_pt)) {
00100                 _result = NO_INTERSECTION;
00101                 return _result;
00102             }
00103         }
00104     } else {
00105         for (int i=0; i<3; i++)
00106             if(line_t(_trian->vertex(i), _trian->vertex(i-1)).
00107                                 has_on_negative_side(*_pt)){
00108                 _result = NO_INTERSECTION;
00109                 return _result;
00110             }
00111     }
00112 */
00113 }
00114 
00115 
00116 
00117 template <class K>
00118 typename K::Point_2
00119 Point_2_Triangle_2_pair<K>::
00120 intersection_point() const
00121 {
00122     if (!_known)
00123         intersection_type();
00124     CGAL_kernel_assertion(_result == POINT);
00125     return *_pt;
00126 }
00127 
00128 
00129 
00130 template <class K>
00131 Object
00132 intersection(const typename K::Point_2 &pt, 
00133              const typename K::Triangle_2 &tr,
00134              const K&)
00135 {
00136     typedef Point_2_Triangle_2_pair<K> is_t;
00137     is_t ispair(&pt, &tr);
00138     switch (ispair.intersection_type()) {
00139     case is_t::NO_INTERSECTION:
00140     default:
00141         return Object();
00142     case is_t::POINT:
00143         return make_object(pt);
00144     }
00145 }
00146 
00147 template <class K>
00148 inline
00149 Object
00150 intersection(const typename K::Triangle_2 &tr,
00151              const typename K::Point_2 &pt, 
00152              const K&k)
00153 {
00154   return CGALi::intersection(pt, tr, k);
00155 }
00156 
00157 } // namespace CGALi
00158 
00159 
00160 template <class K>
00161 inline 
00162 bool
00163 do_intersect(const Triangle_2<K> &tr, const Point_2<K> &pt)
00164 {
00165   typedef typename K::Do_intersect_2 Do_intersect;
00166   return Do_intersect()(pt, tr);
00167 }
00168 
00169 template <class K>
00170 inline 
00171 bool 
00172 do_intersect(const Point_2<K> &pt, const Triangle_2<K> &tr)
00173 {
00174   typedef typename K::Do_intersect_2 Do_intersect;
00175   return Do_intersect()(pt, tr);
00176 }
00177 
00178 template <class K>
00179 inline Object
00180 intersection(const Triangle_2<K> &tr, const Point_2<K> &pt)
00181 {
00182   typedef typename K::Intersect_2 Intersect;
00183   return Intersect()(pt, tr);
00184 }
00185 
00186 template <class K>
00187 inline Object
00188 intersection(const Point_2<K> &pt, const Triangle_2<K> &tr)
00189 {
00190   typedef typename K::Intersect_2 Intersect;
00191   return Intersect()(pt, tr);
00192 }
00193 
00194 CGAL_END_NAMESPACE
00195 
00196 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines