Skip to content

Commit

Permalink
hollowness for cube, cuboid, cylinder, ellipsoid and sphere (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesagraham authored Jun 5, 2024
1 parent 5abacec commit 503e312
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
10 changes: 7 additions & 3 deletions src/ttmask/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def cube(
cube_sidelength: float =typer.Option(...),
soft_edge_width: float = typer.Option(0),
pixel_size: float = typer.Option(...),
output: str = typer.Option("cube.mrc")
output: str = typer.Option("cube.mrc"),
wall_thickness: float = typer.Option(0),
):
c = sidelength // 2
center = np.array([c, c, c])
Expand All @@ -29,10 +30,13 @@ def cube(
print('calculating distance')
difference = np.abs(positions - center) # (100, 100, 100, 3)

in_cube = np.all(difference < (np.array(cube_sidelength / pixel_size) / 2), axis=-1)

in_cube = np.all(difference < np.array(cube_sidelength) / (pixel_size * 2), axis=-1)
mask[in_cube] = 1

if wall_thickness != 0:
within_hollowing = np.all(difference < ((np.array(cube_sidelength) / (pixel_size * 2)) - wall_thickness), axis=-1)
mask[within_hollowing] = 0

distance_from_edge = distance_transform_edt(mask == 0)
boundary_pixels = (distance_from_edge <= soft_edge_width) & (distance_from_edge != 0)
normalised_distance_from_edge = (distance_from_edge[boundary_pixels] / soft_edge_width) * np.pi
Expand Down
5 changes: 5 additions & 0 deletions src/ttmask/cuboid.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def cuboid(
soft_edge_width: float = typer.Option(0),
pixel_size: float = typer.Option(...),
output: str = typer.Option("cuboid.mrc"),
wall_thickness: float = typer.Option(0),
):
c = sidelength // 2
center = np.array([c, c, c])
Expand Down Expand Up @@ -43,6 +44,10 @@ def cuboid(

mask[inside_cuboid] = 1

if wall_thickness != 0:
within_hollowing = np.all(difference < ((np.array(cuboid_sidelengths) / (2 * pixel_size)) - wall_thickness), axis=-1)
mask[within_hollowing] = 0

distance_from_edge = distance_transform_edt(mask == 0)
boundary_pixels = (distance_from_edge <= soft_edge_width) & (distance_from_edge != 0)
normalised_distance_from_edge = (distance_from_edge[boundary_pixels] / soft_edge_width) * np.pi
Expand Down
21 changes: 11 additions & 10 deletions src/ttmask/cylinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
def cylinder(
sidelength: int = typer.Option(...),
cylinder_height: float = typer.Option(...),
cylinder_outer_diameter: float = typer.Option(...),
cylinder_inner_diameter: float = typer.Option(0),
cylinder_diameter: float = typer.Option(...),
wall_thickness: float = typer.Option(0),
soft_edge_width: int = typer.Option(0),
pixel_size: float = typer.Option(...),
output: str = typer.Option("cylinder.mrc")
):
cylinder_outer_radius = cylinder_outer_diameter / 2
cylinder_inner_radius = cylinder_inner_diameter / 2
cylinder_radius = cylinder_diameter / 2

c = sidelength // 2
center = np.array([c, c, c])
Expand All @@ -29,15 +28,17 @@ def cylinder(

print('calculating distance')
difference = np.abs(positions - center) # (100, 100, 100, 3)

xy_distance = np.sum(difference[:, :, :, [1, 2]] ** 2, axis=-1) ** 0.5
within_z = difference[:, :, :, 0] < (cylinder_height / (2 * pixel_size))
within_xy = xy_distance < (cylinder_radius / pixel_size)


idx_z = difference[:, :, :, 0] < (cylinder_height / (2 * pixel_size))
idx_xy_outer = xy_distance < (cylinder_outer_radius / pixel_size)
idx_xy_inner = xy_distance < (cylinder_inner_radius / pixel_size)
mask[np.logical_and(within_z, within_xy)] = 1

mask[np.logical_and(idx_z, idx_xy_outer)] = 1
mask[np.logical_and(idx_z, idx_xy_inner)] = 0
if wall_thickness != 0:
within_z_hollowing = difference[:, :, :, 0] < (cylinder_height - wall_thickness) / (2 * pixel_size)
within_xy_hollowing = xy_distance < ((cylinder_radius - wall_thickness) / pixel_size)
mask[np.logical_and(within_z_hollowing, within_xy_hollowing)] = 0

distance_from_edge = distance_transform_edt(mask == 0)
boundary_pixels = (distance_from_edge <= soft_edge_width) & (distance_from_edge != 0)
Expand Down
6 changes: 6 additions & 0 deletions src/ttmask/ellipsoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def ellipsoid(
soft_edge_width: int = typer.Option(0),
pixel_size: float = typer.Option(...),
output: str = typer.Option("ellipsoid.mrc"),
wall_thickness: float = typer.Option(0),
):
c = sidelength // 2
center = np.array([c, c, c])
Expand All @@ -39,6 +40,11 @@ def ellipsoid(
(z_magnitude ** 2) / (z_axis_length ** 2)) <= 1
mask[in_ellipsoid] = 1

if wall_thickness != 0:
in_hollowing = (((x_magnitude) ** 2) / ((x_axis_length - wall_thickness) ** 2)) + ((y_magnitude ** 2) / ((y_axis_length - wall_thickness) ** 2)) + (
(z_magnitude ** 2) / ((z_axis_length - wall_thickness) ** 2)) <= 1
mask[in_hollowing] = 0

distance_from_edge = distance_transform_edt(mask == 0)
boundary_pixels = (distance_from_edge <= soft_edge_width) & (distance_from_edge != 0)
normalised_distance_from_edge = (distance_from_edge[boundary_pixels] / soft_edge_width) * np.pi
Expand Down
5 changes: 5 additions & 0 deletions src/ttmask/sphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def sphere(
soft_edge_width: int = typer.Option(0),
pixel_size: float = typer.Option(...),
output: str = typer.Option("sphere.mrc"),
wall_thickness: float = typer.Option(0),
):
sphere_radius = sphere_diameter / 2
c = sidelength // 2
Expand All @@ -34,6 +35,10 @@ def sphere(
idx = distance < (sphere_radius / pixel_size)
mask[idx] = 1

if wall_thickness != 0:
within_hollowing = distance < ((sphere_radius - wall_thickness) / pixel_size)
mask[within_hollowing] = 0

distance_from_edge = distance_transform_edt(mask == 0)
boundary_pixels = (distance_from_edge <= soft_edge_width) & (distance_from_edge != 0)
normalised_distance_from_edge = (distance_from_edge[boundary_pixels] / soft_edge_width) * np.pi
Expand Down

0 comments on commit 503e312

Please sign in to comment.