Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fix #5670. No double endpoints in Path.to_polygon #5672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 15, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix #5670. No double endpoints in Path.to_polygon
  • Loading branch information
mdboom committed Dec 14, 2015
commit 36ce4a7f3afd775dfb8b91fe4997a2ee0ab929fc
37 changes: 37 additions & 0 deletions 37 lib/matplotlib/tests/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from nose.tools import assert_raises, assert_equal
from matplotlib.testing.decorators import image_comparison
import matplotlib.pyplot as plt
from matplotlib import transforms


def test_readonly_path():
Expand Down Expand Up @@ -113,6 +114,42 @@ def test_marker_paths_pdf():
plt.ylim(-1, 7)


def test_path_no_doubled_point_in_to_polygon():
hand = np.array(
[[ 1.64516129, 1.16145833 ],
[ 1.64516129, 1.59375 ],
[ 1.35080645, 1.921875 ],
[ 1.375 , 2.18229167 ],
[ 1.68548387, 1.9375 ],
[ 1.60887097, 2.55208333 ],
[ 1.68548387, 2.69791667 ],
[ 1.76209677, 2.56770833 ],
[ 1.83064516, 1.97395833 ],
[ 1.89516129, 2.75 ],
[ 1.9516129 , 2.84895833 ],
[ 2.01209677, 2.76041667 ],
[ 1.99193548, 1.99479167 ],
[ 2.11290323, 2.63020833 ],
[ 2.2016129 , 2.734375 ],
[ 2.25403226, 2.60416667 ],
[ 2.14919355, 1.953125 ],
[ 2.30645161, 2.36979167 ],
[ 2.39112903, 2.36979167 ],
[ 2.41532258, 2.1875 ],
[ 2.1733871 , 1.703125 ],
[ 2.07782258, 1.16666667 ]])

(r0, c0, r1, c1) = (1.0, 1.5, 2.1, 2.5)

poly = Path(np.vstack((hand[:, 1], hand[:, 0])).T, closed=True)
clip_rect = transforms.Bbox([[r0, c0], [r1, c1]])
poly_clipped = poly.clip_to_bbox(clip_rect).to_polygons()[0]

assert np.all(poly_clipped[-2] != poly_clipped[-1])
assert np.all(poly_clipped[-1] == poly_clipped[0])



if __name__ == '__main__':
import nose
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)
45 changes: 35 additions & 10 deletions 45 src/_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ struct XY
XY(double x_, double y_) : x(x_), y(y_)
{
}

bool operator==(const XY& o)
{
return (x == o.x && y == o.y);
}

bool operator!=(const XY& o)
{
return (x != o.x || y != o.y);
}
};

//
Expand Down Expand Up @@ -838,6 +848,25 @@ bool path_intersects_path(PathIterator1 &p1, PathIterator2 &p2)
return false;
}

void _finalize_polygon(std::vector<Polygon> &result)
{
Polygon &polygon = result.back();

if (result.size() == 0) {
return;
}

/* Clean up the last polygon in the result. If less than a
triangle, remove it. */
if (polygon.size() < 3) {
result.pop_back();
} else {
if (polygon.front() != polygon.back()) {
polygon.push_back(polygon.front());
}
}
}

template <class PathIterator>
void convert_path_to_polygons(PathIterator &path,
agg::trans_affine &trans,
Expand Down Expand Up @@ -867,24 +896,20 @@ void convert_path_to_polygons(PathIterator &path,

while ((code = curve.vertex(&x, &y)) != agg::path_cmd_stop) {
if ((code & agg::path_cmd_end_poly) == agg::path_cmd_end_poly) {
if (polygon->size() >= 1) {
polygon->push_back((*polygon)[0]);
result.push_back(Polygon());
polygon = &result.back();
}
_finalize_polygon(result);
result.push_back(Polygon());
polygon = &result.back();
} else {
if (code == agg::path_cmd_move_to && polygon->size() >= 1) {
polygon->push_back((*polygon)[0]);
if (code == agg::path_cmd_move_to) {
_finalize_polygon(result);
result.push_back(Polygon());
polygon = &result.back();
}
polygon->push_back(XY(x, y));
}
}

if (polygon->size() == 0) {
result.pop_back();
}
_finalize_polygon(result);
}

template <class VertexSource>
Expand Down
23 changes: 18 additions & 5 deletions 23 src/_path_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,29 @@
PyObject *convert_polygon_vector(std::vector<Polygon> &polygons)
{
PyObject *pyresult = PyList_New(polygons.size());
bool fix_endpoints;

for (size_t i = 0; i < polygons.size(); ++i) {
Polygon poly = polygons[i];
npy_intp dims[] = {(npy_intp)poly.size() + 1, 2 };
numpy::array_view<double, 2> subresult(dims);
npy_intp dims[2];
dims[1] = 2;

if (poly.front() != poly.back()) {
/* Make last point same as first, if not already */
dims[0] = (npy_intp)poly.size() + 1;
fix_endpoints = true;
} else {
dims[0] = (npy_intp)poly.size();
fix_endpoints = false;
}

/* Make last point same as first. */
numpy::array_view<double, 2> subresult(dims);
memcpy(subresult.data(), &poly[0], sizeof(double) * poly.size() * 2);
subresult(poly.size(), 0) = poly[0].x;
subresult(poly.size(), 1) = poly[0].y;

if (fix_endpoints) {
subresult(poly.size(), 0) = poly.front().x;
subresult(poly.size(), 1) = poly.front().y;
}

if (PyList_SetItem(pyresult, i, subresult.pyobj())) {
Py_DECREF(pyresult);
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.