Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Vector2.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 #ifndef _Vector2_hh_
27 #define _Vector2_hh_
28 
29 #include "bias_config.h"
30 
31 //additional math constants are not used by default in WIN32 - so set usage
32 //#define _USE_MATH_DEFINES
33 //#include <math.h>
34 
35 #include <iostream>
36 #include <sstream>
37 #include <iomanip>
38 
39 #ifdef BIAS_HAVE_CPP11
40  #include <initializer_list>
41 #endif
42 
43 // BIAS
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>
50 
51 // dim of this vector
52 #define VECTOR2SIZE 2
53 namespace BIAS {
54 
55  // forward declarations to avoid mutual inclusion
56  //template <class T> class BIASMathBase_EXPORT BIAS::Vector;
57  //template <class T> class BIASMathBase_EXPORT BIAS::Matrix;
58  //template <class T> class BIASMathBase_EXPORT BIAS::Matrix2x2;
59 
60  // disabled these forwards for g++4.0, checked with g++3.3
61  // not checkedwith W32, Friso
62 // template <class T> class BIAS::Vector;
63 // template <class T> class BIAS::Matrix;
64 // template <class T> class BIAS::Matrix2x2;
65 
66 
67 
68  /** @class Vector2
69  @ingroup g_math
70  @brief class Vector2 contains a Vector of dim. 2 and fixed dimension
71 
72  It's elment type is templated.
73  manual loop unrolling is used if possible.
74  The Vector is in row-major order (2 rows, 1 column)
75  the indizes begin with zero (to size-1)
76  @author Jan Woetzel
77  @status untested (03/04/2002) **/
78  template <class T>
79  class BIASMathBase_EXPORT Vector2
80  {
81  public:
82  typedef T value_type;
83 
84 
85  /** destructor
86  @author Jan Woetzel
87  @status untested (02/28/2002) **/
88  ~Vector2() {};
89 
90 
91  /** default constructor
92  @author Jan Woetzel
93  @status alpha (02/28/2002) **/
94  Vector2() {};
95 
96  /** copy constructor
97  @author Jan Woetzel
98  @status alpha (03/05/2002)**/
99  inline Vector2(const Vector2<T> &vec) {
100  Copy(vec.GetData());
101  };
102 
103  /** assignment with a constant value for all elements
104  @author Jan Woetzel
105  @status alpha (03/01/2002) **/
106  explicit inline Vector2(const T & scalar) {
107  Set(scalar);
108  };
109 
110 
111  /** constructor with element assigment using explicit values
112  @author Ingo Thomsen
113  @status tested (03/04/2002) **/
114  inline Vector2(const T v0, const T v1) {
115  data_[0] = v0;
116  data_[1] = v1;
117  }
118 
119 
120  /** assignment with an array of values
121  which is copied into this ones class members
122  @author Jan Woetzel
123  @status untested (02/28/2002) **/
124  explicit inline Vector2(const T * pv) {
125  Copy(pv);
126  };
127 
128  /** constructor with element assignment
129  @author Jan Woetzel
130  @status alpha (03/01/2002) **/
131  explicit inline Vector2(char *s) {
132  std::istringstream inst(s);
133  inst >> data_[0];
134  inst >> data_[1];
135  };
136 
137 
138  /** cast constructor
139  @author Jan Woetzel
140  @status alpha (03/22/2002) **/
141  Vector2(const Vector < T > &v);
142 
143 #ifdef BIAS_HAVE_CPP11
144  /** initializer list constructor (C++11)
145  @author Stefan Reinhold
146  @status untested (08/13/2014) **/
147  Vector2(const std::initializer_list< T> &i) {
148  BIASASSERT(i.size() >= 2);
149  auto iter = i.begin();
150  data_[0] = *iter;
151  data_[1] = *(++iter);
152  }
153 #endif
154 
155  /** @name Iterator Functions
156  @{
157  */
158  /** Iterator to first component of vector
159  * @author Stefan Reinhold **/
160  inline T const *begin() const {
161  return &data_[0];
162  }
163  inline T *begin() {
164  return &data_[0];
165  }
166 
167  /** Iterator pointing to one element after the last vector element
168  * @author Stefan Reinhold **/
169  inline T const *end() const {
170  return &data_[VECTOR2SIZE];
171  }
172  inline T *end() {
173  return &data_[VECTOR2SIZE];
174  }
175  /** @}
176  */
177 
178  /** copy the array of vectorsize beginning at *T to this->data_
179  @author Jan Woetzel
180  @status untested (02/28/2002) **/
181  inline void Copy(const T * pv) {
182  // better use memcopy ?
183  data_[0] = pv[0];
184  data_[1] = pv[1];
185  };
186 
187 
188  /** set all elements to a scalar value
189  @author Jan Woetzel
190  @status untested (02/28/2002) **/
191  inline void Set(const T & scalar) {
192  Set(scalar, scalar);
193  };
194 
195 
196  /** @author Jan Woetzel */
197  inline void SetZero() {
198  Set(0,0);
199  };
200 
201  /// stl conform interface
202  inline void clear() {
203  SetZero();
204  };
205 
206  /** @author Jan Woetzel */
207  inline bool IsZero() const {
208  return ( (data_[0]==(T)0)
209  && (data_[1]==(T)0));
210  }
211 
212 
213  /** set elementwise with given scalar values
214  @author Jan Woetzel
215  @status alpha (02/28/2002) **/
216  inline void Set(const T & x, const T & y) {
217  data_[0] = x;
218  data_[1] = y;
219  };
220 
221 
222  /** @return the size of this vector
223  @author Jan Woetzel (02/28/2002) **/
224  inline const unsigned int Size() const { return size(); };
225  // stl conform interface
226  inline const unsigned int size() const { return VECTOR2SIZE; };
227  // compatibility interface
228  inline const unsigned int GetNumElements() const { return size(); };
229 
230 
231  /** get the data pointer
232  the member function itself is const (before {..}) because it doesn't
233  change the this object.
234  @return the const pointer to the data array for reading
235  @author Jan Woetzel
236  @status untested (02/28/2002) **/
237  inline const T *GetData() const {
238  return data_;
239  };
241  inline T *GetData() {
242  return data_;
243  };
244 
245 
246  /** assignment operator
247  set the vector elementwise to scalar value
248  @author Jan Woetzel
249  @status alpha (02/28/2002) **/
250  inline Vector2<T> &operator=(const T& scalar) {
251  Set(scalar);
252  return *this;
253  };
254 
255  /** assignment operator
256  @author grest
257  */
258  inline Vector2<T> &operator=(const Vector2<T> &vec) {
259  Copy(vec.GetData());
260  return *this;
261  };
262 
263  /** access an element of the vector with 0-based indizes.
264  read only (no write)
265  member funciton const because it doesn't change this object
266  @author Jan Woetzel
267  @status untested (03/01/2002) **/
268  inline const T & operator[] (const int i) const {
269  BIASASSERT(0 <= i);
270  BIASASSERT(i < VECTOR2SIZE);
271  return data_[i];
272  };
273 
274  /** access an element of the vector with 0-based indizes.
275  write allowed
276  @author Jan Woetzel
277  @status untested (03/01/2002) **/
278  inline T & operator[] (const int i) {
279  BIASASSERT(0 <= i);
280  BIASASSERT(i < VECTOR2SIZE);
281  return data_[i];
282  };
283 
284 
285  /** read only access functions to use names instead of indizes
286  @return the projective coordinate of the vector
287  (they are usually not euclidean!)
288  @author Jan Woetzel
289  @status untested (03/04/2002) **/
290  inline T & GetX() {
291  return data_[0];
292  };
293  inline T & GetY() {
294  return data_[1];
295  };
296  inline T & GetW() {
297  return data_[1];
298  };
299 
300  // const non reference of the above JW 05/2003:
301  inline T GetX() const {
302  return data_[0];
303  };
304  inline T GetY() const {
305  return data_[1];
306  };
307  inline T GetW() const {
308  return data_[1];
309  };
310 
311 
312 
313  /** set (write) access functions to use names instead of indizes
314  @author Jan Woetzel
315  @status untested (03/04/2002) **/
316  inline void SetX(const T x) {
317  data_[0] = x;
318  };
319  inline void SetY(const T y) {
320  data_[1] = y;
321  };
322  inline void SetW(const T w) {
323  data_[1] = w;
324  };
325 
326  /// polar form JW
327  inline T& Radius()
328  { return (*this)[0]; };
329  inline T Radius() const
330  { return (*this)[0]; };
331 
332  /// polar form JW
333  inline T& Phi()
334  { return (*this)[1]; };
335  inline T Phi() const
336  { return (*this)[1]; };
337 
338 
339  // Define max and min function for clang compiler support
340  // for gcc 4.7
341  // #ifdef __clang__
342  #ifndef WIN32
343  inline T min(const T a, const T b) const {
344  return (a>b) ? b : a;
345  }
347  inline T max(const T a, const T b) const {
348  return (a<b) ? b : a;
349  }
350  #endif
351  // #endif
352 
353 
354  /** scalar product of two vectors, storing the result in result
355  @author Jan Woetzel
356  @status untested **/
357  inline void ScalarProduct(const Vector2<T> &argvec, T & result) const {
358  result = data_[0] * argvec[0] + data_[1] * argvec[1];
359  }
361  inline T ScalarProduct(const Vector2<T> &argvec) const {
362  return ((data_[0] * argvec[0]) + (data_[1] * argvec[1]));
363  }
364 
365 
366  /** multiply two vectors elementwise, storing the result in destvec
367  @author Jan Woetzel (03/06/2002) **/
368  inline void ElementwiseProduct(const Vector2<T> &argvec,
369  Vector2<T> &destvec) const
370  {
371  destvec[0] = data_[0] * argvec[0];
372  destvec[1] = data_[1] * argvec[1];
373  }
375  inline Vector2<T> ElementwiseProduct(const Vector2<T>& argvec) const
376  {
377  Vector2<T>destvec;
378  ElementwiseProduct(argvec, destvec);
379  return destvec;
380  }
381 
382 
383  /* elementwise division JW */
384  inline void ElementwiseDivision(const Vector2<T> &argvec,
385  Vector2<T> &destvec) const
386  {
387 
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];
392  }
394  inline Vector2<T> ElementwiseDivision(const Vector2<T>& argvec) const
395  {
396  Vector2<T>destvec;
397  ElementwiseDivision(argvec, destvec);
398  return destvec;
399  }
400 
401 
402 
403  /** @brief outer product, constructs a matrix (e.g. for covariance).
404  @author koeser 08/2005 (cloned from vector3) */
405  inline void OuterProduct(const BIAS::Vector2<T> &v,
406  BIAS::Matrix2x2<T>& mat) const
407  {
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];
412  }
414  inline Matrix2x2<T> OuterProduct(const BIAS::Vector2<T> &v) const
415  { Matrix2x2<T> m; OuterProduct(v, m); return m; }
416 
417 
418  /** Return the L1 norm: |a| + |b|
419  @author Ingo Thomsen
420  @date 04/11/2002
421  @status untested **/
422  inline T NormL1() const {
423  return (data_[0] > 0 ? data_[0] : data_[0] * -1)
424  + (data_[1] > 0 ? data_[1] : data_[1] * -1);
425  }
426 
427  /** Return the L2 norm: sqrt(a^2 + b^2)
428  @author Ingo Thomsen
429  @date 04/11/2002
430  @status untested **/
431  inline double NormL2() const {
432  return sqrt( (double) ( data_[0] * data_[0] + data_[1] * data_[1] ) );
433  }
434 
435  /// @brief return euclidean length @author JW
436  inline double Length() const
437  { return NormL2(); }
438 
439  /**
440  * @brief Vector2<T>::Normalize
441  */
442  inline void Normalize(){
443  const T len = (T) NormL2();
444  if(len > std::numeric_limits<T>::epsilon()){
445  data_[0] /= len;
446  data_[1] /= len;
447  }
448  else{
449  #ifdef BIAS_DEBUG
450  BEXCEPTION("length of normalized vector is "<<len);
451  #endif
452  }
453  }
454 
455  /** Return the euclidean distance of 2 vectors.
456  @author Birger Streckel
457  @date 08/2002
458  **/
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]) ));
462  }
463 
464  /** Return the squared distance of 2 vectors.
465  @author djung, untested
466  @date 2009/6
467  **/
468  inline double SqrDist(const Vector2<T> &vec) const
469  {
470  return static_cast<double> ((data_[0]-vec[0])*(data_[0]-vec[0]) +
471  ( data_[1]-vec[1])*(data_[1]-vec[1]));
472  }
473 
474  /** Return the maximum distance of 2 vectors.
475  @author djung, untested
476  @date 2009/6
477  **/
478  inline T DistLInf(const Vector2<T> &vec) const
479  {
480  return max(abs((T)(data_[0]-vec[0])), abs((T)(data_[1]-vec[1])));
481  }
482 
483  ///////////////////////////////////////////////////////////////////////
484  ///////////////// Comparison operators using scalars //////////////////
485  ///////////////////////////////////////////////////////////////////////
486 
487 
488  /** Comparison operator 'equal'
489  @author Ingo Thomsen
490  @status tested **/
491  inline bool operator== (const Vector2<T>& arg) const {
492  return ( (data_[0] == arg.data_[0]) && (data_[1] == arg.data_[1]) );
493  }
494 
495  /** Comparison operator 'not equal'
496  @author Ingo Thomsen
497  @status tested **/
498  inline bool operator!= (const Vector2<T>& arg) const {
499  return ( (data_[0] != arg.data_[0]) || (data_[1] != arg.data_[1]) );
500  }
501 
502 
503 
504  /////////////////////////////////////////////////////////////////////
505  //////////////////// Basic Operators using scalars //////////////////
506  /////////////////////////////////////////////////////////////////////
507 
508 
509  /** Addition (in place) of an scalar
510  @author Ingo Thomsen **/
511  inline void AddIP (const T& scalar) {
512  Add(scalar, *this);
513  }
514 
515  /** Addition with a scalar, storing results in destionation vector
516  @author Ingo Thomsen **/
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;
520  }
521 
522  /** Substraction (in place) of an scalar
523  @author Ingo Thomsen **/
524  inline void SubIP (const T& scalar) {
525  Sub(scalar, *this);
526  }
527 
528  /** Substraction with a scalar, storing results in destionation vector
529  @author Ingo Thomsen **/
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;
533  }
534 
535  /** Multiplication (in place) of an scalar
536  @author Ingo Thomsen **/
537  inline void MultiplyIP (const T& scalar) {
538  Multiply(scalar, *this);
539  }
540 
541  /** Multiplication with a scalar, storing results in destionation vector
542  @author Ingo Thomsen **/
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;
546  }
547 
548  /** Division (in place) of an scalar
549  @author Ingo Thomsen **/
550  inline void DivideIP (const T& scalar) {
551  Divide(scalar, *this);
552  }
553 
554  /** elementwise Division (in place) by a vector
555  @author JW **/
556  inline void DivideIP (const Vector2<T>& vec) {
557  Divide(vec, *this);
558  }
559 
560 
561  /** Division with a scalar, storing results in destination vector
562  @author Ingo Thomsen **/
563  inline void Divide (const T& scalar, Vector2<T>& dest) const {
564 #ifdef BIAS_DEBUG
565  if (scalar == 0 )
566  BIASERR("Division by Zero!");
567 #endif
568  dest.data_[0] = data_[0] / scalar;
569  dest.data_[1] = data_[1] / scalar;
570  }
571 
572  /* elementwise division by vector @author JW */
573  inline void Divide (const Vector2<T>& vec, Vector2<T>& dest) const {
574 #ifdef BIAS_DEBUG
575  if (vec[0]==0 || vec[1]==0)
576  BIASERR("Division by Zero!");
577 #endif
578  this->ElementwiseDivision(vec, dest);
579  }
580 
581 
582  /////////////////////////////////////////////////////////////////////
583  //////////////////// Basic Operators using vectors //////////////////
584  /////////////////////////////////////////////////////////////////////
585 
586  /** in place adding
587  @author Ingo Thomsen
588  @status tested **/
589  inline void AddIP(const Vector2<T> &argvec) {
590  Add(argvec, *this);
591  }
592 
593 
594  /** adding of two vectors, storing the result in destvec
595  @author Ingo Thomsen
596  @status tested **/
597  inline void Add(const Vector2<T> &argvec, Vector2<T> &destvec) const {
598  destvec[0] = data_[0] + argvec[0];
599  destvec[1] = data_[1] + argvec[1];
600  }
601 
602  /** in place subtraction
603  @author Ingo Thomsen
604  @status tested **/
605  inline void SubIP(const Vector2<T> &argvec) {
606  Sub(argvec, *this);
607  }
608 
609 
610  /** subtracting of two Vectors, storing the result in destvec
611  @author Ingo Thomsen
612  @status tested **/
613  inline void Sub(const Vector2<T> &argvec, Vector2<T> &destvec) const {
614  destvec[0] = data_[0] - argvec[0];
615  destvec[1] = data_[1] - argvec[1];
616  }
618  inline void Fill(T value)
619  { data_[0] = data_[1] = value; };
620 
621 
622  /** coordinate transform
623  compute the euclidean coord p=(x,y)
624  for (this) given polarcoord (radius, phi) with
625  radius : distance to center,
626  phi : radian angle between positive x-axis and p=(x,y)
627  assume (x,z) is in orthogonal right-hand-system.
628  @author Jan Woetzel 09/2003 */
629  BIAS::Vector2<T> CoordPolarToEuclidean() const;
630 
631  // deprecated version!
632  inline BIAS::Vector2<T> CoordPolarToEuclidian() const {
633  return this->CoordPolarToEuclidean();
634  };
635 
636  /** coordinate transform
637  compute the polar coordinates (radius, phi) for (this) given
638  eucldian point (x,y). with
639  radius : distance to center,
640  phi : radian angle between positive x-axis and p=(x,y)
641  assume (x,z) is in orthogonal right-hand-system.
642  @author Jan Woetzel 09/2003 */
643  BIAS::Vector2<T> CoordEuclideanToPolar() const;
644 
645  // deprecated version!
646  BIAS::Vector2<T> CoordEuclidianToPolar() const {
647  return this->CoordEuclideanToPolar();
648  };
649 
650  /** method to load directly from a given filename.
651  internally using stream operator
652  @author Jan Woetzel 09/2005
653  @return false in case of error, true in case of success */
654  bool Load(const std::string & filename);
655 
656  /** method to save directly to a given filename.
657  internally using stream operator
658  @author Jan Woetzel 09/2009
659  @return false in case of error, true in case of success */
660  bool Save(const std::string & filename) const;
661 
662 #ifdef WIN32
663  inline T abs(const T a) const {
664  T b=(a<0) ? (T)(-1) : (T)(1);
665  return a*b;
666  }
667 
668  inline T max(const T a, const T b) const {
669  return (a<b) ? b : a;
670  }
671 #endif
672 
673 
674  protected:
675  // the 2 elements of the vector without additional pointers etc...
676  // it is the *same* as n individual members, so there is *NO* additional pointer
677  // in particular an array of Vector2 is the same as one big array of T (JW)
678  T data_[VECTOR2SIZE]; // the elements of the vector
679 
680  };
681  // ==============================================================
682  // === END OF CLASS Vector2 ===================================
683  // ==============================================================
684 
685 
686 
687 
688  // --------------- stream I/O ---------------------------------------
689 
690 
691  /** @relates Vector2
692  @brief Output with streams
693  Specialized for uc numerical display
694  @author Jan Woetzel */
695  inline
696  std::ostream& operator<<(std::ostream & os, const Vector2<char> & vec)
697  {
698  if (!IsConsoleStream(os))
699  {
700  os << VECTOR2SIZE << NL;
701  for (int row = 0; row < VECTOR2SIZE; row++) {
702  os<<std::setw(3)<<(int)vec.GetData()[row]<<" "<<NL;
703  }
704  } else {
705  os << "[ ";
706  for (int row = 0; row < VECTOR2SIZE; row++)
707  os<<std::setw(3)<<(int)vec.GetData()[row]<<" ";
708  os << "] ";
709  }
710  return os;
711  }
712 
713  inline
714  std::ostream& operator<<(std::ostream & os, const Vector2<unsigned char> & vec)
715  {
716  if (!IsConsoleStream(os))
717  {
718  os << VECTOR2SIZE << NL;
719  for (int row = 0; row < VECTOR2SIZE; row++) {
720  os<<std::setw(3)<<(unsigned int)vec.GetData()[row]<<" "<<NL;
721  }
722  } else {
723  os << "[ ";
724  for (int row = 0; row < VECTOR2SIZE; row++)
725  os<<std::setw(3)<<(unsigned int)vec.GetData()[row]<<" ";
726  os << "] ";
727  }
728  return os;
729  }
730 
731 
732  /** @relates Vector2
733  @brief Output with streams
734  @author Jan Woetzel */
735  template <class T>
736  inline
737  std::ostream& operator<<(std::ostream & os, const Vector2<T>& vec)
738  {
739  if (!IsConsoleStream(os))
740  {
741  os << VECTOR2SIZE << NL;
742  for (int row = 0; row < VECTOR2SIZE; row++) {
743  os<<std::setprecision( DecimalsToStore<T>() );
744  os<<vec.GetData()[row]<<" "<<NL;
745  }
746  } else {
747  // cout or cerr
748  os<<"[ ";
749  for (int row = 0; row < VECTOR2SIZE; row++){
750  os<<std::setprecision(CONSOLE_PRECISION_DIGITS);
751  os<<vec.GetData()[row]<<" "; // less precise display in console
752  }
753  os<<"] ";
754  }
755  return os;
756  }
757 
758 
759  /** @relates Vector2
760  @brief Input with streams
761  @author Jan Woetzel (03/01/2002) **/
762  inline
763  std::istream& operator>>(std::istream &is, Vector2 <char>& vec)
764  {
765  int N=0; // first argument of the stream is the size
766  is >> N; // read size first to be compatible to Vector<>
767  if (!(N == (int)vec.Size())) {
768  BIASERR("can't assign a Vector2<T> with a dim " << N << " vector");
769  } else {
770  int tmp(0);
771  for (int row = 0; row < N; row++) {
772  is >> tmp;
773  BIASASSERT(tmp>=std::numeric_limits<char>::min() );
774  BIASASSERT(tmp<=std::numeric_limits<char>::max() );
775  vec.GetData()[row]=(char)tmp;
776  };
777  };
778  return is;
779  }
780 
781  inline
782  std::istream& operator>>(std::istream &is, Vector2 <unsigned char>& vec)
783  {
784  int N=0; // first argument of the stream is the size
785  is >> N; // read size first to be compatible to Vector<>
786  if (!(N == (int)vec.Size())) {
787  BIASERR("can't assign a Vector2<T> with a dim " << N << " vector");
788  } else {
789  int tmp(0);
790  for (int row = 0; row < N; row++) {
791  is >> tmp;
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;
795  };
796  };
797  return is;
798  }
799 
800 
801 
802  /** @relates Vector2
803  @brief Input with streams
804  @author Jan Woetzel (03/01/2002) **/
805  template <class T>
806  inline
807  std::istream& operator>>(std::istream &is, Vector2 <T>& vec)
808  {
809  int N=0; // first argument of the stream is the size
810  is >> N; // read size first to be compatible to Vector<>
811  if (!(N == (int)vec.Size())) {
812  // wrong size
813  BIASERR("can't assign a Vector2<T> with a dim " << N << " vector");
814  } else {
815  // correct size, sequential read elementwise
816  for (int row = 0; row < N; row++) {
817  is >> vec.GetData()[row];
818  };
819  };
820  return is;
821  }
822 
823  // ------------------ operators with vectors seee Operators.hh ---------------------------
824 
825 
826 
827  /** add operator for two Vectors
828  @author Ingo Thomsen, tested **/
829  template <class T> BIASMathBase_EXPORT
830  inline Vector2<T>& operator+=(Vector2<T> &vec, const Vector2<T> &argvec) {
831  vec.Add(argvec, vec);
832  return vec;
833  }
834 
835  /** sub operator for two Vectors
836  @author Ingo Thomsen
837  @status tested **/
838  template <class T>
839  inline Vector2<T> &operator-= (Vector2<T> &vec, const Vector2<T> &argvec) {
840  vec.Sub(argvec, vec);
841  return vec;
842  }
843 
844  /** elementwise division @author JW */
845  template <class T> BIASMathBase_EXPORT
846  inline Vector2<T>& operator/=(Vector2<T> &vec, const Vector2<T> &argvec) {
847  vec.DivideIP(argvec, vec);
848  return vec;
849  }
850 
851  // --------------- operators with scalars -----------------------------
852 
853 
854  /** Addition operator with scalar argument
855  @author Ingo Thomsen, tested */
856  template <class T>
857  inline Vector2<T>& operator+=(Vector2<T>& vec, const T& scalar) {
858  vec.AddIP(scalar);
859  return vec;
860  }
861 
862  /** Substraction operator with scalar argument
863  @author Ingo Thomsen, tested **/
864  template <class T>
865  inline Vector2<T>& operator-=(Vector2<T>& vec, const T& scalar) {
866  vec.SubIP(scalar);
867  return vec;
868  }
869 
870  /** Multiplication operator with scalar argument
871  @author Ingo Thomsen, tested **/
872  template <class T>
873  inline Vector2<T>& operator*= (Vector2<T>& vec, const T& scalar) {
874  vec.MultiplyIP(scalar);
875  return vec;
876  }
877 
878  /** Division operator with scalar argument
879  @author Ingo Thomsen, tested **/
880  template <class T>
881  inline Vector2<T>& operator/= (Vector2<T>& vec, const T& scalar) {
882  vec.DivideIP(scalar);
883  return vec;
884  }
885 
886 
887 } // namespace BIAS
888 #endif
889 
void Add(const T &scalar, Vector2< T > &dest) const
Addition with a scalar, storing results in destionation vector.
Definition: Vector2.hh:516
T const * end() const
Iterator pointing to one element after the last vector element.
Definition: Vector2.hh:169
T const * begin() const
Iterator to first component of vector.
Definition: Vector2.hh:160
class for column vectors with arbitrary size
is a &#39;fixed size&#39; quadratic matrix of dim.
Definition: Matrix2x2.hh:48
void Sub(const T &scalar, Vector2< T > &dest) const
Substraction with a scalar, storing results in destionation vector.
Definition: Vector2.hh:529
class BIASMathBase_EXPORT Vector2
Definition: Matrix2x2.hh:45
bool IsConsoleStream(const std::ostream &os)
Helper returning true on console ostreams like cerr, cout Useful to distinguish between console (cout...
Definition: Utils.hh:54
std::istream & operator>>(std::istream &is, Vector2< char > &vec)
Input with streams.
Definition: Vector2.hh:763
BIASMathBase_EXPORT Vector2< T > & operator/=(Vector2< T > &vec, const Vector2< T > &argvec)
elementwise division
Definition: Vector2.hh:846
Vector2(const T v0, const T v1)
constructor with element assigment using explicit values
Definition: Vector2.hh:114
void SubIP(const T &scalar)
Substraction (in place) of an scalar.
Definition: Vector2.hh:523
const unsigned int Size() const
Definition: Vector2.hh:223
void MultiplyIP(const T &scalar)
Multiplication (in place) of an scalar.
Definition: Vector2.hh:536
const T * GetData() const
get the data pointer the member function itself is const (before {..}) because it doesn&#39;t change the ...
Definition: Vector2.hh:236
Vector2< T > & operator-=(Vector2< T > &vec, const Vector2< T > &argvec)
sub operator for two Vectors
Definition: Vector2.hh:839
~Vector2()
destructor
Definition: Vector2.hh:88
class Vector2 contains a Vector of dim.
Definition: Vector2.hh:79
T * begin()
Definition: Vector2.hh:163
BIASMathBase_EXPORT Vector2< T > & operator+=(Vector2< T > &vec, const Vector2< T > &argvec)
add operator for two Vectors
Definition: Vector2.hh:830
void AddIP(const T &scalar)
Addition (in place) of an scalar.
Definition: Vector2.hh:510
Vector2< T > & operator*=(Vector2< T > &vec, const T &scalar)
Multiplication operator with scalar argument.
Definition: Vector2.hh:873
T data_[VECTOR2SIZE]
Definition: Vector2.hh:677
void DivideIP(const T &scalar)
Division (in place) of an scalar.
Definition: Vector2.hh:549
Vector2(const T *pv)
assignment with an array of values which is copied into this ones class members
Definition: Vector2.hh:124
std::istream & operator>>(std::istream &is, Vector2< T > &vec)
Input with streams.
Definition: Vector2.hh:807
Vector2()
default constructor
Definition: Vector2.hh:94
Vector2(char *s)
constructor with element assignment
Definition: Vector2.hh:131
Vector2(const Vector2< T > &vec)
copy constructor
Definition: Vector2.hh:99
Vector2(const T &scalar)
assignment with a constant value for all elements
Definition: Vector2.hh:106
BIASCommon_EXPORT std::istream & operator>>(std::istream &is, BIAS::TimeStamp &ts)
Standard input operator for TimeStamps.
Definition: TimeStamp.cpp:157