Visual Servoing Platform  version 3.6.1 under development (2024-06-15)
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 VISP_NAMESPACE_NAME
39 {
40 #ifdef VISP_HAVE_EIGEN3
41 /* Eigen to ViSP */
42 void eigen2visp(const Eigen::MatrixXd &src, VISP_NAMESPACE_ADDRESSING 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, VISP_NAMESPACE_ADDRESSING vpHomogeneousMatrix &dst)
50 {
51  if ((src.rows() != 4) || (src.cols() != 4)) {
52  throw VISP_NAMESPACE_ADDRESSING vpException(VISP_NAMESPACE_ADDRESSING 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, VISP_NAMESPACE_ADDRESSING 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, VISP_NAMESPACE_ADDRESSING 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 VISP_NAMESPACE_ADDRESSING vpColVector &src, Eigen::VectorXd &dst) { dst = Eigen::VectorXd::Map(src.data, src.size()); }
88 
89 void visp2eigen(const VISP_NAMESPACE_ADDRESSING vpRowVector &src, Eigen::RowVectorXd &dst)
90 {
91  dst = Eigen::RowVectorXd::Map(src.data, src.size());
92 }
93 #endif
94 } // namespace VISP_NAMESPACE_NAME
Implementation of column vector and the associated operations.
Definition: vpColVector.h:171
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:151
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:110
void visp2eigen(const VISP_NAMESPACE_ADDRESSING vpMatrix &src, Eigen::MatrixBase< Derived > &dst)
VISP_EXPORT void eigen2visp(const Eigen::MatrixXd &src, VISP_NAMESPACE_ADDRESSING vpMatrix &dst)