|
| 1 | +/****************************************************************************** |
| 2 | +* SOFA, Simulation Open-Framework Architecture * |
| 3 | +* (c) 2021 INRIA, USTL, UJF, CNRS, MGH * |
| 4 | +* * |
| 5 | +* This program is free software; you can redistribute it and/or modify it * |
| 6 | +* under the terms of the GNU Lesser General Public License as published by * |
| 7 | +* the Free Software Foundation; either version 2.1 of the License, or (at * |
| 8 | +* your option) any later version. * |
| 9 | +* * |
| 10 | +* This program is distributed in the hope that it will be useful, but WITHOUT * |
| 11 | +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * |
| 12 | +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * |
| 13 | +* for more details. * |
| 14 | +* * |
| 15 | +* You should have received a copy of the GNU Lesser General Public License * |
| 16 | +* along with this program. If not, see <http://www.gnu.org/licenses/>. * |
| 17 | +******************************************************************************* |
| 18 | +* Contact information: contact@sofa-framework.org * |
| 19 | +******************************************************************************/ |
| 20 | +#include <SofaPython3/Sofa/Types/Binding_CompressedRowSparseMatrix.h> |
| 21 | + |
| 22 | +#include <sofa/core/objectmodel/BaseData.h> |
| 23 | +#include <sofa/core/objectmodel/Data.h> |
| 24 | +#include <sofa/defaulttype/RigidTypes.h> |
| 25 | +#include <sofa/linearalgebra/CompressedRowSparseMatrixConstraint.h> |
| 26 | +#include <pybind11/eigen.h> |
| 27 | +#include <sofa/linearalgebra/CompressedRowSparseMatrixConstraintEigenUtils.h> |
| 28 | +#include <sofa/type/trait/is_specialization_of.h> |
| 29 | +#include <SofaPython3/PythonFactory.h> |
| 30 | + |
| 31 | + |
| 32 | +namespace py { using namespace pybind11; } |
| 33 | + |
| 34 | +namespace sofapython3 |
| 35 | +{ |
| 36 | + |
| 37 | +template<class TBlock> |
| 38 | +typename sofa::linearalgebra::CompressedRowSparseMatrixToEigenSparseVec<TBlock>::EigenSparseMatrix |
| 39 | +toEigen(const sofa::linearalgebra::CompressedRowSparseMatrixConstraint<TBlock>& matrix) |
| 40 | +{ |
| 41 | + static sofa::linearalgebra::CompressedRowSparseMatrixToEigenSparseVec<TBlock> convert; |
| 42 | + return convert(matrix); |
| 43 | +} |
| 44 | + |
| 45 | +template<class MatrixDeriv> |
| 46 | +void bindCompressedRowSparseMatrixConstraint(pybind11::module& m) |
| 47 | +{ |
| 48 | + static_assert(sofa::type::trait::is_specialization_of_v<MatrixDeriv, sofa::linearalgebra::CompressedRowSparseMatrixConstraint>, |
| 49 | + "Template argument must be a specialization of CompressedRowSparseMatrixConstraint"); |
| 50 | + |
| 51 | + py::class_<sofa::Data<MatrixDeriv>, sofa::core::objectmodel::BaseData, |
| 52 | + std::unique_ptr<sofa::Data<MatrixDeriv>, pybind11::nodelete> > crsmc(m, MatrixDeriv::Name() ); |
| 53 | + |
| 54 | + crsmc.def_property_readonly("value", [](sofa::Data<MatrixDeriv>& self) |
| 55 | + { |
| 56 | + sofa::helper::ReadAccessor accessor(self); |
| 57 | + return toEigen(accessor.ref()); |
| 58 | + }); |
| 59 | + |
| 60 | + PythonFactory::registerType(MatrixDeriv::Name(), [](sofa::core::BaseData* data) -> py::object { |
| 61 | + auto matrix = reinterpret_cast<sofa::Data<MatrixDeriv>*>(data); |
| 62 | + return py::cast(matrix); |
| 63 | + }); |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Given a template parameter pack, create the bindings of all the templates in |
| 68 | + * the pack, making sure that there are no duplicate types. |
| 69 | + */ |
| 70 | +template <typename T, typename... Args> |
| 71 | +void bindAllCompressedRowSparseMatrixConstraintTypes(pybind11::module& m) |
| 72 | +{ |
| 73 | + static_assert(!(std::is_same_v<T, Args> || ...), "Error: Duplicate types found!"); |
| 74 | + |
| 75 | + bindCompressedRowSparseMatrixConstraint<T>(m); |
| 76 | + if constexpr (sizeof...(Args) > 0) |
| 77 | + { |
| 78 | + bindAllCompressedRowSparseMatrixConstraintTypes<Args...>(m); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +void moduleAddCompressedRowSparseMatrix(pybind11::module& m) |
| 83 | +{ |
| 84 | + using namespace sofa::defaulttype; |
| 85 | + |
| 86 | + bindAllCompressedRowSparseMatrixConstraintTypes< |
| 87 | + Vec1Types::MatrixDeriv, |
| 88 | + Vec2Types::MatrixDeriv, |
| 89 | + Vec3Types::MatrixDeriv, |
| 90 | + Vec6Types::MatrixDeriv, |
| 91 | + Rigid2Types::MatrixDeriv, |
| 92 | + Rigid3Types::MatrixDeriv |
| 93 | + >(m); |
| 94 | +} |
| 95 | + |
| 96 | +} |
0 commit comments