ai-chatbot/packages/editor/src/lib/floorplan/apply-alignment.ts
Sudhir Yadav ce6f999310
arch: enforce layer boundaries — ceiling dispatch, store relocation, shared helper (#382)
* Add roof surface placement support for items

Items (e.g. solar panels) can now be placed on sloped roof surfaces.
The placement system computes euler rotation from the roof surface
normal so items sit flush on the slope instead of going inside.

- Add roofStrategy to placement-strategies with enter/move/click/leave
- Wire roof:enter/move/click/leave events in the placement coordinator
- Add calculateRoofRotation in placement-math using surface normals
- Support full 3D cursor rotation for sloped surfaces
- Items on roofs are parented to the level with world-space rotation

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fixed conflict

* editor: per-door-type floor-plan symbols

Render a distinct, static plan symbol for each door type in the
registry floor-plan builder (`packages/nodes/src/door/floorplan.ts`),
independent of the door's live open/close animation:

- single / hinged: fixed 90° swing with a dashed quarter-circle arc
- double / french: two mirrored half-width leaves + dashed arcs
- folding / bifold: static zigzag accordion (~80% span) on the wall face
- sliding: bypass — two overlapping panels on parallel tracks + arrow
- pocket: thin white leaf, ~60% closed, sliding into the solid wall
- barn: surface-mounted panel parked over the wall, dashed closed-ghost
  + slide arrow

The swing arc is dashed in screen-pixel units (the renderer uses
non-scaling-stroke). Symbols are oriented by hingesSide / swingDirection
/ slideDirection as appropriate.

Also includes pre-existing working-tree changes unrelated to the door
symbols: group move/rotate transform and box-select tweaks, and a
regenerated ifc-converter next-env.d.ts.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* Fix recessed ceiling fixtures and draw safety

* feat(editor): magnetic wall-snap with per-kind beacon (2D + 3D)

Snap the wall draft / endpoint-move point onto existing wall geometry —
corners, midpoints, wall–wall intersections, and along-wall edges — and
show a beacon at the snap point whose glyph encodes what it caught
(square = corner, triangle = midpoint, ✕ = intersection, circle = edge).

- Pure snap geometry extracted to wall-snap-geometry.ts (unit-tested).
- Ephemeral useWallSnapIndicator store drives a 3D pillar+glyph beacon
  and a 2D SVG glyph beacon, both indigo to match the alignment guides.
- Gated by a new persisted "Magnetic snap" toggle in the Display menu
  (useEditor); honored by draw + commit + endpoint-move in both views.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* editor: garage and open-doorway floor-plan symbols

Extend the per-door-type plan symbols in the registry floor-plan
builder (packages/nodes/src/door/floorplan.ts):

- open doorway (openingKind === 'opening'): bare gap, no leaf/arc/panel
  (mirrors the 3D system, which renders only the cutout for openings)
- garage sectional: closed leaf + side tracks into the garage + dashed
  parked ghost at the inner end
- garage roll-up: closed leaf + coil barrel (capsule) with a coil hint
- garage tilt-up: closed leaf + dashed parked panel + dashed curved
  up-and-over swing path
- gate the swing arc to actual swing doors (hinged/double/french) so
  other types fall back to the plain footprint

Garage mechanisms sit on the interior (door-local -z) side to match the
3D garage builders, independent of swingDirection.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* arch: enforce layer boundaries — registry dispatch, store relocation, shared helper

Three architectural fixes to bring the branch into full compliance:

1. **ceiling-system kind check → CeilingCutCapability**
   Replace `child.type === 'item'` branch in `ceiling-system` with registry
   dispatch. Add `CeilingCutCapability` type to `packages/core` registry types,
   implement `buildCeilingHole` on `itemDefinition`, and rewrite
   `collectRecessedItemHoles` → `collectCeilingHoles` to dispatch through
   `nodeRegistry` — viewer never again inspects a node's kind directly.

2. **useAlignmentGuides + useWallSnapIndicator → packages/editor**
   These stores are editor-only UI (snap beacons, alignment guides). Move them
   from `packages/core/src/store/` to `packages/editor/src/store/`, re-export
   from `packages/editor`, and update all 34 consumer files across
   `packages/editor` and `packages/nodes` to import from `@pascal-app/editor`.

3. **findLevelAncestorId extracted to core**
   `item-light-system` had a private `resolveNodeLevelId` that duplicated
   level-ancestor traversal logic. Extract it as `findLevelAncestorId` in
   `packages/core` (spatial-grid-sync), export it, and replace the local copy.

All four packages typecheck cleanly (zero errors).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* chore: fix lint, untrack .claude/launch.json

Run bun check --write to clear 8 Biome errors (formatting + import order
+ one unused import). Untrack .claude/launch.json and add it plus
.claude/settings.local.json to .gitignore so local IDE/agent configs
stop landing in commits.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-06-08 13:14:39 -04:00

112 lines
4.1 KiB
TypeScript

import {
type AlignmentAnchor,
type AlignmentGuide,
collectAlignmentAnchors,
resolveAlignment,
useScene,
} from '@pascal-app/core'
import { useAlignmentGuides } from '@pascal-app/editor'
/**
* Fixed Figma-style alignment threshold (meters) for floor-plan placement /
* move — parity with the 3D tools' `ALIGNMENT_THRESHOLD_M`. Pure-2D drafting
* can pass a zoom-scaled `threshold` instead so the magnetic pull stays
* constant in screen pixels across zoom levels.
*/
export const FLOORPLAN_ALIGNMENT_THRESHOLD_M = 0.08
export type FloorplanAlignmentResult = {
/** Plan point with the alignment snap delta applied (grid snap should
* already be baked into the input point). */
point: [number, number]
snapped: boolean
guides: AlignmentGuide[]
}
/**
* Layer Figma-style alignment on top of an already grid-snapped plan point,
* shared by the 2D move sessions (`*FloorplanMoveTarget`) and the structural
* drafting branches in `floorplan-panel`.
*
* Publishes guides to the `useAlignmentGuides` store as a side effect — set
* on a match, cleared otherwise — so the mounted `FloorplanAlignmentGuideLayer`
* renders them. Returns the adjusted point. When `bypass` is true (Alt held)
* the point is returned unchanged and guides are cleared, matching the
* "No snap" affordance the placement tools advertise.
*
* `candidates` should be gathered ONCE per drag (`collectAlignmentAnchors`);
* the scene is stable during a single drag, so re-collecting per pointer-move
* is wasted work.
*/
export function applyFloorplanAlignment(
point: readonly [number, number],
movingAnchors: AlignmentAnchor[],
candidates: AlignmentAnchor[],
opts?: { bypass?: boolean; threshold?: number },
): FloorplanAlignmentResult {
if (opts?.bypass) {
useAlignmentGuides.getState().clear()
return { point: [point[0], point[1]], snapped: false, guides: [] }
}
const result = resolveAlignment({
moving: movingAnchors,
candidates,
threshold: opts?.threshold ?? FLOORPLAN_ALIGNMENT_THRESHOLD_M,
})
useAlignmentGuides.getState().set(result.guides)
if (!result.snap) {
return { point: [point[0], point[1]], snapped: false, guides: result.guides }
}
return {
point: [point[0] + result.snap.dx, point[1] + result.snap.dz],
snapped: true,
guides: result.guides,
}
}
/** Synthetic node id for the in-progress structural-draft vertex. Never
* collides with a real scene node, so `collectAlignmentAnchors` excludes
* nothing real. */
export const FLOORPLAN_DRAFT_ALIGN_ID = '__floorplan_draft__'
/**
* Align a single grid-snapped structural-draft vertex (wall / fence / polygon
* / roof endpoint) against every other node's anchors (incl. wall faces from
* the wall-face anchor work). Treats the vertex as one corner anchor, gathers
* candidates from the live scene, publishes guides (cleared on `bypass`), and
* returns the possibly-snapped point.
*
* Used by BOTH the move-preview branch and the click-commit handler so the
* committed vertex lands exactly where the preview showed it. Caller owns the
* per-kind precedence (existing-wall endpoint/join snap wins; angle-snap
* suppresses alignment) and only calls this when alignment should apply.
*
* `excludeIds` drops those nodes' anchors from the candidate pool — used when
* dragging a wall / fence endpoint so the moving endpoint doesn't try to
* align to its own (and its linked siblings') geometry that moves with it.
*/
export function alignFloorplanDraftPoint(
point: readonly [number, number],
opts?: { bypass?: boolean; threshold?: number; excludeIds?: readonly string[] },
): [number, number] {
if (opts?.bypass) {
useAlignmentGuides.getState().clear()
return [point[0], point[1]]
}
let candidates = collectAlignmentAnchors(useScene.getState().nodes, FLOORPLAN_DRAFT_ALIGN_ID)
if (opts?.excludeIds?.length) {
const excluded = new Set(opts.excludeIds)
candidates = candidates.filter((anchor) => !excluded.has(anchor.nodeId))
}
const { point: snapped } = applyFloorplanAlignment(
point,
[{ nodeId: FLOORPLAN_DRAFT_ALIGN_ID, kind: 'corner', x: point[0], z: point[1] }],
candidates,
{ threshold: opts?.threshold },
)
return snapped
}