From 6357d5374d5ede832c900991ae9dbb25853e6e2c Mon Sep 17 00:00:00 2001 From: Alex Bilger Date: Wed, 17 Jun 2026 15:04:57 +0200 Subject: [PATCH] =?UTF-8?q?add=20binding=20to=20convert=20elastic=20parame?= =?UTF-8?q?ters=20to=20Lam=C3=A9's=20parameters?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SofaDeformable/Binding_LameParameters.cpp | 60 +++++++++++++++++++ .../SofaDeformable/Binding_LameParameters.h | 30 ++++++++++ .../SofaPython3/SofaDeformable/CMakeLists.txt | 7 ++- .../SofaDeformable/Module_SofaDeformable.cpp | 2 + 4 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 bindings/Modules/src/SofaPython3/SofaDeformable/Binding_LameParameters.cpp create mode 100644 bindings/Modules/src/SofaPython3/SofaDeformable/Binding_LameParameters.h diff --git a/bindings/Modules/src/SofaPython3/SofaDeformable/Binding_LameParameters.cpp b/bindings/Modules/src/SofaPython3/SofaDeformable/Binding_LameParameters.cpp new file mode 100644 index 000000000..313a04600 --- /dev/null +++ b/bindings/Modules/src/SofaPython3/SofaDeformable/Binding_LameParameters.cpp @@ -0,0 +1,60 @@ +/****************************************************************************** + * SofaPython3 plugin * + * (c) 2021 CNRS, University of Lille, INRIA * + * * + * 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 . * + ******************************************************************************* + * Contact information: contact@sofa-framework.org * + ******************************************************************************/ + +#include + +#include +#include + +namespace sofapython3 +{ + +void moduleAddLameParameters(pybind11::module &m) +{ + using namespace sofa::component::solidmechanics::fem::elastic; + + m.def( + "toLameParameters2D", + [](SReal youngModulus, SReal poissonRatio) { + LameLambda lambda{0}; + LameMu mu{0}; + toLameParameters<2>(YoungModulus(youngModulus), + PoissonRatio(poissonRatio), lambda, mu); + return std::make_pair(mu.get(), lambda.get()); + }, + pybind11::arg("youngModulus"), pybind11::arg("poissonRatio"), + "Converts Young's modulus and Poisson's ratio to Lamé parameters (mu, " + "lambda) for 2D."); + + m.def( + "toLameParameters3D", + [](SReal youngModulus, SReal poissonRatio) { + LameLambda lambda{0}; + LameMu mu{0}; + toLameParameters<3>(YoungModulus(youngModulus), + PoissonRatio(poissonRatio), lambda, mu); + return std::make_pair(mu.get(), lambda.get()); + }, + pybind11::arg("youngModulus"), pybind11::arg("poissonRatio"), + "Converts Young's modulus and Poisson's ratio to Lamé parameters (mu, " + "lambda) for 3D."); +} + +} // namespace sofapython3 diff --git a/bindings/Modules/src/SofaPython3/SofaDeformable/Binding_LameParameters.h b/bindings/Modules/src/SofaPython3/SofaDeformable/Binding_LameParameters.h new file mode 100644 index 000000000..f79835c5b --- /dev/null +++ b/bindings/Modules/src/SofaPython3/SofaDeformable/Binding_LameParameters.h @@ -0,0 +1,30 @@ +/****************************************************************************** +* SOFA, Simulation Open-Framework Architecture * +* (c) 2021 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 . * +******************************************************************************* +* Contact information: contact@sofa-framework.org * +******************************************************************************/ + +#pragma once + +#include + +namespace sofapython3 +{ + +void moduleAddLameParameters(pybind11::module& m); + +} // namespace sofapython3 diff --git a/bindings/Modules/src/SofaPython3/SofaDeformable/CMakeLists.txt b/bindings/Modules/src/SofaPython3/SofaDeformable/CMakeLists.txt index 5c818b9d6..26b5c7ebd 100644 --- a/bindings/Modules/src/SofaPython3/SofaDeformable/CMakeLists.txt +++ b/bindings/Modules/src/SofaPython3/SofaDeformable/CMakeLists.txt @@ -1,12 +1,14 @@ project(Bindings.Modules.SofaDeformable) set(SOURCE_FILES - ${CMAKE_CURRENT_SOURCE_DIR}/Module_SofaDeformable.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/Binding_LameParameters.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Binding_LinearSpring.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Binding_SpringForceField.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/Module_SofaDeformable.cpp ) set(HEADER_FILES + ${CMAKE_CURRENT_SOURCE_DIR}/Binding_LameParameters.h ${CMAKE_CURRENT_SOURCE_DIR}/Binding_LinearSpring.h ${CMAKE_CURRENT_SOURCE_DIR}/Binding_LinearSpring_doc.h ${CMAKE_CURRENT_SOURCE_DIR}/Binding_SpringForceField.h @@ -18,6 +20,7 @@ if (NOT TARGET SofaPython3::Plugin) endif() sofa_find_package(Sofa.Component.SolidMechanics.Spring REQUIRED) +sofa_find_package(Sofa.Component.SolidMechanics.FEM.Elastic REQUIRED) SP3_add_python_module( TARGET ${PROJECT_NAME} @@ -26,6 +29,6 @@ SP3_add_python_module( DESTINATION Sofa SOURCES ${SOURCE_FILES} HEADERS ${HEADER_FILES} - DEPENDS Sofa.Component.SolidMechanics.Spring SofaPython3::Plugin SofaPython3::Bindings.Sofa.Core + DEPENDS Sofa.Component.SolidMechanics.Spring Sofa.Component.SolidMechanics.FEM.Elastic SofaPython3::Plugin SofaPython3::Bindings.Sofa.Core ) diff --git a/bindings/Modules/src/SofaPython3/SofaDeformable/Module_SofaDeformable.cpp b/bindings/Modules/src/SofaPython3/SofaDeformable/Module_SofaDeformable.cpp index 0fc446e85..c99c636e0 100644 --- a/bindings/Modules/src/SofaPython3/SofaDeformable/Module_SofaDeformable.cpp +++ b/bindings/Modules/src/SofaPython3/SofaDeformable/Module_SofaDeformable.cpp @@ -21,6 +21,7 @@ #include #include #include +#include namespace py { using namespace pybind11; } @@ -34,6 +35,7 @@ PYBIND11_MODULE(SofaDeformable, m) moduleAddLinearSpring(m); moduleAddSpringForceField(m); + moduleAddLameParameters(m); } } // namespace sofapython3