Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/nodes/src/duct-segment/continuation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
type DuctFittingNode,
DuctSegmentNode,
type FloorplanAffordance,
type NodePort,
nodeRegistry,
useScene,
} from '@pascal-app/core'
Expand Down Expand Up @@ -217,6 +218,27 @@ export function activateDuctContinuation(
editor.setTool('duct-segment')
}

export function activateDuctCollarContinuation(node: AnyNode, port: NodePort): void {
const editor = useEditor.getState()
editor.setToolDefaults('duct-segment', {
...editor.toolDefaults['duct-segment'],
continuation: { nodeId: node.id, portId: port.id },
shape: port.shape ?? 'round',
diameter: port.diameter,
width: port.width ?? 14,
height: port.height ?? 8,
system: port.system,
ductMaterial: 'sheet-metal',
elevation: port.position[1],
routingMode:
Math.hypot(port.direction[0], port.direction[2]) < 1e-6 ? 'vertical' : 'horizontal',
targetElevation: port.position[1] + Math.sign(port.direction[1] || 1),
crossingMode: 'connect',
})
useViewer.getState().setSelection({ selectedIds: [] })
editor.setTool('duct-segment')
}

export const ductContinuationAffordance: FloorplanAffordance<DuctSegmentNode> = {
start({ node, payload }) {
const data = payload as { endpoint?: unknown; fittingId?: unknown } | null
Expand Down
11 changes: 11 additions & 0 deletions packages/nodes/src/duct-segment/tool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,17 @@ const DuctSegmentTool = () => {
delete: plan.delete,
})
pendingPromotionRef.current = null
const startOwner =
startConnection.port && !promotedFitting ? sceneApi.get(startConnection.port.nodeId) : null
// A riser out of a fixed collar reaches the target; staying vertical would aim the
// next section at the riser's own top.
if (
hangerDefaults?.routingMode === 'vertical' &&
startOwner &&
startOwner.type !== 'duct-segment' &&
startOwner.type !== 'duct-fitting'
)
updateRunDefaults('duct-segment', { routingMode: 'horizontal' })
const nextDuct = plan.ducts.at(-1)
const nextStart = nextDuct ? nextDuct.path[nextDuct.path.length - 1]! : end
const nextPort = nextDuct ? ductEndpointPort(nextDuct, 'end') : endConnection.port
Expand Down
34 changes: 33 additions & 1 deletion packages/nodes/src/pipe-segment/continuation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { type AnyNode, type AnyNodeId, type FloorplanAffordance, useScene } from '@pascal-app/core'
import {
type AnyNode,
type AnyNodeId,
type FloorplanAffordance,
type NodePort,
useScene,
} from '@pascal-app/core'
import { useEditor } from '@pascal-app/editor'
import type { RunBodyHit, ScenePort } from '@pascal-app/editor/distribution'
import { useViewer } from '@pascal-app/viewer'
Expand Down Expand Up @@ -191,6 +197,32 @@ export function activatePipeContinuation(
editor.setTool('pipe-segment')
}

export function activatePipeCollarContinuation(node: AnyNode, port: NodePort): void {
const editor = useEditor.getState()
editor.setToolDefaults('pipe-segment', {
...editor.toolDefaults['pipe-segment'],
continuation: { nodeId: node.id, portId: port.id },
diameter: port.diameter,
system: port.system,
pipeMaterial: 'pipeMaterial' in node ? node.pipeMaterial : 'pvc',
elevation: port.position[1],
routingMode:
Math.hypot(port.direction[0], port.direction[2]) < 1e-6 ? 'vertical' : 'horizontal',
targetElevation: port.position[1] + Math.sign(port.direction[1] || 1),
...(() => {
const grade = pipeGrade([0, 0, 0], [...port.direction]) ?? 0
return {
fall: Math.abs(grade) > 1e-6 ? 'sloped' : 'level',
gradePercent: Math.abs(grade) * 100,
flowDirection: grade < 0 ? 'reverse' : 'forward',
}
})(),
crossingMode: 'connect',
})
useViewer.getState().setSelection({ selectedIds: [] })
editor.setTool('pipe-segment')
}

export const pipeContinuationAffordance: FloorplanAffordance<PipeSegmentNode> = {
start({ node, payload }) {
const data = payload as { endpoint?: unknown; fittingId?: unknown } | null
Expand Down
4 changes: 4 additions & 0 deletions packages/nodes/src/pipe-segment/tool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,10 @@ const PipeSegmentTool = () => {
if (!sceneApi.applyChanges) throw new Error('Registry SceneApi must support atomic changes')
sceneApi.applyChanges(changes)
pendingPromotionRef.current = null
// A riser out of a fixed collar reaches the target; staying vertical would aim the
// next section at the riser's own top.
if (startCollar && hangerDefaults?.routingMode === 'vertical')
updateRunDefaults('pipe-segment', { routingMode: 'horizontal' })
}
const nextStart = nextPipe ? nextPipe.path[nextPipe.path.length - 1]! : end
const nextPort = nextPipe ? pipeEndpointPort(nextPipe, 'end') : endConnection.port
Expand Down
282 changes: 282 additions & 0 deletions packages/nodes/src/shared/collar-draw.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
import { afterEach, beforeEach, expect, spyOn, test } from 'bun:test'
import {
type AnyNode,
type AnyNodeId,
BuildingNode,
createSceneApi,
type DuctSegmentNode,
DuctTerminalNode,
emitter,
LevelNode,
type PipeFittingNode,
type PipeSegmentNode,
PipeTrapNode,
registerNode,
useScene,
} from '@pascal-app/core'
import {
type EditorGridEvent,
RegistryToolProvider,
useEditor,
useInteractionScope,
} from '@pascal-app/editor'
import { Html } from '@react-three/drei'
import { useThree } from '@react-three/fiber'
import { act, create } from '@react-three/test-renderer'
import type { ReactElement, ReactNode } from 'react'
import { ductFittingDefinition } from '../duct-fitting/definition'
import { activateDuctCollarContinuation } from '../duct-segment/continuation'
import { ductSegmentDefinition } from '../duct-segment/definition'
import DuctSegmentTool from '../duct-segment/tool'
import { ductTerminalDefinition } from '../duct-terminal/definition'
import { pipeFittingDefinition } from '../pipe-fitting/definition'
import { activatePipeCollarContinuation } from '../pipe-segment/continuation'
import { pipeSegmentDefinition } from '../pipe-segment/definition'
import { pipeGrade } from '../pipe-segment/slope'
import PipeSegmentTool from '../pipe-segment/tool'
import { pipeTrapDefinition } from '../pipe-trap/definition'

type Vec3 = readonly [number, number, number]

const building = BuildingNode.parse({})
const level = LevelNode.parse({ parentId: building.id })
const trap = PipeTrapNode.parse({
parentId: level.id,
position: [0, 0.5, 0],
diameter: 1.5,
pipeMaterial: 'abs',
})
const diffuser = DuctTerminalNode.parse({
parentId: level.id,
position: [4, 2.7, 0],
mount: 'ceiling',
terminalType: 'diffuser',
})
const trapPort = (id: 'inlet' | 'outlet') =>
pipeTrapDefinition.ports!(trap).find((p) => p.id === id)!

const labels: unknown[] = []
const textOf = (node: unknown): string => {
if (typeof node === 'string' || typeof node === 'number') return String(node)
if (Array.isArray(node)) return node.map(textOf).join(' ')
if (node && typeof node === 'object') {
const element = node as { props?: { children?: unknown }; children?: unknown }
return textOf(element.props?.children ?? element.children)
}
return ''
}
const shown = () => labels.map(textOf).join(' | ')

// A plan view at 100 px per metre, so a pointer on a collar snaps to it.
const plan = (x: number, z: number): EditorGridEvent => ({
position: [x, 0, z],
localPosition: [x, 0, z],
screenProjection: { pointer: [x * 100, z * 100], localToScreen: [100, 0, 0, 100, 0, 0] },
nativeEvent: {} as EditorGridEvent['nativeEvent'],
})
const move = (x: number, z: number) => act(async () => emitter.emit('grid:move', plan(x, z)))
const click = (x: number, z: number) => act(async () => emitter.emit('grid:click', plan(x, z)))
const setRunDefaults = (kind: 'pipe-segment' | 'duct-segment', patch: Record<string, unknown>) =>
act(async () => {
const editor = useEditor.getState()
editor.setToolDefaults(kind, { ...editor.toolDefaults[kind], ...patch })
})
const runDefaults = (kind: 'pipe-segment' | 'duct-segment') =>
useEditor.getState().toolDefaults[kind] ?? {}
const pipes = () =>
Object.values(useScene.getState().nodes).filter(
(node): node is PipeSegmentNode => node.type === 'pipe-segment',
)
const fittings = () =>
Object.values(useScene.getState().nodes).filter(
(node): node is PipeFittingNode => node.type === 'pipe-fitting',
)
const ducts = () =>
Object.values(useScene.getState().nodes).filter(
(node): node is DuctSegmentNode => node.type === 'duct-segment',
)
const expectPoint = (actual: Vec3 | undefined, expected: Vec3) => {
expect(actual).toBeDefined()
for (let i = 0; i < 3; i++) expect(actual![i]).toBeCloseTo(expected[i]!, 6)
}

function Viewport() {
const gl = useThree((state) => state.gl)
gl.domElement.getBoundingClientRect = () =>
({ left: 0, top: 0, width: 100, height: 100 }) as DOMRect
return null
}
const mountTool = (tool: ReactElement) =>
create(
<RegistryToolProvider
value={{
activeLevelId: level.id,
isCameraDragging: () => false,
sceneApi: createSceneApi(useScene),
selectNode: () => {},
unit: 'metric',
}}
>
<Viewport />
{tool}
</RegistryToolProvider>,
)

const globals = ['window', 'document', 'IS_REACT_ACT_ENVIRONMENT'] as const
let savedGlobals: (PropertyDescriptor | undefined)[] = []
let savedScope: ReturnType<typeof useInteractionScope.getState>
let htmlLabels: ReturnType<typeof spyOn>
beforeEach(() => {
savedGlobals = globals.map((name) => Object.getOwnPropertyDescriptor(globalThis, name))
// Key listeners attach to window; labels portal into document.body.
for (const [name, value] of [
['window', new EventTarget()],
['document', { body: { nodeType: 1, style: {} } }],
['IS_REACT_ACT_ENVIRONMENT', true],
] as const)
Object.defineProperty(globalThis, name, { value, configurable: true, writable: true })
savedScope = useInteractionScope.getState()
labels.length = 0
htmlLabels = spyOn(
Html as unknown as { render: (props: { children?: ReactNode }) => ReactNode },
'render',
).mockImplementation((props) => {
labels.push(props.children)
return null
})
for (const definition of [
pipeSegmentDefinition,
pipeFittingDefinition,
pipeTrapDefinition,
ductSegmentDefinition,
ductFittingDefinition,
ductTerminalDefinition,
])
registerNode(definition as never)
useScene.setState({
nodes: {
[building.id]: { ...building, children: [level.id] },
[level.id]: { ...level, children: [trap.id, diffuser.id] },
[trap.id]: trap,
[diffuser.id]: diffuser,
} as Record<AnyNodeId, AnyNode>,
rootNodeIds: [building.id],
dirtyNodes: new Set(),
readOnly: false,
})
useEditor.setState({
mode: 'build',
magneticSnap: true,
snappingModeByContext: { ...useEditor.getState().snappingModeByContext, wall: 'lines' },
})
})
afterEach(() => {
htmlLabels.mockRestore()
useInteractionScope.setState(savedScope, true)
globals.forEach((name, index) => {
const saved = savedGlobals[index]
if (saved) Object.defineProperty(globalThis, name, saved)
else Reflect.deleteProperty(globalThis, name)
})
})

test('a pipe run from a fixed collar leaves along its axis, then takes the chosen grade', async () => {
const [x, y, z] = trapPort('outlet').position
useEditor.setState({ tool: 'pipe-segment' })
useEditor.getState().setToolDefaults('pipe-segment', {
system: 'waste',
fall: 'sloped',
gradePercent: 2,
})
const renderer = await mountTool(<PipeSegmentTool />)
try {
await move(x, z)
await click(x, z)
// The horizontal outlet is level, so the run adopts that.
expect(runDefaults('pipe-segment')).toMatchObject({ fall: 'level', gradePercent: 0 })

await setRunDefaults('pipe-segment', { fall: 'sloped', gradePercent: 2 })
labels.length = 0
await move(x + 1, z + 0.4)
await click(x + 1, z + 0.4)
expect(pipes()).toHaveLength(0)
expect(shown()).toContain(
'The connection elevation conflicts with the chosen grade. Adjust the grade or target elevation.',
)
expect(shown()).not.toContain('Fitting clearance')

await setRunDefaults('pipe-segment', { fall: 'level' })
await click(x + 1, z + 0.4)
const [first] = pipes()
expectPoint(first?.path[0], [x, y, z])
expectPoint(first?.path.at(-1), [x + 1, y, z])

await setRunDefaults('pipe-segment', { fall: 'sloped', gradePercent: 2 })
await move(x + 3, z)
await click(x + 3, z)
const second = pipes()[1]
expectPoint(second?.path[0], [x + 1, y, z])
expect(pipeGrade(second!.path[0]!, second!.path.at(-1)!)).toBeCloseTo(0.02, 9)
// A 2% fall turns far less than any bend, so only the open end is capped.
expect(fittings().map((fitting) => fitting.fittingType)).toEqual(['end-cap'])
} finally {
await renderer.unmount()
}
})

test('a riser drawn from a vertical pipe collar continues on the plan route', async () => {
const inlet = trapPort('inlet')
const [x, y, z] = inlet.position
activatePipeCollarContinuation(trap, inlet)
expect(runDefaults('pipe-segment')).toMatchObject({ routingMode: 'vertical' })
const renderer = await mountTool(<PipeSegmentTool />)
try {
await click(x + 0.5, z)
expect(pipes()).toHaveLength(1)
expectPoint(pipes()[0]?.path.at(-1), [x, y + 1, z])
expect(runDefaults('pipe-segment').routingMode).toBe('horizontal')

await move(x + 1.5, z)
await click(x + 1.5, z)
const [riser, across] = pipes()
expectPoint(riser?.path[0], [x, y, z])
expectPoint(across?.path.at(-1), [x + 1.5, y + 1, z])
expect(pipeGrade(across!.path[0]!, across!.path.at(-1)!)).toBeCloseTo(0, 9)

await setRunDefaults('pipe-segment', { routingMode: 'vertical', targetElevation: y + 2 })
await click(x + 2, z)
expect(pipes()).toHaveLength(3)
expectPoint(pipes()[2]?.path.at(-1), [x + 1.5, y + 2, z])
expect(runDefaults('pipe-segment').routingMode).toBe('vertical')
} finally {
await renderer.unmount()
}
})

test('a riser drawn from a vertical duct collar continues on the horizontal route', async () => {
const collar = ductTerminalDefinition.ports!(diffuser)[0]!
const [x, y, z] = collar.position
activateDuctCollarContinuation(diffuser, collar)
expect(runDefaults('duct-segment')).toMatchObject({ routingMode: 'vertical' })
const renderer = await mountTool(<DuctSegmentTool />)
try {
await click(x + 0.5, z)
expect(ducts()).toHaveLength(1)
expectPoint(ducts()[0]?.path.at(-1), [x, y + 1, z])
expect(runDefaults('duct-segment').routingMode).toBe('horizontal')

await move(x + 1.5, z)
await click(x + 1.5, z)
const [riser, across] = ducts()
expectPoint(riser?.path[0], [x, y, z])
expectPoint(across?.path.at(-1), [x + 1.5, y + 1, z])

await setRunDefaults('duct-segment', { routingMode: 'vertical', targetElevation: y + 2 })
await click(x + 2, z)
expect(ducts()).toHaveLength(3)
expectPoint(ducts()[2]?.path.at(-1), [x + 1.5, y + 2, z])
expect(runDefaults('duct-segment').routingMode).toBe('vertical')
} finally {
await renderer.unmount()
}
})
Loading
Loading