@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.
- package/README.ja.md +43 -11
- package/README.md +51 -11
- package/dist/axo.d.ts +16 -0
- package/dist/axo.d.ts.map +1 -0
- package/dist/axo.js +211 -0
- package/dist/axo.js.map +1 -0
- package/dist/check.d.ts +109 -0
- package/dist/check.d.ts.map +1 -0
- package/dist/check.js +957 -0
- package/dist/check.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +410 -0
- package/dist/cli.js.map +1 -0
- package/dist/diff.d.ts +105 -0
- package/dist/diff.d.ts.map +1 -0
- package/dist/diff.js +583 -0
- package/dist/diff.js.map +1 -0
- package/dist/fabric.d.ts +28 -0
- package/dist/fabric.d.ts.map +1 -0
- package/dist/fabric.js +105 -0
- package/dist/fabric.js.map +1 -0
- package/dist/graph.d.ts +118 -0
- package/dist/graph.d.ts.map +1 -0
- package/dist/graph.js +627 -0
- package/dist/graph.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/light.d.ts +15 -0
- package/dist/light.d.ts.map +1 -0
- package/dist/light.js +53 -0
- package/dist/light.js.map +1 -0
- package/dist/mcp.d.ts +3 -0
- package/dist/mcp.d.ts.map +1 -0
- package/dist/mcp.js +361 -0
- package/dist/mcp.js.map +1 -0
- package/dist/model.d.ts +379 -0
- package/dist/model.d.ts.map +1 -0
- package/dist/model.js +464 -0
- package/dist/model.js.map +1 -0
- package/dist/parse-file.d.ts +9 -0
- package/dist/parse-file.d.ts.map +1 -0
- package/dist/parse-file.js +22 -0
- package/dist/parse-file.js.map +1 -0
- package/dist/parse.d.ts +16 -0
- package/dist/parse.d.ts.map +1 -0
- package/dist/parse.js +1040 -0
- package/dist/parse.js.map +1 -0
- package/dist/plan.d.ts +10 -0
- package/dist/plan.d.ts.map +1 -0
- package/dist/plan.js +354 -0
- package/dist/plan.js.map +1 -0
- package/dist/poly.d.ts +78 -0
- package/dist/poly.d.ts.map +1 -0
- package/dist/poly.js +277 -0
- package/dist/poly.js.map +1 -0
- package/dist/site.d.ts +24 -0
- package/dist/site.d.ts.map +1 -0
- package/dist/site.js +54 -0
- package/dist/site.js.map +1 -0
- package/dist/vertical.d.ts +176 -0
- package/dist/vertical.d.ts.map +1 -0
- package/dist/vertical.js +669 -0
- package/dist/vertical.js.map +1 -0
- package/examples/basement/main.muro +84 -0
- package/examples/comparison/README.md +64 -1
- package/examples/comparison/gen-ifc4.ts +1 -1
- package/examples/comparison/gen-ifcx.ts +2 -2
- package/examples/comparison/two-rooms.ifcx +1 -1
- package/examples/complex/L1.muro +132 -0
- package/examples/complex/assets.muro +17 -0
- package/examples/complex/basement.muro +40 -0
- package/examples/complex/core.muro +40 -0
- package/examples/complex/hotel.muro +117 -0
- package/examples/complex/main.muro +67 -0
- package/examples/complex/office.muro +41 -0
- package/examples/complex/plant.muro +34 -0
- package/examples/complex/podium.muro +140 -0
- package/examples/complex/site.muro +18 -0
- package/examples/house/L1.muro +16 -0
- package/examples/house/L2.muro +19 -0
- package/examples/house/assets.muro +9 -0
- package/examples/house/main.muro +27 -0
- package/examples/house/site.muro +31 -0
- package/examples/house.muro +3 -3
- package/examples/mansion.muro +18 -13
- package/examples/office.muro +1 -1
- package/examples/steps/01-one-room.muro +4 -0
- package/examples/steps/02-two-rooms.muro +5 -0
- package/examples/steps/03-door.muro +9 -0
- package/examples/steps/04-exterior.muro +15 -0
- package/examples/steps/05-two-storeys.muro +27 -0
- package/examples/steps/06-finished.muro +30 -0
- package/examples/steps/README.md +25 -0
- package/examples/tower/L1.muro +80 -0
- package/examples/tower/L11.muro +49 -0
- package/examples/tower/L2.muro +71 -0
- package/examples/tower/L3.muro +31 -0
- package/examples/tower/assets.muro +14 -0
- package/examples/tower/main.muro +37 -0
- package/examples/tower/site-geometry.muro +7 -0
- package/examples/tower/site.muro +33 -0
- package/examples/tower/typical.muro +131 -0
- package/examples/two-rooms.canonical.json +30 -3
- package/examples/two-rooms.muro +11 -7
- package/package.json +29 -2
- package/src/axo.ts +258 -0
- package/src/check.ts +948 -122
- package/src/cli.ts +175 -12
- package/src/diff.ts +676 -0
- package/src/fabric.ts +123 -0
- package/src/graph.ts +451 -61
- package/src/index.ts +38 -2
- package/src/light.ts +4 -7
- package/src/mcp.ts +397 -0
- package/src/model.ts +462 -67
- package/src/parse-file.ts +28 -0
- package/src/parse.ts +682 -45
- package/src/plan.ts +152 -14
- package/src/poly.ts +295 -0
- package/src/site.ts +19 -11
- package/src/vertical.ts +884 -0
- package/.github/workflows/ci.yml +0 -16
- package/.zenodo.json +0 -20
- package/CITATION.cff +0 -26
- package/docs/decisions/0001-notation-v0.md +0 -29
- package/docs/decisions/0002-height-and-offsets.md +0 -27
- package/docs/decisions/0003-uncounted-divisions.md +0 -36
- package/docs/decisions/0004-typical-floors.md +0 -21
- package/docs/decisions/0005-zones-and-unions.md +0 -23
- package/docs/decisions/0006-voids-and-light.md +0 -31
- package/docs/decisions/0007-semi-outdoor-air.md +0 -27
- package/docs/decisions/0008-vocabulary-and-level-attr.md +0 -23
- package/docs/decisions/0009-site-and-exterior.md +0 -29
- package/docs/ifc-coverage.md +0 -82
- package/docs/ifcx-notes.md +0 -25
- package/docs/img/house-L1.svg +0 -80
- package/docs/img/house-L2.svg +0 -53
- package/docs/img/mansion-L1.svg +0 -113
- package/docs/img/mansion-L10.svg +0 -111
- package/docs/img/mansion-L5.svg +0 -160
- package/docs/img/office-L1.svg +0 -116
- package/docs/img/office-L2.svg +0 -109
- package/docs/img/two-rooms.svg +0 -39
- package/docs/log/2026-07-22.md +0 -13
- package/docs/log/2026-07-23.md +0 -51
- package/docs/roadmap.md +0 -55
- package/docs/writing-architecture.md +0 -119
- package/spec/notation-v0.md +0 -164
- package/spec/vocabulary.md +0 -62
- package/test/core.test.ts +0 -171
- package/test/design2.test.ts +0 -97
- package/test/house.test.ts +0 -74
- package/test/mansion.test.ts +0 -99
- package/test/office.test.ts +0 -71
- package/test/uncounted.test.ts +0 -120
- package/test/vertical.test.ts +0 -75
- package/tsconfig.json +0 -13
package/dist/graph.js
ADDED
|
@@ -0,0 +1,627 @@
|
|
|
1
|
+
// koyu — 空間グラフ
|
|
2
|
+
// 節点が空間、辺が境界。「この室とこの室は繋がっているか」「ここから外へ扉いくつか」が
|
|
3
|
+
// 変換なしにそのままグラフへの問いになる。
|
|
4
|
+
// 空間の領域は矩形の合併 (L字など)。壁は合併の外周と共有辺から導出される。
|
|
5
|
+
import { rectToPoly, srcRef } from "./model.js";
|
|
6
|
+
import * as poly from "./poly.js";
|
|
7
|
+
const EPS = 0.5;
|
|
8
|
+
/** 二つの矩形が共有する辺の線分 (触れていなければ undefined) */
|
|
9
|
+
export function sharedSegment(a, b) {
|
|
10
|
+
for (const [x, ea] of [
|
|
11
|
+
[a.x2, "E"],
|
|
12
|
+
[a.x1, "W"],
|
|
13
|
+
]) {
|
|
14
|
+
const bx = ea === "E" ? b.x1 : b.x2;
|
|
15
|
+
if (Math.abs(x - bx) < EPS) {
|
|
16
|
+
const y1 = Math.max(a.y1, b.y1);
|
|
17
|
+
const y2 = Math.min(a.y2, b.y2);
|
|
18
|
+
if (y2 - y1 > EPS)
|
|
19
|
+
return { x1: x, y1, x2: x, y2, horizontal: false, edgeOfA: ea };
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
for (const [y, ea] of [
|
|
23
|
+
[a.y2, "N"],
|
|
24
|
+
[a.y1, "S"],
|
|
25
|
+
]) {
|
|
26
|
+
const by = ea === "N" ? b.y1 : b.y2;
|
|
27
|
+
if (Math.abs(y - by) < EPS) {
|
|
28
|
+
const x1 = Math.max(a.x1, b.x1);
|
|
29
|
+
const x2 = Math.min(a.x2, b.x2);
|
|
30
|
+
if (x2 - x1 > EPS)
|
|
31
|
+
return { x1, y1: y, x2, y2: y, horizontal: true, edgeOfA: ea };
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* 凸片の軸平行な辺 (向きから N/E/S/W を読む)。頂点列は反時計回りなので、
|
|
38
|
+
* +x へ進む辺が南、+y が東、-x が北、-y が西の面になる。
|
|
39
|
+
* 斜めの辺は返さない — それは描かれた線であり、自分の境界が実現を持っている
|
|
40
|
+
*/
|
|
41
|
+
function polyEdges(poly) {
|
|
42
|
+
const out = [];
|
|
43
|
+
for (let i = 0; i < poly.length; i++) {
|
|
44
|
+
const p = poly[i];
|
|
45
|
+
const q = poly[(i + 1) % poly.length];
|
|
46
|
+
const dx = q.x - p.x;
|
|
47
|
+
const dy = q.y - p.y;
|
|
48
|
+
if (Math.abs(dy) < EPS && Math.abs(dx) > EPS) {
|
|
49
|
+
out.push({
|
|
50
|
+
edge: dx > 0 ? "S" : "N",
|
|
51
|
+
fixed: p.y,
|
|
52
|
+
lo: Math.min(p.x, q.x),
|
|
53
|
+
hi: Math.max(p.x, q.x),
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
else if (Math.abs(dx) < EPS && Math.abs(dy) > EPS) {
|
|
57
|
+
out.push({
|
|
58
|
+
edge: dy > 0 ? "E" : "W",
|
|
59
|
+
fixed: p.x,
|
|
60
|
+
lo: Math.min(p.y, q.y),
|
|
61
|
+
hi: Math.max(p.y, q.y),
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
67
|
+
const FACING = { N: "S", S: "N", E: "W", W: "E" };
|
|
68
|
+
/** 凸片の外周のうち、他の空間の凸片と向かい合っていない区間 (= 外部に面する壁) */
|
|
69
|
+
function pieceOutline(pieces, others) {
|
|
70
|
+
const otherEdges = others.flatMap(polyEdges);
|
|
71
|
+
const segs = [];
|
|
72
|
+
for (let i = 0; i < pieces.length; i++) {
|
|
73
|
+
const siblings = pieces.filter((_, k) => k !== i).flatMap(polyEdges);
|
|
74
|
+
for (const e of polyEdges(pieces[i])) {
|
|
75
|
+
let intervals = [[e.lo, e.hi]];
|
|
76
|
+
for (const o of [...otherEdges, ...siblings]) {
|
|
77
|
+
if (o.edge !== FACING[e.edge])
|
|
78
|
+
continue;
|
|
79
|
+
if (Math.abs(o.fixed - e.fixed) > EPS)
|
|
80
|
+
continue;
|
|
81
|
+
intervals = intervals.flatMap(([s, t]) => {
|
|
82
|
+
const cs = Math.max(s, o.lo);
|
|
83
|
+
const ce = Math.min(t, o.hi);
|
|
84
|
+
if (ce - cs <= EPS)
|
|
85
|
+
return [[s, t]];
|
|
86
|
+
const out = [];
|
|
87
|
+
if (cs - s > EPS)
|
|
88
|
+
out.push([s, cs]);
|
|
89
|
+
if (t - ce > EPS)
|
|
90
|
+
out.push([ce, t]);
|
|
91
|
+
return out;
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
const horizontal = e.edge === "N" || e.edge === "S";
|
|
95
|
+
for (const [s, t] of intervals) {
|
|
96
|
+
segs.push(horizontal
|
|
97
|
+
? { x1: s, y1: e.fixed, x2: t, y2: e.fixed, horizontal: true, edgeOfA: e.edge }
|
|
98
|
+
: { x1: e.fixed, y1: s, x2: e.fixed, y2: t, horizontal: false, edgeOfA: e.edge });
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return segs;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* 二つの領域 (凸片の集合) が共有する軸平行な辺。
|
|
106
|
+
* `sharedSegment` の矩形版を凸片へ広げたもので、切られた形にも正しい。
|
|
107
|
+
* 斜めの辺は返さない — それは描かれた線であり、自分の境界が実現を持っている
|
|
108
|
+
*/
|
|
109
|
+
function sharedFromPieces(A, B) {
|
|
110
|
+
const eb = B.flatMap(polyEdges);
|
|
111
|
+
const out = [];
|
|
112
|
+
for (const ea of A.flatMap(polyEdges)) {
|
|
113
|
+
for (const o of eb) {
|
|
114
|
+
if (o.edge !== FACING[ea.edge])
|
|
115
|
+
continue;
|
|
116
|
+
if (Math.abs(o.fixed - ea.fixed) > EPS)
|
|
117
|
+
continue;
|
|
118
|
+
const lo = Math.max(ea.lo, o.lo);
|
|
119
|
+
const hi = Math.min(ea.hi, o.hi);
|
|
120
|
+
if (hi - lo <= EPS)
|
|
121
|
+
continue;
|
|
122
|
+
out.push(ea.edge === "N" || ea.edge === "S"
|
|
123
|
+
? { x1: lo, y1: ea.fixed, x2: hi, y2: ea.fixed, horizontal: true, edgeOfA: ea.edge }
|
|
124
|
+
: { x1: ea.fixed, y1: lo, x2: ea.fixed, y2: hi, horizontal: false, edgeOfA: ea.edge });
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return out;
|
|
128
|
+
}
|
|
129
|
+
/** 共線で連続する線分をまとめる (L字の合併外周を一本の壁にする) */
|
|
130
|
+
export function mergeCollinear(segs) {
|
|
131
|
+
const groups = new Map();
|
|
132
|
+
for (const s of segs) {
|
|
133
|
+
const key = `${s.horizontal ? "h" : "v"}:${s.horizontal ? s.y1 : s.x1}:${s.edgeOfA ?? ""}`;
|
|
134
|
+
const g = groups.get(key) ?? [];
|
|
135
|
+
g.push(s);
|
|
136
|
+
groups.set(key, g);
|
|
137
|
+
}
|
|
138
|
+
const out = [];
|
|
139
|
+
for (const g of groups.values()) {
|
|
140
|
+
g.sort((p, q) => (p.horizontal ? p.x1 - q.x1 : p.y1 - q.y1));
|
|
141
|
+
let cur = { ...g[0] };
|
|
142
|
+
for (let i = 1; i < g.length; i++) {
|
|
143
|
+
const s = g[i];
|
|
144
|
+
const curEnd = cur.horizontal ? cur.x2 : cur.y2;
|
|
145
|
+
const sStart = s.horizontal ? s.x1 : s.y1;
|
|
146
|
+
if (sStart <= curEnd + EPS) {
|
|
147
|
+
if (cur.horizontal)
|
|
148
|
+
cur.x2 = Math.max(cur.x2, s.x2);
|
|
149
|
+
else
|
|
150
|
+
cur.y2 = Math.max(cur.y2, s.y2);
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
out.push(cur);
|
|
154
|
+
cur = { ...s };
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
out.push(cur);
|
|
158
|
+
}
|
|
159
|
+
return out;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* 既定境界の導出 (ADR-0014) — 垂直の「既定は床」と対称の、水平の「既定は壁」。
|
|
163
|
+
* 同一レベル (未特定同士を含む) で平面が接する領域つき空間の組に、宣言境界がその組に
|
|
164
|
+
* 一つも無ければ kind:wall の既定境界を導く。宣言は例外 (open・手すり) と属性のためにある。
|
|
165
|
+
* 領域を持たない空間 (exterior等) との境界は導かない — 相手の名指しが情報のため宣言する。
|
|
166
|
+
* 合成・スパン展開の完了後に呼ぶ (parse / parseWith の出口)。冪等。
|
|
167
|
+
*/
|
|
168
|
+
export function deriveDefaultBoundaries(model) {
|
|
169
|
+
const declared = new Set();
|
|
170
|
+
for (const b of model.boundaries)
|
|
171
|
+
declared.add([b.a, b.b].sort().join("|"));
|
|
172
|
+
const withRect = [...model.spaces.values()].filter((s) => s.rects.length > 0);
|
|
173
|
+
for (let i = 0; i < withRect.length; i++) {
|
|
174
|
+
for (let j = i + 1; j < withRect.length; j++) {
|
|
175
|
+
const a = withRect[i];
|
|
176
|
+
const b = withRect[j];
|
|
177
|
+
if (a.level !== b.level)
|
|
178
|
+
continue;
|
|
179
|
+
const key = [a.path, b.path].sort().join("|");
|
|
180
|
+
if (declared.has(key))
|
|
181
|
+
continue;
|
|
182
|
+
// 接触は**導出された形**で見る — 線で接触が消えた組に既定の壁を作らない
|
|
183
|
+
if (sharedFromPieces(piecesOf(a), piecesOf(b)).length === 0)
|
|
184
|
+
continue;
|
|
185
|
+
model.boundaries.push({
|
|
186
|
+
a: a.path,
|
|
187
|
+
b: b.path,
|
|
188
|
+
kind: "wall",
|
|
189
|
+
derived: true,
|
|
190
|
+
attrs: {},
|
|
191
|
+
openings: [],
|
|
192
|
+
segs: [],
|
|
193
|
+
line: 0,
|
|
194
|
+
});
|
|
195
|
+
declared.add(key);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* 描かれた線による領域の切り分け (ADR-0022)。
|
|
201
|
+
*
|
|
202
|
+
* 空間の `rects` は**書かれた割付** (セル) であって形ではない。形は凸片 `pieces` として
|
|
203
|
+
* ここで導かれる — 既定は矩形そのまま、境界に線が描かれていればその半平面で切り直す。
|
|
204
|
+
* 切り直しは「二空間の割付の合併を、線の両側へ分け直す」操作なので、
|
|
205
|
+
* 一方が失う三角形をもう一方が得る (合計面積は保存される)。
|
|
206
|
+
*
|
|
207
|
+
* 線の及ぶ範囲は**書かれた区間の外接矩形**に限る。無限直線として全体を切ると、
|
|
208
|
+
* 離れた翼まで巻き添えにするため。冪等ではないので parse の出口で一度だけ呼ぶ。
|
|
209
|
+
*/
|
|
210
|
+
export function derivePieces(model) {
|
|
211
|
+
for (const s of model.spaces.values())
|
|
212
|
+
s.pieces = s.rects.map(rectToPoly);
|
|
213
|
+
// 線は宣言順に効く。derivePieces は parse の出口で一度だけ呼ぶ (冪等ではない)
|
|
214
|
+
for (const b of model.boundaries) {
|
|
215
|
+
if (!b.drawn)
|
|
216
|
+
continue;
|
|
217
|
+
const sa = model.spaces.get(b.a);
|
|
218
|
+
const sb = model.spaces.get(b.b);
|
|
219
|
+
if (!sa || !sb)
|
|
220
|
+
continue;
|
|
221
|
+
const cut = drawnCut(sa, sb, b.drawn.a, b.drawn.b);
|
|
222
|
+
if (!cut)
|
|
223
|
+
continue;
|
|
224
|
+
if (cut.solo) {
|
|
225
|
+
// 外皮を切る線 — 持つ側だけが窓の中で自分の側へ切り落とされる。相手は面積を得ない
|
|
226
|
+
const s = cut.solo;
|
|
227
|
+
const { inside, outside } = poly.splitByRect(s.pieces, cut.window);
|
|
228
|
+
s.pieces = [
|
|
229
|
+
...outside,
|
|
230
|
+
...inside.map((p) => poly.clipHalf(p, b.drawn.a, b.drawn.b, cut.sideA > 0)).filter((p) => p.length > 0),
|
|
231
|
+
];
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
// 二空間の分け直し — 窓の中の割付を合併し、線の両側へ分け直す (合計面積は保存される)
|
|
235
|
+
const splitA = poly.splitByRect(sa.pieces, cut.window);
|
|
236
|
+
const splitB = poly.splitByRect(sb.pieces, cut.window);
|
|
237
|
+
const pool = [...splitA.inside, ...splitB.inside];
|
|
238
|
+
if (pool.length === 0)
|
|
239
|
+
continue;
|
|
240
|
+
sa.pieces = [
|
|
241
|
+
...splitA.outside,
|
|
242
|
+
...pool.map((p) => poly.clipHalf(p, b.drawn.a, b.drawn.b, cut.sideA > 0)).filter((p) => p.length > 0),
|
|
243
|
+
];
|
|
244
|
+
sb.pieces = [
|
|
245
|
+
...splitB.outside,
|
|
246
|
+
...pool.map((p) => poly.clipHalf(p, b.drawn.a, b.drawn.b, cut.sideA < 0)).filter((p) => p.length > 0),
|
|
247
|
+
];
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* 描かれた線の切り方を一度に決める — 窓・残す側・片側かどうか (ADR-0022 / ADR-0027)。
|
|
252
|
+
*
|
|
253
|
+
* **窓と側は必ず一緒に決める。**別々に決めていたために、側は空間の全割付を無限直線で
|
|
254
|
+
* 測るのに切るのは線の近傍だけ、という母集団のずれが生まれ、線から遠い翼が符号を
|
|
255
|
+
* 支配して、残すつもりだった側が黙って切り落とされた (check は緑のまま)。
|
|
256
|
+
* check の LIN01/LIN03 もこの同じ関数を通す — 判定と操作が食い違わないために。
|
|
257
|
+
*/
|
|
258
|
+
export function drawnCut(sa, sb, a, b) {
|
|
259
|
+
const ba = poly.boundsOf(sa.pieces);
|
|
260
|
+
const bb = poly.boundsOf(sb.pieces);
|
|
261
|
+
// 片側が領域を持たない (外部) — 外皮を切る線
|
|
262
|
+
if (!ba || !bb) {
|
|
263
|
+
const solo = ba ? sa : bb ? sb : undefined;
|
|
264
|
+
const sb2 = ba ?? bb;
|
|
265
|
+
if (!solo || !sb2)
|
|
266
|
+
return undefined;
|
|
267
|
+
const w = poly.lineWindow(a, b, sb2);
|
|
268
|
+
if (!poly.validWindow(w))
|
|
269
|
+
return undefined;
|
|
270
|
+
const side = poly.sideOfTouching(solo.pieces, w, a, b);
|
|
271
|
+
if (side === 0)
|
|
272
|
+
return undefined; // 窓の中でちょうど二等分 — 残す側が決まらない (LIN01)
|
|
273
|
+
return { window: w, sideA: solo === sa ? side : -side, solo };
|
|
274
|
+
}
|
|
275
|
+
const w = poly.lineWindow(a, b, ba, bb);
|
|
276
|
+
if (!poly.validWindow(w))
|
|
277
|
+
return undefined;
|
|
278
|
+
let ia = poly.sideOfTouching(sa.pieces, w, a, b);
|
|
279
|
+
let ib = poly.sideOfTouching(sb.pieces, w, a, b);
|
|
280
|
+
if (ia === 0 && ib === 0)
|
|
281
|
+
return undefined; // どちらも偏りなし — 分離が決まらない (LIN01)
|
|
282
|
+
if (ia === 0)
|
|
283
|
+
ia = -ib;
|
|
284
|
+
if (ib === 0)
|
|
285
|
+
ib = -ia;
|
|
286
|
+
if (ia === ib)
|
|
287
|
+
return undefined; // 同じ側 — 分離していない (LIN01)
|
|
288
|
+
return { window: w, sideA: ia };
|
|
289
|
+
}
|
|
290
|
+
/** 境界の壁芯線分を導く。壁の位置は空間の割付から生成される — 壁を置く操作は存在しない */
|
|
291
|
+
export function segmentsFor(model, b) {
|
|
292
|
+
const sa = model.spaces.get(b.a);
|
|
293
|
+
const sb = model.spaces.get(b.b);
|
|
294
|
+
if (!sa || !sb)
|
|
295
|
+
return [];
|
|
296
|
+
// 垂直境界 (stair/shaft/void/暗黙のslab) は壁線分を持たない
|
|
297
|
+
if (b.kind === "stair" || b.kind === "shaft" || b.kind === "void")
|
|
298
|
+
return [];
|
|
299
|
+
// 描かれた線 (ADR-0022): 隣接からの導出ではなく、書かれた線がそのまま境界の実現になる。
|
|
300
|
+
// ただし**一本の設計線は複数の境界に共有されうる** — 貫通通路の壁は、その前を通る
|
|
301
|
+
// 区画の数だけ境界を持つ。この境界が実現するのは、線の両側がちょうど a と b に
|
|
302
|
+
// なっている区間だけであり、線の全長ではない
|
|
303
|
+
if (b.drawn)
|
|
304
|
+
return drawnShare(model, sa, sb, b.drawn.a, b.drawn.b);
|
|
305
|
+
let segs = [];
|
|
306
|
+
const aHas = sa.rects.length > 0;
|
|
307
|
+
const bHas = sb.rects.length > 0;
|
|
308
|
+
if (aHas && bHas) {
|
|
309
|
+
if (sa.level !== sb.level)
|
|
310
|
+
return []; // 異なるレベル間に壁は立たない
|
|
311
|
+
// 共有辺も**導出された領域** (pieces) から取る。割付から取ると、
|
|
312
|
+
// 描かれた線で切り落とした側にまで壁が立つ (隅切りの外へ壁が飛び出す)
|
|
313
|
+
segs.push(...sharedFromPieces(piecesOf(sa), piecesOf(sb)));
|
|
314
|
+
}
|
|
315
|
+
else if (aHas || bHas) {
|
|
316
|
+
// 片側が領域を持たない (外部など): 導出された領域の外周から、
|
|
317
|
+
// 同レベルで向かい合う他室の区間を除いた残り。**割付ではなく形の縁を辿る** —
|
|
318
|
+
// 描かれた線で切り落とされた側には壁が立たない (ADR-0022)
|
|
319
|
+
const roomSpace = aHas ? sa : sb;
|
|
320
|
+
const others = [];
|
|
321
|
+
for (const s of model.spaces.values()) {
|
|
322
|
+
if (s === sa || s === sb)
|
|
323
|
+
continue;
|
|
324
|
+
if (s.level !== roomSpace.level)
|
|
325
|
+
continue;
|
|
326
|
+
others.push(...piecesOf(s));
|
|
327
|
+
}
|
|
328
|
+
segs.push(...pieceOutline(piecesOf(roomSpace), others));
|
|
329
|
+
}
|
|
330
|
+
segs = mergeCollinear(segs);
|
|
331
|
+
if (b.edge)
|
|
332
|
+
segs = segs.filter((s) => s.edgeOfA === b.edge);
|
|
333
|
+
return segs;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* 空間の外周のうち、**何にも面していない**区間 (ADR-0025)。
|
|
337
|
+
* 他の空間とも、宣言された外部境界とも向かい合っていない縁 — 外皮の穴である。
|
|
338
|
+
* 既定境界 (ADR-0014) は領域を持たない空間との間には導かれないので、
|
|
339
|
+
* 外部への境界の書き忘れは黙って壁の不在になる。これを言葉にするための導出。
|
|
340
|
+
*/
|
|
341
|
+
export function envelopeGaps(model, s) {
|
|
342
|
+
if (s.rects.length === 0 || !s.level)
|
|
343
|
+
return [];
|
|
344
|
+
const others = [];
|
|
345
|
+
for (const o of model.spaces.values()) {
|
|
346
|
+
if (o === s || o.level !== s.level)
|
|
347
|
+
continue;
|
|
348
|
+
others.push(...piecesOf(o));
|
|
349
|
+
}
|
|
350
|
+
let gaps = pieceOutline(piecesOf(s), others);
|
|
351
|
+
// 宣言された境界 (外部・斜めを含む) が覆う区間を引く
|
|
352
|
+
for (const b of model.boundaries) {
|
|
353
|
+
if (b.a !== s.path && b.b !== s.path)
|
|
354
|
+
continue;
|
|
355
|
+
if (b.kind === "stair" || b.kind === "shaft" || b.kind === "void")
|
|
356
|
+
continue;
|
|
357
|
+
for (const seg of segmentsFor(model, b)) {
|
|
358
|
+
gaps = gaps.flatMap((g) => subtractOverlap(g, seg));
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
return gaps.filter((g) => segmentLength(g) > 1);
|
|
362
|
+
}
|
|
363
|
+
/** 軸平行の線分から、同一直線上で重なる区間を引く */
|
|
364
|
+
function subtractOverlap(g, o) {
|
|
365
|
+
if (o.diagonal || g.horizontal !== o.horizontal)
|
|
366
|
+
return [g];
|
|
367
|
+
const fixedG = g.horizontal ? g.y1 : g.x1;
|
|
368
|
+
const fixedO = o.horizontal ? o.y1 : o.x1;
|
|
369
|
+
if (Math.abs(fixedG - fixedO) > EPS)
|
|
370
|
+
return [g];
|
|
371
|
+
const [gl, gh] = g.horizontal ? [g.x1, g.x2] : [g.y1, g.y2];
|
|
372
|
+
const [ol, oh] = o.horizontal
|
|
373
|
+
? [Math.min(o.x1, o.x2), Math.max(o.x1, o.x2)]
|
|
374
|
+
: [Math.min(o.y1, o.y2), Math.max(o.y1, o.y2)];
|
|
375
|
+
const cs = Math.max(gl, ol);
|
|
376
|
+
const ce = Math.min(gh, oh);
|
|
377
|
+
if (ce - cs <= EPS)
|
|
378
|
+
return [g];
|
|
379
|
+
const parts = [];
|
|
380
|
+
if (cs - gl > EPS)
|
|
381
|
+
parts.push([gl, cs]);
|
|
382
|
+
if (gh - ce > EPS)
|
|
383
|
+
parts.push([ce, gh]);
|
|
384
|
+
return parts.map(([a, b]) => g.horizontal
|
|
385
|
+
? { ...g, x1: a, x2: b }
|
|
386
|
+
: { ...g, y1: a, y2: b });
|
|
387
|
+
}
|
|
388
|
+
function piecesOf(s) {
|
|
389
|
+
return s.pieces.length > 0 ? s.pieces : s.rects.map(rectToPoly);
|
|
390
|
+
}
|
|
391
|
+
/** 線の左右へ少し離れた点。どちらの空間に属するかを見るための探り */
|
|
392
|
+
const PROBE = 5;
|
|
393
|
+
/**
|
|
394
|
+
* 描かれた線のうち、この二空間が実際に向かい合っている区間だけを返す (ADR-0022)。
|
|
395
|
+
*
|
|
396
|
+
* 一本の設計線は複数の境界に共有される — 貫通通路の壁は、その前を通る区画の数だけ
|
|
397
|
+
* 境界を持つ。各境界が線の全長を実現すると、平面には同じ壁が何本も重なって現れる。
|
|
398
|
+
* 線を両空間の縁で切り、左右がちょうど a と b になっている区間だけを残す。
|
|
399
|
+
*/
|
|
400
|
+
function drawnShare(model, sa, sb, p, q) {
|
|
401
|
+
const dx = q.x - p.x;
|
|
402
|
+
const dy = q.y - p.y;
|
|
403
|
+
const len = Math.hypot(dx, dy);
|
|
404
|
+
if (len < EPS)
|
|
405
|
+
return [];
|
|
406
|
+
const nx = -dy / len;
|
|
407
|
+
const ny = dx / len;
|
|
408
|
+
const A = piecesOf(sa);
|
|
409
|
+
const B = piecesOf(sb);
|
|
410
|
+
// 切り位置: 両空間の凸片の辺と線の交点
|
|
411
|
+
const cuts = new Set([0, 1]);
|
|
412
|
+
for (const poly of [...A, ...B]) {
|
|
413
|
+
for (let i = 0; i < poly.length; i++) {
|
|
414
|
+
const t = lineHit(p, q, poly[i], poly[(i + 1) % poly.length]);
|
|
415
|
+
if (t !== undefined && t > 1e-9 && t < 1 - 1e-9)
|
|
416
|
+
cuts.add(t);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
const ts = [...cuts].sort((x, y) => x - y);
|
|
420
|
+
const kept = [];
|
|
421
|
+
for (let i = 0; i + 1 < ts.length; i++) {
|
|
422
|
+
const m = (ts[i] + ts[i + 1]) / 2;
|
|
423
|
+
const cx = p.x + m * dx;
|
|
424
|
+
const cy = p.y + m * dy;
|
|
425
|
+
const left = { x: cx + nx * PROBE, y: cy + ny * PROBE };
|
|
426
|
+
const right = { x: cx - nx * PROBE, y: cy - ny * PROBE };
|
|
427
|
+
const inA = (pt) => A.some((g) => poly.pointIn(pt, g, 0));
|
|
428
|
+
const inB = (pt) => B.some((g) => poly.pointIn(pt, g, 0));
|
|
429
|
+
// 片側が領域を持たない (外部) 相手なら、持つ側が片側に居るだけで境界になる
|
|
430
|
+
const soloB = sb.rects.length === 0;
|
|
431
|
+
const soloA = sa.rects.length === 0;
|
|
432
|
+
const ok = soloB
|
|
433
|
+
? inA(left) !== inA(right)
|
|
434
|
+
: soloA
|
|
435
|
+
? inB(left) !== inB(right)
|
|
436
|
+
: (inA(left) && inB(right)) || (inB(left) && inA(right));
|
|
437
|
+
if (ok)
|
|
438
|
+
kept.push([ts[i], ts[i + 1]]);
|
|
439
|
+
}
|
|
440
|
+
// 連続する区間をまとめる
|
|
441
|
+
const out = [];
|
|
442
|
+
for (const [a0, a1] of kept) {
|
|
443
|
+
const last = out[out.length - 1];
|
|
444
|
+
const start = { x: p.x + a0 * dx, y: p.y + a0 * dy };
|
|
445
|
+
const end = { x: p.x + a1 * dx, y: p.y + a1 * dy };
|
|
446
|
+
if (last && Math.hypot(last.x2 - start.x, last.y2 - start.y) < EPS) {
|
|
447
|
+
last.x2 = end.x;
|
|
448
|
+
last.y2 = end.y;
|
|
449
|
+
continue;
|
|
450
|
+
}
|
|
451
|
+
const horizontal = Math.abs(dy) < 0.5;
|
|
452
|
+
const vertical = Math.abs(dx) < 0.5;
|
|
453
|
+
out.push({
|
|
454
|
+
x1: start.x,
|
|
455
|
+
y1: start.y,
|
|
456
|
+
x2: end.x,
|
|
457
|
+
y2: end.y,
|
|
458
|
+
horizontal,
|
|
459
|
+
...(horizontal || vertical ? {} : { diagonal: true }),
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
return out;
|
|
463
|
+
}
|
|
464
|
+
/** 無限直線 p→q と線分 u→v の交点の、線側のパラメータ t (平行・範囲外は undefined) */
|
|
465
|
+
function lineHit(p, q, u, v) {
|
|
466
|
+
const rx = q.x - p.x;
|
|
467
|
+
const ry = q.y - p.y;
|
|
468
|
+
const sx = v.x - u.x;
|
|
469
|
+
const sy = v.y - u.y;
|
|
470
|
+
const d = rx * sy - ry * sx;
|
|
471
|
+
if (Math.abs(d) < 1e-9)
|
|
472
|
+
return undefined;
|
|
473
|
+
const t = ((u.x - p.x) * sy - (u.y - p.y) * sx) / d;
|
|
474
|
+
const w = ((u.x - p.x) * ry - (u.y - p.y) * rx) / d;
|
|
475
|
+
return w >= -1e-9 && w <= 1 + 1e-9 ? t : undefined;
|
|
476
|
+
}
|
|
477
|
+
export function segmentLength(s) {
|
|
478
|
+
return Math.hypot(s.x2 - s.x1, s.y2 - s.y1);
|
|
479
|
+
}
|
|
480
|
+
/** 平面上の重なり (垂直隣接の導出に使う)。重ならなければ undefined */
|
|
481
|
+
export function planOverlap(a, b) {
|
|
482
|
+
const x1 = Math.max(a.x1, b.x1);
|
|
483
|
+
const x2 = Math.min(a.x2, b.x2);
|
|
484
|
+
const y1 = Math.max(a.y1, b.y1);
|
|
485
|
+
const y2 = Math.min(a.y2, b.y2);
|
|
486
|
+
if (x2 - x1 > EPS && y2 - y1 > EPS)
|
|
487
|
+
return { x1, y1, x2, y2 };
|
|
488
|
+
return undefined;
|
|
489
|
+
}
|
|
490
|
+
/** 二つの空間が平面上で重なるか (合併同士) */
|
|
491
|
+
export function spacesOverlap(a, b) {
|
|
492
|
+
for (const ra of a.rects) {
|
|
493
|
+
for (const rb of b.rects) {
|
|
494
|
+
if (planOverlap(ra, rb))
|
|
495
|
+
return true;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
return false;
|
|
499
|
+
}
|
|
500
|
+
/** 帯 (開口・seg) を境界線分上に配置する。曖昧なら error を返す */
|
|
501
|
+
export function placeBand(model, b, band, label) {
|
|
502
|
+
// 配置失敗の診断: label が "seg" なら SEG系、開口 (door/window等) なら OPN系のコード
|
|
503
|
+
const fail = (n, message) => ({
|
|
504
|
+
error: `${srcRef(band.line, b.file)}: ${message}`,
|
|
505
|
+
code: (label === "seg" ? `SEG${n}` : `OPN${n}`),
|
|
506
|
+
line: band.line,
|
|
507
|
+
...(b.file !== undefined ? { file: b.file } : {}),
|
|
508
|
+
message,
|
|
509
|
+
});
|
|
510
|
+
let segs = segmentsFor(model, b);
|
|
511
|
+
if (band.edge)
|
|
512
|
+
segs = segs.filter((s) => s.edgeOfA === band.edge);
|
|
513
|
+
if (segs.length === 0) {
|
|
514
|
+
return fail("04", `${label} を置ける境界線分がありません (${b.a} | ${b.b})`);
|
|
515
|
+
}
|
|
516
|
+
if (segs.length > 1) {
|
|
517
|
+
return fail("05", `境界線分が複数あります。edge:N/E/S/W で辺を指定してください (${b.a} | ${b.b})`);
|
|
518
|
+
}
|
|
519
|
+
const seg = segs[0];
|
|
520
|
+
const len = segmentLength(seg);
|
|
521
|
+
if (band.w > len) {
|
|
522
|
+
return fail("06", `${label}の幅 ${band.w} が境界線分の長さ ${len} を超えています`);
|
|
523
|
+
}
|
|
524
|
+
const half = band.w / 2;
|
|
525
|
+
let pos;
|
|
526
|
+
if (band.atAbs !== undefined) {
|
|
527
|
+
// 斜めの線分 (描かれた線) の上では、通り参照は一意に位置を定めない — 比率で書く
|
|
528
|
+
if (seg.diagonal) {
|
|
529
|
+
return fail("07", `${label} の位置 ${band.atRef} は斜めの線分では使えません (at:0..1 の比率で書きます)`);
|
|
530
|
+
}
|
|
531
|
+
// 明示位置: 通り参照で置かれたものはクランプしない — はみ出しは言葉のエラーになる
|
|
532
|
+
const axisOk = seg.horizontal ? band.atAxis === "X" : band.atAxis === "Y";
|
|
533
|
+
if (!axisOk) {
|
|
534
|
+
return fail("07", `${label} の位置 ${band.atRef} は${seg.horizontal ? "水平線分なのでX系" : "垂直線分なのでY系"}の通りで指定します`);
|
|
535
|
+
}
|
|
536
|
+
const start = seg.horizontal ? seg.x1 : seg.y1;
|
|
537
|
+
pos = band.atAbs - start;
|
|
538
|
+
if (pos < half - EPS || pos > len - half + EPS) {
|
|
539
|
+
return fail("08", `位置 ${band.atRef} では ${label} (幅${band.w}) が境界線分からはみ出します (線分 ${Math.round(start)}〜${Math.round(start + len)}mm、中心の許容 ${Math.round(start + half)}〜${Math.round(start + len - half)}mm)`);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
else {
|
|
543
|
+
pos = Math.min(Math.max(band.at * len, half), len - half);
|
|
544
|
+
}
|
|
545
|
+
// 線分上の位置はパラメトリックに取る — 軸平行でも斜めでも同じ一つの式
|
|
546
|
+
const f = len > 0 ? pos / len : 0;
|
|
547
|
+
return {
|
|
548
|
+
segment: seg,
|
|
549
|
+
cx: seg.x1 + f * (seg.x2 - seg.x1),
|
|
550
|
+
cy: seg.y1 + f * (seg.y2 - seg.y1),
|
|
551
|
+
};
|
|
552
|
+
}
|
|
553
|
+
/** 開口を境界線分上に配置する */
|
|
554
|
+
export function placeOpening(model, b, o) {
|
|
555
|
+
return placeBand(model, b, o, o.kind);
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* 通行可能か。open境界と階段は扉なしで通れ、wall境界は扉があるときだけ通れる
|
|
559
|
+
* (手すり = 扉のないwall+air:1 なので自動的に通れない)。
|
|
560
|
+
* shaft (EV等) と void (吹抜け) は空間として連続するが人は通れない
|
|
561
|
+
*/
|
|
562
|
+
export function passable(b) {
|
|
563
|
+
if (b.kind === "open" || b.kind === "stair")
|
|
564
|
+
return true;
|
|
565
|
+
if (b.kind === "shaft" || b.kind === "void")
|
|
566
|
+
return false;
|
|
567
|
+
return b.openings.some((o) => o.kind === "door");
|
|
568
|
+
}
|
|
569
|
+
/** 通過扉数 (open境界・階段=0, 扉付きwall境界=1) */
|
|
570
|
+
function doorCost(b) {
|
|
571
|
+
return b.kind === "wall" ? 1 : 0;
|
|
572
|
+
}
|
|
573
|
+
/** aからbまで扉をいくつ通るか (最小)。到達不能なら undefined */
|
|
574
|
+
export function doorsBetween(model, from, to) {
|
|
575
|
+
if (!model.spaces.has(from) || !model.spaces.has(to))
|
|
576
|
+
return undefined;
|
|
577
|
+
const dist = new Map();
|
|
578
|
+
const prev = new Map();
|
|
579
|
+
dist.set(from, 0);
|
|
580
|
+
const queue = [from];
|
|
581
|
+
const visited = new Set();
|
|
582
|
+
while (queue.length) {
|
|
583
|
+
queue.sort((p, q) => (dist.get(p) ?? Infinity) - (dist.get(q) ?? Infinity));
|
|
584
|
+
const u = queue.shift();
|
|
585
|
+
if (visited.has(u))
|
|
586
|
+
continue;
|
|
587
|
+
visited.add(u);
|
|
588
|
+
for (const b of model.boundaries) {
|
|
589
|
+
if (!passable(b))
|
|
590
|
+
continue;
|
|
591
|
+
const v = b.a === u ? b.b : b.b === u ? b.a : undefined;
|
|
592
|
+
if (!v || visited.has(v))
|
|
593
|
+
continue;
|
|
594
|
+
const nd = (dist.get(u) ?? Infinity) + doorCost(b);
|
|
595
|
+
if (nd < (dist.get(v) ?? Infinity)) {
|
|
596
|
+
dist.set(v, nd);
|
|
597
|
+
prev.set(v, u);
|
|
598
|
+
queue.push(v);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
if (!dist.has(to))
|
|
603
|
+
return undefined;
|
|
604
|
+
const path = [to];
|
|
605
|
+
while (path[0] !== from)
|
|
606
|
+
path.unshift(prev.get(path[0]));
|
|
607
|
+
return { doors: dist.get(to), path };
|
|
608
|
+
}
|
|
609
|
+
export function neighbors(model, path) {
|
|
610
|
+
const out = [];
|
|
611
|
+
for (const b of model.boundaries) {
|
|
612
|
+
const other = b.a === path ? b.b : b.b === path ? b.a : undefined;
|
|
613
|
+
if (!other)
|
|
614
|
+
continue;
|
|
615
|
+
const s = model.spaces.get(other);
|
|
616
|
+
if (!s)
|
|
617
|
+
continue;
|
|
618
|
+
out.push({
|
|
619
|
+
space: s,
|
|
620
|
+
boundary: b,
|
|
621
|
+
passable: passable(b),
|
|
622
|
+
doors: b.openings.filter((o) => o.kind === "door").length,
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
return out;
|
|
626
|
+
}
|
|
627
|
+
//# sourceMappingURL=graph.js.map
|