2D: Ellipse, Spline und Bezier als neue Zeichenwerkzeuge

Ellipse (zwei Bounding-Box-Ecken, achsparallel), Spline (Wegpunkte, geglättet
via Catmull-Rom, wie Polylinie mit rundem statt geradem Verlauf) und Bezier
(klassische manuelle Kontrollpunkt-Kette: Anker/Griff/Griff je Segment).

Neue Drawing2DGeom-Formen + Mathe-Helfer (tools/curves.ts). Rendering
tessellliert zu Linien-/Polygon-Primitiven (wie Polylinie/Rechteck) statt
eigenem SVG-Primitiv — dadurch funktionieren DXF-/Print-SVG-Export und
Hit-Testing ohne Änderung mit. Griff-Editieren (2D-PlanView + 3D-Viewport),
Verschieben/Skalieren, Snapping (Ellipse-Mittelpunkt, Bezier/Spline-Konturen)
und Georef-Rebase entsprechend erweitert.
This commit is contained in:
karim committed 2026-08-22 01:45:32 +02:00
1 parent c8ac5a3d89
commit 121cb5a372
27 files changed
+1299 -7

No files matched your search

+91
View File
@@ -0,0 +1,91 @@
import { describe, it, expect } from "vitest";
import { ellipsePts, sampleBezierChain, sampleCatmullRom } from "./curves";
describe("ellipsePts", () => {
it("liegt auf der Ellipse (rotation 0): Ost/Nord-Punkte bei rx/ry", () => {
const pts = ellipsePts({ x: 0, y: 0 }, 4, 2, 0, 4);
// t=0 → Ost (rx,0); t=π/2 → Nord (0,ry)
expect(pts[0].x).toBeCloseTo(4, 6);
expect(pts[0].y).toBeCloseTo(0, 6);
expect(pts[1].x).toBeCloseTo(0, 6);
expect(pts[1].y).toBeCloseTo(2, 6);
});
it("Drehung um 90° vertauscht die Achsen", () => {
const pts = ellipsePts({ x: 0, y: 0 }, 4, 2, Math.PI / 2, 4);
// Der ehemalige Ost-Punkt (rx,0) liegt jetzt bei (0, rx).
expect(pts[0].x).toBeCloseTo(0, 6);
expect(pts[0].y).toBeCloseTo(4, 6);
});
it("Mittelpunkt-Verschiebung wird respektiert", () => {
const pts = ellipsePts({ x: 10, y: 5 }, 3, 3, 0, 8);
for (const p of pts) {
expect(Math.hypot(p.x - 10, p.y - 5)).toBeCloseTo(3, 6);
}
});
});
describe("sampleBezierChain", () => {
it("weniger als 4 Punkte: Rohpunkte unverändert (Gummiband-Vorschau)", () => {
const pts = [{ x: 0, y: 0 }, { x: 1, y: 1 }];
expect(sampleBezierChain(pts)).toEqual(pts);
});
it("ein Segment: startet bei P0, endet bei P3", () => {
const p0 = { x: 0, y: 0 }, p1 = { x: 1, y: 3 }, p2 = { x: 3, y: 3 }, p3 = { x: 4, y: 0 };
const out = sampleBezierChain([p0, p1, p2, p3], 10);
expect(out[0]).toEqual(p0);
const last = out[out.length - 1];
expect(last.x).toBeCloseTo(p3.x, 6);
expect(last.y).toBeCloseTo(p3.y, 6);
});
it("t=0.5 folgt der De-Casteljau-Formel (symmetrischer Fall)", () => {
const p0 = { x: 0, y: 0 }, p1 = { x: 0, y: 4 }, p2 = { x: 4, y: 4 }, p3 = { x: 4, y: 0 };
const out = sampleBezierChain([p0, p1, p2, p3], 2); // t = 0.5 genau bei Index 1
// Symmetrische Kontrollpunkte → Kurvenmitte liegt exakt bei (2, 3).
expect(out[1].x).toBeCloseTo(2, 6);
expect(out[1].y).toBeCloseTo(3, 6);
});
it("zwei Segmente: verbindet am gemeinsamen Anker P3=P4", () => {
const pts = [
{ x: 0, y: 0 }, { x: 1, y: 1 }, { x: 2, y: 1 }, { x: 3, y: 0 },
{ x: 4, y: -1 }, { x: 5, y: -1 }, { x: 6, y: 0 },
];
const out = sampleBezierChain(pts, 5);
expect(out[0]).toEqual(pts[0]);
const last = out[out.length - 1];
expect(last.x).toBeCloseTo(6, 6);
expect(last.y).toBeCloseTo(0, 6);
});
});
describe("sampleCatmullRom", () => {
it("weniger als 3 Punkte: Rohpunkte unverändert", () => {
const pts = [{ x: 0, y: 0 }, { x: 1, y: 1 }];
expect(sampleCatmullRom(pts, false)).toEqual(pts);
});
it("offen: läuft durch den ersten und letzten Wegpunkt", () => {
const pts = [{ x: 0, y: 0 }, { x: 2, y: 3 }, { x: 5, y: 1 }, { x: 8, y: 4 }];
const out = sampleCatmullRom(pts, false, 8);
expect(out[0]).toEqual(pts[0]);
const last = out[out.length - 1];
expect(last.x).toBeCloseTo(8, 6);
expect(last.y).toBeCloseTo(4, 6);
});
it("Kollineare Punkte ergeben eine Gerade (kein Überschwingen)", () => {
const pts = [{ x: 0, y: 0 }, { x: 1, y: 0 }, { x: 2, y: 0 }, { x: 3, y: 0 }];
const out = sampleCatmullRom(pts, false, 4);
for (const p of out) expect(p.y).toBeCloseTo(0, 6);
});
it("geschlossen: liefert keinen doppelten Schlusspunkt (Aufrufer schliesst selbst)", () => {
const pts = [{ x: 0, y: 0 }, { x: 4, y: 0 }, { x: 4, y: 4 }, { x: 0, y: 4 }];
const out = sampleCatmullRom(pts, true, 6);
expect(out.length).toBe(pts.length * 6);
});
});
+104
View File
@@ -0,0 +1,104 @@
// Geometrie-Helfer für Ellipse/Bezier/Spline — reine Sample-/Tessellierungs-
// Funktionen, gemeinsam genutzt von den Zeichen-Werkzeugen (Vorschau), dem
// Grundriss-Generator (finales Rendering als Linien-/Polygon-Primitive) und
// dem Snapping (Kandidaten-Strecken).
import type { Vec2 } from "../model/types";
/** Punkte einer (ggf. gedrehten) Ellipse, N gleichverteilte Parameterschritte. */
export function ellipsePts(
center: Vec2,
rx: number,
ry: number,
rotation: number,
n = 64,
): Vec2[] {
const pts: Vec2[] = [];
const cosR = Math.cos(rotation);
const sinR = Math.sin(rotation);
for (let i = 0; i < n; i++) {
const t = (i / n) * Math.PI * 2;
const lx = rx * Math.cos(t);
const ly = ry * Math.sin(t);
pts.push({ x: center.x + lx * cosR - ly * sinR, y: center.y + lx * sinR + ly * cosR });
}
return pts;
}
/** Punkt auf einer kubischen Bezier-Kurve (De-Casteljau-Formel) bei Parameter `t` ∈ [0,1]. */
function cubicPoint(p0: Vec2, p1: Vec2, p2: Vec2, p3: Vec2, t: number): Vec2 {
const u = 1 - t;
const a = u * u * u;
const b = 3 * u * u * t;
const c = 3 * u * t * t;
const d = t * t * t;
return {
x: a * p0.x + b * p1.x + c * p2.x + d * p3.x,
y: a * p0.y + b * p1.y + c * p2.y + d * p3.y,
};
}
/**
* Tessellierte Punkte einer kubischen Bezier-Kette. `pts` folgt dem Muster
* Anker·Griff·Griff·Anker·… (`pts.length` ≡ 1 mod 3). Mit weniger als 4
* Punkten (noch im Bau) werden die Rohpunkte unverändert zurückgegeben
* (Gummiband-Vorschau).
*/
export function sampleBezierChain(pts: Vec2[], segsPerCurve = 24): Vec2[] {
if (pts.length < 4) return [...pts];
const out: Vec2[] = [pts[0]];
for (let i = 0; i + 3 < pts.length; i += 3) {
const p0 = pts[i], p1 = pts[i + 1], p2 = pts[i + 2], p3 = pts[i + 3];
for (let s = 1; s <= segsPerCurve; s++) out.push(cubicPoint(p0, p1, p2, p3, s / segsPerCurve));
}
return out;
}
/** Kontrollpunkt-Zugriff für Catmull-Rom mit Rand-Klemmung bzw. Ring-Umlauf. */
function crPoint(pts: Vec2[], i: number, closed: boolean): Vec2 {
const n = pts.length;
if (closed) return pts[((i % n) + n) % n];
if (i < 0) return pts[0];
if (i >= n) return pts[n - 1];
return pts[i];
}
/**
* Tessellierte Punkte einer glatten Kurve DURCH `pts` (uniformer Catmull-
* Rom-Spline). Mit weniger als 3 Punkten (noch im Bau bzw. entartet) werden
* die Rohpunkte unverändert zurückgegeben (Gummiband-Vorschau bzw. Gerade).
* `closed` schliesst den Ring OHNE expliziten Doppelpunkt am Ende — Aufrufer
* schliessen wie bei `polyline` selbst mit einem letzten Segment zurück zu
* `pts[0]`.
*/
export function sampleCatmullRom(pts: Vec2[], closed: boolean, segsPerSpan = 16): Vec2[] {
if (pts.length < 3) return [...pts];
const out: Vec2[] = [];
const spans = closed ? pts.length : pts.length - 1;
for (let i = 0; i < spans; i++) {
const p0 = crPoint(pts, i - 1, closed);
const p1 = crPoint(pts, i, closed);
const p2 = crPoint(pts, i + 1, closed);
const p3 = crPoint(pts, i + 2, closed);
for (let s = 0; s < segsPerSpan; s++) {
const t = s / segsPerSpan;
const t2 = t * t;
const t3 = t2 * t;
const x =
0.5 *
(2 * p1.x +
(-p0.x + p2.x) * t +
(2 * p0.x - 5 * p1.x + 4 * p2.x - p3.x) * t2 +
(-p0.x + 3 * p1.x - 3 * p2.x + p3.x) * t3);
const y =
0.5 *
(2 * p1.y +
(-p0.y + p2.y) * t +
(2 * p0.y - 5 * p1.y + 4 * p2.y - p3.y) * t2 +
(-p0.y + 3 * p1.y - 3 * p2.y + p3.y) * t3);
out.push({ x, y });
}
}
if (!closed) out.push(pts[pts.length - 1]);
return out;
}
+18
View File
@@ -12,6 +12,7 @@ import { add, leftNormal, lineIntersect, normalize, scale, sub } from "../model/
import { wallReferenceOffset } from "../model/wall";
import { centroid as roomCentroid } from "../geometry/roomArea";
import type { SnapKind, SnapResult, SnapSettings } from "./types";
import { sampleBezierChain, sampleCatmullRom } from "./curves";
const dist = (a: Vec2, b: Vec2): number => Math.hypot(a.x - b.x, a.y - b.y);
const mid = (a: Vec2, b: Vec2): Vec2 => ({ x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 });
@@ -101,6 +102,14 @@ function collectSegments(input: SnapInput): Seg[] {
const c1 = g.min, c3 = g.max;
const c2 = { x: g.max.x, y: g.min.y }, c4 = { x: g.min.x, y: g.max.y };
segs.push([c1, c2], [c2, c3], [c3, c4], [c4, c1]);
} else if (g.shape === "bezier") {
const pts = sampleBezierChain(g.pts);
for (let i = 0; i < pts.length - 1; i++) segs.push([pts[i], pts[i + 1]]);
} else if (g.shape === "spline") {
const closed = g.closed && g.pts.length > 2;
const pts = sampleCatmullRom(g.pts, closed);
for (let i = 0; i < pts.length - 1; i++) segs.push([pts[i], pts[i + 1]]);
if (closed) segs.push([pts[pts.length - 1], pts[0]]);
}
}
// Bereits gesetzte Draft-Segmente (Polylinie/Wand im Bau).
@@ -298,6 +307,15 @@ export function computeSnap(input: SnapInput): SnapResult | null {
consider(onCircle(c, c.arc.a1), "endpoint");
}
}
// Ellipse: nur der Mittelpunkt (keine Quadranten — bei gedrehter Ellipse
// wären die "Achsenpunkte" ohnehin keine Kreis-Quadranten mehr).
if (settings.center) {
for (const d of input.project.drawings2d) {
if (d.levelId !== input.levelId) continue;
if (!input.visibleCodes.has(d.categoryCode)) continue;
if (d.geom.shape === "ellipse") consider(d.geom.center, "center");
}
}
}
if (best) return best;
+42
View File
@@ -554,6 +554,45 @@ const arcTool: Tool = {
onCancel: (s) => [s, { draft: null, done: true }],
};
// Ellipse: Platzhalter-Werkzeug, gekoppelt an den `ellipse`-Befehl (zwei
// Bounding-Box-Ecken). Wie Kreis/Bogen nicht floorOnly.
const ellipseTool: Tool = {
id: "ellipse",
labelKey: "tool.ellipse",
hintKey: () => "tool.ellipse.hint",
init: () => ({ phase: "idle" }),
onClick: (s) => [s, { draft: null }],
onMove: (s) => [s, { draft: null }],
onCommitGesture: (s) => [s, { draft: null, done: true }],
onCancel: (s) => [s, { draft: null, done: true }],
};
// Spline: Platzhalter-Werkzeug, gekoppelt an den `spline`-Befehl (Wegpunkte,
// wie Polylinie, aber geglättet). Nicht floorOnly.
const splineTool: Tool = {
id: "spline",
labelKey: "tool.spline",
hintKey: () => "tool.spline.hint",
init: () => ({ phase: "idle" }),
onClick: (s) => [s, { draft: null }],
onMove: (s) => [s, { draft: null }],
onCommitGesture: (s) => [s, { draft: null, done: true }],
onCancel: (s) => [s, { draft: null, done: true }],
};
// Bezier: Platzhalter-Werkzeug, gekoppelt an den `bezier`-Befehl (manuelle
// Kontrollpunkt-Kette). Nicht floorOnly.
const bezierTool: Tool = {
id: "bezier",
labelKey: "tool.bezier",
hintKey: () => "tool.bezier.hint",
init: () => ({ phase: "idle" }),
onClick: (s) => [s, { draft: null }],
onMove: (s) => [s, { draft: null }],
onCommitGesture: (s) => [s, { draft: null, done: true }],
onCancel: (s) => [s, { draft: null, done: true }],
};
// Text: Platzhalter-Werkzeug, gekoppelt an den `text`-Befehl (Ankerpunkt →
// getippte Zeile). Wie Kreis/Bogen nicht floorOnly (freies 2D-Element).
const textTool: Tool = {
@@ -611,6 +650,9 @@ const TOOLS: Record<ToolId, Tool> = {
rect: rectTool,
circle: circleTool,
arc: arcTool,
ellipse: ellipseTool,
spline: splineTool,
bezier: bezierTool,
text: textTool,
textbox: textboxTool,
image: imageTool,
+6
View File
@@ -179,6 +179,12 @@ const mvGeom = (geom: Drawing2DGeom, fn: (p: Vec2) => Vec2): Drawing2DGeom => {
return { ...geom, center: fn(geom.center) };
case "arc":
return { ...geom, center: fn(geom.center) };
case "ellipse":
return { ...geom, center: fn(geom.center) };
case "bezier":
return { ...geom, pts: geom.pts.map(fn) };
case "spline":
return { ...geom, pts: geom.pts.map(fn) };
case "text":
return { ...geom, at: fn(geom.at) };
case "image": {
+3
View File
@@ -21,6 +21,9 @@ export type ToolId =
| "rect"
| "circle"
| "arc"
| "ellipse"
| "spline"
| "bezier"
| "text"
| "textbox"
| "image";