OpenMesh
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
VectorT_inc.hh
1 /*===========================================================================*\
2  * *
3  * OpenMesh *
4  * Copyright (C) 2001-2015 by Computer Graphics Group, RWTH Aachen *
5  * www.openmesh.org *
6  * *
7  *---------------------------------------------------------------------------*
8  * This file is part of OpenMesh. *
9  * *
10  * OpenMesh is free software: you can redistribute it and/or modify *
11  * it under the terms of the GNU Lesser General Public License as *
12  * published by the Free Software Foundation, either version 3 of *
13  * the License, or (at your option) any later version with the *
14  * following exceptions: *
15  * *
16  * If other files instantiate templates or use macros *
17  * or inline functions from this file, or you compile this file and *
18  * link it with other files to produce an executable, this file does *
19  * not by itself cause the resulting executable to be covered by the *
20  * GNU Lesser General Public License. This exception does not however *
21  * invalidate any other reasons why the executable file might be *
22  * covered by the GNU Lesser General Public License. *
23  * *
24  * OpenMesh is distributed in the hope that it will be useful, *
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
27  * GNU Lesser General Public License for more details. *
28  * *
29  * You should have received a copy of the GNU LesserGeneral Public *
30  * License along with OpenMesh. If not, *
31  * see <http://www.gnu.org/licenses/>. *
32  * *
33 \*===========================================================================*/
34 
35 /*===========================================================================*\
36  * *
37  * $Revision: 1194 $ *
38  * $Date: 2015-01-14 14:13:14 +0100 (Mi, 14 Jan 2015) $ *
39  * *
40 \*===========================================================================*/
41 
42 // Set template keywords and class names properly when
43 // parsing with doxygen. This only seems to work this way since
44 // the scope of preprocessor defines is limited to one file in doxy.
45 #ifdef DOXYGEN
46 
47 // Only used for correct doxygen parsing
48 #define OPENMESH_VECTOR_HH
49 
50 #define DIM N
51 #define TEMPLATE_HEADER template <typename Scalar, int N>
52 #define CLASSNAME VectorT
53 #define DERIVED VectorDataT<Scalar,N>
54 #define unroll(expr) for (int i=0; i<N; ++i) expr(i)
55 
56 #endif
57 
58 #if defined( OPENMESH_VECTOR_HH )
59 
60 // ----------------------------------------------------------------------------
61 
62 TEMPLATE_HEADER
63 class CLASSNAME : public DERIVED
64 {
65 private:
66  typedef DERIVED Base;
67 public:
68 
69  //---------------------------------------------------------------- class info
70 
72  typedef Scalar value_type;
73 
76 
78  static inline int dim() { return DIM; }
79 
81  static inline size_t size() { return DIM; }
82 
83  static const size_t size_ = DIM;
84 
85 
86  //-------------------------------------------------------------- constructors
87 
89  inline VectorT() {}
90 
92  explicit inline VectorT(const Scalar& v) {
93 // assert(DIM==1);
94 // values_[0] = v0;
95  vectorize(v);
96  }
97 
98 #if DIM == 2
99  inline VectorT(const Scalar v0, const Scalar v1) {
101  Base::values_[0] = v0; Base::values_[1] = v1;
102  }
103 #endif
104 
105 #if DIM == 3
106  inline VectorT(const Scalar v0, const Scalar v1, const Scalar v2) {
108  Base::values_[0]=v0; Base::values_[1]=v1; Base::values_[2]=v2;
109  }
110 #endif
111 
112 #if DIM == 4
113  inline VectorT(const Scalar v0, const Scalar v1,
115  const Scalar v2, const Scalar v3) {
116  Base::values_[0]=v0; Base::values_[1]=v1; Base::values_[2]=v2; Base::values_[3]=v3;
117  }
118 #endif
119 
120 #if DIM == 5
121  inline VectorT(const Scalar v0, const Scalar v1, const Scalar v2,
123  const Scalar v3, const Scalar v4) {
124  Base::values_[0]=v0; Base::values_[1]=v1;Base::values_[2]=v2; Base::values_[3]=v3; Base::values_[4]=v4;
125  }
126 #endif
127 
128 #if DIM == 6
129  inline VectorT(const Scalar v0, const Scalar v1, const Scalar v2,
131  const Scalar v3, const Scalar v4, const Scalar v5) {
132  Base::values_[0]=v0; Base::values_[1]=v1; Base::values_[2]=v2;
133  Base::values_[3]=v3; Base::values_[4]=v4; Base::values_[5]=v5;
134  }
135 #endif
136 
138  explicit inline VectorT(const Scalar _values[DIM]) {
139  memcpy(Base::values_, _values, DIM*sizeof(Scalar));
140  }
141 
142 
143 #ifdef OM_CC_MIPS
144  // mipspro need this method
146  inline vector_type& operator=(const vector_type& _rhs) {
147  memcpy(Base::values_, _rhs.Base::values_, DIM*sizeof(Scalar));
148  return *this;
149  }
150 #endif
151 
152 
154  template<typename otherScalarType>
155  explicit inline VectorT(const VectorT<otherScalarType,DIM>& _rhs) {
156  operator=(_rhs);
157  }
158 
159 
160 
161 
162  //--------------------------------------------------------------------- casts
163 
165  template<typename otherScalarType>
166  inline vector_type& operator=(const VectorT<otherScalarType,DIM>& _rhs) {
167 #define expr(i) Base::values_[i] = (Scalar)_rhs[i];
168  unroll(expr);
169 #undef expr
170  return *this;
171  }
172 
173 // /// cast to Scalar array
174 // inline operator Scalar*() { return Base::values_; }
175 
176 // /// cast to const Scalar array
177 // inline operator const Scalar*() const { return Base::values_; }
178 
180  inline Scalar* data() { return Base::values_; }
181 
183  inline const Scalar*data() const { return Base::values_; }
184 
185 
186 
187 
188  //----------------------------------------------------------- element access
189 
190 // /// get i'th element read-write
191 // inline Scalar& operator[](int _i) {
192 // assert(_i>=0 && _i<DIM); return Base::values_[_i];
193 // }
194 
195 // /// get i'th element read-only
196 // inline const Scalar& operator[](int _i) const {
197 // assert(_i>=0 && _i<DIM); return Base::values_[_i];
198 // }
199 
201  inline Scalar& operator[](size_t _i) {
202  assert(_i<DIM); return Base::values_[_i];
203  }
204 
206  inline const Scalar& operator[](size_t _i) const {
207  assert(_i<DIM); return Base::values_[_i];
208  }
209 
210 
211 
212 
213  //---------------------------------------------------------------- comparsion
214 
216  inline bool operator==(const vector_type& _rhs) const {
217 #define expr(i) if(Base::values_[i]!=_rhs.Base::values_[i]) return false;
218  unroll(expr);
219 #undef expr
220  return true;
221  }
222 
224  inline bool operator!=(const vector_type& _rhs) const {
225  return !(*this == _rhs);
226  }
227 
228 
229 
230 
231  //---------------------------------------------------------- scalar operators
232 
234  inline vector_type& operator*=(const Scalar& _s) {
235 #define expr(i) Base::values_[i] *= _s;
236  unroll(expr);
237 #undef expr
238  return *this;
239  }
240 
243  inline vector_type& operator/=(const Scalar& _s) {
244 #define expr(i) Base::values_[i] /= _s;
245  unroll(expr);
246 #undef expr
247  return *this;
248  }
249 
250 
252  inline vector_type operator*(const Scalar& _s) const {
253 #if DIM==N
254  return vector_type(*this) *= _s;
255 #else
256 #define expr(i) Base::values_[i] * _s
257  return vector_type(unroll_csv(expr));
258 #undef expr
259 #endif
260  }
261 
262 
264  inline vector_type operator/(const Scalar& _s) const {
265 #if DIM==N
266  return vector_type(*this) /= _s;
267 #else
268 #define expr(i) Base::values_[i] / _s
269  return vector_type(unroll_csv(expr));
270 #undef expr
271 #endif
272  }
273 
274 
275 
276 
277 
278 
279  //---------------------------------------------------------- vector operators
280 
282  inline vector_type& operator*=(const vector_type& _rhs) {
283 #define expr(i) Base::values_[i] *= _rhs[i];
284  unroll(expr);
285 #undef expr
286  return *this;
287  }
288 
290  inline vector_type& operator/=(const vector_type& _rhs) {
291 #define expr(i) Base::values_[i] /= _rhs[i];
292  unroll(expr);
293 #undef expr
294  return *this;
295  }
296 
298  inline vector_type& operator-=(const vector_type& _rhs) {
299 #define expr(i) Base::values_[i] -= _rhs[i];
300  unroll(expr);
301 #undef expr
302  return *this;
303  }
304 
306  inline vector_type& operator+=(const vector_type& _rhs) {
307 #define expr(i) Base::values_[i] += _rhs[i];
308  unroll(expr);
309 #undef expr
310  return *this;
311  }
312 
313 
315  inline vector_type operator*(const vector_type& _v) const {
316 #if DIM==N
317  return vector_type(*this) *= _v;
318 #else
319 #define expr(i) Base::values_[i] * _v.Base::values_[i]
320  return vector_type(unroll_csv(expr));
321 #undef expr
322 #endif
323  }
324 
325 
327  inline vector_type operator/(const vector_type& _v) const {
328 #if DIM==N
329  return vector_type(*this) /= _v;
330 #else
331 #define expr(i) Base::values_[i] / _v.Base::values_[i]
332  return vector_type(unroll_csv(expr));
333 #undef expr
334 #endif
335  }
336 
337 
339  inline vector_type operator+(const vector_type& _v) const {
340 #if DIM==N
341  return vector_type(*this) += _v;
342 #else
343 #define expr(i) Base::values_[i] + _v.Base::values_[i]
344  return vector_type(unroll_csv(expr));
345 #undef expr
346 #endif
347  }
348 
349 
351  inline vector_type operator-(const vector_type& _v) const {
352 #if DIM==N
353  return vector_type(*this) -= _v;
354 #else
355 #define expr(i) Base::values_[i] - _v.Base::values_[i]
356  return vector_type(unroll_csv(expr));
357 #undef expr
358 #endif
359  }
360 
361 
363  inline vector_type operator-(void) const {
364  vector_type v;
365 #define expr(i) v.Base::values_[i] = -Base::values_[i];
366  unroll(expr);
367 #undef expr
368  return v;
369  }
370 
371 
374  inline VectorT<Scalar,3> operator%(const VectorT<Scalar,3>& _rhs) const
375 #if DIM==3
376  {
377  return
378  VectorT<Scalar,3>(Base::values_[1]*_rhs.Base::values_[2]-Base::values_[2]*_rhs.Base::values_[1],
379  Base::values_[2]*_rhs.Base::values_[0]-Base::values_[0]*_rhs.Base::values_[2],
380  Base::values_[0]*_rhs.Base::values_[1]-Base::values_[1]*_rhs.Base::values_[0]);
381  }
382 #else
383  ;
384 #endif
385 
386 
389  inline Scalar operator|(const vector_type& _rhs) const {
390  Scalar p(0);
391 #define expr(i) p += Base::values_[i] * _rhs.Base::values_[i];
392  unroll(expr);
393 #undef expr
394  return p;
395  }
396 
397 
398 
399 
400 
401  //------------------------------------------------------------ euclidean norm
402 
404 
405  inline Scalar norm() const { return (Scalar)sqrt(sqrnorm()); }
407  inline Scalar length() const { return norm(); } // OpenSG interface
408 
410  inline Scalar sqrnorm() const
411  {
412 #if DIM==N
413  Scalar s(0);
414 #define expr(i) s += Base::values_[i] * Base::values_[i];
415  unroll(expr);
416 #undef expr
417  return s;
418 #else
419 #define expr(i) Base::values_[i]*Base::values_[i]
420  return (unroll_comb(expr, +));
421 #undef expr
422 #endif
423  }
424 
428  inline vector_type& normalize()
429  {
430  *this /= norm();
431  return *this;
432  }
433 
437  inline const vector_type normalized() const
438  {
439  return *this / norm();
440  }
441 
444  inline vector_type& normalize_cond()
445  {
446  Scalar n = norm();
447  if (n != (Scalar)0.0)
448  {
449  *this /= n;
450  }
451  return *this;
452  }
453 
455 
456  //------------------------------------------------------------ euclidean norm
457 
459 
460 
462  inline Scalar l1_norm() const
463  {
464 #if DIM==N
465  Scalar s(0);
466 #define expr(i) s += std::abs(Base::values_[i]);
467  unroll(expr);
468 #undef expr
469  return s;
470 #else
471 #define expr(i) std::abs(Base::values_[i])
472  return (unroll_comb(expr, +));
473 #undef expr
474 #endif
475  }
476 
478  inline Scalar l8_norm() const
479  {
480  return max_abs();
481  }
482 
484 
485  //------------------------------------------------------------ max, min, mean
486 
488 
489 
491  inline Scalar max() const
492  {
493  Scalar m(Base::values_[0]);
494  for(int i=1; i<DIM; ++i) if(Base::values_[i]>m) m=Base::values_[i];
495  return m;
496  }
497 
499  inline Scalar max_abs() const
500  {
501  Scalar m(std::abs(Base::values_[0]));
502  for(int i=1; i<DIM; ++i)
503  if(std::abs(Base::values_[i])>m)
504  m=std::abs(Base::values_[i]);
505  return m;
506  }
507 
508 
510  inline Scalar min() const
511  {
512  Scalar m(Base::values_[0]);
513  for(int i=1; i<DIM; ++i) if(Base::values_[i]<m) m=Base::values_[i];
514  return m;
515  }
516 
518  inline Scalar min_abs() const
519  {
520  Scalar m(std::abs(Base::values_[0]));
521  for(int i=1; i<DIM; ++i)
522  if(std::abs(Base::values_[i])<m)
523  m=std::abs(Base::values_[i]);
524  return m;
525  }
526 
528  inline Scalar mean() const {
529  Scalar m(Base::values_[0]);
530  for(int i=1; i<DIM; ++i) m+=Base::values_[i];
531  return m/Scalar(DIM);
532  }
533 
535  inline Scalar mean_abs() const {
536  Scalar m(std::abs(Base::values_[0]));
537  for(int i=1; i<DIM; ++i) m+=std::abs(Base::values_[i]);
538  return m/Scalar(DIM);
539  }
540 
541 
543  inline vector_type& minimize(const vector_type& _rhs) {
544 #define expr(i) if (_rhs[i] < Base::values_[i]) Base::values_[i] = _rhs[i];
545  unroll(expr);
546 #undef expr
547  return *this;
548  }
549 
551  inline bool minimized(const vector_type& _rhs) {
552  bool result(false);
553 #define expr(i) if (_rhs[i] < Base::values_[i]) { Base::values_[i] = _rhs[i]; result = true; }
554  unroll(expr);
555 #undef expr
556  return result;
557  }
558 
560  inline vector_type& maximize(const vector_type& _rhs) {
561 #define expr(i) if (_rhs[i] > Base::values_[i]) Base::values_[i] = _rhs[i];
562  unroll(expr);
563 #undef expr
564  return *this;
565  }
566 
568  inline bool maximized(const vector_type& _rhs) {
569  bool result(false);
570 #define expr(i) if (_rhs[i] > Base::values_[i]) { Base::values_[i] =_rhs[i]; result = true; }
571  unroll(expr);
572 #undef expr
573  return result;
574  }
575 
577  inline vector_type min(const vector_type& _rhs) const {
578  return vector_type(*this).minimize(_rhs);
579  }
580 
582  inline vector_type max(const vector_type& _rhs) const {
583  return vector_type(*this).maximize(_rhs);
584  }
585 
587 
588  //------------------------------------------------------------ misc functions
589 
591  template<typename Functor>
592  inline vector_type apply(const Functor& _func) const {
593  vector_type result;
594 #define expr(i) result[i] = _func(Base::values_[i]);
595  unroll(expr);
596 #undef expr
597  return result;
598  }
599 
601  vector_type& vectorize(const Scalar& _s) {
602 #define expr(i) Base::values_[i] = _s;
603  unroll(expr);
604 #undef expr
605  return *this;
606  }
607 
608 
610  static vector_type vectorized(const Scalar& _s) {
611  return vector_type().vectorize(_s);
612  }
613 
614 
616  bool operator<(const vector_type& _rhs) const {
617 #define expr(i) if (Base::values_[i] != _rhs.Base::values_[i]) \
618  return (Base::values_[i] < _rhs.Base::values_[i]);
619  unroll(expr);
620 #undef expr
621  return false;
622  }
623 };
624 
625 
626 
628 TEMPLATE_HEADER
629 inline std::istream&
630 operator>>(std::istream& is, VectorT<Scalar,DIM>& vec)
631 {
632 #define expr(i) is >> vec[i];
633  unroll(expr);
634 #undef expr
635  return is;
636 }
637 
638 
640 TEMPLATE_HEADER
641 inline std::ostream&
642 operator<<(std::ostream& os, const VectorT<Scalar,DIM>& vec)
643 {
644 #if DIM==N
645  for(int i=0; i<N-1; ++i) os << vec[i] << " ";
646  os << vec[N-1];
647 #else
648 #define expr(i) vec[i]
649  os << unroll_comb(expr, << " " <<);
650 #undef expr
651 #endif
652 
653  return os;
654 }
655 
656 
657 // ----------------------------------------------------------------------------
658 #endif // included by VectorT.hh
659 //=============================================================================
vector_type min(const vector_type &_rhs) const
component-wise min
Definition: VectorT_inc.hh:577
vector_type max(const vector_type &_rhs) const
component-wise max
Definition: VectorT_inc.hh:582
vector_type operator*(const Scalar &_s) const
component-wise multiplication with scalar
Definition: VectorT_inc.hh:252
Scalar length() const
compute euclidean norm
Definition: VectorT_inc.hh:407
vector_type & normalize_cond()
normalize vector, return normalized vector and avoids div by zero
Definition: VectorT_inc.hh:444
vector_type & operator/=(const Scalar &_s)
component-wise self-division by scalar
Definition: VectorT_inc.hh:243
Scalar operator|(const vector_type &_rhs) const
compute scalar product
Definition: VectorT_inc.hh:389
Scalar max() const
return the maximal component
Definition: VectorT_inc.hh:491
vector_type & maximize(const vector_type &_rhs)
maximize values: same as *this = max(*this, _rhs), but faster
Definition: VectorT_inc.hh:560
Scalar mean_abs() const
return absolute arithmetic mean
Definition: VectorT_inc.hh:535
Scalar l8_norm() const
compute l8_norm
Definition: VectorT_inc.hh:478
Scalar mean() const
return arithmetic mean
Definition: VectorT_inc.hh:528
static int dim()
returns dimension of the vector (deprecated)
Definition: VectorT_inc.hh:78
VectorT(const VectorT< otherScalarType, N > &_rhs)
copy & cast constructor (explicit)
Definition: VectorT_inc.hh:155
VectorT< Scalar, N > vector_type
type of this vector
Definition: VectorT_inc.hh:75
vector_type & vectorize(const Scalar &_s)
store the same value in each component (e.g. to clear all entries)
Definition: VectorT_inc.hh:601
Definition: VectorT_inc.hh:63
bool operator<(const vector_type &_rhs) const
lexicographical comparison
Definition: VectorT_inc.hh:616
bool minimized(const vector_type &_rhs)
minimize values and signalize coordinate minimization
Definition: VectorT_inc.hh:551
vector_type operator/(const vector_type &_v) const
component-wise vector division
Definition: VectorT_inc.hh:327
Scalar l1_norm() const
compute L1 (Manhattan) norm
Definition: VectorT_inc.hh:462
VectorT(const Scalar _values[N])
construct from a value array (explicit)
Definition: VectorT_inc.hh:138
bool maximized(const vector_type &_rhs)
maximize values and signalize coordinate maximization
Definition: VectorT_inc.hh:568
static size_t size()
returns dimension of the vector
Definition: VectorT_inc.hh:81
vector_type & operator=(const VectorT< otherScalarType, N > &_rhs)
cast from vector with a different scalar type
Definition: VectorT_inc.hh:166
vector_type & operator*=(const Scalar &_s)
component-wise self-multiplication with scalar
Definition: VectorT_inc.hh:234
const Scalar * data() const
access to const Scalar array
Definition: VectorT_inc.hh:183
vector_type & normalize()
normalize vector, return normalized vector
Definition: VectorT_inc.hh:428
Scalar min_abs() const
return the minimal absolute component
Definition: VectorT_inc.hh:518
VectorT(const Scalar &v)
special constructor for 1D vectors
Definition: VectorT_inc.hh:92
Scalar sqrnorm() const
compute squared euclidean norm
Definition: VectorT_inc.hh:410
vector_type operator-(void) const
unary minus
Definition: VectorT_inc.hh:363
bool operator==(const vector_type &_rhs) const
component-wise comparison
Definition: VectorT_inc.hh:216
vector_type operator-(const vector_type &_v) const
component-wise vector difference
Definition: VectorT_inc.hh:351
static vector_type vectorized(const Scalar &_s)
store the same value in each component
Definition: VectorT_inc.hh:610
vector_type apply(const Functor &_func) const
component-wise apply function object with Scalar operator()(Scalar).
Definition: VectorT_inc.hh:592
VectorT()
default constructor creates uninitialized values.
Definition: VectorT_inc.hh:89
vector_type operator*(const vector_type &_v) const
component-wise vector multiplication
Definition: VectorT_inc.hh:315
Scalar max_abs() const
return the maximal absolute component
Definition: VectorT_inc.hh:499
Scalar value_type
the type of the scalar used in this template
Definition: VectorT_inc.hh:72
const vector_type normalized() const
return normalized vector
Definition: VectorT_inc.hh:437
Scalar * data()
access to Scalar array
Definition: VectorT_inc.hh:180
vector_type & operator+=(const vector_type &_rhs)
vector self-addition
Definition: VectorT_inc.hh:306
vector_type operator+(const vector_type &_v) const
component-wise vector addition
Definition: VectorT_inc.hh:339
vector_type operator/(const Scalar &_s) const
component-wise division by with scalar
Definition: VectorT_inc.hh:264
Scalar & operator[](size_t _i)
get i'th element read-write
Definition: VectorT_inc.hh:201
const Scalar & operator[](size_t _i) const
get i'th element read-only
Definition: VectorT_inc.hh:206
bool operator!=(const vector_type &_rhs) const
component-wise comparison
Definition: VectorT_inc.hh:224
vector_type & operator-=(const vector_type &_rhs)
vector difference from this
Definition: VectorT_inc.hh:298
Scalar min() const
return the minimal component
Definition: VectorT_inc.hh:510
vector_type & operator/=(const vector_type &_rhs)
component-wise self-division
Definition: VectorT_inc.hh:290
vector_type & operator*=(const vector_type &_rhs)
component-wise self-multiplication
Definition: VectorT_inc.hh:282
vector_type & minimize(const vector_type &_rhs)
minimize values: same as *this = min(*this, _rhs), but faster
Definition: VectorT_inc.hh:543

acg pic Project OpenMesh, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .