@kensnzk/koyu 0.5.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 (61) hide show
  1. package/.github/workflows/ci.yml +16 -0
  2. package/.zenodo.json +20 -0
  3. package/CITATION.cff +26 -0
  4. package/LICENSE +73 -0
  5. package/NOTICE +2 -0
  6. package/README.ja.md +71 -0
  7. package/README.md +72 -0
  8. package/docs/decisions/0001-notation-v0.md +29 -0
  9. package/docs/decisions/0002-height-and-offsets.md +27 -0
  10. package/docs/decisions/0003-uncounted-divisions.md +36 -0
  11. package/docs/decisions/0004-typical-floors.md +21 -0
  12. package/docs/decisions/0005-zones-and-unions.md +23 -0
  13. package/docs/decisions/0006-voids-and-light.md +31 -0
  14. package/docs/decisions/0007-semi-outdoor-air.md +27 -0
  15. package/docs/decisions/0008-vocabulary-and-level-attr.md +23 -0
  16. package/docs/decisions/0009-site-and-exterior.md +29 -0
  17. package/docs/ifc-coverage.md +82 -0
  18. package/docs/ifcx-notes.md +25 -0
  19. package/docs/img/house-L1.svg +80 -0
  20. package/docs/img/house-L2.svg +53 -0
  21. package/docs/img/mansion-L1.svg +113 -0
  22. package/docs/img/mansion-L10.svg +111 -0
  23. package/docs/img/mansion-L5.svg +160 -0
  24. package/docs/img/office-L1.svg +116 -0
  25. package/docs/img/office-L2.svg +109 -0
  26. package/docs/img/two-rooms.svg +39 -0
  27. package/docs/log/2026-07-22.md +13 -0
  28. package/docs/log/2026-07-23.md +51 -0
  29. package/docs/roadmap.md +55 -0
  30. package/docs/writing-architecture.md +119 -0
  31. package/examples/comparison/README.md +38 -0
  32. package/examples/comparison/gen-ifc4.ts +133 -0
  33. package/examples/comparison/gen-ifcx.ts +134 -0
  34. package/examples/comparison/two-rooms.ifc +152 -0
  35. package/examples/comparison/two-rooms.ifcx +1030 -0
  36. package/examples/comparison/validate-ifc.ts +33 -0
  37. package/examples/house.muro +89 -0
  38. package/examples/mansion.muro +187 -0
  39. package/examples/office.muro +110 -0
  40. package/examples/two-rooms.canonical.json +111 -0
  41. package/examples/two-rooms.muro +22 -0
  42. package/package.json +33 -0
  43. package/spec/notation-v0.md +164 -0
  44. package/spec/vocabulary.md +62 -0
  45. package/src/check.ts +302 -0
  46. package/src/cli.ts +281 -0
  47. package/src/graph.ts +306 -0
  48. package/src/index.ts +8 -0
  49. package/src/light.ts +66 -0
  50. package/src/model.ts +367 -0
  51. package/src/parse.ts +512 -0
  52. package/src/plan.ts +334 -0
  53. package/src/site.ts +82 -0
  54. package/test/core.test.ts +171 -0
  55. package/test/design2.test.ts +97 -0
  56. package/test/house.test.ts +74 -0
  57. package/test/mansion.test.ts +99 -0
  58. package/test/office.test.ts +71 -0
  59. package/test/uncounted.test.ts +120 -0
  60. package/test/vertical.test.ts +75 -0
  61. package/tsconfig.json +13 -0
package/src/plan.ts ADDED
@@ -0,0 +1,334 @@
1
+ // koyu v0 — 平面図の生成
2
+ // ソースに形は無い。形は必要になった時にルールから生成する。まず平面図、その後に三次元。
3
+ // 壁は「二つの空間の境界」から導かれて現れる — 壁を描く操作はどこにも無い。
4
+
5
+ import { placeBand, placeOpening, segmentsFor, type Segment } from "./graph.js";
6
+ import {
7
+ areaM2,
8
+ displayName,
9
+ isSemiOutdoor,
10
+ type Boundary,
11
+ type Model,
12
+ type Opening,
13
+ } from "./model.js";
14
+
15
+ export interface PlanOptions {
16
+ level?: string;
17
+ /** px per mm */
18
+ scale?: number;
19
+ }
20
+
21
+ const WALL_DEFAULT_T = 100;
22
+ const INK = "#1f1f1f";
23
+ const PAPER = "#faf8f4";
24
+ const ROOM = "#f1ebdd";
25
+ const GRID = "#b5aa94";
26
+
27
+ export function svgPlan(model: Model, opts: PlanOptions = {}): string {
28
+ const level = opts.level ?? Object.keys(model.levels)[0];
29
+ if (!level) throw new Error("レベルが定義されていません");
30
+ const scale = opts.scale ?? 0.05;
31
+
32
+ const rooms = [...model.spaces.values()].filter(
33
+ (s) => s.rects.length > 0 && s.level === level,
34
+ );
35
+ if (rooms.length === 0) throw new Error(`レベル ${level} に領域を持つ空間がありません`);
36
+
37
+ const allRects = rooms.flatMap((s) => s.rects);
38
+ const minX = Math.min(...allRects.map((r) => r.x1));
39
+ const maxX = Math.max(...allRects.map((r) => r.x2));
40
+ const minY = Math.min(...allRects.map((r) => r.y1));
41
+ const maxY = Math.max(...allRects.map((r) => r.y2));
42
+
43
+ const M = 84; // 余白 px (通り芯記号ぶん)
44
+ const W = (maxX - minX) * scale + M * 2;
45
+ const H = (maxY - minY) * scale + M * 2;
46
+ const sx = (x: number) => (x - minX) * scale + M;
47
+ const sy = (y: number) => (maxY - y) * scale + M;
48
+
49
+ const parts: string[] = [];
50
+ parts.push(
51
+ `<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">`,
52
+ );
53
+ parts.push(`<rect width="${W}" height="${H}" fill="${PAPER}"/>`);
54
+
55
+ // 空間の面 (合併の各矩形。同色・輪郭なしなのでL字は一体に見える)
56
+ // 半屋外 (外部にopen/railで接する空間) は淡く塗り分け、屋外であることが図から読めるように
57
+ for (const s of rooms) {
58
+ const isVoid = s.type === "void";
59
+ const semi = isSemiOutdoor(model, s);
60
+ for (const r of s.rects) {
61
+ parts.push(
62
+ `<rect x="${sx(r.x1)}" y="${sy(r.y2)}" width="${(r.x2 - r.x1) * scale}" height="${(r.y2 - r.y1) * scale}" fill="${isVoid ? PAPER : semi ? "#f8f5ec" : ROOM}"/>`,
63
+ );
64
+ if (isVoid) {
65
+ // 吹抜け: 破線の対角線 (作図慣習)
66
+ parts.push(
67
+ `<line x1="${sx(r.x1)}" y1="${sy(r.y1)}" x2="${sx(r.x2)}" y2="${sy(r.y2)}" stroke="#b3ab9c" stroke-width="0.8" stroke-dasharray="6 4"/>`,
68
+ `<line x1="${sx(r.x1)}" y1="${sy(r.y2)}" x2="${sx(r.x2)}" y2="${sy(r.y1)}" stroke="#b3ab9c" stroke-width="0.8" stroke-dasharray="6 4"/>`,
69
+ );
70
+ }
71
+ }
72
+ }
73
+
74
+ // 数えない分節 (area): 床材の切替など。面積にもグラフにも現れない — 淡い面と破線で示す
75
+ for (const s of rooms) {
76
+ for (const a of s.areas) {
77
+ const r = a.rect;
78
+ parts.push(
79
+ `<rect x="${sx(r.x1)}" y="${sy(r.y2)}" width="${(r.x2 - r.x1) * scale}" height="${(r.y2 - r.y1) * scale}" fill="#e7dfcc" fill-opacity="0.55" stroke="#b3ab9c" stroke-width="0.8" stroke-dasharray="4 3"/>`,
80
+ );
81
+ const label = [a.attrs["name"], a.attrs["floor"]]
82
+ .filter((v): v is string => typeof v === "string")
83
+ .join(" ・ ");
84
+ if (label) {
85
+ parts.push(
86
+ `<text x="${sx(r.x1) + 6}" y="${sy(r.y2) + 12}" font-size="8.5" fill="#8a8171">${esc(label)}</text>`,
87
+ );
88
+ }
89
+ }
90
+ }
91
+
92
+ // 通り芯
93
+ for (const [i, x] of model.grid.X.coords.entries()) {
94
+ if (x < minX - 1 || x > maxX + 1) continue;
95
+ const name = model.grid.X.names[i]!;
96
+ parts.push(
97
+ `<line x1="${sx(x)}" y1="${M - 26}" x2="${sx(x)}" y2="${H - M + 26}" stroke="${GRID}" stroke-width="0.8" stroke-dasharray="7 3 1.5 3"/>`,
98
+ `<circle cx="${sx(x)}" cy="${M - 40}" r="11" fill="none" stroke="${GRID}" stroke-width="1"/>`,
99
+ `<text x="${sx(x)}" y="${M - 36}" text-anchor="middle" font-size="10" fill="${GRID}">${name}</text>`,
100
+ );
101
+ }
102
+ for (const [i, y] of model.grid.Y.coords.entries()) {
103
+ if (y < minY - 1 || y > maxY + 1) continue;
104
+ const name = model.grid.Y.names[i]!;
105
+ parts.push(
106
+ `<line x1="${M - 26}" y1="${sy(y)}" x2="${W - M + 26}" y2="${sy(y)}" stroke="${GRID}" stroke-width="0.8" stroke-dasharray="7 3 1.5 3"/>`,
107
+ `<circle cx="${M - 40}" cy="${sy(y)}" r="11" fill="none" stroke="${GRID}" stroke-width="1"/>`,
108
+ `<text x="${M - 40}" y="${sy(y) + 3.5}" text-anchor="middle" font-size="10" fill="${GRID}">${name}</text>`,
109
+ );
110
+ }
111
+
112
+ // 壁 (境界から生成)。このレベルに触れる境界だけを描く
113
+ const placedOpenings: Array<{ b: Boundary; o: Opening; seg: Segment; cx: number; cy: number }> = [];
114
+ for (const b of model.boundaries) {
115
+ const onLevel = [b.a, b.b].some((p) => model.spaces.get(p)?.level === level);
116
+ if (!onLevel) continue;
117
+ if (b.kind === "open") {
118
+ // 開放的な分節: 壁は無いが構成の線として破線で示す (基本計画の抽象度)
119
+ for (const seg of segmentsFor(model, b)) {
120
+ parts.push(
121
+ `<line x1="${sx(seg.x1)}" y1="${sy(seg.y1)}" x2="${sx(seg.x2)}" y2="${sy(seg.y2)}" stroke="#b3ab9c" stroke-width="1" stroke-dasharray="6 4"/>`,
122
+ );
123
+ }
124
+ continue;
125
+ }
126
+ if (b.kind !== "wall") continue;
127
+ if (b.air) {
128
+ // 遮蔽しない物 (手すり・柵 = spec語彙): 細実線 —
129
+ // 壁の黒帯と描き分けることで「囲われていない」ことが図から読める
130
+ for (const seg of segmentsFor(model, b)) {
131
+ parts.push(
132
+ `<line x1="${sx(seg.x1)}" y1="${sy(seg.y1)}" x2="${sx(seg.x2)}" y2="${sy(seg.y2)}" stroke="${INK}" stroke-width="1.4"/>`,
133
+ );
134
+ }
135
+ // 柵の扉 (門扉など): 線を切って軌跡を描く
136
+ for (const o of b.openings) {
137
+ if (o.kind !== "door") continue;
138
+ const placed = placeOpening(model, b, o);
139
+ if (!("error" in placed)) {
140
+ parts.push(bandRect(placed.segment, o.w, placed.cx, placed.cy, 120, scale, sx, sy, PAPER));
141
+ parts.push(doorSwing(model, b, o, placed.segment, placed.cx, placed.cy, scale, sx, sy));
142
+ }
143
+ }
144
+ continue;
145
+ }
146
+ const t = b.t ?? WALL_DEFAULT_T;
147
+ for (const seg of segmentsFor(model, b)) {
148
+ parts.push(wallRect(seg, t, scale, sx, sy));
149
+ }
150
+ // 数えない分節 (seg): 壁材が途中から変わる区間 — 色調を変えて示す
151
+ for (const g of b.segs) {
152
+ const placed = placeBand(model, b, g, "seg");
153
+ if (!("error" in placed)) {
154
+ parts.push(bandRect(placed.segment, g.w, placed.cx, placed.cy, t, scale, sx, sy, "#77716a"));
155
+ const spec = g.attrs["spec"];
156
+ if (typeof spec === "string") {
157
+ const horizontal = placed.segment.horizontal;
158
+ parts.push(
159
+ `<text x="${sx(placed.cx) + (horizontal ? 0 : 8)}" y="${sy(placed.cy) + (horizontal ? -7 : 3)}" text-anchor="${horizontal ? "middle" : "start"}" font-size="8" fill="#77716a">${esc(spec)}</text>`,
160
+ );
161
+ }
162
+ }
163
+ }
164
+ for (const o of b.openings) {
165
+ const placed = placeOpening(model, b, o);
166
+ if (!("error" in placed && placed.error) && "segment" in placed) {
167
+ placedOpenings.push({ b, o, seg: placed.segment, cx: placed.cx, cy: placed.cy });
168
+ }
169
+ }
170
+ }
171
+
172
+ // 開口 (壁を欠き取り、扉は吊元と軌跡を描く)
173
+ for (const { b, o, seg, cx, cy } of placedOpenings) {
174
+ const t = (b.t ?? WALL_DEFAULT_T) + 30; // 欠き取りは少し深く
175
+ parts.push(bandRect(seg, o.w, cx, cy, t, scale, sx, sy, PAPER));
176
+ if (o.kind === "door") {
177
+ parts.push(doorSwing(model, b, o, seg, cx, cy, scale, sx, sy));
178
+ } else {
179
+ // 窓: 芯線一本
180
+ const wpx = o.w * scale;
181
+ if (seg.horizontal) {
182
+ parts.push(
183
+ `<line x1="${sx(cx) - wpx / 2}" y1="${sy(cy)}" x2="${sx(cx) + wpx / 2}" y2="${sy(cy)}" stroke="${INK}" stroke-width="1"/>`,
184
+ );
185
+ } else {
186
+ parts.push(
187
+ `<line x1="${sx(cx)}" y1="${sy(cy) - wpx / 2}" x2="${sx(cx)}" y2="${sy(cy) + wpx / 2}" stroke="${INK}" stroke-width="1"/>`,
188
+ );
189
+ }
190
+ }
191
+ }
192
+
193
+ // 空間ラベル (最大の矩形の中心に置く)
194
+ for (const s of rooms) {
195
+ const r = [...s.rects].sort(
196
+ (a, b) => (b.x2 - b.x1) * (b.y2 - b.y1) - (a.x2 - a.x1) * (a.y2 - a.y1),
197
+ )[0]!;
198
+ const cx = sx((r.x1 + r.x2) / 2);
199
+ const cy = sy((r.y1 + r.y2) / 2);
200
+ const sub =
201
+ s.type === "void"
202
+ ? "吹抜け"
203
+ : `${esc(s.type)} ・ ${areaM2(s)}㎡${isSemiOutdoor(model, s) ? " ・ 半屋外" : ""}`;
204
+ parts.push(
205
+ `<text x="${cx}" y="${cy - 4}" text-anchor="middle" font-size="14" fill="${INK}">${esc(displayName(s))}</text>`,
206
+ `<text x="${cx}" y="${cy + 13}" text-anchor="middle" font-size="10" fill="#8a8171">${sub}</text>`,
207
+ `<text x="${cx}" y="${cy + 27}" text-anchor="middle" font-size="8.5" fill="#b3ab9c">${esc(s.path)}</text>`,
208
+ );
209
+ }
210
+
211
+ // 下階の平面への「上部吹抜け」表示 (作図慣習)
212
+ for (const b of model.boundaries) {
213
+ if (b.kind !== "void") continue;
214
+ const sa = model.spaces.get(b.a);
215
+ const sb = model.spaces.get(b.b);
216
+ if (!sa?.level || !sb?.level) continue;
217
+ const za = model.levels[sa.level]?.z;
218
+ const zb = model.levels[sb.level]?.z;
219
+ if (za === undefined || zb === undefined) continue;
220
+ const lower = za < zb ? sa : sb;
221
+ const upper = za < zb ? sb : sa;
222
+ if (lower.level !== level) continue;
223
+ for (const r of upper.rects) {
224
+ parts.push(
225
+ `<rect x="${sx(r.x1)}" y="${sy(r.y2)}" width="${(r.x2 - r.x1) * scale}" height="${(r.y2 - r.y1) * scale}" fill="none" stroke="#b3ab9c" stroke-width="0.8" stroke-dasharray="6 4"/>`,
226
+ `<text x="${sx((r.x1 + r.x2) / 2)}" y="${sy((r.y1 + r.y2) / 2) + 40}" text-anchor="middle" font-size="9" fill="#b3ab9c">上部吹抜け</text>`,
227
+ );
228
+ }
229
+ }
230
+
231
+ // 表題
232
+ const title = `${model.name ?? "無題"} — ${level} 平面`;
233
+ parts.push(
234
+ `<text x="${M - 62}" y="${H - 18}" font-size="12" fill="${INK}">${esc(title)}</text>`,
235
+ `<text x="${W - M + 62}" y="${H - 18}" text-anchor="end" font-size="9" fill="#a49b8a">koyu v0 — 空間から生成 (壁芯・mm)</text>`,
236
+ );
237
+
238
+ parts.push("</svg>");
239
+ return parts.join("\n") + "\n";
240
+ }
241
+
242
+ function wallRect(
243
+ seg: Segment,
244
+ t: number,
245
+ scale: number,
246
+ sx: (x: number) => number,
247
+ sy: (y: number) => number,
248
+ ): string {
249
+ if (seg.horizontal) {
250
+ const x = sx(seg.x1);
251
+ const y = sy(seg.y1 + t / 2);
252
+ return `<rect x="${x}" y="${y}" width="${(seg.x2 - seg.x1) * scale}" height="${t * scale}" fill="${INK}"/>`;
253
+ }
254
+ const x = sx(seg.x1 - t / 2);
255
+ const y = sy(seg.y2);
256
+ return `<rect x="${x}" y="${y}" width="${t * scale}" height="${(seg.y2 - seg.y1) * scale}" fill="${INK}"/>`;
257
+ }
258
+
259
+ function bandRect(
260
+ seg: Segment,
261
+ w: number,
262
+ cx: number,
263
+ cy: number,
264
+ t: number,
265
+ scale: number,
266
+ sx: (x: number) => number,
267
+ sy: (y: number) => number,
268
+ fill: string,
269
+ ): string {
270
+ if (seg.horizontal) {
271
+ return `<rect x="${sx(cx - w / 2)}" y="${sy(cy + t / 2)}" width="${w * scale}" height="${t * scale}" fill="${fill}"/>`;
272
+ }
273
+ return `<rect x="${sx(cx + t / 2 - t)}" y="${sy(cy + w / 2)}" width="${t * scale}" height="${w * scale}" fill="${fill}"/>`;
274
+ }
275
+
276
+ /** 扉: 吊元から開いた軌跡 (1/4円)。吊り込み側は「先に書いた空間」= boundary.a 側 */
277
+ function doorSwing(
278
+ model: Model,
279
+ b: Boundary,
280
+ o: Opening,
281
+ seg: Segment,
282
+ cx: number,
283
+ cy: number,
284
+ scale: number,
285
+ sx: (x: number) => number,
286
+ sy: (y: number) => number,
287
+ ): string {
288
+ // 開く側の空間: swing:a/b の指定、既定はa側 (領域を持つ方)。合併なら扉に最も近い矩形へ開く
289
+ const sa = model.spaces.get(b.a);
290
+ const sb = model.spaces.get(b.b);
291
+ let into: typeof sa;
292
+ if (o.swing === "a") into = sa;
293
+ else if (o.swing === "b") into = sb;
294
+ else into = sa && sa.rects.length > 0 ? sa : sb;
295
+ if (!into || into.rects.length === 0) return "";
296
+ const dist = (rc: { x1: number; y1: number; x2: number; y2: number }) =>
297
+ ((rc.x1 + rc.x2) / 2 - cx) ** 2 + ((rc.y1 + rc.y2) / 2 - cy) ** 2;
298
+ const r = [...into.rects].sort((p, q) => dist(p) - dist(q))[0]!;
299
+ const c = { x: (r.x1 + r.x2) / 2, y: (r.y1 + r.y2) / 2 };
300
+
301
+ // 世界座標: 吊元 hinge (hinge:W/E/S/N — 既定は始端側)、軌跡は hinge を中心とする1/4円
302
+ let hinge: { x: number; y: number };
303
+ let along: { x: number; y: number };
304
+ let inward: { x: number; y: number };
305
+ if (seg.horizontal) {
306
+ const fromEast = o.hinge === "E";
307
+ hinge = { x: fromEast ? cx + o.w / 2 : cx - o.w / 2, y: cy };
308
+ along = { x: fromEast ? -1 : 1, y: 0 };
309
+ inward = { x: 0, y: c.y > cy ? 1 : -1 };
310
+ } else {
311
+ const fromNorth = o.hinge === "N";
312
+ hinge = { x: cx, y: fromNorth ? cy + o.w / 2 : cy - o.w / 2 };
313
+ along = { x: 0, y: fromNorth ? -1 : 1 };
314
+ inward = { x: c.x > cx ? 1 : -1, y: 0 };
315
+ }
316
+ const leafEnd = { x: hinge.x + inward.x * o.w, y: hinge.y + inward.y * o.w };
317
+ const gapEnd = { x: hinge.x + along.x * o.w, y: hinge.y + along.y * o.w };
318
+
319
+ const p1 = { x: sx(leafEnd.x), y: sy(leafEnd.y) };
320
+ const p2 = { x: sx(gapEnd.x), y: sy(gapEnd.y) };
321
+ const ph = { x: sx(hinge.x), y: sy(hinge.y) };
322
+ const rad = o.w * scale;
323
+ // 掃引方向: 外積で決める
324
+ const crossZ = (p1.x - ph.x) * (p2.y - ph.y) - (p1.y - ph.y) * (p2.x - ph.x);
325
+ const sweep = crossZ > 0 ? 1 : 0;
326
+ return (
327
+ `<line x1="${ph.x}" y1="${ph.y}" x2="${p1.x}" y2="${p1.y}" stroke="${INK}" stroke-width="1.4"/>` +
328
+ `<path d="M ${p1.x} ${p1.y} A ${rad} ${rad} 0 0 ${sweep} ${p2.x} ${p2.y}" fill="none" stroke="${INK}" stroke-width="0.7" stroke-dasharray="3 2.5"/>`
329
+ );
330
+ }
331
+
332
+ function esc(s: string): string {
333
+ return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
334
+ }
package/src/site.ts ADDED
@@ -0,0 +1,82 @@
1
+ // koyu — 敷地の問い (MUN-149/150)
2
+ // 敷地面積・接道・建蔽率・容積率を、宣言ではなく構成から導出する。
3
+ // 敷地 = site:1 属性を持つゾーン。地上の外部空間 (庭・アプローチ) はL1上の実在の空間として
4
+ // 建物の周りをタイルし、道路・隣地は /out を割った複数のexterior空間 (道路は road:幅員)。
5
+
6
+ import { segmentLength, segmentsFor } from "./graph.js";
7
+ import {
8
+ areaM2,
9
+ isSemiOutdoor,
10
+ unionAreaM2,
11
+ type Model,
12
+ type Space,
13
+ type Zone,
14
+ } from "./model.js";
15
+
16
+ export interface RoadFrontage {
17
+ road: Space;
18
+ /** 幅員 mm (road:属性) */
19
+ width: number;
20
+ /** 接道長 mm (導出) */
21
+ frontage: number;
22
+ }
23
+
24
+ export interface SiteReport {
25
+ siteZone?: Zone;
26
+ /** 宣言された敷地面積 m² (zoneのarea:属性 — 測量値) */
27
+ declaredArea?: number;
28
+ /** 導出された敷地面積 m² (敷地内空間+建物水平投影の合併) */
29
+ derivedArea: number;
30
+ /** 建築面積 m² (屋内空間の水平投影の合併 — 庇・バルコニーの算入は粗い) */
31
+ footprint: number;
32
+ /** 延べ面積 m² (屋内床面積の合計) */
33
+ totalFloor: number;
34
+ roads: RoadFrontage[];
35
+ }
36
+
37
+ export function siteReport(model: Model): SiteReport {
38
+ const siteZone = [...model.zones.values()].find((z) => z.attrs["site"] === 1);
39
+ const declared = siteZone?.attrs["area"];
40
+
41
+ const spaces = [...model.spaces.values()];
42
+ const indoor = spaces.filter(
43
+ (s) =>
44
+ s.rects.length > 0 && s.level && s.type !== "void" && s.type !== "exterior" &&
45
+ !isSemiOutdoor(model, s),
46
+ );
47
+ const siteChildren = siteZone
48
+ ? spaces.filter((s) => s.path.startsWith(siteZone.path + "/") && s.rects.length > 0)
49
+ : [];
50
+
51
+ const derivedArea = unionAreaM2([
52
+ ...siteChildren.flatMap((s) => s.rects),
53
+ ...indoor.flatMap((s) => s.rects),
54
+ ]);
55
+ const footprint = unionAreaM2(indoor.flatMap((s) => s.rects));
56
+ const totalFloor =
57
+ Math.round(indoor.reduce((sum, s) => sum + (areaM2(s) ?? 0), 0) * 100) / 100;
58
+
59
+ // 接道: road:幅員 を持つexterior空間ごとに、敷地 (siteゾーン配下の空間) との境界線分の長さを合算。
60
+ // 建物の外壁が道路に面していても、それは接道ではない
61
+ const roads: RoadFrontage[] = [];
62
+ for (const road of spaces) {
63
+ if (road.type !== "exterior" || typeof road.attrs["road"] !== "number") continue;
64
+ let frontage = 0;
65
+ for (const b of model.boundaries) {
66
+ const otherPath = b.a === road.path ? b.b : b.b === road.path ? b.a : undefined;
67
+ if (!otherPath) continue;
68
+ if (!siteZone || !otherPath.startsWith(siteZone.path + "/")) continue;
69
+ for (const seg of segmentsFor(model, b)) frontage += segmentLength(seg);
70
+ }
71
+ roads.push({ road, width: road.attrs["road"], frontage: Math.round(frontage) });
72
+ }
73
+
74
+ return {
75
+ ...(siteZone ? { siteZone } : {}),
76
+ ...(typeof declared === "number" ? { declaredArea: declared } : {}),
77
+ derivedArea,
78
+ footprint,
79
+ totalFloor,
80
+ roads,
81
+ };
82
+ }
@@ -0,0 +1,171 @@
1
+ import assert from "node:assert/strict";
2
+ import { readFileSync } from "node:fs";
3
+ import { test } from "node:test";
4
+ import { fileURLToPath } from "node:url";
5
+ import { check } from "../src/check.js";
6
+ import { doorsBetween, neighbors, placeOpening } from "../src/graph.js";
7
+ import { areaM2, SourceError, toCanonical } from "../src/model.js";
8
+ import { parse, tokenize } from "../src/parse.js";
9
+ import { svgPlan } from "../src/plan.js";
10
+
11
+ const examplePath = fileURLToPath(new URL("../examples/two-rooms.muro", import.meta.url));
12
+ const exampleSrc = readFileSync(examplePath, "utf8");
13
+
14
+ test("二室一扉が読める", () => {
15
+ const m = parse(exampleSrc);
16
+ assert.equal(m.name, "二室一扉");
17
+ assert.equal(m.spaces.size, 3);
18
+ assert.equal(m.boundaries.length, 3);
19
+ const a = m.spaces.get("/L1/a")!;
20
+ assert.deepEqual(a.rects, [{ x1: 0, y1: 0, x2: 3600, y2: 4500 }]);
21
+ assert.equal(a.level, "L1");
22
+ assert.equal(areaM2(a), 16.2);
23
+ assert.equal(m.spaces.get("/out")!.rects.length, 0);
24
+ });
25
+
26
+ test("グラフへの問い: 扉をいくつ通るか", () => {
27
+ const m = parse(exampleSrc);
28
+ assert.equal(doorsBetween(m, "/L1/a", "/L1/b")!.doors, 1);
29
+ assert.equal(doorsBetween(m, "/L1/b", "/out")!.doors, 1);
30
+ const aOut = doorsBetween(m, "/L1/a", "/out")!;
31
+ assert.equal(aOut.doors, 2); // a→b→out。a|outの境界に扉は無い
32
+ assert.deepEqual(aOut.path, ["/L1/a", "/L1/b", "/out"]);
33
+ assert.equal(neighbors(m, "/L1/a").length, 2);
34
+ });
35
+
36
+ test("開口は境界線分の上に配置される", () => {
37
+ const m = parse(exampleSrc);
38
+ const ab = m.boundaries.find((b) => b.b === "/L1/b")!;
39
+ const placed = placeOpening(m, ab, ab.openings[0]!);
40
+ assert.ok("segment" in placed);
41
+ if ("segment" in placed) {
42
+ assert.equal(placed.cx, 3600);
43
+ assert.equal(placed.cy, 2250);
44
+ }
45
+ const bOut = m.boundaries.find((b) => b.a === "/L1/b" && b.b === "/out")!;
46
+ const genkan = placeOpening(m, bOut, bOut.openings[0]!);
47
+ assert.ok("segment" in genkan);
48
+ if ("segment" in genkan) {
49
+ assert.equal(genkan.cy, 0); // S辺
50
+ assert.equal(genkan.cx, 5400);
51
+ }
52
+ });
53
+
54
+ test("整合チェックが通る", () => {
55
+ const m = parse(exampleSrc);
56
+ const r = check(m);
57
+ assert.deepEqual(r.errors, []);
58
+ assert.deepEqual(r.warnings, []);
59
+ });
60
+
61
+ test("外部境界の扉は辺の指定がなければ曖昧としてエラー", () => {
62
+ const m = parse(exampleSrc.replace("edge:S ", ""));
63
+ const r = check(m);
64
+ assert.ok(r.errors.some((e) => e.includes("複数")));
65
+ });
66
+
67
+ test("領域の重なりはエラー", () => {
68
+ const m = parse(`
69
+ grid X 0 3600 7200
70
+ grid Y 0 4500
71
+ level L1 0
72
+ space /L1/a room X1..X3 Y1..Y2
73
+ space /L1/b room X2..X3 Y1..Y2
74
+ `);
75
+ const r = check(m);
76
+ assert.ok(r.errors.some((e) => e.includes("重なって")));
77
+ });
78
+
79
+ test("接していない空間の境界はエラー", () => {
80
+ const m = parse(`
81
+ grid X 0 3600 7200 10800 14400
82
+ grid Y 0 4500
83
+ level L1 0
84
+ space /L1/a room X1..X2 Y1..Y2
85
+ space /L1/c room X3..X4 Y1..Y2
86
+ boundary /L1/a /L1/c t:120
87
+ `);
88
+ const r = check(m);
89
+ assert.ok(r.errors.some((e) => e.includes("接していない")));
90
+ });
91
+
92
+ test("接しているのに境界が無ければ警告", () => {
93
+ const m = parse(`
94
+ grid X 0 3600 7200
95
+ grid Y 0 4500
96
+ level L1 0
97
+ space /L1/a room X1..X2 Y1..Y2
98
+ space /L1/b room X2..X3 Y1..Y2
99
+ `);
100
+ const r = check(m);
101
+ assert.ok(r.warnings.some((w) => w.includes("宣言されていません")));
102
+ });
103
+
104
+ test("開口が線分より広ければエラー", () => {
105
+ const m = parse(`
106
+ grid X 0 3600 7200
107
+ grid Y 0 4500
108
+ level L1 0
109
+ space /L1/a room X1..X2 Y1..Y2
110
+ space /L1/b room X2..X3 Y1..Y2
111
+ boundary /L1/a /L1/b t:120
112
+ door w:99999
113
+ `);
114
+ const r = check(m);
115
+ assert.ok(r.errors.some((e) => e.includes("超えて")));
116
+ });
117
+
118
+ test("open境界は扉なしで通れる", () => {
119
+ const m = parse(`
120
+ grid X 0 3600 7200
121
+ grid Y 0 4500
122
+ level L1 0
123
+ space /L1/a room X1..X2 Y1..Y2
124
+ space /L1/b room X2..X3 Y1..Y2
125
+ boundary /L1/a /L1/b type:open
126
+ `);
127
+ assert.equal(doorsBetween(m, "/L1/a", "/L1/b")!.doors, 0);
128
+ });
129
+
130
+ test("記法のエラーは行番号つきで言葉になる", () => {
131
+ assert.throws(() => parse("space /L1/a"), SourceError);
132
+ assert.throws(
133
+ () =>
134
+ parse(`
135
+ grid X 0 3600
136
+ grid Y 0 4500
137
+ level L1 0
138
+ space /L1/a room X1..X9 Y1..Y2
139
+ `),
140
+ /未定義の通り名/,
141
+ );
142
+ assert.throws(() => parse("nonsense 1 2 3"), /未知のキーワード/);
143
+ });
144
+
145
+ test("引用符で空白を含む値が書ける", () => {
146
+ assert.deepEqual(tokenize('space /L1/a room name:"居室 A"', 1), [
147
+ "space",
148
+ "/L1/a",
149
+ "room",
150
+ "name:居室 A",
151
+ ]);
152
+ });
153
+
154
+ test("正準JSONは安定している", () => {
155
+ const m = parse(exampleSrc);
156
+ const j1 = toCanonical(m);
157
+ const j2 = toCanonical(parse(exampleSrc));
158
+ assert.equal(j1, j2);
159
+ assert.ok(j1.includes('"between"'));
160
+ assert.ok(j1.includes('"koyu": "0.1"'));
161
+ });
162
+
163
+ test("平面図SVGが生成される", () => {
164
+ const m = parse(exampleSrc);
165
+ const svg = svgPlan(m);
166
+ assert.ok(svg.startsWith("<svg"));
167
+ assert.ok(svg.includes("居室A"));
168
+ assert.ok(svg.includes("㎡"));
169
+ assert.ok(svg.includes(" A ")); // 扉の軌跡の円弧
170
+ assert.ok(svg.trimEnd().endsWith("</svg>"));
171
+ });
@@ -0,0 +1,97 @@
1
+ // L字領域・ゾーン・吹抜け (void)・採光 — 2026-07-23 後半の設計の検証
2
+
3
+ import assert from "node:assert/strict";
4
+ import { readFileSync } from "node:fs";
5
+ import { test } from "node:test";
6
+ import { fileURLToPath } from "node:url";
7
+ import { check } from "../src/check.js";
8
+ import { doorsBetween, segmentsFor } from "../src/graph.js";
9
+ import { daylight } from "../src/light.js";
10
+ import { areaM2, effectiveUse } from "../src/model.js";
11
+ import { parse } from "../src/parse.js";
12
+
13
+ const mansion = readFileSync(
14
+ fileURLToPath(new URL("../examples/mansion.muro", import.meta.url)),
15
+ "utf8",
16
+ );
17
+ const office = readFileSync(
18
+ fileURLToPath(new URL("../examples/office.muro", import.meta.url)),
19
+ "utf8",
20
+ );
21
+
22
+ test("L字の室: LDKは2矩形の合併で面積は合計", () => {
23
+ const m = parse(mansion);
24
+ const ldk = m.spaces.get("/L2/A/ldk")!;
25
+ assert.equal(ldk.rects.length, 2);
26
+ assert.equal(areaM2(ldk), 17.08);
27
+ });
28
+
29
+ test("L字の外周: 共線の辺は一本にまとまり、掃き出し窓が置ける", () => {
30
+ const m = parse(mansion);
31
+ const b = m.boundaries.find((x) => x.a === "/L2/A/ldk" && x.b === "/L2/A/balcony")!;
32
+ const segs = segmentsFor(m, b);
33
+ assert.equal(segs.length, 1); // LDKの2矩形のS辺がバルコニーに対し1本にマージ
34
+ assert.equal(segs[0]!.x1, 0);
35
+ assert.equal(segs[0]!.x2, 5800);
36
+ });
37
+
38
+ test("同一ペアの複数線分: LDK|洋室はL字で2辺接し、edgeで扉の辺を選ぶ", () => {
39
+ const m = parse(mansion);
40
+ const b = m.boundaries.find((x) => x.a === "/L2/A/ldk" && x.b === "/L2/A/bedroom")!;
41
+ assert.equal(segmentsFor(m, b).length, 2); // 縦 (W) と横 (N)
42
+ const r = check(m);
43
+ assert.deepEqual(r.errors, []); // door edge:W で曖昧が解けている
44
+ });
45
+
46
+ test("ゾーンのuse継承: 間取りの室は住戸のexclusiveを継ぐ", () => {
47
+ const m = parse(mansion);
48
+ assert.equal(effectiveUse(m, m.spaces.get("/L2/A/ldk")!), "exclusive");
49
+ assert.equal(effectiveUse(m, m.spaces.get("/L2/B")!), "exclusive");
50
+ assert.equal(effectiveUse(m, m.spaces.get("/L2/corridor")!), "common");
51
+ });
52
+
53
+ test("吹抜け: 高さ不変量はvoid境界で宣言的に免除される", () => {
54
+ const m = parse(office);
55
+ const r = check(m);
56
+ assert.deepEqual(r.errors, []); // hall h:6700 は L2 に食い込むが void で免除
57
+ assert.deepEqual(r.warnings, []);
58
+ });
59
+
60
+ test("吹抜けの免除はvoid境界なしでは働かない", () => {
61
+ const m = parse(office.replace("boundary /L1/hall /L2/void type:void", ""));
62
+ const r = check(m);
63
+ assert.ok(r.errors.some((e) => e.includes("食い込み")));
64
+ });
65
+
66
+ test("吹抜けは通行できず、床面積にも入らない", () => {
67
+ const m = parse(office);
68
+ assert.equal(doorsBetween(m, "/L1/hall", "/L2/void"), undefined);
69
+ const total = [...m.spaces.values()]
70
+ .filter((s) => s.type !== "void")
71
+ .reduce((sum, s) => sum + (areaM2(s) ?? 0), 0);
72
+ assert.equal(Math.round(total * 100) / 100, 419.84); // 460.8 - 40.96 (吹抜け)
73
+ });
74
+
75
+ test("採光: 51室すべてが1/7を満たす。バルコニー越しは0.7掛け", () => {
76
+ const m = parse(mansion);
77
+ const results = daylight(m);
78
+ assert.equal(results.length, 51); // (LDK+洋室)×8 + B〜E×8 + PH×3
79
+ assert.ok(results.every((r) => r.ok));
80
+ const ldk = results.find((r) => r.space.path === "/L5/A/ldk")!;
81
+ assert.equal(Math.round(ldk.window * 1000) / 1000, 4.004); // 5.72 × 0.7
82
+ });
83
+
84
+ test("採光: 窓を失えば落ちる", () => {
85
+ const m = parse(`
86
+ grid X 0 3600 7200
87
+ grid Y 0 4500
88
+ level L1 0 h:2400
89
+ space /out exterior
90
+ space /L1/a room X1..X2 Y1..Y2
91
+ boundary /L1/a /out t:150
92
+ window w:600 h:600 edge:S
93
+ `);
94
+ const r = daylight(m);
95
+ assert.equal(r.length, 1);
96
+ assert.equal(r[0]!.ok, false); // 0.36㎡ < 16.2/7
97
+ });