Skip to content

Commit

Permalink
Simplify affine_transform on C++ side
Browse files Browse the repository at this point in the history
  • Loading branch information
QuLogic committed Dec 20, 2023
1 parent 6f8b440 commit 2e72141
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
15 changes: 6 additions & 9 deletions lib/matplotlib/tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,20 +286,17 @@ def test_translate_plus_other(self):

def test_invalid_arguments(self):
t = mtransforms.Affine2D()
# The wrong number of dimensions and a wrong shape with a possible number of
# dimensions are both caught by pybind11, which always raises the less precise
# RuntimeError.
with pytest.raises(RuntimeError):
with pytest.raises(TypeError):
t.transform(1)
with pytest.raises(RuntimeError):
with pytest.raises(TypeError):
t.transform([[[1]]])
with pytest.raises(RuntimeError):
with pytest.raises(TypeError):
t.transform([])
with pytest.raises(RuntimeError):
with pytest.raises(TypeError):
t.transform([1])
with pytest.raises(RuntimeError):
with pytest.raises(TypeError):
t.transform([[1]])
with pytest.raises(RuntimeError):
with pytest.raises(TypeError):
t.transform([[1, 2, 3]])

def test_copy(self):
Expand Down
11 changes: 8 additions & 3 deletions lib/matplotlib/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1867,9 +1867,14 @@ def to_values(self):
def transform_affine(self, values):
mtx = self._get_eigen_matrix()
if isinstance(values, np.ma.MaskedArray):
tpoints = mtx.affine_transform(values.data)
return np.ma.MaskedArray(tpoints, mask=np.ma.getmask(values))
return mtx.affine_transform(values)
tpoints = mtx.affine_transform(values.data.T).T
result = np.ma.MaskedArray(tpoints, mask=np.ma.getmask(values))
else:
values = np.asarray(values)
result = mtx.affine_transform(values.T).T
if len(values.shape) == 2:
result = np.atleast_2d(result)
return result

if DEBUG:
_transform_affine = transform_affine
Expand Down
6 changes: 2 additions & 4 deletions src/_eigen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,8 @@ PYBIND11_MODULE(_eigen, m) {
}
)
.def("affine_transform",
[](const Eigen::Affine2d& self, py::array_t<double> vertices_arr) {
auto vertices = vertices_arr.attr("transpose")().cast<Eigen::Ref<const Eigen::Matrix2Xd>>();
auto result = py::cast(self * vertices, py::return_value_policy::move);
return result.attr("transpose")();
[](const Eigen::Affine2d& self, Eigen::Ref<const Eigen::Matrix2Xd> vertices) {
return self * vertices;
}
)

Expand Down

0 comments on commit 2e72141

Please sign in to comment.