MeVisLab Toolbox Reference
mlFloatingPointVector.h
Go to the documentation of this file.
1 /*************************************************************************************
2 **
3 ** Copyright 2007, MeVis Medical Solutions AG
4 **
5 ** The user may use this file in accordance with the license agreement provided with
6 ** the Software or, alternatively, in accordance with the terms contained in a
7 ** written agreement between the user and MeVis Medical Solutions AG.
8 **
9 ** For further information use the contact form at https://www.mevislab.de/contact
10 **
11 **************************************************************************************/
12 
13 #ifndef ML_FLOATING_POINT_VECTOR_H
14 #define ML_FLOATING_POINT_VECTOR_H
15 
17 
18 //------------------------------------------------------------------------
19 // Includes
20 //----------------------------------------------------------------------
21 #include "mlLinearAlgebraSystem.h"
22 
23 #include <mlTypeDefs.h>
24 #include <mlTypeDefTraits.h>
25 #include <iostream>
26 
27 //------------------------------------------------------------------------
29 //------------------------------------------------------------------------
30 typedef double (*ML_LA_FROM_DOUBLE_TO_DOUBLE)(double);
31 
32 //------------------------------------------------------------------------
33 // Put all stuff in a specific ML_LA_NAMESPACE namespace to avoid
34 // collisions with other libraries.
35 //------------------------------------------------------------------------
36 ML_LA_START_NAMESPACE
37 
40 template <class DT, size_t size>
42 {
43 protected:
44  DT _buffer[size];
45 };
46 
47 
48 //------------------------------------------------------------------------
60 //------------------------------------------------------------------------
61 template <class T, size_t size, class DataContainer = FloatingPointVectorDataContainerBase<T, size> >
62 class FloatingPointVector : public DataContainer
63 {
64 
65 public:
66 
68  typedef T ComponentType;
69 
72  enum { Size = size };
73 
74  //----------------------------------------------------------------------
77  //----------------------------------------------------------------------
80  explicit FloatingPointVector(T value=T(0));
81 
85 
86 
87  //----------------------------------------------------------------------
90  //----------------------------------------------------------------------
93 
96 
104  bool operator <(const FloatingPointVector<T, size, DataContainer>& buffer) const;
106 
107 
108  //----------------------------------------------------------------------
111  //----------------------------------------------------------------------
115  const T& operator[](const size_t i) const;
116 
120  T& operator[](const size_t i);
122 
123 
124  //----------------------------------------------------------------------
127  //----------------------------------------------------------------------
129  size_t getSize() const;
130 
133  T norm2() const;
134 
138 
142 
146 
148  T length() const;
149 
152  {
153  return (*this - buffer).length();
154  }
155 
158  {
159  return (*this - buffer).lengthSquared();
160  }
161 
162 
163 
164 #if ML_DEPRECATED_SINCE(3,5,0)
169  inline ML_DEPRECATED T Normalize() { return normalize(); }
170 
173  inline ML_DEPRECATED T Length() const { return length(); }
174 
177  inline ML_DEPRECATED T Distance(const FloatingPointVector<T, size, DataContainer>& buffer) const
178  {
179  return distance(buffer);
180  }
183  inline ML_DEPRECATED T DistanceSquared(const FloatingPointVector<T, size, DataContainer>& buffer) const
184  {
185  return distanceSquared(buffer);
186  }
189  inline ML_DEPRECATED T Dot(const FloatingPointVector<T, size, DataContainer>& b) const
190  {
191  return dot(b);
192  }
195  inline ML_DEPRECATED T length2() const { return lengthSquared(); }
198  inline ML_DEPRECATED T LengthSquared() const { return lengthSquared(); }
199 
202  inline ML_DEPRECATED FloatingPointVector<T, 3, DataContainer> Cross(const FloatingPointVector<T, 3, DataContainer>& b) const
203  {
204  return cross(b);
205  }
207 #endif
208 
210  T lengthSquared() const;
211 
213  T compSum() const;
214 
216  T compMul() const;
217 
219  T compMaxAbs() const;
220 
223 
226 
228  void compAbs();
229 
233 
235  void compSqr();
236 
239  void compSqrt();
240 
244 
248 
256 
258  void compRound();
259 
261  void compFloor();
262 
264  void compCeil();
265 
268  {
270  r[0] = this->_buffer[1]*b._buffer[2] - this->_buffer[2]*b._buffer[1];
271  r[1] = this->_buffer[2]*b._buffer[0] - this->_buffer[0]*b._buffer[2];
272  r[2] = this->_buffer[0]*b._buffer[1] - this->_buffer[1]*b._buffer[0];
273  return r;
274  }
275 
278 
281  std::ostream& writeOut(std::ostream& os) const;
282 
284  std::istream& readIn(std::istream& is);
286 
287 }; // end of class *FloatingPointVector*
288 
289 
290 //-----------------------------------------------------------------------------------
293 //-----------------------------------------------------------------------------------
294 //-----------------------------------------------------------------------------------
297 //-----------------------------------------------------------------------------------
298 template <class T, size_t size, class DataContainer>
300 {
301  for (size_t i=0; i<size; ++i){ op1[i] += buffer[i]; }
302  return op1;
303 }
304 
305 //-----------------------------------------------------------------------------------
308 //-----------------------------------------------------------------------------------
309 template <class T, size_t size, class DataContainer>
311 {
312  for (size_t i=0; i<size; ++i){ op1[i] -= buffer[i]; }
313  return op1;
314 }
315 
316 //-----------------------------------------------------------------------------------
319 //-----------------------------------------------------------------------------------
320 template <class T, size_t size, class DataContainer>
322 {
323  for (size_t i=0; i<size; ++i){ op1[i] *= static_cast<T>(value); }
324  return op1;
325 }
326 
327 //-----------------------------------------------------------------------------------
330 //-----------------------------------------------------------------------------------
331 template <class T, size_t size, class DataContainer>
333 {
334  for (size_t i=0; i<size; ++i){ op1[i] *= op2[i]; }
335  return op1;
336 }
337 
338 //-----------------------------------------------------------------------------------
342 //-----------------------------------------------------------------------------------
343 template <class T, size_t size, class DataContainer>
345 {
346  // The desired optimization by multiplying by 1.0/value we cannot do since some
347  // people simply use this class also for integer types although that's not really ok.
348  // We don't want to force bad errors for those people; so we spend the time here...
349  for (size_t i=0; i<size; ++i){ op1[i] /= static_cast<T>(value); }
350  return op1;
351 }
352 
353 //-----------------------------------------------------------------------------------
357 //-----------------------------------------------------------------------------------
358 template <class T, size_t size, class DataContainer>
360 {
361  // The desired optimization by multiplying by 1.0/value we cannot do since some
362  // people simply use this class also for integer types although that's not really ok.
363  // We don't want to force bad errors for those people; so we spend the time here...
364  for (size_t i=0; i<size; ++i){ op1[i] /= op2[i]; }
365  return op1;
366 }
367 
368 //--------------------------------------------------------------------
371 //--------------------------------------------------------------------
372 template <class T, size_t size, class DataContainer>
374 {
375  bool v = !a[0];
376  for (size_t c=1; c<size; c++){ v &= !a[c]; }
377  return v;
378 }
379 
380 //-----------------------------------------------------------------------------------
383 //-----------------------------------------------------------------------------------
384 template <class T, size_t size, class DataContainer>
387 {
388  return lhs += rhs;
389 }
390 
391 //-----------------------------------------------------------------------------------
394 //-----------------------------------------------------------------------------------
395 template <class T, size_t size, class DataContainer>
398 {
399  return lhs -= rhs;
400 }
401 
402 //-----------------------------------------------------------------------------------
405 //-----------------------------------------------------------------------------------
406 template <class T, size_t size, class DataContainer>
408 {
410 }
411 
412 
413 //-----------------------------------------------------------------------------------
416 //-----------------------------------------------------------------------------------
417 template <class T, size_t size, class DataContainer>
419 {
421  for (size_t i=0; i<size; ++i){ buf[i] *= -1; }
422  return buf;
423 }
424 
425 //-----------------------------------------------------------------------------------
428 //-----------------------------------------------------------------------------------
429 template <class T, size_t size, class DataContainer>
431 {
432  return a.dot(b);
433 }
434 
435 //-----------------------------------------------------------------------------------
438 //-----------------------------------------------------------------------------------
439 template <class T, size_t size, class DataContainer>
441 {
442  return lhs *= static_cast<T>(rhs);
443 }
444 
445 //-----------------------------------------------------------------------------------
448 //-----------------------------------------------------------------------------------
449 template <class T, size_t size, class DataContainer>
451 {
452  return rhs *= static_cast<T>(lhs);
453 }
454 
455 //-----------------------------------------------------------------------------------
458 //-----------------------------------------------------------------------------------
459 template <class T, size_t size, class DataContainer>
461 {
462  return lhs /= static_cast<T>(rhs);
463 }
464 
465 //-----------------------------------------------------------------------------------
468 //-----------------------------------------------------------------------------------
469 template <class T, size_t size, class DataContainer>
472 {
473  buffer1.compMin(buffer2);
474  return buffer1;
475 }
476 
477 //-----------------------------------------------------------------------------------
480 //-----------------------------------------------------------------------------------
481 template <class T, size_t size, class DataContainer>
484 {
485  buffer1.compMax(buffer2);
486  return buffer1;
487 }
488 
489 //-----------------------------------------------------------------------------------
492 //-----------------------------------------------------------------------------------
493 template <class T, size_t size, class DataContainer>
495 {
496  vec.compAbs();
497  return vec;
498 }
499 
500 //-----------------------------------------------------------------------------------
503 //-----------------------------------------------------------------------------------
504 template <class T, size_t size, class DataContainer>
506 {
507  vec.compSqr();
508  return vec;
509 }
510 
511 
512 //-----------------------------------------------------------------------------------
515 //-----------------------------------------------------------------------------------
516 template <class T, size_t size, class DataContainer>
518 {
519  vec.compSqrt();
520  return vec;
521 }
522 
523 //-----------------------------------------------------------------------------------
527 //-----------------------------------------------------------------------------------
528 template <class T, size_t size, class DataContainer>
531 {
532  vec.compDiv(d);
533  return vec;
534 }
535 
536 //-----------------------------------------------------------------------------------
540 //-----------------------------------------------------------------------------------
541 template <class T, size_t size, class DataContainer>
544 {
545  vec.clampMin(m);
546  return vec;
547 }
548 
549 //-----------------------------------------------------------------------------------
553 //-----------------------------------------------------------------------------------
554 template <class T, size_t size, class DataContainer>
557 {
558  vec.clampMax(m);
559  return vec;
560 }
561 
562 //-----------------------------------------------------------------------------------
568 //-----------------------------------------------------------------------------------
569 template <class T, size_t size, class DataContainer>
573 {
574  vec.clampMax(upper);
575  vec.clampMin(lower);
576  return vec;
577 }
578 
579 //-----------------------------------------------------------------------------------
582 //-----------------------------------------------------------------------------------
583 template <class T, size_t size, class DataContainer>
585 {
586  return vec.compMul();
587 }
589 
590 ML_LA_END_NAMESPACE
591 
592 //-----------------------------------------------------------------------------------
593 // Some IO stream operators are implemented in this namespace by this library.
594 //-----------------------------------------------------------------------------------
595 namespace std {
596 
597  //-----------------------------------------------------------------------------------
599  //-----------------------------------------------------------------------------------
600  template <class T, size_t size, class DataContainer>
601  inline ostream& operator<<(ostream& os, const ML_LA_NAMESPACE::FloatingPointVector<T, size, DataContainer>& v)
602  {
603  return v.writeOut(os);
604  }
605 
606  //-----------------------------------------------------------------------------------
608  //-----------------------------------------------------------------------------------
609  template <class T, size_t size, class DataContainer>
610  inline istream& operator>>(istream& is, ML_LA_NAMESPACE::FloatingPointVector<T, size, DataContainer>& v)
611  {
612  return v.readIn(is);
613  }
614 
615 }
616 
617 ML_LA_START_NAMESPACE
618 
619 //-----------------------------------------------------------------------------------
620 //
621 // IMPLEMENTATION.
622 //
623 //-----------------------------------------------------------------------------------
624 
625 //-----------------------------------------------------------------------------------
626 // Constructors and assignment operators.
627 //-----------------------------------------------------------------------------------
628 //-----------------------------------------------------------------------------------
629 // Default and value constructor. Default is the initialization of all
630 // components with value whose default is 0.
631 //-----------------------------------------------------------------------------------
632 template <class T, size_t size, class DataContainer>
634 {
635  for (size_t i=0; i<size; ++i){ this->_buffer[i] = value; }
636 }
637 
638 //-----------------------------------------------------------------------------------
639 // Assignment of scalar value to all components.
640 //-----------------------------------------------------------------------------------
641 template <class T, size_t size, class DataContainer>
643 {
644  for (size_t i=0; i<size; ++i){ this->_buffer[i] = value; }
645  return *this;
646 }
647 
648 
649 //----------------------------------------------------------------------
650 // Comparison.
651 //----------------------------------------------------------------------
652 //-----------------------------------------------------------------------------------
653 // Returns whether *this and buffer are component wise equal.
654 //-----------------------------------------------------------------------------------
655 template <class T, size_t size, class DataContainer>
657 {
658  for (size_t i=0; i<size; ++i){
659  // Use the numerically better method (although slower to compare) floats to ensure that
660  // insignificant differences after least significant mantissa bit are not considered as
661  // difference.
662  if (MLValuesDifferWOM(this->_buffer[i], buffer._buffer[i])){ return false; }
663  }
664  return true;
665 }
666 
667 //-----------------------------------------------------------------------------------
668 // Returns whether any components of *this and buffer are not equal.
669 //-----------------------------------------------------------------------------------
670 template <class T, size_t size, class DataContainer>
672 {
673  for (size_t i=0; i<size; ++i){
674  // Use the numerically better method (although slower to compare) floats to ensure that
675  // insignificant differences after least significant mantissa bit are not considered as
676  // difference.
677  if (MLValuesDifferWOM(this->_buffer[i], buffer._buffer[i])){ return true; }
678  }
679  return false;
680 }
681 
682 //-----------------------------------------------------------------------------------
683 // Defines an artificial order for use in sort algorithms (lexicographical
684 // order). Starting from the first component(i.e. index 0) all components
685 // from *this are compared with the corresponding one in buffer:
686 // If the components of *this is smaller than the corresponding on in
687 // buffer then true is returned, if the comparison is bigger then
688 // false is returned and on equality the components for the next index are
689 // compared. If all components are equal then false is returned.
690 //-----------------------------------------------------------------------------------
691 template <class T, size_t size, class DataContainer>
693 {
694  for (size_t i=0; i<size; ++i) {
695  if (MLValuesDifferWOM(this->_buffer[i], buffer._buffer[i])){ return (this->_buffer[i]<buffer._buffer[i]); }
696  }
697  return false;
698 }
699 
700 
701 
702 
703 
704 //-----------------------------------------------------------------------------------
705 // Value access and indexing operators.
706 //-----------------------------------------------------------------------------------
707 //-----------------------------------------------------------------------------------
708 // Constant indexing operators. The caller must guarantee that is in [0, size-1].
709 // It is legal to consider all elements as a subsequent array in memory for
710 // pointer addressing of all elements.
711 //-----------------------------------------------------------------------------------
712 template <class T, size_t size, class DataContainer>
714 {
715  return this->_buffer[i];
716 }
717 
718 //-----------------------------------------------------------------------------------
719 // Indexing operator. The caller must guarantee that is in [0, size-1].
720 // It is legal to consider all elements as a subsequent array in memory for
721 // pointer addressing of all elements.
722 //-----------------------------------------------------------------------------------
723 template <class T, size_t size, class DataContainer>
725 {
726  return this->_buffer[i];
727 }
728 
729 
730 
731 
732 
733 
734 
735 
736 //----------------------------------------------------------------------
737 // Other operations.
738 //----------------------------------------------------------------------
739 //-----------------------------------------------------------------------------------
740 // Returns the number of elements of value buffer.
741 //-----------------------------------------------------------------------------------
742 template <class T, size_t size, class DataContainer>
744 {
745  return size;
746 }
747 
748 //-----------------------------------------------------------------------------------
749 // Returns the Euclidean norm (the vector length), i.e., square root of sum
750 // of squares of all components.
751 //-----------------------------------------------------------------------------------
752 template <class T, size_t size, class DataContainer>
754 {
755  T norm = 0;
756  for (size_t i=0; i<size; ++i){ norm += this->_buffer[i]*this->_buffer[i]; }
757 #ifdef WIN32
758  return static_cast<T>(sqrt(norm));
759 #else
760  return static_cast<T>(::sqrt(norm));
761 #endif
762 }
763 
764 //-----------------------------------------------------------------------------------
765 // Returns the weighted Euclidean norm, i.e., square root of sum of squares of all
766 // components multiplied with corresponding squared component of weight.
767 //-----------------------------------------------------------------------------------
768 template <class T, size_t size, class DataContainer>
770 {
771  T norm = 0;
772  for (size_t i=0; i<size; ++i){
773  norm += this->_buffer[i]*this->_buffer[i] * weight._buffer[i]*weight._buffer[i];
774  }
775 #ifdef WIN32
776  return sqrt(norm);
777 #else
778  return ::sqrt(norm);
779 #endif
780 }
781 
782 
783 //-----------------------------------------------------------------------------------
784 // Returns the dot product, i.e., sum of all components multiplied with corresponding
785 // components of buffer.
786 //-----------------------------------------------------------------------------------
787 template <class T, size_t size, class DataContainer>
789 {
790  T sum = 0;
791  for (size_t i=0; i<size; ++i){ sum += this->_buffer[i]*buffer._buffer[i]; }
792  return sum;
793 }
794 
795 //-----------------------------------------------------------------------------------
796 // Normalizes buffer and returns the Euclidean length of vector before normalization,
797 // i.e., norm2. On zero vector length the vector is left unchanged.
798 //-----------------------------------------------------------------------------------
799 template <class T, size_t size, class DataContainer>
801 {
802  T norm = norm2();
803 
804  if (!MLValueIs0WOM(norm)) {
805  T inv_norm = static_cast<T>(1.0/norm);
806  for (size_t i=0; i<size; ++i){ this->_buffer[i] *= inv_norm; }
807  }
808 
809  return norm;
810 }
811 
812 //-----------------------------------------------------------------------------------
813 // Returns the length of the vector, i.e., norm2().
814 //-----------------------------------------------------------------------------------
815 template <class T, size_t size, class DataContainer>
817 {
818  return norm2();
819 }
820 
821 //-----------------------------------------------------------------------------------
822 // Returns the squared length of vector.
823 //-----------------------------------------------------------------------------------
824 template <class T, size_t size, class DataContainer>
826 {
827  return dot(*this);
828 }
829 
830 //-----------------------------------------------------------------------------------
831 // Returns the sum of all components.
832 //-----------------------------------------------------------------------------------
833 template <class T, size_t size, class DataContainer>
835 {
836  T retVal=this->_buffer[0];
837  for (size_t i=1; i<size; ++i){ retVal += this->_buffer[i]; }
838  return retVal;
839 }
840 
841 //-----------------------------------------------------------------------------------
842 // Returns the product of all vector components.
843 //-----------------------------------------------------------------------------------
844 template <class T, size_t size, class DataContainer>
846 {
847  T retVal=this->_buffer[0];
848  for (size_t i=1; i<size; ++i){ retVal *= this->_buffer[i]; }
849  return retVal;
850 }
851 
852 //-----------------------------------------------------------------------------------
853 // Returns the maximum of absolute component values.
854 //-----------------------------------------------------------------------------------
855 template <class T, size_t size, class DataContainer>
857 {
858  size_t i = 0;
859  T m = static_cast<T>( MLAbs(this->_buffer[i++]));
860  T n = 0;
861  for (; i<size; ++i){ m = (n = static_cast<T>( MLAbs(this->_buffer[i])) ) > m ? n : m; }
862  return m;
863 }
864 
865 //-----------------------------------------------------------------------------------
866 // Sets the component wise minimum of *this and buffer in *this.
867 //-----------------------------------------------------------------------------------
868 template <class T, size_t size, class DataContainer>
870 {
871  for (size_t i=0; i<size; ++i){
872  this->_buffer[i] = this->_buffer[i] < buffer._buffer[i] ? this->_buffer[i] : buffer._buffer[i];
873  }
874 }
875 
876 //-----------------------------------------------------------------------------------
877 // Sets the component wise maximum of *this and buffer in *this.
878 //-----------------------------------------------------------------------------------
879 template <class T, size_t size, class DataContainer>
881 {
882  for (size_t i=0; i<size; ++i){
883  this->_buffer[i] = this->_buffer[i] < buffer._buffer[i] ? buffer._buffer[i] :this->_buffer[i];
884  }
885 }
886 
887 //-----------------------------------------------------------------------------------
888 // Kills negative signs from all components.
889 //-----------------------------------------------------------------------------------
890 template <class T, size_t size, class DataContainer>
892 {
893  for (size_t i=0; i<size; ++i){ this->_buffer[i] = static_cast<T>( MLAbs(this->_buffer[i])); }
894 }
895 
896 //-----------------------------------------------------------------------------------
897 // Divides each vector component by the corresponding one of d.
898 // Divisions by zero are not handled; the caller must avoid them.
899 //-----------------------------------------------------------------------------------
900 template <class T, size_t size, class DataContainer>
902 {
903  for (size_t i=0; i<size; ++i){ this->_buffer[i] /= d._buffer[i]; }
904 }
905 
906 //-----------------------------------------------------------------------------------
907 // Calculates and sets the square of all components.
908 //-----------------------------------------------------------------------------------
909 template <class T, size_t size, class DataContainer>
911 {
912  for (size_t i=0; i<size; ++i){ this->_buffer[i] *= this->_buffer[i]; }
913 }
914 
915 //-----------------------------------------------------------------------------------
916 // Calculates and sets the square root of all components.
917 // Negative values have to be avoided by caller.
918 //-----------------------------------------------------------------------------------
919 template <class T, size_t size, class DataContainer>
921 {
922  for (size_t i=0; i<size; ++i){
923 #ifdef WIN32
924  this->_buffer[i] = static_cast<T>( sqrt(this->_buffer[i]));
925 #else
926  this->_buffer[i] = static_cast<T>(::sqrt(this->_buffer[i]));
927 #endif
928  }
929 }
930 
931 //-----------------------------------------------------------------------------------
932 // Calculates and sets all components clamped to lower, i.e., if any component is
933 // smaller than the corresponding one in lower then it is set to the one from lower.
934 //-----------------------------------------------------------------------------------
935 template <class T, size_t size, class DataContainer>
937 {
938  compMax(lower);
939 }
940 
941 //-----------------------------------------------------------------------------------
942 // Calculates and sets all components clamped to upper, i.e., if any component is
943 // greater than the corresponding one in upper then it is set to the one from upper.
944 //-----------------------------------------------------------------------------------
945 template <class T, size_t size, class DataContainer>
947 {
948  compMin(upper);
949 }
950 
951 //-----------------------------------------------------------------------------------
952 // Calculates and sets all components of *this so that they are between minimum
953 // min and maximum max. This is done by applying clampMin(lower) first
954 // and then clampMax(upper). Note that components can become smaller than
955 // lower if a component in upper is smaller than their corresponding one in
956 // in lower.
957 //-----------------------------------------------------------------------------------
958 template <class T, size_t size, class DataContainer>
961 {
962  // Make copy to avoid self modification if lower or upper is equal to *this.
964  buf.clampMin(lower);
965  buf.clampMax(upper);
966  *this = buf;
967 }
968 
969 //-----------------------------------------------------------------------------------
970 // Rounds all components of this vector using floor(component + 0.5).
971 //-----------------------------------------------------------------------------------
972 template <class T, size_t size, class DataContainer>
974 {
975  for (size_t i=0; i<size; ++i){ this->_buffer[i] = std::floor(this->_buffer[i] + static_cast<T>(0.5)); }
976 }
977 
978 //-----------------------------------------------------------------------------------
979 // Rounds all components of this vector using floor(component).
980 //-----------------------------------------------------------------------------------
981 template <class T, size_t size, class DataContainer>
983 {
984  for (size_t i=0; i<size; ++i){ this->_buffer[i] = std::floor(this->_buffer[i]); }
985 }
986 
987 //-----------------------------------------------------------------------------------
988 // Rounds all components of this vector to integer using ceil(component).
989 //-----------------------------------------------------------------------------------
990 template <class T, size_t size, class DataContainer>
992 {
993  for (size_t i=0; i<size; ++i){ this->_buffer[i] = std::ceil(this->_buffer[i]); }
994 }
995 
996 //-----------------------------------------------------------------------------------
997 // Applies function f to each component starting from index 0 to index size-1.
998 //-----------------------------------------------------------------------------------
999 template <class T, size_t size, class DataContainer>
1001 {
1002  for (size_t i=0; i<size; ++i){ this->_buffer[i] = f(this->_buffer[i]); }
1003 }
1004 
1005 //-----------------------------------------------------------------------------------
1006 // Writes all components to ostream os starting from index 0 to size-1.
1007 // Each buffer elements is followed separated by a space symbols " ".
1008 //-----------------------------------------------------------------------------------
1009 template <class T, size_t size, class DataContainer>
1010 inline std::ostream& FloatingPointVector<T, size, DataContainer>::writeOut(std::ostream& os) const
1011 {
1012  for (size_t i=0; i<size; ++i){ os << this->_buffer[i] << " "; }
1013  return os;
1014 }
1015 
1016 //-----------------------------------------------------------------------------------
1017 // Reads all components from istream is starting starting from index 0 to size-1.
1018 //-----------------------------------------------------------------------------------
1019 template <class T, size_t size, class DataContainer>
1020 inline std::istream& FloatingPointVector<T, size, DataContainer>::readIn(std::istream& is)
1021 {
1022  for (size_t i=0; i<size; ++i){ is >> this->_buffer[i]; }
1023  return is;
1024 }
1025 
1026 #if ML_DEPRECATED_SINCE(3,5,0)
1031 #define ScalarVectorTemplate FloatingPointVector
1032 
1033 #if defined(WIN32) && !defined(ML_NO_DEPRECATED_WARNINGS)
1034 #pragma deprecated("ScalarVectorTemplate")
1035 #endif
1036 
1038 #endif
1039 
1040 ML_LA_END_NAMESPACE
1041 
1042 
1043 #endif
1044 
1045 
1046 
#define ML_DEPRECATED
Definition: CSOGroup.h:371
@ T
Definition: SoKeyGrabber.h:71
Base class of the FloatingPointVector class which holds the data buffer.
Template class for vector arithmetic with floating point data types.
FloatingPointVector(T value=T(0))
Default and value constructor.
FloatingPointVector< T, 3, DataContainer > cross(const FloatingPointVector< T, 3, DataContainer > &b) const
Returns the cross product for elements, i.e., return vector vertical to *this and b.
FloatingPointVector< T, size, DataContainer > operator/(FloatingPointVector< T, size, DataContainer > lhs, MLdouble rhs)
Component wise division of lhs by specialized rhs of type MLdouble.
FloatingPointVector< T, size, DataContainer > & operator/=(FloatingPointVector< T, size, DataContainer > &op1, const FloatingPointVector< T, size, DataContainer > &op2)
Arithmetic assignment: Component wise division of *this by the values of a vector of the same size.
FloatingPointVector< T, size, DataContainer > operator*(FloatingPointVector< T, size, DataContainer > lhs, MLdouble rhs)
Component wise multiplication of lhs with rhs.
std::ostream & writeOut(std::ostream &os) const
Writes all components to ostream os starting from index 0 to size-1.
FloatingPointVector< T, size, DataContainer > compMax(FloatingPointVector< T, size, DataContainer > buffer1, const FloatingPointVector< T, size, DataContainer > &buffer2)
Component wise maximum of buffer1 and buffer2.
void clampMax(const FloatingPointVector< T, size, DataContainer > &upper)
Calculates and sets all components clamped to upper, i.e., if any component is greater than the corre...
void compMin(FloatingPointVector< T, size, DataContainer > buffer)
Sets the component wise minimum of *this and buffer in *this.
T & operator[](const size_t i)
Indexing operators.
FloatingPointVector< T, size, DataContainer > & operator*=(FloatingPointVector< T, size, DataContainer > &op1, const FloatingPointVector< T, size, DataContainer > &op2)
Arithmetic assignment: Component wise multiplication *this with a vector of the same size.
T length() const
Returns the length of the vector, i.e. norm2().
void clamp(const FloatingPointVector< T, size, DataContainer > &lower, const FloatingPointVector< T, size, DataContainer > &upper)
Calculates and sets all components of *this so that they are between minimum min and maximum max.
FloatingPointVector< T, size, DataContainer > & operator/=(FloatingPointVector< T, size, DataContainer > &op1, MLdouble value)
Arithmetic assignment: Component wise division of *this by scalar value.
FloatingPointVector< T, size, DataContainer > operator+(const FloatingPointVector< T, size, DataContainer > &buffer)
Unary plus, for completeness and for those who really want to use that...
bool operator==(const FloatingPointVector< T, size, DataContainer > &buffer) const
Returns whether *this and buffer are component wise equal.
FloatingPointVector< T, size, DataContainer > & operator-=(FloatingPointVector< T, size, DataContainer > &op1, const FloatingPointVector< T, size, DataContainer > &buffer)
Arithmetic assignment: Component wise subtraction of buffer from *this.
void compMax(FloatingPointVector< T, size, DataContainer > buffer)
Sets the component wise maximum of *this and buffer in *this.
FloatingPointVector< T, size, DataContainer > clamp(FloatingPointVector< T, size, DataContainer > vec, const FloatingPointVector< T, size, DataContainer > &lower, const FloatingPointVector< T, size, DataContainer > &upper)
Returns a new vector with all components from vec clamped to range [lower, upper].
T normalize()
Normalizes the buffer and returns the Euclidean length of vector before normalization,...
void compAbs()
Kills negative signs from all components.
const T & operator[](const size_t i) const
Constant indexing operators.
T distance(const FloatingPointVector< T, size, DataContainer > &buffer) const
Returns the distance of this vector to a given one.
T compMaxAbs() const
Returns the maximum of absolute component values.
void compFloor()
Rounds all components of this vector using floor(component).
void compDiv(const FloatingPointVector< T, size, DataContainer > &d)
Divides each vector component by the corresponding one of d.
T compMul() const
Returns the product of all vector components.
bool operator!(const FloatingPointVector< T, size, DataContainer > &a)
Returns whether all components are 0.
FloatingPointVector< T, size, DataContainer > & operator=(T value)
Assignment of scalar value to all components.
T compMul(const FloatingPointVector< T, size, DataContainer > &vec)
Returns the product of all components.
FloatingPointVector< T, size, DataContainer > clampMax(FloatingPointVector< T, size, DataContainer > vec, const FloatingPointVector< T, size, DataContainer > &m)
Returns a new vector with all components from vec clamped to maximum m.
FloatingPointVector< T, size, DataContainer > compSqrt(FloatingPointVector< T, size, DataContainer > vec)
Returns a vector with all components from vec square-rooted.
FloatingPointVector< T, size, DataContainer > compSqr(FloatingPointVector< T, size, DataContainer > vec)
Returns a vector with all components from vec squared.
T operator*(const FloatingPointVector< T, size, DataContainer > &a, const FloatingPointVector< T, size, DataContainer > &b)
Dot product, returns a.dot(b).
void compRound()
Rounds all components of this vector using floor(component + 0.5).
T norm2(const FloatingPointVector< T, size, DataContainer > &weight) const
Returns the weighted Euclidean norm, i.e., square root of sum of squares of all components multiplied...
T dot(const FloatingPointVector< T, size, DataContainer > &buffer) const
Returns the dot product, i.e., sum of all components multiplied with corresponding components of buff...
void compSqr()
Calculates and sets the square of all components.
bool operator!=(const FloatingPointVector< T, size, DataContainer > &buffer) const
Returns whether any components of *this and buffer are not equal.
T norm2() const
Returns the Euclidean norm (the vector length), i.e., square root of sum of squares of all components...
T compSum() const
Returns the sum of all components.
FloatingPointVector< T, size, DataContainer > operator+(FloatingPointVector< T, size, DataContainer > lhs, const FloatingPointVector< T, size, DataContainer > &rhs)
Return value is the component wise addition of lhs and rhs.
std::istream & readIn(std::istream &is)
Reads all components from istream is starting starting from index 0 to size-1.
FloatingPointVector< T, size, DataContainer > compMin(FloatingPointVector< T, size, DataContainer > buffer1, const FloatingPointVector< T, size, DataContainer > &buffer2)
Component wise minimum of buffer1 and buffer2.
void compCeil()
Rounds all components of this vector to integer using ceil(component).
size_t getSize() const
Returns the number of elements of value buffer.
void clampMin(const FloatingPointVector< T, size, DataContainer > &lower)
Calculates and sets all components clamped to lower, i.e., if any component is smaller than the corre...
void compSqrt()
Calculates and sets square root of all components.
FloatingPointVector< T, size, DataContainer > & operator+=(FloatingPointVector< T, size, DataContainer > &op1, const FloatingPointVector< T, size, DataContainer > &buffer)
Arithmetic assignment: Component wise addition.
FloatingPointVector< T, size, DataContainer > operator-(FloatingPointVector< T, size, DataContainer > lhs, const FloatingPointVector< T, size, DataContainer > &rhs)
Return value is the component wise subtraction of rhs from lhs.
FloatingPointVector< T, size, DataContainer > operator*(MLdouble lhs, FloatingPointVector< T, size, DataContainer > rhs)
Component wise multiplication of rhs with lhs.
FloatingPointVector< T, size, DataContainer > compAbs(FloatingPointVector< T, size, DataContainer > vec)
Returns a vector with all components from vec without negative sign.
T distanceSquared(const FloatingPointVector< T, size, DataContainer > &buffer) const
Returns the squared distance of this vector to a given one.
void apply(ML_LA_FROM_DOUBLE_TO_DOUBLE f)
Applies the function f to each component starting from index 0 to index size-1.
FloatingPointVector< T, size, DataContainer > & operator*=(FloatingPointVector< T, size, DataContainer > &op1, MLdouble value)
Arithmetic assignment: Component wise multiplication *this with specialized MLdouble scalar value.
FloatingPointVector< T, size, DataContainer > operator-(const FloatingPointVector< T, size, DataContainer > &buffer)
Unary minus, all components of the vector are multiplied with -1.
T ComponentType
A typedef to "export" the type of components.
FloatingPointVector< T, size, DataContainer > clampMin(FloatingPointVector< T, size, DataContainer > vec, const FloatingPointVector< T, size, DataContainer > &m)
Returns a new vector with all components from vec clamped to minimum m.
T lengthSquared() const
Returns the squared length of the vector.
FloatingPointVector< T, size, DataContainer > compDiv(FloatingPointVector< T, size, DataContainer > vec, const FloatingPointVector< T, size, DataContainer > &d)
Component wise division of vec / d.
bool MLValueIs0WOM(MLint8 a)
Returns true if value is 0, otherwise false.
DT MLAbs(const DT val)
Defines templated MLAbs version to circumvent fabs ambiguities on different platforms.
bool MLValuesDifferWOM(MLint8 a, MLint8 b)
Returns true if values differ, otherwise false.
MLEXPORT std::ostream & operator<<(std::ostream &s, const ml::Field &v)
Overloads the operator "<<" for stream output of Field objects.
double(* ML_LA_FROM_DOUBLE_TO_DOUBLE)(double)
Template base class for floating point vectors.
double MLdouble
Definition: mlTypeDefs.h:223
std::istream & operator>>(std::istream &in, ml::Variant &variant)
Definition: mlVariant.h:215
void apply(Fnc &&fnc, const std::tuple< Args... > &tuple)
FloatingPointVector< T, size, DataContainer > compMax(FloatingPointVector< T, size, DataContainer > buffer1, const FloatingPointVector< T, size, DataContainer > &buffer2)
Component wise maximum of buffer1 and buffer2.
FloatingPointVector< T, size, DataContainer > clamp(FloatingPointVector< T, size, DataContainer > vec, const FloatingPointVector< T, size, DataContainer > &lower, const FloatingPointVector< T, size, DataContainer > &upper)
Returns a new vector with all components from vec clamped to range [lower, upper].
bool operator==(const Tmat2< DT > &a, const Tmat2< DT > &b)
a == b ? Return true if yes.
Definition: mlMatrix2.h:425
T compMul(const FloatingPointVector< T, size, DataContainer > &vec)
Returns the product of all components.
FloatingPointVector< T, size, DataContainer > clampMax(FloatingPointVector< T, size, DataContainer > vec, const FloatingPointVector< T, size, DataContainer > &m)
Returns a new vector with all components from vec clamped to maximum m.
FloatingPointVector< T, size, DataContainer > compSqrt(FloatingPointVector< T, size, DataContainer > vec)
Returns a vector with all components from vec square-rooted.
FloatingPointVector< T, size, DataContainer > compSqr(FloatingPointVector< T, size, DataContainer > vec)
Returns a vector with all components from vec squared.
bool operator!=(const Tmat2< DT > &a, const Tmat2< DT > &b)
a != b ? Return true if yes.
Definition: mlMatrix2.h:433
FloatingPointVector< T, size, DataContainer > compMin(FloatingPointVector< T, size, DataContainer > buffer1, const FloatingPointVector< T, size, DataContainer > &buffer2)
Component wise minimum of buffer1 and buffer2.
constexpr Is< T > is(T d)
FloatingPointVector< T, size, DataContainer > compAbs(FloatingPointVector< T, size, DataContainer > vec)
Returns a vector with all components from vec without negative sign.
FloatingPointVector< T, size, DataContainer > clampMin(FloatingPointVector< T, size, DataContainer > vec, const FloatingPointVector< T, size, DataContainer > &m)
Returns a new vector with all components from vec clamped to minimum m.
FloatingPointVector< T, size, DataContainer > compDiv(FloatingPointVector< T, size, DataContainer > vec, const FloatingPointVector< T, size, DataContainer > &d)
Component wise division of vec / d.