58 #define OPENVOLUMEMESH_VECTOR_HH
61 #define TEMPLATE_HEADER template <typename Scalar, int N>
62 #define CLASSNAME VectorT
63 #define DERIVED VectorDataT<Scalar,N>
64 #define unroll(expr) for (int i=0; i<N; ++i) expr(i)
68 #if defined( OPENVOLUMEMESH_VECTOR_HH )
73 class CLASSNAME :
public DERIVED
82 typedef Scalar value_type;
88 static inline int dim() {
return DIM; }
91 static inline size_t size() {
return DIM; }
93 static const size_t size_ = DIM;
102 explicit inline VectorT(
const Scalar& v) {
109 inline VectorT(
const Scalar v0,
const Scalar v1) {
111 Base::values_[0] = v0; Base::values_[1] = v1;
116 inline VectorT(
const Scalar v0,
const Scalar v1,
const Scalar v2) {
118 Base::values_[0]=v0; Base::values_[1]=v1; Base::values_[2]=v2;
123 inline VectorT(
const Scalar v0,
const Scalar v1,
125 const Scalar v2,
const Scalar v3) {
126 Base::values_[0]=v0; Base::values_[1]=v1; Base::values_[2]=v2; Base::values_[3]=v3;
129 VectorT homogenized()
const {
return VectorT(Base::values_[0]/Base::values_[3], Base::values_[1]/Base::values_[3], Base::values_[2]/Base::values_[3], 1); }
133 inline VectorT(
const Scalar v0,
const Scalar v1,
const Scalar v2,
135 const Scalar v3,
const Scalar v4) {
136 Base::values_[0]=v0; Base::values_[1]=v1;Base::values_[2]=v2; Base::values_[3]=v3; Base::values_[4]=v4;
141 inline VectorT(
const Scalar v0,
const Scalar v1,
const Scalar v2,
143 const Scalar v3,
const Scalar v4,
const Scalar v5) {
144 Base::values_[0]=v0; Base::values_[1]=v1; Base::values_[2]=v2;
145 Base::values_[3]=v3; Base::values_[4]=v4; Base::values_[5]=v5;
150 explicit inline VectorT(
const Scalar _values[DIM]) {
151 memcpy(data(), _values, DIM*
sizeof(Scalar));
158 inline vector_type& operator=(
const vector_type& _rhs) {
159 memcpy(Base::values_, _rhs.Base::values_, DIM*
sizeof(Scalar));
166 template<
typename otherScalarType>
177 template<
typename otherScalarType>
179 #define expr(i) Base::values_[i] = (Scalar)_rhs[i];
192 inline Scalar* data() {
return Base::values_; }
195 inline const Scalar*data()
const {
return Base::values_; }
211 inline Scalar& operator[](
size_t _i) {
212 assert(_i<DIM);
return Base::values_[_i];
216 inline const Scalar& operator[](
size_t _i)
const {
217 assert(_i<DIM);
return Base::values_[_i];
226 inline bool operator==(
const vector_type& _rhs)
const {
227 #define expr(i) if(Base::values_[i]!=_rhs.Base::values_[i]) return false;
234 inline bool operator!=(
const vector_type& _rhs)
const {
235 return !(*
this == _rhs);
244 inline vector_type& operator*=(
const Scalar& _s) {
245 #define expr(i) Base::values_[i] *= _s;
253 inline vector_type& operator/=(
const Scalar& _s) {
254 #define expr(i) Base::values_[i] /= _s;
262 inline vector_type
operator*(
const Scalar& _s)
const {
264 return vector_type(*
this) *= _s;
266 #define expr(i) Base::values_[i] * _s
267 return vector_type(unroll_csv(expr));
274 inline vector_type operator/(
const Scalar& _s)
const {
276 return vector_type(*
this) /= _s;
278 #define expr(i) Base::values_[i] / _s
279 return vector_type(unroll_csv(expr));
292 inline vector_type& operator*=(
const vector_type& _rhs) {
293 #define expr(i) Base::values_[i] *= _rhs[i];
300 inline vector_type& operator/=(
const vector_type& _rhs) {
301 #define expr(i) Base::values_[i] /= _rhs[i];
308 inline vector_type& operator-=(
const vector_type& _rhs) {
309 #define expr(i) Base::values_[i] -= _rhs[i];
316 inline vector_type& operator+=(
const vector_type& _rhs) {
317 #define expr(i) Base::values_[i] += _rhs[i];
325 inline vector_type
operator*(
const vector_type& _v)
const {
327 return vector_type(*
this) *= _v;
329 #define expr(i) Base::values_[i] * _v.Base::values_[i]
330 return vector_type(unroll_csv(expr));
337 inline vector_type operator/(
const vector_type& _v)
const {
339 return vector_type(*
this) /= _v;
341 #define expr(i) Base::values_[i] / _v.Base::values_[i]
342 return vector_type(unroll_csv(expr));
349 inline vector_type operator+(
const vector_type& _v)
const {
351 return vector_type(*
this) += _v;
353 #define expr(i) Base::values_[i] + _v.Base::values_[i]
354 return vector_type(unroll_csv(expr));
361 inline vector_type operator-(
const vector_type& _v)
const {
363 return vector_type(*
this) -= _v;
365 #define expr(i) Base::values_[i] - _v.Base::values_[i]
366 return vector_type(unroll_csv(expr));
373 inline vector_type operator-(
void)
const {
375 #define expr(i) v.Base::values_[i] = -Base::values_[i];
388 VectorT<Scalar,3>(Base::values_[1]*_rhs.Base::values_[2]-Base::values_[2]*_rhs.Base::values_[1],
389 Base::values_[2]*_rhs.Base::values_[0]-Base::values_[0]*_rhs.Base::values_[2],
390 Base::values_[0]*_rhs.Base::values_[1]-Base::values_[1]*_rhs.Base::values_[0]);
399 inline Scalar operator|(
const vector_type& _rhs)
const {
401 #define expr(i) p += Base::values_[i] * _rhs.Base::values_[i];
415 inline Scalar norm()
const {
return (Scalar)sqrt(sqrnorm()); }
417 inline Scalar length()
const {
return norm(); }
420 inline Scalar sqrnorm()
const
424 #define expr(i) s += Base::values_[i] * Base::values_[i];
429 #define expr(i) Base::values_[i]*Base::values_[i]
430 return (unroll_comb(expr, +));
438 inline vector_type& normalize()
447 inline const vector_type normalized()
const
449 return *
this / norm();
454 inline vector_type& normalize_cond()
457 if (n != (Scalar)0.0)
472 inline Scalar l1_norm()
const
476 #define expr(i) s += std::abs(Base::values_[i]);
481 #define expr(i) std::abs(Base::values_[i])
482 return (unroll_comb(expr, +));
488 inline Scalar l8_norm()
const
501 inline Scalar max()
const
503 Scalar m(Base::values_[0]);
504 for(
int i=1; i<DIM; ++i) if(Base::values_[i]>m) m=Base::values_[i];
509 inline Scalar max_abs()
const
511 Scalar m(std::abs(Base::values_[0]));
512 for(
int i=1; i<DIM; ++i)
513 if(std::abs(Base::values_[i])>m)
514 m=std::abs(Base::values_[i]);
520 inline Scalar min()
const
522 Scalar m(Base::values_[0]);
523 for(
int i=1; i<DIM; ++i)
if(Base::values_[i]<m) m=Base::values_[i];
528 inline Scalar min_abs()
const
530 Scalar m(std::abs(Base::values_[0]));
531 for(
int i=1; i<DIM; ++i)
532 if(std::abs(Base::values_[i])<m)
533 m=std::abs(Base::values_[i]);
538 inline Scalar mean()
const {
539 Scalar m(Base::values_[0]);
540 for(
int i=1; i<DIM; ++i) m+=Base::values_[i];
541 return m/Scalar(DIM);
545 inline Scalar mean_abs()
const {
546 Scalar m(std::abs(Base::values_[0]));
547 for(
int i=1; i<DIM; ++i) m+=std::abs(Base::values_[i]);
548 return m/Scalar(DIM);
553 inline vector_type& minimize(
const vector_type& _rhs) {
554 #define expr(i) if (_rhs[i] < Base::values_[i]) Base::values_[i] = _rhs[i];
561 inline bool minimized(
const vector_type& _rhs) {
563 #define expr(i) if (_rhs[i] < Base::values_[i]) { Base::values_[i] = _rhs[i]; result = true; }
570 inline vector_type& maximize(
const vector_type& _rhs) {
571 #define expr(i) if (_rhs[i] > Base::values_[i]) Base::values_[i] = _rhs[i];
578 inline bool maximized(
const vector_type& _rhs) {
580 #define expr(i) if (_rhs[i] > Base::values_[i]) { Base::values_[i] =_rhs[i]; result = true; }
587 inline vector_type min(
const vector_type& _rhs)
const {
588 return vector_type(*this).minimize(_rhs);
592 inline vector_type max(
const vector_type& _rhs)
const {
593 return vector_type(*this).maximize(_rhs);
601 template<
typename Functor>
602 inline vector_type apply(
const Functor& _func)
const {
604 #define expr(i) result[i] = _func(Base::values_[i]);
611 vector_type& vectorize(
const Scalar& _s) {
612 #define expr(i) Base::values_[i] = _s;
620 static vector_type vectorized(
const Scalar& _s) {
621 return vector_type().vectorize(_s);
626 bool operator<(
const vector_type& _rhs)
const {
627 #define expr(i) if (Base::values_[i] != _rhs.Base::values_[i]) \
628 return (Base::values_[i] < _rhs.Base::values_[i]);
642 #define expr(i) is >> vec[i];
652 operator<<(std::ostream& os, const VectorT<Scalar,DIM>& vec)
655 for(
int i=0; i<N-1; ++i) os << vec[i] <<
" ";
658 #define expr(i) vec[i]
659 os << unroll_comb(expr, <<
" " <<);
668 #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.