Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Operators.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 #ifndef __Operators_hh__
26 #define __Operators_hh__
27 
28 #include <Base/Math/Matrix.hh>
29 
30 
31 #include <Base/Debug/Error.hh>
32 #include <Base/Math/tnt/cmat.h>
33 
34 namespace BIAS {
35 
36  // forward declarations to avoid nested includes
37  template<class T> class BIASMathBase_EXPORT Matrix;
38  template<class T> class BIASMathBase_EXPORT Vector;
39  template<class T> class BIASMathBase_EXPORT Matrix2x2;
40  template<class T> class BIASMathBase_EXPORT Vector2;
41  template<class T> class BIASMathBase_EXPORT Matrix2x3;
42  template<class T> class BIASMathBase_EXPORT Matrix3x2;
43  template<class T> class BIASMathBase_EXPORT Matrix3x3;
44  template<class T> class BIASMathBase_EXPORT Vector3;
45  template<class T> class BIASMathBase_EXPORT Matrix4x4;
46  template<class T> class BIASMathBase_EXPORT Vector4;
47  template<class T> class BIASMathBase_EXPORT Matrix3x4;
48 
49 
50  ////////////////////////////////////////////////////////////////////////////
51  // unary vector/matrix operators
52  ////////////////////////////////////////////////////////////////////////////
53 
54  /** @relates Vector2
55  @author herzog 2005-07-19 */
56  template <class T> inline Vector2<T> operator-(const Vector2<T>& v)
57  { return Vector2<T>( -v[0],-v[1] ); }
58 
59  /** @relates Vector3
60  @author herzog 2005-07-19 */
61  template <class T> inline Vector3<T> operator-(const Vector3<T>& v)
62  { return Vector3<T>( -v[0],-v[1],-v[2] ); }
63 
64  /** @relates Vector4
65  @author herzog 2005-07-19 */
66  template <class T> inline Vector4<T> operator-(const Vector4<T>& v)
67  { return Vector4<T>( -v[0],-v[1],-v[2],-v[3] ); }
68 
69  ////////////////////////////////////////////////////////////////////////////
70  // vector - scalar operators
71  ////////////////////////////////////////////////////////////////////////////
72 
73  /** @relates Vector */
74  template <class T>
75  inline Vector<T> operator*(const Vector<T>& v, const T& scalar)
76  {
77  Vector<T> res(v.size());
78  v.Multiply(scalar, res);
79  return res;
80  }
81 
82  /** @relates Vector */
83  template <class T>
84  inline Vector<T> operator*(const T& scalar, const Vector<T>& v)
85  {
86  Vector<T> res(v.size());
87  v.Multiply(scalar, res);
88  return res;
89  }
90 
91  /** Addition operator with scalar argument, returning new vector
92  @author Ingo Thomsen, tested **/
93  template <class T>
94  inline Vector2<T> operator+(const Vector2<T>& vec, const T& scalar)
95  {
96  Vector2<T> result;
97  vec.Add(scalar, result);
98  return result;
99  }
100 
101  /** Addition operator with scalar argument, returning new vector
102  @author Ingo Thomsen, djung, copy-paste **/
103  template <class T>
104  inline Vector2<T> operator+(const T& scalar, const Vector2<T>& vec)
105  {
106  Vector2<T> result;
107  vec.Add(scalar, result);
108  return result;
109  }
110 
111  /** @relates Vector2
112  Substraction operator with scalar argument, returning new vector
113  @author Ingo Thomsen, tested **/
114  template <class T>
115  inline Vector2<T> operator-(const Vector2<T>& vec, const T& scalar)
116  {
117  Vector2<T> result;
118  vec.Sub(scalar, result);
119  return result;
120  }
121 
122  /** @relates Vector2
123  Substraction operator with scalar argument, returning new vector
124  @author Ingo Thomsen, djung, copy-paste **/
125  template <class T>
126  inline Vector2<T> operator-(const T& scalar, const Vector2<T>& vec)
127  {
128  Vector2<T> result;
129  vec.Sub(scalar, result);
130  return result;
131  }
132 
133  /** @relates Vector2
134  Multiplication operator with scalar argument, returning new vector
135  @author Ingo Thomsen, tested **/
136  template <class T>
137  inline Vector2<T> operator*(const Vector2<T>& vec, const T& scalar)
138  {
139  Vector2<T> result;
140  vec.Multiply(scalar, result);
141  return result;
142  }
143 
144  /** @relates Vector2
145  Multiplication operator with scalar argument, returning new vector
146  @author Jung, copied for convenience **/
147  template <class T>
148  inline Vector2<T> operator*(const T& scalar, const Vector2<T>& vec)
149  {
150  Vector2<T> result;
151  vec.Multiply(scalar, result);
152  return result;
153  }
154 
155  /** @relates Vector2
156  Division operator with scalar argument, returning new vector
157  @author Ingo Thomsen, tested **/
158  template <class T>
159  inline Vector2<T> operator/(const Vector2<T>& vec, const T& scalar)
160  {
161  BIASASSERT(scalar !=(T)0);
162  Vector2<T> result;
163  vec.Divide(scalar, result);
164  return result;
165  }
166 
167  /** @relates Vector3 */
168  template <class T>
169  inline Vector3<T> operator*(const Vector3<T>& vec, const T& scalar)
170  {
171  Vector3<T> res;
172  vec.Multiply(scalar, res);
173  return res;
174  }
175 
176  /** @relates Vector3 */
177  template <class T>
178  inline Vector3<T> operator*(const T& scalar, const Vector3<T>& vec)
179  {
180  Vector3<T> res;
181  vec.Multiply(scalar, res);
182  return res;
183  }
184 
185  /** @relates Vector3
186  Addition operator with scalar argument, returning new vector
187  @author Ingo Thomsen, woelk, tested **/
188  template <class T>
189  inline Vector3<T> operator+(const Vector3<T>& vec, const T& scalar)
190  {
191  Vector3<T> result;
192  vec.Add(scalar, result);
193  return result;
194  }
195 
196  /** @relates Vector3
197  Substraction operator with scalar argument, returning new vector
198  @author Ingo Thomsen, woelk, tested **/
199  template <class T>
200  inline Vector3<T> operator-(const Vector3<T>& vec, const T& scalar)
201  {
202  return vec.Sub(scalar);
203  }
204 
205  /** @relates Vector3
206  Substraction operator with scalar argument, returning new vector
207  @author woelk, tested **/
208  template <class T>
209  inline Vector3<T> operator-(const T& scalar, const Vector3<T>& vec)
210  {
211  Vector3<T> dest;
212  vec.Mult(-1, dest);
213  dest+=scalar;
214  return dest;
215  }
216 
217  /** @relates Vector3
218  Division operator with scalar argument, returning new vector
219  @author Ingo Thomsen, tested **/
220  template <class T>
221  inline Vector3<T> operator/(const Vector3<T>& vec, const T& scalar)
222  {
223  Vector3<T> result;
224  vec.Divide(scalar, result);
225  return result;
226  }
227 
228  /** @relates Vector4
229  Multiplication operator with scalar argument, returning new vector
230  @author Ingo Thomsen, tested **/
231  template <class T>
232  inline Vector4<T> operator*(const Vector4<T>& v, const T& scalar)
233  {
234  Vector4<T> result;
235  v.Multiply(scalar, result);
236  return result;
237  }
238 
239  /** @relates Vector4
240  Multiplication operator with scalar argument, returning new vector
241  @author Ingo Thomsen, tested **/
242  template <class T>
243  inline Vector4<T> operator*(const T& scalar, const Vector4<T>& v)
244  {
245  Vector4<T> result;
246  v.Multiply(scalar, result);
247  return result;
248  }
249 
250  /** @relates Vector4
251  Division operator with scalar argument, returning new vector
252  @author Ingo Thomsen, tested **/
253  template <class T>
254  inline Vector4<T> operator/(const Vector4<T>& v, const T& scalar)
255  {
256 #ifdef BIAS_DEBUG
257  if (scalar == 0 ) {
258  BIASERR("Division by Zero!");
259  return Vector4<T>((T)0,(T)0,(T)0,(T)0);
260  }
261 #endif
262  Vector4<T> result;
263  v.Divide(scalar, result);
264  return result;
265  }
266 
267  /** @relates Vector4
268  Addition operator with scalar argument, returning new vector
269  @author Ingo Thomsen, tested **/
270  template <class T>
271  inline Vector4<T> operator+(const Vector4<T>& v, const T& scalar)
272  {
273  Vector4<T> result;
274  v.Add(scalar, result);
275  return result;
276  }
277 
278  /** @relates Vector4
279  Subtraction operator with scalar argument, returning new vector
280  @author Ingo Thomsen, tested **/
281  template <class T>
282  inline Vector4<T> operator-(const Vector4<T>& v, const T& scalar)
283  {
284  Vector4<T> result;
285  v.Sub(scalar, result);
286  return result;
287  }
288 
289  ////////////////////////////////////////////////////////////////////////////
290  // vector - vector operators
291  ////////////////////////////////////////////////////////////////////////////
292 
293  /** @relates Vector */
294  template <class T>
295  inline T operator*(const Vector<T>& v, const Vector<T> &argvec)
296  {
297  T destscalar;
298  v.ScalarProduct(argvec, destscalar);
299  return destscalar;
300  }
301 
302  // /** @relates Vector2
303  // @author Dennis Herzog, 2005-07-27 **/
304  // template <class T>
305  // inline bool operator == (const Vector2<T>& a, const Vector2<T>& b)
306  // { return a[0]==b[0] && a[1]==b[1]; }
307 
308  // template <class T>
309  // inline bool operator != (const Vector2<T>& a, const Vector2<T>& b)
310  // { return !(a==b); }
311 
312  /** @relates Vector2
313  @author Dennis Herzog, 2005-07-27 **/
314  template <class T>
315  inline bool operator<= (const Vector2<T>& a, const Vector2<T>& b)
316  { return a[0]<=b[0] && a[1]<=b[1]; }
317 
318  /** @relates Vector2
319  @author Dennis Herzog, 2005-07-27 **/
320  template <class T>
321  inline bool operator< (const Vector2<T>& a, const Vector2<T>& b)
322  { return a[0]<b[0] && a[1]<b[1]; }
323 
324  /** @relates Vector2
325  @author Dennis Herzog, 2005-07-27 **/
326  template <class T>
327  inline bool operator>= (const Vector2<T>& a, const Vector2<T>& b)
328  { return b<=a; }
329 
330  /** @relates Vector2
331  @author Dennis Herzog, 2005-07-27 **/
332  template <class T>
333  inline bool operator> (const Vector2<T>& a, const Vector2<T>& b)
334  { return b<a; }
335 
336  /** @relates Vector2
337  add operator for two Vectors, returning new vector
338  @author Ingo Thomsen, tested **/
339  template <class T>
340  inline Vector2<T> operator+(const Vector2<T> &vec, const Vector2<T> &argvec)
341  {
342  Vector2<T> destvec;
343  vec.Add(argvec, destvec);
344  return destvec;
345  }
346 
347  /** @relates Vector2
348  sub operator for two Vectors, returning new vector
349  @author Ingo Thomsen, tested **/
350  template <class T>
351  inline Vector2<T> operator-(const Vector2<T> &vec, const Vector2<T> &argvec)
352  {
353  Vector2<T> destvec;
354  vec.Sub(argvec, destvec);
355  return destvec;
356  }
357 
358  /** @relates Vector2 */
359  template <class T>
360  inline T operator*(const Vector2<T> &vec, const Vector2<T> &argvec)
361  {
362  return vec[0]*argvec[0]+vec[1]*argvec[1];
363  }
364 
365  // /** @relates Vector3
366  // @author Dennis Herzog, 2005-07-27 **/
367  // template <class T>
368  // inline bool operator == (const Vector3<T>& a, const Vector3<T>& b)
369  // { return a[0]==b[0] && a[1]==b[1] && a[2]==b[2]; }
370 
371  // /** @relates Vector3
372  // @author Dennis Herzog, 2005-07-27 **/
373  // template <class T>
374  // inline bool operator != (const Vector3<T>& a, const Vector3<T>& b)
375  // { return !(a==b); }
376 
377  /** @relates Vector3
378  @author Dennis Herzog, 2005-07-27 **/
379  template <class T>
380  inline bool operator<= (const Vector3<T>& a, const Vector3<T>& b)
381  { return a[0]<=b[0] && a[1]<=b[1] && a[2]<=b[2]; }
382 
383  /** @relates Vector3
384  @author Dennis Herzog, 2005-07-27 **/
385  template <class T>
386  inline bool operator< (const Vector3<T>& a, const Vector3<T>& b)
387  { return a[0]<b[0] && a[1]<b[1] && a[2]<b[2]; }
388 
389  /** @relates Vector3
390  @author Dennis Herzog, 2005-07-27 **/
391  template <class T>
392  inline bool operator>= (const Vector3<T>& a, const Vector3<T>& b)
393  { return b<=a; }
394 
395  /** @relates Vector3
396  @author Dennis Herzog, 2005-07-27 **/
397  template <class T>
398  inline bool operator> (const Vector3<T>& a, const Vector3<T>& b)
399  { return b<a; }
400 
401  /** @relates Vector3 */
402  template <class T>
403  inline T operator*(const Vector3<T>& vec, const Vector3<T>& arg)
404  { T res; vec.ScalarProduct(arg, res); return res; }
405 
406  /** @relates Vector3 */
407  template <class T>
408  inline Vector3<T> operator+(const T& scalar, const Vector3<T>& vec)
409  {
410  Vector3<T> result;
411  vec.Add(scalar, result);
412  return result;
413  }
414 
415  /** @relates Vector3
416  add operator for two Vectors, returning new vector
417  @author Ingo Thomsen, woelk, tested **/
418  template <class T>
419  inline Vector3<T> operator+(const Vector3<T>& vec, const Vector3<T>& argvec)
420  {
421  Vector3<T> destvec;
422  vec.Add(argvec, destvec);
423  return destvec;
424  }
425 
426  /** @relates Vector3
427  add operator for Vector3, and Vector, so u dont have to cast...
428  @author D. Grest, tested **/
429  template <class T>
430  inline Vector3<T> operator+(const Vector3<T>& vec, const Vector<T>& argvec)
431  {
432  Vector3<T> destvec;
433 #ifdef BIAS_DEBUG
434  if (argvec.size()!=3) {
435  BIASERR("operator+: argument does not havesize 3");
436  }
437 #endif
438  destvec[0]=vec[0]+argvec[0];
439  destvec[1]=vec[1]+argvec[1];
440  destvec[2]=vec[2]+argvec[2];
441  return destvec;
442  }
443 
444  /** @relates Vector3
445  sub operator for two Vectors, returning new vector
446  @author Ingo Thomsen, tested **/
447  template <class T>
448  inline Vector3<T> operator-(const Vector3<T>& vec, const Vector3<T>& argvec)
449  {
450  Vector3<T> destvec;
451  vec.Sub(argvec, destvec);
452  return destvec;
453  }
454 
455  // /** @relates Vector4
456  // @author Dennis Herzog, 2005-07-27 **/
457  // template <class T>
458  // inline bool operator == (const Vector4<T>& a, const Vector4<T>& b)
459  // { return a[0]==b[0] && a[1]==b[1] && a[2]==b[2] && a[3]==b[3]; }
460 
461  // /** @relates Vector4
462  // @author Dennis Herzog, 2005-07-27 **/
463  // template <class T>
464  // inline bool operator != (const Vector4<T>& a, const Vector4<T>& b)
465  // { return !(a==b); }
466 
467  /** @relates Vector4
468  @author Dennis Herzog, 2005-07-27 **/
469  template <class T>
470  inline bool operator<= (const Vector4<T>& a, const Vector4<T>& b)
471  { return a[0]<=b[0] && a[1]<=b[1] && a[2]<=b[2] && a[3]<=b[3]; }
472 
473  /** @relates Vector4
474  @author Dennis Herzog, 2005-07-27 **/
475  template <class T>
476  inline bool operator< (const Vector4<T>& a, const Vector4<T>& b)
477  { return a[0]<b[0] && a[1]<b[1] && a[2]<b[2] && a[3]<b[3]; }
478 
479  /** @relates Vector4
480  @author Dennis Herzog, 2005-07-27 **/
481  template <class T>
482  inline bool operator>= (const Vector4<T>& a, const Vector4<T>& b)
483  { return b<=a; }
484 
485  /** @relates Vector4
486  @author Dennis Herzog, 2005-07-27 **/
487  template <class T>
488  inline bool operator> (const Vector4<T>& a, const Vector4<T>& b)
489  { return b<a; }
490 
491  /** @relates Vector4
492  op* to multiply two vectors by their dotproduct
493  @author Jan Woetzel, untested (03/06/2002)**/
494  template <class T>
495  inline T operator*(const Vector4<T>& v, const Vector4<T>& argvec)
496  {
497  T destscalar;
498  v.ScalarProduct(argvec, destscalar);
499  return destscalar;
500  }
501 
502  /** @relates Vector4
503  sub operator for two Vectors, returning new vector
504  @author Ingo Thomsen, tested **/
505  template <class T>
506  inline Vector4<T> operator-(const Vector4<T>& v, const Vector4<T> &argvec)
507  {
508  Vector4<T> destvec;
509  v.Sub(argvec, destvec);
510  return destvec;
511  }
512 
513  /** @relates Vector4
514  add operator for two Vectors, returning new vector
515  @author Ingo Thomsen, tested **/
516  template <class T>
517  inline Vector4<T> operator+(const Vector4<T>& v, const Vector4<T> &argvec)
518  {
519  Vector4<T> destvec;
520  v.Add(argvec, destvec);
521  return destvec;
522  }
523 
524 
525 
526  ////////////////////////////////////////////////////////////////////////////
527  // matrix - scalar operators
528  ////////////////////////////////////////////////////////////////////////////
529 
530  /** @relates Matrix
531  addition operator, returning new matrix
532  @author Ingo Thomsen, woelk **/
533  template<class T>
534  inline Matrix<T> operator+(const Matrix<T>& mat, const T& scalar)
535  { Matrix<T> res; mat.Add(scalar, res); return res; }
536 
537  /** @relates Matrix */
538  template<class T>
539  inline Matrix<T> operator+(const T& scalar, const Matrix<T>& mat)
540  { Matrix<T> res; mat.Add(scalar, res); return res; }
541 
542  /** @relates Matrix
543  substraction operator, returning new matrix
544  @author Ingo Thomsen **/
545  template<class T>
546  inline Matrix<T> operator-(const Matrix<T>& mat, const T& scalar)
547  { Matrix<T> res; mat.Sub(scalar, res); return res; }
548 
549  /** @relates Matrix */
550  template<class T>
551  inline Matrix<T> operator-(const T& scalar, const Matrix<T>& mat)
552  { Matrix<T> res; mat.Sub(scalar, res); return res; }
553 
554  /** @relates Matrix2x3
555  multiplication operator, returning new matrix
556  @author apetersen**/
557  template<class T>
558  inline Matrix2x3<T> operator*(const Matrix2x3<T>& mat, const T& scalar)
559  {
560  Matrix2x3<T> res;
561  T* resP = res.GetData(); T* matP = mat.GetData();
562  for (size_t i=0; i<6; i++) resP[i] = scalar*matP[i];
563  return res;
564  }
565  /** @relates Matrix2x3
566  multiplication operator, returning new matrix
567  @author apetersen**/
568  template<class T>
569  inline Matrix2x3<T> operator*(const T& scalar, const Matrix2x3<T>& mat)
570  {
571  Matrix2x3<T> res;
572  T* resP = res.GetData(); const T* matP = mat.GetData();
573  for (size_t i=0; i<6; i++) resP[i] = scalar*matP[i];
574  return res;
575  }
576 
577  /** @relates Matrix3x2
578  multiplication operator, returning new matrix
579  @author apetersen**/
580  template<class T>
581  inline Matrix3x2<T> operator*(const Matrix3x2<T>& mat, const T& scalar)
582  {
583  Matrix2x3<T> res;
584  T* resP = res.GetData(); const T* matP = mat.GetData();
585  for (size_t i=0; i<6; i++) resP[i] = scalar*matP[i];
586  return res;
587  }
588  /** @relates Matrix3x2
589  multiplication operator, returning new matrix
590  @author apetersen**/
591  template<class T>
592  inline Matrix3x2<T> operator*(const T& scalar, const Matrix3x2<T>& mat)
593  {
594  Matrix2x3<T> res;
595  T* resP = res.GetData(); T* matP = mat.GetData();
596  for (size_t i=0; i<6; i++) resP[i] = scalar*matP[i];
597  return res;
598  }
599 
600  /** @relates Matrix
601  multiplication operator, returning new matrix
602  @author Ingo Thomsen**/
603  template<class T>
604  inline Matrix<T> operator*(const Matrix<T>& mat, const T& scalar)
605  { Matrix<T> res; mat.Multiply(scalar, res); return res; }
606 
607  /** @relates Matrix */
608  template<class T>
609  inline Matrix<T> operator*(const T& scalar, const Matrix<T>& mat)
610  { Matrix<T> res; mat.Multiply(scalar, res); return res; }
611 
612  /** @relates Matrix
613  division operator, returning new matrix
614  @author Ingo Thomsen **/
615  template<class T>
616  inline Matrix<T> operator/(const Matrix<T>& mat, const T& scalar)
617  { Matrix<T> res; mat.Divide(scalar, res); return res; }
618 
619  /** @relates Matrix
620  division operator
621  @author Ingo Thomsen **/
622  template<class T>
623  inline Matrix<T>& operator/=(Matrix<T>& mat, const T& scalar)
624  { mat.DivideIP(scalar); return mat; }
625 
626 
627 
628  ////////////////////////////////////////////////////////////////////////////
629  // vector - matrix operators
630  ////////////////////////////////////////////////////////////////////////////
631 
632  /** @relates Matrix3x3
633  @relates Vector3
634  matrix-vector multiplication using 'Mult'
635  @author Ingo Thomsen, Jan Woetzel,
636  fw moved out of class, untested (04/17/2002)**/
637  template <class T>
638  inline Vector3<T> operator*(const Matrix3x3<T>& mat,
639  const Vector3<T> &argvec)
640  {
641  Vector3<T> destvec;
642  mat.Mult(argvec, destvec);
643  return destvec;
644  }
645 
646  /** @relates Matrix4x4
647  @relates Vector4
648  matrix-vector multiplication using 'Mult'
649  @author Jan Woetzel, untested (04/17/2002) */
650  template <class T>
651  inline Vector4<T> operator*(const Matrix4x4<T>& mat,
652  const Vector4<T> &argvec)
653  {
654  Vector4<T> destvec;
655  mat.Mult(argvec, destvec);
656  return destvec;
657  }
658 
659  /** @relates Matrix
660  @relates Vector */
661  template <class T>
662  inline Vector<T> operator*(const Matrix<T>& mat, const Vector2<T>& vec)
663  {
664  BIASASSERT(mat.num_cols() == 2 );
665  Vector<T> res(mat.num_rows());
666  for (register int i=0; i<mat.num_rows(); i++)
667  res[i]=mat[i][0]*vec[0]+mat[i][1]*vec[1];
668  return res;
669  }
670 
671  /** @relates Matrix2x2
672  @relates Vector2 */
673  template <class T>
674  inline Vector2<T> operator*(const Matrix2x2<T>& mat, const Vector2<T>& vec)
675  {
676  Vector2<T> res;
677  res[0]=mat[0][0]*vec[0]+mat[0][1]*vec[1];
678  res[1]=mat[1][0]*vec[0]+mat[1][1]*vec[1];
679  return res;
680  }
681 
682  /** @relates Matrix2x3
683  @relates Vector3 */
684  template <class T>
685  inline Vector2<T> operator*(const Matrix2x3<T>& mat, const Vector3<T>& vec)
686  {
687  Vector2<T> res;
688  res[0]= mat[0][0]*vec[0] + mat[0][1]*vec[1] + mat[0][2]*vec[2];
689  res[1]= mat[1][0]*vec[0] + mat[1][1]*vec[1] + mat[1][2]*vec[2];
690  return res;
691  }
692 
693  /** @relates Matrix3x2
694  @relates Vector2 */
695  template <class T>
696  inline Vector3<T> operator*(const Matrix3x2<T>& mat, const Vector2<T>& vec)
697  {
698  Vector2<T> res;
699  res[0]= mat[0][0]*vec[0] + mat[0][1]*vec[1];
700  res[1]= mat[1][0]*vec[0] + mat[1][1]*vec[1];
701  res[2]= mat[2][0]*vec[0] + mat[2][1]*vec[1];
702  return res;
703  }
704 
705  /** @relates Matrix
706  @relates Vector3 */
707  template <class T>
708  inline Vector<T>
709  operator*(const BIAS::Matrix<T> &mat, const Vector3<T> &vec)
710  {
711  BIASASSERT(mat.num_cols() == 3);
712  Vector<T> result(mat.num_rows());
713  result.Set((T)0);
714  register int y, i;
715  for (y=0; y<(int)result.Size(); y++)
716  for (i=0; i<3; i++)
717  result[y]+=mat[y][i]*vec[i];
718  return result;
719  }
720 
721  /** @relates Matrix3x4
722  @relates Vector4 */
723  template <class T> inline Vector3<T>
724  operator*(const Matrix3x4<T>& mat, const Vector4<T> &argvec)
725  {
726  Vector3<T> destvec;
727  mat.Mult(argvec, destvec);
728  return destvec;
729  }
730 
731  /** @relates Matrix
732  @relates Vector4 */
733  template <class T> inline Vector<T>
734  operator*(const BIAS::Matrix<T> &mat, const Vector4<T> &vec)
735  {
736  BIASASSERT(mat.num_cols() == 4);
737 
738  Vector<T> result(mat.num_rows());
739  result.Set((T)0);
740  register int y, i;
741  for (y=0; y<result.size(); y++)
742  for (i=0; i<4; i++)
743  result[y]+=mat[y][i]*vec[i];
744  return result;
745  }
746 
747  ////////////////////////////////////////////////////////////////////////////
748  // matrix - matrix operators
749  ////////////////////////////////////////////////////////////////////////////
750 
751  /** @relates Matrix
752  @brief zero padded convolution of two matrices
753  @author woelk 01/2008 (c) www.vision-n.de */
754  template <class T> inline void
755  Conv(const Matrix<T>& left, const Matrix<T> &right ,Matrix<T> &res)
756  {
757  const int lc=left.num_cols(), rc=right.num_cols();
758  const int lr=left.num_rows(), rr=right.num_rows();
759  const int res_x = lc+rc-1, res_y = lr+rr-1;
760  res.newsize(res_y, res_x);
761  double sum;
762  for (int y=0; y<res_y; y++){
763  const int miny = (y<rr)?(0):(y-rr+1);
764  const int maxy = (y<lr)?(y+1):(lr);
765  //std::cout << "y="<<y<<"\tsum ["<<miny<<", "<<maxy<<"]\n";
766  for (int x=0; x<res_x; x++){
767  const int minx = (x<rc)?(0):(x-rc+1);
768  const int maxx = (x<lc)?(x+1):(lc);
769  //std::cout << "x="<<x<<"\tsum ["<<minx<<", "<<maxx<<"]\n";
770  sum = 0.0;
771  for (int r=miny; r<maxy; r++){
772  for (int c=minx; c<maxx; c++){
773  BIASASSERT(r>=0 && r<lr);
774  BIASASSERT(c>=0 && c<lc);
775  BIASASSERT(y-r>=0 && y-r<rr);
776  BIASASSERT(x-c>=0 && x-c<rc);
777  sum += left[r][c] * right[y-r][x-c];
778  }
779  }
780  res[y][x] = (T) sum;
781  }
782  }
783  }
784 
785  /** @relates Matrix2x2
786  untested */
787  template <class T> inline Matrix2x2<T>
788  operator*(const Matrix2x2<T>& mat, const Matrix2x2<T> & argMat)
789  {
790  Matrix2x2<T> destmat;
791  register T *destP = destmat.GetData();
792  const register T *argP = argMat.GetData();
793  const register T *matP = mat.GetData();
794 
795  destP[0] =
796  matP[0] * argP[0] + matP[1] * argP[2];
797  destP[1] =
798  matP[0] * argP[1] + matP[1] * argP[3];
799 
800  destP[2] =
801  matP[2] * argP[0] + matP[3] * argP[2];
802  destP[3] =
803  matP[2] * argP[1] + matP[3] * argP[3];
804  return destmat;
805  }
806 
807  /** @relates Matrix2x3
808  @relates Matrix3x3
809  untested */
810  template <class T> inline Matrix2x3<T>
811  operator*(const Matrix2x3<T>& mat, const Matrix3x3<T> & argmat)
812  {
813  Matrix2x3<T> destmat;
814  register T *destP = destmat.GetData();
815  const register T *argP = argmat.GetData();
816  const register T *matP = mat.GetData();
817 
818  destP[0] = matP[0] * argP[0] + matP[1] * argP[3] + matP[2] * argP[6];
819  destP[1] = matP[0] * argP[1] + matP[1] * argP[4] + matP[2] * argP[7];
820  destP[2] = matP[0] * argP[2] + matP[1] * argP[5] + matP[2] * argP[8];
821  destP[3] = matP[3] * argP[0] + matP[4] * argP[3] + matP[5] * argP[6];
822  destP[4] = matP[3] * argP[1] + matP[4] * argP[4] + matP[5] * argP[7];
823  destP[5] = matP[3] * argP[2] + matP[4] * argP[5] + matP[5] * argP[8];
824 
825  return destmat;
826  }
827 
828  /** @relates Matrix3x2
829  @relates Matrix2x2
830  untested */
831  template <class T> inline Matrix3x2<T>
832  operator*(const Matrix3x2<T>& mat, const Matrix2x2<T> & argmat)
833  {
834  Matrix3x2<T> destmat;
835  register T *destP = destmat.GetData();
836  const register T *argP = argmat.GetData();
837  const register T *matP = mat.GetData();
838 
839  destP[0] = matP[0] * argP[0] + matP[1] * argP[2];
840  destP[1] = matP[0] * argP[1] + matP[1] * argP[3];
841  destP[2] = matP[0] * argP[2] + matP[1] * argP[4];
842  destP[3] = matP[2] * argP[0] + matP[3] * argP[2];
843  destP[4] = matP[2] * argP[1] + matP[3] * argP[3];
844  destP[5] = matP[2] * argP[2] + matP[3] * argP[4];
845 
846  return destmat;
847  }
848 
849  /** @relates Matrix3x3
850  matrix-matrix multiplication using 'Mult'
851  @author Jan Woetzel, tested (04/17/2002) */
852  template <class T>
853  inline Matrix3x3<T> operator*(const Matrix3x3<T> & argL,
854  const Matrix3x3<T> & argR)
855  {
856  Matrix3x3<T> resMat;
857  argL.Mult(argR, resMat);
858  return resMat;
859  }
860 
861  /** @relates Matrix3x3
862  @author woelk (c) www.vision-n.de */
863  template <class T>
864  Matrix3x3<T> operator+(const Matrix3x3<T>& left, const Matrix3x3<T>& right)
865  { Matrix3x3<T> res(left); res+=right; return res; }
866 
867  /** @relates Matrix3x3
868  @author woelk (c) www.vision-n.de */
869  template <class T>
870  Matrix3x3<T> operator-(const Matrix3x3<T>& left, const Matrix3x3<T>& right)
871  { Matrix3x3<T> res(left); res-=right; return res; }
872 
873  /** @relates Matrix3x3
874  @relates Matrix
875  matrix-matrix multiplication using 'Mult'
876  @author Jan Woetzel, tested (04/17/2002) */
877  template <class T>
878  inline Matrix<T> operator*(const Matrix3x3<T> & argLeft,
879  const Matrix<T> & argMat)
880  {
881  return TNT::matmult((Matrix<T>)argLeft, argMat);
882  }
883 
884  /** @relates Matrix4x4
885  matrix-matrix multiplication using 'Mult'
886  @author Jan Woetzel, status untested (04/17/2002) */
887  template <class T>
889  const Matrix4x4<T> & argMat)
890  {
891  Matrix4x4<T> resMat;
892  mat.Mult(argMat, resMat);
893  return resMat;
894  }
895 
896  /** @relates Matrix3x4 */
897  template <class T> inline Matrix3x4<T>
898  operator*(const Matrix3x3<T>& mat, const Matrix3x4<T> &arg)
899  {
900  Matrix3x4<T> dest;
901  mat.Mult(arg, dest);
902  return dest;
903  }
904 
905 
906 } // namespace -------------------
907 
908 
909 
910 
911 #endif // __Operators_hh__
T ScalarProduct(const Vector< T > &argvec) const
scalar product (inner product) of two vectors returning a scalr
Definition: Vector.hh:355
Vector4< T > operator-(const Vector4< T > &v)
Definition: Operators.hh:66
Matrix< T > operator+(const Matrix< T > &mat, const T &scalar)
Definition: Operators.hh:534
void Add(const T &scalar, Vector2< T > &dest) const
Addition with a scalar, storing results in destionation vector.
Definition: Vector2.hh:516
Vector2< T > operator+(const Vector2< T > &vec, const Vector2< T > &argvec)
Definition: Operators.hh:340
void Sub(const T &scalar, Vector4< T > &dest) const
Substraction with a scalar, storing results in destionation vector.
Definition: Vector4.hh:591
Vector< T > operator*(const Matrix< T > &mat, const Vector2< T > &vec)
Definition: Operators.hh:662
Matrix2x3< T > operator*(const Matrix2x3< T > &mat, const T &scalar)
Definition: Operators.hh:558
void Multiply(const T &scalar, Vector4< T > &dest) const
Multiplication with a scalar, storing results in destionation vector.
Definition: Vector4.hh:615
void Add(const T &scalar, Vector4< T > &dest) const
Addition with a scalar, storing results in destionation vector.
Definition: Vector4.hh:567
T operator*(const Vector4< T > &v, const Vector4< T > &argvec)
Definition: Operators.hh:495
Matrix2x3< T > operator*(const Matrix2x3< T > &mat, const Matrix3x3< T > &argmat)
Definition: Operators.hh:811
Matrix3x3< T > operator-(const Matrix3x3< T > &left, const Matrix3x3< T > &right)
Definition: Operators.hh:870
Subscript num_cols() const
Definition: cmat.h:320
class for column vectors with arbitrary size
is a &#39;fixed size&#39; quadratic matrix of dim.
Definition: Matrix2x2.hh:48
is a &#39;fixed size&#39;
Definition: Matrix3x2.hh:46
void Add(const T &scalar, Vector3< T > &dest) const
Addition with a scalar, storing results in destionation vector.
Definition: Vector3.hh:684
void Sub(const T &scalar, Vector2< T > &dest) const
Substraction with a scalar, storing results in destionation vector.
Definition: Vector2.hh:529
Matrix< T > operator*(const T &scalar, const Matrix< T > &mat)
Definition: Operators.hh:609
Vector4< T > operator/(const Vector4< T > &v, const T &scalar)
Definition: Operators.hh:254
Matrix3x4< T > operator*(const Matrix3x3< T > &mat, const Matrix3x4< T > &arg)
Definition: Operators.hh:898
Vector2< T > operator-(const Vector2< T > &v)
Definition: Operators.hh:56
Vector3< T > operator*(const Vector3< T > &vec, const T &scalar)
Definition: Operators.hh:169
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
Vector4< T > operator-(const Vector4< T > &v, const T &scalar)
Definition: Operators.hh:282
Matrix3x3< T > operator*(const Matrix3x3< T > &argL, const Matrix3x3< T > &argR)
Definition: Operators.hh:853
T operator*(const Vector2< T > &vec, const Vector2< T > &argvec)
Definition: Operators.hh:360
Vector3< T > operator*(const Matrix3x4< T > &mat, const Vector4< T > &argvec)
Definition: Operators.hh:724
Vector2< T > operator/(const Vector2< T > &vec, const T &scalar)
Definition: Operators.hh:159
Matrix< T > & newsize(Subscript M, Subscript N)
Definition: cmat.h:269
Matrix< T > operator*(const Matrix3x3< T > &argLeft, const Matrix< T > &argMat)
Definition: Operators.hh:878
void Multiply(const T &scalar, Vector3< T > &dest) const
Multiplication with a scalar, storing results in destination vector.
Definition: Vector3.hh:698
Matrix< T > operator+(const T &scalar, const Matrix< T > &mat)
Definition: Operators.hh:539
Vector3< T > operator-(const Vector3< T > &v)
Definition: Operators.hh:61
void Multiply(const T &scalar, Vector2< T > &dest) const
Multiplication with a scalar, storing results in destionation vector.
Definition: Vector2.hh:542
Vector3< T > operator*(const T &scalar, const Vector3< T > &vec)
Definition: Operators.hh:178
Vector3< T > operator+(const Vector3< T > &vec, const Vector< T > &argvec)
Definition: Operators.hh:430
Vector2< T > operator*(const Matrix2x2< T > &mat, const Vector2< T > &vec)
Definition: Operators.hh:674
Matrix4x4< T > operator*(const Matrix4x4< T > &mat, const Matrix4x4< T > &argMat)
Definition: Operators.hh:888
class Vector2 contains a Vector of dim.
Definition: Vector2.hh:79
class Vector4 contains a Vector of dim.
Definition: Vector4.hh:65
Vector3< T > operator+(const Vector3< T > &vec, const T &scalar)
Definition: Operators.hh:189
Matrix< T > matmult(const Matrix< T > &A, const Matrix< T > &B)
Definition: cmat.h:811
Vector< T > operator*(const T &scalar, const Vector< T > &v)
Definition: Operators.hh:84
void Mult(const Vector3< T > &argvec, Vector3< T > &destvec) const
matrix - vector multiplicate this matrix with Vector3, storing the result in destvec calculates: dest...
Definition: Matrix3x3.hh:302
Matrix2x3< T > operator*(const T &scalar, const Matrix2x3< T > &mat)
Definition: Operators.hh:569
void Mult(const Vector4< T > &argvec, Vector4< T > &destvec) const
matrix - vector multiplicate this matrix with Vector4, storing the result in destvec, calculates: destvec = (this Matrix) * argvec
Definition: Matrix4x4.hh:121
Vector2< T > operator-(const Vector2< T > &vec, const Vector2< T > &argvec)
Definition: Operators.hh:351
void Mult(const T &scalar, Vector3< T > &dest) const
Definition: Vector3.hh:332
Vector4< T > operator*(const Matrix4x4< T > &mat, const Vector4< T > &argvec)
Definition: Operators.hh:651
DualQuaternion< T > operator+(const DualQuaternion< T > &l, const DualQuaternion< T > &r)
void Divide(const T &scalar, Vector4< T > &dest) const
Division with a scalar, storing results in destionation vector.
Definition: Vector4.hh:637
bool operator>(const BIAS::Polynom &p1, const BIAS::Polynom &p2)
Definition: Polynom.hh:302
Vector3< T > operator-(const Vector3< T > &vec, const T &scalar)
Definition: Operators.hh:200
T * GetData()
get the pointer to the data array of the matrix (for faster direct memeory access) ...
Definition: Matrix.hh:185
is a &#39;fixed size&#39; 2x3 matrix templated over the elemnt-type.
Definition: Matrix2x3.hh:49
is a &#39;fixed size&#39; quadratic matrix of dim.
Definition: Matrix.hh:54
Matrix< T > & operator/=(Matrix< T > &mat, const T &scalar)
Definition: Operators.hh:623
void DivideIP(const T &scalar)
in place division function
Definition: Matrix.hh:486
T operator*(const Vector3< T > &vec, const Vector3< T > &arg)
Definition: Operators.hh:403
Vector< T > operator*(const BIAS::Matrix< T > &mat, const Vector4< T > &vec)
Definition: Operators.hh:734
void Set(const T &scalar)
Definition: Vector.hh:153
class Vector3 contains a Vector of fixed dim.
Definition: Matrix.hh:53
Matrix3x2< T > operator*(const Matrix3x2< T > &mat, const Matrix2x2< T > &argmat)
Definition: Operators.hh:832
void Divide(const T &scalar, Vector2< T > &dest) const
Division with a scalar, storing results in destination vector.
Definition: Vector2.hh:562
void ScalarProduct(const Vector4< T > &argvec, T &result) const
scalar product (=inner product) of two vectors, storing the result in result
Definition: Vector4.hh:475
Vector3< T > operator/(const Vector3< T > &vec, const T &scalar)
Definition: Operators.hh:221
void Sub(const T &scalar, Vector3< T > &dest) const
Substraction with a scalar, storing results in destination vector.
Definition: Vector3.hh:705
matrix class with arbitrary size, indexing is row major.
Matrix< T > operator*(const Matrix< T > &mat, const T &scalar)
Definition: Operators.hh:604
Vector3< T > operator-(const Vector3< T > &vec, const Vector3< T > &argvec)
Definition: Operators.hh:448
void Mult(const Vector4< T > &argvec, Vector3< T > &destvec) const
matrix - vector multiplicate this matrix with Vector4, storing the result in destvec calculates: dest...
Definition: Matrix3x4.hh:210
void Multiply(const T &scalar, Vector< T > &res) const
multiply components with scalar storing result in res
Definition: Vector.hh:377
Vector< T > operator*(const Vector< T > &v, const T &scalar)
Definition: Operators.hh:75
Vector4< T > operator*(const Vector4< T > &v, const T &scalar)
Definition: Operators.hh:232
Vector4< T > operator-(const Vector4< T > &v, const Vector4< T > &argvec)
Definition: Operators.hh:506
Vector4< T > operator*(const T &scalar, const Vector4< T > &v)
Definition: Operators.hh:243
Vector3< T > operator-(const T &scalar, const Vector3< T > &vec)
Definition: Operators.hh:209
void Multiply(const T &scalar, Matrix< T > &dest) const
multiplication function, storing data destination matrix
Definition: Matrix.hh:779
void Sub(const T &scalar, Matrix< T > &dest) const
substraction function, storing data destination matrix
Definition: Matrix.hh:766
Matrix< T > operator-(const T &scalar, const Matrix< T > &mat)
Definition: Operators.hh:551
Vector2< T > operator-(const T &scalar, const Vector2< T > &vec)
Definition: Operators.hh:126
void Divide(const T &scalar, Vector3< T > &dest) const
Division with a scalar, storing results in destination vector.
Definition: Vector3.hh:724
Vector2< T > operator-(const Vector2< T > &vec, const T &scalar)
Definition: Operators.hh:115
void Add(const T &scalar, Matrix< T > &dest) const
addition function, storing data destination matrix
Definition: Matrix.hh:753
Vector2< T > operator*(const Matrix2x3< T > &mat, const Vector3< T > &vec)
Definition: Operators.hh:685
T operator*(const Vector< T > &v, const Vector< T > &argvec)
Definition: Operators.hh:295
Vector3< T > operator*(const Matrix3x2< T > &mat, const Vector2< T > &vec)
Definition: Operators.hh:696
Subscript num_rows() const
Definition: cmat.h:319
Vector4< T > operator+(const Vector4< T > &v, const T &scalar)
Definition: Operators.hh:271
Vector2< T > operator*(const T &scalar, const Vector2< T > &vec)
Definition: Operators.hh:148
Matrix2x2< T > operator*(const Matrix2x2< T > &mat, const Matrix2x2< T > &argMat)
Definition: Operators.hh:788
void Divide(const T &scalar, Matrix< T > &dest) const
division function, storing data destination matrix
Definition: Matrix.hh:791
is a &#39;fixed size&#39; rectangular matrix of dim.
Definition: Matrix3x3.hh:39
is a &#39;fixed size&#39; quadratic matrix of dim.
Definition: Matrix4x4.hh:54
Matrix3x2< T > operator*(const T &scalar, const Matrix3x2< T > &mat)
Definition: Operators.hh:592
Vector3< T > operator*(const Matrix3x3< T > &mat, const Vector3< T > &argvec)
Definition: Operators.hh:638
Vector2< T > operator*(const Vector2< T > &vec, const T &scalar)
Definition: Operators.hh:137
void Conv(const Matrix< T > &left, const Matrix< T > &right, Matrix< T > &res)
zero padded convolution of two matrices
Definition: Operators.hh:755
Matrix< T > operator-(const Matrix< T > &mat, const T &scalar)
Definition: Operators.hh:546
Vector3< T > operator+(const Vector3< T > &vec, const Vector3< T > &argvec)
Definition: Operators.hh:419
Matrix< T > operator/(const Matrix< T > &mat, const T &scalar)
Definition: Operators.hh:616
Subscript size() const
Definition: vec.h:262
Matrix3x3< T > operator+(const Matrix3x3< T > &left, const Matrix3x3< T > &right)
Definition: Operators.hh:864
Vector3< T > operator+(const T &scalar, const Vector3< T > &vec)
Definition: Operators.hh:408
Vector4< T > operator+(const Vector4< T > &v, const Vector4< T > &argvec)
Definition: Operators.hh:517
Vector< T > operator*(const BIAS::Matrix< T > &mat, const Vector3< T > &vec)
Definition: Operators.hh:709
Matrix3x2< T > operator*(const Matrix3x2< T > &mat, const T &scalar)
Definition: Operators.hh:581