Skip to content
Closed
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
1 change: 1 addition & 0 deletions packages/core/src/events/bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ type CameraControlEvents = {

type ToolEvents = {
'tool:cancel': undefined
'dimension:focus': 'length' | 'angle'
}

type GuideEvents = {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export {
resolveAssemblyStack,
} from './lib/assembly-stack'
export { loadAssetUrl, saveAsset } from './lib/asset-storage'
export { parseAngle, parseDimension, pointFromLengthAngle } from './lib/units'
export {
BEAM_MATERIAL_LABELS,
BEAM_MATERIAL_LIBRARY_REFS,
Expand Down Expand Up @@ -1347,6 +1348,7 @@ export {
planWallEndRejoins,
planWallInsertion,
planWallSplitAtPoint,
wallSegmentsCoverSegment,
type WallInsertionPlan,
type WallJoinResult,
type WallTopologyChanges,
Expand Down
65 changes: 65 additions & 0 deletions packages/core/src/lib/units.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { describe, expect, it } from 'bun:test'
import { parseAngle, parseDimension, pointFromLengthAngle } from './units'

describe('parseDimension', () => {
it('parses bare number as meters', () => {
expect(parseDimension('3.5')).toBe(3.5)
expect(parseDimension('10')).toBe(10)
})

it('parses meters with "m" suffix', () => {
expect(parseDimension('3.5m')).toBe(3.5)
expect(parseDimension('5 M')).toBe(5)
})

it('parses centimeters with "cm" suffix', () => {
expect(parseDimension('200cm')).toBe(2)
expect(parseDimension('50 cm')).toBe(0.5)
})

it('parses feet with "ft" suffix', () => {
expect(parseDimension('10ft')).toBeCloseTo(3.048, 4)
})

it('returns null on invalid string', () => {
expect(parseDimension('')).toBeNull()
expect(parseDimension('abc')).toBeNull()
expect(parseDimension('5xyz')).toBeNull()
})
})

describe('parseAngle', () => {
it('parses plain degrees', () => {
expect(parseAngle('45')).toBe(45)
expect(parseAngle('90.5')).toBe(90.5)
})

it('handles ° and deg suffix', () => {
expect(parseAngle('90°')).toBe(90)
expect(parseAngle('180deg')).toBe(180)
})

it('normalizes negative and >360 angles', () => {
expect(parseAngle('-90')).toBe(270)
expect(parseAngle('450')).toBe(90)
})

it('returns null on invalid string', () => {
expect(parseAngle('')).toBeNull()
expect(parseAngle('invalid')).toBeNull()
})
})

describe('pointFromLengthAngle', () => {
it('calculates point east (+X) at 0 deg', () => {
const pt = pointFromLengthAngle([0, 0], 5, 0)
expect(pt[0]).toBeCloseTo(5, 4)
expect(pt[1]).toBeCloseTo(0, 4)
})

it('calculates point south (+Z) at 90 deg', () => {
const pt = pointFromLengthAngle([0, 0], 5, 90)
expect(pt[0]).toBeCloseTo(0, 4)
expect(pt[1]).toBeCloseTo(5, 4)
})
})
82 changes: 82 additions & 0 deletions packages/core/src/lib/units.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* Unit parsing for dimension input.
*
* Parses strings like "3.5m", "12ft", "120cm" into meters.
* Numbers without a suffix are treated as project units (meters).
*/

const UNIT_FACTORS: Record<string, number> = {
m: 1,
ft: 0.3048,
cm: 0.01,
in: 0.0254,
mm: 0.001,
}

/**
* Parse a dimension string with optional unit suffix into meters.
*
* Supported suffixes: m, ft, cm, in, mm (case-insensitive).
* A bare number (e.g. "3.5") is treated as meters.
*
* @returns The value in meters, or null if parsing fails.
*/
export function parseDimension(input: string): number | null {
const trimmed = input.trim()
if (!trimmed) return null

const match = trimmed.match(/^([+-]?\d+(?:\.\d+)?)\s*([a-zA-Z]*)$/)
if (!match || !match[1]) return null

const value = parseFloat(match[1])
if (Number.isNaN(value)) return null

const unit = (match[2] ?? '').toLowerCase()
if (unit === '') return value // no suffix → meters

const factor = UNIT_FACTORS[unit]
if (factor === undefined) return null

return value * factor
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dimension parser ignores project units

High Severity

parseDimension and the copy inside setValues treat a bare number as meters and reject architectural feet-inch strings. The editor already has parseDimensionInput, which honors metric vs imperial. In an imperial project, typing 10 or 12'6" during wall/fence/slab/ceiling drafting produces the wrong length or fails to parse.

Additional Locations (2)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 199443e. Configure here.


/**
* Parse an angle string into degrees.
*
* Accepts plain numbers (e.g. "45", "90.5").
* Trailing "°" or "deg" suffixes are stripped if present.
*
* @returns The angle in degrees, or null if parsing fails.
*/
export function parseAngle(input: string): number | null {
const trimmed = input.trim()
if (!trimmed) return null

// Strip common degree suffixes
const cleaned = trimmed.replace(/°|deg$/i, '').trim()
if (!cleaned) return null

const value = parseFloat(cleaned)
if (Number.isNaN(value)) return null

// Normalize to 0-360 range
return ((value % 360) + 360) % 360
}

/**
* Calculate the next point given a start point, length (meters), and angle (degrees).
* Angle is measured clockwise from the +X axis (east).
*
* @returns [x, z] of the calculated point.
*/
export function pointFromLengthAngle(
start: [number, number],
lengthMeters: number,
angleDeg: number,
): [number, number] {
const rad = (angleDeg * Math.PI) / 180
return [
start[0] + Math.cos(rad) * lengthMeters,
start[1] + Math.sin(rad) * lengthMeters,
]
}
12 changes: 10 additions & 2 deletions packages/core/src/systems/wall/wall-topology.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { encodeTerrainField } from '../../lib/terrain-codec'
import { applyHeightPatch, createTerrainField, flattenPatch } from '../../lib/terrain-field'
import { type AnyNode, type AnyNodeId, DoorNode, WallNode, ZoneNode } from '../../schema'
import { getWallArcData, getWallCurveFrameAt } from './wall-curve'
import { planWallInsertion, planWallSplitAtPoint } from './wall-topology'
import { planWallInsertion, planWallSplitAtPoint, wallSegmentsCoverSegment } from './wall-topology'

const LEVEL_ID = 'level_topology' as AnyNodeId

Expand Down Expand Up @@ -1123,4 +1123,12 @@ describe('a wall started in a corner block', () => {
expect(result.plan.resolvedStart).toEqual([-0.5, 0])
expect(result.plan.changes.delete).toEqual(['wall_across'])
})
})
test('wallSegmentsCoverSegment identifies collinear segments covered by host wall for tracking transit', () => {
// across runs from [-3, 0] to [0, 0]
expect(wallSegmentsCoverSegment([-2, 0], [-1, 0], [across])).toBe(true)
expect(wallSegmentsCoverSegment([-3, 0], [-1.5, 0], [across])).toBe(true)
// perpendicular or outside the host wall
expect(wallSegmentsCoverSegment([-1.5, 0], [-1.5, 2], [across])).toBe(false)
expect(wallSegmentsCoverSegment([-3, 0], [-4, 0], [across])).toBe(false)
})
});
2 changes: 1 addition & 1 deletion packages/core/src/systems/wall/wall-topology.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function collinearWallIntervals(
return { intervals, length }
}

function wallSegmentsCoverSegment(start: WallPlanPoint, end: WallPlanPoint, walls: WallNode[]) {
export function wallSegmentsCoverSegment(start: WallPlanPoint, end: WallPlanPoint, walls: WallNode[]): boolean {
const collinear = collinearWallIntervals(start, end, walls)
if (!collinear) return false
const parameterTolerance = WALL_INTERSECTION_EPSILON / collinear.length
Expand Down
Loading
Loading