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