Skip to content

Commit eda0846

Browse files
committed
Separate DrawTool from VisualParam
1 parent 100906c commit eda0846

File tree

7 files changed

+205
-55
lines changed

7 files changed

+205
-55
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/******************************************************************************
2+
* SOFA, Simulation Open-Framework Architecture *
3+
* (c) 2021 INRIA, USTL, UJF, CNRS, MGH *
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+
21+
#include <sofa/type/Quat.h>
22+
#include <pybind11/pybind11.h>
23+
#include <pybind11/pytypes.h>
24+
#include <pybind11/stl.h>
25+
26+
#include <SofaPython3/Sofa/Core/Binding_Base.h>
27+
#include <sofa/core/visual/VisualParams.h>
28+
29+
#include <SofaPython3/Sofa/Core/Binding_DrawTool.h>
30+
#include <SofaPython3/Sofa/Core/Binding_DrawTool_doc.h>
31+
32+
#include <SofaPython3/PythonFactory.h>
33+
#include <sofa/core/objectmodel/Data.h>
34+
#include <sofa/type/RGBAColor.h>
35+
36+
namespace py { using namespace pybind11; }
37+
using sofa::core::objectmodel::BaseData;
38+
using sofa::core::objectmodel::Data;
39+
using sofa::core::objectmodel::BaseObject;
40+
using sofa::core::visual::VisualParams;
41+
using sofa::core::visual::DrawTool;
42+
43+
namespace sofapython3 {
44+
45+
sofa::type::vector<sofa::type::Vec3> getPoints(const py::array_t<double>& array)
46+
{
47+
py::buffer_info buf = array.request();
48+
49+
if (buf.ndim != 2)
50+
throw std::runtime_error("Invalid argument, expecting an array with ndim=2");
51+
52+
size_t rows = buf.shape[0];
53+
size_t cols = buf.shape[1];
54+
55+
double* ptr = static_cast<double*>(buf.ptr);
56+
57+
std::vector<sofa::type::Vec3d> points;
58+
points.resize(rows);
59+
for (size_t i = 0; i < rows; ++i)
60+
for (size_t j = 0; j < cols; ++j)
61+
points[i][j] = ptr[i * cols + j];
62+
63+
return points;
64+
}
65+
66+
67+
void moduleAddDrawTool(py::module &m)
68+
{
69+
py::class_<DrawTool> dt(m, "DrawTool", sofapython3::doc::drawtool::baseDrawToolClass);
70+
71+
// Draw points from vectors...
72+
dt.def("drawPoints", [](DrawTool *self, py::array_t<double> points, float size, sofa::type::RGBAColor& color)
73+
{
74+
self->drawPoints(getPoints(points), size, color);
75+
});
76+
77+
// Draw points from a base data that can be casted to a vector of Vec3
78+
dt.def("drawPoints", [](DrawTool *self, BaseData* dpositions, float size ){
79+
auto positions = dynamic_cast<Data<sofa::type::vector<sofa::type::Vec3>>*>(dpositions);
80+
if(!positions)
81+
throw std::runtime_error("Invalid argument, a base data of type vector<Vec3> was expected, got "+dpositions->getValueTypeString());
82+
83+
self->drawPoints(positions->getValue(), size, sofa::type::RGBAColor::white());
84+
});
85+
86+
dt.def("drawLines", [](DrawTool *self, const std::vector<sofa::type::Vec3> &points, float size ){
87+
self->drawLines(points, size, sofa::type::RGBAColor::white());
88+
});
89+
90+
91+
dt.def("drawFrames", [](DrawTool* self,
92+
const std::vector<sofa::type::Vec3d>& points,
93+
const std::vector<sofa::type::Quatd>& orientations,
94+
const sofa::type::Vec3& size ){
95+
for(unsigned int i=0;i<points.size();i++)
96+
{
97+
self->drawFrame(points[i], orientations[i], size);
98+
}
99+
});
100+
dt.def("drawFrames", [](DrawTool* self, BaseData* dpositions, const sofa::type::Vec3& size ){
101+
using sofa::defaulttype::Rigid3Types;
102+
using Coord = sofa::defaulttype::Rigid3Types::Coord;
103+
auto positions = dynamic_cast<Data<sofa::type::vector<Coord>>*>(dpositions);
104+
if(!positions)
105+
throw std::runtime_error("Invalid argument");
106+
107+
for(auto& position : positions->getValue())
108+
{
109+
self->drawFrame(Rigid3Types::getCPos(position),
110+
Rigid3Types::getCRot(position), size);
111+
}
112+
});
113+
dt.def("drawText", [](DrawTool* self,
114+
const sofa::type::Vec3d& point,
115+
const float size,
116+
const std::string& text)
117+
{
118+
self->draw3DText(point, size, sofa::type::RGBAColor::white(), text.c_str());
119+
});
120+
121+
dt.def("drawOverlayText", [](DrawTool* self, int x, int y, int fontSize, char* text){
122+
self->writeOverlayText(x,y, fontSize, sofa::type::RGBAColor::white(), text);
123+
});
124+
}
125+
126+
} /// namespace sofapython3
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/******************************************************************************
2+
* SOFA, Simulation Open-Framework Architecture *
3+
* (c) 2021 INRIA, USTL, UJF, CNRS, MGH *
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 moduleAddDrawTool(pybind11::module &m);
27+
28+
} /// namespace sofapython3
29+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/******************************************************************************
2+
* SOFA, Simulation Open-Framework Architecture *
3+
* (c) 2021 INRIA, USTL, UJF, CNRS, MGH *
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+
21+
#pragma once
22+
23+
namespace sofapython3::doc::drawtool {
24+
25+
static auto baseDrawToolClass =
26+
R"(DrawTool is a wrapper for low-level rendering draw calls.
27+
It provides higher-level drawing functions like drawing lines, points, spheres, arrows, etc., without
28+
needing to write raw OpenGL each time.)";
29+
30+
} // namespace sofapython3::doc::drawtool

bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_VisualParams.cpp

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -34,67 +34,16 @@
3434
#include <sofa/type/RGBAColor.h>
3535

3636
namespace py { using namespace pybind11; }
37-
using sofa::core::objectmodel::BaseData;
38-
using sofa::core::objectmodel::Data;
39-
using sofa::core::objectmodel::BaseObject;
4037
using sofa::core::visual::VisualParams;
41-
using sofa::core::visual::DrawTool;
4238

4339
namespace sofapython3 {
4440

4541
void moduleAddVisualParams(py::module &m)
4642
{
47-
py::class_<VisualParams> vp(m, "VisualParams", sofapython3::doc::visualParams::baseVisualParamsClass);
43+
py::class_<VisualParams> vp(m, "VisualParams", sofapython3::doc::visualparams::baseVisualParamsClass);
44+
4845
vp.def("getDrawTool", [](VisualParams *self){ return self->drawTool(); },
4946
pybind11::return_value_policy::reference);
50-
51-
py::class_<DrawTool> dt(m, "DrawTool", sofapython3::doc::visualParams::baseVisualParamsClass);
52-
dt.def("drawPoints", [](DrawTool *self, const std::vector<sofa::type::Vec3> &points, float size ){
53-
self->drawPoints(points, size, sofa::type::RGBAColor::white());
54-
});
55-
dt.def("drawPoints", [](DrawTool *self, BaseData* dpositions, float size ){
56-
auto positions = dynamic_cast<Data<sofa::type::vector<sofa::type::Vec3>>*>(dpositions);
57-
if(!positions)
58-
throw std::runtime_error("Invalid argument");
59-
60-
self->drawPoints(positions->getValue(), size, sofa::type::RGBAColor::white());
61-
});
62-
dt.def("drawLines", [](DrawTool *self, const std::vector<sofa::type::Vec3> &points, float size ){
63-
self->drawLines(points, size, sofa::type::RGBAColor::white());
64-
});
65-
dt.def("drawFrames", [](DrawTool* self,
66-
const std::vector<sofa::type::Vec3d>& points,
67-
const std::vector<sofa::type::Quatd>& orientations,
68-
const sofa::type::Vec3& size ){
69-
for(unsigned int i=0;i<points.size();i++)
70-
{
71-
self->drawFrame(points[i], orientations[i], size);
72-
}
73-
});
74-
dt.def("drawFrames", [](DrawTool* self, BaseData* dpositions, const sofa::type::Vec3& size ){
75-
using sofa::defaulttype::Rigid3Types;
76-
using Coord = sofa::defaulttype::Rigid3Types::Coord;
77-
auto positions = dynamic_cast<Data<sofa::type::vector<Coord>>*>(dpositions);
78-
if(!positions)
79-
throw std::runtime_error("Invalid argument");
80-
81-
for(auto& position : positions->getValue())
82-
{
83-
self->drawFrame(Rigid3Types::getCPos(position),
84-
Rigid3Types::getCRot(position), size);
85-
}
86-
});
87-
dt.def("draw3DText", [](DrawTool* self,
88-
const sofa::type::Vec3d& point,
89-
const float size,
90-
const std::string& text)
91-
{
92-
self->draw3DText(point, size, sofa::type::RGBAColor::white(), text.c_str());
93-
});
94-
95-
dt.def("drawText", [](DrawTool* self, int x, int y, int fontSize, char* text){
96-
self->writeOverlayText(x,y, fontSize, sofa::type::RGBAColor::white(), text);
97-
});
9847
}
9948

10049
} /// namespace sofapython3

bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_VisualParams_doc.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,22 @@
2020

2121
#pragma once
2222

23-
namespace sofapython3::doc::visualParams {
23+
namespace sofapython3::doc::visualparams {
2424

2525
static auto baseVisualParamsClass =
2626
R"(
27-
TBD
27+
VisualParams is a component that manages and provides rendering-related parameters for the simulation's objected.
28+
It determine draw distance, access the camera position or viewing frustum and handle display flags
29+
like wireframe, texture, lighting as well as provide a drawTool object that can be used to render things.
30+
31+
Example:
32+
class MyController(Sofa.Core.Controller):
33+
def __init__(self, *args, *kwargs):
34+
Sofa.Core.Controller.__init__(self, *args, **kwargs)
35+
36+
def draw(self, visual_params):
37+
dt = visual_param.getDrawTool()
38+
dt.drawPoint([0,0,0])
2839
)";
2940

3041
} // namespace sofapython3::doc::visualParams

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ set(HEADER_FILES
1919
${CMAKE_CURRENT_SOURCE_DIR}/Binding_Controller_doc.h
2020
${CMAKE_CURRENT_SOURCE_DIR}/Binding_DataEngine.h
2121
${CMAKE_CURRENT_SOURCE_DIR}/Binding_DataEngine_doc.h
22+
${CMAKE_CURRENT_SOURCE_DIR}/Binding_DrawTool.h
23+
${CMAKE_CURRENT_SOURCE_DIR}/Binding_DrawTool_doc.h
2224
${CMAKE_CURRENT_SOURCE_DIR}/Binding_ForceField.h
2325
${CMAKE_CURRENT_SOURCE_DIR}/Binding_ForceField_doc.h
2426
${CMAKE_CURRENT_SOURCE_DIR}/Binding_LinkPath.h
@@ -60,6 +62,7 @@ set(SOURCE_FILES
6062
${CMAKE_CURRENT_SOURCE_DIR}/Binding_BaseClass.cpp
6163
${CMAKE_CURRENT_SOURCE_DIR}/Binding_BaseData.cpp
6264
${CMAKE_CURRENT_SOURCE_DIR}/Binding_DataDict.cpp
65+
${CMAKE_CURRENT_SOURCE_DIR}/Binding_DrawTool.cpp
6366
${CMAKE_CURRENT_SOURCE_DIR}/Binding_BaseObject.cpp
6467
${CMAKE_CURRENT_SOURCE_DIR}/Binding_BaseCamera.cpp
6568
${CMAKE_CURRENT_SOURCE_DIR}/Binding_BaseContext.cpp

bindings/Sofa/src/SofaPython3/Sofa/Core/Submodule_Core.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ using sofa::helper::logging::Message;
2727
#include <SofaPython3/Sofa/Core/Binding_BaseContext.h>
2828
#include <SofaPython3/Sofa/Core/Binding_BaseObject.h>
2929
#include <SofaPython3/Sofa/Core/Binding_DataDict.h>
30+
#include <SofaPython3/Sofa/Core/Binding_DrawTool.h>
3031
#include <SofaPython3/Sofa/Core/Binding_BaseData.h>
3132
#include <SofaPython3/Sofa/Core/Binding_BaseCamera.h>
3233
#include <SofaPython3/Sofa/Core/Binding_ForceField.h>
@@ -136,6 +137,7 @@ PYBIND11_MODULE(Core, core)
136137
moduleAddDataString(core);
137138
moduleAddDataLink(core);
138139
moduleAddDataVectorString(core);
140+
moduleAddDrawTool(core);
139141
moduleAddBaseObject(core);
140142
moduleAddBaseCamera(core);
141143
moduleAddContactListener(core);

0 commit comments

Comments
 (0)