// ====================================================================== // // Copyright (c) 2000 The CGAL Consortium // // This software and related documentation is part of an INTERNAL release // of the Computational Geometry Algorithms Library (CGAL). It is not // intended for general use. // // ---------------------------------------------------------------------- // // release : // release_date : // // file : include/CGAL/Cartesian/Vector_d.C // revision : $Revision$ // revision_date : $Date$ // author : Herve Brönnimann // coordinator : INRIA Sophia-Antipolis (Mariette.Yvinec@sophia.inria.fr) // // ====================================================================== #ifndef CGAL_CARTESIAN_VECTOR_D_C #define CGAL_CARTESIAN_VECTOR_D_C #include #include #include #include #include #include #ifndef CGAL_CTAG #define CGAL_CTAG #endif #ifdef CGAL_CFG_TYPENAME_BUG #define typename #endif CGAL_BEGIN_NAMESPACE template < class R > VectorCd:: VectorCd(int d) { PTR = new _d_tuple(d); } template < class R > VectorCd:: VectorCd(const VectorCd &v) : Handle(v) {} template < class R > inline VectorCd:: VectorCd(const typename VectorCd::Point_d &p) : Handle((const Handle&)p) { } template < class R > inline VectorCd:: VectorCd(const typename VectorCd::Direction_d &d) : Handle((const Handle&)d) { } template < class R > VectorCd:: VectorCd(int dim, const Null_vector &) { CGAL_kernel_precondition( dim > 0); PTR = new _d_tuple(dim); std::fill(begin(), end(), FT(0)); } template < class R > VectorCd::~VectorCd() {} template < class R > VectorCd & VectorCd::operator=(const VectorCd &v) { Handle::operator=((const Handle &)v); return *this; } template < class R > bool VectorCd::operator==(const VectorCd &v) const { if (dimension() != v.dimension()) return false; if (ptr() == v.ptr()) return true; // identical return std::equal(begin(),end(),v.begin()); } template < class R > inline bool VectorCd::operator!=(const VectorCd &v) const { return !(*this == v); } template < class R > bool VectorCd::operator==(const Null_vector &) const { const_iterator non_zero; non_zero = find_if(begin(),end(),bind1st(not_equal_to(),FT(0))); return non_zero == end(); } template < class R > inline bool VectorCd::operator!=(const Null_vector &v) const { return !(*this == v); } template < class R > inline typename VectorCd::FT VectorCd::cartesian(int i) const { CGAL_kernel_precondition( (i>=0) && (i inline typename VectorCd::FT VectorCd::operator[](int i) const { return cartesian(i); } template < class R > typename VectorCd::FT VectorCd::homogeneous(int i) const { CGAL_kernel_precondition( (i>=0) && (i<=dimension()) ); return (i==dimension()) ? FT(1) : cartesian(i); } template < class R > inline VectorCd VectorCd::operator+(const VectorCd &v) const { CGAL_kernel_precondition( dimension() == v.dimension() ); Self w(dimension()); std::transform(begin(),end(),v.begin(),w.begin(),std::plus()); return w; } template < class R > inline VectorCd VectorCd::operator-(const VectorCd &v) const { CGAL_kernel_precondition( dimension() == v.dimension() ); Self w(dimension()); std::transform(begin(),end(),v.begin(),w.begin(),std::minus()); return w; } template < class R > inline VectorCd VectorCd::operator-() const { Self v(dimension()); std::transform(begin(),end(),v.begin(),std::negate()); return v; } template < class R > inline typename VectorCd::FT VectorCd::operator*(const VectorCd &v) const { CGAL_kernel_precondition( dimension() == v.dimension() ); return std::inner_product(begin(),end(),v.begin(),FT(0)); } template < class R > inline VectorCd VectorCd:: operator*(const typename VectorCd::FT &c) const { Self v(dimension()); std::transform(begin(),end(),v.begin(),std::bind2nd(std::multiplies(),c)); return v; } template < class R > inline VectorCd VectorCd:: operator/(const typename VectorCd::FT &c) const { Self v(dimension()); std::transform(begin(),end(),v.begin(),std::bind2nd(std::divides(),c)); return v; } template < class R > inline typename VectorCd::Direction_d VectorCd::direction() const { return Direction_d(*this); } template < class R > VectorCd VectorCd:: transform(const typename VectorCd::Aff_transformation_d &t) const { return t.transform(*this); } #ifndef CGAL_CARTESIAN_NO_OSTREAM_INSERT_VECTORCD template < class R > std::ostream & operator<<(std::ostream &os, const VectorCd &v) { typedef typename VectorCd::FT FT; print_d prt(os); if (os.iword(IO::mode) == IO::PRETTY) os << "VectorCd("; prt(v.dimension()); if (os.iword(IO::mode) == IO::PRETTY) { os << ", ("; prt.reset(); } std::for_each(v.begin(),v.end(),prt(os)); if (os.iword(IO::mode) == IO::PRETTY) os << "))"; return os; } #endif // CGAL_CARTESIAN_NO_OSTREAM_INSERT_VECTORCD #ifndef CGAL_CARTESIAN_NO_ISTREAM_EXTRACT_VECTORCD template < class R > std::istream & operator>>(std::istream &is, VectorCd &v) { typedef typename VectorCd::FT FT; int dim; FT* w; switch(is.iword(IO::mode)) { case IO::ASCII : case IO::BINARY : is >> dim; w = new FT[dim]; std::copy_n(std::istream_iterator(is),dim, w); break; default: std::cerr << "" << std::endl; std::cerr << "Stream must be in ascii or binary mode" << std::endl; break; } if (is) v = VectorCd(dim, w, w+dim); return is; } #endif // CGAL_CARTESIAN_NO_ISTREAM_EXTRACT_VECTORCD CGAL_END_NAMESPACE #ifdef CGAL_CFG_TYPENAME_BUG #undef typename #endif #endif // CGAL_CARTESIAN_VECTOR_D_C