TVector.h
Go to the documentation of this file.
1 #ifndef imath_TVector_included
2 #define imath_TVector_included
3 
4 
5 // Qt includes
6 #include <QtCore/QtGlobal>
7 #if QT_VERSION >= 0x050000
8 #include <QtCore/QtMath>
9 #else
10 #include <QtCore/qmath.h>
11 #endif
12 
13 // ACF includes
14 #include <iser/IArchive.h>
15 
16 #include <imath/imath.h>
17 
18 
19 namespace imath
20 {
21 
22 
26 template <int Size, class Element = double>
27 class TVector
28 {
29 public:
30  typedef Element ElementType;
31  typedef Element Elements[Size];
32 
36  TVector();
40  TVector(const TVector<Size, Element>& vector);
41 
45  const Element& GetElement(int i) const;
46 
50  Element& GetElementRef(int i);
51 
55  void SetElement(int i, const Element& value);
56 
60  void SetAllElements(const Element& value);
61 
66  void Reset();
67 
72  void Clear();
73 
77  const typename TVector<Size, Element>::Elements& GetElements() const;
82 
86  void Translate(const TVector<Size, Element>& vector);
87 
92 
96  void GetTranslated(const TVector<Size, Element>& vector, TVector<Size, Element>& result);
97 
102  void ScaledCumulate(const TVector<Size, Element>& vector, Element scale);
103 
107  bool IsNull(Element tolerance = I_BIG_EPSILON) const;
108 
112  Element GetDotProduct(const TVector<Size, Element>& vector) const;
113 
117  Element GetLength2() const;
121  Element GetLength() const;
122 
126  Element GetDistance2(const TVector<Size, Element>& vector) const;
127 
131  Element GetDistance(const TVector<Size, Element>& vector) const;
132 
136  Element GetElementsSum() const;
137 
143  bool Normalize(Element length = 1.0);
149  bool GetNormalized(TVector<Size, Element>& result, Element length = 1.0) const;
150 
154  void GetMinimal(const TVector<Size, Element>& vector, TVector<Size, Element>& result) const;
158  void GetMaximal(const TVector<Size, Element>& vector, TVector<Size, Element>& result) const;
159 
163  bool Serialize(iser::IArchive& archive);
164 
165  bool operator==(const TVector<Size, Element>& vector) const;
166  bool operator!=(const TVector<Size, Element>& vector) const;
167  bool operator<(const TVector<Size, Element>& vector) const;
168  bool operator>(const TVector<Size, Element>& vector) const;
169  bool operator<=(const TVector<Size, Element>& vector) const;
170  bool operator>=(const TVector<Size, Element>& vector) const;
171 
173 
176  TVector<Size, Element> operator*(Element scalar) const;
177  TVector<Size, Element> operator/(Element scalar) const;
178 
181  TVector<Size, Element>& operator*=(Element scalar);
182  TVector<Size, Element>& operator/=(Element scalar);
183 
184  const Element& operator[](int i) const;
185  Element& operator[](int i);
186 
187  // static methods
188 
192  static int GetElementsCount();
193 
200  static bool SetElementsCount(int count);
201 
205  static const TVector<Size, Element>& GetZero();
206 
207 protected:
209 
210 private:
211  explicit TVector(Element vector);
212 
213  static TVector<Size, Element> s_zero;
214 };
215 
216 
217 // inline constructors
218 
219 template <int Size, class Element>
221 {
222 }
223 
224 
225 template <int Size, class Element>
227 {
228  for (int i = 0; i < Size; ++i){
229  m_elements[i] = vector.m_elements[i];
230  }
231 }
232 
233 
234 // inline methods
235 
236 template <int Size, class Element>
237 inline const Element& TVector<Size, Element>::GetElement(int i) const
238 {
239  return operator[](i);
240 }
241 
242 
243 template <int Size, class Element>
245 {
246  return operator[](i);
247 }
248 
249 
250 template <int Size, class Element>
251 inline void TVector<Size, Element>::SetElement(int i, const Element& value)
252 {
253  operator[](i) = value;
254 }
255 
256 
257 template <int Size, class Element>
258 inline void TVector<Size, Element>::SetAllElements(const Element& value)
259 {
260  for (int i = 0; i < Size; ++i){
261  m_elements[i] = value;
262  }
263 }
264 
265 
266 template <int Size, class Element>
268 {
269  for (int i = 0; i < Size; ++i){
270  m_elements[i] = 0;
271  }
272 }
273 
274 
275 template <int Size, class Element>
277 {
278  for (int i = 0; i < Size; ++i){
279  m_elements[i] = 0;
280  }
281 }
282 
283 
284 template <int Size, class Element>
286 {
287  return m_elements;
288 }
289 
290 
291 template <int Size, class Element>
293 {
294  return m_elements;
295 }
296 
297 
298 template <int Size, class Element>
300 {
301  for (int i = 0; i < Size; ++i){
302  m_elements[i] += vector.m_elements[i];
303  }
304 }
305 
306 
307 template <int Size, class Element>
309 {
310  return *this + vector;
311 }
312 
313 
314 template <int Size, class Element>
316 {
317  result = *this + vector;
318 }
319 
320 
321 template <int Size, class Element>
322 inline void TVector<Size, Element>::ScaledCumulate(const TVector<Size, Element>& vector, Element scale)
323 {
324  for (int i = 0; i < Size; ++i){
325  m_elements[i] += vector.m_elements[i] * scale;
326  }
327 }
328 
329 
330 template <int Size, class Element>
331 inline bool TVector<Size, Element>::IsNull(Element tolerance) const
332 {
333  return GetLength2() <= tolerance * tolerance;
334 }
335 
336 
337 template <int Size, class Element>
339 {
340  Element retVal = 0.0;
341 
342  for (int i = 0; i < Size; ++i){
343  retVal += m_elements[i] * vector.m_elements[i];
344  }
345 
346  return retVal;
347 }
348 
349 
350 template <int Size, class Element>
352 {
353  return GetDotProduct(*this);
354 }
355 
356 
357 template <int Size, class Element>
358 inline Element TVector<Size, Element>::GetLength() const
359 {
360  return qSqrt(GetLength2());
361 }
362 
363 
364 template <int Size, class Element>
366 {
367  return (*this - vector).GetLength2();
368 }
369 
370 
371 template <int Size, class Element>
373 {
374  return qSqrt(GetDistance2(vector));
375 }
376 
377 
378 // operators
379 
380 template <int Size, class Element>
382 {
383  for (int i = 0; i < Size; ++i){
384  if (m_elements[i] != vector.m_elements[i]){
385  return false;
386  }
387  }
388  return true;
389 }
390 
391 
392 template <int Size, class Element>
394 {
395  return !operator==(vector);
396 }
397 
398 
399 template <int Size, class Element>
401 {
402  for (int i = 0; i < Size; ++i){
403  if (m_elements[i] > vector.m_elements[i]){
404  return false;
405  }
406  else if (m_elements[i] < vector.m_elements[i]){
407  return true;
408  }
409  }
410 
411  return false;
412 }
413 
414 
415 template <int Size, class Element>
417 {
418  for (int i = 0; i < Size; ++i){
419  if (m_elements[i] > vector.m_elements[i]){
420  return true;
421  }
422  else if (m_elements[i] < vector.m_elements[i]){
423  return false;
424  }
425  }
426 
427  return false;
428 }
429 
430 
431 template <int Size, class Element>
433 {
434  for (int i = 0; i < Size; ++i){
435  if (m_elements[i] > vector.m_elements[i]){
436  return false;
437  }
438  else if (m_elements[i] < vector.m_elements[i]){
439  return true;
440  }
441  }
442 
443  return true;
444 }
445 
446 
447 template <int Size, class Element>
449 {
450  for (int i = 0; i < Size; ++i){
451  if (m_elements[i] > vector.m_elements[i]){
452  return true;
453  }
454  else if (m_elements[i] < vector.m_elements[i]){
455  return false;
456  }
457  }
458 
459  return true;
460 }
461 
462 
463 template <int Size, class Element>
465 {
466  for (int i = 0; i < Size; ++i){
467  m_elements[i] += vector.m_elements[i];
468  }
469 
470  return *this;
471 }
472 
473 
474 template <int Size, class Element>
476 {
477  for (int i = 0; i < Size; ++i){
478  m_elements[i] -= vector.m_elements[i];
479  }
480 
481  return *this;
482 }
483 
484 
485 template <int Size, class Element>
487 {
488  for (int i = 0; i < Size; ++i){
489  m_elements[i] *= scalar;
490  }
491 
492  return *this;
493 }
494 
495 
496 template <int Size, class Element>
498 {
499  for (int i = 0; i < Size; ++i){
500  m_elements[i] /= scalar;
501  }
502 
503  return *this;
504 }
505 
506 
507 template <int Size, class Element>
509 {
510  TVector<Size, Element> retVal;
511 
512  for (int i = 0; i < Size; ++i){
513  retVal.m_elements[i] = -m_elements[i];
514  }
515 
516  return retVal;
517 }
518 
519 
520 template <int Size, class Element>
522 {
523  TVector<Size, Element> retVal;
524 
525  for (int i = 0; i < Size; ++i){
526  retVal.m_elements[i] = m_elements[i] + vector.m_elements[i];
527  }
528 
529  return retVal;
530 }
531 
532 
533 template <int Size, class Element>
535 {
536  TVector<Size, Element> retVal;
537 
538  for (int i = 0; i < Size; ++i){
539  retVal.m_elements[i] = m_elements[i] - vector.m_elements[i];
540  }
541 
542  return retVal;
543 }
544 
545 
546 template <int Size, class Element>
548 {
549  TVector<Size, Element> retVal;
550 
551  for (int i = 0; i < Size; ++i){
552  retVal.m_elements[i] = m_elements[i] * scalar;
553  }
554 
555  return retVal;
556 }
557 
558 
559 template <int Size, class Element>
561 {
562  TVector<Size, Element> retVal;
563 
564  for (int i = 0; i < Size; ++i){
565  retVal.m_elements[i] = m_elements[i] / scalar;
566  }
567 
568  return retVal;
569 }
570 
571 
572 template <int Size, class Element>
573 const Element& TVector<Size, Element>::operator[](int i) const
574 {
575  Q_ASSERT(i >= 0);
576  Q_ASSERT(i < Size);
577 
578  return m_elements[i];
579 }
580 
581 
582 template <int Size, class Element>
584 {
585  Q_ASSERT(i >= 0);
586  Q_ASSERT(i < Size);
587 
588  return m_elements[i];
589 }
590 
591 
592 // static inline methods
593 
594 template <int Size, class Element>
596 {
597  return Size;
598 }
599 
600 
601 template <int Size, class Element>
603 {
604  return count == Size;
605 }
606 
607 
608 template <int Size, class Element>
610 {
611  return s_zero;
612 }
613 
614 
615 // public methods
616 
617 template <int Size, class Element>
619 {
620  Element retVal = 0;
621 
622  for (int i = 0; i < Size; ++i){
623  retVal += m_elements[i];
624  }
625 
626  return retVal;
627 }
628 
629 
630 template <int Size, class Element>
632 {
633  Element isLength = GetLength();
634 
635  Element proportion = isLength / length;
636 
637  if (qAbs(proportion) > I_BIG_EPSILON){
638  for (int i = 0; i < Size; ++i){
639  m_elements[i] = m_elements[i] / proportion;
640  }
641 
642  return true;
643  }
644  else{
645  return false;
646  }
647 }
648 
649 
650 template <int Size, class Element>
652 {
653  Element isLength = GetLength();
654 
655  Element proportion = isLength / length;
656 
657  if (qAbs(proportion) > I_BIG_EPSILON){
658  for (int i = 0; i < Size; ++i){
659  result.m_elements[i] = m_elements[i] / proportion;
660  }
661 
662  return true;
663  }
664  else{
665  return false;
666  }
667 }
668 
669 
670 template <int Size, class Element>
672 {
673  for (int i = 0; i < Size; ++i){
674  result.SetElement(i, qMin(GetElement(i), vector.GetElement(i)));
675  }
676 }
677 
678 
679 template <int Size, class Element>
681 {
682  for (int i = 0; i < Size; ++i){
683  result.SetElement(i, qMax(GetElement(i), vector.GetElement(i)));
684  }
685 }
686 
687 
688 template <int Size, class Element>
690 {
691  bool retVal = true;
692 
693  for (int i = 0; i < Size; ++i){
694  retVal = retVal && archive.Process(m_elements[i]);
695  }
696 
697  return retVal;
698 }
699 
700 
701 // private methods
702 
703 template <int Size, class Element>
704 TVector<Size, Element>::TVector(Element element)
705 {
706  for (int i = 0; i < Size; ++i){
707  m_elements[i] = element;
708  }
709 }
710 
711 
712 template <int Size, class Element>
713 TVector<Size, Element> TVector<Size, Element>::s_zero(Element(0));
714 
715 
716 } // namespace imath
717 
718 
719 #endif // !imath_TVector_included
720 
721 
Element Elements[Size]
Definition: TVector.h:31
bool Serialize(iser::IArchive &archive)
Serialize this vector to specified archive.
Definition: TVector.h:689
const TVector< Size, Element >::Elements & GetElements() const
Get read-only access to internal element container.
Definition: TVector.h:285
Elements m_elements
Definition: TVector.h:208
Element GetLength2() const
Return euclidian length square.
Definition: TVector.h:351
void SetAllElements(const Element &value)
Set some value to all elements.
Definition: TVector.h:258
bool operator!=(const TVector< Size, Element > &vector) const
Definition: TVector.h:393
TVector< Size, Element > operator-() const
Definition: TVector.h:508
void SetElement(int i, const Element &value)
Set element at specified i.
Definition: TVector.h:251
void GetMaximal(const TVector< Size, Element > &vector, TVector< Size, Element > &result) const
Get vector with maximal elements values.
Definition: TVector.h:680
TVector< Size, Element > operator+(const TVector< Size, Element > &vector) const
Definition: TVector.h:521
virtual bool Process(bool &value)=0
Process primitive type.
bool operator<(const TVector< Size, Element > &vector) const
Definition: TVector.h:400
Element ElementType
Definition: TVector.h:30
void Clear()
Set all coordinates to zero.
Definition: TVector.h:276
static const TVector< Size, Element > & GetZero()
Get vector with all coordinates set to 0.
Definition: TVector.h:609
TVector< Size, Element > operator*(Element scalar) const
Definition: TVector.h:547
Element GetLength() const
Return euclidian length.
Definition: TVector.h:358
bool GetNormalized(TVector< Size, Element > &result, Element length=1.0) const
Return normalized vector with the same direction and specified length.
Definition: TVector.h:651
bool operator==(const TVector< Size, Element > &vector) const
Definition: TVector.h:381
void Translate(const TVector< Size, Element > &vector)
Translate the point.
Definition: TVector.h:299
Represent input/output persistence archive.
Definition: IArchive.h:30
void Reset()
Set all coordinates to zero.
Definition: TVector.h:267
void ScaledCumulate(const TVector< Size, Element > &vector, Element scale)
Add second vector scaled by specified factor.
Definition: TVector.h:322
Implementation of fixed-size mathematical vector with specified type of elements. ...
Definition: TVector.h:27
bool operator>(const TVector< Size, Element > &vector) const
Definition: TVector.h:416
bool Normalize(Element length=1.0)
Normalize vector to specified length.
Definition: TVector.h:631
bool operator>=(const TVector< Size, Element > &vector) const
Definition: TVector.h:448
TVector< Size, Element > & operator-=(const TVector< Size, Element > &vector)
Definition: TVector.h:475
static int GetElementsCount()
Get number of elements.
Definition: TVector.h:595
TVector< Size, Element >::Elements & GetElementsRef()
Get access to internal element container.
Definition: TVector.h:292
TVector< Size, Element > & operator/=(Element scalar)
Definition: TVector.h:497
bool IsNull(Element tolerance=I_BIG_EPSILON) const
Check if this vector is null.
Definition: TVector.h:331
Element GetDotProduct(const TVector< Size, Element > &vector) const
Return dot product of two vectors.
Definition: TVector.h:338
TVector< Size, Element > operator/(Element scalar) const
Definition: TVector.h:560
const Element & GetElement(int i) const
Get element at specified i.
Definition: TVector.h:237
Element & GetElementRef(int i)
Get reference to element at specified i.
Definition: TVector.h:244
void GetMinimal(const TVector< Size, Element > &vector, TVector< Size, Element > &result) const
Get vector with minimal elements values.
Definition: TVector.h:671
TVector< Size, Element > GetTranslated(const TVector< Size, Element > &vector)
Get translated point.
Definition: TVector.h:308
static const double I_BIG_EPSILON
Definition: istd.h:26
TVector()
Create an uninitialized point.
Definition: TVector.h:220
Element GetDistance2(const TVector< Size, Element > &vector) const
Return distance square between two vectors.
Definition: TVector.h:365
TVector< Size, Element > & operator+=(const TVector< Size, Element > &vector)
Definition: TVector.h:464
static bool SetElementsCount(int count)
Set number of elements.
Definition: TVector.h:602
TVector< Size, Element > & operator*=(Element scalar)
Definition: TVector.h:486
bool operator<=(const TVector< Size, Element > &vector) const
Definition: TVector.h:432
Element GetElementsSum() const
Get simple sum of all elements.
Definition: TVector.h:618
const Element & operator[](int i) const
Definition: TVector.h:573
Element GetDistance(const TVector< Size, Element > &vector) const
Return distance between two vectors.
Definition: TVector.h:372

© 2007-2017 Witold Gantzke and Kirill Lepskiy