knotflow Lessons PDF ↓ Deutsch Handbook Modeller ↗ Source

Lesson 3.3 · III Modelling

Walls: moving curves sideways

What it's about: A cup, a bowl, a pot: they all have a wall of a certain thickness. It is measured at right angles to the wall, not horizontally. For this there are three building blocks for flat curves: chaining curves (chain), reversing curves (reverse) and moving curves sideways (offset; the result is also known as a parallel curve). From these, the language itself builds thicken: from a profile, a closed outline with a wall thickness, ready for revolve. This lesson stays in the plane; the wall of a whole surface, with floor and rim, is made by shell (Lesson 3.8).

A cup with a 2.4 mm wall, from a single drawn profile

1. Measured horizontally is too thin

The pot (examples/pot.kf) draws its wall like this: on the outside the point (rt, height), on the inside (rt - wall, height). So the wall is moved inwards horizontally by wall. If the wall is vertical, that is the same thing. If it leans at an angle α, the real thickness at right angles to the wall is only wall · cos α:

Wall Lean α 1.6 mm horizontally gives
the pot (radius 55 → 75 mm over a height of 150 mm) 7.6° 1.59 mm
a bowl with a 45° wall 45° 1.13 mm
a shallow bowl 60° 0.80 mm

For the pot it does not matter: 0.8 % less material. For a bowl with a 45° wall almost a third is missing, at 60° half, and finding that out only in the slicer's print preview is annoying. The right thing is to move every point of the wall along its normal.

2. Offset: every point along its normal

offset(curve, d) moves every point of the curve sideways by d, namely to the left of the direction of travel (seen from above, z pointing up). For a profile drawn from bottom to top (x = radius), left is inwards. A negative d goes to the right.

A profile (blue), its offset curve (red) and the normals in between: the same length everywhere

The surprise: the offset curve of a NURBS curve is in general not a NURBS curve. The normal is the derivative divided by its length, and that length contains a square root. A quotient of polynomials, which is what a NURBS curve is (Lesson 1.2), cannot reproduce a root. The exceptions are straight lines (the length of the derivative is constant) and circles (the offset curve is a circle with a different radius).

So it is approximated, but under control:

Straight lines and arcs stay exact. offset looks at the curve one Bézier piece at a time: a straight piece is moved, a piece of a circle is scaled about its centre, both with the same control points and weights. That is not just nice to have. Built from cubic pieces as well, they would give the pot eight times as many triangles, although nothing about its shape changes. And everything done with the curve afterwards costs more: the outline of a splined shaft, made of arcs and involutes, had four times as many control points once offset, and a boolean with it took 3.3 s instead of 0.1 s.

3. Corners, narrow places, loops

At a corner the normal jumps. The two neighbouring pieces, once moved, no longer fit together:

A square (blue), offset inwards (red) and outwards (green)

Looking only at the two neighbours of a corner is not enough, though. If a piece is shorter than d, say a small chamfer or a step in the floor of a cup, the real offset simply passes over the short piece; a method that only shortens neighbours would find no crossing there and have to give up.

So offset uses the method that drawing programs use as well. It does not think about corners but about distances:

  1. Raw offset. Each piece between two corners is offset on its own. On the outside the arc is added. On the inside, if the pieces cross right at the corner, they are shortened there; otherwise two straight pieces connect them through the corner itself. The result is a closed line with loops.
  2. Find the crossings. Wherever the raw offset crosses itself, it is cut. The search runs over short straight steps along the curve, sorted into a grid: only steps in the same cell are compared. Each crossing of steps is then confirmed on the real curves with Newton's method. So two arcs that merely come close between two steps do not count.
  3. Throw away the loops. Each piece between two cuts is checked at its middle: if it is closer than d to the original curve there, it belongs to a loop and is dropped. What is left is the offset.

An open curve needs one more thing: around its end, the offset can get to the other side of a piece and come too close to it there, without crossing itself. So there the circles of radius d around the start and the end, and the offset to the other side, cut as well. They are not part of the result; they only cut. Whatever tiny loops are still left at the end, for example at the tip where the offset of a sharp bend folds over, are searched for once more and cut out.

4. What can't be done says so

Three things cannot be done, and they are reported, with a location:

The same crossing search also runs in revolve: a profile that crosses itself would give a solid that runs through itself. Instead of making it silently, revolve reports, for example, "the profile crosses itself near (2, 1)". Touching on the axis is the exception: two balls on top of each other touch there, and that is a valid solid. What to do in each of these cases is in the handbook, chapter 6.

5. Chaining curves

A profile often consists of pieces: a straight floor, a curved wall, an arc at the rim. chain([a, b, c]) makes one curve out of them. Each one must start where the previous one ends (otherwise the message says how large the gap is). Three things happen along the way:

On top of that there is reverse(curve): the same curve, run the other way round. You need it to close an outline in a loop.

For closed outlines "to the left of the direction" is awkward: whether left is inside depends on whether the outline runs anticlockwise or clockwise. inset(outline, d) takes care of that. It moves a closed outline inwards by d (outwards for negative d), whichever way round it runs, and is one line in the prelude:

fn inset(c, d) = offset(c, if clockwise(c) then -d else d)

6. thicken: written in the language

With these three building blocks, the wall is one line in the prelude:

fn thicken(c, t) = chain([c, line(at(c, 1), at(inner, 1)), reverse(inner), line(at(inner, 0), at(c, 0))])
  where inner = offset(c, t)

The profile on the outside, a straight rim across at the top, the offset curve back on the inside, and back to the start at the bottom. For a cup, the profile starts on the axis, runs out along the floor and up the wall (examples/cup.kf):

let floor = line((0, 0), (foot, 0))
let side  = interpolate([(foot, 0), (belly, widest * height), (mouth, height)])
export revolve(thicken(chain([floor, side]), wall))

The corner between floor and wall turns left, that is inwards: there the floor and the wall of the inside are shortened until they meet. The floor is as thick as the wall, measured at right angles to both. The start of the inside lies on the axis again, at (0, wall).

A test with a known result: a straight cup, 10 mm outer radius and 20 mm high, wall and floor 1 mm. Its volume is π · (10² · 20 − 9² · 19) = 1,448.3 mm³. The tessellated solid hits that to within 0.1 %, and it is closed.

7. Limits, and where it goes on

8. How it is checked

A fuzz test offsets tens of thousands of random curves (polylines, closed ones, NURBS up to degree 3) by random distances and checks every offset: every point checked exactly d from the curve, on the right side, without a crossing, or one of the three messages. On top of that come malicious single cases: a 0.5 mm chamfer under a 1 mm thick wall, a polyline of 2000 pieces each bending by 0.0005°, a square at x = 10⁸, a zigzag with 20,000 corners.

Try it yourself

cargo run --release -p kf -- render examples/cup.kf -o out/cup.png
cargo run --release -p kf -- render examples/cup.kf -D wall=6 -D belly=60 -o out/cup.stl
cargo run --release -p kf -- svg --demo corners -o out/corners.svg
  1. Rebuild examples/pot.kf with thicken: the profile is polyline([(0, 0), (rb, 0), (rt, height)]). Compare the volumes that kf render prints. Where does the difference come from?
  2. What does offset(circle(10), 10) report, and why exactly that? What happens with -10?
  3. Draw a profile with a notch: a polyline that briefly goes deep inwards and back out again. Increase wall until the message appears. Where does it report the falling apart, and does that match your drawing? What does the offset do at the notch just before that?
  4. Something to think about: the offset curve of a circle is a circle. Why is the offset curve of a parabola not a parabola? (Hint: how does the distance between the points change when you move a parabola inwards?)