liveBook · Manning

Quasi-signed distance function

As explained in Geometry for Programmers

A quasi-signed distance function is an approximate scalar field whose values indicate signed proximity to a target surface without necessarily equaling true signed distance.

A quasi-signed distance function is a smooth, signed scalar field whose zero contour defines a target surface, even though its values do not measure true distance. The approximation is useful when converting pixel data into a continuous geometric boundary. section 12.4.5

A practical construction follows these steps:

  • Represent pixel values as a function, then use inverse-weight interpolation to make the function continuous and smooth. section 12.4.5
  • Subtract a threshold so the desired boundary becomes the zero isoline and the field becomes signed. section 12.4.5
  • Extract an initial pixel-border contour, iteratively fit its vertices to the zero contour, and replace segments with cubic curves. section 12.4.5
  • Normalize gradient-derived tangent vectors when their magnitudes are out of scale; otherwise, smooth curves can form unwanted loops. section 12.4.4
w = 16                                                               #1
h = 16                                                               #2
 
img = [                                                              #3
[0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
[0,  77, 125,38, 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
[0,  120,255,254,203,144,96, 3,  0,  0,  0,  0,  0,  0,  0,  0],
[0,  34, 253,255,255,255,255,230,154,94, 8,  0,  0,  0,  0,  0],
[0,  0,  196,255,255,255,255,255,255,252,241,139,83, 6,  0,  0],
[0,  0,  149,255,255,255,255,255,255,255,255,255,250,213,80, 0],
[0,  0,  98, 255,255,255,255,255,255,255,255,255,255,224,58, 0],
[0,  0,  2,  224,255,255,255,255,255,255,255,242,152,4,  0,  0],
[0,  0,  0,  145,255,255,255,255,255,255,255,154,1,  0,  0,  0],
[0,  0,  0,  82, 251,255,255,255,255,255,255,253,156,1,  0,  0],
[0,  0,  0,  6,  237,255,255,255,255,255,255,255,252,146,3,  0],
[0,  0,  0,  0,  149,255,255,243,149,252,255,255,255,240,21, 0],
[0,  0,  0,  0,  69, 249,255,152,1,  150,252,255,238,71, 0,  0],
[0,  0,  0,  0,  0,  211,224,4,  0,  1,  137,240,86, 0,  0,  0],
[0,  0,  0,  0,  0,  73, 57, 0,  0,  0,  2,  20, 0,  0,  0,  0],
[0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0]
]

The Python image array provides the concrete input for constructing and sampling a quasi-SDF. section 12.4.5

Image to smooth contour

For a 16×16 grayscale image, choose a distance threshold of 128 and define the field as the interpolated image value minus that threshold. Sign changes across neighboring pixel edges produce an initial border; finite-difference gradients then move its vertices toward the zero contour before cubic polynomials smooth the result. section 12.4.5

How Geometry for Programmers covers it

How deep it goes
Explains smoothing, signing, contour fitting, gradients, and tangent normalization, but stops short of true distance guarantees.
Examples
Uses a thresholded 16×16 image, pixel-border extraction, iterative vertex fitting, and cubic contour segments.
What it assumes
Assumes familiarity with signed distance functions, interpolation, gradients, and iterative contour fitting.
What you can do after
Build it from scratch by defining a smooth signed field and fitting a smooth contour to its zero isoline.
How it connects
Connects signed distance functions, interpolation, gradients, isolines, contour fitting, and cubic polynomials.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Geometry for Programmers. The section links point to the parts of the book it draws on.