Skip to content

Commit

Permalink
Change sense of rotation to anticlockwise
Browse files Browse the repository at this point in the history
  • Loading branch information
dhomeier committed Sep 29, 2021
1 parent 8a223d1 commit a0a1e7f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
14 changes: 9 additions & 5 deletions glue/core/roi.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ def pixel_to_axes(axes, x, y):


def rotation(alpha):
"""Return rotation matrix for angle alpha around origin.
"""Return rotation matrix for angle alpha (increasing anticlockwise) around origin.
"""
return np.array([[np.cos(alpha), np.sin(alpha)], [-np.sin(alpha), np.cos(alpha)]])
return np.array([[np.cos(alpha), -np.sin(alpha)], [np.sin(alpha), np.cos(alpha)]])


class Roi(object): # pragma: no cover
Expand Down Expand Up @@ -185,7 +185,7 @@ class RectangularROI(Roi):
ymin, ymax : float, optional
y coordinates of lower and upper border
theta : float, optional
Angle of clockwise rotation around center
Angle of anticlockwise rotation around center
"""

def __init__(self, xmin=None, xmax=None, ymin=None, ymax=None, theta=None):
Expand Down Expand Up @@ -224,6 +224,7 @@ def move_to(self, x, y):
self.ymax += dy

def rotate_to(self, theta):
""" Rotate anticlockwise around center to position angle theta (radian) """
self.theta = 0 if theta is None else theta
if np.isclose(self.theta % np.pi, 0.0, atol=1e-9):
self.rotation = np.identity(2)
Expand Down Expand Up @@ -317,6 +318,7 @@ def defined(self):
return self.xmin is not None

def to_polygon(self):
""" Returns vertices x, y, where each is an array of coordinates """
if self.defined():
if np.isclose(self.theta % np.pi, 0.0, atol=1e-9):
return (np.array([self.xmin, self.xmax, self.xmax, self.xmin, self.xmin]),
Expand Down Expand Up @@ -554,7 +556,7 @@ class EllipticalROI(Roi):
radius_y : float, optional
Semiaxis along y axis
theta : float, optional
Angle of clockwise rotation around (xc, yc)
Angle of anticlockwise rotation around (xc, yc)
"""

def __init__(self, xc=None, yc=None, radius_x=None, radius_y=None, theta=None):
Expand Down Expand Up @@ -641,7 +643,7 @@ def get_center(self):
return self.xc, self.yc

def to_polygon(self):
""" Returns x, y, where each is a list of points """
""" Returns vertices x, y, where each is an array of coordinates """
if not self.defined():
return [], []
theta = np.linspace(0, 2 * np.pi, num=20)
Expand Down Expand Up @@ -671,6 +673,7 @@ def move_to(self, xdelta, ydelta):
self.yc += ydelta

def rotate_to(self, theta):
""" Rotate anticlockwise around center to position angle theta (radian) """
self.theta = 0 if theta is None else theta
if np.isclose(self.theta % np.pi, 0.0, atol=1e-9):
self.rotation = np.identity(2)
Expand Down Expand Up @@ -832,6 +835,7 @@ def move_to(self, xdelta, ydelta):
self.vy = list(map(lambda y: y + ydelta, self.vy))

def rotate_to(self, theta):
""" Rotate anticlockwise around center to position angle theta (radian) """
theta = 0 if theta is None else theta
dtheta = theta - self.theta
if self.defined() and not np.isclose(dtheta % np.pi, 0.0, atol=1e-9):
Expand Down
30 changes: 15 additions & 15 deletions glue/core/tests/test_roi.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ def test_reset(self):

def test_set_rotation(self):
self.roi.update_limits(0, 0, 10, 4)
self.roi.rotate_by(-np.pi / 6)
self.roi.rotate_by(np.pi / 6)
assert self.roi.contains(8, 6)
self.roi.rotate_by(-np.pi / 3)
self.roi.rotate_by(np.pi / 3)
assert not self.roi.contains(9, 6)
assert self.roi.contains(6, 6.9)
self.roi.rotate_to(np.pi / 3)
self.roi.rotate_to(-np.pi / 3)
assert not self.roi.contains(5, 6)
assert self.roi.contains(6, -3)

Expand All @@ -104,7 +104,7 @@ def test_to_polygon(self):
x, y = self.roi.to_polygon()
poly = PolygonalROI(vx=x, vy=y)
assert not poly.contains(8, 3)
assert poly.contains(4, 6)
assert poly.contains(6, 6)

def test_ndarray(self):
self.roi.update_limits(0, 0, 10, 10)
Expand Down Expand Up @@ -353,8 +353,8 @@ def test_contains_on_undefined_contains_raises(self):
with pytest.raises(UndefinedROI):
self.roi_empty.contains(1, 1)
assert self.roi.contains(1, 1)
assert self.roi_rotated.contains(0, 2.5)
assert not self.roi_rotated.contains(0, 2)
assert self.roi_rotated.contains(2, 2.5)
assert not self.roi_rotated.contains(2, 2)

def test_set_center(self):
assert self.roi.contains(0, 0)
Expand All @@ -376,12 +376,12 @@ def test_set_radius(self):

def test_set_rotation(self):
self.roi_rotated.rotate_to(0.55)
assert self.roi_rotated.contains(-1.5, 3.6)
assert self.roi_rotated.contains(-1.5, 0.4)
self.roi_rotated.rotate_by(np.pi / 3)
assert self.roi_rotated.contains(1.2, 4.5)
assert self.roi_rotated.contains(0.8, 4.5)
self.roi_rotated.rotate_to(np.pi / 3)
assert not self.roi_rotated.contains(1.2, 4.5)
assert self.roi_rotated.contains(0.5, 3.5)
assert not self.roi_rotated.contains(0.8, 4.5)
assert self.roi_rotated.contains(0.5, 0.5)

def test_contains_many(self):
x = [0, 0, 0, 0, 0]
Expand All @@ -398,8 +398,8 @@ def test_poly(self):

x, y = self.roi_rotated.to_polygon()
poly = PolygonalROI(vx=x, vy=y)
assert poly.contains(0, 2.5)
assert not poly.contains(0, 2)
assert poly.contains(2, 2.5)
assert not poly.contains(2, 2)

def test_poly_undefined(self):
x, y = self.roi_empty.to_polygon()
Expand Down Expand Up @@ -437,7 +437,7 @@ def test_serialize_rotated(self):
new_roi = roundtrip_roi(self.roi_rotated)
assert new_roi.radius_y == 0.4
assert new_roi.theta == np.pi / 6
assert new_roi.contains(-1.5, 3.5)
assert new_roi.contains(-1.5, 0.5)


class TestPolygon(object):
Expand Down Expand Up @@ -550,8 +550,8 @@ def test_serialization(self):
new_roi = roundtrip_roi(self.roi)
assert new_roi.theta == 0
sqh = np.sqrt(0.5)
assert_almost_equal(new_roi.vx, np.array([-sqh, 0, sqh, 0]) + 0.5)
assert_almost_equal(new_roi.vy, np.array([0, sqh, 0, -sqh]) + 0.5)
assert_almost_equal(new_roi.vx, np.array([0, -sqh, 0, sqh]) + 0.5)
assert_almost_equal(new_roi.vy, np.array([-sqh, 0, sqh, 0]) + 0.5)


class TestProjected3dROI(object):
Expand Down
8 changes: 4 additions & 4 deletions glue/core/tests/test_subset.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ def test_roi_subset_state_with_pretransform(self):
assert_equal(data_clone.subsets[0].to_mask(), [0, 1, 0, 0])

roi.move_to(3, 0)
roi.rotate_to(1.5)
roi.rotate_to(-1.5)

subset = data_clone.new_subset()
subset.subset_state = RoiSubsetState(xatt=data_clone.id['a'], yatt=data_clone.id['c'],
Expand All @@ -882,7 +882,7 @@ def test_roi_subset_state_with_pretransform(self):

def test_roi_subset_state_rotated(self):

roi = RectangularROI(xmin=0, xmax=3, ymin=1.1, ymax=1.4, theta=np.pi / 3)
roi = RectangularROI(xmin=0, xmax=3, ymin=1.1, ymax=1.4, theta=-np.pi / 3)

subset = self.data.new_subset()
subset.subset_state = RoiSubsetState(xatt=self.data.id['a'], yatt=self.data.id['c'],
Expand All @@ -894,7 +894,7 @@ def test_roi_subset_state_rotated(self):
assert_equal(data_clone.subsets[0].to_mask(), [0, 0, 0, 1])

roi = RectangularROI(xmin=0, xmax=3, ymin=1.1, ymax=1.4)
roi.rotate_by(0.9)
roi.rotate_by(-0.9)

subset = data_clone.new_subset()
subset.subset_state = RoiSubsetState(xatt=data_clone.id['a'], yatt=data_clone.id['c'],
Expand Down Expand Up @@ -1089,7 +1089,7 @@ def test_roi_reduction():
roi.rotate_to(np.pi / 4)
state = RoiSubsetState(xatt=data4d.pixel_component_ids[0], yatt=data4d.pixel_component_ids[1], roi=roi)
out = state.to_mask(data4d)
expected_slice = np.array([[0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]])
expected_slice = np.array([[0, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
assert_equal(out[:, :, 0, 0], expected_slice)
assert_equal(out[:, :, 0, 1], expected_slice)
assert_equal(out[:, :, 1, 0], expected_slice)
Expand Down

0 comments on commit a0a1e7f

Please sign in to comment.