Signed Distance Function - Box

Your browser does not support the HTML5 canvas tag.
0

Signed distance function of a box:

The signed distance function of a box with width $w$ and height $h$ is defined as: $$\Phi(\mathbf{x}) = \min(\max(d_x,d_y),0.0) + \left \|\begin{pmatrix} \max(d_x,0.0) \\ \max(d_y,0.0) \end{pmatrix} \right \| - \text{tolerance},$$ where $$d = \begin{pmatrix} |x - x_{\text{box}}| - 0.5 w \\ |y - y_{\text{box}}| - 0.5 h \end{pmatrix}$$. In the example $\mathbf x$ is the red point, $\mathbf{x}_{\text{box}}$ the blue point and the closest point to $\mathbf x$ on the surface of the sphere is rendered in yellow.

Surface normal vector

The normal vector is approximated using central differences, where the derivative of a function $f(x)$ is approximated by $$ \frac{\partial f(x)}{\partial x} \approx \frac{f(x+\varepsilon) - f(x-\varepsilon)}{2\varepsilon}, $$ where $\varepsilon$ is a small constant (in our example $\varepsilon = 10^{-6}).

Since the normal vector is defined as $$ \mathbf n = \frac{\partial \Phi(\mathbf{x})}{\mathbf x}, $$ the normal can be simply approximated by applying central differences on the x- and the y-component of the function $\Phi(\mathbf{x})$.

Closest point on the surface

The closest point $\mathbf s$ on the surface of the box (yellow) can be determined by starting at the point $\mathbf x$ (red) and going by the signed distance in the direction of the negative normal vector: $$\mathbf s = \mathbf x - \Phi(\mathbf{x}) \mathbf n.$$