A robot inspection system may compare a new profile scan with a set of sampled reference points. A scan that stops early can look perfect when each observed point lands exactly on the reference. A different question is whether the scan covers every sampled part of the reference. The order of the two point sets matters when the check uses a directed distance.
For a small fixture, place four reference points along a straight edge at x positions 0, 1, 2, and 3 millimeters. The scan contains the first three points and ends before the final one. The largest distance from any scan point to its nearest reference point is zero. Looking from the reference toward the scan, the endpoint at x equal to 3 is one millimeter away from its nearest scan point.
Keep both directions in the report
SciPy's directed_hausdorff calculates the largest nearest-point distance in one direction using Euclidean distances. The first input provides the points whose nearest neighbors are checked in the second input. The result includes the two point indices responsible for the reported maximum, which can help locate the gap in a scan viewer.
The fixture below uses x and y coordinates in millimeters. It also computes the reference-to-scan distance from a small pairwise distance matrix as a cross-check.
import numpy as np
from scipy.spatial.distance import cdist, directed_hausdorff
reference = np.array([[0, 0], [1, 0], [2, 0], [3, 0]])
scan = np.array([[0, 0], [1, 0], [2, 0]])
scan_fit = directed_hausdorff(scan, reference)[0]
coverage_gap, ref_idx, scan_idx = directed_hausdorff(reference, scan)
assert (scan_fit, coverage_gap) == (0.0, 1.0)
assert (ref_idx, scan_idx) == (3, 2)
assert cdist(reference, scan).min(axis=1).max() == coverage_gap
scan_with_extra = np.vstack([scan, [2, 3]])
assert directed_hausdorff(scan_with_extra, reference)[0] == 3.0
assert directed_hausdorff(reference, scan_with_extra)[0] == 1.0
The extra return is three millimeters above a point that belongs to the reference. It increases the scan-to-reference distance to three millimeters. It leaves the reference-to-scan distance at one millimeter because no reference point has a closer scan neighbor after the extra return is added. A single value formed from the larger direction can summarize the worst discrepancy, while the two directional values and their point pairs help distinguish the causes.
Check the point sets before interpreting the result
Compare points in the same coordinate frame and units, and check that both sets contain finite points. An alignment error can increase the distances in both directions. Reconstructing the validated reference sampling is also important. Sparse sampling on a curved surface can increase nearest-point distances even where the instrument sees the expected surface. Keep the sampling interval, point count, and alignment parameters with the report.
The SciPy cdist reference describes the pairwise distance matrix used for the small cross-check. For larger scans, explicitly forming that matrix can consume substantial memory because it contains one entry for each possible pair. Use the directed routine for the reported maximum and retain the responsible point indices so a reviewer can inspect the underlying measurements.
Sources
SciPy directed_hausdorff reference: https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.directed_hausdorff.html
SciPy cdist reference: https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.cdist.html