ViSP  2.10.0
vpHomogeneousMatrix.cpp
1 /****************************************************************************
2  *
3  * $Id: vpHomogeneousMatrix.cpp 5130 2015-01-06 18:50:43Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Homogeneous matrix.
36  *
37  * Authors:
38  * Eric Marchand
39  *
40  *****************************************************************************/
41 
42 
49 #include <visp/vpDebug.h>
50 #include <visp/vpMatrix.h>
51 #include <visp/vpHomogeneousMatrix.h>
52 #include <visp/vpQuaternionVector.h>
53 
54 // Exception
55 #include <visp/vpException.h>
56 #include <visp/vpMatrixException.h>
57 
58 // Debug trace
59 #include <visp/vpDebug.h>
60 
61 
65 void
67 {
68  unsigned int i,j ;
69 
70  try {
71  resize(4,4) ;
72  }
73  catch(vpException me)
74  {
75  vpERROR_TRACE("Error caught") ;
76  throw ;
77  }
78 
79 
80  for (i=0 ; i < 4 ; i++)
81  for (j=0 ; j < 4; j++)
82  if (i==j)
83  (*this)[i][j] = 1.0 ;
84  else
85  (*this)[i][j] = 0.0;
86 
87 }
88 
90  const vpQuaternionVector &q) : vpMatrix()
91 {
92  init();
93  buildFrom(tv,q);
94 }
95 
100 {
101  init() ;
102 }
103 
104 
109 {
110  init() ;
111  *this = M ;
112 }
113 
115  const vpThetaUVector &tu) : vpMatrix()
116 {
117  init() ;
118  buildFrom(tv,tu) ;
119 }
120 
122  const vpRotationMatrix &R) : vpMatrix()
123 {
124  init() ;
125  insert(R) ;
126  insert(tv) ;
127 }
128 
130 {
131 
132  init() ;
133  buildFrom(p[0],p[1],p[2],p[3],p[4],p[5]) ;
134 }
135 
174 vpHomogeneousMatrix::vpHomogeneousMatrix(const std::vector<float> &v) : vpMatrix()
175 {
176  init() ;
177  buildFrom(v) ;
178 }
179 
218 vpHomogeneousMatrix::vpHomogeneousMatrix(const std::vector<double> &v) : vpMatrix()
219 {
220  init() ;
221  buildFrom(v) ;
222 }
223 
225  const double ty,
226  const double tz,
227  const double tux,
228  const double tuy,
229  const double tuz) : vpMatrix()
230 {
231  init() ;
232  buildFrom(tx, ty, tz,tux, tuy, tuz) ;
233 }
234 
235 void
237  const vpThetaUVector &tu)
238 {
239  insert(tu) ;
240  insert(tv) ;
241 }
242 
243 void
245  const vpRotationMatrix &R)
246 {
247  init() ;
248  insert(R) ;
249  insert(tv) ;
250 }
251 
252 
253 void
255 {
256 
257  vpTranslationVector tv(p[0],p[1],p[2]) ;
258  vpThetaUVector tu(p[3],p[4],p[5]) ;
259 
260  insert(tu) ;
261  insert(tv) ;
262 }
263 
265  const vpQuaternionVector &q)
266 {
267  insert(tv);
268  insert(q);
269 }
270 
271 void
273  const double ty,
274  const double tz,
275  const double tux,
276  const double tuy,
277  const double tuz)
278 {
279  vpRotationMatrix R(tux, tuy, tuz) ;
280  vpTranslationVector tv(tx, ty, tz) ;
281 
282  insert(R) ;
283  insert(tv) ;
284 }
285 
325 void
326 vpHomogeneousMatrix::buildFrom(const std::vector<float> &v)
327 {
328  if (v.size() != 12 && v.size() != 16) {
329  throw(vpException(vpException::dimensionError, "Cannot convert std::vector<float> to vpHomogeneousMatrix"));
330  }
331 
332  for (unsigned int i=0; i < 12; i++)
333  this->data[i] = (double)v[i];
334 }
335 
375 void
376 vpHomogeneousMatrix::buildFrom(const std::vector<double> &v)
377 {
378  if (v.size() != 12 && v.size() != 16) {
379  throw(vpException(vpException::dimensionError, "Cannot convert std::vector<double> to vpHomogeneousMatrix"));
380  }
381 
382  for (unsigned int i=0; i < 12; i++)
383  this->data[i] = v[i];
384 }
385 
393 {
394 
395  if (rowPtrs != M.rowPtrs) init() ;
396 
397  for (int i=0; i<4; i++)
398  {
399  for (int j=0; j<4; j++)
400  {
401  rowPtrs[i][j] = M.rowPtrs[i][j];
402  }
403  }
404  return *this;
405 }
406 
426 {
427  vpHomogeneousMatrix p,p1 ;
428 
429  vpRotationMatrix R1, R2, R ;
430  vpTranslationVector T1, T2 , T;
431 
432 
433  extract(T1) ;
434  M.extract(T2) ;
435 
436  extract (R1) ;
437  M.extract (R2) ;
438 
439  R = R1*R2 ;
440 
441  T = R1*T2 + T1 ;
442 
443  p.insert(T) ;
444  p.insert(R) ;
445 
446  return p;
447 }
448 
451 {
452  vpColVector p(rowNum);
453 
454  p = 0.0;
455 
456  for (unsigned int j=0;j<4;j++) {
457  for (unsigned int i=0;i<4;i++) {
458  p[i]+=rowPtrs[i][j] * v[j];
459  }
460  }
461 
462  return p;
463 }
464 
465 
466 /*********************************************************************/
467 
472 bool
474 {
475  vpRotationMatrix R ;
476  extract(R) ;
477 
478  return R.isARotationMatrix() ;
479 }
480 
485 void
487 {
488  unsigned int i,j ;
489 
490  for (i=0 ; i < 3 ; i++)
491  for (j=0 ; j < 3; j++)
492  R[i][j] = (*this)[i][j] ;
493 }
494 
498 void
500 {
501  tv[0] = (*this)[0][3] ;
502  tv[1] = (*this)[1][3] ;
503  tv[2] = (*this)[2][3] ;
504 }
508 void
510 {
511 
513  (*this).extract(R);
514  tu.buildFrom(R);
515 }
516 
520 void
522 {
524  (*this).extract(R);
525  q.buildFrom(R);
526 }
527 
531 void
533 {
534  unsigned int i,j ;
535 
536  for (i=0 ; i < 3 ; i++)
537  for (j=0 ; j < 3; j++)
538  (*this)[i][j] = R[i][j] ;
539 }
540 
547 void
549 {
550  vpRotationMatrix R(tu) ;
551  insert(R) ;
552 }
553 
557 void
559 {
560  (*this)[0][3] = T[0] ;
561  (*this)[1][3] = T[1] ;
562  (*this)[2][3] = T[2] ;
563 }
564 
571 void
574 }
575 
592 {
594 
595 
596  vpRotationMatrix R ; extract(R) ;
598 
599  vpTranslationVector RtT ; RtT = -(R.t()*T) ;
600 
601 
602  Mi.insert(R.t()) ;
603  Mi.insert(RtT) ;
604 
605  return Mi ;
606 }
607 
612 {
613  (*this)[0][0] = 1 ;
614  (*this)[1][1] = 1 ;
615  (*this)[2][2] = 1 ;
616 
617  (*this)[0][1] = (*this)[0][2] = 0 ;
618  (*this)[1][0] = (*this)[1][2] = 0 ;
619  (*this)[2][0] = (*this)[2][1] = 0 ;
620 
621  (*this)[0][3] = 0 ;
622  (*this)[1][3] = 0 ;
623  (*this)[2][3] = 0 ;
624 }
625 
640 void
642 {
643  M = inverse() ;
644 }
645 
646 
669 void
670 vpHomogeneousMatrix::save(std::ofstream &f) const
671 {
672  if (! f.fail())
673  {
674  f << *this ;
675  }
676  else
677  {
678  vpERROR_TRACE("\t\t file not open " );
679  throw(vpException(vpException::ioError, "\t\t file not open")) ;
680  }
681 }
682 
683 
702 void
703 vpHomogeneousMatrix::load(std::ifstream &f)
704 {
705  if (! f.fail())
706  {
707  for (unsigned int i=0 ; i < 4 ; i++)
708  for (unsigned int j=0 ; j < 4 ; j++)
709  {
710  f >> (*this)[i][j] ;
711  }
712  }
713  else
714  {
715  vpERROR_TRACE("\t\t file not open " );
716  throw(vpException(vpException::ioError, "\t\t file not open")) ;
717  }
718 }
719 
721 void
723 {
724  vpPoseVector r(*this) ;
725  std::cout << r.t() ;
726 }
728 void
730 {
731  init() ;
732 }
733 
738 void vpHomogeneousMatrix::convert(std::vector<float> &M)
739 {
740  M.resize(12);
741  for(unsigned int i=0; i < 12; i++)
742  M[i] = (float)(this->data[i]);
743 }
744 
749 void vpHomogeneousMatrix::convert(std::vector<double> &M)
750 {
751  M.resize(12);
752  for(unsigned int i=0; i < 12; i++)
753  M[i] = this->data[i];
754 }
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
void resize(const unsigned int nrows, const unsigned int ncols, const bool nullify=true)
Definition: vpMatrix.cpp:199
void print()
Print the matrix as a vector [T thetaU].
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
#define vpERROR_TRACE
Definition: vpDebug.h:395
void setIdentity()
Basic initialisation (identity)
error that can be emited by ViSP classes.
Definition: vpException.h:76
vpRotationMatrix t() const
transpose
vpThetaUVector buildFrom(const vpHomogeneousMatrix &M)
void load(std::ifstream &f)
bool isARotationMatrix() const
test if the matrix is an rotation matrix
The vpRotationMatrix considers the particular case of a rotation matrix.
double * data
address of the first element of the data array
Definition: vpMatrix.h:118
vpHomogeneousMatrix & operator=(const vpHomogeneousMatrix &M)
Copy operator from vpHomogeneousMatrix.
void insert(const vpRotationMatrix &R)
double ** rowPtrs
address of the first element of each rows
Definition: vpMatrix.h:121
vpHomogeneousMatrix()
Basic constructor.
vpRowVector t() const
Transpose of a vector.
void extract(vpRotationMatrix &R) const
Defines a quaternion and its basic operations.
void buildFrom(const vpRotationMatrix &R)
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
Construction from translation vector and rotation matrix.
bool isAnHomogeneousMatrix() const
unsigned int rowNum
number of rows
Definition: vpMatrix.h:112
void save(std::ofstream &f) const
vpHomogeneousMatrix operator*(const vpHomogeneousMatrix &M) const
Multiply two homogeneous matrices: aMb = aMc*cMb.
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
The pose is a complete representation of every rigid motion in the euclidian space.
Definition: vpPoseVector.h:92
vpHomogeneousMatrix inverse() const
void init()
Basic initialisation (identity).
void convert(std::vector< float > &M)
Class that consider the case of a translation vector.
Class that consider the case of the parameterization for the rotation.