matio-cpp  v0.2.5
A C++ wrapper of the matio library, with memory ownership handling, to read and write .mat files.
EigenConversions.tpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2022 Fondazione Istituto Italiano di Tecnologia
3  *
4  * This software may be modified and distributed under the terms of the
5  * BSD-2-Clause license (https://opensource.org/licenses/BSD-2-Clause).
6  */
7 
8 #ifndef EIGENCONVERSIONS_TPP
9 #define EIGENCONVERSIONS_TPP
10 
11 #include <cassert>
12 
13 template <typename type>
14 inline Eigen::Map<Eigen::Matrix<type, Eigen::Dynamic, Eigen::Dynamic>> matioCpp::to_eigen(matioCpp::MultiDimensionalArray<type>& input)
15 {
16  assert(input.isValid());
17  assert(input.dimensions().size() == 2);
18  return Eigen::Map<Eigen::Matrix<type, Eigen::Dynamic, Eigen::Dynamic>>(input.data(), input.dimensions()(0), input.dimensions()(1));
19 }
20 
21 template <typename type>
22 inline const Eigen::Map<Eigen::Matrix<type, Eigen::Dynamic, Eigen::Dynamic>> matioCpp::to_eigen(const matioCpp::MultiDimensionalArray<type>& input)
23 {
24  assert(input.isValid());
25  assert(input.dimensions().size() == 2);
26  return Eigen::Map<const Eigen::Matrix<type, Eigen::Dynamic, Eigen::Dynamic>>(input.data(), input.dimensions()(0), input.dimensions()(1));
27 }
28 
29 template <typename type>
30 inline Eigen::Map<Eigen::Matrix<type, Eigen::Dynamic, 1>> matioCpp::to_eigen(matioCpp::Vector<type>& input)
31 {
32  assert(input.isValid());
33  return Eigen::Map<Eigen::Matrix<type, Eigen::Dynamic, 1>>(input.data(), input.size());
34 }
35 
36 template <typename type>
37 inline const Eigen::Map<Eigen::Matrix<type, Eigen::Dynamic, 1>> matioCpp::to_eigen(const matioCpp::Vector<type>& input)
38 {
39  assert(input.isValid());
40  return Eigen::Map<const Eigen::Matrix<type, Eigen::Dynamic, 1>>(input.data(), input.size());
41 }
42 
43 template <typename EigenDerived, typename>
44 inline matioCpp::MultiDimensionalArray<typename EigenDerived::Scalar> matioCpp::make_variable(const std::string& name, const Eigen::MatrixBase<EigenDerived>& input)
45 {
46  matioCpp::MultiDimensionalArray<typename EigenDerived::Scalar> matio(name, {static_cast<size_t>(input.rows()), static_cast<size_t>(input.cols())});
47  matioCpp::to_eigen(matio) = input;
48  return matio;
49 }
50 
51 #endif // EIGENCONVERSIONS_TPP
MultiDimensionalArray is a particular type of Variable specialized for multidimensional arrays of a g...
pointer data()
Direct access to the underlying array.
matioCpp::Span< const size_t > dimensions() const
Get the dimensions of this object.
Definition: Variable.cpp:528
bool isValid() const
Check if the variable is valid.
Definition: Variable.cpp:542
Vector is a particular type of Variable specialized for 1-D arrays of a generic type T.
Definition: Vector.h:23
pointer data()
Direct access to the underlying array.
Definition: Vector.tpp:286
index_type size() const
Get the size of the vector.
Definition: Vector.tpp:260
matioCpp::Vector< typename std::remove_cv_t< typename matioCpp::SpanUtils::container_data< Vector >::type > > make_variable(const std::string &name, const Vector &input)
Conversion from a generic vector to a matioCpp::Vector.