Skip to content

Commit 5ae42ed

Browse files
authored
Introduce bindings for SceneCheck (sofa-framework#508)
1 parent 72ff387 commit 5ae42ed

File tree

9 files changed

+245
-0
lines changed

9 files changed

+245
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/******************************************************************************
2+
* SofaPython3 plugin *
3+
* (c) 2021 CNRS, University of Lille, INRIA *
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: [email protected] *
19+
******************************************************************************/
20+
#include <SofaPython3/Sofa/Simulation/Binding_SceneCheck.h>
21+
#include <SofaPython3/Sofa/Simulation/Binding_SceneCheck_doc.h>
22+
#include <sofa/simulation/SceneCheck.h>
23+
24+
namespace sofapython3
25+
{
26+
27+
namespace py { using namespace pybind11; }
28+
29+
void moduleAddSceneCheck(pybind11::module &m)
30+
{
31+
// create a python binding for the C++ class LinearSpring from SofaDeformable
32+
py::class_<sofa::simulation::SceneCheck, std::shared_ptr<sofa::simulation::SceneCheck>> s
33+
(m, "SceneCheck", sofapython3::doc::simulation::SceneCheckClass);
34+
35+
s.def("getName", &sofa::simulation::SceneCheck::getName, sofapython3::doc::simulation::SceneCheck_getName);
36+
s.def("getDesc", &sofa::simulation::SceneCheck::getDesc, sofapython3::doc::simulation::SceneCheck_getDesc);
37+
}
38+
39+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/******************************************************************************
2+
* SofaPython3 plugin *
3+
* (c) 2021 CNRS, University of Lille, INRIA *
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: [email protected] *
19+
******************************************************************************/
20+
#pragma once
21+
22+
#include <pybind11/pybind11.h>
23+
24+
namespace sofapython3 {
25+
26+
void moduleAddSceneCheck(pybind11::module& m);
27+
28+
} // namespace sofapython3
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/******************************************************************************
2+
* SofaPython3 plugin *
3+
* (c) 2021 CNRS, University of Lille, INRIA *
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: [email protected] *
19+
******************************************************************************/
20+
#include <SofaPython3/Sofa/Simulation/Binding_SceneCheckMainRegistry.h>
21+
#include <SofaPython3/Sofa/Simulation/Binding_SceneCheckMainRegistry_doc.h>
22+
#include <sofa/simulation/SceneCheckMainRegistry.h>
23+
#include <pybind11/stl.h>
24+
25+
namespace sofapython3
26+
{
27+
28+
namespace py { using namespace pybind11; }
29+
30+
void moduleAddSceneCheckMainRegistry(pybind11::module &m)
31+
{
32+
py::class_<sofa::simulation::SceneCheckMainRegistry> registry (m, "SceneCheckMainRegistry",
33+
sofapython3::doc::simulation::SceneCheckMainRegistryClass);
34+
35+
registry.def_static("getRegisteredSceneChecks",
36+
[]() {
37+
return std::vector(sofa::simulation::SceneCheckMainRegistry::getRegisteredSceneChecks());
38+
},
39+
sofapython3::doc::simulation::SceneCheckMainRegistry_getRegisteredSceneChecks);
40+
}
41+
42+
} // namespace sofapython3
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/******************************************************************************
2+
* SofaPython3 plugin *
3+
* (c) 2021 CNRS, University of Lille, INRIA *
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: [email protected] *
19+
******************************************************************************/
20+
#pragma once
21+
22+
#include <pybind11/pybind11.h>
23+
24+
namespace sofapython3 {
25+
26+
void moduleAddSceneCheckMainRegistry(pybind11::module& m);
27+
28+
} // namespace sofapython3
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/******************************************************************************
2+
* SofaPython3 plugin *
3+
* (c) 2021 CNRS, University of Lille, INRIA *
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: [email protected] *
19+
******************************************************************************/
20+
#pragma once
21+
22+
namespace sofapython3::doc::simulation
23+
{
24+
25+
static auto SceneCheckMainRegistryClass = "The main registry containing verification checks on a scene.";
26+
static auto SceneCheckMainRegistry_getRegisteredSceneChecks = "Returns all the verification checks registered into the main registry.";
27+
28+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/******************************************************************************
2+
* SofaPython3 plugin *
3+
* (c) 2021 CNRS, University of Lille, INRIA *
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: [email protected] *
19+
******************************************************************************/
20+
#pragma once
21+
22+
namespace sofapython3::doc::simulation
23+
{
24+
25+
static auto SceneCheckClass = "A verification check on a scene.";
26+
27+
static auto SceneCheck_getName = "Returns the name of the verification check.";
28+
static auto SceneCheck_getDesc = "Returns the description of the verification check.";
29+
30+
}

bindings/Sofa/src/SofaPython3/Sofa/Simulation/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@ project(Bindings.Sofa.Simulation)
22

33
set(HEADER_FILES
44
${CMAKE_CURRENT_SOURCE_DIR}/Submodule_Simulation_doc.h
5+
${CMAKE_CURRENT_SOURCE_DIR}/Binding_SceneCheck.h
6+
${CMAKE_CURRENT_SOURCE_DIR}/Binding_SceneCheck_doc.h
7+
${CMAKE_CURRENT_SOURCE_DIR}/Binding_SceneCheckMainRegistry.h
8+
${CMAKE_CURRENT_SOURCE_DIR}/Binding_SceneCheckMainRegistry_doc.h
59
)
610

711
set(SOURCE_FILES
812
${CMAKE_CURRENT_SOURCE_DIR}/Submodule_Simulation.cpp
13+
${CMAKE_CURRENT_SOURCE_DIR}/Binding_SceneCheck.cpp
14+
${CMAKE_CURRENT_SOURCE_DIR}/Binding_SceneCheckMainRegistry.cpp
915
)
1016

1117
if (NOT TARGET SofaPython3::Plugin)

bindings/Sofa/src/SofaPython3/Sofa/Simulation/Submodule_Simulation.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ using sofa::simulation::Simulation;
3939
#include <sofa/core/visual/VisualParams.h>
4040
#include <sofa/core/visual/VisualLoop.h>
4141
#include <SofaPython3/Sofa/Simulation/Submodule_Simulation_doc.h>
42+
#include <SofaPython3/Sofa/Simulation/Binding_SceneCheck.h>
43+
#include <SofaPython3/Sofa/Simulation/Binding_SceneCheckMainRegistry.h>
4244

4345
#include <sofa/core/init.h>
4446
#include <sofa/simulation/init.h>
@@ -59,6 +61,9 @@ PYBIND11_MODULE(Simulation, simulation)
5961

6062
simulation.doc() =sofapython3::doc::simulation::Class;
6163

64+
moduleAddSceneCheck(simulation);
65+
moduleAddSceneCheckMainRegistry(simulation);
66+
6267
simulation.def("print", [](Node* n){ sofa::simulation::node::print(n); }, sofapython3::doc::simulation::print);
6368
simulation.def("animate", [](Node* n, SReal dt=0.0){ sofa::simulation::node::animate(n, dt); },sofapython3::doc::simulation::animate);
6469
simulation.def("init", [](Node* n){ sofa::simulation::node::init(n); }, sofapython3::doc::simulation::init);

examples/scene_check.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import Sofa
2+
import SofaRuntime
3+
4+
def main():
5+
# Make sure to load all necessary libraries
6+
SofaRuntime.importPlugin("Sofa.Component.StateContainer")
7+
# This is important to register the scene checks contained in this plugin into the registry
8+
SofaRuntime.importPlugin("SceneChecking")
9+
10+
for scene_check in Sofa.Simulation.SceneCheckMainRegistry.getRegisteredSceneChecks():
11+
print(f"{scene_check.getName()}: {scene_check.getDesc()}")
12+
13+
# Call the above function to create the scene graph
14+
root = Sofa.Core.Node("root")
15+
createScene(root)
16+
17+
# Once defined, initialization of the scene graph
18+
Sofa.Simulation.initRoot(root)
19+
20+
# Function called when the scene graph is being created
21+
def createScene(root):
22+
23+
root.addObject('RequiredPlugin', name='Sofa.Component.StateContainer')
24+
25+
# Scene must now include a AnimationLoop
26+
root.addObject('DefaultAnimationLoop')
27+
28+
# Add new nodes and objects in the scene
29+
node1 = root.addChild("Node1")
30+
node2 = root.addChild("Node2")
31+
32+
node1.addObject("MechanicalObject", template="Rigid3d", position="0 0 0 0 0 0 1", showObject="1")
33+
34+
node2.addObject("MechanicalObject", template="Rigid3d", position="1 1 1 0 0 0 1", showObject="1")
35+
36+
return root
37+
38+
if __name__ == '__main__':
39+
main()

0 commit comments

Comments
 (0)