BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Segment_2_Line_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_Line_2_intersection.h $
00019 // $Id: Segment_2_Line_2_intersection.h 45356 2008-09-07 15:09:56Z spion $
00020 // 
00021 //
00022 // Author(s)     : Geert-Jan Giezeman
00023 
00024 
00025 #ifndef CGAL_SEGMENT_2_LINE_2_INTERSECTION_H
00026 #define CGAL_SEGMENT_2_LINE_2_INTERSECTION_H
00027 
00028 #include <CGAL/Line_2.h>
00029 #include <CGAL/Segment_2.h>
00030 #include <CGAL/Point_2.h>
00031 #include <CGAL/kernel_assertions.h>
00032 #include <CGAL/number_utils.h>
00033 #include <CGAL/Object.h>
00034 #include <CGAL/Line_2_Line_2_intersection.h>
00035 
00036 CGAL_BEGIN_NAMESPACE
00037 
00038 namespace CGALi {
00039 
00040 template <class K>
00041 class Segment_2_Line_2_pair {
00042 public:
00043     enum Intersection_results {NO_INTERSECTION, POINT, SEGMENT};
00044     Segment_2_Line_2_pair(typename K::Segment_2 const *seg,
00045                           typename K::Line_2 const *line)
00046       : _seg(seg), _line(line), _known(false) {}
00047 
00048     Intersection_results intersection_type() const;
00049 
00050     typename K::Point_2    intersection_point() const;
00051     typename K::Segment_2  intersection_segment() const;
00052 protected:
00053     typename K::Segment_2 const*_seg;
00054     typename K::Line_2 const *  _line;
00055     mutable bool                    _known;
00056     mutable Intersection_results     _result;
00057     mutable typename K::Point_2         _intersection_point;
00058 };
00059 
00060 template <class K>
00061 inline bool do_intersect(
00062     const typename K::Segment_2 &p1,
00063     const typename K::Line_2 &p2,
00064     const K&)
00065 {
00066     typedef Segment_2_Line_2_pair<K> pair_t;
00067     pair_t pair(&p1, &p2);
00068     return pair.intersection_type() != pair_t::NO_INTERSECTION;
00069 }
00070 
00071 template <class K>
00072 Object
00073 intersection(const typename K::Segment_2 &seg, 
00074              const typename K::Line_2 &line,
00075              const K&)
00076 {
00077     typedef Segment_2_Line_2_pair<K> is_t;
00078     is_t ispair(&seg, &line);
00079     switch (ispair.intersection_type()) {
00080     case is_t::NO_INTERSECTION:
00081     default:
00082         return Object();
00083     case is_t::POINT:
00084         return make_object(ispair.intersection_point());
00085     case is_t::SEGMENT:
00086         return make_object(seg);
00087     }
00088 }
00089 
00090 template <class K>
00091 Object
00092 intersection(const typename K::Line_2 &line,
00093              const typename K::Segment_2 &seg, 
00094              const K& k)
00095 {
00096   return CGALi::intersection(seg, line, k);
00097 }
00098 
00099 
00100 template <class K>
00101 inline bool do_intersect(
00102     const typename K::Line_2 &p1,
00103     const typename K::Segment_2 &p2,
00104     const K&)
00105 {
00106     typedef Segment_2_Line_2_pair<K> pair_t;
00107     pair_t pair(&p2, &p1);
00108     return pair.intersection_type() != pair_t::NO_INTERSECTION;
00109 }
00110 
00111 
00112 template <class K>
00113 typename Segment_2_Line_2_pair<K>::Intersection_results
00114 Segment_2_Line_2_pair<K>::intersection_type() const
00115 {
00116     if (_known)
00117         return _result;
00118     // The non const this pointer is used to cast away const.
00119     _known = true;
00120     const typename K::Line_2 &l1 = _seg->supporting_line();
00121     Line_2_Line_2_pair<K> linepair(&l1, _line);
00122     switch ( linepair.intersection_type()) {
00123     case Line_2_Line_2_pair<K>::NO_INTERSECTION:
00124         _result = NO_INTERSECTION;
00125         break;
00126     case Line_2_Line_2_pair<K>::POINT:
00127         _intersection_point = linepair.intersection_point();
00128         _result = (_seg->collinear_has_on(_intersection_point) )
00129                 ? POINT : NO_INTERSECTION;
00130         break;
00131     case Line_2_Line_2_pair<K>::LINE:
00132         _result = SEGMENT;
00133         break;
00134     }
00135     return _result;
00136 }
00137 
00138 template <class K>
00139 typename K::Point_2
00140 Segment_2_Line_2_pair<K>::intersection_point() const
00141 {
00142     if (!_known)
00143         intersection_type();
00144     CGAL_kernel_assertion(_result == POINT);
00145     return _intersection_point;
00146 }
00147 
00148 template <class K>
00149 typename K::Segment_2
00150 Segment_2_Line_2_pair<K>::intersection_segment() const
00151 {
00152     if (!_known)
00153         intersection_type();
00154     CGAL_kernel_assertion(_result == SEGMENT);
00155     return *_seg;
00156 }
00157 
00158 } // namespace CGALi
00159 
00160 template <class K>
00161 inline bool
00162 do_intersect(const Segment_2<K> &seg, const Line_2<K> &line)
00163 {
00164   typedef typename K::Do_intersect_2 Do_intersect;
00165   return Do_intersect()(seg, line);
00166 }
00167 
00168 template <class K>
00169 inline bool
00170 do_intersect(const Line_2<K> &line, const Segment_2<K> &seg)
00171 {
00172   typedef typename K::Do_intersect_2 Do_intersect;
00173   return Do_intersect()(line, seg);
00174 }
00175 
00176 template <class K>
00177 inline Object
00178 intersection(const Line_2<K> &line, const Segment_2<K> &seg)
00179 {
00180   typedef typename K::Intersect_2 Intersect;
00181   return Intersect()(seg, line);
00182 }
00183 
00184 template <class K>
00185 inline Object
00186 intersection(const Segment_2<K> &seg, const Line_2<K> &line)
00187 {
00188   typedef typename K::Intersect_2 Intersect;
00189   return Intersect()(line, seg);
00190 }
00191 
00192 CGAL_END_NAMESPACE
00193 
00194 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines