@@ -40,6 +40,28 @@ struct XY
4040 }
4141};
4242
43+ typedef std::vector<XY> Polygon;
44+
45+ void _finalize_polygon (std::vector<Polygon> &result, int closed_only)
46+ {
47+ if (result.size () == 0 ) {
48+ return ;
49+ }
50+
51+ Polygon &polygon = result.back ();
52+
53+ /* Clean up the last polygon in the result. */
54+ if (polygon.size () == 0 ) {
55+ result.pop_back ();
56+ } else if (closed_only) {
57+ if (polygon.size () < 3 ) {
58+ result.pop_back ();
59+ } else if (polygon.front () != polygon.back ()) {
60+ polygon.push_back (polygon.front ());
61+ }
62+ }
63+ }
64+
4365//
4466// The following function was found in the Agg 2.3 examples (interactive_polygon.cpp).
4567// It has been generalized to work on (possibly curved) polylines, rather than
@@ -509,8 +531,6 @@ bool path_in_path(PathIterator1 &a,
509531 http://en.wikipedia.org/wiki/Sutherland-Hodgman_clipping_algorithm
510532*/
511533
512- typedef std::vector<XY> Polygon;
513-
514534namespace clip_to_rect_filters
515535{
516536/* There are four different passes needed to create/remove
@@ -696,9 +716,12 @@ clip_path_to_rect(PathIterator &path, agg::rect_d &rect, bool inside, std::vecto
696716
697717 // Empty polygons aren't very useful, so skip them
698718 if (polygon1.size ()) {
719+ _finalize_polygon (results, 1 );
699720 results.push_back (polygon1);
700721 }
701722 } while (code != agg::path_cmd_stop);
723+
724+ _finalize_polygon (results, 1 );
702725}
703726
704727template <class VerticesArray , class ResultArray >
@@ -849,30 +872,12 @@ bool path_intersects_path(PathIterator1 &p1, PathIterator2 &p2)
849872 return false ;
850873}
851874
852- void _finalize_polygon (std::vector<Polygon> &result)
853- {
854- Polygon &polygon = result.back ();
855-
856- if (result.size () == 0 ) {
857- return ;
858- }
859-
860- /* Clean up the last polygon in the result. If less than a
861- triangle, remove it. */
862- if (polygon.size () < 3 ) {
863- result.pop_back ();
864- } else {
865- if (polygon.front () != polygon.back ()) {
866- polygon.push_back (polygon.front ());
867- }
868- }
869- }
870-
871875template <class PathIterator >
872876void convert_path_to_polygons (PathIterator &path,
873877 agg::trans_affine &trans,
874878 double width,
875879 double height,
880+ int closed_only,
876881 std::vector<Polygon> &result)
877882{
878883 typedef agg::conv_transform<py::PathIterator> transformed_path_t ;
@@ -897,20 +902,20 @@ void convert_path_to_polygons(PathIterator &path,
897902
898903 while ((code = curve.vertex (&x, &y)) != agg::path_cmd_stop) {
899904 if ((code & agg::path_cmd_end_poly) == agg::path_cmd_end_poly) {
900- _finalize_polygon (result);
905+ _finalize_polygon (result, 1 );
901906 result.push_back (Polygon ());
902907 polygon = &result.back ();
903908 } else {
904909 if (code == agg::path_cmd_move_to) {
905- _finalize_polygon (result);
910+ _finalize_polygon (result, closed_only );
906911 result.push_back (Polygon ());
907912 polygon = &result.back ();
908913 }
909914 polygon->push_back (XY (x, y));
910915 }
911916 }
912917
913- _finalize_polygon (result);
918+ _finalize_polygon (result, closed_only );
914919}
915920
916921template <class VertexSource >
0 commit comments