Algorithmadvanced
Mid-surface extraction
Also called midsurface abstraction, dimensional reduction of thin solids.
Replaces a thin-walled solid by its mid-surface with thickness attributes, by pairing opposite faces or by the medial axis, so shell elements can model it.
- Time
- O(n^2 k + m^2) worst
- Space
- O(n) extra
- Where
- k = largest number of shrink steps for one sample; m = number of kept medial points; n = number of boundary samples
- Model
- Real RAM
- Properties
- deterministicnot exactiterative
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Mohsen Rezayat, 1996
A thin-walled part, such as a sheet metal bracket, a plastic housing or a ship hull, is a solid whose thickness is small compared with its other sizes. A mesh of solid elements through such a thin wall needs several small elements across , so it is large. Shell elements, such as isogeometric Kirchhoff-Love shells or subdivision shells, model the wall by a surface with a thickness value instead. Mid-surface extraction builds that surface from the CAD solid: the surface halfway between the two faces of the wall, with the local thickness at each point. There are two families. Face pairing finds pairs of opposite faces and offsets one of them by half the distance (Rezayat 1996). Medial methods take the medial axis in 2D or the medial surface in 3D and remove its branches that do not belong to the wall. This page gives the medial method on sampled boundaries in 2D, where the mid-surface is a midline of a cross-section, proves that it is exact on a strip and an annular sector, proves the error when the samples on the two sides do not line up, and measures the result.
In plain words
Think of a thin plate. For a strength check, engineers like to replace it by a flat sheet with no thickness that runs through its middle, and to write the thickness on the sheet as a number. The computation on the sheet is much faster.
To find the middle, roll a ball inside the part so that it always touches both faces of the wall. The center of the ball traces the middle, and the size of the ball gives the thickness.
The method here starts at each point of the border with a huge ball that touches it, then shrinks the ball until no other border point is inside. The final ball touches a second point, usually on the other face. Balls that touch two points on nearly opposite sides belong to the middle. Balls that touch two neighbors near a corner are thrown away.
The problem
The input is a region , the cross-section of a thin wall, given by samples of its boundary and the unit inward normals there. The output is a set of polylines that approximate the midline, with a thickness at each vertex.
The reference midline is defined by the medial axis: the set of centers of the maximal disks in . A medial point with radius touches the boundary in at least two points. Its separation angle is the angle at between two of them. The midline is the part of the medial axis where this angle is close to , so that the disk touches two opposite faces of the wall. The other branches run into corners, where the angle is close to or smaller, and they are not part of the wall.
For a sample with inward normal , the disks that touch the boundary at from inside are with . A sample lies strictly inside if and only if , where
is the radius of the circle tangent to the boundary at through . So the largest disk touching at that holds no sample in its interior has the radius .
The algorithm
MIDLINE(samples p_1..p_n, inward normals n_1..n_n, start radius r_0, angle θ_min, radius r_min, link length ℓ)- 1
for i = 1 to n do - 2
c ← p_i + r n_i ▷ the center, with r = r_0 at the start - 3
j ← the sample nearest to c - 4
if j = i or |p_j - c| ≥ r then go to L6 ▷ no sample inside the disk - 5
r ← ρ(p_j); go to L2 ▷ shrink until p_j is on the circle - 6
if a sample p_k touched the disk then θ ← the angle at c between p_i and p_k, else skip p_i - 7
if θ ≥ θ_min and r ≥ r_min then keep (c, 2r) - 8
link the kept centers into polylines, joining centers closer than ℓ
Lines L2 to L5 are the shrinking ball of Ma, Bae and Choi (2012). Line L5 shrinks the disk to the circle through the nearest sample , and this sample becomes the touching sample of line L6. The loop stops when the nearest sample to the center is on or outside the circle. Line L7 keeps the disks that touch two nearly opposite points, , and drops the small disks that touch two neighboring samples, . The code uses , , the largest distance from a sample to its nearest neighbor, and times the size of the bounding box. Line L8 builds the connected components of the graph with edges shorter than and walks each from its farthest point by nearest neighbors. The code uses equal to three times the largest gap between consecutive samples. In 3D the same lines give points of the mid-surface, and line L8 becomes the meshing of a point set with a thickness at each point.
Example
All runs use the code of this page, with fixed inputs.
A strip. The strip has . Its long sides are sampled at spacing , at the points , and the short sides at the points . No sample is a corner. With aligned samples, the same on both sides:
| run | samples | kept | largest of a center | largest | range | chains |
|---|---|---|---|---|---|---|
| aligned | 220 | 180 | 0 | 0 | 0.55 to 9.45 | 1 |
| top side shifted by | 219 | 181 | 0.00125 | 0.00125 | 0.5 to 9.5 | 1 |
| aligned, no pruning (, ) | 220 | 220 | 0.45 |
The aligned run is exact to the last bit. With the shifted side, the error is , the bound of the correctness section, attained. Without pruning, the 40 samples near the ends give disks with a separation angle of exactly , on the four corner bisectors. The kept centers end at about and , not at the ends of the plate. Each midline is one chain with thickness . The shrinking ball took 1.8 steps per sample on average and 8 at most, from .
An annular sector. The sector , has . Both arcs are sampled at the same 120 angles , and each straight end at 10 points, 260 samples in all. The run keeps 208 centers. All lie on the mean arc with , to . Their angles run from to , and the exact medial arc starts at and ends at , so the kept range matches it within one sample spacing. The midline is one chain.
Correctness
The proof page proves four facts, with exact arithmetic.
- The shrinking ball. If , lines L2 to L5 stop after at most shrinks with , the largest empty disk that touches at . The key fact is that the disks are nested and that is inside exactly when . If no sample has , the disk never shrinks and no sample touches it.
- The strip. For the rectangle with , the medial axis is the midline segment from to , where the separation angle is , and four corner branches, where it is . So any in keeps exactly the segment.
- The sampled strip. Let be a sample on a long side at distance more than from both short sides, where is the smallest offset in from to a sample on the other side. Then the algorithm gives the center at distance from the midline and the radius , with the separation angle . Aligned samples, , give the exact midline, and samples shifted by give the error .
- The annular sector. If and , the points of the medial axis with a separation angle above form exactly the arc , . If both arcs are sampled at the same angles, every arc sample in this range gives a center on that arc with exactly.
The tests check facts 1, 3 and 4 and a tapered strip, where is the thickness measured normal to the sides. The behavior of the samples near the ends, which the pruning removes, is measured, not proved.
Proof: The shrinking ball finds the largest empty tangent disk, and the medial midline is exact on a strip and an annular sectorfull
We use the notation of the page. Arithmetic is exact. For a sample with unit inward normal and , let and . For a sample , let if , and otherwise. Let .
Lemma 1 (inside test). A point lies in the interior of if and only if . It lies on the circle if and only if .
Proof. , because . If , this is at least , so is outside for every , which matches . If , the sign of the expression is the sign of .
So the disks grow with , and holds no sample in its interior exactly when .
Theorem 1 (the shrinking ball). Let . Lines L2 to L5 stop after at most shrinks, with and a touching sample with , which lies on the final circle. If , they stop at once with and no touching sample.
Proof. Invariant: . It holds at the start. Line L5 sets , which is at least by the definition of the minimum. Stop test: the sample lies on every circle, at distance from . So if the nearest sample to is , or if , then no sample lies in the interior of , and Lemma 1 gives . With the invariant, . Shrink step: if the test fails, lies in the interior, so by Lemma 1, and line L5 decreases strictly. Each new value of is for a sample , and the values strictly decrease, so no sample is used twice, and there are at most shrinks. At the end the last touching sample has , so it lies on the circle by Lemma 1. If , no sample satisfies , so the first test succeeds and stays .
Theorem 2 (the strip). Let with . The medial axis of is the segment from to , where the maximal disks have radius and the separation angle is , together with four segments on the corner bisectors, where the separation angle is . So pruning with any in keeps exactly , and is shorter than the midline of the plate by at each end.
Proof. For an interior point , the distance to the boundary is , the distance to the nearest side. The nearest point on a side is the foot of the perpendicular, since projects inside every side of the rectangle. For a convex polygon, is on the medial axis exactly when at least two sides reach the minimum, or is a limit of such points. Two sides tie in these cases. The top and bottom tie when , and they are nearest when also and , which gives . The feet are , opposite each other through , so the separation angle is and the radius is . A long side and a short side tie on a corner bisector, for example for the left and the top side, and they are nearest for and . The feet are and , in the directions and from , so the separation angle is . The left and right sides cannot both be nearest, because that needs and , against . The four bisector segments meet only at its ends, where the top and bottom also tie and the angle is available. So the points with a separation angle above form , and ends at and .
Theorem 3 (the sampled strip). Sample with points on its sides and with the inward normals of the sides. Let be a sample on the bottom side, let be the smallest value of over the samples on the top side, let , and assume . Then the shrinking ball at ends with the radius , the center , and a touching sample on the top side, and the separation angle is . The same holds for the top side with the sign of the center reversed. So line L7 keeps when and .
Proof. Here . A sample on the bottom side has , so . A sample on the top side has and , so , which is smallest, equal to , for the offset . A sample on a short side, for example the left one, is at distance from the vertical line through the center , so it lies outside , and Lemma 1 gives . The right side is the same with . So , attained only on the top side, and Theorem 1 gives the radius and a touching sample on the top side with , so . The center is . For the angle, take the touching top sample , with either sign of the offset. From , the vector to is and the vector to is , of length . So the cosine of the angle is , and the angle is . The last claim follows from line L7.
Corollary. If both long sides are sampled at the same values of , then , and every kept sample of Theorem 3 gives a center on the midline with radius exactly . If the samples are at spacing and the top side is shifted by , then , and the center and the radius are both off by , and this value is attained.
Theorem 4 (the annular sector). Let , , , and assume , with . Let . (a) The points of the medial axis of with a separation angle above form exactly the arc , with radius and angle . (b) If both arcs are sampled at the same angles , then for every sample on an arc with , the shrinking ball gives the center and the radius exactly.
Proof. Write and take a point of . Its distance to the inner arc is and to the outer arc is , with the nearest points and , in the directions and from . So the distance to the nearer arc is at most . Its distance to the line of the straight end is , with the foot in the direction , and the same holds for the end with . The nearest point on an end segment is either this foot or a corner of . A corner is never strictly nearer than an arc: for the corner , , and for , , and the same for the corners at . So a medial point is a tie of the minimum distance between two of the four pieces, with the nearest points as above.
Arc and arc: gives and the distance , and the ends must be no nearer. If , the foot on the line has the abscissa , which lies in , so the foot is on the end segment, at the distance , and the end is strictly nearer. If , then because , so the distance to the line, and hence to the segment, is at least . The end is the same with . So the tie gives exactly . The nearest points are opposite through , so the angle is . This is . Arc and end, for the end : the nearest point on the end is its foot, which needs , and the distance is at most , so and . The angle between and has the cosine , so it is . The end is the same with . End and end: the tie needs and , so , against . An arc or an end alone has a unique nearest point, since the origin is not in . So the points with an angle above form , which proves (a).
For (b), let with . The disk has the center . The outer sample has . Another outer sample , with , has and . If , then . Otherwise is , which expands to , true, and strict since . An inner sample has , so . A sample on the end lies on a line at distance from , so it is not in the interior of , and Lemma 1 gives ; the end is the same. So , attained by , and Theorem 1 gives the radius and the center . A sample on the outer arc is the same with and the roles of the arcs swapped.
Complexity
In the real RAM model, with samples, at most shrinks per sample and kept points:
- Shrinking. Each step of line L3 is a linear scan, , and each sample takes at most steps, so lines L1 to L7 cost . Fact 1 gives , so the worst case is . In the example and the mean is 1.8.
- Nearest neighbors. A kd-tree built once over the samples answers line L3 in expected time for well spread samples, so the shrinking costs expected after an build.
- Linking. Line L8 compares all pairs, , and the default costs by the same scan. Both drop to and with a kd-tree.
- Space. for the samples and the kept points.
Properties
- Local. Each sample is processed alone, so the loop of line L1 runs in parallel without change.
- Thickness for free. The radius of each kept disk is half the local wall thickness, measured normal to the faces.
- Needs normals. The inward normals at the samples must be known. A CAD solid gives them, a scan needs an estimate.
- Short at free edges. The medial axis of a wall stops half a thickness before each free edge of the wall, so the result needs an extension there to match the mid-surface that an engineer expects.
- Not exact. Sampling, pruning and linking all add errors. Only special shapes with aligned samples come out exactly.
Variants and improvements
- Face pairing. Rezayat (1996) finds pairs of faces of the solid that are close and nearly parallel, builds a midsurface patch between each pair, and then extends and joins the patches across the junctions. It works on the exact CAD surfaces and keeps their type, but the joining step is hard at T-joints and where the thickness changes.
- Exact medial surfaces. A medial surface of a polyhedral solid can be traced exactly, then pruned in the same way by the angle or by the ratio of the size of a branch to its radius.
- Shrinking ball in 3D. Ma, Bae and Choi (2012) give the method for 3D point clouds with normals. The code of this page is its 2D form.
- Decomposition first. Split the part into thin regions and thick regions, for example at its medial axis as in medial axis decomposition meshing, and reduce only the thin regions. The thick regions stay solid and are joined to the shells.
- Defeature first. Small features such as fillets, holes and ribs break the pairing of faces, so they are often removed by defeaturing before the extraction.
When to use it
Use mid-surface extraction when the part is a thin wall, with a ratio of span to thickness of about ten or more, and a shell model is accurate enough for the analysis. Use face pairing when the CAD solid has clean opposite faces. Use a medial method when the faces do not pair well or when only a mesh or a point set is available. Keep solid elements near thick regions, ribs and joints, where the shell assumption fails.
Pitfalls
- The free edges. The medial axis of the strip ends at , so the shell is too short by at each free edge, as the example shows. Extend the midline to the edge, or the model loses stiffness and mass.
- Corner branches. Each convex corner gives a branch with separation angle that reaches the midline. Without pruning, they become false shell pieces.
- Small disks at the samples. A disk that touches two neighboring samples can pass the angle test. The test , with about the sample spacing, removes them. With and the shifted strip of the example, the code keeps two such disks at the top corners, with radius 0.0625, angle and centers at .
- Sampling. The samples on the two faces should be denser than the thickness. With the offset between the faces, the center moves by , so the error grows fast if the spacing approaches .
- Junctions. At a T-joint or a change of thickness, the medial axis has a branch point and the shell needs a connection there. Neither pruning nor linking builds it.
- Wrong normals. A flipped normal sends the disk outside the region. Check the orientation before the run.
Implementations
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/midsurface-extraction/. The authors are listed in prov/FG99C0.jsonl.
"""Midline extraction of a thin 2D region by the shrinking ball medial axis.
The region is given by samples of its boundary with unit inward normals.
For each sample the largest disk that touches the boundary at the sample
and holds no other sample inside is found by shrinking (Ma, Bae and Choi
2012). Its center is a point of the approximate medial axis, and twice its
radius is the local thickness. Branches that run into corners are removed
by the separation angle, and the remaining points are linked into
polylines with a thickness at each point: the midline of the thin region,
the 2D form of a midsurface.
"""
from __future__ import annotations
import math
from typing import List, Optional, Sequence, Tuple
Point = Tuple[float, float]
def tangent_radius(p: Point, n: Point, q: Point) -> float:
"""Radius of the circle tangent to the boundary at p (inward normal n) that passes through q.
It is |q - p|^2 / (2 n . (q - p)), and infinity when q is not on the inner side of the tangent.
"""
dx, dy = q[0] - p[0], q[1] - p[1]
h = n[0] * dx + n[1] * dy
if h <= 0:
return math.inf
return (dx * dx + dy * dy) / (2 * h)
def shrinking_ball(
pts: Sequence[Point], nrm: Sequence[Point], i: int, r0: float, rel: float = 1e-12
) -> Tuple[Point, float, Optional[int], int]:
"""(center, radius, index of the touching sample, number of shrink steps) for sample i.
Returns touching index None if the initial disk of radius r0 holds no sample.
"""
p, n = pts[i], nrm[i]
r, touch, steps = r0, None, 0
while True:
c = (p[0] + r * n[0], p[1] + r * n[1]) # @line L2
# nearest sample to the center, by a linear scan
j = min(range(len(pts)), key=lambda k: (pts[k][0] - c[0]) ** 2 + (pts[k][1] - c[1]) ** 2) # @line L3
d = math.hypot(pts[j][0] - c[0], pts[j][1] - c[1])
if j == i or d >= r * (1 - rel): # @line L4
return c, r, touch, steps
r_new = tangent_radius(p, n, pts[j]) # @line L5
if not r_new < r: # round-off guard: the radius must strictly decrease
return c, r, touch, steps
r, touch, steps = r_new, j, steps + 1
def separation_angle(p: Point, c: Point, q: Point) -> float:
"""Angle at c between c -> p and c -> q, in degrees."""
ax, ay = p[0] - c[0], p[1] - c[1]
bx, by = q[0] - c[0], q[1] - c[1]
cosv = (ax * bx + ay * by) / (math.hypot(ax, ay) * math.hypot(bx, by))
return math.degrees(math.acos(max(-1.0, min(1.0, cosv))))
def sample_spacing(pts: Sequence[Point]) -> float:
"""h = the largest distance from a sample to its nearest other sample."""
h = 0.0
for i, p in enumerate(pts):
d = min(math.hypot(q[0] - p[0], q[1] - p[1]) for j, q in enumerate(pts) if j != i)
h = max(h, d)
return h
def medial_points(
pts: Sequence[Point],
nrm: Sequence[Point],
theta_min: float = 120.0,
r_min: Optional[float] = None,
r0: Optional[float] = None,
) -> List[Tuple[Point, float, int, int, float]]:
"""Medial points (center, radius, sample, touching sample, angle) with angle >= theta_min and radius >= r_min.
The default r_min is the sample spacing h: smaller disks touch neighboring samples and come
from the sampling, for example at a corner, not from the shape.
"""
if r0 is None:
xs = [p[0] for p in pts]
ys = [p[1] for p in pts]
r0 = 1e3 * (max(xs) - min(xs) + max(ys) - min(ys))
if r_min is None:
r_min = sample_spacing(pts)
out = []
for i in range(len(pts)): # @line L1
c, r, j, _ = shrinking_ball(pts, nrm, i, r0)
if j is None:
continue
ang = separation_angle(pts[i], c, pts[j]) # @line L6
if ang >= theta_min and r >= r_min: # @line L7
out.append((c, r, i, j, ang))
return out
def link_chains(centers: Sequence[Point], max_link: float) -> List[List[int]]:
"""Order the points into chains: components of the graph with edges shorter than max_link,
each walked by nearest neighbors from an end point."""
m = len(centers)
def dist(a, b):
return math.hypot(centers[a][0] - centers[b][0], centers[a][1] - centers[b][1])
seen = [False] * m
chains = []
for s in range(m):
if seen[s]:
continue
comp, stack = [], [s]
seen[s] = True
while stack:
a = stack.pop()
comp.append(a)
for b in range(m):
if not seen[b] and dist(a, b) <= max_link:
seen[b] = True
stack.append(b)
# an end point: the point farthest from an arbitrary point of the component
start = max(comp, key=lambda b: dist(comp[0], b))
order, left = [start], set(comp) - {start}
while left:
nxt = min(left, key=lambda b: dist(order[-1], b))
order.append(nxt)
left.remove(nxt)
chains.append(order)
return chains
def midline(
pts: Sequence[Point], nrm: Sequence[Point], theta_min: float = 120.0, max_link: Optional[float] = None
) -> List[List[Tuple[float, float, float]]]:
"""Polylines of (x, y, thickness) that approximate the midline of a thin region."""
mp = medial_points(pts, nrm, theta_min)
centers = [m[0] for m in mp]
if max_link is None:
gaps = [math.hypot(pts[k + 1][0] - pts[k][0], pts[k + 1][1] - pts[k][1]) for k in range(len(pts) - 1)]
max_link = 3 * max(gaps)
chains = link_chains(centers, max_link) # @line L8
return [[(centers[k][0], centers[k][1], 2 * mp[k][1]) for k in ch] for ch in chains]
def strip_samples(length: float, t: float, s: float, stagger: bool = False, s_end: Optional[float] = None):
"""Boundary samples and inward normals of the strip [0, length] x [-t/2, t/2].
The long sides are sampled at spacing s, at the midpoints (k + 1/2) s; with stagger the top side
is shifted by s/2. The short sides are sampled at spacing s_end (default s). No sample is a corner.
"""
s_end = s_end or s
pts, nrm = [], []
m = int(round(length / s))
for k in range(m):
pts.append(((k + 0.5) * s, -t / 2))
nrm.append((0.0, 1.0))
top = [(k + 1) * s for k in range(m - 1)] if stagger else [(k + 0.5) * s for k in range(m)]
for x in top:
pts.append((x, t / 2))
nrm.append((0.0, -1.0))
me = max(1, int(round(t / s_end)))
for k in range(me):
y = -t / 2 + (k + 0.5) * t / me
pts += [(0.0, y), (length, y)]
nrm += [(1.0, 0.0), (-1.0, 0.0)]
return pts, nrm
def sector_samples(r1: float, r2: float, phi: float, m_arc: int, m_end: int):
"""Samples and inward normals of the annular sector r1 <= rho <= r2, 0 <= theta <= phi.
Both arcs are sampled at the same m_arc angles (k + 1/2) phi / m_arc, and each radial end at m_end points.
"""
pts, nrm = [], []
for k in range(m_arc):
th = (k + 0.5) * phi / m_arc
c, s = math.cos(th), math.sin(th)
pts += [(r1 * c, r1 * s), (r2 * c, r2 * s)]
nrm += [(c, s), (-c, -s)]
for k in range(m_end):
rho = r1 + (k + 0.5) * (r2 - r1) / m_end
pts.append((rho, 0.0))
nrm.append((0.0, 1.0))
c, s = math.cos(phi), math.sin(phi)
pts.append((rho * c, rho * s))
nrm.append((s, -c))
return pts, nrm
test_midsurface_extraction.py 116 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/midsurface-extraction/. The authors are listed in prov/FG99C0.jsonl.
import math
import random
from midsurface_extraction import (
medial_points,
midline,
sector_samples,
shrinking_ball,
strip_samples,
tangent_radius,
)
L, T, S = 10.0, 1.0, 0.1
def test_shrinking_ball_returns_the_smallest_tangent_radius() -> None:
"""Lemma 1: the loop stops at r* = min over samples of the tangent radius, for any point set."""
rng = random.Random(7)
found = 0
for _ in range(30):
pts = [(rng.uniform(-1, 1), rng.uniform(-1, 1)) for _ in range(40)]
a = rng.uniform(0, 2 * math.pi)
nrm = [(math.cos(a), math.sin(a))] * len(pts)
for i in range(0, 40, 7):
c, r, j, _ = shrinking_ball(pts, nrm, i, 1e6)
best = min(tangent_radius(pts[i], nrm[i], q) for k, q in enumerate(pts) if k != i)
if best == math.inf: # no sample on the inner side: the disk never shrinks
assert j is None and r == 1e6
continue
found += 1
assert j is not None and abs(r - best) <= 1e-12 * max(1.0, best)
assert abs(tangent_radius(pts[i], nrm[i], pts[j]) - r) <= 1e-12 * max(1.0, r)
# no sample lies strictly inside the final disk
assert all(math.hypot(q[0] - c[0], q[1] - c[1]) >= r * (1 - 1e-9) for q in pts)
assert found > 100
def test_strip_aligned_samples_give_the_exact_midline() -> None:
"""Theorem 2: aligned samples on a strip give centers on y = 0 with radius t/2 exactly."""
pts, nrm = strip_samples(L, T, S)
mp = medial_points(pts, nrm, 120.0)
assert len(mp) > 150
for c, r, _, _, ang in mp:
assert abs(c[1]) < 1e-12 and abs(r - T / 2) < 1e-12 and ang > 179.9
xs = [m[0][0] for m in mp]
# the medial axis of the strip runs from x = t/2 to x = L - t/2
assert T / 2 - 1e-9 <= min(xs) <= T / 2 + S and L - T / 2 - S <= max(xs) <= L - T / 2 + 1e-9
def test_strip_staggered_samples_meet_the_error_bound() -> None:
"""Theorem 2: with offset d between the sides, the error of the center and of the radius is d^2 / (2t)."""
pts, nrm = strip_samples(L, T, S, stagger=True)
mp = medial_points(pts, nrm, 120.0)
bound = S * S / (8 * T)
errs = [abs(c[1]) for c, _, _, _, _ in mp] + [abs(r - T / 2) for _, r, _, _, _ in mp]
assert max(errs) <= bound * (1 + 1e-9)
assert max(errs) >= bound * (1 - 1e-9) # the bound is attained for d = s/2
def test_separation_angle_prunes_the_corner_branches() -> None:
pts, nrm = strip_samples(L, T, S)
raw = medial_points(pts, nrm, 0.0, r_min=0.0)
assert any(abs(c[1]) > T / 4 for c, _, _, _, _ in raw) # corner bisector points exist
assert all(abs(ang - 90) < 1e-6 or ang > 179.9 for _, _, _, _, ang in raw)
kept = medial_points(pts, nrm, 120.0)
assert all(abs(c[1]) < 1e-12 for c, _, _, _, _ in kept)
def test_annular_sector_midline_is_the_mean_arc() -> None:
r1, r2, phi = 2.0, 2.5, math.pi / 2
pts, nrm = sector_samples(r1, r2, phi, 120, 10)
mp = medial_points(pts, nrm, 120.0)
rm, t = (r1 + r2) / 2, r2 - r1
theta0 = math.asin(t / (2 * rm))
angles = []
for c, r, _, _, _ in mp:
assert abs(math.hypot(*c) - rm) < 1e-12 and abs(r - t / 2) < 1e-12
angles.append(math.atan2(c[1], c[0]))
h = phi / 120
assert theta0 - h <= min(angles) <= theta0 + h
assert phi - theta0 - h <= max(angles) <= phi - theta0 + h
def test_tapered_strip_gives_the_perpendicular_thickness() -> None:
"""Sides y = +-(t0/2 + k x): the midline is y = 0 and 2r is the thickness normal to the sides."""
t0, k, n = 1.0, 0.05, 100
pts, nrm = [], []
m = 1 / math.sqrt(1 + k * k)
for i in range(n):
x = (i + 0.5) * L / n
h = t0 / 2 + k * x
pts += [(x, -h), (x, h)]
nrm += [(k * m, m), (k * m, -m)]
for i in range(10):
y = -0.5 + (i + 0.5) / 10
pts += [(0.0, y * t0), (L, y * (t0 + 2 * k * L))]
nrm += [(1.0, 0.0), (-1.0, 0.0)]
mp = medial_points(pts, nrm, 120.0)
assert len(mp) > 150
for c, r, _, _, _ in mp:
assert abs(c[1]) < 1e-12
assert abs(r - (t0 / 2 + k * c[0]) * m) < 1e-12
def test_midline_is_one_ordered_chain_with_thickness() -> None:
pts, nrm = strip_samples(L, T, S)
chains = midline(pts, nrm)
assert len(chains) == 1
ch = chains[0]
xs = [p[0] for p in ch]
assert xs == sorted(xs) or xs == sorted(xs, reverse=True)
assert all(abs(p[2] - T) < 1e-12 for p in ch)
assert abs(abs(xs[-1] - xs[0]) - (L - T)) < 2 * S
History
Blum (1967) introduced the medial axis as a shape descriptor. Rezayat (1996) gave the name midsurface abstraction and built it by pairing faces of the CAD solid. Medial methods for the dimensional reduction of thin solids were developed in parallel, as surveyed by Thakur, Banerjee and Gupta (2009). Ma, Bae and Choi (2012) gave the shrinking ball construction of medial points from samples and normals, which this page uses in 2D.
References
- M. Rezayat, Midsurface abstraction from 3D solid models, general theory and applications, Computer-Aided Design 28(11), 1996, 905 to 915, the whole paper (pairing of opposite faces, construction of the midsurface patches, and their joining)
- J. Ma, S. W. Bae and S. Choi, 3D medial axis point approximation using nearest neighbors and the normal field, The Visual Computer 28(1), 2012, 7 to 19, the whole paper, the shrinking ball algorithm that this page implements in 2D, and its pruning by the separation angle
- A. Thakur, A. G. Banerjee and S. K. Gupta, A survey of CAD model simplification techniques for physics-based simulation applications, Computer-Aided Design 41(2), 2009, 65 to 80, the part of the survey on dimensional reduction, which covers midsurface methods by face pairing and by the medial axis
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag FG99C0 Edit this page History Report an error Markdown