diff --git a/packages/core/src/schema/index.ts b/packages/core/src/schema/index.ts index f619abdca4..715e3cbd82 100644 --- a/packages/core/src/schema/index.ts +++ b/packages/core/src/schema/index.ts @@ -285,6 +285,8 @@ export { getDutchRoofMetrics, getEffectiveSegmentSurfaceMaterial, getPitchFromActiveRoofHeight, + getRoofSegmentDeckReach, + getRoofSegmentEndReach, getRoofSegmentSurfaceY, getRoofSegmentVisibleTopBounds, getSegmentFasciaMaterial, @@ -297,6 +299,7 @@ export { RoofSegmentNode, RoofSegmentTrim, RoofType, + roofTypeHasRakes, } from './nodes/roof-segment' export type { DutchRoofShapeMetrics, diff --git a/packages/core/src/schema/nodes/gutter-defaults.test.ts b/packages/core/src/schema/nodes/gutter-defaults.test.ts index 40a503cb4f..975f6cfeb3 100644 --- a/packages/core/src/schema/nodes/gutter-defaults.test.ts +++ b/packages/core/src/schema/nodes/gutter-defaults.test.ts @@ -44,6 +44,25 @@ describe('createDefaultGuttersForSegment', () => { expect(right?.length).toBeCloseTo(6.52) }) + test("a gable's eave gutters run out to its rakes: the deck's edge, its own gable overhang", () => { + const segment = (gableOverhang?: number) => + RoofSegmentNode.parse({ + roofType: 'gable', + width: 8, + depth: 6, + overhang: 0.3, + wallThickness: 0.2, + gableOverhang, + }) + const front = (gableOverhang?: number) => + getGutterRunsForSegment(segment(gableOverhang)).find((run) => run.side === '+Z') + // without one, as far as the eaves in plan: half the wall, then the overhang along the 40° slope + expect(front()?.length).toBeCloseTo(2 * (4 + 0.1 + 0.3 * Math.cos((40 * Math.PI) / 180) - 0.04)) + // with one, the walls' half thickness and the gable overhang past their faces + expect(front(0.9)?.length).toBeCloseTo(2 * (4 + 0.1 + 0.9 - 0.04)) + expect(front(0)?.length).toBeCloseTo(2 * (4 + 0.1 - 0.04)) + }) + test('omits fully trimmed sides and shortens their adjacent eaves', () => { const segment = RoofSegmentNode.parse({ roofType: 'flat', @@ -81,11 +100,13 @@ describe('createDefaultGuttersForSegment', () => { (run) => run.side === '+Z', ) + // from the rake, the deck's edge past half the 0.1 m wall, to the cross gable's tucked eave + const rake = 4 + 0.05 + 0.3 * Math.cos((40 * Math.PI) / 180) - 0.04 expect(frontRuns).toHaveLength(2) - expect(frontRuns[0]?.position[0]).toBeCloseTo(-3.26) - expect(frontRuns[1]?.position[0]).toBeCloseTo(3.26) - expect(frontRuns[0]?.length).toBeCloseTo(2) - expect(frontRuns[1]?.length).toBeCloseTo(2) + expect(frontRuns[0]?.position[0]).toBeCloseTo(-(rake + 2.26) / 2) + expect(frontRuns[1]?.position[0]).toBeCloseTo((rake + 2.26) / 2) + expect(frontRuns[0]?.length).toBeCloseTo(rake - 2.26) + expect(frontRuns[1]?.length).toBeCloseTo(rake - 2.26) }) test('splits an eave around an attached roof-extension range', () => { diff --git a/packages/core/src/schema/nodes/gutter.ts b/packages/core/src/schema/nodes/gutter.ts index 3372108fa8..7d221bc646 100644 --- a/packages/core/src/schema/nodes/gutter.ts +++ b/packages/core/src/schema/nodes/gutter.ts @@ -2,7 +2,11 @@ import dedent from 'dedent' import { z } from 'zod' import { BaseNode, nodeType, objectId } from '../base' import { MaterialSchema } from '../material' -import { normalizeRoofSegmentTrim, type RoofSegmentNode } from './roof-segment' +import { + getRoofSegmentEndReach, + normalizeRoofSegmentTrim, + type RoofSegmentNode, +} from './roof-segment' import { getRoofShapeEaveSides } from './roof-segment-shape' const MIN_DEFAULT_GUTTER_LENGTH_M = 0.2 @@ -152,7 +156,11 @@ function getGutterEnvelope(segment: RoofSegmentNode) { const halfW = Math.max(0, segment.width) / 2 const halfD = Math.max(0, segment.depth) / 2 const overhang = Math.max(0, segment.overhang ?? 0) - const outerHalfW = Math.max(halfW, halfW + overhang - GUTTER_EAVE_TUCK_INWARD) + // an eave gutter runs out to the rakes, as far as they reach + const outerHalfW = Math.max( + halfW, + halfW + getRoofSegmentEndReach(segment) - GUTTER_EAVE_TUCK_INWARD, + ) const outerHalfD = Math.max(halfD, halfD + overhang - GUTTER_EAVE_TUCK_INWARD) const trim = normalizeRoofSegmentTrim(segment) const minX = trim.left > 0 ? -halfW + trim.left : -outerHalfW diff --git a/packages/core/src/schema/nodes/roof-segment-deck-reach.test.ts b/packages/core/src/schema/nodes/roof-segment-deck-reach.test.ts new file mode 100644 index 0000000000..e2d7dfab7f --- /dev/null +++ b/packages/core/src/schema/nodes/roof-segment-deck-reach.test.ts @@ -0,0 +1,52 @@ +import { describe, expect, test } from 'bun:test' +import { + getRoofSegmentDeckReach, + getRoofSegmentVisibleTopBounds, + RoofSegmentNode, +} from './roof-segment' + +const COS_6_12 = Math.cos(Math.atan(0.5)) + +function segment(patch: Partial): RoofSegmentNode { + return RoofSegmentNode.parse({ + roofType: 'gable', + width: 12, + depth: 8, + pitch: (Math.atan(0.5) * 180) / Math.PI, + wallThickness: 0.2, + overhang: 0.5, + shingleThickness: 0, + ...patch, + }) +} + +describe('a roof segment deck reach', () => { + test('without a gable overhang, every edge reaches as far as the eaves, in plan', () => { + const reach = getRoofSegmentDeckReach(segment({})) + expect(reach.z).toBeCloseTo(0.1 + 0.5 * COS_6_12, 9) + expect(reach.x).toBeCloseTo(reach.z, 9) + }) + + test("a gable's, a gambrel's and a shed's ends reach their gable overhang past the walls' outside faces", () => { + for (const roofType of ['gable', 'gambrel', 'shed'] as const) { + const reach = getRoofSegmentDeckReach(segment({ roofType, gableOverhang: 0.9 })) + expect(reach.x).toBeCloseTo(0.1 + 0.9, 9) + expect(reach.z).toBeCloseTo(0.1 + 0.5 * COS_6_12, 9) + } + expect(getRoofSegmentDeckReach(segment({ gableOverhang: 0 })).x).toBeCloseTo(0.1, 9) + }) + + test('a hip, a flat roof and the other forms have no gable end: the field is inert', () => { + for (const roofType of ['hip', 'flat', 'mansard', 'dutch'] as const) { + const reach = getRoofSegmentDeckReach(segment({ roofType, gableOverhang: 0.9 })) + expect(reach.x).toBeCloseTo(reach.z, 9) + } + }) + + test('the visible top reaches the gable overhang at the ends and the eaves along the sides', () => { + const bounds = getRoofSegmentVisibleTopBounds(segment({ gableOverhang: 0.9 })) + expect(bounds.maxX).toBeCloseTo(6 + 0.1 + 0.9, 9) + expect(bounds.minX).toBeCloseTo(-(6 + 0.1 + 0.9), 9) + expect(bounds.maxZ).toBeCloseTo(4 + 0.1 + 0.5 * COS_6_12, 9) + }) +}) diff --git a/packages/core/src/schema/nodes/roof-segment.ts b/packages/core/src/schema/nodes/roof-segment.ts index 946e301e45..67beb65a09 100644 --- a/packages/core/src/schema/nodes/roof-segment.ts +++ b/packages/core/src/schema/nodes/roof-segment.ts @@ -132,6 +132,9 @@ export const RoofSegmentNode = BaseNode.extend({ wallThickness: z.number().default(0.1), deckThickness: z.number().default(0.1), overhang: z.number().default(0.3), + // How far the roof reaches past the gable-end walls' outside faces, in plan (a gable's or + // gambrel's ends, a shed's sides). Absent, as far as the eaves reach (`overhang` is along the slope). + gableOverhang: z.number().min(0).optional(), shingleThickness: z.number().default(0.05), arc: z .object({ @@ -243,7 +246,8 @@ export const RoofSegmentNode = BaseNode.extend({ - wallHeight: height of walls below the roof - pitch: roof slope in degrees (angle of the primary slope face) - wallThickness/deckThickness: structural thicknesses - - overhang: eave overhang distance + - overhang: eave overhang distance, along the slope + - gableOverhang: reach past the gable-end walls in plan (gable, gambrel, shed); absent = the eaves' reach - shingleThickness: outer shingle layer thickness - fascia: plumb fascia and rake boards on the eaves and rakes (gable, hip, shed, flat) (off when absent) - fasciaHighEdge: shed only — a board on the high edge (off when it meets a wall; on when absent) @@ -613,20 +617,55 @@ export type RoofSegmentVisibleTopBounds = { depth: number } +/** Whether a form's ends along the segment's width are rakes, not eaves: they take `gableOverhang`. */ +export function roofTypeHasRakes(roofType: RoofType): boolean { + return roofType === 'gable' || roofType === 'gambrel' || roofType === 'shed' +} + +/** + * How far a segment's deck reaches past its wall centre lines in plan, along its own x (the ridge, + * where a gable, gambrel or shed has its rakes) and z (the eaves): half the wall's thickness, then + * the eaves' overhang, which the node stores along the slope, or at the rakes its `gableOverhang`. + */ +export function getRoofSegmentDeckReach( + segment: Pick & + Partial>, +): { x: number; z: number } { + const half = finiteNonNegative(segment.wallThickness) / 2 + const z = half + finiteNonNegative(segment.overhang) * getSegmentSlopeFrame(segment).cosTheta + const gable = segment.gableOverhang + const raked = roofTypeHasRakes(segment.roofType) && Number.isFinite(gable) + return { x: raked ? half + finiteNonNegative(gable) : z, z } +} + +/** + * How far a segment's ends along its width reach past its wall centre lines in plan, for the readers + * that measure its eaves by the stored `overhang` (gutters, lean-to attachments, the sheets): a + * gable's, gambrel's or shed's ends are rakes, at the deck's edge (`getRoofSegmentDeckReach`); the + * other forms' ends are eaves like their sides, at `overhang`. + */ +export function getRoofSegmentEndReach( + segment: Pick & + Partial>, +): number { + return roofTypeHasRakes(segment.roofType) + ? getRoofSegmentDeckReach(segment).x + : finiteNonNegative(segment.overhang) +} + export function getRoofSegmentVisibleTopBounds( segment: RoofSegmentNode, ): RoofSegmentVisibleTopBounds { - const { activeRh, cosTheta, sinTheta } = getSegmentSlopeFrame(segment) + const { activeRh, sinTheta } = getSegmentSlopeFrame(segment) const width = finitePositive(segment.width, DEFAULT_ROOF_SEGMENT_WIDTH) const depth = finitePositive(segment.depth, DEFAULT_ROOF_SEGMENT_DEPTH) const trim = normalizeRoofSegmentTrim({ ...segment, width, depth }) - const horizontalOverhang = finiteNonNegative(segment.overhang) * cosTheta - const deckExt = finiteNonNegative(segment.wallThickness) / 2 + horizontalOverhang + const reach = getRoofSegmentDeckReach(segment) const shingleOverhang = finiteNonNegative(segment.shingleThickness) * sinTheta - let xExt = deckExt - let frontExt = deckExt - let backExt = deckExt + let xExt = reach.x + let frontExt = reach.z + let backExt = reach.z if ( segment.roofType === 'hip' || diff --git a/packages/core/src/services/roof-bearing.ts b/packages/core/src/services/roof-bearing.ts index 2be593c83b..e60ac94614 100644 --- a/packages/core/src/services/roof-bearing.ts +++ b/packages/core/src/services/roof-bearing.ts @@ -4,6 +4,7 @@ import { type AnyNode, type AnyNodeId, getConicalRoofCoverage, + getRoofSegmentDeckReach, getSegmentSlopeFrame, type LevelNode, type SlabNode, @@ -61,14 +62,14 @@ export function wallSpans(nodes: Nodes, levelId: string): Map ) } -/** A roof mass in plan: its centre, turn and half sizes at the wall centre lines, how far its eaves reach past them, its form and how high it peaks over its eave. */ +/** A roof mass in plan: its centre, turn and half sizes at the wall centre lines, how far its deck reaches past them along its ridge and across it, its form and how high it peaks over its eave. */ export type RoofShape = { cx: number cz: number yaw: number hw: number hd: number - reach: number + reach: { x: number; z: number } roofType: string peak: number /** A cone clipped to a sector, in its own frame (radians); none for a full cone. */ @@ -93,7 +94,7 @@ export function shapeOfRect( yaw: ridgeAxis === 'x' ? 0 : -Math.PI / 2, hw: width / 2, hd: depth / 2, - reach: 0, + reach: { x: 0, z: 0 }, roofType, peak: getSegmentSlopeFrame({ roofType, pitch: pitchDeg, width, depth } as never).activeRh, } @@ -113,7 +114,7 @@ export function segmentShape(nodes: Nodes, segmentId: string): RoofShape | null yaw: roof.rotation + segment.rotation, hw: segment.width / 2, hd: segment.depth / 2, - reach: segment.wallThickness / 2 + segment.overhang * frame.cosTheta, + reach: getRoofSegmentDeckReach(segment), roofType: segment.roofType, peak: frame.activeRh, ...(coverage && !coverage.fullCircle diff --git a/packages/core/src/store/actions/gutter-update.test.ts b/packages/core/src/store/actions/gutter-update.test.ts index 13dc7ac49a..d6b723141e 100644 --- a/packages/core/src/store/actions/gutter-update.test.ts +++ b/packages/core/src/store/actions/gutter-update.test.ts @@ -220,6 +220,31 @@ describe('roof segment default gutters', () => { }) }) + test("lengthens a gable's eave gutters when its walls thicken: they run out to the rakes", () => { + const segment = RoofSegmentNode.parse({ + id: 'rseg_test' as never, + roofType: 'gable', + width: 8, + wallThickness: 0.1, + gableOverhang: 0.6, + metadata: { autoGutter: true }, + }) + setRoofScene(segment) + useScene.getState().updateNode(segment.id as AnyNodeId, { depth: 6 } as Partial) + const front = () => + generatedGutters(useScene.getState().nodes[segment.id as AnyNodeId] as RoofSegmentNode).find( + (gutter) => getDefaultGutterSide(gutter) === '+Z', + )! + const before = front().length + + useScene + .getState() + .updateNode(segment.id as AnyNodeId, { wallThickness: 0.3 } as Partial) + + // the rakes reach half a wall and the gable overhang past the centre lines: 0.1 m further at each end + expect(front().length).toBeCloseTo(before + 0.2, 6) + }) + test('refreshes sibling gutters when an intersecting segment moves', () => { const segment = RoofSegmentNode.parse({ id: 'rseg_main' as never, @@ -259,9 +284,11 @@ describe('roof segment default gutters', () => { const front = generatedGutters(current).filter( (gutter) => getDefaultGutterSide(gutter) === '+Z', ) + // from the rake, the deck's edge past half the 0.1 m wall, to the cross gable's tucked eave + const rake = 4 + 0.05 + 0.3 * Math.cos((40 * Math.PI) / 180) - 0.04 expect(front).toHaveLength(2) - expect(front[0]?.length).toBeCloseTo(2) - expect(front[1]?.length).toBeCloseTo(2) + expect(front[0]?.length).toBeCloseTo(rake - 2.26) + expect(front[1]?.length).toBeCloseTo(rake - 2.26) }) test('refreshes existing gutters when a sibling segment is added and removed', () => { diff --git a/packages/core/src/store/actions/node-actions.ts b/packages/core/src/store/actions/node-actions.ts index 61b83fd954..6692510d76 100644 --- a/packages/core/src/store/actions/node-actions.ts +++ b/packages/core/src/store/actions/node-actions.ts @@ -84,6 +84,7 @@ const DEFAULT_RIDGE_VENT_REFRESH_FIELDS = new Set([ 'depth', 'pitch', 'overhang', + 'gableOverhang', 'wallThickness', 'shingleThickness', 'gambrelLowerWidthRatio', @@ -103,6 +104,8 @@ const DEFAULT_GUTTER_REFRESH_FIELDS = new Set([ 'wallHeight', 'pitch', 'overhang', + 'gableOverhang', + 'wallThickness', 'trim', ]) diff --git a/packages/core/src/store/actions/ridge-vent-update.test.ts b/packages/core/src/store/actions/ridge-vent-update.test.ts index 19bfac4b51..a24a993fac 100644 --- a/packages/core/src/store/actions/ridge-vent-update.test.ts +++ b/packages/core/src/store/actions/ridge-vent-update.test.ts @@ -91,6 +91,44 @@ describe('roof segment default ridge vents', () => { ).toBe(true) }) + test('regenerates default ridge vents when the gable overhang changes', () => { + const roof = RoofNode.parse({ id: 'roof_test' as never, children: [] }) + const segment = RoofSegmentNode.parse({ + id: 'rseg_test' as never, + parentId: roof.id, + roofType: 'gable', + width: 12, + depth: 8, + gableOverhang: 0, + }) + const defaults = createDefaultRidgeVentsForSegment(segment) + useScene.getState().setScene( + { + [roof.id]: { ...roof, children: [segment.id] } as AnyNode, + [segment.id]: { ...segment, children: defaults.map((vent) => vent.id) } as AnyNode, + ...Object.fromEntries( + defaults.map((vent) => [ + vent.id, + { ...vent, parentId: segment.id, roofSegmentId: segment.id } as AnyNode, + ]), + ), + } as Record, + [roof.id as AnyNodeId], + ) + + useScene + .getState() + .updateNode(segment.id as AnyNodeId, { gableOverhang: 0.9 } as Partial) + + const next = useScene.getState().nodes[segment.id as AnyNodeId] as RoofSegmentNode + const lengths = next.children + .map((id) => useScene.getState().nodes[id as AnyNodeId]) + .flatMap((node) => (node?.type === 'ridge-vent' ? [node.length] : [])) + const expected = createDefaultRidgeVentsForSegment(next).map((vent) => vent.length) + expect(lengths).toEqual(expected) + expect(lengths[0]).toBeGreaterThan(defaults[0]?.length ?? 0) + }) + test('does not regenerate default ridge vents when only trim changes', () => { const roof = RoofNode.parse({ id: 'roof_test' as never, children: [] }) const segment = RoofSegmentNode.parse({ diff --git a/packages/editor/src/components/editor/selection-manager.tsx b/packages/editor/src/components/editor/selection-manager.tsx index 217b557be8..118536c067 100644 --- a/packages/editor/src/components/editor/selection-manager.tsx +++ b/packages/editor/src/components/editor/selection-manager.tsx @@ -8,6 +8,7 @@ import { type GridEvent, getEffectiveRoofSurfaceMaterial, getEffectiveSegmentSurfaceMaterial, + getRoofSegmentDeckReach, getRoofSegmentSurfaceY, getSelectableKinds, type ItemNode, @@ -420,9 +421,9 @@ export function resolveRoofSegmentSelectionTarget(event: NodeEvent): RoofSegment object.updateWorldMatrix(true, false) const local = object.worldToLocal(roofSelectionWorldPoint.clone()) - const overhang = segment.overhang ?? 0 - const halfWidth = segment.width / 2 + overhang - const halfDepth = segment.depth / 2 + overhang + const reach = getRoofSegmentDeckReach(segment) + const halfWidth = segment.width / 2 + reach.x + const halfDepth = segment.depth / 2 + reach.z if (Math.abs(local.x) > halfWidth || Math.abs(local.z) > halfDepth) { continue diff --git a/packages/editor/src/components/systems/roof/roof-edit-system.tsx b/packages/editor/src/components/systems/roof/roof-edit-system.tsx index bd4ba106a3..ea90db78e5 100644 --- a/packages/editor/src/components/systems/roof/roof-edit-system.tsx +++ b/packages/editor/src/components/systems/roof/roof-edit-system.tsx @@ -511,6 +511,7 @@ function segmentShapeKey(segment: RoofSegmentNode): string { segment.wallThickness, segment.deckThickness, segment.overhang, + segment.gableOverhang, segment.shingleThickness, segment.gambrelLowerWidthRatio, segment.gambrelLowerHeightRatio, diff --git a/packages/editor/src/lib/floorplan/site-plan/site-parts.ts b/packages/editor/src/lib/floorplan/site-plan/site-parts.ts index c90076c1fd..78af48aa40 100644 --- a/packages/editor/src/lib/floorplan/site-plan/site-parts.ts +++ b/packages/editor/src/lib/floorplan/site-plan/site-parts.ts @@ -10,8 +10,10 @@ import { type AnyNode, type AnyNodeId, type BuildingNode, + getRoofSegmentDeckReach, type LevelNode, migrateSiteMetadata, + type RoofSegmentNode, type SceneSnapshot, type SiteNode, siteFrame, @@ -69,9 +71,9 @@ export function findLowestLevel( } /** - * The roof's outline on the lot — every roof segment's plan rectangle plus - * its overhang, through the segment's, the roof's and the building's turns, - * unioned into the outer ring(s). Dashed on the site plan: what the eye sees + * The roof's outline on the lot — every roof segment's plan rectangle out to + * its deck's reach at its eaves and rakes, through the segment's, the roof's + * and the building's turns, unioned into the outer ring(s). Dashed on the site plan: what the eye sees * from above is the roof, and the setback is measured to the wall under it. */ export function roofOutlineRings( @@ -93,15 +95,10 @@ export function roofOutlineRings( if (node.type !== 'roof-segment') continue const roof = roofs.get(String((node as { parentId?: unknown }).parentId)) if (!roof) continue - const seg = node as unknown as { - position?: number[] - rotation?: unknown - width?: number - depth?: number - overhang?: number - } - const w = (seg.width ?? 0) / 2 + (seg.overhang ?? 0) - const d = (seg.depth ?? 0) / 2 + (seg.overhang ?? 0) + const seg = node as RoofSegmentNode + const reach = getRoofSegmentDeckReach(seg) + const w = (seg.width ?? 0) / 2 + reach.x + const d = (seg.depth ?? 0) / 2 + reach.z if (w <= 0 || d <= 0) continue const segYaw = yawOf(seg.rotation) const roofYaw = yawOf(roof.rotation) diff --git a/packages/editor/src/lib/print-roof-solids.test.ts b/packages/editor/src/lib/print-roof-solids.test.ts index 0ffbc90a0a..025607ce78 100644 --- a/packages/editor/src/lib/print-roof-solids.test.ts +++ b/packages/editor/src/lib/print-roof-solids.test.ts @@ -68,6 +68,21 @@ describe('buildPrintableRoofSegmentSolids', () => { } }) + test("a gable's print shell reaches its gable overhang at the ends, the eaves along the sides", () => { + const extent = (node: RoofSegmentNode) => { + const mesh = onlyMesh(buildPrintableRoofSegmentSolids(node).object!) + mesh.geometry.computeBoundingBox() + const box = mesh.geometry.boundingBox!.clone() + mesh.geometry.dispose() + return box + } + const plain = extent(fixture('gable')) + const raked = extent(fixture('gable', { gableOverhang: 0.6 })) + expect(raked.max.x).toBeCloseTo(4 / 2 + 0.15 / 2 + 0.6, 6) + expect(raked.min.x).toBeCloseTo(-(4 / 2 + 0.15 / 2 + 0.6), 6) + expect(raked.max.z).toBeCloseTo(plain.max.z, 6) + }) + test('preserves segment provenance and local transform', () => { const node = fixture('gable', { position: [1, 2, 3], rotation: Math.PI / 3 }) const result = buildPrintableRoofSegmentSolids(node) diff --git a/packages/editor/src/lib/print-roof-solids.ts b/packages/editor/src/lib/print-roof-solids.ts index 44672ca0aa..6f1467065f 100644 --- a/packages/editor/src/lib/print-roof-solids.ts +++ b/packages/editor/src/lib/print-roof-solids.ts @@ -2,6 +2,7 @@ import { type AnyNode, getConicalRoofCoverage, getRoofModuleFaces, + getRoofSegmentDeckReach, getRoofShapeInsets, getRoofShapeRatios, getSegmentSlopeFrame, @@ -411,11 +412,17 @@ function buildRoofModuleGeometry( function getVolumeFaces( node: RoofSegmentNode, - options: { widthExtension: number; verticalOffset: number; isVoid: boolean }, + options: { + widthExtension: number + /** How far it reaches along x, where a gable's rakes are; `widthExtension` when absent. */ + rakeExtension?: number + verticalOffset: number + isVoid: boolean + }, ): RoofFace[] { const conicalCoverage = getConicalRoofCoverage(node) const { activeRh, tanTheta } = getSegmentSlopeFrame(node) - const width = Math.max(0.01, node.width + options.widthExtension * 2) + const width = Math.max(0.01, node.width + (options.rakeExtension ?? options.widthExtension) * 2) const depth = Math.max(0.01, node.depth + options.widthExtension * 2) const autoDrop = options.widthExtension * tanTheta const wallHeight = Math.max(0.01, node.wallHeight - autoDrop + options.verticalOffset) @@ -448,13 +455,13 @@ function getShingleOuterFaces(node: RoofSegmentNode): RoofFace[] { const conicalCoverage = getConicalRoofCoverage(node) const { activeRh, tanTheta, cosTheta, sinTheta } = getSegmentSlopeFrame(node) const shapeRatios = getRoofShapeRatios(node) - const horizontalOverhang = node.overhang * cosTheta - const deckExtension = node.wallThickness / 2 + horizontalOverhang + const reach = getRoofSegmentDeckReach(node) + const deckExtension = reach.z const deckVerticalThickness = activeRh > 0 ? node.deckThickness / cosTheta : node.deckThickness const deckDrop = deckExtension * tanTheta const shingleHorizontalThickness = node.shingleThickness * sinTheta const shingleVerticalThickness = node.shingleThickness * cosTheta - const baseWidth = Math.max(0.01, node.width + deckExtension * 2) + const baseWidth = Math.max(0.01, node.width + reach.x * 2) const baseDepth = Math.max(0.01, node.depth + deckExtension * 2) const baseWallHeight = node.wallHeight - deckDrop + deckVerticalThickness const baseRoofHeight = @@ -582,7 +589,6 @@ export function buildPrintableRoofSegmentSolids( const diagnostics = blockingDiagnostics(node, nodes) if (diagnostics.length > 0) return { status: 'blocked', object: null, diagnostics } - const { cosTheta } = getSegmentSlopeFrame(node) const wallOuter = getVolumeFaces(node, { widthExtension: node.wallThickness / 2, verticalOffset: 0, @@ -593,9 +599,10 @@ export function buildPrintableRoofSegmentSolids( verticalOffset: 0, isVoid: false, }) - const deckExtension = node.wallThickness / 2 + node.overhang * cosTheta + const reach = getRoofSegmentDeckReach(node) const roofInner = getVolumeFaces(node, { - widthExtension: deckExtension, + widthExtension: reach.z, + rakeExtension: reach.x, verticalOffset: 0, isVoid: true, }) diff --git a/packages/nodes/src/lean-to-extension/placement-validation.ts b/packages/nodes/src/lean-to-extension/placement-validation.ts index e3ef5bfbd2..12a29b7d23 100644 --- a/packages/nodes/src/lean-to-extension/placement-validation.ts +++ b/packages/nodes/src/lean-to-extension/placement-validation.ts @@ -4,6 +4,7 @@ import { type BuildingNode, getActiveRoofHeight, getLevelElevations, + getRoofSegmentEndReach, getWallFaceOffsets, type LeanToExtensionNode, type RoofNode, @@ -293,7 +294,7 @@ function roofSegmentWorldBounds( } function roofSegmentLevelPoints(roof: RoofNode, segment: RoofSegmentNode): [number, number][] { - const halfX = segment.width / 2 + segment.overhang + const halfX = segment.width / 2 + getRoofSegmentEndReach(segment) const halfZ = segment.depth / 2 + segment.overhang const segmentCos = Math.cos(segment.rotation) const segmentSin = Math.sin(segment.rotation) diff --git a/packages/nodes/src/lean-to-extension/roof-attachment.ts b/packages/nodes/src/lean-to-extension/roof-attachment.ts index e9d6aa4cdc..74a7ba8e50 100644 --- a/packages/nodes/src/lean-to-extension/roof-attachment.ts +++ b/packages/nodes/src/lean-to-extension/roof-attachment.ts @@ -2,6 +2,7 @@ import { type AnyNode, type AnyNodeId, getLevelElevations, + getRoofSegmentEndReach, getWallBaseElevationForNodes, getWallCurveFrameAt, getWallCurveLength, @@ -80,7 +81,7 @@ function edgeEndpoints( segment: RoofSegmentNode, edge: LeanToRoofEdge, ): readonly [[number, number], [number, number]] { - const halfWidth = segment.width / 2 + Math.max(0, segment.overhang ?? 0) + const halfWidth = segment.width / 2 + getRoofSegmentEndReach(segment) const halfDepth = segment.depth / 2 + Math.max(0, segment.overhang ?? 0) switch (edge) { case '+X': diff --git a/packages/nodes/src/ridge-vent/renderer.tsx b/packages/nodes/src/ridge-vent/renderer.tsx index 2a5b40500f..78f0fb200e 100644 --- a/packages/nodes/src/ridge-vent/renderer.tsx +++ b/packages/nodes/src/ridge-vent/renderer.tsx @@ -40,6 +40,7 @@ function ridgeVentSegmentGeometryKey(segment: RoofSegmentNode | undefined): stri segment.wallThickness, segment.deckThickness, segment.overhang, + segment.gableOverhang, segment.shingleThickness, segment.gambrelLowerWidthRatio, segment.gambrelLowerHeightRatio, diff --git a/packages/nodes/src/roof-segment/panel.tsx b/packages/nodes/src/roof-segment/panel.tsx index 7a1b428a59..2a2083e555 100644 --- a/packages/nodes/src/roof-segment/panel.tsx +++ b/packages/nodes/src/roof-segment/panel.tsx @@ -5,6 +5,8 @@ import { type AnyNodeId, createDefaultRidgeVentsForSegment, getConicalRoofCoverage, + getRoofSegmentDeckReach, + getSegmentSlopeFrame, isAutoGutterEnabled, isAutoRidgeVentEnabled, isDefaultRidgeVentNode, @@ -13,6 +15,7 @@ import { type RoofSegmentNode, type RoofSegmentTrim, type RoofType, + roofTypeHasRakes, useScene, } from '@pascal-app/core' import { @@ -300,6 +303,10 @@ export default function RoofSegmentPanel() { const showTrimPlanes = shouldShowTrimPlanes(node.metadata) const managedLeanToRoofSegment = isManagedLeanToRoofSegment(node.metadata) const conicalCoverage = getConicalRoofCoverage(node) + // both overhangs in plan, past the walls' outside faces; the node keeps the eaves' along the slope + const reach = getRoofSegmentDeckReach(node) + const halfWall = node.wallThickness / 2 + const { cosTheta } = getSegmentSlopeFrame(node) return ( handleUpdate({ overhang: v })} + onChange={(v) => handleUpdate({ overhang: v / cosTheta })} precision={2} step={0.05} unit="m" - value={node.overhang} + value={reach.z - halfWall} /> + {roofTypeHasRakes(node.roofType) && ( + handleUpdate({ gableOverhang: v })} + precision={2} + step={0.05} + unit="m" + value={reach.x - halfWall} + /> + )} = { groups: [ @@ -143,6 +143,16 @@ export const roofSegmentParametrics: ParametricDescriptor = { step: 0.01, }, { key: 'overhang', kind: 'number', unit: 'm', min: 0, max: 1, step: 0.05 }, + { + key: 'gableOverhang', + kind: 'number', + label: 'Gable overhang', + unit: 'm', + min: 0, + max: 3, + step: 0.05, + visibleIf: (node) => roofTypeHasRakes(node.roofType), + }, { key: 'shingleThickness', kind: 'number', diff --git a/packages/nodes/src/roof/roof-level-target.ts b/packages/nodes/src/roof/roof-level-target.ts index 8f49902180..328224fe79 100644 --- a/packages/nodes/src/roof/roof-level-target.ts +++ b/packages/nodes/src/roof/roof-level-target.ts @@ -53,7 +53,7 @@ function overStorey( yaw: 0, hw: drawn.width / 2, hd: drawn.depth / 2, - reach: 0, + reach: { x: 0, z: 0 }, roofType: drawn.roofType, peak: 0, }) diff --git a/packages/nodes/src/shared/roof-segment-hit.ts b/packages/nodes/src/shared/roof-segment-hit.ts index b5f9e7496d..b8e7ebc423 100644 --- a/packages/nodes/src/shared/roof-segment-hit.ts +++ b/packages/nodes/src/shared/roof-segment-hit.ts @@ -1,5 +1,6 @@ import { type AnyNodeId, + getRoofSegmentDeckReach, getRoofSegmentSurfaceY, type RoofNode, type RoofSegmentNode, @@ -64,9 +65,9 @@ export function resolveRoofSegmentHit( if (!firstSegment) firstSegment = { seg, segObj } - const overhang = seg.overhang ?? 0 - const halfW = seg.width / 2 + overhang - const halfD = seg.depth / 2 + overhang + const reach = getRoofSegmentDeckReach(seg) + const halfW = seg.width / 2 + reach.x + const halfD = seg.depth / 2 + reach.z if (Math.abs(local.x) <= halfW && Math.abs(local.z) <= halfD) { const surfaceY = getRoofSegmentSurfaceY(seg, local.x, local.z) const score = Math.abs(local.y - surfaceY) diff --git a/packages/nodes/src/shared/roof-surface.ts b/packages/nodes/src/shared/roof-surface.ts index 8b56cbf847..4ce4bd1fcd 100644 --- a/packages/nodes/src/shared/roof-surface.ts +++ b/packages/nodes/src/shared/roof-surface.ts @@ -1,6 +1,7 @@ import { getConicalRoofCoverage, getRoofModuleFaces, + getRoofSegmentDeckReach, getRoofSegmentSurfaceY, getRoofShapeInsets, getRoofShapeRatios, @@ -100,6 +101,7 @@ function roofSurfaceFaceCacheKey(segment: RoofSegmentNode): string { segment.wallThickness, segment.deckThickness, segment.overhang, + segment.gableOverhang, segment.shingleThickness, segment.pitch, conicalCoverage.startAngle, @@ -116,17 +118,17 @@ function roofSurfaceFaceCacheKey(segment: RoofSegmentNode): string { function buildRoofSurfaceFaces(segment: RoofSegmentNode): RoofSurfaceFace[] { const conicalCoverage = getConicalRoofCoverage(segment) - const { roofType, width, depth, wallHeight, wallThickness, deckThickness, overhang } = segment + const { roofType, width, depth, wallHeight, deckThickness } = segment const { activeRh, tanTheta, cosTheta, sinTheta } = getSegmentSlopeFrame(segment) const verticalRt = activeRh > 0 ? deckThickness / cosTheta : deckThickness - const horizontalOverhang = (overhang ?? 0) * cosTheta - const deckExt = wallThickness / 2 + horizontalOverhang + const reach = getRoofSegmentDeckReach(segment) + const deckExt = reach.z const shingleThickness = segment.shingleThickness ?? 0 const stSin = shingleThickness * sinTheta const stCos = shingleThickness * cosTheta - const shinBotW = Math.max(0.01, width + 2 * deckExt) + const shinBotW = Math.max(0.01, width + 2 * reach.x) const shinBotD = Math.max(0.01, depth + 2 * deckExt) const deckDrop = deckExt * tanTheta const shinBotWh = wallHeight - deckDrop + verticalRt diff --git a/packages/viewer/src/systems/roof/__fixtures__/roof-plane-cases.ts b/packages/viewer/src/systems/roof/__fixtures__/roof-plane-cases.ts new file mode 100644 index 0000000000..efc29ba49b --- /dev/null +++ b/packages/viewer/src/systems/roof/__fixtures__/roof-plane-cases.ts @@ -0,0 +1,460 @@ +/** + * Every roof the hosted Architect plugin's `hip_roof_planes` and `gable_roof_planes` built, each + * beside the `roof_segment_add` call that covers it and the roof segment that call writes, so the + * two plane tools can retire into it (catalogue slice R). No stored thread called either tool, so + * the cases are what their contracts take (both ridge axes, a hip either way and square, the + * default, wide, flush and wider-than-the-eaves gable overhangs, a thickness, an eave on the plate, + * over it and on posts) and the plugin's own call (a hip at the walls' 2.6 m tops). + * + * The plane tools took the rectangle of the walls' outside faces, a rise per 12, the overhang past + * those faces in metres (a gable's ends their own), a thickness square to the slope (0.29 m by + * default) and an eave height that was the roof's SURFACE at the outside wall face. The segment + * takes the same rectangle (`rectAt: 'outside'`), the pitch, the overhangs and the thickness in + * any unit, and an eave that is the plate it bears on, its default the walls' tops; the walls' + * thickness, which it reads from the walls along those faces (6.5 in where none stand, a porch on + * posts), puts their centre lines inside the faces. + * + * A plane call whose roof stood on its walls, or over them, is the same roof as a segment: same + * outline, surface, ridge and thickness (`lift` 0). + * + * Not reproduced, on purpose (no stored call made either, so each is a call a model could make): + * - A plane call with its eave at the walls' tops, the plugin's own and the likeliest, sank its roof + * into them: its slab's underside stood `thickness / cos(pitch)` under the plate at the wall face. + * A segment is never sunk under the walls it sits on (`eave_under_walls`); the replacement leaves + * the eave out and the roof sits on them, the same outline, pitch and thickness, its surface + * `lift` higher. Those rows say so. + * - Planes over a storey with another over it stood inside that storey; a segment refuses that + * (`into_storey_above`). The hosted table checks the refusal. + * - The plane tools answered each plane's id, so one slope could be edited or painted on its own + * (`update_roof_plane`). A segment is one mass whose slopes change together; one slope on its own + * is `create_roof_plane`'s, which stays, and the retired tools' answer says so. + * + * The viewer runs the segments through the roof geometry against the planes' own geometry + * (`roof-plane-cases.test.ts`); the hosted plugin runs each replacement call through + * `roof_segment_add` and reads back the segment it wrote. + */ + +type Rect = [number, number, number, number] + +const IN = 0.0254 +/** The walls' thickness, a 2x6 wall finished: what `roof_segment_add` reads from them. */ +export const ROOF_PLANE_WALL_THICKNESS = 6.5 * IN +/** The covering a segment lays over its deck, in the thickness the plane tools took whole. */ +export const ROOF_PLANE_COVERING = 0.02 +/** The plane tools' defaults. */ +const PLANE_OVERHANG = 0.457 +const PLANE_GABLE_OVERHANG = 0.305 +const PLANE_THICKNESS = 0.29 + +export type RoofPlaneCall = { + tool: 'hip_roof_planes' | 'gable_roof_planes' + rect: Rect + ridgeAxis?: 'x' | 'z' + /** Rise per 12. */ + pitch: number + /** The roof's surface at the outside wall face, metres above the storey's floor. */ + eaveHeight: number + overhang?: number + gableOverhang?: number + thickness?: number + name?: string +} + +export type RoofSegmentAddCall = { + roofType: 'hip' | 'gable' + name: string + rect: Rect + rectAt: 'outside' + ridgeAxis: 'x' | 'z' + pitchTwelfths: number + overhangIn: string + gableOverhangIn?: string + thicknessIn: string + eaveHeight?: number +} + +/** The segment a replacement call writes, in its storey's plan and metres. */ +export type ExpectedRoofSegment = { + roofType: 'hip' | 'gable' + name: string + /** The centre of its wall centre-line rectangle. */ + centre: [number, number] + /** Along its ridge axis and across it, at the wall centre lines. */ + width: number + depth: number + rotation: number + /** Degrees. */ + pitch: number + /** Along the slope, as the node stores it. */ + overhang: number + /** In plan past the gable-end walls' outside faces; none on a hip. */ + gableOverhang?: number + wallThickness: number + deckThickness: number + shingleThickness: number + /** The plate it bears on, metres above the storey's floor. */ + eave: number +} + +export type RoofPlaneCase = { + name: string + /** The walls under the roof: their outside faces and how tall they stand (none: posts). */ + walls: { rect: Rect; height: number } | null + call: RoofPlaneCall + replacement: RoofSegmentAddCall + segment: ExpectedRoofSegment + /** How far the segment's surface stands over the planes' (0: the same roof). */ + lift: number +} + +const slopeOf = (pitch: number) => Math.atan(pitch / 12) + +/** Where a plane call put the roof's surface at the wall face for a roof bearing on `plate`. */ +function surfaceOnPlate(plate: number, pitch: number, thickness = PLANE_THICKNESS): number { + const slope = slopeOf(pitch) + return plate - (ROOF_PLANE_WALL_THICKNESS / 2) * Math.tan(slope) + thickness / Math.cos(slope) +} + +/** The plate under a plane call's surface: what `eaveHeight` is to a segment. */ +function plateUnder(call: RoofPlaneCall): number { + const slope = slopeOf(call.pitch) + return ( + call.eaveHeight + + (ROOF_PLANE_WALL_THICKNESS / 2) * Math.tan(slope) - + (call.thickness ?? PLANE_THICKNESS) / Math.cos(slope) + ) +} + +function roofPlaneCase( + name: string, + walls: RoofPlaneCase['walls'], + call: RoofPlaneCall, + replacement: RoofSegmentAddCall, +): RoofPlaneCase { + const [ax, az, bx, bz] = call.rect + const t = ROOF_PLANE_WALL_THICKNESS + const w = Math.abs(bx - ax) - t + const d = Math.abs(bz - az) - t + const axis = replacement.ridgeAxis + const slope = slopeOf(call.pitch) + const thickness = call.thickness ?? PLANE_THICKNESS + const under = plateUnder(call) + // a segment bears on its walls' tops at the lowest; over them, where the planes put it + const eave = walls ? Math.max(under, walls.height) : under + return { + name, + walls, + call, + replacement, + segment: { + roofType: replacement.roofType, + name: replacement.name, + centre: [(ax + bx) / 2, (az + bz) / 2], + width: axis === 'x' ? w : d, + depth: axis === 'x' ? d : w, + rotation: axis === 'x' ? 0 : -Math.PI / 2, + pitch: (slope * 180) / Math.PI, + overhang: (call.overhang ?? PLANE_OVERHANG) / Math.cos(slope), + ...(call.tool === 'gable_roof_planes' + ? { gableOverhang: call.gableOverhang ?? PLANE_GABLE_OVERHANG } + : {}), + wallThickness: t, + deckThickness: thickness - ROOF_PLANE_COVERING, + shingleThickness: ROOF_PLANE_COVERING, + eave, + }, + lift: eave - under, + } +} + +/** The 12 × 8 m house, walls 2.6 m tall: its outside faces. */ +const WIDE: Rect = [-0.08255, -0.08255, 12.08255, 8.08255] +/** The same house turned: 8 m wide, 12 m deep. */ +const DEEP: Rect = [-0.08255, -0.08255, 8.08255, 12.08255] +/** A 10 m square house. */ +const SQUARE: Rect = [-0.08255, -0.08255, 10.08255, 10.08255] +/** A porch roof on posts, clear of the house, their outside faces. */ +const PORCH: Rect = [14, 2, 18, 6] +const PLATE = 2.6 + +const house = (rect: Rect) => ({ rect, height: PLATE }) + +export const ROOF_PLANE_CASES: readonly RoofPlaneCase[] = [ + roofPlaneCase( + 'hip_roof_planes defaults, its surface where a roof on the 2.6 m plate has it', + house(WIDE), + { + tool: 'hip_roof_planes', + rect: WIDE, + pitch: 6, + eaveHeight: surfaceOnPlate(PLATE, 6), + name: 'Hip', + }, + { + roofType: 'hip', + name: 'Hip', + rect: WIDE, + rectAt: 'outside', + ridgeAxis: 'x', + pitchTwelfths: 6, + overhangIn: '0.457 m', + thicknessIn: '0.29 m', + }, + ), + roofPlaneCase( + "hip_roof_planes at the walls' tops (the plugin's own call): the planes sank into the walls", + house(WIDE), + { tool: 'hip_roof_planes', rect: WIDE, pitch: 6, eaveHeight: PLATE }, + { + roofType: 'hip', + name: 'Hip', + rect: WIDE, + rectAt: 'outside', + ridgeAxis: 'x', + pitchTwelfths: 6, + overhangIn: '0.457 m', + thicknessIn: '0.29 m', + }, + ), + roofPlaneCase( + 'hip_roof_planes deeper than wide (its ridge north-south), 5:12, 0.6 m eaves, 0.2 m thick', + house(DEEP), + { + tool: 'hip_roof_planes', + rect: DEEP, + pitch: 5, + eaveHeight: surfaceOnPlate(PLATE, 5, 0.2), + overhang: 0.6, + thickness: 0.2, + name: 'Main', + }, + { + roofType: 'hip', + name: 'Main', + rect: DEEP, + rectAt: 'outside', + ridgeAxis: 'z', + pitchTwelfths: 5, + overhangIn: '0.6 m', + thicknessIn: '0.2 m', + }, + ), + roofPlaneCase( + 'hip_roof_planes over a square: a pyramid, 8:12, 0.3 m eaves', + house(SQUARE), + { + tool: 'hip_roof_planes', + rect: SQUARE, + pitch: 8, + eaveHeight: surfaceOnPlate(PLATE, 8), + overhang: 0.3, + }, + { + roofType: 'hip', + name: 'Hip', + rect: SQUARE, + rectAt: 'outside', + ridgeAxis: 'x', + pitchTwelfths: 8, + overhangIn: '0.3 m', + thicknessIn: '0.29 m', + }, + ), + roofPlaneCase( + 'hip_roof_planes flush with the walls (no eaves), 4:12', + house(WIDE), + { + tool: 'hip_roof_planes', + rect: WIDE, + pitch: 4, + eaveHeight: surfaceOnPlate(PLATE, 4), + overhang: 0, + }, + { + roofType: 'hip', + name: 'Hip', + rect: WIDE, + rectAt: 'outside', + ridgeAxis: 'x', + pitchTwelfths: 4, + overhangIn: '0 m', + thicknessIn: '0.29 m', + }, + ), + roofPlaneCase( + 'hip_roof_planes on a plate 0.3 m over the walls: the eave given (floating over them, it stays on their storey)', + house(WIDE), + { + tool: 'hip_roof_planes', + rect: WIDE, + pitch: 6, + eaveHeight: surfaceOnPlate(PLATE + 0.3, 6), + }, + { + roofType: 'hip', + name: 'Hip', + rect: WIDE, + rectAt: 'outside', + ridgeAxis: 'x', + pitchTwelfths: 6, + overhangIn: '0.457 m', + thicknessIn: '0.29 m', + eaveHeight: PLATE + 0.3, + }, + ), + roofPlaneCase( + 'gable_roof_planes defaults, the ridge east-west: 18 in eaves, 12 in at the gable ends', + house(WIDE), + { + tool: 'gable_roof_planes', + rect: WIDE, + ridgeAxis: 'x', + pitch: 6, + eaveHeight: surfaceOnPlate(PLATE, 6), + name: 'Gable', + }, + { + roofType: 'gable', + name: 'Gable', + rect: WIDE, + rectAt: 'outside', + ridgeAxis: 'x', + pitchTwelfths: 6, + overhangIn: '0.457 m', + gableOverhangIn: '0.305 m', + thicknessIn: '0.29 m', + }, + ), + roofPlaneCase( + "gable_roof_planes at the walls' tops: the planes sank into the walls", + house(WIDE), + { tool: 'gable_roof_planes', rect: WIDE, ridgeAxis: 'x', pitch: 6, eaveHeight: PLATE }, + { + roofType: 'gable', + name: 'Gable', + rect: WIDE, + rectAt: 'outside', + ridgeAxis: 'x', + pitchTwelfths: 6, + overhangIn: '0.457 m', + gableOverhangIn: '0.305 m', + thicknessIn: '0.29 m', + }, + ), + roofPlaneCase( + 'gable_roof_planes, the ridge north-south over the deep house, 8:12', + house(DEEP), + { + tool: 'gable_roof_planes', + rect: DEEP, + ridgeAxis: 'z', + pitch: 8, + eaveHeight: surfaceOnPlate(PLATE, 8), + }, + { + roofType: 'gable', + name: 'Gable', + rect: DEEP, + rectAt: 'outside', + ridgeAxis: 'z', + pitchTwelfths: 8, + overhangIn: '0.457 m', + gableOverhangIn: '0.305 m', + thicknessIn: '0.29 m', + }, + ), + roofPlaneCase( + 'gable_roof_planes, the ridge north-south across the wide house, 4:12', + house(WIDE), + { + tool: 'gable_roof_planes', + rect: WIDE, + ridgeAxis: 'z', + pitch: 4, + eaveHeight: surfaceOnPlate(PLATE, 4), + }, + { + roofType: 'gable', + name: 'Gable', + rect: WIDE, + rectAt: 'outside', + ridgeAxis: 'z', + pitchTwelfths: 4, + overhangIn: '0.457 m', + gableOverhangIn: '0.305 m', + thicknessIn: '0.29 m', + }, + ), + roofPlaneCase( + 'gable_roof_planes with gable ends reaching past the eaves: 0.9 m rakes, 0.3 m eaves, 3:12', + house(WIDE), + { + tool: 'gable_roof_planes', + rect: WIDE, + ridgeAxis: 'x', + pitch: 3, + eaveHeight: surfaceOnPlate(PLATE, 3, 0.25), + overhang: 0.3, + gableOverhang: 0.9, + thickness: 0.25, + }, + { + roofType: 'gable', + name: 'Gable', + rect: WIDE, + rectAt: 'outside', + ridgeAxis: 'x', + pitchTwelfths: 3, + overhangIn: '0.3 m', + gableOverhangIn: '0.9 m', + thicknessIn: '0.25 m', + }, + ), + roofPlaneCase( + 'gable_roof_planes with flush gable ends', + house(WIDE), + { + tool: 'gable_roof_planes', + rect: WIDE, + ridgeAxis: 'x', + pitch: 6, + eaveHeight: surfaceOnPlate(PLATE, 6), + gableOverhang: 0, + }, + { + roofType: 'gable', + name: 'Gable', + rect: WIDE, + rectAt: 'outside', + ridgeAxis: 'x', + pitchTwelfths: 6, + overhangIn: '0.457 m', + gableOverhangIn: '0 m', + thicknessIn: '0.29 m', + }, + ), + roofPlaneCase( + 'gable_roof_planes over a porch on posts: its eave on the beam, given', + null, + { + tool: 'gable_roof_planes', + rect: PORCH, + ridgeAxis: 'z', + pitch: 6, + eaveHeight: surfaceOnPlate(2.4, 6), + name: 'Porch', + }, + { + roofType: 'gable', + name: 'Porch', + rect: PORCH, + rectAt: 'outside', + ridgeAxis: 'z', + pitchTwelfths: 6, + overhangIn: '0.457 m', + gableOverhangIn: '0.305 m', + thicknessIn: '0.29 m', + eaveHeight: 2.4, + }, + ), +] diff --git a/packages/viewer/src/systems/roof/roof-fascia.test.ts b/packages/viewer/src/systems/roof/roof-fascia.test.ts index e6d849d2e0..a09de76e62 100644 --- a/packages/viewer/src/systems/roof/roof-fascia.test.ts +++ b/packages/viewer/src/systems/roof/roof-fascia.test.ts @@ -81,6 +81,19 @@ describe('roof fascia', () => { } }) + test('a gable overhang stands the rake boards out at it, the eave boards where they were', () => { + const node = segment({ gableOverhang: 1.2 }) + const rake = node.width / 2 + node.wallThickness / 2 + 1.2 + for (const side of [1, -1] as const) { + const xs = boardPoints(node, 'x', side).map((v) => v.x * side) + expect(xs.length).toBeGreaterThan(0) + expect(Math.min(...xs)).toBeGreaterThan(rake - 0.01) + expect(Math.max(...xs)).toBeLessThan(rake + 0.03) + } + const ys = boardPoints(node, 'z', 1).map((v) => v.y) + expect(Math.max(...ys)).toBeCloseTo(eaveTopY(node), 3) + }) + test('a hip has boards on all four sides, level', () => { const node = segment({ roofType: 'hip' }) for (const [axis, side] of [ diff --git a/packages/viewer/src/systems/roof/roof-plane-cases.test.ts b/packages/viewer/src/systems/roof/roof-plane-cases.test.ts new file mode 100644 index 0000000000..fde308e85d --- /dev/null +++ b/packages/viewer/src/systems/roof/roof-plane-cases.test.ts @@ -0,0 +1,181 @@ +import { describe, expect, test } from 'bun:test' +import { RoofSegmentNode } from '@pascal-app/core' +import * as THREE from 'three' +import { ROOF_PLANE_CASES, type RoofPlaneCall } from './__fixtures__/roof-plane-cases' +import { generateRoofSegmentGeometry } from './roof-system' + +/** + * Slice R's case table, drawn: every roof the retired hip and gable plane tools built, against the + * roof segment that covers it, through the viewer's own roof geometry. The planes' geometry is the + * plane tools' arithmetic: an outline the walls' outside faces grown by the overhangs, a surface + * rising from each outline edge at the pitch from `eaveHeight − overhang × pitch` there, and a slab + * the thickness deep, square to the slope. One difference is the segment's own drawing: under its + * eaves the deck is boxed level 5 cm under its edge (the fascia covers that edge), where a plane's + * slab kept its slope to the edge, so the depth is compared over the house, 1 m in from its walls. + */ + +const COVERING = 3 +const DEFAULTS = { overhang: 0.457, gableOverhang: 0.305, thickness: 0.29 } + +type Box = { x0: number; z0: number; x1: number; z1: number } + +/** The planes' outline in plan. */ +function planesOutline(call: RoofPlaneCall): Box { + const [ax, az, bx, bz] = call.rect + const eave = call.overhang ?? DEFAULTS.overhang + const gable = call.gableOverhang ?? DEFAULTS.gableOverhang + const hip = call.tool === 'hip_roof_planes' + const alongX = hip ? eave : call.ridgeAxis === 'x' ? gable : eave + const alongZ = hip ? eave : call.ridgeAxis === 'z' ? gable : eave + return { + x0: Math.min(ax, bx) - alongX, + x1: Math.max(ax, bx) + alongX, + z0: Math.min(az, bz) - alongZ, + z1: Math.max(az, bz) + alongZ, + } +} + +/** The planes' surface over a plan point: the lowest rise from the outline edges the planes slope from. */ +function planesSurface(call: RoofPlaneCall, x: number, z: number): number { + const o = planesOutline(call) + const rise = call.pitch / 12 + const base = call.eaveHeight - (call.overhang ?? DEFAULTS.overhang) * rise + const fromX = Math.min(x - o.x0, o.x1 - x) + const fromZ = Math.min(z - o.z0, o.z1 - z) + const run = + call.tool === 'hip_roof_planes' + ? Math.min(fromX, fromZ) + : call.ridgeAxis === 'x' + ? fromZ + : fromX + return base + run * rise +} + +const materials = Array.from( + { length: 8 }, + () => new THREE.MeshBasicMaterial({ side: THREE.DoubleSide }), +) + +/** The segment's geometry where it stands in its storey's plan. */ +function segmentMesh(c: (typeof ROOF_PLANE_CASES)[number]): THREE.Mesh { + const s = c.segment + const node = RoofSegmentNode.parse({ + roofType: s.roofType, + width: s.width, + depth: s.depth, + position: [s.centre[0], s.eave, s.centre[1]], + rotation: s.rotation, + pitch: s.pitch, + overhang: s.overhang, + gableOverhang: s.gableOverhang, + wallThickness: s.wallThickness, + deckThickness: s.deckThickness, + shingleThickness: s.shingleThickness, + wallHeight: 0, + fascia: true, + }) + const geometry = generateRoofSegmentGeometry(node) + geometry.applyMatrix4( + new THREE.Matrix4().makeRotationY(s.rotation).setPosition(s.centre[0], s.eave, s.centre[1]), + ) + return new THREE.Mesh(geometry, materials) +} + +/** The plan extent of the covering's upward faces. */ +function coveringOutline(mesh: THREE.Mesh): Box { + const geometry = mesh.geometry + const position = geometry.getAttribute('position') + const index = geometry.getIndex() + const at = (i: number) => + new THREE.Vector3().fromBufferAttribute(position, index ? index.getX(i) : i) + const box = new THREE.Box3() + for (const group of geometry.groups) { + if (group.materialIndex !== COVERING) continue + for (let i = group.start; i < group.start + group.count; i += 3) { + const [a, b, c] = [at(i), at(i + 1), at(i + 2)] + const normal = new THREE.Vector3() + .subVectors(b, a) + .cross(new THREE.Vector3().subVectors(c, a)) + if (normal.normalize().y > 0.3) box.expandByPoint(a).expandByPoint(b).expandByPoint(c) + } + } + return { x0: box.min.x, z0: box.min.z, x1: box.max.x, z1: box.max.z } +} + +const down = new THREE.Vector3(0, -1, 0) + +/** The covering's top over a plan point, and the slab's underside under it. */ +function slabAt(mesh: THREE.Mesh, x: number, z: number) { + const ray = new THREE.Raycaster(new THREE.Vector3(x, 100, z), down) + const hits = ray.intersectObject(mesh) + const top = hits.find((hit) => hit.face?.materialIndex === COVERING) + if (!top) return null + // the deck's underside: the lowest downward face within a slab's depth (the covering's own is above it) + const undersides = hits.filter( + (hit) => + hit.distance > top.distance && + hit.distance < top.distance + 0.6 && + (hit.face?.normal.y ?? 0) < -0.3, + ) + return { top: top.point.y, underside: undersides.at(-1)?.point.y ?? null } +} + +/** Plan points over the roof, 10 cm in from its outline: a 9 × 7 grid. */ +function samples(o: Box): [number, number][] { + const points: [number, number][] = [] + for (let i = 0; i <= 8; i++) + for (let j = 0; j <= 6; j++) + points.push([ + o.x0 + 0.1 + ((o.x1 - o.x0 - 0.2) * i) / 8, + o.z0 + 0.1 + ((o.z1 - o.z0 - 0.2) * j) / 6, + ]) + return points +} + +describe('the hip and gable planes, as roof segments', () => { + for (const c of ROOF_PLANE_CASES) { + test(c.name, () => { + const mesh = segmentMesh(c) + const planes = planesOutline(c.call) + + // the same outline: the covering reaches the planes' edges (its shingles lap 1 cm past the deck) + const covering = coveringOutline(mesh) + for (const side of ['x0', 'z0', 'x1', 'z1'] as const) + expect(Math.abs(covering[side] - planes[side])).toBeLessThan(0.015) + + // the same surface, lifted onto the walls where the planes sank into them + const slope = Math.atan(c.call.pitch / 12) + const thickness = c.call.thickness ?? DEFAULTS.thickness + const [ax, az, bx, bz] = c.call.rect + const overHouse = (x: number, z: number) => + x > Math.min(ax, bx) + 1 && + x < Math.max(ax, bx) - 1 && + z > Math.min(az, bz) + 1 && + z < Math.max(az, bz) - 1 + let deep = 0 + for (const [x, z] of samples(planes)) { + const slab = slabAt(mesh, x, z) + expect(slab).not.toBeNull() + if (!slab) continue + expect(slab.top).toBeCloseTo(planesSurface(c.call, x, z) + c.lift, 2) + // as deep, square to the slope + if (!overHouse(x, z)) continue + deep++ + expect(slab.underside).not.toBeNull() + expect(slab.top - (slab.underside ?? 0)).toBeCloseTo(thickness / Math.cos(slope), 2) + } + expect(deep).toBeGreaterThan(4) + mesh.geometry.dispose() + }) + } + + test('the cases cover both tools, both ridge axes and the eave on, over and sunk into the walls', () => { + const calls = ROOF_PLANE_CASES.map((c) => c.call) + expect(calls.filter((c) => c.tool === 'hip_roof_planes').length).toBeGreaterThan(3) + expect( + new Set(calls.filter((c) => c.tool === 'gable_roof_planes').map((c) => c.ridgeAxis)), + ).toEqual(new Set(['x', 'z'])) + expect(ROOF_PLANE_CASES.some((c) => c.lift > 0.1)).toBe(true) + expect(ROOF_PLANE_CASES.some((c) => c.walls === null)).toBe(true) + }) +}) diff --git a/packages/viewer/src/systems/roof/roof-system.tsx b/packages/viewer/src/systems/roof/roof-system.tsx index 88c18f4032..ee80fb9d35 100644 --- a/packages/viewer/src/systems/roof/roof-system.tsx +++ b/packages/viewer/src/systems/roof/roof-system.tsx @@ -7,6 +7,7 @@ import { getEffectiveNode, getRoofModuleFaces, getRoofPlanBounds, + getRoofSegmentDeckReach, getRoofSegmentSurfaceY, getRoofShapeInsets, getRoofShapeRatios, @@ -1276,7 +1277,12 @@ function buildSegmentTrimCutBrushes(segment: RoofSegmentNode): Brush[] { const { activeRh } = getSegmentSlopeFrame(segment) const extra = - segment.wallThickness + segment.overhang + segment.deckThickness + segment.shingleThickness + 2 + segment.wallThickness + + segment.overhang + + (segment.gableOverhang ?? 0) + + segment.deckThickness + + segment.shingleThickness + + 2 const minX = -segment.width / 2 - extra const maxX = segment.width / 2 + extra const minZ = -segment.depth / 2 - extra @@ -1429,7 +1435,7 @@ const FASCIA_MATERIAL_INDEX = 2 */ function buildFasciaBoards( node: RoofSegmentNode, - deckExt: number, + reach: { x: number; z: number }, eaveTopY: number, tanTheta: number, ): THREE.BufferGeometry | null { @@ -1439,9 +1445,9 @@ function buildFasciaBoards( // A flat roof's boards sized on purpose reach its top, over the membrane's edge: a coping band. const flatTopY = node.fasciaHeight !== undefined ? eaveTopY + node.shingleThickness : eaveTopY const out = bt / 2 - FASCIA_EMBED - const halfD = depth / 2 + deckExt + const halfD = depth / 2 + reach.z const zE = halfD + out - const xE = width / 2 + deckExt + out + const xE = width / 2 + reach.x + out // the eave boards run through the corners to the rake / side boards' // outer faces (a lapped return, no notch at the corner) const xL = xE + bt / 2 @@ -1529,16 +1535,8 @@ export function getRoofSegmentBrushes( node: RoofSegmentNode, options: { fascia?: boolean } = {}, ): RoofSegmentBrushSet | null { - const { - roofType, - width, - depth, - wallHeight, - wallThickness, - deckThickness, - overhang, - shingleThickness, - } = node + const { roofType, width, depth, wallHeight, wallThickness, deckThickness, shingleThickness } = + node const conicalCoverage = getConicalRoofCoverage(node) const { activeRh, tanTheta, cosTheta, sinTheta } = getSegmentSlopeFrame(node) @@ -1558,6 +1556,7 @@ export function getRoofSegmentBrushes( const baseI = Math.min(width, depth) * node.dutchHipWidthRatio const plateSeated = wallHeight <= 0 + // `xExt`: how far the volume reaches along x, where a gable's rakes are; the eaves' `wExt` else. const getVol = ( wExt: number, vOffset: number, @@ -1566,8 +1565,9 @@ export function getRoofSegmentBrushes( isVoid: boolean, materialRule?: (normal: THREE.Vector3) => number, onPlate = false, + xExt = wExt, ) => { - const wV = Math.max(0.01, width + 2 * wExt) + const wV = Math.max(0.01, width + 2 * xExt) const dV = Math.max(0.01, depth + 2 * wExt) const autoDrop = wExt * tanTheta @@ -1616,8 +1616,8 @@ export function getRoofSegmentBrushes( const wallGeo = getVol(wallThickness / 2, 0, 0, 0, false, undefined, true) const innerGeo = getVol(-wallThickness / 2, 0, -5, 2, false, undefined, true) - const horizontalOverhang = overhang * cosTheta - const deckExt = wallThickness / 2 + horizontalOverhang + const reach = getRoofSegmentDeckReach(node) + const deckExt = reach.z // A shed's deck and shingle edges take the wall/trim slot, unless it wears // fascia boards: then they are fascia like a gable's, in the deck slot. @@ -1625,20 +1625,29 @@ export function getRoofSegmentBrushes( const shedRoofSideMaterialRule = shedEdgesInTrimSlot ? (normal: THREE.Vector3) => (normal.y > SHINGLE_SURFACE_EPSILON ? 3 : ROOF_EDGE_MATERIAL_INDEX) : undefined - const deckTopGeo = getVol(deckExt, verticalRt, 0, 1, false, shedRoofSideMaterialRule) - const deckBotGeo = getVol(deckExt, 0, -5, 0, true) + const deckTopGeo = getVol( + deckExt, + verticalRt, + 0, + 1, + false, + shedRoofSideMaterialRule, + false, + reach.x, + ) + const deckBotGeo = getVol(deckExt, 0, -5, 0, true, undefined, false, reach.x) // the deck's top edge at its outer plane, as `getVol` puts it const deckEaveDrop = deckExt * tanTheta const deckEaveTopY = plateSeated && deckEaveDrop > 0 ? wallHeight - deckEaveDrop + verticalRt : Math.max(0.05, wallHeight - deckEaveDrop + verticalRt) - const fasciaGeo = options.fascia ? buildFasciaBoards(node, deckExt, deckEaveTopY, tanTheta) : null + const fasciaGeo = options.fascia ? buildFasciaBoards(node, reach, deckEaveTopY, tanTheta) : null const stSin = shingleThickness * sinTheta const stCos = shingleThickness * cosTheta - const shinBotW = Math.max(0.01, width + 2 * deckExt) + const shinBotW = Math.max(0.01, width + 2 * reach.x) const shinBotD = Math.max(0.01, depth + 2 * deckExt) const deckDrop = deckExt * tanTheta @@ -1795,7 +1804,7 @@ export function getRoofSegmentBrushes( const deckTopBrush = toBrush(deckTopGeo) const deckBotBrush = toBrush(deckBotGeo) if (deckBotBrush) { - const wV = Math.max(0.01, width + 2 * deckExt) + const wV = Math.max(0.01, width + 2 * reach.x) const dV = Math.max(0.01, depth + 2 * deckExt) deckBotBrush.scale.set(1 + eps / wV, 1, 1 + eps / dV) deckBotBrush.updateMatrixWorld() @@ -4084,16 +4093,7 @@ export function getRoofOuterSurfaceFrameAtPoint( lx: number, lz: number, ): SurfaceFrame { - const { - roofType, - width, - depth, - wallHeight, - wallThickness, - deckThickness, - overhang, - shingleThickness, - } = segment + const { roofType, width, depth, wallHeight, deckThickness, shingleThickness } = segment const { activeRh, tanTheta, cosTheta, sinTheta } = getSegmentSlopeFrame(segment) @@ -4105,13 +4105,13 @@ export function getRoofOuterSurfaceFrameAtPoint( } const verticalRt = deckThickness / cosTheta - const horizontalOverhang = overhang * cosTheta - const deckExt = wallThickness / 2 + horizontalOverhang + const reach = getRoofSegmentDeckReach(segment) + const deckExt = reach.z const stSin = shingleThickness * sinTheta const stCos = shingleThickness * cosTheta - const shinBotW = Math.max(0.01, width + 2 * deckExt) + const shinBotW = Math.max(0.01, width + 2 * reach.x) const shinBotD = Math.max(0.01, depth + 2 * deckExt) const deckDrop = deckExt * tanTheta const shinBotWh = wallHeight - deckDrop + verticalRt