@kensnzk/koyu 0.22.0 → 0.23.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 (54) hide show
  1. package/dist/cli.js +51 -2
  2. package/dist/cli.js.map +1 -1
  3. package/dist/core/model.d.ts +20 -1
  4. package/dist/core/model.d.ts.map +1 -1
  5. package/dist/core/model.js +27 -1
  6. package/dist/core/model.js.map +1 -1
  7. package/dist/core/parse.d.ts.map +1 -1
  8. package/dist/core/parse.js +11 -13
  9. package/dist/core/parse.js.map +1 -1
  10. package/dist/core/poly.d.ts +35 -0
  11. package/dist/core/poly.d.ts.map +1 -1
  12. package/dist/core/poly.js +67 -0
  13. package/dist/core/poly.js.map +1 -1
  14. package/dist/core/section.d.ts +88 -0
  15. package/dist/core/section.d.ts.map +1 -0
  16. package/dist/core/section.js +289 -0
  17. package/dist/core/section.js.map +1 -0
  18. package/dist/draw/axo.d.ts.map +1 -1
  19. package/dist/draw/axo.js +13 -31
  20. package/dist/draw/axo.js.map +1 -1
  21. package/dist/draw/index.d.ts +1 -0
  22. package/dist/draw/index.d.ts.map +1 -1
  23. package/dist/draw/index.js +1 -0
  24. package/dist/draw/index.js.map +1 -1
  25. package/dist/draw/plan.d.ts.map +1 -1
  26. package/dist/draw/plan.js +15 -16
  27. package/dist/draw/plan.js.map +1 -1
  28. package/dist/draw/section.d.ts +23 -0
  29. package/dist/draw/section.d.ts.map +1 -0
  30. package/dist/draw/section.js +168 -0
  31. package/dist/draw/section.js.map +1 -0
  32. package/dist/draw/sheet.d.ts +40 -0
  33. package/dist/draw/sheet.d.ts.map +1 -0
  34. package/dist/draw/sheet.js +93 -0
  35. package/dist/draw/sheet.js.map +1 -0
  36. package/dist/form.d.ts +1 -0
  37. package/dist/form.d.ts.map +1 -1
  38. package/dist/form.js +4 -0
  39. package/dist/form.js.map +1 -1
  40. package/dist/mcp.js +67 -3
  41. package/dist/mcp.js.map +1 -1
  42. package/package.json +1 -1
  43. package/src/cli.ts +57 -1
  44. package/src/core/model.ts +32 -1
  45. package/src/core/parse.ts +13 -12
  46. package/src/core/poly.ts +76 -0
  47. package/src/core/section.ts +360 -0
  48. package/src/draw/axo.ts +13 -29
  49. package/src/draw/index.ts +1 -0
  50. package/src/draw/plan.ts +14 -22
  51. package/src/draw/section.ts +224 -0
  52. package/src/draw/sheet.ts +102 -0
  53. package/src/form.ts +18 -0
  54. package/src/mcp.ts +75 -2
package/src/draw/axo.ts CHANGED
@@ -15,6 +15,7 @@
15
15
 
16
16
  import { columnRect, derive, runPrism } from "../core/derive.js";
17
17
  import { type Model, type Pt } from "../core/model.js";
18
+ import { esc, Extent, INK, openSheet, r2 } from "./sheet.js";
18
19
 
19
20
  export interface AxoOptions {
20
21
  /** 見る向き — 建物のどの隅から見下ろすか (既定 SE) */
@@ -39,8 +40,6 @@ interface Prism {
39
40
  depth: number;
40
41
  }
41
42
 
42
- const INK = "#1f1f1f";
43
- const PAPER = "#faf8f4";
44
43
  /** 地盤面を見せる版の下端・上端 mm — 導出値ではなく紙の側の約束 */
45
44
  const GROUND_Z0 = -400;
46
45
  const GROUND_Z1 = -100;
@@ -135,34 +134,23 @@ export function svgAxo(model: Model, opts: AxoOptions = {}): string {
135
134
 
136
135
  // ---- 投影して奥から描く (画家のアルゴリズム) ----
137
136
  prisms.sort((a, b) => a.depth - b.depth);
138
- // **外接範囲は畳んで取る。**Math.min(...pts) は引数の数がスタックの限界に当たる —
139
- // 開口で割られた壁は区間ごとに一片なので、大きな例では十万点を超える
140
- let minX = Infinity;
141
- let maxX = -Infinity;
142
- let minY = Infinity;
143
- let maxY = -Infinity;
144
- const see = (p: [number, number]): void => {
145
- if (p[0] < minX) minX = p[0];
146
- if (p[0] > maxX) maxX = p[0];
147
- if (p[1] < minY) minY = p[1];
148
- if (p[1] > maxY) maxY = p[1];
149
- };
137
+ // **外接範囲は畳んで取る** (Extent) — 引数を展開すると点の数がスタックの限界に当たる
138
+ const ext = new Extent();
150
139
  for (const pr of prisms) {
151
140
  for (let i = 0; i < pr.poly.length; i++) {
152
- see(proj(pr.poly[i]!, pr.top[i]!));
153
- see(proj(pr.poly[i]!, pr.bottom[i]!));
141
+ const t = proj(pr.poly[i]!, pr.top[i]!);
142
+ const b = proj(pr.poly[i]!, pr.bottom[i]!);
143
+ ext.see(t[0], t[1]);
144
+ ext.see(b[0], b[1]);
154
145
  }
155
146
  }
156
147
  const M = 40;
157
- const W = (maxX - minX) * scale + M * 2;
158
- const H = (maxY - minY) * scale + M * 2;
159
- const sx = (v: number) => (v - minX) * scale + M;
160
- const sy = (v: number) => (v - minY) * scale + M;
161
-
162
- const out: string[] = [
163
- `<svg xmlns="http://www.w3.org/2000/svg" width="${W}" height="${H}" viewBox="0 0 ${W} ${H}" font-family="'Hiragino Sans','Noto Sans JP',sans-serif">`,
164
- `<rect width="${W}" height="${H}" fill="${PAPER}"/>`,
165
- ];
148
+ const W = (ext.max0 - ext.min0) * scale + M * 2;
149
+ const H = (ext.max1 - ext.min1) * scale + M * 2;
150
+ const sx = (v: number) => (v - ext.min0) * scale + M;
151
+ const sy = (v: number) => (v - ext.min1) * scale + M;
152
+
153
+ const out: string[] = openSheet(W, H);
166
154
  const face = (ring: Array<[number, number]>, fill: string, shade: number) => {
167
155
  const d = ring.map(([x, y], i) => `${i === 0 ? "M" : "L"} ${r2(sx(x))} ${r2(sy(y))}`).join(" ");
168
156
  return `<path d="${d} Z" fill="${tint(fill, shade)}" stroke="${INK}" stroke-width="0.35" stroke-opacity="0.5"/>`;
@@ -194,7 +182,6 @@ export function svgAxo(model: Model, opts: AxoOptions = {}): string {
194
182
  }
195
183
  out.push(
196
184
  `<text x="${M - 24}" y="${H - 12}" font-size="11" fill="${INK}">${esc(model.name ?? "Untitled")} — axonometric (from ${dir})</text>`,
197
- `<text x="${W - M + 24}" y="${H - 12}" text-anchor="end" font-size="8.5" fill="#a49b8a">koyu — generated from spaces (floors, roofs, walls, columns, vertical circulation)</text>`,
198
185
  "</svg>",
199
186
  );
200
187
  return out.join("\n") + "\n";
@@ -216,6 +203,3 @@ function tint(hex: string, k: number): string {
216
203
  const ch = [n >> 16, (n >> 8) & 255, n & 255].map((v) => Math.round(Math.min(255, v * k)));
217
204
  return `#${ch.map((v) => v.toString(16).padStart(2, "0")).join("")}`;
218
205
  }
219
-
220
- const r2 = (v: number): number => Math.round(v * 100) / 100;
221
- const esc = (s: string): string => s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
package/src/draw/index.ts CHANGED
@@ -9,3 +9,4 @@
9
9
 
10
10
  export { svgPlan, type PlanOptions } from "./plan.js";
11
11
  export { svgAxo, type AxoOptions } from "./axo.js";
12
+ export { svgSection, svgElevation, type SectionOptions, type ElevationOptions } from "./section.js";
package/src/draw/plan.ts CHANGED
@@ -12,6 +12,7 @@
12
12
  import { band, derive, type PlanEntity } from "../core/derive.js";
13
13
  import { canonicalBoundaryOrder, displayName, polyBounds, type Model, type Pt } from "../core/model.js";
14
14
  import { slopeText } from "../core/vertical.js";
15
+ import { esc, Extent, FAINT, GRID, INK, openSheet, PAPER, ROOM } from "./sheet.js";
15
16
 
16
17
  export interface PlanOptions {
17
18
  level?: string;
@@ -21,12 +22,6 @@ export interface PlanOptions {
21
22
  cut?: number;
22
23
  }
23
24
 
24
- const INK = "#1f1f1f";
25
- const PAPER = "#faf8f4";
26
- const ROOM = "#f1ebdd";
27
- const GRID = "#b5aa94";
28
- const FAINT = "#b3ab9c";
29
-
30
25
  export function svgPlan(model: Model, opts: PlanOptions = {}): string {
31
26
  const level = opts.level ?? Object.keys(model.levels)[0];
32
27
  if (!level) throw new Error("No level is defined");
@@ -47,10 +42,17 @@ export function svgPlan(model: Model, opts: PlanOptions = {}): string {
47
42
  const modelRooms = [...model.spaces.values()].filter((s) => s.rects.length > 0 && s.level === level);
48
43
  const allRects = modelRooms.flatMap((s) => s.rects);
49
44
  const polyPts = [...sitePolys.flatMap((p) => p.points), ...rooms.flatMap((s) => s.outline.flat())];
50
- const minX = Math.min(...allRects.map((r) => r.x1), ...polyPts.map((p) => p.x));
51
- const maxX = Math.max(...allRects.map((r) => r.x2), ...polyPts.map((p) => p.x));
52
- const minY = Math.min(...allRects.map((r) => r.y1), ...polyPts.map((p) => p.y));
53
- const maxY = Math.max(...allRects.map((r) => r.y2), ...polyPts.map((p) => p.y));
45
+ // **畳んで取る** (Extent) — 引数を展開すると、大きな階では点の数がスタックの限界に当たる
46
+ const ext = new Extent();
47
+ for (const r of allRects) {
48
+ ext.see(r.x1, r.y1);
49
+ ext.see(r.x2, r.y2);
50
+ }
51
+ for (const p of polyPts) ext.see(p.x, p.y);
52
+ const minX = ext.min0;
53
+ const maxX = ext.max0;
54
+ const minY = ext.min1;
55
+ const maxY = ext.max1;
54
56
 
55
57
  const M = 84; // 余白 px (通り芯記号ぶん)
56
58
  const W = (maxX - minX) * scale + M * 2;
@@ -63,11 +65,7 @@ export function svgPlan(model: Model, opts: PlanOptions = {}): string {
63
65
  `<line x1="${sx(g.x1)}" y1="${sy(g.y1)}" x2="${sx(g.x2)}" y2="${sy(g.y2)}" stroke="${stroke}" stroke-width="${w}"${dash ? ` stroke-dasharray="${dash}"` : ""}/>`;
64
66
  const fill = (poly: Pt[], c: string) => `<path d="${path2d(poly)}" fill="${c}"/>`;
65
67
 
66
- const parts: string[] = [];
67
- parts.push(
68
- `<svg xmlns="http://www.w3.org/2000/svg" width="${W}" height="${H}" viewBox="0 0 ${W} ${H}" font-family="'Hiragino Sans','Noto Sans JP',sans-serif">`,
69
- );
70
- parts.push(`<rect width="${W}" height="${H}" fill="${PAPER}"/>`);
68
+ const parts: string[] = openSheet(W, H);
71
69
 
72
70
  // 敷地境界線 (一点二点鎖線 — 作図慣習)。所与の形をそのまま引く
73
71
  for (const poly of sitePolys) {
@@ -299,10 +297,7 @@ export function svgPlan(model: Model, opts: PlanOptions = {}): string {
299
297
 
300
298
  // 表題
301
299
  const title = `${model.name ?? "Untitled"} — ${level} plan`;
302
- parts.push(
303
- `<text x="${M - 62}" y="${H - 18}" font-size="12" fill="${INK}">${esc(title)}</text>`,
304
- `<text x="${W - M + 62}" y="${H - 18}" text-anchor="end" font-size="9" fill="#a49b8a">koyu — generated from spaces (wall centrelines, mm)</text>`,
305
- );
300
+ parts.push(`<text x="${M - 62}" y="${H - 18}" font-size="12" fill="${INK}">${esc(title)}</text>`);
306
301
 
307
302
  parts.push("</svg>");
308
303
  return parts.join("\n") + "\n";
@@ -370,6 +365,3 @@ function polyArea(poly: Pt[]): number {
370
365
  return Math.abs(s / 2);
371
366
  }
372
367
 
373
- function esc(s: string): string {
374
- return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
375
- }
@@ -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
+ }
@@ -0,0 +1,102 @@
1
+ // koyu — the page every drawing is put on.
2
+ //
3
+ // **Nothing here decides shape.** This module holds what a sheet of paper is: the two inks, the
4
+ // escaping a piece of text needs before it can sit in markup, the rounding that keeps a path
5
+ // readable, the opening element, and the accumulator that measures how far the drawing reaches.
6
+ //
7
+ // It exists because there are three renderers now. While `plan.ts` and `axo.ts` were the only two,
8
+ // each carrying its own copy of `esc` and its own spelling of `#1f1f1f`, the duplication was
9
+ // cheap; a third copy is the point at which "the paper is the same paper" stops being visible in
10
+ // the code. What is **not** here is anything the three do differently — the world-to-paper mapping
11
+ // (a plan flips y, an axonometric does not), the title block, the margin. Sharing those would mean
12
+ // parameterising them until the shared version says less than the three separate ones did.
13
+
14
+ // The palette. Shared so that a plan and a section of the same building read as one set of
15
+ // drawings — which is the whole use of being able to lay them side by side. Every one of these may
16
+ // change; none of them is on a frozen surface.
17
+
18
+ /** The drawing ink. */
19
+ export const INK = "#1f1f1f";
20
+ /** The paper. */
21
+ export const PAPER = "#faf8f4";
22
+ /** The air of a room, cut open. */
23
+ export const ROOM = "#f1ebdd";
24
+ /** Given lines — the grid, and the level datums that are the grid of a section. */
25
+ export const GRID = "#b5aa94";
26
+ /** A line that is present but not the subject. */
27
+ export const FAINT = "#b3ab9c";
28
+
29
+ /** Two decimal places — enough for a coordinate on paper, and it keeps the path text short. */
30
+ export const r2 = (v: number): number => Math.round(v * 100) / 100;
31
+
32
+ /**
33
+ * Text on its way into markup. Only the three characters that would end the surrounding element —
34
+ * no name from the source is ever put inside an attribute, so quotes need no treatment.
35
+ */
36
+ export const esc = (s: string): string =>
37
+ s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
38
+
39
+ /** The opening element, the paper under the drawing, and the mark in the corner. */
40
+ export function openSheet(w: number, h: number): string[] {
41
+ return [
42
+ `<svg xmlns="http://www.w3.org/2000/svg" width="${w}" height="${h}" viewBox="0 0 ${w} ${h}" font-family="'Hiragino Sans','Noto Sans JP',sans-serif">`,
43
+ `<rect width="${w}" height="${h}" fill="${PAPER}"/>`,
44
+ koyuMark(14, 13, 19),
45
+ ];
46
+ }
47
+
48
+ // The mark, drawn rather than linked. A drawing koyu makes should say so on its face, and an SVG
49
+ // that reaches for a file is a drawing that arrives broken as often as not — so the two paths are
50
+ // here, in the light-mode colours, because the paper is light.
51
+ //
52
+ // The outline is a room in plan: a wall running round, stopping short at the foot to leave a
53
+ // doorway, one partition inside it, and an opening in red. Which is the whole notation in one
54
+ // figure, and the reason it is the mark.
55
+ const MARK_INK = "#171A18";
56
+ const MARK_OPENING = "#A84940";
57
+ const MARK_W = 803.138;
58
+ const MARK_H = 872.069;
59
+ const MARK_X = 224.392;
60
+ const MARK_Y = 171.361;
61
+
62
+ /** The koyu mark, its top-left corner at (x, y) and `size` tall. */
63
+ export function koyuMark(x: number, y: number, size: number): string {
64
+ const k = size / MARK_H;
65
+ const tx = r2(x - MARK_X * k);
66
+ const ty = r2(y - MARK_Y * k);
67
+ return (
68
+ `<g transform="translate(${tx} ${ty}) scale(${r2(k * 1000) / 1000})">` +
69
+ `<path d="M1027.53 171.361L224.461 171.363L224.392 1043.39L361.454 1043.42L361.567 308.689L889.587 308.679L889.581 904.864L566.4 904.876L566.387 524.585L419.01 452.015L419.047 1043.39L1027.51 1043.43L1027.53 171.361Z" fill="${MARK_INK}"/>` +
70
+ `<path d="M707 445.022L811.864 445L811.858 613.697C811.831 648.974 812.209 684.759 811.831 720L707.005 719.933L707 445.022Z" fill="${MARK_OPENING}"/>` +
71
+ `</g>`
72
+ );
73
+ }
74
+
75
+ /** How wide the mark drawn by `openSheet` is, so a caller can keep clear of it. */
76
+ export const MARK_WIDTH = (19 / MARK_H) * MARK_W;
77
+
78
+ /**
79
+ * How far the drawing reaches.
80
+ *
81
+ * **It folds; it does not spread.** `Math.min(...points)` passes one argument per point and a large
82
+ * model runs past the limit on the number of arguments a call may take — a wall split by its
83
+ * openings is one body per interval, so the bundled examples reach past a hundred thousand points.
84
+ */
85
+ export class Extent {
86
+ min0 = Infinity;
87
+ max0 = -Infinity;
88
+ min1 = Infinity;
89
+ max1 = -Infinity;
90
+
91
+ see(a: number, b: number): void {
92
+ if (a < this.min0) this.min0 = a;
93
+ if (a > this.max0) this.max0 = a;
94
+ if (b < this.min1) this.min1 = b;
95
+ if (b > this.max1) this.max1 = b;
96
+ }
97
+
98
+ /** Whether anything was ever seen. */
99
+ get empty(): boolean {
100
+ return this.min0 === Infinity;
101
+ }
102
+ }
package/src/form.ts CHANGED
@@ -36,6 +36,24 @@ export {
36
36
  type PlanSubject,
37
37
  } from "./core/derive.js";
38
38
 
39
+ // The section and the elevation. **They read a `Form` and assemble no matter** — the classifying
40
+ // of what a vertical plane cut and what stands behind it is derivation's, for the same reason the
41
+ // horizontal one is (docs/why/plan-is-not-a-section.md).
42
+ export {
43
+ axisOf,
44
+ defaultLook,
45
+ elevationForm,
46
+ sectionArea,
47
+ sectionForm,
48
+ type FormSection,
49
+ type SectionAxis,
50
+ type SectionClass,
51
+ type SectionEntity,
52
+ type SectionPt,
53
+ type SectionSpec,
54
+ type SectionSubject,
55
+ } from "./core/section.js";
56
+
39
57
  export { TOLERANCES } from "./core/tolerance.js";
40
58
 
41
59
  export type { Slab, SlabKind } from "./core/fabric.js";
package/src/mcp.ts CHANGED
@@ -11,17 +11,20 @@ import { dirname, isAbsolute, relative, resolve, sep } from "node:path";
11
11
  import { daylightInputs } from "./core/light.js";
12
12
  import {
13
13
  DEFAULT_LANGUAGE_VERSION,
14
+ type Edge,
15
+ gridRef,
14
16
  KOYU_VERSION,
15
17
  NEWEST_LANGUAGE_VERSION,
16
18
  polygonAreaM2,
17
19
  SUPPORTED_LANGUAGE_VERSIONS,
18
20
  } from "./core/model.js";
21
+ import { axisOf } from "./core/section.js";
19
22
  import { check } from "./core/diagnose.js";
20
23
  import { siteReport } from "./core/site.js";
21
24
  import { assess } from "./validate/assessment.js";
22
25
  import { createSchematicRegistry, SCHEMATIC_PROFILE_ID } from "./validate/builtin/index.js";
23
26
  import { checkDiagnostics } from "./diagnostics.js";
24
- import { svgPlan } from "./draw/index.js";
27
+ import { svgElevation, svgPlan, svgSection } from "./draw/index.js";
25
28
  import { doorsBetween } from "./graph.js";
26
29
  import {
27
30
  areaM2,
@@ -191,6 +194,22 @@ const str = (v: unknown, name: string): string => {
191
194
  return v;
192
195
  };
193
196
 
197
+ /**
198
+ * A compass word, checked rather than trusted.
199
+ *
200
+ * **A schema is what the tool advertises, not what the server enforces.** A raw `tools/call` can
201
+ * carry anything, and a word that is not one of the four falls through the direction tests to a
202
+ * default — so an unsupported `look` would come back as a plausible drawing of a plane nobody
203
+ * asked for, which is worse than an error.
204
+ */
205
+ const edge = (v: unknown, name: string): Edge => {
206
+ const s = str(v, name);
207
+ if (s !== "N" && s !== "E" && s !== "S" && s !== "W") {
208
+ throw new Error(`${name} is one of N / E / S / W: ${s}`);
209
+ }
210
+ return s;
211
+ };
212
+
194
213
  /** An optional array of attribute keys. Absent is not the same as empty, but both group nothing. */
195
214
  const keys = (v: unknown, name: string): string[] => {
196
215
  if (v === undefined) return [];
@@ -427,6 +446,60 @@ const TOOLS: Record<string, Tool> = {
427
446
  },
428
447
  run: (a) => svgPlan(load(str(a.file, "file")), { level: str(a.level, "level") }),
429
448
  },
449
+ section_svg: {
450
+ description:
451
+ "Generates and returns the section SVG cut at a grid reference (form is generated, not written). The reference names the plane: X3 cuts across the X axis, Y2-600 across the Y axis",
452
+ schema: {
453
+ type: "object",
454
+ properties: {
455
+ ...FILE_PROP,
456
+ at: {
457
+ type: "string",
458
+ description: "Grid reference for the cut — X3, X3+450, Y2-600. A plane on a grid line usually runs along a wall; offset it to cut through the rooms",
459
+ },
460
+ look: {
461
+ type: "string",
462
+ enum: ["N", "E", "S", "W"],
463
+ description: "Direction of view. Must cross the plane: E or W for an X reference, N or S for a Y reference. Defaults to W and N",
464
+ },
465
+ },
466
+ required: ["file", "at"],
467
+ },
468
+ run: (a) => {
469
+ const model = load(str(a.file, "file"));
470
+ const at = str(a.at, "at");
471
+ const g = gridRef(model, at);
472
+ if (!g) {
473
+ throw new Error(
474
+ `Undefined grid reference: ${at} (declared: ${model.grid.X.names.join(" ")} ${model.grid.Y.names.join(" ")})`,
475
+ );
476
+ }
477
+ const look = a.look === undefined ? undefined : edge(a.look, "look");
478
+ if (look && axisOf(look) !== g.axis) {
479
+ throw new Error(
480
+ `look ${look} runs along ${at} rather than across it (an X reference is looked at from E or W, a Y reference from N or S)`,
481
+ );
482
+ }
483
+ return svgSection(model, { axis: g.axis, at: g.coord, atRef: at, ...(look ? { look } : {}) });
484
+ },
485
+ },
486
+ elevation_svg: {
487
+ description:
488
+ "Generates and returns the elevation SVG of one face (form is generated, not written). An elevation is a section whose plane stands outside the mass, so it cuts nothing",
489
+ schema: {
490
+ type: "object",
491
+ properties: {
492
+ ...FILE_PROP,
493
+ face: {
494
+ type: "string",
495
+ enum: ["N", "E", "S", "W"],
496
+ description: "The side the viewer stands on. S is the south elevation, seen from the south",
497
+ },
498
+ },
499
+ required: ["file", "face"],
500
+ },
501
+ run: (a) => svgElevation(load(str(a.file, "file")), { face: edge(a.face, "face") }),
502
+ },
430
503
  canonical_json: {
431
504
  description: "The canonical JSON (machine format — one composed model, byte-stable). The ground for diffing and for external connections",
432
505
  schema: { type: "object", properties: FILE_PROP, required: ["file"] },
@@ -476,7 +549,7 @@ function handle(msg: Json): void {
476
549
  },
477
550
  },
478
551
  instructions:
479
- "Server for koyu, a space-first architectural description. Grasp the building with model_summary, read the original layers with layers, and edit with write_layer. check is the gatekeeper of the build and returns errors tagged layer:line — it guarantees structural consistency only. validate delivers the architectural verdicts, which are a separate and unfrozen surface. doors/light/site/spaces are different questions put to the same description. Form (plan_svg) is generated, never written.",
552
+ "Server for koyu, a space-first architectural description. Grasp the building with model_summary, read the original layers with layers, and edit with write_layer. check is the gatekeeper of the build and returns errors tagged layer:line — it guarantees structural consistency only. validate delivers the architectural verdicts, which are a separate and unfrozen surface. doors/light/site/spaces are different questions put to the same description. Form (the drawings — plan_svg, section_svg, elevation_svg) is generated, never written.",
480
553
  });
481
554
  return;
482
555
  }