From 94d5d0bc0af04ec956689d91dcaeade13303099f Mon Sep 17 00:00:00 2001 From: karim Date: Tue, 20 Oct 2026 00:56:59 +0200 Subject: [PATCH] Polylinie: Punkttypen Eckpunkt/Bogenpunkt/Splinepunkt (Kontextmenue), keine Bogen-Griffe mehr an geraden Segmenten --- src/App.tsx | 4 +- src/geometry/polylineVertexType.test.ts | 59 ++++++++ src/geometry/polylineVertexType.ts | 182 ++++++++++++++++++++++++ src/i18n/de.ts | 4 + src/i18n/en.ts | 4 + src/menus/contextMenu.tsx | 21 ++- src/state/projectSlice.ts | 19 +++ src/state/useProjectActions.ts | 2 + 8 files changed, 293 insertions(+), 2 deletions(-) create mode 100644 src/geometry/polylineVertexType.test.ts create mode 100644 src/geometry/polylineVertexType.ts diff --git a/src/App.tsx b/src/App.tsx index 3002d5c7..037e67ab 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1602,6 +1602,7 @@ export default function App() { setBuiltinPaletteVisible, insertDrawingPoint, deleteDrawingPoint, + setPolylineVertexType, patchWallType, setOverrideRules, patchCeilingType, @@ -3939,11 +3940,12 @@ export default function App() { if (selectedDrawingIds.length !== 1) return null; const d = project.drawings2d.find((x) => x.id === selectedDrawingIds[0]); return d && d.geom.shape === "polyline" - ? { id: d.id, pts: d.geom.pts, closed: d.geom.closed } + ? { id: d.id, pts: d.geom.pts, closed: d.geom.closed, bulge: d.geom.bulge } : null; })(), insertPolylinePoint: insertDrawingPoint, deletePolylinePoint: deleteDrawingPoint, + setPolylineVertexType, reorderLevel: (id, op) => reorderDrawingLevels([id], op), })} onClose={() => setMenu(null)} diff --git a/src/geometry/polylineVertexType.test.ts b/src/geometry/polylineVertexType.test.ts new file mode 100644 index 00000000..efec60b5 --- /dev/null +++ b/src/geometry/polylineVertexType.test.ts @@ -0,0 +1,59 @@ +import { describe, expect, it } from "vitest"; +import { bulgeArc, bulgeArcPoints } from "./bulge"; +import { canSetVertexType, vertexTypeOf, withVertexType } from "./polylineVertexType"; + +const g = { pts: [{ x: 0, y: 0 }, { x: 2, y: 1 }, { x: 4, y: 0 }, { x: 6, y: 1 }], closed: false }; + +describe("Punkttypen einer Polylinie", () => { + it("Eckpunkt setzt beide Nachbarsegmente gerade", () => { + const b = withVertexType({ ...g, bulge: [0.3, -0.2, 0.1] }, 1, "corner"); + expect(b).toEqual([0, 0, 0.1]); + }); + + it("Bogenpunkt: beide Segmente liegen auf EINEM Kreis durch die drei Punkte", () => { + const b = withVertexType(g, 1, "arc"); + const a1 = bulgeArc(g.pts[0], g.pts[1], b[0])!; + const a2 = bulgeArc(g.pts[1], g.pts[2], b[1])!; + expect(a1).not.toBeNull(); + expect(Math.hypot(a1.center.x - a2.center.x, a1.center.y - a2.center.y)).toBeLessThan(1e-6); + expect(a1.r).toBeCloseTo(a2.r, 6); + expect(vertexTypeOf({ ...g, bulge: b }, 1)).toBe("arc"); + // Die Bögen bauchen NACH OBEN aus (Punkt 1 liegt oben) — kein Zickzack über den Kreis + const mid1 = bulgeArcPoints(g.pts[0], g.pts[1], b[0]); + const apex = mid1[mid1.length / 2 | 0]; + expect(apex.y).toBeGreaterThan(0.5); + }); + + it("Splinepunkt: Tangenten beider Segmente im Punkt stimmen überein", () => { + const g = { pts: [{ x: 0, y: 0 }, { x: 2, y: 1 }, { x: 5, y: 0.2 }], closed: false }; + const b = withVertexType(g, 1, "spline"); + // exakte Tangenten aus den Bögen (senkrecht zum Radius, Richtung nach Umlaufsinn) + const tan = (a: { x: number; y: number }, c: { x: number; y: number }, bb: number, atEnd: boolean) => { + const arc = bulgeArc(a, c, bb)!; + const ang = atEnd ? arc.a0 + arc.sweep : arc.a0; + const sg = Math.sign(arc.sweep); + return Math.atan2(Math.cos(ang) * sg, -Math.sin(ang) * sg); + }; + const t1 = tan(g.pts[0], g.pts[1], b[0], true); + const t2 = tan(g.pts[1], g.pts[2], b[1], false); + expect(Math.abs(t1 - t2)).toBeLessThan(1e-6); + // Tangente = Richtung Vorgänger→Nachfolger + expect(Math.abs(t2 - Math.atan2(0.2, 5))).toBeLessThan(1e-6); + expect(vertexTypeOf({ ...g, bulge: b }, 1)).toBe("spline"); + }); + + it("offene Enden: Bogen/Spline nicht möglich, Ecke schon", () => { + expect(canSetVertexType(g, 0, "arc")).toBe(false); + expect(canSetVertexType(g, 3, "spline")).toBe(false); + expect(canSetVertexType(g, 0, "corner")).toBe(true); + }); + + it("geschlossene Polylinie: erster Punkt hat beide Nachbarn", () => { + const sq = { pts: [{ x: 0, y: 0 }, { x: 1, y: 0 }, { x: 1, y: 1 }, { x: 0, y: 1 }], closed: true }; + const b = withVertexType(sq, 0, "arc"); + expect(b.length).toBe(4); + expect(b[3]).not.toBe(0); + expect(b[0]).not.toBe(0); + expect(b[1]).toBe(0); + }); +}); diff --git a/src/geometry/polylineVertexType.ts b/src/geometry/polylineVertexType.ts new file mode 100644 index 00000000..0ded4ba1 --- /dev/null +++ b/src/geometry/polylineVertexType.ts @@ -0,0 +1,182 @@ +// Punkttypen einer Polylinie (ArchiCAD/Vectorworks-artig): Eckpunkt, Bogenpunkt, +// Splinepunkt. Rein als Umrechnung der Segment-Bögen (`bulge`) der beiden am Punkt +// anliegenden Segmente — alle Renderer/Exporte verstehen Bulges bereits, es gibt +// also kein neues Geometrie-Feld. +// +// • Eckpunkt: beide Segmente gerade (bulge 0). +// • Bogenpunkt: EIN Kreisbogen durch Vorgänger, Punkt und Nachfolger. +// • Splinepunkt: tangential glatter Übergang — die Tangente im Punkt ist parallel +// zur Verbindung Vorgänger→Nachfolger (wie bei einer Catmull-Rom-Kurve), beide +// Segmente werden zu Bögen, die diese Tangente teilen. +// +// Die Bögen werden über ihren SCHEITELPUNKT bestimmt und mit `bulgeFromSagittaPoint` +// in einen Bulge übersetzt — so bleibt die Vorzeichen-Konvention an EINER Stelle. + +import type { Vec2 } from "../model/types"; +import { bulgeArc, bulgeFromSagittaPoint } from "./bulge"; + +export type PolylineVertexType = "corner" | "arc" | "spline"; + +interface PolylineLike { + pts: Vec2[]; + closed: boolean; + bulge?: number[]; +} + +const EPS = 1e-9; + +/** Indizes des ein- und ausgehenden Segments von Punkt `i` (null = keins). */ +function adjacentSegments(n: number, closed: boolean, i: number): { inSeg: number | null; outSeg: number | null } { + const inSeg = i > 0 ? i - 1 : closed ? n - 1 : null; + const outSeg = i < n - 1 ? i : closed ? n - 1 : null; + return { inSeg, outSeg }; +} + +/** Nachbarpunkte von `i` (null an offenen Enden). */ +function neighbors(g: PolylineLike, i: number): { prev: Vec2 | null; next: Vec2 | null } { + const n = g.pts.length; + const prev = i > 0 ? g.pts[i - 1] : g.closed ? g.pts[n - 1] : null; + const next = i < n - 1 ? g.pts[i + 1] : g.closed ? g.pts[0] : null; + return { prev, next }; +} + +const cross = (a: Vec2, b: Vec2) => a.x * b.y - a.y * b.x; +const sub = (a: Vec2, b: Vec2): Vec2 => ({ x: a.x - b.x, y: a.y - b.y }); + +/** Umkreismittelpunkt von a,b,c (null bei kollinearen Punkten). */ +function circumcenter(a: Vec2, b: Vec2, c: Vec2): Vec2 | null { + const d = 2 * (a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y)); + if (Math.abs(d) < EPS) return null; + const a2 = a.x * a.x + a.y * a.y, b2 = b.x * b.x + b.y * b.y, c2 = c.x * c.x + c.y * c.y; + return { + x: (a2 * (b.y - c.y) + b2 * (c.y - a.y) + c2 * (a.y - b.y)) / d, + y: (a2 * (c.x - b.x) + b2 * (a.x - c.x) + c2 * (b.x - a.x)) / d, + }; +} + +/** + * Bulge des Kreisbogens a→b auf dem Kreis um `c`, und zwar des Bogens, der NICHT + * durch `other` läuft (Scheitel auf der anderen Seite der Sehne als `other`). + */ +function bulgeOnCircle(a: Vec2, b: Vec2, c: Vec2, other: Vec2): number { + const r = Math.hypot(a.x - c.x, a.y - c.y); + const mid = { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 }; + let u = sub(mid, c); + let len = Math.hypot(u.x, u.y); + if (len < EPS) { + const d = sub(b, a); + u = { x: -d.y, y: d.x }; + len = Math.hypot(u.x, u.y); + } + u = { x: u.x / len, y: u.y / len }; + const side = (p: Vec2) => Math.sign(cross(sub(b, a), sub(p, a))); + const cand1 = { x: c.x + u.x * r, y: c.y + u.y * r }; + const cand2 = { x: c.x - u.x * r, y: c.y - u.y * r }; + const apex = side(cand1) !== side(other) ? cand1 : cand2; + return bulgeFromSagittaPoint(a, b, apex); +} + +/** Tangente (Fahrtrichtung a→b) des Bulge-Bogens am Anfang bzw. Ende. */ +function arcTangent(a: Vec2, b: Vec2, bulge: number, atEnd: boolean): Vec2 { + const arc = bulgeArc(a, b, bulge); + if (!arc) { + const d = sub(b, a); + const l = Math.hypot(d.x, d.y) || 1; + return { x: d.x / l, y: d.y / l }; + } + const ang = atEnd ? arc.a0 + arc.sweep : arc.a0; + const s = Math.sign(arc.sweep) || 1; + return { x: -Math.sin(ang) * s, y: Math.cos(ang) * s }; +} + +/** + * Bulge des Bogens a→b mit gegebener Tangente `t` am Ende (atEnd) bzw. Anfang. + * Betrag aus dem Winkel Sehne↔Tangente, Vorzeichen NUMERISCH gewählt (die + * Variante, deren tatsächliche Tangente zu `t` passt). + */ +function bulgeWithTangent(a: Vec2, b: Vec2, t: Vec2, atEnd: boolean): number { + const c = sub(b, a); + const ang = Math.abs(Math.atan2(cross(c, t), c.x * t.x + c.y * t.y)); + const mag = Math.abs(bulgeFromApexAngle(a, b, ang)); + if (mag < 1e-9) return 0; + const dot = (v: Vec2) => v.x * t.x + v.y * t.y; + return dot(arcTangent(a, b, mag, atEnd)) >= dot(arcTangent(a, b, -mag, atEnd)) ? mag : -mag; +} + +const bulgeWithEndTangent = (a: Vec2, b: Vec2, t: Vec2) => bulgeWithTangent(a, b, t, true); +const bulgeWithStartTangent = (a: Vec2, b: Vec2, t: Vec2) => bulgeWithTangent(a, b, t, false); + +/** + * Bogen über der Sehne a→b mit halbem Öffnungswinkel |half| (Sehne↔Tangente); + * Vorzeichen: >0 Scheitel links der Sehne, <0 rechts. Über den Scheitelpunkt in + * einen Bulge übersetzt. + */ +function bulgeFromApexAngle(a: Vec2, b: Vec2, half: number): number { + if (Math.abs(half) < 1e-6) return 0; + // Begrenzen: knapp unter einem Halbkreis je Segment (sonst kippt der Bogen). + const h2 = Math.max(-Math.PI / 2 + 0.01, Math.min(Math.PI / 2 - 0.01, half)); + const d = sub(b, a); + const chord = Math.hypot(d.x, d.y); + if (chord < EPS) return 0; + const left = { x: -d.y / chord, y: d.x / chord }; + const s = (chord / 2) * Math.tan(h2 / 2); // Pfeilhöhe + const apex = { x: (a.x + b.x) / 2 + left.x * s, y: (a.y + b.y) / 2 + left.y * s }; + return bulgeFromSagittaPoint(a, b, apex); +} + +/** Ist Punkttyp `type` am Punkt `i` möglich? (Bogen/Spline brauchen beide Nachbarn.) */ +export function canSetVertexType(g: PolylineLike, i: number, type: PolylineVertexType): boolean { + if (i < 0 || i >= g.pts.length) return false; + if (type === "corner") return true; + const { prev, next } = neighbors(g, i); + return !!prev && !!next; +} + +/** Aktueller Typ (Heuristik): beide Nachbarsegmente gerade → Eckpunkt, sonst null (gemischt). */ +export function vertexTypeOf(g: PolylineLike, i: number): PolylineVertexType | null { + const { inSeg, outSeg } = adjacentSegments(g.pts.length, g.closed, i); + const b = (k: number | null) => (k === null ? 0 : g.bulge?.[k] ?? 0); + if (Math.abs(b(inSeg)) < 1e-9 && Math.abs(b(outSeg)) < 1e-9) return "corner"; + const { prev, next } = neighbors(g, i); + if (!prev || !next || inSeg === null || outSeg === null) return null; + const a1 = bulgeArc(prev, g.pts[i], b(inSeg)); + const a2 = bulgeArc(g.pts[i], next, b(outSeg)); + if (a1 && a2 && Math.hypot(a1.center.x - a2.center.x, a1.center.y - a2.center.y) < 1e-6 * (1 + a1.r)) return "arc"; + return "spline"; +} + +/** Neue Bulge-Liste mit Punkttyp `type` an Punkt `i` (Eingabe bleibt unverändert). */ +export function withVertexType(g: PolylineLike, i: number, type: PolylineVertexType): number[] { + const n = g.pts.length; + const segCount = g.closed ? n : n - 1; + const bulge = Array.from({ length: segCount }, (_, k) => g.bulge?.[k] ?? 0); + if (!canSetVertexType(g, i, type)) return bulge; + const { inSeg, outSeg } = adjacentSegments(n, g.closed, i); + const q = g.pts[i]; + const { prev, next } = neighbors(g, i); + if (type === "corner") { + if (inSeg !== null) bulge[inSeg] = 0; + if (outSeg !== null) bulge[outSeg] = 0; + return bulge; + } + const p = prev!, r = next!; + if (type === "arc") { + const c = circumcenter(p, q, r); + if (!c) { + bulge[inSeg!] = 0; + bulge[outSeg!] = 0; + return bulge; + } + bulge[inSeg!] = bulgeOnCircle(p, q, c, r); + bulge[outSeg!] = bulgeOnCircle(q, r, c, p); + return bulge; + } + // spline: Tangente parallel zu prev→next + const t = sub(r, p); + const tl = Math.hypot(t.x, t.y); + if (tl < EPS) return bulge; + const tu = { x: t.x / tl, y: t.y / tl }; + bulge[inSeg!] = bulgeWithEndTangent(p, q, tu); + bulge[outSeg!] = bulgeWithStartTangent(q, r, tu); + return bulge; +} diff --git a/src/i18n/de.ts b/src/i18n/de.ts index 3ebd1734..134dc411 100644 --- a/src/i18n/de.ts +++ b/src/i18n/de.ts @@ -766,6 +766,10 @@ export const de = { "ctx.print": "Drucken", "ctx.insertPoint": "Punkt einfügen", "ctx.deletePoint": "Punkt löschen", + "ctx.vertexType.corner": "Eckpunkt", + "ctx.vertexType.arc": "Bogenpunkt", + "ctx.vertexType.spline": "Splinepunkt", + "ctx.vertexType.needsNeighbors": "Nur bei Punkten mit Vorgänger und Nachfolger", "ctx.delete": "Löschen", "ctx.delete.lastLayer": "Die letzte Ebene kann nicht gelöscht werden.", "ctx.delete.lastLevel": "Die letzte Zeichnungsebene kann nicht gelöscht werden.", diff --git a/src/i18n/en.ts b/src/i18n/en.ts index ba072101..47761e14 100644 --- a/src/i18n/en.ts +++ b/src/i18n/en.ts @@ -761,6 +761,10 @@ export const en: Record = { "ctx.print": "Print", "ctx.insertPoint": "Insert point", "ctx.deletePoint": "Delete point", + "ctx.vertexType.corner": "Corner point", + "ctx.vertexType.arc": "Arc point", + "ctx.vertexType.spline": "Spline point", + "ctx.vertexType.needsNeighbors": "Only for points with a previous and next point", "ctx.delete": "Delete", "ctx.delete.lastLayer": "The last layer cannot be deleted.", "ctx.delete.lastLevel": "The last drawing level cannot be deleted.", diff --git a/src/menus/contextMenu.tsx b/src/menus/contextMenu.tsx index da0aaa53..3ad3d190 100644 --- a/src/menus/contextMenu.tsx +++ b/src/menus/contextMenu.tsx @@ -8,6 +8,7 @@ // Kommentare deutsch (CONVENTIONS.md). import { t } from "../i18n"; +import { canSetVertexType, vertexTypeOf, type PolylineVertexType } from "../geometry/polylineVertexType"; import { nearestPolylineTarget, toleranceFor } from "../geometry/polylinePoint"; import type { LayerCategory, Project, Vec2 } from "../model/types"; import { flattenCategories } from "../model/types"; @@ -84,7 +85,7 @@ export interface MenuActions { * löschen" im Grundriss-Kontextmenü. `null`, wenn nichts oder etwas * anderes selektiert ist; dann entfallen die beiden Einträge. */ - selectedPolyline: { id: string; pts: Vec2[]; closed: boolean } | null; + selectedPolyline: { id: string; pts: Vec2[]; closed: boolean; bulge?: number[] } | null; /** Ob eine Element-Stil-Zwischenablage gefüllt ist (aktiviert „Einfügen"). */ hasDrawingStyleClipboard: boolean; /** Kopiert das Aussehen des ERSTEN gewählten 2D-Elements. */ @@ -93,6 +94,7 @@ export interface MenuActions { pasteDrawingStyle: () => void; insertPolylinePoint: (id: string, segIndex: number, pt: Vec2) => void; deletePolylinePoint: (id: string, index: number) => void; + setPolylineVertexType: (id: string, index: number, type: PolylineVertexType) => void; /** Anzahl aktuell selektierter Elemente ÜBER ALLE Arten (Wand/2D/Decke/ * Raum/Treppe/Stütze/Dach/Öffnung) — Grundlage für „Sperren"/„Entsperren". */ lockableSelectionCount: number; @@ -289,6 +291,23 @@ function planMenuItems( title: enabled ? undefined : t("alert.polylineMinPoints"), onClick: () => a.deletePolylinePoint(poly.id, target.index), }); + // Punkttyp (ArchiCAD/Vectorworks): Eckpunkt / Bogenpunkt / Splinepunkt. + const current = vertexTypeOf(poly, target.index); + const typeItem = (type: PolylineVertexType, key: string, icon: string): ContextMenuItem => { + const possible = canSetVertexType(poly, target.index, type); + return { + label: `${current === type ? "✓ " : ""}${t(key)}`, + icon: , + disabled: !possible, + title: possible ? undefined : t("ctx.vertexType.needsNeighbors"), + onClick: () => a.setPolylineVertexType(poly.id, target.index, type), + }; + }; + pointItems.push( + typeItem("corner", "ctx.vertexType.corner", "north_east"), + typeItem("arc", "ctx.vertexType.arc", "radio_button_unchecked"), + typeItem("spline", "ctx.vertexType.spline", "gesture"), + ); } // Eigenschaften übertragen — dasselbe Muster wie im Ebenen-Menü, nur für // 2D-Elemente. Kopieren ab einem gewählten Element, Einfügen sobald etwas diff --git a/src/state/projectSlice.ts b/src/state/projectSlice.ts index 0952b590..cb695896 100644 --- a/src/state/projectSlice.ts +++ b/src/state/projectSlice.ts @@ -8,6 +8,7 @@ // laufen. Die Slice tippt daher nur ihre eigenen Felder; `get()` liefert // RootState (siehe appStore.ts), wir greifen punktuell auf View-Felder zu. +import { withVertexType, type PolylineVertexType } from "../geometry/polylineVertexType"; import { sampleProject } from "../model/sampleProject"; import { flattenCategories, @@ -182,6 +183,12 @@ export interface ProjectSlice { * Polylinie braucht ≥2, geschlossene ≥3. */ deleteDrawingPoint: (drawingId: string, index: number) => void; + /** + * Punkttyp eines Polylinien-Stützpunkts setzen (Eckpunkt/Bogenpunkt/Splinepunkt), + * s. `geometry/polylineVertexType.ts` — rechnet nur die Bulges der beiden + * anliegenden Segmente um. + */ + setPolylineVertexType: (drawingId: string, index: number, type: PolylineVertexType) => void; // ── Editier-Griffe + Body-Move des selektierten Elements ────────────────── moveGripOf: ( @@ -1126,6 +1133,18 @@ export function createProjectSlice( }), })), + setPolylineVertexType: (drawingId, index, type) => + setProject((p) => ({ + ...p, + drawings2d: p.drawings2d.map((d) => { + if (d.id !== drawingId || d.geom.shape !== "polyline" || d.locked) return d; + const g = d.geom; + const bulge = withVertexType(g, index, type); + const { bulge: _old, ...rest } = g; + return { ...d, geom: bulge.some((b) => b !== 0) ? { ...rest, bulge } : rest }; + }), + })), + deleteDrawingPoint: (drawingId, index) => setProject((p) => { const d = p.drawings2d.find((x) => x.id === drawingId); diff --git a/src/state/useProjectActions.ts b/src/state/useProjectActions.ts index 8665c4ee..7e94b994 100644 --- a/src/state/useProjectActions.ts +++ b/src/state/useProjectActions.ts @@ -120,6 +120,7 @@ export function useProjectActions( const setBuiltinPaletteVisible = useStore((s) => s.setBuiltinPaletteVisible); const insertDrawingPoint = useStore((s) => s.insertDrawingPoint); const deleteDrawingPoint = useStore((s) => s.deleteDrawingPoint); + const setPolylineVertexType = useStore((s) => s.setPolylineVertexType); // Wandstile: immutable Patch eines Wandtyps (z. B. Schichtfugen-Stile). Kein // eigener Store-Slice nötig — läuft direkt über setProject wie die übrigen // Ressourcen-Mutationen. @@ -468,6 +469,7 @@ export function useProjectActions( setBuiltinPaletteVisible, insertDrawingPoint, deleteDrawingPoint, + setPolylineVertexType, patchWallType, setOverrideRules, patchCeilingType,