57 #define OPENMESH_VECTOR_HH 60 #define TEMPLATE_HEADER template <typename Scalar, int N> 61 #define CLASSNAME VectorT 62 #define DERIVED VectorDataT<Scalar,N> 63 #define unroll(expr) for (int i=0; i<N; ++i) expr(i) 67 #if defined( OPENMESH_VECTOR_HH ) 72 class CLASSNAME :
public DERIVED
87 static inline int dim() {
return DIM; }
90 static inline size_t size() {
return DIM; }
92 static const size_t size_ = DIM;
108 inline VectorT(
const Scalar v0,
const Scalar v1) {
110 Base::values_[0] = v0; Base::values_[1] = v1;
115 inline VectorT(
const Scalar v0,
const Scalar v1,
const Scalar v2) {
117 Base::values_[0]=v0; Base::values_[1]=v1; Base::values_[2]=v2;
122 inline VectorT(
const Scalar v0,
const Scalar v1,
124 const Scalar v2,
const Scalar v3) {
125 Base::values_[0]=v0; Base::values_[1]=v1; Base::values_[2]=v2; Base::values_[3]=v3;
128 VectorT homogenized()
const {
return VectorT(Base::values_[0]/Base::values_[3], Base::values_[1]/Base::values_[3], Base::values_[2]/Base::values_[3], 1); }
132 inline VectorT(
const Scalar v0,
const Scalar v1,
const Scalar v2,
134 const Scalar v3,
const Scalar v4) {
135 Base::values_[0]=v0; Base::values_[1]=v1;Base::values_[2]=v2; Base::values_[3]=v3; Base::values_[4]=v4;
140 inline VectorT(
const Scalar v0,
const Scalar v1,
const Scalar v2,
142 const Scalar v3,
const Scalar v4,
const Scalar v5) {
143 Base::values_[0]=v0; Base::values_[1]=v1; Base::values_[2]=v2;
144 Base::values_[3]=v3; Base::values_[4]=v4; Base::values_[5]=v5;
149 explicit inline VectorT(
const Scalar _values[DIM]) {
150 memcpy(data(), _values, DIM*
sizeof(Scalar));
157 inline vector_type& operator=(
const vector_type& _rhs) {
158 memcpy(Base::values_, _rhs.Base::values_, DIM*
sizeof(Scalar));
165 template<
typename otherScalarType>
176 template<
typename otherScalarType>
178 #define expr(i) Base::values_[i] = (Scalar)_rhs[i]; 191 inline Scalar*
data() {
return Base::values_; }
194 inline const Scalar*
data()
const {
return Base::values_; }
211 assert(_i<DIM);
return Base::values_[_i];
216 assert(_i<DIM);
return Base::values_[_i];
226 #define expr(i) if(Base::values_[i]!=_rhs.Base::values_[i]) return false; 234 return !(*
this == _rhs);
244 #define expr(i) Base::values_[i] *= _s; 253 #define expr(i) Base::values_[i] /= _s; 263 return vector_type(*
this) *= _s;
265 #define expr(i) Base::values_[i] * _s 266 return vector_type(unroll_csv(expr));
275 return vector_type(*
this) /= _s;
277 #define expr(i) Base::values_[i] / _s 278 return vector_type(unroll_csv(expr));
292 #define expr(i) Base::values_[i] *= _rhs[i]; 300 #define expr(i) Base::values_[i] /= _rhs[i]; 308 #define expr(i) Base::values_[i] -= _rhs[i]; 316 #define expr(i) Base::values_[i] += _rhs[i]; 324 inline vector_type
operator*(
const vector_type& _v)
const {
326 return vector_type(*
this) *= _v;
328 #define expr(i) Base::values_[i] * _v.Base::values_[i] 329 return vector_type(unroll_csv(expr));
336 inline vector_type
operator/(
const vector_type& _v)
const {
338 return vector_type(*
this) /= _v;
340 #define expr(i) Base::values_[i] / _v.Base::values_[i] 341 return vector_type(unroll_csv(expr));
348 inline vector_type
operator+(
const vector_type& _v)
const {
350 return vector_type(*
this) += _v;
352 #define expr(i) Base::values_[i] + _v.Base::values_[i] 353 return vector_type(unroll_csv(expr));
360 inline vector_type
operator-(
const vector_type& _v)
const {
362 return vector_type(*
this) -= _v;
364 #define expr(i) Base::values_[i] - _v.Base::values_[i] 365 return vector_type(unroll_csv(expr));
374 #define expr(i) v.Base::values_[i] = -Base::values_[i]; 387 VectorT<Scalar,3>(Base::values_[1]*_rhs.Base::values_[2]-Base::values_[2]*_rhs.Base::values_[1],
388 Base::values_[2]*_rhs.Base::values_[0]-Base::values_[0]*_rhs.Base::values_[2],
389 Base::values_[0]*_rhs.Base::values_[1]-Base::values_[1]*_rhs.Base::values_[0]);
398 inline Scalar
operator|(
const vector_type& _rhs)
const {
400 #define expr(i) p += Base::values_[i] * _rhs.Base::values_[i]; 414 inline Scalar norm()
const {
return (Scalar)sqrt(sqrnorm()); }
416 inline Scalar
length()
const {
return norm(); }
423 #define expr(i) s += Base::values_[i] * Base::values_[i]; 428 #define expr(i) Base::values_[i]*Base::values_[i] 429 return (unroll_comb(expr, +));
448 return *
this / norm();
456 if (n != (Scalar)0.0)
475 #define expr(i) s += std::abs(Base::values_[i]); 480 #define expr(i) std::abs(Base::values_[i]) 481 return (unroll_comb(expr, +));
502 Scalar m(Base::values_[0]);
503 for(
int i=1; i<DIM; ++i) if(Base::values_[i]>m) m=Base::values_[i];
510 Scalar m(std::abs(Base::values_[0]));
511 for(
int i=1; i<DIM; ++i)
512 if(std::abs(Base::values_[i])>m)
513 m=std::abs(Base::values_[i]);
521 Scalar m(Base::values_[0]);
522 for(
int i=1; i<DIM; ++i)
if(Base::values_[i]<m) m=Base::values_[i];
529 Scalar m(std::abs(Base::values_[0]));
530 for(
int i=1; i<DIM; ++i)
531 if(std::abs(Base::values_[i])<m)
532 m=std::abs(Base::values_[i]);
538 Scalar m(Base::values_[0]);
539 for(
int i=1; i<DIM; ++i) m+=Base::values_[i];
540 return m/Scalar(DIM);
545 Scalar m(std::abs(Base::values_[0]));
546 for(
int i=1; i<DIM; ++i) m+=std::abs(Base::values_[i]);
547 return m/Scalar(DIM);
552 inline vector_type&
minimize(
const vector_type& _rhs) {
553 #define expr(i) if (_rhs[i] < Base::values_[i]) Base::values_[i] = _rhs[i]; 562 #define expr(i) if (_rhs[i] < Base::values_[i]) { Base::values_[i] = _rhs[i]; result = true; } 569 inline vector_type&
maximize(
const vector_type& _rhs) {
570 #define expr(i) if (_rhs[i] > Base::values_[i]) Base::values_[i] = _rhs[i]; 579 #define expr(i) if (_rhs[i] > Base::values_[i]) { Base::values_[i] =_rhs[i]; result = true; } 586 inline vector_type
min(
const vector_type& _rhs)
const {
587 return vector_type(*this).
minimize(_rhs);
591 inline vector_type
max(
const vector_type& _rhs)
const {
592 return vector_type(*this).
maximize(_rhs);
600 template<
typename Functor>
601 inline vector_type
apply(
const Functor& _func)
const {
603 #define expr(i) result[i] = _func(Base::values_[i]); 611 #define expr(i) Base::values_[i] = _s; 626 #define expr(i) if (Base::values_[i] != _rhs.Base::values_[i]) \ 627 return (Base::values_[i] < _rhs.Base::values_[i]); 641 #define expr(i) is >> vec[i]; 651 operator<<(std::ostream& os, const VectorT<Scalar,DIM>& vec)
654 for(
int i=0; i<N-1; ++i) os << vec[i] <<
" ";
657 #define expr(i) vec[i] 658 os << unroll_comb(expr, <<
" " <<);
667 #endif // included by VectorT.hh vector_type & normalize()
normalize vector, return normalized vector
Definition: VectorT_inc.hh:437
vector_type operator*(const vector_type &_v) const
component-wise vector multiplication
Definition: VectorT_inc.hh:324
Scalar value_type
the type of the scalar used in this template
Definition: VectorT_inc.hh:81
Scalar mean_abs() const
return absolute arithmetic mean
Definition: VectorT_inc.hh:544
Scalar l1_norm() const
compute L1 (Manhattan) norm
Definition: VectorT_inc.hh:471
bool operator<(const vector_type &_rhs) const
lexicographical comparison
Definition: VectorT_inc.hh:625
const Scalar * data() const
access to const Scalar array
Definition: VectorT_inc.hh:194
static vector_type vectorized(const Scalar &_s)
store the same value in each component
Definition: VectorT_inc.hh:619
Scalar & operator[](size_t _i)
get i'th element read-write
Definition: VectorT_inc.hh:210
vector_type max(const vector_type &_rhs) const
component-wise max
Definition: VectorT_inc.hh:591
vector_type min(const vector_type &_rhs) const
component-wise min
Definition: VectorT_inc.hh:586
bool minimized(const vector_type &_rhs)
minimize values and signalize coordinate minimization
Definition: VectorT_inc.hh:560
vector_type & operator*=(const vector_type &_rhs)
component-wise self-multiplication
Definition: VectorT_inc.hh:291
static size_t size()
returns dimension of the vector
Definition: VectorT_inc.hh:90
vector_type & operator*=(const Scalar &_s)
component-wise self-multiplication with scalar
Definition: VectorT_inc.hh:243
VectorT(const Scalar &v)
special constructor for 1D vectors
Definition: VectorT_inc.hh:101
const Scalar & operator[](size_t _i) const
get i'th element read-only
Definition: VectorT_inc.hh:215
vector_type & normalize_cond()
normalize vector, return normalized vector and avoids div by zero
Definition: VectorT_inc.hh:453
vector_type & operator/=(const Scalar &_s)
component-wise self-division by scalar
Definition: VectorT_inc.hh:252
vector_type & operator/=(const vector_type &_rhs)
component-wise self-division
Definition: VectorT_inc.hh:299
vector_type & minimize(const vector_type &_rhs)
minimize values: same as *this = min(*this, _rhs), but faster
Definition: VectorT_inc.hh:552
Scalar l8_norm() const
compute l8_norm
Definition: VectorT_inc.hh:487
const vector_type normalized() const
return normalized vector
Definition: VectorT_inc.hh:446
vector_type operator*(const Scalar &_s) const
component-wise multiplication with scalar
Definition: VectorT_inc.hh:261
static int dim()
returns dimension of the vector (deprecated)
Definition: VectorT_inc.hh:87
vector_type apply(const Functor &_func) const
component-wise apply function object with Scalar operator()(Scalar).
Definition: VectorT_inc.hh:601
Scalar sqrnorm() const
compute squared euclidean norm
Definition: VectorT_inc.hh:419
Scalar operator|(const vector_type &_rhs) const
compute scalar product
Definition: VectorT_inc.hh:398
vector_type & maximize(const vector_type &_rhs)
maximize values: same as *this = max(*this, _rhs), but faster
Definition: VectorT_inc.hh:569
bool operator==(const vector_type &_rhs) const
component-wise comparison
Definition: VectorT_inc.hh:225
bool operator!=(const vector_type &_rhs) const
component-wise comparison
Definition: VectorT_inc.hh:233
Scalar min_abs() const
return the minimal absolute component
Definition: VectorT_inc.hh:527
vector_type & operator=(const VectorT< otherScalarType, N > &_rhs)
cast from vector with a different scalar type
Definition: VectorT_inc.hh:177
Scalar max() const
return the maximal component
Definition: VectorT_inc.hh:500
vector_type operator/(const Scalar &_s) const
component-wise division by with scalar
Definition: VectorT_inc.hh:273
VectorT(const VectorT< otherScalarType, N > &_rhs)
copy & cast constructor (explicit)
Definition: VectorT_inc.hh:166
VectorT()
default constructor creates uninitialized values.
Definition: VectorT_inc.hh:98
vector_type operator/(const vector_type &_v) const
component-wise vector division
Definition: VectorT_inc.hh:336
vector_type operator-(void) const
unary minus
Definition: VectorT_inc.hh:372
Scalar mean() const
return arithmetic mean
Definition: VectorT_inc.hh:537
VectorT< Scalar, N > vector_type
type of this vector
Definition: VectorT_inc.hh:84
vector_type operator+(const vector_type &_v) const
component-wise vector addition
Definition: VectorT_inc.hh:348
Scalar * data()
access to Scalar array
Definition: VectorT_inc.hh:191
Scalar min() const
return the minimal component
Definition: VectorT_inc.hh:519
vector_type operator-(const vector_type &_v) const
component-wise vector difference
Definition: VectorT_inc.hh:360
Definition: VectorT_inc.hh:72
Scalar max_abs() const
return the maximal absolute component
Definition: VectorT_inc.hh:508
vector_type & operator+=(const vector_type &_rhs)
vector self-addition
Definition: VectorT_inc.hh:315
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(const Scalar _values[N])
construct from a value array (explicit)
Definition: VectorT_inc.hh:149
Scalar length() const
compute euclidean norm
Definition: VectorT_inc.hh:416
bool maximized(const vector_type &_rhs)
maximize values and signalize coordinate maximization
Definition: VectorT_inc.hh:577
vector_type & operator-=(const vector_type &_rhs)
vector difference from this
Definition: VectorT_inc.hh:307