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