diff --git a/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Node.cpp b/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Node.cpp index 85d0de2b..deb12161 100644 --- a/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Node.cpp +++ b/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Node.cpp @@ -20,6 +20,7 @@ /// Neede to have automatic conversion from pybind types to stl container. #include #include +#include #include #include @@ -68,6 +69,18 @@ using sofapython3::PythonEnvironment; using sofa::core::objectmodel::BaseObjectDescription; +#include +using sofa::core::objectmodel::Snapshot; + +#include +using sofa::simulation::SaveSnapshotVisitor; + +#include +using sofa::simulation::LoadSnapshotVisitor; + +#include +using sofapython3::Snapshot_Python; + #include #include @@ -660,6 +673,21 @@ void sendEvent(Node* self, py::object pyUserData, char* eventName) self->propagateEvent(sofa::core::execparams::defaultInstance(), &event); } +void executeSaveSnapshotVisitor(Node* self, Snapshot_Python& snapshot) +{ + auto m_snapshot = std::make_shared(); + auto visitor = SaveSnapshotVisitor(nullptr,*m_snapshot); + self->execute(visitor); + snapshot.push_back(m_snapshot); +} + +void executeLoadSnapshotVisitor(Node* self, Snapshot_Python& snapshot, sofa::Index index) +{ + auto m_loadedsnapshot = std::make_shared(); + auto visitor = LoadSnapshotVisitor(nullptr,*snapshot.m_snapshots[index]); + self->execute(visitor); +} + py::object computeEnergy(Node* self) { sofa::simulation::mechanicalvisitor::MechanicalComputeEnergyVisitor energyVisitor(sofa::core::mechanicalparams::defaultInstance()); @@ -725,6 +753,8 @@ void moduleAddNode(py::module &m) { p.def("getMechanicalMapping", &getMechanicalMapping, sofapython3::doc::sofa::core::Node::getMechanicalMapping); p.def("sendEvent", &sendEvent, sofapython3::doc::sofa::core::Node::sendEvent); p.def("computeEnergy", &computeEnergy, sofapython3::doc::sofa::core::Node::computeEnergy); + p.def("executeSaveSnapshotVisitor", &executeSaveSnapshotVisitor); + p.def("executeLoadSnapshotVisitor", &executeLoadSnapshotVisitor); p.def("__enter__", [](py::object self) { diff --git a/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Snapshot.cpp b/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Snapshot.cpp new file mode 100644 index 00000000..0ca2b36e --- /dev/null +++ b/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Snapshot.cpp @@ -0,0 +1,41 @@ +/****************************************************************************** +* 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 * +******************************************************************************/ + +#include "Binding_Snapshot.h" +#include +#include +#include +#include + +using sofa::core::objectmodel::Snapshot; + +namespace py { using namespace pybind11; } + +namespace sofapython3 { + + void moduleAddSnapshot(py::module &m) + { + py::class_(m, "Snapshot_Python") + .def(py::init<>()) + .def("push_back", &sofapython3::Snapshot_Python::push_back) + .def("getNumberOfSnapshot",&Snapshot_Python::getNumberOfSnapshot) + .def_readwrite("m_snapshots", &Snapshot_Python::m_snapshots); + } +} \ No newline at end of file diff --git a/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Snapshot.h b/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Snapshot.h new file mode 100644 index 00000000..b567cc75 --- /dev/null +++ b/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Snapshot.h @@ -0,0 +1,49 @@ +/****************************************************************************** +* 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 +#include +using sofa::core::objectmodel::Snapshot; + +namespace sofapython3 { +class Snapshot_Python +{ +public : + std::vector> m_snapshots; + + Snapshot_Python() = default; + ~Snapshot_Python() = default; + + void push_back(const std::shared_ptr& snapshot) { + m_snapshots.push_back(snapshot); + } + + int getNumberOfSnapshot() const { + return m_snapshots.size(); + } + +}; + +void moduleAddSnapshot(pybind11::module &m); + +} /// namespace sofapython3 + diff --git a/bindings/Sofa/src/SofaPython3/Sofa/Core/CMakeLists.txt b/bindings/Sofa/src/SofaPython3/Sofa/Core/CMakeLists.txt index c688a9ea..9f4135b5 100644 --- a/bindings/Sofa/src/SofaPython3/Sofa/Core/CMakeLists.txt +++ b/bindings/Sofa/src/SofaPython3/Sofa/Core/CMakeLists.txt @@ -54,6 +54,7 @@ set(HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/Binding_BaseCamera_doc.h ${CMAKE_CURRENT_SOURCE_DIR}/Binding_Topology.h ${CMAKE_CURRENT_SOURCE_DIR}/Binding_BaseMeshTopology.h + ${CMAKE_CURRENT_SOURCE_DIR}/Binding_Snapshot.h ${CMAKE_CURRENT_SOURCE_DIR}/Binding_TaskScheduler.h ${CMAKE_CURRENT_SOURCE_DIR}/Binding_TaskScheduler_doc.h ${CMAKE_CURRENT_SOURCE_DIR}/Binding_VisualParams.h @@ -91,6 +92,7 @@ set(SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/Binding_BaseLink.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Binding_Topology.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Binding_BaseMeshTopology.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/Binding_Snapshot.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Binding_TaskScheduler.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Binding_VisualParams.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Submodule_Core.cpp diff --git a/bindings/Sofa/src/SofaPython3/Sofa/Core/Submodule_Core.cpp b/bindings/Sofa/src/SofaPython3/Sofa/Core/Submodule_Core.cpp index 390425c4..2d3fde34 100644 --- a/bindings/Sofa/src/SofaPython3/Sofa/Core/Submodule_Core.cpp +++ b/bindings/Sofa/src/SofaPython3/Sofa/Core/Submodule_Core.cpp @@ -44,6 +44,7 @@ using sofa::helper::logging::Message; #include #include #include +#include #include #include #include @@ -159,6 +160,7 @@ PYBIND11_MODULE(Core, core) moduleAddBaseMeshTopology(core); moduleAddPointSetTopologyModifier(core); moduleAddTaskScheduler(core); + moduleAddSnapshot(core); moduleAddVisualParams(core); // called when the module is unloaded