knotflow Lessons PDF ↓ Deutsch Handbook Modeller ↗ Source

Lesson 3.1 · III Modelling

Distributing and instances

What it's about: The most common step in node editors: create points and put something on each one. Spikes on a body, links along a chain, rivets on a metal sheet. This takes three things:

Sowing: on the left pure chance, on the right with a minimum spacing

1. Frames

A point says where something goes, but not which way round. A frame is a point with three axes: {origin, x, y, z}. place(shape, frame) places a shape so that its own axes land on those of the frame. A shape that stands on the origin and points up (z) then stands on the frame and points along its z axis. On a surface, that is the normal.

Two small helpers in the prelude:

2. Along curves

divide(curve, n) gives n points at equal distances. That sounds obvious, but it isn't: the parameter of a NURBS curve runs faster where the control points are further apart. Even a quarter circle with its weights (Lesson 1.2) has unequal distances for equal parameter steps. So divide measures the curve: a table of arc length, that is how far you have travelled up to each parameter. In this table it looks up the parameters at which a quarter, a half and so on of the length is reached. On a closed curve the start is not repeated at the end.

frames_along(curve, n) gives frames as well: z along the curve, x and y across it, chosen so that they rotate around the curve as little as possible. This is the same method as for the tube around the knot (Lesson 3.2, double reflection). It lives in geom::distribute, and the sweep uses the same.

A chain along the trefoil knot

The chain needs exactly this. Each link is an oval with its long side in the direction of travel, and every second one is rotated by 90°: if i % 2 == 0 then f else spin(f, 90). So that the links interlock, a link is as long as the distance to the next one plus twice the wire thickness and a little clearance. The model works out the distance itself: the length of the curve divided by the number of links. With an even number, the chain closes cleanly.

3. Over surfaces

scatter(surface, n, seed:, spacing:) scatters n frames at random over a surface or a mesh. Two things make the difference between "random" and "nicely random":

For regular patterns there is grid_on(surface, nu, nv): frames at the centres of nu × nv cells of the (u, v) grid, z again the normal. It is a single line in the prelude, [frame_on(s, (i + 0.5) / nu, (j + 0.5) / nv) | j <- range(nv), i <- range(nu)], and so it follows the surface's parameters, not its length: on a surface of revolution the spacing around is even, but up the side it is as uneven as the profile's parameter steps. If you need even spacing, take scatter with spacing; if you need a grid that fits the shape (studs in rows), take grid_on.

The z axis is the normal, pointing outwards. Whether ∂S/∂u × ∂S/∂v points outwards is decided by the same sign of the volume as for STL (Lesson 1.3). The same seed gives the same points, everywhere, because the random numbers are a pure function of (seed, index), as with rand.

4. Instances: compute once, show many times

instance(shape, frames) is a value type of its own: a shape and a list of placements. The shape is tessellated once, and finely enough for the largest copy. A copy scaled up three times needs a third of the tolerance. Moving, rotating and mirroring only change the placements. realize turns it into separate shapes when needed.

Where the gain is and where it isn't is shown by a measurement with 1,600 spheres on a grid:

command line (STL) browser
separately (translate per sphere) 2.2 s 1,971 ms
as instances 2.0 s 3 ms

On the command line it hardly helps: an STL file needs all 6.3 million triangles, and checking and writing them is what takes the time, not the tessellation. In the browser, by contrast, the graphics card gets the shape only once, plus 1,600 matrices (4 × 4 numbers each). It draws the copies itself; three.js calls this InstancedMesh. The browser only assembles the STL when you download it, with the same function as kf, so it is byte-identical.

Sea urchin: 450 spikes, each a slightly different length

As instances the spikes overlap the body; the STL then has 451 parts, and a slicer merges them when printing. union (Lesson 3.7) makes one solid, but only from copies placed one by one: -D whole=1 folds every spike onto the body with union. That takes noticeably longer, because every copy needs its own intersections.

5. How it is checked

Even spacing is checked by measuring along the curve: all distances between neighbouring points of divide are equal, also on a circle with weights. For scatter the tests count whether every part of the surface gets the share of the points that matches its area (the polar cap its quarter), whether no pair lies nearer than spacing and whether every z axis points outwards; the same seed must give the same points. Instances must give as many triangles and the same volume as the same shapes placed one by one.

6. Limits

Try it yourself

cargo run --release -p kf -- render examples/urchin.kf -D spacing=0 -o out/urchin.png
cargo run --release -p kf -- render examples/chain.kf -D links=47 -o out/chain.png
  1. Set spacing to 0 for the sea urchin. Where do gaps appear, and where clumps?
  2. Give the chain an odd number of links. What happens where it closes, and why?
  3. Use grid_on to put small cones on a vase (examples/vase.kf). Why are they denser in some places than in others, and how does scatter help?
  4. Something to think about: what would an instance have to carry in addition so that each copy can have its own colour, and where would that have to arrive in the browser?