first push
This commit is contained in:
commit
2a81b4a502
845 files changed
+356635
No files matched your search
@@ -0,0 +1,54 @@
|
||||
// The blind of a window: a roller shutter outside (a box above the opening and slats down to the closed fraction), a venetian blind
|
||||
// outside whose slats turn with the cover's tilt, or a roller blind inside the room. A wide window can have a second blind for its
|
||||
// right half.
|
||||
|
||||
import { Color } from "three";
|
||||
import { ALWAYS } from "../geo.ts";
|
||||
import type { Ctx } from "./context.ts";
|
||||
import { BLIND_BOX, FRAME_TOP } from "./palette.ts";
|
||||
import { panel, splitBox } from "./primitives.ts";
|
||||
|
||||
/** Colours of the kinds of blinds: the white shutter, the aluminium of a venetian blind, the light fabric of an inside blind. */
|
||||
const SHUTTER = 0xffffff;
|
||||
const VENETIAN = 0xc9ced6;
|
||||
const FABRIC = 0xefe7d8;
|
||||
/** Spacing and depth of venetian slats (m). */
|
||||
const SLAT_STEP = 0.08;
|
||||
const SLAT_DEPTH = 0.085;
|
||||
|
||||
export function blind(c: Ctx): void {
|
||||
const { info, state, frame, fold } = c;
|
||||
if (state.cover === null) return;
|
||||
const [W, S, T] = [info.width, info.sill, info.top];
|
||||
const kind = info.opening.blind ?? "roller";
|
||||
const outside = -info.faceOut;
|
||||
const inside = info.faceRoom;
|
||||
// the cassette above the opening: a big box outside, a slim one on the room side for an inside blind
|
||||
if (kind === "inside") splitBox(c.frames, frame, { x0: -0.03, x1: W + 0.03, n0: inside, n1: inside + 0.07, y0: T - 0.02, y1: T + 0.07 }, new Color(0xf2f2f2), new Color(0xf7f7f7), fold);
|
||||
else splitBox(c.frames, frame, { x0: -0.05, x1: W + 0.05, n0: outside - 0.15, n1: outside, y0: T, y1: T + 0.2 }, new Color(BLIND_BOX), new Color(FRAME_TOP), fold);
|
||||
const halves: [number, number, number][] = state.cover2 != null ? [[0, W / 2, state.cover], [W / 2, W, state.cover2]] : [[0, W, state.cover]];
|
||||
for (const [x0, x1, cover] of halves) {
|
||||
const closed = Math.min(1, Math.max(0, cover));
|
||||
if (closed <= 0.01) continue;
|
||||
const bottom = T - closed * (T - S);
|
||||
if (kind === "inside") panel(c.blinds, frame, x0 + 0.01, x1 - 0.01, inside + 0.04, bottom, T - 0.02, new Color(FABRIC), fold);
|
||||
else if (kind === "venetian") slats(c, x0, x1, outside - 0.07, bottom, T, state.slats ?? 0);
|
||||
else panel(c.blinds, frame, x0, x1, outside - 0.07, bottom, T, new Color(SHUTTER), fold, 0.045);
|
||||
}
|
||||
}
|
||||
|
||||
/** Venetian slats from the top down to `bottom`, turned by how far they are open: level (1) shows the gaps, shut (0) a closed face. */
|
||||
function slats(c: Ctx, x0: number, x1: number, n: number, bottom: number, top: number, open: number): void {
|
||||
const { frame, fold } = c;
|
||||
const color = new Color(VENETIAN);
|
||||
// the slat's angle from upright: shut slats hang almost upright and overlap, open ones lie level
|
||||
const a = (Math.PI / 2) * (0.12 + 0.88 * Math.min(1, Math.max(0, open)));
|
||||
const dn = (Math.sin(a) * SLAT_DEPTH) / 2;
|
||||
const dy = (Math.cos(a) * SLAT_DEPTH) / 2;
|
||||
for (let y = top - SLAT_STEP / 2; y > bottom; y -= SLAT_STEP) {
|
||||
const bucket = y >= fold.cut ? fold.bucket : ALWAYS;
|
||||
const [p, q, r, s] = [frame(x0, n - dn, y + dy), frame(x1, n - dn, y + dy), frame(x1, n + dn, y - dy), frame(x0, n + dn, y - dy)];
|
||||
c.blinds.tri(p, q, r, color, color, color, [0, 0.5, 1, 0.5, 1, 0.5], bucket);
|
||||
c.blinds.tri(p, r, s, color, color, color, [0, 0.5, 1, 0.5, 0, 0.5], bucket);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import type { OpeningInfo } from "../build.ts";
|
||||
import type { GeoBuffer } from "../geo.ts";
|
||||
import type { Fold, Frame } from "./primitives.ts";
|
||||
import type { OpeningState } from "./types.ts";
|
||||
|
||||
/** Everything the builders of one opening need. */
|
||||
export interface Ctx {
|
||||
info: OpeningInfo;
|
||||
state: OpeningState;
|
||||
frame: Frame;
|
||||
/** The middle between the room face and the outer face of the wall (in n). */
|
||||
mid: number;
|
||||
/** Highlighted while closed (and a sensor knows), instead of while open. */
|
||||
markClosed: boolean;
|
||||
fold: Fold;
|
||||
frames: GeoBuffer;
|
||||
glass: GeoBuffer;
|
||||
blinds: GeoBuffer;
|
||||
/** For a piece of a pane over several edges: whether it is the first or the last one, which carry the jambs. */
|
||||
first: boolean;
|
||||
last: boolean;
|
||||
/**
|
||||
* How far the glass runs past each end of the piece, in metres, to meet the next piece of the pane:
|
||||
* `[at its start, at its end]`. The glass sits in the middle of the wall, and two walls at an angle have
|
||||
* two different middles, so the ends would otherwise stand a few centimetres apart and leave a slot.
|
||||
*/
|
||||
over: [number, number];
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
// Doors and garage doors: the door frame, leaves that turn on their hinges or slide, sidelights, handles, sectional garage doors.
|
||||
|
||||
import { Color } from "three";
|
||||
import { isFrontDoor, openingStyle, sidelightLayout } from "../../model.ts";
|
||||
import { ALWAYS, DEG, shade } from "../geo.ts";
|
||||
import type { Ctx } from "./context.ts";
|
||||
import { DOOR_ANGLE, FRAME, FRAME_TOP, GLASS, HANDLE, HANDLE_TOP, LEAF, LEAF_FRONT, LEAF_FRONT_TOP, LEAF_TOP, OPEN_WARM, SILL, UNLOCKED } from "./palette.ts";
|
||||
import { flatPanel, panel, splitBox, type Frame } from "./primitives.ts";
|
||||
|
||||
/** The frame (Zarge) around a door or garage opening, covering the reveal on both faces. An open garage door glows warm. */
|
||||
export function doorFrame(c: Ctx): void {
|
||||
const { info, state, frame, fold } = c;
|
||||
// B: the floor the door stands on – below 0 in a room set lower than its floor (floor_offset)
|
||||
const [W, T, B] = [info.width, info.top, Math.min(0, info.sill)];
|
||||
const garageOpen = info.opening.type === "garage" && (c.markClosed ? !!state.sensed && (state.cover ?? 1) >= 0.95 : (state.cover ?? 1) < 0.95);
|
||||
const side = garageOpen ? shade(OPEN_WARM, 0.8) : new Color(FRAME);
|
||||
const top = garageOpen ? shade(OPEN_WARM, 1) : new Color(FRAME_TOP);
|
||||
const [n0, n1] = [-info.faceOut - 0.012, info.faceRoom + 0.012];
|
||||
const piece = (x0: number, x1: number, y0: number) => splitBox(c.frames, frame, { x0, x1, n0, n1, y0, y1: T + 0.045 }, side, top, fold);
|
||||
piece(-0.045, 0.02, B);
|
||||
piece(W - 0.02, W + 0.045, B);
|
||||
piece(0.02, W - 0.02, T - 0.02);
|
||||
}
|
||||
|
||||
/** A door: one leaf or two, each turning around its hinge (or sliding), with sidelights, a threshold and handles. */
|
||||
export function doorLeaves(c: Ctx): void {
|
||||
const { info, state, frame, fold, mid } = c;
|
||||
const [W, T, B] = [info.width, info.top, Math.min(0, info.sill)];
|
||||
const H = T - B;
|
||||
const style = openingStyle(info.opening, info.exterior);
|
||||
const passage = style === "passage";
|
||||
const front = isFrontDoor(style);
|
||||
const swing = info.opening.swing === "out" ? -1 : 1;
|
||||
// the leaves are flush with the face they swing towards (the room, or the other side)
|
||||
const face = swing > 0 ? info.faceRoom : -info.faceOut;
|
||||
const double = info.opening.leaves === 2;
|
||||
const frameColor = new Color(FRAME);
|
||||
const frameTop = new Color(FRAME_TOP);
|
||||
|
||||
// fixed glass beside the leaf: one sidelight opposite the hinge, or one on each side
|
||||
let x0 = 0.02;
|
||||
let x1 = W - 0.02;
|
||||
const lights = sidelightLayout(W, style, info.hingeAtStart, info.opening);
|
||||
if (lights) {
|
||||
for (const [a, b] of lights.panels) {
|
||||
const post = (xa: number, xb: number, ya: number, yb: number) => splitBox(c.frames, frame, { x0: xa, x1: xb, n0: mid - 0.03, n1: mid + 0.03, y0: ya, y1: yb }, frameColor, frameTop, fold);
|
||||
post(a, a + 0.04, B + 0.02, T - 0.02);
|
||||
post(b - 0.04, b, B + 0.02, T - 0.02);
|
||||
post(a, b, B + 0.02, B + 0.1);
|
||||
panel(c.glass, frame, a + 0.04, b - 0.04, mid, B + 0.1, T - 0.02, GLASS, fold);
|
||||
}
|
||||
x0 = lights.x0;
|
||||
x1 = lights.x1;
|
||||
}
|
||||
const leafWidth = double ? (x1 - x0) / 2 - 0.004 : x1 - x0;
|
||||
const thickness = front ? 0.06 : 0.04;
|
||||
if (front) {
|
||||
// a threshold across the wall, and a small light over the door outside
|
||||
splitBox(c.frames, frame, { x0: 0.02, x1: W - 0.02, n0: -info.faceOut - 0.02, n1: info.faceRoom, y0: B, y1: B + 0.02 }, new Color(SILL), frameTop, fold);
|
||||
if (info.exterior) splitBox(c.frames, frame, { x0: W / 2 - 0.08, x1: W / 2 + 0.08, n0: -info.faceOut - 0.1, n1: -info.faceOut, y0: T + 0.1, y1: T + 0.17 }, shade(OPEN_WARM, 0.55), shade(OPEN_WARM, 0.85), { cut: fold.cut, bucket: ALWAYS });
|
||||
}
|
||||
|
||||
// the leaf's full swing: its own opening angle, else the usual one
|
||||
const swingAngle = info.opening.open_angle ? Math.min(180, Math.max(5, info.opening.open_angle)) * DEG : DOOR_ANGLE;
|
||||
const leaves: [boolean, number][] = passage ? [] : [[info.hingeAtStart, state.open]];
|
||||
if (double && !passage) leaves.push([!info.hingeAtStart, state.open2 ?? 0]);
|
||||
for (const [atStart, openness] of leaves) {
|
||||
const open = Math.min(1, Math.max(0, openness));
|
||||
const theta = style === "sliding" ? 0 : open * swingAngle;
|
||||
// a sliding leaf runs along the wall face past the hinge side instead of turning
|
||||
const slide = style === "sliding" ? open * leafWidth : 0;
|
||||
const leafFrame: Frame = (u, n, y) => {
|
||||
const along = u * Math.cos(theta) - n * Math.sin(theta) - slide;
|
||||
const across = face + swing * (n * Math.cos(theta) + u * Math.sin(theta) + (slide ? 0.05 : 0));
|
||||
return frame(atStart ? x0 + along : x1 - along, across, y);
|
||||
};
|
||||
// an open leaf has left its wall: keep it visible when the wall folds away
|
||||
const leafFold = { cut: fold.cut, bucket: open > 0.05 ? ALWAYS : fold.bucket };
|
||||
const warm = c.markClosed ? !!state.sensed && open < 0.05 : open > 0.9 && !(atStart === info.hingeAtStart && state.assumed);
|
||||
const side = warm ? shade(OPEN_WARM, 0.7) : new Color(front ? LEAF_FRONT : LEAF);
|
||||
const top = warm ? shade(OPEN_WARM, 0.9) : new Color(front ? LEAF_FRONT_TOP : LEAF_TOP);
|
||||
const part = (xa: number, xb: number, ya: number, yb: number, na = -thickness, nb = 0) => splitBox(c.frames, leafFrame, { x0: xa, x1: xb, n0: na, n1: nb, y0: ya, y1: yb }, side, top, leafFold);
|
||||
if (style === "glass") {
|
||||
// a glass door: slim stiles and rails around a pane
|
||||
part(0, 0.05, B + 0.01, T - 0.01);
|
||||
part(leafWidth - 0.05, leafWidth, B + 0.01, T - 0.01);
|
||||
part(0.05, leafWidth - 0.05, B + 0.01, B + 0.12);
|
||||
part(0.05, leafWidth - 0.05, T - 0.08, T - 0.01);
|
||||
panel(c.glass, leafFrame, 0.05, leafWidth - 0.05, -thickness / 2, B + 0.12, T - 0.08, GLASS, leafFold);
|
||||
} else part(0, leafWidth, B + 0.01, T - 0.01);
|
||||
if (style === "front_glass") panel(c.glass, leafFrame, 0.12, leafWidth - 0.12, 0.001, B + H * 0.55, T - 0.18, GLASS, leafFold);
|
||||
else if (front) panel(c.glass, leafFrame, 0.1, 0.18, 0.001, B + 0.3, T - 0.3, GLASS, leafFold);
|
||||
// a handle on both sides: a knob on a room door, a bar on a front door
|
||||
const height = B + Math.min(1.05, H * 0.5);
|
||||
const half = front ? 0.3 : 0.012;
|
||||
const [hx0, hx1] = front ? [leafWidth - 0.11, leafWidth - 0.08] : [leafWidth - 0.16, leafWidth - 0.05];
|
||||
const handle = (na: number, nb: number) => splitBox(c.frames, leafFrame, { x0: hx0, x1: hx1, n0: na, n1: nb, y0: height - half, y1: height + half }, new Color(HANDLE), new Color(HANDLE_TOP), leafFold);
|
||||
handle(0.004, 0.05);
|
||||
handle(-thickness - 0.05, -thickness - 0.004);
|
||||
// an unlocked smart lock: the leaf's free edge glows in the accent (cyan follows the user's accent colour)
|
||||
if (state.unlocked) {
|
||||
const glow = new Color(UNLOCKED);
|
||||
splitBox(c.frames, leafFrame, { x0: leafWidth - 0.012, x1: leafWidth + 0.004, n0: -thickness - 0.004, n1: 0.004, y0: B + 0.06, y1: T - 0.06 }, glow, glow, { cut: fold.cut, bucket: ALWAYS });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** A sectional garage door: the closed part hangs in the opening, the open part lies under the ceiling. */
|
||||
export function garageDoor(c: Ctx): void {
|
||||
const { info, state, frame, fold } = c;
|
||||
const [W, T, B] = [info.width, info.top, Math.min(0, info.sill)];
|
||||
const closed = Math.min(1, Math.max(0, state.cover ?? 1));
|
||||
const color = new Color(0xd4e0ff);
|
||||
const plane = info.faceRoom - 0.03;
|
||||
if (closed > 0.01) panel(c.blinds, frame, 0.02, W - 0.02, plane, B + (T - B) * (1 - closed), T, color, fold, 0.5);
|
||||
const lifted = (1 - closed) * (T - B);
|
||||
if (lifted > 0.01) flatPanel(c.blinds, frame, 0.02, W - 0.02, plane, plane + lifted, T + 0.03, color, fold.bucket, 0.5);
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
// The light well in front of a cellar window: a concrete pit outside the window from below its sill up to the ground, with a grating
|
||||
// on top at ground level. It comes by itself for an outer window whose sill lies below the ground, and can be switched off per window.
|
||||
|
||||
import { Color } from "three";
|
||||
import { ALWAYS } from "../geo.ts";
|
||||
import type { Ctx } from "./context.ts";
|
||||
import { splitBox } from "./primitives.ts";
|
||||
|
||||
const CONCRETE = 0x4b5262;
|
||||
const CONCRETE_TOP = 0x6b7386;
|
||||
const GRATING = 0x2a303c;
|
||||
const GRATING_TOP = 0x8a94a8;
|
||||
|
||||
/** Whether a window gets a light well: outside, its sill below the ground (ground is in the floor's own coordinates). */
|
||||
export function needsLightWell(ctx: Pick<Ctx, "info">, ground: number): boolean {
|
||||
const { info } = ctx;
|
||||
if (info.opening.type !== "window" || !info.exterior || info.opening.light_well === false) return false;
|
||||
return info.opening.light_well === true || info.sill < ground - 0.02;
|
||||
}
|
||||
|
||||
/** A light well outside the window: three concrete walls, a floor and a grating at ground level. */
|
||||
export function lightWell(c: Ctx, ground: number): void {
|
||||
const { info, frame } = c;
|
||||
const fold = { cut: c.fold.cut, bucket: ALWAYS };
|
||||
const [w0, w1] = [-0.12, info.width + 0.12];
|
||||
const outer = -info.faceOut;
|
||||
const depth = 0.7;
|
||||
const bottom = Math.min(info.sill - 0.25, ground - 0.3);
|
||||
const top = ground;
|
||||
const side = new Color(CONCRETE);
|
||||
const cap = new Color(CONCRETE_TOP);
|
||||
const t = 0.07;
|
||||
const wall = (x0: number, x1: number, n0: number, n1: number) => splitBox(c.frames, frame, { x0, x1, n0, n1, y0: bottom, y1: top }, side, cap, fold);
|
||||
wall(w0 - t, w0, outer - depth, outer);
|
||||
wall(w1, w1 + t, outer - depth, outer);
|
||||
wall(w0 - t, w1 + t, outer - depth - t, outer - depth);
|
||||
splitBox(c.frames, frame, { x0: w0, x1: w1, n0: outer - depth, n1: outer, y0: bottom - t, y1: bottom }, side, cap, fold);
|
||||
// the grating: bars across the pit at ground level
|
||||
const bar = (x0: number, x1: number) => splitBox(c.frames, frame, { x0, x1, n0: outer - depth, n1: outer, y0: top - 0.025, y1: top }, new Color(GRATING), new Color(GRATING_TOP), fold);
|
||||
const bars = Math.max(3, Math.round((w1 - w0) / 0.06));
|
||||
for (let i = 0; i <= bars; i++) {
|
||||
const x = w0 + ((w1 - w0) * i) / bars;
|
||||
bar(x - 0.008, x + 0.008);
|
||||
}
|
||||
splitBox(c.frames, frame, { x0: w0, x1: w1, n0: outer - depth - 0.01, n1: outer - depth + 0.02, y0: top - 0.03, y1: top }, new Color(GRATING), new Color(GRATING_TOP), fold);
|
||||
}
|
||||
@@ -0,0 +1,301 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { newFloor, type Opening } from "../../model.ts";
|
||||
import { buildFloorGeometry } from "../build.ts";
|
||||
import { buildOpeningParts, CLOSED, type OpeningState } from "../openings.ts";
|
||||
import { slidingPanes } from "./windows.ts";
|
||||
import { frontPieces } from "../../geometry/glass-front.ts";
|
||||
|
||||
const room = { id: "a", name: "a", area_id: null, points: [[0, 0], [6, 0], [6, 5], [0, 5]] as [number, number][], floor_material: "wood" as const };
|
||||
|
||||
function infosFor(opening: Partial<Opening>) {
|
||||
const full = { id: "o", room_id: "a", edge: 0, offset: 1.5, width: 1.2, type: "window", sill: 0.9, height: 1.3, hinge: "left", leaves: 1, swing: "in", cover: null, contact: null, contact2: null, tilt: null, ...opening } as Opening;
|
||||
return buildFloorGeometry({ ...newFloor("f", "F", 0), rooms: [room], openings: [full] }, 0.3, 0.1).openings;
|
||||
}
|
||||
|
||||
const triangles = (g: { getAttribute(n: string): { count: number } | undefined }) => (g.getAttribute("position")?.count ?? 0) / 3;
|
||||
const build = (opening: Partial<Opening>, state: Partial<OpeningState> = {}, cut = 1.2) => buildOpeningParts(infosFor(opening), new Map([["o", { ...CLOSED, ...state }]]), cut);
|
||||
const positions = (g: { getAttribute(n: string): { array: ArrayLike<number> } | undefined }) => Array.from(g.getAttribute("position")?.array ?? []);
|
||||
|
||||
test("a window has frame parts and glass, and no blind without a cover", () => {
|
||||
const parts = build({});
|
||||
assert.ok(triangles(parts.frames) > 20);
|
||||
assert.ok(triangles(parts.glass) >= 2);
|
||||
assert.equal(triangles(parts.blinds), 0);
|
||||
});
|
||||
|
||||
test("a blind adds a box above the window and slats down to the closed fraction", () => {
|
||||
const without = build({});
|
||||
const up = build({}, { cover: 0 });
|
||||
const half = build({}, { cover: 0.5 });
|
||||
assert.ok(triangles(up.frames) > triangles(without.frames), "the blind box");
|
||||
assert.equal(triangles(up.blinds), 0, "raised: no slats");
|
||||
assert.ok(triangles(half.blinds) > 0);
|
||||
const lowest = (parts: ReturnType<typeof build>) => Math.min(...positions(parts.blinds).filter((_, i) => i % 3 === 1));
|
||||
assert.ok(lowest(build({}, { cover: 1 })) < lowest(half), "further down when more closed");
|
||||
});
|
||||
|
||||
test("an open window swings its sash into the room, and a tilted one leans", () => {
|
||||
const closed = positions(build({}).frames);
|
||||
const open = positions(build({}, { open: 1 }).frames);
|
||||
const tilted = positions(build({}, { tilt: 1 }).frames);
|
||||
assert.equal(closed.length, open.length);
|
||||
assert.notDeepEqual(closed, open);
|
||||
assert.notDeepEqual(closed, tilted);
|
||||
// the swung-open sash reaches into the room: further from the wall than any part of the closed window
|
||||
const depth = (p: number[]) => Math.max(...p.filter((_, i) => i % 3 === 2));
|
||||
assert.ok(depth(open) > depth(closed));
|
||||
});
|
||||
|
||||
test("a double window has two sashes that move on their own", () => {
|
||||
const single = triangles(build({ leaves: 1 }).frames);
|
||||
const double = triangles(build({ leaves: 2 }).frames);
|
||||
assert.ok(double > single);
|
||||
assert.notDeepEqual(positions(build({ leaves: 2 }, { open: 1, open2: 0 }).frames), positions(build({ leaves: 2 }, { open: 0, open2: 1 }).frames));
|
||||
});
|
||||
|
||||
test("glazing bars add to the sash, a glass wall has mullions instead of sashes", () => {
|
||||
assert.ok(triangles(build({ style: "bars" }).frames) > triangles(build({}).frames));
|
||||
const wall = build({ style: "glass_wall", width: 2.7, sill: 0, height: 2.4 });
|
||||
assert.ok(triangles(wall.glass) >= 3 * 2, "three panes");
|
||||
});
|
||||
|
||||
test("a door has a frame, a leaf and handles; a passage only a gap", () => {
|
||||
const door = build({ type: "door", width: 0.9, sill: 0, height: 2.05 });
|
||||
assert.ok(triangles(door.frames) > 50);
|
||||
const passage = build({ type: "door", style: "passage", width: 0.9, sill: 0, height: 2.05 });
|
||||
assert.equal(triangles(passage.frames), 0);
|
||||
assert.equal(triangles(passage.glass), 0);
|
||||
});
|
||||
|
||||
test("a door leaf swings around its hinge, a sliding one slides along the wall", () => {
|
||||
const door = (state: Partial<OpeningState>, extra: Partial<Opening> = {}) => positions(build({ type: "door", width: 0.9, sill: 0, height: 2.05, ...extra }, state).frames);
|
||||
assert.notDeepEqual(door({ open: 0 }), door({ open: 1 }));
|
||||
const closed = door({ open: 0 }, { style: "sliding" });
|
||||
const slid = door({ open: 1 }, { style: "sliding" });
|
||||
const depth = (p: number[]) => Math.max(...p.filter((_, i) => i % 3 === 2)) - Math.min(...p.filter((_, i) => i % 3 === 2));
|
||||
assert.ok(Math.abs(depth(closed) - depth(slid)) < 0.1, "a sliding leaf stays in the wall's depth");
|
||||
});
|
||||
|
||||
test("front doors and glass doors have glass; room doors have none", () => {
|
||||
const glassOf = (style: string) => triangles(build({ type: "door", style: style as never, width: 0.9, sill: 0, height: 2.05 }).glass);
|
||||
assert.equal(glassOf("interior"), 0);
|
||||
assert.ok(glassOf("front") > 0);
|
||||
assert.ok(glassOf("front_glass") >= glassOf("front"));
|
||||
assert.ok(glassOf("glass") > 0);
|
||||
assert.ok(glassOf("sidelight") > 0, "a fixed pane beside the leaf");
|
||||
});
|
||||
|
||||
test("a garage door shows panels while closed and lifts them under the ceiling when open", () => {
|
||||
const garage = (cover: number) => build({ type: "garage", width: 2.5, sill: 0, height: 2.1 }, { cover });
|
||||
assert.ok(triangles(garage(1).blinds) > 0);
|
||||
assert.ok(triangles(garage(0).blinds) > 0, "the lifted part lies flat under the ceiling");
|
||||
assert.ok(triangles(garage(1).frames) > 0);
|
||||
});
|
||||
|
||||
test("the triangle ranges say which opening each part belongs to", () => {
|
||||
const infos = infosFor({});
|
||||
const parts = buildOpeningParts(infos, new Map(), 1.2);
|
||||
assert.deepEqual(parts.frameTris.map((r) => r.id), ["o"]);
|
||||
assert.equal(parts.frameTris[0].start, 0);
|
||||
assert.equal(parts.frameTris[0].end, triangles(parts.frames));
|
||||
assert.equal(parts.glassTris[0].end, triangles(parts.glass));
|
||||
});
|
||||
|
||||
test("parts above the cut height carry the wall's fold bucket, the rest are always shown", () => {
|
||||
const folds = (cut: number) => Array.from(build({}, {}, cut).frames.getAttribute("fold")!.array);
|
||||
assert.ok(new Set(folds(10)).size === 1, "everything below the cut: always shown");
|
||||
assert.ok(new Set(folds(0.1)).size >= 1);
|
||||
assert.ok(folds(1.2).includes(-1));
|
||||
assert.ok(folds(1.2).some((f) => f !== -1), "the part above the cut folds with the wall");
|
||||
});
|
||||
|
||||
test("an open window glows warm; with 'mark closed' the closed one does", () => {
|
||||
const colorSum = (parts: ReturnType<typeof build>) => Array.from(parts.frames.getAttribute("color")!.array).reduce((a, b) => a + b, 0);
|
||||
assert.ok(colorSum(build({}, { open: 1 })) > colorSum(build({})));
|
||||
const marked = (open: number) => colorSum(build({ mark: "closed" } as never, { open, sensed: true }));
|
||||
assert.ok(marked(0) > marked(1));
|
||||
});
|
||||
|
||||
test("a sliding window: the pane at the hinge side slides in front of the fixed one; four panes with two leaves", () => {
|
||||
const one = slidingPanes(2.4, 0.06, true, 1, 1, 0);
|
||||
assert.equal(one.length, 2);
|
||||
assert.ok(one[0].slide! > 1, "the moving pane runs about half the width");
|
||||
assert.equal(one[1].slide, 0);
|
||||
assert.ok(one[0].x1 > one[1].x0, "the panes overlap in the middle");
|
||||
const four = slidingPanes(3.2, 0.06, true, 2, 1, 1);
|
||||
assert.deepEqual(four.map((p) => Math.sign(p.slide!)), [0, -1, 1, 0]);
|
||||
const mirrored = slidingPanes(2.4, 0.06, false, 1, 1, 0);
|
||||
assert.ok(mirrored[1].slide! < 0 && mirrored[0].slide === 0);
|
||||
});
|
||||
|
||||
test("an open sliding door stays in the wall and moves along it; a top-hung window swings out at the bottom", () => {
|
||||
const door = { opens: "slide" as const, sill: 0, height: 2.15, width: 2.4 };
|
||||
const closed = positions(build(door).frames);
|
||||
const open = positions(build(door, { open: 1 }).frames);
|
||||
const depth = (p: number[]) => Math.max(...p.filter((_, i) => i % 3 === 2));
|
||||
assert.notDeepEqual(closed, open);
|
||||
assert.ok(depth(open) <= depth(closed) + 1e-6, "no part reaches into the room");
|
||||
const top = { opens: "top" as const };
|
||||
const out = (p: number[]) => Math.min(...p.filter((_, i) => i % 3 === 2));
|
||||
assert.ok(out(positions(build(top, { open: 1 }).frames)) < out(positions(build(top).frames)) - 0.1, "the bottom leaves the wall towards outside");
|
||||
// a tilt sensor opens a top-hung window as well
|
||||
assert.notDeepEqual(positions(build(top, { tilt: 1 }).frames), positions(build(top).frames));
|
||||
});
|
||||
|
||||
test("a door in a lowered room stands on that room's floor, not on the floor level", () => {
|
||||
const lowered = { ...room, floor_offset: -0.39 };
|
||||
const opening = { id: "o", room_id: "a", edge: 0, offset: 1.5, width: 0.9, type: "door", sill: 0, height: 1.87, hinge: "left", leaves: 1, swing: "in", cover: null, contact: null, contact2: null, tilt: null } as Opening;
|
||||
const infos = buildFloorGeometry({ ...newFloor("f", "F", 0), rooms: [lowered], openings: [opening] }, 0.3, 0.1).openings;
|
||||
const parts = buildOpeningParts(infos, new Map([["o", CLOSED]]), 1.2);
|
||||
const ys = positions(parts.frames).filter((_, i) => i % 3 === 1);
|
||||
assert.ok(Math.abs(Math.min(...ys) - -0.39) < 0.02, `lowest part at ${Math.min(...ys)}`);
|
||||
});
|
||||
|
||||
test("a glass wall divides into about 0.9 m panes unless it is told how many", () => {
|
||||
const wall = (panes?: number | null) => build({ type: "window", sill: 0, height: 2.4, width: 3.6, style: "glass_wall", ...(panes === undefined ? {} : { panes }) });
|
||||
const auto = wall();
|
||||
const one = wall(0);
|
||||
const three = wall(3);
|
||||
// one pane: no mullion at all, so fewer frame boxes and a single sheet of glass
|
||||
assert.ok(triangles(one.frames) < triangles(auto.frames), "a single pane has no mullions");
|
||||
assert.equal(triangles(one.glass), 4, "one pane is one quad, glazed on both sides");
|
||||
// a number of its own wins over the automatic spacing
|
||||
assert.ok(triangles(three.frames) < triangles(auto.frames));
|
||||
assert.ok(triangles(three.frames) > triangles(one.frames), "three panes have two mullions");
|
||||
assert.equal(triangles(three.glass), 12, "three panes");
|
||||
// an unchanged plan keeps exactly what it drew before: the automatic spacing
|
||||
assert.equal(triangles(auto.glass), 16, "3.6 m gives four panes of about 0.88 m");
|
||||
});
|
||||
|
||||
/** A curved wall: ten edges of 1 m, each turning 10 degrees the same way, closed by one back edge. */
|
||||
function curvedRoom() {
|
||||
const points: [number, number][] = [[0, 0]];
|
||||
let [x, z, a] = [0, 0, 0];
|
||||
for (let i = 0; i < 10; i++) {
|
||||
a += (10 * Math.PI) / 180;
|
||||
x += Math.cos(a);
|
||||
z += Math.sin(a);
|
||||
points.push([x, z]);
|
||||
}
|
||||
return { ...room, points };
|
||||
}
|
||||
|
||||
/** Six pieces of one pane on that wall, in the order the plan lists them. */
|
||||
function pane(backwards = false) {
|
||||
const curved = curvedRoom();
|
||||
const pieces = frontPieces(curved, 0, 6)!;
|
||||
const list = pieces.map(
|
||||
(p, k) =>
|
||||
({
|
||||
id: `g${k}`,
|
||||
room_id: "a",
|
||||
edge: p.edge,
|
||||
offset: p.offset,
|
||||
width: p.width,
|
||||
front: "f",
|
||||
type: "window",
|
||||
sill: 0,
|
||||
height: 2.4,
|
||||
style: "glass_wall",
|
||||
hinge: "left",
|
||||
leaves: 1,
|
||||
swing: "in",
|
||||
cover: null,
|
||||
contact: null,
|
||||
contact2: null,
|
||||
tilt: null,
|
||||
}) as Opening,
|
||||
);
|
||||
// the order the openings are listed in is the order the plan happens to hold them, which on a curved
|
||||
// wall is not necessarily the order along the outline
|
||||
return buildFloorGeometry({ ...newFloor("f", "F", 0), rooms: [curved], openings: backwards ? list.reverse() : list }, 0.3, 0.1).openings;
|
||||
}
|
||||
|
||||
/** The plan points where two pieces of one pane meet, and the two outer ends of the pane. */
|
||||
function paneEnds(infos: readonly { start: [number, number]; axis: [number, number]; width: number }[]) {
|
||||
const key = (p: [number, number]) => `${Math.round(p[0] * 50)},${Math.round(p[1] * 50)}`;
|
||||
const end = (i: number): [number, number] => [infos[i].start[0] + infos[i].axis[0] * infos[i].width, infos[i].start[1] + infos[i].axis[1] * infos[i].width];
|
||||
const count = new Map<string, number>();
|
||||
const at = new Map<string, [number, number]>();
|
||||
infos.forEach((_, i) => {
|
||||
for (const p of [infos[i].start as [number, number], end(i)]) {
|
||||
count.set(key(p), (count.get(key(p)) ?? 0) + 1);
|
||||
at.set(key(p), p);
|
||||
}
|
||||
});
|
||||
const joins = [...count].filter(([, n]) => n > 1).map(([k]) => at.get(k)!);
|
||||
const outer = [...count].filter(([, n]) => n === 1).map(([k]) => at.get(k)!);
|
||||
return { joins, outer };
|
||||
}
|
||||
|
||||
/** One pane must be one sheet: no slot between its pieces and no frame standing in it. */
|
||||
function assertOneSheet(infos: ReturnType<typeof pane>) {
|
||||
const joined = buildOpeningParts(infos, new Map([["f", CLOSED]]), 1.2);
|
||||
const gp = positions(joined.glass);
|
||||
const fp = positions(joined.frames);
|
||||
const { joins, outer } = paneEnds(infos);
|
||||
assert.equal(joins.length, infos.length - 1, "one join between each two pieces, and none at the outer ends");
|
||||
assert.equal(outer.length, 2, "a pane has two outer ends");
|
||||
for (const [jx, jz] of joins) {
|
||||
// the glass sits in the middle of the wall, so two walls at an angle have two different middles, and
|
||||
// ends that stop at their own middle stand 2.6 cm apart on a 10 degree bend — a slot through the pane
|
||||
const near: [number, number, number][] = [];
|
||||
// the glass stands in the middle of the wall, half a thickness off the axis the join sits on
|
||||
for (let i = 0; i + 2 < gp.length; i += 3) if (Math.hypot(gp[i] - jx, gp[i + 2] - jz) < 0.2) near.push([gp[i], gp[i + 1], gp[i + 2]]);
|
||||
let worst = 0;
|
||||
// in plan only: two vertices on one vertical line are the same point of the sheet, whatever their height
|
||||
for (const a of near) for (const b of near) worst = Math.max(worst, Math.hypot(a[0] - b[0], a[2] - b[2]));
|
||||
assert.ok(near.length > 0, "the pieces reach the join");
|
||||
assert.ok(worst < 0.002, `the two pieces meet at (${jx.toFixed(2)},${jz.toFixed(2)}), not ${worst.toFixed(3)} m apart`);
|
||||
// a jamb drawn at a join is a 4 cm post standing in the middle of the glass
|
||||
let post = 0;
|
||||
for (let i = 0; i + 2 < fp.length; i += 3) {
|
||||
if (fp[i + 1] < 0.1 || fp[i + 1] > 2.3) continue;
|
||||
if (Math.hypot(fp[i] - jx, fp[i + 2] - jz) < 0.2) post++;
|
||||
}
|
||||
assert.equal(post, 0, "no frame stands in the glass at a join");
|
||||
}
|
||||
for (const [ox, oz] of outer) {
|
||||
let jamb = 0;
|
||||
for (let i = 0; i + 2 < fp.length; i += 3) {
|
||||
if (fp[i + 1] < 0.1 || fp[i + 1] > 2.3) continue;
|
||||
if (Math.hypot(fp[i] - ox, fp[i + 2] - oz) < 0.2) jamb++;
|
||||
}
|
||||
assert.ok(jamb > 0, "the outer ends of a pane do carry a jamb");
|
||||
}
|
||||
}
|
||||
|
||||
test("a pane over several edges is glazed as one, without jambs where its pieces meet", () => {
|
||||
const infos = pane();
|
||||
// every piece finds its own wall: none is dropped
|
||||
assert.equal(infos.length, 6);
|
||||
const joined = buildOpeningParts(infos, new Map([["f", CLOSED]]), 1.2);
|
||||
const apart = buildOpeningParts(
|
||||
infos.map((i) => ({ ...i, opening: { ...i.opening, front: null } })),
|
||||
new Map(),
|
||||
1.2,
|
||||
);
|
||||
assert.ok(triangles(joined.frames) < triangles(apart.frames), "no jamb where two pieces of one pane meet");
|
||||
// the glass itself stays the same, however the pieces are grouped
|
||||
assert.equal(triangles(joined.glass), triangles(apart.glass));
|
||||
assertOneSheet(infos);
|
||||
});
|
||||
|
||||
test("the order the plan lists the pieces in does not decide where the jambs go", () => {
|
||||
// the openings of a curved wall are stored in the order the plan holds them, which is not always the
|
||||
// order along the outline. Taking the first and the last of that list as the pane's ends puts both
|
||||
// jambs of a two-piece pane right where the two pieces meet.
|
||||
const infos = pane(true);
|
||||
assert.equal(infos.length, 6);
|
||||
assertOneSheet(infos);
|
||||
});
|
||||
|
||||
test("a garage door with a cover gets no roller blind in front of it (#85)", () => {
|
||||
const door = build({ type: "garage", width: 2.5, sill: 0, height: 2.1 }, { cover: 1 });
|
||||
const panelOnly = build({ type: "garage", width: 2.5, sill: 0, height: 2.1 }, { cover: 1, cover2: null });
|
||||
assert.equal(triangles(door.blinds), triangles(panelOnly.blinds));
|
||||
// the outside blind box (cassette above the opening) is not drawn: no frame part reaches 0.2 m above the top
|
||||
const ys = positions(door.frames).filter((_, i) => i % 3 === 1);
|
||||
assert.ok(Math.max(...ys) < 2.1 + 0.15, "no blind cassette above a garage door");
|
||||
});
|
||||
@@ -0,0 +1,27 @@
|
||||
// Colours and angles of doors and windows.
|
||||
|
||||
import { shade } from "../geo.ts";
|
||||
|
||||
export const FRAME = 0x1f3052;
|
||||
export const FRAME_TOP = 0x2a4270;
|
||||
export const SASH = 0x22375f;
|
||||
export const SILL = 0x1c2a47;
|
||||
export const BLIND_BOX = 0x16223a;
|
||||
/** The warm glow of a window or door that is open, or that should be closed. */
|
||||
export const OPEN_WARM = 0xffb547;
|
||||
export const LEAF = 0x1c2c4d;
|
||||
export const LEAF_TOP = 0x27406b;
|
||||
export const LEAF_FRONT = 0x111a30;
|
||||
export const LEAF_FRONT_TOP = 0x1c2a47;
|
||||
/** The free edge of a door whose smart lock is unlocked: the stock cyan, which the neon look turns into the user's accent. */
|
||||
export const UNLOCKED = 0x37e0ff;
|
||||
export const HANDLE = 0x5b7cff;
|
||||
export const HANDLE_TOP = 0x8aa2ff;
|
||||
export const GLASS = shade(0x37e0ff, 0.08);
|
||||
|
||||
/** How far a window sash swings open, how far a door leaf, and how far a tilted sash leans (radians). */
|
||||
export const OPEN_ANGLE = 1.2;
|
||||
export const DOOR_ANGLE = 1.5;
|
||||
export const TILT_ANGLE = 0.2;
|
||||
/** How far a top-hung sash swings out at the bottom when open (about 30°). */
|
||||
export const TOP_HUNG_ANGLE = 0.52;
|
||||
@@ -0,0 +1,114 @@
|
||||
// The moving and state-dependent parts of doors and windows: fixed frames, sashes with glass (open or tilted, from contact
|
||||
// sensors), window sills, door frames, blind boxes and blind slats. They are rebuilt as merged geometry whenever a state
|
||||
// changes (and during the short open/close animation), so all windows of a floor stay three draw calls: frames, glass and blinds.
|
||||
|
||||
import { openingStyle, type Vec2 } from "../../model.ts";
|
||||
import type { OpeningInfo } from "../build.ts";
|
||||
import { GeoBuffer } from "../geo.ts";
|
||||
import { blind } from "./blinds.ts";
|
||||
import type { Ctx } from "./context.ts";
|
||||
import { doorFrame, doorLeaves, garageDoor } from "./doors.ts";
|
||||
import { lightWell, needsLightWell } from "./light-well.ts";
|
||||
import { CLOSED, type OpeningParts, type OpeningState, type TriRange } from "./types.ts";
|
||||
import { glassWall, window } from "./windows.ts";
|
||||
|
||||
/** The id that groups the pieces of one glass pane; a normal opening is a group of its own. */
|
||||
const paneOf = (info: OpeningInfo): string => info.opening.front ?? info.opening.id;
|
||||
|
||||
export function buildOpeningParts(infos: readonly OpeningInfo[], states: ReadonlyMap<string, OpeningState>, cut: number, ground = -Infinity): OpeningParts {
|
||||
const frames = new GeoBuffer();
|
||||
const glass = new GeoBuffer();
|
||||
const blinds = new GeoBuffer(true);
|
||||
const frameTris: TriRange[] = [];
|
||||
const glassTris: TriRange[] = [];
|
||||
const blindTris: TriRange[] = [];
|
||||
|
||||
// pieces of one pane that follows a curved wall: only the two outer ends get a jamb, so the glass is
|
||||
// continuous where two of them meet. Every other opening is a group of one and keeps both jambs.
|
||||
const groups = new Map<string, number[]>();
|
||||
infos.forEach((info, i) => {
|
||||
const key = paneOf(info);
|
||||
groups.set(key, [...(groups.get(key) ?? []), i]);
|
||||
});
|
||||
// Which end is an outer end does not follow from the order of the plan: the pieces of one pane are
|
||||
// stored in whatever order the openings happen to be listed, and on a curved wall that is often not the
|
||||
// order along the outline. It follows from the geometry instead — an end that another piece of the same
|
||||
// pane meets is a join, not the end of the pane. Getting this wrong draws both jambs of a two-piece
|
||||
// pane right at the join between them, which is a 4 cm post standing in the middle of the glass.
|
||||
const endsOf = (info: OpeningInfo): [Vec2, Vec2] => [
|
||||
[info.start[0], info.start[1]],
|
||||
[info.start[0] + info.axis[0] * info.width, info.start[1] + info.axis[1] * info.width],
|
||||
];
|
||||
const keyOf = (p: Vec2) => `${Math.round(p[0] * 50)},${Math.round(p[1] * 50)}`;
|
||||
const ends = new Map<number, [boolean, boolean]>();
|
||||
// Two walls at an angle have two different middles, so the ends of two pieces of one pane stand
|
||||
// 2 * mid * sin(angle/2) apart — 2.6 cm on a 10 degree bend, which reads as a slot through the glass.
|
||||
// Running each piece into the bisector of the bend (mid * tan(angle/2)) puts both end edges on the same
|
||||
// vertical line, and the pane is one surface again. A real corner is left alone: there the miter would
|
||||
// be longer than the wall is thick, and a corner is allowed to look like a corner.
|
||||
const over = new Map<number, [number, number]>();
|
||||
for (const list of groups.values()) {
|
||||
const startsAt = new Map<string, number[]>();
|
||||
const endsAt = new Map<string, number[]>();
|
||||
for (const i of list) {
|
||||
const [s, e] = endsOf(infos[i]);
|
||||
startsAt.set(keyOf(s), [...(startsAt.get(keyOf(s)) ?? []), i]);
|
||||
endsAt.set(keyOf(e), [...(endsAt.get(keyOf(e)) ?? []), i]);
|
||||
}
|
||||
const reach = (i: number, other: number | undefined) => {
|
||||
if (other == null || other === i) return 0;
|
||||
const mid = Math.abs((infos[i].faceRoom - infos[i].faceOut) / 2);
|
||||
const [a, b] = [infos[i].axis, infos[other].axis];
|
||||
const cos = Math.max(-1, Math.min(1, a[0] * b[0] + a[1] * b[1]));
|
||||
const angle = Math.acos(cos);
|
||||
return angle < Math.PI / 2 ? Math.min(mid * Math.tan(angle / 2), mid) : 0;
|
||||
};
|
||||
for (const i of list) {
|
||||
const [s, e] = endsOf(infos[i]);
|
||||
const before = (endsAt.get(keyOf(s)) ?? []).find((j) => j !== i);
|
||||
const after = (startsAt.get(keyOf(e)) ?? []).find((j) => j !== i);
|
||||
ends.set(i, [before == null, after == null]);
|
||||
over.set(i, [reach(i, before), reach(i, after)]);
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0; i < infos.length; i++) {
|
||||
const info = infos[i];
|
||||
const starts = { frames: frames.count, glass: glass.count, blinds: blinds.count };
|
||||
const type = info.opening.type;
|
||||
const style = openingStyle(info.opening, info.exterior);
|
||||
const [first, last] = ends.get(i) ?? [true, true];
|
||||
const ctx: Ctx = {
|
||||
info,
|
||||
state: states.get(paneOf(info)) ?? CLOSED,
|
||||
// local frame: x from the opening's start along the wall, n from the wall axis towards the room
|
||||
frame: (x, n, y) => [info.start[0] + info.axis[0] * x + info.toRoom[0] * n, y, info.start[1] + info.axis[1] * x + info.toRoom[1] * n],
|
||||
mid: (info.faceRoom - info.faceOut) / 2,
|
||||
// highlighted while open, or while closed when the opening asks for it (and a sensor knows)
|
||||
markClosed: info.opening.mark === "closed",
|
||||
fold: { cut, bucket: info.bucket },
|
||||
frames,
|
||||
glass,
|
||||
blinds,
|
||||
first,
|
||||
last,
|
||||
over: over.get(i) ?? [0, 0],
|
||||
};
|
||||
// a passage (a wall opening without a door) shows nothing but the gap in the wall
|
||||
if ((type === "door" && style !== "passage") || type === "garage") doorFrame(ctx);
|
||||
if (type === "door") doorLeaves(ctx);
|
||||
else if (type === "garage") garageDoor(ctx);
|
||||
else if (style === "glass_wall") glassWall(ctx);
|
||||
else window(ctx);
|
||||
// a cellar window below the ground gets its light well
|
||||
if (needsLightWell(ctx, ground)) lightWell(ctx, ground);
|
||||
// a garage door's cover moves the door itself; it has no blind in front of it (#85)
|
||||
if (type !== "garage") blind(ctx);
|
||||
// picking returns the id the model knows, not a piece id
|
||||
const id = paneOf(info);
|
||||
frameTris.push({ id, start: starts.frames, end: frames.count });
|
||||
glassTris.push({ id, start: starts.glass, end: glass.count });
|
||||
blindTris.push({ id, start: starts.blinds, end: blinds.count });
|
||||
}
|
||||
return { frames: frames.geometry(), glass: glass.geometry(), blinds: blinds.geometry(), frameTris, glassTris, blindTris };
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
// Building blocks for the moving parts of doors and windows. Everything is placed in a local frame of an opening: x runs
|
||||
// along the opening from its start, n points from the wall axis towards the room, y is up.
|
||||
|
||||
import { Color } from "three";
|
||||
import { ALWAYS, shade, type GeoBuffer } from "../geo.ts";
|
||||
|
||||
/** Maps a local (x, n, y) to world coordinates. */
|
||||
export type Frame = (x: number, n: number, y: number) => number[];
|
||||
|
||||
export interface Box {
|
||||
x0: number;
|
||||
x1: number;
|
||||
n0: number;
|
||||
n1: number;
|
||||
y0: number;
|
||||
y1: number;
|
||||
}
|
||||
|
||||
/** A box with all six faces; the lower, near and side faces are a little darker than the top and the far face. */
|
||||
export function box(buf: GeoBuffer, frame: Frame, b: Box, side: Color, top: Color, fold: number): void {
|
||||
const p = (x: number, n: number, y: number) => frame(x, n, y);
|
||||
const { x0, x1, n0, n1, y0, y1 } = b;
|
||||
const tint = (k: number) => shade(side.getHex(), k);
|
||||
const faces: [number[], number[], number[], number[], Color][] = [
|
||||
[p(x0, n0, y1), p(x1, n0, y1), p(x1, n1, y1), p(x0, n1, y1), top],
|
||||
[p(x0, n0, y0), p(x1, n0, y0), p(x1, n1, y0), p(x0, n1, y0), tint(0.6)],
|
||||
[p(x0, n1, y0), p(x1, n1, y0), p(x1, n1, y1), p(x0, n1, y1), side],
|
||||
[p(x0, n0, y0), p(x1, n0, y0), p(x1, n0, y1), p(x0, n0, y1), tint(0.85)],
|
||||
[p(x0, n0, y0), p(x0, n1, y0), p(x0, n1, y1), p(x0, n0, y1), tint(0.92)],
|
||||
[p(x1, n0, y0), p(x1, n1, y0), p(x1, n1, y1), p(x1, n0, y1), tint(0.92)],
|
||||
];
|
||||
for (const [a, b2, c, d, color] of faces) {
|
||||
buf.tri(a, b2, c, color, color, color, undefined, fold);
|
||||
buf.tri(a, c, d, color, color, color, undefined, fold);
|
||||
}
|
||||
}
|
||||
|
||||
/** The cut height and the fold bucket of the wall a part sits in: what lies above the cut folds away with the wall. */
|
||||
export interface Fold {
|
||||
cut: number;
|
||||
bucket: number;
|
||||
}
|
||||
|
||||
/** A box split at the cut height: the part above folds away with its wall. */
|
||||
export function splitBox(buf: GeoBuffer, frame: Frame, b: Box, side: Color, top: Color, { cut, bucket }: Fold): void {
|
||||
if (b.y1 <= cut + 1e-6) return box(buf, frame, b, side, top, ALWAYS);
|
||||
if (b.y0 >= cut - 1e-6) return box(buf, frame, b, side, top, bucket);
|
||||
box(buf, frame, { ...b, y1: cut }, side, top, ALWAYS);
|
||||
box(buf, frame, { ...b, y0: cut }, side, top, bucket);
|
||||
}
|
||||
|
||||
/** A flat upright quad in a local plane (glass, slats), split at the cut height, with optional uvs (`vScale` metres per texture repeat). */
|
||||
export function panel(buf: GeoBuffer, frame: Frame, x0: number, x1: number, n: number, y0: number, y1: number, color: Color, { cut, bucket }: Fold, vScale = 0): void {
|
||||
const piece = (ya: number, yb: number, fold: number) => {
|
||||
const v = (y: number) => (vScale ? (y1 - y) / vScale : 0.5);
|
||||
const [a, b, c, d] = [frame(x0, n, ya), frame(x1, n, ya), frame(x1, n, yb), frame(x0, n, yb)];
|
||||
buf.tri(a, b, c, color, color, color, [0, v(ya), 1, v(ya), 1, v(yb)], fold);
|
||||
buf.tri(a, c, d, color, color, color, [0, v(ya), 1, v(yb), 0, v(yb)], fold);
|
||||
};
|
||||
if (y1 <= cut + 1e-6) piece(y0, y1, ALWAYS);
|
||||
else if (y0 >= cut - 1e-6) piece(y0, y1, bucket);
|
||||
else {
|
||||
piece(y0, cut, ALWAYS);
|
||||
piece(cut, y1, bucket);
|
||||
}
|
||||
}
|
||||
|
||||
/** A horizontal quad in the local frame at height y (the open part of a garage door). */
|
||||
export function flatPanel(buf: GeoBuffer, frame: Frame, x0: number, x1: number, n0: number, n1: number, y: number, color: Color, fold: number, vScale: number): void {
|
||||
const [a, b, c, d] = [frame(x0, n0, y), frame(x1, n0, y), frame(x1, n1, y), frame(x0, n1, y)];
|
||||
const v1 = (n1 - n0) / vScale;
|
||||
buf.tri(a, b, c, color, color, color, [0, 0, 1, 0, 1, v1], fold);
|
||||
buf.tri(a, c, d, color, color, color, [0, 0, 1, v1, 0, v1], fold);
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import type { BufferGeometry } from "three";
|
||||
|
||||
export interface OpeningState {
|
||||
/** A window sash or door leaf: 0 closed, 1 swung open. */
|
||||
open: number;
|
||||
/** The second leaf of a double door or window. */
|
||||
open2?: number;
|
||||
/** 0 closed, 1 tilted. */
|
||||
tilt: number;
|
||||
/** The second leaf tilted. */
|
||||
tilt2?: number;
|
||||
/** The closed fraction of the blind or garage door (0 up, 1 down); null: no blind. */
|
||||
cover: number | null;
|
||||
/** A sensor reports the state (contact, tilt sensor or cover); without one "closed" is a guess. */
|
||||
sensed?: boolean;
|
||||
/** A door with a smart lock that stands unlocked: its free edge glows. */
|
||||
unlocked?: boolean;
|
||||
/** The door leaf stands where it is assumed to, as no sensor tells: it does not glow as open. */
|
||||
assumed?: boolean;
|
||||
/** The second blind of a wide window (its right half): its closed fraction. */
|
||||
cover2?: number | null;
|
||||
/** Venetian blinds: how far the slats are open (0 shut, 1 level). */
|
||||
slats?: number | null;
|
||||
}
|
||||
|
||||
export const CLOSED: OpeningState = { open: 0, open2: 0, tilt: 0, tilt2: 0, cover: null };
|
||||
|
||||
export interface TriRange {
|
||||
id: string;
|
||||
start: number;
|
||||
end: number;
|
||||
}
|
||||
|
||||
export interface OpeningParts {
|
||||
frames: BufferGeometry;
|
||||
glass: BufferGeometry;
|
||||
blinds: BufferGeometry;
|
||||
/** The triangle ranges per opening in frames, glass and blinds, so they can be tapped in 3D. */
|
||||
frameTris: TriRange[];
|
||||
glassTris: TriRange[];
|
||||
blindTris: TriRange[];
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
// Windows: the fixed frame, sill and board, sashes that turn into the room or tilt, slide (a lift-and-slide door) or swing out
|
||||
// at the bottom (top-hung), glazing bars, and indoor glass walls.
|
||||
|
||||
import { Color } from "three";
|
||||
import { openingStyle } from "../../model.ts";
|
||||
import { ALWAYS, shade } from "../geo.ts";
|
||||
import type { Ctx } from "./context.ts";
|
||||
import { FRAME, FRAME_TOP, GLASS, OPEN_ANGLE, OPEN_WARM, SASH, SILL, TILT_ANGLE, TOP_HUNG_ANGLE } from "./palette.ts";
|
||||
import { panel, splitBox, type Frame } from "./primitives.ts";
|
||||
|
||||
/**
|
||||
* An indoor glass wall: a slim frame, glass from the floor up, no sashes. The mullions divide it into as many panes as the
|
||||
* opening asks for; without a number they come about every 0.9 m, and none at all for a single full pane.
|
||||
*/
|
||||
export function glassWall(c: Ctx): void {
|
||||
const { info, frame, fold, mid } = c;
|
||||
const [W, S, T] = [info.width, info.sill, info.top];
|
||||
const [fw, fd] = [0.04, 0.025];
|
||||
const frameColor = new Color(FRAME);
|
||||
const frameTop = new Color(FRAME_TOP);
|
||||
const bar = (x0: number, x1: number, y0: number, y1: number) => splitBox(c.frames, frame, { x0, x1, n0: mid - fd, n1: mid + fd, y0, y1 }, frameColor, frameTop, fold);
|
||||
// a pane over several edges has jambs only at its two outer ends; where two pieces meet, the glass runs on
|
||||
if (c.first) bar(0, fw, S, T);
|
||||
if (c.last) bar(W - fw, W, S, T);
|
||||
// the 4 cm at each end is the rebate of a jamb. A piece of a pane that does not end here has no jamb, so
|
||||
// its rails and its glass reach the wall end — and run past it into the bisector of the bend, where the
|
||||
// next piece begins: two pieces whose ends stop at their own wall's middle stand a slot apart.
|
||||
const [xa, xb] = [c.first ? fw : -c.over[0], c.last ? W - fw : W + c.over[1]];
|
||||
bar(xa, xb, S, S + 0.03);
|
||||
bar(xa, xb, T - fw, T);
|
||||
// a piece of a pane over several edges is part of one sheet: it gets no mullions of its own, and a pane
|
||||
// that is to be divided is placed as separate openings instead (their jambs are the divisions)
|
||||
const asked = info.opening.front ? 0 : info.opening.panes;
|
||||
const panes = Math.max(1, asked == null ? Math.max(1, Math.round((xb - xa) / 0.9)) : Math.round(asked));
|
||||
const paneWidth = (xb - xa) / panes;
|
||||
for (let k = 1; k < panes; k++) bar(xa + k * paneWidth - 0.02, xa + k * paneWidth + 0.02, S + 0.03, T - fw);
|
||||
for (let k = 0; k < panes; k++) {
|
||||
const x0 = xa + k * paneWidth + (k ? 0.02 : 0);
|
||||
const x1 = xa + (k + 1) * paneWidth - (k < panes - 1 ? 0.02 : 0);
|
||||
panel(c.glass, frame, x0, x1, mid, S + 0.03, T - fw, GLASS, fold);
|
||||
}
|
||||
}
|
||||
|
||||
interface Sash {
|
||||
atStart: boolean;
|
||||
x0: number;
|
||||
x1: number;
|
||||
open: number;
|
||||
tilt: number;
|
||||
/** A sliding window's pane: how far it moves along the wall when fully open, and its track (0: the fixed panes, further out). */
|
||||
slide?: number;
|
||||
track?: 0 | 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* The panes of a sliding window or lift-and-slide door. One leaf: the pane at the hinge side slides in front of the fixed one.
|
||||
* Two leaves: four panes, the middle two slide outwards in front of the fixed outer ones, each on its own contact.
|
||||
*/
|
||||
export function slidingPanes(W: number, fw: number, hingeAtStart: boolean, leaves: 1 | 2, open: number, open2: number): Sash[] {
|
||||
const inner = W - 2 * fw;
|
||||
const overlap = 0.04;
|
||||
if (leaves === 2) {
|
||||
const pane = inner / 4 + overlap / 2;
|
||||
const run = pane - overlap - 0.02;
|
||||
return [
|
||||
{ atStart: true, x0: fw, x1: fw + pane, open: 0, tilt: 0, slide: 0, track: 0 },
|
||||
{ atStart: true, x0: fw + inner / 4 - overlap / 2, x1: fw + inner / 2, open: hingeAtStart ? open : open2, tilt: 0, slide: -run, track: 1 },
|
||||
{ atStart: false, x0: fw + inner / 2, x1: W - fw - inner / 4 + overlap / 2, open: hingeAtStart ? open2 : open, tilt: 0, slide: run, track: 1 },
|
||||
{ atStart: false, x0: W - fw - pane, x1: W - fw, open: 0, tilt: 0, slide: 0, track: 0 },
|
||||
];
|
||||
}
|
||||
const half = inner / 2;
|
||||
const run = half - overlap - 0.02;
|
||||
// the moving pane at the hinge side slides towards the fixed one
|
||||
return hingeAtStart
|
||||
? [
|
||||
{ atStart: true, x0: fw, x1: fw + half + overlap / 2, open, tilt: 0, slide: run, track: 1 },
|
||||
{ atStart: false, x0: fw + half - overlap / 2, x1: W - fw, open: 0, tilt: 0, slide: 0, track: 0 },
|
||||
]
|
||||
: [
|
||||
{ atStart: true, x0: fw, x1: fw + half + overlap / 2, open: 0, tilt: 0, slide: 0, track: 0 },
|
||||
{ atStart: false, x0: fw + half - overlap / 2, x1: W - fw, open, tilt: 0, slide: -run, track: 1 },
|
||||
];
|
||||
}
|
||||
|
||||
/** A normal window: the fixed frame in the middle of the wall, a board inside and a sill outside, and one or two sashes. */
|
||||
export function window(c: Ctx): void {
|
||||
const { info, state, frame, fold, mid } = c;
|
||||
const [W, S, T] = [info.width, info.sill, info.top];
|
||||
const [fw, fd] = [0.06, 0.035];
|
||||
const frameColor = new Color(FRAME);
|
||||
const frameTop = new Color(FRAME_TOP);
|
||||
const sillColor = new Color(SILL);
|
||||
const bar = (x0: number, x1: number, y0: number, y1: number) => splitBox(c.frames, frame, { x0, x1, n0: mid - fd, n1: mid + fd, y0, y1 }, frameColor, frameTop, fold);
|
||||
const lowerBar = S + (S > 0.05 ? fw : 0.03);
|
||||
bar(0, fw, S, T);
|
||||
bar(W - fw, W, S, T);
|
||||
bar(fw, W - fw, S, lowerBar);
|
||||
bar(fw, W - fw, T - fw, T);
|
||||
// a window board inside and a sill outside (automatic: both above 30 cm; or as chosen for this window)
|
||||
const boards = info.opening.sill_boards ?? "auto";
|
||||
if (boards !== "none" && (boards !== "auto" || S > 0.3)) {
|
||||
if (boards !== "outside") splitBox(c.frames, frame, { x0: -0.04, x1: W + 0.04, n0: mid + fd, n1: info.faceRoom + 0.07, y0: S - 0.03, y1: S }, sillColor, frameTop, fold);
|
||||
if (info.exterior && boards !== "inside") splitBox(c.frames, frame, { x0: -0.03, x1: W + 0.03, n0: -info.faceOut - 0.06, n1: mid - fd, y0: S - 0.04, y1: S - 0.02 }, sillColor, frameTop, fold);
|
||||
}
|
||||
|
||||
// sashes rotate into the room around their hinge, or tilt around their bottom edge; a double window (or French door) has
|
||||
// two sashes meeting in the middle, each tilting on its own
|
||||
const barWidth = 0.055;
|
||||
const [sy0, sy1] = [lowerBar, T - fw];
|
||||
const [n0, n1] = [mid + fd, mid + fd + 0.06];
|
||||
const hinge = info.hingeAtStart;
|
||||
const opens = info.opening.opens ?? "turn";
|
||||
const sashes: Sash[] =
|
||||
opens === "slide"
|
||||
? slidingPanes(W, fw, hinge, info.opening.leaves, state.open, state.open2 ?? 0)
|
||||
: info.opening.leaves === 2
|
||||
? [
|
||||
{ atStart: hinge, x0: hinge ? fw : W / 2, x1: hinge ? W / 2 : W - fw, open: state.open, tilt: state.tilt },
|
||||
{ atStart: !hinge, x0: hinge ? W / 2 : fw, x1: hinge ? W - fw : W / 2, open: state.open2 ?? 0, tilt: state.tilt2 ?? 0 },
|
||||
]
|
||||
: [{ atStart: hinge, x0: fw, x1: W - fw, open: state.open, tilt: state.tilt }];
|
||||
const style = openingStyle(info.opening, info.exterior);
|
||||
for (const sash of sashes) {
|
||||
// a top-hung sash opens with its contact or a tilt sensor alike; a fixed pane never glows
|
||||
const swingOut = opens === "top" ? Math.max(sash.open, sash.tilt) * TOP_HUNG_ANGLE : 0;
|
||||
const moves = opens !== "slide" || !!sash.slide;
|
||||
const ajar = moves && (sash.open > 0.02 || sash.tilt > 0.02);
|
||||
const alert = c.markClosed ? !!state.sensed && !ajar : ajar;
|
||||
const side = alert ? shade(OPEN_WARM, 0.75) : new Color(SASH);
|
||||
const top = alert ? shade(OPEN_WARM, 0.95) : frameTop;
|
||||
const sashWidth = sash.x1 - sash.x0;
|
||||
const turning = opens === "turn";
|
||||
const theta = turning ? sash.open * OPEN_ANGLE : 0;
|
||||
const lean = turning ? sash.tilt * TILT_ANGLE : 0;
|
||||
// a sliding pane runs in the sash plane; the fixed panes of a sliding window sit one track further out, so it passes in front of them
|
||||
const shift = (sash.slide ?? 0) * sash.open;
|
||||
const track = opens === "slide" && sash.track === 0 ? -0.065 : 0;
|
||||
// sash coordinates: u from the hinge across the sash, then n and y
|
||||
const sashFrame: Frame = (u, n, y) => {
|
||||
if (swingOut > 0) {
|
||||
// top-hung: turned about the sash's top edge, so its bottom swings out of the wall (towards -n)
|
||||
const [dn, dy] = [n - n0, y - sy1];
|
||||
const [c, s] = [Math.cos(swingOut), Math.sin(swingOut)];
|
||||
return frame(sash.atStart ? sash.x0 + u : sash.x1 - u, n0 + dn * c + dy * s, sy1 - dn * s + dy * c);
|
||||
}
|
||||
const dy = y - sy0;
|
||||
const tilted = n + dy * Math.sin(lean) + track;
|
||||
const yy = sy0 + dy * Math.cos(lean);
|
||||
const along = u * Math.cos(theta) - (tilted - n0) * Math.sin(theta);
|
||||
const across = n0 + (tilted - n0) * Math.cos(theta) + u * Math.sin(theta);
|
||||
return frame((sash.atStart ? sash.x0 + along : sash.x1 - along) + shift, across, yy);
|
||||
};
|
||||
// a sash that has swung into the room (or out of the wall) belongs to no wall: keep it visible
|
||||
const sashFold = { cut: fold.cut, bucket: theta > 0.05 || swingOut > 0.05 ? ALWAYS : fold.bucket };
|
||||
const part = (xa: number, xb: number, ya: number, yb: number, na = n0, nb = n1) => splitBox(c.frames, sashFrame, { x0: xa, x1: xb, n0: na, n1: nb, y0: ya, y1: yb }, side, top, sashFold);
|
||||
part(0, barWidth, sy0, sy1);
|
||||
part(sashWidth - barWidth, sashWidth, sy0, sy1);
|
||||
part(barWidth, sashWidth - barWidth, sy0, sy0 + barWidth);
|
||||
part(barWidth, sashWidth - barWidth, sy1 - barWidth, sy1);
|
||||
panel(c.glass, sashFrame, barWidth, sashWidth - barWidth, (n0 + n1) / 2, sy0 + barWidth, sy1 - barWidth, alert ? shade(OPEN_WARM, 0.16) : GLASS, sashFold);
|
||||
if (style === "bars") {
|
||||
// glazing bars: a cross over the pane
|
||||
const [ym, nm] = [(sy0 + sy1) / 2, (n0 + n1) / 2];
|
||||
part(barWidth, sashWidth - barWidth, ym - 0.012, ym + 0.012, nm - 0.012, nm + 0.012);
|
||||
part(sashWidth / 2 - 0.012, sashWidth / 2 + 0.012, sy0 + barWidth, sy1 - barWidth, nm - 0.012, nm + 0.012);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user