Shared Materialization

A Doric temple authored as a family tree: sixteen identical fluted columns and thirty-six triglyphs, each with its own key path. The CSG IR is content-addressed, so the evaluator builds one column and one triglyph for each facing, and a second evaluation with wider bays is almost all cache hits. The console shows the counts. The proportions of the order are published figures. The taper, the steps, the cella and the roof are a display model.
Code
This is the full program. The playground runs it in your browser without a change.
ts
import { compound, csg, unwrap } from 'brepjs/quick';
import { color } from 'brepjs/playground';
import { family, el, resolve, evaluateModel, tRotate, tTranslate, type Element } from 'brepjs-families';
// Doric peristyle: many identical columns, one materialized column. Units are millimetres, Z is up.
// The proportions of the order are published figures. The steps, the taper, the roof and the pitch are a display model.
interface PeristyleParams {
columns?: number; // columns on each long side
diameter?: number; // lower diameter of the shaft: every other size follows from it
bay?: number; // triglyph intervals between two columns: 2 is close-set, 3 is wide
explode?: number; // 0 closes the temple, 1 lifts the cornice and the roof to show the frieze
}
type Vec3 = readonly [number, number, number];
type Shape = Parameters<typeof compound>[0][number];
// ---- published figures (Roman Doric order after Vignola, in modules: 1 module = half a diameter)
const ORDER = { column: 16, base: 1, capital: 1, architrave: 1, frieze: 1.5, cornice: 1.5, projection: 2 };
const FLUTES = 20; // shallow channels that meet in a sharp arris
const TRIGLYPH = 1; // width
// ---- display model (modules)
const TAPER = 5 / 6; // upper diameter over lower diameter
const FLUTE = { centre: 1.15, radius: 0.2254 }; // flute circle, as fractions of the shaft radius
const METOPE = 1.5; // clear width between two triglyphs
const STEP = { count: 3, rise: 0.5, tread: 0.9, margin: 1.7 };
const PITCH = 14; // degrees
const ENDS = 4; // columns across each end
const DOOR = { width: 3, height: 10 };
const LIFT = 4; // rise of the cornice and the roof at explode = 1
// The lesson. Each Column call builds the whole fluted recipe again, but the recipe hashes the same
// every time, so the evaluator cuts the 20 flutes once. Only the Translate around it is new.
const Column = family<{ module: number; at: Vec3 }>('Column', (p) =>
el('Geometry', { node: columnRecipe(p.module), transform: [tTranslate(p.at)] })
);
const Triglyph = family<{ module: number; at: Vec3; turn: number }>('Triglyph', (p) =>
el('Geometry', { node: triglyphRecipe(p.module), transform: [tRotate(p.turn), tTranslate(p.at)] })
);
const Peristyle = family<{ columns: number; module: number; bay: number; explode: number }>('Peristyle', (p) => {
const m = p.module;
const pitch = p.bay * (TRIGLYPH + METOPE) * m; // column spacing follows the frieze
const lx = (p.columns - 1) * pitch;
const ly = (ENDS - 1) * pitch;
const face = TAPER * m; // the frieze face stands over the top of the shaft
const top = ORDER.column * m;
const columns: Element[] = [];
for (let i = 0; i < p.columns; i++) {
columns.push(Column({ key: 'south-' + i, module: m, at: [i * pitch, 0, 0] }));
columns.push(Column({ key: 'north-' + i, module: m, at: [i * pitch, ly, 0] }));
}
for (let j = 1; j < ENDS - 1; j++) {
columns.push(Column({ key: 'west-' + j, module: m, at: [0, j * pitch, 0] }));
columns.push(Column({ key: 'east-' + j, module: m, at: [lx, j * pitch, 0] }));
}
const unit = (TRIGLYPH + METOPE) * m;
const z = top + ORDER.architrave * m;
const triglyphs: Element[] = [];
for (let i = 0; i <= (p.columns - 1) * p.bay; i++) {
triglyphs.push(Triglyph({ key: 'tri-south-' + i, module: m, at: [i * unit, -face, z], turn: 0 }));
triglyphs.push(Triglyph({ key: 'tri-north-' + i, module: m, at: [i * unit, ly + face, z], turn: 180 }));
}
for (let j = 0; j <= (ENDS - 1) * p.bay; j++) {
triglyphs.push(Triglyph({ key: 'tri-west-' + j, module: m, at: [-face, j * unit, z], turn: -90 }));
triglyphs.push(Triglyph({ key: 'tri-east-' + j, module: m, at: [lx + face, j * unit, z], turn: 90 }));
}
return el('Group', {}, [...columns, ...triglyphs, ...scenery(m, lx, ly, pitch, p.explode)]);
});
function peristyle({ columns = 6, diameter = 900, bay = 2, explode = 1 }: PeristyleParams = {}) {
const props = { key: 'temple', columns: Math.max(2, Math.round(columns)), module: diameter / 2, explode };
const ev = new csg.Evaluator();
const model = evaluateModel(resolve(Peristyle({ ...props, bay })), ev, {}, { shapes: true });
const s1 = ev.cacheStats();
// Identity is the key path. The recipe under each Translate is the shared part.
const tally = (type: string): string => {
const all = model.root.children.filter((c) => c.type === type);
const recipes = new Set(all.map((c) => (c.geometry.kind === 'Translate' ? c.geometry.target : c.geometry).structuralHash));
return all.length + ' ' + type + ' key paths share ' + recipes.size + ' recipe' + (recipes.size > 1 ? 's' : '');
};
console.log(tally('Column') + ', ' + tally('Triglyph'));
console.log('first eval: ' + s1.misses + ' misses, ' + s1.hits + ' hits, ' + s1.entries + ' cache entries');
// Respace the columns: each Translate is evaluated again, the fluted column never is.
evaluateModel(resolve(Peristyle({ ...props, bay: bay + 1 })), ev);
const s2 = ev.cacheStats();
console.log('respaced: +' + (s2.misses - s1.misses) + ' misses, +' + (s2.hits - s1.hits) + ' hits (the fluted column is a pure hit)');
// Colour by family. compound() copies the shapes, so the evaluator can free its cache.
const groups = new Map<string, Shape[]>();
for (const n of model.byKeyPath.values()) {
if (!n.shape) continue;
groups.set(n.type, [...(groups.get(n.type) ?? []), unwrap(n.shape)]);
}
const bodies = [...groups.entries()].map(([type, shapes]) => color(compound(shapes), COLORS[type] ?? '#f5f2ea'));
ev[Symbol.dispose]();
return bodies;
}
const COLORS: Record<string, string> = {
Column: '#f5f2ea',
Triglyph: '#12b5b0',
Step: '#c3c9d1',
Architrave: '#f5f2ea',
Frieze: '#e4dfd2',
Cornice: '#f5f2ea',
Pediment: '#e4dfd2',
Cella: '#d3a956',
Roof: '#c8643c',
Tile: '#a84f2d',
};
// ---- recipes: plain functions that return a csg node
// A profile in the (radius, height) half-plane, turned about the Z axis.
function lathe(points: ReadonlyArray<readonly [number, number]>): csg.IRNode {
return csg.revolve(csg.polygon(points.map(([r, h]): [number, number, number] => [r, 0, h])));
}
function arc(cx: number, cy: number, r: number, from: number, to: number, steps: number): Array<[number, number]> {
return Array.from({ length: steps + 1 }, (_, i): [number, number] => {
const a = ((from + ((to - from) * i) / steps) * Math.PI) / 180;
return [cx + r * Math.cos(a), cy + r * Math.sin(a)];
});
}
function columnRecipe(m: number): csg.IRNode {
const r0 = m;
const r1 = TAPER * m;
const shaftH = (ORDER.column - ORDER.base - ORDER.capital) * m;
const z0 = ORDER.base * m;
// Base: a square plinth, a torus and a fillet.
const plinth = csg.translate(csg.box(2.7 * m, 2.7 * m, 0.45 * m), [-1.35 * m, -1.35 * m, 0]);
const torus = lathe([[0, 0.45 * m], ...arc(1.1 * m, 0.65 * m, 0.2 * m, -90, 90, 6), [1.08 * m, 0.85 * m], [1.08 * m, z0], [0, z0]]);
// Shaft: one tapered sector between two arrises, with one flute cut in it, then turned 20 times.
const step = 360 / FLUTES;
const sector = csg.revolve(csg.polygon([[0, 0, z0], [r0, 0, z0], [r1, 0, z0 + shaftH], [0, 0, z0 + shaftH]]), step);
const lean = Math.atan(((r0 - r1) * FLUTE.centre) / shaftH);
const tool = csg.translate(
csg.rotate(csg.cone(FLUTE.radius * r0, FLUTE.radius * r1, shaftH / Math.cos(lean)), (-lean * 180) / Math.PI, { axis: [0, 1, 0] }),
[FLUTE.centre * r0, 0, z0]
);
const fluted = csg.cut(sector, csg.rotate(tool, step / 2));
const shaft = csg.compound(Array.from({ length: FLUTES }, (_, i) => csg.rotate(fluted, i * step)));
// Capital: necking, annulet, echinus and a square abacus.
const zc = z0 + shaftH;
const echinus = lathe([
[0, zc], [r1, zc], [r1, zc + 0.33 * m], [r1 + 0.07 * m, zc + 0.33 * m], [r1 + 0.07 * m, zc + 0.43 * m],
...arc(0.85 * m, zc + 0.7 * m, 0.33 * m, -80, 0, 5), [0, zc + 0.7 * m],
]);
const abacus = csg.translate(csg.box(2.5 * m, 2.5 * m, 0.3 * m), [-1.25 * m, -1.25 * m, zc + 0.7 * m]);
return csg.compound([plinth, torus, shaft, echinus, abacus]);
}
// Plan outline with two V channels and two half channels, extruded up, with a cap band.
function triglyphRecipe(m: number): csg.IRNode {
const w = TRIGLYPH * m;
const h = ORDER.frieze * m;
const p = 0.14 * m;
const v = 0.1 * m;
const g = w / 10;
const plan: Array<[number, number]> = [
[-w / 2, 0], [-w / 2, -p + v], [-w / 2 + g, -p],
[-w / 6 - g, -p], [-w / 6, -p + v], [-w / 6 + g, -p],
[w / 6 - g, -p], [w / 6, -p + v], [w / 6 + g, -p],
[w / 2 - g, -p], [w / 2, -p + v], [w / 2, 0],
];
const body = csg.extrude(csg.polygon(plan.map(([x, y]): [number, number, number] => [x, y, 0])), [0, 0, 0.86 * h]);
const cap = csg.translate(csg.box(w, p + 0.03 * m, 0.14 * h), [-w / 2, -p - 0.03 * m, 0.86 * h]);
return csg.compound([body, cap]);
}
// A rectangular ring around the column axes: out is the overhang past the axes, inset is the inner face.
function ring(lx: number, ly: number, out: number, inset: number, z: number, h: number): csg.IRNode {
const outer = csg.translate(csg.box(lx + 2 * out, ly + 2 * out, h), [-out, -out, z]);
const inner = csg.translate(csg.box(lx - 2 * inset, ly - 2 * inset, h), [inset, inset, z]);
return csg.cut(outer, inner);
}
const Part = (type: string) =>
family<{ node: csg.IRNode; lift?: number }>(type, (p) => el('Geometry', { node: p.node, transform: [tTranslate([0, 0, p.lift ?? 0])] }));
const Step = Part('Step');
const Architrave = Part('Architrave');
const Frieze = Part('Frieze');
const Cornice = Part('Cornice');
const Pediment = Part('Pediment');
const Roof = Part('Roof');
const Cella = Part('Cella');
const Tile = family<{ node: csg.IRNode; at: Vec3 }>('Tile', (p) => el('Geometry', { node: p.node, transform: [tTranslate(p.at)] }));
// Steps, entablature, pediments and a tiled roof around the columns.
function scenery(m: number, lx: number, ly: number, pitch: number, explode: number): Element[] {
const face = TAPER * m;
const top = ORDER.column * m;
const steps = Array.from({ length: STEP.count }, (_, k) => {
const out = (STEP.margin + k * STEP.tread) * m;
const node = csg.translate(csg.box(lx + 2 * out, ly + 2 * out, STEP.rise * m), [-out, -out, -(k + 1) * STEP.rise * m]);
return Step({ key: 'step-' + k, node });
});
const zf = top + ORDER.architrave * m;
const zc = zf + ORDER.frieze * m;
const architrave = csg.compound([
ring(lx, ly, face, face, top, 0.84 * m),
ring(lx, ly, face + 0.1 * m, face, top + 0.84 * m, 0.16 * m),
]);
const cornice = csg.compound([
ring(lx, ly, face + 0.45 * m, face, zc, 0.4 * m),
ring(lx, ly, face + ORDER.projection * m, face, zc + 0.4 * m, 0.7 * m),
ring(lx, ly, face + (ORDER.projection + 0.2) * m, face, zc + 1.1 * m, 0.4 * m),
]);
// Cella: the closed room inside the columns, with a doorway to the east.
const room = csg.translate(csg.box(lx - 2 * pitch + 2 * face, ly - 2 * pitch + 2 * face, top), [pitch - face, pitch - face, 0]);
const inside = csg.translate(csg.box(lx - 2 * pitch, ly - 2 * pitch, top), [pitch, pitch, 0]);
const doorway = csg.translate(csg.box(4 * face, DOOR.width * m, DOOR.height * m), [lx - pitch - 2 * face, (ly - DOOR.width * m) / 2, 0]);
const cella = csg.cutAll(room, [inside, doorway]);
const up = LIFT * explode * m;
// Roof: a chevron section along X. Each pediment is the wall under it at one end.
const zr = zc + ORDER.cornice * m;
const eave = face + (ORDER.projection + 0.2) * m;
const slope = Math.tan((PITCH * Math.PI) / 180);
const rise = slope * (ly / 2 + eave);
const t = 0.3 * m;
const yz = (pts: ReadonlyArray<readonly [number, number]>, x: number, len: number): csg.IRNode =>
csg.extrude(csg.polygon(pts.map(([y, z]): [number, number, number] => [x, y, z])), [len, 0, 0]);
const roof = yz(
[[-eave, zr], [ly / 2, zr + rise], [ly + eave, zr], [ly + eave, zr + t], [ly / 2, zr + rise + t], [-eave, zr + t]],
-eave,
lx + 2 * eave
);
const low = slope * (eave - face);
const wall: Array<[number, number]> = [[-face, zr], [ly + face, zr], [ly + face, zr + low], [ly / 2, zr + rise], [-face, zr + low]];
// Cover tiles: one rib recipe for each slope, placed along the roof.
const ribLen = (ly / 2 + eave) / Math.cos((PITCH * Math.PI) / 180);
const rib = csg.translate(csg.box(0.22 * m, ribLen, 0.12 * m), [-0.11 * m, 0, 0]);
const south = csg.translate(csg.rotate(rib, PITCH, { axis: [1, 0, 0] }), [0, -eave, zr + t]);
const north = csg.translate(csg.rotate(csg.translate(rib, [0, -ribLen, 0]), -PITCH, { axis: [1, 0, 0] }), [0, ly + eave, zr + t]);
const ribs = Math.floor((lx + 2 * eave - 0.6 * m) / (1.5 * m));
const x0 = lx / 2 - (ribs * 1.5 * m) / 2;
const tiles = Array.from({ length: ribs + 1 }, (_, i) => [
Tile({ key: 'tile-south-' + i, node: south, at: [x0 + i * 1.5 * m, 0, up] }),
Tile({ key: 'tile-north-' + i, node: north, at: [x0 + i * 1.5 * m, 0, up] }),
]).flat();
return [
...steps,
Architrave({ key: 'architrave', node: architrave }),
Frieze({ key: 'frieze', node: ring(lx, ly, face, face, zf, ORDER.frieze * m) }),
...(lx > 2 * pitch ? [Cella({ key: 'cella', node: cella })] : []), // a short temple has no room
Cornice({ key: 'cornice', node: cornice, lift: up }),
Pediment({ key: 'pediment-west', node: yz(wall, -face, 2 * face), lift: up }),
Pediment({ key: 'pediment-east', node: yz(wall, lx - face, 2 * face), lift: up }),
Roof({ key: 'roof', node: roof, lift: up }),
...tiles,
];
}
export default peristyle();Functions used
compound csg unwrap color family el resolve evaluateModel tRotate tTranslate