BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Vector_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/Vector_2.h $
00019 // $Id: Vector_2.h 49257 2009-05-09 16:08:19Z spion $
00020 //
00021 //
00022 // Author(s)     : Andreas Fabri, Stefan Schirra
00023 
00024 #ifndef CGAL_VECTOR_2_H
00025 #define CGAL_VECTOR_2_H
00026 
00027 #include <CGAL/Origin.h>
00028 #include <CGAL/Kernel/mpl.h>
00029 #include <boost/static_assert.hpp>
00030 #include <boost/type_traits.hpp>
00031 #include <CGAL/Kernel/Return_base_tag.h>
00032 #include <CGAL/representation_tags.h>
00033 #include <CGAL/Dimension.h>
00034 
00035 CGAL_BEGIN_NAMESPACE
00036 
00037 template <class R_>
00038 class Vector_2 : public R_::Kernel_base::Vector_2
00039 {
00040   typedef typename R_::RT             RT;
00041   typedef typename R_::FT             FT;
00042   typedef typename R_::Segment_2      Segment_2;
00043   typedef typename R_::Ray_2          Ray_2;
00044   typedef typename R_::Line_2         Line_2;
00045   typedef typename R_::Point_2        Point_2;
00046   typedef typename R_::Direction_2    Direction_2;
00047   typedef typename R_::Aff_transformation_2  Aff_transformation_2;
00048   typedef typename R_::Kernel_base::Vector_2  RVector_2;
00049 
00050   typedef Vector_2                    Self;
00051   BOOST_STATIC_ASSERT((boost::is_same<Self, typename R_::Vector_2>::value));
00052 
00053 public:
00054 
00055   typedef Dimension_tag<2>  Ambient_dimension;
00056   typedef Dimension_tag<0>  Feature_dimension;
00057 
00058   typedef RVector_2 Rep;
00059   typedef typename R_::Cartesian_const_iterator_2 Cartesian_const_iterator;
00060 
00061   const Rep& rep() const
00062   {
00063     return *this;
00064   }
00065 
00066   Rep& rep()
00067   {
00068     return *this;
00069   }
00070 
00071   typedef  R_                        R;
00072 
00073   Vector_2() {}
00074 
00075   Vector_2(const RVector_2& v)
00076       : RVector_2(v) {}
00077 
00078   Vector_2(const Point_2& a, const Point_2& b)
00079       : RVector_2(typename R::Construct_vector_2()(Return_base_tag(), a, b)) {}
00080 
00081   explicit Vector_2(const Segment_2 &s)
00082       : RVector_2(typename R::Construct_vector_2()(Return_base_tag(), s)) {}
00083 
00084   explicit Vector_2(const Ray_2 &r)
00085       : RVector_2(typename R::Construct_vector_2()(Return_base_tag(), r)) {}
00086 
00087   explicit Vector_2(const Line_2 &l)
00088       : RVector_2(typename R::Construct_vector_2()(Return_base_tag(), l)) {}
00089 
00090   Vector_2(const Null_vector &v)
00091       : RVector_2(typename R::Construct_vector_2()(Return_base_tag(), v)) {}
00092 
00093   template < typename T1, typename T2 >
00094   Vector_2(const T1 &x, const T2 &y)
00095       : RVector_2(typename R::Construct_vector_2()(Return_base_tag(), x,y)) {}
00096 
00097   Vector_2(const RT &x, const RT &y, const RT &w)
00098       : RVector_2(typename R::Construct_vector_2()(Return_base_tag(), x,y,w)) {}
00099 
00100 
00101   typename Qualified_result_of<typename R::Compute_x_2,Vector_2>::type
00102   x() const
00103   {
00104     return R().compute_x_2_object()(*this);
00105   }
00106 
00107   typename Qualified_result_of<typename R::Compute_y_2,Vector_2>::type
00108   y() const
00109   {
00110     return R().compute_y_2_object()(*this);
00111   }
00112 
00113   typename Qualified_result_of<typename R::Compute_y_2,Vector_2>::type
00114   cartesian(int i) const
00115   {
00116     CGAL_kernel_precondition( (i == 0) || (i == 1) );
00117     return (i==0) ?  x() : y();
00118   }
00119 
00120   typename Qualified_result_of<typename R::Compute_x_2,Vector_2>::type
00121   operator[](int i) const
00122   {
00123       return cartesian(i);
00124   }
00125 
00126   Cartesian_const_iterator cartesian_begin() const
00127   {
00128     return typename R::Construct_cartesian_const_iterator_2()(*this);
00129   }
00130 
00131   Cartesian_const_iterator cartesian_end() const
00132   {
00133     return typename R::Construct_cartesian_const_iterator_2()(*this,2);
00134   }
00135 
00136   typename Qualified_result_of<typename R::Compute_hx_2,Vector_2>::type
00137   hx() const
00138   {
00139     return R().compute_hx_2_object()(*this);
00140   }
00141 
00142 
00143   typename Qualified_result_of<typename R::Compute_hy_2,Vector_2>::type
00144   hy() const
00145   {
00146     return R().compute_hy_2_object()(*this);
00147   }
00148 
00149   typename Qualified_result_of<typename R::Compute_hw_2,Vector_2>::type
00150   hw() const
00151   {
00152     return R().compute_hw_2_object()(*this);
00153   }
00154 
00155   typename Qualified_result_of<typename R::Compute_hx_2,Vector_2>::type
00156   homogeneous(int i) const
00157   {
00158     CGAL_kernel_precondition( (i >= 0) || (i <= 2) );
00159     return (i==0) ?  hx() : (i==1)? hy() : hw();
00160   }
00161 
00162   int dimension() const
00163   {
00164       return 2;
00165   }
00166 
00167   Vector_2 operator-() const
00168   {
00169     return R().construct_opposite_vector_2_object()(*this);
00170   }
00171 
00172   Vector_2 operator-(const Vector_2& v) const
00173   {
00174     return R().construct_difference_of_vectors_2_object()(*this,v);
00175   }
00176 
00177   Vector_2 operator+(const Vector_2& v) const
00178   {
00179     return R().construct_sum_of_vectors_2_object()(*this,v);
00180   }
00181 
00182   Vector_2 operator/(const RT& c) const
00183   {
00184    return R().construct_divided_vector_2_object()(*this,c);
00185   }
00186 
00187   Vector_2 operator/(const typename First_if_different<FT,RT>::Type & c) const
00188   {
00189    return R().construct_divided_vector_2_object()(*this,c);
00190   }
00191 
00192   FT squared_length() const
00193   {
00194     return R().compute_squared_length_2_object()(*this);
00195   }
00196 
00197 
00198   Direction_2 direction() const
00199   {
00200     return R().construct_direction_2_object()(*this);
00201   }
00202 
00203   Vector_2 perpendicular(const Orientation &o) const
00204   {
00205     return R().construct_perpendicular_vector_2_object()(*this,o);
00206   }
00207 
00208   Vector_2 transform(const Aff_transformation_2 &t) const
00209   {
00210     return t.transform(*this);
00211   }
00212 
00213 };
00214 
00215 
00216 template < class R >
00217 inline
00218 bool
00219 operator==(const Vector_2<R> &v, const Null_vector &n)
00220 {
00221   return R().equal_2_object()(v, n);
00222 }
00223 
00224 template < class R >
00225 inline
00226 bool
00227 operator==(const Null_vector &n, const Vector_2<R> &v)
00228 {
00229   return v == n;
00230 }
00231 
00232 template < class R >
00233 inline
00234 bool
00235 operator!=(const Vector_2<R> &v, const Null_vector &n)
00236 {
00237   return !(v == n);
00238 }
00239 
00240 template < class R >
00241 inline
00242 bool
00243 operator!=(const Null_vector &n, const Vector_2<R> &v)
00244 {
00245   return !(v == n);
00246 }
00247 
00248 
00249 template <class R >
00250 std::ostream&
00251 insert(std::ostream& os, const Vector_2<R>& v, const Cartesian_tag&)
00252 {
00253     switch(os.iword(IO::mode)) {
00254     case IO::ASCII :
00255         return os << v.x() << ' ' << v.y();
00256     case IO::BINARY :
00257         write(os, v.x());
00258         write(os, v.y());
00259         return os;
00260     default:
00261         return os << "VectorC2(" << v.x() << ", " << v.y() << ')';
00262     }
00263 }
00264 
00265 template <class R >
00266 std::ostream&
00267 insert(std::ostream& os, const Vector_2<R>& v, const Homogeneous_tag&)
00268 {
00269   switch(os.iword(IO::mode))
00270   {
00271     case IO::ASCII :
00272         return os << v.hx() << ' ' << v.hy() << ' ' << v.hw();
00273     case IO::BINARY :
00274         write(os, v.hx());
00275         write(os, v.hy());
00276         write(os, v.hw());
00277         return os;
00278     default:
00279         return os << "VectorH2(" << v.hx() << ", "
00280                                  << v.hy() << ", "
00281                                  << v.hw() << ')';
00282   }
00283 }
00284 
00285 template < class R >
00286 std::ostream&
00287 operator<<(std::ostream& os, const Vector_2<R>& v)
00288 {
00289   return insert(os, v, typename R::Kernel_tag() );
00290 }
00291 
00292 
00293 
00294 template <class R >
00295 std::istream&
00296 extract(std::istream& is, Vector_2<R>& v, const Cartesian_tag&)
00297 {
00298     typename R::FT x, y;
00299     switch(is.iword(IO::mode)) {
00300     case IO::ASCII :
00301         is >> x >> y;
00302         break;
00303     case IO::BINARY :
00304         read(is, x);
00305         read(is, y);
00306         break;
00307     default:
00308         std::cerr << "" << std::endl;
00309         std::cerr << "Stream must be in ascii or binary mode" << std::endl;
00310         break;
00311     }
00312     if (is)
00313         v = Vector_2<R>(x, y);
00314     return is;
00315 }
00316 
00317 
00318 template <class R >
00319 std::istream&
00320 extract(std::istream& is, Vector_2<R>& v, const Homogeneous_tag&)
00321 {
00322   typename R::RT hx, hy, hw;
00323   switch(is.iword(IO::mode))
00324   {
00325     case IO::ASCII :
00326         is >> hx >> hy >> hw;
00327         break;
00328     case IO::BINARY :
00329         read(is, hx);
00330         read(is, hy);
00331         read(is, hw);
00332         break;
00333     default:
00334         std::cerr << "" << std::endl;
00335         std::cerr << "Stream must be in ascii or binary mode" << std::endl;
00336         break;
00337   }
00338   v = Vector_2<R>(hx, hy, hw);
00339   return is;
00340 }
00341 
00342 template < class R >
00343 std::istream&
00344 operator>>(std::istream& is, Vector_2<R>& v)
00345 {
00346   return extract(is, v, typename R::Kernel_tag() );
00347 }
00348 
00349 CGAL_END_NAMESPACE
00350 
00351 #endif // CGAL_VECTOR_2_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines