CVarVector.h
Go to the documentation of this file.
1 #ifndef imath_CVarVector_included
2 #define imath_CVarVector_included
3 
4 
5 // STL includes
6 #include <vector>
7 
8 // Qt includes
9 #include <QtCore/QtGlobal>
10 #if QT_VERSION >= 0x050000
11 #include <QtCore/QtMath>
12 #else
13 #include <QtCore/qmath.h>
14 #endif
15 #include <QtCore/QVector>
16 
17 // ACF includes
18 #include <imath/TVector.h>
19 
20 
21 namespace iser
22 {
23  class IArchive;
24 }
25 
26 
27 namespace imath
28 {
29 
30 
35 {
36 public:
37  typedef std::vector<double> Elements;
38 
42  CVarVector();
43 
47  explicit CVarVector(int componentsCount, double value = 0);
48 
52  template <typename Iterator>
53  CVarVector(Iterator beginIter, Iterator endIter);
54 
58  CVarVector(const CVarVector& vector);
59 
60  template <int Size>
61  CVarVector(const TVector<Size, double>& vector);
62 
66  bool IsEmpty() const;
67 
71  int GetElementsCount() const;
72 
77  bool SetElementsCount(int count, double value = 0);
78 
82  double GetElement(int index) const;
83 
87  double& GetElementRef(int index);
88 
92  void SetElement(int index, double value);
93 
97  void SetAllElements(double value);
98 
102  void Reset();
103 
107  void Reset(int elementsCount, double value);
108 
112  void Clear();
113 
119  void SetElementsFrom(const CVarVector& vector, double expansionValue = 0);
120 
124  const Elements& GetElements() const;
129 
135  bool EnsureElementsCount(int count, double value = 0);
136 
140  void Translate(const CVarVector& vector);
141 
145  CVarVector GetTranslated(const CVarVector& vector);
146 
150  void GetTranslated(const CVarVector& vector, CVarVector& result);
151 
156  void ScaledCumulate(const CVarVector& vector, double scale);
157 
161  bool IsNull(double tolerance = I_BIG_EPSILON) const;
162 
166  double GetDotProduct(const CVarVector& vector) const;
167 
171  double GetLength2() const;
172 
176  double GetLength() const;
177 
181  double GetDistance2(const CVarVector& vector) const;
182 
186  double GetDistance(const CVarVector& vector) const;
187 
191  double GetElementsSum() const;
192 
198  bool Normalize(double length = 1.0);
204  bool GetNormalized(CVarVector& result, double length = 1.0) const;
205 
209  void GetMinimal(const CVarVector& vector, CVarVector& result) const;
213  void GetMaximal(const CVarVector& vector, CVarVector& result) const;
214 
218  bool Serialize(iser::IArchive& archive);
219 
220  bool operator==(const CVarVector& vector) const;
221  bool operator!=(const CVarVector& vector) const;
222  bool operator<(const CVarVector& vector) const;
223  bool operator>(const CVarVector& vector) const;
224  bool operator<=(const CVarVector& vector) const;
225  bool operator>=(const CVarVector& vector) const;
226 
227  CVarVector operator-() const;
228 
229  CVarVector operator+(const CVarVector& vector) const;
230  CVarVector operator-(const CVarVector& vector) const;
231  CVarVector operator*(double scalar) const;
232  CVarVector operator/(double scalar) const;
233 
234  CVarVector& operator+=(const CVarVector& vector);
235  CVarVector& operator-=(const CVarVector& vector);
236  CVarVector& operator*=(double scalar);
237  CVarVector& operator/=(double scalar);
238 
239  CVarVector& operator=(const CVarVector& vector);
240 
241  double operator[](int i) const;
242  double& operator[](int i);
243 
244 private:
245  Elements m_elements;
246 };
247 
248 
249 // inline constructors
250 
252 {
253 }
254 
255 
256 inline CVarVector::CVarVector(int componentsCount, double value)
257 : m_elements(componentsCount, value)
258 {
259 }
260 
261 
262 inline CVarVector::CVarVector(const CVarVector& vector)
263 : m_elements(vector.m_elements)
264 {
265 }
266 
267 
268 template <typename Iterator>
269 inline CVarVector::CVarVector(Iterator beginIter, Iterator endIter)
270 : m_elements(beginIter, endIter)
271 {
272 }
273 
274 
275 // inline methods
276 
277 inline bool CVarVector::IsEmpty() const
278 {
279  return m_elements.empty();
280 }
281 
282 
284 {
285  return int(m_elements.size());
286 }
287 
288 
289 inline bool CVarVector::SetElementsCount(int count, double value)
290 {
291  Q_ASSERT(count >= 0);
292 
293  int oldCount = int(m_elements.size());
294 
295  m_elements.resize(count);
296 
297  for (int i = oldCount; i < count; ++i){
298  m_elements[i] = value;
299  }
300 
301  return true;
302 }
303 
304 
305 inline double CVarVector::GetElement(int i) const
306 {
307  return operator[](i);
308 }
309 
310 
311 inline double& CVarVector::GetElementRef(int i)
312 {
313  return operator[](i);
314 }
315 
316 
317 inline void CVarVector::SetElement(int i, double value)
318 {
319  operator[](i) = value;
320 }
321 
322 
323 inline void CVarVector::SetAllElements(double value)
324 {
325  int elementsCount = GetElementsCount();
326  for (int i = 0; i < elementsCount; ++i){
327  m_elements[i] = value;
328  }
329 }
330 
331 
332 inline void CVarVector::Reset()
333 {
334  m_elements.clear();
335 }
336 
337 
338 inline void CVarVector::Clear()
339 {
340  int elementsCount = GetElementsCount();
341  for (int i = 0; i < elementsCount; ++i){
342  m_elements[i] = 0;
343  }
344 }
345 
346 
347 inline void CVarVector::Reset(int elementsCount, double value)
348 {
349  m_elements.resize(elementsCount);
350 
351  for (int i = 0; i < elementsCount; ++i){
352  m_elements[i] = value;
353  }
354 }
355 
356 
358 {
359  return m_elements;
360 }
361 
362 
364 {
365  return m_elements;
366 }
367 
368 
369 inline void CVarVector::Translate(const CVarVector& vector)
370 {
371  Q_ASSERT(GetElementsCount() == vector.GetElementsCount());
372 
373  int elementsCount = GetElementsCount();
374  for (int i = 0; i < elementsCount; ++i){
375  m_elements[i] += vector.m_elements[i];
376  }
377 }
378 
379 
381 {
382  return *this + vector;
383 }
384 
385 
386 inline void CVarVector::GetTranslated(const CVarVector& vector, CVarVector& result)
387 {
388  result = *this + vector;
389 }
390 
391 
392 inline void CVarVector::ScaledCumulate(const CVarVector& vector, double scale)
393 {
394  Q_ASSERT(GetElementsCount() == vector.GetElementsCount());
395 
396  int elementsCount = GetElementsCount();
397  for (int i = 0; i < elementsCount; ++i){
398  m_elements[i] += vector.m_elements[i] * scale;
399  }
400 }
401 
402 
403 inline bool CVarVector::IsNull(double tolerance) const
404 {
405  return GetLength2() <= tolerance * tolerance;
406 }
407 
408 
409 inline double CVarVector::GetDotProduct(const CVarVector& vector) const
410 {
411  Q_ASSERT(GetElementsCount() == vector.GetElementsCount());
412 
413  double retVal = 0.0;
414 
415  int elementsCount = GetElementsCount();
416  for (int i = 0; i < elementsCount; ++i){
417  retVal += m_elements[i] * vector.m_elements[i];
418  }
419 
420  return retVal;
421 }
422 
423 
424 inline double CVarVector::GetLength2() const
425 {
426  return GetDotProduct(*this);
427 }
428 
429 
430 inline double CVarVector::GetLength() const
431 {
432  return qSqrt(GetLength2());
433 }
434 
435 
436 inline double CVarVector::GetDistance2(const CVarVector& vector) const
437 {
438  return (*this - vector).GetLength2();
439 }
440 
441 
442 inline double CVarVector::GetDistance(const CVarVector& vector) const
443 {
444  return qSqrt(GetDistance2(vector));
445 }
446 
447 
448 // operators
449 
450 inline bool CVarVector::operator==(const CVarVector& vector) const
451 {
452  if (m_elements.size() != vector.m_elements.size()){
453  return false;
454  }
455 
456  bool retVal = true;
457  for ( Elements::const_iterator iter1 = m_elements.begin(), iter2 = vector.m_elements.begin();
458  iter1 != m_elements.end();
459  ++iter1,++iter2){
460  retVal = retVal && qFuzzyCompare(*iter1, *iter2);
461  }
462 
463  return retVal;
464 }
465 
466 
467 inline bool CVarVector::operator!=(const CVarVector& vector) const
468 {
469  return !operator==(vector);
470 }
471 
472 
473 inline bool CVarVector::operator<(const CVarVector& vector) const
474 {
475  int count = int(m_elements.size());
476  int vectorCount = int(vector.m_elements.size());
477  int commonSize = qMin(count, vectorCount);
478  for (int i = 0; i < commonSize; ++i){
479  int element = m_elements[i];
480  int vectorElement = vector.m_elements[i];
481  if (element != vectorElement){
482  return element < vectorElement;
483  }
484  }
485 
486  return count < vectorCount;
487 }
488 
489 
490 inline bool CVarVector::operator>(const CVarVector& vector) const
491 {
492  int count = int(m_elements.size());
493  int vectorCount = int(vector.m_elements.size());
494  int commonSize = qMin(count, vectorCount);
495  for (int i = 0; i < commonSize; ++i){
496  int element = m_elements[i];
497  int vectorElement = vector.m_elements[i];
498  if (element != vectorElement){
499  return element > vectorElement;
500  }
501  }
502 
503  return count > vectorCount;
504 }
505 
506 
507 inline bool CVarVector::operator<=(const CVarVector& vector) const
508 {
509  int count = int(m_elements.size());
510  int vectorCount = int(vector.m_elements.size());
511  int commonSize = qMin(count, vectorCount);
512  for (int i = 0; i < commonSize; ++i){
513  int element = m_elements[i];
514  int vectorElement = vector.m_elements[i];
515  if (element != vectorElement){
516  return element < vectorElement;
517  }
518  }
519 
520  return count <= vectorCount;
521 }
522 
523 
524 inline bool CVarVector::operator>=(const CVarVector& vector) const
525 {
526  int count = int(m_elements.size());
527  int vectorCount = int(vector.m_elements.size());
528  int commonSize = qMin(count, vectorCount);
529  for (int i = 0; i < commonSize; ++i){
530  int element = m_elements[i];
531  int vectorElement = vector.m_elements[i];
532  if (element != vectorElement){
533  return element > vectorElement;
534  }
535  }
536 
537  return count >= vectorCount;
538 }
539 
540 
542 {
543  Q_ASSERT(GetElementsCount() == vector.GetElementsCount());
544 
545  int elementsCount = GetElementsCount();
546  for (int i = 0; i < elementsCount; ++i){
547  m_elements[i] += vector.m_elements[i];
548  }
549 
550  return *this;
551 }
552 
553 
555 {
556  Q_ASSERT(GetElementsCount() == vector.GetElementsCount());
557 
558  int elementsCount = GetElementsCount();
559  for (int i = 0; i < elementsCount; ++i){
560  m_elements[i] -= vector.m_elements[i];
561  }
562 
563  return *this;
564 }
565 
566 
567 inline CVarVector& CVarVector::operator*=(double scalar)
568 {
569  int elementsCount = GetElementsCount();
570  for (int i = 0; i < elementsCount; ++i){
571  m_elements[i] *= scalar;
572  }
573 
574  return *this;
575 }
576 
577 
578 inline CVarVector& CVarVector::operator/=(double scalar)
579 {
580  int elementsCount = GetElementsCount();
581  for (int i = 0; i < elementsCount; ++i){
582  m_elements[i] /= scalar;
583  }
584 
585  return *this;
586 }
587 
588 
590 {
591  SetElementsCount(vector.GetElementsCount(), 0);
592  SetElementsFrom(vector);
593 
594  return *this;
595 }
596 
597 
599 {
600  int elementsCount = GetElementsCount();
601 
602  CVarVector retVal(elementsCount);
603 
604  for (int i = 0; i < elementsCount; ++i){
605  retVal.m_elements[i] = -m_elements[i];
606  }
607 
608  return retVal;
609 }
610 
611 
612 inline CVarVector CVarVector::operator+(const CVarVector& vector) const
613 {
614  CVarVector retVal(*this);
615 
616  retVal += vector;
617 
618  return retVal;
619 }
620 
621 
622 inline CVarVector CVarVector::operator-(const CVarVector& vector) const
623 {
624  CVarVector retVal(*this);
625 
626  retVal -= vector;
627 
628  return retVal;
629 }
630 
631 
632 inline CVarVector CVarVector::operator*(double scalar) const
633 {
634  CVarVector retVal(*this);
635 
636  retVal *= scalar;
637 
638  return retVal;
639 }
640 
641 
642 inline CVarVector CVarVector::operator/(double scalar) const
643 {
644  CVarVector retVal(*this);
645 
646  retVal /= scalar;
647 
648  return retVal;
649 }
650 
651 
652 inline double CVarVector::operator[](int i) const
653 {
654  Q_ASSERT(i >= 0);
655  Q_ASSERT(i < GetElementsCount());
656 
657  return m_elements[i];
658 }
659 
660 
661 inline double& CVarVector::operator[](int i)
662 {
663  Q_ASSERT(i >= 0);
664  Q_ASSERT(i < GetElementsCount());
665 
666  return m_elements[i];
667 }
668 
669 
670 // template methods
671 
672 template <int Size>
674 {
675  m_elements.resize(Size);
676 
677  for (int i = 0; i < Size; ++i){
678  m_elements[i] = vector[i];
679  }
680 }
681 
682 
683 } // namespace imath
684 
685 
686 #endif // !imath_CVarVector_included
687 
688 
double GetLength2() const
Return euclidean length square.
Definition: CVarVector.h:424
double GetDistance2(const CVarVector &vector) const
Return distance square between two vectors.
Definition: CVarVector.h:436
bool operator>(const CVarVector &vector) const
Definition: CVarVector.h:490
CVarVector operator-() const
Definition: CVarVector.h:598
bool IsNull(double tolerance=I_BIG_EPSILON) const
Check if this vector is null.
Definition: CVarVector.h:403
bool SetElementsCount(int count, double value=0)
Set number of elements.
Definition: CVarVector.h:289
Elements & GetElementsRef()
Get access to internal element container.
Definition: CVarVector.h:363
double GetDotProduct(const CVarVector &vector) const
Return dot product of two vectors.
Definition: CVarVector.h:409
CVarVector operator*(double scalar) const
Definition: CVarVector.h:632
double GetElementsSum() const
Get simple sum of all elements.
void GetMinimal(const CVarVector &vector, CVarVector &result) const
Get vector with minimal elements values.
void Translate(const CVarVector &vector)
Translate the point.
Definition: CVarVector.h:369
CVarVector()
Create the vector without components.
Definition: CVarVector.h:251
CVarVector & operator/=(double scalar)
Definition: CVarVector.h:578
int GetElementsCount() const
Get number of elements.
Definition: CVarVector.h:283
CVarVector & operator-=(const CVarVector &vector)
Definition: CVarVector.h:554
void Reset()
Set size to zero.
Definition: CVarVector.h:332
CVarVector & operator*=(double scalar)
Definition: CVarVector.h:567
CVarVector operator+(const CVarVector &vector) const
Definition: CVarVector.h:612
void GetMaximal(const CVarVector &vector, CVarVector &result) const
Get vector with maximal elements values.
bool operator!=(const CVarVector &vector) const
Definition: CVarVector.h:467
bool IsEmpty() const
Get true, if the element ist is empty.
Definition: CVarVector.h:277
void SetElementsFrom(const CVarVector &vector, double expansionValue=0)
Set elements from other vector without resizing.
double GetLength() const
Return euclidian length.
Definition: CVarVector.h:430
Represent input/output persistence archive.
Definition: IArchive.h:30
CVarVector & operator=(const CVarVector &vector)
Definition: CVarVector.h:589
bool operator<=(const CVarVector &vector) const
Definition: CVarVector.h:507
double & GetElementRef(int index)
Get reference to element at specified index.
Definition: CVarVector.h:311
Implementation of fixed-size mathematical vector with specified type of elements. ...
Definition: TVector.h:27
CVarVector GetTranslated(const CVarVector &vector)
Get translated point.
Definition: CVarVector.h:380
bool operator<(const CVarVector &vector) const
Definition: CVarVector.h:473
bool Normalize(double length=1.0)
Normalize vector to specified length.
bool operator==(const CVarVector &vector) const
Definition: CVarVector.h:450
CVarVector operator/(double scalar) const
Definition: CVarVector.h:642
void SetElement(int index, double value)
Set element at specified index.
Definition: CVarVector.h:317
double operator[](int i) const
Definition: CVarVector.h:652
Simple implementation of variable-size vector.
Definition: CVarVector.h:34
static const double I_BIG_EPSILON
Definition: istd.h:26
std::vector< double > Elements
Definition: CVarVector.h:37
bool operator>=(const CVarVector &vector) const
Definition: CVarVector.h:524
bool Serialize(iser::IArchive &archive)
Serialize this vector to specified archive.
void ScaledCumulate(const CVarVector &vector, double scale)
Add second vector scaled by specified factor.
Definition: CVarVector.h:392
void SetAllElements(double value)
Set some value to all elements.
Definition: CVarVector.h:323
void Clear()
Set all coordinates to zero.
Definition: CVarVector.h:338
double GetElement(int index) const
Get element at specified index.
Definition: CVarVector.h:305
const Elements & GetElements() const
Get read-only access to internal element container.
Definition: CVarVector.h:357
double GetDistance(const CVarVector &vector) const
Return distance between two vectors.
Definition: CVarVector.h:442
bool GetNormalized(CVarVector &result, double length=1.0) const
Return normalized vector with the same direction and specified length.
bool EnsureElementsCount(int count, double value=0)
Ensure, that number of elements is at least the specified value.
CVarVector & operator+=(const CVarVector &vector)
Definition: CVarVector.h:541

© 2007-2017 Witold Gantzke and Kirill Lepskiy