Skip to content

Commit d9b84f8

Browse files
committed
Update example and add BaseData support for drawLines
1 parent 083e07d commit d9b84f8

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ sofa::type::vector<sofa::type::Vec3> getPoints(const py::array_t<double>& array)
5757
std::vector<sofa::type::Vec3d> points;
5858
points.resize(rows);
5959
for (size_t i = 0; i < rows; ++i)
60-
for (size_t j = 0; j < cols; ++j)
60+
for (size_t j = 0; j < 3; ++j)
6161
points[i][j] = ptr[i * cols + j];
6262

6363
return points;
@@ -89,11 +89,11 @@ void moduleAddDrawTool(py::module &m)
8989
{
9090
py::class_<DrawTool> dt(m, "DrawTool", sofapython3::doc::drawtool::baseDrawToolClass);
9191

92+
// Draw points
9293
dt.def("drawPoints", [](DrawTool *self, py::array_t<double> points, float size, sofa::type::RGBAColor& color)
9394
{
9495
self->drawPoints(getPoints(points), size, color);
9596
});
96-
9797
dt.def("drawPoints", [](DrawTool *self, BaseData* dpositions, float size, sofa::type::RGBAColor& color){
9898
auto positions = dynamic_cast<Data<sofa::type::vector<sofa::type::Vec3>>*>(dpositions);
9999
if(!positions)
@@ -102,10 +102,20 @@ void moduleAddDrawTool(py::module &m)
102102
self->drawPoints(positions->getValue(), size, color);
103103
});
104104

105+
/// Draw lines
105106
dt.def("drawLines", [](DrawTool *self, const py::array_t<double>& positions, const float size, sofa::type::RGBAColor& color){
106107
self->drawLines(getPoints(positions), size, color);
107108
});
109+
dt.def("drawLines", [](DrawTool *self, BaseData* dpositions, const float size, sofa::type::RGBAColor& color){
110+
auto positions = dynamic_cast<Data<sofa::type::vector<sofa::type::Vec3>>*>(dpositions);
111+
if(!positions)
112+
throw std::runtime_error("Invalid argument, expecting a vector<Rigid3> or vector<Vec3>, got "+dpositions->getValueTypeString());
113+
114+
self->drawLines(positions->getValue(), size, color);
115+
});
108116

117+
118+
// Draw frames
109119
dt.def("drawFrames", [](DrawTool* self,
110120
const py::array_t<double>& points,
111121
const py::array_t<double>& orientations,
@@ -118,7 +128,6 @@ void moduleAddDrawTool(py::module &m)
118128
self->drawFrame(cpoints[i], corientations[i], csize);
119129
}
120130
});
121-
122131
dt.def("drawFrames", [](DrawTool* self, BaseData* dpositions, std::array<double, 3>& size ){
123132
using sofa::defaulttype::Rigid3Types;
124133
using Coord = sofa::defaulttype::Rigid3Types::Coord;
@@ -134,6 +143,7 @@ void moduleAddDrawTool(py::module &m)
134143
}
135144
});
136145

146+
// Draw text
137147
dt.def("drawText", [](DrawTool* self,
138148
const std::array<double,3>& point,
139149
const float size,
@@ -143,6 +153,7 @@ void moduleAddDrawTool(py::module &m)
143153
self->draw3DText(point, size, color, text.c_str());
144154
});
145155

156+
// Draw overlay text
146157
dt.def("drawOverlayText", [](DrawTool* self, const std::array<double,2>& point,
147158
int fontSize, char* text, sofa::type::RGBAColor& color){
148159
self->writeOverlayText(point[0],point[1], fontSize, color, text);

examples/example-drawing-controller.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import Sofa
22
import SofaTypes
3+
import numpy
4+
from numpy import array
35
from Sofa.Types import RGBAColor
46

7+
58
class DrawingExamples(Sofa.Core.Controller):
69
def __init__(self, *args, **kwargs):
710
# These are needed (and the normal way to override from a python class)
@@ -12,7 +15,7 @@ def __init__(self, *args, **kwargs):
1215

1316
def draw(self, visual_context):
1417
dt = visual_context.getDrawTool()
15-
dt.drawPoints([[-1.5,0,-1]], 5.0, RGBAColor("red"))
18+
dt.drawPoints(array([[-1.5,-1.0,0.0]]), 5.0, RGBAColor("red"))
1619
dt.drawPoints([[-1.3,0,-1], [1.3,0,-1]], 10.0, RGBAColor("green"))
1720
dt.drawLines([[-1.3,0,-1], [1.3,0,-1]], 1.0, RGBAColor("green"))
1821
dt.drawFrames([[-1.5,0.1,-1]], [[0.0,0,0,1.0]], [0.1,0.1,0.1])

0 commit comments

Comments
 (0)