@kruschdev/pascal-plugin-plumbing 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +123 -2
- package/docs/AGENT-TOOLS.md +12 -0
- package/docs/ARCHITECTURE.md +15 -0
- package/docs/CAD-CATALOG.md +10 -0
- package/docs/IPC-REFERENCE.md +17 -0
- package/package.json +89 -4
- package/src/agent-operations.ts +854 -0
- package/src/agent-tools.ts +258 -0
- package/src/cad/catalog.ts +151 -0
- package/src/cad/equipment.ts +172 -0
- package/src/cad/fittings.ts +322 -0
- package/src/cad/fixtures.ts +255 -0
- package/src/cad/index.ts +6 -0
- package/src/cad/manifold-interior.ts +248 -0
- package/src/cad/materials.ts +122 -0
- package/src/index.ts +156 -0
- package/src/ipc/audit.ts +927 -0
- package/src/ipc/autopopulate.ts +5071 -0
- package/src/ipc/framing-interop.ts +843 -0
- package/src/ipc/index.ts +9 -0
- package/src/ipc/pricing.ts +571 -0
- package/src/ipc/routing.ts +2410 -0
- package/src/ipc/rules.ts +271 -0
- package/src/ipc/schedule.ts +653 -0
- package/src/ipc/sizing.ts +663 -0
- package/src/ipc/tables.ts +780 -0
- package/src/nodes/equipment.ts +330 -0
- package/src/nodes/fixture.ts +245 -0
- package/src/nodes/manifold.ts +194 -0
- package/src/nodes/parametrics.ts +170 -0
- package/src/nodes/pipe-run.ts +393 -0
- package/src/render.ts +64 -0
- package/src/ui/chat-card.tsx +209 -0
- package/src/ui/inspector-card.tsx +243 -0
- package/src/ui/manifold-cutaway-view.tsx +297 -0
- package/src/ui/plumbing-panel.tsx +812 -0
- package/src/ui/schedule-view.tsx +523 -0
- package/src/ui/store.ts +98 -0
|
@@ -0,0 +1,2410 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure 3D Plumbing Routing Engine (`src/ipc/routing.ts`).
|
|
3
|
+
*
|
|
4
|
+
* Trade-Priority Hierarchy:
|
|
5
|
+
* 1. Structural (`structural:beam`, `structural:column`, `beam`, `column`) - inviolable
|
|
6
|
+
* 2. Plumbing (DWV gravity first, then Hot/Cold supply & Fuel Gas) - FIRST MEP right-of-way
|
|
7
|
+
* 3. HVAC (yields to plumbing)
|
|
8
|
+
* 4. Electrical (yields to plumbing & HVAC; Plumbing respects NEC 110.26(E) overhead panel keep-out)
|
|
9
|
+
*
|
|
10
|
+
* Framework-free: zero React, Three.js, or `@pascal-app/*` imports.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
type BuildingAnatomy,
|
|
15
|
+
type CanonicalWall,
|
|
16
|
+
type CavityBand,
|
|
17
|
+
type CavityRouteResult,
|
|
18
|
+
type LegalCavityGraph,
|
|
19
|
+
type OccupancyObstacle,
|
|
20
|
+
type RoutingPhysicsMode,
|
|
21
|
+
TradeOccupancyGrid,
|
|
22
|
+
compileBuildingAnatomy,
|
|
23
|
+
compileLegalCavityGraph,
|
|
24
|
+
compileLevelRoofShell,
|
|
25
|
+
countPathElbows as kernelCountPathElbows,
|
|
26
|
+
extractBaseRoomPolygons,
|
|
27
|
+
extractCanonicalWalls,
|
|
28
|
+
extractTradeOccupancyObstacles,
|
|
29
|
+
pointInPolygon2D as kernelPointInPolygon2D,
|
|
30
|
+
polygonAreaAndCentroid as kernelPolygonAreaAndCentroid,
|
|
31
|
+
polylineHorizontalLengthM as kernelPolylineHorizontalLengthM,
|
|
32
|
+
polylineLengthM as kernelPolylineLengthM,
|
|
33
|
+
resolveLevelHeightM as kernelResolveLevelHeightM,
|
|
34
|
+
routeInLegalCavities,
|
|
35
|
+
segmentIntersectsAabb3,
|
|
36
|
+
} from '@kruschdev/pascal-mep-kernel'
|
|
37
|
+
import {
|
|
38
|
+
type SceneNodeLike,
|
|
39
|
+
type Vec3,
|
|
40
|
+
belongsToLevel,
|
|
41
|
+
computePipeFramingPenetrations,
|
|
42
|
+
extractBonesPlumbingContext,
|
|
43
|
+
} from './framing-interop'
|
|
44
|
+
import { IPC_RULE_THRESHOLDS } from './rules'
|
|
45
|
+
import { getMinRequiredDrainSlopeInPerFt } from './sizing'
|
|
46
|
+
import {
|
|
47
|
+
type FoundationType,
|
|
48
|
+
METERS_TO_FEET,
|
|
49
|
+
type OccupancyType,
|
|
50
|
+
type PipeMaterial,
|
|
51
|
+
type PipeNominalSize,
|
|
52
|
+
getPipeOuterDiameterM,
|
|
53
|
+
round2,
|
|
54
|
+
} from './tables'
|
|
55
|
+
|
|
56
|
+
export type Aabb3 = {
|
|
57
|
+
minX: number
|
|
58
|
+
minY: number
|
|
59
|
+
minZ: number
|
|
60
|
+
maxX: number
|
|
61
|
+
maxY: number
|
|
62
|
+
maxZ: number
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export type PlumbingObstacle = {
|
|
66
|
+
id: string
|
|
67
|
+
/** Underlying scene node ID (shared across subdivided `:segN` boxes on diagonal beams). */
|
|
68
|
+
nodeId?: string
|
|
69
|
+
category: 'structural' | 'electrical-dedicated-space'
|
|
70
|
+
/** Structural member role so vertical columns are never treated as under-floor beams to duck beneath. */
|
|
71
|
+
structuralKind?: 'beam' | 'column'
|
|
72
|
+
/** Optional structural beam role (e.g. 'header', 'floor-girder', 'ridge-beam'). */
|
|
73
|
+
beamRole?: string
|
|
74
|
+
label: string
|
|
75
|
+
box: Aabb3
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export type WallOpeningSpan = {
|
|
79
|
+
id: string
|
|
80
|
+
wallId: string
|
|
81
|
+
kind: 'door' | 'window'
|
|
82
|
+
name: string
|
|
83
|
+
/** Center distance along host wall from `wall.start` toward `wall.end` (meters). */
|
|
84
|
+
uCenterM: number
|
|
85
|
+
/** Rough opening width (meters). */
|
|
86
|
+
widthM: number
|
|
87
|
+
/** Start distance along host wall (meters). */
|
|
88
|
+
startU: number
|
|
89
|
+
/** End distance along host wall (meters). */
|
|
90
|
+
endU: number
|
|
91
|
+
/** Sill height above wall base (meters, 0 for doors). */
|
|
92
|
+
sillYM: number
|
|
93
|
+
/** Head height above wall base (meters). */
|
|
94
|
+
headYM: number
|
|
95
|
+
/** World-space horizontal center of opening along wall centerline. */
|
|
96
|
+
centerX: number
|
|
97
|
+
centerZ: number
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export type WallSegment2D = {
|
|
101
|
+
id: string
|
|
102
|
+
start: readonly [number, number]
|
|
103
|
+
end: readonly [number, number]
|
|
104
|
+
lengthM: number
|
|
105
|
+
thicknessM: number
|
|
106
|
+
heightM: number
|
|
107
|
+
ux: number
|
|
108
|
+
uz: number
|
|
109
|
+
nx: number
|
|
110
|
+
nz: number
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export type RoomPolygon2D = {
|
|
114
|
+
id: string
|
|
115
|
+
name: string
|
|
116
|
+
category:
|
|
117
|
+
| 'bathroom'
|
|
118
|
+
| 'master-bath'
|
|
119
|
+
| 'half-bath'
|
|
120
|
+
| 'kitchen'
|
|
121
|
+
| 'laundry'
|
|
122
|
+
| 'utility'
|
|
123
|
+
| 'garage'
|
|
124
|
+
| 'bedroom'
|
|
125
|
+
| 'living'
|
|
126
|
+
| 'exterior'
|
|
127
|
+
| 'commercial-restroom'
|
|
128
|
+
| 'commercial-kitchen'
|
|
129
|
+
| 'other'
|
|
130
|
+
polygon: ReadonlyArray<readonly [number, number]>
|
|
131
|
+
centroid: readonly [number, number]
|
|
132
|
+
areaSqm: number
|
|
133
|
+
areaSqft: number
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function polylineLengthM(path: ReadonlyArray<Vec3>): number {
|
|
137
|
+
return kernelPolylineLengthM(path)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function polylineHorizontalLengthM(path: ReadonlyArray<Vec3>): number {
|
|
141
|
+
return kernelPolylineHorizontalLengthM(path)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function countPathElbows(path: ReadonlyArray<Vec3>): { elbows90: number; elbows45: number } {
|
|
145
|
+
return kernelCountPathElbows(path)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Inspect a DWV drain path to verify monotonic gravity fall (no uphill segments)
|
|
150
|
+
* and compute effective horizontal slope (in/ft).
|
|
151
|
+
*/
|
|
152
|
+
export function measureDrainPathSlope(path: ReadonlyArray<Vec3>): {
|
|
153
|
+
hasUphillSegment: boolean
|
|
154
|
+
minSegmentSlopeInPerFt: number
|
|
155
|
+
overallSlopeInPerFt: number
|
|
156
|
+
horizontalSegments: number
|
|
157
|
+
} {
|
|
158
|
+
let hasUphill = false
|
|
159
|
+
let minSlope = Infinity
|
|
160
|
+
let horizDistM = 0
|
|
161
|
+
let horizDropM = 0
|
|
162
|
+
let horizontalSegments = 0
|
|
163
|
+
|
|
164
|
+
for (let i = 1; i < path.length; i++) {
|
|
165
|
+
const a = path[i - 1]!
|
|
166
|
+
const b = path[i]!
|
|
167
|
+
const dx = b[0] - a[0]
|
|
168
|
+
const dy = b[1] - a[1]
|
|
169
|
+
const dz = b[2] - a[2]
|
|
170
|
+
const horiz = Math.hypot(dx, dz)
|
|
171
|
+
// Skip near-vertical risers / fixture drops (slope > 45°)
|
|
172
|
+
if (horiz < 0.05 || Math.abs(dy) > horiz * 0.85) {
|
|
173
|
+
// A vertical drop is always downward (dy <= 0); upward vertical on a drain is an error
|
|
174
|
+
if (dy > 0.02) hasUphill = true
|
|
175
|
+
continue
|
|
176
|
+
}
|
|
177
|
+
horizontalSegments += 1
|
|
178
|
+
horizDistM += horiz
|
|
179
|
+
horizDropM += -dy
|
|
180
|
+
if (dy > 0.002) {
|
|
181
|
+
hasUphill = true
|
|
182
|
+
minSlope = Math.min(minSlope, -(dy / horiz) * 12)
|
|
183
|
+
} else {
|
|
184
|
+
const slopeInPerFt = (-dy / horiz) * 12
|
|
185
|
+
minSlope = Math.min(minSlope, slopeInPerFt)
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const overallSlopeInPerFt =
|
|
190
|
+
horizontalSegments > 0
|
|
191
|
+
? horizDistM > 0.05
|
|
192
|
+
? round2((horizDropM / horizDistM) * 12)
|
|
193
|
+
: round2(minSlope)
|
|
194
|
+
: 0.25
|
|
195
|
+
return {
|
|
196
|
+
hasUphillSegment: hasUphill,
|
|
197
|
+
minSegmentSlopeInPerFt: horizontalSegments > 0 ? round2(minSlope) : 0.25,
|
|
198
|
+
overallSlopeInPerFt,
|
|
199
|
+
horizontalSegments,
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const wallSegmentsCache = new WeakMap<
|
|
204
|
+
Readonly<Record<string, SceneNodeLike>>,
|
|
205
|
+
Map<string, WallSegment2D[]>
|
|
206
|
+
>()
|
|
207
|
+
|
|
208
|
+
export function extractWallSegments(
|
|
209
|
+
nodes: Readonly<Record<string, SceneNodeLike>>,
|
|
210
|
+
levelId: string | null = null,
|
|
211
|
+
): WallSegment2D[] {
|
|
212
|
+
const key = levelId ?? '__all__'
|
|
213
|
+
let byLevel = wallSegmentsCache.get(nodes)
|
|
214
|
+
if (!byLevel) {
|
|
215
|
+
byLevel = new Map()
|
|
216
|
+
wallSegmentsCache.set(nodes, byLevel)
|
|
217
|
+
}
|
|
218
|
+
const cached = byLevel.get(key)
|
|
219
|
+
if (cached) return cached
|
|
220
|
+
|
|
221
|
+
const walls: WallSegment2D[] = []
|
|
222
|
+
for (const n of Object.values(nodes)) {
|
|
223
|
+
if (n.type !== 'wall' || !belongsToLevel(nodes, n, levelId)) continue
|
|
224
|
+
if (!Array.isArray(n.start) || !Array.isArray(n.end)) continue
|
|
225
|
+
const sx = Number(n.start[0])
|
|
226
|
+
const sz = Number(n.start[1])
|
|
227
|
+
const ex = Number(n.end[0])
|
|
228
|
+
const ez = Number(n.end[1])
|
|
229
|
+
const len = Math.hypot(ex - sx, ez - sz)
|
|
230
|
+
if (!Number.isFinite(len) || len < 0.1) continue
|
|
231
|
+
const ux = (ex - sx) / len
|
|
232
|
+
const uz = (ez - sz) / len
|
|
233
|
+
walls.push({
|
|
234
|
+
id: n.id,
|
|
235
|
+
start: [sx, sz],
|
|
236
|
+
end: [ex, ez],
|
|
237
|
+
lengthM: len,
|
|
238
|
+
thicknessM: typeof n.thickness === 'number' && n.thickness > 0 ? n.thickness : 0.14,
|
|
239
|
+
heightM: typeof n.height === 'number' && n.height > 0 ? n.height : 2.6,
|
|
240
|
+
ux,
|
|
241
|
+
uz,
|
|
242
|
+
nx: -uz,
|
|
243
|
+
nz: ux,
|
|
244
|
+
})
|
|
245
|
+
}
|
|
246
|
+
byLevel.set(key, walls)
|
|
247
|
+
return walls
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Map pre-extracted `CanonicalWall` openings (filtering walls with `lengthM >= 0.3`)
|
|
252
|
+
* directly into `WallOpeningSpan[]` without re-scanning scene nodes.
|
|
253
|
+
*/
|
|
254
|
+
export function mapCanonicalWallsToOpenings(
|
|
255
|
+
walls: ReadonlyArray<CanonicalWall>,
|
|
256
|
+
): WallOpeningSpan[] {
|
|
257
|
+
const out: WallOpeningSpan[] = []
|
|
258
|
+
for (const wall of walls) {
|
|
259
|
+
if (wall.lengthM < 0.3) continue
|
|
260
|
+
for (const op of wall.openings) {
|
|
261
|
+
out.push({
|
|
262
|
+
id: op.id,
|
|
263
|
+
wallId: op.wallId,
|
|
264
|
+
kind: op.kind,
|
|
265
|
+
name: op.name ?? op.kind,
|
|
266
|
+
uCenterM: op.uCenterM,
|
|
267
|
+
widthM: op.widthM,
|
|
268
|
+
startU: op.startU,
|
|
269
|
+
endU: op.endU,
|
|
270
|
+
sillYM: op.framedSillYM,
|
|
271
|
+
headYM: op.headYM,
|
|
272
|
+
centerX: op.centerX,
|
|
273
|
+
centerZ: op.centerZ,
|
|
274
|
+
})
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return out
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Extract all Pascal `door` and `window` opening spans along walls on a level
|
|
282
|
+
* (handling both native `door`/`window` nodes and wall-attached opening `item` nodes).
|
|
283
|
+
*/
|
|
284
|
+
export function extractWallOpenings(
|
|
285
|
+
nodes: Readonly<Record<string, SceneNodeLike>>,
|
|
286
|
+
levelId: string | null = null,
|
|
287
|
+
): WallOpeningSpan[] {
|
|
288
|
+
const walls = extractCanonicalWalls(nodes, levelId, { minLengthM: 0.3 })
|
|
289
|
+
if (walls.length === 0) return []
|
|
290
|
+
return mapCanonicalWallsToOpenings(walls)
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
export function classifyRoomName(
|
|
294
|
+
name: string,
|
|
295
|
+
occupancy: OccupancyType = 'residential',
|
|
296
|
+
): RoomPolygon2D['category'] {
|
|
297
|
+
const s = name
|
|
298
|
+
.toLowerCase()
|
|
299
|
+
.normalize('NFD')
|
|
300
|
+
.replace(/[\u0300-\u036f]/g, '')
|
|
301
|
+
.replace(/[\u2018\u2019`]/g, "'")
|
|
302
|
+
const hasIndoorTerm =
|
|
303
|
+
/bath|shower|restroom|powder|ensuite|en-suite|\bwc\d*\b|toilet|salle\s+de\s+bains?|salle\s+d'eau|salle\s+de\s+douche|\bbad\b|badezimmer|\bbano\b|\bbagno\b|kitchen|cuisine|cocina|cucina|\bkuche\b|\bkueche\b|laundry|buanderie|lavanderia|utility|bedroom|\bchambre\b|living|dining|office/.test(
|
|
304
|
+
s,
|
|
305
|
+
)
|
|
306
|
+
if (
|
|
307
|
+
!hasIndoorTerm &&
|
|
308
|
+
/\bpool\b|swimming|piscine|porch|patio|deck|balcony|yard|lanai|courtyard|garden|driveway|exterior|\bbalcon\b|\bterrasse\b|\bterrace\b|\bterraza\b|\bspawn\b/.test(
|
|
309
|
+
s,
|
|
310
|
+
)
|
|
311
|
+
) {
|
|
312
|
+
return 'exterior'
|
|
313
|
+
}
|
|
314
|
+
if (/commercial.*restroom|mens|womens|public.*(bath|restroom)/.test(s)) return 'commercial-restroom'
|
|
315
|
+
if (/restroom/.test(s)) return occupancy === 'commercial' ? 'commercial-restroom' : 'bathroom'
|
|
316
|
+
if (/commercial.*kitchen|prep.*kitchen|scullery/.test(s)) return 'commercial-kitchen'
|
|
317
|
+
if (/master.*bath|primary.*bath|ensuite|en-suite/.test(s)) return 'master-bath'
|
|
318
|
+
if (/half.*bath|powder/.test(s)) return 'half-bath'
|
|
319
|
+
if (
|
|
320
|
+
/bath|shower|salle\s+de\s+bains?|salle\s+d'eau|salle\s+de\s+douche|\bbad\b|badezimmer|\bbano\b|cuarto\s+de\s+bano|\bbagno\b/.test(
|
|
321
|
+
s,
|
|
322
|
+
)
|
|
323
|
+
) {
|
|
324
|
+
return 'bathroom'
|
|
325
|
+
}
|
|
326
|
+
if (/\bwc\d*\b|\btoilettes?\b|\baseo\b/.test(s)) return 'half-bath'
|
|
327
|
+
if (/toilet/.test(s)) return 'bathroom'
|
|
328
|
+
if (/butler'?s?\s*pantry|wet\s*pantry|prep\s*pantry/.test(s)) return 'kitchen'
|
|
329
|
+
if (/\bkitchen\s+pantry\b/.test(s)) return 'other'
|
|
330
|
+
if (/kitchen|cuisine|cocina|cucina|\bkuche\b|\bkueche\b/.test(s)) return 'kitchen'
|
|
331
|
+
if (/laundry|mudroom|washer|buanderie|waschkuche|waschkueche|lavanderia|hauswirtschaftsraum/.test(s)) {
|
|
332
|
+
return 'laundry'
|
|
333
|
+
}
|
|
334
|
+
if (/pantry|closet|\bw\.?\s*i\.?\s*c\.?\b|walk[-\s]*in/.test(s)) return 'other'
|
|
335
|
+
if (/utility|mech|boiler|furnace|storage/.test(s)) return 'utility'
|
|
336
|
+
if (/garage|carport/.test(s)) return 'garage'
|
|
337
|
+
if (/bed|nursery|guest|\bchambre\b|schlafzimmer|dormitorio/.test(s)) return 'bedroom'
|
|
338
|
+
if (/living|great|family|dining|office|den|hall|\bsalon\b|\bsejour\b|wohnzimmer|\bbureau\b/.test(s)) return 'living'
|
|
339
|
+
return 'other'
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
export function polygonAreaAndCentroid(
|
|
343
|
+
polygon: ReadonlyArray<readonly [number, number]>,
|
|
344
|
+
): { areaSqm: number; centroid: readonly [number, number] } {
|
|
345
|
+
if (polygon.length < 3) return { areaSqm: 0, centroid: [0, 0] }
|
|
346
|
+
const { areaSqM, centroid } = kernelPolygonAreaAndCentroid(polygon)
|
|
347
|
+
return { areaSqm: areaSqM, centroid }
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
export function pointInPolygon2D(
|
|
351
|
+
x: number,
|
|
352
|
+
z: number,
|
|
353
|
+
polygon: ReadonlyArray<readonly [number, number]>,
|
|
354
|
+
): boolean {
|
|
355
|
+
return kernelPointInPolygon2D(x, z, polygon)
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
export function extractRoomPolygons(
|
|
359
|
+
nodes: Readonly<Record<string, SceneNodeLike>>,
|
|
360
|
+
levelId: string | null = null,
|
|
361
|
+
occupancy: OccupancyType = 'residential',
|
|
362
|
+
): RoomPolygon2D[] {
|
|
363
|
+
return extractBaseRoomPolygons(nodes, levelId).map((base) => {
|
|
364
|
+
const classifyText = `${base.rawCategory} ${base.name}`.trim()
|
|
365
|
+
return {
|
|
366
|
+
id: base.id,
|
|
367
|
+
name: base.name,
|
|
368
|
+
category: classifyRoomName(classifyText, occupancy),
|
|
369
|
+
polygon: base.polygon,
|
|
370
|
+
centroid: base.centroid,
|
|
371
|
+
areaSqm: round2(base.areaSqM),
|
|
372
|
+
areaSqft: round2(base.areaSqFt),
|
|
373
|
+
}
|
|
374
|
+
})
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
export function resolveLevelHeightM(
|
|
378
|
+
nodes: Readonly<Record<string, SceneNodeLike>>,
|
|
379
|
+
levelId: string | null = null,
|
|
380
|
+
): number {
|
|
381
|
+
return kernelResolveLevelHeightM(nodes, levelId)
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
export type BuildingBounds2D = {
|
|
385
|
+
minX: number
|
|
386
|
+
maxX: number
|
|
387
|
+
minZ: number
|
|
388
|
+
maxZ: number
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Estimate pitched gable roof soffit/ridge elevation at `(x, z)` over the walled building footprint.
|
|
393
|
+
*/
|
|
394
|
+
export function estimateGableRoofYAt(
|
|
395
|
+
x: number,
|
|
396
|
+
z: number,
|
|
397
|
+
bounds: BuildingBounds2D,
|
|
398
|
+
wallH: number,
|
|
399
|
+
pitchRiseOver12 = 6,
|
|
400
|
+
): number {
|
|
401
|
+
const spanX = bounds.maxX - bounds.minX
|
|
402
|
+
const spanZ = bounds.maxZ - bounds.minZ
|
|
403
|
+
if (spanX <= 0.1 || spanZ <= 0.1) return wallH
|
|
404
|
+
const ridgeAlongX = spanX >= spanZ
|
|
405
|
+
const halfSpan = (ridgeAlongX ? spanZ : spanX) / 2
|
|
406
|
+
const center = ridgeAlongX ? (bounds.minZ + bounds.maxZ) / 2 : (bounds.minX + bounds.maxX) / 2
|
|
407
|
+
const distFromRidge = Math.abs((ridgeAlongX ? z : x) - center)
|
|
408
|
+
const riseM = Math.max(0, halfSpan - distFromRidge) * (pitchRiseOver12 / 12)
|
|
409
|
+
return wallH + riseM
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Build a roof-surface evaluator `roofYAt(x, z)` for plumbing clearance checks on `levelId`.
|
|
414
|
+
* Delegates to `@kruschdev/pascal-mep-kernel`'s `compileLevelRoofShell`, supporting all 7 Pascal
|
|
415
|
+
* roof types (`flat`, `shed`, `gable`, `hip`, `dutch`, `mansard`, `conical`), story-ordinal
|
|
416
|
+
* vertical ordering, spatial wall-less roof-level adoption, `pitch === 0` flat behavior, and
|
|
417
|
+
* lower-story flat `wallH` fallback.
|
|
418
|
+
*/
|
|
419
|
+
export function buildPlumbingRoofYAt(
|
|
420
|
+
nodes: Readonly<Record<string, SceneNodeLike>>,
|
|
421
|
+
levelId: string | null,
|
|
422
|
+
bounds: BuildingBounds2D,
|
|
423
|
+
wallH: number,
|
|
424
|
+
pitchRiseOver12 = 6,
|
|
425
|
+
): (x: number, z: number) => number {
|
|
426
|
+
const shell = compileLevelRoofShell(nodes, levelId, wallH, {
|
|
427
|
+
wallBounds: bounds,
|
|
428
|
+
fallbackPitchRiseOver12: pitchRiseOver12,
|
|
429
|
+
clampOverhangToEave: true,
|
|
430
|
+
})
|
|
431
|
+
return shell.roofEnvelopeY
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* Map pre-extracted kernel `OccupancyObstacle[]` into `PlumbingObstacle[]`,
|
|
436
|
+
* subdividing diagonal structural beams once into tight AABB segments.
|
|
437
|
+
*/
|
|
438
|
+
export function mapOccupancyObstaclesToPlumbing(
|
|
439
|
+
kernelObstacles: ReadonlyArray<OccupancyObstacle>,
|
|
440
|
+
): PlumbingObstacle[] {
|
|
441
|
+
const obstacles: PlumbingObstacle[] = []
|
|
442
|
+
|
|
443
|
+
for (const occ of kernelObstacles) {
|
|
444
|
+
if (occ.kind === 'structural-beam' && occ.prism) {
|
|
445
|
+
const p = occ.prism
|
|
446
|
+
const sx = p.centerX + p.lxMin * p.cos
|
|
447
|
+
const sz = p.centerZ + p.lxMin * p.sin
|
|
448
|
+
const ex = p.centerX + p.lxMax * p.cos
|
|
449
|
+
const ez = p.centerZ + p.lxMax * p.sin
|
|
450
|
+
if (Math.abs(ex - sx) > 0.2 && Math.abs(ez - sz) > 0.2) {
|
|
451
|
+
const halfW = (p.lzMax - p.lzMin) / 2
|
|
452
|
+
const len = p.lxMax - p.lxMin
|
|
453
|
+
const steps = Math.max(2, Math.min(24, Math.ceil(len / 0.6)))
|
|
454
|
+
for (let i = 0; i < steps; i++) {
|
|
455
|
+
const t0 = i / steps
|
|
456
|
+
const t1 = (i + 1) / steps
|
|
457
|
+
const x0 = sx + (ex - sx) * t0
|
|
458
|
+
const z0 = sz + (ez - sz) * t0
|
|
459
|
+
const x1 = sx + (ex - sx) * t1
|
|
460
|
+
const z1 = sz + (ez - sz) * t1
|
|
461
|
+
obstacles.push({
|
|
462
|
+
id: `${occ.id}:seg${i}`,
|
|
463
|
+
nodeId: occ.nodeId,
|
|
464
|
+
category: 'structural',
|
|
465
|
+
structuralKind: 'beam',
|
|
466
|
+
...(occ.beamRole ? { beamRole: occ.beamRole } : {}),
|
|
467
|
+
label: occ.label,
|
|
468
|
+
box: {
|
|
469
|
+
minX: Math.min(x0, x1) - halfW,
|
|
470
|
+
maxX: Math.max(x0, x1) + halfW,
|
|
471
|
+
minY: p.yMin,
|
|
472
|
+
maxY: p.yMax,
|
|
473
|
+
minZ: Math.min(z0, z1) - halfW,
|
|
474
|
+
maxZ: Math.max(z0, z1) + halfW,
|
|
475
|
+
},
|
|
476
|
+
})
|
|
477
|
+
}
|
|
478
|
+
continue
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
const category: PlumbingObstacle['category'] =
|
|
483
|
+
occ.kind === 'electrical-dedicated-space' ? 'electrical-dedicated-space' : 'structural'
|
|
484
|
+
obstacles.push({
|
|
485
|
+
id: occ.id,
|
|
486
|
+
nodeId: occ.nodeId,
|
|
487
|
+
category,
|
|
488
|
+
...(occ.structuralKind ? { structuralKind: occ.structuralKind } : {}),
|
|
489
|
+
...(occ.beamRole ? { beamRole: occ.beamRole } : {}),
|
|
490
|
+
label: occ.label,
|
|
491
|
+
box: { ...occ.aabb },
|
|
492
|
+
})
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
return obstacles
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* Extract Priority #1 Structural obstacles AND NEC 110.26(E) Dedicated Electrical Space columns
|
|
500
|
+
* (footprint of any `electrical:panel` or `electrical:transformer` extending from floor to structural ceiling).
|
|
501
|
+
*/
|
|
502
|
+
export function extractPlumbingObstacles(
|
|
503
|
+
nodes: Readonly<Record<string, SceneNodeLike>>,
|
|
504
|
+
levelId: string | null = null,
|
|
505
|
+
): PlumbingObstacle[] {
|
|
506
|
+
const kernelObstacles = extractTradeOccupancyObstacles(nodes, levelId, {
|
|
507
|
+
forTrade: 'plumbing',
|
|
508
|
+
})
|
|
509
|
+
return mapOccupancyObstaclesToPlumbing(kernelObstacles)
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
export function segmentIntersectsAabb(a: Vec3, b: Vec3, box: Aabb3, margin = 0.02): boolean {
|
|
513
|
+
return segmentIntersectsAabb3(a, b, box, margin)
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
export function detectPlumbingClashes(
|
|
517
|
+
path: ReadonlyArray<Vec3>,
|
|
518
|
+
obstacles: ReadonlyArray<PlumbingObstacle>,
|
|
519
|
+
pipeRadiusM = 0.025,
|
|
520
|
+
): PlumbingObstacle[] {
|
|
521
|
+
const hits: PlumbingObstacle[] = []
|
|
522
|
+
const seen = new Set<string>()
|
|
523
|
+
for (let i = 1; i < path.length; i++) {
|
|
524
|
+
const a = path[i - 1]!
|
|
525
|
+
const b = path[i]!
|
|
526
|
+
for (const obs of obstacles) {
|
|
527
|
+
const dedupeKey = obs.nodeId ?? obs.id.replace(/:seg\d+$/, '')
|
|
528
|
+
if (seen.has(dedupeKey)) continue
|
|
529
|
+
if (segmentIntersectsAabb(a, b, obs.box, pipeRadiusM + 0.02)) {
|
|
530
|
+
seen.add(dedupeKey)
|
|
531
|
+
hits.push(obs)
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
return hits
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
export function nearestWallToPoint(
|
|
539
|
+
x: number,
|
|
540
|
+
z: number,
|
|
541
|
+
walls: ReadonlyArray<WallSegment2D>,
|
|
542
|
+
): { wall: WallSegment2D; t: number; point: readonly [number, number]; distanceM: number } | null {
|
|
543
|
+
let best: { wall: WallSegment2D; t: number; point: readonly [number, number]; distanceM: number } | null =
|
|
544
|
+
null
|
|
545
|
+
for (const wall of walls) {
|
|
546
|
+
const dx = x - wall.start[0]
|
|
547
|
+
const dz = z - wall.start[1]
|
|
548
|
+
const rawT = (dx * wall.ux + dz * wall.uz) / wall.lengthM
|
|
549
|
+
const t = Math.max(0.08, Math.min(0.92, rawT))
|
|
550
|
+
const px = wall.start[0] + wall.ux * wall.lengthM * t
|
|
551
|
+
const pz = wall.start[1] + wall.uz * wall.lengthM * t
|
|
552
|
+
const dist = Math.hypot(x - px, z - pz)
|
|
553
|
+
if (!best || dist < best.distanceM) {
|
|
554
|
+
best = { wall, t, point: [px, pz], distanceM: dist }
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
return best
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
/**
|
|
561
|
+
* Deduplicate consecutive nearly identical waypoints while preserving 0.1 mm (0.0001 m) elevation precision
|
|
562
|
+
* so 1/4"/ft (2.08%) gravity slopes are never quantized below code minimums even on short horizontal legs.
|
|
563
|
+
*/
|
|
564
|
+
function cleanPath(pts: Vec3[]): Vec3[] {
|
|
565
|
+
const r4 = (v: number) => Math.round(v * 10000) / 10000
|
|
566
|
+
const out: Vec3[] = []
|
|
567
|
+
for (let i = 0; i < pts.length; i++) {
|
|
568
|
+
const p = pts[i]!
|
|
569
|
+
const prev = out[out.length - 1]
|
|
570
|
+
const dist = prev ? Math.hypot(p[0] - prev[0], p[1] - prev[1], p[2] - prev[2]) : Number.POSITIVE_INFINITY
|
|
571
|
+
if (!prev || dist > 0.008) {
|
|
572
|
+
out.push([r4(p[0]), r4(p[1]), r4(p[2])])
|
|
573
|
+
} else if (i === pts.length - 1 && (dist > 1e-4 || out.length === 1)) {
|
|
574
|
+
// Preserve exact final destination endpoint without tilting the preceding orthogonal segment
|
|
575
|
+
out.push([r4(p[0]), r4(p[1]), r4(p[2])])
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
return out
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
function isUnderFloorBeamObstacle(o: PlumbingObstacle): boolean {
|
|
582
|
+
return (
|
|
583
|
+
o.category === 'structural' &&
|
|
584
|
+
(o.structuralKind === 'beam' ||
|
|
585
|
+
(!o.structuralKind && !/column/i.test(o.label) && o.box.maxY <= 0.25)) &&
|
|
586
|
+
o.box.minY < 0
|
|
587
|
+
)
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
function isElevatedBeamObstacle(o: PlumbingObstacle): boolean {
|
|
591
|
+
return (
|
|
592
|
+
o.category === 'structural' &&
|
|
593
|
+
(o.structuralKind === 'beam' || o.beamRole === 'header' || /header|beam|ridge/i.test(o.label)) &&
|
|
594
|
+
o.box.maxY > 0.5
|
|
595
|
+
)
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/**
|
|
599
|
+
* Given a vertical riser/drop at `(x, z)` that collides with one or more obstacles,
|
|
600
|
+
* generate up to `maxCandidates` (default 8) closest lateral `(x', z')` jog positions just outside the colliding AABBs.
|
|
601
|
+
*/
|
|
602
|
+
function buildLateralEscapeCandidates(
|
|
603
|
+
x: number,
|
|
604
|
+
z: number,
|
|
605
|
+
hits: ReadonlyArray<PlumbingObstacle>,
|
|
606
|
+
clearPad: number,
|
|
607
|
+
maxCandidates = 8,
|
|
608
|
+
): Array<readonly [number, number]> {
|
|
609
|
+
const candidates: Array<readonly [number, number, number]> = []
|
|
610
|
+
const seen = new Set<string>()
|
|
611
|
+
const add = (cx: number, cz: number) => {
|
|
612
|
+
const k = `${Math.round(cx * 100)},${Math.round(cz * 100)}`
|
|
613
|
+
if (seen.has(k)) return
|
|
614
|
+
seen.add(k)
|
|
615
|
+
candidates.push([cx, cz, Math.hypot(cx - x, cz - z)])
|
|
616
|
+
}
|
|
617
|
+
for (const obs of hits) {
|
|
618
|
+
// Try shortest escapes along X and Z past the obstacle's jambs / faces
|
|
619
|
+
const leftX = obs.box.minX - clearPad
|
|
620
|
+
const rightX = obs.box.maxX + clearPad
|
|
621
|
+
const backZ = obs.box.minZ - clearPad
|
|
622
|
+
const frontZ = obs.box.maxZ + clearPad
|
|
623
|
+
add(leftX, z)
|
|
624
|
+
add(rightX, z)
|
|
625
|
+
add(x, backZ)
|
|
626
|
+
add(x, frontZ)
|
|
627
|
+
// Also try diagonal corners in case two beams meet at a corner/post
|
|
628
|
+
add(leftX, backZ)
|
|
629
|
+
add(leftX, frontZ)
|
|
630
|
+
add(rightX, backZ)
|
|
631
|
+
add(rightX, frontZ)
|
|
632
|
+
}
|
|
633
|
+
candidates.sort((a, b) => a[2] - b[2])
|
|
634
|
+
return candidates.slice(0, maxCandidates).map(([cx, cz]) => [cx, cz] as const)
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
function resolveAllowedEnvelope(
|
|
638
|
+
buildingBounds: BuildingBounds2D | undefined,
|
|
639
|
+
from: Vec3,
|
|
640
|
+
toXZ: readonly [number, number],
|
|
641
|
+
margin = 0.08,
|
|
642
|
+
): BuildingBounds2D | null {
|
|
643
|
+
if (!buildingBounds) return null
|
|
644
|
+
const innerMinX = buildingBounds.minX + margin
|
|
645
|
+
const innerMaxX = buildingBounds.maxX - margin
|
|
646
|
+
const innerMinZ = buildingBounds.minZ + margin
|
|
647
|
+
const innerMaxZ = buildingBounds.maxZ - margin
|
|
648
|
+
return {
|
|
649
|
+
minX: Math.min(innerMinX <= innerMaxX ? innerMinX : buildingBounds.minX, from[0], toXZ[0]),
|
|
650
|
+
maxX: Math.max(innerMinX <= innerMaxX ? innerMaxX : buildingBounds.maxX, from[0], toXZ[0]),
|
|
651
|
+
minZ: Math.min(innerMinZ <= innerMaxZ ? innerMinZ : buildingBounds.minZ, from[2], toXZ[1]),
|
|
652
|
+
maxZ: Math.max(innerMinZ <= innerMaxZ ? innerMaxZ : buildingBounds.maxZ, from[2], toXZ[1]),
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
function exceedsEnvelopeXZ(path: ReadonlyArray<Vec3>, env: BuildingBounds2D | null): boolean {
|
|
657
|
+
if (!env) return false
|
|
658
|
+
for (const p of path) {
|
|
659
|
+
if (p[0] < env.minX - 1e-4 || p[0] > env.maxX + 1e-4 || p[2] < env.minZ - 1e-4 || p[2] > env.maxZ + 1e-4) {
|
|
660
|
+
return true
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
return false
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
function minRoofAlongPath(
|
|
667
|
+
path: ReadonlyArray<Vec3>,
|
|
668
|
+
maxRoofYAt?: (x: number, z: number) => number,
|
|
669
|
+
): number {
|
|
670
|
+
if (!maxRoofYAt) return Infinity
|
|
671
|
+
let minY = Infinity
|
|
672
|
+
for (let i = 1; i < path.length; i++) {
|
|
673
|
+
const a = path[i - 1]!
|
|
674
|
+
const b = path[i]!
|
|
675
|
+
const segLen = Math.hypot(b[0] - a[0], b[2] - a[2])
|
|
676
|
+
const steps = Math.max(4, Math.min(64, Math.ceil(segLen / 0.25)))
|
|
677
|
+
for (let s = 0; s <= steps; s++) {
|
|
678
|
+
const f = s / steps
|
|
679
|
+
const px = a[0] + (b[0] - a[0]) * f
|
|
680
|
+
const pz = a[2] + (b[2] - a[2]) * f
|
|
681
|
+
const ry = maxRoofYAt(px, pz)
|
|
682
|
+
if (ry < minY) minY = ry
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
return minY
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
function exceedsRoofPlane(
|
|
689
|
+
path: ReadonlyArray<Vec3>,
|
|
690
|
+
pipeRadiusM: number,
|
|
691
|
+
maxRoofYAt?: (x: number, z: number) => number,
|
|
692
|
+
): boolean {
|
|
693
|
+
if (!maxRoofYAt) return false
|
|
694
|
+
for (let i = 1; i < path.length; i++) {
|
|
695
|
+
const a = path[i - 1]!
|
|
696
|
+
const b = path[i]!
|
|
697
|
+
const segLen = Math.hypot(b[0] - a[0], b[2] - a[2])
|
|
698
|
+
const steps = Math.max(4, Math.min(64, Math.ceil(segLen / 0.25)))
|
|
699
|
+
for (let s = 0; s <= steps; s++) {
|
|
700
|
+
const f = s / steps
|
|
701
|
+
const px = a[0] + (b[0] - a[0]) * f
|
|
702
|
+
const py = a[1] + (b[1] - a[1]) * f
|
|
703
|
+
const pz = a[2] + (b[2] - a[2]) * f
|
|
704
|
+
if (py + pipeRadiusM > maxRoofYAt(px, pz) - 0.08 + 1e-4) return true
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
return false
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
/**
|
|
711
|
+
* Generate obstacle-aware horizontal detour waypoint lists around colliding columns/panels/beams.
|
|
712
|
+
*/
|
|
713
|
+
function buildObstacleDetourWaypoints(
|
|
714
|
+
startXZ: readonly [number, number],
|
|
715
|
+
endXZ: readonly [number, number],
|
|
716
|
+
hits: ReadonlyArray<PlumbingObstacle>,
|
|
717
|
+
edgePad: number,
|
|
718
|
+
): Array<Array<readonly [number, number]>> {
|
|
719
|
+
const out: Array<Array<readonly [number, number]>> = []
|
|
720
|
+
const [sx, sz] = startXZ
|
|
721
|
+
const [ex, ez] = endXZ
|
|
722
|
+
for (const obs of hits) {
|
|
723
|
+
const lx = obs.box.minX - edgePad
|
|
724
|
+
const rx = obs.box.maxX + edgePad
|
|
725
|
+
const bz = obs.box.minZ - edgePad
|
|
726
|
+
const fz = obs.box.maxZ + edgePad
|
|
727
|
+
out.push(
|
|
728
|
+
[[lx, sz], [lx, ez], [ex, ez]],
|
|
729
|
+
[[rx, sz], [rx, ez], [ex, ez]],
|
|
730
|
+
[[sx, bz], [ex, bz], [ex, ez]],
|
|
731
|
+
[[sx, fz], [ex, fz], [ex, ez]],
|
|
732
|
+
[[lx, bz], [ex, bz], [ex, ez]],
|
|
733
|
+
[[rx, fz], [ex, fz], [ex, ez]],
|
|
734
|
+
[[lx, fz], [ex, fz], [ex, ez]],
|
|
735
|
+
[[rx, bz], [ex, bz], [ex, ez]],
|
|
736
|
+
)
|
|
737
|
+
}
|
|
738
|
+
return out
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
export type PlumbingCavityContext = {
|
|
742
|
+
/** Pascal scene nodes (used to compile WeakMap-memoized BuildingAnatomy if `anatomy` is omitted). */
|
|
743
|
+
nodes?: Readonly<Record<string, SceneNodeLike>>
|
|
744
|
+
levelId?: string | null
|
|
745
|
+
/** Pre-compiled BuildingAnatomy (optional). */
|
|
746
|
+
anatomy?: BuildingAnatomy
|
|
747
|
+
/** Pre-compiled LegalCavityGraph (optional — compiled on demand if omitted). */
|
|
748
|
+
graph?: LegalCavityGraph
|
|
749
|
+
/** Pre-populated TradeOccupancyGrid (optional — created on demand if omitted). */
|
|
750
|
+
occupancy?: TradeOccupancyGrid
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
export type RoutePlumbingViaLegalCavityParams = PlumbingCavityContext & {
|
|
754
|
+
from: Vec3
|
|
755
|
+
to: Vec3
|
|
756
|
+
mode: RoutingPhysicsMode
|
|
757
|
+
nominalSize?: PipeNominalSize
|
|
758
|
+
material?: PipeMaterial
|
|
759
|
+
pipeRadiusM?: number
|
|
760
|
+
clearanceM?: number
|
|
761
|
+
allowedBands?: readonly CavityBand[]
|
|
762
|
+
/** Gravity drain minimum slope (in/ft) for `'gravity-slope'` mode. */
|
|
763
|
+
minSlopeInPerFt?: number
|
|
764
|
+
/** Fixture flood-level rim (m) for `'non-descending-vent'` mode (IPC 905.5). */
|
|
765
|
+
floodRimHeightM?: number
|
|
766
|
+
/** Minimum elevation (m) for horizontal legs (overrides `floodRimHeightM + 0.1524 m` when higher). */
|
|
767
|
+
minHorizontalElevationM?: number
|
|
768
|
+
/** Clearance below roof surface (m). Default `0.10`. */
|
|
769
|
+
roofClearanceM?: number
|
|
770
|
+
/** Prefer wall cavities over open plenum corridors when true. */
|
|
771
|
+
preferWallFollowing?: boolean
|
|
772
|
+
/** Foundation hint used for default cavity bands and sub-floor elevation. */
|
|
773
|
+
foundationType?: FoundationType
|
|
774
|
+
/** Sub-floor trench elevation (m) when compiling a cavity graph on demand. */
|
|
775
|
+
subFloorElevationM?: number
|
|
776
|
+
/** Optional extra obstacles (reserved into occupancy grid when `occupancy` is not pre-supplied). */
|
|
777
|
+
obstacles?: ReadonlyArray<PlumbingObstacle>
|
|
778
|
+
/** Reserve the resulting run in `occupancy` (`INV-MEP-07`). */
|
|
779
|
+
reserveOnCommit?: boolean
|
|
780
|
+
runId?: string
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
export type PlumbingCavityRouteResult = CavityRouteResult & {
|
|
784
|
+
readonly path: Vec3[]
|
|
785
|
+
readonly lengthFt: number
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
function hasCavityScene(ctx?: PlumbingCavityContext): boolean {
|
|
789
|
+
if (!ctx) return false
|
|
790
|
+
if (ctx.graph && ctx.graph.vertices.length > 0) return true
|
|
791
|
+
if (ctx.anatomy) return true
|
|
792
|
+
if (ctx.nodes && Object.keys(ctx.nodes).length > 0) return true
|
|
793
|
+
return false
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
function closestPointOnWalls2D(
|
|
797
|
+
x: number,
|
|
798
|
+
z: number,
|
|
799
|
+
walls: ReadonlyArray<WallSegment2D>,
|
|
800
|
+
): { wall: WallSegment2D; t: number; point: readonly [number, number]; distanceM: number } | null {
|
|
801
|
+
let best: { wall: WallSegment2D; t: number; point: readonly [number, number]; distanceM: number } | null = null
|
|
802
|
+
for (const wall of walls) {
|
|
803
|
+
const dx = x - wall.start[0]
|
|
804
|
+
const dz = z - wall.start[1]
|
|
805
|
+
const rawT = (dx * wall.ux + dz * wall.uz) / wall.lengthM
|
|
806
|
+
const t = Math.max(0, Math.min(1, rawT))
|
|
807
|
+
const px = wall.start[0] + wall.ux * wall.lengthM * t
|
|
808
|
+
const pz = wall.start[1] + wall.uz * wall.lengthM * t
|
|
809
|
+
const dist = Math.hypot(x - px, z - pz)
|
|
810
|
+
if (!best || dist < best.distanceM) {
|
|
811
|
+
best = { wall, t, point: [px, pz], distanceM: dist }
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
return best
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
const cachedAnatomyByNodes = new WeakMap<Readonly<Record<string, SceneNodeLike>>, BuildingAnatomy>()
|
|
818
|
+
|
|
819
|
+
function resolveAnatomyForContext(ctx?: PlumbingCavityContext): BuildingAnatomy | undefined {
|
|
820
|
+
if (!ctx) return undefined
|
|
821
|
+
if (ctx.anatomy) return ctx.anatomy
|
|
822
|
+
if (!ctx.nodes) return undefined
|
|
823
|
+
let cached = cachedAnatomyByNodes.get(ctx.nodes)
|
|
824
|
+
if (!cached) {
|
|
825
|
+
cached = compileBuildingAnatomy(ctx.nodes)
|
|
826
|
+
cachedAnatomyByNodes.set(ctx.nodes, cached)
|
|
827
|
+
}
|
|
828
|
+
return cached
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
function toWallSegment2D(w: CanonicalWall): WallSegment2D {
|
|
832
|
+
return {
|
|
833
|
+
id: w.id,
|
|
834
|
+
start: w.start,
|
|
835
|
+
end: w.end,
|
|
836
|
+
lengthM: w.lengthM,
|
|
837
|
+
thicknessM: w.thicknessM,
|
|
838
|
+
heightM: w.heightM,
|
|
839
|
+
ux: w.ux,
|
|
840
|
+
uz: w.uz,
|
|
841
|
+
nx: w.nx,
|
|
842
|
+
nz: w.nz,
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
function isSegmentAlongWalls(
|
|
847
|
+
a: Vec3,
|
|
848
|
+
b: Vec3,
|
|
849
|
+
walls: ReadonlyArray<WallSegment2D>,
|
|
850
|
+
): boolean {
|
|
851
|
+
const maxY = Math.max(a[1], b[1])
|
|
852
|
+
const horiz = Math.hypot(b[0] - a[0], b[2] - a[2])
|
|
853
|
+
if (horiz < 1e-4) {
|
|
854
|
+
const nw = closestPointOnWalls2D(a[0], a[2], walls)
|
|
855
|
+
return Boolean(
|
|
856
|
+
nw &&
|
|
857
|
+
nw.distanceM <= Math.max(0.14, nw.wall.thicknessM / 2 + 0.06) &&
|
|
858
|
+
maxY <= nw.wall.heightM + 0.05,
|
|
859
|
+
)
|
|
860
|
+
}
|
|
861
|
+
const samples = Math.max(4, Math.ceil(horiz / 0.25) + 1)
|
|
862
|
+
for (let s = 0; s <= samples; s++) {
|
|
863
|
+
const t = s / samples
|
|
864
|
+
const sx = a[0] + (b[0] - a[0]) * t
|
|
865
|
+
const sy = a[1] + (b[1] - a[1]) * t
|
|
866
|
+
const sz = a[2] + (b[2] - a[2]) * t
|
|
867
|
+
const nw = closestPointOnWalls2D(sx, sz, walls)
|
|
868
|
+
if (
|
|
869
|
+
!nw ||
|
|
870
|
+
nw.distanceM > Math.max(0.14, nw.wall.thicknessM / 2 + 0.06) ||
|
|
871
|
+
sy > nw.wall.heightM + 0.05
|
|
872
|
+
) {
|
|
873
|
+
return false
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
return true
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
function exceedsConcaveFootprint(
|
|
880
|
+
path: ReadonlyArray<Vec3>,
|
|
881
|
+
ctx: PlumbingCavityContext | undefined,
|
|
882
|
+
enforce: boolean,
|
|
883
|
+
): boolean {
|
|
884
|
+
if (!enforce || !ctx || path.length < 2) return false
|
|
885
|
+
const anatomy = resolveAnatomyForContext(ctx)
|
|
886
|
+
if (!anatomy) return false
|
|
887
|
+
const levelId = ctx.levelId ?? null
|
|
888
|
+
const rawWalls = anatomy.getWalls(levelId)
|
|
889
|
+
if (rawWalls.length === 0) return false
|
|
890
|
+
const footprint = anatomy.getFootprint(levelId)
|
|
891
|
+
// Only enforce concave void exclusion when the level actually has enclosed faces or room polygons
|
|
892
|
+
if (!footprint.boundedFacesComplete && footprint.roomPolygons.length === 0) return false
|
|
893
|
+
const wallSegs: WallSegment2D[] = rawWalls.map(toWallSegment2D)
|
|
894
|
+
for (let i = 1; i < path.length; i++) {
|
|
895
|
+
const a = path[i - 1]!
|
|
896
|
+
const b = path[i]!
|
|
897
|
+
// Sub-slab / underground trenches (y <= 0.05 m) do not float in mid-air over outdoor notches
|
|
898
|
+
if (a[1] <= 0.05 && b[1] <= 0.05) continue
|
|
899
|
+
// A segment traveling strictly inside solid building wall cavities is never floating over an outdoor notch
|
|
900
|
+
if (isSegmentAlongWalls(a, b, wallSegs)) continue
|
|
901
|
+
if (
|
|
902
|
+
!footprint.isSegmentInside(a, b, {
|
|
903
|
+
precedence: 'room-first',
|
|
904
|
+
wallToleranceM: 0.45,
|
|
905
|
+
skipExteriorEndpointMarginM: 0.45,
|
|
906
|
+
})
|
|
907
|
+
) {
|
|
908
|
+
return true
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
return false
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
function isVentPathCodeCompliant(path: ReadonlyArray<Vec3>, minHorizontalY: number): boolean {
|
|
915
|
+
if (path.length < 2) return false
|
|
916
|
+
for (let i = 1; i < path.length; i++) {
|
|
917
|
+
const a = path[i - 1]!
|
|
918
|
+
const b = path[i]!
|
|
919
|
+
// IPC 905.2: dry vent must never descend (zero inverted-U condensate traps)
|
|
920
|
+
if (b[1] < a[1] - 0.002) return false
|
|
921
|
+
// IPC 905.5: horizontal dry vent leg must remain >= 6" above flood-level rim
|
|
922
|
+
const horiz = Math.hypot(b[0] - a[0], b[2] - a[2])
|
|
923
|
+
if (horiz > 0.25 && Math.min(a[1], b[1]) < minHorizontalY - 0.02) return false
|
|
924
|
+
}
|
|
925
|
+
return true
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
function getWallsForCavityContext(ctx?: PlumbingCavityContext): WallSegment2D[] {
|
|
929
|
+
if (!ctx) return []
|
|
930
|
+
const anatomy = resolveAnatomyForContext(ctx)
|
|
931
|
+
if (anatomy) {
|
|
932
|
+
const canonical = anatomy.getWalls(ctx.levelId ?? null)
|
|
933
|
+
if (canonical.length > 0) {
|
|
934
|
+
return canonical.map(toWallSegment2D)
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
if (ctx.nodes) {
|
|
938
|
+
return extractWallSegments(ctx.nodes, ctx.levelId ?? null)
|
|
939
|
+
}
|
|
940
|
+
return []
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
function segmentsCross2D(
|
|
944
|
+
ax: number,
|
|
945
|
+
az: number,
|
|
946
|
+
bx: number,
|
|
947
|
+
bz: number,
|
|
948
|
+
cx: number,
|
|
949
|
+
cz: number,
|
|
950
|
+
dx: number,
|
|
951
|
+
dz: number,
|
|
952
|
+
): boolean {
|
|
953
|
+
const rX = bx - ax
|
|
954
|
+
const rZ = bz - az
|
|
955
|
+
const sX = dx - cx
|
|
956
|
+
const sZ = dz - cz
|
|
957
|
+
const denom = rX * sZ - rZ * sX
|
|
958
|
+
if (Math.abs(denom) < 1e-8) return false
|
|
959
|
+
const t = ((cx - ax) * sZ - (cz - az) * sX) / denom
|
|
960
|
+
const u = ((cx - ax) * rZ - (cz - az) * rX) / denom
|
|
961
|
+
return t > 0.02 && t < 0.98 && u > 0.01 && u < 0.99
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
const round4 = (v: number): number => Math.round(v * 10000) / 10000
|
|
965
|
+
|
|
966
|
+
function resolveBackingWallSnap(
|
|
967
|
+
pt: Vec3,
|
|
968
|
+
ctx: PlumbingCavityContext | undefined,
|
|
969
|
+
obstacles: ReadonlyArray<PlumbingObstacle>,
|
|
970
|
+
pipeRadiusM: number,
|
|
971
|
+
buildingBounds: BuildingBounds2D | undefined,
|
|
972
|
+
checkFootprint: boolean,
|
|
973
|
+
maxDistM = 0.85,
|
|
974
|
+
): Vec3 | null {
|
|
975
|
+
const walls = getWallsForCavityContext(ctx)
|
|
976
|
+
if (walls.length === 0) return null
|
|
977
|
+
const nw = closestPointOnWalls2D(pt[0], pt[2], walls)
|
|
978
|
+
if (!nw || nw.distanceM <= 0.03 || nw.distanceM > maxDistM) return null
|
|
979
|
+
const snap: Vec3 = [round4(nw.point[0]), pt[1], round4(nw.point[1])]
|
|
980
|
+
const stub: Vec3[] = [pt, snap]
|
|
981
|
+
// Allow exterior wall centerlines right on buildingBounds (margin = 0 + 0.05m tolerance)
|
|
982
|
+
if (buildingBounds) {
|
|
983
|
+
const wallEnv = resolveAllowedEnvelope(buildingBounds, pt, [snap[0], snap[2]], -0.05)
|
|
984
|
+
if (exceedsEnvelopeXZ(stub, wallEnv)) return null
|
|
985
|
+
}
|
|
986
|
+
if (exceedsConcaveFootprint(stub, ctx, checkFootprint)) return null
|
|
987
|
+
if (detectPlumbingClashes(stub, obstacles, pipeRadiusM).length > 0) return null
|
|
988
|
+
for (const w of walls) {
|
|
989
|
+
if (w.id === nw.wall.id) continue
|
|
990
|
+
if (segmentsCross2D(pt[0], pt[2], snap[0], snap[2], w.start[0], w.start[1], w.end[0], w.end[1])) {
|
|
991
|
+
return null
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
return snap
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
function resolveWallHeightForContext(ctx: PlumbingCavityContext | undefined, fallback = 2.75): number {
|
|
998
|
+
const walls = getWallsForCavityContext(ctx)
|
|
999
|
+
if (walls.length === 0) return fallback
|
|
1000
|
+
let minH = Infinity
|
|
1001
|
+
for (const w of walls) {
|
|
1002
|
+
if (Number.isFinite(w.heightM) && w.heightM > 1.5 && w.heightM < 8) {
|
|
1003
|
+
if (w.heightM < minH) minH = w.heightM
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
return Number.isFinite(minH) ? minH : fallback
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
function remapOverheadHorizontalBand(
|
|
1010
|
+
path: ReadonlyArray<Vec3>,
|
|
1011
|
+
targetY: number,
|
|
1012
|
+
minHorizY = 1.9,
|
|
1013
|
+
remapFinalHorizontalEndpoint = false,
|
|
1014
|
+
): Vec3[] {
|
|
1015
|
+
if (path.length < 2) return [...path]
|
|
1016
|
+
const out: Vec3[] = path.map((p) => [...p] as Vec3)
|
|
1017
|
+
const rTargetY = round4(targetY)
|
|
1018
|
+
for (let i = 1; i < out.length - 1; i++) {
|
|
1019
|
+
const p = out[i]!
|
|
1020
|
+
const prev = path[i - 1]!
|
|
1021
|
+
const next = path[i + 1]!
|
|
1022
|
+
const prevHoriz = Math.hypot(p[0] - prev[0], p[2] - prev[2]) > 0.03
|
|
1023
|
+
const nextHoriz = Math.hypot(next[0] - p[0], next[2] - p[2]) > 0.03
|
|
1024
|
+
if (p[1] >= minHorizY && (prevHoriz || nextHoriz)) {
|
|
1025
|
+
out[i] = [p[0], rTargetY, p[2]]
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
// For dry vents terminating horizontally into the main vent stack (IPC 905.2), also lift the final
|
|
1029
|
+
// endpoint so raising the penultimate vertex never introduces a downward terminal drop.
|
|
1030
|
+
if (remapFinalHorizontalEndpoint && out.length >= 2) {
|
|
1031
|
+
const lastIdx = out.length - 1
|
|
1032
|
+
const prev = path[lastIdx - 1]!
|
|
1033
|
+
const last = path[lastIdx]!
|
|
1034
|
+
const finalHoriz = Math.hypot(last[0] - prev[0], last[2] - prev[2]) > 0.03
|
|
1035
|
+
if (finalHoriz && last[1] >= minHorizY) {
|
|
1036
|
+
out[lastIdx] = [last[0], Math.max(last[1], rTargetY), last[2]]
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
return cleanPath(out)
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
const defaultCavityGraphCache = new WeakMap<BuildingAnatomy, Map<string, LegalCavityGraph>>()
|
|
1043
|
+
|
|
1044
|
+
function getOrCompileDefaultCavityGraph(
|
|
1045
|
+
anatomy: BuildingAnatomy,
|
|
1046
|
+
levelId: string | null,
|
|
1047
|
+
opts: {
|
|
1048
|
+
roofClearanceM: number
|
|
1049
|
+
pipeRadiusM: number
|
|
1050
|
+
subFloorElevationM: number
|
|
1051
|
+
},
|
|
1052
|
+
): LegalCavityGraph {
|
|
1053
|
+
let byKey = defaultCavityGraphCache.get(anatomy)
|
|
1054
|
+
if (!byKey) {
|
|
1055
|
+
byKey = new Map()
|
|
1056
|
+
defaultCavityGraphCache.set(anatomy, byKey)
|
|
1057
|
+
}
|
|
1058
|
+
const key = `${levelId === null ? '__all__' : JSON.stringify(levelId)}|${opts.subFloorElevationM}|${opts.roofClearanceM}|${opts.pipeRadiusM}`
|
|
1059
|
+
let graph = byKey.get(key)
|
|
1060
|
+
if (!graph) {
|
|
1061
|
+
graph = compileLegalCavityGraph(anatomy, levelId, {
|
|
1062
|
+
roofClearanceM: opts.roofClearanceM,
|
|
1063
|
+
halfY: opts.pipeRadiusM,
|
|
1064
|
+
halfXZ: opts.pipeRadiusM,
|
|
1065
|
+
subFloorElevationM: opts.subFloorElevationM,
|
|
1066
|
+
})
|
|
1067
|
+
byKey.set(key, graph)
|
|
1068
|
+
}
|
|
1069
|
+
return graph
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
/**
|
|
1073
|
+
* Layer 2 — Legal-Space-First Plumbing Cavity Router (`@kruschdev/pascal-mep-kernel/router`).
|
|
1074
|
+
*
|
|
1075
|
+
* Routes plumbing pipes strictly inside pre-compiled legal building cavities (solid wall stud bays
|
|
1076
|
+
* minus 3D door/window prisms, enclosed overhead plenums clamped below 7-type roof surfaces, and
|
|
1077
|
+
* under-slab/crawlspace trenches) with strict physics enforcement:
|
|
1078
|
+
* - `'gravity-slope'`: DWV drains with strict monotonic `minSlopeInPerFt` fall (IPC 704.1)
|
|
1079
|
+
* - `'non-descending-vent'`: Dry re-vents obeying IPC 905.5 (`floodRim + 0.1524 m`) & IPC 905.2 (zero inverted-U)
|
|
1080
|
+
* - `'orthogonal' | 'euclidean-cavity'`: Pressurized supply, fuel gas, and dryer vent runs avoiding
|
|
1081
|
+
* structural beams/columns, NEC 110.26(E) dedicated spaces, outdoor voids, and roof surfaces.
|
|
1082
|
+
*/
|
|
1083
|
+
export function routePlumbingViaLegalCavity(
|
|
1084
|
+
params: RoutePlumbingViaLegalCavityParams,
|
|
1085
|
+
): PlumbingCavityRouteResult {
|
|
1086
|
+
const {
|
|
1087
|
+
from,
|
|
1088
|
+
to,
|
|
1089
|
+
mode,
|
|
1090
|
+
nominalSize,
|
|
1091
|
+
material = mode === 'gravity-slope' || mode === 'non-descending-vent' ? 'pvc-dwv' : 'pex-a',
|
|
1092
|
+
clearanceM = 0.02,
|
|
1093
|
+
roofClearanceM = 0.1,
|
|
1094
|
+
preferWallFollowing = false,
|
|
1095
|
+
foundationType = 'crawlspace',
|
|
1096
|
+
obstacles,
|
|
1097
|
+
reserveOnCommit,
|
|
1098
|
+
runId,
|
|
1099
|
+
} = params
|
|
1100
|
+
|
|
1101
|
+
const levelId = params.levelId ?? null
|
|
1102
|
+
const anatomy = params.anatomy ?? compileBuildingAnatomy(params.nodes ?? {})
|
|
1103
|
+
const pipeRadiusM =
|
|
1104
|
+
params.pipeRadiusM ?? (nominalSize ? getPipeOuterDiameterM(nominalSize, material) / 2 : 0.025)
|
|
1105
|
+
|
|
1106
|
+
const defaultSubFloorY =
|
|
1107
|
+
params.subFloorElevationM ??
|
|
1108
|
+
(mode === 'gravity-slope'
|
|
1109
|
+
? Math.min(foundationType === 'slab' ? -0.35 : -0.24, Math.max(-0.45, Math.min(from[1], to[1])))
|
|
1110
|
+
: foundationType === 'slab'
|
|
1111
|
+
? -0.35
|
|
1112
|
+
: -0.24)
|
|
1113
|
+
|
|
1114
|
+
const graph =
|
|
1115
|
+
params.graph ??
|
|
1116
|
+
getOrCompileDefaultCavityGraph(anatomy, levelId, {
|
|
1117
|
+
roofClearanceM,
|
|
1118
|
+
pipeRadiusM,
|
|
1119
|
+
subFloorElevationM: defaultSubFloorY,
|
|
1120
|
+
})
|
|
1121
|
+
|
|
1122
|
+
let occupancy = params.occupancy
|
|
1123
|
+
if (!occupancy) {
|
|
1124
|
+
occupancy = anatomy.createOccupancyGrid(levelId, { forTrade: 'plumbing' })
|
|
1125
|
+
if (obstacles && obstacles.length > 0) {
|
|
1126
|
+
const existingIds = new Set<string>()
|
|
1127
|
+
for (const o of occupancy.obstacles) {
|
|
1128
|
+
existingIds.add(o.id)
|
|
1129
|
+
existingIds.add(o.nodeId)
|
|
1130
|
+
}
|
|
1131
|
+
for (const obs of obstacles) {
|
|
1132
|
+
const key = obs.nodeId ?? obs.id
|
|
1133
|
+
if (existingIds.has(obs.id) || existingIds.has(key)) continue
|
|
1134
|
+
occupancy.reserveBox({
|
|
1135
|
+
id: obs.id,
|
|
1136
|
+
nodeId: key,
|
|
1137
|
+
trade: obs.category === 'electrical-dedicated-space' ? 'electrical' : 'structural',
|
|
1138
|
+
kind:
|
|
1139
|
+
obs.category === 'electrical-dedicated-space'
|
|
1140
|
+
? 'electrical-dedicated-space'
|
|
1141
|
+
: obs.structuralKind === 'column'
|
|
1142
|
+
? 'structural-column'
|
|
1143
|
+
: 'structural-beam',
|
|
1144
|
+
aabb: obs.box,
|
|
1145
|
+
clearanceM: 0.02,
|
|
1146
|
+
label: obs.label,
|
|
1147
|
+
})
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
let effectiveTo: Vec3 = to
|
|
1153
|
+
let minSlopeInPerFt: number | undefined
|
|
1154
|
+
let minHorizontalElevationM: number | undefined = params.minHorizontalElevationM
|
|
1155
|
+
let allowedBands: readonly CavityBand[]
|
|
1156
|
+
|
|
1157
|
+
if (mode === 'gravity-slope') {
|
|
1158
|
+
minSlopeInPerFt =
|
|
1159
|
+
params.minSlopeInPerFt ??
|
|
1160
|
+
(nominalSize ? getMinRequiredDrainSlopeInPerFt(nominalSize) : IPC_RULE_THRESHOLDS.drainSlopeSmallInPerFt)
|
|
1161
|
+
allowedBands = params.allowedBands ?? ['under-slab', 'crawlspace', 'under-sill', 'mid-wall']
|
|
1162
|
+
const allowsSubFloor = allowedBands.includes('under-slab') || allowedBands.includes('crawlspace')
|
|
1163
|
+
if (allowsSubFloor && graph.subFloorY !== null && effectiveTo[1] > graph.subFloorY) {
|
|
1164
|
+
effectiveTo = [effectiveTo[0], graph.subFloorY, effectiveTo[2]]
|
|
1165
|
+
}
|
|
1166
|
+
} else if (mode === 'non-descending-vent') {
|
|
1167
|
+
const statutoryFloor =
|
|
1168
|
+
params.floodRimHeightM !== undefined
|
|
1169
|
+
? params.floodRimHeightM + IPC_RULE_THRESHOLDS.ventMinAboveFloodRimM
|
|
1170
|
+
: undefined
|
|
1171
|
+
const conservativeFloor =
|
|
1172
|
+
statutoryFloor !== undefined
|
|
1173
|
+
? statutoryFloor + IPC_RULE_THRESHOLDS.ventPlacementMarginM
|
|
1174
|
+
: undefined
|
|
1175
|
+
if (statutoryFloor !== undefined) {
|
|
1176
|
+
if (effectiveTo[1] < statutoryFloor) {
|
|
1177
|
+
effectiveTo = [effectiveTo[0], statutoryFloor, effectiveTo[2]]
|
|
1178
|
+
}
|
|
1179
|
+
const targetFloor = Math.max(statutoryFloor, Math.min(effectiveTo[1], conservativeFloor!))
|
|
1180
|
+
minHorizontalElevationM =
|
|
1181
|
+
minHorizontalElevationM !== undefined ? Math.max(minHorizontalElevationM, targetFloor) : targetFloor
|
|
1182
|
+
}
|
|
1183
|
+
allowedBands = params.allowedBands ?? ['mid-wall', 'top-plate', 'overhead-plenum', 'attic']
|
|
1184
|
+
} else {
|
|
1185
|
+
allowedBands =
|
|
1186
|
+
params.allowedBands ??
|
|
1187
|
+
(foundationType === 'slab'
|
|
1188
|
+
? ['overhead-plenum', 'attic', 'top-plate', 'mid-wall', 'under-sill']
|
|
1189
|
+
: ['under-slab', 'crawlspace', 'under-sill', 'mid-wall', 'top-plate', 'overhead-plenum', 'attic'])
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
const result = routeInLegalCavities({
|
|
1193
|
+
anatomy,
|
|
1194
|
+
graph,
|
|
1195
|
+
occupancy,
|
|
1196
|
+
request: {
|
|
1197
|
+
trade: 'plumbing',
|
|
1198
|
+
from,
|
|
1199
|
+
to: effectiveTo,
|
|
1200
|
+
halfXZ: pipeRadiusM,
|
|
1201
|
+
halfY: pipeRadiusM,
|
|
1202
|
+
clearanceM,
|
|
1203
|
+
allowedBands,
|
|
1204
|
+
mode,
|
|
1205
|
+
...(minSlopeInPerFt !== undefined ? { minSlopeInPerFt } : {}),
|
|
1206
|
+
...(minHorizontalElevationM !== undefined ? { minHorizontalElevationM } : {}),
|
|
1207
|
+
roofClearanceM,
|
|
1208
|
+
preferWallFollowing,
|
|
1209
|
+
...(reserveOnCommit !== undefined ? { reserveOnCommit } : {}),
|
|
1210
|
+
...(runId !== undefined ? { runId } : {}),
|
|
1211
|
+
},
|
|
1212
|
+
})
|
|
1213
|
+
|
|
1214
|
+
const cleanedPath = cleanPath([...result.path])
|
|
1215
|
+
return {
|
|
1216
|
+
...result,
|
|
1217
|
+
path: cleanedPath,
|
|
1218
|
+
lengthFt: round2(polylineLengthM(cleanedPath) * METERS_TO_FEET),
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1222
|
+
/**
|
|
1223
|
+
* Route a gravity-sloped DWV branch from a fixture rough-in to a stack/collector point.
|
|
1224
|
+
* Guarantees continuous downward fall at >= required IPC slope (1/4"/ft for <= 2.5", 1/8"/ft for >= 3")
|
|
1225
|
+
* and automatically detours horizontally around structural columns, beams, and NEC 110.26(E) electrical spaces.
|
|
1226
|
+
*/
|
|
1227
|
+
export function routeDwvDrainRun(params: {
|
|
1228
|
+
from: Vec3
|
|
1229
|
+
to: Vec3
|
|
1230
|
+
nominalSize: PipeNominalSize
|
|
1231
|
+
material?: PipeMaterial
|
|
1232
|
+
pipeRadiusM?: number
|
|
1233
|
+
foundationType?: FoundationType
|
|
1234
|
+
slopeInPerFt?: 0.125 | 0.25
|
|
1235
|
+
obstacles?: ReadonlyArray<PlumbingObstacle>
|
|
1236
|
+
buildingBounds?: BuildingBounds2D
|
|
1237
|
+
cavityContext?: PlumbingCavityContext
|
|
1238
|
+
}): {
|
|
1239
|
+
path: Vec3[]
|
|
1240
|
+
slopeInPerFt: number
|
|
1241
|
+
lengthFt: number
|
|
1242
|
+
detoured: boolean
|
|
1243
|
+
clashes: PlumbingObstacle[]
|
|
1244
|
+
} {
|
|
1245
|
+
const {
|
|
1246
|
+
from,
|
|
1247
|
+
to,
|
|
1248
|
+
nominalSize,
|
|
1249
|
+
material = 'pvc-dwv',
|
|
1250
|
+
foundationType = 'crawlspace',
|
|
1251
|
+
obstacles = [],
|
|
1252
|
+
buildingBounds,
|
|
1253
|
+
cavityContext,
|
|
1254
|
+
} = params
|
|
1255
|
+
const minSlope = getMinRequiredDrainSlopeInPerFt(nominalSize)
|
|
1256
|
+
const slopeInPerFt = Math.max(params.slopeInPerFt ?? 0.25, minSlope)
|
|
1257
|
+
const slopeRatio = slopeInPerFt / 12 // vertical drop per horizontal unit
|
|
1258
|
+
|
|
1259
|
+
const underFloorY = foundationType === 'slab' ? -0.35 : -0.24
|
|
1260
|
+
const startY = Math.min(from[1], underFloorY)
|
|
1261
|
+
const pipeRadiusM = (params.pipeRadiusM ?? getPipeOuterDiameterM(nominalSize, material) / 2) + 0.015
|
|
1262
|
+
const env = resolveAllowedEnvelope(buildingBounds, from, [to[0], to[2]])
|
|
1263
|
+
const checkFootprint = Boolean(buildingBounds) && hasCavityScene(cavityContext)
|
|
1264
|
+
|
|
1265
|
+
const buildSlopedCandidate = (
|
|
1266
|
+
horizWaypoints: ReadonlyArray<readonly [number, number]>,
|
|
1267
|
+
overrideStartY = startY,
|
|
1268
|
+
startJog?: readonly [number, number],
|
|
1269
|
+
): Vec3[] => {
|
|
1270
|
+
const pts: Vec3[] = [from]
|
|
1271
|
+
let curX = from[0]
|
|
1272
|
+
let curZ = from[2]
|
|
1273
|
+
let curY = overrideStartY
|
|
1274
|
+
if (startJog) {
|
|
1275
|
+
const jogLen = Math.hypot(startJog[0] - from[0], startJog[1] - from[2])
|
|
1276
|
+
const jogY = from[1] - jogLen * slopeRatio
|
|
1277
|
+
if (jogLen >= 0.02 && jogY >= overrideStartY + 0.01) {
|
|
1278
|
+
pts.push([startJog[0], jogY, startJog[1]])
|
|
1279
|
+
curX = startJog[0]
|
|
1280
|
+
curZ = startJog[1]
|
|
1281
|
+
if (jogY > overrideStartY + 0.02) {
|
|
1282
|
+
pts.push([curX, overrideStartY, curZ])
|
|
1283
|
+
curY = overrideStartY
|
|
1284
|
+
} else {
|
|
1285
|
+
curY = jogY
|
|
1286
|
+
}
|
|
1287
|
+
} else if (from[1] > overrideStartY + 0.02) {
|
|
1288
|
+
pts.push([from[0], overrideStartY, from[2]])
|
|
1289
|
+
}
|
|
1290
|
+
} else if (from[1] > overrideStartY + 0.02) {
|
|
1291
|
+
pts.push([from[0], overrideStartY, from[2]])
|
|
1292
|
+
}
|
|
1293
|
+
for (let i = 0; i < horizWaypoints.length; i++) {
|
|
1294
|
+
const [wx, wz] = horizWaypoints[i]!
|
|
1295
|
+
const leg = Math.hypot(wx - curX, wz - curZ)
|
|
1296
|
+
const isLast = i === horizWaypoints.length - 1
|
|
1297
|
+
if (!isLast && leg < 0.15) continue
|
|
1298
|
+
curY -= leg * slopeRatio
|
|
1299
|
+
pts.push([wx, curY, wz])
|
|
1300
|
+
if (isLast && to[1] < curY - 0.008) {
|
|
1301
|
+
pts.push([wx, to[1], wz])
|
|
1302
|
+
curY = to[1]
|
|
1303
|
+
}
|
|
1304
|
+
curX = wx
|
|
1305
|
+
curZ = wz
|
|
1306
|
+
}
|
|
1307
|
+
return cleanPath(pts)
|
|
1308
|
+
}
|
|
1309
|
+
|
|
1310
|
+
// Wall-drained fixtures (rough-in >= 0.25 m, e.g., lavatories, kitchen sinks, disposals, dishwashers, washers):
|
|
1311
|
+
// stub horizontally back into the backing wet-wall centerline at >= minSlopeInPerFt so the vertical drop
|
|
1312
|
+
// stays concealed inside the wall stud cavity instead of dropping through open room air.
|
|
1313
|
+
const wallDrainSnap =
|
|
1314
|
+
hasCavityScene(cavityContext) && from[1] >= 0.25
|
|
1315
|
+
? resolveBackingWallSnap(from, cavityContext, obstacles, pipeRadiusM, buildingBounds, checkFootprint, 0.24)
|
|
1316
|
+
: null
|
|
1317
|
+
const wallDrainJog: readonly [number, number] | undefined = wallDrainSnap
|
|
1318
|
+
? [wallDrainSnap[0], wallDrainSnap[2]]
|
|
1319
|
+
: undefined
|
|
1320
|
+
|
|
1321
|
+
const primary = buildSlopedCandidate(
|
|
1322
|
+
[
|
|
1323
|
+
[to[0], wallDrainJog ? wallDrainJog[1] : from[2]],
|
|
1324
|
+
[to[0], to[2]],
|
|
1325
|
+
],
|
|
1326
|
+
startY,
|
|
1327
|
+
wallDrainJog,
|
|
1328
|
+
)
|
|
1329
|
+
const drainEnv =
|
|
1330
|
+
wallDrainJog && buildingBounds
|
|
1331
|
+
? resolveAllowedEnvelope(buildingBounds, from, [to[0], to[2]], -0.05)
|
|
1332
|
+
: env
|
|
1333
|
+
let bestPath = primary
|
|
1334
|
+
let bestClashes = detectPlumbingClashes(primary, obstacles, pipeRadiusM)
|
|
1335
|
+
let bestBoundsViolation =
|
|
1336
|
+
exceedsEnvelopeXZ(primary, drainEnv) || exceedsConcaveFootprint(primary, cavityContext, checkFootprint)
|
|
1337
|
+
let detoured = false
|
|
1338
|
+
let underBeamY: number | null = null
|
|
1339
|
+
|
|
1340
|
+
if (bestClashes.length > 0 || bestBoundsViolation) {
|
|
1341
|
+
const tryCandidate = (cand: Vec3[]): boolean => {
|
|
1342
|
+
const boundsViol =
|
|
1343
|
+
exceedsEnvelopeXZ(cand, drainEnv) || exceedsConcaveFootprint(cand, cavityContext, checkFootprint)
|
|
1344
|
+
if (boundsViol && !bestBoundsViolation) return false
|
|
1345
|
+
const c = detectPlumbingClashes(cand, obstacles, pipeRadiusM)
|
|
1346
|
+
const better =
|
|
1347
|
+
(bestBoundsViolation && !boundsViol) ||
|
|
1348
|
+
(boundsViol === bestBoundsViolation && c.length < bestClashes.length)
|
|
1349
|
+
if (better) {
|
|
1350
|
+
bestPath = cand
|
|
1351
|
+
bestClashes = c
|
|
1352
|
+
bestBoundsViolation = boundsViol
|
|
1353
|
+
detoured = true
|
|
1354
|
+
}
|
|
1355
|
+
return bestClashes.length === 0 && !bestBoundsViolation
|
|
1356
|
+
}
|
|
1357
|
+
|
|
1358
|
+
// Layer 2 — Legal-Space-First Cavity Candidate (gravity-slope) when scene anatomy/graph is present
|
|
1359
|
+
if (hasCavityScene(cavityContext)) {
|
|
1360
|
+
const cav = routePlumbingViaLegalCavity({
|
|
1361
|
+
...cavityContext,
|
|
1362
|
+
from,
|
|
1363
|
+
to: [to[0], Math.min(to[1], underFloorY), to[2]],
|
|
1364
|
+
mode: 'gravity-slope',
|
|
1365
|
+
nominalSize,
|
|
1366
|
+
material,
|
|
1367
|
+
pipeRadiusM,
|
|
1368
|
+
foundationType,
|
|
1369
|
+
minSlopeInPerFt: slopeInPerFt,
|
|
1370
|
+
subFloorElevationM: underFloorY,
|
|
1371
|
+
obstacles,
|
|
1372
|
+
})
|
|
1373
|
+
if (cav.path.length >= 2) {
|
|
1374
|
+
const cavSlope = measureDrainPathSlope(cav.path)
|
|
1375
|
+
if (
|
|
1376
|
+
!cavSlope.hasUphillSegment &&
|
|
1377
|
+
(cavSlope.horizontalSegments === 0 || cavSlope.minSegmentSlopeInPerFt >= minSlope - 0.02)
|
|
1378
|
+
) {
|
|
1379
|
+
tryCandidate(cav.path)
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
1382
|
+
}
|
|
1383
|
+
|
|
1384
|
+
// Compute safe elevation below colliding (and deepest level) under-floor girders
|
|
1385
|
+
const allUnderBeams = obstacles.filter(isUnderFloorBeamObstacle)
|
|
1386
|
+
const structuralHits = bestClashes.filter(isUnderFloorBeamObstacle)
|
|
1387
|
+
const beamPool = structuralHits.length > 0 ? structuralHits : allUnderBeams
|
|
1388
|
+
if (beamPool.length > 0) {
|
|
1389
|
+
const lowestMinY = Math.min(...beamPool.map((o) => o.box.minY))
|
|
1390
|
+
underBeamY = Math.min(startY - 0.18, lowestMinY - pipeRadiusM - 0.06)
|
|
1391
|
+
}
|
|
1392
|
+
const allLowestMinY =
|
|
1393
|
+
allUnderBeams.length > 0 ? Math.min(...allUnderBeams.map((o) => o.box.minY)) : null
|
|
1394
|
+
const allUnderBeamY =
|
|
1395
|
+
allLowestMinY !== null ? Math.min(startY - 0.18, allLowestMinY - pipeRadiusM - 0.06) : null
|
|
1396
|
+
|
|
1397
|
+
if ((bestClashes.length > 0 || bestBoundsViolation) && underBeamY !== null) {
|
|
1398
|
+
for (const sy of allUnderBeamY !== null && allUnderBeamY < underBeamY - 0.01 ? [underBeamY, allUnderBeamY] : [underBeamY]) {
|
|
1399
|
+
for (const wp of [
|
|
1400
|
+
[[to[0], from[2]], [to[0], to[2]]] as const,
|
|
1401
|
+
[[from[0], to[2]], [to[0], to[2]]] as const,
|
|
1402
|
+
]) {
|
|
1403
|
+
if (tryCandidate(buildSlopedCandidate(wp, sy))) break
|
|
1404
|
+
}
|
|
1405
|
+
if (bestClashes.length === 0 && !bestBoundsViolation) break
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
|
|
1409
|
+
if (bestClashes.length > 0 || bestBoundsViolation) {
|
|
1410
|
+
const midX = (from[0] + to[0]) / 2
|
|
1411
|
+
const midZ = (from[2] + to[2]) / 2
|
|
1412
|
+
const edgePad = pipeRadiusM + 0.08
|
|
1413
|
+
const startYs = Array.from(
|
|
1414
|
+
new Set(
|
|
1415
|
+
[underBeamY, allUnderBeamY, startY].filter((v): v is number => v !== null),
|
|
1416
|
+
),
|
|
1417
|
+
)
|
|
1418
|
+
|
|
1419
|
+
// Check if the vertical drop from `from` directly collides with an under-floor girder or column
|
|
1420
|
+
// Probe to the deepest candidate startY so deeper beams below underBeamY are never missed.
|
|
1421
|
+
const dropProbeY = startYs.length > 0 ? Math.min(...startYs) : startY
|
|
1422
|
+
const dropHits =
|
|
1423
|
+
from[1] > dropProbeY + 0.02
|
|
1424
|
+
? detectPlumbingClashes([from, [from[0], dropProbeY, from[2]]], obstacles, pipeRadiusM)
|
|
1425
|
+
: []
|
|
1426
|
+
const startJogs: Array<readonly [number, number] | undefined> = [
|
|
1427
|
+
...(wallDrainJog ? [wallDrainJog] : []),
|
|
1428
|
+
undefined,
|
|
1429
|
+
...(dropHits.length > 0 ? buildLateralEscapeCandidates(from[0], from[2], dropHits, edgePad) : []),
|
|
1430
|
+
]
|
|
1431
|
+
|
|
1432
|
+
const baseCandidates: Array<Array<readonly [number, number]>> = [
|
|
1433
|
+
[[to[0], from[2]], [to[0], to[2]]],
|
|
1434
|
+
[[from[0], to[2]], [to[0], to[2]]],
|
|
1435
|
+
[[to[0], from[2] + 0.55], [to[0], to[2]]],
|
|
1436
|
+
[[to[0], from[2] - 0.55], [to[0], to[2]]],
|
|
1437
|
+
[[from[0] + 0.55, to[2]], [to[0], to[2]]],
|
|
1438
|
+
[[from[0] - 0.55, to[2]], [to[0], to[2]]],
|
|
1439
|
+
[[midX + 0.75, from[2] + 0.75], [to[0] + 0.75, to[2]], [to[0], to[2]]],
|
|
1440
|
+
[[midX - 0.75, from[2] - 0.75], [to[0] - 0.75, to[2]], [to[0], to[2]]],
|
|
1441
|
+
[[to[0] + 0.95, from[2]], [to[0] + 0.95, to[2]], [to[0], to[2]]],
|
|
1442
|
+
[[to[0] - 0.95, from[2]], [to[0] - 0.95, to[2]], [to[0], to[2]]],
|
|
1443
|
+
[[from[0], to[2] + 0.95], [to[0], to[2] + 0.95], [to[0], to[2]]],
|
|
1444
|
+
[[from[0], to[2] - 0.95], [to[0], to[2] - 0.95], [to[0], to[2]]],
|
|
1445
|
+
...buildObstacleDetourWaypoints([from[0], from[2]], [to[0], to[2]], bestClashes, edgePad),
|
|
1446
|
+
]
|
|
1447
|
+
|
|
1448
|
+
for (const jog of startJogs) {
|
|
1449
|
+
const jx = jog ? jog[0] : from[0]
|
|
1450
|
+
const jz = jog ? jog[1] : from[2]
|
|
1451
|
+
const wpList: Array<Array<readonly [number, number]>> = jog
|
|
1452
|
+
? [
|
|
1453
|
+
[[to[0], jz], [to[0], to[2]]],
|
|
1454
|
+
[[jx, to[2]], [to[0], to[2]]],
|
|
1455
|
+
...buildObstacleDetourWaypoints([jx, jz], [to[0], to[2]], bestClashes, edgePad),
|
|
1456
|
+
...baseCandidates,
|
|
1457
|
+
]
|
|
1458
|
+
: baseCandidates
|
|
1459
|
+
for (const sy of startYs) {
|
|
1460
|
+
for (const wp of wpList) {
|
|
1461
|
+
if (tryCandidate(buildSlopedCandidate(wp, sy, jog))) break
|
|
1462
|
+
}
|
|
1463
|
+
if (bestClashes.length === 0 && !bestBoundsViolation) break
|
|
1464
|
+
}
|
|
1465
|
+
if (bestClashes.length === 0 && !bestBoundsViolation) break
|
|
1466
|
+
}
|
|
1467
|
+
}
|
|
1468
|
+
}
|
|
1469
|
+
|
|
1470
|
+
return {
|
|
1471
|
+
path: bestPath,
|
|
1472
|
+
slopeInPerFt,
|
|
1473
|
+
lengthFt: round2(polylineLengthM(bestPath) * METERS_TO_FEET),
|
|
1474
|
+
detoured,
|
|
1475
|
+
clashes: bestClashes,
|
|
1476
|
+
}
|
|
1477
|
+
}
|
|
1478
|
+
|
|
1479
|
+
/**
|
|
1480
|
+
* Route a dry re-vent from a fixture wet wall up to >= 6" above flood-level rim (IPC 905.5)
|
|
1481
|
+
* and horizontally over to the main vent stack, detouring around structural headers, beams,
|
|
1482
|
+
* columns, and NEC 110.26(E) obstacles while keeping strictly non-descending elevation (IPC 905.2).
|
|
1483
|
+
*/
|
|
1484
|
+
export function routeVentRun(params: {
|
|
1485
|
+
from: Vec3
|
|
1486
|
+
stackPoint: readonly [number, number]
|
|
1487
|
+
floodRimHeightM: number
|
|
1488
|
+
ceilingHeightM?: number
|
|
1489
|
+
nominalSize?: PipeNominalSize
|
|
1490
|
+
material?: PipeMaterial
|
|
1491
|
+
pipeRadiusM?: number
|
|
1492
|
+
obstacles?: ReadonlyArray<PlumbingObstacle>
|
|
1493
|
+
buildingBounds?: BuildingBounds2D
|
|
1494
|
+
maxRoofYAt?: (x: number, z: number) => number
|
|
1495
|
+
cavityContext?: PlumbingCavityContext
|
|
1496
|
+
}): {
|
|
1497
|
+
path: Vec3[]
|
|
1498
|
+
tieInHeightM: number
|
|
1499
|
+
lengthFt: number
|
|
1500
|
+
detoured: boolean
|
|
1501
|
+
clashes: PlumbingObstacle[]
|
|
1502
|
+
} {
|
|
1503
|
+
const {
|
|
1504
|
+
from,
|
|
1505
|
+
stackPoint,
|
|
1506
|
+
floodRimHeightM,
|
|
1507
|
+
ceilingHeightM = 2.6,
|
|
1508
|
+
nominalSize,
|
|
1509
|
+
material = 'pvc-dwv',
|
|
1510
|
+
obstacles = [],
|
|
1511
|
+
buildingBounds,
|
|
1512
|
+
maxRoofYAt,
|
|
1513
|
+
cavityContext,
|
|
1514
|
+
} = params
|
|
1515
|
+
const pipeRadiusM =
|
|
1516
|
+
params.pipeRadiusM ?? (nominalSize ? getPipeOuterDiameterM(nominalSize, material) / 2 : 0.03)
|
|
1517
|
+
const env = resolveAllowedEnvelope(buildingBounds, from, stackPoint)
|
|
1518
|
+
const checkFootprint = Boolean(buildingBounds) && hasCavityScene(cavityContext)
|
|
1519
|
+
// IPC 905.5 (from IPC_DECLARATIVE_RULES): horizontal vent tie-in MUST be >= 6" (0.1524 m) above flood-level rim
|
|
1520
|
+
// IPC 905.2: dry vents must never descend below `from[1]` (zero inverted-U condensate traps)
|
|
1521
|
+
const ipcMinVentY = Math.max(from[1], floodRimHeightM + IPC_RULE_THRESHOLDS.ventMinAboveFloodRimM + 0.01)
|
|
1522
|
+
const minHorizontalVentY = Math.max(from[1], floodRimHeightM + IPC_RULE_THRESHOLDS.ventPlacementMinAboveFloodRimM)
|
|
1523
|
+
const minTieInY = Math.max(from[1], floodRimHeightM + IPC_RULE_THRESHOLDS.ventMinAboveFloodRimM + 0.1)
|
|
1524
|
+
const maxSafeCeilingY = ceilingHeightM - 0.08 - pipeRadiusM
|
|
1525
|
+
const rawTieInHeightM = Math.max(minTieInY, Math.min(ceilingHeightM - 0.15, maxSafeCeilingY))
|
|
1526
|
+
const minEndpointRoofCap = maxRoofYAt
|
|
1527
|
+
? Math.min(maxRoofYAt(from[0], from[2]), maxRoofYAt(stackPoint[0], stackPoint[1])) - pipeRadiusM - 0.09
|
|
1528
|
+
: Infinity
|
|
1529
|
+
const tieInHeightM = Number.isFinite(minEndpointRoofCap)
|
|
1530
|
+
? Math.max(ipcMinVentY, Math.min(rawTieInHeightM, minEndpointRoofCap))
|
|
1531
|
+
: rawTieInHeightM
|
|
1532
|
+
|
|
1533
|
+
const buildVentCandidate = (
|
|
1534
|
+
waypoints: ReadonlyArray<readonly [number, number]>,
|
|
1535
|
+
tieY = tieInHeightM,
|
|
1536
|
+
riserJog?: { x: number; z: number; jogY: number },
|
|
1537
|
+
): Vec3[] => {
|
|
1538
|
+
const safeTieY = Math.max(from[1], tieY)
|
|
1539
|
+
// Clamp jog elevation into [from[1], safeTieY] so dry vents never descend (IPC 905.2)
|
|
1540
|
+
const jy = riserJog ? Math.max(from[1], Math.min(riserJog.jogY, safeTieY)) : safeTieY
|
|
1541
|
+
const pts: Vec3[] = riserJog
|
|
1542
|
+
? [
|
|
1543
|
+
from,
|
|
1544
|
+
[from[0], jy, from[2]],
|
|
1545
|
+
[riserJog.x, jy, riserJog.z],
|
|
1546
|
+
...(safeTieY > jy + 0.008 ? [[riserJog.x, safeTieY, riserJog.z] as Vec3] : []),
|
|
1547
|
+
]
|
|
1548
|
+
: [from, [from[0], safeTieY, from[2]]]
|
|
1549
|
+
for (const [wx, wz] of waypoints) {
|
|
1550
|
+
pts.push([wx, safeTieY, wz])
|
|
1551
|
+
}
|
|
1552
|
+
return cleanPath(pts)
|
|
1553
|
+
}
|
|
1554
|
+
|
|
1555
|
+
const ventWalls =
|
|
1556
|
+
nominalSize && cavityContext?.nodes
|
|
1557
|
+
? extractWallSegments(cavityContext.nodes, cavityContext.levelId ?? null)
|
|
1558
|
+
: []
|
|
1559
|
+
const ventBones =
|
|
1560
|
+
ventWalls.length > 0 && cavityContext?.nodes
|
|
1561
|
+
? extractBonesPlumbingContext(cavityContext.nodes, cavityContext.levelId ?? null)
|
|
1562
|
+
: null
|
|
1563
|
+
const countIllegalStudBores = (cand: ReadonlyArray<Vec3>): number => {
|
|
1564
|
+
if (!nominalSize || ventWalls.length === 0) return 0
|
|
1565
|
+
const pens = computePipeFramingPenetrations({
|
|
1566
|
+
pipeRunId: 'vent_probe',
|
|
1567
|
+
path: cand,
|
|
1568
|
+
pipeSize: nominalSize,
|
|
1569
|
+
material,
|
|
1570
|
+
walls: ventWalls,
|
|
1571
|
+
bonesContext: ventBones,
|
|
1572
|
+
})
|
|
1573
|
+
let bad = 0
|
|
1574
|
+
for (const p of pens) {
|
|
1575
|
+
if (p.memberKind === 'stud' && !p.codeCompliant) bad += 1
|
|
1576
|
+
}
|
|
1577
|
+
return bad
|
|
1578
|
+
}
|
|
1579
|
+
|
|
1580
|
+
const primary = buildVentCandidate([
|
|
1581
|
+
[stackPoint[0], from[2]],
|
|
1582
|
+
[stackPoint[0], stackPoint[1]],
|
|
1583
|
+
])
|
|
1584
|
+
let bestPath = primary
|
|
1585
|
+
let bestClashes = detectPlumbingClashes(primary, obstacles, pipeRadiusM)
|
|
1586
|
+
let bestBoundsViolation =
|
|
1587
|
+
exceedsEnvelopeXZ(primary, env) || exceedsConcaveFootprint(primary, cavityContext, checkFootprint)
|
|
1588
|
+
let bestRoofViolation = exceedsRoofPlane(primary, pipeRadiusM, maxRoofYAt)
|
|
1589
|
+
let bestStudBores = countIllegalStudBores(primary)
|
|
1590
|
+
let bestTieInY = tieInHeightM
|
|
1591
|
+
let detoured = false
|
|
1592
|
+
|
|
1593
|
+
// Wall-Cavity-First Vent Concealment: when scene walls/cavity graph are present, try routing
|
|
1594
|
+
// along connected top-plate wall cavities first (strictly < wallH so 100% hidden in Full Walls mode
|
|
1595
|
+
// and >= wallH - 0.08 so zero IRC R602.6 horizontal stud-bore errors!) before open-room L-shapes.
|
|
1596
|
+
if (hasCavityScene(cavityContext)) {
|
|
1597
|
+
const wallH = resolveWallHeightForContext(cavityContext, ceilingHeightM)
|
|
1598
|
+
const desiredTopChaseY = round4(
|
|
1599
|
+
Math.max(
|
|
1600
|
+
minHorizontalVentY,
|
|
1601
|
+
Math.min(wallH - 0.055, Number.isFinite(minEndpointRoofCap) ? minEndpointRoofCap : wallH - 0.055),
|
|
1602
|
+
),
|
|
1603
|
+
)
|
|
1604
|
+
let adoptedWallVent = false
|
|
1605
|
+
for (const bandSet of [['top-plate'] as const, ['top-plate', 'overhead-plenum', 'attic'] as const]) {
|
|
1606
|
+
if (adoptedWallVent) break
|
|
1607
|
+
const cavWall = routePlumbingViaLegalCavity({
|
|
1608
|
+
...cavityContext,
|
|
1609
|
+
from,
|
|
1610
|
+
to: [stackPoint[0], desiredTopChaseY, stackPoint[1]],
|
|
1611
|
+
mode: 'non-descending-vent',
|
|
1612
|
+
nominalSize,
|
|
1613
|
+
material,
|
|
1614
|
+
pipeRadiusM,
|
|
1615
|
+
floodRimHeightM,
|
|
1616
|
+
minHorizontalElevationM: minHorizontalVentY,
|
|
1617
|
+
preferWallFollowing: true,
|
|
1618
|
+
allowedBands: bandSet,
|
|
1619
|
+
// Allow top-plate topology discovery under low/default eaves before remapping to desiredTopChaseY
|
|
1620
|
+
// and enforcing full roof clearance via `exceedsRoofPlane` below.
|
|
1621
|
+
...(bandSet.length === 1 && bandSet[0] === 'top-plate'
|
|
1622
|
+
? { graph: undefined, roofClearanceM: 0.01 }
|
|
1623
|
+
: {}),
|
|
1624
|
+
obstacles,
|
|
1625
|
+
})
|
|
1626
|
+
if (cavWall.path.length >= 2) {
|
|
1627
|
+
const remapped =
|
|
1628
|
+
desiredTopChaseY <= wallH - 0.04 && desiredTopChaseY >= minHorizontalVentY
|
|
1629
|
+
? remapOverheadHorizontalBand(cavWall.path, desiredTopChaseY, minHorizontalVentY - 0.05, true)
|
|
1630
|
+
: cavWall.path
|
|
1631
|
+
const wallEnv = buildingBounds ? resolveAllowedEnvelope(buildingBounds, from, stackPoint, -0.05) : env
|
|
1632
|
+
for (const cand of [remapped, cavWall.path]) {
|
|
1633
|
+
if (
|
|
1634
|
+
isVentPathCodeCompliant(cand, floodRimHeightM + IPC_RULE_THRESHOLDS.ventMinAboveFloodRimM) &&
|
|
1635
|
+
!exceedsEnvelopeXZ(cand, wallEnv) &&
|
|
1636
|
+
!exceedsConcaveFootprint(cand, cavityContext, checkFootprint) &&
|
|
1637
|
+
!exceedsRoofPlane(cand, pipeRadiusM, maxRoofYAt) &&
|
|
1638
|
+
countIllegalStudBores(cand) === 0 &&
|
|
1639
|
+
detectPlumbingClashes(cand, obstacles, pipeRadiusM).length === 0
|
|
1640
|
+
) {
|
|
1641
|
+
if (bestClashes.length > 0 || bestBoundsViolation || bestRoofViolation || bestStudBores > 0) {
|
|
1642
|
+
detoured = true
|
|
1643
|
+
}
|
|
1644
|
+
bestPath = cand
|
|
1645
|
+
bestClashes = []
|
|
1646
|
+
bestBoundsViolation = false
|
|
1647
|
+
bestRoofViolation = false
|
|
1648
|
+
bestStudBores = 0
|
|
1649
|
+
bestTieInY = cand[cand.length - 1]![1]
|
|
1650
|
+
adoptedWallVent = true
|
|
1651
|
+
break
|
|
1652
|
+
}
|
|
1653
|
+
}
|
|
1654
|
+
}
|
|
1655
|
+
}
|
|
1656
|
+
}
|
|
1657
|
+
|
|
1658
|
+
const ventDetourEnv =
|
|
1659
|
+
buildingBounds && hasCavityScene(cavityContext)
|
|
1660
|
+
? resolveAllowedEnvelope(buildingBounds, from, stackPoint, -0.05)
|
|
1661
|
+
: env
|
|
1662
|
+
if (bestClashes.length > 0 || bestBoundsViolation || bestRoofViolation || bestStudBores > 0) {
|
|
1663
|
+
const tryVent = (cand: Vec3[], tieY: number): boolean => {
|
|
1664
|
+
const boundsViol =
|
|
1665
|
+
exceedsEnvelopeXZ(cand, ventDetourEnv) || exceedsConcaveFootprint(cand, cavityContext, checkFootprint)
|
|
1666
|
+
const roofViol = exceedsRoofPlane(cand, pipeRadiusM, maxRoofYAt)
|
|
1667
|
+
if (boundsViol && !bestBoundsViolation) return false
|
|
1668
|
+
if (roofViol && !bestRoofViolation && boundsViol === bestBoundsViolation) return false
|
|
1669
|
+
const c = detectPlumbingClashes(cand, obstacles, pipeRadiusM)
|
|
1670
|
+
if (
|
|
1671
|
+
boundsViol === bestBoundsViolation &&
|
|
1672
|
+
roofViol === bestRoofViolation &&
|
|
1673
|
+
c.length > bestClashes.length
|
|
1674
|
+
) {
|
|
1675
|
+
return false
|
|
1676
|
+
}
|
|
1677
|
+
const studBores = countIllegalStudBores(cand)
|
|
1678
|
+
const better =
|
|
1679
|
+
(bestBoundsViolation && !boundsViol) ||
|
|
1680
|
+
(boundsViol === bestBoundsViolation && bestRoofViolation && !roofViol) ||
|
|
1681
|
+
(boundsViol === bestBoundsViolation &&
|
|
1682
|
+
roofViol === bestRoofViolation &&
|
|
1683
|
+
c.length < bestClashes.length) ||
|
|
1684
|
+
(boundsViol === bestBoundsViolation &&
|
|
1685
|
+
roofViol === bestRoofViolation &&
|
|
1686
|
+
c.length === bestClashes.length &&
|
|
1687
|
+
studBores < bestStudBores)
|
|
1688
|
+
if (better) {
|
|
1689
|
+
bestPath = cand
|
|
1690
|
+
bestClashes = c
|
|
1691
|
+
bestBoundsViolation = boundsViol
|
|
1692
|
+
bestRoofViolation = roofViol
|
|
1693
|
+
bestStudBores = studBores
|
|
1694
|
+
bestTieInY = tieY
|
|
1695
|
+
detoured = true
|
|
1696
|
+
}
|
|
1697
|
+
return bestClashes.length === 0 && !bestBoundsViolation && !bestRoofViolation && bestStudBores === 0
|
|
1698
|
+
}
|
|
1699
|
+
|
|
1700
|
+
const edgePad = pipeRadiusM + 0.07
|
|
1701
|
+
const maxAtticTieY = ceilingHeightM + 0.36
|
|
1702
|
+
const elevatedHits = bestClashes.filter(isElevatedBeamObstacle)
|
|
1703
|
+
const allElevatedBeams = obstacles.filter(isElevatedBeamObstacle)
|
|
1704
|
+
|
|
1705
|
+
const primaryMinRoofCap = maxRoofYAt
|
|
1706
|
+
? minRoofAlongPath(primary, maxRoofYAt) - pipeRadiusM - 0.09
|
|
1707
|
+
: Infinity
|
|
1708
|
+
const altPrimary = buildVentCandidate([
|
|
1709
|
+
[from[0], stackPoint[1]],
|
|
1710
|
+
[stackPoint[0], stackPoint[1]],
|
|
1711
|
+
])
|
|
1712
|
+
const altMinRoofCap = maxRoofYAt
|
|
1713
|
+
? minRoofAlongPath(altPrimary, maxRoofYAt) - pipeRadiusM - 0.09
|
|
1714
|
+
: Infinity
|
|
1715
|
+
|
|
1716
|
+
// Candidate attic/upper-wall tie-in elevations:
|
|
1717
|
+
// 1. Default tieInHeightM
|
|
1718
|
+
// 2. Hop cleanly ABOVE wall headers into ceiling/attic space (while remaining strictly under roof envelope)
|
|
1719
|
+
// 3. Duck cleanly BELOW a high ceiling/ridge beam or low/flat explicit roof segment while staying >= 6" above flood rim (IPC 905.5)
|
|
1720
|
+
const tieYs: number[] = [tieInHeightM]
|
|
1721
|
+
const pushTieY = (y: number, minFloorY = ipcMinVentY) => {
|
|
1722
|
+
if (!Number.isFinite(y) || y < minFloorY || y > maxAtticTieY) return
|
|
1723
|
+
if (!tieYs.some((v) => Math.abs(v - y) < 0.02)) tieYs.push(y)
|
|
1724
|
+
}
|
|
1725
|
+
if (bestStudBores > 0) {
|
|
1726
|
+
const minVentWallH = ventWalls.length > 0 ? Math.min(...ventWalls.map((w) => w.heightM)) : ceilingHeightM
|
|
1727
|
+
pushTieY(minVentWallH - 0.055)
|
|
1728
|
+
pushTieY(ceilingHeightM - 0.055)
|
|
1729
|
+
}
|
|
1730
|
+
if (elevatedHits.length > 0) {
|
|
1731
|
+
const aboveHit = Math.max(...elevatedHits.map((o) => o.box.maxY)) + pipeRadiusM + 0.05
|
|
1732
|
+
const belowHit = Math.min(...elevatedHits.map((o) => o.box.minY)) - pipeRadiusM - 0.05
|
|
1733
|
+
pushTieY(aboveHit)
|
|
1734
|
+
pushTieY(belowHit)
|
|
1735
|
+
}
|
|
1736
|
+
if (Number.isFinite(minEndpointRoofCap)) pushTieY(minEndpointRoofCap)
|
|
1737
|
+
if (Number.isFinite(primaryMinRoofCap)) pushTieY(primaryMinRoofCap)
|
|
1738
|
+
if (Number.isFinite(altMinRoofCap)) pushTieY(altMinRoofCap)
|
|
1739
|
+
const headerBeams = allElevatedBeams.filter(
|
|
1740
|
+
(o) => o.beamRole === 'header' || /header/i.test(o.label) || o.box.maxY <= ceilingHeightM + 0.08,
|
|
1741
|
+
)
|
|
1742
|
+
if (headerBeams.length > 0) {
|
|
1743
|
+
pushTieY(Math.max(...headerBeams.map((o) => o.box.maxY)) + pipeRadiusM + 0.05)
|
|
1744
|
+
}
|
|
1745
|
+
pushTieY(ceilingHeightM - 0.15)
|
|
1746
|
+
pushTieY(Math.min(ceilingHeightM - 0.15, maxSafeCeilingY))
|
|
1747
|
+
pushTieY(ceilingHeightM - 0.25)
|
|
1748
|
+
pushTieY(ceilingHeightM - 0.45)
|
|
1749
|
+
pushTieY(ceilingHeightM + 0.14)
|
|
1750
|
+
pushTieY(ceilingHeightM + 0.28)
|
|
1751
|
+
pushTieY(minHorizontalVentY)
|
|
1752
|
+
pushTieY(ipcMinVentY)
|
|
1753
|
+
|
|
1754
|
+
// Check if the vertical riser at `from` hits a window/door header or beam directly above the fixture.
|
|
1755
|
+
// If so, offset horizontally past the header jamb at `jogY >= floodRim + 6"` (IPC 905.5) before rising!
|
|
1756
|
+
const riserHits = detectPlumbingClashes(
|
|
1757
|
+
[from, [from[0], Math.max(...tieYs), from[2]]],
|
|
1758
|
+
obstacles,
|
|
1759
|
+
pipeRadiusM,
|
|
1760
|
+
)
|
|
1761
|
+
const riserJogs: Array<{ x: number; z: number; jogY: number } | undefined> = [undefined]
|
|
1762
|
+
if (riserHits.length > 0) {
|
|
1763
|
+
const lowestHitY = Math.min(...riserHits.map((o) => o.box.minY))
|
|
1764
|
+
const safeJogY = Math.max(
|
|
1765
|
+
minHorizontalVentY,
|
|
1766
|
+
Math.min(lowestHitY - pipeRadiusM - 0.06, tieInHeightM - 0.2),
|
|
1767
|
+
)
|
|
1768
|
+
const escapes = buildLateralEscapeCandidates(from[0], from[2], riserHits, edgePad + 0.03)
|
|
1769
|
+
for (const [rx, rz] of escapes) {
|
|
1770
|
+
riserJogs.push({ x: rx, z: rz, jogY: safeJogY })
|
|
1771
|
+
}
|
|
1772
|
+
for (const d of [0.45, -0.45, 0.9, -0.9]) {
|
|
1773
|
+
riserJogs.push({ x: from[0] + d, z: from[2], jogY: safeJogY })
|
|
1774
|
+
riserJogs.push({ x: from[0], z: from[2] + d, jogY: safeJogY })
|
|
1775
|
+
}
|
|
1776
|
+
}
|
|
1777
|
+
|
|
1778
|
+
for (const jog of riserJogs) {
|
|
1779
|
+
const sx = jog ? jog.x : from[0]
|
|
1780
|
+
const sz = jog ? jog.z : from[2]
|
|
1781
|
+
const candidates: Array<Array<readonly [number, number]>> = [
|
|
1782
|
+
[[stackPoint[0], sz], [stackPoint[0], stackPoint[1]]],
|
|
1783
|
+
[[sx, stackPoint[1]], [stackPoint[0], stackPoint[1]]],
|
|
1784
|
+
[[stackPoint[0] + 0.16, sz], [stackPoint[0] + 0.16, stackPoint[1]], [stackPoint[0], stackPoint[1]]],
|
|
1785
|
+
[[stackPoint[0] - 0.16, sz], [stackPoint[0] - 0.16, stackPoint[1]], [stackPoint[0], stackPoint[1]]],
|
|
1786
|
+
[[sx, sz + 0.16], [stackPoint[0] + 0.16, sz + 0.16], [stackPoint[0] + 0.16, stackPoint[1]], [stackPoint[0], stackPoint[1]]],
|
|
1787
|
+
[[sx, sz - 0.16], [stackPoint[0] - 0.16, sz - 0.16], [stackPoint[0] - 0.16, stackPoint[1]], [stackPoint[0], stackPoint[1]]],
|
|
1788
|
+
[[sx, sz + 0.16], [stackPoint[0] - 0.16, sz + 0.16], [stackPoint[0] - 0.16, stackPoint[1]], [stackPoint[0], stackPoint[1]]],
|
|
1789
|
+
[[sx, sz - 0.16], [stackPoint[0] + 0.16, sz - 0.16], [stackPoint[0] + 0.16, stackPoint[1]], [stackPoint[0], stackPoint[1]]],
|
|
1790
|
+
// Corner-stack open-stud-bay entry: step ±0.16m off both axes so final 16cm stub stays inside a single stud bay away from corner walls
|
|
1791
|
+
...([0.16, -0.16] as const).flatMap((ox) =>
|
|
1792
|
+
([0.16, -0.16] as const).flatMap((oz): Array<Array<readonly [number, number]>> => [
|
|
1793
|
+
[
|
|
1794
|
+
[stackPoint[0] + ox, sz],
|
|
1795
|
+
[stackPoint[0] + ox, stackPoint[1] + oz],
|
|
1796
|
+
[stackPoint[0], stackPoint[1] + oz],
|
|
1797
|
+
[stackPoint[0], stackPoint[1]],
|
|
1798
|
+
],
|
|
1799
|
+
[
|
|
1800
|
+
[sx, stackPoint[1] + oz],
|
|
1801
|
+
[stackPoint[0] + ox, stackPoint[1] + oz],
|
|
1802
|
+
[stackPoint[0] + ox, stackPoint[1]],
|
|
1803
|
+
[stackPoint[0], stackPoint[1]],
|
|
1804
|
+
],
|
|
1805
|
+
[
|
|
1806
|
+
[sx, sz + oz],
|
|
1807
|
+
[stackPoint[0] + ox, sz + oz],
|
|
1808
|
+
[stackPoint[0] + ox, stackPoint[1] + oz],
|
|
1809
|
+
[stackPoint[0], stackPoint[1] + oz],
|
|
1810
|
+
[stackPoint[0], stackPoint[1]],
|
|
1811
|
+
],
|
|
1812
|
+
[
|
|
1813
|
+
[sx, sz + oz],
|
|
1814
|
+
[stackPoint[0] + ox, sz + oz],
|
|
1815
|
+
[stackPoint[0] + ox, stackPoint[1]],
|
|
1816
|
+
[stackPoint[0], stackPoint[1]],
|
|
1817
|
+
],
|
|
1818
|
+
[
|
|
1819
|
+
[sx + ox, sz],
|
|
1820
|
+
[sx + ox, stackPoint[1] + oz],
|
|
1821
|
+
[stackPoint[0] + ox, stackPoint[1] + oz],
|
|
1822
|
+
[stackPoint[0] + ox, stackPoint[1]],
|
|
1823
|
+
[stackPoint[0], stackPoint[1]],
|
|
1824
|
+
],
|
|
1825
|
+
[
|
|
1826
|
+
[sx + ox, sz],
|
|
1827
|
+
[sx + ox, stackPoint[1] + oz],
|
|
1828
|
+
[stackPoint[0], stackPoint[1] + oz],
|
|
1829
|
+
[stackPoint[0], stackPoint[1]],
|
|
1830
|
+
],
|
|
1831
|
+
[
|
|
1832
|
+
[sx + ox, sz + oz],
|
|
1833
|
+
[stackPoint[0] + ox, sz + oz],
|
|
1834
|
+
[stackPoint[0] + ox, stackPoint[1] + oz],
|
|
1835
|
+
[stackPoint[0], stackPoint[1] + oz],
|
|
1836
|
+
[stackPoint[0], stackPoint[1]],
|
|
1837
|
+
],
|
|
1838
|
+
[
|
|
1839
|
+
[sx + ox, sz + oz],
|
|
1840
|
+
[sx + ox, stackPoint[1] + oz],
|
|
1841
|
+
[stackPoint[0] + ox, stackPoint[1] + oz],
|
|
1842
|
+
[stackPoint[0] + ox, stackPoint[1]],
|
|
1843
|
+
[stackPoint[0], stackPoint[1]],
|
|
1844
|
+
],
|
|
1845
|
+
]),
|
|
1846
|
+
),
|
|
1847
|
+
[[stackPoint[0], sz + 0.55], [stackPoint[0], stackPoint[1]]],
|
|
1848
|
+
[[stackPoint[0], sz - 0.55], [stackPoint[0], stackPoint[1]]],
|
|
1849
|
+
[[sx + 0.55, stackPoint[1]], [stackPoint[0], stackPoint[1]]],
|
|
1850
|
+
[[sx - 0.55, stackPoint[1]], [stackPoint[0], stackPoint[1]]],
|
|
1851
|
+
[[stackPoint[0] + 0.95, sz], [stackPoint[0] + 0.95, stackPoint[1]], [stackPoint[0], stackPoint[1]]],
|
|
1852
|
+
[[stackPoint[0] - 0.95, sz], [stackPoint[0] - 0.95, stackPoint[1]], [stackPoint[0], stackPoint[1]]],
|
|
1853
|
+
...buildObstacleDetourWaypoints([sx, sz], stackPoint, bestClashes, edgePad),
|
|
1854
|
+
]
|
|
1855
|
+
for (const tieY of tieYs) {
|
|
1856
|
+
for (const wp of candidates) {
|
|
1857
|
+
if (tryVent(buildVentCandidate(wp, tieY, jog), tieY)) break
|
|
1858
|
+
}
|
|
1859
|
+
if (bestClashes.length === 0 && !bestBoundsViolation && !bestRoofViolation && bestStudBores === 0) break
|
|
1860
|
+
}
|
|
1861
|
+
if (bestClashes.length === 0 && !bestBoundsViolation && !bestRoofViolation && bestStudBores === 0) break
|
|
1862
|
+
}
|
|
1863
|
+
|
|
1864
|
+
// Layer 2 — Legal-Space-First Cavity Rescue (non-descending-vent, IPC 905.5 & 905.2)
|
|
1865
|
+
if (
|
|
1866
|
+
(bestClashes.length > 0 || bestBoundsViolation || bestRoofViolation || bestStudBores > 0) &&
|
|
1867
|
+
hasCavityScene(cavityContext)
|
|
1868
|
+
) {
|
|
1869
|
+
const cavityTargetY =
|
|
1870
|
+
cavityContext?.graph &&
|
|
1871
|
+
(!maxRoofYAt ||
|
|
1872
|
+
cavityContext.graph.ceilingPlenumY + pipeRadiusM <=
|
|
1873
|
+
Math.min(maxRoofYAt(from[0], from[2]), maxRoofYAt(stackPoint[0], stackPoint[1])) - 0.08 + 1e-4)
|
|
1874
|
+
? Math.max(tieInHeightM, cavityContext.graph.ceilingPlenumY)
|
|
1875
|
+
: tieInHeightM
|
|
1876
|
+
const cav = routePlumbingViaLegalCavity({
|
|
1877
|
+
...cavityContext,
|
|
1878
|
+
from,
|
|
1879
|
+
to: [stackPoint[0], cavityTargetY, stackPoint[1]],
|
|
1880
|
+
mode: 'non-descending-vent',
|
|
1881
|
+
nominalSize,
|
|
1882
|
+
material,
|
|
1883
|
+
pipeRadiusM,
|
|
1884
|
+
floodRimHeightM,
|
|
1885
|
+
minHorizontalElevationM: minHorizontalVentY,
|
|
1886
|
+
obstacles,
|
|
1887
|
+
})
|
|
1888
|
+
if (
|
|
1889
|
+
cav.path.length >= 2 &&
|
|
1890
|
+
isVentPathCodeCompliant(cav.path, floodRimHeightM + IPC_RULE_THRESHOLDS.ventMinAboveFloodRimM)
|
|
1891
|
+
) {
|
|
1892
|
+
const cavTieY = cav.path[cav.path.length - 1]![1]
|
|
1893
|
+
tryVent(cav.path, cavTieY)
|
|
1894
|
+
}
|
|
1895
|
+
}
|
|
1896
|
+
}
|
|
1897
|
+
|
|
1898
|
+
return {
|
|
1899
|
+
path: bestPath,
|
|
1900
|
+
tieInHeightM: round2(bestTieInY),
|
|
1901
|
+
lengthFt: round2(polylineLengthM(bestPath) * METERS_TO_FEET),
|
|
1902
|
+
detoured,
|
|
1903
|
+
clashes: bestClashes,
|
|
1904
|
+
}
|
|
1905
|
+
}
|
|
1906
|
+
|
|
1907
|
+
/**
|
|
1908
|
+
* Route a pressurized supply (Cold / Hot / Recirc) or Fuel Gas run:
|
|
1909
|
+
* - Drops vertically inside wall cavities to under-floor crawlspace/basement or attic plane
|
|
1910
|
+
* - Automatically detours below floor girders, over door/window headers, and clear of NEC 110.26(E)
|
|
1911
|
+
* electrical-panel keep-out zones & structural columns (including vertical riser/drop jogs).
|
|
1912
|
+
*/
|
|
1913
|
+
export function routePressurizedPipeRun(params: {
|
|
1914
|
+
from: Vec3
|
|
1915
|
+
to: Vec3
|
|
1916
|
+
routingPlaneY: number
|
|
1917
|
+
nominalSize?: PipeNominalSize
|
|
1918
|
+
material?: PipeMaterial
|
|
1919
|
+
pipeRadiusM?: number
|
|
1920
|
+
euclideanInCavity?: boolean
|
|
1921
|
+
obstacles?: ReadonlyArray<PlumbingObstacle>
|
|
1922
|
+
buildingBounds?: BuildingBounds2D
|
|
1923
|
+
maxRoofYAt?: (x: number, z: number) => number
|
|
1924
|
+
cavityContext?: PlumbingCavityContext
|
|
1925
|
+
/** Optional explicit cavity band whitelist for Layer 2 cavity rescue. */
|
|
1926
|
+
allowedBands?: readonly CavityBand[]
|
|
1927
|
+
}): {
|
|
1928
|
+
path: Vec3[]
|
|
1929
|
+
lengthFt: number
|
|
1930
|
+
detoured: boolean
|
|
1931
|
+
clashes: PlumbingObstacle[]
|
|
1932
|
+
} {
|
|
1933
|
+
const {
|
|
1934
|
+
from,
|
|
1935
|
+
to,
|
|
1936
|
+
routingPlaneY,
|
|
1937
|
+
nominalSize,
|
|
1938
|
+
material = 'pex-a',
|
|
1939
|
+
euclideanInCavity = true,
|
|
1940
|
+
obstacles = [],
|
|
1941
|
+
buildingBounds,
|
|
1942
|
+
maxRoofYAt,
|
|
1943
|
+
cavityContext,
|
|
1944
|
+
} = params
|
|
1945
|
+
const pipeRadiusM =
|
|
1946
|
+
params.pipeRadiusM ?? (nominalSize ? getPipeOuterDiameterM(nominalSize, material) / 2 : 0.02)
|
|
1947
|
+
const env = resolveAllowedEnvelope(buildingBounds, from, [to[0], to[2]])
|
|
1948
|
+
const checkFootprint = Boolean(buildingBounds) && hasCavityScene(cavityContext)
|
|
1949
|
+
|
|
1950
|
+
const buildCandidate = (
|
|
1951
|
+
elbowFirst: 'x' | 'z' | 'diag',
|
|
1952
|
+
offsetM = 0,
|
|
1953
|
+
planeY = routingPlaneY,
|
|
1954
|
+
fromJog?: readonly [number, number],
|
|
1955
|
+
toJog?: readonly [number, number],
|
|
1956
|
+
customMid?: ReadonlyArray<readonly [number, number]>,
|
|
1957
|
+
): Vec3[] => {
|
|
1958
|
+
const fx = fromJog ? fromJog[0] : from[0]
|
|
1959
|
+
const fz = fromJog ? fromJog[1] : from[2]
|
|
1960
|
+
const tx = toJog ? toJog[0] : to[0]
|
|
1961
|
+
const tz = toJog ? toJog[1] : to[2]
|
|
1962
|
+
|
|
1963
|
+
const pts: Vec3[] = [from]
|
|
1964
|
+
if (fromJog) pts.push([fx, from[1], fz])
|
|
1965
|
+
pts.push([fx, planeY, fz])
|
|
1966
|
+
|
|
1967
|
+
if (customMid) {
|
|
1968
|
+
for (const [mx, mz] of customMid) {
|
|
1969
|
+
pts.push([mx, planeY, mz])
|
|
1970
|
+
}
|
|
1971
|
+
} else if (elbowFirst === 'diag') {
|
|
1972
|
+
if (Math.abs(offsetM) >= 1e-4) {
|
|
1973
|
+
pts.push([(fx + tx) / 2 + offsetM, planeY, (fz + tz) / 2 - offsetM])
|
|
1974
|
+
}
|
|
1975
|
+
} else if (elbowFirst === 'x') {
|
|
1976
|
+
if (!euclideanInCavity && Math.abs(offsetM) >= 1e-4) {
|
|
1977
|
+
// Strictly orthogonal 3-segment U/Z offset for rigid / orthogonal runs
|
|
1978
|
+
pts.push([fx, planeY, fz + offsetM])
|
|
1979
|
+
pts.push([tx, planeY, fz + offsetM])
|
|
1980
|
+
} else {
|
|
1981
|
+
pts.push([tx + offsetM, planeY, fz + offsetM])
|
|
1982
|
+
}
|
|
1983
|
+
} else {
|
|
1984
|
+
if (!euclideanInCavity && Math.abs(offsetM) >= 1e-4) {
|
|
1985
|
+
pts.push([fx + offsetM, planeY, fz])
|
|
1986
|
+
pts.push([fx + offsetM, planeY, tz])
|
|
1987
|
+
} else {
|
|
1988
|
+
pts.push([fx + offsetM, planeY, tz + offsetM])
|
|
1989
|
+
}
|
|
1990
|
+
}
|
|
1991
|
+
|
|
1992
|
+
pts.push([tx, planeY, tz])
|
|
1993
|
+
if (toJog) pts.push([tx, to[1], tz])
|
|
1994
|
+
pts.push(to)
|
|
1995
|
+
return cleanPath(pts)
|
|
1996
|
+
}
|
|
1997
|
+
|
|
1998
|
+
const skipMidWallLargeSnap = pipeRadiusM > 0.035 && routingPlaneY > 0.05 && routingPlaneY < 1.8
|
|
1999
|
+
const fromWallSnap =
|
|
2000
|
+
hasCavityScene(cavityContext) && !skipMidWallLargeSnap
|
|
2001
|
+
? resolveBackingWallSnap(from, cavityContext, obstacles, pipeRadiusM, buildingBounds, checkFootprint, 1.05)
|
|
2002
|
+
: null
|
|
2003
|
+
const toWallSnap =
|
|
2004
|
+
hasCavityScene(cavityContext) && !skipMidWallLargeSnap
|
|
2005
|
+
? resolveBackingWallSnap(to, cavityContext, obstacles, pipeRadiusM, buildingBounds, checkFootprint, 1.05)
|
|
2006
|
+
: null
|
|
2007
|
+
const wallFromJog: readonly [number, number] | undefined = fromWallSnap
|
|
2008
|
+
? [fromWallSnap[0], fromWallSnap[2]]
|
|
2009
|
+
: undefined
|
|
2010
|
+
const wallToJog: readonly [number, number] | undefined = toWallSnap
|
|
2011
|
+
? [toWallSnap[0], toWallSnap[2]]
|
|
2012
|
+
: undefined
|
|
2013
|
+
|
|
2014
|
+
const pressWallEnv =
|
|
2015
|
+
buildingBounds && hasCavityScene(cavityContext)
|
|
2016
|
+
? resolveAllowedEnvelope(buildingBounds, from, [to[0], to[2]], -0.05)
|
|
2017
|
+
: env
|
|
2018
|
+
const primaryEnv = wallFromJog || wallToJog ? pressWallEnv : env
|
|
2019
|
+
const primary = buildCandidate(
|
|
2020
|
+
euclideanInCavity ? 'diag' : 'x',
|
|
2021
|
+
0,
|
|
2022
|
+
routingPlaneY,
|
|
2023
|
+
wallFromJog,
|
|
2024
|
+
wallToJog,
|
|
2025
|
+
)
|
|
2026
|
+
let bestPath = primary
|
|
2027
|
+
let bestClashes = detectPlumbingClashes(primary, obstacles, pipeRadiusM)
|
|
2028
|
+
let bestBoundsViolation =
|
|
2029
|
+
exceedsEnvelopeXZ(primary, primaryEnv) || exceedsConcaveFootprint(primary, cavityContext, checkFootprint)
|
|
2030
|
+
let bestRoofViolation = exceedsRoofPlane(primary, pipeRadiusM, maxRoofYAt)
|
|
2031
|
+
let detoured = false
|
|
2032
|
+
|
|
2033
|
+
// Wall-Cavity-First Concealment for overhead pressurized runs (> 1.8 m):
|
|
2034
|
+
// Route between wall-snapped risers strictly inside connected upper wall cavities (top-plate chase)
|
|
2035
|
+
// so pipes stay hidden when Full Walls are enabled, while honoring BUG-PS-05 (>= 2.0 m horizontal travel)
|
|
2036
|
+
// and IRC R602.6 (large mains & fuel gas stay in the top-plate zone >= wallH - 0.08).
|
|
2037
|
+
if (hasCavityScene(cavityContext) && routingPlaneY > 1.8) {
|
|
2038
|
+
const coreFrom: Vec3 = fromWallSnap ?? from
|
|
2039
|
+
const coreTo: Vec3 = toWallSnap ?? to
|
|
2040
|
+
const wallH = resolveWallHeightForContext(cavityContext, Math.max(2.7, routingPlaneY + 0.25))
|
|
2041
|
+
const isLargeOrGas =
|
|
2042
|
+
material === 'black-iron' ||
|
|
2043
|
+
material === 'csst' ||
|
|
2044
|
+
Boolean(nominalSize && getPipeOuterDiameterM(nominalSize, material) > getPipeOuterDiameterM('3/4"', material) + 1e-4)
|
|
2045
|
+
const endpointRoofCap = maxRoofYAt
|
|
2046
|
+
? Math.min(maxRoofYAt(coreFrom[0], coreFrom[2]), maxRoofYAt(coreTo[0], coreTo[2])) - pipeRadiusM - 0.09
|
|
2047
|
+
: Infinity
|
|
2048
|
+
const roofClampedMaxY = Number.isFinite(endpointRoofCap)
|
|
2049
|
+
? Math.min(wallH - 0.06, endpointRoofCap)
|
|
2050
|
+
: wallH - 0.06
|
|
2051
|
+
const desiredChaseY = isLargeOrGas
|
|
2052
|
+
? round4(Math.min(roofClampedMaxY, Math.max(2.05, wallH - 0.075)))
|
|
2053
|
+
: round4(Math.min(roofClampedMaxY, Math.max(2.05, routingPlaneY)))
|
|
2054
|
+
const bandPasses: ReadonlyArray<readonly CavityBand[]> = params.allowedBands
|
|
2055
|
+
? [params.allowedBands]
|
|
2056
|
+
: [['top-plate'], ['top-plate', 'overhead-plenum', 'attic']]
|
|
2057
|
+
let adoptedWallSupply = false
|
|
2058
|
+
for (const preferredBands of bandPasses) {
|
|
2059
|
+
if (adoptedWallSupply) break
|
|
2060
|
+
const cavWall = routePlumbingViaLegalCavity({
|
|
2061
|
+
...cavityContext,
|
|
2062
|
+
from: coreFrom,
|
|
2063
|
+
to: coreTo,
|
|
2064
|
+
mode: euclideanInCavity ? 'euclidean-cavity' : 'orthogonal',
|
|
2065
|
+
nominalSize,
|
|
2066
|
+
material,
|
|
2067
|
+
pipeRadiusM,
|
|
2068
|
+
preferWallFollowing: true,
|
|
2069
|
+
allowedBands: preferredBands,
|
|
2070
|
+
...(preferredBands.length === 1 && preferredBands[0] === 'top-plate'
|
|
2071
|
+
? { graph: undefined, roofClearanceM: 0.01 }
|
|
2072
|
+
: {}),
|
|
2073
|
+
obstacles,
|
|
2074
|
+
})
|
|
2075
|
+
if (cavWall.path.length >= 2) {
|
|
2076
|
+
const wrapStubs = (corePath: ReadonlyArray<Vec3>): Vec3[] => {
|
|
2077
|
+
const pts: Vec3[] = []
|
|
2078
|
+
const first = corePath[0]!
|
|
2079
|
+
const last = corePath[corePath.length - 1]!
|
|
2080
|
+
if (fromWallSnap) {
|
|
2081
|
+
pts.push(from)
|
|
2082
|
+
// Keep stub strictly horizontal/orthogonal if kernel snapped first vertex in Y or XZ
|
|
2083
|
+
if (Math.abs(first[1] - from[1]) > 1e-3) {
|
|
2084
|
+
if (!euclideanInCavity && Math.abs(first[0] - from[0]) > 0.01 && Math.abs(first[2] - from[2]) > 0.01) {
|
|
2085
|
+
pts.push([first[0], from[1], from[2]])
|
|
2086
|
+
}
|
|
2087
|
+
pts.push([first[0], from[1], first[2]])
|
|
2088
|
+
} else if (!euclideanInCavity && Math.abs(first[0] - from[0]) > 0.01 && Math.abs(first[2] - from[2]) > 0.01) {
|
|
2089
|
+
pts.push([first[0], from[1], from[2]])
|
|
2090
|
+
}
|
|
2091
|
+
}
|
|
2092
|
+
pts.push(...corePath)
|
|
2093
|
+
if (toWallSnap) {
|
|
2094
|
+
if (Math.abs(last[1] - to[1]) > 1e-3) {
|
|
2095
|
+
pts.push([last[0], to[1], last[2]])
|
|
2096
|
+
if (!euclideanInCavity && Math.abs(to[0] - last[0]) > 0.01 && Math.abs(to[2] - last[2]) > 0.01) {
|
|
2097
|
+
pts.push([to[0], to[1], last[2]])
|
|
2098
|
+
}
|
|
2099
|
+
} else if (!euclideanInCavity && Math.abs(to[0] - last[0]) > 0.01 && Math.abs(to[2] - last[2]) > 0.01) {
|
|
2100
|
+
pts.push([to[0], to[1], last[2]])
|
|
2101
|
+
}
|
|
2102
|
+
pts.push(to)
|
|
2103
|
+
}
|
|
2104
|
+
return cleanPath(pts)
|
|
2105
|
+
}
|
|
2106
|
+
const remappedCore = remapOverheadHorizontalBand(cavWall.path, desiredChaseY, 2.0)
|
|
2107
|
+
const coreCandidates = [remappedCore, cavWall.path]
|
|
2108
|
+
const obeysOverheadFloor = (p: ReadonlyArray<Vec3>): boolean => {
|
|
2109
|
+
for (let i = 1; i < p.length; i++) {
|
|
2110
|
+
const a = p[i - 1]!
|
|
2111
|
+
const b = p[i]!
|
|
2112
|
+
if (Math.hypot(b[0] - a[0], b[2] - a[2]) > 0.5 && Math.min(a[1], b[1]) < 2.0 - 1e-3) {
|
|
2113
|
+
return false
|
|
2114
|
+
}
|
|
2115
|
+
}
|
|
2116
|
+
return true
|
|
2117
|
+
}
|
|
2118
|
+
const obeysOrthogonality = (p: ReadonlyArray<Vec3>): boolean => {
|
|
2119
|
+
if (euclideanInCavity) return true
|
|
2120
|
+
for (let i = 1; i < p.length; i++) {
|
|
2121
|
+
const a = p[i - 1]!
|
|
2122
|
+
const b = p[i]!
|
|
2123
|
+
const dx = Math.abs(b[0] - a[0])
|
|
2124
|
+
const dz = Math.abs(b[2] - a[2])
|
|
2125
|
+
if ((dx > 0.02 || dz > 0.02) && Math.min(dx, dz) >= 0.01) {
|
|
2126
|
+
return false
|
|
2127
|
+
}
|
|
2128
|
+
}
|
|
2129
|
+
return true
|
|
2130
|
+
}
|
|
2131
|
+
for (const coreCand of coreCandidates) {
|
|
2132
|
+
if (!obeysOverheadFloor(coreCand)) continue
|
|
2133
|
+
const cand = wrapStubs(coreCand)
|
|
2134
|
+
if (
|
|
2135
|
+
obeysOrthogonality(cand) &&
|
|
2136
|
+
!exceedsEnvelopeXZ(cand, pressWallEnv) &&
|
|
2137
|
+
!exceedsConcaveFootprint(cand, cavityContext, checkFootprint) &&
|
|
2138
|
+
!exceedsRoofPlane(cand, pipeRadiusM, maxRoofYAt) &&
|
|
2139
|
+
detectPlumbingClashes(cand, obstacles, pipeRadiusM).length === 0
|
|
2140
|
+
) {
|
|
2141
|
+
if (bestClashes.length > 0 || bestBoundsViolation || bestRoofViolation) detoured = true
|
|
2142
|
+
bestPath = cand
|
|
2143
|
+
bestClashes = []
|
|
2144
|
+
bestBoundsViolation = false
|
|
2145
|
+
bestRoofViolation = false
|
|
2146
|
+
adoptedWallSupply = true
|
|
2147
|
+
break
|
|
2148
|
+
}
|
|
2149
|
+
}
|
|
2150
|
+
}
|
|
2151
|
+
}
|
|
2152
|
+
}
|
|
2153
|
+
|
|
2154
|
+
if (bestClashes.length > 0 || bestBoundsViolation || bestRoofViolation) {
|
|
2155
|
+
const maxEval = 300
|
|
2156
|
+
let evalCount = 0
|
|
2157
|
+
const tryPressurized = (cand: Vec3[], bypassBudget = false): boolean => {
|
|
2158
|
+
if (!bypassBudget && evalCount >= maxEval) return true
|
|
2159
|
+
if (!bypassBudget) evalCount += 1
|
|
2160
|
+
const boundsViol =
|
|
2161
|
+
exceedsEnvelopeXZ(cand, pressWallEnv) || exceedsConcaveFootprint(cand, cavityContext, checkFootprint)
|
|
2162
|
+
const roofViol = exceedsRoofPlane(cand, pipeRadiusM, maxRoofYAt)
|
|
2163
|
+
if (boundsViol && !bestBoundsViolation) return !bypassBudget && evalCount >= maxEval
|
|
2164
|
+
if (roofViol && !bestRoofViolation && boundsViol === bestBoundsViolation) return !bypassBudget && evalCount >= maxEval
|
|
2165
|
+
const c = detectPlumbingClashes(cand, obstacles, pipeRadiusM)
|
|
2166
|
+
const better =
|
|
2167
|
+
(bestBoundsViolation && !boundsViol) ||
|
|
2168
|
+
(boundsViol === bestBoundsViolation && bestRoofViolation && !roofViol) ||
|
|
2169
|
+
(boundsViol === bestBoundsViolation && roofViol === bestRoofViolation && c.length < bestClashes.length)
|
|
2170
|
+
if (better) {
|
|
2171
|
+
bestPath = cand
|
|
2172
|
+
bestClashes = c
|
|
2173
|
+
bestBoundsViolation = boundsViol
|
|
2174
|
+
bestRoofViolation = roofViol
|
|
2175
|
+
detoured = true
|
|
2176
|
+
}
|
|
2177
|
+
return (
|
|
2178
|
+
(bestClashes.length === 0 && !bestBoundsViolation && !bestRoofViolation) ||
|
|
2179
|
+
(!bypassBudget && evalCount >= maxEval)
|
|
2180
|
+
)
|
|
2181
|
+
}
|
|
2182
|
+
|
|
2183
|
+
const edgePad = pipeRadiusM + 0.07
|
|
2184
|
+
const allUnderBeams = obstacles.filter(isUnderFloorBeamObstacle)
|
|
2185
|
+
const structuralHits = bestClashes.filter(isUnderFloorBeamObstacle)
|
|
2186
|
+
const elevatedHits = bestClashes.filter(isElevatedBeamObstacle)
|
|
2187
|
+
const allElevatedAreHeaders =
|
|
2188
|
+
elevatedHits.length > 0 &&
|
|
2189
|
+
elevatedHits.every((o) => o.beamRole === 'header' || /header/i.test(o.label))
|
|
2190
|
+
|
|
2191
|
+
const planeYs: number[] = []
|
|
2192
|
+
const pushPlaneY = (y: number) => {
|
|
2193
|
+
if (!planeYs.some((v) => Math.abs(v - y) < 0.015)) planeYs.push(y)
|
|
2194
|
+
}
|
|
2195
|
+
|
|
2196
|
+
if (structuralHits.length > 0) {
|
|
2197
|
+
pushPlaneY(
|
|
2198
|
+
Math.min(routingPlaneY - 0.16, Math.min(...structuralHits.map((o) => o.box.minY)) - pipeRadiusM - 0.06),
|
|
2199
|
+
)
|
|
2200
|
+
}
|
|
2201
|
+
if (allUnderBeams.length > 0 && routingPlaneY <= 0.25) {
|
|
2202
|
+
pushPlaneY(
|
|
2203
|
+
Math.min(routingPlaneY - 0.16, Math.min(...allUnderBeams.map((o) => o.box.minY)) - pipeRadiusM - 0.06),
|
|
2204
|
+
)
|
|
2205
|
+
}
|
|
2206
|
+
if (elevatedHits.length > 0) {
|
|
2207
|
+
const aboveHit = Math.max(...elevatedHits.map((o) => o.box.maxY)) + pipeRadiusM + 0.06
|
|
2208
|
+
const belowHit = Math.min(...elevatedHits.map((o) => o.box.minY)) - pipeRadiusM - 0.06
|
|
2209
|
+
const aboveViolatesRoof =
|
|
2210
|
+
maxRoofYAt !== undefined &&
|
|
2211
|
+
aboveHit + pipeRadiusM >
|
|
2212
|
+
Math.min(maxRoofYAt(from[0], from[2]), maxRoofYAt(to[0], to[2])) - 0.08 + 1e-4
|
|
2213
|
+
// Always try routing OVER door/window headers first; suppress belowHit on overhead runs (> 1.8m)
|
|
2214
|
+
// when routing over fits beneath the roof envelope so supply pipes never drop across open doorways/windows.
|
|
2215
|
+
pushPlaneY(aboveHit)
|
|
2216
|
+
if (!(allElevatedAreHeaders && routingPlaneY > 1.8 && !aboveViolatesRoof)) {
|
|
2217
|
+
pushPlaneY(belowHit)
|
|
2218
|
+
}
|
|
2219
|
+
}
|
|
2220
|
+
pushPlaneY(routingPlaneY)
|
|
2221
|
+
if (!(allElevatedAreHeaders && routingPlaneY > 1.8)) {
|
|
2222
|
+
pushPlaneY(routingPlaneY - 0.28)
|
|
2223
|
+
}
|
|
2224
|
+
if (maxRoofYAt) {
|
|
2225
|
+
const endpointRoofCap =
|
|
2226
|
+
Math.min(maxRoofYAt(from[0], from[2]), maxRoofYAt(to[0], to[2])) - pipeRadiusM - 0.09
|
|
2227
|
+
const primaryRoofCap = minRoofAlongPath(primary, maxRoofYAt) - pipeRadiusM - 0.09
|
|
2228
|
+
const roofSafeY = Math.min(endpointRoofCap, primaryRoofCap)
|
|
2229
|
+
if (bestRoofViolation || roofSafeY < routingPlaneY) {
|
|
2230
|
+
if (Number.isFinite(endpointRoofCap) && endpointRoofCap < routingPlaneY) {
|
|
2231
|
+
pushPlaneY(endpointRoofCap)
|
|
2232
|
+
}
|
|
2233
|
+
if (Number.isFinite(primaryRoofCap) && primaryRoofCap < routingPlaneY) {
|
|
2234
|
+
pushPlaneY(primaryRoofCap)
|
|
2235
|
+
}
|
|
2236
|
+
if (Number.isFinite(roofSafeY) && roofSafeY < routingPlaneY) {
|
|
2237
|
+
pushPlaneY(roofSafeY - 0.12)
|
|
2238
|
+
}
|
|
2239
|
+
}
|
|
2240
|
+
}
|
|
2241
|
+
|
|
2242
|
+
// Probe the FULL vertical riser/drop envelope [minPlaneY, maxPlaneY] so headers/girders
|
|
2243
|
+
// sitting anywhere between endpoint elevation and any candidate routing plane are caught.
|
|
2244
|
+
const minPlaneY = Math.min(...planeYs)
|
|
2245
|
+
const maxPlaneY = Math.max(...planeYs)
|
|
2246
|
+
const fromDropHits = detectPlumbingClashes(
|
|
2247
|
+
[
|
|
2248
|
+
[from[0], Math.min(from[1], minPlaneY), from[2]],
|
|
2249
|
+
[from[0], Math.max(from[1], maxPlaneY), from[2]],
|
|
2250
|
+
],
|
|
2251
|
+
obstacles,
|
|
2252
|
+
pipeRadiusM,
|
|
2253
|
+
)
|
|
2254
|
+
const toRiseHits = detectPlumbingClashes(
|
|
2255
|
+
[
|
|
2256
|
+
[to[0], Math.min(to[1], minPlaneY), to[2]],
|
|
2257
|
+
[to[0], Math.max(to[1], maxPlaneY), to[2]],
|
|
2258
|
+
],
|
|
2259
|
+
obstacles,
|
|
2260
|
+
pipeRadiusM,
|
|
2261
|
+
)
|
|
2262
|
+
|
|
2263
|
+
const prefilterJogs = (
|
|
2264
|
+
anchor: Vec3,
|
|
2265
|
+
rawEscapes: ReadonlyArray<readonly [number, number]>,
|
|
2266
|
+
limit = 4,
|
|
2267
|
+
): Array<readonly [number, number] | undefined> => {
|
|
2268
|
+
if (rawEscapes.length === 0) return [undefined]
|
|
2269
|
+
const yLo = Math.min(anchor[1], minPlaneY)
|
|
2270
|
+
const yHi = Math.max(anchor[1], maxPlaneY)
|
|
2271
|
+
const scored = rawEscapes
|
|
2272
|
+
.filter(([jx, jz]) => {
|
|
2273
|
+
if (
|
|
2274
|
+
pressWallEnv &&
|
|
2275
|
+
(jx < pressWallEnv.minX - 1e-4 ||
|
|
2276
|
+
jx > pressWallEnv.maxX + 1e-4 ||
|
|
2277
|
+
jz < pressWallEnv.minZ - 1e-4 ||
|
|
2278
|
+
jz > pressWallEnv.maxZ + 1e-4)
|
|
2279
|
+
) {
|
|
2280
|
+
return false
|
|
2281
|
+
}
|
|
2282
|
+
if (
|
|
2283
|
+
checkFootprint &&
|
|
2284
|
+
exceedsConcaveFootprint([anchor, [jx, anchor[1], jz]], cavityContext, true)
|
|
2285
|
+
) {
|
|
2286
|
+
return false
|
|
2287
|
+
}
|
|
2288
|
+
return true
|
|
2289
|
+
})
|
|
2290
|
+
.map((jog) => {
|
|
2291
|
+
const leg: Vec3[] = [
|
|
2292
|
+
anchor,
|
|
2293
|
+
[jog[0], anchor[1], jog[1]],
|
|
2294
|
+
[jog[0], yLo, jog[1]],
|
|
2295
|
+
[jog[0], yHi, jog[1]],
|
|
2296
|
+
]
|
|
2297
|
+
const clashCount = detectPlumbingClashes(leg, obstacles, pipeRadiusM).length
|
|
2298
|
+
const dist = Math.hypot(jog[0] - anchor[0], jog[1] - anchor[2])
|
|
2299
|
+
return { jog, clashCount, dist }
|
|
2300
|
+
})
|
|
2301
|
+
scored.sort((a, b) => a.clashCount - b.clashCount || a.dist - b.dist)
|
|
2302
|
+
return [undefined, ...scored.slice(0, limit).map((s) => s.jog)]
|
|
2303
|
+
}
|
|
2304
|
+
|
|
2305
|
+
const fromJogs = prefilterJogs(
|
|
2306
|
+
from,
|
|
2307
|
+
fromDropHits.length > 0 ? buildLateralEscapeCandidates(from[0], from[2], fromDropHits, edgePad) : [],
|
|
2308
|
+
)
|
|
2309
|
+
const toJogs = prefilterJogs(
|
|
2310
|
+
to,
|
|
2311
|
+
toRiseHits.length > 0 ? buildLateralEscapeCandidates(to[0], to[2], toRiseHits, edgePad) : [],
|
|
2312
|
+
)
|
|
2313
|
+
|
|
2314
|
+
const modes: ReadonlyArray<readonly ['x' | 'z' | 'diag', number]> = euclideanInCavity
|
|
2315
|
+
? [
|
|
2316
|
+
['diag', 0],
|
|
2317
|
+
['x', 0],
|
|
2318
|
+
['z', 0],
|
|
2319
|
+
['x', 0.55],
|
|
2320
|
+
['x', -0.55],
|
|
2321
|
+
['z', 0.55],
|
|
2322
|
+
['z', -0.55],
|
|
2323
|
+
['diag', 0.75],
|
|
2324
|
+
['diag', -0.75],
|
|
2325
|
+
['x', 1.15],
|
|
2326
|
+
['x', -1.15],
|
|
2327
|
+
['z', 1.15],
|
|
2328
|
+
['z', -1.15],
|
|
2329
|
+
]
|
|
2330
|
+
: [
|
|
2331
|
+
['x', 0],
|
|
2332
|
+
['z', 0],
|
|
2333
|
+
['x', 0.55],
|
|
2334
|
+
['x', -0.55],
|
|
2335
|
+
['z', 0.55],
|
|
2336
|
+
['z', -0.55],
|
|
2337
|
+
['x', 1.15],
|
|
2338
|
+
['x', -1.15],
|
|
2339
|
+
['z', 1.15],
|
|
2340
|
+
['z', -1.15],
|
|
2341
|
+
]
|
|
2342
|
+
|
|
2343
|
+
const isDone = () =>
|
|
2344
|
+
(bestClashes.length === 0 && !bestBoundsViolation && !bestRoofViolation) || evalCount >= maxEval
|
|
2345
|
+
|
|
2346
|
+
for (const fJog of fromJogs) {
|
|
2347
|
+
for (const tJog of toJogs) {
|
|
2348
|
+
const fx = fJog ? fJog[0] : from[0]
|
|
2349
|
+
const fz = fJog ? fJog[1] : from[2]
|
|
2350
|
+
const tx = tJog ? tJog[0] : to[0]
|
|
2351
|
+
const tz = tJog ? tJog[1] : to[2]
|
|
2352
|
+
const dynMids = buildObstacleDetourWaypoints([fx, fz], [tx, tz], bestClashes, edgePad)
|
|
2353
|
+
|
|
2354
|
+
const primaryCount = euclideanInCavity ? 3 : 2
|
|
2355
|
+
for (const py of planeYs) {
|
|
2356
|
+
// Try all zero-offset base modes (['diag',0], ['x',0], ['z',0]) and targeted obstacle detours before blind offsets
|
|
2357
|
+
for (const [m, off] of modes.slice(0, primaryCount)) {
|
|
2358
|
+
if (tryPressurized(buildCandidate(m, off, py, fJog, tJog))) break
|
|
2359
|
+
}
|
|
2360
|
+
if (isDone()) break
|
|
2361
|
+
for (const mid of dynMids) {
|
|
2362
|
+
if (tryPressurized(buildCandidate('x', 0, py, fJog, tJog, mid))) break
|
|
2363
|
+
}
|
|
2364
|
+
if (isDone()) break
|
|
2365
|
+
for (const [m, off] of modes.slice(primaryCount)) {
|
|
2366
|
+
if (tryPressurized(buildCandidate(m, off, py, fJog, tJog))) break
|
|
2367
|
+
}
|
|
2368
|
+
if (isDone()) break
|
|
2369
|
+
}
|
|
2370
|
+
if (isDone()) break
|
|
2371
|
+
}
|
|
2372
|
+
if (isDone()) break
|
|
2373
|
+
}
|
|
2374
|
+
|
|
2375
|
+
// Layer 2 — Legal-Space-First Cavity Rescue:
|
|
2376
|
+
// - Sub-floor planes (<= 0.1 m): strictly under-slab / crawlspace
|
|
2377
|
+
// - Low-elevation appliance runs (0.1 m < y <= 0.6 m, e.g. 4" dryer vent at 0.35 m): allow under-sill / mid-wall / overhead-plenum
|
|
2378
|
+
// - Overhead pressurized water/gas mains (> 0.6 m): strictly overhead-plenum / attic (never intrude on mid-wall/top-plate electrical chases)
|
|
2379
|
+
if ((bestClashes.length > 0 || bestBoundsViolation || bestRoofViolation) && hasCavityScene(cavityContext)) {
|
|
2380
|
+
const allowedBands: readonly CavityBand[] =
|
|
2381
|
+
params.allowedBands ??
|
|
2382
|
+
(routingPlaneY <= 0.1
|
|
2383
|
+
? ['under-slab', 'crawlspace']
|
|
2384
|
+
: routingPlaneY <= 0.6
|
|
2385
|
+
? ['under-sill', 'mid-wall', 'overhead-plenum']
|
|
2386
|
+
: ['overhead-plenum', 'attic'])
|
|
2387
|
+
const cav = routePlumbingViaLegalCavity({
|
|
2388
|
+
...cavityContext,
|
|
2389
|
+
from,
|
|
2390
|
+
to,
|
|
2391
|
+
mode: euclideanInCavity ? 'euclidean-cavity' : 'orthogonal',
|
|
2392
|
+
nominalSize,
|
|
2393
|
+
material,
|
|
2394
|
+
pipeRadiusM,
|
|
2395
|
+
allowedBands,
|
|
2396
|
+
obstacles,
|
|
2397
|
+
})
|
|
2398
|
+
if (cav.path.length >= 2) {
|
|
2399
|
+
tryPressurized(cav.path, true)
|
|
2400
|
+
}
|
|
2401
|
+
}
|
|
2402
|
+
}
|
|
2403
|
+
|
|
2404
|
+
return {
|
|
2405
|
+
path: bestPath,
|
|
2406
|
+
lengthFt: round2(polylineLengthM(bestPath) * METERS_TO_FEET),
|
|
2407
|
+
detoured,
|
|
2408
|
+
clashes: bestClashes,
|
|
2409
|
+
}
|
|
2410
|
+
}
|