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
81 typedef Scalar value_type;
87 static inline int dim() {
return DIM; }
90 static inline size_t size() {
return DIM; }
92 static const size_t size_ = DIM;
101 explicit inline VectorT(
const Scalar& v) {
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_; }
210 inline Scalar& operator[](
size_t _i) {
211 assert(_i<DIM);
return Base::values_[_i];
215 inline const Scalar& operator[](
size_t _i)
const {
216 assert(_i<DIM);
return Base::values_[_i];
225 inline bool operator==(
const vector_type& _rhs)
const {
226 #define expr(i) if(Base::values_[i]!=_rhs.Base::values_[i]) return false; 233 inline bool operator!=(
const vector_type& _rhs)
const {
234 return !(*
this == _rhs);
243 inline vector_type& operator*=(
const Scalar& _s) {
244 #define expr(i) Base::values_[i] *= _s; 252 inline vector_type& operator/=(
const Scalar& _s) {
253 #define expr(i) Base::values_[i] /= _s; 261 inline vector_type
operator*(
const Scalar& _s)
const {
263 return vector_type(*
this) *= _s;
265 #define expr(i) Base::values_[i] * _s 266 return vector_type(unroll_csv(expr));
273 inline vector_type operator/(
const Scalar& _s)
const {
275 return vector_type(*
this) /= _s;
277 #define expr(i) Base::values_[i] / _s 278 return vector_type(unroll_csv(expr));
291 inline vector_type& operator*=(
const vector_type& _rhs) {
292 #define expr(i) Base::values_[i] *= _rhs[i]; 299 inline vector_type& operator/=(
const vector_type& _rhs) {
300 #define expr(i) Base::values_[i] /= _rhs[i]; 307 inline vector_type& operator-=(
const vector_type& _rhs) {
308 #define expr(i) Base::values_[i] -= _rhs[i]; 315 inline vector_type& operator+=(
const vector_type& _rhs) {
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));
372 inline vector_type operator-(
void)
const {
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(); }
419 inline Scalar sqrnorm()
const 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, +));
437 inline vector_type& normalize()
446 inline const vector_type normalized()
const 448 return *
this / norm();
453 inline vector_type& normalize_cond()
456 if (n != (Scalar)0.0)
471 inline Scalar l1_norm()
const 475 #define expr(i) s += std::abs(Base::values_[i]); 480 #define expr(i) std::abs(Base::values_[i]) 481 return (unroll_comb(expr, +));
487 inline Scalar l8_norm()
const 500 inline Scalar max()
const 502 Scalar m(Base::values_[0]);
503 for(
int i=1; i<DIM; ++i) if(Base::values_[i]>m) m=Base::values_[i];
508 inline Scalar max_abs()
const 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]);
519 inline Scalar min()
const 521 Scalar m(Base::values_[0]);
522 for(
int i=1; i<DIM; ++i)
if(Base::values_[i]<m) m=Base::values_[i];
527 inline Scalar min_abs()
const 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]);
537 inline Scalar mean()
const {
538 Scalar m(Base::values_[0]);
539 for(
int i=1; i<DIM; ++i) m+=Base::values_[i];
540 return m/Scalar(DIM);
544 inline Scalar mean_abs()
const {
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]; 560 inline bool minimized(
const vector_type& _rhs) {
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]; 577 inline bool maximized(
const vector_type& _rhs) {
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]); 610 vector_type& vectorize(
const Scalar& _s) {
611 #define expr(i) Base::values_[i] = _s; 619 static vector_type vectorized(
const Scalar& _s) {
620 return vector_type().vectorize(_s);
625 bool operator<(
const vector_type& _rhs)
const {
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 auto operator*(const OtherScalar &_s, const VectorT< Scalar, DIM > &rhs) -> decltype(rhs.operator*(_s))
Component wise multiplication from the left.
std::istream & operator>>(std::istream &is, Matrix4x4T< Scalar > &m)
read the space-separated components of a vector from a stream */