BWAPI
|
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/Ray_2_Line_2_intersection.h $ 00019 // $Id: Ray_2_Line_2_intersection.h 45075 2008-08-21 12:50:41Z spion $ 00020 // 00021 // 00022 // Author(s) : Geert-Jan Giezeman 00023 00024 00025 #ifndef CGAL_RAY_2_LINE_2_INTERSECTION_H 00026 #define CGAL_RAY_2_LINE_2_INTERSECTION_H 00027 00028 #include <CGAL/Line_2.h> 00029 #include <CGAL/Ray_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 Ray_2_Line_2_pair { 00042 public: 00043 enum Intersection_results {NO_INTERSECTION, POINT, RAY}; 00044 Ray_2_Line_2_pair(typename K::Ray_2 const *ray, 00045 typename K::Line_2 const *line) 00046 : _ray(ray), _line(line), _known(false) {} 00047 00048 Intersection_results intersection_type() const; 00049 00050 typename K::Point_2 intersection_point() const; 00051 typename K::Ray_2 intersection_ray() const; 00052 protected: 00053 typename K::Ray_2 const * _ray; 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::Ray_2 &p1, 00063 const typename K::Line_2 &p2) 00064 { 00065 typedef Ray_2_Line_2_pair<K> pair_t; 00066 pair_t pair(&p1, &p2); 00067 return pair.intersection_type() != pair_t::NO_INTERSECTION; 00068 } 00069 00070 00071 00072 template <class K> 00073 Object 00074 intersection(const typename K::Ray_2 &ray, 00075 const typename K::Line_2 &line, 00076 const K&) 00077 { 00078 typedef Ray_2_Line_2_pair<K> is_t; 00079 is_t ispair(&ray, &line); 00080 switch (ispair.intersection_type()) { 00081 case is_t::NO_INTERSECTION: 00082 default: 00083 return Object(); 00084 case is_t::POINT: 00085 return make_object(ispair.intersection_point()); 00086 case is_t::RAY: 00087 return make_object(ray); 00088 } 00089 } 00090 00091 00092 template <class K> 00093 inline 00094 Object 00095 intersection(const typename K::Line_2 &line, 00096 const typename K::Ray_2 &ray, 00097 const K& k) 00098 { 00099 return CGALi::intersection(ray, line, k); 00100 } 00101 00102 00103 template <class K> 00104 inline bool do_intersect( 00105 const typename K::Line_2 &p1, 00106 const typename K::Ray_2 &p2, 00107 const K&) 00108 { 00109 typedef Ray_2_Line_2_pair<K> pair_t; 00110 pair_t pair(&p2, &p1); 00111 return pair.intersection_type() != pair_t::NO_INTERSECTION; 00112 } 00113 00114 00115 00116 template <class K> 00117 typename Ray_2_Line_2_pair<K>::Intersection_results 00118 Ray_2_Line_2_pair<K>::intersection_type() const 00119 { 00120 if (_known) 00121 return _result; 00122 // The non const this pointer is used to cast away const. 00123 _known = true; 00124 const typename K::Line_2 &l1 = _ray->supporting_line(); 00125 Line_2_Line_2_pair<K> linepair(&l1, _line); 00126 switch ( linepair.intersection_type()) { 00127 case Line_2_Line_2_pair<K>::NO_INTERSECTION: 00128 _result = NO_INTERSECTION; 00129 break; 00130 case Line_2_Line_2_pair<K>::POINT: 00131 _intersection_point = linepair.intersection_point(); 00132 _result = (_ray->collinear_has_on(_intersection_point) ) ? 00133 POINT : NO_INTERSECTION; 00134 break; 00135 case Line_2_Line_2_pair<K>::LINE: 00136 _result = RAY; 00137 break; 00138 } 00139 return _result; 00140 } 00141 00142 00143 template <class K> 00144 typename K::Point_2 00145 Ray_2_Line_2_pair<K>::intersection_point() const 00146 { 00147 if (!_known) 00148 intersection_type(); 00149 CGAL_kernel_assertion(_result == POINT); 00150 return _intersection_point; 00151 } 00152 00153 template <class K> 00154 typename K::Ray_2 00155 Ray_2_Line_2_pair<K>::intersection_ray() const 00156 { 00157 if (!_known) 00158 intersection_type(); 00159 CGAL_kernel_assertion(_result == RAY); 00160 return *_ray; 00161 } 00162 00163 } // namespace CGALi 00164 00165 template <class K> 00166 inline bool do_intersect(const Line_2<K> &p1, const Ray_2<K> &p2) 00167 { 00168 typedef typename K::Do_intersect_2 Do_intersect; 00169 return Do_intersect()(p1, p2); 00170 } 00171 00172 template <class K> 00173 inline bool do_intersect(const Ray_2<K> &p2, const Line_2<K> &p1) 00174 { 00175 typedef typename K::Do_intersect_2 Do_intersect; 00176 return Do_intersect()(p1, p2); 00177 } 00178 00179 template <class K> 00180 inline Object 00181 intersection(const Line_2<K> &line, const Ray_2<K> &ray) 00182 { 00183 typedef typename K::Intersect_2 Intersect; 00184 return Intersect()(ray, line); 00185 } 00186 00187 template <class K> 00188 inline Object 00189 intersection(const Ray_2<K> &ray, const Line_2<K> &line) 00190 { 00191 typedef typename K::Intersect_2 Intersect; 00192 return Intersect()(ray, line); 00193 } 00194 00195 CGAL_END_NAMESPACE 00196 00197 #endif