RcsPySim
A robot control and simulation library
eigen_matnd.h
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright (c) 2020, Fabio Muratore, Honda Research Institute Europe GmbH, and
3  Technical University of Darmstadt.
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions are met:
8  1. Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  2. Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  3. Neither the name of Fabio Muratore, Honda Research Institute Europe GmbH,
14  or Technical University of Darmstadt, nor the names of its contributors may
15  be used to endorse or promote products derived from this software without
16  specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  DISCLAIMED. IN NO EVENT SHALL FABIO MURATORE, HONDA RESEARCH INSTITUTE EUROPE GMBH,
22  OR TECHNICAL UNIVERSITY OF DARMSTADT BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
26  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  POSSIBILITY OF SUCH DAMAGE.
29 *******************************************************************************/
30 
31 #ifndef RCSPYSIM_EIGEN_MATND_H
32 #define RCSPYSIM_EIGEN_MATND_H
33 
34 #include <Rcs_MatNd.h>
35 
36 #include <Eigen/Core>
37 
38 namespace Rcs
39 {
40 
41 /**
42  * View an eigen matrix as Rcs MatNd.
43  * @tparam M eigen matrix type
44  * @param src eigen matrix
45  * @return MatNd struct referencing the data of src.
46  */
47 template<typename M>
48 MatNd viewEigen2MatNd(M& src)
49 {
50  static_assert(std::is_same<typename M::Scalar, double>::value,
51  "MatNd is only compatible with double matrices.");
52  static_assert(M::IsRowMajor || M::IsVectorAtCompileTime,
53  "MatNd is only compatible with row major matrices or vectors.");
54  MatNd mat;
55  mat.m = src.rows();
56  mat.n = src.cols();
57  mat.size = src.size();
58  mat.ele = src.data();
59  mat.stackMem = true;
60  return mat;
61 }
62 
63 // MatNd is row major. Eigen, by default, is column major.
64 // This alias provides a type closest to M that has a MatNd-compatible layout.
65 // For vectors, we do nothing, since there are no inner strides.
66 template<typename M>
67 using MatNdMappable = typename std::conditional<
68  M::IsRowMajor || M::IsVectorAtCompileTime,
69  M,
70  Eigen::Matrix<
71  typename M::Scalar,
72  M::RowsAtCompileTime,
73  M::ColsAtCompileTime,
74  Eigen::RowMajor> >::type;
75 
76 
77 /**
78  * Wrap a Rcs MatNd in a mutable Eigen Map object.
79  *
80  * If M is column major, the resulting map will still wrap a row major matrix, since that is the storage
81  * order of MatNd.
82  *
83  * @tparam M desired eigen matrix type
84  * @param src rcs matrix
85  * @return Eigen::Map referencing the data of src.
86  */
87 template<typename M = Eigen::MatrixXd>
88 Eigen::Map<MatNdMappable<M>> viewMatNd2Eigen(MatNd* src)
89 {
90  static_assert(std::is_same<typename M::Scalar, double>::value,
91  "MatNd is only compatible with double matrices.");
92  return Eigen::Map<MatNdMappable<M>>(src->ele, src->m, src->n);
93 }
94 
95 /**
96  * Wrap a read-only Rcs MatNd in a const Eigen Map object.
97  *
98  * If M is column major, the resulting map will still wrap a row major matrix, since that is the storage
99  * order of MatNd.
100  *
101  * @tparam M desired eigen matrix type
102  * @param src rcs matrix
103  * @return Eigen::Map referencing the data of src.
104  */
105 template<typename M = Eigen::MatrixXd>
106 Eigen::Map<const MatNdMappable<M>> viewMatNd2Eigen(const MatNd* src)
107 {
108  static_assert(std::is_same<typename M::Scalar, double>::value,
109  "MatNd is only compatible with double matrices.");
110  return Eigen::Map<const MatNdMappable<M>>(src->ele, src->m, src->n);
111 }
112 
113 /**
114  * Copy a matrix from Rcs to Eigen.
115  * @tparam M eigen matrix type
116  * @param dst destination eigen matrix
117  * @param src source rcs matrix
118  */
119 template<typename M>
120 void copyMatNd2Eigen(M& dst, const MatNd* src)
121 {
122  dst = viewMatNd2Eigen<M>(src);
123 }
124 
125 /**
126  * Copy a matrix from Eigen to Rcs.
127  * @tparam M eigen matrix type
128  * @param dst destination rcs matrix
129  * @param src source eigen matrix
130  */
131 template<typename M>
132 void copyEigen2MatNd(MatNd* dst, const M& src)
133 {
134  viewMatNd2Eigen<M>(dst) = src;
135 }
136 
137 }
138 
139 #endif //RCSPYSIM_EIGEN_MATND_H
void copyMatNd2Eigen(M &dst, const MatNd *src)
Definition: eigen_matnd.h:120
Eigen::Map< MatNdMappable< M > > viewMatNd2Eigen(MatNd *src)
Definition: eigen_matnd.h:88
void copyEigen2MatNd(MatNd *dst, const M &src)
Definition: eigen_matnd.h:132
typename std::conditional< M::IsRowMajor||M::IsVectorAtCompileTime, M, Eigen::Matrix< typename M::Scalar, M::RowsAtCompileTime, M::ColsAtCompileTime, Eigen::RowMajor > >::type MatNdMappable
Definition: eigen_matnd.h:74
MatNd viewEigen2MatNd(M &src)
Definition: eigen_matnd.h:48