Roof Gallery

Six roofs of the brepjs-bim roof builder on six small pavilions: gable, hip, pyramid, flat, shed and dome. Each one is a real IfcRoof solid that one addRoof call shapes from its type and its pitch, and the IFC export writes each body. The 35 degree pitch is the published minimum for clay plain tiles. The kernel measures the tiled area of each roof: the gable and the hip have the same area. The pavilions are a display model.
Code
This is the full program. The playground runs it in your browser without a change.
ts
import { BimModel, placedSolids, toIfc } from 'brepjs-bim';
import { box, compound, faceCenter, getBounds, getFaces, measureArea, normalAt, translate, unwrap } from 'brepjs/quick';
import { color, present } from 'brepjs/playground';
// Roof gallery: the roof shapes of the brepjs-bim roof builder, each one a real IfcRoof on a small pavilion.
// The lesson is in CATALOGUE and in roofOf(): one addRoof call, and predefinedType with pitch selects the shape.
// The pavilions are a display model. Units are millimetres, Z is up, and the fronts look toward +X.
interface GalleryParams {
pitch?: number; // degrees, for the gable, the hip and the pyramid
eaves?: number; // how far each roof goes past its walls
lift?: number; // raise each roof off its walls to show it as one solid
}
type P3 = [number, number, number];
type Id = Extract<ReturnType<BimModel['addRoof']>, { ok: true }>['value'];
type Body = ReturnType<typeof box>;
const X: P3 = [1, 0, 0], Y: P3 = [0, 1, 0], Z: P3 = [0, 0, 1];
// ---- published figures
const TILE_PITCH = 35; // degrees: the smallest pitch for clay plain tiles
// ---- display model
const SHED_PITCH = 15;
const DOOR = { width: 900, height: 2100 };
const WALL = { height: 2700, thickness: 300 };
const FLOOR = { thickness: 200, edge: 150 };
const COURT = 1800; // paving past the pavilions
const GRID = 9300; // from one pavilion to the next
// One entry for each roof: its type, its pitch, the plan of its walls, and its place in the grid.
const CATALOGUE = [
{ name: 'Gable', kind: 'GABLE_ROOF', pitch: TILE_PITCH, plan: [4800, 6600], at: [0, 0] },
{ name: 'Hip', kind: 'HIP_ROOF', pitch: TILE_PITCH, plan: [4800, 6600], at: [0, 1] },
{ name: 'Pyramid', kind: 'HIP_ROOF', pitch: TILE_PITCH, plan: [5400, 5400], at: [0, 2] }, // a hip on a square plan
{ name: 'Flat', kind: 'FLAT_ROOF', pitch: undefined, plan: [4800, 6600], at: [1, 0] },
{ name: 'Shed', kind: 'SHED_ROOF', pitch: SHED_PITCH, plan: [4800, 6600], at: [1, 1] },
{ name: 'Dome', kind: 'DOME_ROOF', pitch: TILE_PITCH, plan: [5400, 5400], at: [1, 2] }, // the dome ignores the pitch
] as const;
type Entry = (typeof CATALOGUE)[number];
function gallery({ pitch = TILE_PITCH, eaves = 450, lift = 0 }: GalleryParams = {}) {
const model = new BimModel();
model.init({ name: 'Roof gallery' });
const site = unwrap(model.addSite({ name: 'Site' }));
const building = unwrap(model.addBuilding({ name: 'Pavilions' }));
const storey = unwrap(model.addStorey({ name: 'Ground floor', elevation: 0 }));
const project = model.getProject();
if (project) model.aggregate(project.localId, site);
model.aggregate(site, building);
model.aggregate(building, storey);
const put = (made: ReturnType<BimModel['addRoof']>): Id => {
const id = unwrap(made);
model.placeIn(id, storey);
return id;
};
// THE LESSON. A roof is a plan of length by width, a thickness, a type and a pitch. The ridge runs along the
// local X axis. With axisX along +Y the width goes toward -X, so the origin is the front corner of the eaves.
const roofOf = (e: Entry, x0: number, y0: number): Id => {
const [a, b] = e.plan;
const dome = e.kind === 'DOME_ROOF';
const out = dome ? -300 : eaves; // the dome stands inside the edge of its flat deck
const slope = e.pitch === undefined ? undefined : e.kind === 'SHED_ROOF' || dome ? e.pitch : pitch;
return put(model.addRoof({
length: b + 2 * out, width: a + 2 * out, thickness: e.kind === 'FLAT_ROOF' ? 250 : 120,
origin: [x0 + a + out, y0 - out, WALL.height + lift + (dome ? 200 : 0)], axisX: Y, axisZ: Z,
predefinedType: e.kind, pitch: slope, materialName: e.kind === 'FLAT_ROOF' ? 'Membrane' : 'Clay tile',
}));
};
const roofs = CATALOGUE.map((e) => roofOf(e, e.at[0] * GRID, e.at[1] * GRID));
// The scenery: a pavilion under each roof, on one paved court.
const owned: Body[] = [];
const fills: { glass: Id[]; door: Id[]; sill: Id[]; step: Id[] } = { glass: [], door: [], sill: [], step: [] };
const block = (name: string, lo: P3, hi: P3, material: string): Id => {
const unit = box(hi[0] - lo[0], hi[1] - lo[1], hi[2] - lo[2]);
owned.push(unit);
return put(model.addProxy({ name, solid: translate(unit, lo), materialName: material }));
};
for (const e of CATALOGUE) {
const [a, b] = e.plan, t = WALL.thickness, x0 = e.at[0] * GRID, y0 = e.at[1] * GRID;
const wall = (length: number, origin: P3, axisX: P3): Id =>
put(model.addWall({ length, height: WALL.height, thickness: t, origin, axisX, axisZ: Z, materialName: 'Rendered masonry', isExternal: true }));
const front = wall(b, [x0 + a, y0, 0], Y); // the thickness goes toward -X
wall(b, [x0, y0 + b, 0], [0, -1, 0]);
const south = wall(a - 2 * t, [x0 + t, y0, 0], X);
const north = wall(a - 2 * t, [x0 + a - t, y0 + b, 0], [-1, 0, 0]);
put(model.addSlab({ length: a + 2 * FLOOR.edge, width: b + 2 * FLOOR.edge, thickness: FLOOR.thickness, origin: [x0 - FLOOR.edge, y0 - FLOOR.edge, -FLOOR.thickness], axisX: X, axisZ: Z, predefinedType: 'BASESLAB', materialName: 'Concrete' }));
if (e.kind === 'DOME_ROOF') put(model.addSlab({ length: a + 2 * eaves, width: b + 2 * eaves, thickness: 200, origin: [x0 - eaves, y0 - eaves, WALL.height], axisX: X, axisZ: Z, predefinedType: 'ROOF', materialName: 'Concrete' }));
// The door and a window in the front, and a window in each side. Each opening is a void in its wall.
const door = { ...DOOR, offsetAlongWall: 900, offsetFromFloor: 0 };
const pane = { width: 1500, height: 1200, offsetAlongWall: b - 2700, offsetFromFloor: 900 };
const side = { ...pane, offsetAlongWall: (a - 2 * t - pane.width) / 2 };
put(model.addDoor({ ...door, wallLocalId: front, materialName: 'Timber' }));
put(model.addWindow({ ...pane, wallLocalId: front, materialName: 'Aluminium' }));
put(model.addWindow({ ...side, wallLocalId: south, materialName: 'Aluminium' }));
put(model.addWindow({ ...side, wallLocalId: north, materialName: 'Aluminium' }));
// A leaf, a pane of glass and a sill are proxies in the outer part of each opening. The IFC writer gives
// each door and window a panel in the inner 100 mm of its wall, so the proxies stay out of that part.
const fx = x0 + a, sx = x0 + t + side.offsetAlongWall, top = pane.offsetFromFloor + pane.height;
fills.door.push(block('Door leaf', [fx - 140, y0 + door.offsetAlongWall, 0], [fx - 90, y0 + door.offsetAlongWall + door.width, door.height], 'Timber'));
fills.step.push(block('Step', [fx + FLOOR.edge, y0 + door.offsetAlongWall - 200, -FLOOR.thickness], [fx + FLOOR.edge + 500, y0 + door.offsetAlongWall + door.width + 200, -40], 'Stone'));
fills.glass.push(block('Glazing', [fx - 160, y0 + pane.offsetAlongWall, 950], [fx - 130, y0 + pane.offsetAlongWall + pane.width, top], 'Glass'));
fills.sill.push(block('Sill', [fx - t + 100, y0 + pane.offsetAlongWall, 900], [fx + 60, y0 + pane.offsetAlongWall + pane.width, 950], 'Stone'));
for (const [glassAt, sillAt] of [[y0 + 130, y0 - 60], [y0 + b - 160, y0 + b - t + 100]] as const) {
fills.glass.push(block('Glazing', [sx, glassAt, 950], [sx + side.width, glassAt + 30, top], 'Glass'));
fills.sill.push(block('Sill', [sx, sillAt, 900], [sx + side.width, sillAt + t - 40, 950], 'Stone'));
}
}
const court = put(model.addSlab({ length: GRID + 5400 + 2 * COURT, width: 2 * GRID + 6600 + 2 * COURT, thickness: 150, origin: [-COURT, -COURT, -FLOOR.thickness - 150], axisX: X, axisZ: Z, predefinedType: 'BASESLAB', materialName: 'Paving' }));
// The kernel measures the tiled area of each roof: every face that is not vertical and is not the underside.
const elements = new Map(model.getAllElements().map((el) => [el.localId, el]));
const solidsOf = (ids: readonly Id[]): Body[] => {
const list = ids.flatMap((id) => unwrap(placedSolids(elements.get(id) as never)) as Body[]);
owned.push(...list);
return list;
};
const tiled = solidsOf(roofs).map((roof) => {
const base = getBounds(roof).zMin;
const faces = getFaces(roof).filter((f) => Math.abs(normalAt(f)[2]) > 0.05 && faceCenter(f)[2] > base + 1);
return faces.reduce((sum, f) => sum + unwrap(measureArea(f)), 0) / 1e6;
});
const shown = {
roofs: compound(solidsOf(roofs)),
walls: compound(solidsOf(model.getWalls().map((w) => w.localId))),
floors: compound(solidsOf(model.getSlabs().map((s) => s.localId).filter((id) => id !== court))),
court: compound(solidsOf([court])),
doors: compound(solidsOf(fills.door)),
glass: compound(solidsOf(fills.glass)),
sills: compound(solidsOf([...fills.sill, ...fills.step])),
};
owned.splice(0).forEach((s) => s.delete());
return { model, shown, tiled, pitch };
}
// Try gallery({ pitch: 50 }), gallery({ eaves: 900 }) or gallery({ lift: 1500 }).
const g = gallery();
const area = (name: string): string => (g.tiled[CATALOGUE.findIndex((e) => e.name === name)] as number).toFixed(1);
console.log('Roof area from the kernel, in m2: gable ' + area('Gable') + ', hip ' + area('Hip') + ' (the same plan and pitch give the same area: plan / cos ' + g.pitch + '), pyramid ' + area('Pyramid') + ', flat ' + area('Flat') + ', shed ' + area('Shed') + ', dome ' + area('Dome'));
export default present(
[
color(g.shown.roofs, '#12b5b0'),
color(g.shown.walls, '#f5f2ea'),
color(g.shown.floors, '#c3c9d1'),
color(g.shown.court, '#48515c'),
color(g.shown.doors, '#ff5a1f'),
color(g.shown.glass, '#9fc3d4'),
color(g.shown.sills, '#c3c9d1'),
],
{
bimTree: g.model.toTreeSummary(),
ifc: async () => unwrap(await toIfc(g.model, { applicationName: 'brepjs playground', applicationVersion: '1.0' })),
},
);Functions used
placedSolids toIfc box compound faceCenter getBounds getFaces measureArea normalAt translate unwrap color present