VectorT_inc.hh
/* ========================================================================= *
* *
* OpenMesh *
* Copyright (c) 2001-2015, RWTH-Aachen University *
* Department of Computer Graphics and Multimedia *
* All rights reserved. *
* www.openmesh.org *
* *
*---------------------------------------------------------------------------*
* This file is part of OpenMesh. *
*---------------------------------------------------------------------------*
* *
* Redistribution and use in source and binary forms, with or without *
* modification, are permitted provided that the following conditions *
* are met: *
* *
* 1. Redistributions of source code must retain the above copyright notice, *
* this list of conditions and the following disclaimer. *
* *
* 2. Redistributions in binary form must reproduce the above copyright *
* notice, this list of conditions and the following disclaimer in the *
* documentation and/or other materials provided with the distribution. *
* *
* 3. Neither the name of the copyright holder nor the names of its *
* contributors may be used to endorse or promote products derived from *
* this software without specific prior written permission. *
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
* *
* ========================================================================= */
/*===========================================================================*\
* *
* $Revision$ *
* $Date$ *
* *
\*===========================================================================*/
// Set template keywords and class names properly when
// parsing with doxygen. This only seems to work this way since
// the scope of preprocessor defines is limited to one file in doxy.
#ifdef DOXYGEN
// Only used for correct doxygen parsing
#define OPENMESH_VECTOR_HH
#define DIM N
#define TEMPLATE_HEADER template <typename Scalar, int N>
#define CLASSNAME VectorT
#define DERIVED VectorDataT<Scalar,N>
#define unroll(expr) for (int i=0; i<N; ++i) expr(i)
#endif
#if defined( OPENMESH_VECTOR_HH )
// ----------------------------------------------------------------------------
TEMPLATE_HEADER
class CLASSNAME : public DERIVED
{
private:
typedef DERIVED Base;
public:
//---------------------------------------------------------------- class info
typedef Scalar value_type;
typedef VectorT<Scalar,DIM> vector_type;
static inline int dim() { return DIM; }
static inline size_t size() { return DIM; }
static const size_t size_ = DIM;
//-------------------------------------------------------------- constructors
inline VectorT() {}
explicit inline VectorT(const Scalar& v) {
// assert(DIM==1);
// values_[0] = v0;
vectorize(v);
}
#if DIM == 2
inline VectorT(const Scalar v0, const Scalar v1) {
Base::values_[0] = v0; Base::values_[1] = v1;
}
#endif
#if DIM == 3
inline VectorT(const Scalar v0, const Scalar v1, const Scalar v2) {
Base::values_[0]=v0; Base::values_[1]=v1; Base::values_[2]=v2;
}
#endif
#if DIM == 4
inline VectorT(const Scalar v0, const Scalar v1,
const Scalar v2, const Scalar v3) {
Base::values_[0]=v0; Base::values_[1]=v1; Base::values_[2]=v2; Base::values_[3]=v3;
}
VectorT homogenized() const { return VectorT(Base::values_[0]/Base::values_[3], Base::values_[1]/Base::values_[3], Base::values_[2]/Base::values_[3], 1); }
#endif
#if DIM == 5
inline VectorT(const Scalar v0, const Scalar v1, const Scalar v2,
const Scalar v3, const Scalar v4) {
Base::values_[0]=v0; Base::values_[1]=v1;Base::values_[2]=v2; Base::values_[3]=v3; Base::values_[4]=v4;
}
#endif
#if DIM == 6
inline VectorT(const Scalar v0, const Scalar v1, const Scalar v2,
const Scalar v3, const Scalar v4, const Scalar v5) {
Base::values_[0]=v0; Base::values_[1]=v1; Base::values_[2]=v2;
Base::values_[3]=v3; Base::values_[4]=v4; Base::values_[5]=v5;
}
#endif
explicit inline VectorT(const Scalar _values[DIM]) {
memcpy(data(), _values, DIM*sizeof(Scalar));
}
#ifdef OM_CC_MIPS
// mipspro need this method
inline vector_type& operator=(const vector_type& _rhs) {
memcpy(Base::values_, _rhs.Base::values_, DIM*sizeof(Scalar));
return *this;
}
#endif
template<typename otherScalarType>
explicit inline VectorT(const VectorT<otherScalarType,DIM>& _rhs) {
operator=(_rhs);
}
//--------------------------------------------------------------------- casts
template<typename otherScalarType>
inline vector_type& operator=(const VectorT<otherScalarType,DIM>& _rhs) {
#define expr(i) Base::values_[i] = (Scalar)_rhs[i];
unroll(expr);
#undef expr
return *this;
}
// /// cast to Scalar array
// inline operator Scalar*() { return Base::values_; }
// /// cast to const Scalar array
// inline operator const Scalar*() const { return Base::values_; }
inline Scalar* data() { return Base::values_; }
inline const Scalar*data() const { return Base::values_; }
//----------------------------------------------------------- element access
// /// get i'th element read-write
// inline Scalar& operator[](int _i) {
// assert(_i>=0 && _i<DIM); return Base::values_[_i];
// }
// /// get i'th element read-only
// inline const Scalar& operator[](int _i) const {
// assert(_i>=0 && _i<DIM); return Base::values_[_i];
// }
inline Scalar& operator[](size_t _i) {
assert(_i<DIM); return Base::values_[_i];
}
inline const Scalar& operator[](size_t _i) const {
assert(_i<DIM); return Base::values_[_i];
}
//---------------------------------------------------------------- comparsion
inline bool operator==(const vector_type& _rhs) const {
#define expr(i) if(Base::values_[i]!=_rhs.Base::values_[i]) return false;
unroll(expr);
#undef expr
return true;
}
inline bool operator!=(const vector_type& _rhs) const {
return !(*this == _rhs);
}
//---------------------------------------------------------- scalar operators
inline vector_type& operator*=(const Scalar& _s) {
#define expr(i) Base::values_[i] *= _s;
unroll(expr);
#undef expr
return *this;
}
inline vector_type& operator/=(const Scalar& _s) {
#define expr(i) Base::values_[i] /= _s;
unroll(expr);
#undef expr
return *this;
}
inline vector_type operator*(const Scalar& _s) const {
#if DIM==N
return vector_type(*this) *= _s;
#else
#define expr(i) Base::values_[i] * _s
return vector_type(unroll_csv(expr));
#undef expr
#endif
}
inline vector_type operator/(const Scalar& _s) const {
#if DIM==N
return vector_type(*this) /= _s;
#else
#define expr(i) Base::values_[i] / _s
return vector_type(unroll_csv(expr));
#undef expr
#endif
}
//---------------------------------------------------------- vector operators
inline vector_type& operator*=(const vector_type& _rhs) {
#define expr(i) Base::values_[i] *= _rhs[i];
unroll(expr);
#undef expr
return *this;
}
inline vector_type& operator/=(const vector_type& _rhs) {
#define expr(i) Base::values_[i] /= _rhs[i];
unroll(expr);
#undef expr
return *this;
}
inline vector_type& operator-=(const vector_type& _rhs) {
#define expr(i) Base::values_[i] -= _rhs[i];
unroll(expr);
#undef expr
return *this;
}
inline vector_type& operator+=(const vector_type& _rhs) {
#define expr(i) Base::values_[i] += _rhs[i];
unroll(expr);
#undef expr
return *this;
}
inline vector_type operator*(const vector_type& _v) const {
#if DIM==N
return vector_type(*this) *= _v;
#else
#define expr(i) Base::values_[i] * _v.Base::values_[i]
return vector_type(unroll_csv(expr));
#undef expr
#endif
}
inline vector_type operator/(const vector_type& _v) const {
#if DIM==N
return vector_type(*this) /= _v;
#else
#define expr(i) Base::values_[i] / _v.Base::values_[i]
return vector_type(unroll_csv(expr));
#undef expr
#endif
}
inline vector_type operator+(const vector_type& _v) const {
#if DIM==N
return vector_type(*this) += _v;
#else
#define expr(i) Base::values_[i] + _v.Base::values_[i]
return vector_type(unroll_csv(expr));
#undef expr
#endif
}
inline vector_type operator-(const vector_type& _v) const {
#if DIM==N
return vector_type(*this) -= _v;
#else
#define expr(i) Base::values_[i] - _v.Base::values_[i]
return vector_type(unroll_csv(expr));
#undef expr
#endif
}
inline vector_type operator-(void) const {
vector_type v;
#define expr(i) v.Base::values_[i] = -Base::values_[i];
unroll(expr);
#undef expr
return v;
}
inline VectorT<Scalar,3> operator%(const VectorT<Scalar,3>& _rhs) const
#if DIM==3
{
return
VectorT<Scalar,3>(Base::values_[1]*_rhs.Base::values_[2]-Base::values_[2]*_rhs.Base::values_[1],
Base::values_[2]*_rhs.Base::values_[0]-Base::values_[0]*_rhs.Base::values_[2],
Base::values_[0]*_rhs.Base::values_[1]-Base::values_[1]*_rhs.Base::values_[0]);
}
#else
;
#endif
inline Scalar operator|(const vector_type& _rhs) const {
Scalar p(0);
#define expr(i) p += Base::values_[i] * _rhs.Base::values_[i];
unroll(expr);
#undef expr
return p;
}
//------------------------------------------------------------ euclidean norm
inline Scalar norm() const { return (Scalar)sqrt(sqrnorm()); }
inline Scalar length() const { return norm(); } // OpenSG interface
inline Scalar sqrnorm() const
{
#if DIM==N
Scalar s(0);
#define expr(i) s += Base::values_[i] * Base::values_[i];
unroll(expr);
#undef expr
return s;
#else
#define expr(i) Base::values_[i]*Base::values_[i]
return (unroll_comb(expr, +));
#undef expr
#endif
}
inline vector_type& normalize()
{
*this /= norm();
return *this;
}
inline const vector_type normalized() const
{
return *this / norm();
}
inline vector_type& normalize_cond()
{
Scalar n = norm();
if (n != (Scalar)0.0)
{
*this /= n;
}
return *this;
}
//------------------------------------------------------------ euclidean norm
inline Scalar l1_norm() const
{
#if DIM==N
Scalar s(0);
#define expr(i) s += std::abs(Base::values_[i]);
unroll(expr);
#undef expr
return s;
#else
#define expr(i) std::abs(Base::values_[i])
return (unroll_comb(expr, +));
#undef expr
#endif
}
inline Scalar l8_norm() const
{
return max_abs();
}
//------------------------------------------------------------ max, min, mean
inline Scalar max() const
{
Scalar m(Base::values_[0]);
for(int i=1; i<DIM; ++i) if(Base::values_[i]>m) m=Base::values_[i];
return m;
}
inline Scalar max_abs() const
{
Scalar m(std::abs(Base::values_[0]));
for(int i=1; i<DIM; ++i)
if(std::abs(Base::values_[i])>m)
m=std::abs(Base::values_[i]);
return m;
}
inline Scalar min() const
{
Scalar m(Base::values_[0]);
for(int i=1; i<DIM; ++i) if(Base::values_[i]<m) m=Base::values_[i];
return m;
}
inline Scalar min_abs() const
{
Scalar m(std::abs(Base::values_[0]));
for(int i=1; i<DIM; ++i)
if(std::abs(Base::values_[i])<m)
m=std::abs(Base::values_[i]);
return m;
}
inline Scalar mean() const {
Scalar m(Base::values_[0]);
for(int i=1; i<DIM; ++i) m+=Base::values_[i];
return m/Scalar(DIM);
}
inline Scalar mean_abs() const {
Scalar m(std::abs(Base::values_[0]));
for(int i=1; i<DIM; ++i) m+=std::abs(Base::values_[i]);
return m/Scalar(DIM);
}
inline vector_type& minimize(const vector_type& _rhs) {
#define expr(i) if (_rhs[i] < Base::values_[i]) Base::values_[i] = _rhs[i];
unroll(expr);
#undef expr
return *this;
}
inline bool minimized(const vector_type& _rhs) {
bool result(false);
#define expr(i) if (_rhs[i] < Base::values_[i]) { Base::values_[i] = _rhs[i]; result = true; }
unroll(expr);
#undef expr
return result;
}
inline vector_type& maximize(const vector_type& _rhs) {
#define expr(i) if (_rhs[i] > Base::values_[i]) Base::values_[i] = _rhs[i];
unroll(expr);
#undef expr
return *this;
}
inline bool maximized(const vector_type& _rhs) {
bool result(false);
#define expr(i) if (_rhs[i] > Base::values_[i]) { Base::values_[i] =_rhs[i]; result = true; }
unroll(expr);
#undef expr
return result;
}
inline vector_type min(const vector_type& _rhs) const {
return vector_type(*this).minimize(_rhs);
}
inline vector_type max(const vector_type& _rhs) const {
return vector_type(*this).maximize(_rhs);
}
//------------------------------------------------------------ misc functions
template<typename Functor>
inline vector_type apply(const Functor& _func) const {
vector_type result;
#define expr(i) result[i] = _func(Base::values_[i]);
unroll(expr);
#undef expr
return result;
}
vector_type& vectorize(const Scalar& _s) {
#define expr(i) Base::values_[i] = _s;
unroll(expr);
#undef expr
return *this;
}
static vector_type vectorized(const Scalar& _s) {
return vector_type().vectorize(_s);
}
bool operator<(const vector_type& _rhs) const {
#define expr(i) if (Base::values_[i] != _rhs.Base::values_[i]) \
return (Base::values_[i] < _rhs.Base::values_[i]);
unroll(expr);
#undef expr
return false;
}
};
TEMPLATE_HEADER
inline std::istream&
operator>>(std::istream& is, VectorT<Scalar,DIM>& vec)
{
#define expr(i) is >> vec[i];
unroll(expr);
#undef expr
return is;
}
TEMPLATE_HEADER
inline std::ostream&
operator<<(std::ostream& os, const VectorT<Scalar,DIM>& vec)
{
#if DIM==N
for(int i=0; i<N-1; ++i) os << vec[i] << " ";
os << vec[N-1];
#else
#define expr(i) vec[i]
os << unroll_comb(expr, << " " <<);
#undef expr
#endif
return os;
}
// ----------------------------------------------------------------------------
#endif // included by VectorT.hh
//=============================================================================
VectorT::normalize
vector_type & normalize()
normalize vector, return normalized vector
Definition: VectorT_inc.hh:437
VectorT::operator*
vector_type operator*(const vector_type &_v) const
component-wise vector multiplication
Definition: VectorT_inc.hh:324
VectorT::value_type
Scalar value_type
the type of the scalar used in this template
Definition: VectorT_inc.hh:81
VectorT::mean_abs
Scalar mean_abs() const
return absolute arithmetic mean
Definition: VectorT_inc.hh:544
VectorT::l1_norm
Scalar l1_norm() const
compute L1 (Manhattan) norm
Definition: VectorT_inc.hh:471
VectorT::operator<
bool operator<(const vector_type &_rhs) const
lexicographical comparison
Definition: VectorT_inc.hh:625
VectorT::data
const Scalar * data() const
access to const Scalar array
Definition: VectorT_inc.hh:194
VectorT::vectorized
static vector_type vectorized(const Scalar &_s)
store the same value in each component
Definition: VectorT_inc.hh:619
VectorT::operator[]
Scalar & operator[](size_t _i)
get i&#39;th element read-write
Definition: VectorT_inc.hh:210
VectorT::max
vector_type max(const vector_type &_rhs) const
component-wise max
Definition: VectorT_inc.hh:591
VectorT::min
vector_type min(const vector_type &_rhs) const
component-wise min
Definition: VectorT_inc.hh:586
VectorT::minimized
bool minimized(const vector_type &_rhs)
minimize values and signalize coordinate minimization
Definition: VectorT_inc.hh:560
VectorT::operator*=
vector_type & operator*=(const vector_type &_rhs)
component-wise self-multiplication
Definition: VectorT_inc.hh:291
VectorT::size
static size_t size()
returns dimension of the vector
Definition: VectorT_inc.hh:90
VectorT::operator*=
vector_type & operator*=(const Scalar &_s)
component-wise self-multiplication with scalar
Definition: VectorT_inc.hh:243
VectorT::VectorT
VectorT(const Scalar &v)
special constructor for 1D vectors
Definition: VectorT_inc.hh:101
VectorT::operator[]
const Scalar & operator[](size_t _i) const
get i&#39;th element read-only
Definition: VectorT_inc.hh:215
VectorT::normalize_cond
vector_type & normalize_cond()
normalize vector, return normalized vector and avoids div by zero
Definition: VectorT_inc.hh:453
VectorT::operator/=
vector_type & operator/=(const Scalar &_s)
component-wise self-division by scalar
Definition: VectorT_inc.hh:252
VectorT::operator/=
vector_type & operator/=(const vector_type &_rhs)
component-wise self-division
Definition: VectorT_inc.hh:299
VectorT::minimize
vector_type & minimize(const vector_type &_rhs)
minimize values: same as *this = min(*this, _rhs), but faster
Definition: VectorT_inc.hh:552
VectorT::l8_norm
Scalar l8_norm() const
compute l8_norm
Definition: VectorT_inc.hh:487
VectorT::normalized
const vector_type normalized() const
return normalized vector
Definition: VectorT_inc.hh:446
VectorT::operator*
vector_type operator*(const Scalar &_s) const
component-wise multiplication with scalar
Definition: VectorT_inc.hh:261
VectorT::dim
static int dim()
returns dimension of the vector (deprecated)
Definition: VectorT_inc.hh:87
VectorT::apply
vector_type apply(const Functor &_func) const
component-wise apply function object with Scalar operator()(Scalar).
Definition: VectorT_inc.hh:601
VectorT::sqrnorm
Scalar sqrnorm() const
compute squared euclidean norm
Definition: VectorT_inc.hh:419
VectorT::operator|
Scalar operator|(const vector_type &_rhs) const
compute scalar product
Definition: VectorT_inc.hh:398
VectorT::maximize
vector_type & maximize(const vector_type &_rhs)
maximize values: same as *this = max(*this, _rhs), but faster
Definition: VectorT_inc.hh:569
VectorT::operator==
bool operator==(const vector_type &_rhs) const
component-wise comparison
Definition: VectorT_inc.hh:225
VectorT::operator!=
bool operator!=(const vector_type &_rhs) const
component-wise comparison
Definition: VectorT_inc.hh:233
VectorT::min_abs
Scalar min_abs() const
return the minimal absolute component
Definition: VectorT_inc.hh:527
VectorT::operator=
vector_type & operator=(const VectorT< otherScalarType, N > &_rhs)
cast from vector with a different scalar type
Definition: VectorT_inc.hh:177
VectorT::max
Scalar max() const
return the maximal component
Definition: VectorT_inc.hh:500
VectorT::operator/
vector_type operator/(const Scalar &_s) const
component-wise division by with scalar
Definition: VectorT_inc.hh:273
VectorT::VectorT
VectorT(const VectorT< otherScalarType, N > &_rhs)
copy & cast constructor (explicit)
Definition: VectorT_inc.hh:166
VectorT::VectorT
VectorT()
default constructor creates uninitialized values.
Definition: VectorT_inc.hh:98
VectorT::operator/
vector_type operator/(const vector_type &_v) const
component-wise vector division
Definition: VectorT_inc.hh:336
VectorT::operator-
vector_type operator-(void) const
unary minus
Definition: VectorT_inc.hh:372
VectorT::mean
Scalar mean() const
return arithmetic mean
Definition: VectorT_inc.hh:537
VectorT::vector_type
VectorT< Scalar, N > vector_type
type of this vector
Definition: VectorT_inc.hh:84
VectorT::operator+
vector_type operator+(const vector_type &_v) const
component-wise vector addition
Definition: VectorT_inc.hh:348
VectorT::data
Scalar * data()
access to Scalar array
Definition: VectorT_inc.hh:191
VectorT::min
Scalar min() const
return the minimal component
Definition: VectorT_inc.hh:519
VectorT::operator-
vector_type operator-(const vector_type &_v) const
component-wise vector difference
Definition: VectorT_inc.hh:360
VectorT
Definition: VectorT_inc.hh:72
VectorT::max_abs
Scalar max_abs() const
return the maximal absolute component
Definition: VectorT_inc.hh:508
VectorT::operator+=
vector_type & operator+=(const vector_type &_rhs)
vector self-addition
Definition: VectorT_inc.hh:315
VectorT::vectorize
vector_type & vectorize(const Scalar &_s)
store the same value in each component (e.g. to clear all entries)
Definition: VectorT_inc.hh:610
VectorT::VectorT
VectorT(const Scalar _values[N])
construct from a value array (explicit)
Definition: VectorT_inc.hh:149
VectorT::length
Scalar length() const
compute euclidean norm
Definition: VectorT_inc.hh:416
VectorT::maximized
bool maximized(const vector_type &_rhs)
maximize values and signalize coordinate maximization
Definition: VectorT_inc.hh:577
VectorT::operator-=
vector_type & operator-=(const vector_type &_rhs)
vector difference from this
Definition: VectorT_inc.hh:307