This repository has been archived on 2026-11-16. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
DOSSIER-STANDALONE/src/tools/curves.ts
T

157 lines
5.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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;
}
/** Punkt auf einem uniformen kubischen B-Spline-Segment (Standard-Basis, 4 Kontrollpunkte). */
function bsplineSegPoint(p0: Vec2, p1: Vec2, p2: Vec2, p3: Vec2, t: number): Vec2 {
const t2 = t * t;
const t3 = t2 * t;
const b0 = (1 - t) ** 3 / 6;
const b1 = (3 * t3 - 6 * t2 + 4) / 6;
const b2 = (-3 * t3 + 3 * t2 + 3 * t + 1) / 6;
const b3 = t3 / 6;
return {
x: p0.x * b0 + p1.x * b1 + p2.x * b2 + p3.x * b3,
y: p0.y * b0 + p1.y * b1 + p2.y * b2 + p3.y * b3,
};
}
/**
* Tessellierte Punkte eines kubischen B-Splines über den KONTROLLPUNKTEN
* `pts` — Gegenstück zu `sampleCatmullRom`: die Kurve verläuft NICHT durch
* die inneren Punkte, sie wird nur von ihnen „gezogen" (Kontrollnetz statt
* Durchgangspunkte, wie CV-Modus in AutoCAD/Rhino). Offen: GEKLEMMT — die
* Kurve berührt exakt `pts[0]`/`pts[n-1]` (Rand-Kontrollpunkte werden intern
* je 2× zusätzlich dupliziert, Multiplizität 3 an beiden Enden — der
* Standard-Trick für kubische B-Splines, ohne eine generische
* Knotenvektor-Auswertung/Cox-de-Boor-Rekursion zu brauchen). Geschlossen:
* periodisch (Kontrollpunkte laufen zyklisch um), die Kurve berührt dann
* KEINEN der Punkte. Mit weniger als 3 Punkten identisches
* Fallback-Verhalten wie `sampleCatmullRom` (Rohpunkte, Gummiband-Vorschau).
*/
export function sampleUniformBSpline(pts: Vec2[], closed: boolean, segsPerSpan = 16): Vec2[] {
if (pts.length < 3) return [...pts];
const out: Vec2[] = [];
if (closed) {
const n = pts.length;
const at = (i: number) => pts[((i % n) + n) % n];
for (let i = 0; i < n; i++) {
for (let s = 0; s < segsPerSpan; s++) {
out.push(bsplineSegPoint(at(i - 1), at(i), at(i + 1), at(i + 2), s / segsPerSpan));
}
}
return out;
}
const last = pts[pts.length - 1];
const padded = [pts[0], pts[0], ...pts, last, last];
const spans = padded.length - 3;
for (let i = 0; i < spans; i++) {
for (let s = 0; s < segsPerSpan; s++) {
out.push(bsplineSegPoint(padded[i], padded[i + 1], padded[i + 2], padded[i + 3], s / segsPerSpan));
}
}
out.push(last);
return out;
}