@kruschdev/pascal-mep-kernel 0.1.0 → 0.1.1

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.
@@ -8,7 +8,7 @@ import {
8
8
  segmentIntersectsOrientedPrismY2,
9
9
  extractNodeYawRad2,
10
10
  round32
11
- } from "./chunk-jfzqztgh.js";
11
+ } from "./chunk-yd3hcm7n.js";
12
12
 
13
13
  // src/anatomy/levels.ts
14
14
  function resolveNodeLevelId2(nodes, node, options = {}) {
@@ -41,14 +41,12 @@ function belongsToLevel2(nodes, node, levelId, options = {}) {
41
41
  let cur = node;
42
42
  while (cur?.parentId) {
43
43
  const pid = cur.parentId;
44
- if (pid === levelId)
45
- return true;
46
44
  if (visited.has(pid))
47
45
  break;
48
46
  visited.add(pid);
49
47
  const parent = nodes[pid];
50
48
  if (!parent)
51
- return false;
49
+ return pid === levelId;
52
50
  if (parent.type === "level")
53
51
  return parent.id === levelId;
54
52
  cur = parent;
@@ -96,6 +94,19 @@ function levelSortOrder2(level) {
96
94
  return level.index;
97
95
  return 0;
98
96
  }
97
+ var QUANTIZE_LIMIT = 1e9;
98
+ function normalizeVerticalScalar(value) {
99
+ return typeof value === "number" && Number.isFinite(value) ? value : 0;
100
+ }
101
+ function compareVerticalScalars(aRaw, bRaw) {
102
+ const a = normalizeVerticalScalar(aRaw);
103
+ const b = normalizeVerticalScalar(bRaw);
104
+ if (Math.abs(a) < QUANTIZE_LIMIT && Math.abs(b) < QUANTIZE_LIMIT) {
105
+ const diff = Math.round(a * 1e6) - Math.round(b * 1e6);
106
+ return diff !== 0 ? diff / 1e6 : 0;
107
+ }
108
+ return a < b ? -1 : a > b ? 1 : 0;
109
+ }
99
110
  function compareLevelsVertically2(a, b) {
100
111
  if (!a && !b)
101
112
  return 0;
@@ -103,27 +114,19 @@ function compareLevelsVertically2(a, b) {
103
114
  return -1;
104
115
  if (!b)
105
116
  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);
117
+ const pDiff = compareVerticalScalars(levelSortOrder2(a), levelSortOrder2(b));
118
+ if (pDiff !== 0)
119
+ return pDiff;
120
+ const eDiff = compareVerticalScalars(a.elevation, b.elevation);
121
+ if (eDiff !== 0)
122
+ return eDiff;
123
+ const bDiff = compareVerticalScalars(a.baseElevation, b.baseElevation);
124
+ if (bDiff !== 0)
125
+ return bDiff;
126
+ const iDiff = compareVerticalScalars(a.index, b.index);
127
+ if (iDiff !== 0)
128
+ return iDiff;
129
+ return 0;
127
130
  }
128
131
  function sameBuildingId(bA, bB, strictNullBuilding) {
129
132
  if (strictNullBuilding)
@@ -132,11 +135,21 @@ function sameBuildingId(bA, bB, strictNullBuilding) {
132
135
  return false;
133
136
  return true;
134
137
  }
138
+ var levelHierarchyCache = new WeakMap;
135
139
  function buildLevelHierarchyIndex(nodes, includeHiddenWalls) {
140
+ let entry = levelHierarchyCache.get(nodes);
141
+ if (!entry) {
142
+ entry = {};
143
+ levelHierarchyCache.set(nodes, entry);
144
+ }
145
+ const cached = includeHiddenWalls ? entry.all : entry.visible;
146
+ if (cached)
147
+ return cached;
136
148
  const levels = [];
137
149
  const buildingByChildLevel = new Map;
138
150
  const walledLevelIds = new Set;
139
151
  const maxWallHeightByLevel = new Map;
152
+ const maxHiddenWallHeightByLevel = new Map;
140
153
  for (const n of Object.values(nodes)) {
141
154
  if (n.type === "level") {
142
155
  levels.push(n);
@@ -147,14 +160,21 @@ function buildLevelHierarchyIndex(nodes, includeHiddenWalls) {
147
160
  }
148
161
  }
149
162
  } else if (n.type === "wall") {
150
- if (!includeHiddenWalls && n.visible === false)
151
- continue;
152
163
  const lid = resolveNodeLevelId2(nodes, n, { allowDanglingParentId: false });
153
164
  if (!lid)
154
165
  continue;
155
- walledLevelIds.add(lid);
156
166
  const h = n.height;
157
- if (typeof h === "number" && Number.isFinite(h) && h > 1.5 && h <= 12) {
167
+ const validFullHeight = typeof h === "number" && Number.isFinite(h) && h > 1.5 && h <= 12;
168
+ if (n.visible === false && !includeHiddenWalls) {
169
+ if (validFullHeight) {
170
+ const prevHidden = maxHiddenWallHeightByLevel.get(lid) ?? 0;
171
+ if (h > prevHidden)
172
+ maxHiddenWallHeightByLevel.set(lid, h);
173
+ }
174
+ continue;
175
+ }
176
+ walledLevelIds.add(lid);
177
+ if (validFullHeight) {
158
178
  const prev = maxWallHeightByLevel.get(lid) ?? 0;
159
179
  if (h > prev)
160
180
  maxWallHeightByLevel.set(lid, h);
@@ -180,7 +200,19 @@ function buildLevelHierarchyIndex(nodes, includeHiddenWalls) {
180
200
  }
181
201
  buildingByLevelId.set(lvl.id, bId);
182
202
  }
183
- return { levels, buildingByLevelId, walledLevelIds, maxWallHeightByLevel };
203
+ const idx = {
204
+ levels,
205
+ buildingByLevelId,
206
+ walledLevelIds,
207
+ maxWallHeightByLevel,
208
+ maxHiddenWallHeightByLevel
209
+ };
210
+ if (includeHiddenWalls) {
211
+ entry.all = idx;
212
+ } else {
213
+ entry.visible = idx;
214
+ }
215
+ return idx;
184
216
  }
185
217
  function isBasementLevel2(level) {
186
218
  if (!level)
@@ -222,25 +254,52 @@ function resolveLevelHeightM2(nodes, levelId = null, options = {}) {
222
254
  const includeHiddenWalls = options.includeHiddenWalls ?? false;
223
255
  const includeUnparentedWalls = options.includeUnparentedWalls ?? false;
224
256
  let maxWallH = 0;
257
+ let maxHiddenWallH = 0;
225
258
  for (const n of Object.values(nodes)) {
226
259
  if (n.type !== "wall")
227
260
  continue;
228
- if (!includeHiddenWalls && n.visible === false)
229
- continue;
230
261
  if (levelId && !belongsToLevel2(nodes, n, levelId, { includeUnparented: includeUnparentedWalls })) {
231
262
  continue;
232
263
  }
233
264
  const h = n.height;
234
- if (typeof h === "number" && Number.isFinite(h) && h > 1.5 && h <= 12 && h > maxWallH) {
265
+ if (typeof h !== "number" || !Number.isFinite(h) || h <= 1.5 || h > 12)
266
+ continue;
267
+ if (n.visible === false) {
268
+ if (includeHiddenWalls) {
269
+ if (h > maxWallH)
270
+ maxWallH = h;
271
+ } else if (h > maxHiddenWallH) {
272
+ maxHiddenWallH = h;
273
+ }
274
+ } else if (h > maxWallH) {
235
275
  maxWallH = h;
236
276
  }
237
277
  }
238
278
  let resolved = maxWallH;
239
279
  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) {
280
+ let lh;
281
+ if (levelId) {
282
+ const raw = nodes[levelId]?.height;
283
+ if (typeof raw === "number" && Number.isFinite(raw) && raw > 1.5 && raw <= 12) {
284
+ lh = raw;
285
+ }
286
+ } else {
287
+ let maxLevelH = 0;
288
+ for (const n of Object.values(nodes)) {
289
+ if (n.type !== "level")
290
+ continue;
291
+ const h = n.height;
292
+ if (typeof h === "number" && Number.isFinite(h) && h > 1.5 && h <= 12 && h > maxLevelH) {
293
+ maxLevelH = h;
294
+ }
295
+ }
296
+ if (maxLevelH > 0)
297
+ lh = maxLevelH;
298
+ }
299
+ if (lh !== undefined) {
243
300
  resolved = lh;
301
+ } else if (maxHiddenWallH > 0) {
302
+ resolved = maxHiddenWallH;
244
303
  } else {
245
304
  resolved = defaultHeightM;
246
305
  }
@@ -310,10 +369,13 @@ function resolveLowerWalledLevelsSummary2(nodes, levelId, options = {}) {
310
369
  let cumulativeBelowHeightM = 0;
311
370
  for (const lvl of lower) {
312
371
  const maxWallH = idx.maxWallHeightByLevel.get(lvl.id) ?? 0;
372
+ const maxHiddenWallH = idx.maxHiddenWallHeightByLevel.get(lvl.id) ?? 0;
313
373
  if (maxWallH > 0) {
314
374
  cumulativeBelowHeightM += maxWallH;
315
375
  } else if (typeof lvl.height === "number" && Number.isFinite(lvl.height) && lvl.height > 1.5 && lvl.height <= 12) {
316
376
  cumulativeBelowHeightM += lvl.height;
377
+ } else if (maxHiddenWallH > 0) {
378
+ cumulativeBelowHeightM += maxHiddenWallH;
317
379
  } else {
318
380
  cumulativeBelowHeightM += 2.7;
319
381
  }
@@ -367,8 +429,8 @@ function findImmediateLowerWalledLevel2(nodes, levelId, options = {}) {
367
429
  }
368
430
 
369
431
  // 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/;
432
+ var ITEM_DOOR_RE = /(?:^|[^a-z0-9])doors?(?:$|[^a-z0-9])|garage[-_\s]?doors?|sliding[-_\s]?doors?|patio[-_\s]?doors?/;
433
+ var ITEM_WINDOW_RE = /(?:^|[^a-z0-9])windows?(?:$|[^a-z0-9])/;
372
434
  function classifyOpeningKind2(node, opts) {
373
435
  if (opts?.includeHidden === false && node.visible === false)
374
436
  return null;
@@ -403,7 +465,7 @@ function extractCanonicalWallOpeningsFromCandidates(nodes, wall, candidates, chi
403
465
  const ex = Number(wall.end[0]);
404
466
  const ez = Number(wall.end[1]);
405
467
  const wallLenM = Math.hypot(ex - sx, ez - sz);
406
- if (!Number.isFinite(wallLenM) || wallLenM < 0.1)
468
+ if (!Number.isFinite(wallLenM) || wallLenM < 0.05)
407
469
  return [];
408
470
  const ux = (ex - sx) / wallLenM;
409
471
  const uz = (ez - sz) / wallLenM;
@@ -426,16 +488,17 @@ function extractCanonicalWallOpeningsFromCandidates(nodes, wall, candidates, chi
426
488
  const syScale = typeof scale[1] === "number" && Number.isFinite(scale[1]) && scale[1] > 0 ? scale[1] : 1;
427
489
  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
490
  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));
491
+ const nominalWidthM = Math.min(wallLenM, Math.max(0.2, rawW));
430
492
  const pos = Array.isArray(o.position) ? o.position : undefined;
431
493
  const itemRotY = extractNodeYawRad2(o.rotation);
432
494
  const assetOffset = Array.isArray(asset?.offset) ? asset.offset : undefined;
433
495
  let rawUCenter = wallLenM / 2;
434
496
  const isWallChild = o.parentId === wall.id || childIds.has(o.id);
497
+ const localPerpLimit = Math.max(0.45, thicknessM / 2 + 0.25);
435
498
  if (pos && typeof pos[0] === "number" && Number.isFinite(pos[0])) {
436
499
  const px = pos[0];
437
500
  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) {
501
+ if (isWallChild && Math.abs(pz) < localPerpLimit && px >= -0.25 && px <= wallLenM + 0.25) {
439
502
  rawUCenter = px;
440
503
  if (o.type === "item" && assetOffset) {
441
504
  const ox = typeof assetOffset[0] === "number" && Number.isFinite(assetOffset[0]) ? assetOffset[0] : 0;
@@ -470,17 +533,19 @@ function extractCanonicalWallOpeningsFromCandidates(nodes, wall, candidates, chi
470
533
  } else {
471
534
  if (rawPosY !== null && rawPosY > 0.02) {
472
535
  rawSillYM = Math.max(0, rawPosY);
473
- } else if (rawPosY === 0 && rawH >= wallHeightM - 0.35) {
474
- rawSillYM = 0;
536
+ } else if (rawPosY !== null && rawPosY >= 0 && rawPosY <= 0.02 && rawH >= wallHeightM - 0.35) {
537
+ rawSillYM = rawPosY;
475
538
  } else {
476
539
  rawSillYM = 0.9144;
477
540
  }
478
541
  }
479
542
  rawSillYM = Math.max(0, Math.min(Math.max(0, wallHeightM - 0.2), rawSillYM));
480
543
  }
481
- const headYM = Math.max(rawSillYM + 0.1, Math.min(wallHeightM, rawSillYM + rawH));
544
+ const headYM = Math.min(wallHeightM, Math.max(rawSillYM + 0.1, rawSillYM + rawH));
482
545
  const heightM = headYM - rawSillYM;
483
- const framedSillYM = kind === "door" ? 0 : Math.max(0.08, rawSillYM);
546
+ if (heightM < 0.02)
547
+ continue;
548
+ const framedSillYM = kind === "door" ? 0 : Math.min(headYM, Math.max(0.08, rawSillYM));
484
549
  const centerX = sx + ux * uCenterM;
485
550
  const centerZ = sz + uz * uCenterM;
486
551
  const jambInset = span > 0.1 ? 0.04 : 0;
@@ -552,7 +617,7 @@ function extractCanonicalWallOpenings2(nodes, wall, opts = {}) {
552
617
  function extractCanonicalWalls2(nodes, levelId, opts = {}) {
553
618
  const includeHidden = opts.includeHidden ?? false;
554
619
  const includeUnparented = opts.includeUnparented ?? false;
555
- const minLengthM = opts.minLengthM ?? 0.1;
620
+ const minLengthM = typeof opts.minLengthM === "number" && Number.isFinite(opts.minLengthM) ? Math.max(0.0001, opts.minLengthM) : 0.1;
556
621
  const defaultWallHeightM = typeof opts.defaultWallHeightM === "number" && Number.isFinite(opts.defaultWallHeightM) && opts.defaultWallHeightM > 0 ? opts.defaultWallHeightM : resolveLevelHeightM2(nodes, levelId ?? null, {
557
622
  includeHiddenWalls: includeHidden,
558
623
  includeUnparentedWalls: includeUnparented,
@@ -735,6 +800,9 @@ function normalizeTextNfd(text) {
735
800
  return text.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase().replace(/\s+/g, " ").trim();
736
801
  }
737
802
  function distancePointToSegment2D2(px, pz, ax, az, bx, bz) {
803
+ if (!Number.isFinite(px) || !Number.isFinite(pz) || !Number.isFinite(ax) || !Number.isFinite(az) || !Number.isFinite(bx) || !Number.isFinite(bz)) {
804
+ return Number.POSITIVE_INFINITY;
805
+ }
738
806
  const dx = bx - ax;
739
807
  const dz = bz - az;
740
808
  const lenSq = dx * dx + dz * dz;
@@ -745,8 +813,9 @@ function distancePointToSegment2D2(px, pz, ax, az, bx, bz) {
745
813
  }
746
814
  function pointInPolygon2D2(px, pz, polygon, edgeTol = 0) {
747
815
  const n = polygon.length;
748
- if (n < 3)
816
+ if (n < 3 || !Number.isFinite(px) || !Number.isFinite(pz))
749
817
  return false;
818
+ const safeEdgeTol = Number.isFinite(edgeTol) && edgeTol > 0 ? edgeTol : 0;
750
819
  let inside = false;
751
820
  for (let i = 0, j = n - 1;i < n; j = i++) {
752
821
  const pi = polygon[i];
@@ -755,10 +824,13 @@ function pointInPolygon2D2(px, pz, polygon, edgeTol = 0) {
755
824
  const zi = pi[1];
756
825
  const xj = pj[0];
757
826
  const zj = pj[1];
758
- if (edgeTol > 0 && distancePointToSegment2D2(px, pz, xj, zj, xi, zi) <= edgeTol) {
827
+ if (!Number.isFinite(xi) || !Number.isFinite(zi) || !Number.isFinite(xj) || !Number.isFinite(zj)) {
828
+ return false;
829
+ }
830
+ if (safeEdgeTol > 0 && distancePointToSegment2D2(px, pz, xj, zj, xi, zi) <= safeEdgeTol) {
759
831
  return true;
760
832
  }
761
- const intersect = zi > pz !== zj > pz && px < (xj - xi) * (pz - zi) / (zj - zi + 0.000000000000000001) + xi;
833
+ const intersect = zi > pz !== zj > pz && px < (xj - xi) * (pz - zi) / (zj - zi) + xi;
762
834
  if (intersect)
763
835
  inside = !inside;
764
836
  }
@@ -772,14 +844,24 @@ function polygonArea2D2(polygon) {
772
844
  for (let i = 0;i < n; i++) {
773
845
  const a = polygon[i];
774
846
  const b = polygon[(i + 1) % n];
847
+ if (!Number.isFinite(a[0]) || !Number.isFinite(a[1]) || !Number.isFinite(b[0]) || !Number.isFinite(b[1])) {
848
+ return 0;
849
+ }
775
850
  sum += a[0] * b[1] - b[0] * a[1];
776
851
  }
777
- return Math.abs(sum) * 0.5;
852
+ const area = Math.abs(sum) * 0.5;
853
+ return Number.isFinite(area) ? area : 0;
778
854
  }
779
855
  function polygonAreaAndCentroid2(polygon) {
780
856
  const n = polygon.length;
781
857
  if (n === 0)
782
858
  return { areaSqM: 0, centroid: [0, 0] };
859
+ for (let i = 0;i < n; i++) {
860
+ const p = polygon[i];
861
+ if (!Number.isFinite(p[0]) || !Number.isFinite(p[1])) {
862
+ return { areaSqM: 0, centroid: [0, 0] };
863
+ }
864
+ }
783
865
  if (n < 3) {
784
866
  let avgX = 0;
785
867
  let avgZ = 0;
@@ -801,7 +883,7 @@ function polygonAreaAndCentroid2(polygon) {
801
883
  cz += (a[1] + b[1]) * cross;
802
884
  }
803
885
  const areaSqM = Math.abs(signed) * 0.5;
804
- if (areaSqM < 0.000001) {
886
+ if (!Number.isFinite(areaSqM) || areaSqM < 0.000001) {
805
887
  let avgX = 0;
806
888
  let avgZ = 0;
807
889
  for (const p of polygon) {
@@ -810,7 +892,18 @@ function polygonAreaAndCentroid2(polygon) {
810
892
  }
811
893
  return { areaSqM: 0, centroid: [avgX / n, avgZ / n] };
812
894
  }
813
- return { areaSqM, centroid: [cx / (3 * signed), cz / (3 * signed)] };
895
+ const outX = cx / (3 * signed);
896
+ const outZ = cz / (3 * signed);
897
+ if (!Number.isFinite(outX) || !Number.isFinite(outZ)) {
898
+ let avgX = 0;
899
+ let avgZ = 0;
900
+ for (const p of polygon) {
901
+ avgX += p[0];
902
+ avgZ += p[1];
903
+ }
904
+ return { areaSqM, centroid: [avgX / n, avgZ / n] };
905
+ }
906
+ return { areaSqM, centroid: [outX, outZ] };
814
907
  }
815
908
  function polygonBounds2D2(polygon) {
816
909
  let minX = Number.POSITIVE_INFINITY;
@@ -818,8 +911,10 @@ function polygonBounds2D2(polygon) {
818
911
  let minZ = Number.POSITIVE_INFINITY;
819
912
  let maxZ = Number.NEGATIVE_INFINITY;
820
913
  for (const pt of polygon) {
821
- const x = pt[0];
822
- const z = pt[1];
914
+ if (!Array.isArray(pt) || pt.length < 2)
915
+ continue;
916
+ const x = Number(pt[0]);
917
+ const z = Number(pt[1]);
823
918
  if (!Number.isFinite(x) || !Number.isFinite(z))
824
919
  continue;
825
920
  if (x < minX)
@@ -837,13 +932,22 @@ function polygonBounds2D2(polygon) {
837
932
  }
838
933
  function clipSegmentToPolygon2D2(a, b, polygon) {
839
934
  const n = polygon.length;
840
- if (n < 3)
935
+ if (n < 3 || !Number.isFinite(a[0]) || !Number.isFinite(a[1]) || !Number.isFinite(b[0]) || !Number.isFinite(b[1])) {
841
936
  return [];
937
+ }
938
+ for (let i = 0;i < n; i++) {
939
+ const v = polygon[i];
940
+ if (!Array.isArray(v) || v.length < 2 || !Number.isFinite(v[0]) || !Number.isFinite(v[1])) {
941
+ return [];
942
+ }
943
+ }
842
944
  const dx = b[0] - a[0];
843
945
  const dz = b[1] - a[1];
844
946
  const segLen = Math.hypot(dx, dz);
845
- if (segLen < 0.000001)
947
+ if (!Number.isFinite(segLen) || segLen < 0.000001)
846
948
  return [];
949
+ const segLenSq = segLen * segLen;
950
+ const boundaryTol = 0.000001;
847
951
  const ts = [0, 1];
848
952
  for (let i = 0;i < n; i++) {
849
953
  const v0 = polygon[i];
@@ -857,15 +961,12 @@ function clipSegmentToPolygon2D2(a, b, polygon) {
857
961
  if (t > 0.0000001 && t < 1 - 0.0000001 && u >= -0.000001 && u <= 1 + 0.000001) {
858
962
  ts.push(t);
859
963
  }
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
- }
964
+ }
965
+ const perp = Math.abs((v0[0] - a[0]) * dz - (v0[1] - a[1]) * dx) / segLen;
966
+ if (perp <= boundaryTol) {
967
+ const tProj = ((v0[0] - a[0]) * dx + (v0[1] - a[1]) * dz) / segLenSq;
968
+ if (tProj > 0.0000001 && tProj < 1 - 0.0000001)
969
+ ts.push(tProj);
869
970
  }
870
971
  }
871
972
  ts.sort((x, y) => x - y);
@@ -881,7 +982,11 @@ function clipSegmentToPolygon2D2(a, b, polygon) {
881
982
  if (t1 - t0 < 0.000001)
882
983
  continue;
883
984
  const tm = (t0 + t1) * 0.5;
884
- if (pointInPolygon2D2(a[0] + dx * tm, a[1] + dz * tm, polygon, 0.0001)) {
985
+ const mx = a[0] + dx * tm;
986
+ const mz = a[1] + dz * tm;
987
+ const insideStrict = pointInPolygon2D2(mx, mz, polygon, 0);
988
+ const insideBoundary = !insideStrict && pointInPolygon2D2(mx, mz, polygon, boundaryTol) && pointInPolygon2D2(a[0] + dx * t0, a[1] + dz * t0, polygon, boundaryTol) && pointInPolygon2D2(a[0] + dx * t1, a[1] + dz * t1, polygon, boundaryTol);
989
+ if (insideStrict || insideBoundary) {
885
990
  const prev = kept[kept.length - 1];
886
991
  if (prev && t0 - prev[1] <= 0.00001)
887
992
  prev[1] = t1;
@@ -951,7 +1056,7 @@ function extractBoundedWallFaces2(walls) {
951
1056
  }
952
1057
  const nodes2D = [];
953
1058
  const outgoing = [];
954
- const VERTEX_MERGE_EPS = 0.2;
1059
+ const VERTEX_MERGE_EPS = JUNCTION_SNAP;
955
1060
  const findOrAddVertex = (x, z) => {
956
1061
  for (let idx = 0;idx < nodes2D.length; idx++) {
957
1062
  const v = nodes2D[idx];
@@ -1367,6 +1472,15 @@ function clusterLevelFootprintsFromNodes(nodes, levelId, walls, slabs, gapTolera
1367
1472
  return [];
1368
1473
  const fallbackH = "heightM" in b && typeof b.heightM === "number" ? b.heightM : resolveLevelHeightM2(nodes, levelId);
1369
1474
  const globalAvgH = wallBoxes.length > 0 ? wallBoxes.reduce((s, w) => s + w.height, 0) / wallBoxes.length : fallbackH;
1475
+ let fallbackMinElev = Number.POSITIVE_INFINITY;
1476
+ for (const wb of wallBoxes) {
1477
+ if (wb.elev < fallbackMinElev)
1478
+ fallbackMinElev = wb.elev;
1479
+ }
1480
+ for (const sb of slabBoxes) {
1481
+ if (sb.elev < fallbackMinElev)
1482
+ fallbackMinElev = sb.elev;
1483
+ }
1370
1484
  return [
1371
1485
  {
1372
1486
  minX: b.minX,
@@ -1379,7 +1493,7 @@ function clusterLevelFootprintsFromNodes(nodes, levelId, walls, slabs, gapTolera
1379
1493
  centerX: (b.minX + b.maxX) / 2,
1380
1494
  centerZ: (b.minZ + b.maxZ) / 2,
1381
1495
  avgWallHeightM: globalAvgH,
1382
- baseElevationM: 0,
1496
+ baseElevationM: Number.isFinite(fallbackMinElev) ? fallbackMinElev : 0,
1383
1497
  slabIds: slabBoxes.map((s) => s.id),
1384
1498
  wallIds: wallBoxes.map((w) => w.id)
1385
1499
  }
@@ -1451,7 +1565,18 @@ function isOutdoorOrUnwalledSlab2(slab, walls, outdoorZonePolys = [], baseWallEl
1451
1565
  return true;
1452
1566
  if (!Array.isArray(slab.polygon) || slab.polygon.length < 3)
1453
1567
  return true;
1454
- const poly = slab.polygon;
1568
+ const poly = [];
1569
+ for (const pt of slab.polygon) {
1570
+ if (!Array.isArray(pt) || pt.length < 2)
1571
+ continue;
1572
+ const x = Number(pt[0]);
1573
+ const z = Number(pt[1]);
1574
+ if (Number.isFinite(x) && Number.isFinite(z)) {
1575
+ poly.push([x, z]);
1576
+ }
1577
+ }
1578
+ if (poly.length < 3)
1579
+ return true;
1455
1580
  const b = polygonBounds2D2(poly);
1456
1581
  if (!b)
1457
1582
  return true;
@@ -1567,8 +1692,27 @@ function compileLevelFootprint2(nodes, levelId = null, walls) {
1567
1692
  const levelBounds2D = Number.isFinite(bMinX) && Number.isFinite(bMinZ) ? { minX: bMinX, maxX: bMaxX, minZ: bMinZ, maxZ: bMaxZ } : null;
1568
1693
  const roomPolygons = buildBaseRoomPolygonsFromNodes(roomNodes);
1569
1694
  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);
1695
+ const defaultExteriorEntries = roomPolygons.filter((r) => OUTDOOR_ZONE_RE.test(r.normalizedText)).map((r) => ({ polygon: r.polygon, bounds: r.bounds }));
1696
+ const defaultInteriorEntries = roomPolygons.filter((r) => !OUTDOOR_ZONE_RE.test(r.normalizedText)).map((r) => ({ polygon: r.polygon, bounds: r.bounds }));
1697
+ const pointInRoomList = (px, pz, overridePolys, defaultEntries) => {
1698
+ if (overridePolys !== undefined) {
1699
+ for (let i = 0;i < overridePolys.length; i++) {
1700
+ const poly = overridePolys[i];
1701
+ if (poly.length >= 3 && pointInPolygon2D2(px, pz, poly))
1702
+ return true;
1703
+ }
1704
+ return false;
1705
+ }
1706
+ for (let i = 0;i < defaultEntries.length; i++) {
1707
+ const e = defaultEntries[i];
1708
+ const eb = e.bounds;
1709
+ if (px < eb.minX || px > eb.maxX || pz < eb.minZ || pz > eb.maxZ)
1710
+ continue;
1711
+ if (e.polygon.length >= 3 && pointInPolygon2D2(px, pz, e.polygon))
1712
+ return true;
1713
+ }
1714
+ return false;
1715
+ };
1572
1716
  const boundedFaceAabbs = boundedFaces.map((f) => polygonBounds2D2(f));
1573
1717
  const pointInAnyBoundedFace = (px, pz) => {
1574
1718
  for (let i = 0;i < boundedFaces.length; i++) {
@@ -1604,15 +1748,15 @@ function compileLevelFootprint2(nodes, levelId = null, walls) {
1604
1748
  if (!Number.isFinite(px) || !Number.isFinite(pz))
1605
1749
  return false;
1606
1750
  const precedence = opts?.precedence ?? "wall-first";
1607
- const outdoorPolys = opts?.outdoorPolygons ?? defaultExteriorPolys;
1608
- const interiorPolys = opts?.interiorPolygons ?? defaultInteriorPolys;
1751
+ const outdoorOverride = opts?.outdoorPolygons;
1752
+ const interiorOverride = opts?.interiorPolygons;
1753
+ const outdoorCount = outdoorOverride ? outdoorOverride.length : defaultExteriorEntries.length;
1754
+ const interiorCount = interiorOverride ? interiorOverride.length : defaultInteriorEntries.length;
1609
1755
  if (precedence === "room-first") {
1610
1756
  const wallTol = opts?.wallToleranceM ?? 0.45;
1611
- const inInteriorRoom = interiorPolys.some((poly) => poly.length >= 3 && pointInPolygon2D2(px, pz, poly));
1612
- if (inInteriorRoom)
1757
+ if (pointInRoomList(px, pz, interiorOverride, defaultInteriorEntries))
1613
1758
  return true;
1614
- const inExteriorRoom = outdoorPolys.some((poly) => poly.length >= 3 && pointInPolygon2D2(px, pz, poly));
1615
- if (inExteriorRoom)
1759
+ if (pointInRoomList(px, pz, outdoorOverride, defaultExteriorEntries))
1616
1760
  return false;
1617
1761
  if (effectiveWalls.length >= 3) {
1618
1762
  if (wallTol > 0) {
@@ -1628,15 +1772,15 @@ function compileLevelFootprint2(nodes, levelId = null, walls) {
1628
1772
  }
1629
1773
  return isPointEnclosedByWalls2(px, pz, effectiveWalls);
1630
1774
  }
1631
- return interiorPolys.length === 0 && outdoorPolys.length === 0;
1775
+ return interiorCount === 0 && outdoorCount === 0;
1632
1776
  }
1633
1777
  const wallTol = opts?.wallToleranceM ?? 0.025;
1634
1778
  if (effectiveWalls.length === 0) {
1635
- if (outdoorPolys.some((poly) => poly.length >= 3 && pointInPolygon2D2(px, pz, poly))) {
1779
+ if (pointInRoomList(px, pz, outdoorOverride, defaultExteriorEntries)) {
1636
1780
  return false;
1637
1781
  }
1638
- if (interiorPolys.length > 0) {
1639
- return interiorPolys.some((poly) => poly.length >= 3 && pointInPolygon2D2(px, pz, poly));
1782
+ if (interiorCount > 0) {
1783
+ return pointInRoomList(px, pz, interiorOverride, defaultInteriorEntries);
1640
1784
  }
1641
1785
  return true;
1642
1786
  }
@@ -1644,10 +1788,10 @@ function compileLevelFootprint2(nodes, levelId = null, walls) {
1644
1788
  if (proj && proj.distM <= proj.wall.thicknessM / 2 + wallTol) {
1645
1789
  return true;
1646
1790
  }
1647
- if (outdoorPolys.some((poly) => poly.length >= 3 && pointInPolygon2D2(px, pz, poly))) {
1791
+ if (pointInRoomList(px, pz, outdoorOverride, defaultExteriorEntries)) {
1648
1792
  return false;
1649
1793
  }
1650
- if (interiorPolys.some((poly) => poly.length >= 3 && pointInPolygon2D2(px, pz, poly))) {
1794
+ if (pointInRoomList(px, pz, interiorOverride, defaultInteriorEntries)) {
1651
1795
  return true;
1652
1796
  }
1653
1797
  if (boundedFacesComplete) {
@@ -1664,7 +1808,7 @@ function compileLevelFootprint2(nodes, levelId = null, walls) {
1664
1808
  }
1665
1809
  return true;
1666
1810
  };
1667
- const isSegmentInside = (a, b, opts) => {
1811
+ const isSegmentInsideWithEndpointSkip = (a, b, opts, allowStartSkip, allowEndSkip) => {
1668
1812
  if (!isVec32(a) || !isVec32(b))
1669
1813
  return false;
1670
1814
  const dx = b[0] - a[0];
@@ -1673,20 +1817,33 @@ function compileLevelFootprint2(nodes, levelId = null, walls) {
1673
1817
  if (!Number.isFinite(segLenXZ))
1674
1818
  return false;
1675
1819
  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;
1820
+ const skipMargin = typeof rawSkip === "number" && Number.isFinite(rawSkip) && rawSkip > 0 ? rawSkip : 0;
1821
+ const startSkip = allowStartSkip ? skipMargin : 0;
1822
+ const endSkip = allowEndSkip ? skipMargin : 0;
1823
+ if (startSkip > 0 || endSkip > 0) {
1680
1824
  const aInside = isPointInside(a[0], a[2], opts);
1681
1825
  const bInside = isPointInside(b[0], b[2], opts);
1826
+ if (!aInside && startSkip === 0)
1827
+ return false;
1828
+ if (!bInside && endSkip === 0)
1829
+ return false;
1830
+ if (segLenXZ < 0.000001) {
1831
+ return aInside || bInside;
1832
+ }
1833
+ const tStart = aInside ? 0 : Math.min(1, startSkip / segLenXZ);
1834
+ const tEnd = bInside ? 1 : Math.max(0, 1 - endSkip / segLenXZ);
1835
+ if (tStart > tEnd + 0.000000001) {
1836
+ return isPointInside((a[0] + b[0]) * 0.5, (a[2] + b[2]) * 0.5, opts);
1837
+ }
1682
1838
  const rawStep = opts?.stepM;
1683
1839
  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)
1840
+ const spanLen = (tEnd - tStart) * segLenXZ;
1841
+ const steps = spanLen < 0.000001 ? 0 : Math.max(1, Math.min(1024, Math.ceil(spanLen / Math.max(0.1, stepM))));
1842
+ for (let s = 0;s <= steps; s++) {
1843
+ const t = steps === 0 ? tStart : tStart + (tEnd - tStart) * s / steps;
1844
+ if (s === 0 && t === 0 && aInside)
1688
1845
  continue;
1689
- if (!bInside && (1 - t) * segLenXZ <= skipMargin)
1846
+ if (s === steps && t === 1 && bInside)
1690
1847
  continue;
1691
1848
  if (!isPointInside(a[0] + dx * t, a[2] + dz * t, opts))
1692
1849
  return false;
@@ -1694,11 +1851,11 @@ function compileLevelFootprint2(nodes, levelId = null, walls) {
1694
1851
  return true;
1695
1852
  }
1696
1853
  if (segLenXZ < 0.02) {
1697
- return isPointInside(a[0], a[2], opts);
1854
+ return isPointInside(a[0], a[2], opts) && isPointInside(b[0], b[2], opts);
1698
1855
  }
1699
1856
  const rawStep = opts?.stepM;
1700
1857
  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))));
1858
+ const steps = Math.max(2, Math.min(1024, Math.ceil(segLenXZ / Math.max(0.1, stepM))));
1702
1859
  for (let s = 0;s <= steps; s++) {
1703
1860
  const f = s / steps;
1704
1861
  if (!isPointInside(a[0] + dx * f, a[2] + dz * f, opts))
@@ -1706,6 +1863,7 @@ function compileLevelFootprint2(nodes, levelId = null, walls) {
1706
1863
  }
1707
1864
  return true;
1708
1865
  };
1866
+ const isSegmentInside = (a, b, opts) => isSegmentInsideWithEndpointSkip(a, b, opts, opts?.allowStartExteriorSkip !== false, opts?.allowEndExteriorSkip !== false);
1709
1867
  const pathCrossesOutdoorVoid = (path, opts) => {
1710
1868
  if (path.length < 2)
1711
1869
  return false;
@@ -1713,13 +1871,23 @@ function compileLevelFootprint2(nodes, levelId = null, walls) {
1713
1871
  ...opts,
1714
1872
  skipExteriorEndpointMarginM: opts?.precedence === "room-first" ? 0.35 : 0
1715
1873
  };
1874
+ const allowStart = effectiveOpts.allowStartExteriorSkip !== false;
1875
+ const allowEnd = effectiveOpts.allowEndExteriorSkip !== false;
1716
1876
  for (let i = 1;i < path.length; i++) {
1717
- if (!isSegmentInside(path[i - 1], path[i], effectiveOpts))
1877
+ if (!isSegmentInsideWithEndpointSkip(path[i - 1], path[i], effectiveOpts, allowStart && i === 1, allowEnd && i === path.length - 1)) {
1718
1878
  return true;
1879
+ }
1719
1880
  }
1720
1881
  return false;
1721
1882
  };
1722
1883
  const clampPointInset = (px, pz, insetM = 0.12, opts) => {
1884
+ if (!Number.isFinite(px) || !Number.isFinite(pz)) {
1885
+ if (bounds) {
1886
+ return [round32((bounds.minX + bounds.maxX) * 0.5), round32((bounds.minZ + bounds.maxZ) * 0.5)];
1887
+ }
1888
+ return [0, 0];
1889
+ }
1890
+ const safeInsetM = typeof insetM === "number" && Number.isFinite(insetM) ? insetM : 0.12;
1723
1891
  if (effectiveWalls.length === 0)
1724
1892
  return [round32(px), round32(pz)];
1725
1893
  if (isPointInside(px, pz, opts)) {
@@ -1728,7 +1896,7 @@ function compileLevelFootprint2(nodes, levelId = null, walls) {
1728
1896
  const proj = findNearestWallToPoint2(px, pz, effectiveWalls);
1729
1897
  if (!proj)
1730
1898
  return [round32(px), round32(pz)];
1731
- const stepOff = proj.wall.thicknessM / 2 + Math.max(0.04, insetM);
1899
+ const stepOff = proj.wall.thicknessM / 2 + Math.max(0.04, safeInsetM);
1732
1900
  const posCand = [
1733
1901
  round32(proj.projectedX + proj.wall.nx * stepOff),
1734
1902
  round32(proj.projectedZ + proj.wall.nz * stepOff)
@@ -1789,9 +1957,26 @@ var STRUCTURAL_CLEARANCE_M = 0.03;
1789
1957
  var HVAC_EQUIPMENT_CLEARANCE_M = 0.15;
1790
1958
  var ELECTRICAL_CONDUIT_CLEARANCE_M = 0.03;
1791
1959
  var ELECTRICAL_SPACE_CLEARANCE_M = 0.02;
1960
+ var DECIMAL_TO_NOMINAL_KEY = {
1961
+ 0.5: "1/2",
1962
+ 0.75: "3/4",
1963
+ 1.25: "1-1/4",
1964
+ 1.5: "1-1/2",
1965
+ 2.5: "2-1/2"
1966
+ };
1967
+ function canonicalizeNumericNominalKey(value) {
1968
+ const exact = DECIMAL_TO_NOMINAL_KEY[value];
1969
+ if (exact)
1970
+ return exact;
1971
+ if (Math.abs(value - Math.round(value)) < 0.000001) {
1972
+ return String(Math.round(value));
1973
+ }
1974
+ return String(value);
1975
+ }
1792
1976
  function normalizeNominalInchKey(raw) {
1793
- if (typeof raw === "number" && Number.isFinite(raw) && raw > 0)
1794
- return String(raw);
1977
+ if (typeof raw === "number" && Number.isFinite(raw) && raw > 0) {
1978
+ return canonicalizeNumericNominalKey(raw);
1979
+ }
1795
1980
  if (typeof raw !== "string")
1796
1981
  return null;
1797
1982
  const stripped = raw.trim().replace(/["'″]+/g, "").trim();
@@ -1801,7 +1986,15 @@ function normalizeNominalInchKey(raw) {
1801
1986
  if (mixedSpaced)
1802
1987
  return `${mixedSpaced[1]}-${mixedSpaced[2]}`;
1803
1988
  const compact = stripped.replace(/\s+/g, "");
1804
- return compact.length > 0 ? compact : null;
1989
+ if (compact.length === 0)
1990
+ return null;
1991
+ if (/^\d+(?:\.\d+)?$/.test(compact)) {
1992
+ const parsed = Number(compact);
1993
+ if (Number.isFinite(parsed) && parsed > 0) {
1994
+ return canonicalizeNumericNominalKey(parsed);
1995
+ }
1996
+ }
1997
+ return compact;
1805
1998
  }
1806
1999
  function parseNominalInches2(raw) {
1807
2000
  if (typeof raw === "number") {
@@ -1942,6 +2135,33 @@ function createSweptSegmentPrism(a, b, halfXZ, halfY) {
1942
2135
  yMax
1943
2136
  });
1944
2137
  }
2138
+ var MAX_SLOPED_SEGMENT_DY_M = 0.15;
2139
+ var MAX_SLOPED_SUBDIVISIONS = 16;
2140
+ function createSweptSegmentPrisms(a, b, halfXZ, halfY) {
2141
+ const horizLen = Math.hypot(b[0] - a[0], b[2] - a[2]);
2142
+ const dy = Math.abs(b[1] - a[1]);
2143
+ if (dy <= MAX_SLOPED_SEGMENT_DY_M || horizLen <= 0.2) {
2144
+ return [createSweptSegmentPrism(a, b, halfXZ, halfY)];
2145
+ }
2146
+ const steps = Math.min(MAX_SLOPED_SUBDIVISIONS, Math.ceil(dy / MAX_SLOPED_SEGMENT_DY_M));
2147
+ const prisms = [];
2148
+ for (let s = 0;s < steps; s++) {
2149
+ const t0 = s / steps;
2150
+ const t1 = (s + 1) / steps;
2151
+ const p0 = [
2152
+ a[0] + (b[0] - a[0]) * t0,
2153
+ a[1] + (b[1] - a[1]) * t0,
2154
+ a[2] + (b[2] - a[2]) * t0
2155
+ ];
2156
+ const p1 = [
2157
+ a[0] + (b[0] - a[0]) * t1,
2158
+ a[1] + (b[1] - a[1]) * t1,
2159
+ a[2] + (b[2] - a[2]) * t1
2160
+ ];
2161
+ prisms.push(createSweptSegmentPrism(p0, p1, halfXZ, halfY));
2162
+ }
2163
+ return prisms;
2164
+ }
1945
2165
  function clampNonNegative(v, fallback = 0) {
1946
2166
  return typeof v === "number" && Number.isFinite(v) ? Math.max(0, v) : fallback;
1947
2167
  }
@@ -1970,27 +2190,23 @@ function extractBeamPrism2(node, levelH = 2.7) {
1970
2190
  const safeLevelH = Number.isFinite(levelH) && levelH > 0 ? levelH : 2.7;
1971
2191
  let yMin;
1972
2192
  let yMax;
2193
+ const sy = start3D && typeof node.start[1] === "number" && Number.isFinite(node.start[1]) ? node.start[1] : null;
2194
+ const ey = end3D && typeof node.end[1] === "number" && Number.isFinite(node.end[1]) ? node.end[1] : null;
1973
2195
  if (typeof node.elevationY === "number" && Number.isFinite(node.elevationY)) {
1974
2196
  yMin = node.elevationY - depthM / 2;
1975
2197
  yMax = node.elevationY + depthM / 2;
1976
2198
  } else if (typeof node.elevation === "number" && Number.isFinite(node.elevation)) {
1977
2199
  yMin = node.elevation - depthM / 2;
1978
2200
  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]);
2201
+ } else if (sy !== null && ey !== null) {
2202
+ yMin = Math.min(sy, ey) - depthM / 2;
2203
+ yMax = Math.max(sy, ey) + depthM / 2;
2204
+ } else if (sy !== null) {
1992
2205
  yMin = sy - depthM / 2;
1993
2206
  yMax = sy + depthM / 2;
2207
+ } else if (ey !== null) {
2208
+ yMin = ey - depthM / 2;
2209
+ yMax = ey + depthM / 2;
1994
2210
  } else {
1995
2211
  const y = safeLevelH - depthM / 2;
1996
2212
  yMin = y - depthM / 2;
@@ -2470,7 +2686,7 @@ function extractEquipmentPrisms2(node, levelH = 2.7, opts = {}) {
2470
2686
  if (!isVec32(node.position))
2471
2687
  return [];
2472
2688
  const [x, y, z] = node.position;
2473
- const diamIn = parseNominalInches2(node.diameter) ?? 2;
2689
+ const diamIn = parseNominalInches2(node.nominalSize ?? node.diameter) ?? 2;
2474
2690
  const halfM = Math.max(0.05, diamIn * INCHES_TO_METERS2 / 2 + 0.03);
2475
2691
  const prism = createOrientedPrismY2({
2476
2692
  centerX: x,
@@ -2548,19 +2764,22 @@ function pushPathObstacles(out, node, trade, kind, label, requiredClearanceM) {
2548
2764
  const p1 = path[i + 1];
2549
2765
  if (!isVec32(p0) || !isVec32(p1))
2550
2766
  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
- });
2767
+ const prisms = createSweptSegmentPrisms(p0, p1, halfXZ, halfY);
2768
+ for (let pi = 0;pi < prisms.length; pi++) {
2769
+ const prism = prisms[pi];
2770
+ out.push({
2771
+ id: prisms.length === 1 ? `${node.id}:seg${i}` : `${node.id}:seg${i}_${pi}`,
2772
+ nodeId: node.id,
2773
+ nodeType: node.type,
2774
+ trade,
2775
+ priorityRank,
2776
+ kind,
2777
+ label,
2778
+ aabb: prism.aabb,
2779
+ prism,
2780
+ requiredClearanceM
2781
+ });
2782
+ }
2564
2783
  }
2565
2784
  }
2566
2785
  function extractTradeOccupancyObstacles2(nodes, levelId, opts) {
@@ -2667,7 +2886,7 @@ function extractTradeOccupancyObstacles2(nodes, levelId, opts) {
2667
2886
  continue;
2668
2887
  }
2669
2888
  if (includeElectricalConduits && t === "electrical:conduit-run") {
2670
- const sizeLabel = String(node.tradeSize ?? '3/4"');
2889
+ const sizeLabel = String(node.tradeSize ?? node.nominalSize ?? node.diameter ?? '3/4"');
2671
2890
  const label = `Electrical ${sizeLabel} conduit (${node.name ?? node.id})`;
2672
2891
  pushPathObstacles(obstacles, node, "electrical", "electrical-conduit", label, ELECTRICAL_CONDUIT_CLEARANCE_M);
2673
2892
  continue;
@@ -2706,24 +2925,27 @@ class TradeOccupancyGrid2 {
2706
2925
  const b = params.path[i + 1];
2707
2926
  if (!isVec32(a) || !isVec32(b))
2708
2927
  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
- });
2928
+ const prisms = createSweptSegmentPrisms(a, b, halfXZ, halfY);
2929
+ for (let pi = 0;pi < prisms.length; pi++) {
2930
+ const prism = prisms[pi];
2931
+ let segId = prisms.length === 1 ? `${reservationId}:seg${i}` : `${reservationId}:seg${i}_${pi}`;
2932
+ while (this._activeIds.has(segId)) {
2933
+ segId = `${reservationId}:seg${i}#${this._seq++}`;
2934
+ }
2935
+ ids.add(segId);
2936
+ this._activeIds.add(segId);
2937
+ this._obstacles.push({
2938
+ id: segId,
2939
+ nodeId: params.runId,
2940
+ trade: params.trade,
2941
+ priorityRank,
2942
+ kind: "reserved-run",
2943
+ label,
2944
+ aabb: prism.aabb,
2945
+ prism,
2946
+ requiredClearanceM: clearanceM
2947
+ });
2948
+ }
2727
2949
  }
2728
2950
  this._reservations.set(reservationId, ids);
2729
2951
  return reservationId;
@@ -2743,7 +2965,7 @@ class TradeOccupancyGrid2 {
2743
2965
  priorityRank: TRADE_PRIORITY_RANK2[params.trade],
2744
2966
  kind: params.kind,
2745
2967
  label: params.label ?? `${params.kind} (${nodeId})`,
2746
- aabb: params.aabb,
2968
+ aabb: params.prism ? params.prism.aabb : params.aabb,
2747
2969
  ...params.prism ? { prism: params.prism } : {},
2748
2970
  requiredClearanceM: clearanceM
2749
2971
  });
@@ -2777,14 +2999,28 @@ class TradeOccupancyGrid2 {
2777
2999
  const extraClear = clampNonNegative(params.extraClearanceM, 0);
2778
3000
  const useObstacleClearance = params.useObstacleClearance ?? true;
2779
3001
  const ignore = params.ignoreNodeIds;
3002
+ const segMinX = a[0] < b[0] ? a[0] : b[0];
3003
+ const segMaxX = a[0] > b[0] ? a[0] : b[0];
3004
+ const segMinY = a[1] < b[1] ? a[1] : b[1];
3005
+ const segMaxY = a[1] > b[1] ? a[1] : b[1];
3006
+ const segMinZ = a[2] < b[2] ? a[2] : b[2];
3007
+ const segMaxZ = a[2] > b[2] ? a[2] : b[2];
2780
3008
  const hits = [];
2781
3009
  for (const obs of this._obstacles) {
2782
3010
  if (ignore && (ignore.has(obs.nodeId) || ignore.has(obs.id)))
2783
3011
  continue;
2784
3012
  const obsClear = useObstacleClearance ? clampNonNegative(obs.requiredClearanceM, 0) : 0;
2785
3013
  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);
3014
+ const padXZ = halfXZ + clear;
3015
+ const padY = halfY + clear;
3016
+ const broadXZ = obs.prism ? padXZ * 1.414214 + 0.00001 : padXZ + 0.00001;
3017
+ const broadY = padY + 0.00001;
3018
+ const ob = obs.aabb;
3019
+ if (segMaxX < ob.minX - broadXZ || segMinX > ob.maxX + broadXZ || segMaxY < ob.minY - broadY || segMinY > ob.maxY + broadY || segMaxZ < ob.minZ - broadXZ || segMinZ > ob.maxZ + broadXZ) {
3020
+ continue;
3021
+ }
3022
+ const pad = [padXZ, padY, padXZ];
3023
+ const hit = obs.prism ? segmentIntersectsOrientedPrismY2(a, b, obs.prism, pad) : segmentIntersectsAabb32(a, b, ob, pad);
2788
3024
  if (hit)
2789
3025
  hits.push(obs);
2790
3026
  }
@@ -2916,7 +3152,7 @@ function sameBuildingId2(bA, bB) {
2916
3152
  return false;
2917
3153
  return true;
2918
3154
  }
2919
- function buildRoofAdoptionContext(nodes) {
3155
+ function buildRoofAdoptionContext(nodes, includeHidden = false) {
2920
3156
  const levels = [];
2921
3157
  const buildingByChildLevel = new Map;
2922
3158
  const walledLevelIds = new Set;
@@ -2930,7 +3166,7 @@ function buildRoofAdoptionContext(nodes) {
2930
3166
  buildingByChildLevel.set(cid, n.id);
2931
3167
  }
2932
3168
  }
2933
- } else if (n.type === "wall" && n.visible !== false) {
3169
+ } else if (n.type === "wall" && (includeHidden || n.visible !== false)) {
2934
3170
  const lid = resolveNodeLevelId2(nodes, n, { allowDanglingParentId: false });
2935
3171
  if (!lid)
2936
3172
  continue;
@@ -3039,12 +3275,12 @@ function shouldAdoptRoofSegmentOntoLevelWithContext(nodes, ctx, targetLevelId, r
3039
3275
  const hostLevel = candidates[candidates.length - 1];
3040
3276
  return hostLevel.id === targetLevelId;
3041
3277
  }
3042
- function shouldAdoptWallLessRoofLevel2(nodes, targetLevelId, roofLevel) {
3043
- const ctx = buildRoofAdoptionContext(nodes);
3278
+ function shouldAdoptWallLessRoofLevel2(nodes, targetLevelId, roofLevel, opts = {}) {
3279
+ const ctx = buildRoofAdoptionContext(nodes, opts.includeHidden ?? false);
3044
3280
  return shouldAdoptWallLessRoofLevelWithContext(nodes, ctx, targetLevelId, roofLevel);
3045
3281
  }
3046
- function shouldAdoptRoofSegmentOntoLevel2(nodes, targetLevelId, roofLevel, segCenterX, segCenterZ, halfX, halfZ) {
3047
- const ctx = buildRoofAdoptionContext(nodes);
3282
+ function shouldAdoptRoofSegmentOntoLevel2(nodes, targetLevelId, roofLevel, segCenterX, segCenterZ, halfX, halfZ, opts = {}) {
3283
+ const ctx = buildRoofAdoptionContext(nodes, opts.includeHidden ?? false);
3048
3284
  return shouldAdoptRoofSegmentOntoLevelWithContext(nodes, ctx, targetLevelId, roofLevel, segCenterX, segCenterZ, halfX, halfZ);
3049
3285
  }
3050
3286
  function evaluateRoofSegmentSurfaceY2(seg, wx, wz, opts = {}) {
@@ -3157,7 +3393,7 @@ function extractCanonicalRoofSegments2(nodes, levelId, wallHeightM, opts = {}) {
3157
3393
  belongsHere = true;
3158
3394
  } else if (enclosingLevelNode !== null) {
3159
3395
  if (!adoptionCtx) {
3160
- adoptionCtx = buildRoofAdoptionContext(nodes);
3396
+ adoptionCtx = buildRoofAdoptionContext(nodes, includeHidden);
3161
3397
  }
3162
3398
  const absCos = Math.abs(Math.cos(yaw));
3163
3399
  const absSin = Math.abs(Math.sin(yaw));
@@ -3310,8 +3546,14 @@ function compileLevelRoofShell2(nodes, levelId, wallHeightM, opts = {}) {
3310
3546
  const syntheticSlope = fallbackPitchRiseOver12 / 12;
3311
3547
  const allowSyntheticGable = hasRoofAbove && isHighestWalled && !isFlatRoof && syntheticSlope > 0.0001 && fallbackBounds !== null;
3312
3548
  const syntheticGableY = (x, z) => {
3313
- if (!allowSyntheticGable || !Number.isFinite(x) || !Number.isFinite(z))
3549
+ if (!allowSyntheticGable || !fallbackBounds || !Number.isFinite(x) || !Number.isFinite(z))
3314
3550
  return baseEaveY;
3551
+ if (ridgeAlongX) {
3552
+ if (x < fallbackBounds.minX - 0.000001 || x > fallbackBounds.maxX + 0.000001)
3553
+ return baseEaveY;
3554
+ } else if (z < fallbackBounds.minZ - 0.000001 || z > fallbackBounds.maxZ + 0.000001) {
3555
+ return baseEaveY;
3556
+ }
3315
3557
  const distFromRidge = Math.abs((ridgeAlongX ? z : x) - ridgeCenter);
3316
3558
  const riseM = Math.max(0, halfSpan - distFromRidge) * syntheticSlope;
3317
3559
  return baseEaveY + riseM;
@@ -3397,7 +3639,10 @@ function compileLevelRoofShell2(nodes, levelId, wallHeightM, opts = {}) {
3397
3639
  const segLen = Math.hypot(b[0] - a[0], b[1] - a[1], b[2] - a[2]);
3398
3640
  if (!Number.isFinite(segLen))
3399
3641
  return false;
3400
- const steps = Math.max(1, Math.ceil(segLen / PATH_SAMPLE_STEP_M));
3642
+ const rawSteps = segLen / PATH_SAMPLE_STEP_M;
3643
+ if (!Number.isFinite(rawSteps))
3644
+ return false;
3645
+ const steps = Math.max(1, Math.min(1024, Math.ceil(rawSteps)));
3401
3646
  for (let s = 0;s <= steps; s++) {
3402
3647
  const t = s / steps;
3403
3648
  const px = a[0] + (b[0] - a[0]) * t;
@@ -3503,20 +3748,22 @@ function compileBuildingAnatomy2(nodes) {
3503
3748
  }
3504
3749
  return val;
3505
3750
  };
3506
- const getOpeningPrisms = (levelId) => {
3507
- const key = encodeLevelCacheKey(levelId);
3751
+ const getOpeningPrisms = (levelId, opts) => {
3752
+ const key = `${encodeLevelCacheKey(levelId)}|${opts?.includeHidden ? 1 : 0}|${opts?.includeUnparented ? 1 : 0}`;
3508
3753
  let val = prismCache.get(key);
3509
3754
  if (!val) {
3510
- val = Object.freeze(extractLevelOpeningPrisms2(getWalls(levelId)));
3755
+ const wallOpts = opts?.includeHidden || opts?.includeUnparented ? { includeHidden: opts.includeHidden, includeUnparented: opts.includeUnparented } : undefined;
3756
+ val = Object.freeze(extractLevelOpeningPrisms2(getWalls(levelId, wallOpts)));
3511
3757
  prismCache.set(key, val);
3512
3758
  }
3513
3759
  return val;
3514
3760
  };
3515
- const getFootprint = (levelId) => {
3516
- const key = encodeLevelCacheKey(levelId);
3761
+ const getFootprint = (levelId, opts) => {
3762
+ const key = `${encodeLevelCacheKey(levelId)}|${opts?.includeHidden ? 1 : 0}|${opts?.includeUnparented ? 1 : 0}`;
3517
3763
  let val = footprintCache.get(key);
3518
3764
  if (!val) {
3519
- const fp = compileLevelFootprint2(nodes, levelId, getWalls(levelId));
3765
+ const wallOpts = opts?.includeHidden || opts?.includeUnparented ? { includeHidden: opts.includeHidden, includeUnparented: opts.includeUnparented } : undefined;
3766
+ const fp = compileLevelFootprint2(nodes, levelId, getWalls(levelId, wallOpts));
3520
3767
  Object.freeze(fp.boundedFaces);
3521
3768
  Object.freeze(fp.roomPolygons);
3522
3769
  for (const cluster of fp.clusters) {