Skip to content

Commit

Permalink
Implement polygon approx for annulus
Browse files Browse the repository at this point in the history
  • Loading branch information
pllim committed May 12, 2023
1 parent fc5dd72 commit fcc436a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
13 changes: 10 additions & 3 deletions glue/core/roi.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,14 +597,21 @@ def defined(self):
status = False
return status

# FIXME: We need 2 polygons, with the inner one inverted, just like subset state.
def to_polygon(self):
if not self.defined():
return [], []
raise NotImplementedError("Cannot transform annulus to simple polygon")
theta = np.linspace(0, 2 * np.pi, num=20)
x_inner = self.xc + self.inner_radius * np.cos(theta)
y_inner = self.yc + self.inner_radius * np.sin(theta)
x_outer = self.xc + self.outer_radius * np.cos(theta)
y_outer = self.yc + self.outer_radius * np.sin(theta)
# theta=2pi=360deg=0deg --> x=r, y=0
x = np.concatenate((x_inner, x_outer[::-1], x_inner[0]), axis=None)
y = np.concatenate((y_inner, y_outer[::-1], y_inner[0]), axis=None)
return x, y

def transformed(self, xfunc=None, yfunc=None):
raise NotImplementedError("Cannot transform annulus to simple polygon")
return PolygonalROI(*self.to_polygon()).transformed(xfunc=xfunc, yfunc=yfunc)

def center(self):
return self.xc, self.yc
Expand Down
7 changes: 5 additions & 2 deletions glue/core/tests/test_roi.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,11 @@ def test_circular_annulus_defined():
assert_almost_equal(new_roi.outer_radius, roi.outer_radius)

# test_poly
with pytest.raises(NotImplementedError, match="Cannot transform annulus to simple polygon"):
roi.to_polygon()
x, y = roi.to_polygon()
poly = PolygonalROI(vx=x, vy=y)
assert not poly.contains(0, 0)
assert poly.contains(0, 2)
assert not poly.contains(2, 0) # We have to cut it at theta=0

# test_reset
assert roi.defined()
Expand Down

0 comments on commit fcc436a

Please sign in to comment.