BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Homogeneous/VectorH2.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/Homogeneous_kernel/include/CGAL/Homogeneous/VectorH2.h $
00019 // $Id: VectorH2.h 49057 2009-04-30 14:03:52Z spion $
00020 // 
00021 //
00022 // Author(s)     : Stefan Schirra
00023  
00024 
00025 #ifndef CGAL_HOMOGENEOUS_VECTOR_2_h
00026 #define CGAL_HOMOGENEOUS_VECTOR_2_h
00027 
00028 #include <CGAL/Origin.h>
00029 #include <CGAL/array.h>
00030 #include <CGAL/Kernel_d/Cartesian_const_iterator_d.h>
00031 
00032 CGAL_BEGIN_NAMESPACE
00033 
00034 template < class R_ >
00035 class VectorH2
00036 {
00037   typedef VectorH2<R_>                      Self;
00038   typedef typename R_::FT                   FT;
00039   typedef typename R_::RT                   RT;
00040   typedef typename R_::Point_2              Point_2;
00041   typedef typename R_::Segment_2            Segment_2;
00042   typedef typename R_::Ray_2                Ray_2;
00043   typedef typename R_::Line_2               Line_2;
00044   typedef typename R_::Direction_2          Direction_2;
00045   typedef typename R_::Vector_2             Vector_2;
00046 
00047   typedef cpp0x::array<RT, 3>               Rep;
00048   typedef typename R_::template Handle<Rep>::type  Base;
00049 
00050   typedef Rational_traits<FT>               Rat_traits;
00051 
00052   Base base;
00053 
00054 public:
00055 
00056   typedef const FT Cartesian_coordinate_type;
00057   typedef const RT& Homogeneous_coordinate_type;
00058   typedef Cartesian_const_iterator_d<const RT*> Cartesian_const_iterator;
00059 
00060   typedef R_                                    R;
00061 
00062    VectorH2() {}
00063 
00064    template < typename Tx, typename Ty >
00065    VectorH2(const Tx & x, const Ty & y,
00066             typename boost::enable_if< boost::mpl::and_<boost::is_convertible<Tx, RT>,
00067                                                         boost::is_convertible<Ty, RT> > >::type* = 0)
00068       : base(CGAL::make_array<RT>(x, y, RT(1))) {}
00069 
00070    VectorH2(const FT& x, const FT& y)
00071       : base(CGAL::make_array<RT>(
00072              Rat_traits().numerator(x) * Rat_traits().denominator(y),
00073              Rat_traits().numerator(y) * Rat_traits().denominator(x),
00074              Rat_traits().denominator(x) * Rat_traits().denominator(y)))
00075    {
00076      CGAL_kernel_assertion(hw() > 0);
00077    }
00078 
00079    VectorH2(const RT& x, const RT& y, const RT& w )
00080      : base( w >= RT(0) ? CGAL::make_array( x,  y,  w)
00081                         : CGAL::make_array<RT>(-x, -y, -w) ) {}
00082 
00083   const Self&
00084   rep() const
00085   {
00086     return static_cast<const Self& >(*this);
00087   }
00088   
00089    bool    operator==( const VectorH2<R>& v) const;
00090    bool    operator!=( const VectorH2<R>& v) const;
00091    bool    operator==( const Null_vector&) const;
00092    bool    operator!=( const Null_vector& v) const;
00093 
00094    const RT & hx() const { return get(base)[0]; };
00095    const RT & hy() const { return get(base)[1]; };
00096    const RT & hw() const { return get(base)[2]; };
00097 
00098    FT      x()  const { return FT(hx()) / FT(hw()); };
00099    FT      y()  const { return FT(hy()) / FT(hw()); };
00100 
00101    FT      cartesian(int i)   const;
00102    const RT & homogeneous(int i) const;
00103    FT      operator[](int i)  const;
00104 
00105    Cartesian_const_iterator cartesian_begin() const
00106    {
00107      return make_cartesian_const_iterator_begin(get(base).begin(),
00108                                                 boost::prior(get(base).end()));
00109    }
00110 
00111    Cartesian_const_iterator cartesian_end() const
00112    {
00113      return make_cartesian_const_iterator_end(boost::prior(get(base).end()));
00114    }
00115 
00116    int     dimension() const;
00117    Direction_2 direction() const;
00118    Vector_2 perpendicular(const Orientation& o ) const;
00119 
00120   //   Vector_2 operator+(const VectorH2 &v) const;
00121    Vector_2 operator-(const VectorH2 &v) const;
00122    Vector_2 operator-() const;
00123    Vector_2 opposite() const;
00124    FT squared_length() const;
00125   //   Vector_2 operator/(const RT &f) const;
00126   //Vector_2 operator/(const FT &f) const;
00127 
00128 // undocumented:
00129    VectorH2(const Direction_2 & dir)
00130       : base ( dir) {}
00131 
00132   VectorH2(const Point_2 & p)
00133      : base ( p) {}
00134 };
00135 
00136 template < class R >
00137 inline
00138 bool
00139 VectorH2<R>::operator==( const Null_vector&) const
00140 { return (hx() == RT(0)) && (hy() == RT(0)); }
00141 
00142 template < class R >
00143 inline
00144 bool
00145 VectorH2<R>::operator!=( const Null_vector& v) const
00146 { return !(*this == v); }
00147 
00148 template < class R >
00149 CGAL_KERNEL_INLINE
00150 bool
00151 VectorH2<R>::operator==( const VectorH2<R>& v) const
00152 {
00153   return (  (hx() * v.hw() == v.hx() * hw() )
00154           &&(hy() * v.hw() == v.hy() * hw() ) );
00155 }
00156 
00157 template < class R >
00158 inline
00159 bool
00160 VectorH2<R>::operator!=( const VectorH2<R>& v) const
00161 { return !(*this == v); }  /* XXX */
00162 
00163 template < class R >
00164 CGAL_KERNEL_INLINE
00165 typename VectorH2<R>::FT
00166 VectorH2<R>::cartesian(int i) const
00167 {
00168   CGAL_kernel_precondition( (i==0 || i==1) );
00169   if (i==0)
00170       return x();
00171   return y();
00172 }
00173 
00174 template < class R >
00175 CGAL_KERNEL_INLINE
00176 const typename VectorH2<R>::RT &
00177 VectorH2<R>::homogeneous(int i) const
00178 {
00179   CGAL_kernel_precondition( (i>=0) && (i<=2) );
00180   return get(base)[i];
00181 }
00182 
00183 template < class R >
00184 inline
00185 typename VectorH2<R>::FT
00186 VectorH2<R>::operator[](int i) const
00187 { return cartesian(i); }
00188 
00189 template < class R >
00190 inline
00191 int
00192 VectorH2<R>::dimension() const
00193 { return 2; }
00194 
00195 template < class R >
00196 CGAL_KERNEL_INLINE
00197 typename VectorH2<R>::Direction_2
00198 VectorH2<R>::direction() const
00199 { return Direction_2(hx(), hy()); }
00200 
00201 template < class R >
00202 inline
00203 typename VectorH2<R>::Vector_2
00204 VectorH2<R>::operator-() const
00205 { return VectorH2<R>(- hx(), - hy(), hw() ); }
00206 
00207 template < class R >
00208 inline
00209 typename VectorH2<R>::Vector_2
00210 VectorH2<R>::opposite() const
00211 { return VectorH2<R>(- hx(), - hy(), hw() ); }
00212 
00213 
00214 template <class R>
00215 CGAL_KERNEL_INLINE
00216 typename VectorH2<R>::Vector_2
00217 VectorH2<R>::operator-(const VectorH2<R>& v) const
00218 {
00219   return VectorH2<R>( hx()*v.hw() - v.hx()*hw(),
00220                       hy()*v.hw() - v.hy()*hw(),
00221                       hw()*v.hw() );
00222 }
00223 
00224 template <class R>
00225 CGAL_KERNEL_INLINE
00226 typename VectorH2<R>::FT
00227 VectorH2<R>::squared_length() const
00228 {
00229   typedef typename R::FT FT;
00230   return 
00231     FT( CGAL_NTS square(hx()) + CGAL_NTS square(hy()) ) / 
00232     FT( CGAL_NTS square(hw()) );
00233 }
00234 
00235 
00236 template < class R >
00237 CGAL_KERNEL_INLINE
00238 typename R::Vector_2
00239 VectorH2<R>::perpendicular(const Orientation& o) const
00240 {
00241   CGAL_kernel_precondition(o != COLLINEAR);
00242   if (o == COUNTERCLOCKWISE)
00243       return typename R::Vector_2(-hy(), hx(), hw());
00244   else
00245       return typename R::Vector_2(hy(), -hx(), hw());
00246 }
00247 
00248 CGAL_END_NAMESPACE
00249 
00250 #endif // CGAL_HOMOGENEOUS_VECTOR_2_h
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines