Skip to content
This repository has been archived by the owner on Jun 3, 2020. It is now read-only.

Commit

Permalink
adds optional constrain lines and points
Browse files Browse the repository at this point in the history
  • Loading branch information
vmora committed Aug 21, 2018
1 parent 4268afb commit 870b670
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
32 changes: 29 additions & 3 deletions fourmy/_fourmy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <CGAL/Polygon_2.h>

#include <boost/python.hpp>
#include <boost/python/overloads.hpp>
#include <iostream>
#include <string>

Expand Down Expand Up @@ -84,13 +85,13 @@ Polygon_2 polygon_from_ring(const boost::python::object & coords)
{
using namespace boost::python;
Polygon_2 poly;
size_t sz = extract<size_t>(coords.attr("__len__")());
const size_t sz = extract<size_t>(coords.attr("__len__")());
for (size_t i=0; i<sz-1; i++)
poly.push_back(Point(extract<double>(coords[i][0]), extract<double>(coords[i][1])));
return std::move(poly);
}

boost::python::object tessellate(const boost::python::object & polygon)
boost::python::object tessellate(const boost::python::object & polygon, const boost::python::object & lines = boost::python::object(), const boost::python::object & points = boost::python::object())
{
using namespace boost::python;

Expand All @@ -100,6 +101,14 @@ boost::python::object tessellate(const boost::python::object & polygon)

CDT cdt;

// insert points
if (points != boost::python::object())
{
const size_t sz = extract<size_t>(points.attr("__len__")());
for (size_t i=0; i<sz; i++)
cdt.push_back(Point(extract<double>(points[i].attr("coords")[0][0]), extract<double>(points[i].attr("coords")[0][1])));
}

{
Polygon_2 poly(polygon_from_ring(polygon.attr("exterior").attr("coords")));
cdt.insert_constraint(poly.vertices_begin(), poly.vertices_end(), true);
Expand All @@ -111,6 +120,22 @@ boost::python::object tessellate(const boost::python::object & polygon)
cdt.insert_constraint(poly.vertices_begin(), poly.vertices_end(), true);
}

// insert line constrains
if (lines != boost::python::object())
{
const size_t nlines = extract<size_t>(lines.attr("__len__")());
for (size_t l=0; l<nlines; l++)
{
const boost::python::object & coords = lines[l].attr("coords");
const size_t sz = extract<size_t>(coords.attr("__len__")());
for (size_t i=1; i<sz; i++)
{
cdt.insert_constraint(Point(extract<double>(coords[i-1][0]), extract<double>(coords[i-1][1])),
Point(extract<double>(coords[i][0]), extract<double>(coords[i][1])));
}
}
}

mark_domains(cdt);

object shapely_geom = import("shapely.geometry");
Expand All @@ -130,11 +155,12 @@ boost::python::object tessellate(const boost::python::object & polygon)
}
}

BOOST_PYTHON_FUNCTION_OVERLOADS(tessellate_overloads, fourmy::tessellate, 1, 3)

BOOST_PYTHON_MODULE(_fourmy)
{
using namespace boost::python;
def("tessellate", &fourmy::tessellate);
def("tessellate", &fourmy::tessellate, tessellate_overloads(args("polygon", "lines", "points"), "This is tesselate's docstring"));
}

// register_exception_translator<std::out_of_range>(translate_out_of_range);
Expand Down
9 changes: 8 additions & 1 deletion test/geometry.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
from fourmy import tessellate
from shapely.geometry import Polygon, Point
from shapely.geometry import Polygon, Point, LineString, MultiLineString

t = tessellate(Polygon([(0,0), (1,0), (1,1), (0,1)]))
print(t.wkt)

t = tessellate(Polygon([(0,0), (1,0), (1,1), (0,1)], [[(.2,.2),(.2,.8),(.8,.8),(.8,.2)]]))
print(t.wkt)

t = tessellate(Polygon([(0,0), (1,0), (1,1), (0,1)]), [LineString([(.2, .6), (.8,.6)]), LineString([(.2, .4), (.8,.4)])])
print(t.wkt)

t = tessellate(Polygon([(0,0), (1,0), (1,1), (0,1)]), MultiLineString([LineString([(.2, .6), (.8,.6)]), LineString([(.2, .4), (.8,.4)])]), [Point(.9, .9)])
print(t.wkt)

t = tessellate(Polygon([(0,0), (1,0), (1,1), (0,1)]), lines=None, points=[Point(.9, .9)])
print(t.wkt)

0 comments on commit 870b670

Please sign in to comment.