Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sofa/framework/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ set(HEADER_FILES
${SRC_ROOT}/collision/IntersectorFactory.h
${SRC_ROOT}/collision/NarrowPhaseDetection.h
${SRC_ROOT}/collision/Pipeline.h
${SRC_ROOT}/data/Data[Eigen].h
${SRC_ROOT}/loader/BaseLoader.h
${SRC_ROOT}/loader/ImageLoader.h
${SRC_ROOT}/loader/MeshLoader.h
Expand Down Expand Up @@ -343,6 +344,7 @@ set(SOURCE_FILES
${SRC_ROOT}/objectmodel/ScriptEvent.cpp
${SRC_ROOT}/objectmodel/Tag.cpp
${SRC_ROOT}/objectmodel/TagSet.cpp
${SRC_ROOT}/data/Data[Eigen].cpp
${SRC_ROOT}/topology/BaseMeshTopology.cpp
${SRC_ROOT}/topology/BaseTopology.cpp
${SRC_ROOT}/topology/BaseTopologyObject.cpp
Expand Down
243 changes: 243 additions & 0 deletions Sofa/framework/Core/src/sofa/core/data/Data[Eigen].cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#include <sofa/core/data/Data[Eigen].h>

#include <sstream>

namespace sofa::core::objectmodel
{

/// Shared IO implementation for Eigen matrices, partially specialized on the
/// whole Eigen::Matrix<Scalar, Rows, Cols, ...> family (dynamic or fixed size,
/// any scalar type) so that a single implementation serves every matrix type.
template<typename MatrixType> struct EigenMatrixDataIO; // only the Eigen::Matrix family is specialized

template<typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
struct EigenMatrixDataIO<Eigen::Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols>>
{
using Matrix = Eigen::Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols>;

/// Reads the dimensions, resizes dynamic matrices accordingly, then reads
/// the coefficients in row-major order. Sets failbit and returns false on
/// invalid dimensions (negative, or conflicting with a compile-time size)
/// or on parsing failure.
static bool read(std::istream& in, Matrix& matrix)
{
Eigen::Index rows = 0;
Eigen::Index cols = 0;
in >> rows >> cols;

const bool dimsValid = !in.fail()
&& rows >= 0 && cols >= 0
&& (Rows == Eigen::Dynamic || rows == Rows)
&& (Cols == Eigen::Dynamic || cols == Cols);
if (!dimsValid)
{
in.setstate(std::ios::failbit);
return false;
}

if constexpr (Rows == Eigen::Dynamic || Cols == Eigen::Dynamic)
{
matrix.resize(rows, cols);
}

for (Eigen::Index row = 0; row < rows; ++row)
{
for (Eigen::Index col = 0; col < cols; ++col)
{
in >> matrix(row, col);
}
}
return !in.fail();
}

/// Prints the dimensions, then the coefficients in row-major order.
static void print(std::ostream& out, const Matrix& matrix)
{
out << matrix.rows() << ' ' << matrix.cols();
for (Eigen::Index row = 0; row < matrix.rows(); ++row)
{
for (Eigen::Index col = 0; col < matrix.cols(); ++col)
{
out << ' ' << matrix(row, col);
}
}
}
};

/// The read() and getValueString() specializations are thin forwarders to the
/// shared EigenMatrixDataIO helper (see Data[Eigen].h).
template<>
bool SOFA_CORE_API Data<Eigen::MatrixXf>::read( const std::string& str )
{
std::istringstream in( str );
const bool ok = EigenMatrixDataIO<Eigen::MatrixXf>::read( in, *beginEdit() );
endEdit();
return ok;
}

template<>
std::string SOFA_CORE_API Data<Eigen::MatrixXf>::getValueString() const
{
std::ostringstream out;
EigenMatrixDataIO<Eigen::MatrixXf>::print( out, getValue() );
return out.str();
}

template<>
bool SOFA_CORE_API Data<Eigen::MatrixXd>::read( const std::string& str )
{
std::istringstream in( str );
const bool ok = EigenMatrixDataIO<Eigen::MatrixXd>::read( in, *beginEdit() );
endEdit();
return ok;
}

template<>
std::string SOFA_CORE_API Data<Eigen::MatrixXd>::getValueString() const
{
std::ostringstream out;
EigenMatrixDataIO<Eigen::MatrixXd>::print( out, getValue() );
return out.str();
}

template<>
bool SOFA_CORE_API Data<Eigen::Matrix2f>::read( const std::string& str )
{
std::istringstream in( str );
const bool ok = EigenMatrixDataIO<Eigen::Matrix2f>::read( in, *beginEdit() );
endEdit();
return ok;
}

template<>
std::string SOFA_CORE_API Data<Eigen::Matrix2f>::getValueString() const
{
std::ostringstream out;
EigenMatrixDataIO<Eigen::Matrix2f>::print( out, getValue() );
return out.str();
}

template<>
bool SOFA_CORE_API Data<Eigen::Matrix2d>::read( const std::string& str )
{
std::istringstream in( str );
const bool ok = EigenMatrixDataIO<Eigen::Matrix2d>::read( in, *beginEdit() );
endEdit();
return ok;
}

template<>
std::string SOFA_CORE_API Data<Eigen::Matrix2d>::getValueString() const
{
std::ostringstream out;
EigenMatrixDataIO<Eigen::Matrix2d>::print( out, getValue() );
return out.str();
}

template<>
bool SOFA_CORE_API Data<Eigen::Matrix3f>::read( const std::string& str )
{
std::istringstream in( str );
const bool ok = EigenMatrixDataIO<Eigen::Matrix3f>::read( in, *beginEdit() );
endEdit();
return ok;
}

template<>
std::string SOFA_CORE_API Data<Eigen::Matrix3f>::getValueString() const
{
std::ostringstream out;
EigenMatrixDataIO<Eigen::Matrix3f>::print( out, getValue() );
return out.str();
}

template<>
bool SOFA_CORE_API Data<Eigen::Matrix3d>::read( const std::string& str )
{
std::istringstream in( str );
const bool ok = EigenMatrixDataIO<Eigen::Matrix3d>::read( in, *beginEdit() );
endEdit();
return ok;
}

template<>
std::string SOFA_CORE_API Data<Eigen::Matrix3d>::getValueString() const
{
std::ostringstream out;
EigenMatrixDataIO<Eigen::Matrix3d>::print( out, getValue() );
return out.str();
}

template<>
bool SOFA_CORE_API Data<Eigen::Matrix4f>::read( const std::string& str )
{
std::istringstream in( str );
const bool ok = EigenMatrixDataIO<Eigen::Matrix4f>::read( in, *beginEdit() );
endEdit();
return ok;
}

template<>
std::string SOFA_CORE_API Data<Eigen::Matrix4f>::getValueString() const
{
std::ostringstream out;
EigenMatrixDataIO<Eigen::Matrix4f>::print( out, getValue() );
return out.str();
}

template<>
bool SOFA_CORE_API Data<Eigen::Matrix4d>::read( const std::string& str )
{
std::istringstream in( str );
const bool ok = EigenMatrixDataIO<Eigen::Matrix4d>::read( in, *beginEdit() );
endEdit();
return ok;
}

template<>
std::string SOFA_CORE_API Data<Eigen::Matrix4d>::getValueString() const
{
std::ostringstream out;
EigenMatrixDataIO<Eigen::Matrix4d>::print( out, getValue() );
return out.str();
}

template<>
bool SOFA_CORE_API Data<Eigen::MatrixXi>::read( const std::string& str )
{
std::istringstream in( str );
const bool ok = EigenMatrixDataIO<Eigen::MatrixXi>::read( in, *beginEdit() );
endEdit();
return ok;
}

template<>
std::string SOFA_CORE_API Data<Eigen::MatrixXi>::getValueString() const
{
std::ostringstream out;
EigenMatrixDataIO<Eigen::MatrixXi>::print( out, getValue() );
return out.str();
}

} // namespace sofa::core::objectmodel
70 changes: 70 additions & 0 deletions Sofa/framework/Core/src/sofa/core/data/Data[Eigen].h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#pragma once

#include <sofa/core/objectmodel/Data.h>
#include <Eigen/Dense>

/// Support of Eigen matrices in sofa::core::objectmodel::Data.
///
/// The string format is "rows cols c00 c01 ... c(rows-1)(cols-1)": the
/// dimensions, then the coefficients in row-major order, all separated by
/// whitespaces. Reading resizes dynamic matrices to the given dimensions, so
/// no pre-sized default value is needed; for fixed-size matrices the
/// dimensions must match the compile-time sizes, otherwise the read fails.
///
/// Both directions are explicit specializations of Data<T>::read() and
/// Data<T>::getValueString(), declared below and defined in Data[Eigen].cpp.
/// No operator<< or operator>> is added to namespace
///
/// Note: a matrix type that is not listed below cannot be stored in a Data:
/// the generic Data<T>::read() would be instantiated and fail to compile, as
/// Eigen matrices have no stream operators.
namespace sofa::core::objectmodel
{
template<> bool SOFA_CORE_API Data<Eigen::MatrixXf>::read( const std::string& str );
template<> std::string SOFA_CORE_API Data<Eigen::MatrixXf>::getValueString() const;

template<> bool SOFA_CORE_API Data<Eigen::MatrixXd>::read( const std::string& str );
template<> std::string SOFA_CORE_API Data<Eigen::MatrixXd>::getValueString() const;

template<> bool SOFA_CORE_API Data<Eigen::Matrix2f>::read( const std::string& str );
template<> std::string SOFA_CORE_API Data<Eigen::Matrix2f>::getValueString() const;

template<> bool SOFA_CORE_API Data<Eigen::Matrix2d>::read( const std::string& str );
template<> std::string SOFA_CORE_API Data<Eigen::Matrix2d>::getValueString() const;

template<> bool SOFA_CORE_API Data<Eigen::Matrix3f>::read( const std::string& str );
template<> std::string SOFA_CORE_API Data<Eigen::Matrix3f>::getValueString() const;

template<> bool SOFA_CORE_API Data<Eigen::Matrix3d>::read( const std::string& str );
template<> std::string SOFA_CORE_API Data<Eigen::Matrix3d>::getValueString() const;

template<> bool SOFA_CORE_API Data<Eigen::Matrix4f>::read( const std::string& str );
template<> std::string SOFA_CORE_API Data<Eigen::Matrix4f>::getValueString() const;

template<> bool SOFA_CORE_API Data<Eigen::Matrix4d>::read( const std::string& str );
template<> std::string SOFA_CORE_API Data<Eigen::Matrix4d>::getValueString() const;

Comment thread
fredroy marked this conversation as resolved.
Resolved
template<> bool SOFA_CORE_API Data<Eigen::MatrixXi>::read( const std::string& str );
template<> std::string SOFA_CORE_API Data<Eigen::MatrixXi>::getValueString() const;
} // namespace sofa::core::objectmodel
1 change: 1 addition & 0 deletions Sofa/framework/Core/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ set(SOURCE_FILES
objectmodel/BaseLink_test.cpp
objectmodel/BaseObjectDescription_test.cpp
objectmodel/Data_test.cpp
objectmodel/DataEigen_test.cpp
objectmodel/DataLink_test.cpp
objectmodel/DataFileName_test.cpp
objectmodel/DataFileNameVector_test.cpp
Expand Down
Loading
Loading