29 #include "bias_config.h"
39 #ifdef BIAS_HAVE_CPP11
40 #include <initializer_list>
44 #include <Base/Debug/Error.hh>
45 #include <Base/Debug/Exception.hh>
46 #include <Base/Math/Vector.hh>
47 #include <Base/Math/Matrix2x2.hh>
48 #include <Base/Math/Operators.hh>
49 #include <Base/Math/Utils.hh>
132 std::istringstream inst(s);
143 #ifdef BIAS_HAVE_CPP11
147 Vector2(
const std::initializer_list< T> &i) {
148 BIASASSERT(i.size() >= 2);
149 auto iter = i.begin();
151 data_[1] = *(++iter);
169 inline T
const *
end()
const {
170 return &data_[VECTOR2SIZE];
173 return &data_[VECTOR2SIZE];
181 inline void Copy(
const T * pv) {
191 inline void Set(
const T & scalar) {
197 inline void SetZero() {
202 inline void clear() {
207 inline bool IsZero()
const {
208 return ( (data_[0]==(T)0)
209 && (data_[1]==(T)0));
216 inline void Set(
const T & x,
const T & y) {
224 inline const unsigned int Size()
const {
return size(); };
226 inline const unsigned int size()
const {
return VECTOR2SIZE; };
228 inline const unsigned int GetNumElements()
const {
return size(); };
237 inline const T *GetData()
const {
241 inline T *GetData() {
250 inline Vector2<T> &operator=(
const T& scalar) {
268 inline const T & operator[] (
const int i)
const {
270 BIASASSERT(i < VECTOR2SIZE);
278 inline T & operator[] (
const int i) {
280 BIASASSERT(i < VECTOR2SIZE);
301 inline T GetX()
const {
304 inline T GetY()
const {
307 inline T GetW()
const {
316 inline void SetX(
const T x) {
319 inline void SetY(
const T y) {
322 inline void SetW(
const T w) {
328 {
return (*
this)[0]; };
329 inline T Radius()
const
330 {
return (*
this)[0]; };
334 {
return (*
this)[1]; };
336 {
return (*
this)[1]; };
343 inline T min(
const T a,
const T b)
const {
344 return (a>b) ? b : a;
347 inline T max(
const T a,
const T b)
const {
348 return (a<b) ? b : a;
357 inline void ScalarProduct(
const Vector2<T> &argvec, T & result)
const {
358 result = data_[0] * argvec[0] + data_[1] * argvec[1];
361 inline T ScalarProduct(
const Vector2<T> &argvec)
const {
362 return ((data_[0] * argvec[0]) + (data_[1] * argvec[1]));
368 inline void ElementwiseProduct(
const Vector2<T> &argvec,
371 destvec[0] = data_[0] * argvec[0];
372 destvec[1] = data_[1] * argvec[1];
378 ElementwiseProduct(argvec, destvec);
384 inline void ElementwiseDivision(
const Vector2<T> &argvec,
388 if (argvec[0]==0.0) BEXCEPTION(
"Division by zero !");
389 if (argvec[1]==0.0) BEXCEPTION(
"Division by zero !");
390 destvec[0] = data_[0] / argvec[0];
391 destvec[1] = data_[1] / argvec[1];
397 ElementwiseDivision(argvec, destvec);
408 mat[0][0] = data_[0]*v[0];
409 mat[0][1] = data_[0]*v[1];
410 mat[1][0] = data_[1]*v[0];
411 mat[1][1] = data_[1]*v[1];
422 inline T NormL1()
const {
423 return (data_[0] > 0 ? data_[0] : data_[0] * -1)
424 + (data_[1] > 0 ? data_[1] : data_[1] * -1);
431 inline double NormL2()
const {
432 return sqrt( (
double) ( data_[0] * data_[0] + data_[1] * data_[1] ) );
436 inline double Length()
const
442 inline void Normalize(){
443 const T len = (T) NormL2();
444 if(len > std::numeric_limits<T>::epsilon()){
450 BEXCEPTION(
"length of normalized vector is "<<len);
459 inline double Dist(
const Vector2<T> &vec)
const {
460 return sqrt( (
double) ( (data_[0]-vec[0])*(data_[0]-vec[0]) +
461 (data_[1]-vec[1])*(data_[1]-vec[1]) ));
468 inline double SqrDist(
const Vector2<T> &vec)
const
470 return static_cast<double> ((data_[0]-vec[0])*(data_[0]-vec[0]) +
471 ( data_[1]-vec[1])*(data_[1]-vec[1]));
478 inline T DistLInf(
const Vector2<T> &vec)
const
480 return max(abs((T)(data_[0]-vec[0])), abs((T)(data_[1]-vec[1])));
491 inline bool operator== (
const Vector2<T>& arg)
const {
492 return ( (data_[0] == arg.
data_[0]) && (data_[1] == arg.
data_[1]) );
498 inline bool operator!= (
const Vector2<T>& arg)
const {
499 return ( (data_[0] != arg.
data_[0]) || (data_[1] != arg.
data_[1]) );
511 inline void AddIP (
const T& scalar) {
517 inline void Add(
const T& scalar,
Vector2<T>& dest)
const{
518 dest.
data_[0] = data_[0] + scalar;
519 dest.
data_[1] = data_[1] + scalar;
524 inline void SubIP (
const T& scalar) {
530 inline void Sub (
const T& scalar,
Vector2<T>& dest)
const {
531 dest.
data_[0] = data_[0] - scalar;
532 dest.
data_[1] = data_[1] - scalar;
537 inline void MultiplyIP (
const T& scalar) {
538 Multiply(scalar, *
this);
543 inline void Multiply (
const T& scalar,
Vector2<T>& dest)
const {
544 dest.
data_[0] = data_[0] * scalar;
545 dest.
data_[1] = data_[1] * scalar;
550 inline void DivideIP (
const T& scalar) {
551 Divide(scalar, *
this);
556 inline void DivideIP (
const Vector2<T>& vec) {
563 inline void Divide (
const T& scalar,
Vector2<T>& dest)
const {
566 BIASERR(
"Division by Zero!");
568 dest.
data_[0] = data_[0] / scalar;
569 dest.
data_[1] = data_[1] / scalar;
575 if (vec[0]==0 || vec[1]==0)
576 BIASERR(
"Division by Zero!");
578 this->ElementwiseDivision(vec, dest);
598 destvec[0] = data_[0] + argvec[0];
599 destvec[1] = data_[1] + argvec[1];
614 destvec[0] = data_[0] - argvec[0];
615 destvec[1] = data_[1] - argvec[1];
618 inline void Fill(T value)
619 { data_[0] = data_[1] = value; };
633 return this->CoordPolarToEuclidean();
647 return this->CoordEuclideanToPolar();
654 bool Load(
const std::string & filename);
660 bool Save(
const std::string & filename)
const;
663 inline T abs(
const T a)
const {
664 T b=(a<0) ? (T)(-1) : (T)(1);
668 inline T max(
const T a,
const T b)
const {
669 return (a<b) ? b : a;
678 T data_[VECTOR2SIZE];
696 std::ostream& operator<<(std::ostream & os, const Vector2<char> & vec)
700 os << VECTOR2SIZE << NL;
701 for (
int row = 0; row < VECTOR2SIZE; row++) {
702 os<<std::setw(3)<<(int)vec.GetData()[row]<<
" "<<NL;
706 for (
int row = 0; row < VECTOR2SIZE; row++)
707 os<<std::setw(3)<<(int)vec.GetData()[row]<<
" ";
714 std::ostream& operator<<(std::ostream & os, const Vector2<unsigned char> & vec)
718 os << VECTOR2SIZE << NL;
719 for (
int row = 0; row < VECTOR2SIZE; row++) {
720 os<<std::setw(3)<<(
unsigned int)vec.GetData()[row]<<
" "<<NL;
724 for (
int row = 0; row < VECTOR2SIZE; row++)
725 os<<std::setw(3)<<(
unsigned int)vec.GetData()[row]<<
" ";
737 std::ostream& operator<<(std::ostream & os, const Vector2<T>& vec)
741 os << VECTOR2SIZE << NL;
742 for (
int row = 0; row < VECTOR2SIZE; row++) {
743 os<<std::setprecision( DecimalsToStore<T>() );
744 os<<vec.GetData()[row]<<
" "<<NL;
749 for (
int row = 0; row < VECTOR2SIZE; row++){
750 os<<std::setprecision(CONSOLE_PRECISION_DIGITS);
751 os<<vec.GetData()[row]<<
" ";
767 if (!(N == (
int)vec.
Size())) {
768 BIASERR(
"can't assign a Vector2<T> with a dim " << N <<
" vector");
771 for (
int row = 0; row < N; row++) {
773 BIASASSERT(tmp>=std::numeric_limits<char>::min() );
774 BIASASSERT(tmp<=std::numeric_limits<char>::max() );
786 if (!(N == (
int)vec.
Size())) {
787 BIASERR(
"can't assign a Vector2<T> with a dim " << N <<
" vector");
790 for (
int row = 0; row < N; row++) {
792 BIASASSERT(tmp>=std::numeric_limits<unsigned char>::min() );
793 BIASASSERT(tmp<=std::numeric_limits<unsigned char>::max() );
794 vec.
GetData()[row]=(
unsigned char)tmp;
811 if (!(N == (
int)vec.
Size())) {
813 BIASERR(
"can't assign a Vector2<T> with a dim " << N <<
" vector");
816 for (
int row = 0; row < N; row++) {
829 template <
class T> BIASMathBase_EXPORT
831 vec.
Add(argvec, vec);
840 vec.
Sub(argvec, vec);
845 template <
class T> BIASMathBase_EXPORT
void Add(const T &scalar, Vector2< T > &dest) const
Addition with a scalar, storing results in destionation vector.
T const * end() const
Iterator pointing to one element after the last vector element.
T const * begin() const
Iterator to first component of vector.
class for column vectors with arbitrary size
is a 'fixed size' quadratic matrix of dim.
void Sub(const T &scalar, Vector2< T > &dest) const
Substraction with a scalar, storing results in destionation vector.
class BIASMathBase_EXPORT Vector2
bool IsConsoleStream(const std::ostream &os)
Helper returning true on console ostreams like cerr, cout Useful to distinguish between console (cout...
std::istream & operator>>(std::istream &is, Vector2< char > &vec)
Input with streams.
BIASMathBase_EXPORT Vector2< T > & operator/=(Vector2< T > &vec, const Vector2< T > &argvec)
elementwise division
Vector2(const T v0, const T v1)
constructor with element assigment using explicit values
void SubIP(const T &scalar)
Substraction (in place) of an scalar.
const unsigned int Size() const
void MultiplyIP(const T &scalar)
Multiplication (in place) of an scalar.
const T * GetData() const
get the data pointer the member function itself is const (before {..}) because it doesn't change the ...
Vector2< T > & operator-=(Vector2< T > &vec, const Vector2< T > &argvec)
sub operator for two Vectors
class Vector2 contains a Vector of dim.
BIASMathBase_EXPORT Vector2< T > & operator+=(Vector2< T > &vec, const Vector2< T > &argvec)
add operator for two Vectors
void AddIP(const T &scalar)
Addition (in place) of an scalar.
Vector2< T > & operator*=(Vector2< T > &vec, const T &scalar)
Multiplication operator with scalar argument.
void DivideIP(const T &scalar)
Division (in place) of an scalar.
Vector2(const T *pv)
assignment with an array of values which is copied into this ones class members
std::istream & operator>>(std::istream &is, Vector2< T > &vec)
Input with streams.
Vector2()
default constructor
Vector2(char *s)
constructor with element assignment
Vector2(const Vector2< T > &vec)
copy constructor
Vector2(const T &scalar)
assignment with a constant value for all elements
BIASCommon_EXPORT std::istream & operator>>(std::istream &is, BIAS::TimeStamp &ts)
Standard input operator for TimeStamps.