[K3d] Plan-Puffer v2: Format, Schreiber/Leser in Rust und TS, GPU-Kompilierung liest v2

Format im Kopf von plan_buf.rs (Gegenstück src/plan/planBuf.ts): Bytes mit f64-Teil und
UTF-16-Texten, Kopf mit Magie PLB2/Version 2, Tabellen für Strings und Schraffuren (JSON für TS +
numerischer Hatch-Datensatz), Gruppen, Offsets, Inhalts-Hashes, Einträge mit Pick-Art/-Id und
Punkt-Pool, voller Primitive-Umfang (Texte/Bild als JSON). v1 und TS-parseColor gelöscht
(Farben parst plan_buf::parse_color bitgleich). GPU-Kompilierung liest Bereiche direkt
(gpu_compile_flat/gpu_compile_held, plan_buf_hold); gemischte Listen kodiert TS schlank.

Claude-Session: https://claude.ai/code/session_01NTYBZBvLYCtugMJoKXaB67
This commit is contained in:
Claude committed 2026-10-28 21:57:01 +00:00
1 parent df468b987e
commit 54a7d270ea
8 files changed
+2391 -681

No files matched your search

+27 -10
View File
@@ -779,9 +779,10 @@ fn draw_circle_like(
c.stroke_polyline(&pts, fill.is_some(), stroke_col, stroke_mm, false);
}
/// Flache Ausgabe (s. Modulkopf); leer bei unlesbarem Plan-Puffer.
pub fn compile_flat(buf: &[f64], px_per_meter: f64, packed: bool) -> Vec<u32> {
let Some(plan) = plan_buf::decode(buf) else { return Vec::new() };
/// Flache Ausgabe (s. Modulkopf) für die Primitive `[from, to)` eines Plan-Puffers (v2); leer bei
/// unlesbarem Puffer oder ungültigem Bereich.
pub fn compile_flat(bytes: &[u8], from: usize, to: usize, px_per_meter: f64, packed: bool) -> Vec<u32> {
let Some(plan) = plan_buf::View::parse(bytes).and_then(|v| v.gpu(from, to)) else { return Vec::new() };
let o = compile(&plan, px_per_meter, packed);
let mut meta: Vec<f64> = vec![
o.origin.0,
@@ -843,7 +844,6 @@ mod tests {
weight_mm: 0.18,
a: v2(ax, 0.0),
b: v2(bx, 0.0),
dash: vec![],
chain,
zigzag: None,
motif: None,
@@ -926,8 +926,8 @@ mod tests {
#[test]
fn kreis_und_bogen() {
let p = plan(vec![
Prim::Circle { style: st(), fill: 1, stroke: 0, weight_mm: 0.0, center: v2(1.0, 1.0), r: 1.0, dash: vec![] },
Prim::DrawingArc { style: st(), stroke: 0, weight_mm: 0.5, center: v2(0.0, 0.0), r: 1.0, a0: 0.0, a1: 0.0, dash: vec![] },
Prim::Circle { style: st(), fill: 1, stroke: 0, weight_mm: 0.0, center: v2(1.0, 1.0), r: 1.0 },
Prim::DrawingArc { style: st(), stroke: 0, weight_mm: 0.5, center: v2(0.0, 0.0), r: 1.0, a0: 0.0, a1: 0.0 },
Prim::Placeholder,
]);
let o = compile(&p, 0.0, false);
@@ -938,9 +938,26 @@ mod tests {
#[test]
fn kaputter_puffer_leer() {
assert!(compile_flat(&[1.0, 2.0, 3.0], 90.0, true).is_empty());
let enc = plan_buf::encode(&plan(vec![line(0.0, f64::NAN, false)]));
let out = compile_flat(&enc, 90.0, true);
assert!(!out.is_empty());
assert!(compile_flat(&[1, 2, 3], 0, 0, 90.0, true).is_empty());
let mut w = plan_buf::Writer::new(false);
w.push(&crate::plan::prim::Primitive::Line {
a: crate::model::Vec2 { x: 0.0, y: 0.0 },
b: crate::model::Vec2 { x: f64::NAN, y: 0.0 },
cls: "c".into(),
weight_mm: 0.18,
dash: None,
zigzag: None,
motif: None,
color: None,
drawing_id: None,
opening_id: None,
stair_id: None,
greyed: None,
opacity: None,
});
let enc = w.finish();
assert!(!compile_flat(&enc, 0, 1, 90.0, true).is_empty());
assert!(compile_flat(&enc, 0, 2, 90.0, true).is_empty(), "Bereich ausserhalb");
assert!(!compile_flat(&enc, 1, 1, 90.0, true).is_empty(), "leerer Bereich = leere Geometrie");
}
}
+81 -7
View File
@@ -3,10 +3,10 @@
//! Zeichen für Zeichen die der TS-Objekte (camelCase, fehlende Felder fehlen).
use crate::model::*;
use serde::Serialize;
use serde::{Deserialize, Deserializer, Serialize};
/// Aufgelöste Schraffur eines Polygons (`HatchRender`).
#[derive(Debug, Clone, PartialEq, Serialize)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HatchRender {
pub pattern: HatchPattern,
@@ -38,7 +38,7 @@ pub struct HatchRender {
}
/// Zeile unter dem Raum-Stempel.
#[derive(Debug, Clone, PartialEq, Serialize)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StampLine {
pub text: String,
pub align: Align,
@@ -47,7 +47,12 @@ pub struct StampLine {
/// `dash?: number[] | null`: fehlt (`None`), `null` (`Some(None)`) oder Muster.
pub type Dash = Option<Option<Vec<f64>>>;
#[derive(Debug, Clone, PartialEq, Serialize)]
/// Liest `dash` so, dass `null` (`Some(None)`) und fehlend (`None`, über `default`) verschieden bleiben.
fn de_dash<'de, D: Deserializer<'de>>(d: D) -> Result<Dash, D::Error> {
Option::<Vec<f64>>::deserialize(d).map(Some)
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind")]
pub enum Primitive {
#[serde(rename = "polygon", rename_all = "camelCase")]
@@ -92,7 +97,7 @@ pub enum Primitive {
b: Vec2,
cls: String,
weight_mm: f64,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "de_dash")]
dash: Dash,
#[serde(skip_serializing_if = "Option::is_none")]
zigzag: Option<LineStyleZigzag>,
@@ -119,7 +124,7 @@ pub enum Primitive {
r: f64,
cls: String,
weight_mm: f64,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "de_dash")]
dash: Dash,
#[serde(skip_serializing_if = "Option::is_none")]
opening_id: Option<String>,
@@ -164,13 +169,82 @@ pub enum Primitive {
#[serde(skip_serializing_if = "Option::is_none")]
opacity: Option<f64>,
},
/// Text entlang eines Pfads (2D-Zeichnung); erzeugt heute nur TS, s. Plan-Puffer v2.
#[serde(rename = "drawingTextPath", rename_all = "camelCase")]
DrawingTextPath {
path_pts: Vec<Vec2>,
text: String,
height_m: f64,
color: String,
#[serde(skip_serializing_if = "Option::is_none")]
drawing_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
greyed: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
opacity: Option<f64>,
},
/// Eingebettetes Rasterbild (2D-Zeichnung).
#[serde(rename = "drawingImage", rename_all = "camelCase")]
DrawingImage {
min: Vec2,
max: Vec2,
src: String,
opacity: f64,
#[serde(skip_serializing_if = "Option::is_none")]
drawing_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
greyed: Option<bool>,
},
/// Glatter Kreis (2D-Zeichnung).
#[serde(rename = "drawingCircle", rename_all = "camelCase")]
DrawingCircle {
center: Vec2,
r: f64,
fill: String,
stroke: String,
weight_mm: f64,
#[serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "de_dash")]
dash: Dash,
#[serde(skip_serializing_if = "Option::is_none")]
drawing_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
greyed: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
opacity: Option<f64>,
},
/// Glatter Bogen (2D-Zeichnung), Winkel im Bogenmass (CCW).
#[serde(rename = "drawingArc", rename_all = "camelCase")]
DrawingArc {
center: Vec2,
r: f64,
a0: f64,
a1: f64,
stroke: String,
weight_mm: f64,
#[serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "de_dash")]
dash: Dash,
#[serde(skip_serializing_if = "Option::is_none")]
drawing_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
greyed: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
opacity: Option<f64>,
},
}
impl Primitive {
/// Setzt `greyed` (für Gruppen, deren Darstellungsmodus gedimmt ist).
pub fn with_greyed(mut self, g: bool) -> Self {
match &mut self {
Primitive::Polygon { greyed, .. } | Primitive::Line { greyed, .. } | Primitive::Arc { greyed, .. } | Primitive::Text { greyed, .. } | Primitive::DrawingText { greyed, .. } => *greyed = Some(g),
Primitive::Polygon { greyed, .. }
| Primitive::Line { greyed, .. }
| Primitive::Arc { greyed, .. }
| Primitive::Text { greyed, .. }
| Primitive::DrawingText { greyed, .. }
| Primitive::DrawingTextPath { greyed, .. }
| Primitive::DrawingImage { greyed, .. }
| Primitive::DrawingCircle { greyed, .. }
| Primitive::DrawingArc { greyed, .. } => *greyed = Some(g),
}
self
}
File diff suppressed because it is too large. Load diff
+11 -4
View File
@@ -2,11 +2,18 @@
use wasm_bindgen::prelude::wasm_bindgen;
/// Plan-Puffer (ein Chunk) → Vertex-/Index-/Batch-Daten als ein `u32`-Puffer; leer bei
/// unlesbarem Puffer.
/// Primitive `[from, to)` eines Plan-Puffers (v2) → Vertex-/Index-/Batch-Daten als ein
/// `u32`-Puffer; leer bei unlesbarem Puffer oder ungültigem Bereich.
#[wasm_bindgen]
pub fn gpu_compile_flat(buf: &[f64], px_per_meter: f64, packed_lines: bool) -> Vec<u32> {
crate::gpu_compile::compile_flat(buf, px_per_meter, packed_lines)
pub fn gpu_compile_flat(buf: &[u8], from: u32, to: u32, px_per_meter: f64, packed_lines: bool) -> Vec<u32> {
crate::gpu_compile::compile_flat(buf, from as usize, to as usize, px_per_meter, packed_lines)
}
/// Wie [`gpu_compile_flat`] auf einem gehaltenen Puffer (`plan_buf_hold`); leer, wenn er nicht
/// mehr gehalten wird.
#[wasm_bindgen]
pub fn gpu_compile_held(id: u32, from: u32, to: u32, px_per_meter: f64, packed_lines: bool) -> Vec<u32> {
super::plan_buf::with_held(id, |b| crate::gpu_compile::compile_flat(b, from as usize, to as usize, px_per_meter, packed_lines)).unwrap_or_default()
}
/// Ear-Clipping eines Rings `[x, y, …]` → Dreiecks-Indizes.
+44 -1
View File
@@ -1,10 +1,53 @@
//! Bindungen des Plan-Puffers (`crate::plan_buf`). Format s. dort.
//!
//! Gehaltene Puffer: TS übergibt einen Plan-Puffer einmal (`plan_buf_hold`) und kompiliert danach
//! beliebig viele GPU-Chunks daraus (`gpu_compile_held`), ohne ihn je Chunk erneut zu kopieren.
//! Höchstens `MAX_HELD` Puffer bzw. `MAX_HELD_BYTES` bleiben liegen (ältester fällt weg);
//! ein verdrängter Puffer liefert leere Ergebnisse, TS übergibt ihn dann neu.
use std::cell::RefCell;
use wasm_bindgen::prelude::wasm_bindgen;
const MAX_HELD: usize = 8;
const MAX_HELD_BYTES: usize = 96 << 20;
thread_local! {
static HELD: RefCell<(u32, Vec<(u32, Vec<u8>)>)> = const { RefCell::new((0, Vec::new())) };
}
/// Plan-Puffer lesen und neu schreiben (Hin-und-zurück-Prüfung der TS-Kodierung); leer bei
/// fremder Version oder kaputtem Puffer.
#[wasm_bindgen]
pub fn plan_buf_recode(buf: &[f64]) -> Vec<f64> {
pub fn plan_buf_recode(buf: &[u8]) -> Vec<u8> {
crate::plan_buf::recode(buf)
}
/// Puffer im Kern halten; Ergebnis ist die Nummer (≥ 1), 0 bei unlesbarem Puffer.
#[wasm_bindgen]
pub fn plan_buf_hold(buf: &[u8]) -> u32 {
if crate::plan_buf::View::parse(buf).is_none() {
return 0;
}
HELD.with(|h| {
let mut h = h.borrow_mut();
h.0 = h.0.wrapping_add(1).max(1);
let id = h.0;
h.1.push((id, buf.to_vec()));
let mut bytes: usize = h.1.iter().map(|e| e.1.len()).sum();
while h.1.len() > MAX_HELD || (bytes > MAX_HELD_BYTES && h.1.len() > 1) {
bytes -= h.1.remove(0).1.len();
}
id
})
}
/// Gehaltenen Puffer freigeben (unbekannte Nummer: nichts).
#[wasm_bindgen]
pub fn plan_buf_release(id: u32) {
HELD.with(|h| h.borrow_mut().1.retain(|e| e.0 != id));
}
/// Mit dem gehaltenen Puffer `id` rechnen; `None`, wenn er nicht (mehr) da ist.
pub(crate) fn with_held<R>(id: u32, f: impl FnOnce(&[u8]) -> R) -> Option<R> {
HELD.with(|h| h.borrow().1.iter().find(|e| e.0 == id).map(|e| f(&e.1)))
}
+23 -6
View File
@@ -1,9 +1,10 @@
/**
* Tessellierung: wandelt Plan-`Primitive` in GPU-fertige Puffer — Fassade über den Rust-Kern
* ([K3b], `src-tauri/core/src/gpu_compile.rs`). TS kodiert die Primitive eines Chunks in den
* Plan-Puffer (`../planBuf.ts`), Rust tesselliert (Ear-Clipping, Striche mit runden Kappen/Ecken,
* Schraffur, Zickzack, Motive, Bögen, Kreise) und liefert Vertex-/Index-/Batch-Daten flach zurück;
* hier werden sie nur noch hochgeladen.
* ([K3b], `src-tauri/core/src/gpu_compile.rs`). Rust tesselliert (Ear-Clipping, Striche mit runden
* Kappen/Ecken, Schraffur, Zickzack, Motive, Bögen, Kreise) und liefert Vertex-/Index-/Batch-Daten
* flach zurück; hier werden sie nur noch hochgeladen. Eingabe ist der Plan-Puffer v2
* (`../planBuf.ts`): Bereiche eines Kern-Puffers (`planDomains`) gehen direkt in die Kompilierung
* ({@link compilePlanBufRange}, [K3d]), gemischte Listen kodiert TS schlank ({@link compilePrimitivesToGpu}).
*
* Koordinaten: alles in BILDSCHIRM-Raum (wie `toScreen`): sx = mx·PX_PER_M, sy = -my·PX_PER_M,
* relativ zum lokalen Ursprung (`origin`, erster abgelegter Punkt).
@@ -12,7 +13,7 @@
import type { Primitive } from '../generatePlan';
import type { Vec2 } from '../../model/types';
import type { BufferGl, GpuGeometry, Rgba } from './glPlanTypes';
import { encodePlanBuf } from '../planBuf';
import { encodePlanBuf, type DecodedPlanBuf } from '../planBuf';
import { core } from '../../engine/coreWasm';
/**
@@ -41,7 +42,23 @@ export function compilePrimitivesToGpu(
pxPerMeter = 0,
): GpuGeometry {
const packed = gl.packedLineVertices === true;
const out = core().gpu_compile_flat(encodePlanBuf(primitives), pxPerMeter, packed);
const out = core().gpu_compile_flat(encodePlanBuf(primitives, { lean: true }), 0, primitives.length, pxPerMeter, packed);
return uploadCompiled(gl, out);
}
/**
* Wie {@link compilePrimitivesToGpu} für die Primitive `[from, to)` eines Kern-Puffers — ohne
* Umweg über Objekte: der Puffer wird einmal im Kern abgelegt (`plan_buf_hold`) und für alle
* Chunks wiederverwendet. Ist er dort verdrängt, wird er neu abgelegt.
*/
export function compilePlanBufRange(gl: BufferGl, src: DecodedPlanBuf, from: number, to: number, pxPerMeter = 0): GpuGeometry {
const packed = gl.packedLineVertices === true;
const c = core();
let out = src.hold ? c.gpu_compile_held(src.hold, from, to, pxPerMeter, packed) : new Uint32Array(0);
if (out.length === 0) {
src.hold = c.plan_buf_hold(src.bytes);
out = src.hold ? c.gpu_compile_held(src.hold, from, to, pxPerMeter, packed) : c.gpu_compile_flat(src.bytes, from, to, pxPerMeter, packed);
}
return uploadCompiled(gl, out);
}
+126 -53
View File
@@ -1,8 +1,10 @@
// Plan-Puffer [K3b]: TS kodiert, Rust liest und schreibt neu — Hin und zurück muss jede Zahl
// erhalten (je Primitiv-Typ), fremde Versionen werden sauber abgelehnt.
// Plan-Puffer v2 [K3d]: TS kodiert und liest, Rust liest und schreibt neu — hin und zurück bleibt
// jedes Primitiv gleich (je Typ, gemischt, über den Kern), fremde Versionen werden sauber abgelehnt.
import { describe, it, expect } from "vitest";
import { encodePlanBuf, encodeHatch, parseColor, PLAN_BUF_MAGIC, PLAN_BUF_VERSION } from "./planBuf";
import { decodePlanBuf, encodeHatch, encodePlanBuf, PLAN_BUF_MAGIC, PLAN_BUF_VERSION } from "./planBuf";
import { documentFor } from "../core/document";
import { sampleProject } from "../model/sampleProject";
import { core } from "../engine/coreWasm";
import type { HatchRender, Primitive } from "./generatePlan";
@@ -51,88 +53,159 @@ const PRIMS: Record<string, Primitive> = {
arc: { kind: "arc", center: { x: 0, y: 0 }, from: { x: 1, y: 0 }, to: { x: 0, y: 1 }, r: 1, cls: "c", weightMm: 0.18, opacity: 0.5 },
drawingCircle: { kind: "drawingCircle", center: { x: 5, y: 5 }, r: 2, fill: "none", stroke: "#123", weightMm: 0.18, dash: [2] },
drawingArc: { kind: "drawingArc", center: { x: 5, y: 5 }, r: 2, a0: 0, a1: 3, stroke: "#123", weightMm: 0.5, greyed: true },
text: { kind: "drawingText", at: { x: 0, y: 0 }, text: "x", heightM: 1, angle: 0, color: "#000" },
image: { kind: "drawingImage", min: { x: 0, y: 0 }, max: { x: 1, y: 1 }, src: "", opacity: 1 },
text: { kind: "drawingText", at: { x: 0, y: 0 }, text: "x 𝄞 ü", heightM: 1, angle: 0, color: "#000", drawingId: "t" },
textPath: { kind: "drawingTextPath", pathPts: [{ x: 0, y: 0 }, { x: 1, y: 2 }], text: "Pfad", heightM: 0.2, color: "#f00", greyed: false },
image: { kind: "drawingImage", min: { x: 0, y: 0 }, max: { x: 1, y: 1 }, src: "data:image/png;base64,AA", opacity: 1 },
stamp: {
kind: "text",
at: { x: 1, y: 1 },
doc: { version: 1, paragraphs: [{ align: "center", runs: [{ text: "Bad", marks: { bold: true } }] }] },
extraLines: [{ text: "4.20 m²", align: "center" }],
basePt: 10,
color: "#333",
roomId: "R1",
} as unknown as Primitive,
dashNull: { kind: "arc", center: { x: 0, y: 0 }, from: { x: 1, y: 0 }, to: { x: 0, y: 1 }, r: 1, cls: "c", weightMm: 0.18, dash: null, openingId: "o", stairId: "s", greyed: false },
manyIds: {
kind: "polygon",
pts: [{ x: 0, y: 0 }, { x: 1, y: 0 }, { x: 1, y: 1 }],
fill: "none",
stroke: "#000",
strokeWidthMm: 0.1,
hatch,
sectionLineId: "sl",
wallId: "w",
stairId: "st",
},
};
/** Gleichheit Zahl für Zahl (NaN = NaN, −0 ≠ +0). */
function sameBits(a: Float64Array, b: Float64Array): boolean {
if (a.length !== b.length) return false;
for (let i = 0; i < a.length; i++) if (!Object.is(a[i], b[i])) return false;
return true;
/**
* JSON mit sortierten Schlüsseln. Fehlende Zahlen (TS `undefined`) reisen als NaN — beide fallen
* hier weg; Kern-Ausgaben vergleichen die Paritätstests mit dem früheren JSON (NaN → null).
*/
function canon(v: unknown): string {
return JSON.stringify(v, (_k, x) =>
typeof x === "number" && Number.isNaN(x)
? undefined
: x && typeof x === "object" && !Array.isArray(x)
? Object.fromEntries(Object.keys(x).sort().map((k) => [k, (x as Record<string, unknown>)[k]]))
: x,
);
}
describe("Plan-Puffer", () => {
/** Flags der Einträge (Kopf und Offsets gelesen wie im Format beschrieben). */
function recFlags(bytes: Uint8Array): number[] {
const f = new Float64Array(bytes.buffer, bytes.byteOffset, bytes.byteLength >> 3);
const [nStrings, , nHatchWords, nGroups, nPrims] = [f[3], f[4], f[5], f[6], f[7]];
const offAt = 16 + 2 * nStrings + nHatchWords + 2 * nGroups;
const recAt = offAt + nPrims * (f[2] & 1 ? 2 : 1);
return Array.from({ length: nPrims }, (_, i) => f[recAt + f[offAt + i] + 1]);
}
describe("Plan-Puffer v2", () => {
for (const [name, prim] of Object.entries(PRIMS)) {
it(`Hin und zurück: ${name}`, () => {
const buf = encodePlanBuf([prim]);
expect(buf[0]).toBe(PLAN_BUF_MAGIC);
expect(buf[1]).toBe(PLAN_BUF_VERSION);
expect(buf[4]).toBe(1);
expect(sameBits(core().plan_buf_recode(buf), buf)).toBe(true);
const head = new Float64Array(buf.buffer, 0, 8);
expect(head[0]).toBe(PLAN_BUF_MAGIC);
expect(head[1]).toBe(PLAN_BUF_VERSION);
expect(head[7]).toBe(1);
expect(canon(decodePlanBuf(buf).prims)).toBe(canon([prim]));
// Über den Kern (lesen + neu schreiben) bleibt der Inhalt gleich.
expect(canon(decodePlanBuf(core().plan_buf_recode(buf)).prims)).toBe(canon([prim]));
});
}
it("Hin und zurück: alle Typen gemischt, ein Eintrag je Primitiv", () => {
it("Hin und zurück: alle Typen gemischt, mit Gruppen und Ausmass", () => {
const prims = Object.values(PRIMS);
const buf = encodePlanBuf(prims);
expect(buf[4]).toBe(prims.length);
expect(sameBits(core().plan_buf_recode(buf), buf)).toBe(true);
const buf = encodePlanBuf(prims, { groups: [[0, 2], [2, prims.length]], bounds: [-1, -2, 3, 4] });
const d = decodePlanBuf(buf);
expect(canon(d.prims)).toBe(canon(prims));
expect(d.groups).toEqual([[0, 2], [2, prims.length]]);
expect(d.bounds).toEqual([-1, -2, 3, 4]);
expect(d.hashes).toBeNull();
const back = decodePlanBuf(core().plan_buf_recode(buf));
expect(canon(back.prims)).toBe(canon(prims));
expect(back.groups).toEqual(d.groups);
// Schraffuren werden einmal gelesen und geteilt.
expect((d.prims[0] as { hatch: unknown }).hatch).toBe((d.prims[prims.length - 1] as { hatch: unknown }).hatch);
});
it("Texte und Bilder sind Platzhalter (nur der Typ-Eintrag 0)", () => {
expect(Array.from(encodePlanBuf([PRIMS.text, PRIMS.image]))).toEqual([PLAN_BUF_MAGIC, PLAN_BUF_VERSION, 0, 0, 2, 0, 0]);
it("Texte mit einzelnen Surrogaten bleiben in TS exakt", () => {
const t = { ...PRIMS.text, text: "a\ud800b\udfff" } as Primitive;
expect(decodePlanBuf(encodePlanBuf([t])).prims[0]).toEqual(t);
});
it("Farben werden einmal geparst und geteilt, ungültige ergeben −1", () => {
const a = { ...PRIMS.line, color: "#ff0000" } as Primitive;
const b = { ...PRIMS.line, color: "nonsense" } as Primitive;
const buf = encodePlanBuf([a, a, b]);
expect(buf[2]).toBe(1);
expect(Array.from(buf.subarray(5, 9))).toEqual(parseColor("#ff0000"));
it("schlank (nur GPU): Texte/Bilder als Platzhalter, ohne Ids und Klassen", () => {
const prims = Object.values(PRIMS);
const lean = encodePlanBuf(prims, { lean: true });
expect(lean.byteLength).toBeLessThan(encodePlanBuf(prims).byteLength);
expect(() => decodePlanBuf(lean)).toThrow();
// Die GPU-Kompilierung liest beide gleich.
const a = core().gpu_compile_flat(lean, 0, prims.length, 90, true);
const b = core().gpu_compile_flat(encodePlanBuf(prims), 0, prims.length, 90, true);
expect(a.length).toBeGreaterThan(30);
expect(Array.from(a)).toEqual(Array.from(b));
});
it("Linienzug-Flag: nur gleiches Element, gleicher Stil und lückenlos", () => {
const seg = (ax: number, bx: number, extra: object = {}): Primitive =>
({ kind: "line", a: { x: ax, y: 0 }, b: { x: bx, y: 0 }, cls: "c", weightMm: 0.18, color: "#000", drawingId: "d", ...extra }) as Primitive;
const flagsOf = (prims: Primitive[]) => {
const buf = encodePlanBuf(prims);
const out: number[] = [];
let i = 5 + buf[2] * 4;
for (let k = 0; k < prims.length; k++) {
out.push(buf[i + 1] & 4);
i += 9 + 1 + buf[i + 9];
}
return out;
};
expect(flagsOf([seg(0, 1), seg(1, 2), seg(2, 3)])).toEqual([0, 4, 4]);
expect(flagsOf([seg(0, 1), seg(1.5, 2)])).toEqual([0, 0]);
expect(flagsOf([seg(0, 1), seg(1, 2, { drawingId: "e" })])).toEqual([0, 0]);
expect(flagsOf([seg(0, 1), seg(1, 2, { weightMm: 0.5 })])).toEqual([0, 0]);
expect(flagsOf([seg(0, 1), seg(1, 2, { greyed: false })])).toEqual([0, 0]);
expect(flagsOf([seg(0, 1, { dash: [1] }), seg(1, 2, { dash: [1] })])).toEqual([0, 0]);
const chain = (prims: Primitive[]) => recFlags(encodePlanBuf(prims)).map((fl) => fl & 8);
expect(chain([seg(0, 1), seg(1, 2), seg(2, 3)])).toEqual([0, 8, 8]);
expect(chain([seg(0, 1), seg(1.5, 2)])).toEqual([0, 0]);
expect(chain([seg(0, 1), seg(1, 2, { drawingId: "e" })])).toEqual([0, 0]);
expect(chain([seg(0, 1), seg(1, 2, { weightMm: 0.5 })])).toEqual([0, 0]);
expect(chain([seg(0, 1), seg(1, 2, { greyed: false })])).toEqual([0, 0]);
// Strichmuster: dieselbe Referenz verbindet, gleiche Werte in neuen Listen nicht.
expect(chain([seg(0, 1, { dash: [1] }), seg(1, 2, { dash: [1] })])).toEqual([0, 0]);
const d = [1];
expect(chain([seg(0, 1, { dash: d }), seg(1, 2, { dash: d })])).toEqual([0, 8]);
expect(chain([seg(0, 1, { dash: null }), seg(1, 2, { dash: null })])).toEqual([0, 8]);
});
it("fremde Version oder kaputter Puffer → leeres Ergebnis, kein Absturz", () => {
const buf = encodePlanBuf(Object.values(PRIMS));
it("fremde Version oder kaputter Puffer → leeres Ergebnis bzw. Fehler, kein Absturz", () => {
const prims = Object.values(PRIMS);
const buf = encodePlanBuf(prims);
const wrong = buf.slice();
wrong[1] = PLAN_BUF_VERSION + 1;
new Float64Array(wrong.buffer, 0, 2)[1] = PLAN_BUF_VERSION + 1;
expect(core().plan_buf_recode(wrong).length).toBe(0);
expect(core().gpu_compile_flat(wrong, 90, true).length).toBe(0);
expect(core().gpu_compile_flat(wrong, 0, 1, 90, true).length).toBe(0);
expect(() => decodePlanBuf(wrong)).toThrow(/Version/);
expect(core().plan_buf_recode(buf.subarray(0, buf.length - 3)).length).toBe(0);
expect(core().gpu_compile_flat(new Float64Array([1, 2, 3]), 90, false).length).toBe(0);
expect(() => decodePlanBuf(buf.subarray(0, buf.length - 2))).toThrow();
expect(core().gpu_compile_flat(new Uint8Array([1, 2, 3]), 0, 0, 90, false).length).toBe(0);
expect(core().gpu_compile_flat(buf, 0, prims.length + 1, 90, false).length).toBe(0);
expect(core().gpu_compile_held(424242, 0, 1, 90, false).length).toBe(0);
expect(core().plan_buf_hold(wrong)).toBe(0);
// Danach arbeitet der Kern normal weiter.
expect(sameBits(core().plan_buf_recode(buf), buf)).toBe(true);
expect(canon(decodePlanBuf(core().plan_buf_recode(buf)).prims)).toBe(canon(prims));
});
it("gehaltener Puffer kompiliert wie der übergebene", () => {
const prims = Object.values(PRIMS);
const buf = encodePlanBuf(prims);
const id = core().plan_buf_hold(buf);
expect(id).toBeGreaterThan(0);
expect(Array.from(core().gpu_compile_held(id, 1, 5, 90, true))).toEqual(Array.from(core().gpu_compile_flat(buf, 1, 5, 90, true)));
core().plan_buf_release(id);
expect(core().gpu_compile_held(id, 1, 5, 90, true).length).toBe(0);
});
it("Fehler einer Kern-Abfrage kommen als Meldung", () => {
const doc = documentFor(sampleProject);
expect(() => decodePlanBuf(doc.queryBin("planDomains", [1], "kein json"))).toThrow(/Plan-Argumente/);
});
it("Einzel-Schraffur hat dasselbe Format wie ein Hatch-Eintrag", () => {
const h = encodeHatch(hatch);
const buf = encodePlanBuf([PRIMS.polygon]);
const colors = buf[2];
// Im Plan-Puffer steht der Farbindex statt −1.
const inPlan = buf.slice(5 + colors * 4, 5 + colors * 4 + h.length);
const f = new Float64Array(buf.buffer, 0, buf.byteLength >> 3);
const at = 16 + 2 * f[3] + 1; // nach dem JSON-String-Index des ersten Eintrags
const inPlan = f.slice(at, at + h.length);
// Im Plan-Puffer steht der Farb-String statt −1.
expect(inPlan[5]).toBeGreaterThanOrEqual(0);
inPlan[5] = -1;
expect(sameBits(inPlan, h)).toBe(true);
expect(Array.from(inPlan)).toEqual(Array.from(h));
});
});
+489 -189
View File
@@ -1,91 +1,73 @@
/**
* Plan-Puffer ([K3b]): kompakte Binärkodierung der zeichnungsrelevanten Plan-Primitive als
* flacher `Float64Array`. TS schreibt ihn vorerst aus `Plan.primitives`, der Rust-Kern liest ihn
* (`src-tauri/core/src/plan_buf.rs`, dort dieselbe Formatbeschreibung) — heute für die GPU-
* Kompilierung (`glPlan/glPlanCompile.ts`), ab [K3a] erzeugt Rust ihn direkt.
* Plan-Puffer v2 ([K3d]): binäre Übergabe der Plan-Primitive zwischen Rust-Kern und TS — Format
* und Bedeutung aller Felder stehen im Kopf von `src-tauri/core/src/plan_buf.rs` (hier gekürzt).
* Der Kern schreibt ihn direkt aus der Plan-Erzeugung (Abfrage `planDomains`); TS liest ihn zu
* `Primitive[]` ({@link decodePlanBuf}: Auswahl, Overlay, SVG/PDF/DXF) und schreibt ihn für
* GPU-Chunks aus gemischten Listen ({@link encodePlanBuf}, `glPlanCompile.ts`). Die GPU-Kompilierung
* liest Bereiche eines Kern-Puffers direkt (`compilePlanBufRange`), ohne Umweg über Objekte.
*
* ```text
* Kopf [MAGIC, VERSION, nColors, nHatches, nPrims]
* MAGIC = 0x504C4231 ("PLB1"), VERSION = 1 — Rust lehnt fremde Werte ab (leeres Ergebnis)
* Farben nColors × [r, g, b, a] (0..1, hier aus dem Farbtext geparst)
* Schraffuren nHatches × Hatch
* Primitive nPrims × Prim (genau ein Eintrag je Primitiv → Indizes bleiben gleich)
*
* Hatch [pattern, kind, flags, scale, angle, colorIdx, lineWeight, originX, originY,
* seed, density, lengthMin, lengthMax, nDash, dash…, (Motiv: length, nPts, x,y…)]
* pattern 0 none · 1 solid · 2 diagonal (auch unbekannt) · 3 crosshatch · 4 insulation · 5 custom
* kind 0 vector · 1 image · 2 gradient (Bild/Verlauf: GPU fällt auf die Füllung zurück)
* flags 1 random · 2 origin · 4 seed · 8 density · 16 lengthMin · 32 lengthMax · 64 Motiv
* (Felder ohne Flag stehen auf 0 und gelten als „nicht gesetzt")
*
* Prim [tag, flags, …] flags 1 greyed · 2 opacity gesetzt · 4 setzt den Linienzug der
* vorigen Linie fort · 8 Zickzack · 16 Motiv
* 0 Platzhalter [0] (Text, Bild … — zeichnet die GPU nicht)
* 1 polygon [1, flags, fillIdx, strokeIdx, strokeWidthMm, hatchIdx, opacity,
* nPts, x,y…, nNoStroke, kante…]
* 2 line [2, flags, colorIdx, weightMm, opacity, ax, ay, bx, by, nDash, dash…,
* (Zickzack: amplitude, wavelength), (Motiv: length, nPts, x,y…)]
* 3 arc [3, flags, colorIdx, weightMm, opacity, cx, cy, fromX, fromY, toX, toY, r,
* nDash, dash…]
* 4 drawingCircle [4, flags, fillIdx, strokeIdx, weightMm, opacity, cx, cy, r, nDash, dash…]
* 5 drawingArc [5, flags, strokeIdx, weightMm, opacity, cx, cy, r, a0, a1, nDash, dash…]
* Bytes: f64-Teil, dann UTF-16-Einheiten aller Texte
* Kopf (16) [MAGIC "PLB2", VERSION 2, flags (1 Hashes, 2 Ausmass), nStrings, nHatches, nHatchWords,
* nGroups, nPrims, nRecWords, nPool, nUnits, 0, minX, minY, maxX, maxY]
* Strings nStrings × [unitOff, unitLen]
* Schraffur nHatches × [jsonStr, Hatch] (Hatch wie encodeHatch, Farbe = String-Index)
* Gruppen nGroups × [start, end)
* Offsets nPrims × Wortindex des Eintrags
* Hashes nPrims × Inhalts-Hash (nur Kern)
* Primitive [tag, flags, pickKind, pickStr, …] 1 polygon · 2 line · 3 arc · 4 drawingCircle ·
* 5 drawingArc · 6 text · 7 drawingText · 8 drawingTextPath · 9 drawingImage (JSON) ·
* 0 Platzhalter (nur `lean`)
* Pool Punkte, Strichmuster, Kantenlisten, Motive, weitere Ids
* ```
* Farb-/Schraffur-Index −1 = keine (Farbtext ungültig, "none", fehlende Schraffur). Längen in
* Metern, Strichstärken/Strichmuster/Zickzack/Motiv in Papier-mm, Winkel von drawingArc in
* Bogenmass, Schraffur-Winkel in Grad (SVG-Drehsinn). Ohne opacity-Flag steht 1 im Feld; fehlende
* Zahlen (`undefined`) werden NaN, der Verbraucher nimmt dann wie bisher seinen Standard.
* Flag 4 (Linienzug): gleiches Element (`drawingId`), gleiche Farbe/Stärke/greyed/opacity/
* Strichmuster (Referenz) und Anfang = Ende der vorigen Linie — dann zeichnet die GPU die
* Einzelsegmente als EINEN Zug (zwei echte Kappen statt 2N). Pick-IDs stehen NICHT im Puffer
* (Picken bleibt vorerst TS).
* Flags: 1 greyed gesetzt · 2 greyed · 4 opacity · 8 Linienzug · 16 Zickzack · 32 Motiv ·
* 64 dash null · 128 dash Liste · 256 noStrokeEdges · 512 Farbe (line/arc) · 1024 weitere Ids.
*/
import type { HatchRender, Primitive } from "./generatePlan";
import type { Vec2 } from "../model/types";
export const PLAN_BUF_MAGIC = 0x504c4231;
export const PLAN_BUF_VERSION = 1;
export const PLAN_BUF_MAGIC = 0x504c4232;
export const PLAN_BUF_VERSION = 2;
/** Fehlerpuffer einer Kern-Abfrage ("PLBe"): `[ERR_MAGIC, nUnits]` + Meldung (UTF-16). */
export const PLAN_BUF_ERR_MAGIC = 0x504c4265;
const HEAD = 16;
/** RGBA-Farbe, Komponenten in [0,1]. */
export type PlanRgba = [number, number, number, number];
const F_GREYED_SET = 1;
const F_GREYED = 2;
const F_OPACITY = 4;
const F_CHAIN = 8;
const F_ZIGZAG = 16;
const F_MOTIF = 32;
const F_DASH_NULL = 64;
const F_DASH_LIST = 128;
const F_NSE = 256;
const F_COLOR = 512;
const F_IDS = 1024;
const FLAG_GREYED = 1;
const FLAG_OPACITY = 2;
const FLAG_CHAIN = 4;
const FLAG_ZIGZAG = 8;
const FLAG_MOTIF = 16;
/**
* Farbe → RGBA[0..1]; ungültig/"none"/"transparent" → null.
* Unterstützt "#rgb", "#rrggbb", "#rrggbbaa" und "rgb()/rgba()".
*/
export function parseColor(hex: string | undefined): PlanRgba | null {
if (!hex) return null;
const s = hex.trim().toLowerCase();
if (s === "none" || s === "transparent") return null;
if (s.startsWith("rgb")) {
const m = s.match(/[\d.]+/g);
if (!m || m.length < 3) return null;
const a = m.length >= 4 ? parseFloat(m[3]) : 1;
return [+m[0] / 255, +m[1] / 255, +m[2] / 255, a > 1 ? a / 255 : a];
}
let h = s[0] === "#" ? s.slice(1) : s;
if (h.length === 3) h = h[0] + h[0] + h[1] + h[1] + h[2] + h[2];
if (h.length !== 6 && h.length !== 8) return null;
const r = parseInt(h.slice(0, 2), 16);
const g = parseInt(h.slice(2, 4), 16);
const b = parseInt(h.slice(4, 6), 16);
const a = h.length === 8 ? parseInt(h.slice(6, 8), 16) / 255 : 1;
if (isNaN(r) || isNaN(g) || isNaN(b)) return null;
return [r / 255, g / 255, b / 255, a];
}
/** Pick-Arten (Index + 1) = Feldname am Primitiv. */
const PICK_KEYS = [
"wallId",
"drawingId",
"ceilingId",
"openingId",
"stairId",
"roomId",
"extrudedSolidId",
"contextObjectId",
"columnId",
"roofId",
"sectionLineId",
] as const;
type PickKey = (typeof PICK_KEYS)[number];
/** Wachsender `Float64Array`-Schreiber. */
class F64Writer {
buf = new Float64Array(1024);
buf: Float64Array;
length = 0;
constructor(cap = 256) {
this.buf = new Float64Array(cap);
}
private grow(need: number): void {
let cap = this.buf.length * 2;
while (cap < need) cap *= 2;
@@ -131,7 +113,7 @@ const PATTERN_CODE: Record<string, number> = {
custom: 5,
};
/** Eine Schraffur im Hatch-Format (auch Eingabe der Einzel-Bindungen in `glPlanHatch.ts`). */
/** Ein Hatch-Datensatz (auch Eingabe der Einzel-Bindungen in `glPlanHatch.ts`). */
function writeHatch(w: F64Writer, h: HatchRender, colorIdx: number): void {
let flags = 0;
if (h.lines === "random") flags |= 1;
@@ -165,14 +147,11 @@ function writeHatch(w: F64Writer, h: HatchRender, colorIdx: number): void {
/** Einzelne Schraffur kodieren (Farbe −1) — Eingabe der Schraffur-Bindungen des Kerns. */
export function encodeHatch(h: HatchRender): Float64Array {
const w = new F64Writer();
const w = new F64Writer(32);
writeHatch(w, h, -1);
return w.buf.subarray(0, w.length);
}
/** Zwischenspeicher Farbtext → Farbindex-Eintrag (geparste Farbe oder null). */
const colorCache = new Map<string, PlanRgba | null>();
type LinePrim = Extract<Primitive, { kind: "line" }>;
const isPlainLine = (p: Primitive | undefined): p is LinePrim =>
@@ -193,164 +172,485 @@ function chainsPrev(prev: Primitive | undefined, p: LinePrim): boolean {
);
}
/** Primitive → Plan-Puffer (Format s. Dateikopf). */
export function encodePlanBuf(prims: readonly Primitive[]): Float64Array {
const colors: PlanRgba[] = [];
const colorIdx = new Map<string, number>();
let lastColor: string | undefined;
let lastColorIdx = -1;
const color = (s: string | undefined): number => {
if (!s) return -1;
if (s === lastColor) return lastColorIdx;
let i = colorIdx.get(s);
if (i !== undefined) {
lastColor = s;
lastColorIdx = i;
return i;
export interface EncodeOptions {
/**
* Nur für die GPU: ohne Pick-Ids, Linienklassen und Schraffur-JSON, Texte/Bilder als Platzhalter
* (tag 0). Schneller; {@link decodePlanBuf} kann so einen Puffer nicht vollständig lesen.
*/
lean?: boolean;
/** Gruppen `[start, end)` in den Kopf schreiben. */
groups?: ReadonlyArray<readonly [number, number]>;
bounds?: readonly [number, number, number, number];
}
/** Primitive → Plan-Puffer v2 (Format s. Dateikopf). */
export function encodePlanBuf(prims: readonly Primitive[], opts: EncodeOptions = {}): Uint8Array {
const lean = opts.lean === true;
const strParts: string[] = [];
const strTab = new F64Writer(64);
const strIdx = new Map<string, number>();
let units = 0;
const str = (s: string | undefined): number => {
if (s === undefined) return -1;
let i = strIdx.get(s);
if (i === undefined) {
i = strTab.length >> 1;
strTab.push(units);
strTab.push(s.length);
units += s.length;
strParts.push(s);
strIdx.set(s, i);
}
let c = colorCache.get(s);
if (c === undefined) {
c = parseColor(s);
if (colorCache.size > 4096) colorCache.clear();
colorCache.set(s, c);
}
i = c ? colors.push(c) - 1 : -1;
colorIdx.set(s, i);
return i;
};
const hatchW = new F64Writer();
const hatchW = new F64Writer(64);
const hatchIdx = new Map<HatchRender, number>();
const hatch = (h: HatchRender | undefined): number => {
if (!h) return -1;
let i = hatchIdx.get(h);
if (i === undefined) {
i = hatchIdx.size;
writeHatch(hatchW, h, color(h.color));
hatchW.push(lean ? -1 : str(JSON.stringify(h)));
writeHatch(hatchW, h, str(h.color));
hatchIdx.set(h, i);
}
return i;
};
const style = (greyed: boolean | undefined, opacity: number | undefined): number =>
(greyed ? FLAG_GREYED : 0) | (opacity !== undefined ? FLAG_OPACITY : 0);
const offsets = new Float64Array(prims.length);
const w = new F64Writer(Math.max(256, prims.length * 14));
const pool = new F64Writer(Math.max(256, prims.length * 4));
const poolList = (l: readonly number[]): void => {
w.push(pool.length);
w.push(l.length);
const o = pool.reserve(l.length);
for (let i = 0; i < l.length; i++) pool.buf[o + i] = l[i];
};
const poolPts = (p: readonly Vec2[]): void => {
w.push(pool.length);
w.push(p.length);
const o = pool.reserve(2 * p.length);
const b = pool.buf;
for (let i = 0; i < p.length; i++) {
b[o + 2 * i] = p[i].x;
b[o + 2 * i + 1] = p[i].y;
}
};
const styleFlags = (greyed: boolean | undefined, opacity: number | undefined): number =>
(greyed === undefined ? 0 : greyed ? F_GREYED_SET | F_GREYED : F_GREYED_SET) | (opacity !== undefined ? F_OPACITY : 0);
const dashFlags = (d: number[] | null | undefined): number => (d === null ? F_DASH_NULL : d ? F_DASH_LIST : 0);
const op = (opacity: number | undefined): number => (opacity !== undefined ? opacity : 1);
/** Kopf [tag, flags, pickKind, pickStr]; liefert die weiteren Ids. */
const head = (tag: number, flags: number, p: Primitive): number[] | null => {
let pk = 0;
let ps = -1;
let more: number[] | null = null;
if (!lean) {
const r = p as Partial<Record<PickKey, string>>;
for (let k = 0; k < PICK_KEYS.length; k++) {
const id = r[PICK_KEYS[k]];
if (id === undefined) continue;
if (pk === 0) {
pk = k + 1;
ps = str(id);
} else (more ??= []).push(k + 1, str(id));
}
}
const o = w.reserve(4);
const b = w.buf;
b[o] = tag;
b[o + 1] = flags | (more ? F_IDS : 0);
b[o + 2] = pk;
b[o + 3] = ps;
return more;
};
const tail = (more: number[] | null): void => {
if (!more) return;
w.push(pool.length);
w.push(more.length / 2);
for (const v of more) pool.push(v);
};
const w = new F64Writer();
for (let i = 0; i < prims.length; i++) {
const p = prims[i];
offsets[i] = w.length;
switch (p.kind) {
case "polygon": {
const fill = color(p.fill);
const stroke = color(p.stroke);
const more = head(1, styleFlags(p.greyed, p.opacity) | (p.noStrokeEdges ? F_NSE : 0), p);
const fill = str(p.fill);
const stroke = str(p.stroke);
const h = hatch(p.hatch);
const o = w.reserve(7);
const o = w.reserve(5);
const b = w.buf;
b[o] = 1;
b[o + 1] = style(p.greyed, p.opacity);
b[o + 2] = fill;
b[o + 3] = stroke;
b[o + 4] = p.strokeWidthMm;
b[o + 5] = h;
b[o + 6] = op(p.opacity);
w.pts(p.pts);
w.list(p.noStrokeEdges);
b[o] = fill;
b[o + 1] = stroke;
b[o + 2] = p.strokeWidthMm;
b[o + 3] = h;
b[o + 4] = op(p.opacity);
poolPts(p.pts);
if (p.noStrokeEdges) poolList(p.noStrokeEdges);
tail(more);
break;
}
case "line": {
let flags = style(p.greyed, p.opacity);
if (isPlainLine(p) && chainsPrev(prims[i - 1], p)) flags |= FLAG_CHAIN;
if (p.zigzag) flags |= FLAG_ZIGZAG;
if (p.motif) flags |= FLAG_MOTIF;
const c = color(p.color);
const o = w.reserve(9);
let flags = styleFlags(p.greyed, p.opacity) | dashFlags(p.dash);
if (isPlainLine(p) && chainsPrev(prims[i - 1], p)) flags |= F_CHAIN;
if (p.zigzag) flags |= F_ZIGZAG;
if (p.motif) flags |= F_MOTIF;
if (p.color !== undefined) flags |= F_COLOR;
const more = head(2, flags, p);
const cls = lean ? -1 : str(p.cls);
const c = str(p.color);
const o = w.reserve(8);
const b = w.buf;
b[o] = 2;
b[o + 1] = flags;
b[o + 2] = c;
b[o + 3] = p.weightMm;
b[o + 4] = op(p.opacity);
b[o + 5] = p.a.x;
b[o + 6] = p.a.y;
b[o + 7] = p.b.x;
b[o + 8] = p.b.y;
w.list(p.dash);
b[o] = cls;
b[o + 1] = c;
b[o + 2] = p.weightMm;
b[o + 3] = op(p.opacity);
b[o + 4] = p.a.x;
b[o + 5] = p.a.y;
b[o + 6] = p.b.x;
b[o + 7] = p.b.y;
if (p.dash) poolList(p.dash);
if (p.zigzag) {
w.push(p.zigzag.amplitude);
w.push(p.zigzag.wavelength);
}
if (p.motif) {
w.push(p.motif.length);
w.pts(p.motif.points);
poolPts(p.motif.points);
}
tail(more);
break;
}
case "arc": {
const c = color((p as { color?: string }).color);
const o = w.reserve(12);
const color = (p as { color?: string }).color;
const more = head(3, styleFlags(p.greyed, p.opacity) | dashFlags(p.dash) | (color !== undefined ? F_COLOR : 0), p);
const cls = lean ? -1 : str(p.cls);
const c = str(color);
const o = w.reserve(11);
const b = w.buf;
b[o] = 3;
b[o + 1] = style(p.greyed, p.opacity);
b[o + 2] = c;
b[o + 3] = p.weightMm;
b[o + 4] = op(p.opacity);
b[o + 5] = p.center.x;
b[o + 6] = p.center.y;
b[o + 7] = p.from.x;
b[o + 8] = p.from.y;
b[o + 9] = p.to.x;
b[o + 10] = p.to.y;
b[o + 11] = p.r;
w.list(p.dash);
b[o] = cls;
b[o + 1] = c;
b[o + 2] = p.weightMm;
b[o + 3] = op(p.opacity);
b[o + 4] = p.center.x;
b[o + 5] = p.center.y;
b[o + 6] = p.from.x;
b[o + 7] = p.from.y;
b[o + 8] = p.to.x;
b[o + 9] = p.to.y;
b[o + 10] = p.r;
if (p.dash) poolList(p.dash);
tail(more);
break;
}
case "drawingCircle": {
const fill = color(p.fill);
const stroke = color(p.stroke);
const o = w.reserve(9);
const more = head(4, styleFlags(p.greyed, p.opacity) | dashFlags(p.dash), p);
const fill = str(p.fill);
const stroke = str(p.stroke);
const o = w.reserve(7);
const b = w.buf;
b[o] = 4;
b[o + 1] = style(p.greyed, p.opacity);
b[o + 2] = fill;
b[o + 3] = stroke;
b[o + 4] = p.weightMm;
b[o + 5] = op(p.opacity);
b[o + 6] = p.center.x;
b[o + 7] = p.center.y;
b[o + 8] = p.r;
w.list(p.dash);
b[o] = fill;
b[o + 1] = stroke;
b[o + 2] = p.weightMm;
b[o + 3] = op(p.opacity);
b[o + 4] = p.center.x;
b[o + 5] = p.center.y;
b[o + 6] = p.r;
if (p.dash) poolList(p.dash);
tail(more);
break;
}
case "drawingArc": {
const stroke = color(p.stroke);
const o = w.reserve(10);
const more = head(5, styleFlags(p.greyed, p.opacity) | dashFlags(p.dash), p);
const stroke = str(p.stroke);
const o = w.reserve(8);
const b = w.buf;
b[o] = 5;
b[o + 1] = style(p.greyed, p.opacity);
b[o + 2] = stroke;
b[o + 3] = p.weightMm;
b[o + 4] = op(p.opacity);
b[o + 5] = p.center.x;
b[o + 6] = p.center.y;
b[o + 7] = p.r;
b[o + 8] = p.a0;
b[o + 9] = p.a1;
w.list(p.dash);
b[o] = stroke;
b[o + 1] = p.weightMm;
b[o + 2] = op(p.opacity);
b[o + 3] = p.center.x;
b[o + 4] = p.center.y;
b[o + 5] = p.r;
b[o + 6] = p.a0;
b[o + 7] = p.a1;
if (p.dash) poolList(p.dash);
tail(more);
break;
}
default:
// Text, Bild …: Platzhalter, damit Indizes/Bereiche gleich bleiben.
w.push(0);
default: {
if (lean) {
head(0, 0, p);
break;
}
const tag = p.kind === "text" ? 6 : p.kind === "drawingText" ? 7 : p.kind === "drawingTextPath" ? 8 : 9;
head(tag, 0, p);
w.push(str(JSON.stringify(p)));
}
}
}
const head = 5 + colors.length * 4;
const out = new Float64Array(head + hatchW.length + w.length);
out[0] = PLAN_BUF_MAGIC;
out[1] = PLAN_BUF_VERSION;
out[2] = colors.length;
out[3] = hatchIdx.size;
out[4] = prims.length;
for (let i = 0; i < colors.length; i++) out.set(colors[i], 5 + i * 4);
out.set(hatchW.buf.subarray(0, hatchW.length), head);
out.set(w.buf.subarray(0, w.length), head + hatchW.length);
return out;
const groups = opts.groups ?? [];
const nStrings = strTab.length >> 1;
const nF64 = HEAD + strTab.length + hatchW.length + 2 * groups.length + prims.length + w.length + pool.length;
const out = new ArrayBuffer(nF64 * 8 + units * 2);
const f = new Float64Array(out, 0, nF64);
const bounds = opts.bounds;
f.set([
PLAN_BUF_MAGIC,
PLAN_BUF_VERSION,
bounds ? 2 : 0,
nStrings,
hatchIdx.size,
hatchW.length,
groups.length,
prims.length,
w.length,
pool.length,
units,
0,
bounds ? bounds[0] : 0,
bounds ? bounds[1] : 0,
bounds ? bounds[2] : 0,
bounds ? bounds[3] : 0,
]);
let k = HEAD;
f.set(strTab.buf.subarray(0, strTab.length), k);
k += strTab.length;
f.set(hatchW.buf.subarray(0, hatchW.length), k);
k += hatchW.length;
for (const [a, b] of groups) {
f[k++] = a;
f[k++] = b;
}
f.set(offsets, k);
k += prims.length;
f.set(w.buf.subarray(0, w.length), k);
k += w.length;
f.set(pool.buf.subarray(0, pool.length), k);
if (units > 0) {
const u = new Uint16Array(out, nF64 * 8, units);
let j = 0;
for (const s of strParts) for (let c = 0; c < s.length; c++) u[j++] = s.charCodeAt(c);
}
return new Uint8Array(out);
}
/** Gelesener Plan-Puffer (Kern-Ergebnis); `bytes` bleibt für die GPU-Kompilierung erhalten. */
export interface DecodedPlanBuf {
bytes: Uint8Array;
prims: Primitive[];
groups: Array<[number, number]>;
bounds?: [number, number, number, number];
/** Inhalts-Hash je Primitiv (vom Kern; Schlüssel des GPU-Chunk-Caches) oder null. */
hashes: Float64Array | null;
/** Nummer des im Kern gehaltenen Puffers (0 = keiner), s. `compilePlanBufRange`. */
hold: number;
}
/** UTF-16-Einheiten → ein String (exakt, auch einzelne Surrogate). */
function unitsToString(u: Uint16Array): string {
let s = "";
for (let i = 0; i < u.length; i += 8192) {
s += String.fromCharCode.apply(null, u.subarray(i, Math.min(u.length, i + 8192)) as unknown as number[]);
}
return s;
}
function bad(why: string): never {
throw new Error(`Plan-Puffer unlesbar: ${why}`);
}
/**
* Plan-Puffer → Primitive (in der JSON-Form, die der Kern früher lieferte: gleiche Felder, gleiche
* Reihenfolge). Wirft bei einem Fehlerpuffer des Kerns (mit dessen Meldung) oder kaputtem Puffer.
*/
export function decodePlanBuf(input: Uint8Array): DecodedPlanBuf {
const bytes = input.byteOffset % 8 === 0 ? input : input.slice();
if (bytes.byteLength >= 16) {
const h = new Float64Array(bytes.buffer, bytes.byteOffset, 2);
if (h[0] === PLAN_BUF_ERR_MAGIC) {
const n = h[1];
throw new Error(unitsToString(new Uint16Array(bytes.buffer, bytes.byteOffset + 16, Math.min(n, (bytes.byteLength - 16) >> 1))));
}
}
if (bytes.byteLength < HEAD * 8) bad("zu kurz");
const hd = new Float64Array(bytes.buffer, bytes.byteOffset, HEAD);
if (hd[0] !== PLAN_BUF_MAGIC || hd[1] !== PLAN_BUF_VERSION) bad("fremde Version");
const [, , flags, nStrings, nHatches, nHatchWords, nGroups, nPrims, nRec, nPool, nUnits] = hd;
const hashes = (flags & 1) !== 0;
const strAt = HEAD;
const hatchAt = strAt + 2 * nStrings;
const groupAt = hatchAt + nHatchWords;
const offAt = groupAt + 2 * nGroups;
const hashAt = offAt + nPrims;
const recAt = hashAt + (hashes ? nPrims : 0);
const poolAt = recAt + nRec;
const nF64 = poolAt + nPool;
if (!Number.isInteger(nF64) || bytes.byteLength !== nF64 * 8 + nUnits * 2) bad("Länge");
const f = new Float64Array(bytes.buffer, bytes.byteOffset, nF64);
const all = unitsToString(new Uint16Array(bytes.buffer, bytes.byteOffset + nF64 * 8, nUnits));
const strs: Array<string | undefined> = new Array(nStrings);
const S = (i: number): string => {
let s = strs[i];
if (s === undefined) {
const o = f[strAt + 2 * i];
s = all.slice(o, o + f[strAt + 2 * i + 1]);
strs[i] = s;
}
return s;
};
// Schraffuren: einmal je Eintrag aus dem JSON (geteilt von allen Polygonen).
const hatchList: HatchRender[] = [];
let k = hatchAt;
for (let i = 0; i < nHatches; i++) {
// [jsonStr, pattern, kind, flags, scale, angle, color, lineWeight, ox, oy, seed, density,
// lengthMin, lengthMax, nDash, dash…, (Motiv: length, nPts, x,y…)]
const js = f[k];
hatchList.push(js >= 0 ? (JSON.parse(S(js)) as HatchRender) : (undefined as unknown as HatchRender));
const hf = f[k + 3];
k += 15 + f[k + 14];
if (hf & 64) k += 2 + 2 * f[k + 1];
if (!(k <= groupAt)) bad("Schraffuren");
}
if (k !== groupAt) bad("Schraffuren");
const groups: Array<[number, number]> = [];
for (let g = 0; g < nGroups; g++) groups.push([f[groupAt + 2 * g], f[groupAt + 2 * g + 1]]);
const pts = (o: number, n: number): Vec2[] => {
const out: Vec2[] = new Array(n);
const b = poolAt + o;
for (let i = 0; i < n; i++) out[i] = { x: f[b + 2 * i], y: f[b + 2 * i + 1] };
return out;
};
const list = (o: number, n: number): number[] => {
const out: number[] = new Array(n);
const b = poolAt + o;
for (let i = 0; i < n; i++) out[i] = f[b + i];
return out;
};
/** Pick-Ids in Art-Reihenfolge (= Feldreihenfolge der Kern-JSON-Form). */
const setIds = (o: Record<string, unknown>, pk: number, ps: number, fl: number, tailAt: number): void => {
if (!(fl & F_IDS)) {
o[PICK_KEYS[pk - 1]] = S(ps);
return;
}
const found: Array<[number, string]> = [[pk, S(ps)]];
const po = poolAt + f[tailAt];
const n = f[tailAt + 1];
for (let j = 0; j < n; j++) found.push([f[po + 2 * j], S(f[po + 2 * j + 1])]);
found.sort((a, b) => a[0] - b[0]);
for (const [kk, id] of found) o[PICK_KEYS[kk - 1]] = id;
};
const prims: Primitive[] = new Array(nPrims);
for (let i = 0; i < nPrims; i++) {
let r = recAt + f[offAt + i];
const tag = f[r];
const fl = f[r + 1];
const pk = f[r + 2];
const ps = f[r + 3];
r += 4;
let o: Record<string, unknown>;
switch (tag) {
case 1: {
o = { kind: "polygon", pts: pts(f[r + 5], f[r + 6]), fill: S(f[r]), stroke: S(f[r + 1]), strokeWidthMm: f[r + 2] };
if (f[r + 3] >= 0) o.hatch = hatchList[f[r + 3]];
if (fl & F_GREYED_SET) o.greyed = (fl & F_GREYED) !== 0;
if (fl & F_OPACITY) o.opacity = f[r + 4];
let t = r + 7;
if (fl & F_NSE) {
o.noStrokeEdges = list(f[t], f[t + 1]);
t += 2;
}
if (pk > 0 || fl & F_IDS) setIds(o, pk, ps, fl, t);
break;
}
case 2: {
o = { kind: "line", a: { x: f[r + 4], y: f[r + 5] }, b: { x: f[r + 6], y: f[r + 7] }, cls: S(f[r]), weightMm: f[r + 2] };
let t = r + 8;
if (fl & F_DASH_NULL) o.dash = null;
else if (fl & F_DASH_LIST) {
o.dash = list(f[t], f[t + 1]);
t += 2;
}
if (fl & F_ZIGZAG) {
o.zigzag = { amplitude: f[t], wavelength: f[t + 1] };
t += 2;
}
if (fl & F_MOTIF) {
o.motif = { points: pts(f[t + 1], f[t + 2]), length: f[t] };
t += 3;
}
if (fl & F_COLOR) o.color = S(f[r + 1]);
if (pk > 0 || fl & F_IDS) setIds(o, pk, ps, fl, t);
if (fl & F_GREYED_SET) o.greyed = (fl & F_GREYED) !== 0;
if (fl & F_OPACITY) o.opacity = f[r + 3];
break;
}
case 3: {
o = {
kind: "arc",
center: { x: f[r + 4], y: f[r + 5] },
from: { x: f[r + 6], y: f[r + 7] },
to: { x: f[r + 8], y: f[r + 9] },
r: f[r + 10],
cls: S(f[r]),
weightMm: f[r + 2],
};
let t = r + 11;
if (fl & F_DASH_NULL) o.dash = null;
else if (fl & F_DASH_LIST) {
o.dash = list(f[t], f[t + 1]);
t += 2;
}
if (fl & F_COLOR) o.color = S(f[r + 1]);
if (pk > 0 || fl & F_IDS) setIds(o, pk, ps, fl, t);
if (fl & F_GREYED_SET) o.greyed = (fl & F_GREYED) !== 0;
if (fl & F_OPACITY) o.opacity = f[r + 3];
break;
}
case 4: {
o = { kind: "drawingCircle", center: { x: f[r + 4], y: f[r + 5] }, r: f[r + 6], fill: S(f[r]), stroke: S(f[r + 1]), weightMm: f[r + 2] };
let t = r + 7;
if (fl & F_DASH_NULL) o.dash = null;
else if (fl & F_DASH_LIST) {
o.dash = list(f[t], f[t + 1]);
t += 2;
}
if (pk > 0 || fl & F_IDS) setIds(o, pk, ps, fl, t);
if (fl & F_GREYED_SET) o.greyed = (fl & F_GREYED) !== 0;
if (fl & F_OPACITY) o.opacity = f[r + 3];
break;
}
case 5: {
o = { kind: "drawingArc", center: { x: f[r + 3], y: f[r + 4] }, r: f[r + 5], a0: f[r + 6], a1: f[r + 7], stroke: S(f[r]), weightMm: f[r + 1] };
let t = r + 8;
if (fl & F_DASH_NULL) o.dash = null;
else if (fl & F_DASH_LIST) {
o.dash = list(f[t], f[t + 1]);
t += 2;
}
if (pk > 0 || fl & F_IDS) setIds(o, pk, ps, fl, t);
if (fl & F_GREYED_SET) o.greyed = (fl & F_GREYED) !== 0;
if (fl & F_OPACITY) o.opacity = f[r + 2];
break;
}
case 6:
case 7:
case 8:
case 9:
o = JSON.parse(S(f[r])) as Record<string, unknown>;
break;
default:
bad(`Primitiv-Typ ${tag}`);
}
prims[i] = o as unknown as Primitive;
}
return {
bytes,
prims,
groups,
bounds: flags & 2 ? [hd[12], hd[13], hd[14], hd[15]] : undefined,
hashes: hashes ? new Float64Array(bytes.buffer, bytes.byteOffset + hashAt * 8, nPrims) : null,
hold: 0,
};
}