[153] Strichmuster: Vollkreise schliessen an der Naht (Muster auf ganze Wiederholungen gestreckt) in Plan-/Druck-SVG, Bildschirm-Szene, DXF/DWG und React-SVG; Phase für Linien/Bögen in der React-SVG-Ansicht

This commit is contained in:
Claude committed 2026-11-11 13:57:49 +00:00
1 parent 649eddfb56
commit b9202f7da3
13 files changed
+286 -22

No files matched your search

+1 -1
View File
@@ -204,7 +204,7 @@ export {
viewForScale,
visibleEdgeRuns,
} from "../plan/planView/geometry";
export { PLAN_DASH_VAR, dashArrayCss, fitDashToPeriod, hatchDashVb, lineDashVb } from "../plan/planView/dashVb";
export { PLAN_DASH_VAR, dashArrayCss, dashOffsetCss, fitDashToPeriod, hatchDashVb, lineDashOffsetVb, lineDashVb, ringDashVb } from "../plan/planView/dashVb";
export {
type ClientMap,
+15
View File
@@ -84,4 +84,19 @@ describe("Druck-SVG: Kreise und Bögen", () => {
const arc = paths.find((p) => p.attrs.has("stroke-dasharray"))!;
expect(arc.attrs.get("stroke-dashoffset")).toBeDefined();
});
it("Vollkreis: Muster auf ganze Wiederholungen über den Umfang, ohne Phase", () => {
for (const n of [50, 100, 200]) {
const r = (7.3 * n) / 1000; // 7.3 mm Radius auf dem Papier
const pts: [number, number][] = [];
for (let i = 0; i <= 96; i++) pts.push([r * Math.cos((2 * Math.PI * i) / 96), r * Math.sin((2 * Math.PI * i) / 96)]);
const sc = { fills: [], outlines: [], polylines: [{ pts, color: [0, 0, 0, 1], widthMm: 0.18, dash: [2, 1], z: 0, widthScreen: false }], arcs: [], lines: [], texts: [], images: [], bounds: { minX: -r, minY: -r, maxX: r, maxY: r } } as unknown as RScene;
const svg = sceneToPrintSvg(sc, { scaleDenominator: n, pageWidthMm: 100, pageHeightMm: 100 }).svg as unknown as FakeEl;
const el = svg.children.find((c) => c.tag !== "defs" && c.attrs.has("stroke-dasharray"))!;
const d = el.attrs.get("stroke-dasharray")!.split(" ").map(Number);
const reps = (2 * Math.PI * 7.3) / (d[0] + d[1]);
expect(Math.abs(reps - Math.round(reps))).toBeLessThan(0.02);
expect(el.attrs.has("stroke-dashoffset")).toBe(false);
}
});
});
+22 -3
View File
@@ -301,14 +301,33 @@ function appendStroke(
if (dash && dash.length) {
// Strichmuster ist bereits in Papier-mm definiert (siehe `toRenderScene.ts`:
// „unveraendert durchgereicht"), also direkt uebernehmen.
el.setAttribute("stroke-dasharray", dash.map((d) => round(d)).join(" "));
const mm = pts.map((p) => map(p));
// [153] Vollkreis (Zug, dessen Ende am Anfang liegt): Muster auf ganze Wiederholungen gestreckt, die Naht
// schliesst; keine Phase noetig.
const ring = closed || isRing(mm);
const useDash = !closed && ring ? ringDash(mm, dash) : dash;
el.setAttribute("stroke-dasharray", useDash.map((d) => round(d)).join(" "));
// Punkte (Strich 0) brauchen runde Enden, sonst unsichtbar ([96]).
if (dash.some((d) => d === 0)) el.setAttribute("stroke-linecap", "round");
setDashOffset(el, pts.map((p) => map(p)), closed, dash);
if (useDash.some((d) => d === 0)) el.setAttribute("stroke-linecap", "round");
if (closed || !ring) setDashOffset(el, mm, closed, dash);
}
svg.appendChild(el);
}
/** Offener Zug mit Ende auf dem Anfang (tesselierter Vollkreis der Szene, mindestens 8 Punkte). */
function isRing(ptsMm: [number, number][]): boolean {
const n = ptsMm.length;
if (n < 8) return false;
return Math.hypot(ptsMm[n - 1][0] - ptsMm[0][0], ptsMm[n - 1][1] - ptsMm[0][1]) < 1e-6;
}
/** [153] Muster des Rings auf ganze Wiederholungen über seine Länge gestreckt (Kern `plan::line_pattern::fit_closed`). */
function ringDash(ptsMm: [number, number][], dash: number[]): number[] {
let len = 0;
for (let i = 0; i + 1 < ptsMm.length; i++) len += Math.hypot(ptsMm[i + 1][0] - ptsMm[i][0], ptsMm[i + 1][1] - ptsMm[i][1]);
return Array.from(core().line_dash_fit_closed(len, Float64Array.from(dash)));
}
/**
* [153] Musterphase eines gestrichelten Zugs (Papier-mm-Punkte): Kern `plan::line_pattern::phase`,
* damit beide Enden möglichst auf einem Strich liegen — gleich wie Plan-/Druck-SVG und Bildschirm.
+12 -1
View File
@@ -1,5 +1,5 @@
import { describe, it, expect } from "vitest";
import { dashArrayCss, fitDashToPeriod, hatchDashVb, lineDashVb } from "./dashVb";
import { dashArrayCss, dashOffsetCss, fitDashToPeriod, hatchDashVb, lineDashOffsetVb, lineDashVb, ringDashVb } from "./dashVb";
import { PX_PER_M } from "./geometry";
describe("Strichmuster im Modell-Raum (SVG-Pfad)", () => {
@@ -27,4 +27,15 @@ describe("Strichmuster im Modell-Raum (SVG-Pfad)", () => {
expect(f[0] + f[1]).toBeCloseTo(8 / 2, 9);
expect(fitDashToPeriod([], 8)).toEqual([]);
});
it("[153] Phase in viewBox-Einheiten = Kern-Phase in Papier-mm, Ring mit ganzen Wiederholungen", () => {
// 10 mm lang bei 1:100 mit [2, 1]: Phase 0.5 mm (Kern `phase`).
const k = (100 / 1000) * PX_PER_M;
expect(lineDashOffsetVb([2, 1], 100, 10 * k)).toBeCloseTo(0.5 * k, 6);
expect(lineDashOffsetVb([2, 1], 100, 0)).toBe(0);
expect(dashOffsetCss(0, true)).toBeUndefined();
expect(dashOffsetCss(3, false)).toBe("3");
const ring = ringDashVb([2, 1], 100, 31.4 * k);
expect((ring[0] + ring[1]) * 10).toBeCloseTo(31.4 * k, 6);
});
});
+35
View File
@@ -5,6 +5,7 @@
import { PX_PER_M } from "./geometry";
import { DASH_MM_TO_M } from "../glPlan/glPlanHatch";
import { core } from "../../engine/coreWasm";
/** CSS-Variable am Plan-SVG: CSS-px je viewBox-Einheit (meet), für Muster unter `non-scaling-stroke`. */
export const PLAN_DASH_VAR = "--plan-dash-k";
@@ -44,3 +45,37 @@ export function dashArrayCss(vb: readonly number[], nonScaling: boolean): string
const r = (v: number) => Math.round(v * 1e4) / 1e4;
return vb.map((v) => (nonScaling ? `calc(var(${PLAN_DASH_VAR}, 1) * ${r(v)}px)` : `${r(v)}`)).join(" ");
}
/** viewBox-Einheiten je Papier-mm im Plot-Massstab 1:`plotN` (ohne gültiges N: 1:100). */
function vbPerMm(plotN: number): number {
const n = Number.isFinite(plotN) && plotN > 0 ? plotN : 100;
return (n / 1000) * PX_PER_M;
}
/**
* [153] Musterphase (viewBox-Einheiten) eines gestrichelten Zugs der Länge `lenVb`: Kern
* `plan::line_pattern::phase`, beide Enden möglichst auf einem Strich — gleich wie Plan-/Druck-SVG und Bildschirm.
*/
export function lineDashOffsetVb(dash: readonly number[], plotN: number, lenVb: number): number {
if (!dash.length || !(lenVb > 0) || !Number.isFinite(lenVb)) return 0;
const k = vbPerMm(plotN);
const ph = core().line_dash_phase(lenVb / k, Float64Array.from(dash));
return Number.isFinite(ph) ? ph * k : 0;
}
/**
* [153] Muster (viewBox-Einheiten) eines Rings der Länge `lenVb` (Vollkreis als Zug, Ende = Anfang) auf ganze
* Wiederholungen gestreckt: die Naht schliesst (Kern `fit_closed`).
*/
export function ringDashVb(dash: readonly number[], plotN: number, lenVb: number): number[] {
if (!dash.length || !(lenVb > 0) || !Number.isFinite(lenVb)) return lineDashVb(dash, plotN);
const k = vbPerMm(plotN);
return lineDashVb(Array.from(core().line_dash_fit_closed(lenVb / k, Float64Array.from(dash))), plotN);
}
/** `stroke-dashoffset` als CSS-Wert (wie [`dashArrayCss`]); `undefined` ohne Phase. */
export function dashOffsetCss(vb: number, nonScaling: boolean): string | undefined {
if (!(vb > 1e-9)) return undefined;
const r = Math.round(vb * 1e4) / 1e4;
return nonScaling ? `calc(var(${PLAN_DASH_VAR}, 1) * ${r}px)` : `${r}`;
}
+35 -10
View File
@@ -13,7 +13,7 @@ import { quantizePen } from "../../../export/sceneToPrintSvg";
import { buildHatchRuns, buildRandomHatchRuns, zigzagPoints, motifPoints, DASH_MM_TO_M } from "../../../core";
import { dashHasDot } from "../../../core";
import { HAIRLINE_PX, PX_PER_M, mmToPx, printStrokeVb, visibleEdgeRuns } from "../../../core";
import { dashArrayCss, fitDashToPeriod, hatchDashVb, lineDashVb } from "../../../core";
import { dashArrayCss, dashOffsetCss, fitDashToPeriod, hatchDashVb, lineDashOffsetVb, lineDashVb, ringDashVb } from "../../../core";
/** Basis-Kachelmaß (viewBox-Einheiten) einer Bild-Schraffur bei scaleX/scaleY = 1. */
const IMG_TILE_VB = 40;
@@ -400,7 +400,12 @@ export function DrawingRunShape({
: mmToPx(run.weightMm);
const vfx = print ? undefined : ("non-scaling-stroke" as const);
// Linientyp in Papier-mm im Plot-Massstab: fest im Modell-Raum (Bildschirm = Druckansicht).
const dashStr = run.dash && run.dash.length ? dashArrayCss(lineDashVb(run.dash, paperScale ?? 100), !print) : undefined;
// Musterphase ([153]): Länge des Zugs in viewBox-Einheiten; Vollkreis (Ende = Anfang) mit ganzen Wiederholungen.
const runLen = (v: { x: number; y: number }[]): number => {
let l = 0;
for (let i = 0; i + 1 < v.length + (run.closed ? 1 : 0); i++) l += Math.hypot(v[(i + 1) % v.length].x - v[i].x, v[(i + 1) % v.length].y - v[i].y);
return l;
};
// Zickzack-/Custom-Motiv-Lauf: im MODELL-Raum tessellieren (Papier-mm →
// Modell-Meter via DASH_MM_TO_M), dann toScreen. Sonst die Roh-Stützpunkte.
const modelPts = run.zigzag
@@ -413,7 +418,17 @@ export function DrawingRunShape({
: run.motif
? motifPoints(run.pts[0], run.pts[run.pts.length - 1], run.motif, DASH_MM_TO_M)
: run.pts;
const pts = modelPts.map(toScreen).map((s) => `${s.x},${s.y}`).join(" ");
const scrPts = modelPts.map(toScreen);
const pts = scrPts.map((s) => `${s.x},${s.y}`).join(" ");
const plotN = paperScale ?? 100;
const ring =
!run.closed && scrPts.length >= 8 && Math.hypot(scrPts[scrPts.length - 1].x - scrPts[0].x, scrPts[scrPts.length - 1].y - scrPts[0].y) < 1e-6;
const dashStr =
run.dash && run.dash.length
? dashArrayCss(ring ? ringDashVb(run.dash, plotN, runLen(scrPts)) : lineDashVb(run.dash, plotN), !print)
: undefined;
const dashOff = run.dash && run.dash.length && !ring ? dashOffsetCss(lineDashOffsetVb(run.dash, plotN, runLen(scrPts)), !print) : undefined;
const dashSt = dashStr ? { strokeDasharray: dashStr, ...(dashOff ? { strokeDashoffset: dashOff } : {}) } : undefined;
// Punktmuster (dash enthält eine 0): runde Kappe, damit die 0-Längen-Segmente
// als Dots erscheinen (Punktlinie / Strich-Punkt).
const cap = dashHasDot(run.dash) ? ("round" as const) : undefined;
@@ -425,7 +440,7 @@ export function DrawingRunShape({
fill="none"
stroke={stroke}
strokeWidth={sw}
style={dashStr ? { strokeDasharray: dashStr } : undefined}
style={dashSt}
strokeLinecap={cap}
vectorEffect={vfx}
/>
@@ -436,7 +451,7 @@ export function DrawingRunShape({
fill="none"
stroke={stroke}
strokeWidth={sw}
style={dashStr ? { strokeDasharray: dashStr } : undefined}
style={dashSt}
strokeLinecap={cap}
vectorEffect={vfx}
/>
@@ -512,9 +527,18 @@ export function renderPrimitive(
// sonst wie bisher in Bildschirm-px (non-scaling).
// Linientypen in Papier-mm im Plot-Massstab: fest im Modell-Raum, auch unter non-scaling-stroke
// (über die meet-Variable am SVG) — der Rhythmus gleicht bei jedem Zoom der Druckansicht.
const dashOf = (dash: number[] | null | undefined): { strokeDasharray: string } | undefined => {
const dashOf = (dash: number[] | null | undefined, lenVb?: number): { strokeDasharray: string; strokeDashoffset?: string } | undefined => {
if (!dash || !dash.length) return undefined;
const css = dashArrayCss(lineDashVb(dash, paperScale ?? 100), !print);
if (!css) return undefined;
// [153] Musterphase des Zugs (Linie, Bogen): beide Enden möglichst auf einem Strich.
const off = lenVb ? dashOffsetCss(lineDashOffsetVb(dash, paperScale ?? 100, lenVb), !print) : undefined;
return { strokeDasharray: css, ...(off ? { strokeDashoffset: off } : {}) };
};
// [153] Vollkreis: Muster auf ganze Wiederholungen über den Umfang gestreckt (Naht schliesst), ohne Phase.
const ringStyle = (dash: number[] | null | undefined, lenVb: number): { strokeDasharray: string } | undefined => {
if (!dash || !dash.length) return undefined;
const css = dashArrayCss(ringDashVb(dash, paperScale ?? 100, lenVb), !print);
return css ? { strokeDasharray: css } : undefined;
};
switch (p.kind) {
@@ -673,7 +697,7 @@ export function renderPrimitive(
className={p.cls}
stroke={p.color}
strokeWidth={weight(p.weightMm)}
style={dashOf(p.dash)}
style={dashOf(p.dash, Math.hypot(b.x - a.x, b.y - a.y))}
strokeLinecap={dashHasDot(p.dash) ? "round" : undefined}
vectorEffect={vfx}
/>
@@ -696,7 +720,7 @@ export function renderPrimitive(
d={d}
className={p.cls}
strokeWidth={weight(p.weightMm)}
style={dashOf(p.dash)}
style={dashOf(p.dash, r * Math.min(Math.PI, Math.abs(Math.atan2(cross, v1.x * v2.x + v1.y * v2.y))))}
strokeLinecap={dashHasDot(p.dash) ? "round" : undefined}
vectorEffect={vfx}
/>
@@ -895,7 +919,8 @@ export function renderPrimitive(
fill={p.fill}
stroke={p.stroke}
strokeWidth={weight(p.weightMm)}
style={dashOf(p.dash)}
style={ringStyle(p.dash, 2 * Math.PI * r)}
strokeLinecap={dashHasDot(p.dash) ? "round" : undefined}
vectorEffect={vfx}
/>
);
@@ -923,7 +948,7 @@ export function renderPrimitive(
fill="none"
stroke={p.stroke}
strokeWidth={weight(p.weightMm)}
style={dashOf(p.dash)}
style={dashOf(p.dash, rPx * sweep)}
strokeLinecap={dashHasDot(p.dash) ? "round" : undefined}
vectorEffect={vfx}
/>