Visual Servoing Platform  version 3.6.1 under development (2024-03-28)
vpHomogeneousMatrix.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 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 _vpHomogeneousMatrix_h_
40 #define _vpHomogeneousMatrix_h_
41 
43 class vpPoseVector;
44 class vpMatrix;
45 class vpRotationMatrix;
46 class vpPoseVector;
47 class vpThetaUVector;
48 class vpQuaternionVector;
49 class vpPoint;
50 
51 #include <fstream>
52 #include <vector>
53 
54 #include <visp3/core/vpArray2D.h>
55 #include <visp3/core/vpRotationMatrix.h>
56 #include <visp3/core/vpThetaUVector.h>
57 //#include <visp3/core/vpTranslationVector.h>
58 #include <visp3/core/vpPoseVector.h>
59 
60 #ifdef VISP_HAVE_NLOHMANN_JSON
61 #include <nlohmann/json.hpp>
62 #endif
63 
198 class VISP_EXPORT vpHomogeneousMatrix : public vpArray2D<double>
199 {
200 public:
206  explicit vpHomogeneousMatrix(const vpPoseVector &p);
207  explicit vpHomogeneousMatrix(const std::vector<float> &v);
208  explicit vpHomogeneousMatrix(const std::vector<double> &v);
209  vpHomogeneousMatrix(double tx, double ty, double tz, double tux, double tuy, double tuz);
210 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
211  vpHomogeneousMatrix(const std::initializer_list<double> &list);
212 #endif
213 
214  void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R);
215  void buildFrom(const vpTranslationVector &t, const vpThetaUVector &tu);
216  void buildFrom(const vpTranslationVector &t, const vpQuaternionVector &q);
217  void buildFrom(const vpPoseVector &p);
218  void buildFrom(const std::vector<float> &v);
219  void buildFrom(const std::vector<double> &v);
220  void buildFrom(double tx, double ty, double tz, double tux, double tuy, double tuz);
221 
222  void convert(std::vector<float> &M);
223  void convert(std::vector<double> &M);
224 
225  // Set to identity
226  void eye();
227 
228  vpColVector getCol(unsigned int j) const;
229  vpRotationMatrix getRotationMatrix() const;
230  vpThetaUVector getThetaUVector() const;
231  vpTranslationVector getTranslationVector() const;
232 
233  // Invert the homogeneous matrix.
234  vpHomogeneousMatrix inverse() const;
235  // Invert the homogeneous matrix.
236  void inverse(vpHomogeneousMatrix &Mi) const;
237 
238  // Test if the rotational part of the matrix is a rotation matrix.
239  bool isAnHomogeneousMatrix(double threshold = 1e-6) const;
240  bool isValid() const;
241 
242  void insert(const vpRotationMatrix &R);
243  void insert(const vpThetaUVector &tu);
244  void insert(const vpTranslationVector &t);
245  void insert(const vpQuaternionVector &t);
246 
247  void extract(vpRotationMatrix &R) const;
248  void extract(vpThetaUVector &tu) const;
249  void extract(vpTranslationVector &t) const;
250  void extract(vpQuaternionVector &q) const;
251 
269  void load(std::ifstream &f);
270 
287  void load(const std::string &filename);
288 
310  void save(std::ofstream &f) const;
311 
332  void save(const std::string &filename) const;
333 
336  vpHomogeneousMatrix &operator*=(const vpHomogeneousMatrix &M);
337 
338  vpColVector operator*(const vpColVector &v) const;
341 
342  // Multiply by a point
343  vpPoint operator*(const vpPoint &bP) const;
344 
345  vpHomogeneousMatrix &operator<<(double val);
346  vpHomogeneousMatrix &operator,(double val);
347 
348  void orthogonalizeRotation();
349 
350  void print() const;
351 
357  void resize(unsigned int nrows, unsigned int ncols, bool flagNullify = true)
358  {
359  (void)nrows;
360  (void)ncols;
361  (void)flagNullify;
362  throw(vpException(vpException::fatalError, "Cannot resize an homogeneous matrix"));
363  }
364 
365  static vpHomogeneousMatrix compute3d3dTransformation(const std::vector<vpPoint> &p, const std::vector<vpPoint> &q);
366 
367  static vpHomogeneousMatrix mean(const std::vector<vpHomogeneousMatrix> &vec_M);
368 
369 #ifdef VISP_HAVE_NLOHMANN_JSON
370 public:
371  static const std::string jsonTypeName;
372 private:
373  friend void to_json(nlohmann::json &j, const vpHomogeneousMatrix &cam);
374  friend void from_json(const nlohmann::json &j, vpHomogeneousMatrix &T);
375  // Conversion helper function to avoid circular dependencies and MSVC errors that are not exported in the DLL
376  void parse_json(const nlohmann::json &j);
377  void convert_to_json(nlohmann::json &j) const;
378 public:
379 
380 #endif
381 
382 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
391  vp_deprecated void init() { }
395  vp_deprecated void setIdentity();
397 #endif
398 
399 protected:
400  unsigned int m_index;
401 };
402 
403 #ifdef VISP_HAVE_NLOHMANN_JSON
404 inline void to_json(nlohmann::json &j, const vpHomogeneousMatrix &T)
405 {
406  T.convert_to_json(j);
407 }
408 inline void from_json(const nlohmann::json &j, vpHomogeneousMatrix &T)
409 {
410  T.parse_json(j);
411 }
412 #endif
413 
414 #endif
Implementation of a generic 2D array used as base class for matrices and vectors.
Definition: vpArray2D.h:125
void insert(const vpArray2D< Type > &A, unsigned int r, unsigned int c)
Definition: vpArray2D.h:431
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:547
static bool load(const std::string &filename, vpArray2D< Type > &A, bool binary=false, char *header=nullptr)
Definition: vpArray2D.h:600
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
Definition: vpArray2D.h:459
static bool save(const std::string &filename, const vpArray2D< Type > &A, bool binary=false, const char *header="")
Definition: vpArray2D.h:802
friend void from_json(const nlohmann::json &j, vpArray2D< T > &array)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ fatalError
Fatal error.
Definition: vpException.h:84
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)
vp_deprecated void init()
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:146
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:77
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:189
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)