Visual Servoing Platform  version 3.6.0 under development (2023-09-27)
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 
200 class VISP_EXPORT vpHomogeneousMatrix : public vpArray2D<double>
201 {
202 public:
208  explicit vpHomogeneousMatrix(const vpPoseVector &p);
209  explicit vpHomogeneousMatrix(const std::vector<float> &v);
210  explicit vpHomogeneousMatrix(const std::vector<double> &v);
211  vpHomogeneousMatrix(double tx, double ty, double tz, double tux, double tuy, double tuz);
212 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
213  vpHomogeneousMatrix(const std::initializer_list<double> &list);
214 #endif
218  virtual ~vpHomogeneousMatrix() { }
219 
220  void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R);
221  void buildFrom(const vpTranslationVector &t, const vpThetaUVector &tu);
222  void buildFrom(const vpTranslationVector &t, const vpQuaternionVector &q);
223  void buildFrom(const vpPoseVector &p);
224  void buildFrom(const std::vector<float> &v);
225  void buildFrom(const std::vector<double> &v);
226  void buildFrom(double tx, double ty, double tz, double tux, double tuy, double tuz);
227 
228  void convert(std::vector<float> &M);
229  void convert(std::vector<double> &M);
230 
231  // Set to identity
232  void eye();
233 
234  vpColVector getCol(unsigned int j) const;
235  vpRotationMatrix getRotationMatrix() const;
236  vpThetaUVector getThetaUVector() const;
237  vpTranslationVector getTranslationVector() const;
238 
239  // Invert the homogeneous matrix.
240  vpHomogeneousMatrix inverse() const;
241  // Invert the homogeneous matrix.
242  void inverse(vpHomogeneousMatrix &Mi) const;
243 
244  // Test if the rotational part of the matrix is a rotation matrix.
245  bool isAnHomogeneousMatrix(double threshold = 1e-6) const;
246  bool isValid() const;
247 
248  void insert(const vpRotationMatrix &R);
249  void insert(const vpThetaUVector &tu);
250  void insert(const vpTranslationVector &t);
251  void insert(const vpQuaternionVector &t);
252 
253  void extract(vpRotationMatrix &R) const;
254  void extract(vpThetaUVector &tu) const;
255  void extract(vpTranslationVector &t) const;
256  void extract(vpQuaternionVector &q) const;
257 
258  // Load an homogeneous matrix from a file
259  void load(std::ifstream &f);
260  // Save an homogeneous matrix in a file
261  void save(std::ofstream &f) const;
262 
265  vpHomogeneousMatrix &operator*=(const vpHomogeneousMatrix &M);
266 
267  vpColVector operator*(const vpColVector &v) const;
270 
271  // Multiply by a point
272  vpPoint operator*(const vpPoint &bP) const;
273 
274  vpHomogeneousMatrix &operator<<(double val);
275  vpHomogeneousMatrix &operator,(double val);
276 
277  void orthogonalizeRotation();
278 
279  void print() const;
280 
286  void resize(unsigned int nrows, unsigned int ncols, bool flagNullify = true)
287  {
288  (void)nrows;
289  (void)ncols;
290  (void)flagNullify;
291  throw(vpException(vpException::fatalError, "Cannot resize an homogeneous matrix"));
292  }
293 
294  static vpHomogeneousMatrix compute3d3dTransformation(const std::vector<vpPoint> &p, const std::vector<vpPoint> &q);
295 
296  static vpHomogeneousMatrix mean(const std::vector<vpHomogeneousMatrix> &vec_M);
297 
298 #ifdef VISP_HAVE_NLOHMANN_JSON
299 public:
300  static const std::string jsonTypeName;
301 private:
302  friend void to_json(nlohmann::json &j, const vpHomogeneousMatrix &cam);
303  friend void from_json(const nlohmann::json &j, vpHomogeneousMatrix &T);
304  // Conversion helper function to avoid circular dependencies and MSVC errors that are not exported in the DLL
305  void parse_json(const nlohmann::json &j);
306  void convert_to_json(nlohmann::json &j) const;
307 public:
308 
309 #endif
310 
311 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
320  vp_deprecated void init() { }
324  vp_deprecated void setIdentity();
326 #endif
327 
328 protected:
329  unsigned int m_index;
330 };
331 
332 #ifdef VISP_HAVE_NLOHMANN_JSON
333 inline void to_json(nlohmann::json &j, const vpHomogeneousMatrix &T)
334 {
335  T.convert_to_json(j);
336 }
337 inline void from_json(const nlohmann::json &j, vpHomogeneousMatrix &T)
338 {
339  T.parse_json(j);
340 }
341 #endif
342 
343 #endif
Implementation of a generic 2D array used as base class for matrices and vectors.
Definition: vpArray2D.h:131
static bool load(const std::string &filename, vpArray2D< Type > &A, bool binary=false, char *header=NULL)
Definition: vpArray2D.h:582
void insert(const vpArray2D< Type > &A, unsigned int r, unsigned int c)
Definition: vpArray2D.h:417
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:529
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
Definition: vpArray2D.h:445
static bool save(const std::string &filename, const vpArray2D< Type > &A, bool binary=false, const char *header="")
Definition: vpArray2D.h:784
friend void from_json(const nlohmann::json &j, vpArray2D< T > &array)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:167
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:152
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:192
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.
vpColVector operator*(const double &x, const vpColVector &v)