28 #include "bias_config.h"
34 #ifdef BIAS_HAVE_CPP11
35 #include <initializer_list>
38 #include <Base/Debug/Error.hh>
41 #include <Base/Math/Vector.hh>
42 #include <Base/Math/Matrix.hh>
43 #include <Base/Math/Matrix4x4.hh>
44 #include <Base/Math/Operators.hh>
45 #include <Base/Math/Utils.hh>
61 template <
class T>
class BIASMathBase_EXPORT
Matrix3x4;
62 template <
class T>
class BIASMathBase_EXPORT
Matrix4x4;
79 explicit inline Vector4(
const T & scalar);
85 explicit inline Vector4(
const T * pv);
90 explicit inline Vector4(
char *s);
95 inline Vector4(
const T v0,
const T v1,
const T v2,
const T v3);
102 #ifdef BIAS_HAVE_CPP11
106 Vector4(
const std::initializer_list< T> &i) {
107 BIASASSERT(i.size() >= 4);
108 auto iter = i.begin();
110 data_[1] = *(++iter);
111 data_[2] = *(++iter);
112 data_[3] = *(++iter);
121 inline void Copy(
const T * pv);
126 inline void Set(
const T & scalar);
131 inline void Set(
const T & x,
const T & y,
const T & z,
const T & w);
135 inline void SetZero();
141 inline const unsigned int Size()
const;
143 inline const unsigned int size()
const;
161 inline T
const *
end()
const {
162 return &data_[VECTOR4SIZE];
165 return &data_[VECTOR4SIZE];
178 inline const T *GetData()
const
189 inline Vector4<T>& operator=(
const T& scalar);
196 memcpy((
void *)data_, (
void *)vec.
GetData(), VECTOR4SIZE*
sizeof(T));
205 inline const T& operator[] (
const int i)
const;
211 inline T & operator[] (
const int i);
217 inline void ScalarProduct(
const Vector4 < T > &argvec, T & result)
const;
237 inline T NormL1()
const;
243 inline double NormL2()
const;
249 inline double Dist(
const Vector4<T> &vec)
const;
258 inline bool operator==(
const Vector4<T>& arg)
const;
262 inline bool operator!=(
const Vector4<T>& arg)
const;
273 inline void AddIP (
const T& scalar);
277 inline void Add(
const T& scalar,
Vector4<T>& dest)
const;
288 inline void SubIP(
const T& scalar);
292 inline void Sub(
const T& scalar,
Vector4<T>& dest)
const;
303 inline void MultiplyIP(
const T& scalar);
307 inline void Multiply(
const T& scalar,
Vector4<T>& dest)
const;
318 inline void DivideIP(
const T& scalar);
322 inline void Divide(
const T& scalar,
Vector4<T>& dest)
const;
363 bool Load(
const std::string & filename);
369 bool Save(
const std::string & filename)
const;
376 T data_[VECTOR4SIZE];
392 std::istringstream inst(s);
423 Set(scalar, scalar, scalar, scalar);
459 BIASASSERT(i < VECTOR4SIZE);
467 BIASASSERT(i < VECTOR4SIZE);
477 result = ScalarProduct(argvec);
484 return data_[0] * argvec[0] +
485 data_[1] * argvec[1] +
486 data_[2] * argvec[2] +
487 data_[3] * argvec[3];
494 destvec[0] = data_[0] * argvec[0];
495 destvec[1] = data_[1] * argvec[1];
496 destvec[2] = data_[2] * argvec[2];
497 destvec[3] = data_[3] * argvec[3];
503 return (data_[0] > 0 ? data_[0] : data_[0] * -1)
504 + (data_[1] > 0 ? data_[1] : data_[1] * -1)
505 + (data_[2] > 0 ? data_[2] : data_[2] * -1)
506 + (data_[3] > 0 ? data_[3] : data_[3] * -1);
512 return sqrt((
double) ( data_[0]*data_[0] +
515 data_[3]*data_[3] ));
521 return sqrt((
double) ( (data_[0]-vec[0])*(data_[0]-vec[0]) +
522 (data_[1]-vec[1])*(data_[1]-vec[1]) +
523 (data_[2]-vec[2])*(data_[2]-vec[2]) +
524 (data_[3]-vec[3])*(data_[3]-vec[3]) ));
531 destmat.
GetData()[0] = data_[0];
532 destmat.
GetData()[0] = data_[1];
533 destmat.
GetData()[0] = data_[2];
534 destmat.
GetData()[0] = data_[3];
541 return ( (data_[0] == arg.
data_[0]) && (data_[1] == arg.
data_[1]) &&
542 (data_[2] == arg.
data_[2]) && (data_[3] == arg.
data_[3]) );
548 return !operator==(arg);
569 dest.
data_[0] = data_[0] + scalar;
570 dest.
data_[1] = data_[1] + scalar;
571 dest.
data_[2] = data_[2] + scalar;
572 dest.
data_[3] = data_[3] + scalar;
593 dest.
data_[0] = data_[0] - scalar;
594 dest.
data_[1] = data_[1] - scalar;
595 dest.
data_[2] = data_[2] - scalar;
596 dest.
data_[3] = data_[3] - scalar;
611 Multiply(scalar, *
this);
617 dest.
data_[0] = data_[0] * scalar;
618 dest.
data_[1] = data_[1] * scalar;
619 dest.
data_[2] = data_[2] * scalar;
620 dest.
data_[3] = data_[3] * scalar;
634 { Divide(scalar, *
this);}
641 BIASERR(
"Division by Zero!");
645 dest.
data_[0] = data_[0] / scalar;
646 dest.
data_[1] = data_[1] / scalar;
647 dest.
data_[2] = data_[2] / scalar;
648 dest.
data_[3] = data_[3] / scalar;
666 template <
class T>
inline void
669 destvec[0] = data_[0] + argvec[0];
670 destvec[1] = data_[1] + argvec[1];
671 destvec[2] = data_[2] + argvec[2];
672 destvec[3] = data_[3] + argvec[3];
690 template <
class T>
inline void
693 destvec[0] = data_[0] - argvec[0];
694 destvec[1] = data_[1] - argvec[1];
695 destvec[2] = data_[2] - argvec[2];
696 destvec[3] = data_[3] - argvec[3];
729 std::ostream& operator<<(std::ostream & os, const Vector4<unsigned char> &vec)
733 os << VECTOR4SIZE << NL;
734 for (
int row = 0; row < VECTOR4SIZE; row++) {
736 os<<std::setw(3)<<(int)vec.GetData()[row]<<
" "<<NL;
740 for (
int row = 0; row < VECTOR4SIZE; row++)
741 os<<std::setw(3)<<(int)vec.GetData()[row]<<
" ";
749 std::ostream& operator<<(std::ostream & os, const Vector4<char> &vec)
753 os << VECTOR4SIZE << NL;
754 for (
int row = 0; row < VECTOR4SIZE; row++) {
756 os<<std::setw(3)<<(int)vec.GetData()[row]<<
" "<<NL;
760 for (
int row = 0; row < VECTOR4SIZE; row++)
761 os<<std::setw(3)<<(
unsigned int)vec.GetData()[row]<<
" ";
775 std::ostream& operator<<(std::ostream & os, const Vector4<T> &vec)
779 os << VECTOR4SIZE << NL;
780 for (
int row = 0; row < VECTOR4SIZE; row++) {
782 os<<std::setprecision( DecimalsToStore<T>() );
784 os<<vec.GetData()[row]<<
" "<<NL;
788 for (
int row = 0; row < VECTOR4SIZE; row++){
789 os<<std::setprecision(CONSOLE_PRECISION_DIGITS);
790 os<<vec.GetData()[row]<<
" ";
804 inline std::istream &
809 if (N != (
int)vec.
Size()){
810 BIASERR(
"can't assign to a Vector4<unsigned char> from a vector with dim " << N );
814 for (
unsigned int row = 0; row < vec.
Size(); row++)
817 BIASASSERT(tmp>=std::numeric_limits<unsigned char>::min() );
818 BIASASSERT(tmp<=std::numeric_limits<unsigned char>::max() );
819 vec.
GetData()[row] = (
unsigned char)tmp;
825 inline std::istream &
830 if (N != (
int)vec.
Size()){
831 BIASERR(
"can't assign to a Vector4<unsigned> from a vector with dim " << N );
835 for (
unsigned int row = 0; row < vec.
Size(); row++)
838 BIASASSERT(tmp>=std::numeric_limits< char>::min() );
839 BIASASSERT(tmp<=std::numeric_limits< char>::max() );
840 vec.
GetData()[row] = (char)tmp;
855 if (!(N == (
int)vec.
Size())) {
856 BIASERR(
"can't assign a Vector4<T> with a dim " << N <<
" vector");
859 for (
unsigned int row = 0; row < vec.
Size(); row++) {
void Sub(const T &scalar, Vector4< T > &dest) const
Substraction with a scalar, storing results in destionation vector.
void Multiply(const T &scalar, Vector4< T > &dest) const
Multiplication with a scalar, storing results in destionation vector.
void Add(const T &scalar, Vector4< T > &dest) const
Addition with a scalar, storing results in destionation vector.
void AddIP(const T &scalar)
Addition (in place) of an scalar.
T const * begin() const
Iterator to first component of vector.
class for column vectors with arbitrary size
void SetZero()
set all values to 0
bool IsConsoleStream(const std::ostream &os)
Helper returning true on console ostreams like cerr, cout Useful to distinguish between console (cout...
T const * end() const
Iterator pointing to one element after the last vector element.
Matrix< T > transposed() const
return a new Matrix with 1 row and 4 columns representing a transposed vector
BIASMathBase_EXPORT Vector2< T > & operator/=(Vector2< T > &vec, const Vector2< T > &argvec)
elementwise division
void ElementwiseProduct(const Vector4< T > &argvec, Vector4< T > &destvec) const
multiply two vectors elementwise, storing the result in destvec
void Set(const T &scalar)
set all elements to a scalat value
std::istream & operator>>(std::istream &is, Vector4< unsigned char > &vec)
Input with streams specialization for numerical display of unsigned char instead of alphebetically...
const T & operator[](const int i) const
access an element of the vector with 0-based indizes.
void SubIP(const T &scalar)
Substraction (in place) of an scalar.
Vector2< T > & operator-=(Vector2< T > &vec, const Vector2< T > &argvec)
sub operator for two Vectors
double NormL2() const
Return the L2 norm: sqrt(a^2 + b^2 + c^2 + d^2)
Vector4< T > & operator+=(const T &scalar)
Addition operator with scalar argument.
std::istream & operator>>(std::istream &is, Vector4< T > &vec)
Input with streams fout format should be consistent with template implementation and Vector<> ...
bool operator==(const Vector4< T > &arg) const
Comparison operator 'equal'.
class Vector4 contains a Vector of dim.
void Copy(const T *pv)
copy the array of vectorsize beginning at *T to this->data_
const unsigned int GetNumElements() const
BIASMathBase_EXPORT Vector2< T > & operator+=(Vector2< T > &vec, const Vector2< T > &argvec)
add operator for two Vectors
Vector2< T > & operator*=(Vector2< T > &vec, const T &scalar)
Multiplication operator with scalar argument.
Vector4< T > & operator*=(const T &scalar)
Multiplication operator with scalar argument.
const unsigned int Size() const
void Divide(const T &scalar, Vector4< T > &dest) const
Division with a scalar, storing results in destionation vector.
double Dist(const Vector4< T > &vec) const
Return the euclidean distance of 2 vectors.
const unsigned int size() const
T * GetData()
get the pointer to the data array of the matrix (for faster direct memeory access) ...
class BIASMathBase_EXPORT Matrix4x4
Vector4< T > & operator/=(const T &scalar)
Division operator with scalar argument.
void ScalarProduct(const Vector4< T > &argvec, T &result) const
scalar product (=inner product) of two vectors, storing the result in result
bool operator!=(const Vector4< T > &arg) const
Comparison operator 'not equal'.
Vector4< T > & operator=(const T &scalar)
assignment operator set the vector elementwise to scalar value
matrix class with arbitrary size, indexing is row major.
const T * GetData() const
get the data pointer the member function itself is const (before {..}) because it doesn't change the...
void MultiplyIP(const T &scalar)
Multiplication (in place) of an scalar.
class BIASMathBase_EXPORT Vector4
Vector4< T > & operator-=(const T &scalar)
Substraction operator with scalar argument.
class BIASMathBase_EXPORT Matrix3x4
void DivideIP(const T &scalar)
Division (in place) of an scalar.
is a 'fixed size' quadratic matrix of dim.
T NormL1() const
Return the L1 norm: |a| + |b| + |c|+ |d|.
Vector4(const Vector4< T > &vec)
copy constructor
BIASCommon_EXPORT std::istream & operator>>(std::istream &is, BIAS::TimeStamp &ts)
Standard input operator for TimeStamps.