From f0061893e620dc0eafa5b8625b29266e4d7ff9f0 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Mon, 1 Dec 2014 14:17:56 -0500 Subject: [PATCH 1/3] Support transforming a single point --- lib/matplotlib/tests/test_transforms.py | 5 ++++ src/_path.h | 31 +++++++++++++++++++++++-- src/_path_wrapper.cpp | 26 +++++++++++++-------- 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/lib/matplotlib/tests/test_transforms.py b/lib/matplotlib/tests/test_transforms.py index f6534513c4bd..809fc0ded59e 100644 --- a/lib/matplotlib/tests/test_transforms.py +++ b/lib/matplotlib/tests/test_transforms.py @@ -487,6 +487,11 @@ def test_bbox_as_strings(): assert_equal(eval(format(getattr(b, k), fmt)), v) +def test_transform_single_point(): + t = mtrans.Affine2D() + t.transform_affine((1, 1)) + + if __name__=='__main__': import nose nose.runmodule(argv=['-s','--with-doctest'], exit=False) diff --git a/src/_path.h b/src/_path.h index 4eaed05245ed..b8e8585eefcd 100644 --- a/src/_path.h +++ b/src/_path.h @@ -687,9 +687,9 @@ clip_path_to_rect(PathIterator &path, agg::rect_d &rect, bool inside, std::vecto } template -void affine_transform(VerticesArray &vertices, agg::trans_affine &trans, ResultArray &result) +void affine_transform_2d(VerticesArray &vertices, agg::trans_affine &trans, ResultArray &result) { - if (vertices.dim(0) != 0 && vertices.dim(1) != 2) { + if (vertices.dim(0) != 1 && vertices.dim(1) != 2) { throw "Invalid vertices array."; } @@ -716,6 +716,33 @@ void affine_transform(VerticesArray &vertices, agg::trans_affine &trans, ResultA } } +template +void affine_transform_1d(VerticesArray &vertices, agg::trans_affine &trans, ResultArray &result) +{ + if (vertices.dim(0) != 2) { + throw "Invalid vertices array."; + } + + double x; + double y; + double t0; + double t1; + double t; + + x = vertices(0); + y = vertices(1); + + t0 = trans.sx * x; + t1 = trans.shx * y; + t = t0 + t1 + trans.tx; + result(0) = t; + + t0 = trans.shy * x; + t1 = trans.sy * y; + t = t0 + t1 + trans.ty; + result(1) = t; +} + template int count_bboxes_overlapping_bbox(agg::rect_d &a, BBoxArray &bboxes) { diff --git a/src/_path_wrapper.cpp b/src/_path_wrapper.cpp index e11adc1d3b13..c38403ef6a86 100644 --- a/src/_path_wrapper.cpp +++ b/src/_path_wrapper.cpp @@ -419,24 +419,30 @@ const char *Py_affine_transform__doc__ = "affine_transform(points, trans)"; static PyObject *Py_affine_transform(PyObject *self, PyObject *args, PyObject *kwds) { - numpy::array_view vertices; + PyObject *vertices_obj; agg::trans_affine trans; if (!PyArg_ParseTuple(args, - "O&O&:affine_transform", - &vertices.converter, - &vertices, + "OO&:affine_transform", + &vertices_obj, &convert_trans_affine, &trans)) { return NULL; } - npy_intp dims[] = { vertices.dim(0), 2 }; - numpy::array_view result(dims); - - CALL_CPP("affine_transform", (affine_transform(vertices, trans, result))); - - return result.pyobj(); + try { + numpy::array_view vertices(vertices_obj); + npy_intp dims[] = { vertices.dim(0), 2 }; + numpy::array_view result(dims); + CALL_CPP("affine_transform", (affine_transform_2d(vertices, trans, result))); + return result.pyobj(); + } catch (py::exception) { + numpy::array_view vertices(vertices_obj); + npy_intp dims[] = { vertices.dim(0) }; + numpy::array_view result(dims); + CALL_CPP("affine_transform", (affine_transform_1d(vertices, trans, result))); + return result.pyobj(); + } } const char *Py_count_bboxes_overlapping_bbox__doc__ = "count_bboxes_overlapping_bbox(bbox, bboxes)"; From 56e2420af0bfdd3efff2adf84d29c287a81b1f43 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Tue, 2 Dec 2014 08:32:23 -0500 Subject: [PATCH 2/3] Fix mistake --- src/_path.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_path.h b/src/_path.h index b8e8585eefcd..fcd3e4f585eb 100644 --- a/src/_path.h +++ b/src/_path.h @@ -689,7 +689,7 @@ clip_path_to_rect(PathIterator &path, agg::rect_d &rect, bool inside, std::vecto template void affine_transform_2d(VerticesArray &vertices, agg::trans_affine &trans, ResultArray &result) { - if (vertices.dim(0) != 1 && vertices.dim(1) != 2) { + if (vertices.dim(0) != 0 && vertices.dim(1) != 2) { throw "Invalid vertices array."; } From 733570e5f12868ab28bb188a828e9cd68bd499c4 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Wed, 3 Dec 2014 12:33:26 -0500 Subject: [PATCH 3/3] Add assert to test --- lib/matplotlib/tests/test_transforms.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_transforms.py b/lib/matplotlib/tests/test_transforms.py index 809fc0ded59e..af9cea700a38 100644 --- a/lib/matplotlib/tests/test_transforms.py +++ b/lib/matplotlib/tests/test_transforms.py @@ -489,8 +489,8 @@ def test_bbox_as_strings(): def test_transform_single_point(): t = mtrans.Affine2D() - t.transform_affine((1, 1)) - + r = t.transform_affine((1, 1)) + assert r.shape == (2,) if __name__=='__main__': import nose