Visual Servoing Platform  version 3.5.1 under development (2023-09-22)
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  for (Eigen::DenseIndex i = 0; i < src.rows(); i++) {
64 #else
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  for (Eigen::DenseIndex i = 0; i < src.cols(); i++) {
76 #else
77  for (Eigen::Index i = 0; i < src.cols(); i++) {
78 #endif
79  dst[static_cast<unsigned int>(i)] = src(i);
80  }
81 }
82 
83 void visp2eigen(const vpColVector &src, Eigen::VectorXd &dst) { dst = Eigen::VectorXd::Map(src.data, src.size()); }
84 
85 void visp2eigen(const vpRowVector &src, Eigen::RowVectorXd &dst)
86 {
87  dst = Eigen::RowVectorXd::Map(src.data, src.size());
88 }
89 #endif
90 } // namespace vp
Type * data
Address of the first element of the data array.
Definition: vpArray2D.h:144
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:305
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:292
Implementation of column vector and the associated operations.
Definition: vpColVector.h:167
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:351
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:152
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:111
void resize(unsigned int i, bool flagNullify=true)
Definition: vpRowVector.h:266
void visp2eigen(const vpMatrix &src, Eigen::MatrixBase< Derived > &dst)
VISP_EXPORT void eigen2visp(const Eigen::MatrixXd &src, vpMatrix &dst)