knotflow Handbook Deutsch Lessons Modeller ↗ Source

Chapter 10

Patterns and copies

Many models repeat something: pegs on a plate, holes on a circle, the links of a chain, spines on a body. In knotflow you describe the part once and then say where it should go. This chapter shows the ways to do that, from a simple grid to hundreds of copies spread at random.

A grid: two generators in one list

param n     = 5  in 2..10  "pegs per direction"
param pitch = 12 in 6..30  "distance between the pegs [mm]"
param peg   = 2  in 1..5   "radius of a peg [mm]"

let side   = n * pitch
let places = [(i - (n - 1) / 2) * pitch | i <- range(n)]

let plate = box(side, side, 3) |> move(z: 1.5)
let pegs  = [cylinder((x, y, 2), (x, y, 12), peg) | x <- places, y <- places]

export fold(pegs, plate, union)

Instead of the union you can simply export the parts as a list: export [plate, ...pegs] (... puts in the elements of a list). kf render then reports parts: 26: 26 bodies that overlap. That is a valid STL, and slicers join overlapping bodies when they print. union, on the other hand, computes one single, exact solid (chapter 7); with many parts that takes longer.

Staggered rows

For a honeycomb pattern every second row moves by half a pitch. The if stands right inside the arithmetic:

param rows  = 6 in 2..12 "rows"
param cols  = 8 in 2..12 "holes per row"
param pitch = 8 in 4..20 "distance between the holes [mm]"

let dy = pitch * sqrt(3) / 2                     // distance between the rows
fn spot(i, j) = (pitch * i + (if j % 2 == 1 then pitch / 2 else 0), dy * j)

let holes = [polygon(6, pitch / 2 - 1) |> rotate(30) |> translate(spot(i, j))
             | j <- range(rows), i <- range(cols)]
let plate = rectangle(pitch * (cols + 1.5), dy * (rows - 1) + 2 * pitch)
  |> move(x: pitch * (cols - 1) / 2 + pitch / 4, y: dy * (rows - 1) / 2)

export fold([extrude(h, 4) |> move(z: -1) | h <- holes], extrude(plate, 2), difference)

With dy = pitch · √3 / 2 all neighbours are the same distance apart. The holes are prisms 4 mm high that start 1 mm below the plate: that way they cut through both sides for sure instead of ending exactly on a face. difference takes one solid at a time; a list as its second argument is an error, hence the fold.

A circle: turn instead of compute

Draw one hole of a bolt circle at 0° on the x axis, then turn it about the upright axis:

param holes  = 8  in 3..24 "holes"
param radius = 30 in 15..60 "radius of the bolt circle [mm]"

let disc = cylinder((0, 0, 0), (0, 0, 4), radius + 10)
let hole = cylinder((radius, 0, -1), (radius, 0, 5), 3)

export fold([rotate(hole, 360 * k / holes) | k <- range(holes)], disc, difference)

rotate(shape, angle) turns about the z axis through the origin. That is why the part stands at its place at 0° before it is turned, not at the origin.

Pitfall: [0 .. holes] includes holes itself. The last hole would then lie at 360°, exactly on the first one. range(holes) stops one before.

Symmetry: mirroring

Many parts are the same on the left and the right, only mirror-inverted. You build one half and mirror it with mirror(shape, normal) at the plane through the origin that is perpendicular to normal:

param reach = 30 in 15..60 "length of the arms [mm]"

let post = cylinder((0, 0, 0), (0, 0, 20), 5)
let arm  = pipe(interpolate([(3, 0, 6), (reach / 2, 10, 10), (reach, 4, 16)]), 2.5)

export [post, arm, mirror(arm, (1, 0, 0))]

Along a curve: divide

param count = 24 in 4..80 "beads"

let path = interpolate([(0, 0), (30, 20), (60, -10), (90, 15)])

export pipe(path, 1) color "gray"
export instance(sphere(3), divide(path, count)) color "terracotta"

Facing the right way: frames and frames_along

A point only says where something goes, not which way round. That is what frames are for: a point with three axes, as a record {origin, x, y, z}, a small coordinate system of its own. A shape standing on the origin and pointing up (+z) stands on the frame once it is placed, pointing along the frame's z axis.

Rings on a wire, with frames_along

param count = 12 in 3..40 "rings"

let path = interpolate([(0, 0, 0), (40, 0, 20), (40, 40, 40), (0, 40, 60)])
let ring = torus(5, 1)

export tube(path, 1.5) color "gray"
export instance(ring, frames_along(path, count)) color "teal"

frames_along(path, count) gives frames at equal distances: z points along the curve, x and y lie across it and turn as little as possible. The torus lies flat around the z axis, so once placed it goes around the wire.

Tools for frames:

call does
place(shape, f) puts a single shape on the frame f
spin(f, angle) turns the frame about its own z axis
sized(f, k) makes its axes k times as long: what stands on it becomes k times as large
frame_at(point, direction) a frame at a point, z along the direction
frame_on(surface, u, v) a frame on a surface, z is the normal

A chain puts oval links along a curve and turns every second one by 90°, so that they interlock:

param links = 24  in 8..60  "links (an even number closes the chain)"
param wire  = 1   in 0.5..2 "wire thickness (radius) [mm]"

let path  = circle(40)
let pitch = 2 * pi * 40 / links                   // distance from link to link
let long  = pitch + 4 * wire + 0.6                // long enough to reach around the next one
fn oval(t) = (long * 0.31 * cos(t), 0, long / 2 * sin(t))
let link  = tube(interpolate([oval(t) | t <- range(0, 360, step: 15)], closed: true), wire)

let frames = frames_along(path, links)
export instance(link, [if i % 2 == 0 then f else spin(f, 90) | (i, f) <- enumerate(frames)]) color "gray"

What frames are exactly and how frames_along carries them on without needless twisting is explained in Lesson 3.1.

Scattering over a surface: scatter

param count   = 200 in 10..1000 "spines (at most)"
param spacing = 4   in 0..10    "minimum spacing [mm]"
param seed    = 1   in 0..100   "random seed"

let body  = sphere(25)
let spike = cone(1.5, 0, 10) |> move(z: -1)
let spots = scatter(body, count, seed: seed, spacing: spacing)
let sizes = [sized(f, 0.6 + 0.8 * rand(seed, i)) | (i, f) <- enumerate(spots)]
let n     = len(spots)

export body color "plum"
export instance(spike, sizes) color "ochre"

scatter takes meshes too, such as the result of isosurface (chapter 12), and solids with holes made by perforate (chapter 6): no frame lies in a hole.

By the surface's grid: grid_on

Studs on a vase, with grid_on

param around = 16 in 4..40 "studs around"
param up     = 6  in 1..20 "studs one above the other"

let vase = revolve(nurbs([(0, 0), (30, 0), (38, 30), (22, 70), (26, 90), (0, 90)], degree: 2))
let stud = sphere(2.5)

export vase color "sage"
export instance(stud, grid_on(vase, around, up)) color "ochre"

grid_on(surface, nu, nv) divides the surface into nu × nv cells and gives the frames at their centres. Around, the distances are equal; up the side they follow the surface's parameters, not its length. That is why the rows are not equally far apart, and because the profile goes in to the axis at the top, the lid gets a row too. If you need even distances, use scatter with spacing.

instance: compute once, show many times

instance(shape, places) takes a shape and a list of frames or points. The shape is cut into triangles once, fine enough for the largest copy; all others are only placements.

move, rotate, scale and mirror work on instances as on other shapes. union, difference and intersection do not take them, though: "union: b must be a surface or a solid, got instances". realize turns instances back into separate shapes:

let frames = [frame_at((x, 0, 0), (0, 0, 1)) | x <- [0, 15, 30]]
let posts  = instance(cylinder((0, 0, 0), (0, 0, 10), 3), frames)
let base   = box(50, 12, 3) |> move(x: 15, z: -1)

export fold(realize(posts), base, union)

Rule of thumb: for many equal parts that are only shown and printed, instance. If they are to merge with something else into one solid, realize and union, and then some patience: for the sea urchin with 450 spines that takes a few seconds.

A single part is worth an instance too when a slider turns or moves it: a turned solid is a new solid and is cut into triangles again at every move, a copy at a new frame takes the triangles of the shape that are already there. In the example differential (instance(part, [pose]) for each gear), turning the drive recomputes in 0.8 instead of 14 seconds this way.

The examples

In the browser under Model, in the source in examples/:

How divide, scatter and instance work inside is explained in lesson 3.1.