Room Composition

A floor of hotel rooms shown as a plan cut. Room is a family composed from Wall, Door and Window, with a Bath family and the furniture inside it, and its group transform places the whole room as a unit: the south row is the same family turned half a turn. Key paths nest through the composition, down to suite/n1/bath/west/voids:door/fill/leaf, and rooms of equal width share their recipes. The door leaf and the mattress sizes are published figures. The plan, the walls and the furniture 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';
// A floor of hotel rooms, composed from families and shown as a plan cut. Units are millimetres, Z is up.
// The door leaf and the mattress sizes are published figures. The plan, the walls and the furniture are a display model.
interface SuiteParams {
widths?: readonly number[]; // room widths along the corridor: a room of 4 m or more gets the large bed
cut?: number; // height of the plan cut: 2700 shows the full walls
open?: number; // swing of the room doors in degrees
}
type Vec3 = readonly [number, number, number];
type Shape = Parameters<typeof compound>[0][number];
// ---- published figures
const LEAF = { width: 838, height: 1981, thickness: 44 }; // standard door leaf
const MATTRESS = { single: 900, large: 1600, length: 2000 }; // European sizes
// ---- display model
const ROOM = { depth: 5000, height: 2700, wall: 200 };
const BATH = { width: 1700, depth: 2200, wall: 100 };
const CORRIDOR = 1800;
const FRAME = 60; // window frame section
const GAP = 10; // between a door leaf and its hole
const WINDOW = { width: 1800, height: 1300, sill: 800 };
// The starter families (npx brepjs add room wall door), without their zod schemas. A wall is built
// along its own X with its outer face at y = 0, then turned into place. Its openings ride with it.
const Wall = family<{ length: number; height: number; thickness: number; at: Vec3; turn: number; voids?: readonly Element[] }>('Wall', (p) =>
el('Box', {
size: [p.length, p.thickness, p.height],
voids: p.voids ?? [],
transform: [tRotate(p.turn), tTranslate(p.at)],
})
);
const Door = family<{ along: number; depth: number; cut: number; open: number }>(
'Door',
(p) =>
el('Box', { size: [LEAF.width + 2 * GAP, p.depth, LEAF.height + GAP], transform: [tTranslate([p.along, 0, 0])] }, [
Leaf({ key: 'leaf', height: Math.min(LEAF.height, p.cut), at: [GAP, p.depth - LEAF.thickness, 0], open: p.open }),
]),
{ role: 'fill' }
);
// A composed family depends on Wall and Door the way a component depends on its children. The group
// transform places the room as a unit: the walls, the bathroom and the furniture stay in room coordinates.
const Room = family<{ width: number; at: Vec3; turn: number; cut: number; open: number }>('Room', (p) => {
const w = p.width;
const d = ROOM.depth;
const t = ROOM.wall;
const shared = { height: Math.min(ROOM.height, p.cut), thickness: t };
const window = Window({ key: 'window', along: (w - WINDOW.width) / 2, depth: t, cut: p.cut });
return el('Group', { transform: [tRotate(p.turn), tTranslate(p.at)] }, [
Wall({ key: 'south', ...shared, length: w, at: [0, 0, 0], turn: 0, voids: [Door({ key: 'door', along: t + 150, depth: t, cut: p.cut, open: p.open })] }),
Wall({ key: 'north', ...shared, length: w, at: [w, d, 0], turn: 180, voids: [window] }),
Wall({ key: 'east', ...shared, length: d - 2 * t, at: [w, t, 0], turn: 90 }),
Wall({ key: 'west', ...shared, length: d - 2 * t, at: [0, d - t, 0], turn: 270 }),
Floor({ key: 'floor', size: [w, d, 150], at: [0, 0, -150] }),
// A second composed family in the first one: its key paths nest one level deeper.
Bath({ key: 'bath', at: [w - t - BATH.width, t, 0], cut: p.cut }),
...furniture(w, p.cut),
]);
});
const Suite = family<{ items: readonly Element[] }>('Suite', (p) => el('Group', {}, p.items));
function suite({ widths = [4600, 3400, 4600], cut = 1200, open = 60 }: SuiteParams = {}) {
// Two rows of rooms on a corridor. The south row is the same family, turned half a turn.
const rooms: Element[] = [];
let x = 0;
widths.forEach((width, i) => {
rooms.push(Room({ key: 'n' + (i + 1), width, at: [x, CORRIDOR / 2, 0], turn: 0, cut, open }));
rooms.push(Room({ key: 's' + (i + 1), width, at: [x + width, -CORRIDOR / 2, 0], turn: 180, cut, open }));
x += width;
});
const corridor = Carpet({ key: 'corridor', size: [x, CORRIDOR, 150], at: [0, -CORRIDOR / 2, -150] });
const tree = resolve(Suite({ key: 'suite', items: [...rooms, corridor] }));
const ev = new csg.Evaluator();
const model = evaluateModel(tree, ev, {}, { shapes: true });
// Key paths compose through the tree.
const paths = [...model.byKeyPath.keys()];
const deepest = paths.reduce((a, b) => (b.split('/').length > a.split('/').length ? b : a), '');
if (!model.byKeyPath.has('suite/n1/south/voids:door')) throw new Error('door missing');
const stats = ev.cacheStats();
console.log(rooms.length + ' rooms, ' + paths.length + ' key paths, for example suite/n1/south/voids:door');
console.log('deepest: ' + deepest);
console.log('evaluator: ' + stats.misses + ' misses, ' + stats.hits + ' hits (equal rooms share their recipes)');
// Colour by family. Opening, Door and Window nodes are the cut bodies of the holes and are not drawn.
const groups = new Map<string, Shape[]>();
for (const n of model.byKeyPath.values()) {
if (COLORS[n.type] && n.shape) 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](); // compound() copied the shapes
return bodies;
}
const COLORS: Record<string, string> = {
Wall: '#f5f2ea',
Floor: '#d3a956',
Carpet: '#48515c',
Leaf: '#12b5b0',
Frame: '#c3c9d1',
Glass: '#9fd3e6',
Tile: '#c3c9d1',
Fixture: '#f5f2ea',
Timber: '#a67c52',
Linen: '#f5f2ea',
Blanket: '#12b5b0',
Seat: '#ff5a1f',
Steel: '#48515c',
};
// ---- the smaller families
const Block = (type: string) =>
family<{ size: Vec3; at: Vec3 }>(type, (p) => el('Box', { size: p.size, transform: [tTranslate(p.at)] }));
const Floor = Block('Floor');
const Carpet = Block('Carpet');
const Glass = Block('Glass');
const Tile = Block('Tile');
const Fixture = Block('Fixture');
const Timber = Block('Timber');
const Linen = Block('Linen');
const Blanket = Block('Blanket');
const Seat = Block('Seat');
const Steel = Block('Steel');
// The leaf turns about its hinge edge and swings into the room.
const Leaf = family<{ height: number; at: Vec3; open: number }>('Leaf', (p) =>
el('Box', {
size: [LEAF.width, LEAF.thickness, p.height],
transform: [tRotate(p.open, { at: [0, LEAF.thickness, 0] }), tTranslate(p.at)],
})
);
// A window is a hole with a frame and a pane. The plan cut can go through it.
const Frame = family<{ width: number; height: number; depth: number; top: boolean }>('Frame', (p) => {
const outer = csg.translate(csg.box(p.width, p.depth, p.height), [0, 60, 0]);
const inner = csg.translate(csg.box(p.width - 2 * FRAME, p.depth, p.height - (p.top ? 2 : 1) * FRAME + (p.top ? 0 : 1)), [FRAME, 60, FRAME]);
return el('Geometry', { node: csg.cut(outer, inner) });
});
const Window = family<{ along: number; depth: number; cut: number }>(
'Window',
(p) => {
const seen = Math.min(WINDOW.height, p.cut - WINDOW.sill); // the part below the plan cut
const parts =
seen > FRAME
? [
Frame({ key: 'frame', width: WINDOW.width, height: seen, depth: 80, top: seen === WINDOW.height }),
Glass({ key: 'glass', size: [WINDOW.width - 2 * FRAME, 20, seen - (seen === WINDOW.height ? 2 : 1) * FRAME], at: [FRAME, 90, FRAME] }),
]
: [];
return el('Box', { size: [WINDOW.width, p.depth, WINDOW.height], transform: [tTranslate([p.along, 0, WINDOW.sill])] }, parts);
},
{ role: 'fill' }
);
// Bathroom: two thin walls in the corner of the room, a door, and the fixtures.
const Bath = family<{ at: Vec3; cut: number }>('Bath', (p) => {
const b = BATH;
const h = Math.min(ROOM.height, p.cut);
const clip = (z: number): number => Math.min(z, p.cut);
const inner = b.width - b.wall;
return el('Group', { transform: [tTranslate(p.at)] }, [
Wall({ key: 'west', length: b.depth, height: h, thickness: b.wall, at: [b.wall, 0, 0], turn: 90, voids: [Door({ key: 'door', along: 700, depth: b.wall, cut: p.cut, open: 75 })] }),
Wall({ key: 'north', length: b.width, height: h, thickness: b.wall, at: [0, b.depth, 0], turn: 0 }),
Tile({ key: 'tiles', size: [inner, b.depth, 15], at: [b.wall, 0, 0] }),
Fixture({ key: 'tray', size: [900, 900, 60], at: [b.width - 900, 0, 15] }),
Glass({ key: 'screen', size: [10, 900, clip(2000) - 15], at: [b.width - 910, 0, 15] }),
Fixture({ key: 'pan', size: [380, 560, 400], at: [b.width - 380 - 200, b.depth - 560, 15] }),
Fixture({ key: 'cistern', size: [380, 180, 380], at: [b.width - 380 - 200, b.depth - 180, 415] }),
Fixture({ key: 'basin', size: [520, 400, 160], at: [b.wall + 80, b.depth - 400, 700] }),
Timber({ key: 'vanity', size: [520, 380, 685], at: [b.wall + 80, b.depth - 380, 15] }),
]);
});
// Furniture in room coordinates. It follows the width of the room.
function furniture(w: number, cut: number): Element[] {
const t = ROOM.wall;
const d = ROOM.depth;
const wide = w >= 4000;
const bed = wide ? MATTRESS.large : MATTRESS.single;
const y1 = 4100; // far side of the bed
const y0 = y1 - bed;
const head = t + 50;
const pillows = wide ? [y0 + 150, y1 - 650] : [y0 + 200];
const table = (key: string, y: number): Element => Timber({ key, size: [450, 400, 500], at: [t, y, 0] });
const dx = w - t - 1300; // desk
const leg = (key: string, x: number, y: number, h: number): Element => Steel({ key, size: [40, 40, h], at: [x, y, 0] });
const cx = dx + 375; // chair
const cy = d - t - 600 - 520;
return [
Timber({ key: 'headboard', size: [50, bed, 1000], at: [t, y0, 0] }),
Timber({ key: 'bed-base', size: [MATTRESS.length, bed, 300], at: [head, y0, 0] }),
Linen({ key: 'mattress', size: [MATTRESS.length, bed, 220], at: [head, y0, 300] }),
Blanket({ key: 'blanket', size: [1250, bed, 30], at: [head + MATTRESS.length - 1250, y0, 520] }),
...pillows.map((y, i) => Fixture({ key: 'pillow-' + i, size: [380, 500, 110], at: [head + 80, y, 520] })),
table('table-north', y1),
...(wide ? [table('table-south', y0 - 400)] : []),
Timber({ key: 'desk-top', size: [1200, 600, 30], at: [dx, d - t - 600, 710] }),
leg('desk-leg-0', dx, d - t - 600, 710),
leg('desk-leg-1', dx + 1160, d - t - 600, 710),
leg('desk-leg-2', dx, d - t - 40, 710),
leg('desk-leg-3', dx + 1160, d - t - 40, 710),
Seat({ key: 'chair-seat', size: [450, 450, 50], at: [cx, cy, 410] }),
Seat({ key: 'chair-back', size: [450, 40, 420], at: [cx, cy, 460] }),
leg('chair-leg-0', cx, cy, 410),
leg('chair-leg-1', cx + 410, cy, 410),
leg('chair-leg-2', cx, cy + 410, 410),
leg('chair-leg-3', cx + 410, cy + 410, 410),
Timber({ key: 'wardrobe', size: [1000, 600, Math.min(2000, cut)], at: [w - t - 1000, t + BATH.depth + BATH.wall, 0] }),
];
}
export default suite();Functions used
compound csg unwrap color family el resolve evaluateModel tRotate tTranslate