@@ -63,63 +63,89 @@ sofa::type::vector<sofa::type::Vec3> getPoints(const py::array_t<double>& array)
6363 return points;
6464}
6565
66+ sofa::type::vector<sofa::type::Quatd> getOrientations (const py::array_t <double >& array)
67+ {
68+ py::buffer_info buf = array.request ();
69+
70+ if (buf.ndim != 2 )
71+ throw std::runtime_error (" Invalid argument, expecting an array with ndim=2" );
72+
73+ size_t rows = buf.shape [0 ];
74+ size_t cols = buf.shape [1 ];
75+
76+ double * ptr = static_cast <double *>(buf.ptr );
77+
78+ std::vector<sofa::type::Quatd> orientations;
79+ orientations.resize (rows);
80+ for (size_t i = 0 ; i < rows; ++i)
81+ for (size_t j = 0 ; j < cols; ++j)
82+ orientations[i][j] = ptr[i * cols + j];
83+
84+ return orientations;
85+ }
86+
6687
6788void moduleAddDrawTool (py::module &m)
6889{
6990 py::class_<DrawTool> dt (m, " DrawTool" , sofapython3::doc::drawtool::baseDrawToolClass);
7091
71- // Draw points from vectors...
7292 dt.def (" drawPoints" , [](DrawTool *self, py::array_t <double > points, float size, sofa::type::RGBAColor& color)
7393 {
7494 self->drawPoints (getPoints (points), size, color);
7595 });
7696
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 ){
97+ dt.def (" drawPoints" , [](DrawTool *self, BaseData* dpositions, float size, sofa::type::RGBAColor& color){
7998 auto positions = dynamic_cast <Data<sofa::type::vector<sofa::type::Vec3>>*>(dpositions);
8099 if (!positions)
81100 throw std::runtime_error (" Invalid argument, a base data of type vector<Vec3> was expected, got " +dpositions->getValueTypeString ());
82101
83- self->drawPoints (positions->getValue (), size, sofa::type::RGBAColor::white () );
102+ self->drawPoints (positions->getValue (), size, color );
84103 });
85104
86- dt.def (" drawLines" , [](DrawTool *self, const std::vector<sofa::type::Vec3> &points, float size ){
87- self->drawLines (points , size, sofa::type::RGBAColor::white () );
105+ dt.def (" drawLines" , [](DrawTool *self, const py:: array_t < double >& positions, const float size, sofa::type::RGBAColor& color ){
106+ self->drawLines (getPoints (positions) , size, color );
88107 });
89108
90-
91109 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++)
110+ const py::array_t <double >& points,
111+ const py::array_t <double >& orientations,
112+ const std::array<double ,3 >& size){
113+ auto cpoints = getPoints (points);
114+ auto corientations = getOrientations (orientations);
115+ sofa::type::Vec3 csize {size[0 ],size[1 ],size[2 ]};
116+ for (unsigned int i=0 ;i<cpoints.size ();i++)
96117 {
97- self->drawFrame (points [i], orientations [i], size );
118+ self->drawFrame (cpoints [i], corientations [i], csize );
98119 }
99120 });
100- dt.def (" drawFrames" , [](DrawTool* self, BaseData* dpositions, const sofa::type::Vec3& size ){
121+
122+ dt.def (" drawFrames" , [](DrawTool* self, BaseData* dpositions, std::array<double , 3 >& size ){
101123 using sofa::defaulttype::Rigid3Types;
102124 using Coord = sofa::defaulttype::Rigid3Types::Coord;
103125 auto positions = dynamic_cast <Data<sofa::type::vector<Coord>>*>(dpositions);
126+ sofa::type::Vec3 csize {size[0 ],size[1 ],size[2 ]};
104127 if (!positions)
105128 throw std::runtime_error (" Invalid argument" );
106129
107130 for (auto & position : positions->getValue ())
108131 {
109132 self->drawFrame (Rigid3Types::getCPos (position),
110- Rigid3Types::getCRot (position), size );
133+ Rigid3Types::getCRot (position), csize );
111134 }
112135 });
136+
113137 dt.def (" drawText" , [](DrawTool* self,
114- const sofa::type::Vec3d& point,
115- const float size,
116- const std::string& text)
138+ const std::array<double ,3 >& point,
139+ const float size,
140+ const std::string& text,
141+ const sofa::type::RGBAColor& color)
117142 {
118- self->draw3DText (point, size, sofa::type::RGBAColor::white () , text.c_str ());
143+ self->draw3DText (point, size, color , text.c_str ());
119144 });
120145
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);
146+ dt.def (" drawOverlayText" , [](DrawTool* self, const std::array<double ,2 >& point,
147+ int fontSize, char * text, sofa::type::RGBAColor& color){
148+ self->writeOverlayText (point[0 ],point[1 ], fontSize, color, text);
123149 });
124150}
125151
0 commit comments