@kensnzk/koyu 0.11.0 → 0.14.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 (111) hide show
  1. package/README.ja.md +3 -3
  2. package/README.md +2 -2
  3. package/dist/axo.d.ts +16 -0
  4. package/dist/axo.d.ts.map +1 -0
  5. package/dist/axo.js +216 -0
  6. package/dist/axo.js.map +1 -0
  7. package/dist/check.d.ts +78 -3
  8. package/dist/check.d.ts.map +1 -1
  9. package/dist/check.js +578 -121
  10. package/dist/check.js.map +1 -1
  11. package/dist/cli.js +93 -2
  12. package/dist/cli.js.map +1 -1
  13. package/dist/diff.d.ts +15 -0
  14. package/dist/diff.d.ts.map +1 -1
  15. package/dist/diff.js +68 -7
  16. package/dist/diff.js.map +1 -1
  17. package/dist/fabric.d.ts +28 -0
  18. package/dist/fabric.d.ts.map +1 -0
  19. package/dist/fabric.js +105 -0
  20. package/dist/fabric.js.map +1 -0
  21. package/dist/graph.d.ts +40 -3
  22. package/dist/graph.d.ts.map +1 -1
  23. package/dist/graph.js +352 -53
  24. package/dist/graph.js.map +1 -1
  25. package/dist/index.d.ts +3 -0
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js +3 -0
  28. package/dist/index.js.map +1 -1
  29. package/dist/light.d.ts.map +1 -1
  30. package/dist/light.js +4 -6
  31. package/dist/light.js.map +1 -1
  32. package/dist/mcp.js +16 -9
  33. package/dist/mcp.js.map +1 -1
  34. package/dist/model.d.ts +123 -8
  35. package/dist/model.d.ts.map +1 -1
  36. package/dist/model.js +171 -68
  37. package/dist/model.js.map +1 -1
  38. package/dist/parse.d.ts.map +1 -1
  39. package/dist/parse.js +123 -5
  40. package/dist/parse.js.map +1 -1
  41. package/dist/plan.d.ts +2 -0
  42. package/dist/plan.d.ts.map +1 -1
  43. package/dist/plan.js +97 -10
  44. package/dist/plan.js.map +1 -1
  45. package/dist/poly.d.ts +78 -0
  46. package/dist/poly.d.ts.map +1 -0
  47. package/dist/poly.js +277 -0
  48. package/dist/poly.js.map +1 -0
  49. package/dist/site.d.ts.map +1 -1
  50. package/dist/site.js +5 -4
  51. package/dist/site.js.map +1 -1
  52. package/dist/vertical.d.ts +176 -0
  53. package/dist/vertical.d.ts.map +1 -0
  54. package/dist/vertical.js +669 -0
  55. package/dist/vertical.js.map +1 -0
  56. package/examples/basement/main.muro +86 -0
  57. package/examples/comparison/README.md +48 -0
  58. package/examples/complex/L1.muro +132 -0
  59. package/examples/complex/assets.muro +17 -0
  60. package/examples/complex/basement.muro +40 -0
  61. package/examples/complex/core.muro +40 -0
  62. package/examples/complex/hotel.muro +117 -0
  63. package/examples/complex/main.muro +67 -0
  64. package/examples/complex/office.muro +41 -0
  65. package/examples/complex/plant.muro +34 -0
  66. package/examples/complex/podium.muro +140 -0
  67. package/examples/complex/site.muro +18 -0
  68. package/examples/house/L1.muro +1 -1
  69. package/examples/house/L2.muro +1 -1
  70. package/examples/house/main.muro +1 -1
  71. package/examples/house.muro +3 -3
  72. package/examples/mansion.muro +18 -13
  73. package/examples/office.muro +1 -1
  74. package/examples/steps/04-exterior.muro +1 -1
  75. package/examples/steps/05-two-storeys.muro +2 -2
  76. package/examples/steps/06-finished.muro +3 -3
  77. package/examples/tower/L11.muro +4 -2
  78. package/examples/tower/L2.muro +2 -0
  79. package/examples/tower/L3.muro +3 -0
  80. package/examples/tower/assets.muro +1 -0
  81. package/examples/tower/main.muro +1 -1
  82. package/examples/tower/typical.muro +16 -9
  83. package/examples/twin/L1.muro +139 -0
  84. package/examples/twin/L5.muro +133 -0
  85. package/examples/twin/assets.muro +19 -0
  86. package/examples/twin/basement.muro +48 -0
  87. package/examples/twin/core.muro +75 -0
  88. package/examples/twin/hotel.muro +129 -0
  89. package/examples/twin/main.muro +96 -0
  90. package/examples/twin/office.muro +104 -0
  91. package/examples/twin/podium.muro +120 -0
  92. package/examples/twin/residence.muro +131 -0
  93. package/examples/twin/site.muro +29 -0
  94. package/examples/two-rooms.canonical.json +26 -2
  95. package/examples/two-rooms.muro +6 -3
  96. package/package.json +3 -2
  97. package/src/axo.ts +263 -0
  98. package/src/check.ts +680 -143
  99. package/src/cli.ts +100 -2
  100. package/src/diff.ts +75 -7
  101. package/src/fabric.ts +123 -0
  102. package/src/graph.ts +366 -56
  103. package/src/index.ts +24 -0
  104. package/src/light.ts +4 -7
  105. package/src/mcp.ts +17 -10
  106. package/src/model.ts +256 -66
  107. package/src/parse.ts +124 -5
  108. package/src/plan.ts +120 -11
  109. package/src/poly.ts +295 -0
  110. package/src/site.ts +6 -6
  111. package/src/vertical.ts +884 -0
package/src/model.ts CHANGED
@@ -2,13 +2,15 @@
2
2
  // 一次要素は空間。壁は二つの空間の「境界」という関係であり、物ではない。
3
3
  // 形はここには無い。形は生成物である。(docs/writing-architecture.md)
4
4
 
5
+ import * as poly from "./poly.js";
6
+
5
7
  export type AttrValue = string | number;
6
8
  export type Attrs = Record<string, AttrValue>;
7
9
 
8
10
  /** このツールが受理する言語版 (ADR-0017)。旧版は意味保存の場合のみ受理される (checkが検査する) */
9
- export const SUPPORTED_LANGUAGE_VERSIONS: readonly string[] = ["0.1", "0.2", "0.3"];
11
+ export const SUPPORTED_LANGUAGE_VERSIONS: readonly string[] = ["0.1", "0.2", "0.3", "0.4", "0.5"];
10
12
  /** 版宣言を省略したときの解釈 — 常に最新版の意味論 (省略はツール版を跨いで意味安定ではない) */
11
- export const DEFAULT_LANGUAGE_VERSION = "0.3";
13
+ export const DEFAULT_LANGUAGE_VERSION = "0.5";
12
14
 
13
15
  /** 方位。edge指定は「最初に書いた空間」の矩形から見た辺。N=+Y, S=-Y, E=+X, W=-X */
14
16
  export type Edge = "N" | "E" | "S" | "W";
@@ -21,6 +23,15 @@ export interface Level {
21
23
  h?: number;
22
24
  /** この階の床組み厚 mm (下階の天井面から自階FLまで: スラブ+懐+仕上) */
23
25
  slab?: number;
26
+ /**
27
+ * 地下の宣言 (ADR-0022)。zの負値から推定はしない — 地盤面は敷地の事実であって
28
+ * 座標系の原点の事実ではないため。集計 (地上/地下の床面積) と矩計の表示が読む。
29
+ * 接土境界の語彙は導入しない (物の名は spec 語彙 — 台帳の規則2)
30
+ */
31
+ underground?: boolean;
32
+ /** 宣言の出所 — level 由来の診断 (LVL01/HGT03/VER03) が位置を持てるように (ADR-0028) */
33
+ line: number;
34
+ file?: string;
24
35
  }
25
36
 
26
37
  export interface GridAxis {
@@ -67,8 +78,14 @@ export interface Space {
67
78
  level?: string;
68
79
  /** グリッド参照。複数矩形の合併でL字などを表す (rectsと同順) */
69
80
  grids: GridRef[];
70
- /** グリッド解決後のmm矩形の合併。exteriorなどは空 */
81
+ /** グリッド解決後のmm矩形の合併。exteriorなどは空。**書かれた割付** (セル) であって形ではない */
71
82
  rects: Rect[];
83
+ /**
84
+ * 導出された領域 — 凸片の集合 (ADR-0022)。既定は rects をそのまま写したもので、
85
+ * 境界に描かれた線 (line) があればその半平面で切り分けた結果になる。
86
+ * 面積・平面図・立体はこちらを読む。rects は「書かれた綴り」として正準JSONに残る
87
+ */
88
+ pieces: Pt[][];
72
89
  /** 数えない分節 (字下げのarea行) */
73
90
  areas: Area[];
74
91
  attrs: Attrs;
@@ -157,11 +174,27 @@ export interface Seg {
157
174
  line: number;
158
175
  }
159
176
 
177
+ /**
178
+ * 描かれた線 (ADR-0022) — 空間を区切る設計の行為そのもの。
179
+ * 端点は通り語 (`X3,Y1` / `X3+600,Y2-900`) で書く。生座標も角度も導入しない。
180
+ * 境界が既定で持つ「隣接から導かれる線分」を、この線が置き換える
181
+ */
182
+ export interface DrawnLine {
183
+ /** 書かれた綴り — 正準JSONと差分が共有する */
184
+ aRef: string;
185
+ bRef: string;
186
+ a: Pt;
187
+ b: Pt;
188
+ line: number;
189
+ }
190
+
160
191
  /** 境界はどちらの空間にも属さない。二つの空間パスを結ぶ第一級の関係 */
161
192
  export interface Boundary {
162
193
  a: string;
163
194
  b: string;
164
195
  kind: BoundaryKind;
196
+ /** 描かれた線 (字下げの line 行)。あれば境界の実現はこの線になる */
197
+ drawn?: DrawnLine;
165
198
  /** 壁厚 mm (通り芯・境界線に対して芯振り分け) */
166
199
  t?: number;
167
200
  /** 遮蔽しない (air:1) — 手すり・柵など、物はあるが外気・光を遮らない。
@@ -199,6 +232,8 @@ export interface Model {
199
232
  /** 敷地形状 — 所与のジオメトリ (ADR-0011)。パス→頂点列 (mm)。
200
233
  * 唯一の自由頂点列 — 空間の領域はグリッド参照の矩形として書かれる */
201
234
  polygons: Map<string, SitePolygon>;
235
+ /** 柱の宣言 (ADR-0023)。位置は書かれない — 通り芯の交点から導出される */
236
+ columns: ColumnDecl[];
202
237
  /** 合成に参加したレイヤー (ローダーのキー、合成順 — entryが先頭)。単一ソースのparseでは空 */
203
238
  layers: string[];
204
239
  /** koyu版が明示宣言されたか (base層でのみ・一度だけ — ADR-0017の合成規則の管理用) */
@@ -211,6 +246,43 @@ export interface Pt {
211
246
  y: number;
212
247
  }
213
248
 
249
+ /**
250
+ * 柱の宣言 (ADR-0023) — 「どの通りに、どの階に、どの寸法で」だけを書く。
251
+ * **位置は書かない**。通り芯 (共有線) の交点のうち、そのレベルの床のある所に立つ、
252
+ * という規則で導出される。壁が境界から現れるのと同じ構えを、点の要素に適用したもの
253
+ */
254
+ export interface ColumnDecl {
255
+ /** 一辺 mm (角柱)。`d:` があれば矩形断面の奥行 */
256
+ size: number;
257
+ depth?: number;
258
+ /** 展開済みレベル名 (宣言順ではなくz昇順) */
259
+ levels: string[];
260
+ /** 限定する通り名。未指定は全通り */
261
+ xNames?: string[];
262
+ yNames?: string[];
263
+ attrs: Attrs;
264
+ line: number;
265
+ file?: string;
266
+ }
267
+
268
+ /** 導出された柱 — 一本の柱 */
269
+ export interface Column {
270
+ x: number;
271
+ y: number;
272
+ /** X方向の幅 mm / Y方向の奥行 mm */
273
+ w: number;
274
+ d: number;
275
+ level: string;
276
+ /** 立っている通りの名 (X3・Y2 のような組) — 図面の言葉 */
277
+ grid: string;
278
+ /**
279
+ * どの宣言から立ったか (model.columns の添字)。**診断の母集団を宣言に戻すために要る** —
280
+ * これが無いと「この宣言に対して一本も立たない」を問えない (ADR-0028)
281
+ */
282
+ decl: number;
283
+ attrs: Attrs;
284
+ }
285
+
214
286
  /** 敷地形状 (ADR-0011) — 測量に由来する所与の多角形。建物の形は生成物のままで、
215
287
  * これはゾーン (site:1) に付く入力データ。隔離レイヤー (別ファイル+import) 推奨 */
216
288
  export interface SitePolygon {
@@ -220,16 +292,8 @@ export interface SitePolygon {
220
292
  file?: string;
221
293
  }
222
294
 
223
- /** 多角形の面積 ㎡ (シューレース公式)。頂点は順不同 (時計/反時計どちらでも) */
224
- export function polygonAreaM2(points: Pt[]): number {
225
- let sum = 0;
226
- for (let i = 0; i < points.length; i++) {
227
- const a = points[i]!;
228
- const b = points[(i + 1) % points.length]!;
229
- sum += a.x * b.y - b.x * a.y;
230
- }
231
- return Math.abs(sum) / 2 / 1e6;
232
- }
295
+ /** 多角形の面積 ㎡。シューレースの実体は poly.ts にひとつだけある (ADR-0027) */
296
+ export const polygonAreaM2 = (points: Pt[]): number => poly.area(points) / 1e6;
233
297
 
234
298
  /** 線分同士が内部で交差するか (端点・境界上の接触は交差としない、許容誤差eps mm)。交点を返す */
235
299
  function properCrossing(a1: Pt, a2: Pt, b1: Pt, b2: Pt, eps = 1): Pt | undefined {
@@ -264,58 +328,32 @@ export function polygonSelfIntersection(poly: Pt[], eps = 1): Pt | undefined {
264
328
  * 四隅の内包に加え、多角形の頂点の矩形内への入り込みと、辺同士の内部交差を検査する
265
329
  */
266
330
  export function rectEscapesPolygon(r: Rect, poly: Pt[], eps = 1): Pt | undefined {
267
- const corners: Pt[] = [
268
- { x: r.x1, y: r.y1 },
269
- { x: r.x2, y: r.y1 },
270
- { x: r.x2, y: r.y2 },
271
- { x: r.x1, y: r.y2 },
272
- ];
273
- for (const c of corners) if (!pointInPolygon(c, poly, eps)) return c;
331
+ return shapeEscapesPolygon(rectToPoly(r), poly, eps);
332
+ }
333
+
334
+ /** 凸片が多角形からはみ出す点 (導出された領域を敷地形状と照合する — ADR-0022) */
335
+ export function shapeEscapesPolygon(shape: Pt[], poly: Pt[], eps = 1): Pt | undefined {
336
+ for (const c of shape) if (!pointInPolygon(c, poly, eps)) return c;
274
337
  for (const p of poly) {
275
- if (p.x > r.x1 + eps && p.x < r.x2 - eps && p.y > r.y1 + eps && p.y < r.y2 - eps) return p;
338
+ // 多角形の頂点が凸片の内部に食い込む (境界上は食い込みではない)
339
+ if (pointInPolygon(p, shape, eps) && !onPolygonEdge(p, shape, eps)) return p;
276
340
  }
277
- const edges: Array<[Pt, Pt]> = [
278
- [corners[0]!, corners[1]!],
279
- [corners[1]!, corners[2]!],
280
- [corners[2]!, corners[3]!],
281
- [corners[3]!, corners[0]!],
282
- ];
283
341
  for (let i = 0; i < poly.length; i++) {
284
342
  const a = poly[i]!;
285
343
  const b = poly[(i + 1) % poly.length]!;
286
- for (const [p, q] of edges) {
287
- const x = properCrossing(a, b, p, q, eps);
344
+ for (let k = 0; k < shape.length; k++) {
345
+ const x = properCrossing(a, b, shape[k]!, shape[(k + 1) % shape.length]!, eps);
288
346
  if (x) return x;
289
347
  }
290
348
  }
291
349
  return undefined;
292
350
  }
293
351
 
294
- /** 点が多角形の内側にあるか (境界上は内側扱い、許容誤差eps mm) */
295
- export function pointInPolygon(p: Pt, poly: Pt[], eps = 1): boolean {
296
- // 境界上の判定 (線分との距離 ≤ eps)
297
- for (let i = 0; i < poly.length; i++) {
298
- const a = poly[i]!;
299
- const b = poly[(i + 1) % poly.length]!;
300
- const dx = b.x - a.x;
301
- const dy = b.y - a.y;
302
- const len2 = dx * dx + dy * dy;
303
- const t = len2 === 0 ? 0 : Math.max(0, Math.min(1, ((p.x - a.x) * dx + (p.y - a.y) * dy) / len2));
304
- const qx = a.x + t * dx;
305
- const qy = a.y + t * dy;
306
- if ((p.x - qx) ** 2 + (p.y - qy) ** 2 <= eps * eps) return true;
307
- }
308
- // レイキャスト
309
- let inside = false;
310
- for (let i = 0, j = poly.length - 1; i < poly.length; j = i++) {
311
- const a = poly[i]!;
312
- const b = poly[j]!;
313
- if (a.y > p.y !== b.y > p.y && p.x < ((b.x - a.x) * (p.y - a.y)) / (b.y - a.y) + a.x) {
314
- inside = !inside;
315
- }
316
- }
317
- return inside;
318
- }
352
+ /** 点が多角形の辺の上にあるか */
353
+ export const onPolygonEdge = poly.onEdge;
354
+
355
+ /** 点が多角形の内側にあるか (境界上は内側扱い) */
356
+ export const pointInPolygon = poly.pointIn;
319
357
 
320
358
  export class SourceError extends Error {
321
359
  constructor(
@@ -330,13 +368,114 @@ export class SourceError extends Error {
330
368
  }
331
369
  }
332
370
 
333
- /** 面積 (壁芯) m²。複数矩形は合計 (重なりはcheckが禁じる) */
371
+ /**
372
+ * 空間の導出された領域 (ADR-0022 / ADR-0027)。**形を読むときの唯一の入口**。
373
+ * parse の出口で必ず埋まるので、割付への退避は「未parseのModelを手で組んだとき」だけに効く。
374
+ * この式が各所に散っていたことが、rects と pieces の取り違えを四度生んだ根である。
375
+ */
376
+ export const regionOf = (s: Space): Pt[][] =>
377
+ s.pieces.length > 0 ? s.pieces : s.rects.map(poly.rectToPoly);
378
+
379
+ /** 面積 (壁芯) m²。導出された凸片の合計 — 描かれた線で切られていればその形の面積になる */
334
380
  export function areaM2(s: Space): number | undefined {
335
381
  if (s.rects.length === 0) return undefined;
336
- const a = s.rects.reduce((sum, r) => sum + (r.x2 - r.x1) * (r.y2 - r.y1), 0) / 1e6;
382
+ const a = poly.areaOf(regionOf(s)) / 1e6;
337
383
  return Math.round(a * 100) / 100;
338
384
  }
339
385
 
386
+ /** 矩形を頂点列へ (反時計回り) */
387
+ export const rectToPoly = poly.rectToPoly;
388
+
389
+ /** 凸多角形を半平面で切る (Sutherland–Hodgman)。
390
+ * 半平面は有向線分 a→b の左側 (外積>0)。切り落とされて空なら [] */
391
+ export function clipHalfPlane(poly: Pt[], a: Pt, b: Pt, keepLeft: boolean, eps = 1e-6): Pt[] {
392
+ const side = (p: Pt): number => {
393
+ const v = (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x);
394
+ return keepLeft ? v : -v;
395
+ };
396
+ const out: Pt[] = [];
397
+ for (let i = 0; i < poly.length; i++) {
398
+ const p = poly[i]!;
399
+ const q = poly[(i + 1) % poly.length]!;
400
+ const sp = side(p);
401
+ const sq = side(q);
402
+ if (sp >= -eps) out.push(p);
403
+ if ((sp > eps && sq < -eps) || (sp < -eps && sq > eps)) {
404
+ const t = sp / (sp - sq);
405
+ out.push({ x: p.x + t * (q.x - p.x), y: p.y + t * (q.y - p.y) });
406
+ }
407
+ }
408
+ // 退化 (面積ゼロ) は捨てる
409
+ return out.length >= 3 && polygonAreaM2(out) > 1e-9 ? out : [];
410
+ }
411
+
412
+ /** 頂点列の外接矩形 */
413
+ export const polyBounds = poly.bounds;
414
+
415
+ /**
416
+ * 宣言 c がそのレベルで狙う交点のうち、**床の上にあるもの**。先勝ちの規則を適用する前の姿。
417
+ *
418
+ * 「一本も立たない」の理由を二つに割るために要る (ADR-0028) — ここが空なら床が無く、
419
+ * 空でないのに一本も立たないなら先の宣言に取られている。直す手が正反対である。
420
+ */
421
+ export function columnSites(
422
+ model: Model,
423
+ c: ColumnDecl,
424
+ level: string,
425
+ ): Array<{ x: number; y: number; grid: string }> {
426
+ if (!c.levels.includes(level)) return [];
427
+ const floors = [...model.spaces.values()].filter(
428
+ (s) => s.level === level && s.type !== "exterior" && s.type !== "void" && s.rects.length > 0,
429
+ );
430
+ if (floors.length === 0) return [];
431
+ const xs = model.grid.X.names
432
+ .map((n, i) => ({ n, v: model.grid.X.coords[i]! }))
433
+ .filter((g) => !c.xNames || c.xNames.includes(g.n));
434
+ const ys = model.grid.Y.names
435
+ .map((n, i) => ({ n, v: model.grid.Y.coords[i]! }))
436
+ .filter((g) => !c.yNames || c.yNames.includes(g.n));
437
+ const out: Array<{ x: number; y: number; grid: string }> = [];
438
+ for (const gx of xs) {
439
+ for (const gy of ys) {
440
+ const inside = floors.some((s) =>
441
+ (s.pieces.length ? s.pieces : s.rects.map(rectToPoly)).some((p) =>
442
+ pointInPolygon({ x: gx.v, y: gy.v }, p, 1),
443
+ ),
444
+ );
445
+ if (inside) out.push({ x: gx.v, y: gy.v, grid: `${gx.n}・${gy.n}` });
446
+ }
447
+ }
448
+ return out;
449
+ }
450
+
451
+ /**
452
+ * そのレベルに立つ柱を導く (ADR-0023)。
453
+ * 通り芯の交点のうち、床のある空間 (exterior・void を除く) の内側にあるものへ柱を置く。
454
+ * 位置はどこにも書かれていない — 通りと床という既にある事実の交わりから現れる
455
+ */
456
+ export function columnsFor(model: Model, level: string): Column[] {
457
+ const out: Column[] = [];
458
+ const seen = new Set<string>();
459
+ for (let ci = 0; ci < model.columns.length; ci++) {
460
+ const c = model.columns[ci]!;
461
+ for (const site of columnSites(model, c, level)) {
462
+ if (seen.has(site.grid)) continue; // 同じ交点に二本は立たない (先の宣言が勝つ)
463
+ seen.add(site.grid);
464
+ out.push({
465
+ x: site.x,
466
+ y: site.y,
467
+ w: c.size,
468
+ d: c.depth ?? c.size,
469
+ level,
470
+ grid: site.grid,
471
+ decl: ci,
472
+ attrs: c.attrs,
473
+ });
474
+ }
475
+ }
476
+ return out;
477
+ }
478
+
340
479
  /**
341
480
  * 半屋外か — 宣言ではなく導出。外部 (type:exterior) に対して
342
481
  * open または air:1 (手すり等、遮蔽しない物) の境界で接する空間は半屋外である (ADR-0007)
@@ -364,13 +503,7 @@ export function isCoveredAbove(model: Model, s: Space): boolean {
364
503
  if (o === s || o.rects.length === 0 || !o.level) continue;
365
504
  const oz = model.levels[o.level]?.z;
366
505
  if (oz === undefined || oz <= z) continue;
367
- for (const ra of s.rects) {
368
- for (const rb of o.rects) {
369
- const x = Math.min(ra.x2, rb.x2) - Math.max(ra.x1, rb.x1);
370
- const y = Math.min(ra.y2, rb.y2) - Math.max(ra.y1, rb.y1);
371
- if (x > 0.5 && y > 0.5) return true;
372
- }
373
- }
506
+ if (poly.overlaps(regionOf(s), regionOf(o))) return true;
374
507
  }
375
508
  return false;
376
509
  }
@@ -398,13 +531,24 @@ export function zoneAreaM2(model: Model, zonePath: string): number {
398
531
  let sum = 0;
399
532
  for (const s of model.spaces.values()) {
400
533
  if (!s.path.startsWith(zonePath + "/")) continue;
401
- if (s.type === "void") continue;
402
- if (isSemiOutdoor(model, s)) continue;
534
+ if (!isIndoor(model, s)) continue;
403
535
  sum += areaM2(s) ?? 0;
404
536
  }
405
537
  return Math.round(sum * 100) / 100;
406
538
  }
407
539
 
540
+ /**
541
+ * 屋内の床面積に数えるか。**「延べ面積」を問う三箇所 (stats / site / MCP) の唯一の答え**。
542
+ *
543
+ * 判定が散っていたときは、外部そのものが屋内床に入り、しかも「隣に道路を書いたか」で
544
+ * 半屋外に落ちるかどうかが変わっていた。母集団は場所ごとに決めるものではない (ADR-0028)。
545
+ */
546
+ export function isIndoor(model: Model, s: Space): boolean {
547
+ if (s.rects.length === 0) return false;
548
+ if (s.type === "exterior" || s.type === "void") return false;
549
+ return !isSemiOutdoor(model, s);
550
+ }
551
+
408
552
  /** 実効use属性 — 自分に無ければ、最も深いゾーン祖先から継承する */
409
553
  export function effectiveUse(model: Model, s: Space): string | undefined {
410
554
  const own = s.attrs["use"];
@@ -500,6 +644,10 @@ export function canonicalBoundaryEntry(b: Boundary): Record<string, unknown> {
500
644
  ...(b.t !== undefined ? { t: b.t } : {}),
501
645
  ...(b.air ? { air: true } : {}),
502
646
  ...(b.edge ? { edge: b.edge } : {}),
647
+ // 描かれた線は書かれた綴りのまま残す — 頂点座標はビルドの産物であって構成ではない
648
+ // 端点の書き順は図形を変えない (線分は向きを持たない — 導出される凸片は同一) ので、
649
+ // 解決座標の昇順に正準化する。**綴りは保つ** (通り参照のまま — 規則3)
650
+ ...(b.drawn ? { line: canonicalLineEnds(b.drawn) } : {}),
503
651
  ...(Object.keys(b.attrs).length ? { attrs: sortObj(b.attrs) } : {}),
504
652
  ...(b.openings.length ? { openings: sortBySerial(b.openings.map(canonicalOpeningEntry)) } : {}),
505
653
  ...(b.segs.length ? { segs: sortBySerial(b.segs.map(canonicalSegEntry)) } : {}),
@@ -546,12 +694,28 @@ export function toCanonical(model: Model): string {
546
694
  z: v.z,
547
695
  ...(v.h !== undefined ? { h: v.h } : {}),
548
696
  ...(v.slab !== undefined ? { slab: v.slab } : {}),
697
+ ...(v.underground ? { underground: 1 } : {}),
549
698
  },
550
699
  ]),
551
700
  ),
552
701
  ),
553
702
  ...(Object.keys(assets).length ? { assets } : {}),
554
703
  ...(Object.keys(polygons).length ? { polygons } : {}),
704
+ // **柱の宣言順は意味である。**同じ交点に二本は立たず、先の宣言が勝つ (ADR-0023) ので、
705
+ // 並べ替えると別の建物が同一のJSONになる。並べ替えてよいのは宣言の**中**の、
706
+ // 順序に意味の無い通り名の列だけである (ADR-0029)
707
+ ...(model.columns.length
708
+ ? {
709
+ columns: model.columns.map((c) => ({
710
+ size: c.size,
711
+ ...(c.depth !== undefined ? { d: c.depth } : {}),
712
+ levels: c.levels,
713
+ ...(c.xNames ? { x: sortGridNames(model.grid.X.names, c.xNames) } : {}),
714
+ ...(c.yNames ? { y: sortGridNames(model.grid.Y.names, c.yNames) } : {}),
715
+ ...(Object.keys(c.attrs).length ? { attrs: sortObj(c.attrs) } : {}),
716
+ })),
717
+ }
718
+ : {}),
555
719
  ...(Object.keys(zones).length ? { zones } : {}),
556
720
  spaces,
557
721
  boundaries,
@@ -559,11 +723,37 @@ export function toCanonical(model: Model): string {
559
723
  return JSON.stringify(doc, null, 2) + "\n";
560
724
  }
561
725
 
726
+ /**
727
+ * 描かれた線の端点の対を、解決座標の昇順に並べる。
728
+ * 線分は向きを持たない — 端点をどちらから書いても導出される凸片は同一なので、
729
+ * 書き順は綴りの揺れである (規則1)。綴り自体は通り参照のまま保つ (規則3)
730
+ */
731
+ function canonicalLineEnds(d: DrawnLine): [string, string] {
732
+ const forward = d.a.x < d.b.x || (d.a.x === d.b.x && d.a.y <= d.b.y);
733
+ return forward ? [d.aRef, d.bRef] : [d.bRef, d.aRef];
734
+ }
735
+
736
+ /** 通り名の列を通りの並び順に整える — `x:X2,X1` と `x:X1,X2` は同じ構成である */
737
+ function sortGridNames(axis: string[], names: string[]): string[] {
738
+ return [...names].sort((p, q) => {
739
+ const ip = axis.indexOf(p);
740
+ const iq = axis.indexOf(q);
741
+ if (ip < 0 || iq < 0) return p < q ? -1 : p > q ? 1 : 0; // 未宣言の通りは辞書順で安定させる
742
+ return ip - iq;
743
+ });
744
+ }
745
+
562
746
  function sortObj<T>(o: Record<string, T>): Record<string, T> {
563
747
  return Object.fromEntries(Object.entries(o).sort(([a], [b]) => (a < b ? -1 : 1)));
564
748
  }
565
749
 
566
- /** 宣言順に意味の無い集合を、直列化したJSONの辞書順に並べる — 正準順の土台 (diffも同じ順で比べる) */
750
+ /**
751
+ * 宣言順に意味の無い集合を、直列化したJSONの辞書順に並べる — 正準順の土台 (diffも同じ順で比べる)。
752
+ *
753
+ * **掛ける前に問う: この配列の順序を入れ替えたら別の構成になるか。**なるなら掛けてはならない。
754
+ * 並べ替えは整形ではなく「順序に意味が無い」ことの表明である。柱 (columns) は先勝ちの規則を
755
+ * 持つので掛けない — 掛けていたとき、別の建物が同一バイトの正準JSONになっていた (ADR-0029)
756
+ */
567
757
  export function sortBySerial<T>(items: T[]): T[] {
568
758
  return items
569
759
  .map((it) => [JSON.stringify(it), it] as const)
package/src/parse.ts CHANGED
@@ -12,13 +12,14 @@ import {
12
12
  type Edge,
13
13
  type Model,
14
14
  type Opening,
15
+ type Pt,
15
16
  type Rect,
16
17
  type Seg,
17
18
  SourceError,
18
19
  type Space,
19
20
  SUPPORTED_LANGUAGE_VERSIONS,
20
21
  } from "./model.js";
21
- import { deriveDefaultBoundaries } from "./graph.js";
22
+ import { deriveDefaultBoundaries, derivePieces } from "./graph.js";
22
23
 
23
24
  const EDGES = new Set(["N", "E", "S", "W"]);
24
25
 
@@ -61,6 +62,7 @@ function emptyModel(): Model {
61
62
  assets: new Map(),
62
63
  boundaries: [],
63
64
  polygons: new Map(),
65
+ columns: [],
64
66
  layers: [],
65
67
  };
66
68
  }
@@ -75,7 +77,11 @@ export type LayerLoader = (
75
77
  export function parse(source: string): Model {
76
78
  const model = emptyModel();
77
79
  ingest(model, source, undefined, new Set(), undefined);
78
- deriveDefaultBoundaries(model); // 水平の既定は壁 (ADR-0014) — 合成・展開の完了後に一度だけ
80
+ // 描かれた線で領域を切り分けてから、既定の壁を導く (ADR-0022 / ADR-0027)。
81
+ // 逆順だと、線で接触が消えた組にも既定境界が生まれ、線分ゼロの境界に
82
+ // 出所の無い BND04 が出る — 書いていない関係を責めることになる
83
+ derivePieces(model);
84
+ deriveDefaultBoundaries(model);
79
85
  return model;
80
86
  }
81
87
 
@@ -89,7 +95,11 @@ export function parseWith(loader: LayerLoader, entry: string): Model {
89
95
  throw new SourceError(0, `ファイルが読めません: ${entry}`);
90
96
  }
91
97
  ingestLayer(model, layer.key, layer.src, new Set(), loader);
92
- deriveDefaultBoundaries(model); // 水平の既定は壁 (ADR-0014) — 合成・展開の完了後に一度だけ
98
+ // 描かれた線で領域を切り分けてから、既定の壁を導く (ADR-0022 / ADR-0027)。
99
+ // 逆順だと、線で接触が消えた組にも既定境界が生まれ、線分ゼロの境界に
100
+ // 出所の無い BND04 が出る — 書いていない関係を責めることになる
101
+ derivePieces(model);
102
+ deriveDefaultBoundaries(model);
93
103
  return model;
94
104
  }
95
105
 
@@ -167,6 +177,16 @@ function ingest(
167
177
  throw new SourceError(ln, "seg は boundary の直下に字下げして書きます");
168
178
  }
169
179
  for (const b of current) b.segs.push(parseSeg(rest, ln, model));
180
+ } else if (head === "line") {
181
+ // 描かれた線 (ADR-0022) — 境界の実現を、隣接からの導出ではなく設計の行為で与える
182
+ if (current.length === 0) {
183
+ throw new SourceError(ln, "line は boundary の直下に字下げして書きます");
184
+ }
185
+ const drawn = parseDrawnLine(rest, ln, model);
186
+ for (const b of current) {
187
+ if (b.drawn) throw new SourceError(ln, `一つの境界に線は一本です: ${b.a} | ${b.b}`);
188
+ b.drawn = { ...drawn };
189
+ }
170
190
  } else if (head === "area") {
171
191
  if (band) {
172
192
  throw new SourceError(
@@ -185,7 +205,7 @@ function ingest(
185
205
  } else {
186
206
  throw new SourceError(
187
207
  ln,
188
- `字下げ行に置けるのは door / window / seg / area / space (band の要素) のみです: ${head}`,
208
+ `字下げ行に置けるのは door / window / seg / line / area / space (band の要素) のみです: ${head}`,
189
209
  );
190
210
  }
191
211
  continue;
@@ -337,6 +357,11 @@ function ingest(
337
357
  const h = takeNumber(attrs, "h", ln);
338
358
  const slab = takeNumber(attrs, "slab", ln);
339
359
  const pitch = takeNumber(attrs, "pitch", ln);
360
+ const under = takeNumber(attrs, "underground", ln);
361
+ if (under !== undefined && under !== 0 && under !== 1) {
362
+ throw new SourceError(ln, "underground は 0 / 1 で指定します (1=地下)");
363
+ }
364
+ const ug = under === 1 ? { underground: true } : {};
340
365
 
341
366
  // 範囲宣言: level L3..L9 6700 pitch:2900 — 基準階のレベルを一度に宣言する
342
367
  const range = /^([A-Za-z]+)(\d+)\.\.([A-Za-z]+)(\d+)$/.exec(name);
@@ -354,8 +379,11 @@ function ingest(
354
379
  model.levels[nm] = {
355
380
  name: nm,
356
381
  z: z + pitch * (k - Number(n1)),
382
+ line: ln,
383
+ ...(file !== undefined ? { file } : {}),
357
384
  ...(h !== undefined ? { h } : {}),
358
385
  ...(slab !== undefined ? { slab } : {}),
386
+ ...ug,
359
387
  };
360
388
  }
361
389
  break;
@@ -367,9 +395,54 @@ function ingest(
367
395
  model.levels[name] = {
368
396
  name,
369
397
  z,
398
+ line: ln,
399
+ ...(file !== undefined ? { file } : {}),
370
400
  ...(h !== undefined ? { h } : {}),
371
401
  ...(slab !== undefined ? { slab } : {}),
402
+ ...ug,
403
+ };
404
+ break;
405
+ }
406
+ case "column": {
407
+ // 柱 (ADR-0023) — 寸法と階と通りだけを書く。位置は通り芯の交点から導出される
408
+ const size = toNumber(rest[0] ?? "", ln, "columnの寸法");
409
+ if (size <= 0) throw new SourceError(ln, "column の寸法は正のmmで書きます");
410
+ const span = rest[1];
411
+ if (!span) {
412
+ throw new SourceError(ln, "column は column <寸法mm> <L?..L?|レベル名> [x:通り,..] [y:通り,..] の形で書きます");
413
+ }
414
+ const levels = /\.\./.test(span)
415
+ ? resolveSpanLevels(model, span, ln)
416
+ : (() => {
417
+ if (!model.levels[span]) throw new SourceError(ln, `未宣言のレベルです: ${span}`);
418
+ return [span];
419
+ })();
420
+ const attrs = parseAttrs(rest.slice(2), ln);
421
+ const depth = takeNumber(attrs, "d", ln);
422
+ const names = (key: "x" | "y"): string[] | undefined => {
423
+ const v = takeString(attrs, key);
424
+ if (v === undefined) return undefined;
425
+ const list = v.split(",").filter(Boolean);
426
+ for (const n of list) {
427
+ if (!model.grid[key === "x" ? "X" : "Y"].names.includes(n)) {
428
+ throw new SourceError(ln, `未定義の通り名です: ${n}`);
429
+ }
430
+ }
431
+ if (list.length === 0) throw new SourceError(ln, `${key}: に通り名を書きます`);
432
+ return list;
372
433
  };
434
+ const xNames = names("x");
435
+ const yNames = names("y");
436
+ model.columns.push({
437
+ size,
438
+ ...(depth !== undefined ? { depth } : {}),
439
+ levels,
440
+ ...(xNames ? { xNames } : {}),
441
+ ...(yNames ? { yNames } : {}),
442
+ attrs,
443
+ line: ln,
444
+ ...(file ? { file } : {}),
445
+ });
373
446
  break;
374
447
  }
375
448
  case "band": {
@@ -524,7 +597,17 @@ function parseSpace(rest: string[], ln: number, model: Model): Space {
524
597
  const seg = path.split("/")[1];
525
598
  const level = explicit ?? (seg && model.levels[seg] ? seg : undefined);
526
599
 
527
- const space: Space = { path, type, level, grids: [], rects: [], areas: [], attrs, line: ln };
600
+ const space: Space = {
601
+ path,
602
+ type,
603
+ level,
604
+ grids: [],
605
+ rects: [],
606
+ pieces: [],
607
+ areas: [],
608
+ attrs,
609
+ line: ln,
610
+ };
528
611
  for (const g of groups) {
529
612
  if (g.length === 0) continue;
530
613
  const r = parseRegion(g, ln, model);
@@ -768,6 +851,42 @@ function parseRegion(
768
851
  };
769
852
  }
770
853
 
854
+ /**
855
+ * 描かれた線 (ADR-0022) — `line X3,Y1 X4+600,Y3`。
856
+ * 端点は通り語の対 (`<X通り>,<Y通り>`、どちらもオフセット可)。
857
+ * **生の座標も角度も書けない** — 位置を定めるのは常に線であり、線の端点は通りである
858
+ */
859
+ function parseDrawnLine(
860
+ rest: string[],
861
+ ln: number,
862
+ model: Model,
863
+ ): { aRef: string; bRef: string; a: Pt; b: Pt; line: number } {
864
+ if (rest.length !== 2) {
865
+ throw new SourceError(ln, "line は line <始点> <終点> の形で書きます (例: line X3,Y1 X4,Y3)");
866
+ }
867
+ const [aRef, bRef] = rest as [string, string];
868
+ const a = resolvePoint(model, aRef, ln);
869
+ const b = resolvePoint(model, bRef, ln);
870
+ if (Math.abs(a.x - b.x) < 0.5 && Math.abs(a.y - b.y) < 0.5) {
871
+ throw new SourceError(ln, `line の両端が同じ点です: ${aRef}`);
872
+ }
873
+ return { aRef, bRef, a, b, line: ln };
874
+ }
875
+
876
+ /** 通り語の対 `X3,Y1` / `X3+600,Y2-900` を点へ */
877
+ function resolvePoint(model: Model, token: string, ln: number): Pt {
878
+ const parts = token.split(",");
879
+ if (parts.length !== 2) {
880
+ throw new SourceError(ln, `点は <X通り>,<Y通り> の形で書きます: ${token}`);
881
+ }
882
+ const p = resolveRef(model, parts[0]!, ln);
883
+ const q = resolveRef(model, parts[1]!, ln);
884
+ if (p.axis !== "X" || q.axis !== "Y") {
885
+ throw new SourceError(ln, `点はX通り,Y通りの順で書きます: ${token}`);
886
+ }
887
+ return { x: p.coord, y: q.coord };
888
+ }
889
+
771
890
  /** 数えない分節: 室内の領域 (床材の切替など) */
772
891
  function parseArea(rest: string[], ln: number, model: Model): Area {
773
892
  const regionTokens = rest.filter((t) => t.includes(".."));