Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/******************************************************************************
* 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 <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#include <SofaPython3/SofaDeformable/Binding_CorotationalFEMForceField.h>

#include <pybind11/numpy.h>

#include <sofa/component/solidmechanics/fem/elastic/CorotationalFEMForceField.h>
#include <sofa/defaulttype/VecTypes.h>

#include <SofaPython3/Sofa/Core/Binding_Base.h>
#include <SofaPython3/PythonFactory.h>

#include <algorithm>
#include <string>

namespace sofapython3
{
namespace py = pybind11;

template <class DataTypes, class ElementType>
void bindCorotationalFEMForceField(py::module& m, const std::string& elementName)
{
using ForceField = sofa::component::solidmechanics::fem::elastic::CorotationalFEMForceField<DataTypes, ElementType>;

const std::string pyclassName = std::string("CorotationalFEMForceField") + DataTypes::Name() + elementName;
py::class_<ForceField, sofa::core::behavior::ForceField<DataTypes>, py_shared_ptr<ForceField>> f(
m, pyclassName.c_str(), py::multiple_inheritance());

f.def("getElementRotations", [](const ForceField& self)
{
constexpr py::ssize_t dim = DataTypes::spatial_dimensions;
const auto& rotations = self.getElementRotations();
py::array_t<SReal> result({static_cast<py::ssize_t>(rotations.size()), dim, dim});
SReal* out = result.mutable_data();
for (const auto& rotation : rotations)
out = std::copy_n(rotation.ptr(), dim * dim, out);
return result;
}, "Rotation of every element, as last computed: shape (nb_elements, spatial_dimension, spatial_dimension).");

PythonFactory::registerType<ForceField>([](sofa::core::objectmodel::Base* object)
{
return py::cast(dynamic_cast<ForceField*>(object));
});
}

void moduleAddCorotationalFEMForceField(py::module& m)
{
using namespace sofa::defaulttype;
using namespace sofa::geometry;

bindCorotationalFEMForceField<Vec2Types, Triangle>(m, "Triangle");
bindCorotationalFEMForceField<Vec2Types, Quad>(m, "Quad");
bindCorotationalFEMForceField<Vec3Types, Tetrahedron>(m, "Tetrahedron");
bindCorotationalFEMForceField<Vec3Types, Hexahedron>(m, "Hexahedron");
}

} // namespace sofapython3
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/******************************************************************************
* 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 <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#pragma once

#include <pybind11/pybind11.h>

namespace sofapython3
{
void moduleAddCorotationalFEMForceField(pybind11::module& m);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ project(Bindings.Modules.SofaDeformable)

set(SOURCE_FILES
${CMAKE_CURRENT_SOURCE_DIR}/Binding_CauchyStress.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Binding_CorotationalFEMForceField.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Binding_LameParameters.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Binding_LinearSpring.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Binding_SpringForceField.cpp
Expand All @@ -10,6 +11,7 @@ set(SOURCE_FILES

set(HEADER_FILES
${CMAKE_CURRENT_SOURCE_DIR}/Binding_CauchyStress.h
${CMAKE_CURRENT_SOURCE_DIR}/Binding_CorotationalFEMForceField.h
${CMAKE_CURRENT_SOURCE_DIR}/Binding_LameParameters.h
${CMAKE_CURRENT_SOURCE_DIR}/Binding_LinearSpring.h
${CMAKE_CURRENT_SOURCE_DIR}/Binding_LinearSpring_doc.h
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <SofaPython3/SofaDeformable/Binding_SpringForceField.h>
#include <SofaPython3/SofaDeformable/Binding_LameParameters.h>
#include <SofaPython3/SofaDeformable/Binding_CauchyStress.h>
#include <SofaPython3/SofaDeformable/Binding_CorotationalFEMForceField.h>


namespace py { using namespace pybind11; }
Expand All @@ -38,6 +39,7 @@ PYBIND11_MODULE(SofaDeformable, m)
moduleAddSpringForceField(m);
moduleAddLameParameters(m);
moduleAddCauchyStress(m);
moduleAddCorotationalFEMForceField(m);
}

} // namespace sofapython3
Loading