knotflow Lessons PDF ↓ Deutsch Handbook Modeller ↗ Source

Lesson 3.8 · III Modelling

Walls: shell

What it's about: A cup, a bowl, a lampshade have a wall of even thickness, often a floor and a flat rim. shell(shape, t) builds it from a shape: from a smooth surface through its offset surface (every point moved along its normal), from a revolved profile or an extruded section with building blocks that already exist, and from combined shapes. The result is a solid of several faces (Lesson 3.4).

Assumed: the offset curve (Lesson 3.3), solids (Lesson 3.4), and for sections and combined shapes the Booleans (Lesson 3.7).

A planter with a foot and a step: one wall, one call

let outside = chain([ … ])                      // the profile from the axis to the rim
export shell(revolve(outside), wall, open: [(0, 0, 1)])

1. What lies inside

A wall of thickness t is everything in the solid that lies closer than t to its surface. So the hollow inside is the set of all points at least t from the outside. Image processing calls this erosion. Roll a ball of radius t along the inside of the wall; wherever its centre gets to is the hollow.

This already tells what corners look like. Where the solid bends outwards (the edge of a box), the hollow stays sharp there: its walls are the box's sides moved inwards, and they meet in an edge. Where the solid bends inwards (the inner corner of an L), the ball comes round the corner, and the hollow gets a rounding of radius t there.

2. Three ways, and why knotflow constructs

A general CAD program moves every face on its own. It extends and trims neighbouring faces at outer edges, and puts in pieces of pipes and balls at inner edges. That needs trimmed faces, faces with any boundary. The solids of Lesson 3.4 only know whole faces that meet at their ends; trimmed faces are a big step of their own, and shell does without them.

A second way would be to build the hollow from slabs, pipes and balls with booleans. But those parts touch each other along whole lines, and that is exactly the weak spot of booleans (Lesson 3.7, section 4).

knotflow takes a third way. It recognises how the surface was made, and builds the wall with the same recipe again. That covers the vessels people draw most often, and it stays exact.

3. Smooth surfaces: the offset surface

The offset of a surface has the same problem as the offset of a curve (Lesson 3.3): the normal contains a square root, so the offset surface is not a NURBS surface. It is approximated, and checked:

Three decisions, and why:

Local, not global. The obvious way would be to lay one surface through all exact points (as interpolate does for curves). It would converge only slowly: a loft through separately interpolated rings carries hundreds of knots, and at each one the curvature jumps a little. A single surface through everything has to bridge these jumps, and its error falls only with the square of the grid spacing instead of the fourth power. Local pieces whose grid lines lie on the knots of the surface never straddle a jump. Between two knots, the surface is one polynomial and its offset is smooth.

Derivatives without rounding noise. Only the shift d · n is differentiated numerically; the derivatives of the surface itself are computed exactly. Section 14 explains why.

Split in the right direction. The lampshade has 311 knots in u and only 7 rings in v; its cells are too wide in u only. Split every cell whose middle is off in both directions, and they get halved in v as well, again and again: the inner surface would have 1,144 control points in v instead of 34, and the solid 5.5 million triangles. So a cell is split in v only if a side in v is off.

What cannot be done is reported, with a location:

4. The smooth wall

A lampshade: a revolved loft through wavy rings with a 1.6 mm wall

let skin = loft([ring(t) | t <- linspace(0, 1, 7)])
export shell(skin, wall) color "ochre"

For a smooth surface, shell(surface, t) builds the wall: the surface, its offset by t inwards, and at each open end a rim. The rim is a ruled surface: straight lines from the edge of the outer surface to the same parameter on the edge of the inner one. Both edge curves are first brought to the same degree and the same knots (exactly, by degree elevation and knot insertion), then they are the two rows of control points of a surface of degree 1 across.

There is also offset_surface(surface, d): the offset surface alone, as a surface.

A check: shell(sphere(10), 1.5) has 1,614.7 mm³ instead of 4/3 π (10³ − 8.5³) = 1,616.3 mm³. The hollow ball lacks 0.1 %: the flat triangles cut off a little of each curved wall, outside more than the inside adds back. A finer tolerance (--tol) brings it closer.

5. Revolved profiles: the wall in the profile

The normal of a surface of revolution always lies in the plane of its profile; it never points around the axis. So moving the surface by t along its normal only moves the profile by t in its plane, and that is offset from Lesson 3.3. offset already does what section 1 asks for: it cuts the corners that point inwards and gives the corners that point outwards an exact arc.

shell(revolve(outside), t) turns this into one profile, which, turned once, is the wall. It takes four steps:

  1. An end of the profile that stays closed and is not on the axis gets its cap as a line to the axis, so the cap becomes wall too.
  2. The whole profile is moved inwards by t. Which side is inside, the direction of the profile says, closed over the axis.
  3. The inner curve must end exactly where the wall ends:
    • on the axis, with x exactly 0 (otherwise turning it makes a tiny hole instead of a pole);
    • in the plane of an open end, which gives the flat rim. If the inner curve goes past the plane, it is cut there. If it falls short, it is carried on along its tangent to the plane.
  4. Outside, rim and the reversed inside are joined with chain into one profile.

A cup whose profile starts on the axis becomes one profile from the axis to the axis. Turned, it is a closed solid with two poles, like a ball. A shape open at both ends (a tube, a lampshade from a revolved profile) becomes a closed loop, and turned it is a ring. A hollow ball becomes two profiles, outside and inside: two shells, one inside the other.

A cone with its tip down shows why step 3 is needed. Moved inwards, its slanted wall gives an inner curve that starts beside the axis. Only when it is carried on to the axis does the inner tip end up t / sin α higher, where α is half the opening angle.

6. Sections: taking the inside away

For a straight extrusion of a section (extrude), the hollow is again an extruded section: the section moved inwards by t. inset (in the prelude) moves a curve inwards, whichever way round it runs. shell takes that inner solid away with difference:

A tray with a corner cut out: round inside where the section bends inwards

export shell(extrude(section, height), wall, open: [(0, 0, 1)])

The inner solid starts t above the floor. At an open end it reaches beyond the surface. If it ended exactly in the plane of the cap, two faces would lie in one plane, one on the other. This way, each face cuts the other across, which is the easy case for booleans.

A section extruded at a slant gets no wall but a message: its walls would not be equally thick everywhere.

7. Lofts through polygons

A loft through polygons is straight across: between two corner curves, each side is a ruled surface. Its exact offset is no ruled surface any more, and where two sides meet the offsets would have to trim each other. So shell approximates the inside:

  1. At many stations along the loft, each side is moved in by t along its normal, as a plane (the tangent plane at its end).
  2. Two neighbouring moved planes meet in a line, the inner edge. The new corner lies on it, so it is t from both sides. Which point of the line? Each side, carried along itself at the station, reaches the line at its own place, because two sides leaning differently are moved along the loft by different amounts. The corner is between the two. (It used to be the middle of the nearest points of the two moved side lines. That lies on neither plane, and the wall there was down to 0.93 t.)
  3. A new loft goes through the inner polygons, extended past the ends.
  4. A big box cuts off a closed end flat, t inside the end's plane: an exact, flat floor.

Between the stations it is measured how far the inside is from the outside. If that is off by more than a hundredth of t, the stations are doubled. Where the sides are planes, the inside is exact: a straight prism gives the same volume as the extruded section. Where the loft twists, its sides are no planes any more, and the normal turns across a side. Then a deviation remains, and shell builds the inside a second time, that very deviation further in. So the wall is nowhere thinner than t, at most a little thicker. If it is off by more than a twentieth, that is reported. It also works within combined shapes (section 10).

8. Which ends stay open

open: names the directions the open ends face. open: [(0, 0, 1)] leaves the top open, and without open: everything is closed, a hollow solid, as when hollowing in a CAD program, where one picks the faces to remove. Only a smooth loft through curves stays open at its ends: there is no flat lid for it. A direction no end faces is an error that names the directions there are.

The directions are in space. A surface that was turned and moved first is still recognised: shell builds it again from the profile and axis it found, and compares the result with the original. Only if everything agrees does it count as revolved.

9. Flat ends for any wall

The smooth wall of section 4 ends in rims that follow the normal, so on a slanted wall they stand a little out of the end plane. A flat end comes from building the surface a little longer and cutting it off flat. That is what between(shape, z0, z1) in the prelude does: it takes the intersection with a box that reaches beyond the shape on every other side.

The lampshade with flat ends

let longer = loft([ring(t) | t <- linspace(-0.1, 1.1, 7)])
export between(shell(longer, wall), 0, height)

10. Combined shapes

A solid made with union, difference and intersection has an inside too: everything at least t from its surface. It can be put together from the parts, by three rules:

Each part knows which surface it was made from (a revolved profile, an extruded section, a closed smooth surface), and moves itself in or grows. An extrusion's section is moved by offset. A revolved profile is mirrored at the axis; offset moves the whole section through the axis, which is then halved again. That is exact, because the shortest way to a surface of revolution always lies in the plane through the axis. A closed smooth surface becomes its offset surface (section 3). The wall is then one difference: the solid without its inside.

Which ends open is checked on the whole solid. open: opens only ends that face one of the directions named and lie free in their middle: on a bottle of body and neck only the neck opens, the shoulder stays closed. An end all covered by another part (the neck on the body, two boxes flush side by side) reaches with its inside a little into the other part, so that no wall stays between the parts. An end covered only in part stays closed; the wall is rather thicker there. Where a shape is nowhere thicker than two walls, nothing is left inside; that is reported.

11. Building blocks first

As with tube (Lesson 3.2), shell has a twin written in the language. examples/shell_in_language.kf builds a cup's wall from these building blocks:

Its volume is the built-in's. So the built-in shell is a shortcut, not magic: what it does, you can write down yourself and change, for example a wall that gets thinner towards the top.

12. How it is checked

Known volumes: a cylindrical cup, πR²H − π(R−t)²(H−t); a hemispherical bowl, ⅔π(R³ − (R−t)³); a hollow ball; a tube; a cone with a raised inner tip; open and closed boxes; an L-shaped section whose inside is larger by exactly t²(1 − π/4), because the inner corner turns round. The offset surface of a loft through wavy rings lies at most 0.0001 mm from the exact point S + d · n at 2,000 random places. Fuzzers build random profiles, sections, lofts and combined shapes, turned and moved: every wall must be closed, its volume must match an independent computation (Pappus's rule for revolved profiles, the Steiner formula of section 14 for smooth walls), and what cannot be done gives a message. Browser and command line give byte-identical files for every example.

13. Limits

14. A deeper look: twist, Steiner and the traps of the smooth wall

Derivatives without rounding noise. Taking the derivatives and the twist from differences of exact points, for example (P(u + h) − P(u − h)) / 2h, goes wrong: with h = 10⁻⁶ of the range, the twist near the pole of a ball is off by a factor of 20. The exact points carry rounding errors of about 10⁻¹¹ mm there, and the twist divides them by two small steps. h = 10⁻⁴ helps for a ball of 10 mm, but not for one of 1 km, whose points carry rounding errors of 10⁻¹⁰ of their size. So only the shift d · n is differentiated numerically; the derivatives of S itself are computed exactly. A unit normal has rounding errors of 10⁻¹⁶ whatever the size of the part.

The Steiner formula. How do you check the volume of a wall for which there is no formula? The area of an offset surface at distance s is A(s) = ∫ (1 − 2Hs + Ks²) dA, where H is the mean and K the Gaussian curvature. That is quadratic in s, so Simpson's rule over outside, middle and inside is exact: V = t/6 · (A(0) + 4 A(t/2) + A(t)). The areas can be measured from the triangles, and the result is independent of the volume kf reports.

And the traps a fuzzer finds in smooth walls:

A hidden zero. The offset surface needs derivatives, and they can give NaN ("not a number") in an unexpected place. interpolate(…, closed: true) (Lesson 3.2) cuts exactly one round out of a longer curve. If the cut lies at a knot that was computed in two ways, the two values may differ by 4.6 · 10⁻¹⁷. Then a span of that length is left at the start of the curve. Evaluating the curve itself works, because it takes the first control point at the start. Its derivative, however, divides by the length of that span.

Two rules prevent this: a cut a rounding error away from an existing knot is made exactly at that knot, and the search for the span of a parameter steps over empty spans at the start as at the end. The same zero turns up in the offset surface itself: a grid line 2 · 10⁻¹⁶ next to a knot. There, too, it snaps. A test builds such rings, lofts them and evaluates the derivatives at u = 0.

The lesson from this: a value that is "practically zero" is not the same as zero. Wherever two computations should give the same number, it is worth either making it the same number (one computation, or snapping), or treating everything closer than a tolerance as equal.

Try it yourself

cargo run --release -p kf -- render examples/planter.kf -o out/planter.png
cargo run --release -p kf -- render examples/tray.kf -D notch=0 -o out/tray.stl
cargo run --release -p kf -- render examples/lampshade.kf -D flat=true -o out/lampshade.stl
  1. Make the flower pot's wall so thick that shell gives up. What does the message say, and where is the shape too narrow?
  2. Leave the pot's floor open too (open: [(0, 0, 1), (0, 0, -1)]). Why does that not work, and what would the profile have to look like for it to work?
  3. Draw a profile whose rim curls inwards (a lip). What does shell say about the flat rim?
  4. Why is the tray's inner corner at the cut-out round, but the other corners sharp?
  5. Change shell_in_language.kf so that the wall gets thinner towards the rim.
  6. Make the lampshade's waves deeper (depth) and its wall thicker (wall) until the message "folds over" comes. Where does it appear first, on a crest or in a trough?
  7. To think about: the offset surface of a ball is a ball, that of a cylinder a cylinder. Is that of a cone a cone? And that of a torus?