Skip to content

Commit

Permalink
Experimental rotation.setter for PolygonPixelRegion
Browse files Browse the repository at this point in the history
  • Loading branch information
dhomeier committed Dec 30, 2021
1 parent e74ec80 commit 605fe44
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
19 changes: 19 additions & 0 deletions regions/shapes/polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def __init__(self, vertices, meta=None, visual=None,
self.visual = visual or RegionVisual()
self.origin = origin
self.vertices = vertices + origin
self._rotation = 0.0 * u.degree

@property
def area(self):
Expand Down Expand Up @@ -301,6 +302,24 @@ def rotate(self, center, angle):
vertices = self.vertices.rotate(center, angle)
return self.copy(vertices=vertices)

@property
def rotation(self):
"""
Rotation angle to apply in-place rotations (operating on this instance).
Since `.setter` will apply the rotation directly on the vertices, this
value will always be reset to 0.
"""
return self._rotation

@rotation.setter
def rotation(self, angle):
self.vertices = self.vertices.rotate(self.centroid, angle - self._rotation)
self._rotation = 0.0 * u.degree
if hasattr(self, '_mpl_selector'):
self._mpl_selector.verts = list(zip(self.vertices.x, self.vertices.y))
if getattr(self, '_mpl_selector_callback', None) is not None:
self._mpl_selector_callback(self)


class RegularPolygonPixelRegion(PolygonPixelRegion):
"""
Expand Down
16 changes: 16 additions & 0 deletions regions/shapes/tests/test_polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,22 @@ def test_origin(self):
reg2 = PolygonPixelRegion(relverts, origin=origin)
assert_equal(reg1.vertices, reg2.vertices)

def test_rotation(self):
"""Test 'in-place' rotation of polygon instance, including full rotation"""
self.reg.rotation = 90 * u.deg
assert_allclose(self.reg.vertices.x, [8/3., 8/3., -1/3.], rtol=1e-9)
assert_allclose(self.reg.vertices.y, [4/3., 10/3., 4/3.], rtol=1e-9)
assert_allclose(self.reg.rotation, 0 * u.deg, rtol=1e-9)

self.reg.rotation = 90 * u.deg
assert_allclose(self.reg.vertices.x, [7/3., 1/3., 7/3.], rtol=1e-9)
assert_allclose(self.reg.vertices.y, [3, 3, 0], rtol=1e-9)

self.reg.rotation = 180 * u.deg
assert_allclose(self.reg.vertices.x, [1, 3, 1], rtol=1e-9)
assert_allclose(self.reg.vertices.y, [1, 1, 4], rtol=1e-9)
assert_allclose(self.reg.rotation, 0 * u.deg, rtol=1e-9)


class TestPolygonSkyRegion(BaseTestSkyRegion):

Expand Down

0 comments on commit 605fe44

Please sign in to comment.