Skip to content

Commit

Permalink
SWIG: Expose S2::S2PolygonLayerOptions and S2::S2Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
MikePlayle committed Jun 8, 2022
1 parent 880756e commit 44bd178
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/python/pywraps2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1070,5 +1070,66 @@ def testUnionDistinct(self):
loop = result.loop(1)
self.assertEqual(4, loop.num_vertices())

class S2BuilderTest(unittest.TestCase):
def setUp(self):
self.p1 = s2.S2LatLng.FromDegrees(10.0, 10.0).ToPoint()
self.p2 = s2.S2LatLng.FromDegrees(10.0, 11.0).ToPoint()
self.p3 = s2.S2LatLng.FromDegrees(11.0, 10.0).ToPoint()
self.p4 = s2.S2LatLng.FromDegrees(11.0, 11.0).ToPoint()

def testSquareDirected(self):
opts = s2.S2PolygonLayerOptions()
opts.set_edge_type(s2.S2Builder.EdgeType_DIRECTED)

result = s2.S2Polygon()
b = s2.S2Builder()
b.StartLayer(s2.S2PolygonLayer(result, opts))

b.AddEdge(self.p1, self.p2)
b.AddEdge(self.p2, self.p4)
b.AddEdge(self.p4, self.p3)
b.AddEdge(self.p3, self.p1)

b.Build()

self.assertEqual(1, result.num_loops())
loop = result.loop(0)
self.assertEqual(4, loop.num_vertices())

def testSquareUndirected(self):
opts = s2.S2PolygonLayerOptions()
opts.set_edge_type(s2.S2Builder.EdgeType_UNDIRECTED)

result = s2.S2Polygon()
b = s2.S2Builder()
b.StartLayer(s2.S2PolygonLayer(result, opts))

b.AddEdge(self.p1, self.p2)
b.AddEdge(self.p1, self.p3)
b.AddEdge(self.p2, self.p4)
b.AddEdge(self.p3, self.p4)

b.Build()

self.assertEqual(1, result.num_loops())
loop = result.loop(0)
self.assertEqual(4, loop.num_vertices())

def testException(self):
opts = s2.S2PolygonLayerOptions()
opts.set_edge_type(s2.S2Builder.EdgeType_DIRECTED)

result = s2.S2Polygon()
b = s2.S2Builder()
b.StartLayer(s2.S2PolygonLayer(result, opts))

b.AddEdge(self.p1, self.p2)
b.AddEdge(self.p1, self.p3)
b.AddEdge(self.p2, self.p4)
b.AddEdge(self.p3, self.p4)

with self.assertRaises(ValueError):
b.Build()

if __name__ == "__main__":
unittest.main()
39 changes: 39 additions & 0 deletions src/python/s2_common.i
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ public:
}
};

// Wrapper for S2PolygonLayer::Options to work around the inability to
// handle nested classes in SWIG.
class S2PolygonLayerOptions {
public:
s2builderutil::S2PolygonLayer::Options opts;

void set_edge_type(S2Builder::EdgeType edge_type) {
opts.set_edge_type(edge_type);
}
};

%}

%inline %{
Expand Down Expand Up @@ -280,6 +291,19 @@ class S2Point {
}
}

%extend S2Builder {
public:
void StartLayer(S2Builder::Layer* layer_disown) {
$self->StartLayer(std::unique_ptr<S2Builder::Layer>(layer_disown));
}
}

class S2PolygonLayerOptions {
public:
s2builderutil::S2PolygonLayer::Options opts;
void set_edge_type(S2Builder::EdgeType);
};

// S2PolygonLayer's constructor takes a pointer to an S2Polygon. We
// need to ensure that the S2Polygon is not destroyed while the
// S2PolygonLayer still references it. To do this, save a reference to
Expand All @@ -288,6 +312,14 @@ class S2Point {
self._incref = args[0]
%}

%extend s2builderutil::S2PolygonLayer {
public:
S2PolygonLayer(S2Polygon* layer,
const S2PolygonLayerOptions& options) {
return new s2builderutil::S2PolygonLayer(layer, options.opts);
}
}

%extend MutableS2ShapeIndex {
public:
void Add(S2Polygon* polygon_disown) {
Expand Down Expand Up @@ -527,6 +559,13 @@ public:
%unignore S2BufferOperationOptions::set_error_fraction;
%unignore S2Builder;
%unignore S2Builder::Layer;
%unignore S2Builder::S2Builder;
%unignore S2Builder::StartLayer(S2Builder::Layer*);
%unignore S2Builder::AddEdge;
%unignore S2Builder::Build;
%unignore S2Builder::EdgeType;
%unignore S2Builder::EdgeType::DIRECTED;
%unignore S2Builder::EdgeType::UNDIRECTED;
%unignore s2builderutil;
%unignore s2builderutil::S2PolygonLayer;
%unignore s2builderutil::S2PolygonLayer::S2PolygonLayer(S2Polygon*);
Expand Down

0 comments on commit 44bd178

Please sign in to comment.