@kensnzk/koyu 0.23.0 → 0.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/draw/plan.ts CHANGED
@@ -9,10 +9,12 @@
9
9
  // 扉の軌跡の中心と半径と掃き方向・切断面を跨ぐ位置は Form が持つ。1/4円を破線で描くか、
10
10
  // 切断線を平行な二本の斜線として引くか、矢印に "UP" と書くかは、ここが決める。
11
11
 
12
- import { band, derive, type PlanEntity } from "../core/derive.js";
13
- import { canonicalBoundaryOrder, displayName, polyBounds, type Model, type Pt } from "../core/model.js";
12
+ import { derive } from "../core/derive.js";
13
+ import { displayName, polyBounds, type Model, type Pt } from "../core/model.js";
14
14
  import { slopeText } from "../core/vertical.js";
15
+ import { planMarks, type Mark } from "./marks.js";
15
16
  import { esc, Extent, FAINT, GRID, INK, openSheet, PAPER, ROOM } from "./sheet.js";
17
+ import { writtenOf } from "./written.js";
16
18
 
17
19
  export interface PlanOptions {
18
20
  level?: string;
@@ -127,112 +129,100 @@ export function svgPlan(model: Model, opts: PlanOptions = {}): string {
127
129
  );
128
130
  }
129
131
 
130
- // 壁 — Form が返す「開口で割られた区間」をそのまま塗る。
131
- // **欠き取り (紙の色で塗り潰して穴に見せる手) は無い** — 壁は最初から穴の空いた列である
132
- const byRef = new Map(form.boundaries.map((b) => [b.ref, b]));
133
- for (const e of plan.entities) {
134
- if (e.of !== "boundary") continue;
135
- const b = byRef.get(e.ref);
136
- if (!e.polygon) {
132
+ // 印 — 形の写しは `planMarks` にある。**この頁が足すのは色・線幅・線種・記号・注記の言葉だけ**
133
+ // である。ugatsu も architype も同じ印から別の見た目を引く。
134
+ const marks = planMarks(form, level);
135
+ const written = writtenOf(model);
136
+ const segByRef = new Map(form.segs.map((g) => [g.ref, g]));
137
+ for (const k of marks) {
138
+ switch (k.role) {
139
+ // 空間の面と分節の帯は下 (`rooms` / `form.segs`) から引く — 切られた面ではなく外形を塗り、
140
+ // 分節には Form しか持たない座と向きが要るため。上部吹抜けは空間ラベルより後ろに置く
141
+ case "space":
142
+ case "space-semi-outdoor":
143
+ case "space-void":
144
+ case "void-hatch":
145
+ case "void-above":
146
+ break;
147
+ // 数えない分節 (seg): 壁材が途中から変わる区間 — 色調を変えて示す。
148
+ // ここから引くのは注記の言葉 (`spec`) だけで、それは形ではないので Form には載らない。
149
+ // **`written.boundary` は正準順の添字である** — 宣言順の配列を引くと、並べ替えただけで
150
+ // 注記が別の境界のものになる
151
+ case "seg": {
152
+ parts.push(fill(k.polygon!, "#77716a"));
153
+ const spec = written.segSpec(k.written!.boundary, k.written!.index!);
154
+ const g = segByRef.get(k.ref);
155
+ if (typeof spec === "string" && g) {
156
+ const h = g.segment.horizontal;
157
+ parts.push(
158
+ `<text x="${sx(g.cx) + (h ? 0 : 8)}" y="${sy(g.cy) + (h ? -7 : 3)}" text-anchor="${h ? "middle" : "start"}" font-size="8" fill="#77716a">${esc(spec)}</text>`,
159
+ );
160
+ }
161
+ break;
162
+ }
137
163
  // 物を持たない境界 (open): 構成の線として破線で示す (基本計画の抽象度)
138
- for (const g of e.lines ?? []) parts.push(line(g, FAINT, 1, "6 4"));
139
- continue;
140
- }
141
- if (e.class === "above") continue; // 垂れ壁は切断面より上 — 平面には出ない
142
- if (b?.air) {
164
+ case "open":
165
+ for (const g of k.lines ?? []) parts.push(line(g, FAINT, 1, "6 4"));
166
+ break;
143
167
  // 遮蔽しない物 (手すり・柵): 細実線 — 「囲われていない」ことが図から読めるように。
144
- // 切断面より上でも下でも一本の線で描く (厚みを持たない作図表現)。
145
168
  // **芯線は Form が持つ** — 足あとの四辺形から復元しない
146
- for (const g of e.lines ?? []) parts.push(line(g, INK, 1.4));
147
- continue;
148
- }
149
- // 切断面が切った区間だけを黒帯にする。腰壁 (class:below) は開口の下なので描かない —
150
- // **これが「欠き取り」の代わりである**。紙の色で塗り潰す操作はもう無い
151
- if (e.class === "cut") parts.push(fill(e.polygon, INK));
152
- }
153
-
154
- // 数えない分節 (seg): 壁材が途中から変わる区間 — 色調を変えて示す。
155
- // 帯の形は core の構成子が起こす。ここから引くのは注記の言葉 (`spec`) だけで、
156
- // それは形ではないので Form には載らない — 添字は宣言の並びの位置 = seg の同一性である
157
- const ordered = canonicalBoundaryOrder(model);
158
- for (const g of form.segs) {
159
- if (g.level !== level) continue;
160
- parts.push(fill(band(g.segment, g.cx, g.cy, g.w, g.t), "#77716a"));
161
- // `g.boundary` は **Form の境界の添字 = 正準順の添字**である (docs/reference/form/index.md)。
162
- // 宣言順の配列を引くと、宣言順を入れ替えただけで注記が別の境界のものになる
163
- const spec = ordered[g.boundary]?.segs[g.index]?.attrs["spec"];
164
- if (typeof spec === "string") {
165
- const h = g.segment.horizontal;
166
- parts.push(
167
- `<text x="${sx(g.cx) + (h ? 0 : 8)}" y="${sy(g.cy) + (h ? -7 : 3)}" text-anchor="${h ? "middle" : "start"}" font-size="8" fill="#77716a">${esc(spec)}</text>`,
168
- );
169
- }
170
- }
171
-
172
- // 開口 — 窓は芯線一本、扉は葉と軌跡 (1/4円の破線)
173
- const openings = new Map(form.openings.map((o) => [o.ref, o]));
174
- for (const e of plan.entities) {
175
- if (e.of !== "opening") continue;
176
- const o = openings.get(e.ref);
177
- if (!o) continue;
178
- if (e.class === "swing") {
179
- if (o.sliding) {
180
- // 引戸・自動ドア: 開き軌跡ではなく吊元側の控え (戸袋側) にパネルを描く
181
- const off = 110; // 壁面からの控え mm (作図表現)
182
- const sw = o.swing!;
183
- const ux = (sw.leaf.x - sw.hinge.x) / o.w;
184
- const uy = (sw.leaf.y - sw.hinge.y) / o.w;
185
- const ax = (sw.jamb.x - sw.hinge.x) / o.w;
186
- const ay = (sw.jamb.y - sw.hinge.y) / o.w;
187
- const s1 = { x: sw.hinge.x - ax * o.w + ux * off, y: sw.hinge.y - ay * o.w + uy * off };
188
- const s2 = { x: sw.hinge.x + ux * off, y: sw.hinge.y + uy * off };
169
+ case "rail":
170
+ for (const g of k.lines ?? []) parts.push(line(g, INK, 1.4));
171
+ break;
172
+ // 切断面が切った区間と柱を黒帯にする。腰壁は開口の下なので印にならない —
173
+ // **これが「欠き取り」の代わりである**。紙の色で塗り潰す操作はもう無い
174
+ case "wall":
175
+ case "column":
176
+ parts.push(fill(k.polygon!, INK));
177
+ break;
178
+ // 引戸・自動ドア: 開き軌跡ではなく吊元側の控え (戸袋側) にパネルを描く
179
+ case "slide-panel":
180
+ for (const g of k.lines ?? []) parts.push(line(g, INK, 2));
181
+ break;
182
+ case "slide-tail":
183
+ for (const g of k.lines ?? []) parts.push(line(g, INK, 0.7));
184
+ break;
185
+ case "door-leaf":
186
+ for (const g of k.lines ?? []) parts.push(line(g, INK, 1.4));
187
+ break;
188
+ case "door-arc": {
189
+ const a = k.arc!;
190
+ const r = a.r * scale;
191
+ // 掃引方向: 世界の反時計回りは、y を反転した紙の上では時計回りになる
192
+ const sweep = a.ccw ? 0 : 1;
189
193
  parts.push(
190
- line({ x1: s1.x, y1: s1.y, x2: s2.x, y2: s2.y }, INK, 2),
191
- line({ x1: s2.x, y1: s2.y, x2: sw.hinge.x, y2: sw.hinge.y }, INK, 0.7),
194
+ `<path d="M ${sx(a.from.x)} ${sy(a.from.y)} A ${r} ${r} 0 0 ${sweep} ${sx(a.to.x)} ${sy(a.to.y)}" fill="none" stroke="${INK}" stroke-width="0.7" stroke-dasharray="3 2.5"/>`,
192
195
  );
193
- continue;
196
+ break;
194
197
  }
195
- if (e.lines) for (const g of e.lines) parts.push(line(g, INK, 1.4));
196
- if (e.arc) {
197
- const r = e.arc.r * scale;
198
- // 掃引方向: 世界の反時計回りは、y を反転した紙の上では時計回りになる
199
- const sweep = e.arc.ccw ? 0 : 1;
198
+ case "window":
199
+ for (const g of k.lines ?? []) parts.push(line(g, INK, 1));
200
+ break;
201
+ case "run-outline":
202
+ for (const g of k.lines ?? []) parts.push(line(g, INK, 1.1));
203
+ break;
204
+ case "run-tread":
205
+ for (const g of k.lines ?? []) parts.push(line(g, INK, 0.7));
206
+ break;
207
+ case "run-break":
208
+ for (const g of k.lines ?? []) parts.push(...breakMark(g, line));
209
+ break;
210
+ case "run-arrow":
211
+ parts.push(...arrow(k, line, sx, sy));
212
+ break;
213
+ // 注記の言葉と丸めはここで初めて生まれる — 印が運ぶのは丸めない事実だけである
214
+ case "run-note": {
215
+ const n = k.note;
216
+ if (!n || n.of === "direction") break; // 座の注記は向きではない
217
+ const text =
218
+ n.of === "stair"
219
+ ? `${n.risers}段 蹴上${Math.round(n.riser)}/踏面${Math.round(n.tread)}`
220
+ : `${n.lanes > 1 ? `${n.lanes}台 ` : ""}勾配 ${slopeText(n.slope)}`;
200
221
  parts.push(
201
- `<path d="M ${sx(e.arc.from.x)} ${sy(e.arc.from.y)} A ${r} ${r} 0 0 ${sweep} ${sx(e.arc.to.x)} ${sy(e.arc.to.y)}" fill="none" stroke="${INK}" stroke-width="0.7" stroke-dasharray="3 2.5"/>`,
222
+ `<text x="${sx(k.at!.x)}" y="${sy(k.at!.y) + 42}" text-anchor="middle" font-size="8" fill="#8a8171">${esc(text)}</text>`,
202
223
  );
224
+ break;
203
225
  }
204
- continue;
205
- }
206
- if (o.kind !== "door" && e.lines) {
207
- // 窓: 芯線一本
208
- for (const g of e.lines) parts.push(line(g, INK, 1));
209
- }
210
- }
211
-
212
- // 柱 (ADR-0023) — 位置はどこにも書かれていない。通り芯の交点と床の交わりから現れる
213
- for (const e of plan.entities) {
214
- if (e.of === "column" && e.polygon) parts.push(fill(e.polygon, INK));
215
- }
216
-
217
- // 縦動線 (ADR-0021) — そのレベルで切った姿。上る走りは切断線で切れ、
218
- // その先には下りる走り (下階の走りを上から見たもの) が現れる
219
- const runs = new Map(form.runs.map((r) => [r.path, r]));
220
- for (const e of plan.entities) {
221
- if (e.of !== "run") continue;
222
- if (e.role === "outline") for (const g of e.lines ?? []) parts.push(line(g, INK, 1.1));
223
- else if (e.role === "tread") for (const g of e.lines ?? []) parts.push(line(g, INK, 0.7));
224
- else if (e.role === "break") for (const g of e.lines ?? []) parts.push(...breakMark(g, line));
225
- else if (e.role === "arrow") parts.push(...arrow(e, line, sx, sy));
226
- else if (e.class === "anchor" && e.anchor) {
227
- const r = runs.get(e.ref);
228
- if (!r) continue;
229
- const text =
230
- r.device === "stair"
231
- ? `${r.risers}段 蹴上${Math.round(r.riser)}/踏面${Math.round(r.tread)}`
232
- : `${r.lanes > 1 ? `${r.lanes}台 ` : ""}勾配 ${slopeText(r.slope)}`;
233
- parts.push(
234
- `<text x="${sx(e.anchor.x)}" y="${sy(e.anchor.y) + 42}" text-anchor="middle" font-size="8" fill="#8a8171">${esc(text)}</text>`,
235
- );
236
226
  }
237
227
  }
238
228
 
@@ -254,13 +244,12 @@ export function svgPlan(model: Model, opts: PlanOptions = {}): string {
254
244
  );
255
245
  }
256
246
 
257
- // 切断面より上のものの投影 (上部吹抜け — 作図慣習)
258
- for (const e of plan.entities) {
259
- if (e.class !== "above" || e.of !== "space" || !e.polygon) continue;
260
- const r = polyBounds(e.polygon);
247
+ // 切断面より上のものの投影 (上部吹抜け — 作図慣習)。空間ラベルの後に置く
248
+ for (const k of marks) {
249
+ if (k.role !== "void-above") continue;
261
250
  parts.push(
262
- `<path d="${path2d(e.polygon)}" fill="none" stroke="${FAINT}" stroke-width="0.8" stroke-dasharray="6 4"/>`,
263
- `<text x="${sx((r.x1 + r.x2) / 2)}" y="${sy((r.y1 + r.y2) / 2) + 40}" text-anchor="middle" font-size="9" fill="${FAINT}">void above</text>`,
251
+ `<path d="${path2d(k.polygon!)}" fill="none" stroke="${FAINT}" stroke-width="0.8" stroke-dasharray="6 4"/>`,
252
+ `<text x="${sx(k.at!.x)}" y="${sy(k.at!.y) + 40}" text-anchor="middle" font-size="9" fill="${FAINT}">void above</text>`,
264
253
  );
265
254
  }
266
255
 
@@ -334,12 +323,12 @@ function breakMark(g: { x1: number; y1: number; x2: number; y2: number }, line:
334
323
 
335
324
  /** 矢印 — 三角の頭と "UP"/"DN" の言葉は、どちらも見た目である */
336
325
  function arrow(
337
- e: PlanEntity,
326
+ k: Mark,
338
327
  line: Line,
339
328
  sx: (x: number) => number,
340
329
  sy: (y: number) => number,
341
330
  ): string[] {
342
- const g = e.lines?.[0];
331
+ const g = k.lines?.[0];
343
332
  if (!g) return [];
344
333
  const dx = g.x2 - g.x1;
345
334
  const dy = g.y2 - g.y1;
@@ -351,7 +340,7 @@ function arrow(
351
340
  return [
352
341
  line(g, INK, 1),
353
342
  `<path d="M ${sx(g.x2)} ${sy(g.y2)} L ${sx(g.x2 - hx + px)} ${sy(g.y2 - hy + py)} L ${sx(g.x2 - hx - px)} ${sy(g.y2 - hy - py)} Z" fill="${INK}"/>`,
354
- `<text x="${sx(g.x1) + 4}" y="${sy(g.y1) + 4}" font-size="9" fill="${INK}">${e.anchor?.up ? "UP" : "DN"}</text>`,
343
+ `<text x="${sx(g.x1) + 4}" y="${sy(g.y1) + 4}" font-size="9" fill="${INK}">${k.note?.of === "direction" && k.note.up ? "UP" : "DN"}</text>`,
355
344
  ];
356
345
  }
357
346
 
@@ -0,0 +1,389 @@
1
+ // koyu — the scene a Form describes (`@kensnzk/koyu/draw`)
2
+ //
3
+ // **Plain data. No three.js, no colour, no word, and no option.** A viewer turns these nodes into
4
+ // meshes with its own materials, its own lighting and its own idea of what is selected; two viewers
5
+ // showing the same building disagree about all of that and about none of this.
6
+ //
7
+ // **Why data and not a scene graph.** The package declares no runtime dependency and a test holds
8
+ // it to that, so a `THREE.Group` cannot be returned from here. That constraint turned out to be the
9
+ // right shape anyway: the two viewers that exist both wrap the result in their own group, one of
10
+ // them explodes the levels apart, and neither wants the other's materials.
11
+ //
12
+ // **No options, deliberately.** Everything a viewer might have asked for — which levels, whether to
13
+ // draw openings, how thick a ground plate is, how far to spread the storeys — is either a filter
14
+ // over the nodes (`nodes.filter`) or a transform on the consumer's own group. Baking any of it in
15
+ // here would make the scene stop being a projection of the Form, and two viewers would then differ
16
+ // by which options they passed rather than by how they draw.
17
+ //
18
+ // Every z is true world z. A plate is a face (`bottom === top`), because Form derives no thickness
19
+ // for the ground and inventing one here would be a paper decision made in the wrong place.
20
+ import { formBodies } from "../core/bodies.js";
21
+ import type { Form, FormSwing } from "../core/derive.js";
22
+ import type { Pt } from "../core/model.js";
23
+ import type { Seg2 } from "../core/vertical.js";
24
+ import { Extent } from "./sheet.js";
25
+
26
+ /** World mm, x east+ / y north+ / z up+. Plain numbers only. */
27
+ export interface ScenePrism {
28
+ /** counter-clockwise plan ring — the winding every Form outline has */
29
+ ring: Pt[];
30
+ /** underside z per vertex. `bottom.length === ring.length` */
31
+ bottom: number[];
32
+ /** upper z per vertex. `top.length === ring.length` */
33
+ top: number[];
34
+ /**
35
+ * True when every `bottom` is one number and every `top` is one number — a right prism.
36
+ * **A fact about the numbers, not a hint about materials.** It is what lets a consumer take the
37
+ * cheap extrude path and know when it must stitch a per-vertex buffer instead (a ramp, a flight).
38
+ */
39
+ level: boolean;
40
+ }
41
+
42
+ /** A polyline at one height. Nothing in Form needs a per-vertex z on a line. */
43
+ export interface SceneLine {
44
+ points: Pt[];
45
+ z: number;
46
+ closed: boolean;
47
+ }
48
+
49
+ /**
50
+ * A place to put a symbol or a word. **It carries no word.** `extent` is the plan bounds of what
51
+ * the mark is about, so the wording a consumer chooses can be sized and offset against it.
52
+ */
53
+ export interface SceneMark {
54
+ x: number;
55
+ y: number;
56
+ z: number;
57
+ extent: { x1: number; y1: number; x2: number; y2: number };
58
+ }
59
+
60
+ export type SceneSubject =
61
+ | "space"
62
+ | "boundary"
63
+ | "opening"
64
+ | "column"
65
+ | "slab"
66
+ | "run"
67
+ | "site"
68
+ | "level";
69
+
70
+ /**
71
+ * The geometric treatment. **Five, and closed.** Anything a consumer branches a *material* on
72
+ * beyond these — glass, selection, theme, storey colour — is read off a word, and words are the
73
+ * consumer's.
74
+ */
75
+ export type SceneRole =
76
+ /** the air a space encloses, floor to ceiling */
77
+ | "volume"
78
+ /** a space or the site read as a horizontal face (`bottom === top`) */
79
+ | "plate"
80
+ /** matter — a wall body, a column, a slab, a tread, a ramp, a leaf */
81
+ | "body"
82
+ /** a line, not a body — a boundary's centreline, the site edge */
83
+ | "edge"
84
+ /** a seat for a symbol or a word */
85
+ | "mark";
86
+
87
+ /** Booleans and free words carried verbatim. No colour, no line type, no wording. */
88
+ export interface SceneFacts {
89
+ /** `FormSpace.void` — a volume with no floor and no matter */
90
+ hollow?: boolean;
91
+ semiOutdoor?: boolean;
92
+ outside?: boolean;
93
+ indoor?: boolean;
94
+ covered?: boolean;
95
+ /** `FormBoundary.air` — does not occlude (a rail, a fence) */
96
+ air?: boolean;
97
+ /** `FormBoundary.derived` — came from contact, not from a declaration */
98
+ derived?: boolean;
99
+ sliding?: boolean;
100
+ /** `FormOpening.style` — a free word, carried and **not read** */
101
+ style?: string;
102
+ /** `FormOpening.name` — likewise */
103
+ name?: string;
104
+ /** `FormColumn.grid` — the drawing name of the intersection it stands on */
105
+ grid?: string;
106
+ areaM2?: number;
107
+ }
108
+
109
+ export interface SceneNode {
110
+ of: SceneSubject;
111
+ role: SceneRole;
112
+ /** the subject's identity in the spelling the rest of `Form` uses */
113
+ ref: string;
114
+ kind?: string;
115
+ /** the authored level this node belongs to, when it has one */
116
+ level?: string;
117
+ /**
118
+ * Place in canonical boundary order — the same meaning as `Mark.written`. **Never an index into
119
+ * `model.boundaries`** (ADR-0041): declaration order is discarded by the canonical form, so
120
+ * indexing by it reads another boundary's attributes and nothing throws.
121
+ */
122
+ written?: { boundary: number; index?: number };
123
+ /** the two spaces a boundary or opening relates */
124
+ pair?: { a: string; b: string };
125
+ /** exactly one of these three, matching `role` */
126
+ solid?: ScenePrism;
127
+ line?: SceneLine;
128
+ mark?: SceneMark;
129
+ /** wall bodies and opening leaves only — the axis, which cannot be read off `ring` */
130
+ centre?: Seg2;
131
+ t?: number;
132
+ /** verbatim, for a viewer that rotates a leaf open */
133
+ swing?: FormSwing;
134
+ facts: SceneFacts;
135
+ }
136
+
137
+ export interface SceneLevel {
138
+ name: string;
139
+ z: number;
140
+ pitch?: number;
141
+ }
142
+
143
+ export interface Scene {
144
+ levels: SceneLevel[];
145
+ /**
146
+ * The level that meets the ground — **the lowest level at or above z 0, not `levels[0]`**. A
147
+ * building with a basement otherwise lands its site and its landscaping on the basement plan.
148
+ * Undefined when nothing stands at or above ground.
149
+ */
150
+ ground?: string;
151
+ nodes: SceneNode[];
152
+ }
153
+
154
+ /** A right prism is one where every underside and every top is the same number. */
155
+ const prism = (ring: Pt[], bottom: number[], top: number[]): ScenePrism => ({
156
+ ring,
157
+ bottom,
158
+ top,
159
+ level: new Set(bottom).size <= 1 && new Set(top).size <= 1,
160
+ });
161
+
162
+ const flat = (ring: Pt[], z0: number, z1: number): ScenePrism =>
163
+ prism(
164
+ ring,
165
+ ring.map(() => z0),
166
+ ring.map(() => z1),
167
+ );
168
+
169
+ /** Drop the keys nobody set, so a node carries facts rather than a row of undefineds. */
170
+ const facts = (o: SceneFacts): SceneFacts => {
171
+ const out: SceneFacts = {};
172
+ for (const [k, v] of Object.entries(o)) if (v !== undefined) (out as Record<string, unknown>)[k] = v;
173
+ return out;
174
+ };
175
+
176
+ /**
177
+ * The scene a `Form` describes.
178
+ *
179
+ * Deterministic and cacheable: the same Form gives the same nodes in the same order, because the
180
+ * order is `Form`'s own and is never re-established here.
181
+ */
182
+ export function sceneOf(form: Form): Scene {
183
+ const nodes: SceneNode[] = [];
184
+ const spaces = new Map(form.spaces.map((s) => [s.path, s]));
185
+ const boundaries = new Map(form.boundaries.map((b) => [b.ref, b]));
186
+ const openings = new Map(form.openings.map((o) => [o.ref, o]));
187
+ const columns = new Map(form.columns.map((c) => [c.ref, c]));
188
+ const runs = new Map(form.runs.map((r) => [r.path, r]));
189
+ const levelZ = new Map(form.levels.map((l) => [l.name, l.z]));
190
+
191
+ // ---- the bodies, straight out of the one enumeration, in its order ----
192
+ for (const body of formBodies(form)) {
193
+ const solid = prism(body.poly, body.bottom, body.top);
194
+ switch (body.of) {
195
+ case "space": {
196
+ const s = spaces.get(body.ref);
197
+ nodes.push({
198
+ of: "space",
199
+ role: "volume",
200
+ ref: body.ref,
201
+ ...(body.kind !== undefined ? { kind: body.kind } : {}),
202
+ ...(s?.level !== undefined ? { level: s.level } : {}),
203
+ solid,
204
+ facts: facts({
205
+ hollow: s?.void,
206
+ semiOutdoor: s?.semiOutdoor,
207
+ outside: s?.outside,
208
+ indoor: s?.indoor,
209
+ covered: s?.covered,
210
+ }),
211
+ });
212
+ break;
213
+ }
214
+ case "boundary": {
215
+ const b = boundaries.get(body.ref);
216
+ nodes.push({
217
+ of: "boundary",
218
+ role: "body",
219
+ ref: body.ref,
220
+ ...(body.kind !== undefined ? { kind: body.kind } : {}),
221
+ ...(b?.level !== undefined ? { level: b.level } : {}),
222
+ ...(b ? { written: { boundary: b.boundary }, pair: { a: b.a, b: b.b } } : {}),
223
+ solid,
224
+ ...(body.centre ? { centre: body.centre } : {}),
225
+ ...(b?.material ? { t: b.material.t } : {}),
226
+ facts: facts({ air: b?.air, derived: b?.derived }),
227
+ });
228
+ break;
229
+ }
230
+ case "opening": {
231
+ const o = openings.get(body.ref);
232
+ nodes.push({
233
+ of: "opening",
234
+ role: "body",
235
+ ref: body.ref,
236
+ ...(body.kind !== undefined ? { kind: body.kind } : {}),
237
+ ...(o?.level !== undefined ? { level: o.level } : {}),
238
+ ...(o ? { written: { boundary: o.boundary, index: o.index }, pair: { a: o.a, b: o.b } } : {}),
239
+ solid,
240
+ ...(body.centre ? { centre: body.centre } : {}),
241
+ ...(o ? { t: o.t } : {}),
242
+ ...(o?.swing ? { swing: o.swing } : {}),
243
+ facts: facts({ sliding: o?.sliding, style: o?.style, name: o?.name }),
244
+ });
245
+ break;
246
+ }
247
+ case "column": {
248
+ const c = columns.get(body.ref);
249
+ nodes.push({
250
+ of: "column",
251
+ role: "body",
252
+ ref: body.ref,
253
+ ...(c?.level !== undefined ? { level: c.level } : {}),
254
+ solid,
255
+ facts: facts({ grid: c?.grid }),
256
+ });
257
+ break;
258
+ }
259
+ case "slab": {
260
+ nodes.push({
261
+ of: "slab",
262
+ role: "body",
263
+ ref: body.ref,
264
+ ...(body.kind !== undefined ? { kind: body.kind } : {}),
265
+ solid,
266
+ facts: {},
267
+ });
268
+ break;
269
+ }
270
+ case "run": {
271
+ const r = runs.get(body.ref);
272
+ nodes.push({
273
+ of: "run",
274
+ role: "body",
275
+ ref: body.ref,
276
+ ...(body.kind !== undefined ? { kind: body.kind } : {}),
277
+ ...(r?.level !== undefined ? { level: r.level } : {}),
278
+ solid,
279
+ facts: {},
280
+ });
281
+ break;
282
+ }
283
+ }
284
+ }
285
+
286
+ // ---- the layers that are not bodies ----
287
+
288
+ // A space read as a face at its own floor, for a viewer that shows storeys as plates rather than
289
+ // as air. `Form` derives no thickness for it, so it is a face and the consumer extrudes.
290
+ for (const s of form.spaces) {
291
+ if (s.level === undefined) continue;
292
+ const z = levelZ.get(s.level);
293
+ if (z === undefined) continue;
294
+ for (const piece of s.outline) {
295
+ nodes.push({
296
+ of: "space",
297
+ role: "plate",
298
+ ref: s.path,
299
+ ...(s.type !== undefined ? { kind: s.type } : {}),
300
+ level: s.level,
301
+ solid: flat(piece, z, z),
302
+ facts: facts({
303
+ hollow: s.void,
304
+ semiOutdoor: s.semiOutdoor,
305
+ outside: s.outside,
306
+ indoor: s.indoor,
307
+ covered: s.covered,
308
+ }),
309
+ });
310
+ }
311
+ }
312
+
313
+ // Every boundary carries a centreline, whether or not it has matter — a relation with nothing in
314
+ // it is still a relation, and a viewer that only meshed bodies would never see one.
315
+ for (const b of form.boundaries) {
316
+ const z = b.level !== undefined ? levelZ.get(b.level) : undefined;
317
+ nodes.push({
318
+ of: "boundary",
319
+ role: "edge",
320
+ ref: b.ref,
321
+ kind: b.kind,
322
+ ...(b.level !== undefined ? { level: b.level } : {}),
323
+ written: { boundary: b.boundary },
324
+ pair: { a: b.a, b: b.b },
325
+ line: {
326
+ points: [
327
+ { x: b.segment.x1, y: b.segment.y1 },
328
+ { x: b.segment.x2, y: b.segment.y2 },
329
+ ],
330
+ z: z ?? 0,
331
+ closed: false,
332
+ },
333
+ facts: facts({ air: b.air, derived: b.derived }),
334
+ });
335
+ }
336
+
337
+ for (const p of form.site) {
338
+ nodes.push({
339
+ of: "site",
340
+ role: "plate",
341
+ ref: p.path,
342
+ solid: flat(p.points, 0, 0),
343
+ facts: { areaM2: p.areaM2 },
344
+ });
345
+ nodes.push({
346
+ of: "site",
347
+ role: "edge",
348
+ ref: p.path,
349
+ line: { points: p.points, z: 0, closed: true },
350
+ facts: { areaM2: p.areaM2 },
351
+ });
352
+ }
353
+
354
+ // A seat per level, for whatever a viewer wants to write beside a storey. The extent is folded,
355
+ // never spread — `Math.min(...pts)` overflows the stack on a model with tens of thousands of
356
+ // solids, and one of the bundled stress models is exactly that.
357
+ for (const l of form.levels) {
358
+ const e = new Extent();
359
+ for (const s of form.spaces) {
360
+ if (s.level !== l.name) continue;
361
+ for (const piece of s.outline) for (const pt of piece) e.see(pt.x, pt.y);
362
+ }
363
+ if (e.empty) continue;
364
+ nodes.push({
365
+ of: "level",
366
+ role: "mark",
367
+ ref: l.name,
368
+ level: l.name,
369
+ mark: {
370
+ x: e.min0,
371
+ y: (e.min1 + e.max1) / 2,
372
+ z: l.z,
373
+ extent: { x1: e.min0, y1: e.min1, x2: e.max0, y2: e.max1 },
374
+ },
375
+ facts: {},
376
+ });
377
+ }
378
+
379
+ const ground = form.levels.filter((l) => l.z >= 0).sort((a, b) => a.z - b.z)[0]?.name;
380
+ return {
381
+ levels: form.levels.map((l) => ({
382
+ name: l.name,
383
+ z: l.z,
384
+ ...(l.pitch !== undefined ? { pitch: l.pitch } : {}),
385
+ })),
386
+ ...(ground !== undefined ? { ground } : {}),
387
+ nodes,
388
+ };
389
+ }