@kensnzk/koyu 0.16.0 → 0.18.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 (254) hide show
  1. package/README.ja.md +12 -8
  2. package/README.md +13 -10
  3. package/dist/analysis/contracts.d.ts +217 -0
  4. package/dist/analysis/contracts.d.ts.map +1 -0
  5. package/dist/analysis/contracts.js +27 -0
  6. package/dist/analysis/contracts.js.map +1 -0
  7. package/dist/analysis/index.d.ts +5 -0
  8. package/dist/analysis/index.d.ts.map +1 -0
  9. package/dist/analysis/index.js +17 -0
  10. package/dist/analysis/index.js.map +1 -0
  11. package/dist/analysis/json.d.ts +21 -0
  12. package/dist/analysis/json.d.ts.map +1 -0
  13. package/dist/analysis/json.js +111 -0
  14. package/dist/analysis/json.js.map +1 -0
  15. package/dist/cli.js +106 -35
  16. package/dist/cli.js.map +1 -1
  17. package/dist/core/derive.d.ts +13 -8
  18. package/dist/core/derive.d.ts.map +1 -1
  19. package/dist/core/derive.js +19 -13
  20. package/dist/core/derive.js.map +1 -1
  21. package/dist/core/diagnose.d.ts +4 -3
  22. package/dist/core/diagnose.d.ts.map +1 -1
  23. package/dist/core/diagnose.js +46 -11
  24. package/dist/core/diagnose.js.map +1 -1
  25. package/dist/core/diff.d.ts +3 -2
  26. package/dist/core/diff.d.ts.map +1 -1
  27. package/dist/core/diff.js +8 -5
  28. package/dist/core/diff.js.map +1 -1
  29. package/dist/core/fabric.d.ts +1 -3
  30. package/dist/core/fabric.d.ts.map +1 -1
  31. package/dist/core/fabric.js +8 -8
  32. package/dist/core/fabric.js.map +1 -1
  33. package/dist/core/graph.d.ts +2 -2
  34. package/dist/core/graph.d.ts.map +1 -1
  35. package/dist/core/graph.js +12 -5
  36. package/dist/core/graph.js.map +1 -1
  37. package/dist/core/light.d.ts.map +1 -1
  38. package/dist/core/light.js +2 -2
  39. package/dist/core/light.js.map +1 -1
  40. package/dist/core/model.d.ts +59 -10
  41. package/dist/core/model.d.ts.map +1 -1
  42. package/dist/core/model.js +128 -26
  43. package/dist/core/model.js.map +1 -1
  44. package/dist/core/parse.d.ts.map +1 -1
  45. package/dist/core/parse.js +83 -82
  46. package/dist/core/parse.js.map +1 -1
  47. package/dist/core/poly.d.ts +0 -4
  48. package/dist/core/poly.d.ts.map +1 -1
  49. package/dist/core/poly.js +1 -5
  50. package/dist/core/poly.js.map +1 -1
  51. package/dist/core/site.d.ts +9 -0
  52. package/dist/core/site.d.ts.map +1 -1
  53. package/dist/core/site.js +8 -2
  54. package/dist/core/site.js.map +1 -1
  55. package/dist/core/tolerance.d.ts +1 -1
  56. package/dist/core/tolerance.js +2 -2
  57. package/dist/core/tolerance.js.map +1 -1
  58. package/dist/core/vertical.d.ts +9 -2
  59. package/dist/core/vertical.d.ts.map +1 -1
  60. package/dist/core/vertical.js +8 -1
  61. package/dist/core/vertical.js.map +1 -1
  62. package/dist/core/vocabulary.d.ts.map +1 -1
  63. package/dist/core/vocabulary.js +17 -5
  64. package/dist/core/vocabulary.js.map +1 -1
  65. package/dist/diagnostics.d.ts +2 -0
  66. package/dist/diagnostics.d.ts.map +1 -0
  67. package/dist/diagnostics.js +3 -0
  68. package/dist/diagnostics.js.map +1 -0
  69. package/dist/diff.d.ts +2 -0
  70. package/dist/diff.d.ts.map +1 -0
  71. package/dist/diff.js +3 -0
  72. package/dist/diff.js.map +1 -0
  73. package/dist/draw/index.js +1 -1
  74. package/dist/draw/index.js.map +1 -1
  75. package/dist/draw/plan.js +5 -5
  76. package/dist/draw/plan.js.map +1 -1
  77. package/dist/form.d.ts +5 -0
  78. package/dist/form.d.ts.map +1 -0
  79. package/dist/form.js +4 -0
  80. package/dist/form.js.map +1 -0
  81. package/dist/graph.d.ts +2 -0
  82. package/dist/graph.d.ts.map +1 -0
  83. package/dist/graph.js +3 -0
  84. package/dist/graph.js.map +1 -0
  85. package/dist/index.d.ts +2 -14
  86. package/dist/index.d.ts.map +1 -1
  87. package/dist/index.js +36 -45
  88. package/dist/index.js.map +1 -1
  89. package/dist/mcp.js +81 -30
  90. package/dist/mcp.js.map +1 -1
  91. package/dist/model.d.ts +3 -0
  92. package/dist/model.d.ts.map +1 -0
  93. package/dist/model.js +3 -0
  94. package/dist/model.js.map +1 -0
  95. package/dist/parse-file.d.ts.map +1 -1
  96. package/dist/parse-file.js +18 -3
  97. package/dist/parse-file.js.map +1 -1
  98. package/dist/validate/assessment.d.ts +8 -0
  99. package/dist/validate/assessment.d.ts.map +1 -0
  100. package/dist/validate/assessment.js +1738 -0
  101. package/dist/validate/assessment.js.map +1 -0
  102. package/dist/validate/builtin/access.d.ts +55 -0
  103. package/dist/validate/builtin/access.d.ts.map +1 -0
  104. package/dist/validate/builtin/access.js +494 -0
  105. package/dist/validate/builtin/access.js.map +1 -0
  106. package/dist/validate/builtin/daylight.d.ts +44 -0
  107. package/dist/validate/builtin/daylight.d.ts.map +1 -0
  108. package/dist/validate/builtin/daylight.js +233 -0
  109. package/dist/validate/builtin/daylight.js.map +1 -0
  110. package/dist/validate/builtin/door-column-collisions.d.ts +47 -0
  111. package/dist/validate/builtin/door-column-collisions.d.ts.map +1 -0
  112. package/dist/validate/builtin/door-column-collisions.js +189 -0
  113. package/dist/validate/builtin/door-column-collisions.js.map +1 -0
  114. package/dist/validate/builtin/envelope.d.ts +30 -0
  115. package/dist/validate/builtin/envelope.d.ts.map +1 -0
  116. package/dist/validate/builtin/envelope.js +169 -0
  117. package/dist/validate/builtin/envelope.js.map +1 -0
  118. package/dist/validate/builtin/freeze.d.ts +3 -0
  119. package/dist/validate/builtin/freeze.d.ts.map +1 -0
  120. package/dist/validate/builtin/freeze.js +21 -0
  121. package/dist/validate/builtin/freeze.js.map +1 -0
  122. package/dist/validate/builtin/index.d.ts +38 -0
  123. package/dist/validate/builtin/index.d.ts.map +1 -0
  124. package/dist/validate/builtin/index.js +106 -0
  125. package/dist/validate/builtin/index.js.map +1 -0
  126. package/dist/validate/builtin/site.d.ts +62 -0
  127. package/dist/validate/builtin/site.d.ts.map +1 -0
  128. package/dist/validate/builtin/site.js +333 -0
  129. package/dist/validate/builtin/site.js.map +1 -0
  130. package/dist/validate/builtin/vertical-runs.d.ts +53 -0
  131. package/dist/validate/builtin/vertical-runs.d.ts.map +1 -0
  132. package/dist/validate/builtin/vertical-runs.js +290 -0
  133. package/dist/validate/builtin/vertical-runs.js.map +1 -0
  134. package/dist/validate/contracts.d.ts +188 -0
  135. package/dist/validate/contracts.d.ts.map +1 -0
  136. package/dist/validate/contracts.js +28 -0
  137. package/dist/validate/contracts.js.map +1 -0
  138. package/dist/validate/index.d.ts +2 -59
  139. package/dist/validate/index.d.ts.map +1 -1
  140. package/dist/validate/index.js +22 -70
  141. package/dist/validate/index.js.map +1 -1
  142. package/dist/vocabulary.d.ts +2 -0
  143. package/dist/vocabulary.d.ts.map +1 -0
  144. package/dist/vocabulary.js +3 -0
  145. package/dist/vocabulary.js.map +1 -0
  146. package/editors/vscode/syntaxes/koyu.tmLanguage.json +2 -2
  147. package/examples/basement/main.muro +3 -3
  148. package/examples/complex/main.muro +1 -1
  149. package/examples/complex/podium.muro +1 -1
  150. package/examples/complex/site.muro +6 -6
  151. package/examples/house/L2.muro +1 -1
  152. package/examples/house/main.muro +1 -1
  153. package/examples/house/site.muro +4 -4
  154. package/examples/house.muro +6 -6
  155. package/examples/mansion.muro +2 -2
  156. package/examples/office.muro +3 -3
  157. package/examples/steps/04-exterior.muro +1 -1
  158. package/examples/steps/05-two-storeys.muro +1 -1
  159. package/examples/steps/06-finished.muro +2 -2
  160. package/examples/tower/L2.muro +1 -1
  161. package/examples/tower/main.muro +1 -1
  162. package/examples/tower/site.muro +4 -4
  163. package/examples/twin/main.muro +1 -1
  164. package/examples/twin/podium.muro +4 -4
  165. package/examples/twin/site.muro +8 -8
  166. package/examples/two-rooms.canonical.json +4 -4
  167. package/examples/two-rooms.muro +2 -2
  168. package/package.json +41 -9
  169. package/src/analysis/contracts.ts +234 -0
  170. package/src/analysis/index.ts +61 -0
  171. package/src/analysis/json.ts +117 -0
  172. package/src/cli.ts +116 -36
  173. package/src/core/derive.ts +32 -15
  174. package/src/core/diagnose.ts +63 -16
  175. package/src/core/diff.ts +10 -6
  176. package/src/core/fabric.ts +18 -9
  177. package/src/core/graph.ts +19 -5
  178. package/src/core/light.ts +2 -2
  179. package/src/core/model.ts +141 -31
  180. package/src/core/parse.ts +100 -85
  181. package/src/core/poly.ts +1 -8
  182. package/src/core/site.ts +19 -2
  183. package/src/core/tolerance.ts +2 -2
  184. package/src/core/vertical.ts +10 -3
  185. package/src/core/vocabulary.ts +18 -5
  186. package/src/diagnostics.ts +8 -0
  187. package/src/diff.ts +14 -0
  188. package/src/draw/index.ts +1 -1
  189. package/src/draw/plan.ts +5 -5
  190. package/src/form.ts +37 -0
  191. package/src/graph.ts +12 -0
  192. package/src/index.ts +40 -175
  193. package/src/mcp.ts +85 -35
  194. package/src/model.ts +43 -0
  195. package/src/parse-file.ts +17 -3
  196. package/src/validate/assessment.ts +1934 -0
  197. package/src/validate/builtin/access.ts +642 -0
  198. package/src/validate/builtin/daylight.ts +321 -0
  199. package/src/validate/builtin/door-column-collisions.ts +266 -0
  200. package/src/validate/builtin/envelope.ts +227 -0
  201. package/src/validate/builtin/freeze.ts +19 -0
  202. package/src/validate/builtin/index.ts +156 -0
  203. package/src/validate/builtin/site.ts +445 -0
  204. package/src/validate/builtin/vertical-runs.ts +390 -0
  205. package/src/validate/contracts.ts +240 -0
  206. package/src/validate/index.ts +45 -99
  207. package/src/vocabulary.ts +9 -0
  208. package/dist/validate/access.d.ts +0 -6
  209. package/dist/validate/access.d.ts.map +0 -1
  210. package/dist/validate/access.js +0 -238
  211. package/dist/validate/access.js.map +0 -1
  212. package/dist/validate/envelope.d.ts +0 -4
  213. package/dist/validate/envelope.d.ts.map +0 -1
  214. package/dist/validate/envelope.js +0 -55
  215. package/dist/validate/envelope.js.map +0 -1
  216. package/dist/validate/light.d.ts +0 -6
  217. package/dist/validate/light.d.ts.map +0 -1
  218. package/dist/validate/light.js +0 -28
  219. package/dist/validate/light.js.map +0 -1
  220. package/dist/validate/runs.d.ts +0 -16
  221. package/dist/validate/runs.d.ts.map +0 -1
  222. package/dist/validate/runs.js +0 -60
  223. package/dist/validate/runs.js.map +0 -1
  224. package/dist/validate/site.d.ts +0 -6
  225. package/dist/validate/site.d.ts.map +0 -1
  226. package/dist/validate/site.js +0 -63
  227. package/dist/validate/site.js.map +0 -1
  228. package/spec/README.md +0 -28
  229. package/spec/canonical-json.md +0 -98
  230. package/spec/composition.md +0 -121
  231. package/spec/derivation.md +0 -393
  232. package/spec/en/README.md +0 -30
  233. package/spec/en/canonical-json.md +0 -101
  234. package/spec/en/composition.md +0 -121
  235. package/spec/en/derivation.md +0 -393
  236. package/spec/en/language.md +0 -164
  237. package/spec/en/notation-v0.md +0 -145
  238. package/spec/en/scope.md +0 -229
  239. package/spec/en/semantics.md +0 -132
  240. package/spec/en/tools.md +0 -108
  241. package/spec/en/validation.md +0 -79
  242. package/spec/en/vocabulary.md +0 -103
  243. package/spec/language.md +0 -163
  244. package/spec/notation-v0.md +0 -184
  245. package/spec/scope.md +0 -229
  246. package/spec/semantics.md +0 -130
  247. package/spec/tools.md +0 -106
  248. package/spec/validation.md +0 -79
  249. package/spec/vocabulary.md +0 -114
  250. package/src/validate/access.ts +0 -279
  251. package/src/validate/envelope.ts +0 -59
  252. package/src/validate/light.ts +0 -43
  253. package/src/validate/runs.ts +0 -87
  254. package/src/validate/site.ts +0 -84
@@ -0,0 +1,321 @@
1
+ import type {
2
+ AnalysisArtifact,
3
+ AnalysisDefinition,
4
+ AnalysisRef,
5
+ ComponentIdentity,
6
+ Evidence,
7
+ Quantity,
8
+ SourceRef,
9
+ SubjectRef,
10
+ } from "../../analysis/contracts.js";
11
+ import { codePointCompare } from "../../analysis/json.js";
12
+ import { COVERED_SEMI_FACTOR, daylightInputs } from "../../core/light.js";
13
+ import {
14
+ canonicalBoundaryOrder,
15
+ canonicalOpeningOrder,
16
+ isCoveredAbove,
17
+ isOutside,
18
+ isSemiOutdoor,
19
+ type Model,
20
+ type Space,
21
+ } from "../../core/model.js";
22
+ import type { Rule, RuleEvaluation, RuleOutcome } from "../contracts.js";
23
+ import { freezeBuiltin } from "./freeze.js";
24
+
25
+ /** The denominator of the required window area. **An architectural number, not a core invariant.** */
26
+ export const DAYLIGHT_DIVISOR = 7;
27
+
28
+ /** The tolerance the legacy comparison used, kept so the migration does not move the boundary. */
29
+ export const DAYLIGHT_AREA_EPSILON = 1e-9;
30
+
31
+ /**
32
+ * A window that counts toward daylight but carries no `h:`, so its area is not in the sum.
33
+ *
34
+ * The shortfall is a **fact of the model**, not a missing external input: the artifact stays
35
+ * `complete` and the count is carried here, so the ratio keeps the population it always had
36
+ * and the gap is still visible.
37
+ */
38
+ export type MissingHeightOpeningFact = {
39
+ readonly ref: string;
40
+ readonly boundaryRef: string;
41
+ readonly widthMm: number;
42
+ readonly location: { readonly file?: string; readonly line: number };
43
+ };
44
+
45
+ export type DaylightSpaceFact = {
46
+ readonly ref: string;
47
+ readonly floorAreaM2: number;
48
+ readonly effectiveWindowAreaM2: number;
49
+ readonly missingHeightOpenings: readonly MissingHeightOpeningFact[];
50
+ };
51
+
52
+ export type DaylightAnalysisValue = {
53
+ readonly spaces: readonly DaylightSpaceFact[];
54
+ };
55
+
56
+ export const DAYLIGHT_ANALYSIS_ID: AnalysisRef<DaylightAnalysisValue> = freezeBuiltin({
57
+ id: "koyu.analysis.daylight",
58
+ revision: "1",
59
+ });
60
+
61
+ export const DAYLIGHT_RATIO_RULE_ID = freezeBuiltin({
62
+ id: "koyu.schematic.daylight.ratio",
63
+ revision: "1",
64
+ } as const);
65
+
66
+ export const DAYLIGHT_UNKNOWN_RULE_ID = freezeBuiltin({
67
+ id: "koyu.schematic.daylight.unknown",
68
+ revision: "1",
69
+ } as const);
70
+
71
+ export const DAYLIGHT_ANALYSIS: AnalysisDefinition<DaylightAnalysisValue> = freezeBuiltin<AnalysisDefinition<DaylightAnalysisValue>>({
72
+ ...DAYLIGHT_ANALYSIS_ID,
73
+ title: "Daylight observations",
74
+ model: "consistent",
75
+ dependencies: [],
76
+ context: [],
77
+ run: ({ model }): AnalysisArtifact<DaylightAnalysisValue> => {
78
+ const coreModel = model as unknown as Model;
79
+ const uncounted = uncountedWindows(coreModel);
80
+ const spaces = daylightInputs(coreModel)
81
+ .map((input): DaylightSpaceFact => ({
82
+ ref: input.space.path,
83
+ floorAreaM2: input.floor,
84
+ effectiveWindowAreaM2: input.window,
85
+ missingHeightOpenings: uncounted.get(input.space.path) ?? [],
86
+ }))
87
+ .sort((a, b) => codePointCompare(a.ref, b.ref));
88
+
89
+ return {
90
+ state: "complete",
91
+ value: { spaces },
92
+ evidence: spaces.map((space) => daylightObservation(space, coreModel.spaces.get(space.ref))),
93
+ };
94
+ },
95
+ });
96
+
97
+ export const DAYLIGHT_RATIO_RULE: Rule = freezeBuiltin<Rule>({
98
+ ...DAYLIGHT_RATIO_RULE_ID,
99
+ title: "Daylight area ratio",
100
+ level: "violation",
101
+ model: "consistent",
102
+ analyses: [{ analysis: DAYLIGHT_ANALYSIS_ID, accept: "complete" }],
103
+ context: [],
104
+ authority: [],
105
+ evaluate: ({ get }) => evaluateDaylightRatio(get(DAYLIGHT_ANALYSIS_ID)),
106
+ });
107
+
108
+ export const DAYLIGHT_UNKNOWN_RULE: Rule = freezeBuiltin<Rule>({
109
+ ...DAYLIGHT_UNKNOWN_RULE_ID,
110
+ title: "Daylight window area completeness",
111
+ level: "caution",
112
+ model: "consistent",
113
+ analyses: [{ analysis: DAYLIGHT_ANALYSIS_ID, accept: "complete" }],
114
+ context: [],
115
+ authority: [],
116
+ evaluate: ({ get }) => evaluateDaylightCompleteness(get(DAYLIGHT_ANALYSIS_ID)),
117
+ });
118
+
119
+ function evaluateDaylightRatio(
120
+ artifact: AnalysisArtifact<DaylightAnalysisValue>,
121
+ ): RuleEvaluation {
122
+ if (artifact.state !== "complete") return analysisIndeterminate(artifact);
123
+ if (artifact.value.spaces.length === 0) return notApplicable(NO_POPULATION);
124
+ const evidenceById = new Map(artifact.evidence.map((item) => [item.id, item]));
125
+
126
+ return applicable(artifact.value.spaces.map((space): RuleOutcome => {
127
+ const required = space.floorAreaM2 / DAYLIGHT_DIVISOR;
128
+ const failed = space.effectiveWindowAreaM2 + DAYLIGHT_AREA_EPSILON < required;
129
+ return {
130
+ id: space.ref,
131
+ status: failed ? "fail" : "pass",
132
+ subjects: [spaceSubject(space.ref)],
133
+ message: failed
134
+ ? `Insufficient daylight: ${space.ref} — effective window ${space.effectiveWindowAreaM2.toFixed(2)} m2 < required ${required.toFixed(2)} m2 (1/${DAYLIGHT_DIVISOR} of the ${space.floorAreaM2.toFixed(2)} m2 floor)`
135
+ : `${space.ref} has effective window ${space.effectiveWindowAreaM2.toFixed(2)} m2 against required ${required.toFixed(2)} m2`,
136
+ evidence: [
137
+ requiredSpaceEvidence(evidenceById, space.ref),
138
+ comparisonEvidence(
139
+ "required-window-area",
140
+ space.ref,
141
+ { value: space.effectiveWindowAreaM2, unit: "m2" },
142
+ ">=",
143
+ { value: required, unit: "m2" },
144
+ DAYLIGHT_RATIO_RULE_ID,
145
+ ),
146
+ ],
147
+ };
148
+ }));
149
+ }
150
+
151
+ function evaluateDaylightCompleteness(
152
+ artifact: AnalysisArtifact<DaylightAnalysisValue>,
153
+ ): RuleEvaluation {
154
+ if (artifact.state !== "complete") return analysisIndeterminate(artifact);
155
+ if (artifact.value.spaces.length === 0) return notApplicable(NO_POPULATION);
156
+ const evidenceById = new Map(artifact.evidence.map((item) => [item.id, item]));
157
+
158
+ return applicable(artifact.value.spaces.map((space): RuleOutcome => {
159
+ const uncounted = space.missingHeightOpenings.length;
160
+ return {
161
+ id: space.ref,
162
+ status: uncounted > 0 ? "fail" : "pass",
163
+ subjects: [spaceSubject(space.ref)],
164
+ message: uncounted > 0
165
+ ? `Window area is not fully counted: ${space.ref} has a window without h: (write h: on it)`
166
+ : `${space.ref} counts the area of every window that faces daylight`,
167
+ evidence: [
168
+ requiredSpaceEvidence(evidenceById, space.ref),
169
+ comparisonEvidence(
170
+ "uncounted-window-openings",
171
+ space.ref,
172
+ { value: uncounted, unit: "count" },
173
+ "<=",
174
+ { value: 0, unit: "count" },
175
+ DAYLIGHT_UNKNOWN_RULE_ID,
176
+ ),
177
+ ],
178
+ };
179
+ }));
180
+ }
181
+
182
+ /**
183
+ * The windows that face daylight but carry no `h:`, grouped by the space that asked the question.
184
+ *
185
+ * The population and the "does this opening count" test are the ones `daylightInputs` applies;
186
+ * only the identity is added here, so the enumeration cannot disagree with the counted area.
187
+ */
188
+ function uncountedWindows(model: Model): Map<string, MissingHeightOpeningFact[]> {
189
+ const out = new Map<string, MissingHeightOpeningFact[]>();
190
+ for (const [boundaryIndex, boundary] of canonicalBoundaryOrder(model).entries()) {
191
+ const boundaryRef = `${boundary.a}|${boundary.b}@${boundaryIndex}`;
192
+ const openings = canonicalOpeningOrder(boundary)
193
+ .map((opening, openingIndex) => ({ opening, openingIndex }))
194
+ .filter(({ opening }) => opening.kind === "window" && opening.h === undefined);
195
+ if (openings.length === 0) continue;
196
+
197
+ for (const near of [boundary.a, boundary.b] as const) {
198
+ const space = model.spaces.get(near);
199
+ if (!space || !asksDaylight(space)) continue;
200
+ const far = model.spaces.get(near === boundary.a ? boundary.b : boundary.a);
201
+ if (!far || daylightFactor(model, far) === 0) continue;
202
+
203
+ const list = out.get(near) ?? [];
204
+ for (const { opening, openingIndex } of openings) {
205
+ list.push({
206
+ ref: `${boundaryRef}/${openingIndex}`,
207
+ boundaryRef,
208
+ widthMm: opening.w,
209
+ location: {
210
+ ...(boundary.file !== undefined ? { file: boundary.file } : {}),
211
+ line: boundary.line,
212
+ },
213
+ });
214
+ }
215
+ out.set(near, list);
216
+ }
217
+ }
218
+ return out;
219
+ }
220
+
221
+ /** Whether the space put the daylight question on itself — never inferred from the type word (ADR-0020). */
222
+ function asksDaylight(space: Space): boolean {
223
+ return space.rects.length > 0 && space.attrs["daylight"] === 1;
224
+ }
225
+
226
+ /** The coefficient `daylightInputs` applies to what lies beyond the window; 0 means it does not count. */
227
+ function daylightFactor(model: Model, far: Space): number {
228
+ if (isOutside(far)) return 1;
229
+ if (!isSemiOutdoor(model, far)) return 0;
230
+ return isCoveredAbove(model, far) ? COVERED_SEMI_FACTOR : 1;
231
+ }
232
+
233
+ function daylightObservation(space: DaylightSpaceFact, source: Space | undefined): Evidence {
234
+ const subject = spaceSubject(space.ref);
235
+ return {
236
+ id: daylightEvidenceId(space.ref),
237
+ kind: "fact",
238
+ name: "daylightInputs",
239
+ value: {
240
+ floor: { value: space.floorAreaM2, unit: "m2" },
241
+ effectiveWindow: { value: space.effectiveWindowAreaM2, unit: "m2" },
242
+ missingHeightOpenings: space.missingHeightOpenings,
243
+ },
244
+ subjects: [subject],
245
+ sources: [{
246
+ kind: "model",
247
+ subject,
248
+ ...(source
249
+ ? {
250
+ location: {
251
+ ...(source.file !== undefined ? { file: source.file } : {}),
252
+ line: source.line,
253
+ },
254
+ }
255
+ : {}),
256
+ }],
257
+ producedBy: DAYLIGHT_ANALYSIS_ID,
258
+ };
259
+ }
260
+
261
+ function comparisonEvidence(
262
+ id: string,
263
+ ref: string,
264
+ observed: Quantity,
265
+ operator: "<=" | ">=",
266
+ required: Quantity,
267
+ producedBy: ComponentIdentity,
268
+ ): Evidence {
269
+ return {
270
+ id,
271
+ kind: "comparison",
272
+ observed,
273
+ operator,
274
+ required,
275
+ subjects: [spaceSubject(ref)],
276
+ sources: [modelSpaceSource(ref)],
277
+ producedBy,
278
+ };
279
+ }
280
+
281
+ function requiredSpaceEvidence(evidence: ReadonlyMap<string, Evidence>, ref: string): Evidence {
282
+ const item = evidence.get(daylightEvidenceId(ref));
283
+ if (!item) throw new Error(`missing daylight evidence for ${ref}`);
284
+ return item;
285
+ }
286
+
287
+ function modelSpaceSource(ref: string): SourceRef {
288
+ return { kind: "model", subject: spaceSubject(ref) };
289
+ }
290
+
291
+ function spaceSubject(ref: string): SubjectRef {
292
+ return { kind: "space", ref };
293
+ }
294
+
295
+ function daylightEvidenceId(ref: string): string {
296
+ return `daylight:${ref}`;
297
+ }
298
+
299
+ const NO_POPULATION = "No space with a region declares daylight:1";
300
+
301
+ function applicable(outcomes: RuleOutcome[]): RuleEvaluation {
302
+ return {
303
+ applicability: "applicable",
304
+ outcomes: outcomes as [RuleOutcome, ...RuleOutcome[]],
305
+ };
306
+ }
307
+
308
+ function notApplicable(reason: string): RuleEvaluation {
309
+ return { applicability: "not-applicable", reason, evidence: [] };
310
+ }
311
+
312
+ function analysisIndeterminate(
313
+ artifact: Exclude<AnalysisArtifact<DaylightAnalysisValue>, { state: "complete" }>,
314
+ ): Extract<RuleEvaluation, { applicability: "indeterminate" }> {
315
+ return {
316
+ applicability: "indeterminate",
317
+ reason: "Daylight observations are incomplete",
318
+ missing: artifact.missing,
319
+ evidence: artifact.state === "partial" ? artifact.evidence : [],
320
+ };
321
+ }
@@ -0,0 +1,266 @@
1
+ import type {
2
+ AnalysisArtifact,
3
+ AnalysisDefinition,
4
+ AnalysisRef,
5
+ Evidence,
6
+ SourceRef,
7
+ SubjectRef,
8
+ } from "../../analysis/contracts.js";
9
+ import { codePointCompare } from "../../analysis/json.js";
10
+ import { placeOpening, type Segment } from "../../core/graph.js";
11
+ import {
12
+ canonicalBoundaryOrder,
13
+ canonicalOpeningOrder,
14
+ columnsFor,
15
+ levelsSorted,
16
+ type Boundary,
17
+ type Column,
18
+ type Model,
19
+ type Opening,
20
+ } from "../../core/model.js";
21
+ import type { Rule, RuleEvaluation, RuleOutcome } from "../contracts.js";
22
+ import { freezeBuiltin } from "./freeze.js";
23
+
24
+ export const COLUMN_BLOCKS_DOOR_RULE_ID = freezeBuiltin({
25
+ id: "koyu.schematic.column.blocksdoor",
26
+ revision: "1",
27
+ } as const);
28
+
29
+ export type DoorSegmentObservation = {
30
+ readonly x1: number;
31
+ readonly y1: number;
32
+ readonly x2: number;
33
+ readonly y2: number;
34
+ readonly horizontal: boolean;
35
+ };
36
+
37
+ export type ColumnObservation = {
38
+ readonly ref: string;
39
+ readonly grid: string;
40
+ readonly level: string;
41
+ readonly centerMm: { readonly x: number; readonly y: number };
42
+ readonly widthMm: number;
43
+ readonly depthMm: number;
44
+ };
45
+
46
+ export type DoorColumnObservation = {
47
+ readonly ref: string;
48
+ readonly boundaryRef: string;
49
+ readonly level: string;
50
+ readonly boundary: { readonly a: string; readonly b: string };
51
+ readonly widthMm: number;
52
+ readonly centerMm: { readonly x: number; readonly y: number };
53
+ readonly segment: DoorSegmentObservation;
54
+ readonly firstColumnIntersection: ColumnObservation | null;
55
+ };
56
+
57
+ export type DoorColumnCollisionsAnalysisValue = {
58
+ readonly doors: readonly DoorColumnObservation[];
59
+ };
60
+
61
+ export const DOOR_COLUMN_COLLISIONS_ANALYSIS_ID: AnalysisRef<DoorColumnCollisionsAnalysisValue> = freezeBuiltin({
62
+ id: "koyu.analysis.door-column-collisions",
63
+ revision: "1",
64
+ });
65
+
66
+ export const DOOR_COLUMN_COLLISIONS_ANALYSIS: AnalysisDefinition<DoorColumnCollisionsAnalysisValue> = freezeBuiltin<AnalysisDefinition<DoorColumnCollisionsAnalysisValue>>({
67
+ ...DOOR_COLUMN_COLLISIONS_ANALYSIS_ID,
68
+ title: "Door and column geometry observations",
69
+ model: "consistent",
70
+ dependencies: [],
71
+ context: [],
72
+ run: ({ model }): AnalysisArtifact<DoorColumnCollisionsAnalysisValue> => {
73
+ const coreModel = model as unknown as Model;
74
+ const columnsByLevel = new Map<string, Column[]>();
75
+ for (const level of levelsSorted(coreModel)) {
76
+ columnsByLevel.set(level.name, columnsFor(coreModel, level.name));
77
+ }
78
+
79
+ const doors: DoorColumnObservation[] = [];
80
+ const evidence: Evidence[] = [];
81
+ for (const [boundaryIndex, boundary] of canonicalBoundaryOrder(coreModel).entries()) {
82
+ const level = model.spaces.get(boundary.a)?.level ?? model.spaces.get(boundary.b)?.level;
83
+ if (!level) continue;
84
+ const boundaryRef = `${boundary.a}|${boundary.b}@${boundaryIndex}`;
85
+ for (const [openingIndex, opening] of canonicalOpeningOrder(boundary).entries()) {
86
+ if (opening.kind !== "door") continue;
87
+ const placed = placeOpening(coreModel, boundary, opening);
88
+ if ("error" in placed) continue;
89
+ const column = firstIntersectingColumn(placed.segment, placed.cx, placed.cy, opening, columnsByLevel.get(level) ?? []);
90
+ const observation: DoorColumnObservation = {
91
+ ref: `${boundaryRef}/${openingIndex}`,
92
+ boundaryRef,
93
+ level,
94
+ boundary: { a: boundary.a, b: boundary.b },
95
+ widthMm: opening.w,
96
+ centerMm: { x: placed.cx, y: placed.cy },
97
+ segment: segmentObservation(placed.segment),
98
+ firstColumnIntersection: column ? columnObservation(column) : null,
99
+ };
100
+ doors.push(observation);
101
+ evidence.push(doorColumnEvidence(observation, boundary, column, coreModel));
102
+ }
103
+ }
104
+
105
+ doors.sort((a, b) => codePointCompare(a.ref, b.ref));
106
+ evidence.sort((a, b) => codePointCompare(a.id, b.id));
107
+ return { state: "complete", value: { doors }, evidence };
108
+ },
109
+ });
110
+
111
+ export const COLUMN_BLOCKS_DOOR_RULE: Rule = freezeBuiltin<Rule>({
112
+ ...COLUMN_BLOCKS_DOOR_RULE_ID,
113
+ title: "Column and door overlap",
114
+ level: "violation",
115
+ model: "consistent",
116
+ analyses: [{ analysis: DOOR_COLUMN_COLLISIONS_ANALYSIS_ID, accept: "complete" }],
117
+ context: [],
118
+ authority: [],
119
+ evaluate: ({ get }) => evaluateColumnDoorGeometry(get(DOOR_COLUMN_COLLISIONS_ANALYSIS_ID)),
120
+ });
121
+
122
+ function evaluateColumnDoorGeometry(
123
+ artifact: AnalysisArtifact<DoorColumnCollisionsAnalysisValue>,
124
+ ): RuleEvaluation {
125
+ if (artifact.state !== "complete") return analysisIndeterminate(artifact);
126
+ if (artifact.value.doors.length === 0) return notApplicable("No placeable door has a resolvable level");
127
+
128
+ const evidenceById = new Map(artifact.evidence.map((item) => [item.id, item]));
129
+ return applicable(artifact.value.doors.map((door): RuleOutcome => {
130
+ const column = door.firstColumnIntersection;
131
+ const failed = column !== null;
132
+ const evidence = evidenceById.get(doorEvidenceId(door.ref));
133
+ if (!evidence) throw new Error(`missing door-column evidence for ${door.ref}`);
134
+ return {
135
+ id: door.ref,
136
+ status: failed ? "fail" : "pass",
137
+ subjects: [spaceSubject(door.boundary.a), spaceSubject(door.boundary.b)],
138
+ message: failed
139
+ ? `${door.ref} intersects the derived column at ${column.grid}`
140
+ : `${door.ref} does not intersect a derived column`,
141
+ evidence: [evidence],
142
+ };
143
+ }));
144
+ }
145
+
146
+ function firstIntersectingColumn(
147
+ segment: Segment,
148
+ centerX: number,
149
+ centerY: number,
150
+ opening: Opening,
151
+ columns: readonly Column[],
152
+ ): Column | undefined {
153
+ const half = opening.w / 2;
154
+ for (const column of columns) {
155
+ const x1 = column.x - column.w / 2;
156
+ const x2 = column.x + column.w / 2;
157
+ const y1 = column.y - column.d / 2;
158
+ const y2 = column.y + column.d / 2;
159
+ const along = segment.horizontal
160
+ ? Math.max(centerX - half, x1) < Math.min(centerX + half, x2)
161
+ : Math.max(centerY - half, y1) < Math.min(centerY + half, y2);
162
+ const across = segment.horizontal
163
+ ? y1 < segment.y1 && segment.y1 < y2
164
+ : x1 < segment.x1 && segment.x1 < x2;
165
+ if (along && across) return column;
166
+ }
167
+ return undefined;
168
+ }
169
+
170
+ function doorColumnEvidence(
171
+ observation: DoorColumnObservation,
172
+ boundary: Boundary,
173
+ column: Column | undefined,
174
+ model: Model,
175
+ ): Evidence {
176
+ const door = openingSubject(observation.ref);
177
+ const columnRef = column ? columnSubject(columnRefOf(column)) : undefined;
178
+ const declaration = column ? model.columns[column.decl] : undefined;
179
+ const sources: SourceRef[] = [modelSource(boundarySubject(observation.boundaryRef), boundary.line, boundary.file)];
180
+ if (columnRef && declaration) sources.push(modelSource(columnRef, declaration.line, declaration.file));
181
+ return {
182
+ id: doorEvidenceId(observation.ref),
183
+ kind: "fact",
184
+ name: "doorColumnGeometry",
185
+ value: observation,
186
+ subjects: columnRef ? [door, columnRef] : [door],
187
+ sources: sources as [SourceRef, ...SourceRef[]],
188
+ producedBy: DOOR_COLUMN_COLLISIONS_ANALYSIS_ID,
189
+ };
190
+ }
191
+
192
+ function columnObservation(column: Column): ColumnObservation {
193
+ return {
194
+ ref: columnRefOf(column),
195
+ grid: column.grid,
196
+ level: column.level,
197
+ centerMm: { x: column.x, y: column.y },
198
+ widthMm: column.w,
199
+ depthMm: column.d,
200
+ };
201
+ }
202
+
203
+ function segmentObservation(segment: Segment): DoorSegmentObservation {
204
+ return {
205
+ x1: segment.x1,
206
+ y1: segment.y1,
207
+ x2: segment.x2,
208
+ y2: segment.y2,
209
+ horizontal: segment.horizontal,
210
+ };
211
+ }
212
+
213
+ function applicable(outcomes: RuleOutcome[]): RuleEvaluation {
214
+ if (outcomes.length === 0) throw new Error("applicable evaluation requires an outcome");
215
+ return {
216
+ applicability: "applicable",
217
+ outcomes: outcomes as [RuleOutcome, ...RuleOutcome[]],
218
+ };
219
+ }
220
+
221
+ function notApplicable(reason: string): RuleEvaluation {
222
+ return { applicability: "not-applicable", reason, evidence: [] };
223
+ }
224
+
225
+ function analysisIndeterminate(
226
+ artifact: Exclude<AnalysisArtifact<DoorColumnCollisionsAnalysisValue>, { state: "complete" }>,
227
+ ): RuleEvaluation {
228
+ return {
229
+ applicability: "indeterminate",
230
+ reason: "Door and column geometry observations are incomplete",
231
+ missing: artifact.missing,
232
+ evidence: artifact.state === "partial" ? artifact.evidence : [],
233
+ };
234
+ }
235
+
236
+ function modelSource(subject: SubjectRef, line: number, file?: string): SourceRef {
237
+ return {
238
+ kind: "model",
239
+ subject,
240
+ location: { ...(file !== undefined ? { file } : {}), line },
241
+ };
242
+ }
243
+
244
+ function openingSubject(ref: string): SubjectRef {
245
+ return { kind: "opening", ref };
246
+ }
247
+
248
+ function columnSubject(ref: string): SubjectRef {
249
+ return { kind: "koyu.column", ref };
250
+ }
251
+
252
+ function boundarySubject(ref: string): SubjectRef {
253
+ return { kind: "boundary", ref };
254
+ }
255
+
256
+ function spaceSubject(ref: string): SubjectRef {
257
+ return { kind: "space", ref };
258
+ }
259
+
260
+ function columnRefOf(column: Column): string {
261
+ return `${column.level}/${column.grid}`;
262
+ }
263
+
264
+ function doorEvidenceId(ref: string): string {
265
+ return `door-column:${ref}`;
266
+ }