Visual Servoing Platform  version 3.0.0
vpHomogeneousMatrix.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Homogeneous matrix.
32  *
33  * Authors:
34  * Eric Marchand
35  *
36  *****************************************************************************/
37 
38 
45 #include <visp3/core/vpDebug.h>
46 #include <visp3/core/vpMatrix.h>
47 #include <visp3/core/vpHomogeneousMatrix.h>
48 #include <visp3/core/vpQuaternionVector.h>
49 #include <visp3/core/vpPoint.h>
50 #include <visp3/core/vpException.h>
51 
56  const vpQuaternionVector &q)
57  : vpArray2D<double>(4, 4)
58 {
59  buildFrom(t,q);
60  (*this)[3][3] = 1.;
61 }
62 
67  : vpArray2D<double>(4, 4)
68 {
69  eye() ;
70 }
71 
76  : vpArray2D<double>(4, 4)
77 {
78  *this = M;
79 }
80 
85  const vpThetaUVector &tu)
86  : vpArray2D<double>(4, 4)
87 {
88  buildFrom(t, tu);
89  (*this)[3][3] = 1.;
90 }
91 
96  const vpRotationMatrix &R)
97  : vpArray2D<double>(4, 4)
98 {
99  insert(R);
100  insert(t);
101  (*this)[3][3] = 1.;
102 }
103 
108  : vpArray2D<double>(4, 4)
109 {
110  buildFrom(p[0], p[1], p[2], p[3], p[4], p[5]) ;
111  (*this)[3][3] = 1.;
112 }
113 
152 vpHomogeneousMatrix::vpHomogeneousMatrix(const std::vector<float> &v)
153  : vpArray2D<double>(4, 4)
154 {
155  buildFrom(v) ;
156  (*this)[3][3] = 1.;
157 }
158 
197 vpHomogeneousMatrix::vpHomogeneousMatrix(const std::vector<double> &v)
198  : vpArray2D<double>(4, 4)
199 {
200  buildFrom(v) ;
201  (*this)[3][3] = 1.;
202 }
203 
209  const double ty,
210  const double tz,
211  const double tux,
212  const double tuy,
213  const double tuz)
214  : vpArray2D<double>(4, 4)
215 {
216  buildFrom(tx, ty, tz, tux, tuy, tuz);
217  (*this)[3][3] = 1.;
218 }
219 
224 void
226  const vpThetaUVector &tu)
227 {
228  insert(tu) ;
229  insert(t) ;
230 }
231 
236 void
238  const vpRotationMatrix &R)
239 {
240  insert(R) ;
241  insert(t) ;
242 }
243 
247 void
249 {
250  vpTranslationVector tv(p[0], p[1], p[2]);
251  vpThetaUVector tu(p[3], p[4], p[5]);
252 
253  insert(tu) ;
254  insert(tv) ;
255 }
256 
262  const vpQuaternionVector &q)
263 {
264  insert(t);
265  insert(q);
266 }
267 
272 void
274  const double ty,
275  const double tz,
276  const double tux,
277  const double tuy,
278  const double tuz)
279 {
280  vpRotationMatrix R(tux, tuy, tuz) ;
281  vpTranslationVector t(tx, ty, tz) ;
282 
283  insert(R) ;
284  insert(t) ;
285 }
286 
326 void
327 vpHomogeneousMatrix::buildFrom(const std::vector<float> &v)
328 {
329  if (v.size() != 12 && v.size() != 16) {
330  throw(vpException(vpException::dimensionError, "Cannot convert std::vector<float> to vpHomogeneousMatrix"));
331  }
332 
333  for (unsigned int i=0; i < 12; i++)
334  this->data[i] = (double)v[i];
335 }
336 
376 void
377 vpHomogeneousMatrix::buildFrom(const std::vector<double> &v)
378 {
379  if (v.size() != 12 && v.size() != 16) {
380  throw(vpException(vpException::dimensionError, "Cannot convert std::vector<double> to vpHomogeneousMatrix"));
381  }
382 
383  for (unsigned int i=0; i < 12; i++)
384  this->data[i] = v[i];
385 }
386 
394 {
395  for (int i=0; i<4; i++) {
396  for (int j=0; j<4; j++) {
397  rowPtrs[i][j] = M.rowPtrs[i][j];
398  }
399  }
400  return *this;
401 }
402 
422 {
423  vpHomogeneousMatrix p,p1 ;
424 
425  vpRotationMatrix R1, R2, R ;
426  vpTranslationVector T1, T2 , T;
427 
428  extract(T1) ;
429  M.extract(T2) ;
430 
431  extract (R1) ;
432  M.extract (R2) ;
433 
434  R = R1*R2 ;
435 
436  T = R1*T2 + T1 ;
437 
438  p.insert(T) ;
439  p.insert(R) ;
440 
441  return p;
442 }
443 
451 {
452  if (v.getRows() != 4) {
454  "Cannot multiply a (4x4) homogeneous matrix by a (%dx1) column vector",
455  v.getRows()));
456  }
457  vpColVector p(rowNum);
458 
459  p = 0.0;
460 
461  for (unsigned int j=0;j<4;j++) {
462  for (unsigned int i=0;i<4;i++) {
463  p[i]+=rowPtrs[i][j] * v[j];
464  }
465  }
466 
467  return p;
468 }
469 
480 {
481  vpPoint aP ;
482 
483  vpColVector v(4),v1(4) ;
484 
485  v[0] = bP.get_X() ;
486  v[1] = bP.get_Y() ;
487  v[2] = bP.get_Z() ;
488  v[3] = bP.get_W() ;
489 
490  v1[0] = (*this)[0][0]*v[0] + (*this)[0][1]*v[1]+ (*this)[0][2]*v[2]+ (*this)[0][3]*v[3] ;
491  v1[1] = (*this)[1][0]*v[0] + (*this)[1][1]*v[1]+ (*this)[1][2]*v[2]+ (*this)[1][3]*v[3] ;
492  v1[2] = (*this)[2][0]*v[0] + (*this)[2][1]*v[1]+ (*this)[2][2]*v[2]+ (*this)[2][3]*v[3] ;
493  v1[3] = (*this)[3][0]*v[0] + (*this)[3][1]*v[1]+ (*this)[3][2]*v[2]+ (*this)[3][3]*v[3] ;
494 
495  v1 /= v1[3] ;
496 
497  // v1 = M*v ;
498  aP.set_X(v1[0]) ;
499  aP.set_Y(v1[1]) ;
500  aP.set_Z(v1[2]) ;
501  aP.set_W(v1[3]) ;
502 
503  aP.set_oX(v1[0]) ;
504  aP.set_oY(v1[1]) ;
505  aP.set_oZ(v1[2]) ;
506  aP.set_oW(v1[3]) ;
507 
508  return aP ;
509 }
510 
511 /*********************************************************************/
512 
517 bool
519 {
520  vpRotationMatrix R ;
521  extract(R) ;
522 
523  return R.isARotationMatrix() ;
524 }
525 
530 void
532 {
533  for (unsigned int i=0 ; i < 3 ; i++)
534  for (unsigned int j=0 ; j < 3; j++)
535  R[i][j] = (*this)[i][j] ;
536 }
537 
541 void
543 {
544  t[0] = (*this)[0][3] ;
545  t[1] = (*this)[1][3] ;
546  t[2] = (*this)[2][3] ;
547 }
551 void
553 {
555  (*this).extract(R);
556  tu.buildFrom(R);
557 }
558 
562 void
564 {
566  (*this).extract(R);
567  q.buildFrom(R);
568 }
569 
573 void
575 {
576  for (unsigned int i=0 ; i < 3 ; i++)
577  for (unsigned int j=0 ; j < 3; j++)
578  (*this)[i][j] = R[i][j] ;
579 }
580 
587 void
589 {
590  vpRotationMatrix R(tu) ;
591  insert(R) ;
592 }
593 
597 void
599 {
600  (*this)[0][3] = t[0] ;
601  (*this)[1][3] = t[1] ;
602  (*this)[2][3] = t[2] ;
603 }
604 
611 void
614 }
615 
632 {
634 
635  vpRotationMatrix R ; extract(R) ;
637 
638  vpTranslationVector RtT ; RtT = -(R.t()*T) ;
639 
640 
641  Mi.insert(R.t()) ;
642  Mi.insert(RtT) ;
643 
644  return Mi ;
645 }
646 
651 {
652  (*this)[0][0] = 1 ;
653  (*this)[1][1] = 1 ;
654  (*this)[2][2] = 1 ;
655  (*this)[3][3] = 1 ;
656 
657  (*this)[0][1] = (*this)[0][2] = (*this)[0][3] = 0 ;
658  (*this)[1][0] = (*this)[1][2] = (*this)[1][3] = 0 ;
659  (*this)[2][0] = (*this)[2][1] = (*this)[2][3] = 0 ;
660  (*this)[3][0] = (*this)[3][1] = (*this)[3][2] = 0 ;
661 }
662 
677 void
679 {
680  M = inverse() ;
681 }
682 
683 
706 void
707 vpHomogeneousMatrix::save(std::ofstream &f) const
708 {
709  if (! f.fail()) {
710  f << *this ;
711  }
712  else {
714  "Cannot save homogeneous matrix: ostream not open")) ;
715  }
716 }
717 
718 
737 void
738 vpHomogeneousMatrix::load(std::ifstream &f)
739 {
740  if (! f.fail()) {
741  for (unsigned int i=0 ; i < 4 ; i++) {
742  for (unsigned int j=0 ; j < 4 ; j++) {
743  f >> (*this)[i][j] ;
744  }
745  }
746  }
747  else {
749  "Cannot laad homogeneous matrix: ifstream not open")) ;
750  }
751 }
752 
754 void
756 {
757  vpPoseVector r(*this) ;
758  std::cout << r.t() ;
759 }
764 void
766 {
767  eye() ;
768 }
769 
774 void vpHomogeneousMatrix::convert(std::vector<float> &M)
775 {
776  M.resize(12);
777  for(unsigned int i=0; i < 12; i++)
778  M[i] = (float)(this->data[i]);
779 }
780 
785 void vpHomogeneousMatrix::convert(std::vector<double> &M)
786 {
787  M.resize(12);
788  for(unsigned int i=0; i < 12; i++)
789  M[i] = this->data[i];
790 }
791 
796 {
798  this->extract(tr);
799  return tr;
800 }
801 
806 {
808  this->extract(R);
809  return R;
810 }
811 
817 {
818  vpThetaUVector tu;
819  this->extract(tu);
820  return tu;
821 }
822 
852 vpHomogeneousMatrix::getCol(const unsigned int j) const
853 {
854  if (j >= getCols())
856  "Unable to extract a column vector from the homogeneous matrix"));
857  unsigned int nb_rows = getRows();
858  vpColVector c(nb_rows);
859  for (unsigned int i=0 ; i < nb_rows ; i++)
860  c[i] = (*this)[i][j];
861  return c;
862 }
void set_oZ(const double oZ)
Set the point Z coordinate in the object frame.
Definition: vpPoint.cpp:491
Implementation of an homogeneous matrix and operations on such kind of matrices.
vp_deprecated void setIdentity()
error that can be emited by ViSP classes.
Definition: vpException.h:73
vpRotationMatrix t() const
double * data
Address of the first element of the data array.
Definition: vpArray2D.h:84
vpQuaternionVector buildFrom(const double qx, const double qy, const double qz, const double qw)
Implementation of a generic 2D array used as vase class of matrices and vectors.
Definition: vpArray2D.h:70
unsigned int getCols() const
Return the number of columns of the 2D array.
Definition: vpArray2D.h:154
void set_X(const double X)
Set the point X coordinate in the camera frame.
Definition: vpPoint.cpp:478
vpThetaUVector buildFrom(const vpHomogeneousMatrix &M)
double get_W() const
Get the point W coordinate in the camera frame.
Definition: vpPoint.cpp:444
void load(std::ifstream &f)
Class that defines what is a point.
Definition: vpPoint.h:59
bool isARotationMatrix() const
Implementation of a rotation matrix and operations on such kind of matrices.
vpHomogeneousMatrix & operator=(const vpHomogeneousMatrix &M)
vpTranslationVector getTranslationVector()
void insert(const vpRotationMatrix &R)
unsigned int rowNum
Number of rows in the array.
Definition: vpArray2D.h:74
void set_oW(const double oW)
Set the point W coordinate in the object frame.
Definition: vpPoint.cpp:493
void set_Z(const double Z)
Set the point Z coordinate in the camera frame.
Definition: vpPoint.cpp:482
vpRotationMatrix getRotationMatrix()
void set_W(const double W)
Set the point W coordinate in the camera frame.
Definition: vpPoint.cpp:484
void set_oX(const double oX)
Set the point X coordinate in the object frame.
Definition: vpPoint.cpp:487
void set_Y(const double Y)
Set the point Y coordinate in the camera frame.
Definition: vpPoint.cpp:480
void extract(vpRotationMatrix &R) const
Implementation of a rotation vector as quaternion angle minimal representation.
unsigned int getRows() const
Return the number of rows of the 2D array.
Definition: vpArray2D.h:152
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
double get_Y() const
Get the point Y coordinate in the camera frame.
Definition: vpPoint.cpp:440
double get_Z() const
Get the point Z coordinate in the camera frame.
Definition: vpPoint.cpp:442
bool isAnHomogeneousMatrix() const
void save(std::ofstream &f) const
vpHomogeneousMatrix operator*(const vpHomogeneousMatrix &M) const
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
void set_oY(const double oY)
Set the point Y coordinate in the object frame.
Definition: vpPoint.cpp:489
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:93
vpHomogeneousMatrix inverse() const
vpThetaUVector getThetaUVector()
vpColVector getCol(const unsigned int j) const
void convert(std::vector< float > &M)
void print() const
Print the matrix as a pose vector .
double get_X() const
Get the point X coordinate in the camera frame.
Definition: vpPoint.cpp:438
Class that consider the case of a translation vector.
Implementation of a rotation vector as axis-angle minimal representation.
double ** rowPtrs
Address of the first element of each rows.
Definition: vpArray2D.h:78
vpRowVector t() const