Skip to content

Commit

Permalink
Fixed use of const when converting from a const multidimensional arra…
Browse files Browse the repository at this point in the history
…y to a eigen matrix.

Added test
  • Loading branch information
S-Dafarra committed Dec 20, 2024
1 parent 24fcd90 commit 3a48853
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion include/matioCpp/EigenConversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ inline Eigen::Map<Eigen::Matrix<type, Eigen::Dynamic, Eigen::Dynamic>> to_eigen(
* @return A const map from the internal data of the MultiDimensionalArray
*/
template <typename type>
inline const Eigen::Map<Eigen::Matrix<type, Eigen::Dynamic, Eigen::Dynamic>> to_eigen(const MultiDimensionalArray<type>& input);
inline Eigen::Map<const Eigen::Matrix<type, Eigen::Dynamic, Eigen::Dynamic>> to_eigen(const MultiDimensionalArray<type>& input);

/**
* @brief Conversion from a MultiDimensionalArray to an Eigen matrix
Expand Down
2 changes: 1 addition & 1 deletion include/matioCpp/impl/EigenConversions.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ inline Eigen::Map<Eigen::Matrix<type, Eigen::Dynamic, Eigen::Dynamic>> matioCpp:
}

template <typename type>
inline const Eigen::Map<Eigen::Matrix<type, Eigen::Dynamic, Eigen::Dynamic>> matioCpp::to_eigen(const matioCpp::MultiDimensionalArray<type>& input)
inline Eigen::Map<const Eigen::Matrix<type, Eigen::Dynamic, Eigen::Dynamic>> matioCpp::to_eigen(const matioCpp::MultiDimensionalArray<type>& input)
{
assert(input.isValid());
assert(input.dimensions().size() == 2);
Expand Down
8 changes: 6 additions & 2 deletions test/ExogenousConversionsUnitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ TEST_CASE("Eigen Conversions")
Eigen::MatrixXf toEigenMatrix = matioCpp::to_eigen(matioCppMatrix);
checkSameMatrix(toEigenMatrix, matioCppMatrix);

const auto& constMatioCppMatrix = matioCppMatrix;
Eigen::MatrixXf toEigenMatrixConst = matioCpp::to_eigen(constMatioCppMatrix);
checkSameMatrix(toEigenMatrixConst, constMatioCppMatrix);

std::vector<float> tensor(12);
for (size_t i = 0; i < 12; ++i)
{
Expand Down Expand Up @@ -125,11 +129,11 @@ TEST_CASE("Eigen Conversions")
expectedSlice << 8.0;
expectedSlice.isApprox(matioCpp::to_eigen(matioCppMatrix2, { 1, 1, 1 }), 1e-5);

const auto& constMatioCppMatrix = matioCppMatrix2;
const auto& constMatioCppMatrix2 = matioCppMatrix2;
expectedSlice.resize(2, 3);
expectedSlice << 1.0, 5.0, 9.0,
3.0, 6.0, 11.0;
expectedSlice.isApprox(matioCpp::to_eigen(constMatioCppMatrix, { 0, -1, -1 }), 1e-5);
expectedSlice.isApprox(matioCpp::to_eigen(constMatioCppMatrix2, { 0, -1, -1 }), 1e-5);

}

Expand Down

0 comments on commit 3a48853

Please sign in to comment.