Skip to content

Commit 81cff26

Browse files
committed
Add enable/disableLighting, drawDisk, drawCircle, drawTriangles, drawQuads
1 parent d9b84f8 commit 81cff26

File tree

2 files changed

+98
-3
lines changed

2 files changed

+98
-3
lines changed

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

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include <SofaPython3/Sofa/Core/Binding_DrawTool.h>
3030
#include <SofaPython3/Sofa/Core/Binding_DrawTool_doc.h>
3131

32+
#include <sofa/core/topology/BaseMeshTopology.h>
33+
3234
#include <SofaPython3/PythonFactory.h>
3335
#include <sofa/core/objectmodel/Data.h>
3436
#include <sofa/type/RGBAColor.h>
@@ -114,6 +116,85 @@ void moduleAddDrawTool(py::module &m)
114116
self->drawLines(positions->getValue(), size, color);
115117
});
116118

119+
// Draw disk
120+
dt.def("drawDisk", [](DrawTool *self, float radius, double from, double to, int resolution, sofa::type::RGBAColor& color) {
121+
self->drawDisk(radius, from, to, resolution, color);
122+
});
123+
dt.def("drawCircle", [](DrawTool *self, float radius, float lineThickness, int resolution, sofa::type::RGBAColor& color) {
124+
self->drawCircle(radius, lineThickness, resolution, color);
125+
});
126+
127+
// Draw mesh
128+
dt.def("drawTriangles", [](DrawTool *self, BaseData* dpositions, BaseData* dtriangles, sofa::type::RGBAColor& color){
129+
auto positions = dynamic_cast<Data<sofa::type::vector<sofa::type::Vec3d>>*>(dpositions);
130+
if(!positions)
131+
throw std::runtime_error("Invalid argument, expecting a vector<Rigid3> or vector<Vec3>, got "+dpositions->getValueTypeString());
132+
133+
auto triangles = dynamic_cast<Data<sofa::type::vector<sofa::topology::Triangle>>*>(dtriangles);
134+
if(!triangles)
135+
throw std::runtime_error("Invalid argument, expecting vector<Triangle>, got "+dtriangles->getValueTypeString());
136+
137+
auto& cpos = positions->getValue();
138+
auto& ctris = triangles->getValue();
139+
140+
std::vector<sofa::type::Vec3> tripos;
141+
tripos.resize(ctris.size()*3);
142+
143+
for(auto& ctri : ctris)
144+
{
145+
tripos.emplace_back(cpos[ctri[0]]);
146+
tripos.emplace_back(cpos[ctri[1]]);
147+
tripos.emplace_back(cpos[ctri[2]]);
148+
}
149+
150+
self->drawTriangles(tripos, color);
151+
});
152+
153+
// Draw mesh
154+
dt.def("drawQuads", [](DrawTool *self, BaseData* dpositions, BaseData* dquads, sofa::type::RGBAColor& color){
155+
auto positions = dynamic_cast<Data<sofa::type::vector<sofa::type::Vec3d>>*>(dpositions);
156+
if(!positions)
157+
throw std::runtime_error("Invalid argument, expecting a vector<Rigid3> or vector<Vec3>, got "+dpositions->getValueTypeString());
158+
159+
auto quads = dynamic_cast<Data<sofa::type::vector<sofa::topology::Quad>>*>(dquads);
160+
if(!quads)
161+
throw std::runtime_error("Invalid argument, expecting vector<Quad>, got "+dquads->getValueTypeString());
162+
163+
auto& cpos = positions->getValue();
164+
auto& ctris = quads->getValue();
165+
166+
std::vector<sofa::type::Vec3> quadpos;
167+
quadpos.resize(ctris.size()*4);
168+
169+
for(auto& ctri : ctris)
170+
{
171+
quadpos.emplace_back(cpos[ctri[0]]);
172+
quadpos.emplace_back(cpos[ctri[1]]);
173+
quadpos.emplace_back(cpos[ctri[2]]);
174+
quadpos.emplace_back(cpos[ctri[3]]);
175+
}
176+
177+
self->drawQuads(quadpos, color);
178+
});
179+
180+
181+
// Draw spheres
182+
dt.def("drawSpheres", [](DrawTool *self, const py::array_t<double>& positions, const std::vector<float>& radius, sofa::type::RGBAColor& color){
183+
self->drawSpheres(getPoints(positions), radius, color);
184+
});
185+
dt.def("drawSpheres", [](DrawTool *self, BaseData* dpositions, const float radius, sofa::type::RGBAColor& color){
186+
auto positions = dynamic_cast<Data<sofa::type::vector<sofa::type::Vec3>>*>(dpositions);
187+
if(!positions)
188+
throw std::runtime_error("Invalid argument, expecting a vector<Rigid3> or vector<Vec3>, got "+dpositions->getValueTypeString());
189+
self->drawSpheres(positions->getValue(), radius, color);
190+
});
191+
192+
// Draw boundingBox
193+
dt.def("boundingBox", [](DrawTool *self, const std::array<double,4>& min, const std::array<double, 4>& max, double width){
194+
sofa::type::Vec3d cmin { min[0], min[1], min[2] };
195+
sofa::type::Vec3d cmax { max[0], max[1], max[2] };
196+
self->drawBoundingBox( cmin, cmax, width);
197+
});
117198

118199
// Draw frames
119200
dt.def("drawFrames", [](DrawTool* self,
@@ -143,6 +224,9 @@ void moduleAddDrawTool(py::module &m)
143224
}
144225
});
145226

227+
dt.def("enableLighting", [](DrawTool* self){ self->enableLighting(); });
228+
dt.def("disableLighting", [](DrawTool* self){ self->disableLighting(); });
229+
146230
// Draw text
147231
dt.def("drawText", [](DrawTool* self,
148232
const std::array<double,3>& point,
@@ -158,6 +242,9 @@ void moduleAddDrawTool(py::module &m)
158242
int fontSize, char* text, sofa::type::RGBAColor& color){
159243
self->writeOverlayText(point[0],point[1], fontSize, color, text);
160244
});
245+
246+
247+
161248
}
162249

163250
} /// namespace sofapython3

examples/example-drawing-controller.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,24 @@ def __init__(self, *args, **kwargs):
1515

1616
def draw(self, visual_context):
1717
dt = visual_context.getDrawTool()
18+
19+
dt.disableLighting()
1820
dt.drawPoints(array([[-1.5,-1.0,0.0]]), 5.0, RGBAColor("red"))
1921
dt.drawPoints([[-1.3,0,-1], [1.3,0,-1]], 10.0, RGBAColor("green"))
2022
dt.drawLines([[-1.3,0,-1], [1.3,0,-1]], 1.0, RGBAColor("green"))
2123
dt.drawFrames([[-1.5,0.1,-1]], [[0.0,0,0,1.0]], [0.1,0.1,0.1])
2224

23-
if self.target is not None:
24-
dt.drawPoints(self.target.position, 2.0, RGBAColor("blue"))
25-
2625
dt.drawText([-2.0,0.0,0.0], 0.5, "This is not a raptor", RGBAColor("white"))
2726
dt.drawOverlayText([10, 10], 12, "Overlay text", RGBAColor("pink"))
27+
dt.enableLighting()
28+
29+
if self.target is not None:
30+
dt.disableLighting()
31+
dt.drawPoints(self.target.position, 5.0, RGBAColor("blue"))
32+
33+
dt.drawTriangles(self.target.position, self.target.triangles, RGBAColor("white"))
34+
dt.drawQuads(self.target.position, self.target.quads, RGBAColor("white"))
35+
dt.enableLighting()
2836

2937
dt.drawFrames(self.mo.position, [0.1,0.1,0.1])
3038

0 commit comments

Comments
 (0)