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
3 changes: 3 additions & 0 deletions packages/core/src/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ export {
getDutchRoofMetrics,
getEffectiveSegmentSurfaceMaterial,
getPitchFromActiveRoofHeight,
getRoofSegmentDeckReach,
getRoofSegmentEndReach,
getRoofSegmentSurfaceY,
getRoofSegmentVisibleTopBounds,
getSegmentFasciaMaterial,
Expand All @@ -297,6 +299,7 @@ export {
RoofSegmentNode,
RoofSegmentTrim,
RoofType,
roofTypeHasRakes,
} from './nodes/roof-segment'
export type {
DutchRoofShapeMetrics,
Expand Down
29 changes: 25 additions & 4 deletions packages/core/src/schema/nodes/gutter-defaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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', () => {
Expand Down
12 changes: 10 additions & 2 deletions packages/core/src/schema/nodes/gutter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions packages/core/src/schema/nodes/roof-segment-deck-reach.test.ts
Original file line number Diff line number Diff line change
@@ -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>): 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)
})
})
53 changes: 46 additions & 7 deletions packages/core/src/schema/nodes/roof-segment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<RoofSegmentNode, 'roofType' | 'pitch' | 'width' | 'depth'> &
Partial<Pick<RoofSegmentNode, 'wallThickness' | 'overhang' | 'gableOverhang'>>,
): { 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<RoofSegmentNode, 'roofType' | 'pitch' | 'width' | 'depth'> &
Partial<Pick<RoofSegmentNode, 'wallThickness' | 'overhang' | 'gableOverhang'>>,
): 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' ||
Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/services/roof-bearing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
type AnyNode,
type AnyNodeId,
getConicalRoofCoverage,
getRoofSegmentDeckReach,
getSegmentSlopeFrame,
type LevelNode,
type SlabNode,
Expand Down Expand Up @@ -61,14 +62,14 @@ export function wallSpans(nodes: Nodes, levelId: string): Map<string, WallSpan>
)
}

/** 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. */
Expand All @@ -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,
}
Expand All @@ -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
Expand Down
31 changes: 29 additions & 2 deletions packages/core/src/store/actions/gutter-update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AnyNode>)
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<AnyNode>)

// 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,
Expand Down Expand Up @@ -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', () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/store/actions/node-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const DEFAULT_RIDGE_VENT_REFRESH_FIELDS = new Set<string>([
'depth',
'pitch',
'overhang',
'gableOverhang',
'wallThickness',
'shingleThickness',
'gambrelLowerWidthRatio',
Expand All @@ -103,6 +104,8 @@ const DEFAULT_GUTTER_REFRESH_FIELDS = new Set<string>([
'wallHeight',
'pitch',
'overhang',
'gableOverhang',
'wallThickness',
'trim',
])

Expand Down
38 changes: 38 additions & 0 deletions packages/core/src/store/actions/ridge-vent-update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AnyNodeId, AnyNode>,
[roof.id as AnyNodeId],
)

useScene
.getState()
.updateNode(segment.id as AnyNodeId, { gableOverhang: 0.9 } as Partial<AnyNode>)

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({
Expand Down
7 changes: 4 additions & 3 deletions packages/editor/src/components/editor/selection-manager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
type GridEvent,
getEffectiveRoofSurfaceMaterial,
getEffectiveSegmentSurfaceMaterial,
getRoofSegmentDeckReach,
getRoofSegmentSurfaceY,
getSelectableKinds,
type ItemNode,
Expand Down Expand Up @@ -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
Expand Down
Loading
Loading