Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpHomogeneousMatrix.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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 {
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 
463 {
464  (*this) = (*this) * M;
465  return (*this);
466 }
467 
475 {
476  if (v.getRows() != 4) {
478  "Cannot multiply a (4x4) homogeneous matrix by a (%dx1) column vector",
479  v.getRows()));
480  }
481  vpColVector p(rowNum);
482 
483  p = 0.0;
484 
485  for (unsigned int j=0;j<4;j++) {
486  for (unsigned int i=0;i<4;i++) {
487  p[i]+=rowPtrs[i][j] * v[j];
488  }
489  }
490 
491  return p;
492 }
493 
504 {
505  vpPoint aP ;
506 
507  vpColVector v(4),v1(4) ;
508 
509  v[0] = bP.get_X() ;
510  v[1] = bP.get_Y() ;
511  v[2] = bP.get_Z() ;
512  v[3] = bP.get_W() ;
513 
514  v1[0] = (*this)[0][0]*v[0] + (*this)[0][1]*v[1]+ (*this)[0][2]*v[2]+ (*this)[0][3]*v[3] ;
515  v1[1] = (*this)[1][0]*v[0] + (*this)[1][1]*v[1]+ (*this)[1][2]*v[2]+ (*this)[1][3]*v[3] ;
516  v1[2] = (*this)[2][0]*v[0] + (*this)[2][1]*v[1]+ (*this)[2][2]*v[2]+ (*this)[2][3]*v[3] ;
517  v1[3] = (*this)[3][0]*v[0] + (*this)[3][1]*v[1]+ (*this)[3][2]*v[2]+ (*this)[3][3]*v[3] ;
518 
519  v1 /= v1[3] ;
520 
521  // v1 = M*v ;
522  aP.set_X(v1[0]) ;
523  aP.set_Y(v1[1]) ;
524  aP.set_Z(v1[2]) ;
525  aP.set_W(v1[3]) ;
526 
527  aP.set_oX(v1[0]) ;
528  aP.set_oY(v1[1]) ;
529  aP.set_oZ(v1[2]) ;
530  aP.set_oW(v1[3]) ;
531 
532  return aP ;
533 }
534 
544 {
545  vpTranslationVector t_out;
546  t_out[0] = (*this)[0][0]*t[0] + (*this)[0][1]*t[1]+ (*this)[0][2]*t[2]+ (*this)[0][3];
547  t_out[1] = (*this)[1][0]*t[0] + (*this)[1][1]*t[1]+ (*this)[1][2]*t[2]+ (*this)[1][3];
548  t_out[2] = (*this)[2][0]*t[0] + (*this)[2][1]*t[1]+ (*this)[2][2]*t[2]+ (*this)[2][3];
549 
550  return t_out ;
551 }
552 
553 /*********************************************************************/
554 
559 bool
561 {
562  vpRotationMatrix R ;
563  extract(R) ;
564 
565  return R.isARotationMatrix() ;
566 }
567 
572 void
574 {
575  for (unsigned int i=0 ; i < 3 ; i++)
576  for (unsigned int j=0 ; j < 3; j++)
577  R[i][j] = (*this)[i][j] ;
578 }
579 
583 void
585 {
586  t[0] = (*this)[0][3] ;
587  t[1] = (*this)[1][3] ;
588  t[2] = (*this)[2][3] ;
589 }
593 void
595 {
597  (*this).extract(R);
598  tu.buildFrom(R);
599 }
600 
604 void
606 {
608  (*this).extract(R);
609  q.buildFrom(R);
610 }
611 
615 void
617 {
618  for (unsigned int i=0 ; i < 3 ; i++)
619  for (unsigned int j=0 ; j < 3; j++)
620  (*this)[i][j] = R[i][j] ;
621 }
622 
629 void
631 {
632  vpRotationMatrix R(tu) ;
633  insert(R) ;
634 }
635 
639 void
641 {
642  (*this)[0][3] = t[0] ;
643  (*this)[1][3] = t[1] ;
644  (*this)[2][3] = t[2] ;
645 }
646 
653 void
656 }
657 
674 {
676 
677  vpRotationMatrix R ; extract(R) ;
679 
680  vpTranslationVector RtT ; RtT = -(R.t()*T) ;
681 
682 
683  Mi.insert(R.t()) ;
684  Mi.insert(RtT) ;
685 
686  return Mi ;
687 }
688 
693 {
694  (*this)[0][0] = 1 ;
695  (*this)[1][1] = 1 ;
696  (*this)[2][2] = 1 ;
697  (*this)[3][3] = 1 ;
698 
699  (*this)[0][1] = (*this)[0][2] = (*this)[0][3] = 0 ;
700  (*this)[1][0] = (*this)[1][2] = (*this)[1][3] = 0 ;
701  (*this)[2][0] = (*this)[2][1] = (*this)[2][3] = 0 ;
702  (*this)[3][0] = (*this)[3][1] = (*this)[3][2] = 0 ;
703 }
704 
719 void
721 {
722  M = inverse() ;
723 }
724 
725 
748 void
749 vpHomogeneousMatrix::save(std::ofstream &f) const
750 {
751  if (! f.fail()) {
752  f << *this ;
753  }
754  else {
756  "Cannot save homogeneous matrix: ostream not open")) ;
757  }
758 }
759 
760 
779 void
780 vpHomogeneousMatrix::load(std::ifstream &f)
781 {
782  if (! f.fail()) {
783  for (unsigned int i=0 ; i < 4 ; i++) {
784  for (unsigned int j=0 ; j < 4 ; j++) {
785  f >> (*this)[i][j] ;
786  }
787  }
788  }
789  else {
791  "Cannot laad homogeneous matrix: ifstream not open")) ;
792  }
793 }
794 
796 void
798 {
799  vpPoseVector r(*this) ;
800  std::cout << r.t() ;
801 }
802 
807 void vpHomogeneousMatrix::convert(std::vector<float> &M)
808 {
809  M.resize(12);
810  for(unsigned int i=0; i < 12; i++)
811  M[i] = (float)(this->data[i]);
812 }
813 
818 void vpHomogeneousMatrix::convert(std::vector<double> &M)
819 {
820  M.resize(12);
821  for(unsigned int i=0; i < 12; i++)
822  M[i] = this->data[i];
823 }
824 
829 {
831  this->extract(tr);
832  return tr;
833 }
834 
839 {
841  this->extract(R);
842  return R;
843 }
844 
850 {
851  vpThetaUVector tu;
852  this->extract(tu);
853  return tu;
854 }
855 
885 vpHomogeneousMatrix::getCol(const unsigned int j) const
886 {
887  if (j >= getCols())
889  "Unable to extract a column vector from the homogeneous matrix"));
890  unsigned int nb_rows = getRows();
891  vpColVector c(nb_rows);
892  for (unsigned int i=0 ; i < nb_rows ; i++)
893  c[i] = (*this)[i][j];
894  return c;
895 }
896 
897 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
898 
905 void
907 {
908  eye() ;
909 }
910 
911 #endif //#if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
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
vpThetaUVector getThetaUVector() const
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)
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() const
void set_W(const double W)
Set the point W coordinate in the camera frame.
Definition: vpPoint.cpp:484
vpTranslationVector getTranslationVector() const
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
vpColVector getCol(const unsigned int j) const
vpHomogeneousMatrix & operator*=(const vpHomogeneousMatrix &M)
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