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
76 typedef Scalar value_type;
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_; }
205 inline Scalar& operator[](
size_t _i) {
206 assert(_i<DIM);
return Base::values_[_i];
210 inline const Scalar& operator[](
size_t _i)
const {
211 assert(_i<DIM);
return Base::values_[_i];
220 inline bool operator==(
const vector_type& _rhs)
const {
221 #define expr(i) if(Base::values_[i]!=_rhs.Base::values_[i]) return false; 228 inline bool operator!=(
const vector_type& _rhs)
const {
229 return !(*
this == _rhs);
238 inline vector_type& operator*=(
const Scalar& _s) {
239 #define expr(i) Base::values_[i] *= _s; 247 inline vector_type& operator/=(
const Scalar& _s) {
248 #define expr(i) Base::values_[i] /= _s; 256 inline vector_type
operator*(
const Scalar& _s)
const {
258 return vector_type(*
this) *= _s;
260 #define expr(i) Base::values_[i] * _s 261 return vector_type(unroll_csv(expr));
268 inline vector_type operator/(
const Scalar& _s)
const {
270 return vector_type(*
this) /= _s;
272 #define expr(i) Base::values_[i] / _s 273 return vector_type(unroll_csv(expr));
286 inline vector_type& operator*=(
const vector_type& _rhs) {
287 #define expr(i) Base::values_[i] *= _rhs[i]; 294 inline vector_type& operator/=(
const vector_type& _rhs) {
295 #define expr(i) Base::values_[i] /= _rhs[i]; 302 inline vector_type& operator-=(
const vector_type& _rhs) {
303 #define expr(i) Base::values_[i] -= _rhs[i]; 310 inline vector_type& operator+=(
const vector_type& _rhs) {
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));
367 inline vector_type operator-(
void)
const {
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(); }
414 inline Scalar sqrnorm()
const 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, +));
432 inline vector_type& normalize()
441 inline const vector_type normalized()
const 443 return *
this / norm();
448 inline vector_type& normalize_cond()
451 if (n != (Scalar)0.0)
466 inline Scalar l1_norm()
const 470 #define expr(i) s += std::abs(Base::values_[i]); 475 #define expr(i) std::abs(Base::values_[i]) 476 return (unroll_comb(expr, +));
482 inline Scalar l8_norm()
const 495 inline Scalar max()
const 497 Scalar m(Base::values_[0]);
498 for(
int i=1; i<DIM; ++i) if(Base::values_[i]>m) m=Base::values_[i];
503 inline Scalar max_abs()
const 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]);
514 inline Scalar min()
const 516 Scalar m(Base::values_[0]);
517 for(
int i=1; i<DIM; ++i)
if(Base::values_[i]<m) m=Base::values_[i];
522 inline Scalar min_abs()
const 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]);
532 inline Scalar mean()
const {
533 Scalar m(Base::values_[0]);
534 for(
int i=1; i<DIM; ++i) m+=Base::values_[i];
535 return m/Scalar(DIM);
539 inline Scalar mean_abs()
const {
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]; 555 inline bool minimized(
const vector_type& _rhs) {
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]; 572 inline bool maximized(
const vector_type& _rhs) {
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]); 605 vector_type& vectorize(
const Scalar& _s) {
606 #define expr(i) Base::values_[i] = _s; 614 static vector_type vectorized(
const Scalar& _s) {
615 return vector_type().vectorize(_s);
620 bool operator<(
const vector_type& _rhs)
const {
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 std::istream & operator>>(std::istream &is, Matrix4x4T< Scalar > &m)
read the space-separated components of a vector from a stream */
auto operator*(const OtherScalar &_s, const VectorT< Scalar, DIM > &rhs) -> decltype(rhs.operator*(_s))
Component wise multiplication from the left.