Visual Servoing Platform  version 3.6.1 under development (2024-04-19)
vpEigenConversion.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * ViSP <--> Eigen conversion.
33  *
34 *****************************************************************************/
35 
36 #include <visp3/core/vpEigenConversion.h>
37 
38 namespace vp
39 {
40 #ifdef VISP_HAVE_EIGEN3
41 /* Eigen to ViSP */
42 void eigen2visp(const Eigen::MatrixXd &src, vpMatrix &dst)
43 {
44  dst.resize(static_cast<unsigned int>(src.rows()), static_cast<unsigned int>(src.cols()), false, false);
45  Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> >(&dst.data[0], src.rows(),
46  src.cols()) = src;
47 }
48 
49 void eigen2visp(const Eigen::MatrixXd &src, vpHomogeneousMatrix &dst)
50 {
51  if ((src.rows() != 4) || (src.cols() != 4)) {
52  throw vpException(vpException::dimensionError, "Input Eigen Matrix must be of size (4,4)!");
53  }
54 
55  Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> >(&dst.data[0], src.rows(),
56  src.cols()) = src;
57 }
58 
59 void eigen2visp(const Eigen::VectorXd &src, vpColVector &dst)
60 {
61  dst.resize(static_cast<unsigned int>(src.rows()));
62 #if (VP_VERSION_INT(EIGEN_WORLD_VERSION, EIGEN_MAJOR_VERSION, EIGEN_MINOR_VERSION) < 0x030300)
63  Eigen::DenseIndex src_rows = src.rows();
64  for (Eigen::DenseIndex i = 0; i < src_rows; i++) {
65 #else
66  Eigen::Index src_rows = src.rows();
67  for (Eigen::Index i = 0; i < src_rows; ++i) {
68 #endif
69  dst[static_cast<unsigned int>(i)] = src(i);
70  }
71 }
72 
73 void eigen2visp(const Eigen::RowVectorXd &src, vpRowVector &dst)
74 {
75  dst.resize(static_cast<unsigned int>(src.cols()));
76 #if (VP_VERSION_INT(EIGEN_WORLD_VERSION, EIGEN_MAJOR_VERSION, EIGEN_MINOR_VERSION) < 0x030300)
77  Eigen::DenseIndex src_cols = src.cols();
78  for (Eigen::DenseIndex i = 0; i < src_cols; ++i) {
79 #else
80  Eigen::Index src_cols = src.cols();
81  for (Eigen::Index i = 0; i < src_cols; ++i) {
82 #endif
83  dst[static_cast<unsigned int>(i)] = src(i);
84  }
85 }
86 
87 void visp2eigen(const vpColVector &src, Eigen::VectorXd &dst) { dst = Eigen::VectorXd::Map(src.data, src.size()); }
88 
89 void visp2eigen(const vpRowVector &src, Eigen::RowVectorXd &dst)
90 {
91  dst = Eigen::RowVectorXd::Map(src.data, src.size());
92 }
93 #endif
94 } // namespace vp
Type * data
Address of the first element of the data array.
Definition: vpArray2D.h:139
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:352
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:339
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:1056
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ dimensionError
Bad dimension.
Definition: vpException.h:83
Implementation of an homogeneous matrix and operations on such kind of matrices.
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:146
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:107
void resize(unsigned int i, bool flagNullify=true)
Definition: vpRowVector.h:259
void visp2eigen(const vpMatrix &src, Eigen::MatrixBase< Derived > &dst)
VISP_EXPORT void eigen2visp(const Eigen::MatrixXd &src, vpMatrix &dst)