@kruschdev/pascal-mep-kernel 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +29 -2
- package/dist/anatomy/footprint.d.ts +164 -0
- package/dist/anatomy/index.d.ts +31 -0
- package/dist/anatomy/index.js +121 -0
- package/dist/anatomy/levels.d.ts +121 -0
- package/dist/anatomy/occupancy.d.ts +194 -0
- package/dist/anatomy/openings.d.ts +125 -0
- package/dist/anatomy/roof.d.ts +102 -0
- package/dist/chunk-0w98adg9.js +2486 -0
- package/dist/chunk-2e23cyn0.js +3567 -0
- package/dist/chunk-jfzqztgh.js +260 -0
- package/dist/chunk-sab5aaa6.js +615 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +186 -0
- package/dist/router/cavity-graph.d.ts +37 -0
- package/dist/router/index.d.ts +11 -0
- package/dist/router/index.js +12 -0
- package/dist/router/shortest-path.d.ts +37 -0
- package/dist/router/types.d.ts +108 -0
- package/dist/rules/evaluator.d.ts +60 -0
- package/dist/rules/index.d.ts +10 -0
- package/dist/rules/index.js +15 -0
- package/dist/rules/schema.d.ts +227 -0
- package/dist/types.d.ts +137 -0
- package/dist/types.js +48 -0
- package/package.json +73 -4
|
@@ -0,0 +1,3567 @@
|
|
|
1
|
+
import {
|
|
2
|
+
TRADE_PRIORITY_RANK2,
|
|
3
|
+
INCHES_TO_METERS2,
|
|
4
|
+
SQM_TO_SQFT2,
|
|
5
|
+
isVec32,
|
|
6
|
+
segmentIntersectsAabb32,
|
|
7
|
+
createOrientedPrismY2,
|
|
8
|
+
segmentIntersectsOrientedPrismY2,
|
|
9
|
+
extractNodeYawRad2,
|
|
10
|
+
round32
|
|
11
|
+
} from "./chunk-jfzqztgh.js";
|
|
12
|
+
|
|
13
|
+
// src/anatomy/levels.ts
|
|
14
|
+
function resolveNodeLevelId2(nodes, node, options = {}) {
|
|
15
|
+
const allowDangling = options.allowDanglingParentId ?? true;
|
|
16
|
+
if (node.type === "level")
|
|
17
|
+
return node.id;
|
|
18
|
+
const visited = new Set([node.id]);
|
|
19
|
+
let cur = node;
|
|
20
|
+
while (cur?.parentId) {
|
|
21
|
+
const pid = cur.parentId;
|
|
22
|
+
if (visited.has(pid))
|
|
23
|
+
break;
|
|
24
|
+
visited.add(pid);
|
|
25
|
+
const parent = nodes[pid];
|
|
26
|
+
if (!parent) {
|
|
27
|
+
return allowDangling ? pid : null;
|
|
28
|
+
}
|
|
29
|
+
if (parent.type === "level")
|
|
30
|
+
return parent.id;
|
|
31
|
+
cur = parent;
|
|
32
|
+
}
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
function belongsToLevel2(nodes, node, levelId, options = {}) {
|
|
36
|
+
if (!levelId)
|
|
37
|
+
return true;
|
|
38
|
+
if (node.type === "level")
|
|
39
|
+
return node.id === levelId;
|
|
40
|
+
const visited = new Set([node.id]);
|
|
41
|
+
let cur = node;
|
|
42
|
+
while (cur?.parentId) {
|
|
43
|
+
const pid = cur.parentId;
|
|
44
|
+
if (pid === levelId)
|
|
45
|
+
return true;
|
|
46
|
+
if (visited.has(pid))
|
|
47
|
+
break;
|
|
48
|
+
visited.add(pid);
|
|
49
|
+
const parent = nodes[pid];
|
|
50
|
+
if (!parent)
|
|
51
|
+
return false;
|
|
52
|
+
if (parent.type === "level")
|
|
53
|
+
return parent.id === levelId;
|
|
54
|
+
cur = parent;
|
|
55
|
+
}
|
|
56
|
+
if (options.includeUnparented && (node.parentId == null || node.parentId === "")) {
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
function resolveLevelBuildingId2(nodes, level) {
|
|
62
|
+
if (!level)
|
|
63
|
+
return null;
|
|
64
|
+
if (typeof level.parentId === "string" && level.parentId.length > 0) {
|
|
65
|
+
const p = nodes[level.parentId];
|
|
66
|
+
if (p?.type === "building")
|
|
67
|
+
return p.id;
|
|
68
|
+
}
|
|
69
|
+
for (const n of Object.values(nodes)) {
|
|
70
|
+
if (n.type === "building" && Array.isArray(n.children) && n.children.includes(level.id)) {
|
|
71
|
+
return n.id;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return typeof level.parentId === "string" && level.parentId.length > 0 ? level.parentId : null;
|
|
75
|
+
}
|
|
76
|
+
function areLevelsInSameBuilding2(nodes, a, b, strictNullBuilding = false) {
|
|
77
|
+
const bA = resolveLevelBuildingId2(nodes, a);
|
|
78
|
+
const bB = resolveLevelBuildingId2(nodes, b);
|
|
79
|
+
if (strictNullBuilding)
|
|
80
|
+
return bA === bB;
|
|
81
|
+
if (bA && bB && bA !== bB)
|
|
82
|
+
return false;
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
function levelSortOrder2(level) {
|
|
86
|
+
if (!level)
|
|
87
|
+
return 0;
|
|
88
|
+
if (typeof level.level === "number" && Number.isFinite(level.level))
|
|
89
|
+
return level.level;
|
|
90
|
+
if (typeof level.elevation === "number" && Number.isFinite(level.elevation))
|
|
91
|
+
return level.elevation;
|
|
92
|
+
if (typeof level.baseElevation === "number" && Number.isFinite(level.baseElevation)) {
|
|
93
|
+
return level.baseElevation;
|
|
94
|
+
}
|
|
95
|
+
if (typeof level.index === "number" && Number.isFinite(level.index))
|
|
96
|
+
return level.index;
|
|
97
|
+
return 0;
|
|
98
|
+
}
|
|
99
|
+
function compareLevelsVertically2(a, b) {
|
|
100
|
+
if (!a && !b)
|
|
101
|
+
return 0;
|
|
102
|
+
if (!a)
|
|
103
|
+
return -1;
|
|
104
|
+
if (!b)
|
|
105
|
+
return 1;
|
|
106
|
+
const aLvl = typeof a.level === "number" && Number.isFinite(a.level) ? a.level : null;
|
|
107
|
+
const bLvl = typeof b.level === "number" && Number.isFinite(b.level) ? b.level : null;
|
|
108
|
+
if (aLvl !== null && bLvl !== null && Math.abs(aLvl - bLvl) > 0.000001) {
|
|
109
|
+
return aLvl - bLvl;
|
|
110
|
+
}
|
|
111
|
+
const aElev = typeof a.elevation === "number" && Number.isFinite(a.elevation) ? a.elevation : null;
|
|
112
|
+
const bElev = typeof b.elevation === "number" && Number.isFinite(b.elevation) ? b.elevation : null;
|
|
113
|
+
if (aElev !== null && bElev !== null && Math.abs(aElev - bElev) > 0.000001) {
|
|
114
|
+
return aElev - bElev;
|
|
115
|
+
}
|
|
116
|
+
const aBase = typeof a.baseElevation === "number" && Number.isFinite(a.baseElevation) ? a.baseElevation : null;
|
|
117
|
+
const bBase = typeof b.baseElevation === "number" && Number.isFinite(b.baseElevation) ? b.baseElevation : null;
|
|
118
|
+
if (aBase !== null && bBase !== null && Math.abs(aBase - bBase) > 0.000001) {
|
|
119
|
+
return aBase - bBase;
|
|
120
|
+
}
|
|
121
|
+
const aIdx = typeof a.index === "number" && Number.isFinite(a.index) ? a.index : null;
|
|
122
|
+
const bIdx = typeof b.index === "number" && Number.isFinite(b.index) ? b.index : null;
|
|
123
|
+
if (aIdx !== null && bIdx !== null && Math.abs(aIdx - bIdx) > 0.000001) {
|
|
124
|
+
return aIdx - bIdx;
|
|
125
|
+
}
|
|
126
|
+
return levelSortOrder2(a) - levelSortOrder2(b);
|
|
127
|
+
}
|
|
128
|
+
function sameBuildingId(bA, bB, strictNullBuilding) {
|
|
129
|
+
if (strictNullBuilding)
|
|
130
|
+
return bA === bB;
|
|
131
|
+
if (bA && bB && bA !== bB)
|
|
132
|
+
return false;
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
function buildLevelHierarchyIndex(nodes, includeHiddenWalls) {
|
|
136
|
+
const levels = [];
|
|
137
|
+
const buildingByChildLevel = new Map;
|
|
138
|
+
const walledLevelIds = new Set;
|
|
139
|
+
const maxWallHeightByLevel = new Map;
|
|
140
|
+
for (const n of Object.values(nodes)) {
|
|
141
|
+
if (n.type === "level") {
|
|
142
|
+
levels.push(n);
|
|
143
|
+
} else if (n.type === "building" && Array.isArray(n.children)) {
|
|
144
|
+
for (const cid of n.children) {
|
|
145
|
+
if (!buildingByChildLevel.has(cid)) {
|
|
146
|
+
buildingByChildLevel.set(cid, n.id);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
} else if (n.type === "wall") {
|
|
150
|
+
if (!includeHiddenWalls && n.visible === false)
|
|
151
|
+
continue;
|
|
152
|
+
const lid = resolveNodeLevelId2(nodes, n, { allowDanglingParentId: false });
|
|
153
|
+
if (!lid)
|
|
154
|
+
continue;
|
|
155
|
+
walledLevelIds.add(lid);
|
|
156
|
+
const h = n.height;
|
|
157
|
+
if (typeof h === "number" && Number.isFinite(h) && h > 1.5 && h <= 12) {
|
|
158
|
+
const prev = maxWallHeightByLevel.get(lid) ?? 0;
|
|
159
|
+
if (h > prev)
|
|
160
|
+
maxWallHeightByLevel.set(lid, h);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
const buildingByLevelId = new Map;
|
|
165
|
+
for (const lvl of levels) {
|
|
166
|
+
let bId = null;
|
|
167
|
+
if (typeof lvl.parentId === "string" && lvl.parentId.length > 0) {
|
|
168
|
+
const p = nodes[lvl.parentId];
|
|
169
|
+
if (p?.type === "building") {
|
|
170
|
+
bId = p.id;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (bId === null) {
|
|
174
|
+
const viaChild = buildingByChildLevel.get(lvl.id);
|
|
175
|
+
if (viaChild !== undefined) {
|
|
176
|
+
bId = viaChild;
|
|
177
|
+
} else if (typeof lvl.parentId === "string" && lvl.parentId.length > 0) {
|
|
178
|
+
bId = lvl.parentId;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
buildingByLevelId.set(lvl.id, bId);
|
|
182
|
+
}
|
|
183
|
+
return { levels, buildingByLevelId, walledLevelIds, maxWallHeightByLevel };
|
|
184
|
+
}
|
|
185
|
+
function isBasementLevel2(level) {
|
|
186
|
+
if (!level)
|
|
187
|
+
return false;
|
|
188
|
+
if (typeof level.level === "number" && Number.isFinite(level.level) && level.level < 0)
|
|
189
|
+
return true;
|
|
190
|
+
if (typeof level.baseElevation === "number" && Number.isFinite(level.baseElevation) && level.baseElevation < -0.5) {
|
|
191
|
+
return true;
|
|
192
|
+
}
|
|
193
|
+
if (typeof level.elevation === "number" && Number.isFinite(level.elevation) && level.elevation < -0.5) {
|
|
194
|
+
return true;
|
|
195
|
+
}
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
function levelHasWalls2(nodes, levelId, includeHidden = false) {
|
|
199
|
+
for (const n of Object.values(nodes)) {
|
|
200
|
+
if (n.type !== "wall")
|
|
201
|
+
continue;
|
|
202
|
+
if (!includeHidden && n.visible === false)
|
|
203
|
+
continue;
|
|
204
|
+
if (belongsToLevel2(nodes, n, levelId))
|
|
205
|
+
return true;
|
|
206
|
+
}
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
function resolveWallHeightM2(nodes, wall, fallbackM = 2.55) {
|
|
210
|
+
if (typeof wall.height === "number" && Number.isFinite(wall.height) && wall.height > 0.5 && wall.height <= 12) {
|
|
211
|
+
return wall.height;
|
|
212
|
+
}
|
|
213
|
+
const lvlId = resolveNodeLevelId2(nodes, wall);
|
|
214
|
+
const lvl = lvlId ? nodes[lvlId] : undefined;
|
|
215
|
+
if (lvl && typeof lvl.height === "number" && Number.isFinite(lvl.height) && lvl.height > 1.5 && lvl.height <= 12) {
|
|
216
|
+
return lvl.height;
|
|
217
|
+
}
|
|
218
|
+
return fallbackM;
|
|
219
|
+
}
|
|
220
|
+
function resolveLevelHeightM2(nodes, levelId = null, options = {}) {
|
|
221
|
+
const defaultHeightM = options.defaultHeightM ?? 2.7;
|
|
222
|
+
const includeHiddenWalls = options.includeHiddenWalls ?? false;
|
|
223
|
+
const includeUnparentedWalls = options.includeUnparentedWalls ?? false;
|
|
224
|
+
let maxWallH = 0;
|
|
225
|
+
for (const n of Object.values(nodes)) {
|
|
226
|
+
if (n.type !== "wall")
|
|
227
|
+
continue;
|
|
228
|
+
if (!includeHiddenWalls && n.visible === false)
|
|
229
|
+
continue;
|
|
230
|
+
if (levelId && !belongsToLevel2(nodes, n, levelId, { includeUnparented: includeUnparentedWalls })) {
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
const h = n.height;
|
|
234
|
+
if (typeof h === "number" && Number.isFinite(h) && h > 1.5 && h <= 12 && h > maxWallH) {
|
|
235
|
+
maxWallH = h;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
let resolved = maxWallH;
|
|
239
|
+
if (resolved <= 0) {
|
|
240
|
+
const levelNode = levelId ? nodes[levelId] : Object.values(nodes).find((n) => n.type === "level" && typeof n.height === "number" && n.height > 1.5 && n.height <= 12);
|
|
241
|
+
const lh = levelNode?.height;
|
|
242
|
+
if (typeof lh === "number" && Number.isFinite(lh) && lh > 1.5 && lh <= 12) {
|
|
243
|
+
resolved = lh;
|
|
244
|
+
} else {
|
|
245
|
+
resolved = defaultHeightM;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
if (typeof options.minClampM === "number" && Number.isFinite(options.minClampM)) {
|
|
249
|
+
return Math.max(options.minClampM, resolved);
|
|
250
|
+
}
|
|
251
|
+
return resolved;
|
|
252
|
+
}
|
|
253
|
+
function getLowerWalledLevelIds2(nodes, levelId, options = {}) {
|
|
254
|
+
if (!levelId)
|
|
255
|
+
return [];
|
|
256
|
+
const cur = nodes[levelId];
|
|
257
|
+
if (!cur || cur.type !== "level")
|
|
258
|
+
return [];
|
|
259
|
+
const excludeBasements = options.excludeBasements ?? false;
|
|
260
|
+
const includeHiddenWalls = options.includeHiddenWalls ?? false;
|
|
261
|
+
const strictNullBuilding = options.strictNullBuilding ?? false;
|
|
262
|
+
const idx = buildLevelHierarchyIndex(nodes, includeHiddenWalls);
|
|
263
|
+
const curBuildingId = idx.buildingByLevelId.get(levelId) ?? null;
|
|
264
|
+
const lower = [];
|
|
265
|
+
for (const n of idx.levels) {
|
|
266
|
+
if (n.id === levelId)
|
|
267
|
+
continue;
|
|
268
|
+
if (!sameBuildingId(curBuildingId, idx.buildingByLevelId.get(n.id) ?? null, strictNullBuilding)) {
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
if (excludeBasements && isBasementLevel2(n))
|
|
272
|
+
continue;
|
|
273
|
+
if (compareLevelsVertically2(n, cur) >= 0)
|
|
274
|
+
continue;
|
|
275
|
+
if (!idx.walledLevelIds.has(n.id))
|
|
276
|
+
continue;
|
|
277
|
+
lower.push(n);
|
|
278
|
+
}
|
|
279
|
+
lower.sort(compareLevelsVertically2);
|
|
280
|
+
return lower.map((l) => l.id);
|
|
281
|
+
}
|
|
282
|
+
function resolveLowerWalledLevelsSummary2(nodes, levelId, options = {}) {
|
|
283
|
+
if (!levelId) {
|
|
284
|
+
return { cumulativeBelowHeightM: 0, lowerLevelIds: [], groundLevelId: null };
|
|
285
|
+
}
|
|
286
|
+
const cur = nodes[levelId];
|
|
287
|
+
if (!cur || cur.type !== "level") {
|
|
288
|
+
return { cumulativeBelowHeightM: 0, lowerLevelIds: [], groundLevelId: null };
|
|
289
|
+
}
|
|
290
|
+
const excludeBasements = options.excludeBasements ?? true;
|
|
291
|
+
const includeHiddenWalls = options.includeHiddenWalls ?? false;
|
|
292
|
+
const idx = buildLevelHierarchyIndex(nodes, includeHiddenWalls);
|
|
293
|
+
const curBuildingId = idx.buildingByLevelId.get(levelId) ?? null;
|
|
294
|
+
const lower = [];
|
|
295
|
+
for (const n of idx.levels) {
|
|
296
|
+
if (n.id === levelId)
|
|
297
|
+
continue;
|
|
298
|
+
if (!sameBuildingId(curBuildingId, idx.buildingByLevelId.get(n.id) ?? null, false))
|
|
299
|
+
continue;
|
|
300
|
+
if (excludeBasements && isBasementLevel2(n))
|
|
301
|
+
continue;
|
|
302
|
+
if (compareLevelsVertically2(n, cur) >= 0)
|
|
303
|
+
continue;
|
|
304
|
+
if (!idx.walledLevelIds.has(n.id))
|
|
305
|
+
continue;
|
|
306
|
+
lower.push(n);
|
|
307
|
+
}
|
|
308
|
+
lower.sort(compareLevelsVertically2);
|
|
309
|
+
const lowerLevelIds = lower.map((l) => l.id);
|
|
310
|
+
let cumulativeBelowHeightM = 0;
|
|
311
|
+
for (const lvl of lower) {
|
|
312
|
+
const maxWallH = idx.maxWallHeightByLevel.get(lvl.id) ?? 0;
|
|
313
|
+
if (maxWallH > 0) {
|
|
314
|
+
cumulativeBelowHeightM += maxWallH;
|
|
315
|
+
} else if (typeof lvl.height === "number" && Number.isFinite(lvl.height) && lvl.height > 1.5 && lvl.height <= 12) {
|
|
316
|
+
cumulativeBelowHeightM += lvl.height;
|
|
317
|
+
} else {
|
|
318
|
+
cumulativeBelowHeightM += 2.7;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
return {
|
|
322
|
+
cumulativeBelowHeightM,
|
|
323
|
+
lowerLevelIds,
|
|
324
|
+
groundLevelId: lowerLevelIds[0] ?? null
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
function isGroundOrLowestWalledLevel2(nodes, levelId, options = {}) {
|
|
328
|
+
if (!levelId)
|
|
329
|
+
return true;
|
|
330
|
+
const cur = nodes[levelId];
|
|
331
|
+
if (!cur || cur.type !== "level")
|
|
332
|
+
return true;
|
|
333
|
+
const lower = getLowerWalledLevelIds2(nodes, levelId, {
|
|
334
|
+
excludeBasements: options.excludeBasements ?? false,
|
|
335
|
+
includeHiddenWalls: options.includeHiddenWalls ?? false
|
|
336
|
+
});
|
|
337
|
+
return lower.length === 0;
|
|
338
|
+
}
|
|
339
|
+
function isHighestWalledLevelInBuilding2(nodes, levelId, options = {}) {
|
|
340
|
+
if (!levelId)
|
|
341
|
+
return true;
|
|
342
|
+
const cur = nodes[levelId];
|
|
343
|
+
if (!cur || cur.type !== "level")
|
|
344
|
+
return true;
|
|
345
|
+
const includeHiddenWalls = options.includeHiddenWalls ?? false;
|
|
346
|
+
const idx = buildLevelHierarchyIndex(nodes, includeHiddenWalls);
|
|
347
|
+
const curBuildingId = idx.buildingByLevelId.get(levelId) ?? null;
|
|
348
|
+
for (const n of idx.levels) {
|
|
349
|
+
if (n.id === levelId)
|
|
350
|
+
continue;
|
|
351
|
+
if (!sameBuildingId(curBuildingId, idx.buildingByLevelId.get(n.id) ?? null, false))
|
|
352
|
+
continue;
|
|
353
|
+
if (compareLevelsVertically2(n, cur) <= 0)
|
|
354
|
+
continue;
|
|
355
|
+
if (idx.walledLevelIds.has(n.id))
|
|
356
|
+
return false;
|
|
357
|
+
}
|
|
358
|
+
return true;
|
|
359
|
+
}
|
|
360
|
+
function findImmediateLowerWalledLevel2(nodes, levelId, options = {}) {
|
|
361
|
+
const lowerIds = getLowerWalledLevelIds2(nodes, levelId, {
|
|
362
|
+
excludeBasements: false,
|
|
363
|
+
includeHiddenWalls: options.includeHiddenWalls ?? false
|
|
364
|
+
});
|
|
365
|
+
const last = lowerIds[lowerIds.length - 1];
|
|
366
|
+
return last ? nodes[last] ?? null : null;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// src/anatomy/openings.ts
|
|
370
|
+
var ITEM_DOOR_RE = /\bdoor\b|garage[-_\s]?door|sliding[-_\s]?door|patio[-_\s]?door/;
|
|
371
|
+
var ITEM_WINDOW_RE = /\bwindow\b/;
|
|
372
|
+
function classifyOpeningKind2(node, opts) {
|
|
373
|
+
if (opts?.includeHidden === false && node.visible === false)
|
|
374
|
+
return null;
|
|
375
|
+
if (node.type === "door")
|
|
376
|
+
return "door";
|
|
377
|
+
if (node.type === "window")
|
|
378
|
+
return "window";
|
|
379
|
+
if (node.type === "item") {
|
|
380
|
+
const asset = node.asset && typeof node.asset === "object" ? node.asset : undefined;
|
|
381
|
+
const rawCat = typeof asset?.category === "string" && asset.category.trim().length > 0 ? asset.category : typeof node.category === "string" ? node.category : "";
|
|
382
|
+
const cat = rawCat.toLowerCase().trim();
|
|
383
|
+
if (cat === "door" || cat === "doors")
|
|
384
|
+
return "door";
|
|
385
|
+
if (cat === "window" || cat === "windows")
|
|
386
|
+
return "window";
|
|
387
|
+
const text = `${String(node.name ?? "")} ${String(asset?.id ?? "")} ${String(asset?.name ?? "")}`.toLowerCase();
|
|
388
|
+
if (ITEM_DOOR_RE.test(text))
|
|
389
|
+
return "door";
|
|
390
|
+
if (ITEM_WINDOW_RE.test(text))
|
|
391
|
+
return "window";
|
|
392
|
+
}
|
|
393
|
+
return null;
|
|
394
|
+
}
|
|
395
|
+
function isCanonicalWallLike(wall) {
|
|
396
|
+
return "lengthM" in wall && typeof wall.lengthM === "number" && "ux" in wall && typeof wall.ux === "number" && "thicknessM" in wall && typeof wall.thicknessM === "number";
|
|
397
|
+
}
|
|
398
|
+
function extractCanonicalWallOpeningsFromCandidates(nodes, wall, candidates, childIds, opts = {}) {
|
|
399
|
+
if (!Array.isArray(wall.start) || !Array.isArray(wall.end))
|
|
400
|
+
return [];
|
|
401
|
+
const sx = Number(wall.start[0]);
|
|
402
|
+
const sz = Number(wall.start[1]);
|
|
403
|
+
const ex = Number(wall.end[0]);
|
|
404
|
+
const ez = Number(wall.end[1]);
|
|
405
|
+
const wallLenM = Math.hypot(ex - sx, ez - sz);
|
|
406
|
+
if (!Number.isFinite(wallLenM) || wallLenM < 0.1)
|
|
407
|
+
return [];
|
|
408
|
+
const ux = (ex - sx) / wallLenM;
|
|
409
|
+
const uz = (ez - sz) / wallLenM;
|
|
410
|
+
const canonical = isCanonicalWallLike(wall);
|
|
411
|
+
const rawWallNode = nodes[wall.id];
|
|
412
|
+
const wallHeightM = typeof opts.wallHeightM === "number" && Number.isFinite(opts.wallHeightM) && opts.wallHeightM > 0 ? opts.wallHeightM : canonical && Number.isFinite(wall.heightM) && wall.heightM > 0 ? wall.heightM : resolveWallHeightM2(nodes, rawWallNode ?? wall, 2.55);
|
|
413
|
+
const thicknessM = canonical && Number.isFinite(wall.thicknessM) && wall.thicknessM > 0 ? wall.thicknessM : !canonical && typeof wall.thickness === "number" && Number.isFinite(wall.thickness) && wall.thickness > 0 ? wall.thickness : typeof rawWallNode?.thickness === "number" && Number.isFinite(rawWallNode.thickness) && rawWallNode.thickness > 0 ? rawWallNode.thickness : 0.14;
|
|
414
|
+
const includeHidden = opts.includeHidden ?? false;
|
|
415
|
+
const out = [];
|
|
416
|
+
for (const o of candidates) {
|
|
417
|
+
if (!includeHidden && o.visible === false)
|
|
418
|
+
continue;
|
|
419
|
+
const kind = classifyOpeningKind2(o, { includeHidden });
|
|
420
|
+
if (!kind)
|
|
421
|
+
continue;
|
|
422
|
+
const asset = o.asset && typeof o.asset === "object" ? o.asset : undefined;
|
|
423
|
+
const dims = Array.isArray(asset?.dimensions) ? asset.dimensions : undefined;
|
|
424
|
+
const scale = Array.isArray(o.scale) ? o.scale : [1, 1, 1];
|
|
425
|
+
const sxScale = typeof scale[0] === "number" && Number.isFinite(scale[0]) && scale[0] > 0 ? scale[0] : 1;
|
|
426
|
+
const syScale = typeof scale[1] === "number" && Number.isFinite(scale[1]) && scale[1] > 0 ? scale[1] : 1;
|
|
427
|
+
const rawW = typeof o.width === "number" && Number.isFinite(o.width) && o.width > 0 ? o.width : dims && typeof dims[0] === "number" && Number.isFinite(dims[0]) && dims[0] > 0 ? dims[0] * sxScale : kind === "door" ? 0.9144 : 1.2192;
|
|
428
|
+
const rawH = typeof o.height === "number" && Number.isFinite(o.height) && o.height > 0 ? o.height : dims && typeof dims[1] === "number" && Number.isFinite(dims[1]) && dims[1] > 0 ? dims[1] * syScale : kind === "door" ? 2.0828 : 1.2192;
|
|
429
|
+
const nominalWidthM = Math.max(0.2, Math.min(wallLenM, rawW));
|
|
430
|
+
const pos = Array.isArray(o.position) ? o.position : undefined;
|
|
431
|
+
const itemRotY = extractNodeYawRad2(o.rotation);
|
|
432
|
+
const assetOffset = Array.isArray(asset?.offset) ? asset.offset : undefined;
|
|
433
|
+
let rawUCenter = wallLenM / 2;
|
|
434
|
+
const isWallChild = o.parentId === wall.id || childIds.has(o.id);
|
|
435
|
+
if (pos && typeof pos[0] === "number" && Number.isFinite(pos[0])) {
|
|
436
|
+
const px = pos[0];
|
|
437
|
+
const pz = typeof pos[2] === "number" && Number.isFinite(pos[2]) ? pos[2] : 0;
|
|
438
|
+
if (isWallChild && Math.abs(pz) < 0.45 && px >= -0.25 && px <= wallLenM + 0.25) {
|
|
439
|
+
rawUCenter = px;
|
|
440
|
+
if (o.type === "item" && assetOffset) {
|
|
441
|
+
const ox = typeof assetOffset[0] === "number" && Number.isFinite(assetOffset[0]) ? assetOffset[0] : 0;
|
|
442
|
+
const oz = typeof assetOffset[2] === "number" && Number.isFinite(assetOffset[2]) ? assetOffset[2] : 0;
|
|
443
|
+
rawUCenter += ox * Math.cos(itemRotY) + oz * Math.sin(itemRotY);
|
|
444
|
+
}
|
|
445
|
+
} else {
|
|
446
|
+
rawUCenter = (px - sx) * ux + (pz - sz) * uz;
|
|
447
|
+
}
|
|
448
|
+
} else if (typeof o.offset === "number" && Number.isFinite(o.offset)) {
|
|
449
|
+
rawUCenter = o.offset;
|
|
450
|
+
}
|
|
451
|
+
const startU = Math.max(0, rawUCenter - nominalWidthM / 2);
|
|
452
|
+
const endU = Math.min(wallLenM, rawUCenter + nominalWidthM / 2);
|
|
453
|
+
const span = endU - startU;
|
|
454
|
+
if (span < 0.05)
|
|
455
|
+
continue;
|
|
456
|
+
const uCenterM = (startU + endU) / 2;
|
|
457
|
+
const widthM = span;
|
|
458
|
+
const rawPosY = pos && typeof pos[1] === "number" && Number.isFinite(pos[1]) ? pos[1] : null;
|
|
459
|
+
const explicitSill = typeof o.sillHeight === "number" && Number.isFinite(o.sillHeight) ? o.sillHeight : null;
|
|
460
|
+
let rawSillYM = 0;
|
|
461
|
+
if (kind === "window") {
|
|
462
|
+
if (explicitSill !== null) {
|
|
463
|
+
rawSillYM = Math.max(0, explicitSill);
|
|
464
|
+
} else if (o.type === "window") {
|
|
465
|
+
if (rawPosY !== null && rawPosY > 0) {
|
|
466
|
+
rawSillYM = Math.max(0, rawPosY - rawH / 2);
|
|
467
|
+
} else {
|
|
468
|
+
rawSillYM = 0.9144;
|
|
469
|
+
}
|
|
470
|
+
} else {
|
|
471
|
+
if (rawPosY !== null && rawPosY > 0.02) {
|
|
472
|
+
rawSillYM = Math.max(0, rawPosY);
|
|
473
|
+
} else if (rawPosY === 0 && rawH >= wallHeightM - 0.35) {
|
|
474
|
+
rawSillYM = 0;
|
|
475
|
+
} else {
|
|
476
|
+
rawSillYM = 0.9144;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
rawSillYM = Math.max(0, Math.min(Math.max(0, wallHeightM - 0.2), rawSillYM));
|
|
480
|
+
}
|
|
481
|
+
const headYM = Math.max(rawSillYM + 0.1, Math.min(wallHeightM, rawSillYM + rawH));
|
|
482
|
+
const heightM = headYM - rawSillYM;
|
|
483
|
+
const framedSillYM = kind === "door" ? 0 : Math.max(0.08, rawSillYM);
|
|
484
|
+
const centerX = sx + ux * uCenterM;
|
|
485
|
+
const centerZ = sz + uz * uCenterM;
|
|
486
|
+
const jambInset = span > 0.1 ? 0.04 : 0;
|
|
487
|
+
const halfSpan = Math.max(0.01, span / 2 - jambInset);
|
|
488
|
+
const halfPerp = thicknessM / 2 + 0.05;
|
|
489
|
+
const prismYMin = kind === "door" ? 0.02 : rawSillYM;
|
|
490
|
+
const prismYMax = Math.max(prismYMin + 0.02, kind === "door" ? Math.min(wallHeightM, headYM) : headYM);
|
|
491
|
+
const prism = createOrientedPrismY2({
|
|
492
|
+
centerX,
|
|
493
|
+
centerZ,
|
|
494
|
+
cos: ux,
|
|
495
|
+
sin: uz,
|
|
496
|
+
lxMin: -halfSpan,
|
|
497
|
+
lxMax: halfSpan,
|
|
498
|
+
lzMin: -halfPerp,
|
|
499
|
+
lzMax: halfPerp,
|
|
500
|
+
yMin: prismYMin,
|
|
501
|
+
yMax: prismYMax
|
|
502
|
+
});
|
|
503
|
+
out.push({
|
|
504
|
+
id: o.id,
|
|
505
|
+
wallId: wall.id,
|
|
506
|
+
kind,
|
|
507
|
+
name: String(o.name ?? asset?.name ?? kind),
|
|
508
|
+
uCenterM,
|
|
509
|
+
startU,
|
|
510
|
+
endU,
|
|
511
|
+
widthM,
|
|
512
|
+
heightM,
|
|
513
|
+
rawSillYM,
|
|
514
|
+
framedSillYM,
|
|
515
|
+
headYM,
|
|
516
|
+
centerX,
|
|
517
|
+
centerZ,
|
|
518
|
+
prism
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
out.sort((a, b) => a.uCenterM - b.uCenterM || a.id.localeCompare(b.id));
|
|
522
|
+
return out;
|
|
523
|
+
}
|
|
524
|
+
function extractCanonicalWallOpenings2(nodes, wall, opts = {}) {
|
|
525
|
+
if (!Array.isArray(wall.start) || !Array.isArray(wall.end))
|
|
526
|
+
return [];
|
|
527
|
+
const canonical = isCanonicalWallLike(wall);
|
|
528
|
+
const rawWallNode = nodes[wall.id];
|
|
529
|
+
const childIds = new Set;
|
|
530
|
+
if (!canonical && Array.isArray(wall.children)) {
|
|
531
|
+
for (const cid of wall.children)
|
|
532
|
+
childIds.add(cid);
|
|
533
|
+
}
|
|
534
|
+
if (rawWallNode && Array.isArray(rawWallNode.children)) {
|
|
535
|
+
for (const cid of rawWallNode.children)
|
|
536
|
+
childIds.add(cid);
|
|
537
|
+
}
|
|
538
|
+
const seenIds = new Set;
|
|
539
|
+
const candidates = [];
|
|
540
|
+
for (const n of Object.values(nodes)) {
|
|
541
|
+
if (n.id === wall.id)
|
|
542
|
+
continue;
|
|
543
|
+
if (n.parentId === wall.id || n.wallId === wall.id || childIds.has(n.id)) {
|
|
544
|
+
if (!seenIds.has(n.id)) {
|
|
545
|
+
seenIds.add(n.id);
|
|
546
|
+
candidates.push(n);
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
return extractCanonicalWallOpeningsFromCandidates(nodes, wall, candidates, childIds, opts);
|
|
551
|
+
}
|
|
552
|
+
function extractCanonicalWalls2(nodes, levelId, opts = {}) {
|
|
553
|
+
const includeHidden = opts.includeHidden ?? false;
|
|
554
|
+
const includeUnparented = opts.includeUnparented ?? false;
|
|
555
|
+
const minLengthM = opts.minLengthM ?? 0.1;
|
|
556
|
+
const defaultWallHeightM = typeof opts.defaultWallHeightM === "number" && Number.isFinite(opts.defaultWallHeightM) && opts.defaultWallHeightM > 0 ? opts.defaultWallHeightM : resolveLevelHeightM2(nodes, levelId ?? null, {
|
|
557
|
+
includeHiddenWalls: includeHidden,
|
|
558
|
+
includeUnparentedWalls: includeUnparented,
|
|
559
|
+
defaultHeightM: 2.7
|
|
560
|
+
});
|
|
561
|
+
const wallNodes = [];
|
|
562
|
+
const openingsByWallId = new Map;
|
|
563
|
+
for (const n of Object.values(nodes)) {
|
|
564
|
+
if (n.type === "wall") {
|
|
565
|
+
const visible = n.visible !== false;
|
|
566
|
+
if (!includeHidden && !visible)
|
|
567
|
+
continue;
|
|
568
|
+
if (levelId && !belongsToLevel2(nodes, n, levelId, { includeUnparented }))
|
|
569
|
+
continue;
|
|
570
|
+
wallNodes.push(n);
|
|
571
|
+
continue;
|
|
572
|
+
}
|
|
573
|
+
if (n.type === "door" || n.type === "window" || n.type === "item") {
|
|
574
|
+
if (!includeHidden && n.visible === false)
|
|
575
|
+
continue;
|
|
576
|
+
if (typeof n.parentId === "string" && n.parentId.length > 0 && n.parentId !== n.id) {
|
|
577
|
+
let list = openingsByWallId.get(n.parentId);
|
|
578
|
+
if (!list) {
|
|
579
|
+
list = [];
|
|
580
|
+
openingsByWallId.set(n.parentId, list);
|
|
581
|
+
}
|
|
582
|
+
list.push(n);
|
|
583
|
+
}
|
|
584
|
+
if (typeof n.wallId === "string" && n.wallId.length > 0 && n.wallId !== n.id && n.wallId !== n.parentId) {
|
|
585
|
+
let list = openingsByWallId.get(n.wallId);
|
|
586
|
+
if (!list) {
|
|
587
|
+
list = [];
|
|
588
|
+
openingsByWallId.set(n.wallId, list);
|
|
589
|
+
}
|
|
590
|
+
list.push(n);
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
const walls = [];
|
|
595
|
+
for (const n of wallNodes) {
|
|
596
|
+
if (!Array.isArray(n.start) || !Array.isArray(n.end))
|
|
597
|
+
continue;
|
|
598
|
+
const sx = Number(n.start[0]);
|
|
599
|
+
const sz = Number(n.start[1]);
|
|
600
|
+
const ex = Number(n.end[0]);
|
|
601
|
+
const ez = Number(n.end[1]);
|
|
602
|
+
if (!Number.isFinite(sx) || !Number.isFinite(sz) || !Number.isFinite(ex) || !Number.isFinite(ez)) {
|
|
603
|
+
continue;
|
|
604
|
+
}
|
|
605
|
+
const lengthM = Math.hypot(ex - sx, ez - sz);
|
|
606
|
+
if (!Number.isFinite(lengthM) || lengthM < minLengthM)
|
|
607
|
+
continue;
|
|
608
|
+
const visible = n.visible !== false;
|
|
609
|
+
const ux = (ex - sx) / lengthM;
|
|
610
|
+
const uz = (ez - sz) / lengthM;
|
|
611
|
+
const nx = -uz;
|
|
612
|
+
const nz = ux;
|
|
613
|
+
const thicknessM = typeof n.thickness === "number" && Number.isFinite(n.thickness) && n.thickness > 0 ? n.thickness : 0.14;
|
|
614
|
+
const heightM = resolveWallHeightM2(nodes, n, defaultWallHeightM);
|
|
615
|
+
const wallLevelId = resolveNodeLevelId2(nodes, n);
|
|
616
|
+
const partialWall = {
|
|
617
|
+
id: n.id,
|
|
618
|
+
levelId: wallLevelId,
|
|
619
|
+
start: [sx, sz],
|
|
620
|
+
end: [ex, ez],
|
|
621
|
+
lengthM,
|
|
622
|
+
ux,
|
|
623
|
+
uz,
|
|
624
|
+
nx,
|
|
625
|
+
nz,
|
|
626
|
+
heightM,
|
|
627
|
+
thicknessM,
|
|
628
|
+
visible,
|
|
629
|
+
openings: []
|
|
630
|
+
};
|
|
631
|
+
const childIds = new Set;
|
|
632
|
+
if (Array.isArray(n.children)) {
|
|
633
|
+
for (const cid of n.children)
|
|
634
|
+
childIds.add(cid);
|
|
635
|
+
}
|
|
636
|
+
const indexed = openingsByWallId.get(n.id);
|
|
637
|
+
let candidates;
|
|
638
|
+
if (childIds.size === 0) {
|
|
639
|
+
candidates = indexed ?? [];
|
|
640
|
+
} else {
|
|
641
|
+
const seenIds = new Set;
|
|
642
|
+
candidates = [];
|
|
643
|
+
if (indexed) {
|
|
644
|
+
for (const c of indexed) {
|
|
645
|
+
if (!seenIds.has(c.id)) {
|
|
646
|
+
seenIds.add(c.id);
|
|
647
|
+
candidates.push(c);
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
for (const cid of childIds) {
|
|
652
|
+
if (cid === n.id || seenIds.has(cid))
|
|
653
|
+
continue;
|
|
654
|
+
const child = nodes[cid];
|
|
655
|
+
if (child) {
|
|
656
|
+
seenIds.add(cid);
|
|
657
|
+
candidates.push(child);
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
const openings = candidates.length > 0 ? extractCanonicalWallOpeningsFromCandidates(nodes, partialWall, candidates, childIds, {
|
|
662
|
+
includeHidden,
|
|
663
|
+
wallHeightM: heightM
|
|
664
|
+
}) : [];
|
|
665
|
+
walls.push({
|
|
666
|
+
...partialWall,
|
|
667
|
+
openings
|
|
668
|
+
});
|
|
669
|
+
}
|
|
670
|
+
return walls;
|
|
671
|
+
}
|
|
672
|
+
function extractLevelOpeningPrisms2(walls) {
|
|
673
|
+
const prisms = [];
|
|
674
|
+
for (const wall of walls) {
|
|
675
|
+
for (const op of wall.openings) {
|
|
676
|
+
prisms.push({
|
|
677
|
+
...op.prism,
|
|
678
|
+
id: op.id,
|
|
679
|
+
wallId: op.wallId,
|
|
680
|
+
kind: op.kind
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
return prisms;
|
|
685
|
+
}
|
|
686
|
+
function countPathOpeningPrismHits2(path, prisms, padM = 0) {
|
|
687
|
+
if (prisms.length === 0 || path.length < 2)
|
|
688
|
+
return 0;
|
|
689
|
+
let hits = 0;
|
|
690
|
+
for (let i = 0;i < path.length - 1; i++) {
|
|
691
|
+
const a = path[i];
|
|
692
|
+
const b = path[i + 1];
|
|
693
|
+
for (let pi = 0;pi < prisms.length; pi++) {
|
|
694
|
+
if (segmentIntersectsOrientedPrismY2(a, b, prisms[pi], padM)) {
|
|
695
|
+
hits += 1;
|
|
696
|
+
break;
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
return hits;
|
|
701
|
+
}
|
|
702
|
+
function findNearestWallToPoint2(px, pz, walls) {
|
|
703
|
+
if (!Number.isFinite(px) || !Number.isFinite(pz))
|
|
704
|
+
return null;
|
|
705
|
+
let best = null;
|
|
706
|
+
for (let i = 0;i < walls.length; i++) {
|
|
707
|
+
const wall = walls[i];
|
|
708
|
+
if (wall.lengthM < 0.0001)
|
|
709
|
+
continue;
|
|
710
|
+
const rawT = (px - wall.start[0]) * wall.ux + (pz - wall.start[1]) * wall.uz;
|
|
711
|
+
const t = Math.max(0, Math.min(wall.lengthM, rawT));
|
|
712
|
+
const projectedX = wall.start[0] + wall.ux * t;
|
|
713
|
+
const projectedZ = wall.start[1] + wall.uz * t;
|
|
714
|
+
const distM = Math.hypot(px - projectedX, pz - projectedZ);
|
|
715
|
+
const perpDistM = (px - projectedX) * wall.nx + (pz - projectedZ) * wall.nz;
|
|
716
|
+
if (!best || distM < best.distM - 0.0001) {
|
|
717
|
+
best = {
|
|
718
|
+
wall,
|
|
719
|
+
wallIdx: i,
|
|
720
|
+
t,
|
|
721
|
+
distM,
|
|
722
|
+
perpDistM,
|
|
723
|
+
projectedX,
|
|
724
|
+
projectedZ
|
|
725
|
+
};
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
return best;
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
// src/anatomy/footprint.ts
|
|
732
|
+
var OUTDOOR_SLAB_RE = /\b(pool|swimming|piscine|piscina|patio|deck|porch|terrace|terrasse|balcony|balcon|yard|garden|jardin|lanai|veranda|courtyard|driveway|walkway|sidewalk|step|steps|stair|stairs|landing|apron|stoop|exterior|outdoor|lawn|plaza|paving)\b/i;
|
|
733
|
+
var OUTDOOR_ZONE_RE = /\b(exterior|outdoor|pool|swimming|piscine|piscina|patio|deck|porch|balcon|balcony|terrasse|terrace|lanai|veranda|yard|garden|jardin|courtyard|driveway|lawn|plaza)\b/i;
|
|
734
|
+
function normalizeTextNfd(text) {
|
|
735
|
+
return text.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase().replace(/\s+/g, " ").trim();
|
|
736
|
+
}
|
|
737
|
+
function distancePointToSegment2D2(px, pz, ax, az, bx, bz) {
|
|
738
|
+
const dx = bx - ax;
|
|
739
|
+
const dz = bz - az;
|
|
740
|
+
const lenSq = dx * dx + dz * dz;
|
|
741
|
+
if (lenSq < 0.0000000001)
|
|
742
|
+
return Math.hypot(px - ax, pz - az);
|
|
743
|
+
const t = Math.max(0, Math.min(1, ((px - ax) * dx + (pz - az) * dz) / lenSq));
|
|
744
|
+
return Math.hypot(px - (ax + t * dx), pz - (az + t * dz));
|
|
745
|
+
}
|
|
746
|
+
function pointInPolygon2D2(px, pz, polygon, edgeTol = 0) {
|
|
747
|
+
const n = polygon.length;
|
|
748
|
+
if (n < 3)
|
|
749
|
+
return false;
|
|
750
|
+
let inside = false;
|
|
751
|
+
for (let i = 0, j = n - 1;i < n; j = i++) {
|
|
752
|
+
const pi = polygon[i];
|
|
753
|
+
const pj = polygon[j];
|
|
754
|
+
const xi = pi[0];
|
|
755
|
+
const zi = pi[1];
|
|
756
|
+
const xj = pj[0];
|
|
757
|
+
const zj = pj[1];
|
|
758
|
+
if (edgeTol > 0 && distancePointToSegment2D2(px, pz, xj, zj, xi, zi) <= edgeTol) {
|
|
759
|
+
return true;
|
|
760
|
+
}
|
|
761
|
+
const intersect = zi > pz !== zj > pz && px < (xj - xi) * (pz - zi) / (zj - zi + 0.000000000000000001) + xi;
|
|
762
|
+
if (intersect)
|
|
763
|
+
inside = !inside;
|
|
764
|
+
}
|
|
765
|
+
return inside;
|
|
766
|
+
}
|
|
767
|
+
function polygonArea2D2(polygon) {
|
|
768
|
+
const n = polygon.length;
|
|
769
|
+
if (n < 3)
|
|
770
|
+
return 0;
|
|
771
|
+
let sum = 0;
|
|
772
|
+
for (let i = 0;i < n; i++) {
|
|
773
|
+
const a = polygon[i];
|
|
774
|
+
const b = polygon[(i + 1) % n];
|
|
775
|
+
sum += a[0] * b[1] - b[0] * a[1];
|
|
776
|
+
}
|
|
777
|
+
return Math.abs(sum) * 0.5;
|
|
778
|
+
}
|
|
779
|
+
function polygonAreaAndCentroid2(polygon) {
|
|
780
|
+
const n = polygon.length;
|
|
781
|
+
if (n === 0)
|
|
782
|
+
return { areaSqM: 0, centroid: [0, 0] };
|
|
783
|
+
if (n < 3) {
|
|
784
|
+
let avgX = 0;
|
|
785
|
+
let avgZ = 0;
|
|
786
|
+
for (const p of polygon) {
|
|
787
|
+
avgX += p[0];
|
|
788
|
+
avgZ += p[1];
|
|
789
|
+
}
|
|
790
|
+
return { areaSqM: 0, centroid: [avgX / n, avgZ / n] };
|
|
791
|
+
}
|
|
792
|
+
let signed = 0;
|
|
793
|
+
let cx = 0;
|
|
794
|
+
let cz = 0;
|
|
795
|
+
for (let i = 0;i < n; i++) {
|
|
796
|
+
const a = polygon[i];
|
|
797
|
+
const b = polygon[(i + 1) % n];
|
|
798
|
+
const cross = a[0] * b[1] - b[0] * a[1];
|
|
799
|
+
signed += cross;
|
|
800
|
+
cx += (a[0] + b[0]) * cross;
|
|
801
|
+
cz += (a[1] + b[1]) * cross;
|
|
802
|
+
}
|
|
803
|
+
const areaSqM = Math.abs(signed) * 0.5;
|
|
804
|
+
if (areaSqM < 0.000001) {
|
|
805
|
+
let avgX = 0;
|
|
806
|
+
let avgZ = 0;
|
|
807
|
+
for (const p of polygon) {
|
|
808
|
+
avgX += p[0];
|
|
809
|
+
avgZ += p[1];
|
|
810
|
+
}
|
|
811
|
+
return { areaSqM: 0, centroid: [avgX / n, avgZ / n] };
|
|
812
|
+
}
|
|
813
|
+
return { areaSqM, centroid: [cx / (3 * signed), cz / (3 * signed)] };
|
|
814
|
+
}
|
|
815
|
+
function polygonBounds2D2(polygon) {
|
|
816
|
+
let minX = Number.POSITIVE_INFINITY;
|
|
817
|
+
let maxX = Number.NEGATIVE_INFINITY;
|
|
818
|
+
let minZ = Number.POSITIVE_INFINITY;
|
|
819
|
+
let maxZ = Number.NEGATIVE_INFINITY;
|
|
820
|
+
for (const pt of polygon) {
|
|
821
|
+
const x = pt[0];
|
|
822
|
+
const z = pt[1];
|
|
823
|
+
if (!Number.isFinite(x) || !Number.isFinite(z))
|
|
824
|
+
continue;
|
|
825
|
+
if (x < minX)
|
|
826
|
+
minX = x;
|
|
827
|
+
if (x > maxX)
|
|
828
|
+
maxX = x;
|
|
829
|
+
if (z < minZ)
|
|
830
|
+
minZ = z;
|
|
831
|
+
if (z > maxZ)
|
|
832
|
+
maxZ = z;
|
|
833
|
+
}
|
|
834
|
+
if (!Number.isFinite(minX) || !Number.isFinite(minZ))
|
|
835
|
+
return null;
|
|
836
|
+
return { minX, maxX, minZ, maxZ };
|
|
837
|
+
}
|
|
838
|
+
function clipSegmentToPolygon2D2(a, b, polygon) {
|
|
839
|
+
const n = polygon.length;
|
|
840
|
+
if (n < 3)
|
|
841
|
+
return [];
|
|
842
|
+
const dx = b[0] - a[0];
|
|
843
|
+
const dz = b[1] - a[1];
|
|
844
|
+
const segLen = Math.hypot(dx, dz);
|
|
845
|
+
if (segLen < 0.000001)
|
|
846
|
+
return [];
|
|
847
|
+
const ts = [0, 1];
|
|
848
|
+
for (let i = 0;i < n; i++) {
|
|
849
|
+
const v0 = polygon[i];
|
|
850
|
+
const v1 = polygon[(i + 1) % n];
|
|
851
|
+
const ex = v1[0] - v0[0];
|
|
852
|
+
const ez = v1[1] - v0[1];
|
|
853
|
+
const denom = dx * ez - dz * ex;
|
|
854
|
+
if (Math.abs(denom) > 0.0000000001) {
|
|
855
|
+
const t = ((v0[0] - a[0]) * ez - (v0[1] - a[1]) * ex) / denom;
|
|
856
|
+
const u = ((v0[0] - a[0]) * dz - (v0[1] - a[1]) * dx) / denom;
|
|
857
|
+
if (t > 0.0000001 && t < 1 - 0.0000001 && u >= -0.000001 && u <= 1 + 0.000001) {
|
|
858
|
+
ts.push(t);
|
|
859
|
+
}
|
|
860
|
+
} else {
|
|
861
|
+
for (const v of [v0, v1]) {
|
|
862
|
+
const perp = Math.abs((v[0] - a[0]) * dz - (v[1] - a[1]) * dx) / segLen;
|
|
863
|
+
if (perp <= 0.0001) {
|
|
864
|
+
const t = ((v[0] - a[0]) * dx + (v[1] - a[1]) * dz) / (segLen * segLen);
|
|
865
|
+
if (t > 0.0000001 && t < 1 - 0.0000001)
|
|
866
|
+
ts.push(t);
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
ts.sort((x, y) => x - y);
|
|
872
|
+
const uniq = [ts[0]];
|
|
873
|
+
for (let i = 1;i < ts.length; i++) {
|
|
874
|
+
if (ts[i] - uniq[uniq.length - 1] > 0.000001)
|
|
875
|
+
uniq.push(ts[i]);
|
|
876
|
+
}
|
|
877
|
+
const kept = [];
|
|
878
|
+
for (let i = 0;i < uniq.length - 1; i++) {
|
|
879
|
+
const t0 = uniq[i];
|
|
880
|
+
const t1 = uniq[i + 1];
|
|
881
|
+
if (t1 - t0 < 0.000001)
|
|
882
|
+
continue;
|
|
883
|
+
const tm = (t0 + t1) * 0.5;
|
|
884
|
+
if (pointInPolygon2D2(a[0] + dx * tm, a[1] + dz * tm, polygon, 0.0001)) {
|
|
885
|
+
const prev = kept[kept.length - 1];
|
|
886
|
+
if (prev && t0 - prev[1] <= 0.00001)
|
|
887
|
+
prev[1] = t1;
|
|
888
|
+
else
|
|
889
|
+
kept.push([t0, t1]);
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
return kept;
|
|
893
|
+
}
|
|
894
|
+
function extractBoundedWallFaces2(walls) {
|
|
895
|
+
if (walls.length === 0) {
|
|
896
|
+
return { faces: [], complete: false, aabb: null };
|
|
897
|
+
}
|
|
898
|
+
const validWalls = walls.filter((w) => w.lengthM >= 0.0001);
|
|
899
|
+
if (validWalls.length === 0) {
|
|
900
|
+
return { faces: [], complete: false, aabb: null };
|
|
901
|
+
}
|
|
902
|
+
const splits = validWalls.map((w) => [0, w.lengthM]);
|
|
903
|
+
const JUNCTION_SNAP = 0.25;
|
|
904
|
+
const wallAabbs = validWalls.map((w) => ({
|
|
905
|
+
minX: Math.min(w.start[0], w.end[0]) - JUNCTION_SNAP,
|
|
906
|
+
maxX: Math.max(w.start[0], w.end[0]) + JUNCTION_SNAP,
|
|
907
|
+
minZ: Math.min(w.start[1], w.end[1]) - JUNCTION_SNAP,
|
|
908
|
+
maxZ: Math.max(w.start[1], w.end[1]) + JUNCTION_SNAP
|
|
909
|
+
}));
|
|
910
|
+
for (let i = 0;i < validWalls.length; i++) {
|
|
911
|
+
const a = validWalls[i];
|
|
912
|
+
const aBox = wallAabbs[i];
|
|
913
|
+
for (let j = i + 1;j < validWalls.length; j++) {
|
|
914
|
+
const bBox = wallAabbs[j];
|
|
915
|
+
if (aBox.maxX < bBox.minX || aBox.minX > bBox.maxX || aBox.maxZ < bBox.minZ || aBox.minZ > bBox.maxZ) {
|
|
916
|
+
continue;
|
|
917
|
+
}
|
|
918
|
+
const b = validWalls[j];
|
|
919
|
+
const denom = a.ux * b.uz - a.uz * b.ux;
|
|
920
|
+
if (Math.abs(denom) > 0.0001) {
|
|
921
|
+
const rx = b.start[0] - a.start[0];
|
|
922
|
+
const rz = b.start[1] - a.start[1];
|
|
923
|
+
const ta = (rx * b.uz - rz * b.ux) / denom;
|
|
924
|
+
const tb = (rx * a.uz - rz * a.ux) / denom;
|
|
925
|
+
if (ta >= -JUNCTION_SNAP && ta <= a.lengthM + JUNCTION_SNAP && tb >= -JUNCTION_SNAP && tb <= b.lengthM + JUNCTION_SNAP) {
|
|
926
|
+
splits[i].push(Math.max(0, Math.min(a.lengthM, ta)));
|
|
927
|
+
splits[j].push(Math.max(0, Math.min(b.lengthM, tb)));
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
for (const ep of [a.start, a.end]) {
|
|
931
|
+
const tb = (ep[0] - b.start[0]) * b.ux + (ep[1] - b.start[1]) * b.uz;
|
|
932
|
+
if (tb >= 0 && tb <= b.lengthM) {
|
|
933
|
+
const px = b.start[0] + b.ux * tb;
|
|
934
|
+
const pz = b.start[1] + b.uz * tb;
|
|
935
|
+
if (Math.hypot(ep[0] - px, ep[1] - pz) <= JUNCTION_SNAP) {
|
|
936
|
+
splits[j].push(tb);
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
for (const ep of [b.start, b.end]) {
|
|
941
|
+
const ta = (ep[0] - a.start[0]) * a.ux + (ep[1] - a.start[1]) * a.uz;
|
|
942
|
+
if (ta >= 0 && ta <= a.lengthM) {
|
|
943
|
+
const px = a.start[0] + a.ux * ta;
|
|
944
|
+
const pz = a.start[1] + a.uz * ta;
|
|
945
|
+
if (Math.hypot(ep[0] - px, ep[1] - pz) <= JUNCTION_SNAP) {
|
|
946
|
+
splits[i].push(ta);
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
const nodes2D = [];
|
|
953
|
+
const outgoing = [];
|
|
954
|
+
const VERTEX_MERGE_EPS = 0.2;
|
|
955
|
+
const findOrAddVertex = (x, z) => {
|
|
956
|
+
for (let idx = 0;idx < nodes2D.length; idx++) {
|
|
957
|
+
const v = nodes2D[idx];
|
|
958
|
+
if (Math.hypot(v.x - x, v.z - z) <= VERTEX_MERGE_EPS)
|
|
959
|
+
return idx;
|
|
960
|
+
}
|
|
961
|
+
nodes2D.push({ x, z });
|
|
962
|
+
outgoing.push([]);
|
|
963
|
+
return nodes2D.length - 1;
|
|
964
|
+
};
|
|
965
|
+
const halfEdges = [];
|
|
966
|
+
const seenUndirected = new Set;
|
|
967
|
+
for (let i = 0;i < validWalls.length; i++) {
|
|
968
|
+
const w = validWalls[i];
|
|
969
|
+
const ts = splits[i].sort((a, b) => a - b);
|
|
970
|
+
let prevV = -1;
|
|
971
|
+
for (const t of ts) {
|
|
972
|
+
const vIdx = findOrAddVertex(w.start[0] + w.ux * t, w.start[1] + w.uz * t);
|
|
973
|
+
if (prevV !== -1 && prevV !== vIdx) {
|
|
974
|
+
const u = prevV;
|
|
975
|
+
const v = vIdx;
|
|
976
|
+
const key = u < v ? `${u}:${v}` : `${v}:${u}`;
|
|
977
|
+
if (!seenUndirected.has(key)) {
|
|
978
|
+
const nu = nodes2D[u];
|
|
979
|
+
const nv = nodes2D[v];
|
|
980
|
+
if (Math.hypot(nv.x - nu.x, nv.z - nu.z) >= 0.0001) {
|
|
981
|
+
seenUndirected.add(key);
|
|
982
|
+
const id1 = halfEdges.length;
|
|
983
|
+
const id2 = id1 + 1;
|
|
984
|
+
halfEdges.push({
|
|
985
|
+
id: id1,
|
|
986
|
+
from: u,
|
|
987
|
+
to: v,
|
|
988
|
+
twin: id2,
|
|
989
|
+
angle: Math.atan2(nv.z - nu.z, nv.x - nu.x)
|
|
990
|
+
});
|
|
991
|
+
halfEdges.push({
|
|
992
|
+
id: id2,
|
|
993
|
+
from: v,
|
|
994
|
+
to: u,
|
|
995
|
+
twin: id1,
|
|
996
|
+
angle: Math.atan2(nu.z - nv.z, nu.x - nv.x)
|
|
997
|
+
});
|
|
998
|
+
outgoing[u].push(id1);
|
|
999
|
+
outgoing[v].push(id2);
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
prevV = vIdx;
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
const faces = [];
|
|
1007
|
+
const faceAabbs = [];
|
|
1008
|
+
if (halfEdges.length > 0) {
|
|
1009
|
+
const outIndex = new Int32Array(halfEdges.length);
|
|
1010
|
+
for (let v = 0;v < outgoing.length; v++) {
|
|
1011
|
+
const list = outgoing[v];
|
|
1012
|
+
list.sort((a, b) => halfEdges[a].angle - halfEdges[b].angle);
|
|
1013
|
+
for (let k = 0;k < list.length; k++) {
|
|
1014
|
+
outIndex[list[k]] = k;
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
const visited = new Uint8Array(halfEdges.length);
|
|
1018
|
+
const maxSteps = halfEdges.length + 2;
|
|
1019
|
+
for (let i = 0;i < halfEdges.length; i++) {
|
|
1020
|
+
if (visited[i])
|
|
1021
|
+
continue;
|
|
1022
|
+
const poly = [];
|
|
1023
|
+
let cur = i;
|
|
1024
|
+
let steps = 0;
|
|
1025
|
+
while (!visited[cur] && steps < maxSteps) {
|
|
1026
|
+
visited[cur] = 1;
|
|
1027
|
+
const he = halfEdges[cur];
|
|
1028
|
+
const nv = nodes2D[he.from];
|
|
1029
|
+
poly.push([nv.x, nv.z]);
|
|
1030
|
+
const nextList = outgoing[he.to];
|
|
1031
|
+
const deg = nextList.length;
|
|
1032
|
+
if (deg === 0)
|
|
1033
|
+
break;
|
|
1034
|
+
const twinPos = outIndex[he.twin];
|
|
1035
|
+
cur = nextList[(twinPos - 1 + deg) % deg];
|
|
1036
|
+
steps += 1;
|
|
1037
|
+
}
|
|
1038
|
+
if (cur !== i || poly.length < 3)
|
|
1039
|
+
continue;
|
|
1040
|
+
let area2 = 0;
|
|
1041
|
+
let fMinX = Number.POSITIVE_INFINITY;
|
|
1042
|
+
let fMaxX = Number.NEGATIVE_INFINITY;
|
|
1043
|
+
let fMinZ = Number.POSITIVE_INFINITY;
|
|
1044
|
+
let fMaxZ = Number.NEGATIVE_INFINITY;
|
|
1045
|
+
for (let k = 0;k < poly.length; k++) {
|
|
1046
|
+
const p1 = poly[k];
|
|
1047
|
+
const p2 = poly[(k + 1) % poly.length];
|
|
1048
|
+
area2 += p1[0] * p2[1] - p2[0] * p1[1];
|
|
1049
|
+
if (p1[0] < fMinX)
|
|
1050
|
+
fMinX = p1[0];
|
|
1051
|
+
if (p1[0] > fMaxX)
|
|
1052
|
+
fMaxX = p1[0];
|
|
1053
|
+
if (p1[1] < fMinZ)
|
|
1054
|
+
fMinZ = p1[1];
|
|
1055
|
+
if (p1[1] > fMaxZ)
|
|
1056
|
+
fMaxZ = p1[1];
|
|
1057
|
+
}
|
|
1058
|
+
if (area2 > 0.2) {
|
|
1059
|
+
faces.push(poly);
|
|
1060
|
+
faceAabbs.push({ minX: fMinX, maxX: fMaxX, minZ: fMinZ, maxZ: fMaxZ });
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
let minX = Number.POSITIVE_INFINITY;
|
|
1065
|
+
let maxX = Number.NEGATIVE_INFINITY;
|
|
1066
|
+
let minZ = Number.POSITIVE_INFINITY;
|
|
1067
|
+
let maxZ = Number.NEGATIVE_INFINITY;
|
|
1068
|
+
let totalWallLen = 0;
|
|
1069
|
+
let coveredWallLen = 0;
|
|
1070
|
+
for (const w of validWalls) {
|
|
1071
|
+
for (const pt of [w.start, w.end]) {
|
|
1072
|
+
if (pt[0] < minX)
|
|
1073
|
+
minX = pt[0];
|
|
1074
|
+
if (pt[0] > maxX)
|
|
1075
|
+
maxX = pt[0];
|
|
1076
|
+
if (pt[1] < minZ)
|
|
1077
|
+
minZ = pt[1];
|
|
1078
|
+
if (pt[1] > maxZ)
|
|
1079
|
+
maxZ = pt[1];
|
|
1080
|
+
}
|
|
1081
|
+
if (w.lengthM < 0.05)
|
|
1082
|
+
continue;
|
|
1083
|
+
totalWallLen += w.lengthM;
|
|
1084
|
+
if (faces.length > 0) {
|
|
1085
|
+
const mx = (w.start[0] + w.end[0]) / 2;
|
|
1086
|
+
const mz = (w.start[1] + w.end[1]) / 2;
|
|
1087
|
+
const probe = w.thicknessM / 2 + 0.2;
|
|
1088
|
+
const pxPos = mx + w.nx * probe;
|
|
1089
|
+
const pzPos = mz + w.nz * probe;
|
|
1090
|
+
const pxNeg = mx - w.nx * probe;
|
|
1091
|
+
const pzNeg = mz - w.nz * probe;
|
|
1092
|
+
let touchesFace = false;
|
|
1093
|
+
for (let fi = 0;fi < faces.length; fi++) {
|
|
1094
|
+
const fb = faceAabbs[fi];
|
|
1095
|
+
const face = faces[fi];
|
|
1096
|
+
if (pxPos >= fb.minX && pxPos <= fb.maxX && pzPos >= fb.minZ && pzPos <= fb.maxZ && pointInPolygon2D2(pxPos, pzPos, face)) {
|
|
1097
|
+
touchesFace = true;
|
|
1098
|
+
break;
|
|
1099
|
+
}
|
|
1100
|
+
if (pxNeg >= fb.minX && pxNeg <= fb.maxX && pzNeg >= fb.minZ && pzNeg <= fb.maxZ && pointInPolygon2D2(pxNeg, pzNeg, face)) {
|
|
1101
|
+
touchesFace = true;
|
|
1102
|
+
break;
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
if (touchesFace)
|
|
1106
|
+
coveredWallLen += w.lengthM;
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
const complete = faces.length > 0 && totalWallLen > 0 && coveredWallLen / totalWallLen >= 0.72;
|
|
1110
|
+
const aabb = Number.isFinite(minX) && Number.isFinite(minZ) ? { minX, maxX, minZ, maxZ } : null;
|
|
1111
|
+
return { faces, complete, aabb };
|
|
1112
|
+
}
|
|
1113
|
+
function isPointEnclosedByWalls2(px, pz, walls) {
|
|
1114
|
+
if (walls.length < 3)
|
|
1115
|
+
return false;
|
|
1116
|
+
const RAYS = 16;
|
|
1117
|
+
const phase = 0.037;
|
|
1118
|
+
for (let k = 0;k < RAYS; k++) {
|
|
1119
|
+
const ang = phase + k * Math.PI * 2 / RAYS;
|
|
1120
|
+
const rdx = Math.cos(ang);
|
|
1121
|
+
const rdz = Math.sin(ang);
|
|
1122
|
+
let hit = false;
|
|
1123
|
+
for (let i = 0;i < walls.length; i++) {
|
|
1124
|
+
const w = walls[i];
|
|
1125
|
+
const ax = w.start[0];
|
|
1126
|
+
const az = w.start[1];
|
|
1127
|
+
const ex = w.end[0] - ax;
|
|
1128
|
+
const ez = w.end[1] - az;
|
|
1129
|
+
const denom = rdx * ez - rdz * ex;
|
|
1130
|
+
if (Math.abs(denom) <= 0.0000000001)
|
|
1131
|
+
continue;
|
|
1132
|
+
const t = ((ax - px) * ez - (az - pz) * ex) / denom;
|
|
1133
|
+
if (t <= 0.0001)
|
|
1134
|
+
continue;
|
|
1135
|
+
const u = ((ax - px) * rdz - (az - pz) * rdx) / denom;
|
|
1136
|
+
const ext = Math.min(0.25, 0.15 / Math.max(0.25, w.lengthM));
|
|
1137
|
+
if (u >= -ext && u <= 1 + ext) {
|
|
1138
|
+
hit = true;
|
|
1139
|
+
break;
|
|
1140
|
+
}
|
|
1141
|
+
}
|
|
1142
|
+
if (!hit)
|
|
1143
|
+
return false;
|
|
1144
|
+
}
|
|
1145
|
+
return true;
|
|
1146
|
+
}
|
|
1147
|
+
function buildBaseRoomPolygonsFromNodes(roomNodes) {
|
|
1148
|
+
const rooms = [];
|
|
1149
|
+
for (const n of roomNodes) {
|
|
1150
|
+
if (!Array.isArray(n.polygon) || n.polygon.length < 3)
|
|
1151
|
+
continue;
|
|
1152
|
+
const poly = [];
|
|
1153
|
+
for (const pt of n.polygon) {
|
|
1154
|
+
if (!Array.isArray(pt) || pt.length < 2)
|
|
1155
|
+
continue;
|
|
1156
|
+
const x = Number(pt[0]);
|
|
1157
|
+
const z = Number(pt[1]);
|
|
1158
|
+
if (Number.isFinite(x) && Number.isFinite(z)) {
|
|
1159
|
+
poly.push([x, z]);
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
if (poly.length < 3)
|
|
1163
|
+
continue;
|
|
1164
|
+
const bounds = polygonBounds2D2(poly);
|
|
1165
|
+
if (!bounds)
|
|
1166
|
+
continue;
|
|
1167
|
+
const { areaSqM, centroid } = polygonAreaAndCentroid2(poly);
|
|
1168
|
+
const name = typeof n.name === "string" && n.name.trim().length > 0 ? n.name.trim() : n.id;
|
|
1169
|
+
const rawCategory = typeof n.category === "string" && n.category.trim().length > 0 ? n.category.trim() : typeof n.roomCategory === "string" && n.roomCategory.trim().length > 0 ? n.roomCategory.trim() : "";
|
|
1170
|
+
const normalizedText = normalizeTextNfd(rawCategory ? `${name} ${rawCategory}` : name);
|
|
1171
|
+
rooms.push({
|
|
1172
|
+
id: n.id,
|
|
1173
|
+
name,
|
|
1174
|
+
rawCategory,
|
|
1175
|
+
normalizedText,
|
|
1176
|
+
polygon: poly,
|
|
1177
|
+
areaSqM,
|
|
1178
|
+
areaSqFt: areaSqM * SQM_TO_SQFT2,
|
|
1179
|
+
centroid,
|
|
1180
|
+
bounds
|
|
1181
|
+
});
|
|
1182
|
+
}
|
|
1183
|
+
return rooms;
|
|
1184
|
+
}
|
|
1185
|
+
function extractBaseRoomPolygons2(nodes, levelId, opts) {
|
|
1186
|
+
const includeHidden = opts?.includeHidden ?? false;
|
|
1187
|
+
const includeUnparented = opts?.includeUnparented ?? false;
|
|
1188
|
+
const roomNodes = [];
|
|
1189
|
+
for (const n of Object.values(nodes)) {
|
|
1190
|
+
if (n.type !== "zone" && n.type !== "room")
|
|
1191
|
+
continue;
|
|
1192
|
+
if (!includeHidden && n.visible === false)
|
|
1193
|
+
continue;
|
|
1194
|
+
if (levelId && !belongsToLevel2(nodes, n, levelId, { includeUnparented }))
|
|
1195
|
+
continue;
|
|
1196
|
+
roomNodes.push(n);
|
|
1197
|
+
}
|
|
1198
|
+
return buildBaseRoomPolygonsFromNodes(roomNodes);
|
|
1199
|
+
}
|
|
1200
|
+
function mergeConnectedBoxes(boxes, gapToleranceM) {
|
|
1201
|
+
const n = boxes.length;
|
|
1202
|
+
if (n <= 1)
|
|
1203
|
+
return boxes.map((b) => ({ ...b }));
|
|
1204
|
+
const used = new Uint8Array(n);
|
|
1205
|
+
const clusters = [];
|
|
1206
|
+
for (let i = 0;i < n; i++) {
|
|
1207
|
+
if (used[i])
|
|
1208
|
+
continue;
|
|
1209
|
+
used[i] = 1;
|
|
1210
|
+
const cur = { ...boxes[i] };
|
|
1211
|
+
let grew = true;
|
|
1212
|
+
while (grew) {
|
|
1213
|
+
grew = false;
|
|
1214
|
+
for (let j = i + 1;j < n; j++) {
|
|
1215
|
+
if (used[j])
|
|
1216
|
+
continue;
|
|
1217
|
+
const b = boxes[j];
|
|
1218
|
+
if (cur.minX <= b.maxX + gapToleranceM && cur.maxX >= b.minX - gapToleranceM && cur.minZ <= b.maxZ + gapToleranceM && cur.maxZ >= b.minZ - gapToleranceM) {
|
|
1219
|
+
if (b.minX < cur.minX)
|
|
1220
|
+
cur.minX = b.minX;
|
|
1221
|
+
if (b.maxX > cur.maxX)
|
|
1222
|
+
cur.maxX = b.maxX;
|
|
1223
|
+
if (b.minZ < cur.minZ)
|
|
1224
|
+
cur.minZ = b.minZ;
|
|
1225
|
+
if (b.maxZ > cur.maxZ)
|
|
1226
|
+
cur.maxZ = b.maxZ;
|
|
1227
|
+
used[j] = 1;
|
|
1228
|
+
grew = true;
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
for (let k = clusters.length - 1;k >= 0; k--) {
|
|
1232
|
+
const prev = clusters[k];
|
|
1233
|
+
if (cur.minX <= prev.maxX + gapToleranceM && cur.maxX >= prev.minX - gapToleranceM && cur.minZ <= prev.maxZ + gapToleranceM && cur.maxZ >= prev.minZ - gapToleranceM) {
|
|
1234
|
+
if (prev.minX < cur.minX)
|
|
1235
|
+
cur.minX = prev.minX;
|
|
1236
|
+
if (prev.maxX > cur.maxX)
|
|
1237
|
+
cur.maxX = prev.maxX;
|
|
1238
|
+
if (prev.minZ < cur.minZ)
|
|
1239
|
+
cur.minZ = prev.minZ;
|
|
1240
|
+
if (prev.maxZ > cur.maxZ)
|
|
1241
|
+
cur.maxZ = prev.maxZ;
|
|
1242
|
+
const last = clusters.length - 1;
|
|
1243
|
+
if (k !== last)
|
|
1244
|
+
clusters[k] = clusters[last];
|
|
1245
|
+
clusters.pop();
|
|
1246
|
+
grew = true;
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
}
|
|
1250
|
+
clusters.push(cur);
|
|
1251
|
+
}
|
|
1252
|
+
return clusters;
|
|
1253
|
+
}
|
|
1254
|
+
function computeLevelBounds2(nodes, levelId) {
|
|
1255
|
+
let minX = Number.POSITIVE_INFINITY;
|
|
1256
|
+
let maxX = Number.NEGATIVE_INFINITY;
|
|
1257
|
+
let minZ = Number.POSITIVE_INFINITY;
|
|
1258
|
+
let maxZ = Number.NEGATIVE_INFINITY;
|
|
1259
|
+
for (const n of Object.values(nodes)) {
|
|
1260
|
+
if (n.visible === false)
|
|
1261
|
+
continue;
|
|
1262
|
+
if (!belongsToLevel2(nodes, n, levelId))
|
|
1263
|
+
continue;
|
|
1264
|
+
if (n.type === "wall" && Array.isArray(n.start) && Array.isArray(n.end)) {
|
|
1265
|
+
const sx = Number(n.start[0]);
|
|
1266
|
+
const sz = Number(n.start[1]);
|
|
1267
|
+
const ex = Number(n.end[0]);
|
|
1268
|
+
const ez = Number(n.end[1]);
|
|
1269
|
+
if (Number.isFinite(sx) && Number.isFinite(sz) && Number.isFinite(ex) && Number.isFinite(ez)) {
|
|
1270
|
+
if (sx < minX)
|
|
1271
|
+
minX = sx;
|
|
1272
|
+
if (ex < minX)
|
|
1273
|
+
minX = ex;
|
|
1274
|
+
if (sx > maxX)
|
|
1275
|
+
maxX = sx;
|
|
1276
|
+
if (ex > maxX)
|
|
1277
|
+
maxX = ex;
|
|
1278
|
+
if (sz < minZ)
|
|
1279
|
+
minZ = sz;
|
|
1280
|
+
if (ez < minZ)
|
|
1281
|
+
minZ = ez;
|
|
1282
|
+
if (sz > maxZ)
|
|
1283
|
+
maxZ = sz;
|
|
1284
|
+
if (ez > maxZ)
|
|
1285
|
+
maxZ = ez;
|
|
1286
|
+
}
|
|
1287
|
+
} else if (n.type === "slab" && Array.isArray(n.polygon)) {
|
|
1288
|
+
for (const pt of n.polygon) {
|
|
1289
|
+
if (!Array.isArray(pt) || pt.length < 2)
|
|
1290
|
+
continue;
|
|
1291
|
+
const x = Number(pt[0]);
|
|
1292
|
+
const z = Number(pt[1]);
|
|
1293
|
+
if (Number.isFinite(x) && Number.isFinite(z)) {
|
|
1294
|
+
if (x < minX)
|
|
1295
|
+
minX = x;
|
|
1296
|
+
if (x > maxX)
|
|
1297
|
+
maxX = x;
|
|
1298
|
+
if (z < minZ)
|
|
1299
|
+
minZ = z;
|
|
1300
|
+
if (z > maxZ)
|
|
1301
|
+
maxZ = z;
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
1304
|
+
}
|
|
1305
|
+
}
|
|
1306
|
+
if (!Number.isFinite(minX) || !Number.isFinite(minZ))
|
|
1307
|
+
return null;
|
|
1308
|
+
const widthM = Math.max(0, maxX - minX);
|
|
1309
|
+
const depthM = Math.max(0, maxZ - minZ);
|
|
1310
|
+
const heightM = resolveLevelHeightM2(nodes, levelId);
|
|
1311
|
+
return {
|
|
1312
|
+
minX,
|
|
1313
|
+
maxX,
|
|
1314
|
+
minZ,
|
|
1315
|
+
maxZ,
|
|
1316
|
+
widthM,
|
|
1317
|
+
depthM,
|
|
1318
|
+
heightM,
|
|
1319
|
+
centerX: (minX + maxX) / 2,
|
|
1320
|
+
centerZ: (minZ + maxZ) / 2
|
|
1321
|
+
};
|
|
1322
|
+
}
|
|
1323
|
+
function clusterLevelFootprintsFromNodes(nodes, levelId, walls, slabs, gapToleranceM, precomputedBounds) {
|
|
1324
|
+
const slabBoxes = [];
|
|
1325
|
+
for (const slab of slabs) {
|
|
1326
|
+
const b = polygonBounds2D2(slab.polygon);
|
|
1327
|
+
if (!b)
|
|
1328
|
+
continue;
|
|
1329
|
+
if (b.maxX - b.minX > 0.5 && b.maxZ - b.minZ > 0.5) {
|
|
1330
|
+
const elev = typeof slab.elevation === "number" && Number.isFinite(slab.elevation) ? slab.elevation : 0;
|
|
1331
|
+
slabBoxes.push({ id: slab.id, ...b, elev });
|
|
1332
|
+
}
|
|
1333
|
+
}
|
|
1334
|
+
const seedBoxes = slabBoxes.map((b) => ({ minX: b.minX, maxX: b.maxX, minZ: b.minZ, maxZ: b.maxZ }));
|
|
1335
|
+
const wallBoxes = [];
|
|
1336
|
+
for (const w of walls) {
|
|
1337
|
+
const sx = Number(w.start[0]);
|
|
1338
|
+
const sz = Number(w.start[1]);
|
|
1339
|
+
const ex = Number(w.end[0]);
|
|
1340
|
+
const ez = Number(w.end[1]);
|
|
1341
|
+
if (!Number.isFinite(sx) || !Number.isFinite(sz) || !Number.isFinite(ex) || !Number.isFinite(ez)) {
|
|
1342
|
+
continue;
|
|
1343
|
+
}
|
|
1344
|
+
const wb = {
|
|
1345
|
+
id: w.id,
|
|
1346
|
+
minX: Math.min(sx, ex),
|
|
1347
|
+
maxX: Math.max(sx, ex),
|
|
1348
|
+
minZ: Math.min(sz, ez),
|
|
1349
|
+
maxZ: Math.max(sz, ez),
|
|
1350
|
+
height: resolveWallHeightM2(nodes, w, 2.55),
|
|
1351
|
+
elev: typeof w.elevation === "number" && Number.isFinite(w.elevation) ? w.elevation : 0
|
|
1352
|
+
};
|
|
1353
|
+
wallBoxes.push(wb);
|
|
1354
|
+
const touchesSlab = slabBoxes.some((sb) => wb.minX <= sb.maxX + gapToleranceM && wb.maxX >= sb.minX - gapToleranceM && wb.minZ <= sb.maxZ + gapToleranceM && wb.maxZ >= sb.minZ - gapToleranceM);
|
|
1355
|
+
if (!touchesSlab) {
|
|
1356
|
+
seedBoxes.push({ minX: wb.minX, maxX: wb.maxX, minZ: wb.minZ, maxZ: wb.maxZ });
|
|
1357
|
+
}
|
|
1358
|
+
}
|
|
1359
|
+
const merged = mergeConnectedBoxes(seedBoxes, gapToleranceM).filter((c) => c.maxX - c.minX > 0.5 && c.maxZ - c.minZ > 0.5);
|
|
1360
|
+
if (merged.length === 0) {
|
|
1361
|
+
const b = precomputedBounds !== undefined ? precomputedBounds : computeLevelBounds2(nodes, levelId);
|
|
1362
|
+
if (!b)
|
|
1363
|
+
return [];
|
|
1364
|
+
const widthM = Math.max(0, b.maxX - b.minX);
|
|
1365
|
+
const depthM = Math.max(0, b.maxZ - b.minZ);
|
|
1366
|
+
if (widthM <= 0.5 || depthM <= 0.5)
|
|
1367
|
+
return [];
|
|
1368
|
+
const fallbackH = "heightM" in b && typeof b.heightM === "number" ? b.heightM : resolveLevelHeightM2(nodes, levelId);
|
|
1369
|
+
const globalAvgH = wallBoxes.length > 0 ? wallBoxes.reduce((s, w) => s + w.height, 0) / wallBoxes.length : fallbackH;
|
|
1370
|
+
return [
|
|
1371
|
+
{
|
|
1372
|
+
minX: b.minX,
|
|
1373
|
+
maxX: b.maxX,
|
|
1374
|
+
minZ: b.minZ,
|
|
1375
|
+
maxZ: b.maxZ,
|
|
1376
|
+
widthM,
|
|
1377
|
+
depthM,
|
|
1378
|
+
areaSqM: widthM * depthM,
|
|
1379
|
+
centerX: (b.minX + b.maxX) / 2,
|
|
1380
|
+
centerZ: (b.minZ + b.maxZ) / 2,
|
|
1381
|
+
avgWallHeightM: globalAvgH,
|
|
1382
|
+
baseElevationM: 0,
|
|
1383
|
+
slabIds: slabBoxes.map((s) => s.id),
|
|
1384
|
+
wallIds: wallBoxes.map((w) => w.id)
|
|
1385
|
+
}
|
|
1386
|
+
];
|
|
1387
|
+
}
|
|
1388
|
+
const globalAvgH = wallBoxes.length > 0 ? wallBoxes.reduce((s, w) => s + w.height, 0) / wallBoxes.length : 2.55;
|
|
1389
|
+
const clusters = merged.map((c) => {
|
|
1390
|
+
let hSum = 0;
|
|
1391
|
+
let hCount = 0;
|
|
1392
|
+
let minElev = Number.POSITIVE_INFINITY;
|
|
1393
|
+
const wallIds = [];
|
|
1394
|
+
const slabIds = [];
|
|
1395
|
+
for (const wb of wallBoxes) {
|
|
1396
|
+
if (wb.minX <= c.maxX + gapToleranceM && wb.maxX >= c.minX - gapToleranceM && wb.minZ <= c.maxZ + gapToleranceM && wb.maxZ >= c.minZ - gapToleranceM) {
|
|
1397
|
+
wallIds.push(wb.id);
|
|
1398
|
+
hSum += wb.height;
|
|
1399
|
+
hCount += 1;
|
|
1400
|
+
if (wb.elev < minElev)
|
|
1401
|
+
minElev = wb.elev;
|
|
1402
|
+
}
|
|
1403
|
+
}
|
|
1404
|
+
for (const sb of slabBoxes) {
|
|
1405
|
+
if (sb.minX <= c.maxX + gapToleranceM && sb.maxX >= c.minX - gapToleranceM && sb.minZ <= c.maxZ + gapToleranceM && sb.maxZ >= c.minZ - gapToleranceM) {
|
|
1406
|
+
slabIds.push(sb.id);
|
|
1407
|
+
if (sb.elev < minElev)
|
|
1408
|
+
minElev = sb.elev;
|
|
1409
|
+
}
|
|
1410
|
+
}
|
|
1411
|
+
const widthM = c.maxX - c.minX;
|
|
1412
|
+
const depthM = c.maxZ - c.minZ;
|
|
1413
|
+
return {
|
|
1414
|
+
minX: c.minX,
|
|
1415
|
+
maxX: c.maxX,
|
|
1416
|
+
minZ: c.minZ,
|
|
1417
|
+
maxZ: c.maxZ,
|
|
1418
|
+
widthM,
|
|
1419
|
+
depthM,
|
|
1420
|
+
areaSqM: widthM * depthM,
|
|
1421
|
+
centerX: (c.minX + c.maxX) / 2,
|
|
1422
|
+
centerZ: (c.minZ + c.maxZ) / 2,
|
|
1423
|
+
avgWallHeightM: hCount > 0 ? hSum / hCount : globalAvgH,
|
|
1424
|
+
baseElevationM: Number.isFinite(minElev) ? minElev : 0,
|
|
1425
|
+
slabIds,
|
|
1426
|
+
wallIds
|
|
1427
|
+
};
|
|
1428
|
+
});
|
|
1429
|
+
clusters.sort((a, b) => b.areaSqM - a.areaSqM || a.minX - b.minX || a.minZ - b.minZ);
|
|
1430
|
+
return clusters;
|
|
1431
|
+
}
|
|
1432
|
+
function clusterLevelFootprints2(nodes, levelId, gapToleranceM = 0.35) {
|
|
1433
|
+
const walls = [];
|
|
1434
|
+
const slabs = [];
|
|
1435
|
+
for (const n of Object.values(nodes)) {
|
|
1436
|
+
if (n.visible === false)
|
|
1437
|
+
continue;
|
|
1438
|
+
if (!belongsToLevel2(nodes, n, levelId))
|
|
1439
|
+
continue;
|
|
1440
|
+
if (n.type === "wall" && Array.isArray(n.start) && Array.isArray(n.end)) {
|
|
1441
|
+
walls.push(n);
|
|
1442
|
+
} else if (n.type === "slab" && Array.isArray(n.polygon) && n.polygon.length >= 3) {
|
|
1443
|
+
slabs.push(n);
|
|
1444
|
+
}
|
|
1445
|
+
}
|
|
1446
|
+
return clusterLevelFootprintsFromNodes(nodes, levelId, walls, slabs, gapToleranceM);
|
|
1447
|
+
}
|
|
1448
|
+
function isOutdoorOrUnwalledSlab2(slab, walls, outdoorZonePolys = [], baseWallElev = 0) {
|
|
1449
|
+
const rawLabel = `${String(slab.name ?? "")} ${String(slab.category ?? "")} ${String(slab.role ?? "")} ${String(slab.material ?? "")}`;
|
|
1450
|
+
if (OUTDOOR_SLAB_RE.test(normalizeTextNfd(rawLabel)))
|
|
1451
|
+
return true;
|
|
1452
|
+
if (!Array.isArray(slab.polygon) || slab.polygon.length < 3)
|
|
1453
|
+
return true;
|
|
1454
|
+
const poly = slab.polygon;
|
|
1455
|
+
const b = polygonBounds2D2(poly);
|
|
1456
|
+
if (!b)
|
|
1457
|
+
return true;
|
|
1458
|
+
let cx = 0;
|
|
1459
|
+
let cz = 0;
|
|
1460
|
+
for (const pt of poly) {
|
|
1461
|
+
cx += pt[0];
|
|
1462
|
+
cz += pt[1];
|
|
1463
|
+
}
|
|
1464
|
+
cx /= poly.length;
|
|
1465
|
+
cz /= poly.length;
|
|
1466
|
+
const samples = [];
|
|
1467
|
+
if (pointInPolygon2D2(cx, cz, poly, 0))
|
|
1468
|
+
samples.push([cx, cz]);
|
|
1469
|
+
const gridN = 5;
|
|
1470
|
+
for (let ix = 0;ix < gridN; ix++) {
|
|
1471
|
+
const sx = b.minX + (b.maxX - b.minX) * (ix + 0.5) / gridN;
|
|
1472
|
+
for (let iz = 0;iz < gridN; iz++) {
|
|
1473
|
+
const sz = b.minZ + (b.maxZ - b.minZ) * (iz + 0.5) / gridN;
|
|
1474
|
+
if (pointInPolygon2D2(sx, sz, poly, 0))
|
|
1475
|
+
samples.push([sx, sz]);
|
|
1476
|
+
}
|
|
1477
|
+
}
|
|
1478
|
+
if (samples.length === 0)
|
|
1479
|
+
samples.push([cx, cz]);
|
|
1480
|
+
if (outdoorZonePolys.length > 0) {
|
|
1481
|
+
let outdoorCount = 0;
|
|
1482
|
+
for (const [sx, sz] of samples) {
|
|
1483
|
+
if (outdoorZonePolys.some((zp) => pointInPolygon2D2(sx, sz, zp, 0.02)))
|
|
1484
|
+
outdoorCount += 1;
|
|
1485
|
+
}
|
|
1486
|
+
if (outdoorCount / samples.length >= 0.5)
|
|
1487
|
+
return true;
|
|
1488
|
+
}
|
|
1489
|
+
if (walls.length >= 3) {
|
|
1490
|
+
let enclosed = 0;
|
|
1491
|
+
for (const [sx, sz] of samples) {
|
|
1492
|
+
if (isPointEnclosedByWalls2(sx, sz, walls))
|
|
1493
|
+
enclosed += 1;
|
|
1494
|
+
}
|
|
1495
|
+
const enclosedRatio = enclosed / samples.length;
|
|
1496
|
+
const area = polygonArea2D2(poly);
|
|
1497
|
+
const elev = typeof slab.elevation === "number" && Number.isFinite(slab.elevation) ? slab.elevation : 0;
|
|
1498
|
+
if (area < 4.5 && elev > baseWallElev + 0.12 && enclosedRatio < 0.5)
|
|
1499
|
+
return true;
|
|
1500
|
+
if (enclosedRatio < 0.35)
|
|
1501
|
+
return true;
|
|
1502
|
+
}
|
|
1503
|
+
return false;
|
|
1504
|
+
}
|
|
1505
|
+
function compileLevelFootprint2(nodes, levelId = null, walls) {
|
|
1506
|
+
const effectiveWalls = walls ?? extractCanonicalWalls2(nodes, levelId);
|
|
1507
|
+
const { faces: boundedFaces, complete: boundedFacesComplete, aabb: wallAabb } = extractBoundedWallFaces2(effectiveWalls);
|
|
1508
|
+
const roomNodes = [];
|
|
1509
|
+
const visibleWallNodes = [];
|
|
1510
|
+
const visibleSlabNodes = [];
|
|
1511
|
+
let bMinX = Number.POSITIVE_INFINITY;
|
|
1512
|
+
let bMaxX = Number.NEGATIVE_INFINITY;
|
|
1513
|
+
let bMinZ = Number.POSITIVE_INFINITY;
|
|
1514
|
+
let bMaxZ = Number.NEGATIVE_INFINITY;
|
|
1515
|
+
for (const n of Object.values(nodes)) {
|
|
1516
|
+
if (n.visible === false)
|
|
1517
|
+
continue;
|
|
1518
|
+
if (levelId && !belongsToLevel2(nodes, n, levelId))
|
|
1519
|
+
continue;
|
|
1520
|
+
if (n.type === "zone" || n.type === "room") {
|
|
1521
|
+
roomNodes.push(n);
|
|
1522
|
+
} else if (n.type === "wall" && Array.isArray(n.start) && Array.isArray(n.end)) {
|
|
1523
|
+
visibleWallNodes.push(n);
|
|
1524
|
+
const sx = Number(n.start[0]);
|
|
1525
|
+
const sz = Number(n.start[1]);
|
|
1526
|
+
const ex = Number(n.end[0]);
|
|
1527
|
+
const ez = Number(n.end[1]);
|
|
1528
|
+
if (Number.isFinite(sx) && Number.isFinite(sz) && Number.isFinite(ex) && Number.isFinite(ez)) {
|
|
1529
|
+
if (sx < bMinX)
|
|
1530
|
+
bMinX = sx;
|
|
1531
|
+
if (ex < bMinX)
|
|
1532
|
+
bMinX = ex;
|
|
1533
|
+
if (sx > bMaxX)
|
|
1534
|
+
bMaxX = sx;
|
|
1535
|
+
if (ex > bMaxX)
|
|
1536
|
+
bMaxX = ex;
|
|
1537
|
+
if (sz < bMinZ)
|
|
1538
|
+
bMinZ = sz;
|
|
1539
|
+
if (ez < bMinZ)
|
|
1540
|
+
bMinZ = ez;
|
|
1541
|
+
if (sz > bMaxZ)
|
|
1542
|
+
bMaxZ = sz;
|
|
1543
|
+
if (ez > bMaxZ)
|
|
1544
|
+
bMaxZ = ez;
|
|
1545
|
+
}
|
|
1546
|
+
} else if (n.type === "slab" && Array.isArray(n.polygon)) {
|
|
1547
|
+
if (n.polygon.length >= 3)
|
|
1548
|
+
visibleSlabNodes.push(n);
|
|
1549
|
+
for (const pt of n.polygon) {
|
|
1550
|
+
if (!Array.isArray(pt) || pt.length < 2)
|
|
1551
|
+
continue;
|
|
1552
|
+
const x = Number(pt[0]);
|
|
1553
|
+
const z = Number(pt[1]);
|
|
1554
|
+
if (Number.isFinite(x) && Number.isFinite(z)) {
|
|
1555
|
+
if (x < bMinX)
|
|
1556
|
+
bMinX = x;
|
|
1557
|
+
if (x > bMaxX)
|
|
1558
|
+
bMaxX = x;
|
|
1559
|
+
if (z < bMinZ)
|
|
1560
|
+
bMinZ = z;
|
|
1561
|
+
if (z > bMaxZ)
|
|
1562
|
+
bMaxZ = z;
|
|
1563
|
+
}
|
|
1564
|
+
}
|
|
1565
|
+
}
|
|
1566
|
+
}
|
|
1567
|
+
const levelBounds2D = Number.isFinite(bMinX) && Number.isFinite(bMinZ) ? { minX: bMinX, maxX: bMaxX, minZ: bMinZ, maxZ: bMaxZ } : null;
|
|
1568
|
+
const roomPolygons = buildBaseRoomPolygonsFromNodes(roomNodes);
|
|
1569
|
+
const clusters = clusterLevelFootprintsFromNodes(nodes, levelId, visibleWallNodes, visibleSlabNodes, 0.35, levelBounds2D);
|
|
1570
|
+
const defaultExteriorPolys = roomPolygons.filter((r) => OUTDOOR_ZONE_RE.test(r.normalizedText)).map((r) => r.polygon);
|
|
1571
|
+
const defaultInteriorPolys = roomPolygons.filter((r) => !OUTDOOR_ZONE_RE.test(r.normalizedText)).map((r) => r.polygon);
|
|
1572
|
+
const boundedFaceAabbs = boundedFaces.map((f) => polygonBounds2D2(f));
|
|
1573
|
+
const pointInAnyBoundedFace = (px, pz) => {
|
|
1574
|
+
for (let i = 0;i < boundedFaces.length; i++) {
|
|
1575
|
+
const fb = boundedFaceAabbs[i];
|
|
1576
|
+
if (fb && (px < fb.minX || px > fb.maxX || pz < fb.minZ || pz > fb.maxZ))
|
|
1577
|
+
continue;
|
|
1578
|
+
if (pointInPolygon2D2(px, pz, boundedFaces[i]))
|
|
1579
|
+
return true;
|
|
1580
|
+
}
|
|
1581
|
+
return false;
|
|
1582
|
+
};
|
|
1583
|
+
let bounds = levelBounds2D ?? wallAabb;
|
|
1584
|
+
if (!bounds && roomPolygons.length > 0) {
|
|
1585
|
+
let minX = Number.POSITIVE_INFINITY;
|
|
1586
|
+
let maxX = Number.NEGATIVE_INFINITY;
|
|
1587
|
+
let minZ = Number.POSITIVE_INFINITY;
|
|
1588
|
+
let maxZ = Number.NEGATIVE_INFINITY;
|
|
1589
|
+
for (const r of roomPolygons) {
|
|
1590
|
+
if (r.bounds.minX < minX)
|
|
1591
|
+
minX = r.bounds.minX;
|
|
1592
|
+
if (r.bounds.maxX > maxX)
|
|
1593
|
+
maxX = r.bounds.maxX;
|
|
1594
|
+
if (r.bounds.minZ < minZ)
|
|
1595
|
+
minZ = r.bounds.minZ;
|
|
1596
|
+
if (r.bounds.maxZ > maxZ)
|
|
1597
|
+
maxZ = r.bounds.maxZ;
|
|
1598
|
+
}
|
|
1599
|
+
if (Number.isFinite(minX) && Number.isFinite(minZ)) {
|
|
1600
|
+
bounds = { minX, maxX, minZ, maxZ };
|
|
1601
|
+
}
|
|
1602
|
+
}
|
|
1603
|
+
const isPointInside = (px, pz, opts) => {
|
|
1604
|
+
if (!Number.isFinite(px) || !Number.isFinite(pz))
|
|
1605
|
+
return false;
|
|
1606
|
+
const precedence = opts?.precedence ?? "wall-first";
|
|
1607
|
+
const outdoorPolys = opts?.outdoorPolygons ?? defaultExteriorPolys;
|
|
1608
|
+
const interiorPolys = opts?.interiorPolygons ?? defaultInteriorPolys;
|
|
1609
|
+
if (precedence === "room-first") {
|
|
1610
|
+
const wallTol = opts?.wallToleranceM ?? 0.45;
|
|
1611
|
+
const inInteriorRoom = interiorPolys.some((poly) => poly.length >= 3 && pointInPolygon2D2(px, pz, poly));
|
|
1612
|
+
if (inInteriorRoom)
|
|
1613
|
+
return true;
|
|
1614
|
+
const inExteriorRoom = outdoorPolys.some((poly) => poly.length >= 3 && pointInPolygon2D2(px, pz, poly));
|
|
1615
|
+
if (inExteriorRoom)
|
|
1616
|
+
return false;
|
|
1617
|
+
if (effectiveWalls.length >= 3) {
|
|
1618
|
+
if (wallTol > 0) {
|
|
1619
|
+
const proj = findNearestWallToPoint2(px, pz, effectiveWalls);
|
|
1620
|
+
if (proj && proj.distM <= wallTol)
|
|
1621
|
+
return true;
|
|
1622
|
+
}
|
|
1623
|
+
if (boundedFacesComplete) {
|
|
1624
|
+
return pointInAnyBoundedFace(px, pz);
|
|
1625
|
+
}
|
|
1626
|
+
if (boundedFaces.length > 0 && pointInAnyBoundedFace(px, pz)) {
|
|
1627
|
+
return true;
|
|
1628
|
+
}
|
|
1629
|
+
return isPointEnclosedByWalls2(px, pz, effectiveWalls);
|
|
1630
|
+
}
|
|
1631
|
+
return interiorPolys.length === 0 && outdoorPolys.length === 0;
|
|
1632
|
+
}
|
|
1633
|
+
const wallTol = opts?.wallToleranceM ?? 0.025;
|
|
1634
|
+
if (effectiveWalls.length === 0) {
|
|
1635
|
+
if (outdoorPolys.some((poly) => poly.length >= 3 && pointInPolygon2D2(px, pz, poly))) {
|
|
1636
|
+
return false;
|
|
1637
|
+
}
|
|
1638
|
+
if (interiorPolys.length > 0) {
|
|
1639
|
+
return interiorPolys.some((poly) => poly.length >= 3 && pointInPolygon2D2(px, pz, poly));
|
|
1640
|
+
}
|
|
1641
|
+
return true;
|
|
1642
|
+
}
|
|
1643
|
+
const proj = findNearestWallToPoint2(px, pz, effectiveWalls);
|
|
1644
|
+
if (proj && proj.distM <= proj.wall.thicknessM / 2 + wallTol) {
|
|
1645
|
+
return true;
|
|
1646
|
+
}
|
|
1647
|
+
if (outdoorPolys.some((poly) => poly.length >= 3 && pointInPolygon2D2(px, pz, poly))) {
|
|
1648
|
+
return false;
|
|
1649
|
+
}
|
|
1650
|
+
if (interiorPolys.some((poly) => poly.length >= 3 && pointInPolygon2D2(px, pz, poly))) {
|
|
1651
|
+
return true;
|
|
1652
|
+
}
|
|
1653
|
+
if (boundedFacesComplete) {
|
|
1654
|
+
return pointInAnyBoundedFace(px, pz);
|
|
1655
|
+
}
|
|
1656
|
+
if (boundedFaces.length > 0 && pointInAnyBoundedFace(px, pz)) {
|
|
1657
|
+
return true;
|
|
1658
|
+
}
|
|
1659
|
+
if (isPointEnclosedByWalls2(px, pz, effectiveWalls)) {
|
|
1660
|
+
return true;
|
|
1661
|
+
}
|
|
1662
|
+
if (wallAabb) {
|
|
1663
|
+
return px >= wallAabb.minX - wallTol && px <= wallAabb.maxX + wallTol && pz >= wallAabb.minZ - wallTol && pz <= wallAabb.maxZ + wallTol;
|
|
1664
|
+
}
|
|
1665
|
+
return true;
|
|
1666
|
+
};
|
|
1667
|
+
const isSegmentInside = (a, b, opts) => {
|
|
1668
|
+
if (!isVec32(a) || !isVec32(b))
|
|
1669
|
+
return false;
|
|
1670
|
+
const dx = b[0] - a[0];
|
|
1671
|
+
const dz = b[2] - a[2];
|
|
1672
|
+
const segLenXZ = Math.hypot(dx, dz);
|
|
1673
|
+
if (!Number.isFinite(segLenXZ))
|
|
1674
|
+
return false;
|
|
1675
|
+
const rawSkip = opts?.skipExteriorEndpointMarginM;
|
|
1676
|
+
const skipMargin = typeof rawSkip === "number" && Number.isFinite(rawSkip) ? rawSkip : 0;
|
|
1677
|
+
if (skipMargin > 0) {
|
|
1678
|
+
if (segLenXZ < 0.25)
|
|
1679
|
+
return true;
|
|
1680
|
+
const aInside = isPointInside(a[0], a[2], opts);
|
|
1681
|
+
const bInside = isPointInside(b[0], b[2], opts);
|
|
1682
|
+
const rawStep = opts?.stepM;
|
|
1683
|
+
const stepM = typeof rawStep === "number" && Number.isFinite(rawStep) ? rawStep : 0.5;
|
|
1684
|
+
const steps = Math.max(2, Math.ceil(segLenXZ / Math.max(0.1, stepM)));
|
|
1685
|
+
for (let i = 1;i < steps; i++) {
|
|
1686
|
+
const t = i / steps;
|
|
1687
|
+
if (!aInside && t * segLenXZ <= skipMargin)
|
|
1688
|
+
continue;
|
|
1689
|
+
if (!bInside && (1 - t) * segLenXZ <= skipMargin)
|
|
1690
|
+
continue;
|
|
1691
|
+
if (!isPointInside(a[0] + dx * t, a[2] + dz * t, opts))
|
|
1692
|
+
return false;
|
|
1693
|
+
}
|
|
1694
|
+
return true;
|
|
1695
|
+
}
|
|
1696
|
+
if (segLenXZ < 0.02) {
|
|
1697
|
+
return isPointInside(a[0], a[2], opts);
|
|
1698
|
+
}
|
|
1699
|
+
const rawStep = opts?.stepM;
|
|
1700
|
+
const stepM = typeof rawStep === "number" && Number.isFinite(rawStep) ? rawStep : 0.35;
|
|
1701
|
+
const steps = Math.max(2, Math.min(64, Math.ceil(segLenXZ / Math.max(0.1, stepM))));
|
|
1702
|
+
for (let s = 0;s <= steps; s++) {
|
|
1703
|
+
const f = s / steps;
|
|
1704
|
+
if (!isPointInside(a[0] + dx * f, a[2] + dz * f, opts))
|
|
1705
|
+
return false;
|
|
1706
|
+
}
|
|
1707
|
+
return true;
|
|
1708
|
+
};
|
|
1709
|
+
const pathCrossesOutdoorVoid = (path, opts) => {
|
|
1710
|
+
if (path.length < 2)
|
|
1711
|
+
return false;
|
|
1712
|
+
const effectiveOpts = opts?.skipExteriorEndpointMarginM !== undefined ? opts : {
|
|
1713
|
+
...opts,
|
|
1714
|
+
skipExteriorEndpointMarginM: opts?.precedence === "room-first" ? 0.35 : 0
|
|
1715
|
+
};
|
|
1716
|
+
for (let i = 1;i < path.length; i++) {
|
|
1717
|
+
if (!isSegmentInside(path[i - 1], path[i], effectiveOpts))
|
|
1718
|
+
return true;
|
|
1719
|
+
}
|
|
1720
|
+
return false;
|
|
1721
|
+
};
|
|
1722
|
+
const clampPointInset = (px, pz, insetM = 0.12, opts) => {
|
|
1723
|
+
if (effectiveWalls.length === 0)
|
|
1724
|
+
return [round32(px), round32(pz)];
|
|
1725
|
+
if (isPointInside(px, pz, opts)) {
|
|
1726
|
+
return [round32(px), round32(pz)];
|
|
1727
|
+
}
|
|
1728
|
+
const proj = findNearestWallToPoint2(px, pz, effectiveWalls);
|
|
1729
|
+
if (!proj)
|
|
1730
|
+
return [round32(px), round32(pz)];
|
|
1731
|
+
const stepOff = proj.wall.thicknessM / 2 + Math.max(0.04, insetM);
|
|
1732
|
+
const posCand = [
|
|
1733
|
+
round32(proj.projectedX + proj.wall.nx * stepOff),
|
|
1734
|
+
round32(proj.projectedZ + proj.wall.nz * stepOff)
|
|
1735
|
+
];
|
|
1736
|
+
const negCand = [
|
|
1737
|
+
round32(proj.projectedX - proj.wall.nx * stepOff),
|
|
1738
|
+
round32(proj.projectedZ - proj.wall.nz * stepOff)
|
|
1739
|
+
];
|
|
1740
|
+
const strictOpts = { ...opts, wallToleranceM: 0 };
|
|
1741
|
+
const posIn = isPointInside(posCand[0], posCand[1], strictOpts);
|
|
1742
|
+
const negIn = isPointInside(negCand[0], negCand[1], strictOpts);
|
|
1743
|
+
if (posIn && !negIn)
|
|
1744
|
+
return posCand;
|
|
1745
|
+
if (negIn && !posIn)
|
|
1746
|
+
return negCand;
|
|
1747
|
+
return [round32(proj.projectedX), round32(proj.projectedZ)];
|
|
1748
|
+
};
|
|
1749
|
+
return {
|
|
1750
|
+
levelId,
|
|
1751
|
+
boundedFaces,
|
|
1752
|
+
boundedFacesComplete,
|
|
1753
|
+
roomPolygons,
|
|
1754
|
+
clusters,
|
|
1755
|
+
bounds,
|
|
1756
|
+
isPointInside,
|
|
1757
|
+
isSegmentInside,
|
|
1758
|
+
pathCrossesOutdoorVoid,
|
|
1759
|
+
clampPointInset
|
|
1760
|
+
};
|
|
1761
|
+
}
|
|
1762
|
+
|
|
1763
|
+
// src/anatomy/occupancy.ts
|
|
1764
|
+
var PLUMBING_PIPE_HALF_OD_M2 = {
|
|
1765
|
+
"1/2": 0.008,
|
|
1766
|
+
"3/4": 0.011,
|
|
1767
|
+
"1": 0.015,
|
|
1768
|
+
"1-1/4": 0.021,
|
|
1769
|
+
"1-1/2": 0.024,
|
|
1770
|
+
"2": 0.03,
|
|
1771
|
+
"2-1/2": 0.0365,
|
|
1772
|
+
"3": 0.044,
|
|
1773
|
+
"4": 0.057,
|
|
1774
|
+
"6": 0.084
|
|
1775
|
+
};
|
|
1776
|
+
var ELECTRICAL_CONDUIT_HALF_OD_M2 = {
|
|
1777
|
+
"1/2": 0.011,
|
|
1778
|
+
"3/4": 0.013,
|
|
1779
|
+
"1": 0.017,
|
|
1780
|
+
"1-1/4": 0.021,
|
|
1781
|
+
"1-1/2": 0.024,
|
|
1782
|
+
"2": 0.03,
|
|
1783
|
+
"2-1/2": 0.036,
|
|
1784
|
+
"3": 0.044,
|
|
1785
|
+
"4": 0.057
|
|
1786
|
+
};
|
|
1787
|
+
var DEFAULT_CLEARANCE_M = 0.05;
|
|
1788
|
+
var STRUCTURAL_CLEARANCE_M = 0.03;
|
|
1789
|
+
var HVAC_EQUIPMENT_CLEARANCE_M = 0.15;
|
|
1790
|
+
var ELECTRICAL_CONDUIT_CLEARANCE_M = 0.03;
|
|
1791
|
+
var ELECTRICAL_SPACE_CLEARANCE_M = 0.02;
|
|
1792
|
+
function normalizeNominalInchKey(raw) {
|
|
1793
|
+
if (typeof raw === "number" && Number.isFinite(raw) && raw > 0)
|
|
1794
|
+
return String(raw);
|
|
1795
|
+
if (typeof raw !== "string")
|
|
1796
|
+
return null;
|
|
1797
|
+
const stripped = raw.trim().replace(/["'″]+/g, "").trim();
|
|
1798
|
+
if (stripped.length === 0)
|
|
1799
|
+
return null;
|
|
1800
|
+
const mixedSpaced = /^(\d+)\s+(\d+\/\d+)$/.exec(stripped);
|
|
1801
|
+
if (mixedSpaced)
|
|
1802
|
+
return `${mixedSpaced[1]}-${mixedSpaced[2]}`;
|
|
1803
|
+
const compact = stripped.replace(/\s+/g, "");
|
|
1804
|
+
return compact.length > 0 ? compact : null;
|
|
1805
|
+
}
|
|
1806
|
+
function parseNominalInches2(raw) {
|
|
1807
|
+
if (typeof raw === "number") {
|
|
1808
|
+
return Number.isFinite(raw) && raw > 0 ? raw : null;
|
|
1809
|
+
}
|
|
1810
|
+
const key = normalizeNominalInchKey(raw);
|
|
1811
|
+
if (!key)
|
|
1812
|
+
return null;
|
|
1813
|
+
const mixed = /^(\d+)-(\d+)\/(\d+)$/.exec(key);
|
|
1814
|
+
if (mixed) {
|
|
1815
|
+
const den = Number(mixed[3]);
|
|
1816
|
+
if (den > 0) {
|
|
1817
|
+
const val = Number(mixed[1]) + Number(mixed[2]) / den;
|
|
1818
|
+
return Number.isFinite(val) && val > 0 ? val : null;
|
|
1819
|
+
}
|
|
1820
|
+
return null;
|
|
1821
|
+
}
|
|
1822
|
+
const frac = /^(\d+)\/(\d+)$/.exec(key);
|
|
1823
|
+
if (frac) {
|
|
1824
|
+
const den = Number(frac[2]);
|
|
1825
|
+
if (den > 0) {
|
|
1826
|
+
const val = Number(frac[1]) / den;
|
|
1827
|
+
return Number.isFinite(val) && val > 0 ? val : null;
|
|
1828
|
+
}
|
|
1829
|
+
return null;
|
|
1830
|
+
}
|
|
1831
|
+
const num = Number(key);
|
|
1832
|
+
return Number.isFinite(num) && num > 0 ? num : null;
|
|
1833
|
+
}
|
|
1834
|
+
function positiveFinite(v) {
|
|
1835
|
+
return typeof v === "number" && Number.isFinite(v) && v > 0 ? v : null;
|
|
1836
|
+
}
|
|
1837
|
+
function resolveMepRunExtents2(run) {
|
|
1838
|
+
const t = run.type;
|
|
1839
|
+
if (t === "lineset") {
|
|
1840
|
+
const diaIn = parseNominalInches2(run.diameter) ?? 1.5;
|
|
1841
|
+
const r = Math.max(0.025, diaIn * INCHES_TO_METERS2 / 2);
|
|
1842
|
+
return { halfXZ: r, halfY: r };
|
|
1843
|
+
}
|
|
1844
|
+
if (t === "hvac:duct-run" || t === "duct-segment") {
|
|
1845
|
+
const shape = typeof run.shape === "string" ? run.shape : "round";
|
|
1846
|
+
if (shape === "rectangular") {
|
|
1847
|
+
const widthIn = parseNominalInches2(run.width) ?? (t === "duct-segment" ? 14 : 10);
|
|
1848
|
+
const heightIn = parseNominalInches2(run.height) ?? 8;
|
|
1849
|
+
return {
|
|
1850
|
+
halfXZ: Math.max(0.025, widthIn * INCHES_TO_METERS2 / 2),
|
|
1851
|
+
halfY: Math.max(0.025, heightIn * INCHES_TO_METERS2 / 2)
|
|
1852
|
+
};
|
|
1853
|
+
}
|
|
1854
|
+
const diaIn = parseNominalInches2(run.diameter) ?? parseNominalInches2(run.width) ?? (t === "duct-segment" ? 8 : 6);
|
|
1855
|
+
const r = Math.max(0.015, diaIn * INCHES_TO_METERS2 / 2);
|
|
1856
|
+
return { halfXZ: r, halfY: r };
|
|
1857
|
+
}
|
|
1858
|
+
if (t === "plumbing:pipe-run" || t === "pipe-segment" || t === "liquid-line") {
|
|
1859
|
+
const rawSize = run.nominalSize ?? run.diameter;
|
|
1860
|
+
const key = normalizeNominalInchKey(rawSize);
|
|
1861
|
+
if (key) {
|
|
1862
|
+
const exact = PLUMBING_PIPE_HALF_OD_M2[key];
|
|
1863
|
+
if (exact !== undefined) {
|
|
1864
|
+
return { halfXZ: exact, halfY: exact };
|
|
1865
|
+
}
|
|
1866
|
+
const inches = parseNominalInches2(key);
|
|
1867
|
+
if (inches !== null) {
|
|
1868
|
+
const r = Math.max(0.008, inches * 1.12 * INCHES_TO_METERS2 / 2);
|
|
1869
|
+
return { halfXZ: r, halfY: r };
|
|
1870
|
+
}
|
|
1871
|
+
}
|
|
1872
|
+
if (t === "liquid-line") {
|
|
1873
|
+
const r = PLUMBING_PIPE_HALF_OD_M2["3/4"];
|
|
1874
|
+
return { halfXZ: r, halfY: r };
|
|
1875
|
+
}
|
|
1876
|
+
return { halfXZ: 0.025, halfY: 0.025 };
|
|
1877
|
+
}
|
|
1878
|
+
if (t === "electrical:conduit-run") {
|
|
1879
|
+
const rawSize = run.tradeSize ?? run.nominalSize ?? run.diameter;
|
|
1880
|
+
const key = normalizeNominalInchKey(rawSize);
|
|
1881
|
+
if (key) {
|
|
1882
|
+
const exact = ELECTRICAL_CONDUIT_HALF_OD_M2[key];
|
|
1883
|
+
if (exact !== undefined) {
|
|
1884
|
+
return { halfXZ: exact, halfY: exact };
|
|
1885
|
+
}
|
|
1886
|
+
const inches = parseNominalInches2(key);
|
|
1887
|
+
if (inches !== null) {
|
|
1888
|
+
const r = Math.max(0.011, inches * 1.12 * INCHES_TO_METERS2 / 2);
|
|
1889
|
+
return { halfXZ: r, halfY: r };
|
|
1890
|
+
}
|
|
1891
|
+
}
|
|
1892
|
+
return { halfXZ: 0.014, halfY: 0.014 };
|
|
1893
|
+
}
|
|
1894
|
+
return { halfXZ: 0.025, halfY: 0.025 };
|
|
1895
|
+
}
|
|
1896
|
+
function createThreeYawPrism(params) {
|
|
1897
|
+
return createOrientedPrismY2({
|
|
1898
|
+
centerX: params.centerX,
|
|
1899
|
+
centerZ: params.centerZ,
|
|
1900
|
+
cos: Math.cos(params.rotY),
|
|
1901
|
+
sin: -Math.sin(params.rotY),
|
|
1902
|
+
lxMin: params.lxMin,
|
|
1903
|
+
lxMax: params.lxMax,
|
|
1904
|
+
lzMin: params.lzMin,
|
|
1905
|
+
lzMax: params.lzMax,
|
|
1906
|
+
yMin: params.yMin,
|
|
1907
|
+
yMax: params.yMax
|
|
1908
|
+
});
|
|
1909
|
+
}
|
|
1910
|
+
function createSweptSegmentPrism(a, b, halfXZ, halfY) {
|
|
1911
|
+
const dx = b[0] - a[0];
|
|
1912
|
+
const dz = b[2] - a[2];
|
|
1913
|
+
const horizLen = Math.hypot(dx, dz);
|
|
1914
|
+
const yMin = Math.min(a[1], b[1]) - halfY;
|
|
1915
|
+
const yMax = Math.max(a[1], b[1]) + halfY;
|
|
1916
|
+
if (horizLen < 0.0001) {
|
|
1917
|
+
return createOrientedPrismY2({
|
|
1918
|
+
centerX: (a[0] + b[0]) / 2,
|
|
1919
|
+
centerZ: (a[2] + b[2]) / 2,
|
|
1920
|
+
cos: 1,
|
|
1921
|
+
sin: 0,
|
|
1922
|
+
lxMin: -halfXZ,
|
|
1923
|
+
lxMax: halfXZ,
|
|
1924
|
+
lzMin: -halfXZ,
|
|
1925
|
+
lzMax: halfXZ,
|
|
1926
|
+
yMin,
|
|
1927
|
+
yMax
|
|
1928
|
+
});
|
|
1929
|
+
}
|
|
1930
|
+
const ux = dx / horizLen;
|
|
1931
|
+
const uz = dz / horizLen;
|
|
1932
|
+
return createOrientedPrismY2({
|
|
1933
|
+
centerX: a[0],
|
|
1934
|
+
centerZ: a[2],
|
|
1935
|
+
cos: ux,
|
|
1936
|
+
sin: uz,
|
|
1937
|
+
lxMin: -halfXZ,
|
|
1938
|
+
lxMax: horizLen + halfXZ,
|
|
1939
|
+
lzMin: -halfXZ,
|
|
1940
|
+
lzMax: halfXZ,
|
|
1941
|
+
yMin,
|
|
1942
|
+
yMax
|
|
1943
|
+
});
|
|
1944
|
+
}
|
|
1945
|
+
function clampNonNegative(v, fallback = 0) {
|
|
1946
|
+
return typeof v === "number" && Number.isFinite(v) ? Math.max(0, v) : fallback;
|
|
1947
|
+
}
|
|
1948
|
+
function extractBeamPrism2(node, levelH = 2.7) {
|
|
1949
|
+
if (!Array.isArray(node.start) || node.start.length < 2)
|
|
1950
|
+
return null;
|
|
1951
|
+
if (!Array.isArray(node.end) || node.end.length < 2)
|
|
1952
|
+
return null;
|
|
1953
|
+
const start3D = node.start.length >= 3 && typeof node.start[2] === "number" && Number.isFinite(node.start[2]);
|
|
1954
|
+
const end3D = node.end.length >= 3 && typeof node.end[2] === "number" && Number.isFinite(node.end[2]);
|
|
1955
|
+
const sx = Number(node.start[0]);
|
|
1956
|
+
const sz = Number(start3D ? node.start[2] : node.start[1]);
|
|
1957
|
+
const ex = Number(node.end[0]);
|
|
1958
|
+
const ez = Number(end3D ? node.end[2] : node.end[1]);
|
|
1959
|
+
if (!Number.isFinite(sx) || !Number.isFinite(sz) || !Number.isFinite(ex) || !Number.isFinite(ez)) {
|
|
1960
|
+
return null;
|
|
1961
|
+
}
|
|
1962
|
+
const bdx = ex - sx;
|
|
1963
|
+
const bdz = ez - sz;
|
|
1964
|
+
const len = Math.hypot(bdx, bdz);
|
|
1965
|
+
if (len < 0.0001)
|
|
1966
|
+
return null;
|
|
1967
|
+
const isStructuralBeam = node.type === "structural:beam";
|
|
1968
|
+
const widthM = isStructuralBeam ? positiveFinite(node.widthIn) !== null ? positiveFinite(node.widthIn) * INCHES_TO_METERS2 : positiveFinite(node.width) ?? 3.5 * INCHES_TO_METERS2 : positiveFinite(node.width) ?? (positiveFinite(node.widthIn) !== null ? positiveFinite(node.widthIn) * INCHES_TO_METERS2 : 0.2);
|
|
1969
|
+
const depthM = isStructuralBeam ? positiveFinite(node.depthIn) !== null ? positiveFinite(node.depthIn) * INCHES_TO_METERS2 : positiveFinite(node.height) ?? 11.875 * INCHES_TO_METERS2 : positiveFinite(node.height) ?? (positiveFinite(node.depthIn) !== null ? positiveFinite(node.depthIn) * INCHES_TO_METERS2 : 0.3);
|
|
1970
|
+
const safeLevelH = Number.isFinite(levelH) && levelH > 0 ? levelH : 2.7;
|
|
1971
|
+
let yMin;
|
|
1972
|
+
let yMax;
|
|
1973
|
+
if (typeof node.elevationY === "number" && Number.isFinite(node.elevationY)) {
|
|
1974
|
+
yMin = node.elevationY - depthM / 2;
|
|
1975
|
+
yMax = node.elevationY + depthM / 2;
|
|
1976
|
+
} else if (typeof node.elevation === "number" && Number.isFinite(node.elevation)) {
|
|
1977
|
+
yMin = node.elevation - depthM / 2;
|
|
1978
|
+
yMax = node.elevation + depthM / 2;
|
|
1979
|
+
} else if (start3D && end3D) {
|
|
1980
|
+
const sy = Number(node.start[1]);
|
|
1981
|
+
const ey = Number(node.end[1]);
|
|
1982
|
+
if (Number.isFinite(sy) && Number.isFinite(ey)) {
|
|
1983
|
+
yMin = Math.min(sy, ey) - depthM / 2;
|
|
1984
|
+
yMax = Math.max(sy, ey) + depthM / 2;
|
|
1985
|
+
} else {
|
|
1986
|
+
const y = safeLevelH - depthM / 2;
|
|
1987
|
+
yMin = y - depthM / 2;
|
|
1988
|
+
yMax = y + depthM / 2;
|
|
1989
|
+
}
|
|
1990
|
+
} else if (start3D && Number.isFinite(Number(node.start[1]))) {
|
|
1991
|
+
const sy = Number(node.start[1]);
|
|
1992
|
+
yMin = sy - depthM / 2;
|
|
1993
|
+
yMax = sy + depthM / 2;
|
|
1994
|
+
} else {
|
|
1995
|
+
const y = safeLevelH - depthM / 2;
|
|
1996
|
+
yMin = y - depthM / 2;
|
|
1997
|
+
yMax = y + depthM / 2;
|
|
1998
|
+
}
|
|
1999
|
+
const ux = bdx / len;
|
|
2000
|
+
const uz = bdz / len;
|
|
2001
|
+
return createOrientedPrismY2({
|
|
2002
|
+
centerX: sx,
|
|
2003
|
+
centerZ: sz,
|
|
2004
|
+
cos: ux,
|
|
2005
|
+
sin: uz,
|
|
2006
|
+
lxMin: 0,
|
|
2007
|
+
lxMax: len,
|
|
2008
|
+
lzMin: -widthM / 2,
|
|
2009
|
+
lzMax: widthM / 2,
|
|
2010
|
+
yMin,
|
|
2011
|
+
yMax
|
|
2012
|
+
});
|
|
2013
|
+
}
|
|
2014
|
+
function extractColumnPrism2(node, levelH = 2.7) {
|
|
2015
|
+
if (!isVec32(node.position))
|
|
2016
|
+
return null;
|
|
2017
|
+
const [x, y, z] = node.position;
|
|
2018
|
+
const safeLevelH = Number.isFinite(levelH) && levelH > 0 ? levelH : 2.7;
|
|
2019
|
+
const isStructuralCol = node.type === "structural:column";
|
|
2020
|
+
const w = isStructuralCol ? (positiveFinite(node.widthIn) ?? 5.5) * INCHES_TO_METERS2 : positiveFinite(node.width) ?? 0.25;
|
|
2021
|
+
const d = isStructuralCol ? (positiveFinite(node.depthIn) ?? 5.5) * INCHES_TO_METERS2 : positiveFinite(node.depth) ?? w;
|
|
2022
|
+
const h = isStructuralCol ? positiveFinite(node.heightM) ?? positiveFinite(node.height) ?? safeLevelH : positiveFinite(node.height) ?? positiveFinite(node.heightM) ?? safeLevelH;
|
|
2023
|
+
const rotY = extractNodeYawRad2(node.rotation);
|
|
2024
|
+
return createThreeYawPrism({
|
|
2025
|
+
centerX: x,
|
|
2026
|
+
centerZ: z,
|
|
2027
|
+
rotY,
|
|
2028
|
+
lxMin: -w / 2,
|
|
2029
|
+
lxMax: w / 2,
|
|
2030
|
+
lzMin: -d / 2,
|
|
2031
|
+
lzMax: d / 2,
|
|
2032
|
+
yMin: y,
|
|
2033
|
+
yMax: y + h
|
|
2034
|
+
});
|
|
2035
|
+
}
|
|
2036
|
+
function getHvacEquipmentPrism2(node) {
|
|
2037
|
+
if (!isVec32(node.position))
|
|
2038
|
+
return null;
|
|
2039
|
+
const [x, y, z] = node.position;
|
|
2040
|
+
const asset = node.asset && typeof node.asset === "object" ? node.asset : null;
|
|
2041
|
+
const explicitDims = isVec32(node.dimensions) ? node.dimensions : isVec32(asset?.dimensions) ? asset.dimensions : isVec32(node.size) ? node.size : null;
|
|
2042
|
+
const eqKind = typeof node.equipmentKind === "string" ? node.equipmentKind : "";
|
|
2043
|
+
const defaultDims = eqKind === "rooftop-package-unit" ? [1.55, 1.05, 1.15] : eqKind === "air-handler" || eqKind === "gas-furnace-96" || eqKind === "gas-furnace-80" || eqKind.startsWith("gas-furnace") ? [0.56, 0.72, 1.42] : eqKind === "heat-pump-condenser" || eqKind === "ac-condenser" ? [0.85, 0.85, 0.95] : eqKind === "erv-ventilator" ? [0.6, 0.45, 0.45] : eqKind === "range-hood" ? [0.76, 0.5, 0.35] : eqKind === "bath-exhaust-fan" ? [0.3, 0.3, 0.22] : eqKind === "mini-split-outdoor" ? [0.82, 0.34, 0.6] : eqKind === "mini-split-indoor" ? [0.85, 0.24, 0.3] : eqKind === "dehumidifier-whole-house" ? [0.52, 0.38, 0.4] : [0.6, 0.7, 1.1];
|
|
2044
|
+
const w = positiveFinite(explicitDims?.[0]) ?? positiveFinite(node.width) ?? defaultDims[0];
|
|
2045
|
+
const h = positiveFinite(explicitDims?.[1]) ?? positiveFinite(node.height) ?? defaultDims[2];
|
|
2046
|
+
const d = positiveFinite(explicitDims?.[2]) ?? positiveFinite(node.depth) ?? defaultDims[1];
|
|
2047
|
+
const rotY = extractNodeYawRad2(node.rotation);
|
|
2048
|
+
return createThreeYawPrism({
|
|
2049
|
+
centerX: x,
|
|
2050
|
+
centerZ: z,
|
|
2051
|
+
rotY,
|
|
2052
|
+
lxMin: -w / 2,
|
|
2053
|
+
lxMax: w / 2,
|
|
2054
|
+
lzMin: -d / 2,
|
|
2055
|
+
lzMax: d / 2,
|
|
2056
|
+
yMin: y,
|
|
2057
|
+
yMax: y + h
|
|
2058
|
+
});
|
|
2059
|
+
}
|
|
2060
|
+
function getHvacEquipmentAabb2(node) {
|
|
2061
|
+
return getHvacEquipmentPrism2(node)?.aabb ?? null;
|
|
2062
|
+
}
|
|
2063
|
+
function getHvacPlenumPrism2(node) {
|
|
2064
|
+
if (!isVec32(node.position))
|
|
2065
|
+
return null;
|
|
2066
|
+
const [x, y, z] = node.position;
|
|
2067
|
+
const w = Math.max(0.24, (positiveFinite(node.widthIn) ?? 18) * INCHES_TO_METERS2);
|
|
2068
|
+
const h = Math.max(0.2, (positiveFinite(node.heightIn) ?? 10) * INCHES_TO_METERS2);
|
|
2069
|
+
const d = Math.max(0.24, (positiveFinite(node.depthIn) ?? 24) * INCHES_TO_METERS2);
|
|
2070
|
+
const rotY = extractNodeYawRad2(node.rotation);
|
|
2071
|
+
return createThreeYawPrism({
|
|
2072
|
+
centerX: x,
|
|
2073
|
+
centerZ: z,
|
|
2074
|
+
rotY,
|
|
2075
|
+
lxMin: -w / 2,
|
|
2076
|
+
lxMax: w / 2,
|
|
2077
|
+
lzMin: -d / 2,
|
|
2078
|
+
lzMax: d / 2,
|
|
2079
|
+
yMin: y - h / 2,
|
|
2080
|
+
yMax: y + h / 2
|
|
2081
|
+
});
|
|
2082
|
+
}
|
|
2083
|
+
function getHvacPlenumAabb2(node) {
|
|
2084
|
+
return getHvacPlenumPrism2(node)?.aabb ?? null;
|
|
2085
|
+
}
|
|
2086
|
+
function getPlumbingFixturePrism2(node) {
|
|
2087
|
+
if (!isVec32(node.position))
|
|
2088
|
+
return null;
|
|
2089
|
+
const [x, y, z] = node.position;
|
|
2090
|
+
const kind = typeof node.fixtureKind === "string" ? node.fixtureKind : "";
|
|
2091
|
+
let w = 0.55;
|
|
2092
|
+
let h = 0.85;
|
|
2093
|
+
let d = 0.5;
|
|
2094
|
+
let roughInY = 0.45;
|
|
2095
|
+
let centeredY = false;
|
|
2096
|
+
switch (kind) {
|
|
2097
|
+
case "bathtub":
|
|
2098
|
+
w = 1.52;
|
|
2099
|
+
h = 0.5;
|
|
2100
|
+
d = 0.76;
|
|
2101
|
+
roughInY = 0.15;
|
|
2102
|
+
break;
|
|
2103
|
+
case "tub-shower-combo":
|
|
2104
|
+
w = 1.52;
|
|
2105
|
+
h = 2.05;
|
|
2106
|
+
d = 0.8;
|
|
2107
|
+
roughInY = 0.15;
|
|
2108
|
+
break;
|
|
2109
|
+
case "shower":
|
|
2110
|
+
w = 0.96;
|
|
2111
|
+
h = 2.05;
|
|
2112
|
+
d = 0.96;
|
|
2113
|
+
roughInY = 0.05;
|
|
2114
|
+
break;
|
|
2115
|
+
case "water-closet":
|
|
2116
|
+
w = 0.42;
|
|
2117
|
+
h = 0.78;
|
|
2118
|
+
d = 0.7;
|
|
2119
|
+
roughInY = 0.15;
|
|
2120
|
+
break;
|
|
2121
|
+
case "lavatory":
|
|
2122
|
+
w = 0.6;
|
|
2123
|
+
h = 0.84;
|
|
2124
|
+
d = 0.5;
|
|
2125
|
+
roughInY = 0.53;
|
|
2126
|
+
break;
|
|
2127
|
+
case "kitchen-sink":
|
|
2128
|
+
w = 0.84;
|
|
2129
|
+
h = 0.91;
|
|
2130
|
+
d = 0.56;
|
|
2131
|
+
roughInY = 0.46;
|
|
2132
|
+
break;
|
|
2133
|
+
case "laundry-sink":
|
|
2134
|
+
case "bar-sink":
|
|
2135
|
+
case "mop-sink":
|
|
2136
|
+
w = 0.6;
|
|
2137
|
+
h = 0.86;
|
|
2138
|
+
d = 0.52;
|
|
2139
|
+
roughInY = 0.46;
|
|
2140
|
+
break;
|
|
2141
|
+
case "dishwasher":
|
|
2142
|
+
w = 0.6;
|
|
2143
|
+
h = 0.86;
|
|
2144
|
+
d = 0.6;
|
|
2145
|
+
roughInY = 0.4;
|
|
2146
|
+
break;
|
|
2147
|
+
case "clothes-washer":
|
|
2148
|
+
w = 0.68;
|
|
2149
|
+
h = 0.96;
|
|
2150
|
+
d = 0.7;
|
|
2151
|
+
roughInY = 0;
|
|
2152
|
+
break;
|
|
2153
|
+
case "garbage-disposal":
|
|
2154
|
+
w = 0.22;
|
|
2155
|
+
h = 0.34;
|
|
2156
|
+
d = 0.22;
|
|
2157
|
+
centeredY = true;
|
|
2158
|
+
break;
|
|
2159
|
+
case "hose-bibb":
|
|
2160
|
+
w = 0.12;
|
|
2161
|
+
h = 0.12;
|
|
2162
|
+
d = 0.12;
|
|
2163
|
+
centeredY = true;
|
|
2164
|
+
break;
|
|
2165
|
+
case "floor-drain":
|
|
2166
|
+
w = 0.18;
|
|
2167
|
+
h = 0.1;
|
|
2168
|
+
d = 0.18;
|
|
2169
|
+
centeredY = true;
|
|
2170
|
+
break;
|
|
2171
|
+
case "bidet":
|
|
2172
|
+
w = 0.4;
|
|
2173
|
+
h = 0.42;
|
|
2174
|
+
d = 0.6;
|
|
2175
|
+
roughInY = 0.15;
|
|
2176
|
+
break;
|
|
2177
|
+
case "urinal":
|
|
2178
|
+
w = 0.38;
|
|
2179
|
+
h = 0.65;
|
|
2180
|
+
d = 0.36;
|
|
2181
|
+
roughInY = 0.45;
|
|
2182
|
+
break;
|
|
2183
|
+
case "drinking-fountain":
|
|
2184
|
+
w = 0.45;
|
|
2185
|
+
h = 0.5;
|
|
2186
|
+
d = 0.45;
|
|
2187
|
+
roughInY = 0.45;
|
|
2188
|
+
break;
|
|
2189
|
+
case "grease-interceptor":
|
|
2190
|
+
w = 0.65;
|
|
2191
|
+
h = 0.55;
|
|
2192
|
+
d = 0.5;
|
|
2193
|
+
roughInY = 0;
|
|
2194
|
+
break;
|
|
2195
|
+
default:
|
|
2196
|
+
break;
|
|
2197
|
+
}
|
|
2198
|
+
const explicitW = positiveFinite(node.width);
|
|
2199
|
+
const explicitH = positiveFinite(node.height);
|
|
2200
|
+
const explicitD = positiveFinite(node.depth);
|
|
2201
|
+
if (explicitW !== null)
|
|
2202
|
+
w = explicitW;
|
|
2203
|
+
if (explicitH !== null)
|
|
2204
|
+
h = explicitH;
|
|
2205
|
+
if (explicitD !== null)
|
|
2206
|
+
d = explicitD;
|
|
2207
|
+
const rotY = extractNodeYawRad2(node.rotation);
|
|
2208
|
+
const minY = centeredY ? y - h / 2 : y >= 0 ? Math.max(0, y - roughInY) : y - roughInY;
|
|
2209
|
+
const maxY = centeredY ? y + h / 2 : minY + h;
|
|
2210
|
+
return createThreeYawPrism({
|
|
2211
|
+
centerX: x,
|
|
2212
|
+
centerZ: z,
|
|
2213
|
+
rotY,
|
|
2214
|
+
lxMin: -w / 2,
|
|
2215
|
+
lxMax: w / 2,
|
|
2216
|
+
lzMin: -d / 2,
|
|
2217
|
+
lzMax: d / 2,
|
|
2218
|
+
yMin: minY,
|
|
2219
|
+
yMax: maxY
|
|
2220
|
+
});
|
|
2221
|
+
}
|
|
2222
|
+
function getPlumbingFixtureAabb2(node) {
|
|
2223
|
+
return getPlumbingFixturePrism2(node)?.aabb ?? null;
|
|
2224
|
+
}
|
|
2225
|
+
function getPlumbingEquipmentPrism2(node) {
|
|
2226
|
+
if (!isVec32(node.position))
|
|
2227
|
+
return null;
|
|
2228
|
+
const [x, y, z] = node.position;
|
|
2229
|
+
const isManifold = node.type === "plumbing:manifold";
|
|
2230
|
+
const eqKind = typeof node.equipmentKind === "string" ? node.equipmentKind : "";
|
|
2231
|
+
let w = isManifold ? 0.48 : 0.6;
|
|
2232
|
+
let d = isManifold ? 0.48 : 0.6;
|
|
2233
|
+
let minY = isManifold ? y - 0.35 : y;
|
|
2234
|
+
let maxY = isManifold ? y + 0.35 : y + 1.55;
|
|
2235
|
+
if (!isManifold) {
|
|
2236
|
+
if (eqKind === "expansion-tank") {
|
|
2237
|
+
w = 0.24;
|
|
2238
|
+
d = 0.24;
|
|
2239
|
+
minY = y - 0.15;
|
|
2240
|
+
maxY = y + 0.25;
|
|
2241
|
+
} else if (eqKind === "water-heater-tankless-gas" || eqKind.startsWith("water-heater-tankless")) {
|
|
2242
|
+
w = 0.44;
|
|
2243
|
+
d = 0.44;
|
|
2244
|
+
minY = y + 0.9;
|
|
2245
|
+
maxY = y + 1.95;
|
|
2246
|
+
} else if (eqKind === "recirc-pump" || eqKind === "main-shutoff-prv" || eqKind === "backwater-valve" || eqKind === "gas-meter") {
|
|
2247
|
+
w = 0.32;
|
|
2248
|
+
d = 0.32;
|
|
2249
|
+
minY = y;
|
|
2250
|
+
maxY = y + 0.45;
|
|
2251
|
+
} else if (eqKind === "sump-pump" || eqKind === "sewage-ejector") {
|
|
2252
|
+
w = 0.48;
|
|
2253
|
+
d = 0.48;
|
|
2254
|
+
minY = y;
|
|
2255
|
+
maxY = y + 0.55;
|
|
2256
|
+
} else if (eqKind === "water-softener" || eqKind === "pressure-tank") {
|
|
2257
|
+
w = 0.48;
|
|
2258
|
+
d = 0.48;
|
|
2259
|
+
minY = y;
|
|
2260
|
+
maxY = y + 1.35;
|
|
2261
|
+
}
|
|
2262
|
+
}
|
|
2263
|
+
const explicitW = positiveFinite(node.width);
|
|
2264
|
+
const explicitD = positiveFinite(node.depth);
|
|
2265
|
+
const explicitH = positiveFinite(node.height);
|
|
2266
|
+
if (explicitW !== null)
|
|
2267
|
+
w = explicitW;
|
|
2268
|
+
if (explicitD !== null)
|
|
2269
|
+
d = explicitD;
|
|
2270
|
+
if (explicitH !== null) {
|
|
2271
|
+
if (isManifold) {
|
|
2272
|
+
minY = y - explicitH / 2;
|
|
2273
|
+
maxY = y + explicitH / 2;
|
|
2274
|
+
} else {
|
|
2275
|
+
maxY = minY + explicitH;
|
|
2276
|
+
}
|
|
2277
|
+
}
|
|
2278
|
+
const rotY = extractNodeYawRad2(node.rotation);
|
|
2279
|
+
return createThreeYawPrism({
|
|
2280
|
+
centerX: x,
|
|
2281
|
+
centerZ: z,
|
|
2282
|
+
rotY,
|
|
2283
|
+
lxMin: -w / 2,
|
|
2284
|
+
lxMax: w / 2,
|
|
2285
|
+
lzMin: -d / 2,
|
|
2286
|
+
lzMax: d / 2,
|
|
2287
|
+
yMin: minY,
|
|
2288
|
+
yMax: maxY
|
|
2289
|
+
});
|
|
2290
|
+
}
|
|
2291
|
+
function getPlumbingEquipmentAabb2(node) {
|
|
2292
|
+
return getPlumbingEquipmentPrism2(node)?.aabb ?? null;
|
|
2293
|
+
}
|
|
2294
|
+
function getElectricalDedicatedSpacePrisms2(node, levelH = 2.7, opts = {}) {
|
|
2295
|
+
if (node.type !== "electrical:panel" && node.type !== "electrical:transformer")
|
|
2296
|
+
return null;
|
|
2297
|
+
if (!isVec32(node.position))
|
|
2298
|
+
return null;
|
|
2299
|
+
const [x, y, z] = node.position;
|
|
2300
|
+
const safeLevelH = Number.isFinite(levelH) && levelH > 0 ? levelH : 2.7;
|
|
2301
|
+
const isTransformer = node.type === "electrical:transformer";
|
|
2302
|
+
const w = positiveFinite(node.width) ?? (isTransformer ? 0.76 : 0.42);
|
|
2303
|
+
const d = positiveFinite(node.depth) ?? (isTransformer ? 0.64 : 0.16);
|
|
2304
|
+
const rotY = extractNodeYawRad2(node.rotation);
|
|
2305
|
+
const topY = isTransformer ? y + (positiveFinite(node.height) ?? 0.95) : y + (positiveFinite(node.height) !== null ? positiveFinite(node.height) / 2 : 0.46);
|
|
2306
|
+
const dedicated = createThreeYawPrism({
|
|
2307
|
+
centerX: x,
|
|
2308
|
+
centerZ: z,
|
|
2309
|
+
rotY,
|
|
2310
|
+
lxMin: -(w / 2 + 0.03),
|
|
2311
|
+
lxMax: w / 2 + 0.03,
|
|
2312
|
+
lzMin: -(d / 2 + 0.03),
|
|
2313
|
+
lzMax: d / 2 + 0.03,
|
|
2314
|
+
yMin: 0,
|
|
2315
|
+
yMax: Math.max(safeLevelH + 0.6, topY + 1.83)
|
|
2316
|
+
});
|
|
2317
|
+
if (opts.includeWorkingSpace && !isTransformer) {
|
|
2318
|
+
const wsDepth = 0.914;
|
|
2319
|
+
const wsHalfW = Math.max(0.762, w) / 2;
|
|
2320
|
+
const workingSpace = createThreeYawPrism({
|
|
2321
|
+
centerX: x,
|
|
2322
|
+
centerZ: z,
|
|
2323
|
+
rotY,
|
|
2324
|
+
lxMin: -wsHalfW,
|
|
2325
|
+
lxMax: wsHalfW,
|
|
2326
|
+
lzMin: d / 2,
|
|
2327
|
+
lzMax: d / 2 + wsDepth,
|
|
2328
|
+
yMin: 0,
|
|
2329
|
+
yMax: Math.max(1.981, topY)
|
|
2330
|
+
});
|
|
2331
|
+
return { dedicated, workingSpace };
|
|
2332
|
+
}
|
|
2333
|
+
return { dedicated };
|
|
2334
|
+
}
|
|
2335
|
+
function getHvacTerminalPrism(node) {
|
|
2336
|
+
if (!isVec32(node.position))
|
|
2337
|
+
return null;
|
|
2338
|
+
const [x, y, z] = node.position;
|
|
2339
|
+
const termKind = typeof node.terminalKind === "string" ? node.terminalKind : "";
|
|
2340
|
+
if (termKind === "mini-split-wall-head") {
|
|
2341
|
+
const rotY = extractNodeYawRad2(node.rotation);
|
|
2342
|
+
return createThreeYawPrism({
|
|
2343
|
+
centerX: x,
|
|
2344
|
+
centerZ: z,
|
|
2345
|
+
rotY,
|
|
2346
|
+
lxMin: -0.425,
|
|
2347
|
+
lxMax: 0.425,
|
|
2348
|
+
lzMin: -0.12,
|
|
2349
|
+
lzMax: 0.12,
|
|
2350
|
+
yMin: y - 0.15,
|
|
2351
|
+
yMax: y + 0.15
|
|
2352
|
+
});
|
|
2353
|
+
}
|
|
2354
|
+
const isLowProfileSensor = termKind === "smart-thermostat" || termKind === "zone-sensor";
|
|
2355
|
+
const neckIn = positiveFinite(node.neckSizeIn) ?? 6;
|
|
2356
|
+
const half = isLowProfileSensor ? 0.06 : node.type === "hvac:terminal" ? Math.max(0.14, neckIn * INCHES_TO_METERS2 / 2 + 0.06) : 0.14;
|
|
2357
|
+
const halfY = isLowProfileSensor ? 0.06 : node.type === "hvac:terminal" ? 0.1 : half;
|
|
2358
|
+
return createOrientedPrismY2({
|
|
2359
|
+
centerX: x,
|
|
2360
|
+
centerZ: z,
|
|
2361
|
+
cos: 1,
|
|
2362
|
+
sin: 0,
|
|
2363
|
+
lxMin: -half,
|
|
2364
|
+
lxMax: half,
|
|
2365
|
+
lzMin: -half,
|
|
2366
|
+
lzMax: half,
|
|
2367
|
+
yMin: y - halfY,
|
|
2368
|
+
yMax: y + halfY
|
|
2369
|
+
});
|
|
2370
|
+
}
|
|
2371
|
+
function extractEquipmentPrisms2(node, levelH = 2.7, opts = {}) {
|
|
2372
|
+
const t = node.type;
|
|
2373
|
+
if (t === "hvac:equipment" || t === "hvac-equipment") {
|
|
2374
|
+
const prism = getHvacEquipmentPrism2(node);
|
|
2375
|
+
if (!prism)
|
|
2376
|
+
return [];
|
|
2377
|
+
return [
|
|
2378
|
+
{
|
|
2379
|
+
id: node.id,
|
|
2380
|
+
nodeId: node.id,
|
|
2381
|
+
nodeType: t,
|
|
2382
|
+
trade: "hvac",
|
|
2383
|
+
priorityRank: TRADE_PRIORITY_RANK2.hvac,
|
|
2384
|
+
kind: "hvac-equipment",
|
|
2385
|
+
label: `HVAC equipment (${node.name ?? node.id})`,
|
|
2386
|
+
aabb: prism.aabb,
|
|
2387
|
+
prism,
|
|
2388
|
+
requiredClearanceM: HVAC_EQUIPMENT_CLEARANCE_M
|
|
2389
|
+
}
|
|
2390
|
+
];
|
|
2391
|
+
}
|
|
2392
|
+
if (t === "hvac:plenum") {
|
|
2393
|
+
const prism = getHvacPlenumPrism2(node);
|
|
2394
|
+
if (!prism)
|
|
2395
|
+
return [];
|
|
2396
|
+
return [
|
|
2397
|
+
{
|
|
2398
|
+
id: node.id,
|
|
2399
|
+
nodeId: node.id,
|
|
2400
|
+
nodeType: t,
|
|
2401
|
+
trade: "hvac",
|
|
2402
|
+
priorityRank: TRADE_PRIORITY_RANK2.hvac,
|
|
2403
|
+
kind: "hvac-plenum",
|
|
2404
|
+
label: `HVAC plenum (${node.name ?? node.id})`,
|
|
2405
|
+
aabb: prism.aabb,
|
|
2406
|
+
prism,
|
|
2407
|
+
requiredClearanceM: DEFAULT_CLEARANCE_M
|
|
2408
|
+
}
|
|
2409
|
+
];
|
|
2410
|
+
}
|
|
2411
|
+
if (t === "hvac:terminal" || t === "duct-terminal" || t === "duct-fitting") {
|
|
2412
|
+
const prism = getHvacTerminalPrism(node);
|
|
2413
|
+
if (!prism)
|
|
2414
|
+
return [];
|
|
2415
|
+
return [
|
|
2416
|
+
{
|
|
2417
|
+
id: node.id,
|
|
2418
|
+
nodeId: node.id,
|
|
2419
|
+
nodeType: t,
|
|
2420
|
+
trade: "hvac",
|
|
2421
|
+
priorityRank: TRADE_PRIORITY_RANK2.hvac,
|
|
2422
|
+
kind: "hvac-terminal",
|
|
2423
|
+
label: `HVAC ${t} (${node.name ?? node.id})`,
|
|
2424
|
+
aabb: prism.aabb,
|
|
2425
|
+
prism,
|
|
2426
|
+
requiredClearanceM: DEFAULT_CLEARANCE_M
|
|
2427
|
+
}
|
|
2428
|
+
];
|
|
2429
|
+
}
|
|
2430
|
+
if (t === "plumbing:equipment" || t === "plumbing:manifold") {
|
|
2431
|
+
const prism = getPlumbingEquipmentPrism2(node);
|
|
2432
|
+
if (!prism)
|
|
2433
|
+
return [];
|
|
2434
|
+
const isManifold = t === "plumbing:manifold";
|
|
2435
|
+
return [
|
|
2436
|
+
{
|
|
2437
|
+
id: node.id,
|
|
2438
|
+
nodeId: node.id,
|
|
2439
|
+
nodeType: t,
|
|
2440
|
+
trade: "plumbing",
|
|
2441
|
+
priorityRank: TRADE_PRIORITY_RANK2.plumbing,
|
|
2442
|
+
kind: isManifold ? "plumbing-manifold" : "plumbing-equipment",
|
|
2443
|
+
label: `${isManifold ? "Plumbing manifold" : "Plumbing equipment"} (${node.name ?? node.id})`,
|
|
2444
|
+
aabb: prism.aabb,
|
|
2445
|
+
prism,
|
|
2446
|
+
requiredClearanceM: DEFAULT_CLEARANCE_M
|
|
2447
|
+
}
|
|
2448
|
+
];
|
|
2449
|
+
}
|
|
2450
|
+
if (t === "plumbing:fixture") {
|
|
2451
|
+
const prism = getPlumbingFixturePrism2(node);
|
|
2452
|
+
if (!prism)
|
|
2453
|
+
return [];
|
|
2454
|
+
return [
|
|
2455
|
+
{
|
|
2456
|
+
id: node.id,
|
|
2457
|
+
nodeId: node.id,
|
|
2458
|
+
nodeType: t,
|
|
2459
|
+
trade: "plumbing",
|
|
2460
|
+
priorityRank: TRADE_PRIORITY_RANK2.plumbing,
|
|
2461
|
+
kind: "plumbing-fixture",
|
|
2462
|
+
label: `Plumbing fixture (${node.name ?? node.id})`,
|
|
2463
|
+
aabb: prism.aabb,
|
|
2464
|
+
prism,
|
|
2465
|
+
requiredClearanceM: DEFAULT_CLEARANCE_M
|
|
2466
|
+
}
|
|
2467
|
+
];
|
|
2468
|
+
}
|
|
2469
|
+
if (t === "pipe-fitting" || t === "pipe-trap") {
|
|
2470
|
+
if (!isVec32(node.position))
|
|
2471
|
+
return [];
|
|
2472
|
+
const [x, y, z] = node.position;
|
|
2473
|
+
const diamIn = parseNominalInches2(node.diameter) ?? 2;
|
|
2474
|
+
const halfM = Math.max(0.05, diamIn * INCHES_TO_METERS2 / 2 + 0.03);
|
|
2475
|
+
const prism = createOrientedPrismY2({
|
|
2476
|
+
centerX: x,
|
|
2477
|
+
centerZ: z,
|
|
2478
|
+
cos: 1,
|
|
2479
|
+
sin: 0,
|
|
2480
|
+
lxMin: -halfM,
|
|
2481
|
+
lxMax: halfM,
|
|
2482
|
+
lzMin: -halfM,
|
|
2483
|
+
lzMax: halfM,
|
|
2484
|
+
yMin: y - halfM,
|
|
2485
|
+
yMax: y + halfM
|
|
2486
|
+
});
|
|
2487
|
+
return [
|
|
2488
|
+
{
|
|
2489
|
+
id: node.id,
|
|
2490
|
+
nodeId: node.id,
|
|
2491
|
+
nodeType: t,
|
|
2492
|
+
trade: "plumbing",
|
|
2493
|
+
priorityRank: TRADE_PRIORITY_RANK2.plumbing,
|
|
2494
|
+
kind: "plumbing-pipe",
|
|
2495
|
+
label: `Plumbing ${t} (${node.name ?? node.id})`,
|
|
2496
|
+
aabb: prism.aabb,
|
|
2497
|
+
prism,
|
|
2498
|
+
requiredClearanceM: DEFAULT_CLEARANCE_M
|
|
2499
|
+
}
|
|
2500
|
+
];
|
|
2501
|
+
}
|
|
2502
|
+
if (t === "electrical:panel" || t === "electrical:transformer") {
|
|
2503
|
+
const spaces = getElectricalDedicatedSpacePrisms2(node, levelH, {
|
|
2504
|
+
includeWorkingSpace: opts.includeElectricalWorkingSpace
|
|
2505
|
+
});
|
|
2506
|
+
if (!spaces)
|
|
2507
|
+
return [];
|
|
2508
|
+
const out = [
|
|
2509
|
+
{
|
|
2510
|
+
id: node.id,
|
|
2511
|
+
nodeId: node.id,
|
|
2512
|
+
nodeType: t,
|
|
2513
|
+
trade: "electrical",
|
|
2514
|
+
priorityRank: TRADE_PRIORITY_RANK2.electrical,
|
|
2515
|
+
kind: "electrical-dedicated-space",
|
|
2516
|
+
label: `NEC 110.26(E) Dedicated Space (${node.name ?? node.id})`,
|
|
2517
|
+
aabb: spaces.dedicated.aabb,
|
|
2518
|
+
prism: spaces.dedicated,
|
|
2519
|
+
requiredClearanceM: ELECTRICAL_SPACE_CLEARANCE_M
|
|
2520
|
+
}
|
|
2521
|
+
];
|
|
2522
|
+
if (spaces.workingSpace) {
|
|
2523
|
+
out.push({
|
|
2524
|
+
id: `${node.id}:working-space`,
|
|
2525
|
+
nodeId: node.id,
|
|
2526
|
+
nodeType: t,
|
|
2527
|
+
trade: "electrical",
|
|
2528
|
+
priorityRank: TRADE_PRIORITY_RANK2.electrical,
|
|
2529
|
+
kind: "electrical-working-space",
|
|
2530
|
+
label: `NEC 110.26(A) Working Space (${node.name ?? node.id})`,
|
|
2531
|
+
aabb: spaces.workingSpace.aabb,
|
|
2532
|
+
prism: spaces.workingSpace,
|
|
2533
|
+
requiredClearanceM: ELECTRICAL_SPACE_CLEARANCE_M
|
|
2534
|
+
});
|
|
2535
|
+
}
|
|
2536
|
+
return out;
|
|
2537
|
+
}
|
|
2538
|
+
return [];
|
|
2539
|
+
}
|
|
2540
|
+
function pushPathObstacles(out, node, trade, kind, label, requiredClearanceM) {
|
|
2541
|
+
const path = node.path;
|
|
2542
|
+
if (!Array.isArray(path) || path.length < 2)
|
|
2543
|
+
return;
|
|
2544
|
+
const { halfXZ, halfY } = resolveMepRunExtents2(node);
|
|
2545
|
+
const priorityRank = TRADE_PRIORITY_RANK2[trade];
|
|
2546
|
+
for (let i = 0;i < path.length - 1; i++) {
|
|
2547
|
+
const p0 = path[i];
|
|
2548
|
+
const p1 = path[i + 1];
|
|
2549
|
+
if (!isVec32(p0) || !isVec32(p1))
|
|
2550
|
+
continue;
|
|
2551
|
+
const prism = createSweptSegmentPrism(p0, p1, halfXZ, halfY);
|
|
2552
|
+
out.push({
|
|
2553
|
+
id: `${node.id}:seg${i}`,
|
|
2554
|
+
nodeId: node.id,
|
|
2555
|
+
nodeType: node.type,
|
|
2556
|
+
trade,
|
|
2557
|
+
priorityRank,
|
|
2558
|
+
kind,
|
|
2559
|
+
label,
|
|
2560
|
+
aabb: prism.aabb,
|
|
2561
|
+
prism,
|
|
2562
|
+
requiredClearanceM
|
|
2563
|
+
});
|
|
2564
|
+
}
|
|
2565
|
+
}
|
|
2566
|
+
function extractTradeOccupancyObstacles2(nodes, levelId, opts) {
|
|
2567
|
+
const { forTrade } = opts;
|
|
2568
|
+
const includeHidden = opts.includeHidden ?? false;
|
|
2569
|
+
const includeUnparented = opts.includeUnparented ?? false;
|
|
2570
|
+
const includePlumbingFixtures = opts.includePlumbingFixtures ?? false;
|
|
2571
|
+
const includeStructural = forTrade !== "structural";
|
|
2572
|
+
const includePlumbing = forTrade === "hvac" || forTrade === "electrical" || forTrade === "structural";
|
|
2573
|
+
const includeHvac = forTrade === "electrical" || forTrade === "structural";
|
|
2574
|
+
const includeElectricalDedicated = forTrade === "plumbing" || forTrade === "hvac";
|
|
2575
|
+
const includeElectricalWorking = opts.includeElectricalWorkingSpace ?? forTrade === "hvac";
|
|
2576
|
+
const includeElectricalConduits = forTrade === "structural";
|
|
2577
|
+
const levelH = resolveLevelHeightM2(nodes, levelId, {
|
|
2578
|
+
includeHiddenWalls: includeHidden,
|
|
2579
|
+
includeUnparentedWalls: includeUnparented
|
|
2580
|
+
});
|
|
2581
|
+
const obstacles = [];
|
|
2582
|
+
for (const node of Object.values(nodes)) {
|
|
2583
|
+
if (!includeHidden && node.visible === false)
|
|
2584
|
+
continue;
|
|
2585
|
+
if (!belongsToLevel2(nodes, node, levelId, { includeUnparented }))
|
|
2586
|
+
continue;
|
|
2587
|
+
const t = node.type;
|
|
2588
|
+
if (includeStructural && (t === "structural:beam" || t === "beam")) {
|
|
2589
|
+
const prism = extractBeamPrism2(node, levelH);
|
|
2590
|
+
if (prism) {
|
|
2591
|
+
const beamRole = typeof node.role === "string" ? node.role : undefined;
|
|
2592
|
+
const tag = String(node.designation ?? node.beamType ?? "").trim();
|
|
2593
|
+
const label = tag ? `Structural beam ${tag} (${node.name ?? node.id})` : `Structural beam (${node.name ?? node.id})`;
|
|
2594
|
+
obstacles.push({
|
|
2595
|
+
id: node.id,
|
|
2596
|
+
nodeId: node.id,
|
|
2597
|
+
nodeType: t,
|
|
2598
|
+
trade: "structural",
|
|
2599
|
+
priorityRank: TRADE_PRIORITY_RANK2.structural,
|
|
2600
|
+
kind: "structural-beam",
|
|
2601
|
+
label,
|
|
2602
|
+
aabb: prism.aabb,
|
|
2603
|
+
prism,
|
|
2604
|
+
requiredClearanceM: STRUCTURAL_CLEARANCE_M,
|
|
2605
|
+
structuralKind: "beam",
|
|
2606
|
+
...beamRole ? { beamRole } : {}
|
|
2607
|
+
});
|
|
2608
|
+
}
|
|
2609
|
+
continue;
|
|
2610
|
+
}
|
|
2611
|
+
if (includeStructural && (t === "structural:column" || t === "column")) {
|
|
2612
|
+
const prism = extractColumnPrism2(node, levelH);
|
|
2613
|
+
if (prism) {
|
|
2614
|
+
const tag = String(node.designation ?? node.columnType ?? "").trim();
|
|
2615
|
+
const label = tag ? `Structural column ${tag} (${node.name ?? node.id})` : `Structural column (${node.name ?? node.id})`;
|
|
2616
|
+
obstacles.push({
|
|
2617
|
+
id: node.id,
|
|
2618
|
+
nodeId: node.id,
|
|
2619
|
+
nodeType: t,
|
|
2620
|
+
trade: "structural",
|
|
2621
|
+
priorityRank: TRADE_PRIORITY_RANK2.structural,
|
|
2622
|
+
kind: "structural-column",
|
|
2623
|
+
label,
|
|
2624
|
+
aabb: prism.aabb,
|
|
2625
|
+
prism,
|
|
2626
|
+
requiredClearanceM: STRUCTURAL_CLEARANCE_M,
|
|
2627
|
+
structuralKind: "column"
|
|
2628
|
+
});
|
|
2629
|
+
}
|
|
2630
|
+
continue;
|
|
2631
|
+
}
|
|
2632
|
+
if (includePlumbing) {
|
|
2633
|
+
if (t === "plumbing:pipe-run" || t === "pipe-segment" || t === "liquid-line") {
|
|
2634
|
+
const sizeLabel = String(node.nominalSize ?? node.diameter ?? (t === "liquid-line" ? '3/4"' : '2"'));
|
|
2635
|
+
const label = t === "liquid-line" ? `Water line (${node.name ?? node.id})` : `Plumbing ${sizeLabel} ${String(node.system ?? "pipe")} (${node.name ?? node.id})`;
|
|
2636
|
+
pushPathObstacles(obstacles, node, "plumbing", "plumbing-pipe", label, DEFAULT_CLEARANCE_M);
|
|
2637
|
+
continue;
|
|
2638
|
+
}
|
|
2639
|
+
if (t === "plumbing:equipment" || t === "plumbing:manifold" || t === "pipe-fitting" || t === "pipe-trap" || includePlumbingFixtures && t === "plumbing:fixture") {
|
|
2640
|
+
obstacles.push(...extractEquipmentPrisms2(node, levelH));
|
|
2641
|
+
continue;
|
|
2642
|
+
}
|
|
2643
|
+
}
|
|
2644
|
+
if (includeHvac) {
|
|
2645
|
+
if (t === "hvac:duct-run" || t === "duct-segment" || t === "lineset") {
|
|
2646
|
+
const shape = typeof node.shape === "string" ? node.shape : "round";
|
|
2647
|
+
const label = t === "lineset" ? `Refrigerant lineset (${node.name ?? node.id})` : `HVAC ${shape} duct (${node.name ?? node.id})`;
|
|
2648
|
+
pushPathObstacles(obstacles, node, "hvac", "hvac-duct", label, DEFAULT_CLEARANCE_M);
|
|
2649
|
+
continue;
|
|
2650
|
+
}
|
|
2651
|
+
if (t === "hvac:equipment" || t === "hvac-equipment" || t === "hvac:plenum" || t === "hvac:terminal" || t === "duct-terminal" || t === "duct-fitting") {
|
|
2652
|
+
obstacles.push(...extractEquipmentPrisms2(node, levelH));
|
|
2653
|
+
continue;
|
|
2654
|
+
}
|
|
2655
|
+
}
|
|
2656
|
+
if ((includeElectricalDedicated || includeElectricalWorking) && (t === "electrical:panel" || t === "electrical:transformer")) {
|
|
2657
|
+
const items = extractEquipmentPrisms2(node, levelH, {
|
|
2658
|
+
includeElectricalWorkingSpace: includeElectricalWorking
|
|
2659
|
+
});
|
|
2660
|
+
for (const item of items) {
|
|
2661
|
+
if (item.kind === "electrical-dedicated-space" && !includeElectricalDedicated)
|
|
2662
|
+
continue;
|
|
2663
|
+
if (item.kind === "electrical-working-space" && !includeElectricalWorking)
|
|
2664
|
+
continue;
|
|
2665
|
+
obstacles.push(item);
|
|
2666
|
+
}
|
|
2667
|
+
continue;
|
|
2668
|
+
}
|
|
2669
|
+
if (includeElectricalConduits && t === "electrical:conduit-run") {
|
|
2670
|
+
const sizeLabel = String(node.tradeSize ?? '3/4"');
|
|
2671
|
+
const label = `Electrical ${sizeLabel} conduit (${node.name ?? node.id})`;
|
|
2672
|
+
pushPathObstacles(obstacles, node, "electrical", "electrical-conduit", label, ELECTRICAL_CONDUIT_CLEARANCE_M);
|
|
2673
|
+
continue;
|
|
2674
|
+
}
|
|
2675
|
+
}
|
|
2676
|
+
return obstacles.sort((a, b) => a.priorityRank - b.priorityRank);
|
|
2677
|
+
}
|
|
2678
|
+
|
|
2679
|
+
class TradeOccupancyGrid2 {
|
|
2680
|
+
_obstacles;
|
|
2681
|
+
_activeIds = new Set;
|
|
2682
|
+
_reservations = new Map;
|
|
2683
|
+
_seq = 1;
|
|
2684
|
+
constructor(initialObstacles = []) {
|
|
2685
|
+
this._obstacles = [...initialObstacles];
|
|
2686
|
+
for (const obs of this._obstacles) {
|
|
2687
|
+
this._activeIds.add(obs.id);
|
|
2688
|
+
}
|
|
2689
|
+
}
|
|
2690
|
+
get obstacles() {
|
|
2691
|
+
return this._obstacles;
|
|
2692
|
+
}
|
|
2693
|
+
reserveRun(params) {
|
|
2694
|
+
let reservationId = `res:${params.runId}:${this._seq++}`;
|
|
2695
|
+
while (this._reservations.has(reservationId) || this._activeIds.has(reservationId)) {
|
|
2696
|
+
reservationId = `res:${params.runId}:${this._seq++}`;
|
|
2697
|
+
}
|
|
2698
|
+
const halfXZ = clampNonNegative(params.halfXZ, 0);
|
|
2699
|
+
const halfY = clampNonNegative(params.halfY, 0);
|
|
2700
|
+
const clearanceM = clampNonNegative(params.clearanceM, DEFAULT_CLEARANCE_M);
|
|
2701
|
+
const priorityRank = TRADE_PRIORITY_RANK2[params.trade];
|
|
2702
|
+
const label = params.label ?? `Reserved ${params.trade} run (${params.runId})`;
|
|
2703
|
+
const ids = new Set;
|
|
2704
|
+
for (let i = 0;i < params.path.length - 1; i++) {
|
|
2705
|
+
const a = params.path[i];
|
|
2706
|
+
const b = params.path[i + 1];
|
|
2707
|
+
if (!isVec32(a) || !isVec32(b))
|
|
2708
|
+
continue;
|
|
2709
|
+
const prism = createSweptSegmentPrism(a, b, halfXZ, halfY);
|
|
2710
|
+
let segId = `${reservationId}:seg${i}`;
|
|
2711
|
+
while (this._activeIds.has(segId)) {
|
|
2712
|
+
segId = `${reservationId}:seg${i}#${this._seq++}`;
|
|
2713
|
+
}
|
|
2714
|
+
ids.add(segId);
|
|
2715
|
+
this._activeIds.add(segId);
|
|
2716
|
+
this._obstacles.push({
|
|
2717
|
+
id: segId,
|
|
2718
|
+
nodeId: params.runId,
|
|
2719
|
+
trade: params.trade,
|
|
2720
|
+
priorityRank,
|
|
2721
|
+
kind: "reserved-run",
|
|
2722
|
+
label,
|
|
2723
|
+
aabb: prism.aabb,
|
|
2724
|
+
prism,
|
|
2725
|
+
requiredClearanceM: clearanceM
|
|
2726
|
+
});
|
|
2727
|
+
}
|
|
2728
|
+
this._reservations.set(reservationId, ids);
|
|
2729
|
+
return reservationId;
|
|
2730
|
+
}
|
|
2731
|
+
reserveBox(params) {
|
|
2732
|
+
let reservationId = params.id;
|
|
2733
|
+
while (this._reservations.has(reservationId) || this._activeIds.has(reservationId)) {
|
|
2734
|
+
reservationId = `${params.id}#${this._seq++}`;
|
|
2735
|
+
}
|
|
2736
|
+
const clearanceM = clampNonNegative(params.clearanceM, DEFAULT_CLEARANCE_M);
|
|
2737
|
+
const nodeId = params.nodeId ?? params.id;
|
|
2738
|
+
this._activeIds.add(reservationId);
|
|
2739
|
+
this._obstacles.push({
|
|
2740
|
+
id: reservationId,
|
|
2741
|
+
nodeId,
|
|
2742
|
+
trade: params.trade,
|
|
2743
|
+
priorityRank: TRADE_PRIORITY_RANK2[params.trade],
|
|
2744
|
+
kind: params.kind,
|
|
2745
|
+
label: params.label ?? `${params.kind} (${nodeId})`,
|
|
2746
|
+
aabb: params.aabb,
|
|
2747
|
+
...params.prism ? { prism: params.prism } : {},
|
|
2748
|
+
requiredClearanceM: clearanceM
|
|
2749
|
+
});
|
|
2750
|
+
this._reservations.set(reservationId, new Set([reservationId]));
|
|
2751
|
+
return reservationId;
|
|
2752
|
+
}
|
|
2753
|
+
unreserve(reservationId) {
|
|
2754
|
+
const ids = this._reservations.get(reservationId);
|
|
2755
|
+
if (!ids)
|
|
2756
|
+
return;
|
|
2757
|
+
this._reservations.delete(reservationId);
|
|
2758
|
+
if (ids.size === 0)
|
|
2759
|
+
return;
|
|
2760
|
+
let write = 0;
|
|
2761
|
+
for (let i = 0;i < this._obstacles.length; i++) {
|
|
2762
|
+
const obs = this._obstacles[i];
|
|
2763
|
+
if (!ids.has(obs.id)) {
|
|
2764
|
+
this._obstacles[write++] = obs;
|
|
2765
|
+
}
|
|
2766
|
+
}
|
|
2767
|
+
this._obstacles.length = write;
|
|
2768
|
+
for (const id of ids) {
|
|
2769
|
+
this._activeIds.delete(id);
|
|
2770
|
+
}
|
|
2771
|
+
}
|
|
2772
|
+
segmentClashes(a, b, params) {
|
|
2773
|
+
if (!isVec32(a) || !isVec32(b))
|
|
2774
|
+
return [];
|
|
2775
|
+
const halfXZ = clampNonNegative(params.halfXZ, 0);
|
|
2776
|
+
const halfY = clampNonNegative(params.halfY, 0);
|
|
2777
|
+
const extraClear = clampNonNegative(params.extraClearanceM, 0);
|
|
2778
|
+
const useObstacleClearance = params.useObstacleClearance ?? true;
|
|
2779
|
+
const ignore = params.ignoreNodeIds;
|
|
2780
|
+
const hits = [];
|
|
2781
|
+
for (const obs of this._obstacles) {
|
|
2782
|
+
if (ignore && (ignore.has(obs.nodeId) || ignore.has(obs.id)))
|
|
2783
|
+
continue;
|
|
2784
|
+
const obsClear = useObstacleClearance ? clampNonNegative(obs.requiredClearanceM, 0) : 0;
|
|
2785
|
+
const clear = Math.max(obsClear, extraClear);
|
|
2786
|
+
const pad = [halfXZ + clear, halfY + clear, halfXZ + clear];
|
|
2787
|
+
const hit = obs.prism ? segmentIntersectsOrientedPrismY2(a, b, obs.prism, pad) : segmentIntersectsAabb32(a, b, obs.aabb, pad);
|
|
2788
|
+
if (hit)
|
|
2789
|
+
hits.push(obs);
|
|
2790
|
+
}
|
|
2791
|
+
return hits;
|
|
2792
|
+
}
|
|
2793
|
+
detectPathClashes(path, params) {
|
|
2794
|
+
if (path.length < 2)
|
|
2795
|
+
return [];
|
|
2796
|
+
const deduplicateByNode = params.deduplicateByNode ?? false;
|
|
2797
|
+
const seen = new Set;
|
|
2798
|
+
const records = [];
|
|
2799
|
+
for (let i = 0;i < path.length - 1; i++) {
|
|
2800
|
+
const a = path[i];
|
|
2801
|
+
const b = path[i + 1];
|
|
2802
|
+
if (!isVec32(a) || !isVec32(b))
|
|
2803
|
+
continue;
|
|
2804
|
+
const segHits = this.segmentClashes(a, b, params);
|
|
2805
|
+
for (const obstacle of segHits) {
|
|
2806
|
+
const key = deduplicateByNode ? `${obstacle.nodeId}::${obstacle.kind}` : `${obstacle.nodeId}::${obstacle.kind}:${i}`;
|
|
2807
|
+
if (seen.has(key))
|
|
2808
|
+
continue;
|
|
2809
|
+
seen.add(key);
|
|
2810
|
+
records.push({ obstacle, segmentIndex: i });
|
|
2811
|
+
}
|
|
2812
|
+
}
|
|
2813
|
+
return records;
|
|
2814
|
+
}
|
|
2815
|
+
}
|
|
2816
|
+
|
|
2817
|
+
// src/anatomy/roof.ts
|
|
2818
|
+
var VALID_ROOF_KINDS = new Set([
|
|
2819
|
+
"flat",
|
|
2820
|
+
"shed",
|
|
2821
|
+
"gable",
|
|
2822
|
+
"hip",
|
|
2823
|
+
"dutch",
|
|
2824
|
+
"mansard",
|
|
2825
|
+
"conical"
|
|
2826
|
+
]);
|
|
2827
|
+
var FLAT_PITCH_EPS_RAD = 0.0001;
|
|
2828
|
+
var MAX_PITCH_DEG = 65;
|
|
2829
|
+
var MAX_PITCH_RAD = MAX_PITCH_DEG * Math.PI / 180;
|
|
2830
|
+
var DEFAULT_PITCH_DEG = 26.57;
|
|
2831
|
+
var DEFAULT_OVERHANG_M = 0.35;
|
|
2832
|
+
var DEFAULT_FOOTPRINT_PAD_M = 0.02;
|
|
2833
|
+
var ADOPTION_WALL_PAD_M = 0.8;
|
|
2834
|
+
var PATH_SAMPLE_STEP_M = 0.35;
|
|
2835
|
+
function normalizeRoofSegmentKind(raw) {
|
|
2836
|
+
if (typeof raw === "string") {
|
|
2837
|
+
const lower = raw.trim().toLowerCase();
|
|
2838
|
+
if (VALID_ROOF_KINDS.has(lower)) {
|
|
2839
|
+
return lower;
|
|
2840
|
+
}
|
|
2841
|
+
}
|
|
2842
|
+
return "gable";
|
|
2843
|
+
}
|
|
2844
|
+
function segmentIntersectsAabb2D(ax, az, bx, bz, minX, maxX, minZ, maxZ) {
|
|
2845
|
+
const wMinX = Math.min(ax, bx);
|
|
2846
|
+
const wMaxX = Math.max(ax, bx);
|
|
2847
|
+
const wMinZ = Math.min(az, bz);
|
|
2848
|
+
const wMaxZ = Math.max(az, bz);
|
|
2849
|
+
if (wMaxX < minX || wMinX > maxX || wMaxZ < minZ || wMinZ > maxZ)
|
|
2850
|
+
return false;
|
|
2851
|
+
let tMin = 0;
|
|
2852
|
+
let tMax = 1;
|
|
2853
|
+
const dx = bx - ax;
|
|
2854
|
+
const dz = bz - az;
|
|
2855
|
+
if (Math.abs(dx) < 0.000000001) {
|
|
2856
|
+
if (ax < minX || ax > maxX)
|
|
2857
|
+
return false;
|
|
2858
|
+
} else {
|
|
2859
|
+
let t1 = (minX - ax) / dx;
|
|
2860
|
+
let t2 = (maxX - ax) / dx;
|
|
2861
|
+
if (t1 > t2) {
|
|
2862
|
+
const tmp = t1;
|
|
2863
|
+
t1 = t2;
|
|
2864
|
+
t2 = tmp;
|
|
2865
|
+
}
|
|
2866
|
+
tMin = Math.max(tMin, t1);
|
|
2867
|
+
tMax = Math.min(tMax, t2);
|
|
2868
|
+
if (tMin > tMax)
|
|
2869
|
+
return false;
|
|
2870
|
+
}
|
|
2871
|
+
if (Math.abs(dz) < 0.000000001) {
|
|
2872
|
+
if (az < minZ || az > maxZ)
|
|
2873
|
+
return false;
|
|
2874
|
+
} else {
|
|
2875
|
+
let t1 = (minZ - az) / dz;
|
|
2876
|
+
let t2 = (maxZ - az) / dz;
|
|
2877
|
+
if (t1 > t2) {
|
|
2878
|
+
const tmp = t1;
|
|
2879
|
+
t1 = t2;
|
|
2880
|
+
t2 = tmp;
|
|
2881
|
+
}
|
|
2882
|
+
tMin = Math.max(tMin, t1);
|
|
2883
|
+
tMax = Math.min(tMax, t2);
|
|
2884
|
+
if (tMin > tMax)
|
|
2885
|
+
return false;
|
|
2886
|
+
}
|
|
2887
|
+
return tMax >= 0 && tMin <= 1;
|
|
2888
|
+
}
|
|
2889
|
+
function resolveSegmentRiseAndSlope(roofType, width, depth, effWidth, effDepth, pitchRad) {
|
|
2890
|
+
if (roofType === "flat" || pitchRad <= FLAT_PITCH_EPS_RAD) {
|
|
2891
|
+
return { riseM: 0, effSlopeTan: 0 };
|
|
2892
|
+
}
|
|
2893
|
+
const slopeTan = Math.tan(pitchRad);
|
|
2894
|
+
const halfRun = effDepth / 2;
|
|
2895
|
+
if (roofType === "shed") {
|
|
2896
|
+
return { riseM: effDepth * slopeTan, effSlopeTan: slopeTan };
|
|
2897
|
+
}
|
|
2898
|
+
if (roofType === "conical") {
|
|
2899
|
+
const radius = Math.min(width, depth) / 2;
|
|
2900
|
+
const riseM = radius * slopeTan;
|
|
2901
|
+
return { riseM, effSlopeTan: slopeTan };
|
|
2902
|
+
}
|
|
2903
|
+
if (roofType === "mansard") {
|
|
2904
|
+
const inset = Math.min(effWidth, effDepth) * 0.18;
|
|
2905
|
+
const riseM = Math.min(2.2, inset * slopeTan);
|
|
2906
|
+
return { riseM, effSlopeTan: inset > 0.000001 ? riseM / inset : 0 };
|
|
2907
|
+
}
|
|
2908
|
+
if (roofType === "dutch") {
|
|
2909
|
+
const riseM = Math.min(halfRun * slopeTan, halfRun * 1.35);
|
|
2910
|
+
return { riseM, effSlopeTan: halfRun > 0.000001 ? riseM / halfRun : 0 };
|
|
2911
|
+
}
|
|
2912
|
+
return { riseM: halfRun * slopeTan, effSlopeTan: slopeTan };
|
|
2913
|
+
}
|
|
2914
|
+
function sameBuildingId2(bA, bB) {
|
|
2915
|
+
if (bA && bB && bA !== bB)
|
|
2916
|
+
return false;
|
|
2917
|
+
return true;
|
|
2918
|
+
}
|
|
2919
|
+
function buildRoofAdoptionContext(nodes) {
|
|
2920
|
+
const levels = [];
|
|
2921
|
+
const buildingByChildLevel = new Map;
|
|
2922
|
+
const walledLevelIds = new Set;
|
|
2923
|
+
const wallsByLevelId = new Map;
|
|
2924
|
+
for (const n of Object.values(nodes)) {
|
|
2925
|
+
if (n.type === "level") {
|
|
2926
|
+
levels.push(n);
|
|
2927
|
+
} else if (n.type === "building" && Array.isArray(n.children)) {
|
|
2928
|
+
for (const cid of n.children) {
|
|
2929
|
+
if (!buildingByChildLevel.has(cid)) {
|
|
2930
|
+
buildingByChildLevel.set(cid, n.id);
|
|
2931
|
+
}
|
|
2932
|
+
}
|
|
2933
|
+
} else if (n.type === "wall" && n.visible !== false) {
|
|
2934
|
+
const lid = resolveNodeLevelId2(nodes, n, { allowDanglingParentId: false });
|
|
2935
|
+
if (!lid)
|
|
2936
|
+
continue;
|
|
2937
|
+
walledLevelIds.add(lid);
|
|
2938
|
+
if (!Array.isArray(n.start) || !Array.isArray(n.end))
|
|
2939
|
+
continue;
|
|
2940
|
+
const sx = Number(n.start[0]);
|
|
2941
|
+
const sz = Number(n.start[1]);
|
|
2942
|
+
const ex = Number(n.end[0]);
|
|
2943
|
+
const ez = Number(n.end[1]);
|
|
2944
|
+
if (!Number.isFinite(sx) || !Number.isFinite(sz) || !Number.isFinite(ex) || !Number.isFinite(ez)) {
|
|
2945
|
+
continue;
|
|
2946
|
+
}
|
|
2947
|
+
let list = wallsByLevelId.get(lid);
|
|
2948
|
+
if (!list) {
|
|
2949
|
+
list = [];
|
|
2950
|
+
wallsByLevelId.set(lid, list);
|
|
2951
|
+
}
|
|
2952
|
+
list.push({ sx, sz, ex, ez });
|
|
2953
|
+
}
|
|
2954
|
+
}
|
|
2955
|
+
const buildingByLevelId = new Map;
|
|
2956
|
+
for (const lvl of levels) {
|
|
2957
|
+
let bId = null;
|
|
2958
|
+
if (typeof lvl.parentId === "string" && lvl.parentId.length > 0) {
|
|
2959
|
+
const p = nodes[lvl.parentId];
|
|
2960
|
+
if (p?.type === "building") {
|
|
2961
|
+
bId = p.id;
|
|
2962
|
+
}
|
|
2963
|
+
}
|
|
2964
|
+
if (bId === null) {
|
|
2965
|
+
const viaChild = buildingByChildLevel.get(lvl.id);
|
|
2966
|
+
if (viaChild !== undefined) {
|
|
2967
|
+
bId = viaChild;
|
|
2968
|
+
} else if (typeof lvl.parentId === "string" && lvl.parentId.length > 0) {
|
|
2969
|
+
bId = lvl.parentId;
|
|
2970
|
+
}
|
|
2971
|
+
}
|
|
2972
|
+
buildingByLevelId.set(lvl.id, bId);
|
|
2973
|
+
}
|
|
2974
|
+
return { levels, buildingByLevelId, walledLevelIds, wallsByLevelId };
|
|
2975
|
+
}
|
|
2976
|
+
function resolveBuildingForLevelFromContext(nodes, ctx, level) {
|
|
2977
|
+
if (ctx.buildingByLevelId.has(level.id)) {
|
|
2978
|
+
return ctx.buildingByLevelId.get(level.id) ?? null;
|
|
2979
|
+
}
|
|
2980
|
+
if (typeof level.parentId === "string" && level.parentId.length > 0) {
|
|
2981
|
+
const p = nodes[level.parentId];
|
|
2982
|
+
if (p?.type === "building")
|
|
2983
|
+
return p.id;
|
|
2984
|
+
return level.parentId;
|
|
2985
|
+
}
|
|
2986
|
+
return null;
|
|
2987
|
+
}
|
|
2988
|
+
function shouldAdoptWallLessRoofLevelWithContext(nodes, ctx, targetLevelId, roofLevel) {
|
|
2989
|
+
const targetLevel = nodes[targetLevelId];
|
|
2990
|
+
if (!targetLevel)
|
|
2991
|
+
return false;
|
|
2992
|
+
const targetBuilding = resolveBuildingForLevelFromContext(nodes, ctx, targetLevel);
|
|
2993
|
+
const roofBuilding = resolveBuildingForLevelFromContext(nodes, ctx, roofLevel);
|
|
2994
|
+
if (!sameBuildingId2(targetBuilding, roofBuilding))
|
|
2995
|
+
return false;
|
|
2996
|
+
if (ctx.walledLevelIds.has(roofLevel.id))
|
|
2997
|
+
return false;
|
|
2998
|
+
const siblingWalledLevels = ctx.levels.filter((n) => n.id !== roofLevel.id && ctx.walledLevelIds.has(n.id) && sameBuildingId2(roofBuilding, ctx.buildingByLevelId.get(n.id) ?? null)).sort(compareLevelsVertically2);
|
|
2999
|
+
if (siblingWalledLevels.length === 0)
|
|
3000
|
+
return false;
|
|
3001
|
+
const belowRoof = siblingWalledLevels.filter((l) => compareLevelsVertically2(l, roofLevel) <= 0);
|
|
3002
|
+
const hostLevel = belowRoof.length > 0 ? belowRoof[belowRoof.length - 1] : siblingWalledLevels[siblingWalledLevels.length - 1];
|
|
3003
|
+
return hostLevel.id === targetLevelId;
|
|
3004
|
+
}
|
|
3005
|
+
function shouldAdoptRoofSegmentOntoLevelWithContext(nodes, ctx, targetLevelId, roofLevel, segCenterX, segCenterZ, halfX, halfZ) {
|
|
3006
|
+
const targetLevel = nodes[targetLevelId];
|
|
3007
|
+
if (!targetLevel)
|
|
3008
|
+
return false;
|
|
3009
|
+
const targetBuilding = resolveBuildingForLevelFromContext(nodes, ctx, targetLevel);
|
|
3010
|
+
const roofBuilding = resolveBuildingForLevelFromContext(nodes, ctx, roofLevel);
|
|
3011
|
+
if (!sameBuildingId2(targetBuilding, roofBuilding))
|
|
3012
|
+
return false;
|
|
3013
|
+
if (ctx.walledLevelIds.has(roofLevel.id))
|
|
3014
|
+
return false;
|
|
3015
|
+
const siblingWalledLevels = ctx.levels.filter((n) => n.id !== roofLevel.id && ctx.walledLevelIds.has(n.id) && sameBuildingId2(roofBuilding, ctx.buildingByLevelId.get(n.id) ?? null)).sort(compareLevelsVertically2);
|
|
3016
|
+
if (siblingWalledLevels.length === 0)
|
|
3017
|
+
return false;
|
|
3018
|
+
const belowRoof = siblingWalledLevels.filter((l) => compareLevelsVertically2(l, roofLevel) <= 0);
|
|
3019
|
+
const candidates = belowRoof.length > 0 ? belowRoof : siblingWalledLevels;
|
|
3020
|
+
const minX = segCenterX - halfX - ADOPTION_WALL_PAD_M;
|
|
3021
|
+
const maxX = segCenterX + halfX + ADOPTION_WALL_PAD_M;
|
|
3022
|
+
const minZ = segCenterZ - halfZ - ADOPTION_WALL_PAD_M;
|
|
3023
|
+
const maxZ = segCenterZ + halfZ + ADOPTION_WALL_PAD_M;
|
|
3024
|
+
const overlapping = candidates.filter((lvl) => {
|
|
3025
|
+
const levelWalls = ctx.wallsByLevelId.get(lvl.id);
|
|
3026
|
+
if (!levelWalls || levelWalls.length === 0)
|
|
3027
|
+
return false;
|
|
3028
|
+
for (let i = 0;i < levelWalls.length; i++) {
|
|
3029
|
+
const w = levelWalls[i];
|
|
3030
|
+
if (segmentIntersectsAabb2D(w.sx, w.sz, w.ex, w.ez, minX, maxX, minZ, maxZ)) {
|
|
3031
|
+
return true;
|
|
3032
|
+
}
|
|
3033
|
+
}
|
|
3034
|
+
return false;
|
|
3035
|
+
});
|
|
3036
|
+
if (overlapping.length > 0) {
|
|
3037
|
+
return overlapping[overlapping.length - 1].id === targetLevelId;
|
|
3038
|
+
}
|
|
3039
|
+
const hostLevel = candidates[candidates.length - 1];
|
|
3040
|
+
return hostLevel.id === targetLevelId;
|
|
3041
|
+
}
|
|
3042
|
+
function shouldAdoptWallLessRoofLevel2(nodes, targetLevelId, roofLevel) {
|
|
3043
|
+
const ctx = buildRoofAdoptionContext(nodes);
|
|
3044
|
+
return shouldAdoptWallLessRoofLevelWithContext(nodes, ctx, targetLevelId, roofLevel);
|
|
3045
|
+
}
|
|
3046
|
+
function shouldAdoptRoofSegmentOntoLevel2(nodes, targetLevelId, roofLevel, segCenterX, segCenterZ, halfX, halfZ) {
|
|
3047
|
+
const ctx = buildRoofAdoptionContext(nodes);
|
|
3048
|
+
return shouldAdoptRoofSegmentOntoLevelWithContext(nodes, ctx, targetLevelId, roofLevel, segCenterX, segCenterZ, halfX, halfZ);
|
|
3049
|
+
}
|
|
3050
|
+
function evaluateRoofSegmentSurfaceY2(seg, wx, wz, opts = {}) {
|
|
3051
|
+
if (!Number.isFinite(wx) || !Number.isFinite(wz))
|
|
3052
|
+
return null;
|
|
3053
|
+
const clampOverhangToEave = opts.clampOverhangToEave ?? false;
|
|
3054
|
+
const padM = typeof opts.footprintPadM === "number" && Number.isFinite(opts.footprintPadM) && opts.footprintPadM >= 0 ? opts.footprintPadM : DEFAULT_FOOTPRINT_PAD_M;
|
|
3055
|
+
const dx = wx - seg.cx;
|
|
3056
|
+
const dz = wz - seg.cz;
|
|
3057
|
+
const lx = dx * seg.effCos - dz * seg.effSin;
|
|
3058
|
+
const lz = dx * seg.effSin + dz * seg.effCos;
|
|
3059
|
+
const { riseM, effSlopeTan } = resolveSegmentRiseAndSlope(seg.roofType, seg.width, seg.depth, seg.effWidth, seg.effDepth, seg.pitchRad);
|
|
3060
|
+
if (seg.roofType === "conical") {
|
|
3061
|
+
const r = Math.min(seg.width, seg.depth) / 2;
|
|
3062
|
+
const dist = Math.hypot(lx, lz);
|
|
3063
|
+
if (dist > r + seg.overhangM + padM)
|
|
3064
|
+
return null;
|
|
3065
|
+
if (seg.pitchRad <= FLAT_PITCH_EPS_RAD)
|
|
3066
|
+
return seg.eaveY;
|
|
3067
|
+
const d = clampOverhangToEave ? Math.max(0, r - dist) : r - dist;
|
|
3068
|
+
return seg.eaveY + d * effSlopeTan;
|
|
3069
|
+
}
|
|
3070
|
+
const halfW = seg.effWidth / 2;
|
|
3071
|
+
const halfRun = seg.effDepth / 2;
|
|
3072
|
+
const hasEndOverhang = seg.roofType === "hip" || seg.roofType === "dutch" || seg.roofType === "mansard";
|
|
3073
|
+
const padX = hasEndOverhang ? seg.overhangM + padM : padM;
|
|
3074
|
+
const padZ = seg.overhangM + padM;
|
|
3075
|
+
if (Math.abs(lx) > halfW + padX || Math.abs(lz) > halfRun + padZ) {
|
|
3076
|
+
return null;
|
|
3077
|
+
}
|
|
3078
|
+
if (seg.roofType === "flat" || seg.pitchRad <= FLAT_PITCH_EPS_RAD) {
|
|
3079
|
+
return seg.eaveY;
|
|
3080
|
+
}
|
|
3081
|
+
if (seg.roofType === "shed") {
|
|
3082
|
+
const d = clampOverhangToEave ? Math.max(0, halfRun - lz) : halfRun - lz;
|
|
3083
|
+
return seg.eaveY + d * effSlopeTan;
|
|
3084
|
+
}
|
|
3085
|
+
if (seg.roofType === "hip" || seg.roofType === "dutch" || seg.roofType === "mansard") {
|
|
3086
|
+
const dRaw = Math.min(halfW - Math.abs(lx), halfRun - Math.abs(lz));
|
|
3087
|
+
const d = clampOverhangToEave ? Math.max(0, dRaw) : dRaw;
|
|
3088
|
+
return seg.eaveY + Math.min(riseM, d * effSlopeTan);
|
|
3089
|
+
}
|
|
3090
|
+
const dRaw = halfRun - Math.abs(lz);
|
|
3091
|
+
const d = clampOverhangToEave ? Math.max(0, dRaw) : dRaw;
|
|
3092
|
+
return seg.eaveY + d * effSlopeTan;
|
|
3093
|
+
}
|
|
3094
|
+
function extractCanonicalRoofSegments2(nodes, levelId, wallHeightM, opts = {}) {
|
|
3095
|
+
const includeHidden = opts.includeHidden ?? false;
|
|
3096
|
+
const levelBaseY = typeof opts.levelBaseY === "number" && Number.isFinite(opts.levelBaseY) ? opts.levelBaseY : 0;
|
|
3097
|
+
const safeWallHeightM = Number.isFinite(wallHeightM) ? wallHeightM : 2.7;
|
|
3098
|
+
const isLowestOrGround = isGroundOrLowestWalledLevel2(nodes, levelId, {
|
|
3099
|
+
excludeBasements: true,
|
|
3100
|
+
includeHiddenWalls: includeHidden
|
|
3101
|
+
});
|
|
3102
|
+
let adoptionCtx = null;
|
|
3103
|
+
const segments = [];
|
|
3104
|
+
for (const node of Object.values(nodes)) {
|
|
3105
|
+
if (node.type !== "roof-segment")
|
|
3106
|
+
continue;
|
|
3107
|
+
let anyHidden = node.visible === false;
|
|
3108
|
+
const segPos = Array.isArray(node.position) ? node.position : undefined;
|
|
3109
|
+
let px = typeof segPos?.[0] === "number" && Number.isFinite(segPos[0]) ? segPos[0] : 0;
|
|
3110
|
+
let py = typeof segPos?.[1] === "number" && Number.isFinite(segPos[1]) ? segPos[1] : 0;
|
|
3111
|
+
let pz = typeof segPos?.[2] === "number" && Number.isFinite(segPos[2]) ? segPos[2] : 0;
|
|
3112
|
+
let yaw = extractNodeYawRad2(node.rotation);
|
|
3113
|
+
const visited = new Set([node.id]);
|
|
3114
|
+
let curParentId = typeof node.parentId === "string" && node.parentId.length > 0 ? node.parentId : null;
|
|
3115
|
+
let enclosingLevelNode = null;
|
|
3116
|
+
let danglingLevelId = null;
|
|
3117
|
+
while (curParentId) {
|
|
3118
|
+
if (visited.has(curParentId))
|
|
3119
|
+
break;
|
|
3120
|
+
visited.add(curParentId);
|
|
3121
|
+
const parent = nodes[curParentId];
|
|
3122
|
+
if (!parent) {
|
|
3123
|
+
danglingLevelId = curParentId;
|
|
3124
|
+
break;
|
|
3125
|
+
}
|
|
3126
|
+
if (parent.type === "roof") {
|
|
3127
|
+
if (parent.visible === false)
|
|
3128
|
+
anyHidden = true;
|
|
3129
|
+
const rot = extractNodeYawRad2(parent.rotation);
|
|
3130
|
+
const rp = Array.isArray(parent.position) ? parent.position : undefined;
|
|
3131
|
+
const rpx = typeof rp?.[0] === "number" && Number.isFinite(rp[0]) ? rp[0] : 0;
|
|
3132
|
+
const rpy = typeof rp?.[1] === "number" && Number.isFinite(rp[1]) ? rp[1] : 0;
|
|
3133
|
+
const rpz = typeof rp?.[2] === "number" && Number.isFinite(rp[2]) ? rp[2] : 0;
|
|
3134
|
+
const pc = Math.cos(rot);
|
|
3135
|
+
const ps = Math.sin(rot);
|
|
3136
|
+
const nx = px * pc + pz * ps;
|
|
3137
|
+
const nz = -px * ps + pz * pc;
|
|
3138
|
+
px = nx + rpx;
|
|
3139
|
+
py += rpy;
|
|
3140
|
+
pz = nz + rpz;
|
|
3141
|
+
yaw += rot;
|
|
3142
|
+
} else if (parent.type === "level") {
|
|
3143
|
+
enclosingLevelNode = parent;
|
|
3144
|
+
break;
|
|
3145
|
+
}
|
|
3146
|
+
curParentId = typeof parent.parentId === "string" && parent.parentId.length > 0 ? parent.parentId : null;
|
|
3147
|
+
}
|
|
3148
|
+
if (!includeHidden && anyHidden)
|
|
3149
|
+
continue;
|
|
3150
|
+
const width = typeof node.width === "number" && Number.isFinite(node.width) && node.width > 0.2 ? node.width : 6;
|
|
3151
|
+
const depth = typeof node.depth === "number" && Number.isFinite(node.depth) && node.depth > 0.2 ? node.depth : 6;
|
|
3152
|
+
let belongsHere = false;
|
|
3153
|
+
let adoptedFromWallLessLevel = false;
|
|
3154
|
+
if (levelId === null) {
|
|
3155
|
+
belongsHere = true;
|
|
3156
|
+
} else if (enclosingLevelNode?.id === levelId || danglingLevelId === levelId) {
|
|
3157
|
+
belongsHere = true;
|
|
3158
|
+
} else if (enclosingLevelNode !== null) {
|
|
3159
|
+
if (!adoptionCtx) {
|
|
3160
|
+
adoptionCtx = buildRoofAdoptionContext(nodes);
|
|
3161
|
+
}
|
|
3162
|
+
const absCos = Math.abs(Math.cos(yaw));
|
|
3163
|
+
const absSin = Math.abs(Math.sin(yaw));
|
|
3164
|
+
const halfX = (width * absCos + depth * absSin) / 2;
|
|
3165
|
+
const halfZ = (width * absSin + depth * absCos) / 2;
|
|
3166
|
+
if (shouldAdoptRoofSegmentOntoLevelWithContext(nodes, adoptionCtx, levelId, enclosingLevelNode, px, pz, halfX, halfZ)) {
|
|
3167
|
+
belongsHere = true;
|
|
3168
|
+
adoptedFromWallLessLevel = true;
|
|
3169
|
+
}
|
|
3170
|
+
}
|
|
3171
|
+
if (!belongsHere)
|
|
3172
|
+
continue;
|
|
3173
|
+
const roofType = normalizeRoofSegmentKind(node.roofType);
|
|
3174
|
+
const rawPitch = typeof node.pitch === "number" && Number.isFinite(node.pitch) ? node.pitch : DEFAULT_PITCH_DEG;
|
|
3175
|
+
let pitchDeg;
|
|
3176
|
+
let pitchRad;
|
|
3177
|
+
if (roofType === "flat" || rawPitch <= 0) {
|
|
3178
|
+
pitchDeg = 0;
|
|
3179
|
+
pitchRad = 0;
|
|
3180
|
+
} else if (rawPitch > 1.4) {
|
|
3181
|
+
pitchDeg = Math.min(MAX_PITCH_DEG, rawPitch);
|
|
3182
|
+
pitchRad = pitchDeg * Math.PI / 180;
|
|
3183
|
+
} else {
|
|
3184
|
+
pitchRad = Math.min(MAX_PITCH_RAD, rawPitch);
|
|
3185
|
+
pitchDeg = pitchRad * 180 / Math.PI;
|
|
3186
|
+
}
|
|
3187
|
+
const overhangM = typeof node.overhang === "number" && Number.isFinite(node.overhang) && node.overhang >= 0 ? node.overhang : DEFAULT_OVERHANG_M;
|
|
3188
|
+
const segWallH = typeof node.wallHeight === "number" && Number.isFinite(node.wallHeight) && node.wallHeight >= 0 ? node.wallHeight : 0;
|
|
3189
|
+
const isDirectUpperWingRoof = !adoptedFromWallLessLevel && !isLowestOrGround && py <= 0.05;
|
|
3190
|
+
const eaveY = adoptedFromWallLessLevel ? levelBaseY + safeWallHeightM + (py > 0.05 ? py : 0) + segWallH : py > 0.05 ? levelBaseY + py + segWallH : isDirectUpperWingRoof ? levelBaseY + segWallH : levelBaseY + safeWallHeightM + segWallH;
|
|
3191
|
+
const isRidgeFollowsLongAxis = roofType === "hip" || roofType === "dutch" || roofType === "mansard";
|
|
3192
|
+
const swappedAxes = isRidgeFollowsLongAxis && width < depth;
|
|
3193
|
+
const effYaw = swappedAxes ? yaw + Math.PI / 2 : yaw;
|
|
3194
|
+
const effWidth = swappedAxes ? depth : width;
|
|
3195
|
+
const effDepth = swappedAxes ? width : depth;
|
|
3196
|
+
const cos = Math.cos(yaw);
|
|
3197
|
+
const sin = Math.sin(yaw);
|
|
3198
|
+
const effCos = Math.cos(effYaw);
|
|
3199
|
+
const effSin = Math.sin(effYaw);
|
|
3200
|
+
const { riseM } = resolveSegmentRiseAndSlope(roofType, width, depth, effWidth, effDepth, pitchRad);
|
|
3201
|
+
const peakY = eaveY + riseM;
|
|
3202
|
+
segments.push({
|
|
3203
|
+
id: node.id,
|
|
3204
|
+
levelId: enclosingLevelNode?.id ?? danglingLevelId ?? null,
|
|
3205
|
+
adoptedFromWallLessLevel,
|
|
3206
|
+
cx: px,
|
|
3207
|
+
cz: pz,
|
|
3208
|
+
eaveY,
|
|
3209
|
+
width,
|
|
3210
|
+
depth,
|
|
3211
|
+
wallHeightM: segWallH,
|
|
3212
|
+
pitchDeg,
|
|
3213
|
+
pitchRad,
|
|
3214
|
+
roofType,
|
|
3215
|
+
yaw,
|
|
3216
|
+
cos,
|
|
3217
|
+
sin,
|
|
3218
|
+
effWidth,
|
|
3219
|
+
effDepth,
|
|
3220
|
+
effYaw,
|
|
3221
|
+
effCos,
|
|
3222
|
+
effSin,
|
|
3223
|
+
peakY,
|
|
3224
|
+
overhangM
|
|
3225
|
+
});
|
|
3226
|
+
}
|
|
3227
|
+
return segments;
|
|
3228
|
+
}
|
|
3229
|
+
function computeLevelWallBounds2D(nodes, levelId, includeHidden) {
|
|
3230
|
+
let minX = Number.POSITIVE_INFINITY;
|
|
3231
|
+
let maxX = Number.NEGATIVE_INFINITY;
|
|
3232
|
+
let minZ = Number.POSITIVE_INFINITY;
|
|
3233
|
+
let maxZ = Number.NEGATIVE_INFINITY;
|
|
3234
|
+
for (const n of Object.values(nodes)) {
|
|
3235
|
+
if (n.type !== "wall")
|
|
3236
|
+
continue;
|
|
3237
|
+
if (!includeHidden && n.visible === false)
|
|
3238
|
+
continue;
|
|
3239
|
+
if (levelId && !belongsToLevel2(nodes, n, levelId))
|
|
3240
|
+
continue;
|
|
3241
|
+
if (!Array.isArray(n.start) || !Array.isArray(n.end))
|
|
3242
|
+
continue;
|
|
3243
|
+
const sx = Number(n.start[0]);
|
|
3244
|
+
const sz = Number(n.start[1]);
|
|
3245
|
+
const ex = Number(n.end[0]);
|
|
3246
|
+
const ez = Number(n.end[1]);
|
|
3247
|
+
if (!Number.isFinite(sx) || !Number.isFinite(sz) || !Number.isFinite(ex) || !Number.isFinite(ez))
|
|
3248
|
+
continue;
|
|
3249
|
+
if (sx < minX)
|
|
3250
|
+
minX = sx;
|
|
3251
|
+
if (sx > maxX)
|
|
3252
|
+
maxX = sx;
|
|
3253
|
+
if (ex < minX)
|
|
3254
|
+
minX = ex;
|
|
3255
|
+
if (ex > maxX)
|
|
3256
|
+
maxX = ex;
|
|
3257
|
+
if (sz < minZ)
|
|
3258
|
+
minZ = sz;
|
|
3259
|
+
if (sz > maxZ)
|
|
3260
|
+
maxZ = sz;
|
|
3261
|
+
if (ez < minZ)
|
|
3262
|
+
minZ = ez;
|
|
3263
|
+
if (ez > maxZ)
|
|
3264
|
+
maxZ = ez;
|
|
3265
|
+
}
|
|
3266
|
+
if (!Number.isFinite(minX) || !Number.isFinite(minZ) || maxX - minX < 0.1 || maxZ - minZ < 0.1) {
|
|
3267
|
+
return null;
|
|
3268
|
+
}
|
|
3269
|
+
return { minX, maxX, minZ, maxZ };
|
|
3270
|
+
}
|
|
3271
|
+
function resolveFallbackPitchRiseOver12(nodes, levelId, explicitOverride) {
|
|
3272
|
+
if (typeof explicitOverride === "number" && Number.isFinite(explicitOverride) && explicitOverride >= 0) {
|
|
3273
|
+
return explicitOverride;
|
|
3274
|
+
}
|
|
3275
|
+
for (const n of Object.values(nodes)) {
|
|
3276
|
+
if (n.type !== "structural:assembly")
|
|
3277
|
+
continue;
|
|
3278
|
+
if (levelId && !belongsToLevel2(nodes, n, levelId))
|
|
3279
|
+
continue;
|
|
3280
|
+
const r = n.roofPitchRise;
|
|
3281
|
+
if (typeof r === "number" && Number.isFinite(r) && r >= 0) {
|
|
3282
|
+
return r;
|
|
3283
|
+
}
|
|
3284
|
+
}
|
|
3285
|
+
return 6;
|
|
3286
|
+
}
|
|
3287
|
+
function compileLevelRoofShell2(nodes, levelId, wallHeightM, opts = {}) {
|
|
3288
|
+
const includeHidden = opts.includeHidden ?? false;
|
|
3289
|
+
const clampOverhangToEave = opts.clampOverhangToEave ?? false;
|
|
3290
|
+
const footprintPadM = typeof opts.footprintPadM === "number" && Number.isFinite(opts.footprintPadM) && opts.footprintPadM >= 0 ? opts.footprintPadM : DEFAULT_FOOTPRINT_PAD_M;
|
|
3291
|
+
const levelBaseY = typeof opts.levelBaseY === "number" && Number.isFinite(opts.levelBaseY) ? opts.levelBaseY : 0;
|
|
3292
|
+
const safeWallHeightM = Number.isFinite(wallHeightM) ? wallHeightM : 2.7;
|
|
3293
|
+
const baseEaveY = levelBaseY + safeWallHeightM;
|
|
3294
|
+
const segments = extractCanonicalRoofSegments2(nodes, levelId, safeWallHeightM, {
|
|
3295
|
+
includeHidden,
|
|
3296
|
+
levelBaseY
|
|
3297
|
+
});
|
|
3298
|
+
const isHighestWalled = isHighestWalledLevelInBuilding2(nodes, levelId, {
|
|
3299
|
+
includeHiddenWalls: includeHidden
|
|
3300
|
+
});
|
|
3301
|
+
const hasRoofAbove = isHighestWalled || segments.length > 0;
|
|
3302
|
+
const fallbackPitchRiseOver12 = resolveFallbackPitchRiseOver12(nodes, levelId, opts.fallbackPitchRiseOver12);
|
|
3303
|
+
const isFlatRoof = segments.length > 0 ? segments.every((s) => s.roofType === "flat" || s.pitchRad <= FLAT_PITCH_EPS_RAD) : fallbackPitchRiseOver12 <= 0.0001;
|
|
3304
|
+
const fallbackBounds = opts.wallBounds && Number.isFinite(opts.wallBounds.minX) && Number.isFinite(opts.wallBounds.maxX) && Number.isFinite(opts.wallBounds.minZ) && Number.isFinite(opts.wallBounds.maxZ) && opts.wallBounds.maxX - opts.wallBounds.minX >= 0.1 && opts.wallBounds.maxZ - opts.wallBounds.minZ >= 0.1 ? opts.wallBounds : computeLevelWallBounds2D(nodes, levelId, includeHidden);
|
|
3305
|
+
const spanX = fallbackBounds ? Math.max(0.5, fallbackBounds.maxX - fallbackBounds.minX) : 0;
|
|
3306
|
+
const spanZ = fallbackBounds ? Math.max(0.5, fallbackBounds.maxZ - fallbackBounds.minZ) : 0;
|
|
3307
|
+
const ridgeAlongX = spanX >= spanZ;
|
|
3308
|
+
const halfSpan = (ridgeAlongX ? spanZ : spanX) / 2;
|
|
3309
|
+
const ridgeCenter = fallbackBounds ? ridgeAlongX ? (fallbackBounds.minZ + fallbackBounds.maxZ) / 2 : (fallbackBounds.minX + fallbackBounds.maxX) / 2 : 0;
|
|
3310
|
+
const syntheticSlope = fallbackPitchRiseOver12 / 12;
|
|
3311
|
+
const allowSyntheticGable = hasRoofAbove && isHighestWalled && !isFlatRoof && syntheticSlope > 0.0001 && fallbackBounds !== null;
|
|
3312
|
+
const syntheticGableY = (x, z) => {
|
|
3313
|
+
if (!allowSyntheticGable || !Number.isFinite(x) || !Number.isFinite(z))
|
|
3314
|
+
return baseEaveY;
|
|
3315
|
+
const distFromRidge = Math.abs((ridgeAlongX ? z : x) - ridgeCenter);
|
|
3316
|
+
const riseM = Math.max(0, halfSpan - distFromRidge) * syntheticSlope;
|
|
3317
|
+
return baseEaveY + riseM;
|
|
3318
|
+
};
|
|
3319
|
+
let maxRidgeY;
|
|
3320
|
+
if (segments.length > 0) {
|
|
3321
|
+
maxRidgeY = segments[0].peakY;
|
|
3322
|
+
for (let i = 1;i < segments.length; i++) {
|
|
3323
|
+
const p = segments[i].peakY;
|
|
3324
|
+
if (p > maxRidgeY)
|
|
3325
|
+
maxRidgeY = p;
|
|
3326
|
+
}
|
|
3327
|
+
} else if (allowSyntheticGable) {
|
|
3328
|
+
maxRidgeY = baseEaveY + halfSpan * syntheticSlope;
|
|
3329
|
+
} else {
|
|
3330
|
+
maxRidgeY = baseEaveY;
|
|
3331
|
+
}
|
|
3332
|
+
const evalOpts = { clampOverhangToEave, footprintPadM };
|
|
3333
|
+
const roofSurfaceY = (x, z) => {
|
|
3334
|
+
if (segments.length === 0)
|
|
3335
|
+
return null;
|
|
3336
|
+
let bestY = null;
|
|
3337
|
+
for (const seg of segments) {
|
|
3338
|
+
const sy = evaluateRoofSegmentSurfaceY2(seg, x, z, evalOpts);
|
|
3339
|
+
if (sy !== null && (bestY === null || sy > bestY)) {
|
|
3340
|
+
bestY = sy;
|
|
3341
|
+
}
|
|
3342
|
+
}
|
|
3343
|
+
return bestY;
|
|
3344
|
+
};
|
|
3345
|
+
const roofEnvelopeY = (x, z) => {
|
|
3346
|
+
if (!Number.isFinite(x) || !Number.isFinite(z))
|
|
3347
|
+
return baseEaveY;
|
|
3348
|
+
const explicitY = roofSurfaceY(x, z);
|
|
3349
|
+
if (explicitY !== null)
|
|
3350
|
+
return explicitY;
|
|
3351
|
+
if (allowSyntheticGable)
|
|
3352
|
+
return syntheticGableY(x, z);
|
|
3353
|
+
return baseEaveY;
|
|
3354
|
+
};
|
|
3355
|
+
const minRoofYOverAabb = (minX, maxX, minZ, maxZ) => {
|
|
3356
|
+
if (!Number.isFinite(minX) || !Number.isFinite(maxX) || !Number.isFinite(minZ) || !Number.isFinite(maxZ)) {
|
|
3357
|
+
return baseEaveY;
|
|
3358
|
+
}
|
|
3359
|
+
const x0 = Math.min(minX, maxX);
|
|
3360
|
+
const x1 = Math.max(minX, maxX);
|
|
3361
|
+
const z0 = Math.min(minZ, maxZ);
|
|
3362
|
+
const z1 = Math.max(minZ, maxZ);
|
|
3363
|
+
if (x1 - x0 < 0.0001 && z1 - z0 < 0.0001) {
|
|
3364
|
+
return roofEnvelopeY(x0, z0);
|
|
3365
|
+
}
|
|
3366
|
+
const xm = (x0 + x1) * 0.5;
|
|
3367
|
+
const zm = (z0 + z1) * 0.5;
|
|
3368
|
+
let minY = Number.POSITIVE_INFINITY;
|
|
3369
|
+
for (const sx of [x0, xm, x1]) {
|
|
3370
|
+
for (const sz of [z0, zm, z1]) {
|
|
3371
|
+
const ry = roofEnvelopeY(sx, sz);
|
|
3372
|
+
if (ry < minY)
|
|
3373
|
+
minY = ry;
|
|
3374
|
+
}
|
|
3375
|
+
}
|
|
3376
|
+
return minY;
|
|
3377
|
+
};
|
|
3378
|
+
const isPathClearOfRoof = (path, halfV, clearanceM = 0.14) => {
|
|
3379
|
+
if (path.length === 0)
|
|
3380
|
+
return true;
|
|
3381
|
+
for (let i = 0;i < path.length; i++) {
|
|
3382
|
+
if (!isVec32(path[i]))
|
|
3383
|
+
return false;
|
|
3384
|
+
}
|
|
3385
|
+
if (!hasRoofAbove)
|
|
3386
|
+
return true;
|
|
3387
|
+
const safeHalfV = Number.isFinite(halfV) ? Math.max(0, halfV) : 0;
|
|
3388
|
+
const safeClearance = Number.isFinite(clearanceM) ? clearanceM : 0.14;
|
|
3389
|
+
const topOffset = safeHalfV + safeClearance;
|
|
3390
|
+
if (path.length === 1) {
|
|
3391
|
+
const pt = path[0];
|
|
3392
|
+
return pt[1] + topOffset <= roofEnvelopeY(pt[0], pt[2]) + 0.000001;
|
|
3393
|
+
}
|
|
3394
|
+
for (let i = 0;i < path.length - 1; i++) {
|
|
3395
|
+
const a = path[i];
|
|
3396
|
+
const b = path[i + 1];
|
|
3397
|
+
const segLen = Math.hypot(b[0] - a[0], b[1] - a[1], b[2] - a[2]);
|
|
3398
|
+
if (!Number.isFinite(segLen))
|
|
3399
|
+
return false;
|
|
3400
|
+
const steps = Math.max(1, Math.ceil(segLen / PATH_SAMPLE_STEP_M));
|
|
3401
|
+
for (let s = 0;s <= steps; s++) {
|
|
3402
|
+
const t = s / steps;
|
|
3403
|
+
const px = a[0] + (b[0] - a[0]) * t;
|
|
3404
|
+
const py = a[1] + (b[1] - a[1]) * t;
|
|
3405
|
+
const pz = a[2] + (b[2] - a[2]) * t;
|
|
3406
|
+
if (py + topOffset > roofEnvelopeY(px, pz) + 0.000001) {
|
|
3407
|
+
return false;
|
|
3408
|
+
}
|
|
3409
|
+
}
|
|
3410
|
+
}
|
|
3411
|
+
return true;
|
|
3412
|
+
};
|
|
3413
|
+
const safeGableCorridorHalfWidth = (y, clearanceM = 0.08) => {
|
|
3414
|
+
if (!hasRoofAbove)
|
|
3415
|
+
return null;
|
|
3416
|
+
if (!Number.isFinite(y))
|
|
3417
|
+
return 0;
|
|
3418
|
+
if (isFlatRoof)
|
|
3419
|
+
return 0;
|
|
3420
|
+
const safeClearance = Number.isFinite(clearanceM) ? clearanceM : 0.08;
|
|
3421
|
+
if (segments.length > 0) {
|
|
3422
|
+
let bestHalfWidth = 0;
|
|
3423
|
+
for (const seg of segments) {
|
|
3424
|
+
const { riseM, effSlopeTan } = resolveSegmentRiseAndSlope(seg.roofType, seg.width, seg.depth, seg.effWidth, seg.effDepth, seg.pitchRad);
|
|
3425
|
+
if (riseM <= 0.0001 || effSlopeTan <= 0.0001)
|
|
3426
|
+
continue;
|
|
3427
|
+
const halfRun = seg.roofType === "conical" ? Math.min(seg.width, seg.depth) / 2 : seg.effDepth / 2;
|
|
3428
|
+
const neededRise = y + safeClearance - seg.eaveY;
|
|
3429
|
+
let segHalfW;
|
|
3430
|
+
if (neededRise <= 0) {
|
|
3431
|
+
segHalfW = halfRun;
|
|
3432
|
+
} else if (neededRise > riseM) {
|
|
3433
|
+
segHalfW = 0;
|
|
3434
|
+
} else if (seg.roofType === "shed") {
|
|
3435
|
+
segHalfW = Math.max(0, (seg.effDepth - neededRise / effSlopeTan) / 2);
|
|
3436
|
+
} else {
|
|
3437
|
+
segHalfW = Math.max(0, halfRun - neededRise / effSlopeTan);
|
|
3438
|
+
}
|
|
3439
|
+
if (segHalfW > bestHalfWidth)
|
|
3440
|
+
bestHalfWidth = segHalfW;
|
|
3441
|
+
}
|
|
3442
|
+
return bestHalfWidth;
|
|
3443
|
+
}
|
|
3444
|
+
if (!allowSyntheticGable)
|
|
3445
|
+
return null;
|
|
3446
|
+
const neededRise = y + safeClearance - baseEaveY;
|
|
3447
|
+
if (neededRise <= 0)
|
|
3448
|
+
return halfSpan;
|
|
3449
|
+
return Math.max(0, halfSpan - neededRise / syntheticSlope);
|
|
3450
|
+
};
|
|
3451
|
+
return {
|
|
3452
|
+
levelId,
|
|
3453
|
+
wallHeightM: safeWallHeightM,
|
|
3454
|
+
segments,
|
|
3455
|
+
hasRoofAbove,
|
|
3456
|
+
isFlatRoof,
|
|
3457
|
+
maxRidgeY,
|
|
3458
|
+
fallbackBounds,
|
|
3459
|
+
fallbackPitchRiseOver12,
|
|
3460
|
+
roofSurfaceY,
|
|
3461
|
+
roofEnvelopeY,
|
|
3462
|
+
minRoofYOverAabb,
|
|
3463
|
+
isPathClearOfRoof,
|
|
3464
|
+
safeGableCorridorHalfWidth
|
|
3465
|
+
};
|
|
3466
|
+
}
|
|
3467
|
+
|
|
3468
|
+
// src/anatomy/index.ts
|
|
3469
|
+
var anatomyCache = new WeakMap;
|
|
3470
|
+
function encodeLevelCacheKey(levelId) {
|
|
3471
|
+
return levelId === null ? "all:" : `lvl:${JSON.stringify(levelId)}`;
|
|
3472
|
+
}
|
|
3473
|
+
function compileBuildingAnatomy2(nodes) {
|
|
3474
|
+
const existing = anatomyCache.get(nodes);
|
|
3475
|
+
if (existing)
|
|
3476
|
+
return existing;
|
|
3477
|
+
const levels = Object.values(nodes).filter((n) => n.type === "level").sort(compareLevelsVertically2);
|
|
3478
|
+
const levelIds = Object.freeze(levels.map((l) => l.id));
|
|
3479
|
+
const heightCache = new Map;
|
|
3480
|
+
const wallCache = new Map;
|
|
3481
|
+
const prismCache = new Map;
|
|
3482
|
+
const roofCache = new Map;
|
|
3483
|
+
const footprintCache = new Map;
|
|
3484
|
+
const getLevelHeightM = (levelId, opts) => {
|
|
3485
|
+
const key = `${encodeLevelCacheKey(levelId)}|${opts?.minClampM ?? ""}|${opts?.defaultHeightM ?? ""}|${opts?.includeHiddenWalls ? 1 : 0}|${opts?.includeUnparentedWalls ? 1 : 0}`;
|
|
3486
|
+
let val = heightCache.get(key);
|
|
3487
|
+
if (val === undefined) {
|
|
3488
|
+
val = resolveLevelHeightM2(nodes, levelId, opts);
|
|
3489
|
+
heightCache.set(key, val);
|
|
3490
|
+
}
|
|
3491
|
+
return val;
|
|
3492
|
+
};
|
|
3493
|
+
const getWalls = (levelId, opts) => {
|
|
3494
|
+
const key = `${encodeLevelCacheKey(levelId)}|${opts?.includeHidden ? 1 : 0}|${opts?.includeUnparented ? 1 : 0}|${opts?.minLengthM ?? ""}|${opts?.defaultWallHeightM ?? ""}`;
|
|
3495
|
+
let val = wallCache.get(key);
|
|
3496
|
+
if (!val) {
|
|
3497
|
+
const extracted = extractCanonicalWalls2(nodes, levelId, opts);
|
|
3498
|
+
for (const wall of extracted) {
|
|
3499
|
+
Object.freeze(wall.openings);
|
|
3500
|
+
}
|
|
3501
|
+
val = Object.freeze(extracted);
|
|
3502
|
+
wallCache.set(key, val);
|
|
3503
|
+
}
|
|
3504
|
+
return val;
|
|
3505
|
+
};
|
|
3506
|
+
const getOpeningPrisms = (levelId) => {
|
|
3507
|
+
const key = encodeLevelCacheKey(levelId);
|
|
3508
|
+
let val = prismCache.get(key);
|
|
3509
|
+
if (!val) {
|
|
3510
|
+
val = Object.freeze(extractLevelOpeningPrisms2(getWalls(levelId)));
|
|
3511
|
+
prismCache.set(key, val);
|
|
3512
|
+
}
|
|
3513
|
+
return val;
|
|
3514
|
+
};
|
|
3515
|
+
const getFootprint = (levelId) => {
|
|
3516
|
+
const key = encodeLevelCacheKey(levelId);
|
|
3517
|
+
let val = footprintCache.get(key);
|
|
3518
|
+
if (!val) {
|
|
3519
|
+
const fp = compileLevelFootprint2(nodes, levelId, getWalls(levelId));
|
|
3520
|
+
Object.freeze(fp.boundedFaces);
|
|
3521
|
+
Object.freeze(fp.roomPolygons);
|
|
3522
|
+
for (const cluster of fp.clusters) {
|
|
3523
|
+
Object.freeze(cluster.slabIds);
|
|
3524
|
+
Object.freeze(cluster.wallIds);
|
|
3525
|
+
}
|
|
3526
|
+
Object.freeze(fp.clusters);
|
|
3527
|
+
val = Object.freeze(fp);
|
|
3528
|
+
footprintCache.set(key, val);
|
|
3529
|
+
}
|
|
3530
|
+
return val;
|
|
3531
|
+
};
|
|
3532
|
+
const getRoofShell = (levelId, opts) => {
|
|
3533
|
+
const includeHidden = opts?.includeHidden ?? false;
|
|
3534
|
+
const wallH = getLevelHeightM(levelId, includeHidden ? { includeHiddenWalls: true } : undefined);
|
|
3535
|
+
const key = `${encodeLevelCacheKey(levelId)}|${includeHidden ? 1 : 0}|${opts?.clampOverhangToEave ? 1 : 0}|${opts?.footprintPadM ?? ""}|${opts?.fallbackPitchRiseOver12 ?? ""}|${opts?.levelBaseY ?? ""}|${opts?.wallBounds ? `${opts.wallBounds.minX},${opts.wallBounds.maxX},${opts.wallBounds.minZ},${opts.wallBounds.maxZ}` : ""}`;
|
|
3536
|
+
let val = roofCache.get(key);
|
|
3537
|
+
if (!val) {
|
|
3538
|
+
const resolvedWallBounds = opts?.wallBounds ? opts.wallBounds : includeHidden ? null : getFootprint(levelId).bounds ?? null;
|
|
3539
|
+
const shell = compileLevelRoofShell2(nodes, levelId, wallH, {
|
|
3540
|
+
...opts,
|
|
3541
|
+
wallBounds: resolvedWallBounds
|
|
3542
|
+
});
|
|
3543
|
+
Object.freeze(shell.segments);
|
|
3544
|
+
val = Object.freeze(shell);
|
|
3545
|
+
roofCache.set(key, val);
|
|
3546
|
+
}
|
|
3547
|
+
return val;
|
|
3548
|
+
};
|
|
3549
|
+
const createOccupancyGrid = (levelId, opts) => {
|
|
3550
|
+
const obstacles = extractTradeOccupancyObstacles2(nodes, levelId, opts);
|
|
3551
|
+
return new TradeOccupancyGrid2(obstacles);
|
|
3552
|
+
};
|
|
3553
|
+
const anatomy = Object.freeze({
|
|
3554
|
+
nodes,
|
|
3555
|
+
levelIds,
|
|
3556
|
+
getLevelHeightM,
|
|
3557
|
+
getWalls,
|
|
3558
|
+
getOpeningPrisms,
|
|
3559
|
+
getRoofShell,
|
|
3560
|
+
getFootprint,
|
|
3561
|
+
createOccupancyGrid
|
|
3562
|
+
});
|
|
3563
|
+
anatomyCache.set(nodes, anatomy);
|
|
3564
|
+
return anatomy;
|
|
3565
|
+
}
|
|
3566
|
+
|
|
3567
|
+
export { resolveNodeLevelId2, belongsToLevel2, resolveLevelBuildingId2, areLevelsInSameBuilding2, levelSortOrder2, compareLevelsVertically2, isBasementLevel2, levelHasWalls2, resolveWallHeightM2, resolveLevelHeightM2, getLowerWalledLevelIds2, resolveLowerWalledLevelsSummary2, isGroundOrLowestWalledLevel2, isHighestWalledLevelInBuilding2, findImmediateLowerWalledLevel2, classifyOpeningKind2, extractCanonicalWallOpenings2, extractCanonicalWalls2, extractLevelOpeningPrisms2, countPathOpeningPrismHits2, findNearestWallToPoint2, distancePointToSegment2D2, pointInPolygon2D2, polygonArea2D2, polygonAreaAndCentroid2, polygonBounds2D2, clipSegmentToPolygon2D2, extractBoundedWallFaces2, isPointEnclosedByWalls2, extractBaseRoomPolygons2, computeLevelBounds2, clusterLevelFootprints2, isOutdoorOrUnwalledSlab2, compileLevelFootprint2, PLUMBING_PIPE_HALF_OD_M2, ELECTRICAL_CONDUIT_HALF_OD_M2, parseNominalInches2, resolveMepRunExtents2, extractBeamPrism2, extractColumnPrism2, getHvacEquipmentPrism2, getHvacEquipmentAabb2, getHvacPlenumPrism2, getHvacPlenumAabb2, getPlumbingFixturePrism2, getPlumbingFixtureAabb2, getPlumbingEquipmentPrism2, getPlumbingEquipmentAabb2, getElectricalDedicatedSpacePrisms2, extractEquipmentPrisms2, extractTradeOccupancyObstacles2, TradeOccupancyGrid2, shouldAdoptWallLessRoofLevel2, shouldAdoptRoofSegmentOntoLevel2, evaluateRoofSegmentSurfaceY2, extractCanonicalRoofSegments2, compileLevelRoofShell2, compileBuildingAnatomy2 };
|