BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Ray_2.h
Go to the documentation of this file.
00001 // Copyright (c) 1999  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/Kernel_23/include/CGAL/Ray_2.h $
00019 // $Id: Ray_2.h 42932 2008-04-17 10:13:31Z spion $
00020 // 
00021 //
00022 // Author(s)     : Andreas Fabri
00023 
00024 #ifndef CGAL_RAY_2_H
00025 #define CGAL_RAY_2_H
00026 
00027 #include <boost/static_assert.hpp>
00028 #include <boost/type_traits.hpp>
00029 #include <CGAL/Kernel/Return_base_tag.h>
00030 #include <CGAL/representation_tags.h>
00031 #include <CGAL/Dimension.h>
00032 
00033 CGAL_BEGIN_NAMESPACE
00034 
00035 template <class R_>
00036 class Ray_2 : public R_::Kernel_base::Ray_2
00037 {
00038   typedef typename R_::RT                    RT;
00039   typedef typename R_::FT                    FT;
00040   typedef typename R_::Point_2               Point_2;
00041   typedef typename R_::Direction_2           Direction_2;
00042   typedef typename R_::Vector_2              Vector_2;
00043   typedef typename R_::Line_2                Line_2;
00044   typedef typename R_::Aff_transformation_2  Aff_transformation_2;
00045 
00046   typedef typename R_::Kernel_base::Ray_2    RRay_2;
00047 
00048   typedef Ray_2                              Self;
00049   BOOST_STATIC_ASSERT((boost::is_same<Self, typename R_::Ray_2>::value));
00050 
00051 public:
00052 
00053   typedef Dimension_tag<2>  Ambient_dimension;
00054   typedef Dimension_tag<1>  Feature_dimension;
00055 
00056   typedef RRay_2 Rep;
00057 
00058   const Rep& rep() const
00059   {
00060     return *this;
00061   }
00062 
00063   Rep& rep()
00064   {
00065     return *this;
00066   }
00067 
00068   typedef  R_   R;
00069 
00070   Ray_2() {}
00071 
00072   Ray_2(const RRay_2& r)
00073     : RRay_2(r) {}
00074 
00075   Ray_2(const Point_2 &sp, const Point_2 &secondp)
00076     : RRay_2(typename R::Construct_ray_2()(Return_base_tag(), sp, secondp)) {}
00077 
00078   Ray_2(const Point_2 &sp, const Direction_2 &d)
00079     : RRay_2(typename R::Construct_ray_2()(Return_base_tag(), sp, d)) {}
00080 
00081   Ray_2(const Point_2 &sp, const Vector_2 &v)
00082     : RRay_2(typename R::Construct_ray_2()(Return_base_tag(), sp, v)) {}
00083 
00084   Ray_2(const Point_2 &sp, const Line_2 &l)
00085     : RRay_2(typename R::Construct_ray_2()(Return_base_tag(), sp, l)) {}
00086 
00087 
00088   typename Qualified_result_of<typename R_::Construct_source_2, Ray_2>::type
00089   source() const
00090   {
00091     return R().construct_source_2_object()(*this);
00092   }
00093 
00094   typename Qualified_result_of<typename R_::Construct_second_point_2, Ray_2>::type
00095   second_point() const
00096   {
00097     return R().construct_second_point_2_object()(*this);
00098   }
00099 
00100 
00101   Point_2
00102   point(int i) const
00103   {
00104     CGAL_kernel_precondition( i >= 0 );
00105     
00106     typename R::Construct_vector_2 construct_vector;
00107     typename R::Construct_scaled_vector_2 construct_scaled_vector;
00108     typename R::Construct_translated_point_2 construct_translated_point;
00109     if (i == 0) return source();
00110     if (i == 1) return second_point();
00111     return construct_translated_point(source(),
00112                                       construct_scaled_vector(construct_vector(source(), 
00113                                                                                second_point()),
00114                                                               FT(i)));
00115   }
00116 
00117 
00118   typename Qualified_result_of<typename R_::Construct_source_2, Ray_2, int >::type
00119   start() const
00120   {
00121     return source();
00122   }
00123 
00124   bool is_horizontal() const
00125   {
00126     return R().equal_y_2_object()(source(), second_point());
00127   }
00128 
00129   bool is_vertical() const
00130   {
00131     return R().equal_x_2_object()(source(), second_point());
00132   }
00133 
00134   bool is_degenerate() const
00135   {
00136     return R().is_degenerate_2_object()(*this);
00137   }
00138 
00139   Direction_2
00140   direction() const
00141   {
00142     typename R::Construct_vector_2 construct_vector;
00143     typename R::Construct_direction_2 construct_direction;
00144     return construct_direction( construct_vector(source(), second_point()) );
00145   }
00146 
00147 
00148   Vector_2
00149   to_vector() const
00150   {
00151     typename R::Construct_vector_2 construct_vector;
00152     return construct_vector(source(), second_point());
00153   }
00154 
00155   bool
00156   has_on(const Point_2 &p) const
00157   {
00158     typename R::Construct_vector_2  construct_vector;
00159     return p == source() ||
00160          ( R().collinear_2_object()(source(), p, second_point()) &&
00161            Direction_2(construct_vector( source(), p)) == direction() );
00162   }
00163 
00164 
00165 
00166   bool
00167   collinear_has_on(const Point_2 &p) const
00168   {
00169     return R().collinear_has_on_2_object()(*this, p);
00170   }
00171 
00172   Ray_2
00173   opposite() const
00174   {
00175     return Ray_2( source(), - direction() );
00176   }
00177 
00178   Line_2
00179   supporting_line() const
00180   {
00181     return R().construct_line_2_object()(source(), second_point());
00182   }
00183 
00184   bool
00185   operator==(const Ray_2& r) const
00186   {
00187     return R().equal_2_object()(*this, r);
00188   }
00189 
00190   bool
00191   operator!=(const Ray_2& r) const
00192   {
00193     return !(*this == r);
00194   }
00195 
00196   Ray_2 
00197   transform(const Aff_transformation_2 &t) const
00198   {
00199     return Ray_2(t.transform(source()), t.transform(second_point()));
00200   }
00201 
00202 };
00203 
00204 
00205 template <class R >
00206 std::ostream&
00207 insert(std::ostream& os, const Ray_2<R>& r, const Cartesian_tag&) 
00208 {
00209     switch(os.iword(IO::mode)) {
00210     case IO::ASCII :
00211         return os << r.source() << ' ' << r.second_point();
00212     case IO::BINARY :
00213         return os << r.source() << r.second_point();
00214     default:
00215         return os << "RayC2(" << r.source() <<  ", " << r.second_point() << ")";
00216     }
00217 }
00218 
00219 template <class R >
00220 std::ostream&
00221 insert(std::ostream& os, const Ray_2<R>& r, const Homogeneous_tag&)
00222 {
00223   switch(os.iword(IO::mode))
00224   {
00225     case IO::ASCII :
00226         return os << r.source() << ' ' << r.second_point();
00227     case IO::BINARY :
00228         return os << r.source() << r.second_point();
00229     default:
00230        return os << "RayH2(" << r.source() <<  ", " << r.second_point() << ")";
00231   }
00232 }
00233 
00234 template < class R >
00235 std::ostream&
00236 operator<<(std::ostream& os, const Ray_2<R>& r)
00237 {
00238   return insert(os, r, typename R::Kernel_tag() );
00239 }
00240 
00241 
00242 template <class R >
00243 std::istream&
00244 extract(std::istream& is, Ray_2<R>& r, const Cartesian_tag&) 
00245 {
00246     typename R::Point_2 p, q;
00247     is >> p >> q;
00248     if (is)
00249         r = Ray_2<R>(p, q);
00250     return is;
00251 }
00252 
00253 
00254 template <class R >
00255 std::istream&
00256 extract(std::istream& is, Ray_2<R>& r, const Homogeneous_tag&) 
00257 {
00258   typename R::Point_2 p, q;
00259   is >> p >> q;
00260   if (is)
00261     r = Ray_2<R>(p, q);
00262   return is;
00263 }
00264 
00265 template < class R >
00266 std::istream&
00267 operator>>(std::istream& is, Ray_2<R>& r)
00268 {
00269   return extract(is, r, typename R::Kernel_tag() );
00270 }
00271 
00272 CGAL_END_NAMESPACE
00273 
00274 #endif  // CGAL_RAY_2_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines