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
30 changes: 30 additions & 0 deletions bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
/// Neede to have automatic conversion from pybind types to stl container.
#include <pybind11/stl.h>
#include <pybind11/numpy.h>
#include <iostream>

#include <sofa/simulation/Simulation.h>
#include <sofa/simulation/mechanicalvisitor/MechanicalComputeEnergyVisitor.h>
Expand Down Expand Up @@ -68,6 +69,18 @@ using sofapython3::PythonEnvironment;

using sofa::core::objectmodel::BaseObjectDescription;

#include <sofa/core/objectmodel/Snapshot.h>
using sofa::core::objectmodel::Snapshot;

#include <sofa/simulation/SaveSnapshotVisitor.h>
using sofa::simulation::SaveSnapshotVisitor;

#include <sofa/simulation/LoadSnapshotVisitor.h>
using sofa::simulation::LoadSnapshotVisitor;

#include <SofaPython3/Sofa/Core/Binding_Snapshot.h>
using sofapython3::Snapshot_Python;

#include <queue>
#include <sofa/core/objectmodel/Link.h>

Expand Down Expand Up @@ -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<sofa::core::objectmodel::Snapshot>();
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<sofa::core::objectmodel::Snapshot>();
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());
Expand Down Expand Up @@ -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)
{
Expand Down
41 changes: 41 additions & 0 deletions bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Snapshot.cpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Contact information: contact@sofa-framework.org *
******************************************************************************/

#include "Binding_Snapshot.h"
#include <pybind11/pybind11.h>
#include <pybind11/pytypes.h>
#include <SofaPython3/Sofa/Core/Binding_Snapshot.h>
#include <SofaPython3/PythonFactory.h>

using sofa::core::objectmodel::Snapshot;

namespace py { using namespace pybind11; }

namespace sofapython3 {

void moduleAddSnapshot(py::module &m)
{
py::class_<Snapshot_Python>(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);
}
}
49 changes: 49 additions & 0 deletions bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Snapshot.h
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Contact information: contact@sofa-framework.org *
******************************************************************************/

#pragma once

#include <pybind11/pybind11.h>
#include <sofa/core/objectmodel/Snapshot.h>
using sofa::core::objectmodel::Snapshot;

namespace sofapython3 {
class Snapshot_Python
{
public :
std::vector<std::shared_ptr<Snapshot>> m_snapshots;

Snapshot_Python() = default;
~Snapshot_Python() = default;

void push_back(const std::shared_ptr<Snapshot>& snapshot) {
m_snapshots.push_back(snapshot);
}

int getNumberOfSnapshot() const {
return m_snapshots.size();
}

};

void moduleAddSnapshot(pybind11::module &m);

} /// namespace sofapython3

2 changes: 2 additions & 0 deletions bindings/Sofa/src/SofaPython3/Sofa/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions bindings/Sofa/src/SofaPython3/Sofa/Core/Submodule_Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ using sofa::helper::logging::Message;
#include <SofaPython3/Sofa/Core/Binding_NodeIterator.h>
#include <SofaPython3/Sofa/Core/Binding_Prefab.h>
#include <SofaPython3/Sofa/Core/Binding_BaseLink.h>
#include <SofaPython3/Sofa/Core/Binding_Snapshot.h>
#include <SofaPython3/Sofa/Core/Binding_PointSetTopologyModifier.h>
#include <SofaPython3/Sofa/Core/Binding_PythonScriptEvent.h>
#include <SofaPython3/Sofa/Core/Binding_Topology.h>
Expand Down Expand Up @@ -159,6 +160,7 @@ PYBIND11_MODULE(Core, core)
moduleAddBaseMeshTopology(core);
moduleAddPointSetTopologyModifier(core);
moduleAddTaskScheduler(core);
moduleAddSnapshot(core);
moduleAddVisualParams(core);

// called when the module is unloaded
Expand Down