Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
vpHomogeneousMatrix.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
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 https://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 
39 #ifndef VP_HOMOGENEOUS_MATRIX_H
40 #define VP_HOMOGENEOUS_MATRIX_H
41 
42 #include <fstream>
43 #include <vector>
44 
45 #include <visp3/core/vpConfig.h>
46 
48 
50 class vpPoseVector;
51 class vpMatrix;
52 class vpRotationMatrix;
53 class vpPoseVector;
54 class vpThetaUVector;
55 class vpQuaternionVector;
56 class vpPoint;
57 
58 END_VISP_NAMESPACE
59 
60 #include <visp3/core/vpArray2D.h>
61 #include <visp3/core/vpRotationMatrix.h>
62 #include <visp3/core/vpThetaUVector.h>
63 #include <visp3/core/vpPoseVector.h>
64 
65 #ifdef VISP_HAVE_NLOHMANN_JSON
66 #include <nlohmann/json.hpp>
67 #endif
68 
220 class VISP_EXPORT vpHomogeneousMatrix : public vpArray2D<double>
221 {
222 public:
228  VP_EXPLICIT vpHomogeneousMatrix(const vpPoseVector &p);
229  VP_EXPLICIT vpHomogeneousMatrix(const std::vector<float> &v);
230  VP_EXPLICIT vpHomogeneousMatrix(const std::vector<double> &v);
231  vpHomogeneousMatrix(double tx, double ty, double tz, double tux, double tuy, double tuz);
232 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
233  VP_EXPLICIT vpHomogeneousMatrix(const std::initializer_list<double> &list);
234 #endif
235 
236 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
237  VP_DEPRECATED void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R);
238  VP_DEPRECATED void buildFrom(const vpTranslationVector &t, const vpThetaUVector &tu);
239  VP_DEPRECATED void buildFrom(const vpTranslationVector &t, const vpQuaternionVector &q);
240  VP_DEPRECATED void buildFrom(const vpPoseVector &p);
241  VP_DEPRECATED void buildFrom(const std::vector<float> &v);
242  VP_DEPRECATED void buildFrom(const std::vector<double> &v);
243  VP_DEPRECATED void buildFrom(double tx, double ty, double tz, double tux, double tuy, double tuz);
244 #endif
245  vpHomogeneousMatrix &build(const vpTranslationVector &t, const vpRotationMatrix &R);
246  vpHomogeneousMatrix &build(const vpTranslationVector &t, const vpThetaUVector &tu);
248  vpHomogeneousMatrix &build(const vpPoseVector &p);
249  vpHomogeneousMatrix &build(const std::vector<float> &v);
250  vpHomogeneousMatrix &build(const std::vector<double> &v);
251  vpHomogeneousMatrix &build(const double &tx, const double &ty, const double &tz, const double &tux, const double &tuy, const double &tuz);
252 
253  void convert(std::vector<float> &M);
254  void convert(std::vector<double> &M);
255 
256  // Set to identity
257  void eye();
258 
259  vpColVector getCol(unsigned int j) const;
260  vpRotationMatrix getRotationMatrix() const;
261  vpThetaUVector getThetaUVector() const;
262  vpTranslationVector getTranslationVector() const;
263 
264  // Invert the homogeneous matrix.
265  vpHomogeneousMatrix inverse() const;
266  // Invert the homogeneous matrix.
267  void inverse(vpHomogeneousMatrix &Mi) const;
268 
269  // Test if the rotational part of the matrix is a rotation matrix.
270  bool isAnHomogeneousMatrix(double threshold = 1e-6) const;
271  bool isValid() const;
272 
273  void insert(const vpRotationMatrix &R);
274  void insert(const vpThetaUVector &tu);
275  void insert(const vpTranslationVector &t);
276  void insert(const vpQuaternionVector &t);
277 
278  void extract(vpRotationMatrix &R) const;
279  void extract(vpThetaUVector &tu) const;
280  void extract(vpTranslationVector &t) const;
281  void extract(vpQuaternionVector &q) const;
282 
300  void load(std::ifstream &f);
301 
318  void load(const std::string &filename);
319 
341  void save(std::ofstream &f) const;
342 
363  void save(const std::string &filename) const;
364 
367  vpHomogeneousMatrix &operator*=(const vpHomogeneousMatrix &M);
368 
369  vpColVector operator*(const vpColVector &v) const;
372 
373  // Multiply by a point
374  vpPoint operator*(const vpPoint &bP) const;
375 
376  vpHomogeneousMatrix &operator<<(double val);
377  vpHomogeneousMatrix &operator,(double val);
378 
379  void orthogonalizeRotation();
380 
381  void print() const;
382 
388  void resize(unsigned int nrows, unsigned int ncols, bool flagNullify = true)
389  {
390  (void)nrows;
391  (void)ncols;
392  (void)flagNullify;
393  throw(vpException(vpException::fatalError, "Cannot resize an homogeneous matrix"));
394  }
395 
396  static vpHomogeneousMatrix compute3d3dTransformation(const std::vector<vpPoint> &p, const std::vector<vpPoint> &q);
397 
398  static vpHomogeneousMatrix mean(const std::vector<vpHomogeneousMatrix> &vec_M);
399 
400 #ifdef VISP_HAVE_NLOHMANN_JSON
401 public:
402  static const std::string jsonTypeName;
403 private:
404  friend void to_json(nlohmann::json &j, const vpHomogeneousMatrix &T);
405  friend void from_json(const nlohmann::json &j, vpHomogeneousMatrix &T);
406  // Conversion helper function to avoid circular dependencies and MSVC errors that are not exported in the DLL
407  void parse_json(const nlohmann::json &j);
408  void convert_to_json(nlohmann::json &j) const;
409 public:
410 
411 #endif
412 
413 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
422  VP_DEPRECATED void init() { }
426  VP_DEPRECATED void setIdentity();
428 #endif
429 
430 protected:
431  unsigned int m_index;
432 };
433 
434 #ifdef VISP_HAVE_NLOHMANN_JSON
435 inline void to_json(nlohmann::json &j, const vpHomogeneousMatrix &T)
436 {
437  T.convert_to_json(j);
438 }
439 
440 inline void from_json(const nlohmann::json &j, vpHomogeneousMatrix &T)
441 {
442  T.parse_json(j);
443 }
444 #endif
445 END_VISP_NAMESPACE
446 #endif
Implementation of a generic 2D array used as base class for matrices and vectors.
Definition: vpArray2D.h:145
void insert(const vpArray2D< Type > &A, unsigned int r, unsigned int c)
Definition: vpArray2D.h:494
friend void to_json(nlohmann::json &j, const vpArray2D< T > &array)
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:611
static bool load(const std::string &filename, vpArray2D< Type > &A, bool binary=false, char *header=nullptr)
Definition: vpArray2D.h:666
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
Definition: vpArray2D.h:523
static bool save(const std::string &filename, const vpArray2D< Type > &A, bool binary=false, const char *header="")
Definition: vpArray2D.h:871
friend void from_json(const nlohmann::json &j, vpArray2D< T > &array)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
error that can be emitted by ViSP classes.
Definition: vpException.h:60
@ fatalError
Fatal error.
Definition: vpException.h:72
Implementation of an homogeneous matrix and operations on such kind of matrices.
static const std::string jsonTypeName
VP_DEPRECATED void init()
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true)
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:79
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:203
Implementation of a rotation vector as quaternion angle minimal representation.
Implementation of a rotation matrix and operations on such kind of matrices.
Implementation of a rotation vector as axis-angle minimal representation.
Class that consider the case of a translation vector.
vpMatrix operator*(const double &x, const vpMatrix &A)