diff --git a/docs/ref/util.rst b/docs/ref/util.rst index 5139f31..eeca2ef 100644 --- a/docs/ref/util.rst +++ b/docs/ref/util.rst @@ -11,7 +11,7 @@ Shapes lentil.circle lentil.hexagon - lentil.slit + lentil.rectangle Array manipulation ------------------ diff --git a/lentil/__init__.py b/lentil/__init__.py index 7969e9f..d8025e9 100644 --- a/lentil/__init__.py +++ b/lentil/__init__.py @@ -45,7 +45,7 @@ from lentil.shape import ( circle, hexagon, - slit + rectangle ) from lentil import util diff --git a/lentil/shape.py b/lentil/shape.py index 91523b9..6be0d20 100644 --- a/lentil/shape.py +++ b/lentil/shape.py @@ -75,31 +75,37 @@ def hexagon(shape, radius, shift=(0, 0), rotate=False, antialias=True): return mask -def slit(shape, width, length=None): - """Draw a rectangular slit +def rectangle(shape, width, height, shift=(0,0), antialias=True): + """Draw a rectangle Parameters ---------- shape : array_like Size of output in pixels (nrows, ncols) width : float - Slit width in pixels - length : float, optional - Slit length in pixels. If not specified, the slit spans - the entire column shape (default). - + Width of rectangle in pixels + height : float + Height of rectangle in pixels + shift : tuple of floats, optional + How far to shift center in (rows, cols). Default is (0, 0). + antialias : bool, optional + If True (default), the shape edges are antialiased. + Returns ------- ndarray - + """ - rr, cc = lentil.helper.mesh(shape) - slit = np.ones(shape) + shape = np.broadcast_to(shape, (2,)) + rr, cc = lentil.helper.mesh(shape, shift) + rect = np.ones(shape) + + width_clip = np.clip(0.5 + (width/2) - np.abs(cc), 0, 1) + height_clip = np.clip(0.5 + (height/2) - np.abs(rr), 0, 1) - length = shape[1] if length is None else length - width_clip = np.clip(0.5 + (width/2) - np.abs(rr), 0, 1) - length_clip = np.clip(0.5 + (length/2) - np.abs(cc), 0, 1) + rect = np.minimum(np.minimum(rect, width_clip), height_clip) - slit = np.minimum(np.minimum(slit, width_clip), length_clip) + if not antialias: + rect[rect > 0] = 1 - return slit + return rect