@kensnzk/koyu 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/ci.yml +16 -0
- package/.zenodo.json +20 -0
- package/CITATION.cff +26 -0
- package/LICENSE +73 -0
- package/NOTICE +2 -0
- package/README.ja.md +71 -0
- package/README.md +72 -0
- package/docs/decisions/0001-notation-v0.md +29 -0
- package/docs/decisions/0002-height-and-offsets.md +27 -0
- package/docs/decisions/0003-uncounted-divisions.md +36 -0
- package/docs/decisions/0004-typical-floors.md +21 -0
- package/docs/decisions/0005-zones-and-unions.md +23 -0
- package/docs/decisions/0006-voids-and-light.md +31 -0
- package/docs/decisions/0007-semi-outdoor-air.md +27 -0
- package/docs/decisions/0008-vocabulary-and-level-attr.md +23 -0
- package/docs/decisions/0009-site-and-exterior.md +29 -0
- package/docs/ifc-coverage.md +82 -0
- package/docs/ifcx-notes.md +25 -0
- package/docs/img/house-L1.svg +80 -0
- package/docs/img/house-L2.svg +53 -0
- package/docs/img/mansion-L1.svg +113 -0
- package/docs/img/mansion-L10.svg +111 -0
- package/docs/img/mansion-L5.svg +160 -0
- package/docs/img/office-L1.svg +116 -0
- package/docs/img/office-L2.svg +109 -0
- package/docs/img/two-rooms.svg +39 -0
- package/docs/log/2026-07-22.md +13 -0
- package/docs/log/2026-07-23.md +51 -0
- package/docs/roadmap.md +55 -0
- package/docs/writing-architecture.md +119 -0
- package/examples/comparison/README.md +38 -0
- package/examples/comparison/gen-ifc4.ts +133 -0
- package/examples/comparison/gen-ifcx.ts +134 -0
- package/examples/comparison/two-rooms.ifc +152 -0
- package/examples/comparison/two-rooms.ifcx +1030 -0
- package/examples/comparison/validate-ifc.ts +33 -0
- package/examples/house.muro +89 -0
- package/examples/mansion.muro +187 -0
- package/examples/office.muro +110 -0
- package/examples/two-rooms.canonical.json +111 -0
- package/examples/two-rooms.muro +22 -0
- package/package.json +33 -0
- package/spec/notation-v0.md +164 -0
- package/spec/vocabulary.md +62 -0
- package/src/check.ts +302 -0
- package/src/cli.ts +281 -0
- package/src/graph.ts +306 -0
- package/src/index.ts +8 -0
- package/src/light.ts +66 -0
- package/src/model.ts +367 -0
- package/src/parse.ts +512 -0
- package/src/plan.ts +334 -0
- package/src/site.ts +82 -0
- package/test/core.test.ts +171 -0
- package/test/design2.test.ts +97 -0
- package/test/house.test.ts +74 -0
- package/test/mansion.test.ts +99 -0
- package/test/office.test.ts +71 -0
- package/test/uncounted.test.ts +120 -0
- package/test/vertical.test.ts +75 -0
- package/tsconfig.json +13 -0
package/src/cli.ts
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// koyu v0.1 — CLI
|
|
3
|
+
// npm run koyu -- check examples/office.muro
|
|
4
|
+
// npm run koyu -- plan examples/office.muro -l L2 -o out/office-L2.svg
|
|
5
|
+
// npm run koyu -- doors examples/office.muro /L2/office /out
|
|
6
|
+
// npm run koyu -- graph examples/office.muro
|
|
7
|
+
// npm run koyu -- stats examples/office.muro
|
|
8
|
+
// npm run koyu -- levels examples/office.muro # テキストの矩計 (高さの積み上がり)
|
|
9
|
+
// npm run koyu -- json examples/office.muro
|
|
10
|
+
|
|
11
|
+
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
12
|
+
import { dirname } from "node:path";
|
|
13
|
+
import { check } from "./check.js";
|
|
14
|
+
import { doorsBetween, neighbors } from "./graph.js";
|
|
15
|
+
import { daylight } from "./light.js";
|
|
16
|
+
import { siteReport } from "./site.js";
|
|
17
|
+
import {
|
|
18
|
+
areaM2,
|
|
19
|
+
displayName,
|
|
20
|
+
effectiveUse,
|
|
21
|
+
heff,
|
|
22
|
+
isSemiOutdoor,
|
|
23
|
+
levelsSorted,
|
|
24
|
+
SourceError,
|
|
25
|
+
toCanonical,
|
|
26
|
+
zoneAreaM2,
|
|
27
|
+
type Model,
|
|
28
|
+
} from "./model.js";
|
|
29
|
+
import { parse } from "./parse.js";
|
|
30
|
+
import { svgPlan } from "./plan.js";
|
|
31
|
+
|
|
32
|
+
function load(file: string): Model {
|
|
33
|
+
const src = readFileSync(file, "utf8");
|
|
34
|
+
return parse(src);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function opt(rest: string[], ...names: string[]): string | undefined {
|
|
38
|
+
for (const n of names) {
|
|
39
|
+
const i = rest.indexOf(n);
|
|
40
|
+
if (i >= 0 && rest[i + 1]) return rest[i + 1];
|
|
41
|
+
}
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function main(argv: string[]): number {
|
|
46
|
+
const [cmd, file, ...rest] = argv;
|
|
47
|
+
if (!cmd || !file) {
|
|
48
|
+
console.log(
|
|
49
|
+
"使い方: koyu <check|plan|doors|graph|stats|levels|light|site|json> <file.muro> [引数...]",
|
|
50
|
+
);
|
|
51
|
+
return 2;
|
|
52
|
+
}
|
|
53
|
+
const model = load(file);
|
|
54
|
+
|
|
55
|
+
switch (cmd) {
|
|
56
|
+
case "check": {
|
|
57
|
+
const { errors, warnings } = check(model);
|
|
58
|
+
for (const w of warnings) console.log(`⚠ ${w}`);
|
|
59
|
+
for (const e of errors) console.log(`✖ ${e}`);
|
|
60
|
+
if (errors.length === 0) {
|
|
61
|
+
console.log(
|
|
62
|
+
`✔ 整合 — 空間 ${model.spaces.size} / 境界 ${model.boundaries.length}` +
|
|
63
|
+
(warnings.length ? ` (警告 ${warnings.length})` : ""),
|
|
64
|
+
);
|
|
65
|
+
return 0;
|
|
66
|
+
}
|
|
67
|
+
return 1;
|
|
68
|
+
}
|
|
69
|
+
case "json": {
|
|
70
|
+
process.stdout.write(toCanonical(model));
|
|
71
|
+
return 0;
|
|
72
|
+
}
|
|
73
|
+
case "plan": {
|
|
74
|
+
const level = opt(rest, "-l", "--level") ?? Object.keys(model.levels)[0];
|
|
75
|
+
const explicit = opt(rest, "-o");
|
|
76
|
+
const outFile =
|
|
77
|
+
explicit ?? `${file.replace(/\.muro$/, "")}-${level}.svg`;
|
|
78
|
+
const svg = svgPlan(model, { level });
|
|
79
|
+
mkdirSync(dirname(outFile), { recursive: true });
|
|
80
|
+
writeFileSync(outFile, svg);
|
|
81
|
+
console.log(`平面図を生成しました: ${outFile}`);
|
|
82
|
+
return 0;
|
|
83
|
+
}
|
|
84
|
+
case "doors": {
|
|
85
|
+
const [from, to] = rest;
|
|
86
|
+
if (!from || !to) {
|
|
87
|
+
console.log("使い方: koyu doors <file> /パスA /パスB");
|
|
88
|
+
return 2;
|
|
89
|
+
}
|
|
90
|
+
const route = doorsBetween(model, from, to);
|
|
91
|
+
if (!route) {
|
|
92
|
+
console.log(`${from} から ${to} へは到達できません`);
|
|
93
|
+
return 1;
|
|
94
|
+
}
|
|
95
|
+
console.log(`${route.doors}枚 — ${route.path.join(" → ")}`);
|
|
96
|
+
return 0;
|
|
97
|
+
}
|
|
98
|
+
case "graph": {
|
|
99
|
+
for (const s of model.spaces.values()) {
|
|
100
|
+
const ns = neighbors(model, s.path);
|
|
101
|
+
console.log(`${s.path} (${displayName(s)})`);
|
|
102
|
+
for (const n of ns) {
|
|
103
|
+
const mark =
|
|
104
|
+
n.boundary.kind === "open"
|
|
105
|
+
? "〰 開放"
|
|
106
|
+
: n.boundary.kind === "wall" && n.boundary.air && !n.passable
|
|
107
|
+
? "| 手すり等(外気開放・通行不可)"
|
|
108
|
+
: n.boundary.kind === "stair"
|
|
109
|
+
? "↕ 階段"
|
|
110
|
+
: n.boundary.kind === "shaft"
|
|
111
|
+
? "↕ シャフト(通行不可)"
|
|
112
|
+
: n.boundary.kind === "void"
|
|
113
|
+
? "↕ 吹抜け"
|
|
114
|
+
: n.passable
|
|
115
|
+
? `— 扉${n.doors}`
|
|
116
|
+
: "| 壁";
|
|
117
|
+
const attrs = Object.entries(n.boundary.attrs)
|
|
118
|
+
.map(([k, v]) => `${k}:${v}`)
|
|
119
|
+
.join(" ");
|
|
120
|
+
console.log(` ${mark} → ${n.space.path}${attrs ? ` (${attrs})` : ""}`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return 0;
|
|
124
|
+
}
|
|
125
|
+
case "stats": {
|
|
126
|
+
const levels = levelsSorted(model);
|
|
127
|
+
const spaces = [...model.spaces.values()];
|
|
128
|
+
let total = 0;
|
|
129
|
+
const byType = new Map<string, number>();
|
|
130
|
+
const byUse = new Map<string, number>();
|
|
131
|
+
let semiTotal = 0;
|
|
132
|
+
for (const l of levels) {
|
|
133
|
+
const onLevel = spaces.filter((s) => s.level === l.name && s.rects.length > 0);
|
|
134
|
+
if (onLevel.length === 0) continue;
|
|
135
|
+
console.log(`${l.name}`);
|
|
136
|
+
let sub = 0;
|
|
137
|
+
for (const s of onLevel) {
|
|
138
|
+
if (s.type === "void") {
|
|
139
|
+
console.log(` ${s.path}\t${displayName(s)}\t吹抜け (床面積不算入)`);
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
const a = areaM2(s)!;
|
|
143
|
+
if (isSemiOutdoor(model, s)) {
|
|
144
|
+
semiTotal += a;
|
|
145
|
+
console.log(
|
|
146
|
+
` ${s.path}\t${displayName(s)}\t${s.type}\t${a.toFixed(2)}㎡ (半屋外・別掲)`,
|
|
147
|
+
);
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
sub += a;
|
|
151
|
+
total += a;
|
|
152
|
+
byType.set(s.type, (byType.get(s.type) ?? 0) + a);
|
|
153
|
+
const use = effectiveUse(model, s);
|
|
154
|
+
if (use) byUse.set(use, (byUse.get(use) ?? 0) + a);
|
|
155
|
+
console.log(` ${s.path}\t${displayName(s)}\t${s.type}\t${a.toFixed(2)}㎡`);
|
|
156
|
+
}
|
|
157
|
+
console.log(` 小計 ${sub.toFixed(2)}㎡`);
|
|
158
|
+
}
|
|
159
|
+
console.log(`合計 ${total.toFixed(2)}㎡ (屋内床面積)`);
|
|
160
|
+
if (semiTotal > 0) {
|
|
161
|
+
console.log(`半屋外 ${semiTotal.toFixed(2)}㎡ (バルコニー・屋外階段等 — 算入条件は法規細部のため別掲)`);
|
|
162
|
+
}
|
|
163
|
+
const zonesToShow = [...model.zones.values()].filter((z) => z.attrs["site"] !== 1);
|
|
164
|
+
if (zonesToShow.length > 0) {
|
|
165
|
+
console.log("ゾーン別 (数える集約):");
|
|
166
|
+
for (const z of zonesToShow.sort((a, b) => (a.path < b.path ? -1 : 1))) {
|
|
167
|
+
const nm = z.attrs["name"];
|
|
168
|
+
console.log(
|
|
169
|
+
` ${z.path}\t${typeof nm === "string" ? nm : ""}\t${zoneAreaM2(model, z.path).toFixed(2)}㎡`,
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
for (const [t, a] of byType) console.log(` ${t}: ${a.toFixed(2)}㎡`);
|
|
174
|
+
if (byUse.size > 0) {
|
|
175
|
+
const parts = [...byUse.entries()].map(
|
|
176
|
+
([u, a]) => `${u} ${a.toFixed(2)}㎡ (${((a / total) * 100).toFixed(1)}%)`,
|
|
177
|
+
);
|
|
178
|
+
console.log(`use別: ${parts.join(" / ")}`);
|
|
179
|
+
}
|
|
180
|
+
return 0;
|
|
181
|
+
}
|
|
182
|
+
case "light": {
|
|
183
|
+
const results = daylight(model);
|
|
184
|
+
if (results.length === 0) {
|
|
185
|
+
console.log("対象の居室 (住居系) がありません");
|
|
186
|
+
return 0;
|
|
187
|
+
}
|
|
188
|
+
let fail = 0;
|
|
189
|
+
for (const r of results) {
|
|
190
|
+
if (!r.ok) fail++;
|
|
191
|
+
const ratio = r.window > 0 ? `1/${(r.floor / r.window).toFixed(1)}` : "窓なし";
|
|
192
|
+
console.log(
|
|
193
|
+
`${r.ok ? "✔" : "✖"} ${r.space.path}\t${displayName(r.space)}\t窓 ${r.window.toFixed(2)}㎡ / 床 ${r.floor.toFixed(2)}㎡ = ${ratio} (必要 1/7 ≈ ${r.need.toFixed(2)}㎡)` +
|
|
194
|
+
(r.missingH ? " ⚠ h未指定の窓は数えていません" : ""),
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
console.log(
|
|
198
|
+
fail === 0
|
|
199
|
+
? `✔ 全${results.length}室が 1/7 を満たします (補正係数なしの粗い判定)`
|
|
200
|
+
: `✖ ${results.length}室中 ${fail}室が不足しています`,
|
|
201
|
+
);
|
|
202
|
+
return fail === 0 ? 0 : 1;
|
|
203
|
+
}
|
|
204
|
+
case "site": {
|
|
205
|
+
// 敷地の問い: 敷地面積・接道・建蔽率・容積率 (基本計画のボリューム検討の数字)
|
|
206
|
+
const r = siteReport(model);
|
|
207
|
+
if (!r.siteZone && r.roads.length === 0) {
|
|
208
|
+
console.log("敷地がありません (zone に site:1 を、道路に road:幅員 を宣言します)");
|
|
209
|
+
return 1;
|
|
210
|
+
}
|
|
211
|
+
const site = r.declaredArea ?? r.derivedArea;
|
|
212
|
+
if (r.siteZone) {
|
|
213
|
+
const nm = r.siteZone.attrs["name"];
|
|
214
|
+
console.log(`敷地 ${r.siteZone.path}${typeof nm === "string" ? ` (${nm})` : ""}`);
|
|
215
|
+
}
|
|
216
|
+
if (r.declaredArea !== undefined) {
|
|
217
|
+
const ok = Math.abs(r.declaredArea - r.derivedArea) < 0.05;
|
|
218
|
+
console.log(
|
|
219
|
+
` 敷地面積: 宣言 ${r.declaredArea.toFixed(2)}㎡ / 導出 ${r.derivedArea.toFixed(2)}㎡ ${ok ? "✔ 一致" : "⚠ 不一致 (タイルの隙間か重なり)"}`,
|
|
220
|
+
);
|
|
221
|
+
} else {
|
|
222
|
+
console.log(` 敷地面積 (導出): ${r.derivedArea.toFixed(2)}㎡`);
|
|
223
|
+
}
|
|
224
|
+
for (const road of r.roads) {
|
|
225
|
+
const nm = road.road.attrs["name"];
|
|
226
|
+
console.log(
|
|
227
|
+
` 接道: ${road.road.path}${typeof nm === "string" ? ` (${nm})` : ""} 幅員${road.width}mm ・ 接道長 ${road.frontage}mm ${road.frontage >= 2000 ? "✔ 2m以上" : "✖ 2m未満"}`,
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
console.log(` 建築面積 (水平投影・粗): ${r.footprint.toFixed(2)}㎡ → 建蔽率 ${((r.footprint / site) * 100).toFixed(1)}%`);
|
|
231
|
+
console.log(` 延べ面積: ${r.totalFloor.toFixed(2)}㎡ → 容積率 ${((r.totalFloor / site) * 100).toFixed(1)}%`);
|
|
232
|
+
return 0;
|
|
233
|
+
}
|
|
234
|
+
case "levels": {
|
|
235
|
+
// テキストの矩計: レベルの積み上がりと高さの検算
|
|
236
|
+
const levels = levelsSorted(model);
|
|
237
|
+
if (levels.length === 0) {
|
|
238
|
+
console.log("レベルが定義されていません");
|
|
239
|
+
return 1;
|
|
240
|
+
}
|
|
241
|
+
for (let i = levels.length - 1; i >= 0; i--) {
|
|
242
|
+
const l = levels[i]!;
|
|
243
|
+
const upper = levels[i + 1];
|
|
244
|
+
console.log(
|
|
245
|
+
`${l.name}\tz:${l.z}` +
|
|
246
|
+
(l.h !== undefined ? `\th:${l.h}` : "") +
|
|
247
|
+
(l.slab !== undefined ? `\tslab:${l.slab}` : ""),
|
|
248
|
+
);
|
|
249
|
+
if (upper) {
|
|
250
|
+
const pitch = upper.z - l.z;
|
|
251
|
+
const detail =
|
|
252
|
+
l.h !== undefined && upper.slab !== undefined
|
|
253
|
+
? ` = 天井${l.h} + slab${upper.slab}` +
|
|
254
|
+
(pitch - l.h - upper.slab > 0 ? ` + 余り${pitch - l.h - upper.slab}` : "")
|
|
255
|
+
: "";
|
|
256
|
+
console.log(` ↑ 階高 ${pitch}${detail}`);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
const spaces = [...model.spaces.values()].filter((s) => s.rects.length > 0 && s.level);
|
|
260
|
+
const overrides = spaces.filter((s) => typeof s.attrs["h"] === "number");
|
|
261
|
+
for (const s of overrides) {
|
|
262
|
+
console.log(`個別天井高: ${s.path} h:${heff(model, s)}`);
|
|
263
|
+
}
|
|
264
|
+
return 0;
|
|
265
|
+
}
|
|
266
|
+
default:
|
|
267
|
+
console.log(`未知のコマンドです: ${cmd}`);
|
|
268
|
+
return 2;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
try {
|
|
273
|
+
process.exitCode = main(process.argv.slice(2));
|
|
274
|
+
} catch (e) {
|
|
275
|
+
if (e instanceof SourceError) {
|
|
276
|
+
console.error(`✖ ${e.message}`);
|
|
277
|
+
process.exitCode = 1;
|
|
278
|
+
} else {
|
|
279
|
+
throw e;
|
|
280
|
+
}
|
|
281
|
+
}
|
package/src/graph.ts
ADDED
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
// koyu — 空間グラフ
|
|
2
|
+
// 節点が空間、辺が境界。「この室とこの室は繋がっているか」「ここから外へ扉いくつか」が
|
|
3
|
+
// 変換なしにそのままグラフへの問いになる。
|
|
4
|
+
// 空間の領域は矩形の合併 (L字など)。壁は合併の外周と共有辺から導出される。
|
|
5
|
+
|
|
6
|
+
import type { Boundary, Edge, Model, Opening, Rect, Space } from "./model.js";
|
|
7
|
+
|
|
8
|
+
/** 壁芯線分 (mm)。水平なら y1===y2、垂直なら x1===x2 */
|
|
9
|
+
export interface Segment {
|
|
10
|
+
x1: number;
|
|
11
|
+
y1: number;
|
|
12
|
+
x2: number;
|
|
13
|
+
y2: number;
|
|
14
|
+
horizontal: boolean;
|
|
15
|
+
/** boundary.a 側 (領域を持つ側) の矩形から見た辺 */
|
|
16
|
+
edgeOfA?: Edge;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const EPS = 0.5;
|
|
20
|
+
|
|
21
|
+
/** 二つの矩形が共有する辺の線分 (触れていなければ undefined) */
|
|
22
|
+
export function sharedSegment(a: Rect, b: Rect): Segment | undefined {
|
|
23
|
+
for (const [x, ea] of [
|
|
24
|
+
[a.x2, "E"],
|
|
25
|
+
[a.x1, "W"],
|
|
26
|
+
] as const) {
|
|
27
|
+
const bx = ea === "E" ? b.x1 : b.x2;
|
|
28
|
+
if (Math.abs(x - bx) < EPS) {
|
|
29
|
+
const y1 = Math.max(a.y1, b.y1);
|
|
30
|
+
const y2 = Math.min(a.y2, b.y2);
|
|
31
|
+
if (y2 - y1 > EPS) return { x1: x, y1, x2: x, y2, horizontal: false, edgeOfA: ea };
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
for (const [y, ea] of [
|
|
35
|
+
[a.y2, "N"],
|
|
36
|
+
[a.y1, "S"],
|
|
37
|
+
] as const) {
|
|
38
|
+
const by = ea === "N" ? b.y1 : b.y2;
|
|
39
|
+
if (Math.abs(y - by) < EPS) {
|
|
40
|
+
const x1 = Math.max(a.x1, b.x1);
|
|
41
|
+
const x2 = Math.min(a.x2, b.x2);
|
|
42
|
+
if (x2 - x1 > EPS) return { x1, y1: y, x2, y2: y, horizontal: true, edgeOfA: ea };
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 矩形の指定辺の線分から、他の矩形と重なる区間を除いた残り */
|
|
49
|
+
function rectPerimeterRemainder(room: Rect, edge: Edge, others: Rect[]): Segment[] {
|
|
50
|
+
const horizontal = edge === "N" || edge === "S";
|
|
51
|
+
const fixed = edge === "N" ? room.y2 : edge === "S" ? room.y1 : edge === "E" ? room.x2 : room.x1;
|
|
52
|
+
const lo = horizontal ? room.x1 : room.y1;
|
|
53
|
+
const hi = horizontal ? room.x2 : room.y2;
|
|
54
|
+
|
|
55
|
+
let intervals: Array<[number, number]> = [[lo, hi]];
|
|
56
|
+
for (const o of others) {
|
|
57
|
+
const touches = horizontal
|
|
58
|
+
? Math.abs((edge === "N" ? o.y1 : o.y2) - fixed) < EPS
|
|
59
|
+
: Math.abs((edge === "E" ? o.x1 : o.x2) - fixed) < EPS;
|
|
60
|
+
if (!touches) continue;
|
|
61
|
+
const olo = horizontal ? o.x1 : o.y1;
|
|
62
|
+
const ohi = horizontal ? o.x2 : o.y2;
|
|
63
|
+
intervals = intervals.flatMap(([s, e]) => {
|
|
64
|
+
const cs = Math.max(s, olo);
|
|
65
|
+
const ce = Math.min(e, ohi);
|
|
66
|
+
if (ce - cs <= EPS) return [[s, e] as [number, number]];
|
|
67
|
+
const out: Array<[number, number]> = [];
|
|
68
|
+
if (cs - s > EPS) out.push([s, cs]);
|
|
69
|
+
if (e - ce > EPS) out.push([ce, e]);
|
|
70
|
+
return out;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
return intervals.map(([s, e]) =>
|
|
74
|
+
horizontal
|
|
75
|
+
? { x1: s, y1: fixed, x2: e, y2: fixed, horizontal: true, edgeOfA: edge }
|
|
76
|
+
: { x1: fixed, y1: s, x2: fixed, y2: e, horizontal: false, edgeOfA: edge },
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** 共線で連続する線分をまとめる (L字の合併外周を一本の壁にする) */
|
|
81
|
+
export function mergeCollinear(segs: Segment[]): Segment[] {
|
|
82
|
+
const groups = new Map<string, Segment[]>();
|
|
83
|
+
for (const s of segs) {
|
|
84
|
+
const key = `${s.horizontal ? "h" : "v"}:${s.horizontal ? s.y1 : s.x1}:${s.edgeOfA ?? ""}`;
|
|
85
|
+
const g = groups.get(key) ?? [];
|
|
86
|
+
g.push(s);
|
|
87
|
+
groups.set(key, g);
|
|
88
|
+
}
|
|
89
|
+
const out: Segment[] = [];
|
|
90
|
+
for (const g of groups.values()) {
|
|
91
|
+
g.sort((p, q) => (p.horizontal ? p.x1 - q.x1 : p.y1 - q.y1));
|
|
92
|
+
let cur = { ...g[0]! };
|
|
93
|
+
for (let i = 1; i < g.length; i++) {
|
|
94
|
+
const s = g[i]!;
|
|
95
|
+
const curEnd = cur.horizontal ? cur.x2 : cur.y2;
|
|
96
|
+
const sStart = s.horizontal ? s.x1 : s.y1;
|
|
97
|
+
if (sStart <= curEnd + EPS) {
|
|
98
|
+
if (cur.horizontal) cur.x2 = Math.max(cur.x2, s.x2);
|
|
99
|
+
else cur.y2 = Math.max(cur.y2, s.y2);
|
|
100
|
+
} else {
|
|
101
|
+
out.push(cur);
|
|
102
|
+
cur = { ...s };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
out.push(cur);
|
|
106
|
+
}
|
|
107
|
+
return out;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** 境界の壁芯線分を導く。壁の位置は空間の割付から生成される — 壁を置く操作は存在しない */
|
|
111
|
+
export function segmentsFor(model: Model, b: Boundary): Segment[] {
|
|
112
|
+
const sa = model.spaces.get(b.a);
|
|
113
|
+
const sb = model.spaces.get(b.b);
|
|
114
|
+
if (!sa || !sb) return [];
|
|
115
|
+
|
|
116
|
+
// 垂直境界 (stair/shaft/void/暗黙のslab) は壁線分を持たない
|
|
117
|
+
if (b.kind === "stair" || b.kind === "shaft" || b.kind === "void") return [];
|
|
118
|
+
|
|
119
|
+
let segs: Segment[] = [];
|
|
120
|
+
const aHas = sa.rects.length > 0;
|
|
121
|
+
const bHas = sb.rects.length > 0;
|
|
122
|
+
|
|
123
|
+
if (aHas && bHas) {
|
|
124
|
+
if (sa.level !== sb.level) return []; // 異なるレベル間に壁は立たない
|
|
125
|
+
for (const ra of sa.rects) {
|
|
126
|
+
for (const rb of sb.rects) {
|
|
127
|
+
const s = sharedSegment(ra, rb);
|
|
128
|
+
if (s) segs.push(s);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
} else if (aHas || bHas) {
|
|
132
|
+
// 片側が領域を持たない (外部など): 合併の外周から、同レベルで接する他室の区間を除いた残り
|
|
133
|
+
const roomSpace = aHas ? sa : sb;
|
|
134
|
+
const others: Rect[] = [];
|
|
135
|
+
for (const s of model.spaces.values()) {
|
|
136
|
+
if (s === sa || s === sb) continue;
|
|
137
|
+
if (s.level !== roomSpace.level) continue;
|
|
138
|
+
others.push(...s.rects);
|
|
139
|
+
}
|
|
140
|
+
for (let ri = 0; ri < roomSpace.rects.length; ri++) {
|
|
141
|
+
const room = roomSpace.rects[ri]!;
|
|
142
|
+
const siblings = roomSpace.rects.filter((_, i) => i !== ri);
|
|
143
|
+
const blockers = [...others, ...siblings];
|
|
144
|
+
for (const e of ["N", "E", "S", "W"] as const) {
|
|
145
|
+
segs.push(...rectPerimeterRemainder(room, e, blockers));
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
segs = mergeCollinear(segs);
|
|
150
|
+
if (b.edge) segs = segs.filter((s) => s.edgeOfA === b.edge);
|
|
151
|
+
return segs;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function segmentLength(s: Segment): number {
|
|
155
|
+
return s.horizontal ? s.x2 - s.x1 : s.y2 - s.y1;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** 平面上の重なり (垂直隣接の導出に使う)。重ならなければ undefined */
|
|
159
|
+
export function planOverlap(a: Rect, b: Rect): Rect | undefined {
|
|
160
|
+
const x1 = Math.max(a.x1, b.x1);
|
|
161
|
+
const x2 = Math.min(a.x2, b.x2);
|
|
162
|
+
const y1 = Math.max(a.y1, b.y1);
|
|
163
|
+
const y2 = Math.min(a.y2, b.y2);
|
|
164
|
+
if (x2 - x1 > EPS && y2 - y1 > EPS) return { x1, y1, x2, y2 };
|
|
165
|
+
return undefined;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** 二つの空間が平面上で重なるか (合併同士) */
|
|
169
|
+
export function spacesOverlap(a: Space, b: Space): boolean {
|
|
170
|
+
for (const ra of a.rects) {
|
|
171
|
+
for (const rb of b.rects) {
|
|
172
|
+
if (planOverlap(ra, rb)) return true;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** 境界線分上に位置を持つもの (開口・seg) の共通形 */
|
|
179
|
+
export interface Band {
|
|
180
|
+
w: number;
|
|
181
|
+
at: number;
|
|
182
|
+
edge?: Edge;
|
|
183
|
+
line: number;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export interface PlacedBand {
|
|
187
|
+
segment: Segment;
|
|
188
|
+
/** 中心の座標 mm */
|
|
189
|
+
cx: number;
|
|
190
|
+
cy: number;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** 帯 (開口・seg) を境界線分上に配置する。曖昧なら error を返す */
|
|
194
|
+
export function placeBand(
|
|
195
|
+
model: Model,
|
|
196
|
+
b: Boundary,
|
|
197
|
+
band: Band,
|
|
198
|
+
label: string,
|
|
199
|
+
): PlacedBand | { error: string } {
|
|
200
|
+
let segs = segmentsFor(model, b);
|
|
201
|
+
if (band.edge) segs = segs.filter((s) => s.edgeOfA === band.edge);
|
|
202
|
+
if (segs.length === 0) {
|
|
203
|
+
return { error: `${band.line}行目: ${label} を置ける境界線分がありません (${b.a} | ${b.b})` };
|
|
204
|
+
}
|
|
205
|
+
if (segs.length > 1) {
|
|
206
|
+
return {
|
|
207
|
+
error: `${band.line}行目: 境界線分が複数あります。edge:N/E/S/W で辺を指定してください (${b.a} | ${b.b})`,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
const seg = segs[0]!;
|
|
211
|
+
const len = segmentLength(seg);
|
|
212
|
+
if (band.w > len) {
|
|
213
|
+
return {
|
|
214
|
+
error: `${band.line}行目: ${label}の幅 ${band.w} が境界線分の長さ ${len} を超えています`,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
const half = band.w / 2;
|
|
218
|
+
const pos = Math.min(Math.max(band.at * len, half), len - half);
|
|
219
|
+
return {
|
|
220
|
+
segment: seg,
|
|
221
|
+
cx: seg.horizontal ? seg.x1 + pos : seg.x1,
|
|
222
|
+
cy: seg.horizontal ? seg.y1 : seg.y1 + pos,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** 開口を境界線分上に配置する */
|
|
227
|
+
export function placeOpening(model: Model, b: Boundary, o: Opening): PlacedBand | { error: string } {
|
|
228
|
+
return placeBand(model, b, o, o.kind);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* 通行可能か。open境界と階段は扉なしで通れ、wall境界は扉があるときだけ通れる
|
|
233
|
+
* (手すり = 扉のないwall+air:1 なので自動的に通れない)。
|
|
234
|
+
* shaft (EV等) と void (吹抜け) は空間として連続するが人は通れない
|
|
235
|
+
*/
|
|
236
|
+
export function passable(b: Boundary): boolean {
|
|
237
|
+
if (b.kind === "open" || b.kind === "stair") return true;
|
|
238
|
+
if (b.kind === "shaft" || b.kind === "void") return false;
|
|
239
|
+
return b.openings.some((o) => o.kind === "door");
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
/** 通過扉数 (open境界・階段=0, 扉付きwall境界=1) */
|
|
244
|
+
function doorCost(b: Boundary): number {
|
|
245
|
+
return b.kind === "wall" ? 1 : 0;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export interface Route {
|
|
249
|
+
doors: number;
|
|
250
|
+
path: string[];
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** aからbまで扉をいくつ通るか (最小)。到達不能なら undefined */
|
|
254
|
+
export function doorsBetween(model: Model, from: string, to: string): Route | undefined {
|
|
255
|
+
if (!model.spaces.has(from) || !model.spaces.has(to)) return undefined;
|
|
256
|
+
const dist = new Map<string, number>();
|
|
257
|
+
const prev = new Map<string, string>();
|
|
258
|
+
dist.set(from, 0);
|
|
259
|
+
const queue: string[] = [from];
|
|
260
|
+
const visited = new Set<string>();
|
|
261
|
+
while (queue.length) {
|
|
262
|
+
queue.sort((p, q) => (dist.get(p) ?? Infinity) - (dist.get(q) ?? Infinity));
|
|
263
|
+
const u = queue.shift()!;
|
|
264
|
+
if (visited.has(u)) continue;
|
|
265
|
+
visited.add(u);
|
|
266
|
+
for (const b of model.boundaries) {
|
|
267
|
+
if (!passable(b)) continue;
|
|
268
|
+
const v = b.a === u ? b.b : b.b === u ? b.a : undefined;
|
|
269
|
+
if (!v || visited.has(v)) continue;
|
|
270
|
+
const nd = (dist.get(u) ?? Infinity) + doorCost(b);
|
|
271
|
+
if (nd < (dist.get(v) ?? Infinity)) {
|
|
272
|
+
dist.set(v, nd);
|
|
273
|
+
prev.set(v, u);
|
|
274
|
+
queue.push(v);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
if (!dist.has(to)) return undefined;
|
|
279
|
+
const path = [to];
|
|
280
|
+
while (path[0] !== from) path.unshift(prev.get(path[0]!)!);
|
|
281
|
+
return { doors: dist.get(to)!, path };
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export interface NeighborInfo {
|
|
285
|
+
space: Space;
|
|
286
|
+
boundary: Boundary;
|
|
287
|
+
passable: boolean;
|
|
288
|
+
doors: number;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export function neighbors(model: Model, path: string): NeighborInfo[] {
|
|
292
|
+
const out: NeighborInfo[] = [];
|
|
293
|
+
for (const b of model.boundaries) {
|
|
294
|
+
const other = b.a === path ? b.b : b.b === path ? b.a : undefined;
|
|
295
|
+
if (!other) continue;
|
|
296
|
+
const s = model.spaces.get(other);
|
|
297
|
+
if (!s) continue;
|
|
298
|
+
out.push({
|
|
299
|
+
space: s,
|
|
300
|
+
boundary: b,
|
|
301
|
+
passable: passable(b),
|
|
302
|
+
doors: b.openings.filter((o) => o.kind === "door").length,
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
return out;
|
|
306
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// koyu v0 — 公開API
|
|
2
|
+
export * from "./model.js";
|
|
3
|
+
export { parse, tokenize } from "./parse.js";
|
|
4
|
+
export * from "./graph.js";
|
|
5
|
+
export { check, type CheckResult } from "./check.js";
|
|
6
|
+
export { daylight, type DaylightResult } from "./light.js";
|
|
7
|
+
export { siteReport, type SiteReport, type RoadFrontage } from "./site.js";
|
|
8
|
+
export { svgPlan, type PlanOptions } from "./plan.js";
|
package/src/light.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// koyu — 採光の粗い判定 (法規検証の芽・第一号)
|
|
2
|
+
// 住居系の居室について 窓面積/床面積 ≥ 1/7 を確かめる。
|
|
3
|
+
// 採光補正係数は掛けない甘い判定であり、基本計画の解像度に合わせた早期警報である。
|
|
4
|
+
// 対象: unit / room / ldk / bedroom / living (hab:0 で除外、hab:1 で他の型も対象に)
|
|
5
|
+
|
|
6
|
+
import { areaM2, isCoveredAbove, isSemiOutdoor, type Model, type Space } from "./model.js";
|
|
7
|
+
|
|
8
|
+
const HABITABLE = new Set(["unit", "room", "ldk", "bedroom", "living"]);
|
|
9
|
+
|
|
10
|
+
/** 庇下・バルコニー下 (上に空間がある半屋外) 越しの窓の係数 — 縁側補正に倣う粗い値。
|
|
11
|
+
* 上が開いた半屋外 (庭・最上階バルコニー) 越しは 1.0 (ADR-0009) */
|
|
12
|
+
const COVERED_SEMI_FACTOR = 0.7;
|
|
13
|
+
|
|
14
|
+
export interface DaylightResult {
|
|
15
|
+
space: Space;
|
|
16
|
+
/** 床面積 m² */
|
|
17
|
+
floor: number;
|
|
18
|
+
/** 外部に面する窓の面積 m² (w×h) */
|
|
19
|
+
window: number;
|
|
20
|
+
/** 必要面積 m² (床/7) */
|
|
21
|
+
need: number;
|
|
22
|
+
ok: boolean;
|
|
23
|
+
/** h未指定で数えられなかった窓があるか */
|
|
24
|
+
missingH: boolean;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function daylight(model: Model): DaylightResult[] {
|
|
28
|
+
const out: DaylightResult[] = [];
|
|
29
|
+
for (const s of model.spaces.values()) {
|
|
30
|
+
if (s.rects.length === 0) continue;
|
|
31
|
+
const hab = s.attrs["hab"];
|
|
32
|
+
const target = hab === 1 || (HABITABLE.has(s.type) && hab !== 0);
|
|
33
|
+
if (!target) continue;
|
|
34
|
+
const floor = areaM2(s)!;
|
|
35
|
+
let win = 0;
|
|
36
|
+
let missingH = false;
|
|
37
|
+
for (const b of model.boundaries) {
|
|
38
|
+
const other = b.a === s.path ? b.b : b.b === s.path ? b.a : undefined;
|
|
39
|
+
if (!other) continue;
|
|
40
|
+
const os = model.spaces.get(other);
|
|
41
|
+
if (!os) continue;
|
|
42
|
+
// 外部に直接面する窓は係数1。半屋外越しは、その半屋外が庇下 (上に空間がある) なら0.7、
|
|
43
|
+
// 上が開いていれば (庭・最上階バルコニー) 1.0
|
|
44
|
+
const factor =
|
|
45
|
+
os.type === "exterior"
|
|
46
|
+
? 1
|
|
47
|
+
: isSemiOutdoor(model, os)
|
|
48
|
+
? isCoveredAbove(model, os)
|
|
49
|
+
? COVERED_SEMI_FACTOR
|
|
50
|
+
: 1
|
|
51
|
+
: 0;
|
|
52
|
+
if (factor === 0) continue;
|
|
53
|
+
for (const o of b.openings) {
|
|
54
|
+
if (o.kind !== "window") continue;
|
|
55
|
+
if (o.h === undefined) {
|
|
56
|
+
missingH = true;
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
win += (o.w * o.h * factor) / 1e6;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const need = floor / 7;
|
|
63
|
+
out.push({ space: s, floor, window: win, need, ok: win + 1e-9 >= need, missingH });
|
|
64
|
+
}
|
|
65
|
+
return out;
|
|
66
|
+
}
|