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