|
| 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/SofaConstraintSolver/Binding_ConstraintSolver.h> |
| 21 | +#include <SofaPython3/SofaConstraintSolver/Binding_ConstraintSolver_doc.h> |
| 22 | +#include <pybind11/pybind11.h> |
| 23 | +#include <pybind11/eigen.h> |
| 24 | + |
| 25 | +#include <SofaPython3/Sofa/Core/Binding_Base.h> |
| 26 | +#include <SofaPython3/PythonFactory.h> |
| 27 | + |
| 28 | +#include <sofa/component/constraint/lagrangian/solver/ConstraintSolverImpl.h> |
| 29 | + |
| 30 | +namespace py { using namespace pybind11; } |
| 31 | + |
| 32 | +namespace sofapython3 { |
| 33 | + |
| 34 | +using sofa::component::constraint::lagrangian::solver::ConstraintSolverImpl; |
| 35 | +using EigenDenseMatrix = Eigen::Matrix<SReal, Eigen::Dynamic, Eigen::Dynamic>; |
| 36 | +using EigenMatrixMap = Eigen::Map<EigenDenseMatrix>; |
| 37 | + |
| 38 | +void moduleAddConstraintSolver(py::module &m) |
| 39 | +{ |
| 40 | + const auto typeName = ConstraintSolverImpl::GetClass()->className; |
| 41 | + py::class_<ConstraintSolverImpl, |
| 42 | + sofa::core::objectmodel::BaseObject, |
| 43 | + sofapython3::py_shared_ptr<ConstraintSolverImpl> > c(m, typeName.c_str(), sofapython3::doc::constraintsolver::constraintSolverClass); |
| 44 | + |
| 45 | + c.def("W", [](ConstraintSolverImpl& self) -> EigenMatrixMap |
| 46 | + { |
| 47 | + assert(self.getConstraintProblem()); |
| 48 | + auto& W_matrix = self.getConstraintProblem()->W; |
| 49 | + return { W_matrix.ptr(), W_matrix.rows(), W_matrix.cols()}; |
| 50 | + }, sofapython3::doc::constraintsolver::constraintSolver_W); |
| 51 | + |
| 52 | + c.def("lambda_force", [](ConstraintSolverImpl& self) -> Eigen::Map<Eigen::Matrix<SReal, Eigen::Dynamic, 1> > |
| 53 | + { |
| 54 | + assert(self.getConstraintProblem()); |
| 55 | + auto& lambda = self.getConstraintProblem()->f; |
| 56 | + return { lambda.ptr(), lambda.size()}; |
| 57 | + }, sofapython3::doc::constraintsolver::constraintSolver_lambda); |
| 58 | + |
| 59 | + c.def("dfree", [](ConstraintSolverImpl& self) -> Eigen::Map<Eigen::Matrix<SReal, Eigen::Dynamic, 1> > |
| 60 | + { |
| 61 | + assert(self.getConstraintProblem()); |
| 62 | + auto& dfree = self.getConstraintProblem()->dFree; |
| 63 | + return { dfree.ptr(), dfree.size()}; |
| 64 | + }, sofapython3::doc::constraintsolver::constraintSolver_dfree); |
| 65 | + |
| 66 | + /// register the binding in the downcasting subsystem |
| 67 | + PythonFactory::registerType<ConstraintSolverImpl>([](sofa::core::objectmodel::Base* object) |
| 68 | + { |
| 69 | + return py::cast(dynamic_cast<ConstraintSolverImpl*>(object)); |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | +} |
0 commit comments