Last updated: 2026-11-26

12. Built objects: solid geometry and a Dalek

For new readers

Some things are best made by cutting and joining solids: a hole is a cylinder taken away, a lens is where two balls overlap. This page opens constructive solid geometry (union, intersection and difference of shapes), and then uses it to build a Dalek of about a hundred parts. The shapes it combines are those of the pages that follow.

Constructive Solid Geometry

Union, intersection and difference of solids. A CSG shape holds two operands, each of which may be a primitive, a group or another CSG shape, so trees of any size can be built from binary operations.

Step 1 — the ray's line as a set

Along a ray, a closed solid is a set of stretches of the t line: the ray is inside between its 1st and 2nd hit, its 3rd and 4th, and so on. A CSG operation is the same boolean combination of the two operands' sets, and the boundary of the result is exactly the hits at which the combination changes. Walk the merged, sorted hits; every hit on an operand enters or leaves it, so two flags inl and inr say which operands the ray is inside; at each hit intersection_allowed(op, lhit, inl, inr) asks whether the combination changes there:

union          (lhit and not inr) or (not lhit and not inl)
intersection   (lhit and inr)     or (not lhit and inl)
difference     (lhit and not inr) or (not lhit and inl)

The hits that remain are still hits on primitives, so a surface keeps the material and the normal of the primitive that made it. The coloured walls of a drilled block are the colours of the drills, and no normal is ever computed for the CSG shape.

As with groups (Groups and trees of boxes), shapes are values here, so the "parent" of a hit is attached on the way out: the CSG shape is appended to the hit object's chain of ancestors, and the chain carries the transformation of every enclosing group or CSG shape for world_to_object and normal_to_world. shape_includes decides whether a hit belongs to the left operand by id, recursively through groups and CSG shapes. A CSG shape also keeps a bounding box: the union of both boxes for a union, the left box for a difference, and the overlap for an intersection (empty when the boxes miss, and then nothing can hit it).

make a function called csg takes operation, left, right returns c
  let lb = parent_space_bounds(left)
  let rb = parent_space_bounds(right)
  let bounds = false
  if operation == "union" then
    let bounds = bounds_union(lb, rb)
  elif operation == "difference" then
    let bounds = lb
  elif lb != false and rb != false then
    let bounds = bounds_overlap(lb, rb)
  end
  return list_set(new_shape("csg"), 6, [operation, left, right, bounds, leaf_ids(left)])
end

make a function called csg_operation takes c returns op
  return c[6][0]
end

make a function called csg_left takes c returns s
  return c[6][1]
end

make a function called csg_right takes c returns s
  return c[6][2]
end

# the box common to two boxes, or false if they do not meet
make a function called bounds_overlap takes a, b returns o
  let lo = [0, 0, 0]
  let hi = [0, 0, 0]
  let k = 0
  while k < 3 do
    let lo = list_set(lo, k, num_max(a[0][k], b[0][k]))
    let hi = list_set(hi, k, num_min(a[1][k], b[1][k]))
    if lo[k] > hi[k] then
      return false
    end
    let k = k + 1
  end
  return [lo, hi]
end

# does shape a contain shape b (by id): a group or a CSG shape through its
# operands, anything else only if it is b
make a function called shape_includes takes a, b returns yes
  if a[5] == "group" then
    let kids = a[6][0]
    let k = 0
    while k < kids.length do
      if shape_includes(kids[k], b) then
        return true
      end
      let k = k + 1
    end
    return false
  end
  if a[5] == "csg" then
    return shape_includes(a[6][1], b) or shape_includes(a[6][2], b)
  end
  return a[0] == b[0]
end

# lhit: the hit is on the left operand; inl / inr: the ray is inside the left /
# right operand at the hit. The book's pseudocode calls the second operation
# "intersect", its tables "intersection": both are accepted.
make a function called intersection_allowed takes op, lhit, inl, inr returns ok
  if op == "union" then
    return (lhit and not inr) or (not lhit and not inl)
  end
  if op == "intersection" or op == "intersect" then
    return (lhit and inr) or (not lhit and inl)
  end
  if op == "difference" then
    return (lhit and not inr) or (not lhit and inl)
  end
  return false
end

make a function called filter_intersections takes c, xs returns kept
  let ids = c[6][4]
  let inl = false
  let inr = false
  let kept = []
  let k = 0
  while k < xs.length do
    let lhit = id_listed(ids, xs[k][1][0])
    if intersection_allowed(c[6][0], lhit, inl, inr) then
      let kept = list_push(kept, xs[k])
    end
    if lhit then
      let inl = not inl
    else
      let inr = not inr
    end
    let k = k + 1
  end
  return kept
end

make a function called csg_local_intersect takes c, r returns xs
  let params = c[6]
  if bounds_on(c) then
    if params[3] == false then
      return []
    end
    if not box_hit(params[3], r) then
      return []
    end
  end
  let all = intersections(list_concat(intersect(params[1], r), intersect(params[2], r)))
  let kept = filter_intersections(c, all)
  let frame = list_set(list_set(c, 6, [params[0], false, false, false]), 7, false)
  let xs = []
  let k = 0
  while k < kept.length do
    let xs = list_push(xs, list_set(kept[k], 1, attach_outer(kept[k][1], frame)))
    let k = k + 1
  end
  return xs
end

make a function called list_concat takes a, b returns out
  let out = a
  let k = 0
  while k < b.length do
    let out = list_push(out, b[k])
    let k = k + 1
  end
  return out
end

# the medium a hit belongs to when tracking which solids a ray is inside: the
# outermost CSG shape above it (one solid, however many primitives bound it), else
# the shape itself
make a function called container_key takes s returns key
  let key = s[0]
  let p = s[7]
  while p != false do
    if p[5] == "csg" then
      let key = p[0]
    end
    let p = p[7]
  end
  return key
end

Step 2 — refraction through a solid

Mirrors and glass finds n1 and n2 by walking the sorted hits and keeping a list of the objects the ray is inside, toggling an object in or out at each of its hits. That cannot work for a glass lens made by intersecting two spheres: the ray enters through one sphere and leaves through the other, so the walk sees "inside A, inside B" at the exit, never "outside". The fix is to keep the list by solid: a hit's medium is the outermost CSG shape above it, if any. Entering and leaving a lens are then the two toggles of one solid, and n1, n2 come out as (1, 3/2) and (3/2, 1). A test for this fails with the old rule and passes with the new one.cf. Mirrors and glass: this fixes the same trap

Step 3 — checks

The rule for keeping a hit is checked in all 24 cases against what the three operations mean (a hit on one operand is on the result's surface exactly when the point is inside or outside the other operand, as the operation requires), and every CSG shape is checked against its implicit function, the smaller of the operands' functions for a union, the larger for an intersection: each hit on the surface, no crossing missed, normals along the gradient. With and without the bounding boxes the hit lists are the same.

Further checks, sharing no code with what they check:

  • Membership: for seven CSG trees of spheres and cubes (all three operations, nested, with transformed operands), 567 rays are cast. For each, the points just before the first hit, between hit 2k and 2k+1, between 2k+1 and 2k+2, and after the last are tested by evaluating the tree on plain point-in-solid tests (|p| < 1, max |x| < 1, combined with and, or, and not). They must read outside, inside, outside, ... exactly, and every ray has an even number of hits. 300 inside stretches are checked.
  • Symmetry: a union and an intersection give the same boundary whichever operand is on the left.
  • Bounding boxes: the box rules, and boxes on = boxes off for 91 rays on the seven trees.
  • Normals through a transformed CSG shape, and the lens's n1, n2.

Step 4 — Putting it together

A cube rounded by intersecting it with a sphere and then drilled by three cylinders, one red, one green, one blue (the walls of each hole take its drill's colour); a die, the same rounded cube with six small spheres subtracted for the pips; and a glass lens, the intersection of two spheres.

# chapter 16: a cube rounded by a sphere and drilled by three cylinders (the
# cutters' colours show on the walls of the holes), a die with pips cut out by
# spheres, and a glass lens, the intersection of two spheres
make a function called carved_cube returns c
  let yellow = material_with(color((9/10), (4/5), (1/10)), (1/10), (7/10), (3/10), 100)
  let pale = material_with(color((3/5), (7/10), (9/10)), (1/10), (7/10), (3/10), 100)
  let red = material_with(color((9/10), (1/10), (1/10)), (1/10), (7/10), (3/10), 100)
  let green = material_with(color((1/10), (4/5), (1/10)), (1/10), (7/10), (3/10), 100)
  let blue = material_with(color((1/10), (1/5), (9/10)), (1/10), (7/10), (3/10), 100)
  let core = csg("intersection", set_material(cube(), yellow), set_material(set_transform(sphere(), scaling((27/20), (27/20), (27/20))), pale))
  let thin = scaling((1/2), 1, (1/2))
  let cx = set_material(set_transform(cylinder(), mat_mul(rotation_z(pi() / 2), thin)), red)
  let cy = set_material(set_transform(cylinder(), thin), green)
  let cz = set_material(set_transform(cylinder(), mat_mul(rotation_x(pi() / 2), thin)), blue)
  return csg("difference", csg("difference", csg("difference", core, cx), cy), cz)
end

make a function called die returns c
  let ivory = material_with(color((19/20), (19/20), (9/10)), (1/10), (7/10), (3/10), 100)
  let ink = material_with(color((1/20), (1/20), (1/20)), (1/10), (7/10), (3/10), 100)
  let body = csg("intersection", set_material(cube(), ivory), set_material(set_transform(sphere(), scaling((27/20), (27/20), (27/20))), ivory))
  # pips: two on the front (z = -1), three on top (y = 1), one on the right (x = 1)
  let centres = [[-(9/20), (9/20), -(21/20)], [(9/20), -(9/20), -(21/20)], [-(1/2), (21/20), (1/2)], [0, (21/20), 0], [(1/2), (21/20), -(1/2)], [(21/20), 0, 0]]
  let c = body
  let k = 0
  while k < centres.length do
    let p = centres[k]
    let pip = set_material(set_transform(sphere(), mat_mul(translation(p[0], p[1], p[2]), scaling((11/50), (11/50), (11/50)))), ink)
    let c = csg("difference", c, pip)
    let k = k + 1
  end
  return c
end

make a function called lens returns c
  let glass = with_reflective(with_transparency(with_refractive_index(material_with(color((1/20), (1/20), (2/25)), (1/20), (1/10), 1, 300), (3/2)), (9/10)), (9/10))
  let a = set_material(set_transform(sphere(), translation(0, 0, (1/2))), glass)
  let b = set_material(set_transform(sphere(), translation(0, 0, -(1/2))), glass)
  return csg("intersection", a, b)
end

make a function called build_csg_scene returns w
  let floor_m = with_pattern(material_with(color(1, 1, 1), (1/10), (9/10), 0, 200), set_pattern_transform(checkers_pattern(color((9/10), (9/10), (9/10)), color((3/10), (3/10), (3/10))), scaling((1/2), (1/2), (1/2))))
  let floor_p = set_material(plane(), floor_m)
  let left = set_transform(carved_cube(), mat_mul(translation(-(12/5), (17/20), 0), mat_mul(rotation_y((7/10)), mat_mul(rotation_x((3/10)), scaling((17/20), (17/20), (17/20))))))
  let mid = set_transform(die(), mat_mul(translation(0, (7/10), (3/10)), mat_mul(rotation_y(-(1/2)), scaling((7/10), (7/10), (7/10)))))
  let right = set_transform(lens(), mat_mul(translation((12/5), (9/10), 0), scaling((4/5), (4/5), (4/5))))
  return [[floor_p, left, mid, right], demo_light(), true]
end
# Chapter 16, "Putting It Together": a carved cube, a die and a glass lens, all made
# by combining spheres, cubes and cylinders.
include "../lib/spec.patlang"
include "../lib/png.patlang"
include "../lib/camera.patlang"
include "../lib/demo_scene.patlang"

let w = build_csg_scene()
let cam = set_camera_transform(camera(400, 200, pi() / 3), view_transform(point(0, 2.5, -7), point(0, (9/10), 0), vector(0, 1, 0)))
render_example(cam, w, "../out/ch16_csg.png")
> tools/prender.sh examples/ch16_csg.patlang
assembled 400x200 from 40 bands
rendered with 9 parallel processes (40 bands) in 368 s wall clock
A drilled cube, a die and a glass lens

Checking it independently

The recursive tracer learned CSG. Its trees carry the full world transformation on every primitive, its hits come from the primitives' own formulas, and the tree is evaluated by the book's merge-and-filter walk on plain arrays and hash tables. A whole tree is one medium for refraction. Every second pixel in each direction, glass lens included:

> powershell.exe -NoProfile -File tools/verify_recursive.ps1 out/ch16_csg.png -Scene csg -Stride 2
decoded by .NET: 400x200; pixels differing from the independent recursive tracer by more than 1 level: 0 (max difference 0); stride 2 in each direction, 20000 pixels checked

Negative control: the same tracer against the Triangles and meshes from files image, which it should reject.

> powershell.exe -NoProfile -File tools/verify_recursive.ps1 out/ch15_meshes.png -Scene csg -Stride 4
decoded by .NET: 400x200; pixels differing from the independent recursive tracer by more than 1 level: 4159 (max difference 218); stride 4 in each direction, 5000 pixels checked

Not done

The book's final suggestions: shapes that opt out of casting shadows (so that a transparent cutter can be subtracted without leaving a shadow), and subdividing groups into smaller boxes.

A Dalek, in Constructive Solid Geometry

The first half of this page gave the tracer CSG, part IV the shapes, and Light at a point, and shadows soft light and anti-aliasing. This page spends them all on one model. The target was a photograph of a Dalek; the model is built only from the tracer's own solids (spheres, cubes, cylinders, cones, tori) and from unions, intersections and differences of them. No mesh, no new shape, no change to the maths: everything below is a function in lib/dalek.patlang that returns a shape.

Step 1 — the tools

Five small functions turn the primitives into building blocks, each of them one transform and one material:

  • slab — a cube scaled to given half sizes and moved to a centre;
  • drum — the cylinder between two heights, with ends;
  • ball — a sphere of a given radius and centre;
  • ring — a torus about the vertical axis;
  • limb — a closed cylinder from one point to another. The transform is a translation to the first point, a rotation taking the y axis to the direction between the points (orient: turn about x by the angle from y, then about y to the azimuth) and a scaling by the radius and the length.
# the rotation taking the y axis to the direction (dx, dy, dz): turn about x by the
# angle from y, then about y to the right azimuth
make a function called orient takes dx, dy, dz returns m
  let len = sqrt(dx * dx + dy * dy + dz * dz)
  let theta = acos(dy / len)
  let phi = atan2(dx, dz)
  if dx == 0 and dz == 0 then
    let phi = 0
  end
  return mat_mul(rotation_y(phi), rotation_x(theta))
end

# a cylinder of radius r from point a to point b, ends closed
make a function called limb takes a, b, r, m returns s
  let dx = b[0] - a[0]
  let dy = b[1] - a[1]
  let dz = b[2] - a[2]
  let len = sqrt(dx * dx + dy * dy + dz * dz)
  let c = with_closed(with_limits(cylinder(), 0, 1), true)
  return set_material(set_transform(c, mat_mul(translation(a[0], a[1], a[2]), mat_mul(orient(dx, dy, dz), scaling(r, len, r)))), m)
end

# a disc-like slice of the vertical cylinder of radius r between heights y0 and y1
make a function called drum takes r, y0, y1, m returns s
  let c = with_closed(with_limits(cylinder(), y0, y1), true)
  return set_material(set_transform(c, scaling(r, 1, r)), m)
end

make a function called ball takes x, y, z, r, m returns s
  return set_material(set_transform(sphere(), mat_mul(translation(x, y, z), scaling(r, r, r))), m)
end

# a ring (torus) of radius r and tube radius t about the vertical axis at height y
make a function called ring takes r, t, y, m returns s
  return set_material(set_transform(torus(r, t), translation(0, y, 0)), m)
end

# a box with the given half sizes centred at (x, y, z)
make a function called slab takes x, y, z, hx, hy, hz, m returns s
  return set_material(set_transform(cube(), mat_mul(translation(x, y, z), scaling(hx, hy, hz))), m)
end

Step 2 — the parts

The model is seven groups, from the floor up.

The base is a regular octagon: two squares of the same size, one turned by 45°, and their intersection.

make a function called base_plate returns s
  let a = (33/50)
  let sq = set_transform(cube(), mat_mul(translation(0, (2/25), 0), scaling(a, (2/25), a)))
  let diamond = set_transform(cube(), mat_mul(translation(0, (2/25), 0), mat_mul(rotation_y(pi() / 4), scaling(a, (2/25), a))))
  let plate = csg("intersection", set_material(sq, slate()), set_material(diamond, slate()))
  return plate
end

The skirt is the Dalek's tapering eight-sided wall. Each facet is a tilted plane, and the solid behind a plane is a half-space, which a very large cube provides (its front face is the plane; the rest is out of sight). facet_space(k) puts the face through (0, 0.07, −0.56), tilts it by atan(16/73) so the wall narrows with height, and turns it k eighths of a turn. The skirt is the intersection of the eight half-spaces and a slab that gives it its height. Written as a chain, a ray would walk nine nested filters; written as a balanced tree it walks four levels.

# half-angle of the skirt's taper: the inradius shrinks 0.16 over a height of 0.73
make a function called skirt_tilt returns a
  return atan(16 / 73)
end

# inradius of the skirt's octagon at height y
make a function called skirt_inradius takes y returns r
  return (14/25) - (y - (7/100)) * 16 / 73
end

make a function called base_plate returns s
  let a = (33/50)
  let sq = set_transform(cube(), mat_mul(translation(0, (2/25), 0), scaling(a, (2/25), a)))
  let diamond = set_transform(cube(), mat_mul(translation(0, (2/25), 0), mat_mul(rotation_y(pi() / 4), scaling(a, (2/25), a))))
  let plate = csg("intersection", set_material(sq, slate()), set_material(diamond, slate()))
  return plate
end

# the half-space behind the facet k of the skirt: a big cube whose front face is the
# tilted plane through (0, 0.07, -0.56), turned k eighths of a turn about y
make a function called facet_space takes k, m returns s
  let big = 4
  let t = mat_mul(rotation_y(k * pi() / 4), mat_mul(translation(0, (7/100), -(14/25)), mat_mul(rotation_x(skirt_tilt()), mat_mul(translation(0, 0, big), scaling(big, big, big)))))
  return set_material(set_transform(cube(), t), m)
end

make a function called skirt returns s
  let m = bronze()
  # the eight facet half-spaces and the height slab, combined as a balanced tree (four
  # levels deep rather than nine) so that a ray runs through fewer nested filters
  let level = []
  let k = 0
  while k < 8 do
    let level = list_push(level, facet_space(k, m))
    let k = k + 1
  end
  # between the base plate (top at 0.08) and the shoulders (bottom at 0.80)
  let level = list_push(level, slab(0, (87/200), 0, 2, (73/200), 2, m))
  while level.length > 1 do
    let next = []
    let i = 0
    while i + 1 < level.length do
      let next = list_push(next, csg("intersection", level[i], level[i + 1]))
      let i = i + 2
    end
    if i < level.length then
      let next = list_push(next, level[i])
    end
    let level = next
  end
  return level[0]
end

The domes on the skirt: each is a sphere cut flat at the back by an intersection with a box, so the flat flank lies on the facet plane. A facet carries two columns of four. The eight domes of one facet are a group of their own, turned to that facet; the group's bounding box is what lets a ray skip the domes of the seven facets it cannot reach.

# one dome on a facet: a sphere cut flat at the back, set on the facet plane
make a function called stud takes m returns s
  let r = (85/1000)
  let shell = set_material(set_transform(sphere(), scaling(r, r, r)), m)
  let backing = slab(0, 0, -(1/10), (1/10), (1/10), (1/10), m)
  return csg("intersection", shell, backing)
end

make a function called studs returns g
  let m = dark_bronze()
  let g = group()
  let rows = [(19/100), (34/100), (49/100), (64/100)]
  let k = 0
  while k < 8 do
    # the eight domes of one facet, in a group of their own turned to that facet, so that a
    # ray is only tested against the domes of the facets it can reach
    let facet = group()
    let r = 0
    while r < 4 do
      let flank = 0
      while flank < 2 do
        let y = rows[r]
        let lateral = (flank - (1/2)) * (23/100)
        let t = mat_mul(translation(lateral, y, -skirt_inradius(y)), rotation_x(skirt_tilt()))
        let facet = add_child(facet, set_transform(stud(m), t))
        let flank = flank + 1
      end
      let r = r + 1
    end
    let g = add_child(g, set_transform(facet, rotation_y(k * pi() / 4)))
    let k = k + 1
  end
  return g
end

The shoulders and grille are cylinders between rings (tori), with three plates on the front and, above, a black core with three steel slats (a thin drum each) round it.

make a function called shoulders returns g
  let m = bronze()
  let g = group()
  let g = add_child(g, drum((43/100), (4/5), (23/20), m))
  let g = add_child(g, ring((43/100), (2/100), (4/5), dark_bronze()))
  let g = add_child(g, ring((43/100), (2/100), (23/20), dark_bronze()))
  # three plates on the front of the shoulders
  let p = steel()
  let g = add_child(g, slab(-(15/100), (96/100), -(43/100), (7/100), (6/100), (8/1000), dark_bronze()))
  let g = add_child(g, slab((2/100), (96/100), -(43/100), (9/100), (7/100), (8/1000), dark_bronze()))
  let g = add_child(g, slab((18/100), (96/100), -(43/100), (7/100), (6/100), (8/1000), dark_bronze()))
  return g
end

make a function called grille returns g
  let g = group()
  let black = rubber()
  let g = add_child(g, drum((26/100), (23/20), (143/100), black))
  let g = add_child(g, drum((31/100), (23/20), (122/100), dark_bronze()))
  let heights = [(124/100), (131/100), (138/100)]
  let k = 0
  while k < 3 do
    let g = add_child(g, drum((285/1000), heights[k] - (15/1000), heights[k] + (15/1000), steel()))
    let k = k + 1
  end
  return g
end

The head is a hemisphere (a sphere intersected with the box above its equator), a rim ring, an eye stalk with a blue lens inside a ring, and two ears. The arms are a plunger on one side (a rubber ball on a short cylinder at the end of a rod) and a gun with two barrels on the other. Both point ahead, each turned out from straight ahead by about 6 degrees (a slope of 21/200, kept rational so that the model stays exact), so that the two arms are about 12 degrees apart.rational slopes keep the model exact

# the head dome: a hemisphere, the sphere of radius 0.285 cut at its equator
make a function called dome returns s
  let r = (285/1000)
  let sph = set_material(set_transform(sphere(), mat_mul(translation(0, (143/100), 0), scaling(r, r, r))), bronze())
  let above = slab(0, (143/100) + (2/5), 0, (2/5), (2/5), (2/5), bronze())
  return csg("intersection", sph, above)
end

make a function called head_gear returns g
  let g = group()
  let g = add_child(g, dome())
  let g = add_child(g, ring((285/1000), (15/1000), (143/100), dark_bronze()))
  # the eye stalk and its lens
  let a = [0, (160/100), -(22/100)]
  let b = [0, (166/100), -(44/100)]
  let g = add_child(g, limb(a, b, (2/100), steel()))
  let g = add_child(g, ball(b[0], b[1], b[2], (45/1000), lens_material()))
  let t = mat_mul(translation(b[0], b[1], b[2]), orient(0, (6/100), -(22/100)))
  let g = add_child(g, set_material(set_transform(torus((6/100), (12/1000)), t), steel()))
  # ears
  let flank = -1
  while flank <= 1 do
    let g = add_child(g, limb([flank * (2/10), (155/100), 0], [flank * (3/10), (166/100), 0], (15/1000), steel()))
    let g = add_child(g, ball(flank * (3/10), (166/100), 0, (25/1000), steel()))
    let flank = flank + 2
  end
  return g
end

# the plunger arm (to the -x side) and the gun (to the +x side): both point ahead, towards -z, each turned out from straight
# ahead by `splay` degrees, so that the two are 2 x splay = about 12 degrees apart
# the slope of an arm out from straight ahead, 21/200 (the tangent of 6.0 degrees, as a rational so that the model stays exact)
make a function called arm_slope returns t
  return (21/200)
end

# the point `ahead` in front of `from` (towards -z) on the arm that leaves it turned out by the splay, to the side `side` (+1 is +x, -1 is -x)
make a function called along_arm takes from, ahead, side returns p
  return [from[0] + side * ahead * arm_slope(), from[1], from[2] - ahead]
end

make a function called arms returns g
  let g = group()
  # the plunger: a block on the flank, a rod, a thin drum and the rubber cup
  let p0 = [-(40/100), (98/100), -(30/100)]
  let g = add_child(g, slab(-(40/100), (98/100), -(26/100), (7/100), (7/100), (8/100), dark_bronze()))
  let g = add_child(g, limb(p0, along_arm(p0, (36/100), -1), (26/1000), steel()))
  let g = add_child(g, limb(along_arm(p0, (36/100), -1), along_arm(p0, (38/100), -1), (65/1000), rubber()))
  let c = along_arm(p0, (44/100), -1)
  let g = add_child(g, ball(c[0], c[1], c[2], (75/1000), rubber()))
  # the gun: a block on the other flank and two barrels
  let g = add_child(g, slab((40/100), (98/100), -(26/100), (6/100), (6/100), (8/100), dark_bronze()))
  let q0 = [(37/100), (99/100), -(30/100)]
  let q1 = [(43/100), (97/100), -(30/100)]
  let g = add_child(g, limb(q0, along_arm(q0, (26/100), 1), (22/1000), steel()))
  let g = add_child(g, limb(q1, along_arm(q1, (24/100), 1), (15/1000), steel()))
  let e = along_arm(q0, (26/100), 1)
  let g = add_child(g, ball(e[0], e[1], e[2], (28/1000), steel()))
  return g
end

Step 3 — the scene and the cameras

A floor, a back wall, the model turned a little about the vertical axis, and a 3 × 3 area light (Light at a point, and shadows) to the front left, with the jitter on. Two cameras: front, slightly above and to the right of the Dalek, and side, low and from the plunger arm's side. The picture is 1.4 times as tall as it is wide.

make a function called drum_along_x takes x0, x1, y, z, r, m returns s
  return limb([x0, y, z], [x1, y, z], r, m)
end

# the whole model, standing on y = 0
make a function called dalek returns g
  let g = group()
  let g = add_child(g, base_plate())
  let g = add_child(g, skirt())
  let g = add_child(g, studs())
  let g = add_child(g, shoulders())
  let g = add_child(g, grille())
  let g = add_child(g, head_gear())
  let g = add_child(g, arms())
  return g
end

make a function called build_dalek_scene returns w
  let floor_m = with_reflective(material_with(color((17/20), (17/20), (9/10)), (1/10), (9/10), 0, 200), (1/10))
  let floor_p = set_material(plane(), floor_m)
  let wall_m = material_with(color((7/10), (7/10), (3/4)), (1/10), (9/10), 0, 200)
  let wall = set_material(set_transform(plane(), mat_mul(translation(0, 0, 5), rotation_x(pi() / 2))), wall_m)
  let model = set_transform(dalek(), rotation_y((1/5)))
  let light = area_light(point(-3, 4, -4), vector(2, 0, 0), 3, vector(0, 0, 2), 3, color(1, 1, 1), "r2")
  return [[floor_p, wall, model], light, true]
end

Step 4 — scenarios and oracles

The measurements are all exact consequences of the dimensions in the file:

  • The dome and the grille: a ray down the axis meets the top of the head at y = 1.43 + 0.285; a ray at a slat's height meets the slat's rim at radius 0.285, one between slats meets the core at radius 0.26; the rim ring sits at the equator.
  • The skirt: a ray through the middle of the front facet meets the tilted plane at exactly the inradius of the tapering octagon, and the hit's normal is (0, 16, −73)/|…|. Along a diagonal the skirt is just as wide (eight identical facets); above and below its height band it is absent.
  • The base is a regular octagon: the same inradius along an axis and along a diagonal, and inradius / cos 22.5° to a corner.
  • The studs: a dome meets a ray twice, at its curved front (z = −0.085) and its flat back; a ray 0.09 off axis misses; eight facet groups of eight domes each, all inside the base's footprint.
  • Symmetry: the skirt, domes and shoulders are mirror images, so a ray at x and one at −x give the same hits.
  • Arms and eye: the plunger ball, the plunger arm and the lens are met where their dimensions put them; the whole model stands on the floor and tops out under 1.75.
  • The arms: the axes of the plunger's rod and of the gun's two barrels, taken through their transformations, are 11.99 degrees apart (a tolerance of 1), all pointing ahead, the plunger turned out to -x and the gun to +x.

Step 5 — Putting it together

Both pictures use adaptive anti-aliasing (the corners and centre of each pixel, cut in four where a corner differs from the centre by more than 3/20, one cut deep) and the 3 × 3 jittered area light, so each picture-pixel inside the model costs up to a few dozen rays and nine shadow rays for each. They were rendered by 18 threads sharing one built model (One model, many threads, and the graphics card), in 1168 seconds (front) and 1098 seconds (side).

The Dalek from the front The Dalek from its plunger side

Speed

The Dalek has 244 shapes. Three changes of the library came out of making it affordable, and each leaves every earlier picture and test untouched:

  • the box test and the cube's slab test read the ray's components directly instead of building multivector products (a bounding box is tested for every group entered);
  • the 64 domes were one flat group of 64, so every ray tested its box; grouping them by facet made it eight boxes then at most eight domes;
  • the skirt's nine-deep chain of intersections became a balanced tree.

Profiled at 3 × 3 light samples, a pixel inside the model costs about 64 ms, of which shadow rays are about 50: with shadows off it costs 14.5 ms.

Checking it independently

The recursive PowerShell tracer of the the pages it builds on takes the scene from a text file, so the model stays defined once. tools/export_scene.patlang writes the flattened world (every primitive with its full matrix, the CSG nodes by reference, the light's sample points, the camera), and the tracer reads it with its own CSG filter, its own adaptive sampler and its own shading. The comparison is on every 16th pixel in each direction.

> v2_interp.exe tools/export_scene.patlang --view front --out out/dalek_front.txt
wrote out/dalek_front.txt (244 shapes)
> v2_interp.exe tools/export_scene.patlang --view side --out out/dalek_side.txt
wrote out/dalek_side.txt (244 shapes)

The front picture:

> powershell.exe -NoProfile -File tools/verify_recursive.ps1 out/ch19_dalek_front.png -SceneFile out/dalek_front.txt -Adaptive 1,0.15 -Stride 16
decoded by .NET: 400x560; pixels differing from the independent recursive tracer by more than 1 level: 0 (max difference 0); stride 16 in each direction, 875 pixels checked

The side picture:

> powershell.exe -NoProfile -File tools/verify_recursive.ps1 out/ch19_dalek_side.png -SceneFile out/dalek_side.txt -Adaptive 1,0.15 -Stride 16
decoded by .NET: 400x560; pixels differing from the independent recursive tracer by more than 1 level: 0 (max difference 0); stride 16 in each direction, 875 pixels checked

Negative control: the front picture against the side view's scene, which should be rejected nearly everywhere the model is:

> powershell.exe -NoProfile -File tools/verify_recursive.ps1 out/ch19_dalek_front.png -SceneFile out/dalek_side.txt -Adaptive 1,0.15 -Stride 16
decoded by .NET: 400x560; pixels differing from the independent recursive tracer by more than 1 level: 840 (max difference 179); stride 16 in each direction, 875 pixels checked

How it is checked

> v2_interp.exe tests/run.patlang --node shapes/csg --depth 2
[ ok ] Constructive solid geometry
[ ok ]   The rule for keeping a hit follows from what union, intersection and difference mean (all 24 cases)
[ ok ]   A CSG shape knows its operation and its two parts, which know their parent
[ ok ]   A ray that misses both parts misses the whole
[ ok ]   Union, against the smaller of the two functions
[ ok ]     Every hit lies on the surface
[ ok ]     No crossing of the surface is missed
[ ok ]     Normals follow the gradient of the surface's function
[ ok ]   Intersection, against the larger
[ ok ]     Every hit lies on the surface
[ ok ]     No crossing of the surface is missed
[ ok ]     Normals follow the gradient of the surface's function
[ ok ]   Difference, against max(f_left, -f_right)
[ ok ]     Every hit lies on the surface
[ ok ]     No crossing of the surface is missed
[ ok ]     Normals follow the gradient of the surface's function
[ ok ]   A difference of a union, turned and moved
[ ok ]     Every hit lies on the surface
[ ok ]     No crossing of the surface is missed
[ ok ]     Normals follow the gradient of the surface's function
[ ok ]   With and without bounding boxes  (all, giving the same answer)
[ ok ]     naive: intersect both parts every time
[ ok ]     faster: skip what the ray's box test rules out
[ ok ]   Further checks, independent of the code they check
[ ok ]     Hits mark the solid's boundary
[ ok ]     A union is symmetric: same boundary either way round
[ ok ]     Bounds of a union: both operands
[ ok ]     A ray at x = 10 misses the CSG scaled by 2 (its sphere spans x 0..4, its cube -2..2)
checks: 21 passed, 0 failed, 0 blocked, 0 skipped, 0 not run
ALL PASSED
> v2_interp.exe tests/run.patlang --node scene/dalek --depth 2
[ ok ] The Dalek
[ ok ]   The Dalek
[ ok ]     Down the axis the first hit is the top of the dome: y = 1.43 + 0.285, t = 1.285
[ ok ]     A ray through the middle of the front facet meets the skirt twice (front and back facet)
[ ok ]     The base plate: along an axis it is 0.66 from the centre
[ ok ]     A dome meets a ray twice: the curved front (z = -0.085) and the flat back (z = 0)
[ ok ]     The skirt, domes and shoulders are mirror symmetric
[ ok ]     The plunger's rubber ball (radius 0.075, centred 0.44 ahead of the arm's start and 0.0462 ...
[ ok ]     The plunger's rod and the gun's barrels point about 12 degrees apart
checks: 7 passed, 0 failed, 0 blocked, 0 skipped, 0 not run
ALL PASSED

Graduate Attributes

Modelled on this page

  • Subject Specialist: Complex geometric operations are reduced to boolean combinations of ray segments, allowing precise control over material properties and normals.
  • Researcher: Implementation logic is validated against all twenty-four possible cases of boolean operations to ensure correctness.
  • Digitally Fluent: A complex model is constructed entirely from primitive shapes and mathematical operations without relying on external mesh files.

University of Reading Graduate Attributes framework.