A 3D scan-to-CAD inspection pipeline: build a nominal part, simulate a scan of it with known errors, align the scan to the CAD two different ways, and show that the alignment decides which feature gets blamed.
pip install -r requirements.txt
python run.py # part, scan, both alignments, report (~2 min)
python tests/test_inspection.py # 24 checks, closed-form anchorsICP is implemented here from scratch — about sixty lines — rather than called from Open3D. The behaviour that matters is exactly what a library hides.
Same scan. Same 41,414 points. Same measurement. Two coordinate systems:
| feature | best fit (ICP) | datum A|B|C | change |
|---|---|---|---|
| plate top | 98.6% in tolerance | 61.3% | −37 pts |
| boss outer ⌀40 | 73.0% | 92.4% | +19 pts |
| datum A (bottom) | 82.4% | 100.0% | +18 pts |
| boss top | 100.0% | 87.6% | −12 pts |
| bore ⌀20, dowels, sides | 94–100% | 100% | — |
The part was built with a 0.15 mm tilt on its top face and a nearly clean bottom face. Best fit rotates the part until the overall residual is smallest, which quietly absorbs that tilt into the pose — so the tilted face passes at 98.6% and the error is pushed onto datum A, the reference surface, which is fine. Align to the datums instead and the error lands where the fixture and the assembly will actually feel it.
Both are "97% within tolerance" sorts of answers at the headline level — 93.7% against 84.3%. The engineering conclusion is the opposite in each case. Which machine you go and adjust changes.
The scan is synthetic, so the errors are known exactly, and the pipeline can be asked to recover them rather than merely to produce something plausible:
| injected | measured (datum alignment) | error |
|---|---|---|
| boss oversize +0.0800 | +0.0802 | +0.0002 mm |
| bore undersize +0.0400 | +0.0397 | −0.0003 mm |
| top face tilt, mean +0.0750 | +0.0786 | +0.0036 mm |
| plate side, no error | −0.0001 | −0.0001 mm |
Against a measurement noise of σ = 0.012 mm and a tolerance of ±0.10 mm. The end-to-end test asserts every one of these.
Each of these produced confident, wrong numbers first. They are the reason the repo is worth reading.
Trimmed ICP ignores the furthest 10% of correspondences, which is the standard defence against occlusion and outliers. Started that way, ICP converged — cleanly, RMS improvement below 1e-7 — to a pose 9.7 mm along the plate from the correct one.
Measuring the objective directly along that axis shows why:
| offset from true pose | untrimmed RMS | trimmed at 0.9 |
|---|---|---|
| 0 mm | 0.062 | 0.047 |
| 3 mm | 1.218 | 0.813 |
| 7 mm | 2.367 | 1.399 |
| 9 mm | 2.696 | 1.364 |
| 12 mm | 3.291 | 1.522 |
Untrimmed, the objective climbs monotonically — one basin, no ambiguity. Trimmed, it flattens out past 7 mm and stops telling the optimiser anything, because the points carrying the information needed to slide back are precisely the ones furthest away, so trimming throws them out.
Trimming is right near the solution and harmful far from it, so it is annealed: twelve untrimmed iterations to find the basin, then trimming to clean up. The switch is visible as the step at iteration 12 below.
Four prealignment candidates, all refined: they finish at RMS 0.0245, 0.0357,
3.592 and 5.945 mm. The 3.592 mm result reports converged=True — it sat
down in a wrong minimum and stopped improving, which is all convergence means.
One ICP run reports the minimum it happened to land in and has no way to know another exists. Refining every candidate costs four times as much and is the difference between an alignment and a guess.
trimesh.sample.sample_surface(mesh, n, seed=s) builds default_rng(s)
internally. Seeding a local generator with the same s to decide which points
the scanner can see replays the very numbers that positioned those points,
so visibility became a deterministic function of position.
The boss wall came back with 3,180 points on one side and 117 on the other, from a sampler that is uniform in angle to within counting noise. Half a cylinder vanished and the deviation report over what was left would have been perfectly confident.
The fix is independent streams spawned from one seed
(np.random.SeedSequence(seed).spawn(2)), not a different number.
test_scan_visibility_is_independent_of_point_position is the regression test.
Datum A was selected as "points within 1.2 mm of z = 0". That also catches the bottom millimetre of every vertical face around the perimeter — 593 extra points, sitting on surfaces running perpendicular to the datum. A least-squares plane through the mixture was dragged 0.06 mm, most of the part tolerance, by a selection rule that looked entirely reasonable.
Datum features are now selected by surface orientation as well as position, which is what a CAD-driven inspection plan does.
A real part sits on a surface plate and touches it at three high points. Everything else stands off. A least-squares plane runs through the surface with roughly half the points on each side, and no part ever rests there.
Datum A on this part is bowed 0.05 mm, so the two definitions differ:
| datum plane fit | datum A | plate top | boss | within tolerance |
|---|---|---|---|---|
| injected | −0.0190 | +0.0750 | +0.0800 | — |
| least squares | −0.0001 | +0.0565 | +0.0796 | 90.56% |
| contacting (used here) | −0.0225 | +0.0786 | +0.0802 | 84.30% |
The least-squares datum under-reports the tilt by 0.0185 mm — 18% of the whole tolerance — and makes the part look six points better than it is. The contacting plane is solved as a linear program: minimise total stand-off subject to every point lying on the material side.
But the first version of that fix was worse than the problem. An unfiltered contacting fit rests on the most extreme point it can find, which on real data is the worst noise spike: it settled within 0.002 mm of the single lowest sample out of 4,000 and overshot the tilt by 0.028 mm, having been 0.018 mm short. Averaging within 5 mm cells first drops the noise without touching the form, which is why real metrology filters before fitting (ISO 16610). It cuts the bias about fourfold rather than eliminating it — a contacting fit is still decided by the extreme of whatever it is given.
Positive means extra material. An oversized boss and an undersized bore both come out positive, because both have too much metal. Unsigned distance cannot distinguish a part that is uniformly oversize from one that is uniformly undersize.
Conformance has three outcomes. 6,501 points fail. 2,892 more are inside tolerance but within the expanded uncertainty U = 0.015 mm of the limit — this measurement has not proven them either way (ISO 14253-1). Calling those a pass ships the measurement uncertainty to the customer.
Percent-within-tolerance is area-weighted as well as counted — 84.30% by count against 88.30% by area. A scanner samples the faces it points at far more densely than the ones it grazes, so a point count partly measures the setup rather than the part. Local spacing estimates the area each point stands for.
The part is deliberately asymmetric. Two identical dowel holes on the centreline map onto themselves under a 180° rotation, and best fit then finds two poses with identical RMS — because they are identical. No measurement can choose between them. Offsetting datum C breaks it in the geometry, which is where it has to be broken (Poka-Yoke). The early version was symmetric and the ambiguity showed up as two candidates tying exactly.
Occluded points are absent, not zero. The lower bore wall is never seen, so it is removed. A report that silently fills it with zeros claims the feature was inspected.
Cylinder faceting is a real error source. At ⌀40 with 128 sections the sagitta is 0.006 mm, under half the measurement uncertainty. At 16 sections it would be 0.38 mm and would swamp the 0.10 mm tolerance — the faceting of a CAD export is a genuine contributor, not an artefact of this model.
| File | Role |
|---|---|
src/part.py |
Nominal bracket built from primitives, with its datum scheme. |
src/scan.py |
Synthetic scan: form errors, misalignment, noise, density bias, occlusion. |
src/align.py |
Kabsch, point-to-plane ICP, prealignment, plane/circle fits, datum alignment. |
src/deviation.py |
Signed deviation, area weighting, guard-banded verdicts, per-feature stats. |
src/charts.py, src/report.py |
Four figures and a printable PDF report. |
src/paper.py |
Page composition: A4 pages, tables, figure bands. |
run.py |
Everything, in order. |
data/nominal.stl |
The nominal part, exported. |
Point-to-plane rather than point-to-point ICP: on a part made mostly of flat faces, point-to-point barely penalises sliding along a surface and convergence crawls. Point-to-plane pins motion across each face while letting the scan slide freely over it, which is the right physics for planes and cylinders.
24/24 passed
Closed-form anchors and ground truth, not golden output:
- Kabsch recovers a known rigid transform to 1e-9, and never returns a reflection — an unguarded SVD will mirror a left-hand part into a conforming right-hand one
- a circle fit and a plane fit recover known values exactly
- ICP on a noise-free, error-free scan returns to the true pose within 0.05 mm and 0.05°
- a point 0.05 mm outside the material reads +0.050, one inside reads −0.050, and a uniformly offset face reads the offset everywhere
- 0.095 mm on a 0.100 mm tolerance with U = 0.015 mm is indeterminate, not a pass
- the contacting plane rests on a dished surface instead of cutting through it, and needs filtering to survive noise (the regression test for the fix that first made things worse)
- datum C may clock the frame but must not tilt it — the hierarchy, as an assertion
- scan visibility is independent of point position (the seed-collision test)
- the full pipeline recovers every injected error to within 0.01 mm
- the two alignments demonstrably blame different features
Three of those tests failed first on bugs in the tests, not the code — two placed probe points on the boss axis, where the through-bore means there is no material to be outside of, and one asserted a bias magnitude I had guessed instead of measured. The measured values are now in the assertions.
| English | Deutsch |
|---|---|
| Deviation / nominal-actual comparison | Abweichung / Soll-Ist-Vergleich |
| Alignment / registration | Ausrichtung / Registrierung |
| Best fit | Bestpassung |
| Datum / datum system | Bezug / Bezugssystem |
| Contacting (tangent) plane | Anlageebene / Tastebene |
| Form and position tolerance | Form- und Lagetoleranz |
| Profile tolerance | Profiltoleranz |
| Measurement uncertainty | Messunsicherheit |
| Conformance / acceptance zone | Konformität / Annahmebereich |
| Coordinate measuring machine | Koordinatenmessgerät (KMG) |
| Point cloud | Punktwolke |
| Scrap / rework | Ausschuss / Nacharbeit |
| Flatness | Ebenheit |
| Mistake-proofing | Poka-Yoke / Fehlervermeidung |
The part and the scan are synthetic. The inspection is not: real ICP, real plane and circle fitting, real datum hierarchy, real signed deviation against a triangulated surface, and a ground-truth check on every result.
Not implemented: reading a real scanner's output format (the pipeline takes an
(n, 3) array, so this is an importer, not a change to the method), true GD&T
evaluation of individual callouts (flatness, position, runout) as opposed to
profile-style deviation, minimum-zone fitting per ISO 5459 rather than the
contacting plane used here, curvature-aware or feature-based prealignment for
parts without three distinct extents, and an uncertainty budget built up from
its contributors instead of a single declared U.
The deviation is also point-to-surface, which slightly under-reports on sharp convex edges where the nearest surface point is the edge itself rather than the face the point belongs to.
Built by Prathamesh Bedre — M.Sc. Intelligent Manufacturing, TU Clausthal. Companion repos: cmm-spc-dashboard (capability and control charts from CMM data), tolerance-stackup (worst case, RSS and Monte Carlo), opcua-machine-monitor (OPC UA → historian → OEE) and production-line-sim (discrete-event line simulation).


