BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Line_2_Iso_rectangle_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/Line_2_Iso_rectangle_2_intersection.h $
00019 // $Id: Line_2_Iso_rectangle_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_ISO_RECTANGLE_2_INTERSECTION_H
00026 #define CGAL_LINE_2_ISO_RECTANGLE_2_INTERSECTION_H
00027 
00028 #include <CGAL/Line_2.h>
00029 #include <CGAL/Iso_rectangle_2.h>
00030 #include <CGAL/Line_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_Iso_rectangle_2_pair {
00041 public:
00042     enum Intersection_results {NO_INTERSECTION, POINT, SEGMENT};
00043     Line_2_Iso_rectangle_2_pair(typename K::Line_2 const *line,
00044                             typename K::Iso_rectangle_2 const *iso)
00045       : _known(false),
00046         _ref_point(line->point()),
00047         _dir(line->direction().to_vector()),
00048         _isomin((iso->min)()),
00049         _isomax((iso->max)()) {}
00050 
00051     Intersection_results intersection_type() const;
00052 
00053     typename K::Point_2    intersection_point() const;
00054     typename K::Segment_2  intersection_segment() const;
00055 protected:
00056     mutable bool                        _known;
00057     mutable Intersection_results        _result;
00058     mutable typename K::FT              _min, _max;
00059     typename K::Point_2             _ref_point;
00060     typename K::Vector_2            _dir;
00061     typename K::Point_2             _isomin;
00062     typename K::Point_2             _isomax;
00063 };
00064 
00065 template <class K>
00066 inline bool do_intersect(const typename K::Line_2 &p1,
00067                          const typename K::Iso_rectangle_2 &p2,
00068                          const K&)
00069 {
00070     typedef Line_2_Iso_rectangle_2_pair<K> pair_t;
00071     pair_t pair(&p1, &p2);
00072     return pair.intersection_type() != pair_t::NO_INTERSECTION;
00073 }
00074 
00075 template <class K>
00076 inline bool do_intersect(const typename K::Iso_rectangle_2 &p2,
00077                          const typename K::Line_2 &p1,
00078                          const K& k)
00079 {
00080   return CGALi::do_intersect(p1, p2, k);
00081 }
00082 
00083 
00084 
00085 template <class K>
00086 typename Line_2_Iso_rectangle_2_pair<K>::Intersection_results
00087 Line_2_Iso_rectangle_2_pair<K>::intersection_type() const
00088 {
00089     typedef typename K::Line_2 line_t;
00090     if (_known)
00091         return _result;
00092 // The non const this pointer is used to cast away const.
00093     _known = true;
00094     typedef typename K::FT FT;
00095     typedef typename K::RT RT;
00096     bool all_values = true;
00097 
00098     typename K::Construct_cartesian_const_iterator_2 construct_cccit;
00099     typename K::Cartesian_const_iterator_2 ref_point_it = construct_cccit(_ref_point);
00100     typename K::Cartesian_const_iterator_2 end = construct_cccit(_ref_point, 0);
00101     typename K::Cartesian_const_iterator_2 isomin_it = construct_cccit(_isomin);
00102     typename K::Cartesian_const_iterator_2 isomax_it = construct_cccit(_isomax);
00103 
00104     for (unsigned int i=0; ref_point_it != end; ++i, ++ref_point_it, ++isomin_it, ++isomax_it) {
00105 
00106         if (_dir.homogeneous(i) == RT(0)) {
00107             if (*ref_point_it < *isomin_it) {
00108                 _result = NO_INTERSECTION;
00109                 return NO_INTERSECTION;
00110             }
00111             if (*ref_point_it > *isomax_it) {
00112                 _result = NO_INTERSECTION;
00113                 return NO_INTERSECTION;
00114             }
00115         } else {
00116             FT newmin, newmax;
00117             if (_dir.homogeneous(i) > RT(0)) {
00118                 newmin = (*isomin_it - *ref_point_it) /
00119                     _dir.cartesian(i);
00120                 newmax = (*isomax_it - *ref_point_it) /
00121                     _dir.cartesian(i);
00122             } else {
00123                 newmin = (*isomax_it - *ref_point_it) /
00124                     _dir.cartesian(i);
00125                 newmax = (*isomin_it - *ref_point_it) /
00126                     _dir.cartesian(i);
00127             }
00128             if (all_values) {
00129                 _min = newmin;
00130                 _max = newmax;
00131             } else {
00132                 if (newmin > _min)
00133                     _min = newmin;
00134                 if (newmax < _max)
00135                     _max = newmax;
00136                 if (_max < _min) {
00137                     _result = NO_INTERSECTION;
00138                     return NO_INTERSECTION;
00139                 }
00140             }
00141             all_values = false;
00142         }
00143     }
00144     CGAL_kernel_assertion(!all_values);
00145     if (_max == _min) {
00146         _result = POINT;
00147         return POINT;
00148     }
00149     _result = SEGMENT;
00150     return SEGMENT;
00151 }
00152 
00153 
00154 template <class K>
00155 typename K::Point_2
00156 Line_2_Iso_rectangle_2_pair<K>::
00157 intersection_point() const
00158 {
00159   typename K::Construct_translated_point_2 translated_point;
00160   typename K::Construct_scaled_vector_2 construct_scaled_vector;
00161 
00162     if (!_known)
00163         intersection_type();
00164     CGAL_kernel_assertion(_result == POINT);
00165     return translated_point(_ref_point, construct_scaled_vector(_dir, _min));
00166 }
00167 
00168 template <class K>
00169 typename K::Segment_2
00170 Line_2_Iso_rectangle_2_pair<K>::
00171 intersection_segment() const
00172 {
00173   typename K::Construct_segment_2 construct_segment_2;
00174   typename K::Construct_translated_point_2 translated_point;
00175   typename K::Construct_scaled_vector_2 construct_scaled_vector;
00176     if (!_known)
00177         intersection_type();
00178     CGAL_kernel_assertion(_result == SEGMENT);
00179     return construct_segment_2(translated_point(_ref_point, construct_scaled_vector(_dir,_min)), 
00180                                translated_point(_ref_point, construct_scaled_vector(_dir,_max)));
00181 }
00182 
00183 
00184 
00185 template <class K>
00186 Object
00187 intersection(const typename K::Line_2 &line, 
00188              const typename K::Iso_rectangle_2 &iso,
00189              const K&)
00190 {
00191     typename K::Construct_object_2 construct_object;
00192     typedef Line_2_Iso_rectangle_2_pair<K> is_t;
00193     is_t ispair(&line, &iso);
00194     switch (ispair.intersection_type()) {
00195     case is_t::NO_INTERSECTION:
00196     default:
00197         return Object();
00198     case is_t::POINT:
00199         return construct_object(ispair.intersection_point());
00200     case is_t::SEGMENT:
00201         return construct_object(ispair.intersection_segment());
00202     }
00203 }
00204 
00205 template <class K>
00206 inline
00207 Object
00208 intersection(const typename K::Iso_rectangle_2 &iso,
00209              const typename K::Line_2 &line, 
00210              const K& k)
00211 {
00212   return CGALi::intersection(line, iso, k);
00213 }
00214 
00215 } // namespace CGALi
00216 
00217 
00218 template <class K>
00219 inline bool do_intersect(
00220     const Line_2<K> &p1,
00221     const Iso_rectangle_2<K> &p2)
00222 {
00223   typedef typename K::Do_intersect_2 Do_intersect;
00224   return Do_intersect()(p1, p2);
00225 }
00226 
00227 template <class K>
00228 inline bool do_intersect(
00229     const Iso_rectangle_2<K> &p1,
00230     const Line_2<K> &p2)
00231 {
00232   typedef typename K::Do_intersect_2 Do_intersect;
00233   return Do_intersect()(p2, p1);
00234 }
00235 
00236 template <class K>
00237 Object
00238 intersection(const Line_2<K> &line, const Iso_rectangle_2<K> &iso)
00239 {
00240   typedef typename K::Intersect_2 Intersect;
00241   return Intersect()(line, iso);
00242 }
00243 
00244 template <class K>
00245 inline Object
00246 intersection(const Iso_rectangle_2<K> &iso, const Line_2<K> &line)
00247 {
00248   typedef typename K::Intersect_2 Intersect;
00249     return Intersect()(line, iso);
00250 }
00251 
00252 CGAL_END_NAMESPACE
00253 
00254 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines