Kern/Huelle Phase A.1: Kern-Lecks behoben (View-/Detail-Typen nach model/viewTypes, Pick-Typen nach plan/pickTypes, geomTransform/curves/lineSegments/layoutModel in den Kern verschoben)
This commit is contained in:
1 parent
1ea0fab6ce
commit
cb79c13753
46 files changed
+373
-344
No files matched your search
@@ -0,0 +1,140 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { ellipsePts, sampleBezierChain, sampleCatmullRom, sampleUniformBSpline } 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);
|
||||
});
|
||||
});
|
||||
|
||||
describe("sampleUniformBSpline (Kontrollpunkt-/CV-Modus)", () => {
|
||||
it("weniger als 3 Punkte: Rohpunkte unverändert (Gummiband-Vorschau, wie sampleCatmullRom)", () => {
|
||||
const pts = [{ x: 0, y: 0 }, { x: 3, y: 4 }];
|
||||
expect(sampleUniformBSpline(pts, false, 8)).toEqual(pts);
|
||||
});
|
||||
|
||||
it("offen, geklemmt: berührt exakt Start- und Endpunkt", () => {
|
||||
const pts = [{ x: 0, y: 0 }, { x: 2, y: 5 }, { x: 6, y: 5 }, { x: 8, y: 0 }];
|
||||
const out = sampleUniformBSpline(pts, false, 10);
|
||||
expect(out[0]).toEqual(pts[0]);
|
||||
const last = out[out.length - 1];
|
||||
expect(last.x).toBeCloseTo(8, 9);
|
||||
expect(last.y).toBeCloseTo(0, 9);
|
||||
});
|
||||
|
||||
it("offen, geklemmt: berührt die INNEREN Kontrollpunkte NICHT (Gegensatz zu Catmull-Rom, das interpoliert)", () => {
|
||||
// Zwei Kontrollpunkte weit ausserhalb der Verbindungslinie Start-Ende —
|
||||
// eine interpolierende Kurve (Catmull-Rom) würde exakt durch sie
|
||||
// verlaufen; die B-Spline-Kurve wird nur "angezogen".
|
||||
const pts = [{ x: 0, y: 0 }, { x: 2, y: 10 }, { x: 6, y: 10 }, { x: 8, y: 0 }];
|
||||
const out = sampleUniformBSpline(pts, false, 20);
|
||||
const touchesInner = out.some(
|
||||
(p) => Math.hypot(p.x - 2, p.y - 10) < 1e-6 || Math.hypot(p.x - 6, p.y - 10) < 1e-6,
|
||||
);
|
||||
expect(touchesInner).toBe(false);
|
||||
// Bleibt aber innerhalb der konvexen Hülle der Kontrollpunkte (y ≤ 10).
|
||||
for (const p of out) expect(p.y).toBeLessThanOrEqual(10 + 1e-9);
|
||||
});
|
||||
|
||||
it("zwei effektive Kontrollpunkte (Start/Ende mehrfach dupliziert) ergeben eine echte Gerade", () => {
|
||||
// Mit 3 kollinearen Punkten bleibt die Kurve (als Kombination kollinearer
|
||||
// Kontrollpunkte) exakt auf der Verbindungsgeraden.
|
||||
const pts = [{ x: 0, y: 0 }, { x: 3, y: 0 }, { x: 6, y: 0 }];
|
||||
const out = sampleUniformBSpline(pts, false, 8);
|
||||
for (const p of out) expect(p.y).toBeCloseTo(0, 9);
|
||||
expect(out[0]).toEqual(pts[0]);
|
||||
expect(out[out.length - 1]).toEqual(pts[2]);
|
||||
});
|
||||
|
||||
it("geschlossen (periodisch): liefert keinen doppelten Schlusspunkt UND berührt keinen der Kontrollpunkte", () => {
|
||||
const pts = [{ x: 0, y: 0 }, { x: 4, y: 0 }, { x: 4, y: 4 }, { x: 0, y: 4 }];
|
||||
const out = sampleUniformBSpline(pts, true, 6);
|
||||
expect(out.length).toBe(pts.length * 6);
|
||||
for (const cp of pts) {
|
||||
expect(out.some((p) => Math.hypot(p.x - cp.x, p.y - cp.y) < 1e-6)).toBe(false);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,156 @@
|
||||
// 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;
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
// Punkt-Abbildung von 2D-Zeichengeometrie (Verschieben/Spiegeln/Drehen/Skalieren). Reine
|
||||
// Geometrie ohne UI — Quelle für Transformations-Befehle, Block-Rendering und den Plan-Generator.
|
||||
|
||||
import type { Drawing2DGeom, Vec2 } from "../model/types";
|
||||
|
||||
|
||||
/**
|
||||
* Erkennt, ob `fn` die Umlauf-Orientierung umkehrt (Spiegelung) — via
|
||||
* Flächenvorzeichen eines Test-Dreiecks unter `fn` (negativ ⇒ Spiegelung).
|
||||
* Funktioniert für JEDE Isometrie/Similarity (Move/Rotate/Scale erhalten die
|
||||
* Orientierung, Mirror kehrt sie um), ohne dass eine Operation ihre Spiegel-
|
||||
* Eigenschaft explizit markieren müsste.
|
||||
*/
|
||||
export function reversesOrientation(fn: (p: Vec2) => Vec2): boolean {
|
||||
const p0 = fn({ x: 0, y: 0 }), p1 = fn({ x: 1, y: 0 }), p2 = fn({ x: 0, y: 1 });
|
||||
return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y) < 0;
|
||||
}
|
||||
|
||||
/** Bildet jeden Punkt einer 2D-Zeichengeometrie unter `fn` ab (Move/Mirror/Rotate/Array + Block-Rendering). */
|
||||
/**
|
||||
* Wie {@link mvGeom}, zusätzlich für eine SKALIERUNG: `mvGeom` bildet nur
|
||||
* PUNKTE ab — Radien, Schrifthöhen und der Bemassungs-Abstand sind aber keine
|
||||
* Punkte und blieben sonst unverändert stehen (ein Kreis behielte unter einer
|
||||
* 2×-Skalierung seinen Radius). Diese Funktion zieht die betroffenen
|
||||
* Grössenfelder um denselben Faktor nach.
|
||||
*
|
||||
* Bewusst NICHT angefasst: `bulge` (ein Formverhältnis, skaleninvariant) und
|
||||
* die Papier-mm-Felder von Bemassung/Linienenden — die beziehen sich auf das
|
||||
* Blatt, nicht aufs Modell, und dürfen beim Skalieren des Objekts gleich
|
||||
* bleiben.
|
||||
*/
|
||||
/** Skalierungsfaktor eines `op:"scale"`-Punkte-Tripels bei Interpolation `t`. */
|
||||
export function scaleFactor(pts: Vec2[], t: number): number {
|
||||
const c = pts[0];
|
||||
const ref = Math.hypot(pts[1].x - c.x, pts[1].y - c.y) || 1;
|
||||
const tgt = Math.hypot(pts[2].x - c.x, pts[2].y - c.y);
|
||||
return 1 + (tgt / ref - 1) * t;
|
||||
}
|
||||
|
||||
export const scaleGeom = (geom: Drawing2DGeom, fn: (p: Vec2) => Vec2, f: number): Drawing2DGeom => {
|
||||
const g = mvGeom(geom, fn);
|
||||
switch (g.shape) {
|
||||
case "circle":
|
||||
return { ...g, r: g.r * f };
|
||||
case "arc":
|
||||
return { ...g, r: g.r * f };
|
||||
case "ellipse":
|
||||
return { ...g, rx: g.rx * f, ry: g.ry * f };
|
||||
case "dimension":
|
||||
// `offset` ist der Normalabstand der Masslinie in Modellmass.
|
||||
return { ...g, offset: g.offset * f };
|
||||
case "text":
|
||||
return {
|
||||
...g,
|
||||
height: g.height * f,
|
||||
...(g.width !== undefined ? { width: g.width * f } : {}),
|
||||
};
|
||||
case "scalebar":
|
||||
// Segmentlänge ist eine ECHTE Modell-Distanz (kein Papier-mm-Feld wie
|
||||
// bei Kote/Bemassung) — muss bei einer Modell-Skalierung mitwachsen,
|
||||
// sonst zeigt der Balken nach dem Skalieren die FALSCHE Distanz an.
|
||||
return { ...g, segmentLength: g.segmentLength * f };
|
||||
case "block":
|
||||
// Blockinstanz-Skalierung (s. Typ-Kommentar in model/types.ts) —
|
||||
// ohne diesen Fall wäre der `scale`-Befehl auf eine Blockinstanz ein
|
||||
// kompletter No-Op (kein Feld zum Multiplizieren).
|
||||
return { ...g, scale: (g.scale ?? 1) * f };
|
||||
default:
|
||||
return g;
|
||||
}
|
||||
};
|
||||
|
||||
export const mvGeom = (geom: Drawing2DGeom, fn: (p: Vec2) => Vec2): Drawing2DGeom => {
|
||||
switch (geom.shape) {
|
||||
case "line":
|
||||
case "xline":
|
||||
case "ray":
|
||||
return { ...geom, a: fn(geom.a), b: fn(geom.b) };
|
||||
case "polyline": {
|
||||
const pts = geom.pts.map(fn);
|
||||
if (!geom.bulge) return { ...geom, pts };
|
||||
// Bulge-Vorzeichen ist rein relativ zur Zeichenrichtung definiert (links
|
||||
// von a→b) — eine Spiegelung kehrt die Umlauf-Orientierung um und muss
|
||||
// daher auch jeden Bogen umkehren, sonst wandert er auf die falsche Seite.
|
||||
const bulge = reversesOrientation(fn) ? geom.bulge.map((b) => -b) : geom.bulge;
|
||||
return { ...geom, pts, bulge };
|
||||
}
|
||||
case "rect": {
|
||||
// Achsparallel nur bei reiner Verschiebung erhaltbar; sonst → Polylinie.
|
||||
const c = [
|
||||
geom.min,
|
||||
{ x: geom.max.x, y: geom.min.y },
|
||||
geom.max,
|
||||
{ x: geom.min.x, y: geom.max.y },
|
||||
].map(fn);
|
||||
const axisAligned =
|
||||
Math.abs(c[0].y - c[1].y) < 1e-6 &&
|
||||
Math.abs(c[1].x - c[2].x) < 1e-6 &&
|
||||
Math.abs(c[2].y - c[3].y) < 1e-6;
|
||||
if (axisAligned) {
|
||||
const xs = c.map((p) => p.x), ys = c.map((p) => p.y);
|
||||
return {
|
||||
shape: "rect",
|
||||
min: { x: Math.min(...xs), y: Math.min(...ys) },
|
||||
max: { x: Math.max(...xs), y: Math.max(...ys) },
|
||||
};
|
||||
}
|
||||
return { shape: "polyline", pts: c, closed: true };
|
||||
}
|
||||
case "circle":
|
||||
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 "dimension":
|
||||
return { ...geom, pts: geom.pts.map(fn) };
|
||||
case "kote":
|
||||
return { ...geom, at: fn(geom.at) };
|
||||
case "detailref":
|
||||
return { ...geom, at: fn(geom.at) };
|
||||
case "scalebar":
|
||||
return { ...geom, at: fn(geom.at) };
|
||||
case "pointmarker":
|
||||
return { ...geom, at: fn(geom.at) };
|
||||
case "text":
|
||||
// Leader-Ziel bzw. Pfadpunkte (Text-entlang-Pfad) müssen mitwandern,
|
||||
// sonst zeigt der Pfeil/Pfad nach einem Move/Mirror/Rotate/Array auf die
|
||||
// alte Position.
|
||||
return {
|
||||
...geom,
|
||||
at: fn(geom.at),
|
||||
...(geom.leaderTarget ? { leaderTarget: fn(geom.leaderTarget) } : {}),
|
||||
...(geom.leaderBendPoints ? { leaderBendPoints: geom.leaderBendPoints.map(fn) } : {}),
|
||||
...(geom.pathPts ? { pathPts: geom.pathPts.map(fn) } : {}),
|
||||
};
|
||||
case "block": {
|
||||
// Wie Column: Winkel aus einem transformierten Richtungspunkt zurück-
|
||||
// rechnen, damit Rotate/Mirror auch die Instanz-Ausrichtung mitnehmen
|
||||
// (nicht nur den Einfügepunkt) — reine Translation liesse die Symbole
|
||||
// beim Drehen der Auswahl in der alten Orientierung stehen.
|
||||
const tip = { x: geom.at.x + Math.cos(geom.rotation), y: geom.at.y + Math.sin(geom.rotation) };
|
||||
const nb = fn(geom.at);
|
||||
const nt = fn(tip);
|
||||
return { ...geom, at: nb, rotation: Math.atan2(nt.y - nb.y, nt.x - nb.x) };
|
||||
}
|
||||
case "image": {
|
||||
// Wie Text: nur die Position folgt der Transformation (Zentrum wandert
|
||||
// dorthin, wo `fn` es abbildet); die Box bleibt achsparallel/unverzerrt —
|
||||
// ein Bild kann nicht geschert/rotiert dargestellt werden (kein `angle`-Feld).
|
||||
const oldCenter = { x: (geom.min.x + geom.max.x) / 2, y: (geom.min.y + geom.max.y) / 2 };
|
||||
const newCenter = fn(oldCenter);
|
||||
const dx = newCenter.x - oldCenter.x;
|
||||
const dy = newCenter.y - oldCenter.y;
|
||||
return {
|
||||
...geom,
|
||||
min: { x: geom.min.x + dx, y: geom.min.y + dy },
|
||||
max: { x: geom.max.x + dx, y: geom.max.y + dy },
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,111 @@
|
||||
// Modulares Linien-Segment-System: Abbildung Segmentliste ↔ dash-Array,
|
||||
// Punkt-Erkennung (Round-Cap) und die Schnell-Presets.
|
||||
|
||||
import { describe, it, expect } from "vitest";
|
||||
import {
|
||||
segmentsToDash,
|
||||
dashToSegments,
|
||||
dashHasDot,
|
||||
presetOfDash,
|
||||
LINE_PRESETS,
|
||||
type LineSegment,
|
||||
} from "./lineSegments";
|
||||
|
||||
describe("segmentsToDash", () => {
|
||||
it("bildet eine alternierende Strich/Lücke-Folge direkt ab", () => {
|
||||
const segs: LineSegment[] = [
|
||||
{ type: "dash", length: 2 },
|
||||
{ type: "gap", length: 1 },
|
||||
{ type: "dash", length: 3 },
|
||||
{ type: "gap", length: 2 },
|
||||
];
|
||||
expect(segmentsToDash(segs)).toEqual([2, 1, 3, 2]);
|
||||
});
|
||||
|
||||
it("bildet einen Punkt als 0-Länge ab (Strich-Punkt)", () => {
|
||||
const segs: LineSegment[] = [
|
||||
{ type: "dash", length: 4 },
|
||||
{ type: "gap", length: 2 },
|
||||
{ type: "dot", length: 0 },
|
||||
{ type: "gap", length: 2 },
|
||||
];
|
||||
expect(segmentsToDash(segs)).toEqual([4, 2, 0, 2]);
|
||||
});
|
||||
|
||||
it("fügt eine 0-Lücke zwischen zwei AN-Segmenten ein (Strich·Punkt)", () => {
|
||||
const segs: LineSegment[] = [
|
||||
{ type: "dash", length: 3 },
|
||||
{ type: "dot", length: 0 },
|
||||
];
|
||||
// 3 (Strich), 0 (eingefügte Lücke), 0 (Punkt), 0 (Schluss-Lücke, gerade Länge)
|
||||
expect(segmentsToDash(segs)).toEqual([3, 0, 0, 0]);
|
||||
});
|
||||
|
||||
it("fasst zwei Lücken in Folge zusammen", () => {
|
||||
const segs: LineSegment[] = [
|
||||
{ type: "dash", length: 2 },
|
||||
{ type: "gap", length: 1 },
|
||||
{ type: "gap", length: 2 },
|
||||
];
|
||||
expect(segmentsToDash(segs)).toEqual([2, 3]);
|
||||
});
|
||||
|
||||
it("endet stets auf einer AUS-Länge (gerade Array-Länge)", () => {
|
||||
expect(segmentsToDash([{ type: "dash", length: 5 }])).toEqual([5, 0]);
|
||||
});
|
||||
|
||||
it("leere Folge ⇒ null (Volllinie)", () => {
|
||||
expect(segmentsToDash([])).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("dashToSegments", () => {
|
||||
it("kehrt die Abbildung um (0 ⇒ Punkt)", () => {
|
||||
expect(dashToSegments([4, 2, 0, 2])).toEqual([
|
||||
{ type: "dash", length: 4 },
|
||||
{ type: "gap", length: 2 },
|
||||
{ type: "dot", length: 0 },
|
||||
{ type: "gap", length: 2 },
|
||||
]);
|
||||
});
|
||||
|
||||
it("null ⇒ leere Folge", () => {
|
||||
expect(dashToSegments(null)).toEqual([]);
|
||||
});
|
||||
|
||||
it("round-trip: dash → segments → dash bleibt stabil", () => {
|
||||
for (const d of [[2, 1, 3, 2], [4, 2, 0, 2], [0, 2], [3, 2]]) {
|
||||
expect(segmentsToDash(dashToSegments(d))).toEqual(d);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("dashHasDot", () => {
|
||||
it("erkennt Punkte (0 im dash) für die runde Kappe", () => {
|
||||
expect(dashHasDot([0, 2])).toBe(true);
|
||||
expect(dashHasDot([4, 2, 0, 2])).toBe(true);
|
||||
});
|
||||
it("keine Punkte ⇒ false (Butt-Kappe bleibt)", () => {
|
||||
expect(dashHasDot([3, 2])).toBe(false);
|
||||
expect(dashHasDot(null)).toBe(false);
|
||||
expect(dashHasDot([])).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Presets", () => {
|
||||
it("die vier Grundtypen sind erkennbar", () => {
|
||||
expect(presetOfDash(LINE_PRESETS.solid)).toBe("solid");
|
||||
expect(presetOfDash(LINE_PRESETS.dash)).toBe("dash");
|
||||
expect(presetOfDash(LINE_PRESETS.dot)).toBe("dot");
|
||||
expect(presetOfDash(LINE_PRESETS.dashDot)).toBe("dashDot");
|
||||
});
|
||||
it("Punktlinie/Strich-Punkt enthalten einen Dot, Voll/Strich nicht", () => {
|
||||
expect(dashHasDot(LINE_PRESETS.dot)).toBe(true);
|
||||
expect(dashHasDot(LINE_PRESETS.dashDot)).toBe(true);
|
||||
expect(dashHasDot(LINE_PRESETS.solid)).toBe(false);
|
||||
expect(dashHasDot(LINE_PRESETS.dash)).toBe(false);
|
||||
});
|
||||
it("Abweichungen ⇒ custom", () => {
|
||||
expect(presetOfDash([1, 2, 3, 4])).toBe("custom");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,107 @@
|
||||
// Modulares Linien-Segment-System — die Brücke zwischen dem Segment-Editor
|
||||
// (ResourceManager) und der Datenbasis `LineStyle.dash` (alternierend AN/AUS in
|
||||
// mm, loopend). Eine musterbasierte Linie ist eine geordnete, loopende Folge aus
|
||||
// drei Segment-Arten:
|
||||
// • "dash" — ein sichtbarer Strich (Länge in mm > 0),
|
||||
// • "dot" — ein Punkt (AN-Segment der Länge 0; wird mit runder Strichkappe zum
|
||||
// Dot, siehe dashHasDot + `stroke-linecap: round` in den Renderern),
|
||||
// • "gap" — eine Lücke (AUS-Länge in mm).
|
||||
// Die letzte Lücke der Folge ist die „Schluss-Lücke" (Ende des Loops bis zum
|
||||
// Start des nächsten). Bezeichner englisch, Kommentare deutsch (CONVENTIONS.md).
|
||||
|
||||
/** Eine Segment-Art des Linien-Musters. */
|
||||
export type LineSegmentType = "dash" | "dot" | "gap";
|
||||
|
||||
/** Ein Segment der Muster-Folge (Länge in mm; bei „dot" ignoriert/0). */
|
||||
export interface LineSegment {
|
||||
type: LineSegmentType;
|
||||
/** Länge in mm (Papier). Bei „dot" stets 0. */
|
||||
length: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bildet eine geordnete Segment-Folge auf ein `dash`-Array ab
|
||||
* (`[on, off, on, off, …]` in mm; ein `on`-Wert von 0 ⇒ Punkt). Da SVG-Dash
|
||||
* strikt AN/AUS alterniert, werden 0-Längen eingefügt bzw. gleichartige
|
||||
* Nachbarn zusammengefasst, um die Alternation zu wahren:
|
||||
* • zwei AN-Segmente in Folge (z. B. Strich·Punkt) ⇒ 0-Lücke dazwischen,
|
||||
* • zwei Lücken in Folge ⇒ zur vorigen Lücke addiert.
|
||||
* Das Muster endet stets auf einer AUS-Länge (gerade Array-Länge), damit es
|
||||
* sauber loopt. Leere Folge ⇒ `null` (Volllinie).
|
||||
*/
|
||||
export function segmentsToDash(segments: LineSegment[]): number[] | null {
|
||||
const dash: number[] = [];
|
||||
for (const seg of segments) {
|
||||
const on = seg.type !== "gap";
|
||||
const value = seg.type === "dot" ? 0 : Math.max(0, seg.length);
|
||||
const nextSlotIsOn = dash.length % 2 === 0;
|
||||
if (on) {
|
||||
if (!nextSlotIsOn) dash.push(0); // 0-Lücke, um die AN/AUS-Alternation zu wahren
|
||||
dash.push(value);
|
||||
} else {
|
||||
if (nextSlotIsOn) {
|
||||
if (dash.length > 0) {
|
||||
// zwei Lücken in Folge → in die vorherige Lücke einrechnen
|
||||
dash[dash.length - 1] += value;
|
||||
continue;
|
||||
}
|
||||
dash.push(0); // führende Lücke: 0-Strich davor
|
||||
}
|
||||
dash.push(value);
|
||||
}
|
||||
}
|
||||
// Muster muss mit einer AUS-Länge enden (gerade Länge), damit es sauber loopt.
|
||||
if (dash.length % 2 === 1) dash.push(0);
|
||||
return dash.length ? dash : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Kehrt `segmentsToDash` um: ein `dash`-Array (`[on, off, …]`) wird zur
|
||||
* Segment-Folge. Gerade Indizes sind AN (0 ⇒ Punkt, sonst Strich), ungerade
|
||||
* Indizes sind Lücken. `null`/leer ⇒ leere Folge (Volllinie).
|
||||
*/
|
||||
export function dashToSegments(dash: number[] | null | undefined): LineSegment[] {
|
||||
if (!dash) return [];
|
||||
return dash.map((v, i) =>
|
||||
i % 2 === 0
|
||||
? { type: v === 0 ? "dot" : "dash", length: v }
|
||||
: { type: "gap", length: v },
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enthält das Strichmuster einen Punkt (ein AN-Segment der Länge 0)? Solche
|
||||
* Linien müssen mit runder Strichkappe (`stroke-linecap: round`) gezeichnet
|
||||
* werden, damit die 0-Längen-Segmente als Dots sichtbar werden.
|
||||
*/
|
||||
export function dashHasDot(dash: number[] | null | undefined): boolean {
|
||||
return !!dash && dash.some((v) => v === 0);
|
||||
}
|
||||
|
||||
/** Schnell-Presets für die drei Grundtypen (+ Strich-Punkt), Längen in mm. */
|
||||
export const LINE_PRESETS = {
|
||||
/** Volllinie — durchgezogen. */
|
||||
solid: null as number[] | null,
|
||||
/** Strichlinie. */
|
||||
dash: [3, 2] as number[] | null,
|
||||
/** Punktlinie (Dots). */
|
||||
dot: [0, 2] as number[] | null,
|
||||
/** Strich-Punkt-Linie. */
|
||||
dashDot: [4, 2, 0, 2] as number[] | null,
|
||||
} as const;
|
||||
|
||||
export type LinePresetKey = keyof typeof LINE_PRESETS | "custom";
|
||||
|
||||
/** Erkennt, welchem Preset ein `dash`-Array entspricht (sonst „custom"). */
|
||||
export function presetOfDash(dash: number[] | null | undefined): LinePresetKey {
|
||||
const eq = (a: number[] | null, b: number[] | null): boolean => {
|
||||
if (a === null || b === null) return a === b;
|
||||
return a.length === b.length && a.every((v, i) => v === b[i]);
|
||||
};
|
||||
const d = dash ?? null;
|
||||
if (eq(d, LINE_PRESETS.solid)) return "solid";
|
||||
if (eq(d, LINE_PRESETS.dash)) return "dash";
|
||||
if (eq(d, LINE_PRESETS.dot)) return "dot";
|
||||
if (eq(d, LINE_PRESETS.dashDot)) return "dashDot";
|
||||
return "custom";
|
||||
}
|
||||
Reference in new issue
Block a user