Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Vector3.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 _Vector3_hh_
27 #define _Vector3_hh_
28 
29 #include <bias_config.h>
30 
31 #include <iostream>
32 #include <vector>
33 //#include <limits>
34 //#include <float.h>
35 
36 #ifdef BIAS_HAVE_CPP11
37  #include <initializer_list>
38 #endif
39 
40 #include <Base/Debug/Error.hh>
41 #include <Base/Debug/Exception.hh>
42 #include <Base/Math/Vector.hh>
43 #include <Base/Math/Matrix3x3.hh>
44 #include <Base/Math/Operators.hh>
45 #include <Base/Math/Utils.hh>
46 
47 #define VECTOR3_SIZE 3
48 
49 namespace BIAS {
50 
51  /** @class Vector3
52  @ingroup g_math
53  @brief class Vector3 contains a Vector of fixed dim. 3
54 
55  It's elment type is templated.
56  manual loop unrolling is used if possible.
57  The Vector is in row-major order (3 rows, 1 column)
58  the indizes begin with zero (to size-1)
59 
60  operators are no longer class members
61  removed memory leak by returning reference in operators
62  removed inline code from class definition, woelk 01 2003
63 
64  @author Jan Woetzel, woelk
65  **/
66 
67  // forward declarations
68  // DLL JW
69  //template <class T> class BIASMathBase_EXPORT Matrix3x3;
70 
71 
72 
73  template <class T>
74  class BIASMathBase_EXPORT Vector3 {
75  public:
76  typedef T value_type;
77 
78  Vector3() {};
79 
80  ~Vector3() {};
81 
82  /** copy constructor
83  @author Jan Woetzel (03/05/2002)**/
84  inline Vector3(const Vector3<T>& vec) { *this = vec; };
85 
86  /** assignment with a constant value for all elements
87  @author Jan Woetzel (03/01/2002) **/
88  explicit inline Vector3(const T & scalar) { Set(scalar); };
89 
90  /** assignment with an array of values
91  which is copied into this ones class members
92  @author Jan Woetzel (02/28/2002) **/
93  explicit inline Vector3(const T* pv) { Set(pv); };
94 
95  /** constructor with element assignment
96  @author Jan Woetzel
97  @status alpha (03/01/2002) **/
98  explicit inline Vector3(char *s);
99 
100  /** constructor with element assigment using explicit values
101  @author Ingo Thomsen, tested (03/04/2002)**/
102  inline Vector3(const T &x, const T &y, const T &z) { Set(x, y, z); };
103 
104  /** cast constructor
105  @author Jan Woetzel, untested (03/22/2002) **/
106  Vector3(const Vector<T>& v);
107 
108 #ifdef BIAS_HAVE_CPP11
109 
110  /** cast constructor
111  @author Stefan Reinhold, (05/08/2015) **/
112  template <class S>
113  inline Vector3(Vector3<S> const& v)
114  : Vector3(static_cast<T>(v[0]), static_cast<T>(v[1]),
115  static_cast<T>(v[2])) {}
116 
117  /** initializer list constructor (C++11)
118  @author Stefan Reinhold
119  @status untested (08/13/2014) **/
120  Vector3(const std::initializer_list< T> &i) {
121  BIASASSERT(i.size() >= 3);
122  auto iter = i.begin();
123  data_[0] = *iter;
124  data_[1] = *(++iter);
125  data_[2] = *(++iter);
126  }
127 #endif
128 
129  /** @name Iterator Functions
130  @{
131  */
132  /** Iterator to first component of vector
133  * @author Stefan Reinhold **/
134  inline T const *begin() const {
135  return &data_[0];
136  }
137  inline T *begin() {
138  return &data_[0];
139  }
140 
141  /** Iterator pointing to one element after the last vector element
142  * @author Stefan Reinhold **/
143  inline T const *end() const {
144  return &data_[VECTOR3_SIZE];
145  }
146  inline T *end() {
147  return &data_[VECTOR3_SIZE];
148  }
149  /** @}
150  */
151 
152  /** @name Set Functions
153  @{
154  */
155  /** copy the array of vectorsize beginning at *T to this->data_
156  @author Jan Woetzel, untested (02/28/2002) **/
157  inline void Set(const T *pv);
158 
159  /** set all elements to a scalar value
160  @author Jan Woetzel, untested (02/28/2002) **/
161  inline void Set(const T& scalar) { Set(scalar, scalar, scalar); };
162 
163  /** set elementwise with given scalar values
164  @author Jan Woetzel
165  @status alpha (02/28/2002) **/
166  inline void Set(const T& x, const T& y, const T& z);
167 
168  /** Sets this form elements of Vector<T>. */
169  inline void Set(const Vector<T> &vec);
170 
171  /** set all values to 0
172  @author Jan Woetzel 01/2003 **/
173  inline void SetZero();
174  /// stl conform interface
175  inline void clear();
176 
177  // @}
178 
179  /** @name Get Functions
180  @{
181  */
182  /** @return the size of this vector
183  @author Jan Woetzel (02/28/2002) **/
184  inline const unsigned int Size() const { return size(); };
185  // stl conform interface
186  inline const unsigned int size() const { return VECTOR3_SIZE; };
187  // compatibility interface
188  inline const unsigned int GetNumElements() const { return size(); };
189 
190  /** @return true iff all elements are equal zero JW */
191  inline bool IsZero() const;
192 
193  /** @brief returns the Euclidean Length of the Vector @author Jan Woetzel */
194  inline double Length() const
195  { return NormL2(); };
196 
197  /** get the data pointer
198  the member function itself is const (before {..}) because it doesn't
199  change the this object.
200  @return the const pointer to the data array for reading
201  @author Jan Woetzel
202  @status untested (02/28/2002) **/
203  inline const T *GetData() const { return data_; };
204  inline T *GetData() { return data_; };
205 
206  /** Return the L1 norm: |a| + |b| + |c|
207  @author Ingo Thomsen
208  @date 04/11/2002
209  @status tested **/
210  inline T NormL1() const;
211 
212  /** the L2 norm sqrt(a^2 + b^2 + c^2)
213  @author woelk 01 2003 */
214  inline double NormL2() const;
215 
216  /** Return the euclidean distance of 2 vectors.
217  @author Birger Streckel
218  @date 08/2002
219  **/
220  inline double Dist(const Vector3<T> &vec) const;
221 
222  /** Return the L inf distance of 2 vectors.
223  @author djung
224  @date 2009/07 untested
225  **/
226  inline double DistLinf(const Vector3<T> &vec) const;
227 
228  // @}
229 
230  /** @name Normalization
231  @{
232  */
233  /** @brief normalize this vector to length 1
234  @author Jan Woetzel / koeser
235  @return reference to updated (*this)
236  @date 04/25/2002 **/
237  inline Vector3<T>& Normalize();
238 
239  /** @brief normalizes the vector to 1 and transfroms the associated
240  covariance matrix
241  @author woelk 03/2006 */
242  Vector3<T>& Normalize(Matrix3x3<T>& cov);
243 
244 
245  /** @brief return a normalized vector of this
246 
247  norm. to length 1
248  @author Jan Woetzel
249  @date 06/2003 **/
250  BIAS::Vector3<T> GetNormalized() const;
251 
252  // @}
253 
254 
255  /** @name Arithmetic
256  @{
257  */
258  /** cross product of two vectors
259  destvec = this x argvec
260  @author Ingo Thomsen, Jan Woetzel
261  @status alpha **/
262  inline void CrossProduct(const Vector3<T>& argvec,
263  Vector3<T>& destvec) const;
264 
265  /** @return cross product = this x argvec
266  @author Jan Woetzel
267  @status alpha **/
268  inline Vector3<T> CrossProduct(const Vector3<T>& argvec) const
269  { Vector3<T> destvec; CrossProduct(argvec, destvec); return destvec; };
270 
271  /** scalar product (=inner product) of two vectors,
272  storing the result in result
273  @author Jan Woetzel **/
274  inline void ScalarProduct(const Vector3<T>& argvec, T& result) const;
275 
276  /** scalar product (=inner product) of two vectors returns a scalar
277  @author Jan Woetzel, Ingo Thomsen
278  @status alpha **/
279  inline T ScalarProduct(const Vector3<T>& argvec) const;
280 
281  /** multiply two vectors elementwise and store the result vector to destvec
282  @author Jan Woetzel
283  @status alpha (03/06/2002) **/
284  inline void ElementwiseProduct(const Vector3<T>& argvec,
285  Vector3<T>& destvec) const;
286 
287  /** multiply two vectors elementwise and return the result vector
288  @author Jan Woetzel (03/06/2002) **/
289  inline Vector3<T> ElementwiseProduct(const Vector3<T>& argvec) const
290  {
291  Vector3<T>destvec;
292  ElementwiseProduct(argvec, destvec);
293  return destvec;
294  }
295 
296  /* divide elementwise @author JW */
297  inline void ElementwiseDivision(const Vector3<T>& argvec,
298  Vector3<T>& destvec) const;
299 
300  /** divide elementwise @author Jan Woetzel */
301  inline Vector3<T> ElementwiseDivision(const Vector3<T>& argvec) const
302  {
303  Vector3<T>destvec;
304  ElementwiseDivision(argvec, destvec);
305  return destvec;
306  }
307 
308 
309  /** Addition (in place) of an scalar
310  @author Ingo Thomsen **/
311  inline void AddIP(const T& scalar) { Add(scalar, *this); }
312 
313  /** Addition with a scalar, storing results in destionation vector
314  @author Ingo Thomsen **/
315  inline void Add(const T& scalar, Vector3<T>& dest) const;
316 
317  /** Substraction (in place) of an scalar
318  @author Ingo Thomsen **/
319  inline void SubIP(const T& scalar) { Sub(scalar, *this); }
320 
321  /** Substraction with a scalar, storing results in destination vector
322  @author Ingo Thomsen **/
323  inline void Sub(const T& scalar, Vector3<T>& dest) const;
324 
325  /** Multiplication (in place) of an scalar
326  @author Ingo Thomsen **/
327  inline void MultiplyIP(const T& scalar) { Multiply(scalar, *this); }
328  inline void MultIP(const T& scalar) { MultiplyIP(scalar); }
329 
330  /** Multiplication with a scalar, storing results in destination vector
331  @author Ingo Thomsen **/
332  inline void Multiply(const T& scalar, Vector3<T>& dest) const;
333  inline void Mult(const T& scalar, Vector3<T>& dest) const
334  { Multiply(scalar, dest); }
335 
336  /** Division (in place) of an scalar
337  @author Ingo Thomsen**/
338  inline void DivideIP(const T& scalar);
339  inline void DivIP(const T& scalar) { DivideIP(scalar); }
340 
341  /** Division with a scalar, storing results in destination vector
342  @author Ingo Thomsen **/
343  inline void Divide(const T& scalar, Vector3<T>& dest) const ;
344  inline void Div(const T& scalar, Vector3<T>& dest) const
345  {Divide(scalar, dest);};
346 
347  /** in place adding
348  @author Ingo Thomsen
349  @status tested **/
350  inline void AddIP(const Vector3<T>&argvec) { Add(argvec, *this);}
351 
352  /** adding of two vectors, storing the result in destvec
353  @author Ingo Thomsen
354  @status tested **/
355  inline void Add(const Vector3<T>&argvec, Vector3<T>&destvec) const;
356 
357  /** in place substraction
358  @author Ingo Thomsen
359  @status tested **/
360  inline void SubIP(const Vector3<T>& argvec) { Sub(argvec, *this); }
361 
362  /** subtracting of two Vectors, storing the result in destvec
363  @author Ingo Thomsen
364  @status tested **/
365  inline void Sub(const Vector3<T>&argvec, Vector3<T>&destvec) const;
366 
367  // @}
368 
369  /** @name Misc
370  @{
371  */
372 
373  /** outer product, constructs a matrix.
374  Often written as (*this) * v^T for col vectors
375  made it fast
376  @author Daniel Grest, Sept 2003, woelk 07/2004
377  @status tested */
378  void OuterProduct(const Vector3<T> &v, Matrix3x3<T>& res) const;
380  inline Matrix3x3<T> OuterProduct(const Vector3<T> &v) const
381  {
382  Matrix3x3<T> m;
383  OuterProduct(v, m);
384  return m;
385  }
386 
387  /** constructs a skew symmetric 3x3 matrix from (*this),
388  which can be used instead of the cross product
389  @author koeser 09/2003 */
390  Matrix<T> GetSkewSymmetricMatrix() const;
391 
392 
393  /** coordinate transfrom.
394  compute the euclidean coord p=(x,y,z) for (this) given sphere
395  coord sph=(radius, phi, theta)
396  with
397  radius : distance to center,
398  theta : radian angle between positive z-axis and p=(x,y,z). (0-pi)
399  phi : radian angle between positive x-axis and projection
400  of p=(x,y,z) into XY plane. (0-2pi)
401  assume (x,y,z) is in orthogonal right-hand-system.
402  @author Jan Woetzel 09/2003 */
403  BIAS::Vector3<T> CoordSphereToEuclidean() const;
404  BIAS::Vector3<T> PolarToCartesian() const {
405  return this->CoordSphereToEuclidean();
406  };
407  // deprecated version!
408  inline BIAS::Vector3<T> CoordSphereToEuclidian() const {
409  return this->CoordSphereToEuclidean();
410  };
411 
412  /** coordinate transform.
413  compute the sphere coordinates(r, phi, theta) for (this) given
414  euclidean point p=(x,y,z)
415  with
416  radius : distance to center,
417  theta : radian angle between positive z-axis and p=(x,y,z). (0-pi)
418  phi : radian angle between positive x-axis and projection
419  of p=(x,y,z) into XY plane. (0-2pi)
420  assume (x,y,z) is in orthogonal right-hand-system.
421  @author Jan Woetzel 09/2003 */
422  BIAS::Vector3<T> CoordEuclideanToSphere() const;
423  BIAS::Vector3<T> CartesianToPolar() const {
424  return this->CoordEuclideanToSphere();
425  }
426  // deprecated version!
427  inline BIAS::Vector3<T> CoordEuclidianToSphere() const {
428  return this->CoordEuclideanToSphere();
429  };
430 
431  /** r for polar/sphere content JW */
432  inline T& Radius()
433  { return (*this)[0]; };
434  inline T Radius() const
435  { return (*this)[0]; };
437  inline T& Phi()
438  { return (*this)[1]; };
439  inline T Phi() const
440  { return (*this)[1]; };
442  inline T& Theta()
443  { return (*this)[2]; };
444  inline T Theta() const
445  { return (*this)[2]; };
446 
447  /** reads the TC part of a BOG file
448  which is used by Daimler Chrysler
449  for storing Camera center position.
450  @return 0 for success, negative for errror
451  @author Jan Woetzel 03/2005 */
452  int LoadBogTC(const std::string & filename);
453 
454  /** method to load directly from a given filename.
455  internally using stream operator
456  @author Jan Woetzel 09/2005
457  @return false in case of error, true in case of success */
458  bool Load(const std::string & filename);
459 
460  /** method to save directly to a given filename.
461  internally using stream operator
462  @author Jan Woetzel 09/2009
463  @return false in case of error, true in case of success */
464  bool Save(const std::string & filename) const;
465 
466 
467  // @}
468 
469  /** @name Operators
470  @{
471  */
472  /** assignment operator
473  @author woelk 01 2003 **/
474  inline Vector3<T>& operator=(const Vector3<T>& vec)
475  { memcpy((void *)data_, (void *)vec.GetData(), VECTOR3_SIZE*sizeof(T));
476  return *this; };
477 
478  /** assignment operator
479  @author frahm 03 2004 **/
480  inline Vector3<T>& operator=(const Vector<T>& vec)
481  { if (vec.size() == VECTOR3_SIZE)
482  memcpy((void *)data_, (void *)vec.GetData(), VECTOR3_SIZE*sizeof(T));
483  return *this; };
484 
485  /** access an element of the vector with 0-based indizes.
486  read only (no write)
487  member function const because it doesn't change this object
488  @author Jan Woetzel
489  @status untested (03/01/2002) **/
490  inline const T& operator[] (const int i) const;
491 
492  /** access an element of the vector with 0-based indizes.
493  write allowed
494  @author Jan Woetzel
495  @status untested (03/01/2002) **/
496  inline T& operator[] (const int i);
497 
498  /** Comparison operator 'equal'
499  @author Ingo Thomsen
500  @status tested **/
501  inline bool operator==(const Vector3<T>& arg) const
502  { return ((data_[0]==arg.data_[0])&&(data_[1]==arg.data_[1])&&(data_[2]==arg.data_[2])); }
503 
504  /** Comparison operator 'not equal'
505  @author woelk 01 2003 **/
506  inline bool operator!=(const Vector3<T>& arg) const
507  { return ! operator==(arg); }
508 
509  // @}
510 
511  protected:
512  // the 3 elements of the vector without additional pointers etc...
513  // it is the *same* as 3 individual members, so there is *NO* additional pointer
514  // in particular an array of Vector3 is th3 same as one big array of T (JW)
515  T data_[VECTOR3_SIZE];
516 
517  };
518  // ======================================================
519  // === END OF CLASS DECLARATION =========================
520  // ======================================================
521 
522 
523  template <class T>
525  std::istringstream inst(s);
526  inst >> data_[0];
527  inst >> data_[1];
528  inst >> data_[2];
529  }
530 
531  template <class T>
532  void Vector3<T>::Set(const T *pv) {
533  // better use memcopy ?
534  data_[0] = pv[0];
535  data_[1] = pv[1];
536  data_[2] = pv[2];
537  }
538 
539  template <class T>
540  void Vector3<T>::Set(const T& x, const T& y, const T& z) {
541  data_[0] = x;
542  data_[1] = y;
543  data_[2] = z;
544  }
545 
546  template <class T>
547  void Vector3<T>::Set(const Vector<T>& vec) {
548  if (vec.size() != VECTOR3_SIZE){
549  BIASERR("The vector for initialization of Vector3 has not length 3");
550  } else {
551  data_[0] = vec[0];
552  data_[1] = vec[1];
553  data_[2] = vec[2];
554  }
555  }
556 
557  // JW 01/2003
558  template <class T>
560  Set( (T)0);
561  }
562 
563  template <class T>
565  SetZero();
566  }
567 
568  // JW 09/2003
569  template <class T>
570  bool
572  return ( (data_[0]==(T)0)
573  && (data_[1]==(T)0)
574  && (data_[2]==(T)0) );
575  }
576 
577 
578 
579  template <class T>
580  const T& Vector3<T>::operator[] (const int i) const {
581  BIASASSERT(0 <= i);
582  BIASASSERT(i < VECTOR3_SIZE);
583  return data_[i];
584  }
585 
586  template <class T>
587  T& Vector3<T>::operator[] (const int i) {
588  BIASASSERT(0 <= i);
589  BIASASSERT(i < VECTOR3_SIZE);
590  return data_[i];
591  }
592 
593  template <class T>
595  Vector3<T>& destvec) const {
596  destvec[0] = data_[1] * argvec[2] - data_[2] * argvec[1];
597  destvec[1] = data_[2] * argvec[0] - data_[0] * argvec[2];
598  destvec[2] = data_[0] * argvec[1] - data_[1] * argvec[0];
599  }
600 
601 
602  template <class T>
603  void Vector3<T>::ScalarProduct(const Vector3<T>& argvec, T& result) const {
604  result = data_[0] * argvec[0];
605  result += data_[1] * argvec[1];
606  result += data_[2] * argvec[2];
607  }
608 
609  template <class T>
610  T Vector3<T>::ScalarProduct(const Vector3<T>& argvec) const {
611  T result;
612  ScalarProduct(argvec, result);
613  return result;
614  }
615 
616  template <class T>
618  Vector3<T>& destvec) const {
619  destvec[0] = data_[0] * argvec[0];
620  destvec[1] = data_[1] * argvec[1];
621  destvec[2] = data_[2] * argvec[2];
622  }
623 
624  template <class T>
626  Vector3<T>& destvec) const {
627  destvec[0] = data_[0] / argvec[0];
628  destvec[1] = data_[1] / argvec[1];
629  destvec[2] = data_[2] / argvec[2];
630  }
631 
632  template <class T>
633  double Vector3<T>::NormL2() const {
634  return sqrt(double(data_[0]*data_[0] + data_[1]*data_[1]
635  + data_[2]*data_[2]));
636  }
637 
638  template <class T>
639  double Vector3<T>::Dist(const Vector3<T> &vec) const {
640  return sqrt(double((data_[0]-vec[0])*(data_[0]-vec[0]) +
641  (data_[1]-vec[1])*(data_[1]-vec[1]) +
642  (data_[2]-vec[2])*(data_[2]-vec[2])));
643  }
644 
645  template <class T>
646  double Vector3<T>::DistLinf(const Vector3<T> &vec) const
647  {
648  double
649  res = 0.,
650  tmp = 0.;
651  for(unsigned int i = 0; i < 3; i++)
652  {
653  tmp = fabs(static_cast<double> (data_[i] - vec[i]));
654  if(res < tmp)
655  {
656  res = tmp;
657  }
658  }
659  return res;
660  }
661 
662  template <class T>
664  const T act_length = (T) NormL2(); // euclidean length
665  // check if not zero
666  if (act_length > std::numeric_limits<T>::epsilon() )
667  {
668  // divide each element by length
669  this->DivideIP(act_length);
670  } else {
671  //BEXCEPTION("no normalization possible because act_length="<<act_length);
672  }
673  return (*this);
674  }
675 
676  template <class T>
677  T Vector3<T>::NormL1() const {
678  return (data_[0] > 0 ? data_[0] : data_[0] * -1)
679  + (data_[1] > 0 ? data_[1] : data_[1] * -1)
680  + (data_[2] > 0 ? data_[2] : data_[2] * -1);
681  }
682 
683  template <class T>
684  void Vector3<T>::Add(const T& scalar, Vector3<T>& dest) const {
685  dest.data_[0] = data_[0] + scalar;
686  dest.data_[1] = data_[1] + scalar;
687  dest.data_[2] = data_[2] + scalar;
688  }
689 
690  template <class T>
691  void Vector3<T>::Add(const Vector3<T>& argvec, Vector3<T>& destvec) const {
692  destvec[0] = data_[0] + argvec[0];
693  destvec[1] = data_[1] + argvec[1];
694  destvec[2] = data_[2] + argvec[2];
695  }
696 
697  template <class T>
698  void Vector3<T>::Multiply (const T& scalar, Vector3<T>& dest) const {
699  dest.data_[0] = data_[0] * scalar;
700  dest.data_[1] = data_[1] * scalar;
701  dest.data_[2] = data_[2] * scalar;
702  }
703 
704  template <class T>
705  void Vector3<T>::Sub(const T& scalar, Vector3<T>& dest) const {
706  dest.data_[0] = data_[0] - scalar;
707  dest.data_[1] = data_[1] - scalar;
708  dest.data_[2] = data_[2] - scalar;
709  }
710 
711  template <class T>
712  void Vector3<T>::Sub(const Vector3<T>&argvec, Vector3<T>&destvec) const {
713  destvec[0] = data_[0] - argvec[0];
714  destvec[1] = data_[1] - argvec[1];
715  destvec[2] = data_[2] - argvec[2];
716  }
717 
718  template <class T>
719  void Vector3<T>::DivideIP(const T& scalar) {
720  Divide(scalar, *this);
721  }
722 
723  template <class T>
724  void Vector3<T>::Divide(const T& scalar, Vector3<T>& dest) const {
725  if (scalar == 0.0)
726  BEXCEPTION("Division by Zero!");
727  dest.data_[0] = data_[0] / scalar;
728  dest.data_[1] = data_[1] / scalar;
729  dest.data_[2] = data_[2] / scalar;
730  }
731 
732 
733  //////////////////////////////////////////////////////////////////////////
734  // operators
735  // woelk 01 2003
736  /////////////////////////////////////////////////////////////////////////
737 
738  /** @relates Vector3 */
739  template <class T>
740  inline Vector3<T>& operator*=(Vector3<T>& vec, const T& scalar) {
741  vec.MultiplyIP(scalar);
742  return vec;
743  }
744 
745  /** @relates Vector3
746  Addition operator with scalar argument
747  @author Ingo Thomsen, woelk
748  @status tested **/
749  template <class T>
750  inline Vector3<T>& operator+=(Vector3<T>& vec, const T& scalar) {
751  vec.AddIP(scalar);
752  return vec;
753  }
754 
755  /** @relates Vector3
756  add operator for two Vectors
757  @author Ingo Thomsen, woelk
758  @status tested **/
759  template <class T>
760  inline Vector3<T>& operator+=(Vector3<T>& vec, const Vector3<T>& argvec) {
761  vec.AddIP(argvec);
762  return vec;
763  }
764 
765 
766  /** @relates Vector3
767  Substraction operator with scalar argument
768  @author Ingo Thomsen
769  @status tested **/
770  template <class T>
771  inline Vector3<T>& operator-=(Vector3<T>& vec, const T& scalar) {
772  vec.SubIP(scalar);
773  return vec;
774  }
775 
776  /** @relates Vector3
777  sub operator for two Vectors
778  @author Ingo Thomsen, tested **/
779  template <class T>
780  inline Vector3<T>& operator-=(Vector3<T>& vec, const Vector3<T>&argvec) {
781  vec.SubIP(argvec);
782  return vec;
783  }
784 
785 
786  /** @relates Vector3
787  Division operator with scalar argument
788  @author Ingo Thomsen
789  @status tested **/
790  template <class T>
791  inline Vector3<T>& operator/=(Vector3<T>& vec, const T& scalar) {
792  vec.DivideIP(scalar);
793  return vec;
794  }
795 
796  /////////////////////
797  // stream io
798  /////////////////////////
799 
800  /**
801  @relates Vector3
802  @brief Output with streams
803  specialization for numerical display of unsigned char instead of alphabetically.
804  fout format should be consistent with template implementation and Vector<>
805  \todo refactor into parameterized Print class member method
806  @author Jan Woetzel */
807  inline
808  std::ostream &
809  operator<<(std::ostream& os, const BIAS::Vector3<unsigned char>& vec)
810  {
811  if (!IsConsoleStream(os))
812  { // not cout and not cerr, display trailing dim for consistency with Vector<>
813  os << VECTOR3_SIZE << NL;
814  for (int row = 0; row < VECTOR3_SIZE; row++) {
815  os << (unsigned int)vec.GetData()[row] << " " << NL;
816  }
817  } else {
818  // shorter and aligned for console
819  os << "[ ";
820  for (int row = 0; row < VECTOR3_SIZE; row++)
821  os<<std::setw(3)<<(unsigned int)vec.GetData()[row]<<" ";
822  os<<"] ";
823  }
824  return os;
825  }
826 
827  inline
828  std::ostream &
829  operator<<(std::ostream& os, const BIAS::Vector3<char>& vec)
830  {
831  if (!IsConsoleStream(os))
832  { // not cout and not cerr, display trailing dim for consistency with Vector<>
833  os << VECTOR3_SIZE << NL;
834  for (int row = 0; row < VECTOR3_SIZE; row++) {
835  os << (unsigned int)vec.GetData()[row] << " " << NL;
836  }
837  } else {
838  // shorter and aligned for console
839  os << "[ ";
840  for (int row = 0; row < VECTOR3_SIZE; row++)
841  os<<std::setw(3)<<(int)vec.GetData()[row]<<" ";
842  os<<"] ";
843  }
844  return os;
845  }
846 
847 
848  /** @relates Vector3
849  Output with streams. Usese different format for cout,cerr and other ostreasm
850  to be consistenv with Vector<> but have pretty display in console.
851  @author Jan Woetzel, JMF (08/08/2002) **/
852  template <class T>
853  inline
854  std::ostream &
855  operator<<(std::ostream& os, const BIAS::Vector3<T>& vec)
856  {
857  if (!IsConsoleStream(os))
858  { // not cout and not cerr, display trailing dim for consistency with Vector<>
859  os << VECTOR3_SIZE << NL;
860  for (int row = 0; row < VECTOR3_SIZE; row++) {
861  os<<std::setprecision( DecimalsToStore<T>() ); // at least digits10
862  // print out one element per line
863  os<<vec.GetData()[row] << " " << NL;
864  }
865  } else {
866  // cout or cerr
867  os<<"[ ";
868  for (int row = 0; row < VECTOR3_SIZE; row++){
869  os<<std::setprecision(CONSOLE_PRECISION_DIGITS);
870  os<<vec.GetData()[row] << " ";
871  }
872  os<<"] ";
873  }
874  return os;
875  }
876 
877 
878 
879  /**
880  @relates Vector3
881  @brief Input with streams
882  specialization for numerical display of unsigned char instead of alphebetically.
883  fout format should be consistent with template implementation and Vector<>
884  @author Jan Woetzel */
885  inline std::istream &
886  operator>>(std::istream & is, Vector3<unsigned char>&vec)
887  {
888  int N;
889  is >> N;
890  if (N != VECTOR3_SIZE) {
891  BIASERR("can't assign to a Vector3<unsigned char> from a vector with dim " << N );
892  } else {
893  // correct size, sequential read elementwise
894  int tmp(0); //< used for castign between unsignaed char are written int
895  for (int row = 0; row < N; row++)
896  {
897  is >> tmp;
898  BIASASSERT(tmp>=std::numeric_limits<unsigned char>::min() );
899  BIASASSERT(tmp<=std::numeric_limits<unsigned char>::max() );
900  vec.GetData()[row] = (unsigned char)tmp;
901  };
902  };
903  return is;
904  }
905 
906  inline std::istream &
907  operator>>(std::istream & is, Vector3< char>&vec)
908  {
909  int N;
910  is >> N;
911  if (N != VECTOR3_SIZE) {
912  BIASERR("can't assign to a Vector3<unsigned char> from a vector with dim " << N );
913  } else {
914  // correct size, sequential read elementwise
915  int tmp(0); //< used for castign between unsignaed char are written int
916  for (int row = 0; row < N; row++)
917  {
918  is >> tmp;
919  BIASASSERT(tmp>=std::numeric_limits< char>::min() );
920  BIASASSERT(tmp<=std::numeric_limits< char>::max() );
921  vec.GetData()[row] = (char)tmp;
922  };
923  };
924  return is;
925  }
926 
927 
928  /** @relates Vector3
929  @brief Input with streams
930  @author Jan Woetzel (03/01/2002) **/
931  template <class T>
932  inline
933  std::istream &
934  operator>>(std::istream & is, Vector3<T>&vec)
935  {
936  int N;
937  is >> N;
938  if (N != VECTOR3_SIZE) {
939  BIASERR("can't assign to a Vector3<T> from a vector with dim " << N );
940  } else {
941  // correct size, sequential read elementwise
942  for (int row = 0; row < N; row++) {
943  is >> vec.GetData()[row];
944  };
945  };
946  return is;
947  }
948 
949 
950 } // namespace BIAS
951 
952 
953 #endif // _Vector3_hh_
T * GetData()
Definition: Vector3.hh:203
Vector3(const Vector3< T > &vec)
copy constructor
Definition: Vector3.hh:84
void Set(const T *pv)
copy the array of vectorsize beginning at *T to this-&gt;data_
Definition: Vector3.hh:532
const T * GetData() const
get the data pointer the member function itself is const (before {..}) because it doesn&#39;t change the ...
Definition: Vector3.hh:202
double DistLinf(const Vector3< T > &vec) const
Return the L inf distance of 2 vectors.
Definition: Vector3.hh:646
T NormL1() const
Return the L1 norm: |a| + |b| + |c|.
Definition: Vector3.hh:677
T data_[VECTOR3_SIZE]
Definition: Vector3.hh:514
class for column vectors with arbitrary size
void Add(const T &scalar, Vector3< T > &dest) const
Addition with a scalar, storing results in destionation vector.
Definition: Vector3.hh:684
Vector3< T > & operator*=(Vector3< T > &vec, const T &scalar)
Definition: Vector3.hh:740
Vector3(const T &x, const T &y, const T &z)
constructor with element assigment using explicit values
Definition: Vector3.hh:102
void ScalarProduct(const Vector3< T > &argvec, T &result) const
scalar product (=inner product) of two vectors, storing the result in result
Definition: Vector3.hh:603
void SetZero()
set all values to 0
Definition: Vector3.hh:559
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
void Multiply(const T &scalar, Vector3< T > &dest) const
Multiplication with a scalar, storing results in destination vector.
Definition: Vector3.hh:698
T * begin()
Definition: Vector3.hh:137
class BIASMathBase_EXPORT Vector3
Definition: Operators.hh:44
void clear()
stl conform interface
Definition: Vector3.hh:564
std::istream & operator>>(std::istream &is, Vector3< T > &vec)
Input with streams.
Definition: Vector3.hh:934
Vector3< T > & operator+=(Vector3< T > &vec, const T &scalar)
Definition: Vector3.hh:750
double Dist(const Vector3< T > &vec) const
Return the euclidean distance of 2 vectors.
Definition: Vector3.hh:639
Vector3(const T *pv)
assignment with an array of values which is copied into this ones class members
Definition: Vector3.hh:93
void CrossProduct(const Vector3< T > &argvec, Vector3< T > &destvec) const
cross product of two vectors destvec = this x argvec
Definition: Vector3.hh:594
T const * begin() const
Iterator to first component of vector.
Definition: Vector3.hh:134
void AddIP(const T &scalar)
Addition (in place) of an scalar.
Definition: Vector3.hh:310
void ElementwiseProduct(const Vector3< T > &argvec, Vector3< T > &destvec) const
multiply two vectors elementwise and store the result vector to destvec
Definition: Vector3.hh:617
Vector3< T > & operator+=(Vector3< T > &vec, const Vector3< T > &argvec)
Definition: Vector3.hh:760
bool IsZero() const
Definition: Vector3.hh:571
const T & operator[](const int i) const
access an element of the vector with 0-based indizes.
Definition: Vector3.hh:580
is a &#39;fixed size&#39; quadratic matrix of dim.
Definition: Matrix.hh:54
void MultiplyIP(const T &scalar)
Multiplication (in place) of an scalar.
Definition: Vector3.hh:326
class Vector3 contains a Vector of fixed dim.
Definition: Matrix.hh:53
void ElementwiseDivision(const Vector3< T > &argvec, Vector3< T > &destvec) const
Definition: Vector3.hh:625
void Sub(const T &scalar, Vector3< T > &dest) const
Substraction with a scalar, storing results in destination vector.
Definition: Vector3.hh:705
T * GetData() const
get the pointer to the data array of the vector (for faster direct memory access) ...
Definition: Vector.hh:219
matrix class with arbitrary size, indexing is row major.
void SubIP(const T &scalar)
Substraction (in place) of an scalar.
Definition: Vector3.hh:318
Vector3< T > & operator/=(Vector3< T > &vec, const T &scalar)
Definition: Vector3.hh:791
std::istream & operator>>(std::istream &is, Vector3< unsigned char > &vec)
Input with streams specialization for numerical display of unsigned char instead of alphebetically...
Definition: Vector3.hh:886
void Divide(const T &scalar, Vector3< T > &dest) const
Division with a scalar, storing results in destination vector.
Definition: Vector3.hh:724
Vector3< T > & operator-=(Vector3< T > &vec, const Vector3< T > &argvec)
Definition: Vector3.hh:780
void DivideIP(const T &scalar)
Division (in place) of an scalar.
Definition: Vector3.hh:719
T const * end() const
Iterator pointing to one element after the last vector element.
Definition: Vector3.hh:143
Vector3< T > & Normalize()
normalize this vector to length 1
Definition: Vector3.hh:663
Subscript size() const
Definition: vec.h:262
double NormL2() const
the L2 norm sqrt(a^2 + b^2 + c^2)
Definition: Vector3.hh:633
BIASCommon_EXPORT std::istream & operator>>(std::istream &is, BIAS::TimeStamp &ts)
Standard input operator for TimeStamps.
Definition: TimeStamp.cpp:157
Vector3(const T &scalar)
assignment with a constant value for all elements
Definition: Vector3.hh:88
Vector3< T > & operator-=(Vector3< T > &vec, const T &scalar)
Definition: Vector3.hh:771