@kensnzk/koyu 0.22.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.
Files changed (74) hide show
  1. package/dist/cli.js +51 -2
  2. package/dist/cli.js.map +1 -1
  3. package/dist/core/bodies.d.ts +26 -0
  4. package/dist/core/bodies.d.ts.map +1 -0
  5. package/dist/core/bodies.js +67 -0
  6. package/dist/core/bodies.js.map +1 -0
  7. package/dist/core/model.d.ts +20 -1
  8. package/dist/core/model.d.ts.map +1 -1
  9. package/dist/core/model.js +27 -1
  10. package/dist/core/model.js.map +1 -1
  11. package/dist/core/parse.d.ts.map +1 -1
  12. package/dist/core/parse.js +11 -13
  13. package/dist/core/parse.js.map +1 -1
  14. package/dist/core/poly.d.ts +35 -0
  15. package/dist/core/poly.d.ts.map +1 -1
  16. package/dist/core/poly.js +67 -0
  17. package/dist/core/poly.js.map +1 -1
  18. package/dist/core/section.d.ts +88 -0
  19. package/dist/core/section.d.ts.map +1 -0
  20. package/dist/core/section.js +247 -0
  21. package/dist/core/section.js.map +1 -0
  22. package/dist/draw/axo.d.ts.map +1 -1
  23. package/dist/draw/axo.js +13 -31
  24. package/dist/draw/axo.js.map +1 -1
  25. package/dist/draw/index.d.ts +4 -0
  26. package/dist/draw/index.d.ts.map +1 -1
  27. package/dist/draw/index.js +4 -0
  28. package/dist/draw/index.js.map +1 -1
  29. package/dist/draw/marks.d.ts +126 -0
  30. package/dist/draw/marks.d.ts.map +1 -0
  31. package/dist/draw/marks.js +227 -0
  32. package/dist/draw/marks.js.map +1 -0
  33. package/dist/draw/plan.d.ts.map +1 -1
  34. package/dist/draw/plan.js +117 -133
  35. package/dist/draw/plan.js.map +1 -1
  36. package/dist/draw/scene.d.ts +133 -0
  37. package/dist/draw/scene.d.ts.map +1 -0
  38. package/dist/draw/scene.js +251 -0
  39. package/dist/draw/scene.js.map +1 -0
  40. package/dist/draw/section.d.ts +23 -0
  41. package/dist/draw/section.d.ts.map +1 -0
  42. package/dist/draw/section.js +168 -0
  43. package/dist/draw/section.js.map +1 -0
  44. package/dist/draw/sheet.d.ts +40 -0
  45. package/dist/draw/sheet.d.ts.map +1 -0
  46. package/dist/draw/sheet.js +93 -0
  47. package/dist/draw/sheet.js.map +1 -0
  48. package/dist/draw/written.d.ts +27 -0
  49. package/dist/draw/written.d.ts.map +1 -0
  50. package/dist/draw/written.js +50 -0
  51. package/dist/draw/written.js.map +1 -0
  52. package/dist/form.d.ts +1 -0
  53. package/dist/form.d.ts.map +1 -1
  54. package/dist/form.js +4 -0
  55. package/dist/form.js.map +1 -1
  56. package/dist/mcp.js +67 -3
  57. package/dist/mcp.js.map +1 -1
  58. package/package.json +2 -2
  59. package/src/cli.ts +57 -1
  60. package/src/core/bodies.ts +90 -0
  61. package/src/core/model.ts +32 -1
  62. package/src/core/parse.ts +13 -12
  63. package/src/core/poly.ts +76 -0
  64. package/src/core/section.ts +311 -0
  65. package/src/draw/axo.ts +13 -29
  66. package/src/draw/index.ts +24 -0
  67. package/src/draw/marks.ts +315 -0
  68. package/src/draw/plan.ts +111 -130
  69. package/src/draw/scene.ts +389 -0
  70. package/src/draw/section.ts +224 -0
  71. package/src/draw/sheet.ts +102 -0
  72. package/src/draw/written.ts +64 -0
  73. package/src/form.ts +18 -0
  74. package/src/mcp.ts +75 -2
@@ -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
+ }
@@ -0,0 +1,224 @@
1
+ // koyu — the section and the elevation (ADR-0064)
2
+ //
3
+ // **There is not one rule of shape here.** Which bodies the plane crossed, which stand behind it,
4
+ // where each one lands on the sheet and how far back it is — all of that arrives in the
5
+ // `FormSection` that `sectionForm` returns. This page decides the ink, the line weights, the
6
+ // stacking, the words of the annotation, the scale and the margins, and nothing else. That is the
7
+ // same division `plan.ts` keeps, and it makes this the thinnest of the drawings: it performs no
8
+ // geometry at all.
9
+ //
10
+ // **One renderer, two entry points.** An elevation is a section whose plane misses the mass, so
11
+ // `cut` comes back empty and everything is `beyond`. Writing it as a second renderer would be
12
+ // writing the same painter twice and inviting the two to drift.
13
+
14
+ import { derive, type Form } from "../core/derive.js";
15
+ import { displayName, type Edge, type Model } from "../core/model.js";
16
+ import {
17
+ defaultLook,
18
+ elevationForm,
19
+ sectionForm,
20
+ type FormSection,
21
+ type SectionAxis,
22
+ type SectionEntity,
23
+ type SectionPt,
24
+ } from "../core/section.js";
25
+ import { esc, Extent, FAINT, GRID, INK, openSheet, r2, ROOM } from "./sheet.js";
26
+
27
+ export interface SectionOptions {
28
+ /** which axis the plane is named on. `"X"` means the plane `x = at` */
29
+ axis: SectionAxis;
30
+ /** where the plane sits, world mm */
31
+ at: number;
32
+ /** the grid reference it was named by — it goes on the sheet, so the reader can find the cut */
33
+ atRef?: string;
34
+ /** the direction of view. Defaults to W across an X plane and N across a Y plane */
35
+ look?: Edge;
36
+ /** px per mm (default 0.05, the same as a plan, so the two can be laid side by side) */
37
+ scale?: number;
38
+ }
39
+
40
+ export interface ElevationOptions {
41
+ /** the side the viewer stands on. `S` is the south elevation, seen from the south */
42
+ face: Edge;
43
+ /** px per mm */
44
+ scale?: number;
45
+ }
46
+
47
+ /** What stands behind the plane, in a tone that does not compete with the cut. */
48
+ const BEYOND = "#b8b0a0";
49
+ /** Glazing, cool so that it does not read as matter. */
50
+ const GLASS = "#b9c3c0";
51
+ /** Annotation. */
52
+ const LABEL = "#8a8171";
53
+
54
+ export function svgSection(model: Model, opts: SectionOptions): string {
55
+ const look = opts.look ?? defaultLook(opts.axis);
56
+ const form = derive(model);
57
+ const section = sectionForm(form, {
58
+ axis: opts.axis,
59
+ at: opts.at,
60
+ ...(opts.atRef !== undefined ? { atRef: opts.atRef } : {}),
61
+ look,
62
+ });
63
+ const where = opts.atRef ?? `${opts.axis} ${opts.at}`;
64
+ return sheet(model, form, section, opts.scale, `${model.name ?? "Untitled"} — section at ${where} looking ${look}`);
65
+ }
66
+
67
+ export function svgElevation(model: Model, opts: ElevationOptions): string {
68
+ const form = derive(model);
69
+ const section = elevationForm(form, opts.face);
70
+ return sheet(model, form, section, opts.scale, `${model.name ?? "Untitled"} — ${opts.face} elevation`);
71
+ }
72
+
73
+ /**
74
+ * The stacking order, back to front.
75
+ *
76
+ * What is behind the plane is painted **far first**, so a nearer body covers a further one. That is
77
+ * the painter's algorithm ADR-0026 already accepted as enough for a drawing meant to check with,
78
+ * and here it is stronger than it is for an axonometric: there is one depth axis rather than the
79
+ * sum of three, so the order is exact whenever two bodies do not interpenetrate — and
80
+ * interpenetration in section is a contradiction `check` already reports (HGT01 / HGT02).
81
+ */
82
+ function sheet(
83
+ model: Model,
84
+ form: Form,
85
+ section: FormSection,
86
+ scale: number | undefined,
87
+ heading: string,
88
+ ): string {
89
+ const s = scale ?? 0.05;
90
+
91
+ // **Folded, never spread** — a wall is one body per interval, so a large building runs past the
92
+ // limit on how many arguments a call may take.
93
+ const ext = new Extent();
94
+ for (const e of section.entities) for (const p of e.polygon) ext.see(p.u, p.z);
95
+ if (ext.empty) throw new Error("There is nothing to draw");
96
+ const minU = ext.min0;
97
+ const maxU = ext.max0;
98
+ // **A level with no matter on it still has a datum**, so the storey ladder widens the height of
99
+ // the sheet on its own — leave it out and a roof level declared without spaces falls off it.
100
+ let minZ = ext.min1;
101
+ let maxZ = ext.max1;
102
+ for (const l of form.levels) {
103
+ if (l.z < minZ) minZ = l.z;
104
+ if (l.z > maxZ) maxZ = l.z;
105
+ }
106
+
107
+ const ML = 132; // the left margin carries the level names and their heights
108
+ const MR = 84;
109
+ const MT = 84;
110
+ const MB = 84;
111
+ const W = (maxU - minU) * s + ML + MR;
112
+ const H = (maxZ - minZ) * s + MT + MB;
113
+ const sx = (u: number): number => r2((u - minU) * s + ML);
114
+ const sy = (z: number): number => r2((maxZ - z) * s + MT); // z is up on the page
115
+ const path2d = (poly: SectionPt[]): string =>
116
+ poly.map((p, i) => `${i === 0 ? "M" : "L"} ${sx(p.u)} ${sy(p.z)}`).join(" ") + " Z";
117
+
118
+ const parts: string[] = openSheet(W, H);
119
+
120
+ // ---- the air the plane opened ----
121
+ // Behind everything, because it is not a surface. It is the storey's volume, and it shows only
122
+ // where nothing stands behind the plane to be seen instead.
123
+ for (const e of section.entities) {
124
+ if (e.class !== "cut" || e.of !== "space") continue;
125
+ parts.push(`<path d="${path2d(e.polygon)}" fill="${ROOM}"/>`);
126
+ }
127
+
128
+ // ---- what stands behind the plane, far first ----
129
+ // An opening sits inside the wall it pierces, at the same distance, so it is painted after the
130
+ // walls of its own depth rather than being sorted against them by a tie nobody decided.
131
+ const beyond = section.entities
132
+ .filter((e) => e.class === "beyond")
133
+ .sort((a, b) => b.depth - a.depth || rank(a) - rank(b));
134
+ for (const e of beyond) {
135
+ const face = e.of === "opening" ? GLASS : BEYOND;
136
+ parts.push(
137
+ `<path d="${path2d(e.polygon)}" fill="${face}" stroke="${INK}" stroke-width="0.35" stroke-opacity="0.5"/>`,
138
+ );
139
+ }
140
+
141
+ // ---- the storey ladder ----
142
+ // A level datum is to a section what a grid line is to a plan, so it is drawn as one.
143
+ for (const l of form.levels) {
144
+ if (l.z < minZ - 1 || l.z > maxZ + 1) continue;
145
+ parts.push(
146
+ `<line x1="${ML - 26}" y1="${sy(l.z)}" x2="${W - MR + 26}" y2="${sy(l.z)}" stroke="${GRID}" stroke-width="0.8" stroke-dasharray="7 3 1.5 3"/>`,
147
+ `<text x="${ML - 34}" y="${sy(l.z) - 4}" text-anchor="end" font-size="10" fill="${GRID}">${esc(l.name)}</text>`,
148
+ `<text x="${ML - 34}" y="${sy(l.z) + 9}" text-anchor="end" font-size="8.5" fill="${FAINT}">${l.z >= 0 ? "+" : ""}${l.z}</text>`,
149
+ );
150
+ }
151
+
152
+ // ---- the ground ----
153
+ // **koyu holds no ground level.** `origin elevation:` is the height of model z = 0 in a vertical
154
+ // reference system and says so on its own page — it is not GL, not 地盤面, not 平均地盤面. So this
155
+ // line is a convention of the sheet, exactly like the slab `axo` puts under a building, and the
156
+ // reference page says so. It is drawn at z = 0 because that is where the ground storey sits in
157
+ // every model that does not say otherwise, and because nothing in the source can say otherwise.
158
+ if (minZ - 1 <= 0 && 0 <= maxZ + 1) {
159
+ parts.push(
160
+ `<line x1="0" y1="${sy(0)}" x2="${W}" y2="${sy(0)}" stroke="${INK}" stroke-width="1.2"/>`,
161
+ `<text x="${W - MR + 26}" y="${sy(0) + 14}" text-anchor="end" font-size="8" fill="${LABEL}">z 0</text>`,
162
+ );
163
+ }
164
+
165
+ // ---- the grid, along the sheet ----
166
+ // The axis the plane is named on gets no bubbles — every one of its lines would land on the same
167
+ // point. The other axis is what runs across the sheet, so that is the one that marks it.
168
+ const across = section.axis === "X" ? model.grid.Y : model.grid.X;
169
+ const sign = section.look === "W" || section.look === "N" ? 1 : -1;
170
+ for (const [i, c] of across.coords.entries()) {
171
+ const u = c * sign;
172
+ if (u < minU - 1 || u > maxU + 1) continue;
173
+ parts.push(
174
+ `<line x1="${sx(u)}" y1="${MT - 26}" x2="${sx(u)}" y2="${H - MB + 26}" stroke="${GRID}" stroke-width="0.8" stroke-dasharray="7 3 1.5 3"/>`,
175
+ `<circle cx="${sx(u)}" cy="${MT - 40}" r="11" fill="none" stroke="${GRID}" stroke-width="1"/>`,
176
+ `<text x="${sx(u)}" y="${MT - 36}" text-anchor="middle" font-size="10" fill="${GRID}">${across.names[i]!}</text>`,
177
+ );
178
+ }
179
+
180
+ // ---- what the plane cut ----
181
+ // Solid ink, painted last, exactly as a plan paints its black bands last. There is no operation
182
+ // that paints an opening back out in the paper colour: a wall arrives as the run of intervals its
183
+ // openings split it into, so the hole is in it from the start.
184
+ for (const e of section.entities) {
185
+ if (e.class !== "cut" || e.of === "space" || e.of === "opening") continue;
186
+ parts.push(`<path d="${path2d(e.polygon)}" fill="${INK}"/>`);
187
+ }
188
+ // A leaf the plane cut is not matter in the way a wall is — it reads as the opening it fills.
189
+ for (const e of section.entities) {
190
+ if (e.class !== "cut" || e.of !== "opening") continue;
191
+ parts.push(
192
+ `<path d="${path2d(e.polygon)}" fill="${e.kind === "window" ? GLASS : ROOM}" stroke="${INK}" stroke-width="1"/>`,
193
+ );
194
+ }
195
+
196
+ // ---- the rooms, named ----
197
+ for (const e of section.entities) {
198
+ if (e.class !== "cut" || e.of !== "space") continue;
199
+ const space = model.spaces.get(e.ref);
200
+ if (!space) continue;
201
+ const b = box(e.polygon);
202
+ if ((b.u1 - b.u0) * s < 46 || (b.z1 - b.z0) * s < 22) continue; // no room for the words
203
+ const cx = sx((b.u0 + b.u1) / 2);
204
+ const cy = sy((b.z0 + b.z1) / 2);
205
+ parts.push(
206
+ `<text x="${cx}" y="${cy}" text-anchor="middle" font-size="12" fill="${INK}">${esc(displayName(space))}</text>`,
207
+ `<text x="${cx}" y="${cy + 14}" text-anchor="middle" font-size="9" fill="${LABEL}">h ${Math.round(b.z1 - b.z0)}</text>`,
208
+ );
209
+ }
210
+
211
+ parts.push(`<text x="${ML - 62}" y="${H - 18}" font-size="12" fill="${INK}">${esc(heading)}</text>`, "</svg>");
212
+ return parts.join("\n") + "\n";
213
+ }
214
+
215
+ /** Within one distance, walls before the openings that pierce them. */
216
+ function rank(e: SectionEntity): number {
217
+ return e.of === "opening" ? 1 : 0;
218
+ }
219
+
220
+ function box(poly: SectionPt[]): { u0: number; u1: number; z0: number; z1: number } {
221
+ const ext = new Extent();
222
+ for (const p of poly) ext.see(p.u, p.z);
223
+ return { u0: ext.min0, u1: ext.max0, z0: ext.min1, z1: ext.max1 };
224
+ }