Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
vpEigenConversion.cpp
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  * ViSP <--> Eigen conversion.
32  */
33 
34 #include <visp3/core/vpEigenConversion.h>
35 
36 namespace VISP_NAMESPACE_NAME
37 {
38 #ifdef VISP_HAVE_EIGEN3
39 /* Eigen to ViSP */
40 void eigen2visp(const Eigen::MatrixXd &src, vpMatrix &dst)
41 {
42  dst.resize(static_cast<unsigned int>(src.rows()), static_cast<unsigned int>(src.cols()), false, false);
43  Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> >(&dst.data[0], src.rows(),
44  src.cols()) = src;
45 }
46 
47 void eigen2visp(const Eigen::MatrixXd &src, vpHomogeneousMatrix &dst)
48 {
49  if ((src.rows() != 4) || (src.cols() != 4)) {
50  throw vpException(vpException::dimensionError, "Input Eigen Matrix must be of size (4,4)!");
51  }
52 
53  Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> >(&dst.data[0], src.rows(),
54  src.cols()) = src;
55 }
56 
57 void eigen2visp(const Eigen::VectorXd &src, vpColVector &dst)
58 {
59  dst.resize(static_cast<unsigned int>(src.rows()));
60 #if (VP_VERSION_INT(EIGEN_WORLD_VERSION, EIGEN_MAJOR_VERSION, EIGEN_MINOR_VERSION) < 0x030300)
61  Eigen::DenseIndex src_rows = src.rows();
62  for (Eigen::DenseIndex i = 0; i < src_rows; i++) {
63 #else
64  Eigen::Index src_rows = src.rows();
65  for (Eigen::Index i = 0; i < src_rows; ++i) {
66 #endif
67  dst[static_cast<unsigned int>(i)] = src(i);
68  }
69 }
70 
71 void eigen2visp(const Eigen::RowVectorXd &src, vpRowVector &dst)
72 {
73  dst.resize(static_cast<unsigned int>(src.cols()));
74 #if (VP_VERSION_INT(EIGEN_WORLD_VERSION, EIGEN_MAJOR_VERSION, EIGEN_MINOR_VERSION) < 0x030300)
75  Eigen::DenseIndex src_cols = src.cols();
76  for (Eigen::DenseIndex i = 0; i < src_cols; ++i) {
77 #else
78  Eigen::Index src_cols = src.cols();
79  for (Eigen::Index i = 0; i < src_cols; ++i) {
80 #endif
81  dst[static_cast<unsigned int>(i)] = src(i);
82  }
83 }
84 
85 void visp2eigen(const vpColVector &src, Eigen::VectorXd &dst) { dst = Eigen::VectorXd::Map(src.data, src.size()); }
86 
87 void visp2eigen(const vpRowVector &src, Eigen::RowVectorXd &dst)
88 {
89  dst = Eigen::RowVectorXd::Map(src.data, src.size());
90 }
91 #endif
92 } // namespace VISP_NAMESPACE_NAME
Type * data
Address of the first element of the data array.
Definition: vpArray2D.h:148
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:362
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:349
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:1143
error that can be emitted by ViSP classes.
Definition: vpException.h:60
@ dimensionError
Bad dimension.
Definition: vpException.h:71
Implementation of an homogeneous matrix and operations on such kind of matrices.
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:124
void resize(unsigned int i, bool flagNullify=true)
Definition: vpRowVector.h:287
VISP_EXPORT void eigen2visp(const Eigen::MatrixXd &src, vpMatrix &dst)
void visp2eigen(const vpMatrix &src, Eigen::MatrixBase< Derived > &dst)