@kruschdev/pascal-mep-kernel 0.0.0-stage → 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.
- package/LICENSE +21 -0
- package/README.md +86 -2
- package/dist/anatomy/footprint.d.ts +168 -0
- package/dist/anatomy/index.d.ts +37 -0
- package/dist/anatomy/index.js +121 -0
- package/dist/anatomy/levels.d.ts +125 -0
- package/dist/anatomy/occupancy.d.ts +194 -0
- package/dist/anatomy/openings.d.ts +125 -0
- package/dist/anatomy/roof.d.ts +106 -0
- package/dist/chunk-92mknkfa.js +667 -0
- package/dist/chunk-kmtew7g8.js +3814 -0
- package/dist/chunk-q8cxxynd.js +2733 -0
- package/dist/chunk-yd3hcm7n.js +277 -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 +232 -0
- package/dist/types.d.ts +137 -0
- package/dist/types.js +48 -0
- package/package.json +73 -4
|
@@ -0,0 +1,2733 @@
|
|
|
1
|
+
import {
|
|
2
|
+
segmentIntersectsOrientedPrismY2,
|
|
3
|
+
polylineLengthM2,
|
|
4
|
+
polylineHorizontalLengthM2,
|
|
5
|
+
countPathBendDegrees2,
|
|
6
|
+
countPathElbows2,
|
|
7
|
+
round22,
|
|
8
|
+
round32,
|
|
9
|
+
round42
|
|
10
|
+
} from "./chunk-yd3hcm7n.js";
|
|
11
|
+
import {
|
|
12
|
+
countPathOpeningPrismHits2,
|
|
13
|
+
findNearestWallToPoint2
|
|
14
|
+
} from "./chunk-kmtew7g8.js";
|
|
15
|
+
|
|
16
|
+
// src/router/cavity-graph.ts
|
|
17
|
+
var DEFAULT_ROOF_CLEARANCE_M = 0.14;
|
|
18
|
+
var DEFAULT_HALF_Y = 0.02;
|
|
19
|
+
var DEFAULT_HALF_XZ = 0.02;
|
|
20
|
+
var DEFAULT_PLENUM_GRID_STEP_M = 1.5;
|
|
21
|
+
var DEFAULT_SUB_FLOOR_ELEVATION_M = -0.35;
|
|
22
|
+
var WALL_JUNCTION_SNAP_M = 0.25;
|
|
23
|
+
var WALL_VERTEX_SHARE_EPS_M = 0.1;
|
|
24
|
+
var WALL_STATION_DEDUPE_M = 0.04;
|
|
25
|
+
var JAMB_OFFSET_M = 0.08;
|
|
26
|
+
var JAMB_EDGE_CLAMP_M = 0.03;
|
|
27
|
+
var UNDER_SILL_Y = 0.25;
|
|
28
|
+
var MID_WALL_Y = 1.15;
|
|
29
|
+
var TOP_PLATE_MARGIN_M = 0.06;
|
|
30
|
+
var RISER_MIN_Y = 0.02;
|
|
31
|
+
var RISER_TOP_MARGIN_M = 0.04;
|
|
32
|
+
var WALL_BANDS = ["under-sill", "mid-wall", "top-plate"];
|
|
33
|
+
var COST_WALL_HORIZONTAL = 1;
|
|
34
|
+
var COST_WALL_VERTICAL = 1.1;
|
|
35
|
+
var COST_OVERHEAD_PLENUM = 1.05;
|
|
36
|
+
var COST_ATTIC = 1;
|
|
37
|
+
var COST_SUB_FLOOR = 1.15;
|
|
38
|
+
var COST_LAYER_TRANSITION = 1.2;
|
|
39
|
+
var FOOTPRINT_STRICT_WALL_TOL_M = 0.02;
|
|
40
|
+
var MAX_TOTAL_VERTICES_TARGET = 580;
|
|
41
|
+
var MAX_STATIONS_PER_WALL = 24;
|
|
42
|
+
var floor3 = (n) => Math.floor((n + 0.000000001) * 1000) / 1000;
|
|
43
|
+
function clamp(v, lo, hi) {
|
|
44
|
+
return v < lo ? lo : v > hi ? hi : v;
|
|
45
|
+
}
|
|
46
|
+
function dist3(a, b) {
|
|
47
|
+
return Math.hypot(b[0] - a[0], b[1] - a[1], b[2] - a[2]);
|
|
48
|
+
}
|
|
49
|
+
function distXZ(a, b) {
|
|
50
|
+
return Math.hypot(b[0] - a[0], b[2] - a[2]);
|
|
51
|
+
}
|
|
52
|
+
function segmentIntersectsAnyOpening(a, b, prisms, pad) {
|
|
53
|
+
for (let i = 0;i < prisms.length; i++) {
|
|
54
|
+
if (segmentIntersectsOrientedPrismY2(a, b, prisms[i], pad)) {
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
function pointIntersectsAnyOpening(x, y, z, prisms, pad) {
|
|
61
|
+
const pt = [x, y, z];
|
|
62
|
+
return segmentIntersectsAnyOpening(pt, pt, prisms, pad);
|
|
63
|
+
}
|
|
64
|
+
function deduplicateSortedNumbers(values, eps, minVal, maxVal) {
|
|
65
|
+
if (values.length === 0)
|
|
66
|
+
return [];
|
|
67
|
+
values.sort((a, b) => a - b);
|
|
68
|
+
const out = [];
|
|
69
|
+
for (let i = 0;i < values.length; i++) {
|
|
70
|
+
const v = clamp(values[i], minVal, maxVal);
|
|
71
|
+
if (out.length === 0) {
|
|
72
|
+
out.push(v);
|
|
73
|
+
} else if (v - out[out.length - 1] > eps) {
|
|
74
|
+
out.push(v);
|
|
75
|
+
} else if (Math.abs(v - maxVal) <= 0.000001) {
|
|
76
|
+
out[out.length - 1] = maxVal;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (out.length > 0 && Math.abs(out[0] - minVal) <= eps) {
|
|
80
|
+
out[0] = minVal;
|
|
81
|
+
}
|
|
82
|
+
if (out.length > 0 && Math.abs(out[out.length - 1] - maxVal) <= eps) {
|
|
83
|
+
out[out.length - 1] = maxVal;
|
|
84
|
+
}
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
function computeWallSplitParameters(walls, halfXZ, subdivStepM) {
|
|
88
|
+
const splits = walls.map((w) => [0, w.lengthM]);
|
|
89
|
+
for (let i = 0;i < walls.length; i++) {
|
|
90
|
+
const a = walls[i];
|
|
91
|
+
for (let j = i + 1;j < walls.length; j++) {
|
|
92
|
+
const b = walls[j];
|
|
93
|
+
const denom = a.ux * b.uz - a.uz * b.ux;
|
|
94
|
+
if (Math.abs(denom) > 0.0001) {
|
|
95
|
+
const rx = b.start[0] - a.start[0];
|
|
96
|
+
const rz = b.start[1] - a.start[1];
|
|
97
|
+
const ta = (rx * b.uz - rz * b.ux) / denom;
|
|
98
|
+
const tb = (rx * a.uz - rz * a.ux) / denom;
|
|
99
|
+
if (ta >= -WALL_JUNCTION_SNAP_M && ta <= a.lengthM + WALL_JUNCTION_SNAP_M && tb >= -WALL_JUNCTION_SNAP_M && tb <= b.lengthM + WALL_JUNCTION_SNAP_M) {
|
|
100
|
+
splits[i].push(clamp(ta, 0, a.lengthM));
|
|
101
|
+
splits[j].push(clamp(tb, 0, b.lengthM));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
for (const ep of [a.start, a.end]) {
|
|
105
|
+
const tb = (ep[0] - b.start[0]) * b.ux + (ep[1] - b.start[1]) * b.uz;
|
|
106
|
+
if (tb >= -WALL_JUNCTION_SNAP_M && tb <= b.lengthM + WALL_JUNCTION_SNAP_M) {
|
|
107
|
+
const clampedTb = clamp(tb, 0, b.lengthM);
|
|
108
|
+
const px = b.start[0] + b.ux * clampedTb;
|
|
109
|
+
const pz = b.start[1] + b.uz * clampedTb;
|
|
110
|
+
if (Math.hypot(ep[0] - px, ep[1] - pz) <= WALL_JUNCTION_SNAP_M) {
|
|
111
|
+
splits[j].push(clampedTb);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
for (const ep of [b.start, b.end]) {
|
|
116
|
+
const ta = (ep[0] - a.start[0]) * a.ux + (ep[1] - a.start[1]) * a.uz;
|
|
117
|
+
if (ta >= -WALL_JUNCTION_SNAP_M && ta <= a.lengthM + WALL_JUNCTION_SNAP_M) {
|
|
118
|
+
const clampedTa = clamp(ta, 0, a.lengthM);
|
|
119
|
+
const px = a.start[0] + a.ux * clampedTa;
|
|
120
|
+
const pz = a.start[1] + a.uz * clampedTa;
|
|
121
|
+
if (Math.hypot(ep[0] - px, ep[1] - pz) <= WALL_JUNCTION_SNAP_M) {
|
|
122
|
+
splits[i].push(clampedTa);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
const result = [];
|
|
129
|
+
for (let i = 0;i < walls.length; i++) {
|
|
130
|
+
const w = walls[i];
|
|
131
|
+
const ts = splits[i];
|
|
132
|
+
const minClamp = Math.min(JAMB_EDGE_CLAMP_M, w.lengthM * 0.5);
|
|
133
|
+
const maxClamp = Math.max(w.lengthM - JAMB_EDGE_CLAMP_M, w.lengthM * 0.5);
|
|
134
|
+
for (const op of w.openings) {
|
|
135
|
+
ts.push(clamp(op.startU - JAMB_OFFSET_M, minClamp, maxClamp));
|
|
136
|
+
ts.push(clamp(op.endU + JAMB_OFFSET_M, minClamp, maxClamp));
|
|
137
|
+
if (halfXZ > JAMB_OFFSET_M) {
|
|
138
|
+
const wideOff = halfXZ + 0.02;
|
|
139
|
+
ts.push(clamp(op.startU - wideOff, minClamp, maxClamp));
|
|
140
|
+
ts.push(clamp(op.endU + wideOff, minClamp, maxClamp));
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
const baseSorted = deduplicateSortedNumbers(ts, WALL_STATION_DEDUPE_M, 0, w.lengthM);
|
|
144
|
+
const withSubdiv = [...baseSorted];
|
|
145
|
+
const maxStep = Math.max(2, subdivStepM);
|
|
146
|
+
for (let k = 0;k < baseSorted.length - 1; k++) {
|
|
147
|
+
const t0 = baseSorted[k];
|
|
148
|
+
const t1 = baseSorted[k + 1];
|
|
149
|
+
const span = t1 - t0;
|
|
150
|
+
if (span <= maxStep * 1.15)
|
|
151
|
+
continue;
|
|
152
|
+
const mid = (t0 + t1) * 0.5;
|
|
153
|
+
const insideOpening = w.openings.some((op) => mid > op.startU - 0.02 && mid < op.endU + 0.02);
|
|
154
|
+
if (insideOpening)
|
|
155
|
+
continue;
|
|
156
|
+
const pieces = Math.min(8, Math.ceil(span / maxStep));
|
|
157
|
+
for (let p = 1;p < pieces; p++) {
|
|
158
|
+
withSubdiv.push(t0 + span * p / pieces);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
let finalTs = deduplicateSortedNumbers(withSubdiv, WALL_STATION_DEDUPE_M, 0, w.lengthM);
|
|
162
|
+
if (finalTs.length > MAX_STATIONS_PER_WALL) {
|
|
163
|
+
if (baseSorted.length <= MAX_STATIONS_PER_WALL) {
|
|
164
|
+
finalTs = baseSorted;
|
|
165
|
+
} else {
|
|
166
|
+
const sampled = [];
|
|
167
|
+
const lastIdx = baseSorted.length - 1;
|
|
168
|
+
for (let k = 0;k < MAX_STATIONS_PER_WALL; k++) {
|
|
169
|
+
const idx = Math.round(k * lastIdx / (MAX_STATIONS_PER_WALL - 1));
|
|
170
|
+
const val = baseSorted[idx];
|
|
171
|
+
if (sampled.length === 0 || val > sampled[sampled.length - 1] + 0.000001) {
|
|
172
|
+
sampled.push(val);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
finalTs = sampled;
|
|
176
|
+
if (finalTs[finalTs.length - 1] !== w.lengthM) {
|
|
177
|
+
finalTs[finalTs.length - 1] = w.lengthM;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
result.push(finalTs);
|
|
182
|
+
}
|
|
183
|
+
return result;
|
|
184
|
+
}
|
|
185
|
+
function adaptGridStep(spanX, spanZ, baseStep, maxGridCells) {
|
|
186
|
+
let step = Math.max(0.25, baseStep);
|
|
187
|
+
const safeSpanX = Math.max(0.25, spanX);
|
|
188
|
+
const safeSpanZ = Math.max(0.25, spanZ);
|
|
189
|
+
const countCells = (s) => Math.max(1, Math.floor(safeSpanX / s)) * Math.max(1, Math.floor(safeSpanZ / s));
|
|
190
|
+
if (countCells(step) <= maxGridCells)
|
|
191
|
+
return step;
|
|
192
|
+
const area = safeSpanX * safeSpanZ;
|
|
193
|
+
step = Math.max(step, Math.sqrt(area / Math.max(1, maxGridCells)));
|
|
194
|
+
while (countCells(step) > maxGridCells) {
|
|
195
|
+
step *= 1.1;
|
|
196
|
+
}
|
|
197
|
+
return round32(step);
|
|
198
|
+
}
|
|
199
|
+
function compileLegalCavityGraph2(anatomy, levelId, opts = {}) {
|
|
200
|
+
const roofClearanceM = typeof opts.roofClearanceM === "number" && Number.isFinite(opts.roofClearanceM) && opts.roofClearanceM >= 0 ? opts.roofClearanceM : DEFAULT_ROOF_CLEARANCE_M;
|
|
201
|
+
const halfY = typeof opts.halfY === "number" && Number.isFinite(opts.halfY) && opts.halfY >= 0 ? opts.halfY : DEFAULT_HALF_Y;
|
|
202
|
+
const halfXZ = typeof opts.halfXZ === "number" && Number.isFinite(opts.halfXZ) && opts.halfXZ >= 0 ? opts.halfXZ : DEFAULT_HALF_XZ;
|
|
203
|
+
const requestedGridStepM = typeof opts.plenumGridStepM === "number" && Number.isFinite(opts.plenumGridStepM) && opts.plenumGridStepM > 0.1 ? opts.plenumGridStepM : DEFAULT_PLENUM_GRID_STEP_M;
|
|
204
|
+
const includeSubFloorLayer = opts.includeSubFloorLayer !== false;
|
|
205
|
+
const subFloorBand = opts.subFloorBand === "crawlspace" ? "crawlspace" : "under-slab";
|
|
206
|
+
const maxAllowedSubFloorY = floor3(-Math.max(halfY, 0.05));
|
|
207
|
+
const subFloorY = includeSubFloorLayer ? round32(Math.min(maxAllowedSubFloorY, typeof opts.subFloorElevationM === "number" && Number.isFinite(opts.subFloorElevationM) ? opts.subFloorElevationM : DEFAULT_SUB_FLOOR_ELEVATION_M)) : null;
|
|
208
|
+
const levelH = anatomy.getLevelHeightM(levelId);
|
|
209
|
+
const walls = [...anatomy.getWalls(levelId)].sort((a, b) => a.id.localeCompare(b.id));
|
|
210
|
+
const openingPrisms = anatomy.getOpeningPrisms(levelId);
|
|
211
|
+
const roofShell = anatomy.getRoofShell(levelId);
|
|
212
|
+
const footprint = anatomy.getFootprint(levelId);
|
|
213
|
+
const flatSoffitMarginM = typeof opts.flatSoffitMarginM === "number" && Number.isFinite(opts.flatSoffitMarginM) && opts.flatSoffitMarginM >= 0 ? opts.flatSoffitMarginM : levelH >= 2.6 ? 0.16 : 0.08;
|
|
214
|
+
let ceilingPlenumY = floor3(levelH - flatSoffitMarginM);
|
|
215
|
+
if (roofShell.hasRoofAbove && (roofShell.isFlatRoof || ceilingPlenumY + halfY + roofClearanceM > roofShell.maxRidgeY + 0.000001)) {
|
|
216
|
+
ceilingPlenumY = floor3(Math.min(ceilingPlenumY, roofShell.maxRidgeY - roofClearanceM - halfY));
|
|
217
|
+
}
|
|
218
|
+
let atticY = null;
|
|
219
|
+
if (roofShell.hasRoofAbove && !roofShell.isFlatRoof) {
|
|
220
|
+
const desiredAtticY = typeof opts.atticElevationM === "number" && Number.isFinite(opts.atticElevationM) ? opts.atticElevationM : levelH + 0.35;
|
|
221
|
+
const maxAllowedAtticY = roofShell.maxRidgeY - roofClearanceM - halfY;
|
|
222
|
+
const candidateAtticY = Math.min(desiredAtticY, maxAllowedAtticY);
|
|
223
|
+
if (candidateAtticY > levelH + 0.05) {
|
|
224
|
+
atticY = floor3(candidateAtticY);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
const openingPad = [halfXZ, halfY, halfXZ];
|
|
228
|
+
const fpPointOpts = {
|
|
229
|
+
precedence: "room-first",
|
|
230
|
+
wallToleranceM: FOOTPRINT_STRICT_WALL_TOL_M,
|
|
231
|
+
outdoorPolygons: opts.outdoorPolygons,
|
|
232
|
+
interiorPolygons: opts.interiorPolygons
|
|
233
|
+
};
|
|
234
|
+
const fpStrictPointOpts = {
|
|
235
|
+
precedence: "room-first",
|
|
236
|
+
wallToleranceM: 0,
|
|
237
|
+
outdoorPolygons: opts.outdoorPolygons,
|
|
238
|
+
interiorPolygons: opts.interiorPolygons
|
|
239
|
+
};
|
|
240
|
+
const baseSegmentStepM = 0.35;
|
|
241
|
+
const fpSegmentOpts = {
|
|
242
|
+
precedence: "room-first",
|
|
243
|
+
wallToleranceM: FOOTPRINT_STRICT_WALL_TOL_M,
|
|
244
|
+
outdoorPolygons: opts.outdoorPolygons,
|
|
245
|
+
interiorPolygons: opts.interiorPolygons,
|
|
246
|
+
stepM: baseSegmentStepM
|
|
247
|
+
};
|
|
248
|
+
const singleSegmentRoof = roofShell.hasRoofAbove && roofShell.segments.length === 1;
|
|
249
|
+
const roofTopOffset = halfY + roofClearanceM;
|
|
250
|
+
const isRoofPointClear = (x, y, z) => {
|
|
251
|
+
if (!roofShell.hasRoofAbove)
|
|
252
|
+
return true;
|
|
253
|
+
return y + roofTopOffset <= roofShell.roofEnvelopeY(x, z) + 0.000001;
|
|
254
|
+
};
|
|
255
|
+
const isRoofSegmentClear = (a, b) => {
|
|
256
|
+
if (!roofShell.hasRoofAbove)
|
|
257
|
+
return true;
|
|
258
|
+
if (roofShell.isFlatRoof && roofShell.segments.length === 0) {
|
|
259
|
+
return a[1] + roofTopOffset <= roofShell.roofEnvelopeY(a[0], a[2]) + 0.000001 && b[1] + roofTopOffset <= roofShell.roofEnvelopeY(b[0], b[2]) + 0.000001;
|
|
260
|
+
}
|
|
261
|
+
if (singleSegmentRoof) {
|
|
262
|
+
const syA = roofShell.roofSurfaceY(a[0], a[2]);
|
|
263
|
+
const syB = roofShell.roofSurfaceY(b[0], b[2]);
|
|
264
|
+
if (syA !== null && syB !== null) {
|
|
265
|
+
return a[1] + roofTopOffset <= syA + 0.000001 && b[1] + roofTopOffset <= syB + 0.000001;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return roofShell.isPathClearOfRoof([a, b], halfY, roofClearanceM);
|
|
269
|
+
};
|
|
270
|
+
const vertices = [];
|
|
271
|
+
const adjacency = [];
|
|
272
|
+
const edgeKeys = new Set;
|
|
273
|
+
const bandsPresent = new Set;
|
|
274
|
+
const wallRiserPiers = new Map;
|
|
275
|
+
let minBaseWeight = 1;
|
|
276
|
+
const wallBandVertexIds = {
|
|
277
|
+
"under-sill": [],
|
|
278
|
+
"mid-wall": [],
|
|
279
|
+
"top-plate": [],
|
|
280
|
+
"overhead-plenum": [],
|
|
281
|
+
attic: [],
|
|
282
|
+
"under-slab": [],
|
|
283
|
+
crawlspace: []
|
|
284
|
+
};
|
|
285
|
+
const addVertex = (v) => {
|
|
286
|
+
const id = vertices.length;
|
|
287
|
+
vertices.push({ id, ...v });
|
|
288
|
+
adjacency.push([]);
|
|
289
|
+
bandsPresent.add(v.band);
|
|
290
|
+
return id;
|
|
291
|
+
};
|
|
292
|
+
const addUndirectedEdge = (u, v, params) => {
|
|
293
|
+
if (u === v)
|
|
294
|
+
return false;
|
|
295
|
+
const key = u < v ? `${u}:${v}` : `${v}:${u}`;
|
|
296
|
+
if (edgeKeys.has(key))
|
|
297
|
+
return false;
|
|
298
|
+
const va = vertices[u];
|
|
299
|
+
const vb = vertices[v];
|
|
300
|
+
const lengthM = round32(dist3(va.pos, vb.pos));
|
|
301
|
+
const horizontalLengthM = round32(distXZ(va.pos, vb.pos));
|
|
302
|
+
if (lengthM < 0.00001)
|
|
303
|
+
return false;
|
|
304
|
+
edgeKeys.add(key);
|
|
305
|
+
if (Number.isFinite(params.baseCostWeight) && params.baseCostWeight > 0 && params.baseCostWeight < minBaseWeight) {
|
|
306
|
+
minBaseWeight = params.baseCostWeight;
|
|
307
|
+
}
|
|
308
|
+
const edgeUV = {
|
|
309
|
+
to: v,
|
|
310
|
+
lengthM,
|
|
311
|
+
horizontalLengthM,
|
|
312
|
+
band: params.bandOverride ?? vb.band,
|
|
313
|
+
isWallEdge: params.isWallEdge,
|
|
314
|
+
isVerticalTransition: params.isVerticalTransition,
|
|
315
|
+
...params.wallId !== undefined ? { wallId: params.wallId } : {},
|
|
316
|
+
baseCostWeight: params.baseCostWeight
|
|
317
|
+
};
|
|
318
|
+
const edgeVU = {
|
|
319
|
+
to: u,
|
|
320
|
+
lengthM,
|
|
321
|
+
horizontalLengthM,
|
|
322
|
+
band: params.bandOverride ?? va.band,
|
|
323
|
+
isWallEdge: params.isWallEdge,
|
|
324
|
+
isVerticalTransition: params.isVerticalTransition,
|
|
325
|
+
...params.wallId !== undefined ? { wallId: params.wallId } : {},
|
|
326
|
+
baseCostWeight: params.baseCostWeight
|
|
327
|
+
};
|
|
328
|
+
adjacency[u].push(edgeUV);
|
|
329
|
+
adjacency[v].push(edgeVU);
|
|
330
|
+
return true;
|
|
331
|
+
};
|
|
332
|
+
const boundsSpanX = footprint.bounds ? footprint.bounds.maxX - footprint.bounds.minX : 0;
|
|
333
|
+
const boundsSpanZ = footprint.bounds ? footprint.bounds.maxZ - footprint.bounds.minZ : 0;
|
|
334
|
+
const initialWallSubdivStep = Math.max(2, requestedGridStepM, Math.max(boundsSpanX, boundsSpanZ) / 10);
|
|
335
|
+
const wallSplitParams = computeWallSplitParameters(walls, halfXZ, initialWallSubdivStep);
|
|
336
|
+
const wallStations = [];
|
|
337
|
+
const findOrCreateWallVertex = (x, y, z, band, wallId, wallT, isRiserPier, maxClearY) => {
|
|
338
|
+
const existingIds = wallBandVertexIds[band];
|
|
339
|
+
let bestId = -1;
|
|
340
|
+
let bestDist = WALL_VERTEX_SHARE_EPS_M + 0.000001;
|
|
341
|
+
for (let i = 0;i < existingIds.length; i++) {
|
|
342
|
+
const id = existingIds[i];
|
|
343
|
+
const ev = vertices[id];
|
|
344
|
+
if (Math.abs(ev.pos[1] - y) > 0.08)
|
|
345
|
+
continue;
|
|
346
|
+
const d = Math.hypot(ev.pos[0] - x, ev.pos[2] - z);
|
|
347
|
+
if (d < bestDist) {
|
|
348
|
+
bestDist = d;
|
|
349
|
+
bestId = id;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
if (bestId !== -1) {
|
|
353
|
+
const ev = vertices[bestId];
|
|
354
|
+
const nextMaxClearY = ev.maxClearY !== undefined ? Math.min(ev.maxClearY, floor3(maxClearY)) : floor3(maxClearY);
|
|
355
|
+
const nextIsRiserPier = Boolean(ev.isRiserPier && isRiserPier);
|
|
356
|
+
if (nextMaxClearY !== ev.maxClearY || nextIsRiserPier !== Boolean(ev.isRiserPier)) {
|
|
357
|
+
vertices[bestId] = {
|
|
358
|
+
...ev,
|
|
359
|
+
isRiserPier: nextIsRiserPier,
|
|
360
|
+
maxClearY: nextMaxClearY
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
return bestId;
|
|
364
|
+
}
|
|
365
|
+
const id = addVertex({
|
|
366
|
+
pos: [x, y, z],
|
|
367
|
+
band,
|
|
368
|
+
wallId,
|
|
369
|
+
wallT: round32(wallT),
|
|
370
|
+
isRiserPier,
|
|
371
|
+
maxClearY: floor3(maxClearY)
|
|
372
|
+
});
|
|
373
|
+
existingIds.push(id);
|
|
374
|
+
return id;
|
|
375
|
+
};
|
|
376
|
+
for (let wi = 0;wi < walls.length; wi++) {
|
|
377
|
+
const wall = walls[wi];
|
|
378
|
+
const ts = wallSplitParams[wi];
|
|
379
|
+
const piers = [];
|
|
380
|
+
const stations = [];
|
|
381
|
+
for (let si = 0;si < ts.length; si++) {
|
|
382
|
+
const t = ts[si];
|
|
383
|
+
const x = round32(wall.start[0] + wall.ux * t);
|
|
384
|
+
const z = round32(wall.start[1] + wall.uz * t);
|
|
385
|
+
const wallTopY = Math.min(levelH, wall.heightM);
|
|
386
|
+
const roofY = roofShell.hasRoofAbove ? roofShell.roofEnvelopeY(x, z) : Number.POSITIVE_INFINITY;
|
|
387
|
+
const roofClearY = roofShell.hasRoofAbove ? roofY - roofClearanceM - halfY : Number.POSITIVE_INFINITY;
|
|
388
|
+
const topPlateY = floor3(Math.min(wallTopY - TOP_PLATE_MARGIN_M, roofClearY));
|
|
389
|
+
const pierMaxY = floor3(Math.min(wallTopY - RISER_TOP_MARGIN_M, roofShell.hasRoofAbove ? roofY - Math.max(RISER_TOP_MARGIN_M, roofClearanceM + halfY) : Number.POSITIVE_INFINITY));
|
|
390
|
+
const isRiserPier = pierMaxY > RISER_MIN_Y + 0.05 && !segmentIntersectsAnyOpening([x, RISER_MIN_Y, z], [x, pierMaxY, z], openingPrisms, openingPad);
|
|
391
|
+
if (isRiserPier) {
|
|
392
|
+
piers.push({
|
|
393
|
+
wallId: wall.id,
|
|
394
|
+
t: round32(t),
|
|
395
|
+
x,
|
|
396
|
+
z,
|
|
397
|
+
minSafeY: RISER_MIN_Y,
|
|
398
|
+
maxSafeY: pierMaxY
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
const bandVertexIds = {};
|
|
402
|
+
const maxClearY = Number.isFinite(roofClearY) ? roofClearY : wallTopY;
|
|
403
|
+
if (UNDER_SILL_Y < topPlateY - 0.05 && !pointIntersectsAnyOpening(x, UNDER_SILL_Y, z, openingPrisms, openingPad) && isRoofPointClear(x, UNDER_SILL_Y, z)) {
|
|
404
|
+
bandVertexIds["under-sill"] = findOrCreateWallVertex(x, UNDER_SILL_Y, z, "under-sill", wall.id, t, isRiserPier, maxClearY);
|
|
405
|
+
}
|
|
406
|
+
if (MID_WALL_Y < topPlateY - 0.05 && !pointIntersectsAnyOpening(x, MID_WALL_Y, z, openingPrisms, openingPad) && isRoofPointClear(x, MID_WALL_Y, z)) {
|
|
407
|
+
bandVertexIds["mid-wall"] = findOrCreateWallVertex(x, MID_WALL_Y, z, "mid-wall", wall.id, t, isRiserPier, maxClearY);
|
|
408
|
+
}
|
|
409
|
+
if (topPlateY > 0.1 && !pointIntersectsAnyOpening(x, topPlateY, z, openingPrisms, openingPad) && isRoofPointClear(x, topPlateY, z)) {
|
|
410
|
+
bandVertexIds["top-plate"] = findOrCreateWallVertex(x, topPlateY, z, "top-plate", wall.id, t, isRiserPier, maxClearY);
|
|
411
|
+
}
|
|
412
|
+
if (isRiserPier) {
|
|
413
|
+
const verticalIds = [];
|
|
414
|
+
for (const b of WALL_BANDS) {
|
|
415
|
+
const vid = bandVertexIds[b];
|
|
416
|
+
if (vid !== undefined)
|
|
417
|
+
verticalIds.push(vid);
|
|
418
|
+
}
|
|
419
|
+
verticalIds.sort((a, b) => vertices[a].pos[1] - vertices[b].pos[1]);
|
|
420
|
+
for (let k = 0;k < verticalIds.length - 1; k++) {
|
|
421
|
+
const vLow = verticalIds[k];
|
|
422
|
+
const vHigh = verticalIds[k + 1];
|
|
423
|
+
const pLow = vertices[vLow].pos;
|
|
424
|
+
const pHigh = vertices[vHigh].pos;
|
|
425
|
+
if (!segmentIntersectsAnyOpening(pLow, pHigh, openingPrisms, openingPad) && isRoofSegmentClear(pLow, pHigh)) {
|
|
426
|
+
addUndirectedEdge(vLow, vHigh, {
|
|
427
|
+
isWallEdge: true,
|
|
428
|
+
isVerticalTransition: true,
|
|
429
|
+
wallId: wall.id,
|
|
430
|
+
baseCostWeight: COST_WALL_VERTICAL
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
stations.push({
|
|
436
|
+
t,
|
|
437
|
+
x,
|
|
438
|
+
z,
|
|
439
|
+
topPlateY,
|
|
440
|
+
pierMaxY,
|
|
441
|
+
isRiserPier,
|
|
442
|
+
bandVertexIds
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
wallRiserPiers.set(wall.id, piers);
|
|
446
|
+
wallStations.push(stations);
|
|
447
|
+
for (const band of WALL_BANDS) {
|
|
448
|
+
let prevVid = null;
|
|
449
|
+
for (let si = 0;si < stations.length; si++) {
|
|
450
|
+
const curVid = stations[si].bandVertexIds[band] ?? null;
|
|
451
|
+
if (curVid === null) {
|
|
452
|
+
prevVid = null;
|
|
453
|
+
continue;
|
|
454
|
+
}
|
|
455
|
+
if (prevVid !== null && prevVid !== curVid) {
|
|
456
|
+
const pa = vertices[prevVid].pos;
|
|
457
|
+
const pb = vertices[curVid].pos;
|
|
458
|
+
if (!segmentIntersectsAnyOpening(pa, pb, openingPrisms, openingPad) && isRoofSegmentClear(pa, pb)) {
|
|
459
|
+
addUndirectedEdge(prevVid, curVid, {
|
|
460
|
+
isWallEdge: true,
|
|
461
|
+
isVerticalTransition: false,
|
|
462
|
+
wallId: wall.id,
|
|
463
|
+
baseCostWeight: COST_WALL_HORIZONTAL,
|
|
464
|
+
bandOverride: band
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
prevVid = curVid;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
for (const band of WALL_BANDS) {
|
|
473
|
+
const ids = wallBandVertexIds[band];
|
|
474
|
+
for (let i = 0;i < ids.length; i++) {
|
|
475
|
+
const uid = ids[i];
|
|
476
|
+
const vu = vertices[uid];
|
|
477
|
+
for (let j = i + 1;j < ids.length; j++) {
|
|
478
|
+
const vid = ids[j];
|
|
479
|
+
const vv = vertices[vid];
|
|
480
|
+
if (vu.wallId === vv.wallId)
|
|
481
|
+
continue;
|
|
482
|
+
if (Math.abs(vu.pos[1] - vv.pos[1]) > 0.08)
|
|
483
|
+
continue;
|
|
484
|
+
const d = Math.hypot(vu.pos[0] - vv.pos[0], vu.pos[2] - vv.pos[2]);
|
|
485
|
+
if (d > WALL_VERTEX_SHARE_EPS_M && d <= WALL_JUNCTION_SNAP_M) {
|
|
486
|
+
if (!segmentIntersectsAnyOpening(vu.pos, vv.pos, openingPrisms, openingPad) && isRoofSegmentClear(vu.pos, vv.pos)) {
|
|
487
|
+
addUndirectedEdge(uid, vid, {
|
|
488
|
+
isWallEdge: true,
|
|
489
|
+
isVerticalTransition: false,
|
|
490
|
+
baseCostWeight: COST_WALL_HORIZONTAL,
|
|
491
|
+
bandOverride: band
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
const bounds = footprint.bounds;
|
|
499
|
+
const num2DLayers = 1 + (atticY !== null ? 1 : 0) + (subFloorY !== null ? 1 : 0);
|
|
500
|
+
const remainingVertexBudget = Math.max(90, MAX_TOTAL_VERTICES_TARGET - vertices.length);
|
|
501
|
+
const maxVertsPerLayer = Math.max(30, Math.floor(remainingVertexBudget / num2DLayers));
|
|
502
|
+
const maxGridCellsPerLayer = Math.max(20, Math.floor(maxVertsPerLayer * 0.78));
|
|
503
|
+
const effectiveGridStepM = bounds ? adaptGridStep(bounds.maxX - bounds.minX, bounds.maxZ - bounds.minZ, requestedGridStepM, maxGridCellsPerLayer) : requestedGridStepM;
|
|
504
|
+
const layerSegmentOpts = {
|
|
505
|
+
...fpSegmentOpts,
|
|
506
|
+
stepM: Math.max(0.35, Math.min(0.65, effectiveGridStepM * 0.25))
|
|
507
|
+
};
|
|
508
|
+
const fpCoordKey = (x, z) => `${Math.round(x * 1000)}:${Math.round(z * 1000)}`;
|
|
509
|
+
const fpStrictPointCache = new Map;
|
|
510
|
+
const isFootprintStrictPointInside = (x, z) => {
|
|
511
|
+
const k = fpCoordKey(x, z);
|
|
512
|
+
const cached = fpStrictPointCache.get(k);
|
|
513
|
+
if (cached !== undefined)
|
|
514
|
+
return cached;
|
|
515
|
+
const res = footprint.isPointInside(x, z, fpStrictPointOpts);
|
|
516
|
+
fpStrictPointCache.set(k, res);
|
|
517
|
+
return res;
|
|
518
|
+
};
|
|
519
|
+
const fpLayerSegCache = new Map;
|
|
520
|
+
const isFootprintLayerSegInside = (a, b) => {
|
|
521
|
+
const ka = fpCoordKey(a[0], a[2]);
|
|
522
|
+
const kb = fpCoordKey(b[0], b[2]);
|
|
523
|
+
const k = ka < kb ? `${ka}|${kb}` : `${kb}|${ka}`;
|
|
524
|
+
const cached = fpLayerSegCache.get(k);
|
|
525
|
+
if (cached !== undefined)
|
|
526
|
+
return cached;
|
|
527
|
+
const res = footprint.isSegmentInside(a, b, layerSegmentOpts);
|
|
528
|
+
fpLayerSegCache.set(k, res);
|
|
529
|
+
return res;
|
|
530
|
+
};
|
|
531
|
+
const sortedRooms = [...footprint.roomPolygons].sort((a, b) => a.id.localeCompare(b.id));
|
|
532
|
+
const seedDedupeRadius = Math.max(0.6, effectiveGridStepM * 0.65);
|
|
533
|
+
const pierInteriorSeeds = [];
|
|
534
|
+
const pushPierSeed = (x, z) => {
|
|
535
|
+
for (let i = 0;i < pierInteriorSeeds.length; i++) {
|
|
536
|
+
const prev = pierInteriorSeeds[i];
|
|
537
|
+
if (Math.hypot(prev.x - x, prev.z - z) <= seedDedupeRadius)
|
|
538
|
+
return;
|
|
539
|
+
}
|
|
540
|
+
pierInteriorSeeds.push({ x, z });
|
|
541
|
+
};
|
|
542
|
+
for (let wi = 0;wi < walls.length; wi++) {
|
|
543
|
+
const wall = walls[wi];
|
|
544
|
+
const piers = wallRiserPiers.get(wall.id) ?? [];
|
|
545
|
+
const inset = Math.max(0.12, wall.thicknessM / 2 + 0.1);
|
|
546
|
+
for (let pi = 0;pi < piers.length; pi++) {
|
|
547
|
+
const pier = piers[pi];
|
|
548
|
+
const posX = round32(pier.x + wall.nx * inset);
|
|
549
|
+
const posZ = round32(pier.z + wall.nz * inset);
|
|
550
|
+
const negX = round32(pier.x - wall.nx * inset);
|
|
551
|
+
const negZ = round32(pier.z - wall.nz * inset);
|
|
552
|
+
const posIn = isFootprintStrictPointInside(posX, posZ);
|
|
553
|
+
const negIn = isFootprintStrictPointInside(negX, negZ);
|
|
554
|
+
if (posIn && negIn) {
|
|
555
|
+
if (isFootprintStrictPointInside(pier.x, pier.z)) {
|
|
556
|
+
pushPierSeed(pier.x, pier.z);
|
|
557
|
+
} else {
|
|
558
|
+
pushPierSeed(posX, posZ);
|
|
559
|
+
}
|
|
560
|
+
} else if (posIn) {
|
|
561
|
+
pushPierSeed(posX, posZ);
|
|
562
|
+
} else if (negIn) {
|
|
563
|
+
pushPierSeed(negX, negZ);
|
|
564
|
+
} else if (footprint.isPointInside(pier.x, pier.z, fpPointOpts)) {
|
|
565
|
+
pushPierSeed(pier.x, pier.z);
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
const gridSpanX = bounds ? bounds.maxX - bounds.minX : 0;
|
|
570
|
+
const gridSpanZ = bounds ? bounds.maxZ - bounds.minZ : 0;
|
|
571
|
+
const gridCols = bounds ? Math.max(1, Math.floor(gridSpanX / effectiveGridStepM)) : 0;
|
|
572
|
+
const gridRows = bounds ? Math.max(1, Math.floor(gridSpanZ / effectiveGridStepM)) : 0;
|
|
573
|
+
const gridStepX = gridCols > 0 ? gridSpanX / gridCols : 0;
|
|
574
|
+
const gridStepZ = gridRows > 0 ? gridSpanZ / gridRows : 0;
|
|
575
|
+
const gridX = [];
|
|
576
|
+
const gridZ = [];
|
|
577
|
+
const gridInside = [];
|
|
578
|
+
const gridRoofEnvY = [];
|
|
579
|
+
const gridRoofOnSingleSeg = [];
|
|
580
|
+
const gridEdgeEastInside = [];
|
|
581
|
+
const gridEdgeSouthInside = [];
|
|
582
|
+
if (bounds && gridCols > 0 && gridRows > 0) {
|
|
583
|
+
for (let ix = 0;ix < gridCols; ix++) {
|
|
584
|
+
gridX.push(round32(bounds.minX + (ix + 0.5) * gridStepX));
|
|
585
|
+
}
|
|
586
|
+
for (let iz = 0;iz < gridRows; iz++) {
|
|
587
|
+
gridZ.push(round32(bounds.minZ + (iz + 0.5) * gridStepZ));
|
|
588
|
+
}
|
|
589
|
+
for (let ix = 0;ix < gridCols; ix++) {
|
|
590
|
+
const x = gridX[ix];
|
|
591
|
+
const insideCol = new Uint8Array(gridRows);
|
|
592
|
+
const roofCol = new Float64Array(gridRows);
|
|
593
|
+
const singleSegCol = new Uint8Array(gridRows);
|
|
594
|
+
for (let iz = 0;iz < gridRows; iz++) {
|
|
595
|
+
const z = gridZ[iz];
|
|
596
|
+
const isIn = isFootprintStrictPointInside(x, z);
|
|
597
|
+
insideCol[iz] = isIn ? 1 : 0;
|
|
598
|
+
if (isIn && roofShell.hasRoofAbove) {
|
|
599
|
+
if (singleSegmentRoof) {
|
|
600
|
+
const sy = roofShell.roofSurfaceY(x, z);
|
|
601
|
+
if (sy !== null) {
|
|
602
|
+
roofCol[iz] = sy;
|
|
603
|
+
singleSegCol[iz] = 1;
|
|
604
|
+
} else {
|
|
605
|
+
roofCol[iz] = roofShell.roofEnvelopeY(x, z);
|
|
606
|
+
}
|
|
607
|
+
} else {
|
|
608
|
+
roofCol[iz] = roofShell.roofEnvelopeY(x, z);
|
|
609
|
+
if (roofShell.isFlatRoof && roofShell.segments.length === 0)
|
|
610
|
+
singleSegCol[iz] = 1;
|
|
611
|
+
}
|
|
612
|
+
} else {
|
|
613
|
+
roofCol[iz] = Number.POSITIVE_INFINITY;
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
gridInside.push(insideCol);
|
|
617
|
+
gridRoofEnvY.push(roofCol);
|
|
618
|
+
gridRoofOnSingleSeg.push(singleSegCol);
|
|
619
|
+
gridEdgeEastInside.push(new Uint8Array(gridRows));
|
|
620
|
+
gridEdgeSouthInside.push(new Uint8Array(gridRows));
|
|
621
|
+
}
|
|
622
|
+
for (let ix = 0;ix < gridCols; ix++) {
|
|
623
|
+
const x = gridX[ix];
|
|
624
|
+
for (let iz = 0;iz < gridRows; iz++) {
|
|
625
|
+
if (!gridInside[ix][iz])
|
|
626
|
+
continue;
|
|
627
|
+
const z = gridZ[iz];
|
|
628
|
+
if (ix + 1 < gridCols && gridInside[ix + 1][iz]) {
|
|
629
|
+
if (isFootprintLayerSegInside([x, 0, z], [gridX[ix + 1], 0, z])) {
|
|
630
|
+
gridEdgeEastInside[ix][iz] = 1;
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
if (iz + 1 < gridRows && gridInside[ix][iz + 1]) {
|
|
634
|
+
if (isFootprintLayerSegInside([x, 0, z], [x, 0, gridZ[iz + 1]])) {
|
|
635
|
+
gridEdgeSouthInside[ix][iz] = 1;
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
const buildHorizontalLayer = (band, layerY, checkRoof, allowDiagonals, baseCostWeight) => {
|
|
642
|
+
const layerIds = [];
|
|
643
|
+
if (!bounds || gridCols === 0 || gridRows === 0)
|
|
644
|
+
return { vertexIds: layerIds };
|
|
645
|
+
const layerOpeningPrisms = band === "under-slab" || band === "crawlspace" ? [] : openingPrisms.filter((p) => layerY + halfY >= p.yMin - 0.000001 && layerY - halfY <= p.yMax + 0.000001);
|
|
646
|
+
const hasLayerOpenings = layerOpeningPrisms.length > 0;
|
|
647
|
+
const isPointLegal = (x, z) => {
|
|
648
|
+
if (!isFootprintStrictPointInside(x, z))
|
|
649
|
+
return false;
|
|
650
|
+
if (checkRoof && !isRoofPointClear(x, layerY, z))
|
|
651
|
+
return false;
|
|
652
|
+
if (hasLayerOpenings && pointIntersectsAnyOpening(x, layerY, z, layerOpeningPrisms, openingPad)) {
|
|
653
|
+
return false;
|
|
654
|
+
}
|
|
655
|
+
return true;
|
|
656
|
+
};
|
|
657
|
+
const isEdgeLegal = (a, b) => {
|
|
658
|
+
if (!isFootprintLayerSegInside(a, b))
|
|
659
|
+
return false;
|
|
660
|
+
if (checkRoof && !isRoofSegmentClear(a, b))
|
|
661
|
+
return false;
|
|
662
|
+
if (hasLayerOpenings && segmentIntersectsAnyOpening(a, b, layerOpeningPrisms, openingPad)) {
|
|
663
|
+
return false;
|
|
664
|
+
}
|
|
665
|
+
return true;
|
|
666
|
+
};
|
|
667
|
+
const gridIds = Array.from({ length: gridCols }, () => new Array(gridRows).fill(-1));
|
|
668
|
+
for (let ix = 0;ix < gridCols; ix++) {
|
|
669
|
+
const x = gridX[ix];
|
|
670
|
+
const insideCol = gridInside[ix];
|
|
671
|
+
const roofCol = gridRoofEnvY[ix];
|
|
672
|
+
for (let iz = 0;iz < gridRows; iz++) {
|
|
673
|
+
if (!insideCol[iz])
|
|
674
|
+
continue;
|
|
675
|
+
const roofY = roofCol[iz];
|
|
676
|
+
if (checkRoof && roofShell.hasRoofAbove && layerY + roofTopOffset > roofY + 0.000001)
|
|
677
|
+
continue;
|
|
678
|
+
const z = gridZ[iz];
|
|
679
|
+
if (hasLayerOpenings && pointIntersectsAnyOpening(x, layerY, z, layerOpeningPrisms, openingPad)) {
|
|
680
|
+
continue;
|
|
681
|
+
}
|
|
682
|
+
const defaultMaxClearY = layerY < 0 ? floor3(-halfY) : round32(levelH);
|
|
683
|
+
const maxClearY = checkRoof && roofShell.hasRoofAbove ? floor3(roofY - roofTopOffset) : defaultMaxClearY;
|
|
684
|
+
const id = addVertex({
|
|
685
|
+
pos: [x, layerY, z],
|
|
686
|
+
band,
|
|
687
|
+
maxClearY
|
|
688
|
+
});
|
|
689
|
+
gridIds[ix][iz] = id;
|
|
690
|
+
layerIds.push(id);
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
for (let ix = 0;ix < gridCols; ix++) {
|
|
694
|
+
for (let iz = 0;iz < gridRows; iz++) {
|
|
695
|
+
const uid = gridIds[ix][iz];
|
|
696
|
+
if (uid === -1)
|
|
697
|
+
continue;
|
|
698
|
+
const pa = vertices[uid].pos;
|
|
699
|
+
const uRoofFast = !checkRoof || !roofShell.hasRoofAbove || gridRoofOnSingleSeg[ix][iz] === 1;
|
|
700
|
+
if (ix + 1 < gridCols && gridEdgeEastInside[ix][iz]) {
|
|
701
|
+
const vid = gridIds[ix + 1][iz];
|
|
702
|
+
if (vid !== -1 && (uRoofFast && gridRoofOnSingleSeg[ix + 1][iz] === 1 || !checkRoof || isRoofSegmentClear(pa, vertices[vid].pos)) && (!hasLayerOpenings || !segmentIntersectsAnyOpening(pa, vertices[vid].pos, layerOpeningPrisms, openingPad))) {
|
|
703
|
+
addUndirectedEdge(uid, vid, {
|
|
704
|
+
isWallEdge: false,
|
|
705
|
+
isVerticalTransition: false,
|
|
706
|
+
baseCostWeight,
|
|
707
|
+
bandOverride: band
|
|
708
|
+
});
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
if (iz + 1 < gridRows && gridEdgeSouthInside[ix][iz]) {
|
|
712
|
+
const vid = gridIds[ix][iz + 1];
|
|
713
|
+
if (vid !== -1 && (uRoofFast && gridRoofOnSingleSeg[ix][iz + 1] === 1 || !checkRoof || isRoofSegmentClear(pa, vertices[vid].pos)) && (!hasLayerOpenings || !segmentIntersectsAnyOpening(pa, vertices[vid].pos, layerOpeningPrisms, openingPad))) {
|
|
714
|
+
addUndirectedEdge(uid, vid, {
|
|
715
|
+
isWallEdge: false,
|
|
716
|
+
isVerticalTransition: false,
|
|
717
|
+
baseCostWeight,
|
|
718
|
+
bandOverride: band
|
|
719
|
+
});
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
if (allowDiagonals && ix + 1 < gridCols) {
|
|
723
|
+
for (const dz of [1, -1]) {
|
|
724
|
+
const nz = iz + dz;
|
|
725
|
+
if (nz < 0 || nz >= gridRows)
|
|
726
|
+
continue;
|
|
727
|
+
const vid = gridIds[ix + 1][nz];
|
|
728
|
+
if (vid === -1)
|
|
729
|
+
continue;
|
|
730
|
+
const pb = vertices[vid].pos;
|
|
731
|
+
if (!isFootprintLayerSegInside(pa, pb))
|
|
732
|
+
continue;
|
|
733
|
+
if (hasLayerOpenings && segmentIntersectsAnyOpening(pa, pb, layerOpeningPrisms, openingPad)) {
|
|
734
|
+
continue;
|
|
735
|
+
}
|
|
736
|
+
if (uRoofFast && gridRoofOnSingleSeg[ix + 1][nz] === 1 || !checkRoof || isRoofSegmentClear(pa, pb)) {
|
|
737
|
+
addUndirectedEdge(uid, vid, {
|
|
738
|
+
isWallEdge: false,
|
|
739
|
+
isVerticalTransition: false,
|
|
740
|
+
baseCostWeight,
|
|
741
|
+
bandOverride: band
|
|
742
|
+
});
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
const extraSeeds = [];
|
|
749
|
+
if (band === "attic") {
|
|
750
|
+
for (let si = 0;si < roofShell.segments.length; si++) {
|
|
751
|
+
const seg = roofShell.segments[si];
|
|
752
|
+
extraSeeds.push({ x: round32(seg.cx), z: round32(seg.cz) });
|
|
753
|
+
const halfRidge = seg.effWidth * 0.25;
|
|
754
|
+
if (halfRidge >= 0.4) {
|
|
755
|
+
extraSeeds.push({
|
|
756
|
+
x: round32(seg.cx + halfRidge * seg.effCos),
|
|
757
|
+
z: round32(seg.cz - halfRidge * seg.effSin)
|
|
758
|
+
});
|
|
759
|
+
extraSeeds.push({
|
|
760
|
+
x: round32(seg.cx - halfRidge * seg.effCos),
|
|
761
|
+
z: round32(seg.cz + halfRidge * seg.effSin)
|
|
762
|
+
});
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
for (let ri = 0;ri < sortedRooms.length; ri++) {
|
|
767
|
+
const room = sortedRooms[ri];
|
|
768
|
+
extraSeeds.push({ x: round32(room.centroid[0]), z: round32(room.centroid[1]) });
|
|
769
|
+
}
|
|
770
|
+
for (let pi = 0;pi < pierInteriorSeeds.length; pi++) {
|
|
771
|
+
extraSeeds.push(pierInteriorSeeds[pi]);
|
|
772
|
+
}
|
|
773
|
+
const dedupeRadiusSq = seedDedupeRadius * seedDedupeRadius;
|
|
774
|
+
const primaryConnectRadius = Math.max(2.6, effectiveGridStepM * 1.85);
|
|
775
|
+
const fallbackConnectRadius = Math.max(4.5, effectiveGridStepM * 2.8);
|
|
776
|
+
const fallbackConnectRadiusSq = fallbackConnectRadius * fallbackConnectRadius;
|
|
777
|
+
const coordKey = (x, z) => `${Math.round(x * 1000)}:${Math.round(z * 1000)}`;
|
|
778
|
+
const layerCoordMap = new Map;
|
|
779
|
+
for (let li = 0;li < layerIds.length; li++) {
|
|
780
|
+
const lid = layerIds[li];
|
|
781
|
+
const lv = vertices[lid];
|
|
782
|
+
layerCoordMap.set(coordKey(lv.pos[0], lv.pos[2]), lid);
|
|
783
|
+
}
|
|
784
|
+
const findOrCreateCornerVertex = (cx, cz, allowCreate) => {
|
|
785
|
+
const key = coordKey(cx, cz);
|
|
786
|
+
const existing = layerCoordMap.get(key);
|
|
787
|
+
if (existing !== undefined)
|
|
788
|
+
return existing;
|
|
789
|
+
if (!allowCreate)
|
|
790
|
+
return -1;
|
|
791
|
+
const defaultCornerMaxClearY = layerY < 0 ? floor3(-halfY) : round32(levelH);
|
|
792
|
+
const cornerMaxClearY = checkRoof && roofShell.hasRoofAbove ? floor3(roofShell.roofEnvelopeY(cx, cz) - roofTopOffset) : defaultCornerMaxClearY;
|
|
793
|
+
const cid = addVertex({
|
|
794
|
+
pos: [cx, layerY, cz],
|
|
795
|
+
band,
|
|
796
|
+
maxClearY: cornerMaxClearY
|
|
797
|
+
});
|
|
798
|
+
layerIds.push(cid);
|
|
799
|
+
layerCoordMap.set(key, cid);
|
|
800
|
+
return cid;
|
|
801
|
+
};
|
|
802
|
+
const maxSeedConnections = allowDiagonals ? 3 : 2;
|
|
803
|
+
for (let si = 0;si < extraSeeds.length; si++) {
|
|
804
|
+
if (layerIds.length >= maxVertsPerLayer)
|
|
805
|
+
break;
|
|
806
|
+
const seed = extraSeeds[si];
|
|
807
|
+
if (!isPointLegal(seed.x, seed.z))
|
|
808
|
+
continue;
|
|
809
|
+
const seedPos = [seed.x, layerY, seed.z];
|
|
810
|
+
let tooClose = false;
|
|
811
|
+
for (let li = 0;li < layerIds.length; li++) {
|
|
812
|
+
const ev = vertices[layerIds[li]];
|
|
813
|
+
const dx = ev.pos[0] - seed.x;
|
|
814
|
+
const dz = ev.pos[2] - seed.z;
|
|
815
|
+
if (dx * dx + dz * dz <= dedupeRadiusSq) {
|
|
816
|
+
if (isEdgeLegal(seedPos, ev.pos)) {
|
|
817
|
+
tooClose = true;
|
|
818
|
+
break;
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
if (tooClose)
|
|
823
|
+
continue;
|
|
824
|
+
const defaultSeedMaxClearY = layerY < 0 ? floor3(-halfY) : round32(levelH);
|
|
825
|
+
const maxClearY = checkRoof && roofShell.hasRoofAbove ? floor3(roofShell.roofEnvelopeY(seed.x, seed.z) - roofTopOffset) : defaultSeedMaxClearY;
|
|
826
|
+
const newId = addVertex({
|
|
827
|
+
pos: seedPos,
|
|
828
|
+
band,
|
|
829
|
+
maxClearY
|
|
830
|
+
});
|
|
831
|
+
layerCoordMap.set(coordKey(seed.x, seed.z), newId);
|
|
832
|
+
const candidates = [];
|
|
833
|
+
for (let li = 0;li < layerIds.length; li++) {
|
|
834
|
+
const otherId = layerIds[li];
|
|
835
|
+
const ov = vertices[otherId];
|
|
836
|
+
const dx = ov.pos[0] - seed.x;
|
|
837
|
+
const dz = ov.pos[2] - seed.z;
|
|
838
|
+
const dSq = dx * dx + dz * dz;
|
|
839
|
+
if (dSq <= fallbackConnectRadiusSq && dSq > 0.00000001) {
|
|
840
|
+
candidates.push({ id: otherId, distSq: dSq });
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
candidates.sort((a, b) => a.distSq - b.distSq || a.id - b.id);
|
|
844
|
+
const primaryConnectRadiusSq = primaryConnectRadius * primaryConnectRadius;
|
|
845
|
+
let connected = 0;
|
|
846
|
+
for (let ci = 0;ci < candidates.length && connected < maxSeedConnections; ci++) {
|
|
847
|
+
const cand = candidates[ci];
|
|
848
|
+
if (cand.distSq > primaryConnectRadiusSq && connected > 0)
|
|
849
|
+
break;
|
|
850
|
+
const targetPos = vertices[cand.id].pos;
|
|
851
|
+
const isAxisAligned = Math.abs(seed.x - targetPos[0]) < 0.001 || Math.abs(seed.z - targetPos[2]) < 0.001;
|
|
852
|
+
if (allowDiagonals || isAxisAligned) {
|
|
853
|
+
if (isEdgeLegal(seedPos, targetPos)) {
|
|
854
|
+
addUndirectedEdge(newId, cand.id, {
|
|
855
|
+
isWallEdge: false,
|
|
856
|
+
isVerticalTransition: false,
|
|
857
|
+
baseCostWeight,
|
|
858
|
+
bandOverride: band
|
|
859
|
+
});
|
|
860
|
+
connected += 1;
|
|
861
|
+
}
|
|
862
|
+
} else {
|
|
863
|
+
const corners = [
|
|
864
|
+
[seed.x, layerY, targetPos[2]],
|
|
865
|
+
[targetPos[0], layerY, seed.z]
|
|
866
|
+
];
|
|
867
|
+
for (let ki = 0;ki < corners.length; ki++) {
|
|
868
|
+
const cornerPos = corners[ki];
|
|
869
|
+
if (!isPointLegal(cornerPos[0], cornerPos[2]))
|
|
870
|
+
continue;
|
|
871
|
+
if (!isEdgeLegal(seedPos, cornerPos) || !isEdgeLegal(cornerPos, targetPos))
|
|
872
|
+
continue;
|
|
873
|
+
const cornerId = findOrCreateCornerVertex(cornerPos[0], cornerPos[2], layerIds.length < maxVertsPerLayer || connected === 0);
|
|
874
|
+
if (cornerId === -1)
|
|
875
|
+
continue;
|
|
876
|
+
const e1 = addUndirectedEdge(newId, cornerId, {
|
|
877
|
+
isWallEdge: false,
|
|
878
|
+
isVerticalTransition: false,
|
|
879
|
+
baseCostWeight,
|
|
880
|
+
bandOverride: band
|
|
881
|
+
});
|
|
882
|
+
const e2 = addUndirectedEdge(cornerId, cand.id, {
|
|
883
|
+
isWallEdge: false,
|
|
884
|
+
isVerticalTransition: false,
|
|
885
|
+
baseCostWeight,
|
|
886
|
+
bandOverride: band
|
|
887
|
+
});
|
|
888
|
+
if (e1 || e2) {
|
|
889
|
+
connected += 1;
|
|
890
|
+
break;
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
layerIds.push(newId);
|
|
896
|
+
}
|
|
897
|
+
return { vertexIds: layerIds };
|
|
898
|
+
};
|
|
899
|
+
const plenumLayer = ceilingPlenumY > 0.1 ? buildHorizontalLayer("overhead-plenum", ceilingPlenumY, true, false, COST_OVERHEAD_PLENUM) : { vertexIds: [] };
|
|
900
|
+
const atticLayer = atticY !== null ? buildHorizontalLayer("attic", atticY, true, true, COST_ATTIC) : { vertexIds: [] };
|
|
901
|
+
const subFloorLayer = subFloorY !== null ? buildHorizontalLayer(subFloorBand, subFloorY, false, false, COST_SUB_FLOOR) : { vertexIds: [] };
|
|
902
|
+
const connectToNearestInLayer = (srcId, targetLayerIds, maxRadiusM, checkRoof, checkOpenings, preferOrthogonal = false) => {
|
|
903
|
+
if (targetLayerIds.length === 0)
|
|
904
|
+
return false;
|
|
905
|
+
const srcPos = vertices[srcId].pos;
|
|
906
|
+
const maxRadiusSq = maxRadiusM * maxRadiusM;
|
|
907
|
+
const candidates = [];
|
|
908
|
+
for (let i = 0;i < targetLayerIds.length; i++) {
|
|
909
|
+
const tid = targetLayerIds[i];
|
|
910
|
+
const tp = vertices[tid].pos;
|
|
911
|
+
const dx = tp[0] - srcPos[0];
|
|
912
|
+
const dz = tp[2] - srcPos[2];
|
|
913
|
+
const dSq = dx * dx + dz * dz;
|
|
914
|
+
if (dSq <= maxRadiusSq) {
|
|
915
|
+
candidates.push({ id: tid, distSq: dSq });
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
candidates.sort((a, b) => a.distSq - b.distSq || a.id - b.id);
|
|
919
|
+
for (let i = 0;i < candidates.length; i++) {
|
|
920
|
+
const cand = candidates[i];
|
|
921
|
+
const tid = cand.id;
|
|
922
|
+
const targetVertex = vertices[tid];
|
|
923
|
+
const targetPos = targetVertex.pos;
|
|
924
|
+
const dx = Math.abs(targetPos[0] - srcPos[0]);
|
|
925
|
+
const dy = Math.abs(targetPos[1] - srcPos[1]);
|
|
926
|
+
const dz = Math.abs(targetPos[2] - srcPos[2]);
|
|
927
|
+
const horizDist = Math.hypot(dx, dz);
|
|
928
|
+
if (preferOrthogonal && horizDist > 0.002) {
|
|
929
|
+
const isAxisAligned = dx <= 0.001 || dz <= 0.001;
|
|
930
|
+
if (dy <= 0.001) {
|
|
931
|
+
if (isAxisAligned) {
|
|
932
|
+
const directOk = isFootprintLayerSegInside(srcPos, targetPos) && (!checkRoof || isRoofSegmentClear(srcPos, targetPos)) && (!checkOpenings || !segmentIntersectsAnyOpening(srcPos, targetPos, openingPrisms, openingPad));
|
|
933
|
+
if (directOk) {
|
|
934
|
+
addUndirectedEdge(srcId, tid, {
|
|
935
|
+
isWallEdge: false,
|
|
936
|
+
isVerticalTransition: false,
|
|
937
|
+
baseCostWeight: COST_LAYER_TRANSITION,
|
|
938
|
+
bandOverride: targetVertex.band
|
|
939
|
+
});
|
|
940
|
+
return true;
|
|
941
|
+
}
|
|
942
|
+
} else if (vertices.length + 1 <= MAX_TOTAL_VERTICES_TARGET) {
|
|
943
|
+
const corners = [
|
|
944
|
+
[srcPos[0], targetPos[1], targetPos[2]],
|
|
945
|
+
[targetPos[0], targetPos[1], srcPos[2]]
|
|
946
|
+
];
|
|
947
|
+
for (let ki = 0;ki < corners.length; ki++) {
|
|
948
|
+
const cornerPos = corners[ki];
|
|
949
|
+
const seg1Ok = isFootprintLayerSegInside(srcPos, cornerPos) && (!checkRoof || isRoofSegmentClear(srcPos, cornerPos)) && (!checkOpenings || !segmentIntersectsAnyOpening(srcPos, cornerPos, openingPrisms, openingPad));
|
|
950
|
+
if (!seg1Ok)
|
|
951
|
+
continue;
|
|
952
|
+
const seg2Ok = isFootprintLayerSegInside(cornerPos, targetPos) && (!checkRoof || isRoofSegmentClear(cornerPos, targetPos)) && (!checkOpenings || !segmentIntersectsAnyOpening(cornerPos, targetPos, openingPrisms, openingPad));
|
|
953
|
+
if (!seg2Ok)
|
|
954
|
+
continue;
|
|
955
|
+
const cornerMaxClearY = targetPos[1] < 0 ? floor3(-halfY) : checkRoof && roofShell.hasRoofAbove ? floor3(roofShell.roofEnvelopeY(cornerPos[0], cornerPos[2]) - roofTopOffset) : round32(levelH);
|
|
956
|
+
const cornerId = addVertex({
|
|
957
|
+
pos: cornerPos,
|
|
958
|
+
band: targetVertex.band,
|
|
959
|
+
maxClearY: cornerMaxClearY
|
|
960
|
+
});
|
|
961
|
+
addUndirectedEdge(srcId, cornerId, {
|
|
962
|
+
isWallEdge: false,
|
|
963
|
+
isVerticalTransition: false,
|
|
964
|
+
baseCostWeight: COST_LAYER_TRANSITION,
|
|
965
|
+
bandOverride: targetVertex.band
|
|
966
|
+
});
|
|
967
|
+
addUndirectedEdge(cornerId, tid, {
|
|
968
|
+
isWallEdge: false,
|
|
969
|
+
isVerticalTransition: false,
|
|
970
|
+
baseCostWeight: COST_LAYER_TRANSITION,
|
|
971
|
+
bandOverride: targetVertex.band
|
|
972
|
+
});
|
|
973
|
+
return true;
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
} else if (vertices.length + 2 <= MAX_TOTAL_VERTICES_TARGET) {
|
|
977
|
+
const plumbPos = [srcPos[0], targetPos[1], srcPos[2]];
|
|
978
|
+
const plumbOk = (!checkRoof || isRoofSegmentClear(srcPos, plumbPos)) && (!checkOpenings || !segmentIntersectsAnyOpening(srcPos, plumbPos, openingPrisms, openingPad));
|
|
979
|
+
if (plumbOk) {
|
|
980
|
+
if (isAxisAligned) {
|
|
981
|
+
const legOk = isFootprintLayerSegInside(plumbPos, targetPos) && (!checkRoof || isRoofSegmentClear(plumbPos, targetPos)) && (!checkOpenings || !segmentIntersectsAnyOpening(plumbPos, targetPos, openingPrisms, openingPad));
|
|
982
|
+
if (legOk) {
|
|
983
|
+
const plumbMaxClearY = targetPos[1] < 0 ? floor3(-halfY) : checkRoof && roofShell.hasRoofAbove ? floor3(roofShell.roofEnvelopeY(plumbPos[0], plumbPos[2]) - roofTopOffset) : round32(levelH);
|
|
984
|
+
const plumbId = addVertex({
|
|
985
|
+
pos: plumbPos,
|
|
986
|
+
band: targetVertex.band,
|
|
987
|
+
maxClearY: plumbMaxClearY
|
|
988
|
+
});
|
|
989
|
+
addUndirectedEdge(srcId, plumbId, {
|
|
990
|
+
isWallEdge: false,
|
|
991
|
+
isVerticalTransition: true,
|
|
992
|
+
baseCostWeight: COST_LAYER_TRANSITION
|
|
993
|
+
});
|
|
994
|
+
addUndirectedEdge(plumbId, tid, {
|
|
995
|
+
isWallEdge: false,
|
|
996
|
+
isVerticalTransition: false,
|
|
997
|
+
baseCostWeight: COST_LAYER_TRANSITION,
|
|
998
|
+
bandOverride: targetVertex.band
|
|
999
|
+
});
|
|
1000
|
+
return true;
|
|
1001
|
+
}
|
|
1002
|
+
} else {
|
|
1003
|
+
const corners = [
|
|
1004
|
+
[plumbPos[0], targetPos[1], targetPos[2]],
|
|
1005
|
+
[targetPos[0], targetPos[1], plumbPos[2]]
|
|
1006
|
+
];
|
|
1007
|
+
for (let ki = 0;ki < corners.length; ki++) {
|
|
1008
|
+
const cornerPos = corners[ki];
|
|
1009
|
+
const seg1Ok = isFootprintLayerSegInside(plumbPos, cornerPos) && (!checkRoof || isRoofSegmentClear(plumbPos, cornerPos)) && (!checkOpenings || !segmentIntersectsAnyOpening(plumbPos, cornerPos, openingPrisms, openingPad));
|
|
1010
|
+
if (!seg1Ok)
|
|
1011
|
+
continue;
|
|
1012
|
+
const seg2Ok = isFootprintLayerSegInside(cornerPos, targetPos) && (!checkRoof || isRoofSegmentClear(cornerPos, targetPos)) && (!checkOpenings || !segmentIntersectsAnyOpening(cornerPos, targetPos, openingPrisms, openingPad));
|
|
1013
|
+
if (!seg2Ok)
|
|
1014
|
+
continue;
|
|
1015
|
+
const plumbMaxClearY = targetPos[1] < 0 ? floor3(-halfY) : checkRoof && roofShell.hasRoofAbove ? floor3(roofShell.roofEnvelopeY(plumbPos[0], plumbPos[2]) - roofTopOffset) : round32(levelH);
|
|
1016
|
+
const cornerMaxClearY = targetPos[1] < 0 ? floor3(-halfY) : checkRoof && roofShell.hasRoofAbove ? floor3(roofShell.roofEnvelopeY(cornerPos[0], cornerPos[2]) - roofTopOffset) : round32(levelH);
|
|
1017
|
+
const plumbId = addVertex({
|
|
1018
|
+
pos: plumbPos,
|
|
1019
|
+
band: targetVertex.band,
|
|
1020
|
+
maxClearY: plumbMaxClearY
|
|
1021
|
+
});
|
|
1022
|
+
const cornerId = addVertex({
|
|
1023
|
+
pos: cornerPos,
|
|
1024
|
+
band: targetVertex.band,
|
|
1025
|
+
maxClearY: cornerMaxClearY
|
|
1026
|
+
});
|
|
1027
|
+
addUndirectedEdge(srcId, plumbId, {
|
|
1028
|
+
isWallEdge: false,
|
|
1029
|
+
isVerticalTransition: true,
|
|
1030
|
+
baseCostWeight: COST_LAYER_TRANSITION
|
|
1031
|
+
});
|
|
1032
|
+
addUndirectedEdge(plumbId, cornerId, {
|
|
1033
|
+
isWallEdge: false,
|
|
1034
|
+
isVerticalTransition: false,
|
|
1035
|
+
baseCostWeight: COST_LAYER_TRANSITION,
|
|
1036
|
+
bandOverride: targetVertex.band
|
|
1037
|
+
});
|
|
1038
|
+
addUndirectedEdge(cornerId, tid, {
|
|
1039
|
+
isWallEdge: false,
|
|
1040
|
+
isVerticalTransition: false,
|
|
1041
|
+
baseCostWeight: COST_LAYER_TRANSITION,
|
|
1042
|
+
bandOverride: targetVertex.band
|
|
1043
|
+
});
|
|
1044
|
+
return true;
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
if (cand.distSq > 0.00000001 && !isFootprintLayerSegInside(srcPos, targetPos))
|
|
1051
|
+
continue;
|
|
1052
|
+
if (checkRoof && !isRoofSegmentClear(srcPos, targetPos))
|
|
1053
|
+
continue;
|
|
1054
|
+
if (checkOpenings && segmentIntersectsAnyOpening(srcPos, targetPos, openingPrisms, openingPad)) {
|
|
1055
|
+
continue;
|
|
1056
|
+
}
|
|
1057
|
+
addUndirectedEdge(srcId, tid, {
|
|
1058
|
+
isWallEdge: false,
|
|
1059
|
+
isVerticalTransition: dy > 0.001,
|
|
1060
|
+
baseCostWeight: COST_LAYER_TRANSITION
|
|
1061
|
+
});
|
|
1062
|
+
return true;
|
|
1063
|
+
}
|
|
1064
|
+
return false;
|
|
1065
|
+
};
|
|
1066
|
+
const wallToPlenumRadius = Math.max(3, effectiveGridStepM * 2.2);
|
|
1067
|
+
const wallToAtticRadius = Math.max(3.6, effectiveGridStepM * 2.5);
|
|
1068
|
+
const plenumToAtticRadius = Math.max(2.2, effectiveGridStepM * 1.45);
|
|
1069
|
+
const topPlateIds = wallBandVertexIds["top-plate"];
|
|
1070
|
+
for (let i = 0;i < topPlateIds.length; i++) {
|
|
1071
|
+
const tpId = topPlateIds[i];
|
|
1072
|
+
connectToNearestInLayer(tpId, plenumLayer.vertexIds, wallToPlenumRadius, true, true, true);
|
|
1073
|
+
if (atticLayer.vertexIds.length > 0) {
|
|
1074
|
+
connectToNearestInLayer(tpId, atticLayer.vertexIds, wallToAtticRadius, true, true, false);
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
for (let i = 0;i < atticLayer.vertexIds.length; i++) {
|
|
1078
|
+
const avId = atticLayer.vertexIds[i];
|
|
1079
|
+
connectToNearestInLayer(avId, plenumLayer.vertexIds, plenumToAtticRadius, true, true, false);
|
|
1080
|
+
}
|
|
1081
|
+
if (subFloorLayer.vertexIds.length > 0) {
|
|
1082
|
+
const connectedPierRoots = new Set;
|
|
1083
|
+
for (let wi = 0;wi < wallStations.length; wi++) {
|
|
1084
|
+
const stations = wallStations[wi];
|
|
1085
|
+
for (let si = 0;si < stations.length; si++) {
|
|
1086
|
+
const st = stations[si];
|
|
1087
|
+
if (!st.isRiserPier)
|
|
1088
|
+
continue;
|
|
1089
|
+
const rootId = st.bandVertexIds["under-sill"] ?? st.bandVertexIds["mid-wall"] ?? st.bandVertexIds["top-plate"];
|
|
1090
|
+
if (rootId === undefined || connectedPierRoots.has(rootId))
|
|
1091
|
+
continue;
|
|
1092
|
+
connectedPierRoots.add(rootId);
|
|
1093
|
+
connectToNearestInLayer(rootId, subFloorLayer.vertexIds, wallToPlenumRadius, false, true, true);
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
return {
|
|
1098
|
+
levelId,
|
|
1099
|
+
levelHeightM: levelH,
|
|
1100
|
+
vertices,
|
|
1101
|
+
adjacency,
|
|
1102
|
+
wallRiserPiers,
|
|
1103
|
+
bandsPresent,
|
|
1104
|
+
ceilingPlenumY,
|
|
1105
|
+
atticY,
|
|
1106
|
+
subFloorY,
|
|
1107
|
+
compiledHalfXZ: halfXZ,
|
|
1108
|
+
compiledHalfY: halfY,
|
|
1109
|
+
compiledRoofClearanceM: roofClearanceM,
|
|
1110
|
+
minBaseWeight
|
|
1111
|
+
};
|
|
1112
|
+
}
|
|
1113
|
+
function findLegalWallRiserPier2(graph, wallId, desiredT, fromY, toY) {
|
|
1114
|
+
if (!Number.isFinite(desiredT) || !Number.isFinite(fromY) || !Number.isFinite(toY))
|
|
1115
|
+
return null;
|
|
1116
|
+
const piers = graph.wallRiserPiers.get(wallId);
|
|
1117
|
+
if (!piers || piers.length === 0)
|
|
1118
|
+
return null;
|
|
1119
|
+
const loY = Math.min(fromY, toY);
|
|
1120
|
+
const hiY = Math.max(fromY, toY);
|
|
1121
|
+
const effectiveLoY = loY < RISER_MIN_Y ? RISER_MIN_Y : loY;
|
|
1122
|
+
let best = null;
|
|
1123
|
+
let bestDelta = Number.POSITIVE_INFINITY;
|
|
1124
|
+
for (let i = 0;i < piers.length; i++) {
|
|
1125
|
+
const pier = piers[i];
|
|
1126
|
+
if (pier.minSafeY > effectiveLoY + 0.0001)
|
|
1127
|
+
continue;
|
|
1128
|
+
if (pier.maxSafeY < hiY - 0.0001)
|
|
1129
|
+
continue;
|
|
1130
|
+
const delta = Math.abs(pier.t - desiredT);
|
|
1131
|
+
if (delta < bestDelta - 0.000001 || Math.abs(delta - bestDelta) <= 0.000001 && best && pier.t < best.t) {
|
|
1132
|
+
bestDelta = delta;
|
|
1133
|
+
best = pier;
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
return best;
|
|
1137
|
+
}
|
|
1138
|
+
// src/router/shortest-path.ts
|
|
1139
|
+
var DEFAULT_ROOF_CLEARANCE_M2 = 0.14;
|
|
1140
|
+
var DEFAULT_MIN_SLOPE_IN_PER_FT = 0.25;
|
|
1141
|
+
var UPHILL_TOL_M = 0.002;
|
|
1142
|
+
var DIR_BUCKETS = 19;
|
|
1143
|
+
var CLASH_PENALTY = 5000;
|
|
1144
|
+
var OPENING_PENALTY = 2000;
|
|
1145
|
+
var ROOF_PENALTY = 2000;
|
|
1146
|
+
var OUTDOOR_PENALTY = 2000;
|
|
1147
|
+
var PHYSICS_PENALTY = 1000;
|
|
1148
|
+
var BEND_COST_PER_DEG = 0.0025;
|
|
1149
|
+
var WALL_BANDS2 = new Set([
|
|
1150
|
+
"under-sill",
|
|
1151
|
+
"mid-wall",
|
|
1152
|
+
"top-plate"
|
|
1153
|
+
]);
|
|
1154
|
+
var OPEN_CAVITY_BANDS = new Set([
|
|
1155
|
+
"attic",
|
|
1156
|
+
"crawlspace",
|
|
1157
|
+
"overhead-plenum",
|
|
1158
|
+
"under-slab"
|
|
1159
|
+
]);
|
|
1160
|
+
var autoRunCounter = 1;
|
|
1161
|
+
|
|
1162
|
+
class BinaryMinHeap {
|
|
1163
|
+
items = [];
|
|
1164
|
+
get size() {
|
|
1165
|
+
return this.items.length;
|
|
1166
|
+
}
|
|
1167
|
+
push(entry) {
|
|
1168
|
+
const items = this.items;
|
|
1169
|
+
items.push(entry);
|
|
1170
|
+
let idx = items.length - 1;
|
|
1171
|
+
while (idx > 0) {
|
|
1172
|
+
const parentIdx = idx - 1 >> 1;
|
|
1173
|
+
const parent = items[parentIdx];
|
|
1174
|
+
if (this.compare(entry, parent) >= 0)
|
|
1175
|
+
break;
|
|
1176
|
+
items[idx] = parent;
|
|
1177
|
+
idx = parentIdx;
|
|
1178
|
+
}
|
|
1179
|
+
items[idx] = entry;
|
|
1180
|
+
}
|
|
1181
|
+
pop() {
|
|
1182
|
+
const items = this.items;
|
|
1183
|
+
const n = items.length;
|
|
1184
|
+
if (n === 0)
|
|
1185
|
+
return;
|
|
1186
|
+
const top = items[0];
|
|
1187
|
+
const last = items.pop();
|
|
1188
|
+
if (n > 1) {
|
|
1189
|
+
let idx = 0;
|
|
1190
|
+
const half = n - 1 >> 1;
|
|
1191
|
+
while (idx < half) {
|
|
1192
|
+
let leftIdx = (idx << 1) + 1;
|
|
1193
|
+
const rightIdx = leftIdx + 1;
|
|
1194
|
+
let smallest = items[leftIdx];
|
|
1195
|
+
if (rightIdx < n - 1) {
|
|
1196
|
+
const right = items[rightIdx];
|
|
1197
|
+
if (this.compare(right, smallest) < 0) {
|
|
1198
|
+
leftIdx = rightIdx;
|
|
1199
|
+
smallest = right;
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
if (this.compare(last, smallest) <= 0)
|
|
1203
|
+
break;
|
|
1204
|
+
items[idx] = smallest;
|
|
1205
|
+
idx = leftIdx;
|
|
1206
|
+
}
|
|
1207
|
+
items[idx] = last;
|
|
1208
|
+
}
|
|
1209
|
+
return top;
|
|
1210
|
+
}
|
|
1211
|
+
compare(a, b) {
|
|
1212
|
+
if (a.f !== b.f)
|
|
1213
|
+
return a.f - b.f;
|
|
1214
|
+
if (a.g !== b.g)
|
|
1215
|
+
return a.g - b.g;
|
|
1216
|
+
return a.seq - b.seq;
|
|
1217
|
+
}
|
|
1218
|
+
}
|
|
1219
|
+
function sanitizeVec3(v) {
|
|
1220
|
+
const x = typeof v[0] === "number" && Number.isFinite(v[0]) ? v[0] : 0;
|
|
1221
|
+
const y = typeof v[1] === "number" && Number.isFinite(v[1]) ? v[1] : 0;
|
|
1222
|
+
const z = typeof v[2] === "number" && Number.isFinite(v[2]) ? v[2] : 0;
|
|
1223
|
+
return [x, y, z];
|
|
1224
|
+
}
|
|
1225
|
+
function roundVec3(v) {
|
|
1226
|
+
return [round42(v[0]), round42(v[1]), round42(v[2])];
|
|
1227
|
+
}
|
|
1228
|
+
function dist32(a, b) {
|
|
1229
|
+
return Math.hypot(b[0] - a[0], b[1] - a[1], b[2] - a[2]);
|
|
1230
|
+
}
|
|
1231
|
+
function distXZ2(a, b) {
|
|
1232
|
+
return Math.hypot(b[0] - a[0], b[2] - a[2]);
|
|
1233
|
+
}
|
|
1234
|
+
function unitDir(a, b) {
|
|
1235
|
+
const dx = b[0] - a[0];
|
|
1236
|
+
const dy = b[1] - a[1];
|
|
1237
|
+
const dz = b[2] - a[2];
|
|
1238
|
+
const len = Math.hypot(dx, dy, dz);
|
|
1239
|
+
if (len < 0.00001)
|
|
1240
|
+
return null;
|
|
1241
|
+
return [dx / len, dy / len, dz / len];
|
|
1242
|
+
}
|
|
1243
|
+
function turnAngleDeg(d1, d2) {
|
|
1244
|
+
if (!d1 || !d2)
|
|
1245
|
+
return 0;
|
|
1246
|
+
const dot = Math.max(-1, Math.min(1, d1[0] * d2[0] + d1[1] * d2[1] + d1[2] * d2[2]));
|
|
1247
|
+
const deg = Math.acos(dot) * 180 / Math.PI;
|
|
1248
|
+
return deg > 2 ? deg : 0;
|
|
1249
|
+
}
|
|
1250
|
+
function dirToBucket(d) {
|
|
1251
|
+
if (!d)
|
|
1252
|
+
return 0;
|
|
1253
|
+
const [x, y, z] = d;
|
|
1254
|
+
const ax = Math.abs(x);
|
|
1255
|
+
const ay = Math.abs(y);
|
|
1256
|
+
const az = Math.abs(z);
|
|
1257
|
+
if (ay >= 0.85)
|
|
1258
|
+
return y >= 0 ? 5 : 6;
|
|
1259
|
+
if (ay > 0.25) {
|
|
1260
|
+
const quad = x >= 0 ? z >= 0 ? 0 : 1 : z >= 0 ? 2 : 3;
|
|
1261
|
+
return (y >= 0 ? 11 : 15) + quad;
|
|
1262
|
+
}
|
|
1263
|
+
if (ax >= 0.85)
|
|
1264
|
+
return x >= 0 ? 1 : 2;
|
|
1265
|
+
if (az >= 0.85)
|
|
1266
|
+
return z >= 0 ? 3 : 4;
|
|
1267
|
+
if (x >= 0 && z >= 0)
|
|
1268
|
+
return 7;
|
|
1269
|
+
if (x >= 0 && z < 0)
|
|
1270
|
+
return 8;
|
|
1271
|
+
if (x < 0 && z >= 0)
|
|
1272
|
+
return 9;
|
|
1273
|
+
return 10;
|
|
1274
|
+
}
|
|
1275
|
+
function segmentHitsAnyOpening(a, b, prisms) {
|
|
1276
|
+
for (let i = 0;i < prisms.length; i++) {
|
|
1277
|
+
if (segmentIntersectsOrientedPrismY2(a, b, prisms[i], 0))
|
|
1278
|
+
return true;
|
|
1279
|
+
}
|
|
1280
|
+
return false;
|
|
1281
|
+
}
|
|
1282
|
+
function compactPolyline(points, eps = 0.0001) {
|
|
1283
|
+
const deduped = [];
|
|
1284
|
+
for (const pt of points) {
|
|
1285
|
+
const clean = sanitizeVec3(pt);
|
|
1286
|
+
if (deduped.length === 0 || dist32(deduped[deduped.length - 1], clean) > eps) {
|
|
1287
|
+
deduped.push(clean);
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1290
|
+
if (deduped.length <= 2)
|
|
1291
|
+
return deduped;
|
|
1292
|
+
const out = [deduped[0]];
|
|
1293
|
+
for (let i = 1;i < deduped.length - 1; i++) {
|
|
1294
|
+
const prev = out[out.length - 1];
|
|
1295
|
+
const cur = deduped[i];
|
|
1296
|
+
const next = deduped[i + 1];
|
|
1297
|
+
const d1 = unitDir(prev, cur);
|
|
1298
|
+
const d2 = unitDir(cur, next);
|
|
1299
|
+
if (d1 && d2) {
|
|
1300
|
+
const dot = d1[0] * d2[0] + d1[1] * d2[1] + d1[2] * d2[2];
|
|
1301
|
+
if (dot > 0.9998)
|
|
1302
|
+
continue;
|
|
1303
|
+
}
|
|
1304
|
+
out.push(cur);
|
|
1305
|
+
}
|
|
1306
|
+
out.push(deduped[deduped.length - 1]);
|
|
1307
|
+
return out;
|
|
1308
|
+
}
|
|
1309
|
+
function checkSegmentPhysicsOk(a, b, ctx) {
|
|
1310
|
+
const hDist = distXZ2(a, b);
|
|
1311
|
+
const dy = b[1] - a[1];
|
|
1312
|
+
if (ctx.mode === "gravity-slope") {
|
|
1313
|
+
if (dy > UPHILL_TOL_M)
|
|
1314
|
+
return false;
|
|
1315
|
+
return true;
|
|
1316
|
+
}
|
|
1317
|
+
if (ctx.mode === "non-descending-vent") {
|
|
1318
|
+
if (dy < -UPHILL_TOL_M)
|
|
1319
|
+
return false;
|
|
1320
|
+
const floorY = ctx.minHorizY;
|
|
1321
|
+
if (floorY !== undefined && hDist > 0.01) {
|
|
1322
|
+
if (a[1] < floorY - 0.0001 || b[1] < floorY - 0.0001)
|
|
1323
|
+
return false;
|
|
1324
|
+
}
|
|
1325
|
+
const ceilingEnforced = floorY === undefined || ctx.tieInY >= floorY - 0.0001;
|
|
1326
|
+
if (ceilingEnforced) {
|
|
1327
|
+
if (a[1] > ctx.tieInY + 0.0001 || b[1] > ctx.tieInY + 0.0001)
|
|
1328
|
+
return false;
|
|
1329
|
+
}
|
|
1330
|
+
return true;
|
|
1331
|
+
}
|
|
1332
|
+
if (ctx.minHorizY !== undefined && hDist > 0.01) {
|
|
1333
|
+
if (a[1] < ctx.minHorizY - 0.0001 || b[1] < ctx.minHorizY - 0.0001)
|
|
1334
|
+
return false;
|
|
1335
|
+
}
|
|
1336
|
+
return true;
|
|
1337
|
+
}
|
|
1338
|
+
function evaluatePolyline(rawPoints, ctx, allowStartExteriorMargin, allowEndExteriorMargin = allowStartExteriorMargin) {
|
|
1339
|
+
const points = compactPolyline(rawPoints);
|
|
1340
|
+
const clashSet = new Set;
|
|
1341
|
+
let openingHits = 0;
|
|
1342
|
+
let footprintOk = true;
|
|
1343
|
+
let physicsHardOk = true;
|
|
1344
|
+
let lengthM = 0;
|
|
1345
|
+
let internalBendDeg = 0;
|
|
1346
|
+
let firstDir = null;
|
|
1347
|
+
let lastDir = null;
|
|
1348
|
+
for (let i = 1;i < points.length; i++) {
|
|
1349
|
+
const a = points[i - 1];
|
|
1350
|
+
const b = points[i];
|
|
1351
|
+
const segLen = dist32(a, b);
|
|
1352
|
+
lengthM += segLen;
|
|
1353
|
+
const d = unitDir(a, b);
|
|
1354
|
+
if (d) {
|
|
1355
|
+
if (!firstDir)
|
|
1356
|
+
firstDir = d;
|
|
1357
|
+
if (lastDir)
|
|
1358
|
+
internalBendDeg += turnAngleDeg(lastDir, d);
|
|
1359
|
+
lastDir = d;
|
|
1360
|
+
}
|
|
1361
|
+
const hits = ctx.occupancy.segmentClashes(a, b, ctx.clashParams);
|
|
1362
|
+
for (const obs of hits) {
|
|
1363
|
+
clashSet.add(obs.nodeId);
|
|
1364
|
+
ctx.encounteredObstacleIds.add(obs.nodeId);
|
|
1365
|
+
}
|
|
1366
|
+
if (segmentHitsAnyOpening(a, b, ctx.openingPrisms)) {
|
|
1367
|
+
openingHits += 1;
|
|
1368
|
+
}
|
|
1369
|
+
if (footprintOk) {
|
|
1370
|
+
const segStartSkip = i === 1 && allowStartExteriorMargin;
|
|
1371
|
+
const segEndSkip = i === points.length - 1 && allowEndExteriorMargin;
|
|
1372
|
+
const segFpOpts = segStartSkip || segEndSkip ? {
|
|
1373
|
+
skipExteriorEndpointMarginM: 0.35,
|
|
1374
|
+
allowStartExteriorSkip: segStartSkip,
|
|
1375
|
+
allowEndExteriorSkip: segEndSkip
|
|
1376
|
+
} : undefined;
|
|
1377
|
+
if (!ctx.footprint.isSegmentInside(a, b, segFpOpts)) {
|
|
1378
|
+
footprintOk = false;
|
|
1379
|
+
}
|
|
1380
|
+
}
|
|
1381
|
+
if (physicsHardOk && !checkSegmentPhysicsOk(a, b, ctx)) {
|
|
1382
|
+
physicsHardOk = false;
|
|
1383
|
+
}
|
|
1384
|
+
}
|
|
1385
|
+
const roofOk = ctx.roofShell.isPathClearOfRoof(points, ctx.halfY, ctx.roofClearanceM);
|
|
1386
|
+
return {
|
|
1387
|
+
points,
|
|
1388
|
+
lengthM,
|
|
1389
|
+
internalBendDeg,
|
|
1390
|
+
firstDir,
|
|
1391
|
+
lastDir,
|
|
1392
|
+
clashNodeIds: [...clashSet],
|
|
1393
|
+
openingHits,
|
|
1394
|
+
roofOk,
|
|
1395
|
+
footprintOk,
|
|
1396
|
+
physicsHardOk
|
|
1397
|
+
};
|
|
1398
|
+
}
|
|
1399
|
+
function evalPenalty(ev) {
|
|
1400
|
+
let pen = 0;
|
|
1401
|
+
pen += ev.clashNodeIds.length * CLASH_PENALTY;
|
|
1402
|
+
pen += ev.openingHits * OPENING_PENALTY;
|
|
1403
|
+
if (!ev.roofOk)
|
|
1404
|
+
pen += ROOF_PENALTY;
|
|
1405
|
+
if (!ev.footprintOk)
|
|
1406
|
+
pen += OUTDOOR_PENALTY;
|
|
1407
|
+
if (!ev.physicsHardOk)
|
|
1408
|
+
pen += PHYSICS_PENALTY;
|
|
1409
|
+
return pen;
|
|
1410
|
+
}
|
|
1411
|
+
function isHardClean(ev) {
|
|
1412
|
+
return ev.clashNodeIds.length === 0 && ev.openingHits === 0 && ev.roofOk && ev.footprintOk && ev.physicsHardOk;
|
|
1413
|
+
}
|
|
1414
|
+
function selectCandidateSpliceVertices(pt, graph, allowedBandSet, expanded = false) {
|
|
1415
|
+
const scored = [];
|
|
1416
|
+
for (let i = 0;i < graph.vertices.length; i++) {
|
|
1417
|
+
const v = graph.vertices[i];
|
|
1418
|
+
if (!allowedBandSet.has(v.band))
|
|
1419
|
+
continue;
|
|
1420
|
+
const dx = v.pos[0] - pt[0];
|
|
1421
|
+
const dy = v.pos[1] - pt[1];
|
|
1422
|
+
const dz = v.pos[2] - pt[2];
|
|
1423
|
+
const dXZSq = dx * dx + dz * dz;
|
|
1424
|
+
scored.push({ v, dXZSq, d3Sq: dXZSq + dy * dy });
|
|
1425
|
+
}
|
|
1426
|
+
if (scored.length === 0)
|
|
1427
|
+
return [];
|
|
1428
|
+
const byXZ = [...scored].sort((a, b) => a.dXZSq - b.dXZSq || a.d3Sq - b.d3Sq || a.v.id - b.v.id);
|
|
1429
|
+
const by3D = [...scored].sort((a, b) => a.d3Sq - b.d3Sq || a.v.id - b.v.id);
|
|
1430
|
+
const minDistXZ = Math.sqrt(byXZ[0].dXZSq);
|
|
1431
|
+
const localRadiusXZ = expanded ? Number.POSITIVE_INFINITY : Math.max(2, minDistXZ + 1.5);
|
|
1432
|
+
const localRadiusXZSq = Number.isFinite(localRadiusXZ) ? localRadiusXZ * localRadiusXZ : Number.POSITIVE_INFINITY;
|
|
1433
|
+
const maxXZCount = expanded ? 16 : 8;
|
|
1434
|
+
const max3DCount = expanded ? 12 : 6;
|
|
1435
|
+
const selectedIds = new Set;
|
|
1436
|
+
const picked = [];
|
|
1437
|
+
const addVertex = (v) => {
|
|
1438
|
+
if (!selectedIds.has(v.id)) {
|
|
1439
|
+
selectedIds.add(v.id);
|
|
1440
|
+
picked.push(v);
|
|
1441
|
+
}
|
|
1442
|
+
};
|
|
1443
|
+
for (let i = 0;i < byXZ.length; i++) {
|
|
1444
|
+
if (picked.length >= maxXZCount)
|
|
1445
|
+
break;
|
|
1446
|
+
const s = byXZ[i];
|
|
1447
|
+
if (s.dXZSq <= localRadiusXZSq || picked.length < 2)
|
|
1448
|
+
addVertex(s.v);
|
|
1449
|
+
}
|
|
1450
|
+
let added3D = 0;
|
|
1451
|
+
for (let i = 0;i < by3D.length; i++) {
|
|
1452
|
+
if (added3D >= max3DCount)
|
|
1453
|
+
break;
|
|
1454
|
+
const s = by3D[i];
|
|
1455
|
+
if (s.dXZSq <= localRadiusXZSq || picked.length < 2) {
|
|
1456
|
+
addVertex(s.v);
|
|
1457
|
+
added3D++;
|
|
1458
|
+
}
|
|
1459
|
+
}
|
|
1460
|
+
let addedPiers = 0;
|
|
1461
|
+
for (let i = 0;i < byXZ.length && addedPiers < 4; i++) {
|
|
1462
|
+
const s = byXZ[i];
|
|
1463
|
+
if (s.v.isRiserPier && s.dXZSq <= localRadiusXZSq) {
|
|
1464
|
+
addVertex(s.v);
|
|
1465
|
+
addedPiers++;
|
|
1466
|
+
}
|
|
1467
|
+
}
|
|
1468
|
+
const perBandCounts = new Map;
|
|
1469
|
+
for (let i = 0;i < byXZ.length; i++) {
|
|
1470
|
+
const v = byXZ[i].v;
|
|
1471
|
+
const c = perBandCounts.get(v.band) ?? 0;
|
|
1472
|
+
if (c < 1) {
|
|
1473
|
+
perBandCounts.set(v.band, 1);
|
|
1474
|
+
addVertex(v);
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
return picked;
|
|
1478
|
+
}
|
|
1479
|
+
function buildHorizontalCandidates(a, b, mode, wall) {
|
|
1480
|
+
const dx = Math.abs(b[0] - a[0]);
|
|
1481
|
+
const dz = Math.abs(b[2] - a[2]);
|
|
1482
|
+
const y = a[1];
|
|
1483
|
+
if (dx < 0.03 || dz < 0.03) {
|
|
1484
|
+
return [[a, [b[0], y, b[2]]]];
|
|
1485
|
+
}
|
|
1486
|
+
if (mode === "euclidean-cavity") {
|
|
1487
|
+
return [
|
|
1488
|
+
[a, [b[0], y, b[2]]],
|
|
1489
|
+
[a, [b[0], y, a[2]], [b[0], y, b[2]]],
|
|
1490
|
+
[a, [a[0], y, b[2]], [b[0], y, b[2]]]
|
|
1491
|
+
];
|
|
1492
|
+
}
|
|
1493
|
+
const cands = [];
|
|
1494
|
+
if (wall && Math.abs(wall.ux) > 0.05 && Math.abs(wall.uz) > 0.05) {
|
|
1495
|
+
const t = (a[0] - wall.start[0]) * wall.ux + (a[2] - wall.start[1]) * wall.uz;
|
|
1496
|
+
const clampedT = Math.max(0, Math.min(wall.lengthM, t));
|
|
1497
|
+
const px = wall.start[0] + wall.ux * clampedT;
|
|
1498
|
+
const pz = wall.start[1] + wall.uz * clampedT;
|
|
1499
|
+
cands.push([a, [px, y, pz], [b[0], y, b[2]]]);
|
|
1500
|
+
}
|
|
1501
|
+
cands.push([a, [b[0], y, a[2]], [b[0], y, b[2]]]);
|
|
1502
|
+
cands.push([a, [a[0], y, b[2]], [b[0], y, b[2]]]);
|
|
1503
|
+
return cands;
|
|
1504
|
+
}
|
|
1505
|
+
function buildSpliceCandidatePolylines(a, b, role, mode, wall) {
|
|
1506
|
+
const hDist = distXZ2(a, b);
|
|
1507
|
+
const dy = Math.abs(b[1] - a[1]);
|
|
1508
|
+
if (dist32(a, b) < 0.0001)
|
|
1509
|
+
return [[a, b]];
|
|
1510
|
+
if (hDist < 0.03)
|
|
1511
|
+
return [[a, b]];
|
|
1512
|
+
if (dy < 0.03) {
|
|
1513
|
+
return buildHorizontalCandidates(a, b, mode, wall).map((pts) => {
|
|
1514
|
+
const copy = [...pts];
|
|
1515
|
+
copy[copy.length - 1] = b;
|
|
1516
|
+
return copy;
|
|
1517
|
+
});
|
|
1518
|
+
}
|
|
1519
|
+
const candidates = [];
|
|
1520
|
+
const verticalFirst = () => {
|
|
1521
|
+
const mid = [a[0], b[1], a[2]];
|
|
1522
|
+
for (const hLeg of buildHorizontalCandidates(mid, b, mode, wall)) {
|
|
1523
|
+
candidates.push([a, ...hLeg]);
|
|
1524
|
+
}
|
|
1525
|
+
};
|
|
1526
|
+
const horizontalFirst = () => {
|
|
1527
|
+
const endAtAY = [b[0], a[1], b[2]];
|
|
1528
|
+
for (const hLeg of buildHorizontalCandidates(a, endAtAY, mode, wall)) {
|
|
1529
|
+
candidates.push([...hLeg, b]);
|
|
1530
|
+
}
|
|
1531
|
+
};
|
|
1532
|
+
if (role === "enter") {
|
|
1533
|
+
verticalFirst();
|
|
1534
|
+
horizontalFirst();
|
|
1535
|
+
} else {
|
|
1536
|
+
horizontalFirst();
|
|
1537
|
+
verticalFirst();
|
|
1538
|
+
}
|
|
1539
|
+
if (mode === "euclidean-cavity" && (dy < 0.35 || hDist < 0.35)) {
|
|
1540
|
+
candidates.push([a, b]);
|
|
1541
|
+
}
|
|
1542
|
+
return candidates;
|
|
1543
|
+
}
|
|
1544
|
+
function evaluateSpliceBatch(endpoint, role, candidates, wallMap, ctx, endpointExterior, maxBendDegrees) {
|
|
1545
|
+
const results = [];
|
|
1546
|
+
const validMaxBend = typeof maxBendDegrees === "number" && Number.isFinite(maxBendDegrees) ? maxBendDegrees : undefined;
|
|
1547
|
+
for (const v of candidates) {
|
|
1548
|
+
const a = role === "enter" ? endpoint : v.pos;
|
|
1549
|
+
const b = role === "enter" ? v.pos : endpoint;
|
|
1550
|
+
const wall = v.wallId ? wallMap.get(v.wallId) : undefined;
|
|
1551
|
+
const polylines = buildSpliceCandidatePolylines(a, b, role, ctx.mode, wall);
|
|
1552
|
+
const seenKeys = new Set;
|
|
1553
|
+
const ranked = [];
|
|
1554
|
+
for (let pi = 0;pi < polylines.length; pi++) {
|
|
1555
|
+
const compacted = compactPolyline(polylines[pi]);
|
|
1556
|
+
let key = "";
|
|
1557
|
+
for (let qi = 0;qi < compacted.length; qi++) {
|
|
1558
|
+
const p = compacted[qi];
|
|
1559
|
+
if (qi > 0)
|
|
1560
|
+
key += "|";
|
|
1561
|
+
key += `${Math.round(p[0] * 1e4)},${Math.round(p[1] * 1e4)},${Math.round(p[2] * 1e4)}`;
|
|
1562
|
+
}
|
|
1563
|
+
if (seenKeys.has(key))
|
|
1564
|
+
continue;
|
|
1565
|
+
seenKeys.add(key);
|
|
1566
|
+
ranked.push({ pts: compacted, rawLen: polylineLengthM2(compacted) });
|
|
1567
|
+
}
|
|
1568
|
+
ranked.sort((x, y) => x.rawLen - y.rawLen);
|
|
1569
|
+
let bestClean = null;
|
|
1570
|
+
let bestCleanScore = Number.POSITIVE_INFINITY;
|
|
1571
|
+
const bestCleanByDir = new Map;
|
|
1572
|
+
let bestAny = null;
|
|
1573
|
+
let bestAnyScore = Number.POSITIVE_INFINITY;
|
|
1574
|
+
for (let ri = 0;ri < ranked.length; ri++) {
|
|
1575
|
+
const cand = ranked[ri];
|
|
1576
|
+
const ev = evaluatePolyline(cand.pts, ctx, role === "enter" && endpointExterior, role === "exit" && endpointExterior);
|
|
1577
|
+
const baseScore = ev.lengthM + ev.internalBendDeg * BEND_COST_PER_DEG;
|
|
1578
|
+
const bendExcessPen = validMaxBend !== undefined && ev.internalBendDeg > validMaxBend + 0.001 ? 200 * (ev.internalBendDeg - validMaxBend) / 90 : 0;
|
|
1579
|
+
const totalScore = baseScore + bendExcessPen + evalPenalty(ev);
|
|
1580
|
+
const bendOk = validMaxBend === undefined || ev.internalBendDeg <= validMaxBend + 0.001;
|
|
1581
|
+
if (isHardClean(ev) && bendOk) {
|
|
1582
|
+
if (baseScore < bestCleanScore) {
|
|
1583
|
+
bestClean = ev;
|
|
1584
|
+
bestCleanScore = baseScore;
|
|
1585
|
+
}
|
|
1586
|
+
const joinBucket = dirToBucket(role === "enter" ? ev.lastDir : ev.firstDir);
|
|
1587
|
+
const existingDir = bestCleanByDir.get(joinBucket);
|
|
1588
|
+
if (!existingDir || baseScore < existingDir.score) {
|
|
1589
|
+
bestCleanByDir.set(joinBucket, { ev, score: baseScore });
|
|
1590
|
+
}
|
|
1591
|
+
}
|
|
1592
|
+
if (totalScore < bestAnyScore) {
|
|
1593
|
+
bestAny = ev;
|
|
1594
|
+
bestAnyScore = totalScore;
|
|
1595
|
+
}
|
|
1596
|
+
}
|
|
1597
|
+
if (bestAny) {
|
|
1598
|
+
const hardCleanByDir = [...bestCleanByDir.values()].sort((x, y) => x.score - y.score).map((x) => x.ev);
|
|
1599
|
+
results.push({
|
|
1600
|
+
vertexId: v.id,
|
|
1601
|
+
band: v.band,
|
|
1602
|
+
isWallEdge: WALL_BANDS2.has(v.band),
|
|
1603
|
+
hardClean: bestClean,
|
|
1604
|
+
hardCleanByDir,
|
|
1605
|
+
bestFallback: bestAny
|
|
1606
|
+
});
|
|
1607
|
+
}
|
|
1608
|
+
}
|
|
1609
|
+
return results;
|
|
1610
|
+
}
|
|
1611
|
+
function buildSpliceConnections(endpoint, role, graph, allowedBandSet, wallMap, ctx, maxBendDegrees) {
|
|
1612
|
+
const endpointExterior = !ctx.footprint.isPointInside(endpoint[0], endpoint[2]);
|
|
1613
|
+
const localCandidates = selectCandidateSpliceVertices(endpoint, graph, allowedBandSet, false);
|
|
1614
|
+
const localResults = evaluateSpliceBatch(endpoint, role, localCandidates, wallMap, ctx, endpointExterior, maxBendDegrees);
|
|
1615
|
+
if (localResults.some((s) => s.hardClean !== null)) {
|
|
1616
|
+
return localResults;
|
|
1617
|
+
}
|
|
1618
|
+
const seenIds = new Set(localCandidates.map((v) => v.id));
|
|
1619
|
+
const expandedCandidates = selectCandidateSpliceVertices(endpoint, graph, allowedBandSet, true).filter((v) => !seenIds.has(v.id));
|
|
1620
|
+
if (expandedCandidates.length === 0)
|
|
1621
|
+
return localResults;
|
|
1622
|
+
return [
|
|
1623
|
+
...localResults,
|
|
1624
|
+
...evaluateSpliceBatch(endpoint, role, expandedCandidates, wallMap, ctx, endpointExterior, maxBendDegrees)
|
|
1625
|
+
];
|
|
1626
|
+
}
|
|
1627
|
+
function resolveSharedDirectBand(from, to, graph, walls, allowedBandSet, mode) {
|
|
1628
|
+
const hDist = distXZ2(from, to);
|
|
1629
|
+
const dy = Math.abs(to[1] - from[1]);
|
|
1630
|
+
const isOrthogonalCompatible = mode !== "orthogonal" || hDist < 0.03 || dy < 0.03 && (Math.abs(to[0] - from[0]) < 0.03 || Math.abs(to[2] - from[2]) < 0.03);
|
|
1631
|
+
if (allowedBandSet.has("attic") && graph.atticY !== null && Math.abs(from[1] - graph.atticY) <= 0.45 && Math.abs(to[1] - graph.atticY) <= 0.45 && isOrthogonalCompatible) {
|
|
1632
|
+
return { band: "attic", isWall: false };
|
|
1633
|
+
}
|
|
1634
|
+
if (allowedBandSet.has("crawlspace") && graph.subFloorY !== null && Math.abs(from[1] - graph.subFloorY) <= 0.5 && Math.abs(to[1] - graph.subFloorY) <= 0.5 && isOrthogonalCompatible) {
|
|
1635
|
+
return { band: "crawlspace", isWall: false };
|
|
1636
|
+
}
|
|
1637
|
+
if (allowedBandSet.has("under-slab") && graph.subFloorY !== null && Math.abs(from[1] - graph.subFloorY) <= 0.5 && Math.abs(to[1] - graph.subFloorY) <= 0.5 && isOrthogonalCompatible) {
|
|
1638
|
+
return { band: "under-slab", isWall: false };
|
|
1639
|
+
}
|
|
1640
|
+
if (allowedBandSet.has("overhead-plenum") && Math.abs(from[1] - graph.ceilingPlenumY) <= 0.35 && Math.abs(to[1] - graph.ceilingPlenumY) <= 0.35 && isOrthogonalCompatible) {
|
|
1641
|
+
return { band: "overhead-plenum", isWall: false };
|
|
1642
|
+
}
|
|
1643
|
+
if (walls.length > 0) {
|
|
1644
|
+
const pFrom = findNearestWallToPoint2(from[0], from[2], walls);
|
|
1645
|
+
const pTo = findNearestWallToPoint2(to[0], to[2], walls);
|
|
1646
|
+
if (pFrom && pTo && pFrom.wall.id === pTo.wall.id && pFrom.distM <= pFrom.wall.thicknessM / 2 + 0.2 && pTo.distM <= pTo.wall.thicknessM / 2 + 0.2) {
|
|
1647
|
+
const dy = Math.abs(to[1] - from[1]);
|
|
1648
|
+
if (mode !== "orthogonal" || hDist < 0.03 || dy < 0.03) {
|
|
1649
|
+
const avgY = (from[1] + to[1]) * 0.5;
|
|
1650
|
+
const wallH = pFrom.wall.heightM;
|
|
1651
|
+
const inferredBand = avgY >= wallH - 0.35 ? "top-plate" : avgY <= 0.65 ? "under-sill" : "mid-wall";
|
|
1652
|
+
if (allowedBandSet.has(inferredBand)) {
|
|
1653
|
+
return { band: inferredBand, isWall: true };
|
|
1654
|
+
}
|
|
1655
|
+
for (const wb of ["mid-wall", "under-sill", "top-plate"]) {
|
|
1656
|
+
if (allowedBandSet.has(wb))
|
|
1657
|
+
return { band: wb, isWall: true };
|
|
1658
|
+
}
|
|
1659
|
+
}
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1662
|
+
return null;
|
|
1663
|
+
}
|
|
1664
|
+
var MAX_PARETO_LABELS_PER_STATE = 16;
|
|
1665
|
+
function runConstrainedAStar(params) {
|
|
1666
|
+
const {
|
|
1667
|
+
graph,
|
|
1668
|
+
from,
|
|
1669
|
+
to,
|
|
1670
|
+
allowedBandSet,
|
|
1671
|
+
request,
|
|
1672
|
+
startSplices,
|
|
1673
|
+
goalSplicesByVertex,
|
|
1674
|
+
directShortcut,
|
|
1675
|
+
edgeCache,
|
|
1676
|
+
ctx,
|
|
1677
|
+
minWeightScale,
|
|
1678
|
+
allowDegraded
|
|
1679
|
+
} = params;
|
|
1680
|
+
const nVerts = graph.vertices.length;
|
|
1681
|
+
const START_ID = nVerts;
|
|
1682
|
+
const GOAL_ID = nVerts + 1;
|
|
1683
|
+
const preferWall = request.preferWallFollowing === true;
|
|
1684
|
+
const maxBendDeg = typeof request.maxBendDegrees === "number" && Number.isFinite(request.maxBendDegrees) ? request.maxBendDegrees : undefined;
|
|
1685
|
+
const usePareto = maxBendDeg !== undefined;
|
|
1686
|
+
const compiledWithinOpeningEnvelope = typeof graph.compiledHalfXZ === "number" && typeof graph.compiledHalfY === "number" && ctx.clashParams.halfXZ <= graph.compiledHalfXZ + 0.000000001 && ctx.halfY <= graph.compiledHalfY + 0.000000001;
|
|
1687
|
+
const compiledWithinRoofEnvelope = typeof graph.compiledHalfY === "number" && typeof graph.compiledRoofClearanceM === "number" && ctx.halfY <= graph.compiledHalfY + 0.000000001 && ctx.roofClearanceM <= graph.compiledRoofClearanceM + 0.000000001;
|
|
1688
|
+
const wallFactorFor = (isWall) => preferWall ? isWall ? 0.85 : 1.15 : 1;
|
|
1689
|
+
const getEdgeEval = (uId, vId, uPos, vPos, isWallEdge, uBand, vBand) => {
|
|
1690
|
+
const physicsHardOk = checkSegmentPhysicsOk(uPos, vPos, ctx);
|
|
1691
|
+
if (!allowDegraded && !physicsHardOk) {
|
|
1692
|
+
return { dir: null, penalty: PHYSICS_PENALTY, hardClean: false };
|
|
1693
|
+
}
|
|
1694
|
+
const cacheKey = uId < vId ? uId * nVerts + vId : vId * nVerts + uId;
|
|
1695
|
+
let uEval = edgeCache.get(cacheKey);
|
|
1696
|
+
if (!uEval || allowDegraded && !uEval.fullEvaluated) {
|
|
1697
|
+
const hits = ctx.occupancy.segmentClashes(uPos, vPos, ctx.clashParams);
|
|
1698
|
+
const clashSet = new Set;
|
|
1699
|
+
for (let i = 0;i < hits.length; i++) {
|
|
1700
|
+
const obs = hits[i];
|
|
1701
|
+
clashSet.add(obs.nodeId);
|
|
1702
|
+
ctx.encounteredObstacleIds.add(obs.nodeId);
|
|
1703
|
+
}
|
|
1704
|
+
if (!allowDegraded && clashSet.size > 0) {
|
|
1705
|
+
uEval = {
|
|
1706
|
+
clashNodeIds: [...clashSet],
|
|
1707
|
+
openingHit: false,
|
|
1708
|
+
roofOk: true,
|
|
1709
|
+
footprintOk: true,
|
|
1710
|
+
undirectedPenalty: clashSet.size * CLASH_PENALTY,
|
|
1711
|
+
undirectedHardClean: false,
|
|
1712
|
+
fullEvaluated: false
|
|
1713
|
+
};
|
|
1714
|
+
edgeCache.set(cacheKey, uEval);
|
|
1715
|
+
} else {
|
|
1716
|
+
const isSubFloorEdge = uBand === "under-slab" || uBand === "crawlspace" || vBand === "under-slab" || vBand === "crawlspace";
|
|
1717
|
+
const skipOpeningCheck = compiledWithinOpeningEnvelope && isWallEdge;
|
|
1718
|
+
const skipRoofCheck = compiledWithinRoofEnvelope && !isSubFloorEdge;
|
|
1719
|
+
let openingHit = false;
|
|
1720
|
+
let roofOk = true;
|
|
1721
|
+
let footprintOk = true;
|
|
1722
|
+
let fullEvaluated = true;
|
|
1723
|
+
if (!skipOpeningCheck) {
|
|
1724
|
+
openingHit = segmentHitsAnyOpening(uPos, vPos, ctx.openingPrisms);
|
|
1725
|
+
}
|
|
1726
|
+
if (!allowDegraded && openingHit) {
|
|
1727
|
+
fullEvaluated = false;
|
|
1728
|
+
} else {
|
|
1729
|
+
if (!skipRoofCheck) {
|
|
1730
|
+
roofOk = ctx.roofShell.isPathClearOfRoof([uPos, vPos], ctx.halfY, ctx.roofClearanceM);
|
|
1731
|
+
}
|
|
1732
|
+
if (!allowDegraded && !roofOk) {
|
|
1733
|
+
fullEvaluated = false;
|
|
1734
|
+
} else {
|
|
1735
|
+
footprintOk = ctx.footprint.isSegmentInside(uPos, vPos);
|
|
1736
|
+
}
|
|
1737
|
+
}
|
|
1738
|
+
let undirectedPenalty = clashSet.size * CLASH_PENALTY;
|
|
1739
|
+
if (openingHit)
|
|
1740
|
+
undirectedPenalty += OPENING_PENALTY;
|
|
1741
|
+
if (!roofOk)
|
|
1742
|
+
undirectedPenalty += ROOF_PENALTY;
|
|
1743
|
+
if (!footprintOk)
|
|
1744
|
+
undirectedPenalty += OUTDOOR_PENALTY;
|
|
1745
|
+
uEval = {
|
|
1746
|
+
clashNodeIds: [...clashSet],
|
|
1747
|
+
openingHit,
|
|
1748
|
+
roofOk,
|
|
1749
|
+
footprintOk,
|
|
1750
|
+
undirectedPenalty,
|
|
1751
|
+
undirectedHardClean: clashSet.size === 0 && !openingHit && roofOk && footprintOk,
|
|
1752
|
+
fullEvaluated
|
|
1753
|
+
};
|
|
1754
|
+
edgeCache.set(cacheKey, uEval);
|
|
1755
|
+
}
|
|
1756
|
+
}
|
|
1757
|
+
const hardClean = uEval.undirectedHardClean && physicsHardOk;
|
|
1758
|
+
if (!allowDegraded && !hardClean) {
|
|
1759
|
+
return { dir: null, penalty: uEval.undirectedPenalty, hardClean: false };
|
|
1760
|
+
}
|
|
1761
|
+
return {
|
|
1762
|
+
dir: unitDir(uPos, vPos),
|
|
1763
|
+
penalty: uEval.undirectedPenalty + (physicsHardOk ? 0 : PHYSICS_PENALTY),
|
|
1764
|
+
hardClean
|
|
1765
|
+
};
|
|
1766
|
+
};
|
|
1767
|
+
const heap = new BinaryMinHeap;
|
|
1768
|
+
const bestG = new Map;
|
|
1769
|
+
const paretoLabels = new Map;
|
|
1770
|
+
const parentState = new Map;
|
|
1771
|
+
const tryAcceptLabel = (stateKey, gNext, nextBend, entrySeq) => {
|
|
1772
|
+
if (!usePareto) {
|
|
1773
|
+
const prevBest = bestG.get(stateKey);
|
|
1774
|
+
if (prevBest !== undefined && gNext >= prevBest - 0.000000001)
|
|
1775
|
+
return false;
|
|
1776
|
+
bestG.set(stateKey, gNext);
|
|
1777
|
+
return true;
|
|
1778
|
+
}
|
|
1779
|
+
let labels = paretoLabels.get(stateKey);
|
|
1780
|
+
if (!labels) {
|
|
1781
|
+
paretoLabels.set(stateKey, [{ g: gNext, bendDeg: nextBend, seq: entrySeq }]);
|
|
1782
|
+
return true;
|
|
1783
|
+
}
|
|
1784
|
+
for (let i = 0;i < labels.length; i++) {
|
|
1785
|
+
const L = labels[i];
|
|
1786
|
+
if (L.g <= gNext + 0.000000001 && L.bendDeg <= nextBend + 0.001) {
|
|
1787
|
+
return false;
|
|
1788
|
+
}
|
|
1789
|
+
}
|
|
1790
|
+
const filtered = [];
|
|
1791
|
+
for (let i = 0;i < labels.length; i++) {
|
|
1792
|
+
const L = labels[i];
|
|
1793
|
+
if (gNext <= L.g + 0.000000001 && nextBend <= L.bendDeg + 0.001)
|
|
1794
|
+
continue;
|
|
1795
|
+
filtered.push(L);
|
|
1796
|
+
}
|
|
1797
|
+
let insertIdx = filtered.length;
|
|
1798
|
+
for (let i = 0;i < filtered.length; i++) {
|
|
1799
|
+
if (gNext < filtered[i].g) {
|
|
1800
|
+
insertIdx = i;
|
|
1801
|
+
break;
|
|
1802
|
+
}
|
|
1803
|
+
}
|
|
1804
|
+
const candidate = { g: gNext, bendDeg: nextBend, seq: entrySeq };
|
|
1805
|
+
filtered.splice(insertIdx, 0, candidate);
|
|
1806
|
+
if (filtered.length > MAX_PARETO_LABELS_PER_STATE) {
|
|
1807
|
+
const isExtreme = insertIdx === 0 || insertIdx === filtered.length - 1;
|
|
1808
|
+
if (!isExtreme) {
|
|
1809
|
+
filtered.splice(insertIdx, 1);
|
|
1810
|
+
paretoLabels.set(stateKey, filtered);
|
|
1811
|
+
return false;
|
|
1812
|
+
}
|
|
1813
|
+
filtered.splice(filtered.length - 2, 1);
|
|
1814
|
+
}
|
|
1815
|
+
paretoLabels.set(stateKey, filtered);
|
|
1816
|
+
return true;
|
|
1817
|
+
};
|
|
1818
|
+
let nextSeq = 1;
|
|
1819
|
+
const startSeq = 0;
|
|
1820
|
+
const startKey = START_ID * DIR_BUCKETS;
|
|
1821
|
+
tryAcceptLabel(startKey, 0, 0, startSeq);
|
|
1822
|
+
heap.push({
|
|
1823
|
+
f: dist32(from, to) * minWeightScale,
|
|
1824
|
+
g: 0,
|
|
1825
|
+
seq: startSeq,
|
|
1826
|
+
nodeId: START_ID,
|
|
1827
|
+
dirBucket: 0,
|
|
1828
|
+
dir: null,
|
|
1829
|
+
bendDeg: 0,
|
|
1830
|
+
stateKey: startKey
|
|
1831
|
+
});
|
|
1832
|
+
let winningGoalSeq = null;
|
|
1833
|
+
while (heap.size > 0) {
|
|
1834
|
+
const cur = heap.pop();
|
|
1835
|
+
if (!usePareto) {
|
|
1836
|
+
const recordedBest = bestG.get(cur.stateKey);
|
|
1837
|
+
if (recordedBest !== undefined && cur.g > recordedBest + 0.000000001)
|
|
1838
|
+
continue;
|
|
1839
|
+
} else {
|
|
1840
|
+
const labels = paretoLabels.get(cur.stateKey);
|
|
1841
|
+
let active = false;
|
|
1842
|
+
if (labels) {
|
|
1843
|
+
for (let i = 0;i < labels.length; i++) {
|
|
1844
|
+
if (labels[i].seq === cur.seq) {
|
|
1845
|
+
active = true;
|
|
1846
|
+
break;
|
|
1847
|
+
}
|
|
1848
|
+
}
|
|
1849
|
+
}
|
|
1850
|
+
if (!active)
|
|
1851
|
+
continue;
|
|
1852
|
+
}
|
|
1853
|
+
if (cur.nodeId === GOAL_ID) {
|
|
1854
|
+
winningGoalSeq = cur.seq;
|
|
1855
|
+
break;
|
|
1856
|
+
}
|
|
1857
|
+
if (cur.nodeId === START_ID) {
|
|
1858
|
+
if (directShortcut && (allowDegraded || isHardClean(directShortcut.eval))) {
|
|
1859
|
+
const ev = directShortcut.eval;
|
|
1860
|
+
const bend = ev.internalBendDeg;
|
|
1861
|
+
if (maxBendDeg === undefined || bend <= maxBendDeg + 0.001 || allowDegraded) {
|
|
1862
|
+
const bendExcessPen = maxBendDeg !== undefined && bend > maxBendDeg + 0.001 ? 200 * (bend - maxBendDeg) / 90 : 0;
|
|
1863
|
+
const penalty = allowDegraded ? evalPenalty(ev) : 0;
|
|
1864
|
+
const gNext = ev.lengthM * wallFactorFor(directShortcut.isWall) + bend * BEND_COST_PER_DEG + bendExcessPen + penalty;
|
|
1865
|
+
const bucket = dirToBucket(ev.lastDir);
|
|
1866
|
+
const nextKey = GOAL_ID * DIR_BUCKETS + bucket;
|
|
1867
|
+
const entrySeq = nextSeq++;
|
|
1868
|
+
if (tryAcceptLabel(nextKey, gNext, bend, entrySeq)) {
|
|
1869
|
+
parentState.set(entrySeq, {
|
|
1870
|
+
prevSeq: cur.seq,
|
|
1871
|
+
prevNode: START_ID,
|
|
1872
|
+
points: ev.points,
|
|
1873
|
+
band: directShortcut.band
|
|
1874
|
+
});
|
|
1875
|
+
heap.push({
|
|
1876
|
+
f: gNext,
|
|
1877
|
+
g: gNext,
|
|
1878
|
+
seq: entrySeq,
|
|
1879
|
+
nodeId: GOAL_ID,
|
|
1880
|
+
dirBucket: bucket,
|
|
1881
|
+
dir: ev.lastDir,
|
|
1882
|
+
bendDeg: bend,
|
|
1883
|
+
stateKey: nextKey
|
|
1884
|
+
});
|
|
1885
|
+
}
|
|
1886
|
+
}
|
|
1887
|
+
}
|
|
1888
|
+
for (const splice of startSplices) {
|
|
1889
|
+
const spliceVariants = splice.hardCleanByDir.length > 0 ? splice.hardCleanByDir : allowDegraded ? [splice.bestFallback] : [];
|
|
1890
|
+
for (let si = 0;si < spliceVariants.length; si++) {
|
|
1891
|
+
const ev = spliceVariants[si];
|
|
1892
|
+
const bend = ev.internalBendDeg;
|
|
1893
|
+
if (!allowDegraded && maxBendDeg !== undefined && bend > maxBendDeg + 0.001)
|
|
1894
|
+
continue;
|
|
1895
|
+
const bendExcessPen = maxBendDeg !== undefined && bend > maxBendDeg + 0.001 ? 200 * (bend - maxBendDeg) / 90 : 0;
|
|
1896
|
+
const penalty = allowDegraded ? evalPenalty(ev) : 0;
|
|
1897
|
+
const gNext = ev.lengthM * wallFactorFor(splice.isWallEdge) + bend * BEND_COST_PER_DEG + bendExcessPen + penalty;
|
|
1898
|
+
const bucket = dirToBucket(ev.lastDir);
|
|
1899
|
+
const nextKey = splice.vertexId * DIR_BUCKETS + bucket;
|
|
1900
|
+
const entrySeq = nextSeq++;
|
|
1901
|
+
if (tryAcceptLabel(nextKey, gNext, bend, entrySeq)) {
|
|
1902
|
+
const v = graph.vertices[splice.vertexId];
|
|
1903
|
+
parentState.set(entrySeq, {
|
|
1904
|
+
prevSeq: cur.seq,
|
|
1905
|
+
prevNode: START_ID,
|
|
1906
|
+
points: ev.points,
|
|
1907
|
+
band: splice.band,
|
|
1908
|
+
wallId: v.wallId
|
|
1909
|
+
});
|
|
1910
|
+
const h = dist32(v.pos, to) * minWeightScale;
|
|
1911
|
+
heap.push({
|
|
1912
|
+
f: gNext + h,
|
|
1913
|
+
g: gNext,
|
|
1914
|
+
seq: entrySeq,
|
|
1915
|
+
nodeId: splice.vertexId,
|
|
1916
|
+
dirBucket: bucket,
|
|
1917
|
+
dir: ev.lastDir,
|
|
1918
|
+
bendDeg: bend,
|
|
1919
|
+
stateKey: nextKey
|
|
1920
|
+
});
|
|
1921
|
+
}
|
|
1922
|
+
}
|
|
1923
|
+
}
|
|
1924
|
+
continue;
|
|
1925
|
+
}
|
|
1926
|
+
const uVertex = graph.vertices[cur.nodeId];
|
|
1927
|
+
const goalSplice = goalSplicesByVertex.get(cur.nodeId);
|
|
1928
|
+
if (goalSplice) {
|
|
1929
|
+
const goalVariants = goalSplice.hardCleanByDir.length > 0 ? goalSplice.hardCleanByDir : allowDegraded ? [goalSplice.bestFallback] : [];
|
|
1930
|
+
for (let gi = 0;gi < goalVariants.length; gi++) {
|
|
1931
|
+
const ev = goalVariants[gi];
|
|
1932
|
+
const joinTurn = turnAngleDeg(cur.dir, ev.firstDir);
|
|
1933
|
+
const nextBend = cur.bendDeg + joinTurn + ev.internalBendDeg;
|
|
1934
|
+
if (allowDegraded || maxBendDeg === undefined || nextBend <= maxBendDeg + 0.001) {
|
|
1935
|
+
const incBendExcess = maxBendDeg !== undefined && nextBend > maxBendDeg + 0.001 ? Math.max(0, nextBend - Math.max(maxBendDeg, cur.bendDeg)) : 0;
|
|
1936
|
+
const bendExcessPen = incBendExcess > 0 ? 200 * incBendExcess / 90 : 0;
|
|
1937
|
+
const penalty = allowDegraded ? evalPenalty(ev) : 0;
|
|
1938
|
+
const gNext = cur.g + ev.lengthM * wallFactorFor(goalSplice.isWallEdge) + (joinTurn + ev.internalBendDeg) * BEND_COST_PER_DEG + bendExcessPen + penalty;
|
|
1939
|
+
const bucket = dirToBucket(ev.lastDir ?? cur.dir);
|
|
1940
|
+
const nextKey = GOAL_ID * DIR_BUCKETS + bucket;
|
|
1941
|
+
const entrySeq = nextSeq++;
|
|
1942
|
+
if (tryAcceptLabel(nextKey, gNext, nextBend, entrySeq)) {
|
|
1943
|
+
parentState.set(entrySeq, {
|
|
1944
|
+
prevSeq: cur.seq,
|
|
1945
|
+
prevNode: cur.nodeId,
|
|
1946
|
+
points: ev.points,
|
|
1947
|
+
band: goalSplice.band,
|
|
1948
|
+
wallId: uVertex.wallId
|
|
1949
|
+
});
|
|
1950
|
+
heap.push({
|
|
1951
|
+
f: gNext,
|
|
1952
|
+
g: gNext,
|
|
1953
|
+
seq: entrySeq,
|
|
1954
|
+
nodeId: GOAL_ID,
|
|
1955
|
+
dirBucket: bucket,
|
|
1956
|
+
dir: ev.lastDir ?? cur.dir,
|
|
1957
|
+
bendDeg: nextBend,
|
|
1958
|
+
stateKey: nextKey
|
|
1959
|
+
});
|
|
1960
|
+
}
|
|
1961
|
+
}
|
|
1962
|
+
}
|
|
1963
|
+
}
|
|
1964
|
+
const edges = graph.adjacency[cur.nodeId] ?? [];
|
|
1965
|
+
for (let ei = 0;ei < edges.length; ei++) {
|
|
1966
|
+
const edge = edges[ei];
|
|
1967
|
+
if (!allowedBandSet.has(edge.band))
|
|
1968
|
+
continue;
|
|
1969
|
+
const vVertex = graph.vertices[edge.to];
|
|
1970
|
+
if (!vVertex || !allowedBandSet.has(vVertex.band))
|
|
1971
|
+
continue;
|
|
1972
|
+
const ev = getEdgeEval(cur.nodeId, edge.to, uVertex.pos, vVertex.pos, edge.isWallEdge, uVertex.band, vVertex.band);
|
|
1973
|
+
if (!allowDegraded && !ev.hardClean)
|
|
1974
|
+
continue;
|
|
1975
|
+
const turn = turnAngleDeg(cur.dir, ev.dir);
|
|
1976
|
+
const nextBend = cur.bendDeg + turn;
|
|
1977
|
+
if (!allowDegraded && maxBendDeg !== undefined && nextBend > maxBendDeg + 0.001)
|
|
1978
|
+
continue;
|
|
1979
|
+
const incBendExcess = maxBendDeg !== undefined && nextBend > maxBendDeg + 0.001 ? Math.max(0, nextBend - Math.max(maxBendDeg, cur.bendDeg)) : 0;
|
|
1980
|
+
const bendExcessPen = incBendExcess > 0 ? 200 * incBendExcess / 90 : 0;
|
|
1981
|
+
const baseW = Number.isFinite(edge.baseCostWeight) && edge.baseCostWeight > 0 ? edge.baseCostWeight : 1;
|
|
1982
|
+
const stepLen = Number.isFinite(edge.lengthM) && edge.lengthM >= 0 ? edge.lengthM : dist32(uVertex.pos, vVertex.pos);
|
|
1983
|
+
const gNext = cur.g + stepLen * baseW * wallFactorFor(edge.isWallEdge) + turn * BEND_COST_PER_DEG + bendExcessPen + (allowDegraded ? ev.penalty : 0);
|
|
1984
|
+
const nextDir = ev.dir ?? cur.dir;
|
|
1985
|
+
const bucket = dirToBucket(nextDir);
|
|
1986
|
+
const nextKey = edge.to * DIR_BUCKETS + bucket;
|
|
1987
|
+
const entrySeq = nextSeq++;
|
|
1988
|
+
if (tryAcceptLabel(nextKey, gNext, nextBend, entrySeq)) {
|
|
1989
|
+
parentState.set(entrySeq, {
|
|
1990
|
+
prevSeq: cur.seq,
|
|
1991
|
+
prevNode: cur.nodeId,
|
|
1992
|
+
points: [uVertex.pos, vVertex.pos],
|
|
1993
|
+
band: edge.band,
|
|
1994
|
+
wallId: edge.wallId ?? vVertex.wallId
|
|
1995
|
+
});
|
|
1996
|
+
const h = dist32(vVertex.pos, to) * minWeightScale;
|
|
1997
|
+
heap.push({
|
|
1998
|
+
f: gNext + h,
|
|
1999
|
+
g: gNext,
|
|
2000
|
+
seq: entrySeq,
|
|
2001
|
+
nodeId: edge.to,
|
|
2002
|
+
dirBucket: bucket,
|
|
2003
|
+
dir: nextDir,
|
|
2004
|
+
bendDeg: nextBend,
|
|
2005
|
+
stateKey: nextKey
|
|
2006
|
+
});
|
|
2007
|
+
}
|
|
2008
|
+
}
|
|
2009
|
+
}
|
|
2010
|
+
if (winningGoalSeq === null)
|
|
2011
|
+
return null;
|
|
2012
|
+
const segments = [];
|
|
2013
|
+
let cursorSeq = winningGoalSeq;
|
|
2014
|
+
while (cursorSeq !== startSeq) {
|
|
2015
|
+
const step = parentState.get(cursorSeq);
|
|
2016
|
+
if (!step)
|
|
2017
|
+
break;
|
|
2018
|
+
segments.push({ points: step.points, band: step.band, wallId: step.wallId });
|
|
2019
|
+
cursorSeq = step.prevSeq;
|
|
2020
|
+
}
|
|
2021
|
+
segments.reverse();
|
|
2022
|
+
const waypoints = [];
|
|
2023
|
+
const vertexBands = [];
|
|
2024
|
+
const wallIdsByPoint = [];
|
|
2025
|
+
const bandsUsedSet = new Set;
|
|
2026
|
+
for (const seg of segments) {
|
|
2027
|
+
if (seg.band && polylineLengthM2(seg.points) > 0.0001)
|
|
2028
|
+
bandsUsedSet.add(seg.band);
|
|
2029
|
+
for (let i = 0;i < seg.points.length; i++) {
|
|
2030
|
+
const pt = seg.points[i];
|
|
2031
|
+
if (waypoints.length === 0 || dist32(waypoints[waypoints.length - 1], pt) > 0.0001) {
|
|
2032
|
+
waypoints.push(pt);
|
|
2033
|
+
vertexBands.push(seg.band);
|
|
2034
|
+
wallIdsByPoint.push(seg.wallId);
|
|
2035
|
+
} else if (seg.band && vertexBands[vertexBands.length - 1] === null) {
|
|
2036
|
+
vertexBands[vertexBands.length - 1] = seg.band;
|
|
2037
|
+
}
|
|
2038
|
+
}
|
|
2039
|
+
}
|
|
2040
|
+
if (waypoints.length < 2) {
|
|
2041
|
+
return {
|
|
2042
|
+
waypoints: [from, to],
|
|
2043
|
+
vertexBands: [segments[0]?.band ?? null, segments[0]?.band ?? null],
|
|
2044
|
+
bandsUsed: [...bandsUsedSet],
|
|
2045
|
+
wallIdsByPoint: [undefined, undefined]
|
|
2046
|
+
};
|
|
2047
|
+
}
|
|
2048
|
+
return {
|
|
2049
|
+
waypoints,
|
|
2050
|
+
vertexBands,
|
|
2051
|
+
bandsUsed: [...bandsUsedSet],
|
|
2052
|
+
wallIdsByPoint
|
|
2053
|
+
};
|
|
2054
|
+
}
|
|
2055
|
+
function applyLateralStagger(rawPoints, wallIdsByPoint, staggerM, wallMap, ctx, maxBendDegrees) {
|
|
2056
|
+
if (!Number.isFinite(staggerM) || Math.abs(staggerM) < 0.0001 || rawPoints.length < 2) {
|
|
2057
|
+
return rawPoints;
|
|
2058
|
+
}
|
|
2059
|
+
const firstPt = rawPoints[0];
|
|
2060
|
+
const lastPt = rawPoints[rawPoints.length - 1];
|
|
2061
|
+
const allowStartExteriorMargin = !ctx.footprint.isPointInside(firstPt[0], firstPt[2]);
|
|
2062
|
+
const allowEndExteriorMargin = !ctx.footprint.isPointInside(lastPt[0], lastPt[2]);
|
|
2063
|
+
const segCount = rawPoints.length - 1;
|
|
2064
|
+
const segWalls = new Array(segCount).fill(undefined);
|
|
2065
|
+
let hasWallSeg = false;
|
|
2066
|
+
for (let k = 0;k < segCount; k++) {
|
|
2067
|
+
const a = rawPoints[k];
|
|
2068
|
+
const b = rawPoints[k + 1];
|
|
2069
|
+
if (Math.abs(b[1] - a[1]) > 0.001)
|
|
2070
|
+
continue;
|
|
2071
|
+
const hDist = distXZ2(a, b);
|
|
2072
|
+
if (hDist <= 0.001)
|
|
2073
|
+
continue;
|
|
2074
|
+
const ux = (b[0] - a[0]) / hDist;
|
|
2075
|
+
const uz = (b[2] - a[2]) / hDist;
|
|
2076
|
+
const mx = (a[0] + b[0]) * 0.5;
|
|
2077
|
+
const mz = (a[2] + b[2]) * 0.5;
|
|
2078
|
+
const candIds = [wallIdsByPoint[k], wallIdsByPoint[k + 1]];
|
|
2079
|
+
for (let ci = 0;ci < candIds.length; ci++) {
|
|
2080
|
+
const wid = candIds[ci];
|
|
2081
|
+
if (!wid)
|
|
2082
|
+
continue;
|
|
2083
|
+
const w = wallMap.get(wid);
|
|
2084
|
+
if (!w)
|
|
2085
|
+
continue;
|
|
2086
|
+
const parallelDot = Math.abs(ux * w.ux + uz * w.uz);
|
|
2087
|
+
if (parallelDot < 0.98)
|
|
2088
|
+
continue;
|
|
2089
|
+
const projT = (mx - w.start[0]) * w.ux + (mz - w.start[1]) * w.uz;
|
|
2090
|
+
const clampedT = Math.max(0, Math.min(w.lengthM, projT));
|
|
2091
|
+
const px = w.start[0] + w.ux * clampedT;
|
|
2092
|
+
const pz = w.start[1] + w.uz * clampedT;
|
|
2093
|
+
if (Math.hypot(mx - px, mz - pz) <= w.thicknessM / 2 + 0.25) {
|
|
2094
|
+
segWalls[k] = w;
|
|
2095
|
+
hasWallSeg = true;
|
|
2096
|
+
break;
|
|
2097
|
+
}
|
|
2098
|
+
}
|
|
2099
|
+
}
|
|
2100
|
+
if (!hasWallSeg)
|
|
2101
|
+
return rawPoints;
|
|
2102
|
+
const wallNormalSign = new Map;
|
|
2103
|
+
const strictInteriorOpts = { precedence: "room-first", wallToleranceM: 0 };
|
|
2104
|
+
for (let k = 0;k < segCount; k++) {
|
|
2105
|
+
const w = segWalls[k];
|
|
2106
|
+
if (!w || wallNormalSign.has(w.id))
|
|
2107
|
+
continue;
|
|
2108
|
+
const midX = (w.start[0] + w.end[0]) * 0.5;
|
|
2109
|
+
const midZ = (w.start[1] + w.end[1]) * 0.5;
|
|
2110
|
+
const probeD = Math.max(0.15, w.thicknessM / 2 + 0.1);
|
|
2111
|
+
const posIn = ctx.footprint.isPointInside(midX + w.nx * probeD, midZ + w.nz * probeD, strictInteriorOpts);
|
|
2112
|
+
const negIn = ctx.footprint.isPointInside(midX - w.nx * probeD, midZ - w.nz * probeD, strictInteriorOpts);
|
|
2113
|
+
wallNormalSign.set(w.id, negIn && !posIn ? -1 : 1);
|
|
2114
|
+
}
|
|
2115
|
+
const shiftedPt = (pt, w, sign) => {
|
|
2116
|
+
const off = staggerM * sign * (wallNormalSign.get(w.id) ?? 1);
|
|
2117
|
+
return [pt[0] + w.nx * off, pt[1], pt[2] + w.nz * off];
|
|
2118
|
+
};
|
|
2119
|
+
const baseEval = evaluatePolyline(rawPoints, ctx, allowStartExteriorMargin, allowEndExteriorMargin);
|
|
2120
|
+
for (const sign of [1, -1]) {
|
|
2121
|
+
const out = [firstPt];
|
|
2122
|
+
const firstWall = segWalls[0];
|
|
2123
|
+
if (firstWall) {
|
|
2124
|
+
out.push(shiftedPt(firstPt, firstWall, sign));
|
|
2125
|
+
}
|
|
2126
|
+
for (let k = 1;k < segCount; k++) {
|
|
2127
|
+
const pt = rawPoints[k];
|
|
2128
|
+
const wPrev = segWalls[k - 1];
|
|
2129
|
+
const wCur = segWalls[k];
|
|
2130
|
+
if (!wPrev && !wCur) {
|
|
2131
|
+
out.push(pt);
|
|
2132
|
+
} else if (!wPrev && wCur) {
|
|
2133
|
+
out.push(pt);
|
|
2134
|
+
out.push(shiftedPt(pt, wCur, sign));
|
|
2135
|
+
} else if (wPrev && !wCur) {
|
|
2136
|
+
out.push(shiftedPt(pt, wPrev, sign));
|
|
2137
|
+
out.push(pt);
|
|
2138
|
+
} else if (wPrev && wCur) {
|
|
2139
|
+
if (wPrev.id === wCur.id) {
|
|
2140
|
+
out.push(shiftedPt(pt, wCur, sign));
|
|
2141
|
+
} else {
|
|
2142
|
+
const sPrev = shiftedPt(pt, wPrev, sign);
|
|
2143
|
+
const sCur = shiftedPt(pt, wCur, sign);
|
|
2144
|
+
const denom = wPrev.ux * wCur.uz - wPrev.uz * wCur.ux;
|
|
2145
|
+
let mitered = false;
|
|
2146
|
+
if (Math.abs(denom) > 0.001) {
|
|
2147
|
+
const t = ((sCur[0] - sPrev[0]) * wCur.uz - (sCur[2] - sPrev[2]) * wCur.ux) / denom;
|
|
2148
|
+
const ix = sPrev[0] + wPrev.ux * t;
|
|
2149
|
+
const iz = sPrev[2] + wPrev.uz * t;
|
|
2150
|
+
if (Math.hypot(ix - pt[0], iz - pt[2]) <= Math.abs(staggerM) * 4 + 0.05) {
|
|
2151
|
+
out.push([ix, pt[1], iz]);
|
|
2152
|
+
mitered = true;
|
|
2153
|
+
}
|
|
2154
|
+
}
|
|
2155
|
+
if (!mitered) {
|
|
2156
|
+
if (distXZ2(sPrev, sCur) < 0.0001) {
|
|
2157
|
+
out.push(sCur);
|
|
2158
|
+
} else {
|
|
2159
|
+
out.push(sPrev);
|
|
2160
|
+
out.push(pt);
|
|
2161
|
+
out.push(sCur);
|
|
2162
|
+
}
|
|
2163
|
+
}
|
|
2164
|
+
}
|
|
2165
|
+
}
|
|
2166
|
+
}
|
|
2167
|
+
const lastWall = segWalls[segCount - 1];
|
|
2168
|
+
if (lastWall) {
|
|
2169
|
+
out.push(shiftedPt(lastPt, lastWall, sign));
|
|
2170
|
+
}
|
|
2171
|
+
out.push(lastPt);
|
|
2172
|
+
const compacted = compactPolyline(out);
|
|
2173
|
+
if (maxBendDegrees !== undefined && Number.isFinite(maxBendDegrees) && countPathBendDegrees2(compacted) > maxBendDegrees + 0.01) {
|
|
2174
|
+
continue;
|
|
2175
|
+
}
|
|
2176
|
+
const testEval = evaluatePolyline(compacted, ctx, allowStartExteriorMargin, allowEndExteriorMargin);
|
|
2177
|
+
if (testEval.clashNodeIds.length <= baseEval.clashNodeIds.length && testEval.openingHits <= baseEval.openingHits && (!baseEval.roofOk || testEval.roofOk) && (!baseEval.footprintOk || testEval.footprintOk) && (!baseEval.physicsHardOk || testEval.physicsHardOk)) {
|
|
2178
|
+
return compacted;
|
|
2179
|
+
}
|
|
2180
|
+
}
|
|
2181
|
+
return rawPoints;
|
|
2182
|
+
}
|
|
2183
|
+
function shapeEuclideanCavity(waypoints, vertexBands, ctx, maxBendDegrees, startExterior = false, endExterior = false) {
|
|
2184
|
+
const compactedBase = compactPolyline(waypoints);
|
|
2185
|
+
if (waypoints.length <= 2)
|
|
2186
|
+
return compactedBase;
|
|
2187
|
+
const canShortcut = (a, b, allowStartSkip, allowEndSkip) => {
|
|
2188
|
+
if (ctx.occupancy.segmentClashes(a, b, ctx.clashParams).length > 0)
|
|
2189
|
+
return false;
|
|
2190
|
+
if (segmentHitsAnyOpening(a, b, ctx.openingPrisms))
|
|
2191
|
+
return false;
|
|
2192
|
+
if (!ctx.roofShell.isPathClearOfRoof([a, b], ctx.halfY, ctx.roofClearanceM))
|
|
2193
|
+
return false;
|
|
2194
|
+
const fpOpts = allowStartSkip || allowEndSkip ? {
|
|
2195
|
+
skipExteriorEndpointMarginM: 0.35,
|
|
2196
|
+
allowStartExteriorSkip: allowStartSkip,
|
|
2197
|
+
allowEndExteriorSkip: allowEndSkip
|
|
2198
|
+
} : undefined;
|
|
2199
|
+
if (!ctx.footprint.isSegmentInside(a, b, fpOpts))
|
|
2200
|
+
return false;
|
|
2201
|
+
if (ctx.minHorizY !== undefined && distXZ2(a, b) > 0.01) {
|
|
2202
|
+
if (a[1] < ctx.minHorizY - 0.0001 || b[1] < ctx.minHorizY - 0.0001)
|
|
2203
|
+
return false;
|
|
2204
|
+
}
|
|
2205
|
+
return true;
|
|
2206
|
+
};
|
|
2207
|
+
const isOpenPoint = (idx) => {
|
|
2208
|
+
const b = vertexBands[idx];
|
|
2209
|
+
return b !== null && b !== undefined && OPEN_CAVITY_BANDS.has(b);
|
|
2210
|
+
};
|
|
2211
|
+
const result = [waypoints[0]];
|
|
2212
|
+
let i = 0;
|
|
2213
|
+
while (i < waypoints.length - 1) {
|
|
2214
|
+
let bestJ = i + 1;
|
|
2215
|
+
if (isOpenPoint(i)) {
|
|
2216
|
+
for (let j = waypoints.length - 1;j > i + 1; j--) {
|
|
2217
|
+
if (!isOpenPoint(j))
|
|
2218
|
+
continue;
|
|
2219
|
+
let spanValid = true;
|
|
2220
|
+
for (let k = i;k <= j; k++) {
|
|
2221
|
+
if (!isOpenPoint(k) || Math.abs(waypoints[k][1] - waypoints[i][1]) > 0.5) {
|
|
2222
|
+
spanValid = false;
|
|
2223
|
+
break;
|
|
2224
|
+
}
|
|
2225
|
+
}
|
|
2226
|
+
if (!spanValid)
|
|
2227
|
+
continue;
|
|
2228
|
+
const segStartSkip = i === 0 && startExterior;
|
|
2229
|
+
const segEndSkip = j === waypoints.length - 1 && endExterior;
|
|
2230
|
+
if (canShortcut(waypoints[i], waypoints[j], segStartSkip, segEndSkip)) {
|
|
2231
|
+
bestJ = j;
|
|
2232
|
+
break;
|
|
2233
|
+
}
|
|
2234
|
+
}
|
|
2235
|
+
}
|
|
2236
|
+
result.push(waypoints[bestJ]);
|
|
2237
|
+
i = bestJ;
|
|
2238
|
+
}
|
|
2239
|
+
const compacted = compactPolyline(result);
|
|
2240
|
+
if (typeof maxBendDegrees === "number" && Number.isFinite(maxBendDegrees) && countPathBendDegrees2(compacted) > maxBendDegrees + 0.01 && countPathBendDegrees2(compactedBase) <= maxBendDegrees + 0.01) {
|
|
2241
|
+
return compactedBase;
|
|
2242
|
+
}
|
|
2243
|
+
return compacted;
|
|
2244
|
+
}
|
|
2245
|
+
function buildGravitySlopedCandidate(pts, slopeRatio, remHorizM, toY, anchorToTargetStart, hasHeadForTarget) {
|
|
2246
|
+
const out = [pts[0]];
|
|
2247
|
+
let curY = pts[0][1];
|
|
2248
|
+
let startedHorizontal = false;
|
|
2249
|
+
for (let i = 1;i < pts.length; i++) {
|
|
2250
|
+
const prev = out[out.length - 1];
|
|
2251
|
+
const raw = pts[i];
|
|
2252
|
+
const hDist = distXZ2(prev, raw);
|
|
2253
|
+
const minTargetY = hasHeadForTarget ? toY + remHorizM[i] * slopeRatio : Number.NEGATIVE_INFINITY;
|
|
2254
|
+
if (hDist < 0.0001) {
|
|
2255
|
+
let desiredY = raw[1];
|
|
2256
|
+
if (hasHeadForTarget) {
|
|
2257
|
+
if (anchorToTargetStart && !startedHorizontal && remHorizM[i] > 0.0001) {
|
|
2258
|
+
desiredY = Math.min(pts[0][1], minTargetY);
|
|
2259
|
+
} else {
|
|
2260
|
+
desiredY = Math.max(desiredY, minTargetY);
|
|
2261
|
+
}
|
|
2262
|
+
}
|
|
2263
|
+
const nextY = Math.min(curY, desiredY);
|
|
2264
|
+
if (curY - nextY > 0.0001) {
|
|
2265
|
+
out.push([raw[0], nextY, raw[2]]);
|
|
2266
|
+
curY = nextY;
|
|
2267
|
+
}
|
|
2268
|
+
continue;
|
|
2269
|
+
}
|
|
2270
|
+
if (anchorToTargetStart && hasHeadForTarget && !startedHorizontal) {
|
|
2271
|
+
const horizStartY = Math.min(curY, toY + remHorizM[i - 1] * slopeRatio);
|
|
2272
|
+
if (curY - horizStartY > 0.0001) {
|
|
2273
|
+
out.push([prev[0], horizStartY, prev[2]]);
|
|
2274
|
+
curY = horizStartY;
|
|
2275
|
+
}
|
|
2276
|
+
}
|
|
2277
|
+
startedHorizontal = true;
|
|
2278
|
+
const slopedY = curY - hDist * slopeRatio;
|
|
2279
|
+
const dropFloorY = hasHeadForTarget ? Math.max(raw[1], minTargetY) : raw[1];
|
|
2280
|
+
if (dropFloorY < slopedY - 0.0001) {
|
|
2281
|
+
out.push([raw[0], slopedY, raw[2]]);
|
|
2282
|
+
out.push([raw[0], dropFloorY, raw[2]]);
|
|
2283
|
+
curY = dropFloorY;
|
|
2284
|
+
} else {
|
|
2285
|
+
out.push([raw[0], slopedY, raw[2]]);
|
|
2286
|
+
curY = slopedY;
|
|
2287
|
+
}
|
|
2288
|
+
}
|
|
2289
|
+
return out.length >= 2 ? compactPolyline(out) : [pts[0], [pts[pts.length - 1][0], curY, pts[pts.length - 1][2]]];
|
|
2290
|
+
}
|
|
2291
|
+
function shapeGravitySlope(rawPoints, minSlopeInPerFt, ctx, allowStartExteriorMargin, allowEndExteriorMargin) {
|
|
2292
|
+
const pts = compactPolyline(rawPoints);
|
|
2293
|
+
if (pts.length < 2) {
|
|
2294
|
+
return { path: pts, slopeInPerFt: undefined };
|
|
2295
|
+
}
|
|
2296
|
+
const slopeRatio = Math.max(0, minSlopeInPerFt) / 12;
|
|
2297
|
+
const remHorizM = new Float64Array(pts.length);
|
|
2298
|
+
for (let i = pts.length - 2;i >= 0; i--) {
|
|
2299
|
+
remHorizM[i] = remHorizM[i + 1] + distXZ2(pts[i], pts[i + 1]);
|
|
2300
|
+
}
|
|
2301
|
+
const totalInputHorizM = remHorizM[0];
|
|
2302
|
+
const fromY = pts[0][1];
|
|
2303
|
+
const toY = pts[pts.length - 1][1];
|
|
2304
|
+
const hasHeadForTarget = totalInputHorizM > 0.0001 && fromY >= toY + totalInputHorizM * slopeRatio - 0.000001;
|
|
2305
|
+
let finalPts;
|
|
2306
|
+
if (hasHeadForTarget) {
|
|
2307
|
+
const targetAnchored = buildGravitySlopedCandidate(pts, slopeRatio, remHorizM, toY, true, true);
|
|
2308
|
+
const trenchAnchored = buildGravitySlopedCandidate(pts, slopeRatio, remHorizM, toY, false, true);
|
|
2309
|
+
const evalTarget = evaluatePolyline(targetAnchored, ctx, allowStartExteriorMargin, allowEndExteriorMargin);
|
|
2310
|
+
const penTarget = evalPenalty(evalTarget);
|
|
2311
|
+
const evalTrench = evaluatePolyline(trenchAnchored, ctx, allowStartExteriorMargin, allowEndExteriorMargin);
|
|
2312
|
+
const penTrench = evalPenalty(evalTrench);
|
|
2313
|
+
if (penTrench < penTarget) {
|
|
2314
|
+
finalPts = trenchAnchored;
|
|
2315
|
+
} else if (penTarget < penTrench) {
|
|
2316
|
+
finalPts = targetAnchored;
|
|
2317
|
+
} else {
|
|
2318
|
+
finalPts = evalTarget.lengthM <= evalTrench.lengthM + 0.0001 ? targetAnchored : trenchAnchored;
|
|
2319
|
+
}
|
|
2320
|
+
} else {
|
|
2321
|
+
finalPts = buildGravitySlopedCandidate(pts, slopeRatio, remHorizM, toY, false, false);
|
|
2322
|
+
evaluatePolyline(finalPts, ctx, allowStartExteriorMargin, allowEndExteriorMargin);
|
|
2323
|
+
}
|
|
2324
|
+
let totalHorizM = 0;
|
|
2325
|
+
let totalHorizDropM = 0;
|
|
2326
|
+
for (let i = 1;i < finalPts.length; i++) {
|
|
2327
|
+
const a = finalPts[i - 1];
|
|
2328
|
+
const b = finalPts[i];
|
|
2329
|
+
const h = distXZ2(a, b);
|
|
2330
|
+
if (h > 0.0001) {
|
|
2331
|
+
totalHorizM += h;
|
|
2332
|
+
totalHorizDropM += a[1] - b[1];
|
|
2333
|
+
}
|
|
2334
|
+
}
|
|
2335
|
+
const slopeInPerFt = totalHorizM > 0.0001 ? round42(totalHorizDropM / totalHorizM * 12) : undefined;
|
|
2336
|
+
return { path: finalPts, slopeInPerFt };
|
|
2337
|
+
}
|
|
2338
|
+
function shapeNonDescendingVent(rawPoints, from, to, minHorizontalElevationM, ctx, allowStartExteriorMargin, allowEndExteriorMargin) {
|
|
2339
|
+
const pts = compactPolyline(rawPoints);
|
|
2340
|
+
if (pts.length < 2)
|
|
2341
|
+
return pts;
|
|
2342
|
+
const floorY = minHorizontalElevationM ?? from[1];
|
|
2343
|
+
const tieInY = to[1];
|
|
2344
|
+
const ceilingY = Math.max(floorY, tieInY);
|
|
2345
|
+
const out = [pts[0]];
|
|
2346
|
+
let curY = pts[0][1];
|
|
2347
|
+
let accumBelowFloorHorizM = 0;
|
|
2348
|
+
for (let i = 1;i < pts.length; i++) {
|
|
2349
|
+
const prev = out[out.length - 1];
|
|
2350
|
+
const raw = pts[i];
|
|
2351
|
+
const hDist = distXZ2(prev, raw);
|
|
2352
|
+
const isLast = i === pts.length - 1;
|
|
2353
|
+
const targetY = Math.max(curY, Math.min(raw[1], ceilingY));
|
|
2354
|
+
if (hDist < 0.0001) {
|
|
2355
|
+
if (targetY > curY + 0.0001) {
|
|
2356
|
+
out.push([raw[0], targetY, raw[2]]);
|
|
2357
|
+
curY = targetY;
|
|
2358
|
+
}
|
|
2359
|
+
continue;
|
|
2360
|
+
}
|
|
2361
|
+
if (curY < floorY - 0.0001 && targetY < floorY - 0.0001 && hDist <= 0.01 && accumBelowFloorHorizM + hDist <= 0.01 - 0.000001) {
|
|
2362
|
+
if (targetY > curY + 0.0001) {
|
|
2363
|
+
out.push([prev[0], targetY, prev[2]]);
|
|
2364
|
+
curY = targetY;
|
|
2365
|
+
}
|
|
2366
|
+
accumBelowFloorHorizM += hDist;
|
|
2367
|
+
out.push([raw[0], curY, raw[2]]);
|
|
2368
|
+
continue;
|
|
2369
|
+
}
|
|
2370
|
+
const clampedRawY = Math.min(raw[1], ceilingY);
|
|
2371
|
+
const horizY = Math.min(ceilingY, Math.max(curY, floorY, clampedRawY));
|
|
2372
|
+
if (horizY > curY + 0.0001) {
|
|
2373
|
+
out.push([prev[0], horizY, prev[2]]);
|
|
2374
|
+
curY = horizY;
|
|
2375
|
+
if (curY >= floorY - 0.0001) {
|
|
2376
|
+
accumBelowFloorHorizM = 0;
|
|
2377
|
+
}
|
|
2378
|
+
}
|
|
2379
|
+
out.push([raw[0], curY, raw[2]]);
|
|
2380
|
+
if (isLast && tieInY > curY + 0.0001) {
|
|
2381
|
+
out.push([raw[0], tieInY, raw[2]]);
|
|
2382
|
+
curY = tieInY;
|
|
2383
|
+
}
|
|
2384
|
+
}
|
|
2385
|
+
const shaped = compactPolyline(out);
|
|
2386
|
+
evaluatePolyline(shaped, ctx, allowStartExteriorMargin, allowEndExteriorMargin);
|
|
2387
|
+
return shaped;
|
|
2388
|
+
}
|
|
2389
|
+
function buildDisconnectedFallbackPath(from, to, mode, graph, allowedBands) {
|
|
2390
|
+
if (mode === "euclidean-cavity") {
|
|
2391
|
+
return compactPolyline([from, to]);
|
|
2392
|
+
}
|
|
2393
|
+
const primaryBand = allowedBands[0];
|
|
2394
|
+
const cruiseY = primaryBand === "attic" && graph.atticY !== null ? graph.atticY : (primaryBand === "under-slab" || primaryBand === "crawlspace") && graph.subFloorY !== null ? graph.subFloorY : primaryBand === "overhead-plenum" ? graph.ceilingPlenumY : mode === "gravity-slope" ? Math.min(from[1], to[1]) : Math.max(from[1], to[1]);
|
|
2395
|
+
const pts = [from];
|
|
2396
|
+
if (Math.abs(cruiseY - from[1]) > 0.0001) {
|
|
2397
|
+
pts.push([from[0], cruiseY, from[2]]);
|
|
2398
|
+
}
|
|
2399
|
+
if (Math.abs(to[0] - from[0]) > 0.0001 && Math.abs(to[2] - from[2]) > 0.0001) {
|
|
2400
|
+
pts.push([to[0], cruiseY, from[2]]);
|
|
2401
|
+
}
|
|
2402
|
+
if (Math.abs(to[1] - cruiseY) > 0.0001) {
|
|
2403
|
+
pts.push([to[0], cruiseY, to[2]]);
|
|
2404
|
+
}
|
|
2405
|
+
pts.push(to);
|
|
2406
|
+
return compactPolyline(pts);
|
|
2407
|
+
}
|
|
2408
|
+
function routeInLegalCavities2(params) {
|
|
2409
|
+
const { anatomy, graph, occupancy, request } = params;
|
|
2410
|
+
const from = sanitizeVec3(request.from);
|
|
2411
|
+
const to = sanitizeVec3(request.to);
|
|
2412
|
+
const halfXZ = Number.isFinite(request.halfXZ) ? Math.max(0, request.halfXZ) : 0;
|
|
2413
|
+
const halfY = Number.isFinite(request.halfY) ? Math.max(0, request.halfY) : 0;
|
|
2414
|
+
const clearanceM = Number.isFinite(request.clearanceM) ? Math.max(0, request.clearanceM) : 0;
|
|
2415
|
+
const roofClearanceM = typeof request.roofClearanceM === "number" && Number.isFinite(request.roofClearanceM) ? Math.max(0, request.roofClearanceM) : DEFAULT_ROOF_CLEARANCE_M2;
|
|
2416
|
+
const minSlopeInPerFt = typeof request.minSlopeInPerFt === "number" && Number.isFinite(request.minSlopeInPerFt) && request.minSlopeInPerFt >= 0 ? request.minSlopeInPerFt : DEFAULT_MIN_SLOPE_IN_PER_FT;
|
|
2417
|
+
const minHorizY = typeof request.minHorizontalElevationM === "number" && Number.isFinite(request.minHorizontalElevationM) ? request.minHorizontalElevationM : undefined;
|
|
2418
|
+
const openingPrisms = anatomy.getOpeningPrisms(graph.levelId);
|
|
2419
|
+
const roofShell = anatomy.getRoofShell(graph.levelId);
|
|
2420
|
+
const footprint = anatomy.getFootprint(graph.levelId);
|
|
2421
|
+
const walls = anatomy.getWalls(graph.levelId);
|
|
2422
|
+
const wallMap = new Map;
|
|
2423
|
+
for (const w of walls)
|
|
2424
|
+
wallMap.set(w.id, w);
|
|
2425
|
+
const allowedBandSet = new Set(request.allowedBands);
|
|
2426
|
+
const clashParams = {
|
|
2427
|
+
halfXZ,
|
|
2428
|
+
halfY,
|
|
2429
|
+
extraClearanceM: clearanceM,
|
|
2430
|
+
ignoreNodeIds: request.ignoreObstacleNodeIds
|
|
2431
|
+
};
|
|
2432
|
+
const encounteredObstacleIds = new Set;
|
|
2433
|
+
const ctx = {
|
|
2434
|
+
occupancy,
|
|
2435
|
+
clashParams,
|
|
2436
|
+
openingPrisms,
|
|
2437
|
+
roofShell,
|
|
2438
|
+
footprint,
|
|
2439
|
+
halfY,
|
|
2440
|
+
roofClearanceM,
|
|
2441
|
+
mode: request.mode,
|
|
2442
|
+
minHorizY,
|
|
2443
|
+
tieInY: to[1],
|
|
2444
|
+
encounteredObstacleIds
|
|
2445
|
+
};
|
|
2446
|
+
for (const obs of occupancy.segmentClashes(from, to, clashParams)) {
|
|
2447
|
+
encounteredObstacleIds.add(obs.nodeId);
|
|
2448
|
+
}
|
|
2449
|
+
if (dist32(from, to) < 0.0001) {
|
|
2450
|
+
const trivialPath = [roundVec3(from), roundVec3(to)];
|
|
2451
|
+
return finalizeRouteResult({
|
|
2452
|
+
path: trivialPath,
|
|
2453
|
+
bandsUsed: [],
|
|
2454
|
+
slopeInPerFt: undefined,
|
|
2455
|
+
encounteredObstacleIds,
|
|
2456
|
+
occupancy,
|
|
2457
|
+
clashParams,
|
|
2458
|
+
openingPrisms,
|
|
2459
|
+
roofShell,
|
|
2460
|
+
footprint,
|
|
2461
|
+
halfXZ,
|
|
2462
|
+
halfY,
|
|
2463
|
+
clearanceM,
|
|
2464
|
+
roofClearanceM,
|
|
2465
|
+
request
|
|
2466
|
+
});
|
|
2467
|
+
}
|
|
2468
|
+
let minBaseWeight = 1;
|
|
2469
|
+
if (typeof graph.minBaseWeight === "number" && Number.isFinite(graph.minBaseWeight) && graph.minBaseWeight > 0) {
|
|
2470
|
+
minBaseWeight = Math.min(1, graph.minBaseWeight);
|
|
2471
|
+
} else {
|
|
2472
|
+
for (let u = 0;u < graph.adjacency.length; u++) {
|
|
2473
|
+
const edges = graph.adjacency[u];
|
|
2474
|
+
if (!edges)
|
|
2475
|
+
continue;
|
|
2476
|
+
for (let e = 0;e < edges.length; e++) {
|
|
2477
|
+
const w = edges[e].baseCostWeight;
|
|
2478
|
+
if (Number.isFinite(w) && w > 0 && w < minBaseWeight) {
|
|
2479
|
+
minBaseWeight = w;
|
|
2480
|
+
}
|
|
2481
|
+
}
|
|
2482
|
+
}
|
|
2483
|
+
}
|
|
2484
|
+
const minWeightScale = Math.max(0.000001, minBaseWeight * (request.preferWallFollowing ? 0.85 : 1));
|
|
2485
|
+
const startExterior = !footprint.isPointInside(from[0], from[2]);
|
|
2486
|
+
const endExterior = !footprint.isPointInside(to[0], to[2]);
|
|
2487
|
+
const sharedDirect = resolveSharedDirectBand(from, to, graph, walls, allowedBandSet, request.mode);
|
|
2488
|
+
const skipWallDirectForStagger = sharedDirect?.isWall === true && request.mode === "orthogonal" && typeof request.lateralStaggerM === "number" && Math.abs(request.lateralStaggerM) > 0.0001;
|
|
2489
|
+
const directShortcut = sharedDirect && !skipWallDirectForStagger ? {
|
|
2490
|
+
eval: evaluatePolyline([from, to], ctx, startExterior, endExterior),
|
|
2491
|
+
band: sharedDirect.band,
|
|
2492
|
+
isWall: sharedDirect.isWall
|
|
2493
|
+
} : null;
|
|
2494
|
+
const startSplices = buildSpliceConnections(from, "enter", graph, allowedBandSet, wallMap, ctx, request.maxBendDegrees);
|
|
2495
|
+
const goalSplices = buildSpliceConnections(to, "exit", graph, allowedBandSet, wallMap, ctx, request.maxBendDegrees);
|
|
2496
|
+
const goalSplicesByVertex = new Map;
|
|
2497
|
+
for (const gs of goalSplices)
|
|
2498
|
+
goalSplicesByVertex.set(gs.vertexId, gs);
|
|
2499
|
+
const edgeCache = new Map;
|
|
2500
|
+
let searchResult = runConstrainedAStar({
|
|
2501
|
+
graph,
|
|
2502
|
+
from,
|
|
2503
|
+
to,
|
|
2504
|
+
allowedBandSet,
|
|
2505
|
+
request,
|
|
2506
|
+
startSplices,
|
|
2507
|
+
goalSplicesByVertex,
|
|
2508
|
+
directShortcut,
|
|
2509
|
+
edgeCache,
|
|
2510
|
+
ctx,
|
|
2511
|
+
minWeightScale,
|
|
2512
|
+
allowDegraded: false
|
|
2513
|
+
});
|
|
2514
|
+
if (!searchResult) {
|
|
2515
|
+
searchResult = runConstrainedAStar({
|
|
2516
|
+
graph,
|
|
2517
|
+
from,
|
|
2518
|
+
to,
|
|
2519
|
+
allowedBandSet,
|
|
2520
|
+
request,
|
|
2521
|
+
startSplices,
|
|
2522
|
+
goalSplicesByVertex,
|
|
2523
|
+
directShortcut,
|
|
2524
|
+
edgeCache,
|
|
2525
|
+
ctx,
|
|
2526
|
+
minWeightScale,
|
|
2527
|
+
allowDegraded: true
|
|
2528
|
+
});
|
|
2529
|
+
}
|
|
2530
|
+
let rawPath;
|
|
2531
|
+
let vertexBands;
|
|
2532
|
+
let bandsUsed;
|
|
2533
|
+
let wallIdsByPoint;
|
|
2534
|
+
if (searchResult) {
|
|
2535
|
+
rawPath = searchResult.waypoints;
|
|
2536
|
+
vertexBands = searchResult.vertexBands;
|
|
2537
|
+
bandsUsed = searchResult.bandsUsed;
|
|
2538
|
+
wallIdsByPoint = searchResult.wallIdsByPoint;
|
|
2539
|
+
} else {
|
|
2540
|
+
const fallbackPts = buildDisconnectedFallbackPath(from, to, request.mode, graph, request.allowedBands);
|
|
2541
|
+
const fallbackBand = request.allowedBands[0] ?? null;
|
|
2542
|
+
rawPath = fallbackPts;
|
|
2543
|
+
vertexBands = fallbackPts.map(() => fallbackBand);
|
|
2544
|
+
bandsUsed = fallbackBand ? [fallbackBand] : [];
|
|
2545
|
+
wallIdsByPoint = fallbackPts.map(() => {
|
|
2546
|
+
return;
|
|
2547
|
+
});
|
|
2548
|
+
}
|
|
2549
|
+
let shapedPath = compactPolyline(rawPath);
|
|
2550
|
+
let slopeInPerFt;
|
|
2551
|
+
if (request.mode === "orthogonal") {
|
|
2552
|
+
if (typeof request.lateralStaggerM === "number" && Math.abs(request.lateralStaggerM) > 0.0001) {
|
|
2553
|
+
shapedPath = applyLateralStagger(rawPath, wallIdsByPoint, request.lateralStaggerM, wallMap, ctx, request.maxBendDegrees);
|
|
2554
|
+
}
|
|
2555
|
+
shapedPath = compactPolyline(shapedPath);
|
|
2556
|
+
} else if (request.mode === "euclidean-cavity") {
|
|
2557
|
+
shapedPath = shapeEuclideanCavity(rawPath, vertexBands, ctx, request.maxBendDegrees, startExterior, endExterior);
|
|
2558
|
+
} else if (request.mode === "gravity-slope") {
|
|
2559
|
+
const sloped = shapeGravitySlope(shapedPath, minSlopeInPerFt, ctx, startExterior, endExterior);
|
|
2560
|
+
shapedPath = sloped.path;
|
|
2561
|
+
slopeInPerFt = sloped.slopeInPerFt;
|
|
2562
|
+
} else if (request.mode === "non-descending-vent") {
|
|
2563
|
+
shapedPath = shapeNonDescendingVent(shapedPath, from, to, minHorizY, ctx, startExterior, endExterior);
|
|
2564
|
+
}
|
|
2565
|
+
const roundedPath = [];
|
|
2566
|
+
for (let i = 0;i < shapedPath.length; i++) {
|
|
2567
|
+
const pt = roundVec3(shapedPath[i]);
|
|
2568
|
+
const prev = roundedPath[roundedPath.length - 1];
|
|
2569
|
+
if (!prev || prev[0] !== pt[0] || prev[1] !== pt[1] || prev[2] !== pt[2]) {
|
|
2570
|
+
roundedPath.push(pt);
|
|
2571
|
+
}
|
|
2572
|
+
}
|
|
2573
|
+
if (roundedPath.length === 0) {
|
|
2574
|
+
roundedPath.push(roundVec3(from), roundVec3(to));
|
|
2575
|
+
} else if (roundedPath.length === 1) {
|
|
2576
|
+
roundedPath.push(roundVec3(to));
|
|
2577
|
+
}
|
|
2578
|
+
return finalizeRouteResult({
|
|
2579
|
+
path: roundedPath,
|
|
2580
|
+
bandsUsed,
|
|
2581
|
+
slopeInPerFt,
|
|
2582
|
+
encounteredObstacleIds,
|
|
2583
|
+
occupancy,
|
|
2584
|
+
clashParams,
|
|
2585
|
+
openingPrisms,
|
|
2586
|
+
roofShell,
|
|
2587
|
+
footprint,
|
|
2588
|
+
halfXZ,
|
|
2589
|
+
halfY,
|
|
2590
|
+
clearanceM,
|
|
2591
|
+
roofClearanceM,
|
|
2592
|
+
request
|
|
2593
|
+
});
|
|
2594
|
+
}
|
|
2595
|
+
function finalizeRouteResult(params) {
|
|
2596
|
+
const {
|
|
2597
|
+
path,
|
|
2598
|
+
bandsUsed,
|
|
2599
|
+
slopeInPerFt,
|
|
2600
|
+
encounteredObstacleIds,
|
|
2601
|
+
occupancy,
|
|
2602
|
+
clashParams,
|
|
2603
|
+
openingPrisms,
|
|
2604
|
+
roofShell,
|
|
2605
|
+
footprint,
|
|
2606
|
+
halfXZ,
|
|
2607
|
+
halfY,
|
|
2608
|
+
clearanceM,
|
|
2609
|
+
roofClearanceM,
|
|
2610
|
+
request
|
|
2611
|
+
} = params;
|
|
2612
|
+
const pathClashes = occupancy.detectPathClashes(path, {
|
|
2613
|
+
...clashParams,
|
|
2614
|
+
deduplicateByNode: true
|
|
2615
|
+
});
|
|
2616
|
+
const clashSet = new Set;
|
|
2617
|
+
for (const c of pathClashes)
|
|
2618
|
+
clashSet.add(c.obstacle.nodeId);
|
|
2619
|
+
const clashObstacleIds = [...clashSet];
|
|
2620
|
+
const avoidedObstacleIds = [];
|
|
2621
|
+
for (const id of encounteredObstacleIds) {
|
|
2622
|
+
if (!clashSet.has(id))
|
|
2623
|
+
avoidedObstacleIds.push(id);
|
|
2624
|
+
}
|
|
2625
|
+
const openingHits = countPathOpeningPrismHits2(path, openingPrisms, 0);
|
|
2626
|
+
const roofViolation = !roofShell.isPathClearOfRoof(path, halfY, roofClearanceM);
|
|
2627
|
+
const sanitizedFrom = sanitizeVec3(request.from);
|
|
2628
|
+
const sanitizedTo = sanitizeVec3(request.to);
|
|
2629
|
+
const startExterior = !footprint.isPointInside(sanitizedFrom[0], sanitizedFrom[2]);
|
|
2630
|
+
const endExterior = !footprint.isPointInside(sanitizedTo[0], sanitizedTo[2]);
|
|
2631
|
+
const outdoorVoidViolation = startExterior || endExterior ? footprint.pathCrossesOutdoorVoid(path, {
|
|
2632
|
+
skipExteriorEndpointMarginM: 0.35,
|
|
2633
|
+
allowStartExteriorSkip: startExterior,
|
|
2634
|
+
allowEndExteriorSkip: endExterior
|
|
2635
|
+
}) : footprint.pathCrossesOutdoorVoid(path);
|
|
2636
|
+
const lengthM = round42(polylineLengthM2(path));
|
|
2637
|
+
const horizontalLengthM = round42(polylineHorizontalLengthM2(path));
|
|
2638
|
+
const bendDegrees = round22(countPathBendDegrees2(path));
|
|
2639
|
+
const { elbows90, elbows45 } = countPathElbows2(path);
|
|
2640
|
+
const elbowCount = elbows90 + elbows45;
|
|
2641
|
+
const bendViolation = typeof request.maxBendDegrees === "number" && Number.isFinite(request.maxBendDegrees) && bendDegrees > request.maxBendDegrees + 0.01;
|
|
2642
|
+
const minHorizY = typeof request.minHorizontalElevationM === "number" && Number.isFinite(request.minHorizontalElevationM) ? request.minHorizontalElevationM : undefined;
|
|
2643
|
+
let physicsViolation = false;
|
|
2644
|
+
if (minHorizY !== undefined && path.length >= 2) {
|
|
2645
|
+
for (let i = 1;i < path.length; i++) {
|
|
2646
|
+
const a = path[i - 1];
|
|
2647
|
+
const b = path[i];
|
|
2648
|
+
if (distXZ2(a, b) > 0.01 && (a[1] < minHorizY - 0.001 || b[1] < minHorizY - 0.001)) {
|
|
2649
|
+
physicsViolation = true;
|
|
2650
|
+
break;
|
|
2651
|
+
}
|
|
2652
|
+
}
|
|
2653
|
+
}
|
|
2654
|
+
if (!physicsViolation && request.mode === "gravity-slope" && path.length >= 2) {
|
|
2655
|
+
const fromY = sanitizedFrom[1];
|
|
2656
|
+
const toY = sanitizedTo[1];
|
|
2657
|
+
const slopeRatio = (typeof request.minSlopeInPerFt === "number" && Number.isFinite(request.minSlopeInPerFt) && request.minSlopeInPerFt >= 0 ? request.minSlopeInPerFt : DEFAULT_MIN_SLOPE_IN_PER_FT) / 12;
|
|
2658
|
+
if (toY > fromY + UPHILL_TOL_M) {
|
|
2659
|
+
physicsViolation = true;
|
|
2660
|
+
}
|
|
2661
|
+
let pathHorizM = 0;
|
|
2662
|
+
for (let i = 1;i < path.length; i++) {
|
|
2663
|
+
const a = path[i - 1];
|
|
2664
|
+
const b = path[i];
|
|
2665
|
+
if (b[1] - a[1] > UPHILL_TOL_M) {
|
|
2666
|
+
physicsViolation = true;
|
|
2667
|
+
break;
|
|
2668
|
+
}
|
|
2669
|
+
pathHorizM += distXZ2(a, b);
|
|
2670
|
+
}
|
|
2671
|
+
if (!physicsViolation && fromY >= toY + pathHorizM * slopeRatio - 0.0001) {
|
|
2672
|
+
const lastPt = path[path.length - 1];
|
|
2673
|
+
if (Math.abs(lastPt[1] - toY) > 0.02) {
|
|
2674
|
+
physicsViolation = true;
|
|
2675
|
+
}
|
|
2676
|
+
}
|
|
2677
|
+
} else if (!physicsViolation && request.mode === "non-descending-vent" && path.length >= 2) {
|
|
2678
|
+
const fromY = sanitizedFrom[1];
|
|
2679
|
+
const toY = sanitizedTo[1];
|
|
2680
|
+
const ceilingEnforced = minHorizY === undefined || minHorizY <= toY + UPHILL_TOL_M;
|
|
2681
|
+
if (fromY > toY + UPHILL_TOL_M) {
|
|
2682
|
+
physicsViolation = true;
|
|
2683
|
+
}
|
|
2684
|
+
for (let i = 1;!physicsViolation && i < path.length; i++) {
|
|
2685
|
+
const a = path[i - 1];
|
|
2686
|
+
const b = path[i];
|
|
2687
|
+
if (b[1] - a[1] < -UPHILL_TOL_M) {
|
|
2688
|
+
physicsViolation = true;
|
|
2689
|
+
break;
|
|
2690
|
+
}
|
|
2691
|
+
if (ceilingEnforced && (a[1] > toY + UPHILL_TOL_M || b[1] > toY + UPHILL_TOL_M)) {
|
|
2692
|
+
physicsViolation = true;
|
|
2693
|
+
break;
|
|
2694
|
+
}
|
|
2695
|
+
}
|
|
2696
|
+
if (!physicsViolation) {
|
|
2697
|
+
const lastPt = path[path.length - 1];
|
|
2698
|
+
if (Math.abs(lastPt[1] - toY) > 0.02) {
|
|
2699
|
+
physicsViolation = true;
|
|
2700
|
+
}
|
|
2701
|
+
}
|
|
2702
|
+
}
|
|
2703
|
+
const ok = clashObstacleIds.length === 0 && openingHits === 0 && !roofViolation && !outdoorVoidViolation && !bendViolation && !physicsViolation;
|
|
2704
|
+
let reservationId;
|
|
2705
|
+
if (request.reserveOnCommit === true && path.length >= 2) {
|
|
2706
|
+
const runId = request.runId ?? `${request.trade}-run-${autoRunCounter++}`;
|
|
2707
|
+
reservationId = occupancy.reserveRun({
|
|
2708
|
+
runId,
|
|
2709
|
+
trade: request.trade,
|
|
2710
|
+
path,
|
|
2711
|
+
halfXZ,
|
|
2712
|
+
halfY,
|
|
2713
|
+
clearanceM
|
|
2714
|
+
});
|
|
2715
|
+
}
|
|
2716
|
+
return {
|
|
2717
|
+
ok,
|
|
2718
|
+
path,
|
|
2719
|
+
lengthM,
|
|
2720
|
+
horizontalLengthM,
|
|
2721
|
+
bendDegrees,
|
|
2722
|
+
elbowCount,
|
|
2723
|
+
bandsUsed,
|
|
2724
|
+
avoidedObstacleIds,
|
|
2725
|
+
clashObstacleIds,
|
|
2726
|
+
openingHits,
|
|
2727
|
+
roofViolation,
|
|
2728
|
+
outdoorVoidViolation,
|
|
2729
|
+
...slopeInPerFt !== undefined ? { slopeInPerFt } : {},
|
|
2730
|
+
...reservationId !== undefined ? { reservationId } : {}
|
|
2731
|
+
};
|
|
2732
|
+
}
|
|
2733
|
+
export { compileLegalCavityGraph2, findLegalWallRiserPier2, routeInLegalCavities2 };
|