Dilation in Geometry for Programmers
Dilation is a fundamental operation in the field of geometry and computer graphics, particularly when working with Signed Distance Functions (SDFs) and voxel models. It involves expanding a shape or model by adding material to its boundaries. This process can be visualized and implemented in various ways, as described below.
Dilation in Signed Distance Functions (SDFs)
In the context of Signed Distance Functions, dilation is an offset operation that enlarges the model. This is achieved by subtracting a dilation distance from the SDF. The formula for a dilated SDF can be expressed as:
SDF_dilated = SDF - offset
This operation effectively increases the size of the model by the specified offset distance.
[Figure 10.15: Dilation and Erosion of a Circle](https://livebook.manning.com/book/geometry-for-programmers/chapter-10/158)
Figure 10.15 A circle with a radius of 1 dilated by 0.1 (on the left) and eroded by 0.1 (on the right)
This figure illustrates the effect of dilation on a circle with a radius of 1. The circle is dilated by 0.1 on the left, showing an increase in size, while the right side demonstrates erosion, where the circle is reduced by 0.1.
Dilation in Voxel Models
Dilation can also be applied to voxel models, where it involves growing an image by adding voxels to its border. This process checks neighboring voxels and fills empty ones if they have a filled neighbor within a certain distance.
[Figure 12.6: Dilation in Voxel Models](https://livebook.manning.com/book/geometry-for-programmers/chapter-12/53)
Figure 12.6 Dilation adds “material” to your voxel model.
This figure demonstrates how dilation adds material to the border of a voxel model, effectively increasing its size.
[Figure 12.9: Dilation by One Voxel](https://livebook.manning.com/book/geometry-for-programmers/chapter-12/53)
Figure 12.9 Dilation by one voxel
This figure shows the result of dilating a model by one voxel, highlighting the addition of material to the model's boundary.
Implementing Dilation in Code
The following Python function demonstrates how to perform dilation on a 2D image represented as a grid of voxels. The function dilate takes an image and a depth value, and it modifies the image by adding voxels to its border based on the specified depth.
def dilate(img, depth):
distances = [[inf for _ in row] for row in img] #1
for i in range(len(img)): #2
for j in range(len(img[0])):
if img[i][j] == 1:
distances[i][j] = 0
continue
if j > 0 and img[i][j-1] == 1:
distances[i][j] = 1
if i > 0 and img[i-1][j] == 1:
distances[i][j] = 1
candidates = [distances[i][j]]
if j > 0:
candidates += [distances[i][j-1] + 1]
if i > 0:
candidates += [distances[i-1][j] + 1]
distances[i][j] = min(candidates)
for i in reversed(range(len(img))): #3
for j in reversed(range(len(img[0]))):
if img[i][j] == 1:
distances[i][j] = 0
continue
if j < len(img[0]) - 1 and img[i][j+1] == 1:
distances[i][j] = 1
if i < len(img) - 1 and img[i+1][j] == 1:
distances[i][j] = 1
candidates = [distances[i][j]]
if j > 0:
candidates += [distances[i][j-1] + 1]
if i > 0:
candidates += [distances[i-1][j] + 1]
if j < len(img[0]) - 1:
candidates += [distances[i][j+1] + 1]
if i < len(img) - 1:
candidates += [distances[i+1][j] + 1]
distances[i][j] = min(candidates)
for i in range(len(img)): #4
for j in range(len(img[0])):
if distances[i][j] <= depth:
img[i][j] = 1
return img
#1 Initializes the distance map with infinite values to denote still-undefined distances
#2 Performs the first travel: left to right, top to bottom
#3 Performs the reverse travel: right to left, bottom to top
#4 Makes all the voxels with a distance lower or equal to the depth value part of the model
This code initializes a distance map, performs two passes over the image to calculate distances, and finally updates the image to include voxels within the specified depth.
FAQ (Frequently asked questions)
What is dilation in the context of SDFs?
What does a dilated circle look like?
How do you represent a dilated SDF?
How does dilation work on 3D images?
What is shown in Figure 12.9?
How is dilation implemented in the provided Python code?