@kensnzk/koyu 0.5.0 → 0.13.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 (160) hide show
  1. package/README.ja.md +43 -11
  2. package/README.md +51 -11
  3. package/dist/axo.d.ts +16 -0
  4. package/dist/axo.d.ts.map +1 -0
  5. package/dist/axo.js +211 -0
  6. package/dist/axo.js.map +1 -0
  7. package/dist/check.d.ts +109 -0
  8. package/dist/check.d.ts.map +1 -0
  9. package/dist/check.js +957 -0
  10. package/dist/check.js.map +1 -0
  11. package/dist/cli.d.ts +3 -0
  12. package/dist/cli.d.ts.map +1 -0
  13. package/dist/cli.js +410 -0
  14. package/dist/cli.js.map +1 -0
  15. package/dist/diff.d.ts +105 -0
  16. package/dist/diff.d.ts.map +1 -0
  17. package/dist/diff.js +583 -0
  18. package/dist/diff.js.map +1 -0
  19. package/dist/fabric.d.ts +28 -0
  20. package/dist/fabric.d.ts.map +1 -0
  21. package/dist/fabric.js +105 -0
  22. package/dist/fabric.js.map +1 -0
  23. package/dist/graph.d.ts +118 -0
  24. package/dist/graph.d.ts.map +1 -0
  25. package/dist/graph.js +627 -0
  26. package/dist/graph.js.map +1 -0
  27. package/dist/index.d.ts +12 -0
  28. package/dist/index.d.ts.map +1 -0
  29. package/dist/index.js +13 -0
  30. package/dist/index.js.map +1 -0
  31. package/dist/light.d.ts +15 -0
  32. package/dist/light.d.ts.map +1 -0
  33. package/dist/light.js +53 -0
  34. package/dist/light.js.map +1 -0
  35. package/dist/mcp.d.ts +3 -0
  36. package/dist/mcp.d.ts.map +1 -0
  37. package/dist/mcp.js +361 -0
  38. package/dist/mcp.js.map +1 -0
  39. package/dist/model.d.ts +379 -0
  40. package/dist/model.d.ts.map +1 -0
  41. package/dist/model.js +464 -0
  42. package/dist/model.js.map +1 -0
  43. package/dist/parse-file.d.ts +9 -0
  44. package/dist/parse-file.d.ts.map +1 -0
  45. package/dist/parse-file.js +22 -0
  46. package/dist/parse-file.js.map +1 -0
  47. package/dist/parse.d.ts +16 -0
  48. package/dist/parse.d.ts.map +1 -0
  49. package/dist/parse.js +1040 -0
  50. package/dist/parse.js.map +1 -0
  51. package/dist/plan.d.ts +10 -0
  52. package/dist/plan.d.ts.map +1 -0
  53. package/dist/plan.js +354 -0
  54. package/dist/plan.js.map +1 -0
  55. package/dist/poly.d.ts +78 -0
  56. package/dist/poly.d.ts.map +1 -0
  57. package/dist/poly.js +277 -0
  58. package/dist/poly.js.map +1 -0
  59. package/dist/site.d.ts +24 -0
  60. package/dist/site.d.ts.map +1 -0
  61. package/dist/site.js +54 -0
  62. package/dist/site.js.map +1 -0
  63. package/dist/vertical.d.ts +176 -0
  64. package/dist/vertical.d.ts.map +1 -0
  65. package/dist/vertical.js +669 -0
  66. package/dist/vertical.js.map +1 -0
  67. package/examples/basement/main.muro +84 -0
  68. package/examples/comparison/README.md +64 -1
  69. package/examples/comparison/gen-ifc4.ts +1 -1
  70. package/examples/comparison/gen-ifcx.ts +2 -2
  71. package/examples/comparison/two-rooms.ifcx +1 -1
  72. package/examples/complex/L1.muro +132 -0
  73. package/examples/complex/assets.muro +17 -0
  74. package/examples/complex/basement.muro +40 -0
  75. package/examples/complex/core.muro +40 -0
  76. package/examples/complex/hotel.muro +117 -0
  77. package/examples/complex/main.muro +67 -0
  78. package/examples/complex/office.muro +41 -0
  79. package/examples/complex/plant.muro +34 -0
  80. package/examples/complex/podium.muro +140 -0
  81. package/examples/complex/site.muro +18 -0
  82. package/examples/house/L1.muro +16 -0
  83. package/examples/house/L2.muro +19 -0
  84. package/examples/house/assets.muro +9 -0
  85. package/examples/house/main.muro +27 -0
  86. package/examples/house/site.muro +31 -0
  87. package/examples/house.muro +3 -3
  88. package/examples/mansion.muro +18 -13
  89. package/examples/office.muro +1 -1
  90. package/examples/steps/01-one-room.muro +4 -0
  91. package/examples/steps/02-two-rooms.muro +5 -0
  92. package/examples/steps/03-door.muro +9 -0
  93. package/examples/steps/04-exterior.muro +15 -0
  94. package/examples/steps/05-two-storeys.muro +27 -0
  95. package/examples/steps/06-finished.muro +30 -0
  96. package/examples/steps/README.md +25 -0
  97. package/examples/tower/L1.muro +80 -0
  98. package/examples/tower/L11.muro +49 -0
  99. package/examples/tower/L2.muro +71 -0
  100. package/examples/tower/L3.muro +31 -0
  101. package/examples/tower/assets.muro +14 -0
  102. package/examples/tower/main.muro +37 -0
  103. package/examples/tower/site-geometry.muro +7 -0
  104. package/examples/tower/site.muro +33 -0
  105. package/examples/tower/typical.muro +131 -0
  106. package/examples/two-rooms.canonical.json +30 -3
  107. package/examples/two-rooms.muro +11 -7
  108. package/package.json +29 -2
  109. package/src/axo.ts +258 -0
  110. package/src/check.ts +948 -122
  111. package/src/cli.ts +175 -12
  112. package/src/diff.ts +676 -0
  113. package/src/fabric.ts +123 -0
  114. package/src/graph.ts +451 -61
  115. package/src/index.ts +38 -2
  116. package/src/light.ts +4 -7
  117. package/src/mcp.ts +397 -0
  118. package/src/model.ts +462 -67
  119. package/src/parse-file.ts +28 -0
  120. package/src/parse.ts +682 -45
  121. package/src/plan.ts +152 -14
  122. package/src/poly.ts +295 -0
  123. package/src/site.ts +19 -11
  124. package/src/vertical.ts +884 -0
  125. package/.github/workflows/ci.yml +0 -16
  126. package/.zenodo.json +0 -20
  127. package/CITATION.cff +0 -26
  128. package/docs/decisions/0001-notation-v0.md +0 -29
  129. package/docs/decisions/0002-height-and-offsets.md +0 -27
  130. package/docs/decisions/0003-uncounted-divisions.md +0 -36
  131. package/docs/decisions/0004-typical-floors.md +0 -21
  132. package/docs/decisions/0005-zones-and-unions.md +0 -23
  133. package/docs/decisions/0006-voids-and-light.md +0 -31
  134. package/docs/decisions/0007-semi-outdoor-air.md +0 -27
  135. package/docs/decisions/0008-vocabulary-and-level-attr.md +0 -23
  136. package/docs/decisions/0009-site-and-exterior.md +0 -29
  137. package/docs/ifc-coverage.md +0 -82
  138. package/docs/ifcx-notes.md +0 -25
  139. package/docs/img/house-L1.svg +0 -80
  140. package/docs/img/house-L2.svg +0 -53
  141. package/docs/img/mansion-L1.svg +0 -113
  142. package/docs/img/mansion-L10.svg +0 -111
  143. package/docs/img/mansion-L5.svg +0 -160
  144. package/docs/img/office-L1.svg +0 -116
  145. package/docs/img/office-L2.svg +0 -109
  146. package/docs/img/two-rooms.svg +0 -39
  147. package/docs/log/2026-07-22.md +0 -13
  148. package/docs/log/2026-07-23.md +0 -51
  149. package/docs/roadmap.md +0 -55
  150. package/docs/writing-architecture.md +0 -119
  151. package/spec/notation-v0.md +0 -164
  152. package/spec/vocabulary.md +0 -62
  153. package/test/core.test.ts +0 -171
  154. package/test/design2.test.ts +0 -97
  155. package/test/house.test.ts +0 -74
  156. package/test/mansion.test.ts +0 -99
  157. package/test/office.test.ts +0 -71
  158. package/test/uncounted.test.ts +0 -120
  159. package/test/vertical.test.ts +0 -75
  160. package/tsconfig.json +0 -13
package/src/check.ts CHANGED
@@ -3,62 +3,425 @@
3
3
  // 高さ方向の一貫性 — BIMが3Dであることで暗黙に担保していたもの — は、
4
4
  // ここでは宣言された不変量 (天井高 + 上階slab ≤ 階高) の検査として明示的に持つ。
5
5
  // 吹抜け (type:void) はこの不変量の宣言的な免除である。
6
+ //
7
+ // 診断契約 (ADR-0016): 一次形式は checkDiagnostics の Diagnostic[] — code / severity /
8
+ // 日本語本文 / 出所 / 対象パス。check は互換層で、従来の文字列 (位置接頭辞つき) を組み立てる。
9
+ // severity はコードの不変属性 — 重さを変えたくなったら新コードを切る。
6
10
 
7
11
  import {
8
12
  placeBand,
9
13
  placeOpening,
10
14
  planOverlap,
11
15
  segmentsFor,
12
- sharedSegment,
16
+ segmentLength,
17
+ envelopeGaps,
18
+ drawnCut,
13
19
  spacesOverlap,
14
20
  } from "./graph.js";
15
- import { heff, isSemiOutdoor, levelsSorted, type Model, type Space } from "./model.js";
21
+ import { heff, isSemiOutdoor, levelsSorted, type Attrs, type Boundary, type Edge, type Level, type Model, type Pt, type Rect, type Space,
22
+ columnSites,
23
+ columnsFor,
24
+ pointInPolygon,
25
+ rectToPoly,
26
+ regionOf,
27
+ srcRef,
28
+ polygonAreaM2,
29
+ polygonSelfIntersection,
30
+ shapeEscapesPolygon,
31
+ } from "./model.js";
32
+ import { cutsInWindow } from "./poly.js";
33
+ import { runDecls, runIssues } from "./vertical.js";
16
34
 
17
35
  export interface CheckResult {
18
36
  errors: string[];
19
37
  warnings: string[];
20
38
  }
21
39
 
40
+ /** 構造化診断 (ADR-0016)。message は本文のみ — 位置接頭辞「file:N行目: 」を含まない */
41
+ export interface Diagnostic {
42
+ /** 台帳 DIAGNOSTIC_CODES のコード (領域2-3字 + 2桁連番) */
43
+ code: DiagnosticCode;
44
+ severity: "error" | "warning";
45
+ /** 日本語の本文 (位置接頭辞なし) */
46
+ message: string;
47
+ /** 出所の行 (位置を持たない診断は省略) */
48
+ line?: number;
49
+ /** 出所レイヤー (合成時) */
50
+ file?: string;
51
+ /** 対象の空間/ゾーン/polygonのパス (境界は両パス) */
52
+ path?: string[];
53
+ /** 関連位置 (重複の既出側・重なりの相手など) */
54
+ related?: Array<{ line: number; file?: string }>;
55
+ }
56
+
57
+ /**
58
+ * 診断コードの台帳 — 全コードと規範severity。specの表 (semantics.md §5) とテストで一致を守る。
59
+ * BND07 は欠番 — 「接しているのに境界が無い」警告はADR-0014 (既定境界) で廃止された。
60
+ * SYN01 は構文・合成エラー (SourceError) の写し — checkは例外を診断にしない。CLIの check --json だけが写す。
61
+ */
62
+ export const DIAGNOSTIC_CODES = {
63
+ REF01: "error", // 境界が未定義の空間パスを参照
64
+ BND01: "error", // 同一空間同士の境界
65
+ BND02: "error", // 同一空間対の境界の重複 (edge限定まで同一 — ADR-0013)
66
+ BND03: "error", // 異レベルの空間への壁境界
67
+ BND04: "error", // 接していない空間の境界
68
+ BND05: "warning", // 同一空間対でedge限定の有無が混在
69
+ BND06: "warning", // 境界線分がゼロ
70
+ LVL01: "error", // レベルzの重複
71
+ GEO01: "error", // 自らの領域 (合併の矩形) 同士の重なり
72
+ GEO02: "error", // 同一レベルの空間同士の重なり
73
+ VRT01: "error", // 垂直境界の前提 (領域とレベルを持つ空間同士)
74
+ VRT02: "error", // 非隣接レベル間の垂直境界
75
+ VRT03: "error", // 平面が重ならない垂直境界
76
+ VRT04: "warning", // void境界の上側が非void
77
+ VRT05: "warning", // 垂直境界の開口 (解釈されない)
78
+ VRT06: "warning", // 垂直境界のseg (解釈されない)
79
+ OPN01: "error", // hingeの軸違い
80
+ OPN02: "error", // 開口同士の重なり
81
+ OPN03: "warning", // open境界の開口 (通行に影響しない)
82
+ OPN04: "error", // 開口を置ける境界線分が無い
83
+ OPN05: "error", // 境界線分が複数で曖昧
84
+ OPN06: "error", // 開口の幅が線分長を超える
85
+ OPN07: "error", // 開口の明示位置の軸違い
86
+ OPN08: "error", // 開口の明示位置のはみ出し
87
+ SEG01: "error", // 領域を持たない空間へのarea
88
+ SEG02: "warning", // areaのはみ出し
89
+ SEG03: "warning", // open境界のseg (解釈されない)
90
+ SEG04: "error", // segを置ける境界線分が無い
91
+ SEG05: "error", // segの境界線分が複数で曖昧
92
+ SEG06: "error", // segの幅が線分長を超える
93
+ SEG07: "error", // segの明示位置の軸違い
94
+ SEG08: "error", // segの明示位置のはみ出し
95
+ ZON01: "warning", // 空のゾーン
96
+ ZON02: "warning", // ゾーンと同パスの空間
97
+ HGT01: "error", // 上階への食い込み (高さ不変量違反)
98
+ HGT02: "error", // 部分吹抜けの被覆不足
99
+ HGT03: "warning", // 上階slab未宣言で高さ検査ができない
100
+ HGT04: "warning", // 天井高不明で高さ検査ができない
101
+ HGT05: "warning", // レベルが特定できない領域つき空間
102
+ SIT01: "error", // 敷地形状の重複頂点
103
+ SIT02: "error", // 敷地形状の自己交差
104
+ SIT03: "error", // 建物の敷地形状からのはみ出し
105
+ SIT04: "warning", // 対応するゾーンの無いpolygon
106
+ SIT05: "warning", // 敷地面積の宣言と導出の食い違い
107
+ UID01: "error", // 数字だけのuid (ADR-0015)
108
+ UID02: "error", // 空白を含むuid
109
+ UID03: "error", // uidの重複
110
+ ATT01: "error", // 解釈される属性の値が数値でない (ADR-0028)
111
+ ATT02: "error", // 解釈される属性の値が台帳の語彙にない (ADR-0028)
112
+ DAY01: "error", // daylightの値が 0/1 以外 (ADR-0020)
113
+ RUN01: "error", // 一つの空間に縦動線の宣言が複数 (ADR-0021)
114
+ RUN02: "error", // 縦動線の値が上る向き (N/E/S/W) でない
115
+ RUN03: "error", // 縦動線の領域が矩形一つでない / レベルが不明
116
+ RUN04: "warning", // 上にレベルが無く縦動線の形が生成できない
117
+ RUN05: "error", // form の値が不正、または形が決まらない
118
+ RUN06: "warning", // 導出された段の寸法が窮屈 (書かないが検査する)
119
+ RUN07: "warning", // 導出された勾配が宣言・常用域から外れる
120
+ RUN08: "warning", // 縦動線の形はあるが上下を繋ぐ垂直境界が無い
121
+ LIN01: "error", // 描かれた線が二つの空間を分離しない (ADR-0022)
122
+ LIN02: "error", // 垂直境界に描かれた線
123
+ LIN03: "warning", // 描かれた線が何も切っていない
124
+ ENV01: "warning", // 外皮に穴 — 何にも面していない外周 (ADR-0025)
125
+ COL01: "warning", // 柱の宣言に対して立つ柱が0本 (ADR-0023)
126
+ COL02: "warning", // 同じ通りの交点に複数の柱宣言が重なる (先の宣言が勝つ)
127
+ VER01: "error", // koyu 0.1 での既定境界の導出 (ADR-0017)
128
+ VER02: "error", // koyu 0.3以前で採光の推定対象だった型に daylight が無い (ADR-0020)
129
+ VER03: "error", // koyu 0.4以前のファイルに0.5の語 (縦動線・線・柱・地下)
130
+ SYN01: "error", // 構文・合成エラー (SourceError の写し — check --json のみ)
131
+ } as const satisfies Record<string, "error" | "warning">;
132
+
133
+ /**
134
+ * 診断コードの型 (ADR-0016)。台帳が唯一の出所であり、**登録していないコードは型が通らない**。
135
+ * 以前は Record<string, ...> だったので、台帳に無いコードを emit すると severity が
136
+ * undefined になり、error が黙って warning に落ち、--strict でも終了コード0になった。
137
+ * 「忘れる自由は無い」という契約を、文ではなく型が守る。
138
+ */
139
+ export type DiagnosticCode = keyof typeof DIAGNOSTIC_CODES;
140
+
22
141
  const EPS = 0.5;
142
+ /** 敷地まわりの幾何の許容 (境界上は内側扱い) — ADR-0011の1mm */
143
+ const EPS_SITE = 1;
23
144
  const VERTICAL = new Set(["stair", "shaft", "void"]);
145
+ /** 0.3以前が採光の対象と推定していた型 (ADR-0020で廃止)。旧版の受理条件の判定にだけ使う — 意味論には効かない */
146
+ const LEGACY_DAYLIT = new Set(["unit", "room", "ldk", "bedroom", "living"]);
147
+
148
+ /** 互換層 — 従来の文字列形式。位置を持つ診断は「file:N行目: 本文」に組み立てる */
149
+ /**
150
+ * 解釈される属性の値の台帳 (ADR-0028)。**spec/vocabulary.md の★が契約であり、これはその写しである。**
151
+ *
152
+ * `of` があれば列挙、無ければ正の数値。ここに無い属性は自由に書けてそのまま運ばれる
153
+ * (台帳の規則3)。daylight は DAY01 が、form は RUN05 が、縦動線の向きは RUN02 が
154
+ * 既に守っているので重ねない。
155
+ */
156
+ const ATTR_RULES: Record<string, Record<string, { of?: Array<string | number> }>> = {
157
+ space: {
158
+ h: {},
159
+ ceiling: { of: [0, 1] },
160
+ turn: { of: ["R", "L"] },
161
+ riser: {},
162
+ tread: {},
163
+ entry: {},
164
+ landing: {},
165
+ lane: {},
166
+ slope: {},
167
+ road: {},
168
+ },
169
+ zone: { site: { of: [0, 1] }, area: {} },
170
+ // air:1 の境界の天端高 (手すり・腰壁) — 立体が読むので台帳に載る (ADR-0028)
171
+ boundary: { h: {} },
172
+ opening: { style: { of: ["hinged", "sliding", "auto"] } },
173
+ };
174
+
175
+ interface AttrSubject {
176
+ rules: Record<string, { of?: Array<string | number> }>;
177
+ of: Attrs;
178
+ }
179
+
180
+ /** 値を検査すべき宣言を、出所つきで数え上げる — 母集団は**書かれた宣言**である */
181
+ function attrSubjects(
182
+ model: Model,
183
+ ): Array<[string, AttrSubject, { line?: number; file?: string; path?: string[] }]> {
184
+ const out: Array<[string, AttrSubject, { line?: number; file?: string; path?: string[] }]> = [];
185
+ for (const s of model.spaces.values()) {
186
+ out.push([s.path, { rules: ATTR_RULES["space"]!, of: s.attrs }, { line: s.line, file: s.file, path: [s.path] }]);
187
+ }
188
+ for (const z of model.zones.values()) {
189
+ out.push([`ゾーン ${z.path}`, { rules: ATTR_RULES["zone"]!, of: z.attrs }, { line: z.line, file: z.file, path: [z.path] }]);
190
+ }
191
+ for (const b of model.boundaries) {
192
+ const at = { line: b.line, file: b.file, path: [b.a, b.b] };
193
+ out.push([`境界 ${b.a} | ${b.b}`, { rules: ATTR_RULES["boundary"]!, of: b.attrs }, at]);
194
+ for (const o of b.openings) {
195
+ out.push([
196
+ `${o.kind} (${b.a} | ${b.b})`,
197
+ { rules: ATTR_RULES["opening"]!, of: o.attrs },
198
+ { line: o.line, file: b.file, path: [b.a, b.b] },
199
+ ]);
200
+ }
201
+ }
202
+ return out;
203
+ }
204
+
205
+ /** 書かれた通り語で矩形を名指す — 「どの組が重なっているか」を言えるようにする */
206
+ function gridRefText(s: Space, i: number): string {
207
+ const g = s.grids[i];
208
+ if (!g) return `#${i + 1}`;
209
+ return `${g.xa}..${g.xb} ${g.ya}..${g.yb}`;
210
+ }
24
211
 
25
212
  export function check(model: Model): CheckResult {
26
213
  const errors: string[] = [];
27
214
  const warnings: string[] = [];
215
+ for (const d of checkDiagnostics(model)) {
216
+ const text = d.line !== undefined ? `${srcRef(d.line, d.file)}: ${d.message}` : d.message;
217
+ (d.severity === "error" ? errors : warnings).push(text);
218
+ }
219
+ return { errors, warnings };
220
+ }
221
+
222
+ /**
223
+ * 節が共有する文脈。**節をまたいで読まれる値だけ**がここに載る —
224
+ * seenBoundary・pairEdges・siteZones・envelopedLevels・stood・uidOwners・
225
+ * voidPartners・byLevel はどれも一つの節の内側で閉じているので、載せない。
226
+ */
227
+ /** 診断の出所 (境界に対する診断が共有する) */
228
+ type At = { line?: number; file?: string; path?: string[]; related?: Array<{ line: number; file?: string }> };
229
+
230
+ interface Ctx {
231
+ model: Model;
232
+ emit: (
233
+ code: DiagnosticCode,
234
+ message: string,
235
+ at?: {
236
+ line?: number;
237
+ file?: string;
238
+ path?: string[];
239
+ related?: Array<{ line: number; file?: string }>;
240
+ },
241
+ ) => void;
242
+ loc: (line: number, file?: string) => { line: number; file?: string };
243
+ /** 領域を持つ空間 (7つの節が読む) */
244
+ withRect: Space[];
245
+ /** z昇順のレベル */
246
+ levels: Level[];
247
+ /** レベル名 → z昇順の添字 (境界の妥当性と高さの節が読む) */
248
+ levelIndex: Map<string, number>;
249
+ }
250
+
251
+ export function checkDiagnostics(model: Model): Diagnostic[] {
252
+ const diags: Diagnostic[] = [];
253
+ /** line:0 は「位置なし」— フィールドごと省略する (既定境界などの導出物) */
254
+ const emit = (
255
+ code: DiagnosticCode,
256
+ message: string,
257
+ at: {
258
+ line?: number;
259
+ file?: string;
260
+ path?: string[];
261
+ related?: Array<{ line: number; file?: string }>;
262
+ } = {},
263
+ ) => {
264
+ diags.push({
265
+ code,
266
+ severity: DIAGNOSTIC_CODES[code],
267
+ message,
268
+ ...(at.line ? { line: at.line } : {}),
269
+ ...(at.line && at.file !== undefined ? { file: at.file } : {}),
270
+ ...(at.path ? { path: at.path } : {}),
271
+ ...(at.related ? { related: at.related } : {}),
272
+ });
273
+ };
274
+ const loc = (line: number, file?: string): { line: number; file?: string } => ({
275
+ line,
276
+ ...(file !== undefined ? { file } : {}),
277
+ });
28
278
 
279
+
280
+ const withRect = [...model.spaces.values()].filter((s) => s.rects.length > 0);
281
+ const levels = levelsSorted(model);
282
+ const levelIndex = new Map(levels.map((l, i) => [l.name, i]));
283
+ const ctx: Ctx = { model, emit, loc, withRect, levels, levelIndex };
284
+
285
+ // **節の順序が出力の順序である。**互換層は診断を出た順に文字列へ写すので、
286
+ // ここの並びを入れ替えると check の出力が変わる (test/diagnostics.test.ts が固定している)。
287
+ // 節の粒度は「走査単位」であって「診断コードの族」ではない — 一つのループが
288
+ // 複数のコードを出すとき、それらは走査の順に交互に出る。族で割ると並びが崩れる
289
+ checkBoundaryRefs(ctx);
290
+ checkBoundaryIdentity(ctx);
291
+ checkLevelDepth(ctx);
292
+ checkSelfOverlap(ctx);
293
+ checkSpaceOverlap(ctx);
294
+ checkDaylightScope(ctx);
295
+ checkAttrValues(ctx);
296
+ checkRuns(ctx);
297
+ checkDrawnLines(ctx);
298
+ checkEnvelopeGaps(ctx);
299
+ checkColumns(ctx);
300
+ checkLanguageVersion(ctx);
301
+ checkUids(ctx);
302
+ checkBoundaryValidity(ctx);
303
+ checkAreas(ctx);
304
+ checkZones(ctx);
305
+ checkHeights(ctx);
306
+ checkOrphanSpaces(ctx);
307
+ checkSite(ctx);
308
+ return diags;
309
+ }
310
+
311
+
312
+ /** 吹抜けが下階の空間の平面をどれだけ覆うか (0..1) */
313
+ function voidCoverage(s: Space, partners: Space[]): number {
314
+ let inter = 0;
315
+ for (const r of s.rects) {
316
+ for (const p of partners) {
317
+ for (const pr of p.rects) {
318
+ const o = planOverlap(r, pr);
319
+ if (o) inter += (o.x2 - o.x1) * (o.y2 - o.y1);
320
+ }
321
+ }
322
+ }
323
+ const area = s.rects.reduce((sum, r) => sum + (r.x2 - r.x1) * (r.y2 - r.y1), 0);
324
+ return area > 0 ? inter / area : 0;
325
+ }
326
+
327
+ /** 境界の参照先 — REF01 / BND01 */
328
+ function checkBoundaryRefs(ctx: Ctx): void {
329
+ const { model, emit, loc, withRect, levels, levelIndex } = ctx;
29
330
  // 境界の参照先
30
331
  for (const b of model.boundaries) {
31
332
  for (const p of [b.a, b.b]) {
32
333
  if (!model.spaces.has(p)) {
33
- errors.push(`${b.line}行目: 未定義の空間を参照しています: ${p}`);
334
+ emit("REF01", `未定義の空間を参照しています: ${p}`, { line: b.line, file: b.file, path: [b.a, b.b] });
34
335
  }
35
336
  }
36
337
  if (b.a === b.b) {
37
- errors.push(`${b.line}行目: 同じ空間同士の境界は書けません: ${b.a}`);
338
+ emit("BND01", `同じ空間同士の境界は書けません: ${b.a}`, { line: b.line, file: b.file, path: [b.a, b.b] });
38
339
  }
39
340
  }
341
+ }
40
342
 
41
- const withRect = [...model.spaces.values()].filter((s) => s.rects.length > 0);
42
- const levels = levelsSorted(model);
343
+ /** 境界の同一性 — BND02 / BND05 */
344
+ function checkBoundaryIdentity(ctx: Ctx): void {
345
+ const { model, emit, loc, withRect, levels, levelIndex } = ctx;
346
+ // 境界の同一性: 同じ空間対 (edge限定まで同一) の重複宣言は矛盾の温床 — エラー (ADR-0013)。
347
+ // wall/openの食い違いもこの検査が捕まえる。edge限定の有無が混在する対は線分が重なるため警告
348
+ const seenBoundary = new Map<string, Boundary>();
349
+ const pairEdges = new Map<string, Set<string>>();
350
+ for (const b of model.boundaries) {
351
+ const pair = [b.a, b.b].sort().join(" | ");
352
+ // 描かれた線を持つ境界は、線そのものが実現なので同一性の鍵に線の綴りが入る。
353
+ // 同じ空間対に二本の線 (二箇所の隅切りなど) を引くのは矛盾ではない
354
+ const key = `${pair}#${b.edge ?? ""}#${b.drawn ? `${b.drawn.aRef}..${b.drawn.bRef}` : ""}`;
355
+ const prev = seenBoundary.get(key);
356
+ if (prev) {
357
+ emit(
358
+ "BND02",
359
+ `境界が重複しています: ${pair}${b.edge ? ` edge:${b.edge}` : ""} (既出: ${srcRef(prev.line, prev.file)})`,
360
+ { line: b.line, file: b.file, path: [b.a, b.b], related: [loc(prev.line, prev.file)] },
361
+ );
362
+ } else {
363
+ seenBoundary.set(key, b);
364
+ }
365
+ // 線を持つ境界は外周の残りを取らない (線が線分そのもの) ので、edge の混在の話に入れない
366
+ if (!b.drawn) {
367
+ const set = pairEdges.get(pair) ?? new Set<string>();
368
+ set.add(b.edge ?? "");
369
+ pairEdges.set(pair, set);
370
+ }
371
+ }
372
+ for (const [pair, edges] of pairEdges) {
373
+ if (edges.has("") && edges.size > 1) {
374
+ // 集合に対する診断でも、集合を作った宣言の行は必ず指す —
375
+ // 「どこかで併存している」だけでは直す場所が無い
376
+ const members = model.boundaries.filter((b) => !b.drawn && [b.a, b.b].sort().join(" | ") === pair);
377
+ const first = members.find((b) => !b.edge) ?? members[0];
378
+ emit("BND05", `同じ空間対に edge 限定つきと無しの境界が併存しています (線分が重なります): ${pair}`, {
379
+ ...(first ? { line: first.line, file: first.file } : {}),
380
+ path: pair.split(" | "),
381
+ related: members.filter((b) => b !== first).map((b) => loc(b.line, b.file)),
382
+ });
383
+ }
384
+ }
385
+ }
43
386
 
387
+ /** レベルの重複 — LVL01 */
388
+ function checkLevelDepth(ctx: Ctx): void {
389
+ const { model, emit, loc, withRect, levels, levelIndex } = ctx;
44
390
  // レベルの重複
45
391
  for (let i = 1; i < levels.length; i++) {
46
392
  if (Math.abs(levels[i]!.z - levels[i - 1]!.z) < EPS) {
47
- errors.push(`レベル ${levels[i - 1]!.name} と ${levels[i]!.name} のzが同じです`);
393
+ emit("LVL01", `レベル ${levels[i - 1]!.name} と ${levels[i]!.name} のzが同じです`, {
394
+ line: levels[i]!.line,
395
+ file: levels[i]!.file,
396
+ related: [loc(levels[i - 1]!.line, levels[i - 1]!.file)],
397
+ });
48
398
  }
49
399
  }
400
+ }
50
401
 
402
+ /** 自らの領域の重なり — GEO01 */
403
+ function checkSelfOverlap(ctx: Ctx): void {
404
+ const { model, emit, loc, withRect, levels, levelIndex } = ctx;
51
405
  // 自らの領域 (合併の矩形同士) の重なり
52
406
  for (const s of withRect) {
53
407
  for (let i = 0; i < s.rects.length; i++) {
54
408
  for (let j = i + 1; j < s.rects.length; j++) {
55
409
  if (planOverlap(s.rects[i]!, s.rects[j]!)) {
56
- errors.push(`${s.line}行目: ${s.path} の領域同士が重なっています`);
410
+ // どの組かを言わなければ、三つ重なる空間にバイト同一の診断が三件並ぶ
411
+ emit("GEO01", `${s.path} の領域同士が重なっています: ${gridRefText(s, i)} と ${gridRefText(s, j)}`, {
412
+ line: s.line,
413
+ file: s.file,
414
+ path: [s.path],
415
+ });
57
416
  }
58
417
  }
59
418
  }
60
419
  }
420
+ }
61
421
 
422
+ /** 空間同士の重なり — GEO02 */
423
+ function checkSpaceOverlap(ctx: Ctx): void {
424
+ const { model, emit, loc, withRect, levels, levelIndex } = ctx;
62
425
  // 同一レベルでの空間同士の重なり
63
426
  for (let i = 0; i < withRect.length; i++) {
64
427
  for (let j = i + 1; j < withRect.length; j++) {
@@ -66,146 +429,536 @@ export function check(model: Model): CheckResult {
66
429
  const b = withRect[j]!;
67
430
  if (a.level !== b.level) continue;
68
431
  if (spacesOverlap(a, b)) {
69
- errors.push(`空間の領域が重なっています: ${a.path} と ${b.path}`);
432
+ emit("GEO02", `空間の領域が重なっています: ${a.path} と ${b.path}`, {
433
+ line: a.line,
434
+ file: a.file,
435
+ path: [a.path, b.path],
436
+ related: [loc(b.line, b.file)],
437
+ });
70
438
  }
71
439
  }
72
440
  }
441
+ }
73
442
 
74
- // 接しているのに境界が宣言されていない組 (同一レベル)
75
- const declared = new Set(model.boundaries.map((b) => [b.a, b.b].sort().join("|")));
76
- for (let i = 0; i < withRect.length; i++) {
77
- for (let j = i + 1; j < withRect.length; j++) {
78
- const a = withRect[i]!;
79
- const b = withRect[j]!;
80
- if (a.level !== b.level) continue;
81
- const touching = a.rects.some((ra) => b.rects.some((rb) => sharedSegment(ra, rb)));
82
- if (!touching) continue;
83
- if (!declared.has([a.path, b.path].sort().join("|"))) {
84
- warnings.push(`接しているのに境界が宣言されていません: ${a.path} | ${b.path}`);
443
+ /** 採光の対象の宣言 — DAY01 */
444
+ function checkDaylightScope(ctx: Ctx): void {
445
+ const { model, emit, loc, withRect, levels, levelIndex } = ctx;
446
+ // 採光の対象の宣言 (ADR-0020): daylight は「この室に 1/7 の判定を掛ける」という二値の宣言。
447
+ // 綴りの揺れ (daylight:true / daylight:yes) が黙って対象外に落ちるのを防ぐ
448
+ for (const s of model.spaces.values()) {
449
+ const v = s.attrs["daylight"];
450
+ if (v === undefined) continue;
451
+ if (v !== 0 && v !== 1) {
452
+ emit("DAY01", `daylight は 1 (採光判定の対象) か 0 (対象外) です: ${s.path} に daylight:${v}`, {
453
+ line: s.line,
454
+ file: s.file,
455
+ path: [s.path],
456
+ });
457
+ }
458
+ }
459
+ }
460
+
461
+ /** 解釈される属性の値 — ATT01 / ATT02 */
462
+ function checkAttrValues(ctx: Ctx): void {
463
+ const { model, emit, loc, withRect, levels, levelIndex } = ctx;
464
+ // 解釈される属性の値 (ADR-0028): **書いたのに解釈されなかった値は、黙って既定へ落とさない。**
465
+ //
466
+ // daylight だけが DAY01 で守られていて、他の★属性は素通りだった。帰結は黙殺である —
467
+ // `site:yes` は敷地の検査 (SIT03 error) を丸ごと無効にし、`h:35OO` は高さ不変量
468
+ // (HGT01 error) を消し、`ceiling:none` は天井を張り、`turn:l` は階段を鏡像にする。
469
+ // どれも check は緑のままだった。台帳 (spec/vocabulary.md) が契約である以上、
470
+ // 台帳の型に合わない値はエラーである。
471
+ for (const [where, attrs, at] of attrSubjects(model)) {
472
+ for (const [key, rule] of Object.entries(attrs.rules)) {
473
+ const v = attrs.of[key];
474
+ if (v === undefined) continue;
475
+ if (rule.of) {
476
+ if (!rule.of.includes(v as string | number)) {
477
+ emit("ATT02", `${where} の ${key} は ${rule.of.join(" / ")} のどれかです: ${key}:${v}`, at);
478
+ }
479
+ } else if (typeof v !== "number" || !(v > 0)) {
480
+ emit("ATT01", `${where} の ${key} は正の数値で書きます: ${key}:${v}`, at);
85
481
  }
86
482
  }
87
483
  }
484
+ }
88
485
 
89
- // 境界の妥当性
90
- const levelIndex = new Map(levels.map((l, i) => [l.name, i]));
486
+ /** 縦動線 — RUN01〜RUN08 */
487
+ function checkRuns(ctx: Ctx): void {
488
+ const { model, emit, loc, withRect, levels, levelIndex } = ctx;
489
+ // 縦動線 (ADR-0021): 宣言の妥当性と、**書かれていない導出値**の妥当性。
490
+ // 段数も踏面も勾配も書かない — だから導出したものを検査する
491
+ for (const i of runIssues(model)) {
492
+ emit(i.code, i.message, { line: i.line, file: i.file, path: [i.path] });
493
+ }
494
+ }
495
+
496
+ /** 描かれた線 — LIN01〜LIN03 */
497
+ function checkDrawnLines(ctx: Ctx): void {
498
+ const { model, emit, loc, withRect, levels, levelIndex } = ctx;
499
+ // 描かれた線 (ADR-0022): 線は二つの空間を実際に分離しなければならない。
500
+ // 「宣言どおりに切れているか」は arrangement の検査であり、check の仕事
501
+ for (const b of model.boundaries) {
502
+ if (!b.drawn) continue;
503
+ const bAt = { line: b.drawn.line, file: b.file, path: [b.a, b.b] };
504
+ if (VERTICAL.has(b.kind)) {
505
+ emit("LIN02", `垂直境界に線は描けません (線は平面を区切る行為です): ${b.a} | ${b.b}`, bAt);
506
+ continue;
507
+ }
508
+ const sa = model.spaces.get(b.a);
509
+ const sb = model.spaces.get(b.b);
510
+ if (!sa || !sb) continue;
511
+ if (sa.rects.length === 0 && sb.rects.length === 0) {
512
+ emit("LIN01", `領域を持たない空間同士に線は描けません: ${b.a} | ${b.b}`, bAt);
513
+ continue;
514
+ }
515
+ // 判定と操作を同じ関数に通す (ADR-0027)。窓の中で分離が決まらなければ LIN01、
516
+ // 決まるが実際には何も切っていなければ LIN03。以前は判定だけが別の窓 (線分の
517
+ // 外接矩形) を使っていたので、軸平行の線では窓が潰れて必ず誤報していた
518
+ const cut = drawnCut(sa, sb, b.drawn.a, b.drawn.b);
519
+ if (!cut) {
520
+ emit(
521
+ "LIN01",
522
+ sa.rects.length > 0 && sb.rects.length > 0
523
+ ? `線 ${b.drawn.aRef}..${b.drawn.bRef} は ${b.a} と ${b.b} を分離していません (二つの割付が線の両側に来るように引きます)`
524
+ : `線 ${b.drawn.aRef}..${b.drawn.bRef} が割付をちょうど二等分していて、どちらを残すか決まりません`,
525
+ bAt,
526
+ );
527
+ continue;
528
+ }
529
+ const target = cut.solo ? [cut.solo] : [sa, sb];
530
+ if (!target.some((s) => cutsInWindow(s.rects.map(rectToPoly), cut.window, b.drawn!.a, b.drawn!.b))) {
531
+ emit(
532
+ "LIN03",
533
+ `線 ${b.drawn.aRef}..${b.drawn.bRef} は何も切っていません (既定の隣接線と同じか、割付の外にあります)`,
534
+ bAt,
535
+ );
536
+ }
537
+ }
538
+ }
539
+
540
+ /** 外皮の穴 — ENV01 */
541
+ function checkEnvelopeGaps(ctx: Ctx): void {
542
+ const { model, emit, loc, withRect, levels, levelIndex } = ctx;
543
+ // 外皮の穴 (ADR-0025): 既定境界は領域を持たない空間との間には導かれない (ADR-0014) ので、
544
+ // 外部への境界の書き忘れは黙って壁の不在になる。導出された外周のうち、
545
+ // 他の空間とも宣言された境界とも向かい合っていない区間を数える。
546
+ // 外構のタイル (site:1 ゾーンの配下)・外部・半屋外は囲われていないのが正常なので数えない。
547
+ // そして**外皮を書き始めているレベルだけ**を見る — 外部への境界が一本も無い階は
548
+ // 外皮をまだ模型にしていないだけであって、穴が開いているのではない。
549
+ // 「書き始めたなら閉じきる」という整合の検査であって、完全性の要求ではない
550
+ const siteZones = [...model.zones.values()].filter((z) => z.attrs["site"] === 1).map((z) => z.path);
551
+ const envelopedLevels = new Set<string>();
91
552
  for (const b of model.boundaries) {
553
+ if (b.derived || VERTICAL.has(b.kind)) continue;
92
554
  const sa = model.spaces.get(b.a);
93
555
  const sb = model.spaces.get(b.b);
94
556
  if (!sa || !sb) continue;
557
+ const outer = sa.rects.length === 0 ? sb : sb.rects.length === 0 ? sa : undefined;
558
+ if (outer?.level) envelopedLevels.add(outer.level);
559
+ }
560
+ for (const s of withRect) {
561
+ if (!s.level || !envelopedLevels.has(s.level)) continue;
562
+ if (s.type === "exterior" || isSemiOutdoor(model, s)) continue;
563
+ if (siteZones.some((z) => s.path.startsWith(z + "/"))) continue;
564
+ const gaps = envelopeGaps(model, s);
565
+ if (gaps.length === 0) continue;
566
+ // **どの辺かを言う。**合計長だけでは、edge を書き分けている図面で直す辺が特定できない。
567
+ // envelopeGaps は座標つきの線分を返しているので、方角は既に手元にある
568
+ const byDir = new Map<string, number>();
569
+ for (const g of gaps) {
570
+ const d = g.edgeOfA ?? (g.horizontal ? "N/S" : "E/W");
571
+ byDir.set(d, (byDir.get(d) ?? 0) + segmentLength(g));
572
+ }
573
+ const total = gaps.reduce((a, g) => a + segmentLength(g), 0);
574
+ const where = [...byDir].map(([d, mm]) => `${d} ${Math.round(mm)}mm`).join(" / ");
575
+ emit(
576
+ "ENV01",
577
+ `外皮に面していない外周があります: ${s.path} — ${where} (合計 ${Math.round(total)}mm・${gaps.length}区間)。外部への境界を書きます`,
578
+ { line: s.line, file: s.file, path: [s.path] },
579
+ );
580
+ }
581
+ }
95
582
 
96
- if (VERTICAL.has(b.kind)) {
97
- // 垂直境界: 隣り合うレベルの、平面で重なる空間同士にしか張れない
98
- if (sa.rects.length === 0 || sb.rects.length === 0 || !sa.level || !sb.level) {
99
- errors.push(`${b.line}行目: ${b.kind} 境界は領域とレベルを持つ空間同士に書きます`);
100
- continue;
101
- }
102
- const ia = levelIndex.get(sa.level);
103
- const ib = levelIndex.get(sb.level);
104
- if (ia === undefined || ib === undefined || Math.abs(ia - ib) !== 1) {
105
- errors.push(
106
- `${b.line}行目: ${b.kind} 境界は隣り合うレベルの間に書きます: ${b.a} | ${b.b}`,
583
+ /** 柱 — COL01 / COL02 */
584
+ function checkColumns(ctx: Ctx): void {
585
+ const { model, emit, loc, withRect, levels, levelIndex } = ctx;
586
+ // 柱 (ADR-0023): 位置は書かれない。宣言に対して一本も立たなければ、
587
+ // 通りか階の指定が実際の床とすれ違っている
588
+ //
589
+ // **母集団は宣言である。**「このレベルに何本立ったか」ではなく「この宣言から何本立ったか」を
590
+ // 問う。前者を数えると、同じ階の別の宣言が一本でも立てた瞬間に、一本も立たない宣言が
591
+ // 黙って通る (ADR-0028)。Column.decl がその帰属を持つ。
592
+ const stood = new Map<number, number>();
593
+ const levelsOfDecl = new Set<string>();
594
+ for (const c of model.columns) for (const lv of c.levels) levelsOfDecl.add(lv);
595
+ for (const lv of levelsOfDecl) {
596
+ for (const col of columnsFor(model, lv)) stood.set(col.decl, (stood.get(col.decl) ?? 0) + 1);
597
+ }
598
+ for (let ci = 0; ci < model.columns.length; ci++) {
599
+ const c = model.columns[ci]!;
600
+ if ((stood.get(ci) ?? 0) > 0) continue;
601
+ const at = { line: c.line, file: c.file };
602
+ // 一本も立たない理由は二つある。**狙う交点に床が無い**か、床はあるが先の宣言に
603
+ // 取られたか。後者を「床がありません」と言うと、直しようのない場所へ人を送る
604
+ const sites = new Set(c.levels.flatMap((lv) => columnSites(model, c, lv).map((p) => `${lv}|${p.grid}`)));
605
+ if (sites.size > 0) {
606
+ // 影を作ったのは、この宣言と同じ交点に**実際に立った**先の宣言だけである
607
+ const shadow = model.columns.filter((o, oi) => {
608
+ if (oi >= ci) return false;
609
+ return o.levels.some((lv) =>
610
+ columnsFor(model, lv).some((k) => k.decl === oi && sites.has(`${lv}|${k.grid}`)),
107
611
  );
108
- } else if (!spacesOverlap(sa, sb)) {
109
- errors.push(
110
- `${b.line}行目: ${b.kind} 境界の空間が平面上で重なっていません: ${b.a} | ${b.b}`,
612
+ });
613
+ emit(
614
+ "COL02",
615
+ `この柱の宣言 (${c.levels.join(",")} ${c.size}角) は同じ交点を先の宣言に取られていて、一本も立ちません (同じ交点では先の宣言が勝ちます)`,
616
+ { ...at, related: shadow.map((o) => loc(o.line, o.file)) },
617
+ );
618
+ } else {
619
+ emit(
620
+ "COL01",
621
+ `柱の宣言に対して立つ柱がありません (通りの交点に床がありません): ${c.levels.join(",")} ${c.size}角`,
622
+ at,
623
+ );
624
+ }
625
+ }
626
+ }
627
+
628
+ /** 言語版の受理条件 — VER01〜VER03 */
629
+ function checkLanguageVersion(ctx: Ctx): void {
630
+ const { model, emit, loc, withRect, levels, levelIndex } = ctx;
631
+ // 言語版の受理条件 (ADR-0017): 旧版は意味保存の場合のみ受理する。
632
+ // 既定境界 (ADR-0014) が導出されるファイルは、0.1の意味 (境界なし+警告) と食い違う — エラーで二択を示す
633
+ if (model.version === "0.1") {
634
+ for (const b of model.boundaries) {
635
+ if (b.derived) {
636
+ emit(
637
+ "VER01",
638
+ `koyu 0.1 のファイルに境界が宣言されていない接触ペアがあります: ${b.a} | ${b.b} — 0.2では既定の壁が導出され意味が変わります。境界を宣言するか、koyu 0.2 へ上げます`,
639
+ { path: [b.a, b.b] },
111
640
  );
112
641
  }
113
- if (b.kind === "void") {
114
- const upper = (ia ?? 0) > (ib ?? 0) ? sa : sb;
115
- if (upper.type !== "void") {
116
- warnings.push(
117
- `${b.line}行目: void境界の上側は type:void の空間を想定しています: ${upper.path}`,
118
- );
119
- }
642
+ }
643
+ }
644
+
645
+ // 0.3以前は型から採光の対象を推定していた (ADR-0020で廃止)。推定対象だった型の空間に daylight が
646
+ // 書かれていなければ、0.4では判定から黙って外れる — 意味が変わるのでエラーで二択を示す
647
+ if (["0.1", "0.2", "0.3"].includes(model.version)) {
648
+ for (const s of model.spaces.values()) {
649
+ if (!LEGACY_DAYLIT.has(s.type) || s.attrs["daylight"] !== undefined) continue;
650
+ emit(
651
+ "VER02",
652
+ `koyu ${model.version} のファイルに daylight の無い ${s.type} があります: ${s.path} — 0.4では型から採光の対象を推定しないので判定から外れます。daylight:1 (対象) か daylight:0 (対象外) を書いてから koyu 0.4 へ上げます`,
653
+ { line: s.line, file: s.file, path: [s.path] },
654
+ );
655
+ }
656
+ }
657
+
658
+ // 0.5 で入った語 (縦動線の宣言・描かれた線・柱・地下) は 0.4 以前の処理系が知らない。
659
+ // 知らない処理系では黙って形が生成されないので、版を上げずに使うのはエラー (ADR-0017 決定3)
660
+ if (model.version !== "0.5") {
661
+ const older = `koyu ${model.version} のファイルに 0.5 の語があります`;
662
+ for (const s of model.spaces.values()) {
663
+ const d = runDecls(s);
664
+ if (d.length > 0) {
665
+ emit("VER03", `${older}: ${s.path} の ${d[0]!.device}: (縦動線) — koyu 0.5 へ上げます`, {
666
+ line: s.line,
667
+ file: s.file,
668
+ path: [s.path],
669
+ });
120
670
  }
121
- if (b.openings.length > 0) {
122
- warnings.push(`${b.line}行目: 垂直境界の開口は解釈されません`);
671
+ }
672
+ for (const b of model.boundaries) {
673
+ if (b.drawn) {
674
+ emit("VER03", `${older}: ${b.a} | ${b.b} の line (描かれた線) — koyu 0.5 へ上げます`, {
675
+ line: b.drawn.line,
676
+ file: b.file,
677
+ path: [b.a, b.b],
678
+ });
123
679
  }
124
- if (b.segs.length > 0) {
125
- warnings.push(`${b.line}行目: 垂直境界の seg は解釈されません`);
680
+ }
681
+ for (const c of model.columns) {
682
+ emit("VER03", `${older}: column (柱) — koyu 0.5 へ上げます`, { line: c.line, file: c.file });
683
+ }
684
+ for (const l of Object.values(model.levels)) {
685
+ if (l.underground) {
686
+ emit("VER03", `${older}: level ${l.name} の underground: — koyu 0.5 へ上げます`, {
687
+ line: l.line,
688
+ file: l.file,
689
+ });
126
690
  }
127
- continue;
128
691
  }
692
+ }
693
+ }
129
694
 
130
- // 水平境界
131
- if (sa.rects.length > 0 && sb.rects.length > 0 && sa.level !== sb.level) {
132
- errors.push(
133
- `${b.line}行目: 異なるレベルの空間に壁境界は書けません (垂直は type:stair/shaft/void): ${b.a} | ${b.b}`,
695
+ /** uid — UID01〜UID03 */
696
+ function checkUids(ctx: Ctx): void {
697
+ const { model, emit, loc, withRect, levels, levelIndex } = ctx;
698
+ // uid (ADR-0015): 不透明トークン、space/zone横断でモデル全体一意。
699
+ // 数字だけの形は禁じる — parseの数値化で 0123 が 123 になり、書いたトークンの区別が失われる
700
+ const uidOwners = new Map<string, Array<{ kind: string; path: string; line: number; file?: string }>>();
701
+ const collectUid = (kind: string, path: string, attrs: Attrs, line: number, file?: string) => {
702
+ const v = attrs["uid"];
703
+ if (v === undefined) return;
704
+ if (typeof v === "number") {
705
+ emit("UID01", `uid は数字だけのトークンにできません: uid:${v} (sp-${v} のような形にします)`, {
706
+ line,
707
+ file,
708
+ path: [path],
709
+ });
710
+ return;
711
+ }
712
+ if (v === "" || /\s/.test(v)) {
713
+ emit("UID02", `uid に空白は使えません: "${v}"`, { line, file, path: [path] });
714
+ return;
715
+ }
716
+ const arr = uidOwners.get(v) ?? [];
717
+ arr.push({ kind, path, line, file });
718
+ uidOwners.set(v, arr);
719
+ };
720
+ for (const s of model.spaces.values()) collectUid("space", s.path, s.attrs, s.line, s.file);
721
+ for (const z of model.zones.values()) collectUid("zone", z.path, z.attrs, z.line, z.file);
722
+ for (const [uid, owners] of uidOwners) {
723
+ if (owners.length > 1) {
724
+ emit(
725
+ "UID03",
726
+ `uid が重複しています: ${uid} (${owners.map((o) => `${o.kind} ${o.path} — ${srcRef(o.line, o.file)}`).join(", ")})`,
727
+ { path: owners.map((o) => o.path), related: owners.map((o) => loc(o.line, o.file)) },
134
728
  );
135
- continue;
136
729
  }
137
- const segs = segmentsFor(model, b);
138
- if (sa.rects.length > 0 && sb.rects.length > 0 && segs.length === 0) {
139
- errors.push(`${b.line}行目: 空間が接していないため境界を導けません: ${b.a} | ${b.b}`);
730
+ }
731
+ }
732
+
733
+ /** 境界の妥当性 — VRT / BND03〜06 / OPN / SEG */
734
+ function checkBoundaryValidity(ctx: Ctx): void {
735
+ const { model, emit } = ctx;
736
+ // 境界の妥当性。**一本のループを保つ** — 一つの境界が出す複数のコードは
737
+ // 走査の順に固まって出るので、コードの族で節に割ると並びが崩れる (ADR-0028)。
738
+ // ループの `continue` は「この境界の残りを飛ばす」を意味するので、
739
+ // 段に切り出すときは**呼び出し側の continue として残す** — 判定の綴りを
740
+ // ヘルパの中へ持ち込むと、同じ条件が二箇所に分かれて片方だけ直る
741
+ for (const b of model.boundaries) {
742
+ const sa = model.spaces.get(b.a);
743
+ const sb = model.spaces.get(b.b);
744
+ if (!sa || !sb) continue; // 未定義の参照は REF01 が言う — 形の検査には進まない
745
+ const bAt = { line: b.line, file: b.file, path: [b.a, b.b] };
746
+
747
+ if (VERTICAL.has(b.kind)) {
748
+ checkVerticalBoundary(ctx, b, sa, sb, bAt);
749
+ continue; // 垂直境界は水平の検査 (線分・開口・seg) を一切受けない
750
+ }
751
+ if (sa.rects.length > 0 && sb.rects.length > 0 && sa.level !== sb.level) {
752
+ emit(
753
+ "BND03",
754
+ `異なるレベルの空間に壁境界は書けません (垂直は type:stair/shaft/void): ${b.a} | ${b.b}`,
755
+ bAt,
756
+ );
757
+ continue; // レベルを跨ぐ壁は成立していない — 線分も開口も問わない
140
758
  }
141
- if ((sa.rects.length > 0 ? 1 : 0) + (sb.rects.length > 0 ? 1 : 0) === 1 && segs.length === 0) {
142
- warnings.push(`${b.line}行目: 外周に残る辺が無く、境界線分がゼロです: ${b.a} | ${b.b}`);
759
+ checkBoundarySegments(ctx, b, sa, sb, bAt);
760
+ checkOpenings(ctx, b);
761
+ checkBoundarySegs(ctx, b);
762
+ }
763
+ }
764
+
765
+ /** 垂直境界 (stair/shaft/void) — VRT01〜VRT06 */
766
+ function checkVerticalBoundary(ctx: Ctx, b: Boundary, sa: Space, sb: Space, bAt: At): void {
767
+ const { emit, levelIndex } = ctx;
768
+ // 垂直境界: 隣り合うレベルの、平面で重なる空間同士にしか張れない
769
+ if (sa.rects.length === 0 || sb.rects.length === 0 || !sa.level || !sb.level) {
770
+ emit("VRT01", `${b.kind} 境界は領域とレベルを持つ空間同士に書きます`, bAt);
771
+ return; // 前提が崩れているので、以降の判定は意味を持たない
772
+ }
773
+ const ia = levelIndex.get(sa.level);
774
+ const ib = levelIndex.get(sb.level);
775
+ if (ia === undefined || ib === undefined || Math.abs(ia - ib) !== 1) {
776
+ emit("VRT02", `${b.kind} 境界は隣り合うレベルの間に書きます: ${b.a} | ${b.b}`, bAt);
777
+ } else if (!spacesOverlap(sa, sb)) {
778
+ emit("VRT03", `${b.kind} 境界の空間が平面上で重なっていません: ${b.a} | ${b.b}`, bAt);
779
+ }
780
+ if (b.kind === "void") {
781
+ const upper = (ia ?? 0) > (ib ?? 0) ? sa : sb;
782
+ if (upper.type !== "void") {
783
+ emit("VRT04", `void境界の上側は type:void の空間を想定しています: ${upper.path}`, bAt);
143
784
  }
144
- if (b.kind === "open" && b.openings.length > 0) {
145
- warnings.push(`${b.line}行目: open境界の開口は通行に影響しません (常に通れます)`);
785
+ }
786
+ // 咎めているのは字下げされた door / window / seg の行そのものなので、
787
+ // 親の境界宣言ではなくその行を指す。宣言が複数あれば診断も複数出る
788
+ for (const o of b.openings) {
789
+ emit("VRT05", `垂直境界の${o.kind}は解釈されません`, { line: o.line, file: b.file, path: [b.a, b.b] });
790
+ }
791
+ for (const g of b.segs) {
792
+ emit("VRT06", `垂直境界の seg は解釈されません`, { line: g.line, file: b.file, path: [b.a, b.b] });
793
+ }
794
+ }
795
+
796
+ /** 水平境界の線分がゼロ — BND04 / BND06 */
797
+ function checkBoundarySegments(ctx: Ctx, b: Boundary, sa: Space, sb: Space, bAt: At): void {
798
+ const { model, emit } = ctx;
799
+ // 線を持つ境界の線分ゼロは「接していない」ではない — 線が分離していないか
800
+ // 何も切っていないかであり、それは LIN01 / LIN03 が言う
801
+ if (b.drawn) return;
802
+ const segs = segmentsFor(model, b);
803
+ if (sa.rects.length > 0 && sb.rects.length > 0 && segs.length === 0) {
804
+ // **線分がゼロの理由は二つある。**接していないか、edge: で絞った先に共有辺が
805
+ // 無いか。前者だと断言すると、実際には接している二室について「割付を直せ」と
806
+ // 言うことになる — 直すべきは方角一語である (N=+Y, S=-Y, E=+X, W=-X)
807
+ const without = b.edge ? segmentsFor(model, { ...b, edge: undefined }) : [];
808
+ if (without.length > 0) {
809
+ const dirs = [...new Set(without.map((g) => g.edgeOfA))].filter((d): d is Edge => d !== undefined);
810
+ emit(
811
+ "BND04",
812
+ `edge:${b.edge} に共有辺がありません: ${b.a} | ${b.b} (実際に接しているのは ${
813
+ dirs.join("・") || "別の辺"
814
+ } です)`,
815
+ bAt,
816
+ );
817
+ } else {
818
+ emit("BND04", `空間が接していないため境界を導けません: ${b.a} | ${b.b}`, bAt);
146
819
  }
820
+ }
821
+ if ((sa.rects.length > 0 ? 1 : 0) + (sb.rects.length > 0 ? 1 : 0) === 1 && segs.length === 0) {
822
+ emit(
823
+ "BND06",
824
+ `${b.edge ? `edge:${b.edge} の` : ""}外周に残る辺が無く、境界線分がゼロです: ${b.a} | ${b.b}`,
825
+ bAt,
826
+ );
827
+ }
828
+ }
829
+
830
+ /**
831
+ * 開口 — OPN03 / 配置エラー (OPN04〜08) / OPN01 / OPN02。
832
+ * **一つの関数に保つ** — 配置に失敗した開口は OPN01 も受けず OPN02 の母集団からも外れる。
833
+ * 配置の結果 (placedOnSeg) を跨いで持つのはここだけである
834
+ */
835
+ function checkOpenings(ctx: Ctx, b: Boundary): void {
836
+ const { model, emit, loc } = ctx;
837
+ if (b.kind === "open" && b.openings.length > 0) {
147
838
  for (const o of b.openings) {
148
- const placed = placeOpening(model, b, o);
149
- if ("error" in placed && placed.error) {
150
- errors.push(placed.error);
151
- continue;
152
- }
153
- if (o.hinge && "segment" in placed) {
154
- const okAxis = placed.segment.horizontal
155
- ? o.hinge === "W" || o.hinge === "E"
156
- : o.hinge === "N" || o.hinge === "S";
157
- if (!okAxis) {
158
- errors.push(
159
- `${o.line}行目: hinge:${o.hinge} は${
160
- placed.segment.horizontal ? "水平線分 (W/E)" : "垂直線分 (N/S)"
161
- }で指定します`,
162
- );
163
- }
164
- }
839
+ emit("OPN03", `open境界の${o.kind}は通行に影響しません (常に通れます)`, {
840
+ line: o.line,
841
+ file: b.file,
842
+ path: [b.a, b.b],
843
+ });
165
844
  }
166
- for (const g of b.segs) {
167
- if (b.kind === "open") {
168
- warnings.push(`${g.line}行目: open境界 (壁が無い) の seg は解釈されません`);
169
- continue;
845
+ }
846
+ const placedOnSeg: Array<{ o: (typeof b.openings)[number]; key: string; c: number }> = [];
847
+ for (const o of b.openings) {
848
+ const placed = placeOpening(model, b, o);
849
+ if ("error" in placed && placed.error) {
850
+ emit(placed.code, placed.message, { line: placed.line, file: placed.file, path: [b.a, b.b] });
851
+ continue; // 置けなかった開口は、以降 (hinge の軸・重なり) の対象にならない
852
+ }
853
+ if ("segment" in placed) {
854
+ const s = placed.segment;
855
+ placedOnSeg.push({
856
+ o,
857
+ key: `${s.x1},${s.y1},${s.x2},${s.y2}`,
858
+ c: s.horizontal ? placed.cx : placed.cy,
859
+ });
860
+ }
861
+ if (o.hinge && "segment" in placed) {
862
+ const okAxis = placed.segment.horizontal
863
+ ? o.hinge === "W" || o.hinge === "E"
864
+ : o.hinge === "N" || o.hinge === "S";
865
+ if (!okAxis) {
866
+ emit(
867
+ "OPN01",
868
+ `hinge:${o.hinge} は${placed.segment.horizontal ? "水平線分 (W/E)" : "垂直線分 (N/S)"}で指定します`,
869
+ { line: o.line, file: b.file, path: [b.a, b.b] },
870
+ );
170
871
  }
171
- const placed = placeBand(model, b, g, "seg");
172
- if ("error" in placed && placed.error) {
173
- errors.push(placed.error);
872
+ }
873
+ }
874
+ // 同じ線分の上の開口同士は重なってはならない
875
+ const byKey = new Map<string, typeof placedOnSeg>();
876
+ for (const p of placedOnSeg) {
877
+ const arr = byKey.get(p.key) ?? [];
878
+ arr.push(p);
879
+ byKey.set(p.key, arr);
880
+ }
881
+ for (const group of byKey.values()) {
882
+ group.sort((p, q) => p.c - q.c);
883
+ for (let k = 0; k + 1 < group.length; k++) {
884
+ const p = group[k]!;
885
+ const q = group[k + 1]!;
886
+ const need = (p.o.w + q.o.w) / 2;
887
+ if (q.c - p.c < need - EPS) {
888
+ emit(
889
+ "OPN02",
890
+ `開口同士が重なっています (${p.o.kind}と${q.o.kind} — 中心間 ${Math.round(
891
+ q.c - p.c,
892
+ )}mm < 必要 ${Math.round(need)}mm)`,
893
+ { line: q.o.line, file: b.file, path: [b.a, b.b], related: [loc(p.o.line, b.file)] },
894
+ );
174
895
  }
175
896
  }
176
897
  }
898
+ }
177
899
 
900
+ /** 数えない分節 (seg) の配置 — SEG03 / SEG04〜08 */
901
+ function checkBoundarySegs(ctx: Ctx, b: Boundary): void {
902
+ const { model, emit } = ctx;
903
+ for (const g of b.segs) {
904
+ if (b.kind === "open") {
905
+ emit("SEG03", `open境界 (壁が無い) の seg は解釈されません`, { line: g.line, file: b.file, path: [b.a, b.b] });
906
+ continue;
907
+ }
908
+ const placed = placeBand(model, b, g, "seg");
909
+ if ("error" in placed && placed.error) {
910
+ emit(placed.code, placed.message, { line: placed.line, file: placed.file, path: [b.a, b.b] });
911
+ }
912
+ }
913
+ }
914
+
915
+ /** 数えない分節 — SEG01 / SEG02 */
916
+ function checkAreas(ctx: Ctx): void {
917
+ const { model, emit, loc, withRect, levels, levelIndex } = ctx;
178
918
  // 数えない分節 (area) は親の領域に収まっているか
179
919
  for (const s of model.spaces.values()) {
180
920
  for (const a of s.areas) {
181
921
  if (s.rects.length === 0) {
182
- errors.push(`${a.line}行目: 領域を持たない空間 ${s.path} に area は書けません`);
922
+ emit("SEG01", `領域を持たない空間 ${s.path} に area は書けません`, {
923
+ line: a.line,
924
+ file: s.file,
925
+ path: [s.path],
926
+ });
183
927
  continue;
184
928
  }
185
- const inside = s.rects.some(
186
- (r) =>
187
- a.rect.x1 >= r.x1 - EPS &&
188
- a.rect.x2 <= r.x2 + EPS &&
189
- a.rect.y1 >= r.y1 - EPS &&
190
- a.rect.y2 <= r.y2 + EPS,
929
+ // 内包も導出された形で見る — 割付で見ると、切り落とした側に置いた床材が通ってしまう
930
+ const inside = regionOf(s).some((g) =>
931
+ rectToPoly(a.rect).every((pt) => pointInPolygon(pt, g, EPS)),
191
932
  );
192
933
  if (!inside) {
193
- warnings.push(`${a.line}行目: area が ${s.path} の領域からはみ出しています`);
934
+ emit("SEG02", `area が ${s.path} の領域からはみ出しています`, { line: a.line, file: s.file, path: [s.path] });
194
935
  }
195
936
  }
196
937
  }
938
+ }
197
939
 
940
+ /** ゾーン — ZON01 / ZON02 */
941
+ function checkZones(ctx: Ctx): void {
942
+ const { model, emit, loc, withRect, levels, levelIndex } = ctx;
198
943
  // ゾーン: 束ねる空間が実在するか
199
944
  for (const z of model.zones.values()) {
200
945
  const children = [...model.spaces.keys()].filter((p) => p.startsWith(z.path + "/"));
201
946
  if (children.length === 0) {
202
- warnings.push(`${z.line}行目: ゾーン ${z.path} の下に空間がありません`);
947
+ emit("ZON01", `ゾーン ${z.path} の下に空間がありません`, { line: z.line, file: z.file, path: [z.path] });
203
948
  }
204
949
  if (model.spaces.has(z.path)) {
205
- warnings.push(`${z.line}行目: ゾーンと同じパスの空間があります (どちらかに寄せます): ${z.path}`);
950
+ emit("ZON02", `ゾーンと同じパスの空間があります (どちらかに寄せます): ${z.path}`, {
951
+ line: z.line,
952
+ file: z.file,
953
+ path: [z.path],
954
+ });
206
955
  }
207
956
  }
957
+ }
208
958
 
959
+ /** 高さ方向の一貫性 — HGT01〜HGT04 */
960
+ function checkHeights(ctx: Ctx): void {
961
+ const { model, emit, loc, withRect, levels, levelIndex } = ctx;
209
962
  // 高さ方向の一貫性: 下階の空間の天井高 + 上階のslab ≤ 階高
210
963
  // 吹抜け (void境界) は宣言的な免除だが、免除が効くのは吹抜けが平面を覆う範囲まで —
211
964
  // 部分吹抜けでは下階の天井高は階高内に収める (吹抜け部分の高さは導出) (ADR-0006追記)
@@ -241,6 +994,10 @@ export function check(model: Model): CheckResult {
241
994
  let slabMissing = false;
242
995
  for (const s of below) {
243
996
  if (isSemiOutdoor(model, s)) continue; // 屋外・半屋外 (庭・バルコニー等) に天井は無い
997
+ // 縦動線の宣言的免除 (ADR-0021)。void の免除が「床の不在」だったのに対し、
998
+ // こちらは「天井が面でない」— 階段室・斜路の天井は上の走りに沿って傾いている。
999
+ // h を一つの数で語れない以上、この不変量は成立しない。頭上高さの検査は別の問い
1000
+ if (runDecls(s).length > 0) continue;
244
1001
  const covered = above.some((u) => spacesOverlap(s, u)) || above.length === 0;
245
1002
  if (!covered) continue;
246
1003
  if (lu.slab === undefined) {
@@ -249,54 +1006,123 @@ export function check(model: Model): CheckResult {
249
1006
  }
250
1007
  const h = heff(model, s);
251
1008
  if (h === undefined) {
252
- warnings.push(`${s.path} の天井高が不明で、${lu.name} との高さ検査ができません`);
1009
+ emit("HGT04", `${s.path} の天井高が不明で、${lu.name} との高さ検査ができません`, {
1010
+ line: s.line,
1011
+ file: s.file,
1012
+ path: [s.path],
1013
+ });
253
1014
  continue;
254
1015
  }
255
1016
  if (h + lu.slab > pitch + EPS) {
256
1017
  const partners = voidPartners.get(`${s.path}|${lu.name}`) ?? [];
257
1018
  const cover = partners.length ? voidCoverage(s, partners) : 0;
258
1019
  if (cover >= 0.99) continue; // 全面吹抜け — 宣言的免除
259
- errors.push(
260
- partners.length
261
- ? `${s.path} の天井高${h}は階高${pitch}を超えますが、吹抜けの被覆は${Math.round(
262
- cover * 100,
263
- )}%です。部分吹抜けでは天井高を階高内に収めます (吹抜け部分の高さは導出)`
264
- : `${s.path} が上階に食い込みます: 天井高${h} + ${lu.name}のslab${lu.slab} = ${
265
- h + lu.slab
266
- } > 階高${pitch}`,
267
- );
1020
+ if (partners.length) {
1021
+ // 本文は HGT01 と同じ三項を出す — 決め手が上階の slab のとき
1022
+ // 「天井高3000は階高3000を超えます」という成り立たない不等式になっていた。
1023
+ // 被覆は免除しきい値 (99%) と衝突しないよう小数一桁で言う
1024
+ emit(
1025
+ "HGT02",
1026
+ `${s.path} が上階に食い込みます: 天井高${h} + ${lu.name}のslab${lu.slab} = ${
1027
+ h + lu.slab
1028
+ } > 階高${pitch}。吹抜けの被覆は${(cover * 100).toFixed(1)}%しかありません — 部分吹抜けでは天井高を階高内に収めます (吹抜け部分の高さは導出)`,
1029
+ { line: s.line, file: s.file, path: [s.path] },
1030
+ );
1031
+ } else {
1032
+ emit(
1033
+ "HGT01",
1034
+ `${s.path} が上階に食い込みます: 天井高${h} + ${lu.name}のslab${lu.slab} = ${h + lu.slab} > 階高${pitch}`,
1035
+ { line: s.line, file: s.file, path: [s.path] },
1036
+ );
1037
+ }
268
1038
  }
269
1039
  }
270
1040
  if (slabMissing) {
271
- warnings.push(
272
- `レベル ${lu.name} に slab が未宣言のため、${lb.name} との高さ検査ができません`,
273
- );
1041
+ emit("HGT03", `レベル ${lu.name} に slab が未宣言のため、${lb.name} との高さ検査ができません`, {
1042
+ line: lu.line,
1043
+ file: lu.file,
1044
+ });
274
1045
  }
275
1046
  }
1047
+ }
276
1048
 
1049
+ /** レベルに載らない空間 — HGT05 */
1050
+ function checkOrphanSpaces(ctx: Ctx): void {
1051
+ const { model, emit, loc, withRect, levels, levelIndex } = ctx;
277
1052
  // レベルに載らない領域つき空間
278
1053
  for (const s of withRect) {
279
1054
  if (!s.level) {
280
- warnings.push(
281
- `${s.line}行目: ${s.path} は領域を持ちますが、レベルが特定できません (パス先頭か level: で指定します)`,
282
- );
1055
+ emit("HGT05", `${s.path} は領域を持ちますが、レベルが特定できません (パス先頭か level: で指定します)`, {
1056
+ line: s.line,
1057
+ file: s.file,
1058
+ path: [s.path],
1059
+ });
283
1060
  }
284
1061
  }
285
-
286
- return { errors, warnings };
287
1062
  }
288
1063
 
289
- /** 吹抜けが下階の空間の平面をどれだけ覆うか (0..1) */
290
- function voidCoverage(s: Space, partners: Space[]): number {
291
- let inter = 0;
292
- for (const r of s.rects) {
293
- for (const p of partners) {
294
- for (const pr of p.rects) {
295
- const o = planOverlap(r, pr);
296
- if (o) inter += (o.x2 - o.x1) * (o.y2 - o.y1);
1064
+ /** 敷地形状 — SIT01〜SIT05 */
1065
+ function checkSite(ctx: Ctx): void {
1066
+ const { model, emit, loc, withRect, levels, levelIndex } = ctx;
1067
+ // 敷地形状 (ADR-0011): 形の妥当性、対応ゾーンの存在、宣言面積との照合、建物のはみ出し検査。
1068
+ // はみ出しは四隅の内包に加え、多角形の頂点の入り込みと辺の交差を見る — 凹敷地でも正しい (ADR-0013)。
1069
+ // 地上の外部空間タイル (庭・通路) は近似なので検査しない — 面積の真は多角形が持つ
1070
+ for (const poly of model.polygons.values()) {
1071
+ for (let i = 0; i < poly.points.length; i++) {
1072
+ const a = poly.points[i]!;
1073
+ const b = poly.points[(i + 1) % poly.points.length]!;
1074
+ if (Math.hypot(b.x - a.x, b.y - a.y) <= EPS_SITE) {
1075
+ emit("SIT01", `敷地形状に重複する頂点があります (${Math.round(a.x)},${Math.round(a.y)})`, {
1076
+ line: poly.line,
1077
+ file: poly.file,
1078
+ path: [poly.path],
1079
+ });
1080
+ }
1081
+ }
1082
+ const selfX = polygonSelfIntersection(poly.points);
1083
+ if (selfX) {
1084
+ emit("SIT02", `敷地形状が自己交差しています (${Math.round(selfX.x)},${Math.round(selfX.y)} 付近)`, {
1085
+ line: poly.line,
1086
+ file: poly.file,
1087
+ path: [poly.path],
1088
+ });
1089
+ continue; // 不正な形に対する包含・面積は判定しない
1090
+ }
1091
+ const zone = model.zones.get(poly.path);
1092
+ if (!zone) {
1093
+ emit("SIT04", `polygon ${poly.path} に対応するゾーンがありません`, {
1094
+ line: poly.line,
1095
+ file: poly.file,
1096
+ path: [poly.path],
1097
+ });
1098
+ continue;
1099
+ }
1100
+ if (zone.attrs["site"] !== 1) continue;
1101
+ const declared = zone.attrs["area"];
1102
+ if (typeof declared === "number") {
1103
+ const derived = polygonAreaM2(poly.points);
1104
+ if (Math.abs(declared - derived) >= 0.05) {
1105
+ emit(
1106
+ "SIT05",
1107
+ `敷地面積の宣言と導出が食い違います: 宣言 ${declared}㎡ / 導出 ${derived.toFixed(2)}㎡`,
1108
+ { line: zone.line, file: zone.file, path: [zone.path] },
1109
+ );
1110
+ }
1111
+ }
1112
+ for (const s of withRect) {
1113
+ if (s.type === "exterior" || s.path.startsWith(poly.path + "/")) continue;
1114
+ // 照合するのは割付ではなく**導出された領域** — 敷地なりに切った外形はここで通る
1115
+ for (const r of regionOf(s)) {
1116
+ const out = shapeEscapesPolygon(r, poly.points, EPS_SITE);
1117
+ if (out) {
1118
+ emit("SIT03", `${s.path} が敷地形状からはみ出しています (${Math.round(out.x)},${Math.round(out.y)} 付近)`, {
1119
+ line: s.line,
1120
+ file: s.file,
1121
+ path: [s.path],
1122
+ });
1123
+ break;
1124
+ }
297
1125
  }
298
1126
  }
299
1127
  }
300
- const area = s.rects.reduce((sum, r) => sum + (r.x2 - r.x1) * (r.y2 - r.y1), 0);
301
- return area > 0 ? inter / area : 0;
302
1128
  }