Repository navigation
[Sofa/Core] Add Data[Eigen].h #6385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+469
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
64cb3c7
Add Data[Eigen] to be able to manipulate Data<MatrixFx>.
damienmarchal c6818a1
Merge branch 'master' into pr-add-dataeigen
damienmarchal 67910ee
Generalization of the support for Eigen in Data to numerious matrix f…
damienmarchal 84cda8c
Add missing test
damienmarchal 588ad38
Merge branch 'master' into pr-add-dataeigen
damienmarchal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
|
|
||
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.