52 #define OPENMESH_VECTOR_HH 55 #define TEMPLATE_HEADER template <typename Scalar, int N> 56 #define CLASSNAME VectorT 57 #define DERIVED VectorDataT<Scalar,N> 58 #define unroll(expr) for (int i=0; i<N; ++i) expr(i) 62 #if defined( OPENMESH_VECTOR_HH ) 67 class CLASSNAME :
public DERIVED
82 static inline int dim() {
return DIM; }
85 static inline size_t size() {
return DIM; }
87 static const size_t size_ = DIM;
96 explicit inline VectorT(
const Scalar& v) {
103 inline VectorT(
const Scalar v0,
const Scalar v1) {
105 Base::values_[0] = v0; Base::values_[1] = v1;
110 inline VectorT(
const Scalar v0,
const Scalar v1,
const Scalar v2) {
112 Base::values_[0]=v0; Base::values_[1]=v1; Base::values_[2]=v2;
117 inline VectorT(
const Scalar v0,
const Scalar v1,
119 const Scalar v2,
const Scalar v3) {
120 Base::values_[0]=v0; Base::values_[1]=v1; Base::values_[2]=v2; Base::values_[3]=v3;
123 VectorT homogenized()
const {
return VectorT(Base::values_[0]/Base::values_[3], Base::values_[1]/Base::values_[3], Base::values_[2]/Base::values_[3], 1); }
127 inline VectorT(
const Scalar v0,
const Scalar v1,
const Scalar v2,
129 const Scalar v3,
const Scalar v4) {
130 Base::values_[0]=v0; Base::values_[1]=v1;Base::values_[2]=v2; Base::values_[3]=v3; Base::values_[4]=v4;
135 inline VectorT(
const Scalar v0,
const Scalar v1,
const Scalar v2,
137 const Scalar v3,
const Scalar v4,
const Scalar v5) {
138 Base::values_[0]=v0; Base::values_[1]=v1; Base::values_[2]=v2;
139 Base::values_[3]=v3; Base::values_[4]=v4; Base::values_[5]=v5;
144 explicit inline VectorT(
const Scalar _values[DIM]) {
145 memcpy(data(), _values, DIM*
sizeof(Scalar));
152 inline vector_type& operator=(
const vector_type& _rhs) {
153 memcpy(Base::values_, _rhs.Base::values_, DIM*
sizeof(Scalar));
160 template<
typename otherScalarType>
171 template<
typename otherScalarType>
173 #define expr(i) Base::values_[i] = (Scalar)_rhs[i]; 186 inline Scalar*
data() {
return Base::values_; }
189 inline const Scalar*
data()
const {
return Base::values_; }
206 assert(_i<DIM);
return Base::values_[_i];
211 assert(_i<DIM);
return Base::values_[_i];
221 #define expr(i) if(Base::values_[i]!=_rhs.Base::values_[i]) return false; 229 return !(*
this == _rhs);
239 #define expr(i) Base::values_[i] *= _s; 248 #define expr(i) Base::values_[i] /= _s; 258 return vector_type(*
this) *= _s;
260 #define expr(i) Base::values_[i] * _s 261 return vector_type(unroll_csv(expr));
270 return vector_type(*
this) /= _s;
272 #define expr(i) Base::values_[i] / _s 273 return vector_type(unroll_csv(expr));
287 #define expr(i) Base::values_[i] *= _rhs[i]; 295 #define expr(i) Base::values_[i] /= _rhs[i]; 303 #define expr(i) Base::values_[i] -= _rhs[i]; 311 #define expr(i) Base::values_[i] += _rhs[i]; 319 inline vector_type
operator*(
const vector_type& _v)
const {
321 return vector_type(*
this) *= _v;
323 #define expr(i) Base::values_[i] * _v.Base::values_[i] 324 return vector_type(unroll_csv(expr));
331 inline vector_type
operator/(
const vector_type& _v)
const {
333 return vector_type(*
this) /= _v;
335 #define expr(i) Base::values_[i] / _v.Base::values_[i] 336 return vector_type(unroll_csv(expr));
343 inline vector_type
operator+(
const vector_type& _v)
const {
345 return vector_type(*
this) += _v;
347 #define expr(i) Base::values_[i] + _v.Base::values_[i] 348 return vector_type(unroll_csv(expr));
355 inline vector_type
operator-(
const vector_type& _v)
const {
357 return vector_type(*
this) -= _v;
359 #define expr(i) Base::values_[i] - _v.Base::values_[i] 360 return vector_type(unroll_csv(expr));
369 #define expr(i) v.Base::values_[i] = -Base::values_[i]; 382 VectorT<Scalar,3>(Base::values_[1]*_rhs.Base::values_[2]-Base::values_[2]*_rhs.Base::values_[1],
383 Base::values_[2]*_rhs.Base::values_[0]-Base::values_[0]*_rhs.Base::values_[2],
384 Base::values_[0]*_rhs.Base::values_[1]-Base::values_[1]*_rhs.Base::values_[0]);
393 inline Scalar
operator|(
const vector_type& _rhs)
const {
395 #define expr(i) p += Base::values_[i] * _rhs.Base::values_[i]; 409 inline Scalar norm()
const {
return (Scalar)sqrt(sqrnorm()); }
411 inline Scalar
length()
const {
return norm(); }
418 #define expr(i) s += Base::values_[i] * Base::values_[i]; 423 #define expr(i) Base::values_[i]*Base::values_[i] 424 return (unroll_comb(expr, +));
443 return *
this / norm();
451 if (n != (Scalar)0.0)
470 #define expr(i) s += std::abs(Base::values_[i]); 475 #define expr(i) std::abs(Base::values_[i]) 476 return (unroll_comb(expr, +));
497 Scalar m(Base::values_[0]);
498 for(
int i=1; i<DIM; ++i) if(Base::values_[i]>m) m=Base::values_[i];
505 Scalar m(std::abs(Base::values_[0]));
506 for(
int i=1; i<DIM; ++i)
507 if(std::abs(Base::values_[i])>m)
508 m=std::abs(Base::values_[i]);
516 Scalar m(Base::values_[0]);
517 for(
int i=1; i<DIM; ++i)
if(Base::values_[i]<m) m=Base::values_[i];
524 Scalar m(std::abs(Base::values_[0]));
525 for(
int i=1; i<DIM; ++i)
526 if(std::abs(Base::values_[i])<m)
527 m=std::abs(Base::values_[i]);
533 Scalar m(Base::values_[0]);
534 for(
int i=1; i<DIM; ++i) m+=Base::values_[i];
535 return m/Scalar(DIM);
540 Scalar m(std::abs(Base::values_[0]));
541 for(
int i=1; i<DIM; ++i) m+=std::abs(Base::values_[i]);
542 return m/Scalar(DIM);
547 inline vector_type&
minimize(
const vector_type& _rhs) {
548 #define expr(i) if (_rhs[i] < Base::values_[i]) Base::values_[i] = _rhs[i]; 557 #define expr(i) if (_rhs[i] < Base::values_[i]) { Base::values_[i] = _rhs[i]; result = true; } 564 inline vector_type&
maximize(
const vector_type& _rhs) {
565 #define expr(i) if (_rhs[i] > Base::values_[i]) Base::values_[i] = _rhs[i]; 574 #define expr(i) if (_rhs[i] > Base::values_[i]) { Base::values_[i] =_rhs[i]; result = true; } 581 inline vector_type
min(
const vector_type& _rhs)
const {
582 return vector_type(*this).
minimize(_rhs);
586 inline vector_type
max(
const vector_type& _rhs)
const {
587 return vector_type(*this).
maximize(_rhs);
595 template<
typename Functor>
596 inline vector_type
apply(
const Functor& _func)
const {
598 #define expr(i) result[i] = _func(Base::values_[i]); 606 #define expr(i) Base::values_[i] = _s; 621 #define expr(i) if (Base::values_[i] != _rhs.Base::values_[i]) \ 622 return (Base::values_[i] < _rhs.Base::values_[i]); 636 #define expr(i) is >> vec[i]; 646 operator<<(std::ostream& os, const VectorT<Scalar,DIM>& vec)
649 for(
int i=0; i<N-1; ++i) os << vec[i] <<
" ";
652 #define expr(i) vec[i] 653 os << unroll_comb(expr, <<
" " <<);
662 #endif // included by VectorT.hh bool operator==(const vector_type &_rhs) const
component-wise comparison
Definition: VectorT_inc.hh:220
Scalar length() const
compute euclidean norm
Definition: VectorT_inc.hh:411
VectorT(const VectorT< otherScalarType, N > &_rhs)
copy & cast constructor (explicit)
Definition: VectorT_inc.hh:161
Scalar min_abs() const
return the minimal absolute component
Definition: VectorT_inc.hh:522
Scalar & operator[](size_t _i)
get i'th element read-write
Definition: VectorT_inc.hh:205
Scalar sqrnorm() const
compute squared euclidean norm
Definition: VectorT_inc.hh:414
bool minimized(const vector_type &_rhs)
minimize values and signalize coordinate minimization
Definition: VectorT_inc.hh:555
VectorT()
default constructor creates uninitialized values.
Definition: VectorT_inc.hh:93
vector_type operator-(void) const
unary minus
Definition: VectorT_inc.hh:367
Scalar mean() const
return arithmetic mean
Definition: VectorT_inc.hh:532
vector_type operator*(const Scalar &_s) const
component-wise multiplication with scalar
Definition: VectorT_inc.hh:256
const Scalar * data() const
access to const Scalar array
Definition: VectorT_inc.hh:189
vector_type & operator=(const VectorT< otherScalarType, N > &_rhs)
cast from vector with a different scalar type
Definition: VectorT_inc.hh:172
bool operator!=(const vector_type &_rhs) const
component-wise comparison
Definition: VectorT_inc.hh:228
vector_type & vectorize(const Scalar &_s)
store the same value in each component (e.g. to clear all entries)
Definition: VectorT_inc.hh:605
const vector_type normalized() const
return normalized vector
Definition: VectorT_inc.hh:441
const Scalar & operator[](size_t _i) const
get i'th element read-only
Definition: VectorT_inc.hh:210
vector_type & operator/=(const vector_type &_rhs)
component-wise self-division
Definition: VectorT_inc.hh:294
VectorT(const Scalar _values[N])
construct from a value array (explicit)
Definition: VectorT_inc.hh:144
vector_type & minimize(const vector_type &_rhs)
minimize values: same as *this = min(*this, _rhs), but faster
Definition: VectorT_inc.hh:547
VectorT< Scalar, N > vector_type
type of this vector
Definition: VectorT_inc.hh:79
Scalar max_abs() const
return the maximal absolute component
Definition: VectorT_inc.hh:503
Scalar min() const
return the minimal component
Definition: VectorT_inc.hh:514
vector_type operator+(const vector_type &_v) const
component-wise vector addition
Definition: VectorT_inc.hh:343
vector_type & maximize(const vector_type &_rhs)
maximize values: same as *this = max(*this, _rhs), but faster
Definition: VectorT_inc.hh:564
bool operator<(const vector_type &_rhs) const
lexicographical comparison
Definition: VectorT_inc.hh:620
vector_type & operator*=(const Scalar &_s)
component-wise self-multiplication with scalar
Definition: VectorT_inc.hh:238
Scalar l8_norm() const
compute l8_norm
Definition: VectorT_inc.hh:482
VectorT(const Scalar &v)
special constructor for 1D vectors
Definition: VectorT_inc.hh:96
vector_type max(const vector_type &_rhs) const
component-wise max
Definition: VectorT_inc.hh:586
Scalar max() const
return the maximal component
Definition: VectorT_inc.hh:495
Scalar mean_abs() const
return absolute arithmetic mean
Definition: VectorT_inc.hh:539
Scalar * data()
access to Scalar array
Definition: VectorT_inc.hh:186
static int dim()
returns dimension of the vector (deprecated)
Definition: VectorT_inc.hh:82
bool maximized(const vector_type &_rhs)
maximize values and signalize coordinate maximization
Definition: VectorT_inc.hh:572
vector_type operator/(const Scalar &_s) const
component-wise division by with scalar
Definition: VectorT_inc.hh:268
vector_type & operator-=(const vector_type &_rhs)
vector difference from this
Definition: VectorT_inc.hh:302
static vector_type vectorized(const Scalar &_s)
store the same value in each component
Definition: VectorT_inc.hh:614
vector_type & normalize()
normalize vector, return normalized vector
Definition: VectorT_inc.hh:432
vector_type operator/(const vector_type &_v) const
component-wise vector division
Definition: VectorT_inc.hh:331
Scalar value_type
the type of the scalar used in this template
Definition: VectorT_inc.hh:76
vector_type & normalize_cond()
normalize vector, return normalized vector and avoids div by zero
Definition: VectorT_inc.hh:448
Definition: VectorT_inc.hh:67
vector_type & operator+=(const vector_type &_rhs)
vector self-addition
Definition: VectorT_inc.hh:310
static size_t size()
returns dimension of the vector
Definition: VectorT_inc.hh:85
Scalar operator|(const vector_type &_rhs) const
compute scalar product
Definition: VectorT_inc.hh:393
Scalar l1_norm() const
compute L1 (Manhattan) norm
Definition: VectorT_inc.hh:466
vector_type min(const vector_type &_rhs) const
component-wise min
Definition: VectorT_inc.hh:581
vector_type & operator*=(const vector_type &_rhs)
component-wise self-multiplication
Definition: VectorT_inc.hh:286
vector_type & operator/=(const Scalar &_s)
component-wise self-division by scalar
Definition: VectorT_inc.hh:247
vector_type operator*(const vector_type &_v) const
component-wise vector multiplication
Definition: VectorT_inc.hh:319
vector_type apply(const Functor &_func) const
component-wise apply function object with Scalar operator()(Scalar).
Definition: VectorT_inc.hh:596
vector_type operator-(const vector_type &_v) const
component-wise vector difference
Definition: VectorT_inc.hh:355