Reverse Engineering: Mesh to Parametric CAD
Best practices for reconstructing parametric models from reference STL meshes,
adapted for programmatic JSCAD generation without a CAD kernel.
Standard Pipeline (Scan-to-CAD)
The established pipeline from commercial tools (Geomagic, SpaceClaim, SolidWorks):
- Pre-processing — noise removal, hole filling, mesh simplification
- Segmentation — partition mesh into regions of uniform geometric type
- Surface fitting — fit analytic surfaces or freeform surfaces to each segment
- Feature recognition — identify CAD features from the fitted surface network
- Parametric model construction — build a feature tree that reproduces the shape
Our pipeline (implementable in TypeScript on triangle meshes):
- PCA → establish coordinate frame, find primary axis
- Cross-section slicing along primary axis → contour polygons at each height
- Circle/ellipse fitting to each contour → radial profile for revolve body
- Topology change detection in cross-sections → feature branch points
- Shape decomposition → isolate body, spout, handle, base
- Skeleton extraction for tubular features → hullChain centerline path
- B-spline fitting to profiles and centerlines
- Validation using RMS surface distance
Feature Recognition Methods
Axis of Revolution Detection
- For a surface of revolution, all vertex normals intersect the axis
- Practical: slice mesh at multiple heights, fit circles, axis = line through centers
- Source: Li et al., "GlobFit" (SIGGRAPH 2011)
Tubular Feature Detection (spouts, pipes)
- Extract curve skeleton (medial axis), verify circular cross-sections
- Or: chain adjacent cylindrical RANSAC segments along a spine curve
- Source: Bénière et al., "Comprehensive reverse engineering" (CAD 2013)
Toroidal Features (handles, loops)
- Gaussian curvature changes sign on a torus (+ outer, - inner)
- Topologically: a handle creates genus-1 (loop that doesn't bound a disk)
- Source: Attene et al., "Hierarchical mesh segmentation" (Visual Computer 2006)
Prismatic Features (stepped bases)
- Detect planar regions via RANSAC or normal clustering
- Group co-planar faces, detect step transitions at different heights
- Source: Sunil & Pande, "Automatic feature recognition" (CAD 2008)
Skeleton Extraction for Tubular Features
The centroid-of-cross-sections method is the simplest and most robust:
- Isolate the tubular sub-mesh (via segmentation)
- Identify the two boundary loops (body junction and opening)
- Compute geodesic distance from one boundary
- At each distance value, find the centroid of the level-set contour
- The sequence of centroids IS the centerline → use as hullChain path
- At each point, cross-section radius → sphere radius in the hullChain
More sophisticated: Laplacian mesh contraction (Au et al., SIGGRAPH 2008)
— iteratively contract mesh toward skeleton. ~10-20 iterations of solving
a sparse linear system. Produces clean 1D skeleton with branch detection.
Shape Decomposition
Shape Diameter Function (SDF) — best for our use case
For each face, cast rays inward, measure local thickness.
Parts with different thickness cluster separately:
- Body: thick
- Spout: thin tube
- Handle: thin curved tube
- Base: medium flat
Source: Shapira et al., "Consistent mesh partitioning using SDF"
(Visual Computer 2008)
Dihedral Angle + Graph Cut
Compute dihedral angle at every mesh edge. Concave edges (angle > 180°)
are likely part boundaries. Find minimum-weight cycle separating regions.
Source: Katz & Tal, "Hierarchical mesh decomposition" (SIGGRAPH 2003)
Practical approach for our pipeline
- Cross-section slicing along primary axis
- At each height, compare measured width/depth against revolve body radius
- Where measured extent > body radius + threshold → feature present
- Classify direction: +Y = spout, -Y = handle, stepped widths = base
- Extract feature points at each height → centerline for hullChain
Curve Fitting: B-Splines, Not Polynomials
Why polynomials fail: A single polynomial is C∞ smooth. Real profiles have
tangent discontinuities at feature transitions. Higher degree → Runge oscillation.
B-splines are the standard:
- Piecewise polynomial, degree p, C^{p-1} at interior knots
- Degree 3 (cubic) is the default. C² continuity looks smooth.
- Reduce continuity at feature boundaries by repeating knots
- 10-20 control points suffice for complex profiles
Fitting procedure:
- Parameterize data using chord-length: tᵢ = tᵢ₋₁ + |pᵢ - pᵢ₋₁| / total
- Place knots — uniform or at detected feature boundaries
- Solve least-squares: minimize Σ|C(tᵢ) - pᵢ|² (linear system)
Handling discontinuities:
- Detect jumps in first derivative of profile data
- Segment profile at discontinuities
- Fit separate B-splines to each smooth segment
- Join with C⁰ at sharp corners, C¹ at soft transitions
Implementation:
- B-spline evaluation: de Boor's algorithm (~30 lines)
- Cubic interpolating spline: tridiagonal linear system, O(n)
- npm:
nurbs package, or implement de Boor directly
Sources:
- Piegl & Tiller, The NURBS Book (Springer, 1997) — Chapter 9
- de Boor, A Practical Guide to Splines (Springer, 2001)
Validation Metrics
RMS Surface Distance (primary metric)
- For each vertex on model A, find closest point on B's surface
- Report: mean, RMS, max (Hausdorff), 90th percentile
- This is what commercial scan-comparison tools report
Hausdorff Distance
- Maximum distance any point must travel to reach the other surface
- Use 90th/95th percentile variant for robustness against outliers
- Source: Aspert et al., "MESH" (IEEE ICME, 2002)
Volume IoU (Intersection over Union)
- Voxelize both meshes, count agreeing/disagreeing voxels
- Single 0-1 score for overall shape match
- Fast, easy to implement
Cross-Section Comparison
- Slice both meshes at same heights, compare 2D contours
- Metrics: area difference, contour Hausdorff, width/depth span difference
- Natural for cross-section-based reconstruction
Our current metrics and what to add
| Have | Missing |
|---|
| Aligned AABB comparison | RMS surface distance |
| PCA sorted lengths | Hausdorff distance (90th pct) |
| Profile match (PCA) | Volume IoU |
| MultiSlice span comparison | Per-feature comparison |
| Symmetry analysis | Cross-section area comparison |
Summary: Recommended Improvements
- Add SDF-based decomposition to
detectFeatures — classify by thickness
- Add skeleton extraction (centroid-of-cross-sections) for tubular features
- Replace polynomials with cubic B-splines in
fitProfiles
- Add RMS surface distance as validation metric
- Update
generateScript to:
- Decompose into features first
- Use extrudeRotate for the body (from B-spline profile)
- Use hullChain with skeleton-extracted centerline for tubes
- Use torus for detected loops
- Use stacked cylinders for stepped bases
Sources:
- Varady et al., "Reverse engineering of geometric models" (CAD 1997)
- Schnabel et al., "Efficient RANSAC" (CGF 2007)
- Au et al., "Skeleton extraction by mesh contraction" (SIGGRAPH 2008)
- Shapira et al., "SDF mesh partitioning" (Visual Computer 2008)
- Piegl & Tiller, The NURBS Book (1997)
- Besl & McKay, "ICP registration" (IEEE TPAMI 1992)