Visual Servoing Platform  version 3.6.1 under development (2024-11-21)
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 
47 BEGIN_VISP_NAMESPACE
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 VISP_NLOHMANN_JSON(json.hpp)
67 #endif
68 
69 BEGIN_VISP_NAMESPACE
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  vpHomogeneousMatrix &buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R);
237  vpHomogeneousMatrix &buildFrom(const vpTranslationVector &t, const vpThetaUVector &tu);
238  vpHomogeneousMatrix &buildFrom(const vpTranslationVector &t, const vpQuaternionVector &q);
239  vpHomogeneousMatrix &buildFrom(const vpPoseVector &p);
240  vpHomogeneousMatrix &buildFrom(const std::vector<float> &v);
241  vpHomogeneousMatrix &buildFrom(const std::vector<double> &v);
242  vpHomogeneousMatrix &buildFrom(const double &tx, const double &ty, const double &tz, const double &tux, const double &tuy, const double &tuz);
243 
244  void convert(std::vector<float> &M);
245  void convert(std::vector<double> &M);
246 
247  // Set to identity
248  void eye();
249 
250  vpColVector getCol(unsigned int j) const;
251  vpRotationMatrix getRotationMatrix() const;
252  vpThetaUVector getThetaUVector() const;
253  vpTranslationVector getTranslationVector() const;
254 
255  // Invert the homogeneous matrix.
256  vpHomogeneousMatrix inverse() const;
257  // Invert the homogeneous matrix.
258  void inverse(vpHomogeneousMatrix &Mi) const;
259 
260  // Test if the rotational part of the matrix is a rotation matrix.
261  bool isAnHomogeneousMatrix(double threshold = 1e-6) const;
262  bool isValid() const;
263 
264  void insert(const vpRotationMatrix &R);
265  void insert(const vpThetaUVector &tu);
266  void insert(const vpTranslationVector &t);
267  void insert(const vpQuaternionVector &t);
268 
269  void extract(vpRotationMatrix &R) const;
270  void extract(vpThetaUVector &tu) const;
271  void extract(vpTranslationVector &t) const;
272  void extract(vpQuaternionVector &q) const;
273 
291  void load(std::ifstream &f);
292 
309  void load(const std::string &filename);
310 
332  void save(std::ofstream &f) const;
333 
354  void save(const std::string &filename) const;
355 
358  vpHomogeneousMatrix &operator*=(const vpHomogeneousMatrix &M);
359 
360  vpColVector operator*(const vpColVector &v) const;
363 
364  // Multiply by a point
365  vpPoint operator*(const vpPoint &bP) const;
366 
367  vpHomogeneousMatrix &operator<<(double val);
368  vpHomogeneousMatrix &operator,(double val);
369 
370  void orthogonalizeRotation();
371 
372  void print() const;
373 
379  void resize(unsigned int nrows, unsigned int ncols, bool flagNullify = true)
380  {
381  (void)nrows;
382  (void)ncols;
383  (void)flagNullify;
384  throw(vpException(vpException::fatalError, "Cannot resize an homogeneous matrix"));
385  }
386 
387  static vpHomogeneousMatrix compute3d3dTransformation(const std::vector<vpPoint> &p, const std::vector<vpPoint> &q);
388 
389  static vpHomogeneousMatrix mean(const std::vector<vpHomogeneousMatrix> &vec_M);
390 
391 #ifdef VISP_HAVE_NLOHMANN_JSON
392 public:
393  static const std::string jsonTypeName;
394 private:
395  friend void to_json(nlohmann::json &j, const vpHomogeneousMatrix &T);
396  friend void from_json(const nlohmann::json &j, vpHomogeneousMatrix &T);
397  // Conversion helper function to avoid circular dependencies and MSVC errors that are not exported in the DLL
398  void parse_json(const nlohmann::json &j);
399  void convert_to_json(nlohmann::json &j) const;
400 public:
401 
402 #endif
403 
404 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
413  VP_DEPRECATED void init() { }
417  VP_DEPRECATED void setIdentity();
419 #endif
420 
421 protected:
422  unsigned int m_index;
423 };
424 
425 #ifdef VISP_HAVE_NLOHMANN_JSON
426 inline void to_json(nlohmann::json &j, const vpHomogeneousMatrix &T)
427 {
428  T.convert_to_json(j);
429 }
430 
431 inline void from_json(const nlohmann::json &j, vpHomogeneousMatrix &T)
432 {
433  T.parse_json(j);
434 }
435 #endif
436 END_VISP_NAMESPACE
437 #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:497
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:614
static bool load(const std::string &filename, vpArray2D< Type > &A, bool binary=false, char *header=nullptr)
Definition: vpArray2D.h:669
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
Definition: vpArray2D.h:526
static bool save(const std::string &filename, const vpArray2D< Type > &A, bool binary=false, const char *header="")
Definition: vpArray2D.h:874
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
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)