BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Segment_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/Segment_2_Triangle_2_intersection.h $
00019 // $Id: Segment_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_SEGMENT_2_TRIANGLE_2_INTERSECTION_H
00026 #define CGAL_SEGMENT_2_TRIANGLE_2_INTERSECTION_H
00027 
00028 #include <CGAL/Segment_2.h>
00029 #include <CGAL/Triangle_2.h>
00030 #include <CGAL/Point_2.h>
00031 #include <CGAL/Line_2.h>
00032 #include <CGAL/kernel_assertions.h>
00033 #include <CGAL/number_utils.h>
00034 #include <CGAL/Straight_2.h>
00035 #include <CGAL/Object.h>
00036 
00037 CGAL_BEGIN_NAMESPACE
00038 
00039 namespace CGALi {
00040 
00041 template <class K>
00042 class Segment_2_Triangle_2_pair {
00043 public:
00044     enum Intersection_results {NO_INTERSECTION, POINT, SEGMENT};
00045     Segment_2_Triangle_2_pair(typename K::Segment_2 const *seg,
00046                               typename K::Triangle_2 const *trian)
00047       : _seg(seg), _trian(trian), _known(false) {}
00048 
00049     Intersection_results intersection_type() const;
00050 
00051     typename K::Point_2    intersection_point() const;
00052     typename K::Segment_2  intersection_segment() const;
00053 protected:
00054     typename K::Segment_2 const *  _seg;
00055     typename K::Triangle_2 const * _trian;
00056     mutable bool                       _known;
00057     mutable Intersection_results       _result;
00058     mutable typename K::Point_2            _intersection_point;
00059     mutable typename K::Point_2            _other_point;
00060 };
00061 
00062 template <class K>
00063 inline bool do_intersect(
00064     const typename K::Segment_2 &p1,
00065     const typename K::Triangle_2 &p2,
00066     const K&)
00067 {
00068     typedef Segment_2_Triangle_2_pair<K> pair_t;
00069     pair_t pair(&p1, &p2);
00070     return pair.intersection_type() != pair_t::NO_INTERSECTION;
00071 }
00072 
00073 
00074 
00075 
00076 
00077 template <class K>
00078 typename Segment_2_Triangle_2_pair<K>::Intersection_results
00079 Segment_2_Triangle_2_pair<K>::intersection_type() const
00080 {
00081     if (_known)
00082         return _result;
00083 // The non const this pointer is used to cast away const.
00084     _known = true;
00085     Straight_2_<K> straight(*_seg);
00086     typedef typename K::Line_2 Line_2;
00087 
00088 Line_2 l(_trian->vertex(0), _trian->vertex(1));
00089 if (l.oriented_side(_trian->vertex(2)) == ON_POSITIVE_SIDE) {
00090         straight.cut_right_off(
00091             Line_2(_trian->vertex(0), _trian->vertex(1)));
00092         straight.cut_right_off(
00093             Line_2(_trian->vertex(1), _trian->vertex(2)));
00094         straight.cut_right_off(
00095             Line_2(_trian->vertex(2), _trian->vertex(0)));
00096     } else {
00097         straight.cut_right_off(
00098             Line_2(_trian->vertex(2), _trian->vertex(1)));
00099         straight.cut_right_off(
00100             Line_2(_trian->vertex(1), _trian->vertex(0)));
00101         straight.cut_right_off(
00102             Line_2(_trian->vertex(0), _trian->vertex(2)));
00103     }
00104     switch (straight.current_state()) {
00105     case Straight_2_<K>::EMPTY:
00106         _result = NO_INTERSECTION;
00107         return _result;
00108     case Straight_2_<K>::POINT: {
00109         straight.current(_intersection_point);
00110         _result = POINT;
00111         return _result;
00112         }
00113     case Straight_2_<K>::SEGMENT: {
00114         typename K::Segment_2 seg;
00115         straight.current(seg);
00116         _intersection_point = seg.source();
00117         _other_point = seg.target();
00118         _result = SEGMENT;
00119         return _result;
00120         }
00121     default:  // should not happen.
00122         CGAL_kernel_assertion_msg(false, "Internal CGAL error.");
00123         _result = NO_INTERSECTION;
00124         return _result;
00125     }
00126 }
00127 
00128 
00129 template <class K>
00130 typename K::Point_2
00131 Segment_2_Triangle_2_pair<K>::
00132 intersection_point() const
00133 {
00134     if (!_known)
00135         intersection_type();
00136     CGAL_kernel_assertion(_result == POINT);
00137     return _intersection_point;
00138 }
00139 
00140 template <class K>
00141 typename K::Segment_2
00142 Segment_2_Triangle_2_pair<K>::
00143 intersection_segment() const
00144 {
00145   typedef typename K::Segment_2 Segment_2; 
00146     if (!_known)
00147         intersection_type();
00148     CGAL_kernel_assertion(_result == SEGMENT);
00149     return Segment_2(_intersection_point, _other_point);
00150 }
00151 
00152 
00153 
00154 
00155 template <class K>
00156 Object
00157 intersection(const typename K::Segment_2 &seg, 
00158              const typename K::Triangle_2&tr,
00159              const K&)
00160 {
00161     typedef Segment_2_Triangle_2_pair<K> is_t;
00162     is_t ispair(&seg, &tr);
00163     switch (ispair.intersection_type()) {
00164     case is_t::NO_INTERSECTION:
00165     default:
00166         return Object();
00167     case is_t::POINT:
00168         return make_object(ispair.intersection_point());
00169     case is_t::SEGMENT:
00170         return make_object(ispair.intersection_segment());
00171     }
00172 }
00173 
00174 
00175 template <class K>
00176 Object
00177 intersection(const typename K::Triangle_2&tr,
00178              const typename K::Segment_2 &seg, 
00179              const K& k)
00180 {
00181   return CGALi::intersection(seg, tr, k);
00182 }
00183 
00184 
00185 template <class K>
00186 inline bool do_intersect(
00187     const typename K::Triangle_2 &p1,
00188     const typename K::Segment_2 &p2,
00189     const K&)
00190 {
00191     typedef Segment_2_Triangle_2_pair<K> pair_t;
00192     pair_t pair(&p2, &p1);
00193     return pair.intersection_type() != pair_t::NO_INTERSECTION;
00194 }
00195 
00196 } // namespace CGALi
00197 
00198 template <class K>
00199 inline Object
00200 intersection(const Segment_2<K> &seg, const Triangle_2<K> &tr)
00201 {
00202   typedef typename K::Intersect_2 Intersect;
00203   return Intersect()(seg, tr);
00204 }
00205 
00206 template <class K>
00207 inline Object
00208 intersection(const Triangle_2<K> &tr, const Segment_2<K> &seg)
00209 {
00210   typedef typename K::Intersect_2 Intersect;
00211     return Intersect()(seg, tr);
00212 }
00213 
00214 template <class K>
00215 inline bool
00216 do_intersect(const Segment_2<K> &seg, const Triangle_2<K> &tr)
00217 {
00218   typedef typename K::Do_intersect_2 Do_intersect;
00219   return Do_intersect()(seg, tr);
00220 }
00221 
00222 template <class K>
00223 inline bool
00224 do_intersect(const Triangle_2<K> &tr, const Segment_2<K> &seg)
00225 {
00226   typedef typename K::Do_intersect_2 Do_intersect;
00227   return Do_intersect()(seg, tr);
00228 }
00229 
00230 CGAL_END_NAMESPACE
00231 
00232 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines