Skip to content

Commit

Permalink
[py] added draw_polygon et Polygon bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonRohou committed Apr 11, 2024
1 parent 69b19e6 commit 163c9ef
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
35 changes: 35 additions & 0 deletions python/src/core/geometry/codac_py_geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <pybind11/stl.h>
#include "../contractors/static/codac_py_Ctc.h"
#include "../separators/codac_py_Sep.h"
#include "codac_Polygon.h"

// Generated file from Doxygen XML (doxygen2docstring.py):
#include "codac_py_CtcSegment_docs.h"
Expand All @@ -28,6 +29,34 @@ namespace py = pybind11;
using namespace ibex;
using namespace codac;

Polygon* create_polygon_from_pylist(const vector<py::list>& lst)
{
vector<Vector> v_pts;

if(lst.size() < 1)
throw invalid_argument("size of the input list is 0");

//double (*tmp)[2] = new double[lst.size()][2];
for(size_t i = 0; i < lst.size(); i++)
{
if(lst[i].size() != 2)
{
//delete[] tmp;
throw invalid_argument("sub list must contain only two elements");
}

//tmp[i][0] = lst[i][0].cast<double>();
//tmp[i][1] = lst[i][1].cast<double>();
v_pts.push_back({ lst[i][0].cast<double>(), lst[i][1].cast<double>() });
}

//IntervalVector *instance = new IntervalVector(lst.size(), tmp);
//delete[] tmp;
//return instance;
return new Polygon(v_pts);
// todo: manage delete
}

SepPolygon* SepPolygonFromList(std::vector< std::array<double, 2> >& lst){
size_t n = lst.size();
std::vector<double> ax(n), ay(n),bx(n),by(n);
Expand Down Expand Up @@ -66,4 +95,10 @@ void export_geometry(py::module& m, py::class_<Ctc, pyCtc>& ctc, py::class_<Sep,
.def("separate", &SepPolarXY::separate)
;

// Export Polygon
py::class_<Polygon>(m, "Polygon")
.def(py::init(&create_polygon_from_pylist), "list"_a)
.def(py::init<std::vector<Vector>>())
;

}
4 changes: 4 additions & 0 deletions python/src/core/graphics/codac_py_VIBesFig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,9 @@ void export_VIBesFig(py::module& m)
.def("draw_line", (void (VIBesFig::*)(const std::vector<double>&,const std::vector<double>&,const string&,const vibes::Params &))&VIBesFig::draw_line,
"todo",
"v_x"_a, "v_y"_a, "color"_a="", "params"_a=vibes::Params())

.def("draw_polygon", (void (VIBesFig::*)(const Polygon&,const string&,const vibes::Params &))&VIBesFig::draw_polygon,
VIBESFIG_VOID_DRAW_POLYGON_POLYGON_STRING_VIBESPARAMS,
"p"_a, "color"_a="", "params"_a=vibes::Params())
;
}

0 comments on commit 163c9ef

Please sign in to comment.