diff --git a/src/core/extra.ts b/src/core/extra.ts index ebd4d258..ab96497d 100644 --- a/src/core/extra.ts +++ b/src/core/extra.ts @@ -68,6 +68,8 @@ export { drawingVertices, gripPt, openEdges, polylineEdgeGrips, ringEdges } from export { verticalExtents } from "../geometry/grips"; export type { VerticalExtent } from "../geometry/grips"; export type { GripRequest } from "../geometry/grips"; +export { coincidentGripPoints, excavationGripPoints, gripAxisLock, gripEdgeTarget, gripPolar, gripPolarPoint, openingGripPoint, openingSheetSlide, openingSlidePosition, sheetHeightDrag, stairMoveHandle, wallGripPoints } from "../geometry/gripDrag"; +export type { EdgeDrive, GripLocks, SheetHeightKind } from "../geometry/gripDrag"; export { openingPreviewProject, openingPreviewSvgs } from "../plan/openingPreview"; export type { OpeningPreviewSvgs } from "../plan/openingPreview"; export { surfaceOpeningInfo, surfaceOpeningGrips, surfaceOpeningGripPatch } from "../plan/surfaceOpening"; diff --git a/src/geometry/gripDrag.test.ts b/src/geometry/gripDrag.test.ts new file mode 100644 index 00000000..3db0beca --- /dev/null +++ b/src/geometry/gripDrag.test.ts @@ -0,0 +1,67 @@ +// Fassade der Griff-Zug-Rechnung ([128], Kern `grip_drag.rs`): Übergabe an den Kern und zurück. + +import { describe, expect, it } from "vitest"; +import { + coincidentGripPoints, + gripAxisLock, + gripEdgeTarget, + gripPolar, + gripPolarPoint, + openingGripPoint, + openingSheetSlide, + openingSlidePosition, + sheetHeightDrag, + wallGripPoints, +} from "./gripDrag"; + +const wall = { start: { x: 0, y: 0 }, end: { x: 5, y: 0 } }; + +describe("Griff-Zug (Kern)", () => { + it("Länge/Winkel und Punkt aus Locks", () => { + expect(gripPolar({ x: 1, y: 1 }, { x: 1, y: 3 }, { x: 0, y: 1 })).toEqual({ length: 2, angleDeg: 90, offset: 2 }); + const p = gripPolarPoint({ x: 0, y: 0 }, { length: 5 }, { x: 3, y: 0 }); + expect(p.x).toBeCloseTo(5, 12); + expect(p.y).toBeCloseTo(0, 12); + }); + + it("Shift-Achsen-Lock: Flucht der Nachbarkante, ohne Ring nur H/V", () => { + const ring = [{ x: 0, y: 0 }, { x: 1, y: 1 }, { x: 1, y: 3 }]; + const p = gripAxisLock({ x: 2.1, y: 1.9 }, { x: 0, y: 0 }, { x: 1, y: 1 }, ring, 1, false); + expect(p.x).toBeCloseTo(2, 12); + expect(p.y).toBeCloseTo(2, 12); + expect(gripAxisLock({ x: 3, y: 0.4 }, { x: 0, y: 0 }, null, null, -1, false)).toEqual({ x: 3, y: 0 }); + }); + + it("Kanten-Ziel: geführt mit Raster/Snap, frei mit Ortho, gelockt", () => { + const guided = { mid: { x: 0, y: 0 }, normal: { x: 0, y: 1 }, aIndex: 0, bIndex: 1 }; + expect(gripEdgeTarget(guided, { x: 7, y: 1.3 }, { grid: 0.5 })).toEqual({ x: 0, y: 1.5 }); + expect(gripEdgeTarget(guided, { x: 7, y: 1.3 }, { grid: 0.5, snapped: { x: 2, y: 1.27 } })).toEqual({ x: 0, y: 1.27 }); + expect(gripEdgeTarget(guided, { x: 7, y: 1.3 }, { locks: { length: 0.25 } })).toEqual({ x: 0, y: 0.25 }); + const free = { ...guided, mid: { x: 1, y: 1 }, free: true }; + expect(gripEdgeTarget(free, { x: 3.2, y: 1.4 }, { shift: true })).toEqual({ x: 3.2, y: 1 }); + }); + + it("Höhen-Griffe: Patch je Art", () => { + expect(sheetHeightDrag("wallTop", { base: 3, top: 5.6, bottom: 3 }, 5.7349, false)).toEqual({ top: 5.73, bottom: 3, patch: { height: 2.73 } }); + const r = sheetHeightDrag("openingBottom", { base: 0, top: 2.1, bottom: 0.9 }, 0.6, false); + expect(r.patch).toEqual({ sillHeight: 0.6, height: 1.5 }); + }); + + it("Öffnung entlang der Wand und im Schnitt", () => { + expect(openingGripPoint(wall, { position: 1, width: 1.2 })).toEqual({ x: 1.6, y: 0 }); + expect(openingSlidePosition(wall, { width: 1.2 }, { x: 2.6, y: 3 })).toBe(2); + expect(openingSlidePosition({ start: wall.start, end: wall.start }, { width: 1.2 }, { x: 1, y: 0 })).toBeNull(); + expect(openingSheetSlide(wall, { position: 1, width: 1.2 }, { x: -1, y: 0 }, 0.5)).toEqual({ position: 0.5, du: 0.5 }); + expect(openingSheetSlide(wall, { position: 1, width: 1.2 }, { x: 0.1, y: 0.995 }, 1)).toBeNull(); + }); + + it("Wand-Griffe und koinzidente Ecken", () => { + expect(wallGripPoints({ ...wall })).toHaveLength(2); + const g = wallGripPoints({ start: { x: 0, y: 0 }, end: { x: 4, y: 0 }, bulge: 0.5 }); + expect(g[2].x).toBeCloseTo(2, 12); + expect(g[2].y).toBeCloseTo(1, 12); + const corners = [{ x: 0, y: 0 }, { x: 4, y: 0 }]; + const groups = [[{ x: 4.03, y: 0 }, { x: 4, y: 3 }], [], [{ x: 0, y: 0.04 }]]; + expect(coincidentGripPoints(corners, groups)).toEqual([{ group: 2, index: 0 }, { group: 0, index: 0 }]); + }); +}); diff --git a/src/geometry/gripDrag.ts b/src/geometry/gripDrag.ts new file mode 100644 index 00000000..3024e19c --- /dev/null +++ b/src/geometry/gripDrag.ts @@ -0,0 +1,160 @@ +// Griff-Zug-Rechnung ([128]): dünne Fassade über den Rust-Kern (`src-tauri/core/src/grip_drag.rs`, +// Bindungen `web/grip_drag.rs`). Wohin ein gezogener Griff fällt (Länge/Winkel-Felder, Shift-Achsen-Lock, +// Kanten-Griffe, Höhen-Griffe im Schnitt, Öffnung entlang der Wand, koinzidente Ecken) rechnet der Kern; +// `state/useGripEditing.ts` hält nur Zustand und Vorschau. + +import type { EdgeGrip, Excavation, Opening, Stair, Vec2, Wall } from "../model/types"; +import { core } from "../engine/coreWasm"; + +const v = (f: ArrayLike, i = 0): Vec2 => ({ x: f[i], y: f[i + 1] }); + +function flat(pts: readonly Vec2[]): Float64Array { + const f = new Float64Array(pts.length * 2); + for (let i = 0; i < pts.length; i++) { + f[2 * i] = pts[i].x; + f[2 * i + 1] = pts[i].y; + } + return f; +} + +function points(f: ArrayLike): Vec2[] { + const out: Vec2[] = new Array(f.length >> 1); + for (let i = 0; i < out.length; i++) out[i] = v(f, 2 * i); + return out; +} + +/** Gelockte Länge (m) / Winkel (Grad) des Feld-Controllers; fehlend = folgt dem Cursor. */ +export interface GripLocks { + length?: number; + angle?: number; +} + +/** Länge, Winkel (Grad, [0, 360)) und Abstand entlang `normal` von `anchor` nach `p` (Felder, HUD). */ +export function gripPolar(anchor: Vec2, p: Vec2, normal: Vec2 = { x: 0, y: 0 }): { length: number; angleDeg: number; offset: number } { + const f = core().grip_polar(anchor.x, anchor.y, p.x, p.y, normal.x, normal.y); + return { length: f[0], angleDeg: f[1], offset: f[2] }; +} + +/** Punkt aus Anker + gelockter Länge/Winkel; ungelockte Werte folgen dem rohen Cursor `raw`. */ +export function gripPolarPoint(anchor: Vec2, locks: GripLocks, raw: Vec2): Vec2 { + return v(core().grip_polar_point(anchor.x, anchor.y, locks.length ?? NaN, locks.angle ?? NaN, raw.x, raw.y)); +} + +/** + * Shift-Achsen-Lock eines Punkt-Griffs: Welt-H/V durch den Nachbar-Griff `anchor`, bei einem Vielecks- + * Eckpunkt (`ring` = Punkte des Elements, `index` darin, `origin` = Lage vor dem Zug) zusätzlich die Flucht + * der beiden fixen Nachbarkanten. Die nächstliegende Achse gewinnt. + */ +export function gripAxisLock(raw: Vec2, anchor: Vec2, origin: Vec2 | null, ring: readonly Vec2[] | null, index: number, closed: boolean): Vec2 { + const f = ring && origin ? flat(ring) : new Float64Array(); + return v(core().grip_axis_lock(raw.x, raw.y, anchor.x, anchor.y, origin?.x ?? NaN, origin?.y ?? NaN, f, index, closed)); +} + +/** Führung eines Kanten-Zugs (s. {@link gripEdgeTarget}). */ +export interface EdgeDrive { + /** Feld-Controller offen: Ziel aus Anker + Locks, ohne Raster/Ortho. */ + locks?: GripLocks | null; + shift?: boolean; + /** Rasterweite (0 = aus). */ + grid?: number; + /** Gefangener Geometrie-Snap (nur geführte Kanten). */ + snapped?: Vec2 | null; +} + +/** + * Zielpunkt eines Kanten-Zugs relativ zum Anker `edge.mid`: geführte Kante nur entlang der Normale + * (Geometrie-Snap vor Raster), freie Kante (`edge.free`) voller Cursor mit Ortho/Raster. + */ +export function gripEdgeTarget(edge: EdgeGrip, raw: Vec2, d: EdgeDrive = {}): Vec2 { + const { mid, normal } = edge; + const l = d.locks; + const s = d.snapped; + return v( + core().grip_edge_target( + mid.x, mid.y, normal.x, normal.y, !!edge.free, raw.x, raw.y, + !!l, l?.length ?? NaN, l?.angle ?? NaN, !!d.shift, d.grid ?? 0, s?.x ?? NaN, s?.y ?? NaN, + ), + ); +} + +/** Art eines Höhen-Griffs im Schnitt/in der Ansicht. */ +export type SheetHeightKind = "wallTop" | "openingTop" | "openingBottom"; + +/** + * Höhen-Griff auf die Blatt-Höhe `y` ziehen (1-cm-Raster, `free` = ohne): neue Ober-/Unterkante im Blatt und + * der Patch fürs Bauteil (`height`, Fenster-Unterkante zusätzlich `sillHeight`; mm-genau). + */ +export function sheetHeightDrag( + kind: SheetHeightKind, + g: { base: number; top: number; bottom: number }, + y: number, + free: boolean, +): { top: number; bottom: number; patch: Record } { + const k = kind === "wallTop" ? 0 : kind === "openingTop" ? 1 : 2; + const f = core().grip_sheet_height(k, g.base, g.top, g.bottom, y, free); + const patch: Record = kind === "openingBottom" ? { sillHeight: f[3], height: f[2] } : { height: f[2] }; + return { top: f[0], bottom: f[1], patch }; +} + +/** Mittelpunkt-Griff einer Öffnung auf der Achse ihrer Wirtswand. */ +export function openingGripPoint(w: Pick, op: Pick): Vec2 { + return v(core().grip_opening_point(w.start.x, w.start.y, w.end.x, w.end.y, op.position, op.width)); +} + +/** Neue `position` beim Ziehen des Öffnungs-Griffs nach `pt` (auf die Wand geklemmt); null = Wand ohne Länge. */ +export function openingSlidePosition(w: Pick, op: Pick, pt: Vec2): number | null { + const f = core().grip_opening_slide(w.start.x, w.start.y, w.end.x, w.end.y, op.width, pt.x, pt.y); + return f.length ? f[0] : null; +} + +/** + * Öffnung im Schnitt-Blatt waagrecht ziehen: Blatt-Delta `du` (gesamt, entlang `uAxis`) → neue `position` + * und das wirksame Delta (nach dem Klemmen); null, wenn die Wand fast quer zur Schnittlinie steht. + */ +export function openingSheetSlide( + w: Pick, + op: Pick, + uAxis: Vec2, + du: number, +): { position: number; du: number } | null { + const f = core().grip_opening_slide_sheet(w.start.x, w.start.y, w.end.x, w.end.y, uAxis.x, uAxis.y, op.position, op.width, du); + return f.length ? { position: f[0], du: f[1] } : null; +} + +/** Bewegpunkt einer Treppe (Mitte des ersten Laufs, Wendeltreppe im Zentrum). */ +export function stairMoveHandle(st: Stair): Vec2 { + const c = st.center; + return v(core().grip_stair_handle(st.shape === "spiral", c?.x ?? NaN, c?.y ?? NaN, st.start.x, st.start.y, st.dir?.x ?? 0, st.dir?.y ?? 0, st.runLength)); +} + +/** Griffe einer Wand: Start, Ende, bei einer Bogenwand ([T2]) der Bogenscheitel. */ +export function wallGripPoints(w: Pick): Vec2[] { + return points(core().grip_wall_points(w.start.x, w.start.y, w.end.x, w.end.y, w.bulge ?? 0)); +} + +/** Griff-Punkte einer Baugrube: Umriss-Ecken, dann der Sohlen-Griff (Schwerpunkt, y + Sohlenhöhe). */ +export function excavationGripPoints(e: Excavation): Vec2[] { + return points(core().grip_excavation_points(flat(e.outline), e.bottom)); +} + +/** + * Koinzidente Punkte anderer Elemente (`groups`, je Element seine Griff-Punkte) an den Ecken `corners` des + * verschobenen Elements (Toleranz 5 cm): `{group, index}` je Punkt, ohne Doppel. + */ +export function coincidentGripPoints(corners: readonly Vec2[], groups: readonly (readonly Vec2[])[]): { group: number; index: number }[] { + if (corners.length === 0 || groups.length === 0) return []; + const n = groups.reduce((a, g) => a + 1 + 2 * g.length, 0); + const f = new Float64Array(n); + let k = 0; + for (const g of groups) { + f[k++] = g.length; + for (const p of g) { + f[k++] = p.x; + f[k++] = p.y; + } + } + const r = core().grip_coincident(flat(corners), f); + const out: { group: number; index: number }[] = []; + for (let i = 0; i + 1 < r.length; i += 2) out.push({ group: r[i], index: r[i + 1] }); + return out; +} diff --git a/src/state/excavationGrips.test.ts b/src/state/excavationGrips.test.ts index 7e27b81a..580a491f 100644 --- a/src/state/excavationGrips.test.ts +++ b/src/state/excavationGrips.test.ts @@ -4,7 +4,7 @@ import { describe, expect, it } from "vitest"; import { documentFor } from "../core/document"; import { sampleProject } from "../model/sampleProject"; import type { Excavation, Project } from "../model/types"; -import { excavationGripPoints } from "./useGripEditing"; +import { excavationGripPoints } from "../core"; const pit: Excavation = { id: "bg", diff --git a/src/state/useGripEditing.ts b/src/state/useGripEditing.ts index cdafbd3e..06b65c5d 100644 --- a/src/state/useGripEditing.ts +++ b/src/state/useGripEditing.ts @@ -7,15 +7,30 @@ import { useRef, useState, type RefObject } from "react"; import { useEventBundle, useSameData } from "./useEvent"; -import { add, dot, leftNormal, normalize, scale, sub, wallCorners } from "../model/geometry"; -import type { Ceiling, Drawing2D, EdgeGrip, Excavation, Project, Room, Stair, Vec2 } from "../model/types"; +import { leftNormal, normalize, scale, sub, wallCorners } from "../model/geometry"; +import type { Ceiling, Drawing2D, EdgeGrip, Excavation, Project, Room, Vec2 } from "../model/types"; import { wallTypeThickness } from "../model/types"; import { bulgeArcPoints, drawingVertices, surfaceOpeningGripPatch, surfaceOpeningGrips, surfaceOpeningInfo, wallArcBand } from "../core"; import { gripDrawingIdsFor } from "./gripLimits"; import { openEdges, polylineEdgeGrips, ringEdges } from "../core"; +import { + coincidentGripPoints, + excavationGripPoints, + gripAxisLock, + gripEdgeTarget, + gripPolar, + gripPolarPoint, + openingGripPoint, + openingSheetSlide, + openingSlidePosition, + sheetHeightDrag, + stairMoveHandle, + wallGripPoints, + type GripLocks, +} from "../core"; import { areaMovePreview, drawingMovePreview } from "../tools/transform"; import type { DraftShape, SnapResult, SnapSettings, ToolDraft, ToolMods } from "../tools/types"; -import { applyAngleConstraint, computeSnap } from "../tools/snapping"; +import { computeSnap } from "../tools/snapping"; import { useCommandTabShortcut } from "./useKeyboardShortcuts"; import { sheetOwnerOutlines } from "../tools/sheetSnapGeometry"; import type { BodyTarget } from "../views/plan/PlanView"; @@ -32,18 +47,15 @@ export interface GripEditFieldsState { active: boolean; } -/** Scheitel eines Bogens a→b (Modell-Bulge: Pfeilhöhe = halbe Sehne · bulge, links der Sehne). */ -function arcApex(a: Vec2, b: Vec2, bulge: number): Vec2 { - const h = Math.hypot(b.x - a.x, b.y - a.y) / 2; - return add(scale(add(a, b), 0.5), scale(normalize(leftNormal(sub(b, a))), h * bulge)); -} - -/** Griff-Punkte einer Baugrube: Umriss-Ecken, dann der Sohlen-Griff (Schwerpunkt, y + Sohlenhöhe; Kern `grips.rs`). */ -export function excavationGripPoints(e: Excavation): Vec2[] { - const n = Math.max(1, e.outline.length); - const cx = e.outline.reduce((a, q) => a + q.x, 0) / n; - const cy = e.outline.reduce((a, q) => a + q.y, 0) / n; - return [...e.outline, { x: cx, y: cy + e.bottom }]; +/** Länge/Winkel-Felder des Befehlsfelds (Live-Werte `live`, gelockte aus `ed.locks`). */ +function polarFields( + ed: { active: "length" | "angle"; locks: GripLocks }, + live: { length: number; angleDeg: number }, +): GripEditFieldsState[] { + return [ + { id: "length", labelKey: "cmd.field.length", value: ed.locks.length ?? live.length, locked: ed.locks.length != null, active: ed.active === "length" }, + { id: "angle", labelKey: "cmd.field.angle", value: ed.locks.angle ?? live.angleDeg, locked: ed.locks.angle != null, active: ed.active === "angle" }, + ]; } export function useGripEditing(opts: { @@ -154,15 +166,8 @@ export function useGripEditing(opts: { const { walls: selectedWallIds, drawings: selectedDrawingIds } = selection; const groupChanges = opts.groupChanges ?? ((fn: () => void) => fn()); - // Bewegpunkt (Move-Handle) einer Treppe: liegt auf der Treppe (Mitte des - // ersten Laufs bei gerade/L, Zentrum bei Wendel) und verschiebt beim Ziehen - // die GANZE Treppe (moveStairBy mit dem Delta). Ein einzelner, gut sichtbarer - // Griff — analog zum Öffnungs-Mittelpunkt. - const stairMoveHandle = (st: Stair): Vec2 => { - if (st.shape === "spiral") return st.center ?? st.start; - const d = normalize(st.dir); - return add(st.start, scale(d, st.runLength / 2)); - }; + // Bewegpunkt einer Treppe (`stairMoveHandle`, Kern `grip_drag.rs`): liegt auf der Treppe und verschiebt + // beim Ziehen die GANZE Treppe (moveStairBy mit dem Delta) — analog zum Öffnungs-Mittelpunkt. // Welchem Element + lokalem Vertex-Index ein Griff im flachen `grips`-Array // gehört (Mehrfachauswahl, Nutzer-Report: Griffe verschwanden komplett, @@ -263,8 +268,7 @@ export function useGripEditing(opts: { } const w = op && project.walls.find((x) => x.id === op.hostWallId); if (op && w) { - const axis = normalize(sub(w.end, w.start)); - const pt = add(w.start, scale(axis, op.position + op.width / 2)); + const pt = openingGripPoint(w, op); gripOwners.push({ localIndex: 0 }); gripOwnerRange.push([0, 0]); return [pt]; @@ -344,7 +348,7 @@ export function useGripEditing(opts: { for (const id of selectedWallIds) { const w = project.walls.find((x) => x.id === id); // Bogenwand ([T2]): dritter Griff im Bogenscheitel (zieht den Bogen durch den Punkt). - if (w) pushGroup(w.bulge ? [w.start, w.end, arcApex(w.start, w.end, w.bulge)] : [w.start, w.end], { wallId: id }); + if (w) pushGroup(wallGripPoints(w), { wallId: id }); } } return out; @@ -455,10 +459,7 @@ export function useGripEditing(opts: { // mitgeführt (gemeinsame Ecke bleibt verbunden). Lazy beim ersten Move-Event // bestimmt (Snapshot der Selektion), je Drag in onGripEnd zurückgesetzt. null = // noch nicht bestimmt. - const moveCoincidentRef = useRef< - | { drawingId: string | null; wallId: string | null; index: number; pos: Vec2 }[] - | null - >(null); + const moveCoincidentRef = useRef<{ drawingId: string | null; wallId: string | null; index: number; pos: Vec2 }[] | null>(null); // Eckpunkte eines selektierten Elements (Wand: start/end; 2D: drawingVertices). const elementCorners = (drawingId: string | null, wallId: string | null): Vec2[] => { if (drawingId) { @@ -471,42 +472,23 @@ export function useGripEditing(opts: { } return []; }; - // Koinzidente Sibling-Vertices (unter den ÜBRIGEN gewählten Elementen) an JEDER - // Ecke des verschobenen Elements. Toleranz wie beim 3D-Drag (gemeinsame Ecke). - const COINCIDENT_TOL_2D = 0.05; // m - const findMoveCoincident2D = ( - selfDrawingId: string | null, - selfWallId: string | null, - ): { drawingId: string | null; wallId: string | null; index: number; pos: Vec2 }[] => { + // Koinzidente Sibling-Vertices (unter den ÜBRIGEN gewählten Elementen) an JEDER Ecke des verschobenen + // Elements (Kern `coincidentGripPoints`, Toleranz wie beim 3D-Drag): hier nur die Kandidaten sammeln. + type CoincidentVertex = { drawingId: string | null; wallId: string | null; index: number; pos: Vec2 }; + const findMoveCoincident2D = (selfDrawingId: string | null, selfWallId: string | null): CoincidentVertex[] => { const corners = elementCorners(selfDrawingId, selfWallId); if (corners.length === 0) return []; - const near = (p: Vec2, q: Vec2) => Math.hypot(p.x - q.x, p.y - q.y) <= COINCIDENT_TOL_2D; - const seen = new Set(); - const out: { drawingId: string | null; wallId: string | null; index: number; pos: Vec2 }[] = []; - const add2 = (drawingId: string | null, wallId: string | null, index: number, pos: Vec2) => { - const key = `${drawingId ?? ""}|${wallId ?? ""}|${index}`; - if (seen.has(key)) return; - seen.add(key); - out.push({ drawingId, wallId, index, pos: { ...pos } }); - }; - for (const corner of corners) { - for (const id of selectedWallIds) { - if (id === selfWallId) continue; - const w = project.walls.find((x) => x.id === id); - if (!w) continue; - if (near(corner, w.start)) add2(null, id, 0, w.start); - if (near(corner, w.end)) add2(null, id, 1, w.end); - } - for (const id of selectedDrawingIds) { - if (id === selfDrawingId) continue; - const d = project.drawings2d.find((x) => x.id === id); - if (!d) continue; - drawingVertices(d, scaleDenominator).forEach((v, i) => { - if (near(corner, v)) add2(id, null, i, v); - }); - } + const owners: { drawingId: string | null; wallId: string | null }[] = []; + const groups: Vec2[][] = []; + for (const id of selectedWallIds) { + const w = id !== selfWallId ? project.walls.find((x) => x.id === id) : undefined; + if (w) { owners.push({ drawingId: null, wallId: id }); groups.push([w.start, w.end]); } } - return out; + for (const id of selectedDrawingIds) { + const d = id !== selfDrawingId ? project.drawings2d.find((x) => x.id === id) : undefined; + if (d) { owners.push({ drawingId: id, wallId: null }); groups.push(drawingVertices(d, scaleDenominator)); } + } + return coincidentGripPoints(corners, groups).map(({ group, index }) => ({ ...owners[group], index, pos: { ...groups[group][index] } })); }; // Editieren läuft AD-HOC (gripHandlers → moveGripOf), NICHT durch die Engine. @@ -557,47 +539,15 @@ export function useGripEditing(opts: { // mitdriften). const gripOriginRef = useRef(null); - // Endpunkt aus Anker + (gelockten/Live-)Länge/Winkel — wie lineEndFromFields, - // hier für den Vertex-Drag. Ungelockte Größen folgen dem rohen Cursor `raw`. - const gripEditPoint = ( - anchor: Vec2, - locks: { length?: number; angle?: number }, - raw: Vec2, - ): Vec2 => { - const DEG = Math.PI / 180; - const dx = raw.x - anchor.x; - const dy = raw.y - anchor.y; - const length = locks.length != null ? locks.length : Math.hypot(dx, dy); - const angleDeg = locks.angle != null ? locks.angle : (Math.atan2(dy, dx) * 180) / Math.PI; - const a = angleDeg * DEG; - return { x: anchor.x + Math.cos(a) * length, y: anchor.y + Math.sin(a) * length }; - }; + // Endpunkt aus Anker + (gelockten/Live-)Länge/Winkel (Kern `gripPolarPoint`) — wie lineEndFromFields. + const gripEditPoint = gripPolarPoint; // Felder fürs Befehlsfeld während des Vertex-Drags (Live ODER gelockt). const gripEditFields = (): GripEditFieldsState[] => { const ge = gripEdit; const info = gripDragInfoRef.current; if (!ge || !info) return []; - const dx = info.raw.x - ge.anchor.x; - const dy = info.raw.y - ge.anchor.y; - const liveLen = Math.hypot(dx, dy); - const liveAng = ((((Math.atan2(dy, dx) * 180) / Math.PI) % 360) + 360) % 360; - return [ - { - id: "length", - labelKey: "cmd.field.length", - value: ge.locks.length ?? liveLen, - locked: ge.locks.length != null, - active: ge.active === "length", - }, - { - id: "angle", - labelKey: "cmd.field.angle", - value: ge.locks.angle ?? liveAng, - locked: ge.locks.angle != null, - active: ge.active === "angle", - }, - ]; + return polarFields(ge, gripPolar(ge.anchor, info.raw)); }; // Fixer Bezug (Anker) für Länge/Winkel eines gezogenen Punkt-Griffs. Normal @@ -621,30 +571,12 @@ export function useGripEditing(opts: { return refIndex != null ? grips[refIndex] : null; }; - // Die beiden (fixen) Polygon-Nachbarn eines gezogenen Punkt-Griffs — für die - // Flucht-Kandidaten unten. Anders als `neighborGripIndex` (EIN Nachbar, - // reicht für Länge/Winkel-Anker) werden hier BEIDE gebraucht: ein Vielecks- - // Eckpunkt hängt an ZWEI Kanten, jede liefert eine eigene mögliche Flucht- - // Richtung. Kennt geschlossene Ringe (rect/geschlossene Polylinie/Raum/ - // Decke — Nachbar 0/n-1 wickelt um) vs. offene Ketten (offene Polylinie/ - // Linie — Enden haben nur einen Nachbarn). Kreis/Bogen/Wand/Öffnung/Treppe - // haben keine Polygon-Nachbarn (kein Vielecks-Eckpunkt) → {null,null}. - const ringNeighborPts = ( - pts: Vec2[], - i: number, - closed: boolean, - ): { prev: Vec2 | null; next: Vec2 | null } => { - const n = pts.length; - const prevIdx = i > 0 ? i - 1 : closed ? n - 1 : null; - const nextIdx = i < n - 1 ? i + 1 : closed ? 0 : null; - return { - prev: prevIdx != null && prevIdx !== i ? pts[prevIdx] : null, - next: nextIdx != null && nextIdx !== i && nextIdx !== prevIdx ? pts[nextIdx] : null, - }; - }; - const resolvePolygonNeighbors = (index: number): { prev: Vec2 | null; next: Vec2 | null } => { + // Punkte + Ring-Art des Elements eines gezogenen Punkt-Griffs — für die Flucht-Kandidaten des Shift- + // Achsen-Locks (Kern `gripAxisLock`: Welt-H/V plus die Flucht der beiden fixen Nachbarkanten). Geschlossen: + // rect/geschlossene Polylinie/Raum/Decke (Nachbar 0/n-1 wickelt um); offen: Polylinie/Linie. Kreis/Bogen/ + // Wand/Öffnung/Treppe haben keine Polygon-Nachbarn → null. + const resolvePolygonRing = (index: number): { pts: Vec2[]; index: number; closed: boolean } | null => { const owner = gripOwners[index]; - const none = { prev: null, next: null }; if (!owner?.drawingId && !owner?.wallId) { // Raum/Decke (exklusiv) — kein Owner, `localIndex` fehlt dann auch, also // ist der flache `index` selbst der Ring-Index. @@ -655,44 +587,18 @@ export function useGripEditing(opts: { : selectedRoom ? selectedRoom.boundary : null; - return pts ? ringNeighborPts(pts, owner?.localIndex ?? index, true) : none; + return pts ? { pts, index: owner?.localIndex ?? index, closed: true } : null; } - if (owner.wallId) return none; // Wand: nur 2 Punkte, kein Vielecks-Nachbar - const d = project.drawings2d.find((x) => x.id === owner.drawingId); - const g = d?.geom; - if (!g) return none; - if (g.shape === "polyline" || g.shape === "spline") - return ringNeighborPts(g.pts, owner.localIndex, !!g.closed); + if (owner.wallId) return null; // Wand: nur 2 Punkte, kein Vielecks-Nachbar + const g = project.drawings2d.find((x) => x.id === owner.drawingId)?.geom; + if (!g) return null; + const i = owner.localIndex; + if (g.shape === "polyline" || g.shape === "spline") return { pts: g.pts, index: i, closed: !!g.closed }; if (g.shape === "rect" || g.shape === "image") { - const corners = [g.min, { x: g.max.x, y: g.min.y }, g.max, { x: g.min.x, y: g.max.y }]; - return ringNeighborPts(corners, owner.localIndex, true); + return { pts: [g.min, { x: g.max.x, y: g.min.y }, g.max, { x: g.min.x, y: g.max.y }], index: i, closed: true }; } - if (g.shape === "line") return ringNeighborPts([g.a, g.b], owner.localIndex, false); - return none; // circle/arc: kein Vielecks-Eckpunkt - }; - - // Rohen Cursor `raw` auf die nächstliegende von mehreren Kandidaten-Achsen - // projizieren (jeweils Stützpunkt + Richtung) — "nächstliegend" = kleinster - // senkrechter Abstand von `raw` zur (unendlich verlängerten) Achse. Deckt - // Shift-Ortho (Welt-H/V) UND Flucht-Snap (Richtung anliegender, fixer - // Kanten) in einem einheitlichen Mechanismus ab. - const closestAxisPoint = (raw: Vec2, candidates: { point: Vec2; dir: Vec2 }[]): Vec2 | null => { - let best: Vec2 | null = null; - let bestDist = Infinity; - for (const c of candidates) { - const len = Math.hypot(c.dir.x, c.dir.y); - if (len < 1e-9) continue; // entartete Richtung (koinzidenter Nachbar) → überspringen - const u = { x: c.dir.x / len, y: c.dir.y / len }; - const rel = sub(raw, c.point); - const t = rel.x * u.x + rel.y * u.y; - const proj = add(c.point, scale(u, t)); - const dist = Math.hypot(raw.x - proj.x, raw.y - proj.y); - if (dist < bestDist) { - bestDist = dist; - best = proj; - } - } - return best; + if (g.shape === "line") return { pts: [g.a, g.b], index: i, closed: false }; + return null; // circle/arc: kein Vielecks-Eckpunkt }; // Stabiler Ref auf cycleGripEditField (der globale Tab-Handler hat leere Deps). @@ -716,11 +622,8 @@ export function useGripEditing(opts: { if (!anchor) return; // ohne Anker keine polare Eingabe möglich setGripEdit((prev) => { if (!prev) { - const frozen = grips[info.index]; - const dx = frozen.x - anchor.x; - const dy = frozen.y - anchor.y; - const angle = ((Math.atan2(dy, dx) * 180) / Math.PI + 360) % 360; - return { anchor, active: "length", locks: { length: Math.hypot(dx, dy), angle } }; + const { length, angleDeg: angle } = gripPolar(anchor, grips[info.index]); + return { anchor, active: "length", locks: { length, angle } }; } const active = prev.active === "length" ? "angle" : "length"; const locks = { ...prev.locks }; @@ -767,19 +670,9 @@ export function useGripEditing(opts: { // Letzter roher Cursor + der gezogene Kanten-Griff (Momentaufnahme vom Grab). const edgeDragInfoRef = useRef<{ edge: EdgeGrip; raw: Vec2 } | null>(null); - // Zielpunkt aus Anker + (gelockten/Live-)Werten — bei freien Kanten wie ein - // Punkt-Griff (polar), bei geführten Kanten nur entlang der festen Normale. - const edgeEditPoint = ( - edge: EdgeGrip, - anchor: Vec2, - locks: { length?: number; angle?: number }, - raw: Vec2, - ): Vec2 => { - if (edge.free) return gripEditPoint(anchor, locks, raw); - const live = dot(sub(raw, anchor), edge.normal); - const len = locks.length != null ? locks.length : live; - return add(anchor, scale(edge.normal, len)); - }; + // Zielpunkt aus Anker (= `edge.mid`) + (gelockten/Live-)Werten — bei freien Kanten wie ein Punkt-Griff + // (polar), bei geführten Kanten nur entlang der festen Normale (Kern `gripEdgeTarget`). + const edgeEditPoint = (edge: EdgeGrip, locks: GripLocks, raw: Vec2): Vec2 => gripEdgeTarget(edge, raw, { locks }); // Felder fürs Befehlsfeld während des Kanten-Drags. Geführte Kanten zeigen // NUR "Länge" (kein Winkel — der ist durch die feste Normale vorgegeben). @@ -788,38 +681,11 @@ export function useGripEditing(opts: { const info = edgeDragInfoRef.current; if (!ee || !info) return []; const { edge, raw } = info; + const live = gripPolar(ee.anchor, raw, edge.normal); if (!edge.free) { - const live = dot(sub(raw, ee.anchor), edge.normal); - return [ - { - id: "length", - labelKey: "cmd.field.length", - value: ee.locks.length ?? live, - locked: ee.locks.length != null, - active: true, - }, - ]; + return [{ id: "length", labelKey: "cmd.field.length", value: ee.locks.length ?? live.offset, locked: ee.locks.length != null, active: true }]; } - const dx = raw.x - ee.anchor.x; - const dy = raw.y - ee.anchor.y; - const liveLen = Math.hypot(dx, dy); - const liveAng = ((((Math.atan2(dy, dx) * 180) / Math.PI) % 360) + 360) % 360; - return [ - { - id: "length", - labelKey: "cmd.field.length", - value: ee.locks.length ?? liveLen, - locked: ee.locks.length != null, - active: ee.active === "length", - }, - { - id: "angle", - labelKey: "cmd.field.angle", - value: ee.locks.angle ?? liveAng, - locked: ee.locks.angle != null, - active: ee.active === "angle", - }, - ]; + return polarFields(ee, live); }; const cycleEdgeEditFieldRef = useRef<() => void>(() => {}); @@ -835,15 +701,9 @@ export function useGripEditing(opts: { setEdgeEdit((prev) => { if (!prev) { const anchor = edge.mid; - const frozen = edgeEditPoint(edge, anchor, {}, raw); - const dx = frozen.x - anchor.x; - const dy = frozen.y - anchor.y; - if (edge.free) { - const angle = ((Math.atan2(dy, dx) * 180) / Math.PI + 360) % 360; - return { anchor, active: "length", locks: { length: Math.hypot(dx, dy), angle } }; - } - const offset = dx * edge.normal.x + dy * edge.normal.y; - return { anchor, active: "length", locks: { length: offset } }; + const frozen = gripPolar(anchor, edgeEditPoint(edge, {}, raw), edge.normal); + if (edge.free) return { anchor, active: "length", locks: { length: frozen.length, angle: frozen.angleDeg } }; + return { anchor, active: "length", locks: { length: frozen.offset } }; } if (!edge.free) return prev; // geführte Kante: nur ein Feld, nichts zu zykeln const active = prev.active === "length" ? "angle" : "length"; @@ -877,7 +737,7 @@ export function useGripEditing(opts: { const locks = { ...ee.locks, [ee.active]: value }; const next = edge.free ? (ee.active === "length" ? "angle" : "length") : ee.active; setEdgeEdit({ ...ee, locks, active: next }); - const pt = edgeEditPoint(edge, ee.anchor, locks, raw); + const pt = edgeEditPoint(edge, locks, raw); applyEdgeToward(edge, ee.anchor, pt); }; @@ -906,16 +766,8 @@ export function useGripEditing(opts: { return; } const w = op && project.walls.find((x) => x.id === op.hostWallId); - if (op && w) { - const d = sub(w.end, w.start); - const len = Math.hypot(d.x, d.y); - if (len > 1e-9) { - const axis = scale(d, 1 / len); - const t = dot(sub(pt, w.start), axis); - const pos = Math.max(0, Math.min(len - op.width, t - op.width / 2)); - updateOpening(op.id, { position: pos }); - } - } + const pos = op && w ? openingSlidePosition(w, op, pt) : null; + if (op && pos != null) updateOpening(op.id, { position: pos }); return; } // Treppe: den Bewegpunkt verfolgen → ganze Treppe um das Delta (Ziel − @@ -1135,23 +987,16 @@ export function useGripEditing(opts: { const hudFor = (index: number, pt: Vec2): NonNullable | undefined => { const anchor = resolveGripAnchor(index); if (!anchor) return undefined; - const dx = pt.x - anchor.x; - const dy = pt.y - anchor.y; - const angleDeg = ((Math.atan2(dy, dx) * 180) / Math.PI + 360) % 360; - return { at: pt, length: Math.hypot(dx, dy), angleDeg }; + const { length, angleDeg } = gripPolar(anchor, pt); + return { at: pt, length, angleDeg }; }; // Dasselbe für einen gezogenen Kanten-Griff: freie Kanten zeigen Länge+Winkel // (wie ein Punkt-Griff), geführte Kanten nur EINEN Wert (Abstand entlang der // Normale) — dafür reicht das HUD-Schema nicht (length+angleDeg gehören // zusammen), daher als freier Text `Δ: …m`. const edgeHudFor = (edge: EdgeGrip, anchor: Vec2, pt: Vec2): NonNullable => { - const dx = pt.x - anchor.x; - const dy = pt.y - anchor.y; - if (edge.free) { - const angleDeg = ((Math.atan2(dy, dx) * 180) / Math.PI + 360) % 360; - return { at: pt, length: Math.hypot(dx, dy), angleDeg }; - } - const offset = dx * edge.normal.x + dy * edge.normal.y; + const { length, angleDeg, offset } = gripPolar(anchor, pt, edge.normal); + if (edge.free) return { at: pt, length, angleDeg }; return { at: pt, text: `Δ: ${offset.toFixed(3)}m` }; }; @@ -1235,15 +1080,11 @@ export function useGripEditing(opts: { const op = (project.openings ?? []).find((o) => o.id === openingId); const w = op && project.walls.find((x) => x.id === op.hostWallId); if (!op || !w || op.locked) return; - const axis = normalize(sub(w.end, w.start)); - const factor = dot(sh.uAxis, axis); - if (Math.abs(factor) < 0.15) return; const prevDu = sheetDuRef.current && sheetDuRef.current.id === openingId ? sheetDuRef.current.du : 0; - const len = Math.hypot(w.end.x - w.start.x, w.end.y - w.start.y); - // Auf die Wand klemmen (Öffnung bleibt drin) — auch die Vorschau. - const rawPos = op.position + (prevDu + delta.x) * factor; - const pos = Math.max(0, Math.min(len - op.width, rawPos)); - const du = (pos - op.position) / factor; + // Auf die Wand geklemmt (Öffnung bleibt drin) — auch die Vorschau. + const slide = openingSheetSlide(w, op, sh.uAxis, prevDu + delta.x); + if (!slide) return; + const { position: pos, du } = slide; sheetDuRef.current = { id: openingId, du }; pendingOpeningMoveRef.current = { id: openingId, position: pos }; const preview: DraftShape[] = sheetOwnerOutlines(sh.levelId, "opening", openingId).map((pts) => ({ @@ -1260,23 +1101,9 @@ export function useGripEditing(opts: { if (!modelEditable && sheet) { const g = sheetHeightGrips[index]; if (!g) return; - const v = mods.ctrl ? raw.y : Math.round(raw.y * 100) / 100; // 1-cm-Raster, Strg = frei - let newTop = g.top; - let newBottom = g.bottom; - if (g.kind === "wallTop") { - newTop = Math.max(g.base + 0.3, v); - pendingSheetHeightRef.current = { kind: "wall", id: g.id, patch: { height: Math.round((newTop - g.base) * 1000) / 1000 } }; - } else if (g.kind === "openingTop") { - newTop = Math.max(g.bottom + 0.2, v); - pendingSheetHeightRef.current = { kind: "opening", id: g.id, patch: { height: Math.round((newTop - g.bottom) * 1000) / 1000 } }; - } else { - newBottom = Math.min(g.top - 0.2, Math.max(g.base, v)); - pendingSheetHeightRef.current = { - kind: "opening", - id: g.id, - patch: { sillHeight: Math.round((newBottom - g.base) * 1000) / 1000, height: Math.round((g.top - newBottom) * 1000) / 1000 }, - }; - } + // 1-cm-Raster, Strg = frei; Mindestmasse und mm-Rundung im Kern (`sheetHeightDrag`). + const { top: newTop, bottom: newBottom, patch } = sheetHeightDrag(g.kind, g, raw.y, !!mods.ctrl); + pendingSheetHeightRef.current = { kind: g.kind === "wallTop" ? "wall" : "opening", id: g.id, patch }; const bottom = g.kind === "wallTop" ? g.bottom : newBottom; setDraft({ preview: [{ @@ -1327,17 +1154,9 @@ export function useGripEditing(opts: { // dominiert (kein Snap), damit die Endkoordinate garantiert eine Achse // trifft — identisch zum bisherigen Ortho-Verhalten. if (mods.shift && anchor) { - const candidates: { point: Vec2; dir: Vec2 }[] = [ - { point: anchor, dir: { x: 1, y: 0 } }, - { point: anchor, dir: { x: 0, y: 1 } }, - ]; const origin = gripOriginRef.current; - if (origin) { - const { prev, next } = resolvePolygonNeighbors(index); - if (prev) candidates.push({ point: prev, dir: sub(origin, prev) }); - if (next) candidates.push({ point: next, dir: sub(next, origin) }); - } - const pt = closestAxisPoint(raw, candidates) ?? applyAngleConstraint(anchor, raw, 90); + const ring = origin ? resolvePolygonRing(index) : null; + const pt = gripAxisLock(raw, anchor, origin, ring?.pts ?? null, ring?.index ?? -1, ring?.closed ?? false); applyGrip(index, pt); setDraft({ preview: gripPreviewShapes(), vertices: [], snap: null, hud: hudFor(index, pt) }); return; @@ -1475,49 +1294,27 @@ export function useGripEditing(opts: { // `locks` ist ab dem Öffnen (Tab) immer befüllt (s. cycleEdgeEditField), // die Maus wirkt also erst wieder, sobald ein Feld per Tab gelöst wurde. if (ee && (ee.locks.length != null || ee.locks.angle != null)) { - const pt = edgeEditPoint(edge, anchor, ee.locks, raw); + const pt = edgeEditPoint(edge, ee.locks, raw); applyEdgeToward(edge, anchor, pt); setDraft({ preview: edgePreviewShapes(), vertices: [], snap: null, hud: edgeHudFor(edge, anchor, pt) }); return; } + // Raster nur ohne Strg und bei eingeschaltetem Raster-Snap. + const grid = !mods.ctrl && snap.enabled && snap.grid && snap.gridSize > 0 ? snap.gridSize : 0; if (edge.free) { - // Keine Geometrie-Snap-Projektion (keine feste Kanten-Normale, an die - // sich ein gefangener Punkt anlehnen könnte) — der Cursor bewegt den - // Zielpunkt direkt, nur Ortho/Raster wirken. - let pt = raw; - if (mods.shift) { - const dx = raw.x - anchor.x; - const dy = raw.y - anchor.y; - pt = Math.abs(dx) >= Math.abs(dy) ? { x: raw.x, y: anchor.y } : { x: anchor.x, y: raw.y }; - } - const gs = snap.gridSize; - if (!mods.ctrl && snap.enabled && snap.grid && gs > 0) { - const dx = Math.round((pt.x - anchor.x) / gs) * gs; - const dy = Math.round((pt.y - anchor.y) / gs) * gs; - pt = { x: anchor.x + dx, y: anchor.y + dy }; - } + // Keine Geometrie-Snap-Projektion (keine feste Kanten-Normale, an die sich ein gefangener Punkt + // anlehnen könnte) — der Cursor bewegt den Zielpunkt direkt, nur Ortho/Raster wirken (Kern). + const pt = gripEdgeTarget(edge, raw, { shift: mods.shift, grid }); applyEdgeToward(edge, anchor, pt); setDraft({ preview: edgePreviewShapes(), vertices: [], snap: null, hud: edgeHudFor(edge, anchor, pt) }); return; } - // Cursor zuerst durch die Snap-Engine schicken (Ctrl unterdrückt Snap - // wie überall sonst, siehe computeSnap). Ein GEOMETRIE-Snap (Endpunkt/ - // Mittelpunkt/Schnittpunkt/…, nicht Raster/Ortho) liefert den gefangenen - // Punkt, der dann auf die Außennormale projiziert wird — so rastet die - // Kante an reale Geometrie statt nur ans Raster. + // Cursor zuerst durch die Snap-Engine schicken (Ctrl unterdrückt Snap wie überall sonst, siehe + // computeSnap). Ein GEOMETRIE-Snap (Endpunkt/Mittelpunkt/Schnittpunkt/…, nicht Raster/Ortho) liefert den + // gefangenen Punkt, den der Kern auf die Aussennormale projiziert (vor dem Raster). const snapResult = snapFor(raw, pxPerMeter, mods); const isGeometrySnap = !!snapResult && snapResult.kind !== "grid" && snapResult.kind !== "ortho"; - const cursor = isGeometrySnap ? snapResult!.point : raw; - // Senkrechte Projektion von (Cursor − Anker) auf die Außennormale. - const rel = sub(cursor, anchor); - let offset = rel.x * edge.normal.x + rel.y * edge.normal.y; - // Raster-Schritt (Snap) nur, wenn kein Geometrie-Snap gefangen wurde — - // sonst gewinnt der gefangene Punkt. - const step = snap.gridSize; - if (!isGeometrySnap && !mods.ctrl && snap.enabled && snap.grid && step > 0) { - offset = Math.round(offset / step) * step; - } - const pt = add(anchor, scale(edge.normal, offset)); + const pt = gripEdgeTarget(edge, raw, { grid, snapped: isGeometrySnap ? snapResult!.point : null }); applyEdgeToward(edge, anchor, pt); setDraft({ preview: edgePreviewShapes(),