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/Line_2_Line_2_intersection.h $ 00019 // $Id: Line_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_LINE_2_LINE_2_INTERSECTION_H 00026 #define CGAL_LINE_2_LINE_2_INTERSECTION_H 00027 00028 #include <CGAL/config.h> 00029 #include <CGAL/Line_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 00035 CGAL_BEGIN_NAMESPACE 00036 00037 namespace CGALi { 00038 00039 template <class K> 00040 class Line_2_Line_2_pair { 00041 public: 00042 enum Intersection_results {NO_INTERSECTION, POINT, LINE}; 00043 Line_2_Line_2_pair(typename K::Line_2 const *line1, 00044 typename K::Line_2 const *line2) 00045 : _line1(line1), _line2(line2), _known(false) {} 00046 00047 Intersection_results intersection_type() const; 00048 00049 typename K::Point_2 intersection_point() const; 00050 typename K::Line_2 intersection_line() const; 00051 protected: 00052 typename K::Line_2 const* _line1; 00053 typename K::Line_2 const * _line2; 00054 mutable bool _known; 00055 mutable Intersection_results _result; 00056 mutable typename K::Point_2 _intersection_point; 00057 }; 00058 00059 template <class K> 00060 inline bool do_intersect( 00061 const typename K::Line_2 &p1, 00062 const typename K::Line_2 &p2, 00063 const K&) 00064 { 00065 typedef Line_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::Line_2 &line1, 00075 const typename K::Line_2 &line2, 00076 const K&) 00077 { 00078 typedef Line_2_Line_2_pair<K> is_t; 00079 is_t linepair(&line1, &line2); 00080 switch (linepair.intersection_type()) { 00081 case is_t::NO_INTERSECTION: 00082 default: 00083 return Object(); 00084 case is_t::POINT: 00085 return make_object(linepair.intersection_point()); 00086 case is_t::LINE: 00087 return make_object(line1); 00088 } 00089 } 00090 00091 00092 00093 template <class R, class POINT, class RT> 00094 bool construct_if_finite(POINT &pt, RT x, RT y, RT w, R &, const Cartesian_tag &) 00095 { 00096 typename R::Construct_point_2 construct_point; 00097 typedef typename R::FT FT; 00098 CGAL_kernel_precondition(CGAL_NTS is_finite(x) 00099 && CGAL_NTS is_finite(y) 00100 && w != RT(0)); 00101 00102 FT xw = FT(x)/FT(w); 00103 FT yw = FT(y)/FT(w); 00104 if (!CGAL_NTS is_finite(xw) || !CGAL_NTS is_finite(yw)) 00105 return false; 00106 pt = construct_point(xw, yw); 00107 return true; 00108 } 00109 00110 00111 00112 template <class R, class POINT, class RT> 00113 bool construct_if_finite(POINT &pt, RT x, RT y, RT w, R &, const Homogeneous_tag&) 00114 { 00115 typename R::Construct_point_2 construct_point; 00116 typedef typename R::FT FT; 00117 CGAL_kernel_precondition(CGAL_NTS is_finite(x) 00118 && CGAL_NTS is_finite(y) 00119 && w != RT(0)); 00120 00121 FT xw = FT(x)/FT(w); 00122 FT yw = FT(y)/FT(w); 00123 if (!CGAL_NTS is_finite(xw) || !CGAL_NTS is_finite(yw)) 00124 return false; 00125 pt = construct_point(x, y, w); 00126 return true; 00127 } 00128 00129 00130 template <class R, class POINT, class RT> 00131 inline 00132 bool 00133 construct_if_finite(POINT &pt, RT x, RT y, RT w, const R &r) 00134 { 00135 typedef typename R::Kernel_tag Tag; 00136 Tag tag; 00137 return construct_if_finite(pt, x, y, w, r, tag); 00138 } 00139 00140 00141 template <class K> 00142 typename Line_2_Line_2_pair<K>::Intersection_results 00143 Line_2_Line_2_pair<K>::intersection_type() const 00144 { 00145 typedef typename K::RT RT; 00146 if (_known) 00147 return _result; 00148 RT nom1, nom2, denom; 00149 // The non const this pointer is used to cast away const. 00150 _known = true; 00151 denom = _line1->a()*_line2->b() - _line2->a()*_line1->b(); 00152 if (denom == RT(0)) { 00153 if (RT(0) == (_line1->a()*_line2->c() - _line2->a()*_line1->c()) && 00154 RT(0) == (_line1->b()*_line2->c() - _line2->b()*_line1->c())) 00155 _result = LINE; 00156 else 00157 _result = NO_INTERSECTION; 00158 return _result; 00159 } 00160 nom1 = (_line1->b()*_line2->c() - _line2->b()*_line1->c()); 00161 if (!CGAL_NTS is_finite(nom1)) { 00162 _result = NO_INTERSECTION; 00163 return _result; 00164 } 00165 nom2 = (_line2->a()*_line1->c() - _line1->a()*_line2->c()); 00166 if (!CGAL_NTS is_finite(nom2)) { 00167 _result = NO_INTERSECTION; 00168 return _result; 00169 } 00170 K dummyR; 00171 if (!construct_if_finite(_intersection_point, 00172 nom1, nom2, denom, dummyR)){ 00173 _result = NO_INTERSECTION; 00174 return _result; 00175 } 00176 _result = POINT; 00177 return _result; 00178 } 00179 00180 00181 template <class K> 00182 typename K::Point_2 00183 Line_2_Line_2_pair<K>::intersection_point() const 00184 { 00185 if (!_known) 00186 intersection_type(); 00187 CGAL_kernel_assertion(_result == POINT); 00188 return _intersection_point; 00189 } 00190 00191 template <class K> 00192 typename K::Line_2 00193 Line_2_Line_2_pair<K>::intersection_line() const 00194 { 00195 if (!_known) 00196 intersection_type(); 00197 CGAL_kernel_assertion(_result == LINE); 00198 return *_line1; 00199 } 00200 00201 } // namespace CGALi 00202 00203 00204 template <class K> 00205 inline bool do_intersect( 00206 const Line_2<K> &p1, 00207 const Line_2<K> &p2) 00208 { 00209 typedef typename K::Do_intersect_2 Do_intersect; 00210 return Do_intersect()(p1, p2); 00211 } 00212 00213 template <class K> 00214 Object 00215 intersection(const Line_2<K> &line1, const Line_2<K> &line2) 00216 { 00217 typedef typename K::Intersect_2 Intersect; 00218 return Intersect()(line1, line2); 00219 } 00220 00221 CGAL_END_NAMESPACE 00222 00223 #endif