Merge rust-core-k4 (Laptop): [K1][K3a][K3c][K3d][K4] Kern-Port; IFC-/Mesh-Export bleibt in fileio (Kern-Duplikate entfernt), Exporte holen 3D-Teile aus dem Dokument

Claude-Session: https://claude.ai/code/session_01NTYBZBvLYCtugMJoKXaB67
This commit is contained in:
Claude committed 2026-10-28 17:35:14 +00:00
commit 3bf9eea5f9
111 files changed
+14145 -6506

No files matched your search

+56
View File
@@ -0,0 +1,56 @@
import { describe, expect, it } from "vitest";
import { ProjectDocument } from "./document";
import { sampleProject } from "../model/sampleProject";
import type { Project } from "../model/types";
const plain = (p: Project): unknown => JSON.parse(JSON.stringify(p));
function mutate(p: Project, seed: number): Project {
let x = seed;
const rnd = () => ((x = (x * 1664525 + 1013904223) % 4294967296) / 4294967296);
let out = { ...p };
const walls = [...out.walls];
if (rnd() < 0.5 && walls.length) walls.splice(Math.floor(rnd() * walls.length), 1);
if (rnd() < 0.7 && walls.length) {
const i = Math.floor(rnd() * walls.length);
walls[i] = { ...walls[i], height: (walls[i].height ?? 2.6) + rnd() };
}
if (rnd() < 0.6) walls.splice(Math.floor(rnd() * (walls.length + 1)), 0, { ...p.walls[0], id: `new-${seed}-a` }, { ...p.walls[0], id: `new-${seed}-b` });
if (rnd() < 0.4) walls.reverse();
out = { ...out, walls };
if (rnd() < 0.5) out = { ...out, name: `n${seed}` };
if (rnd() < 0.3) out = { ...out, referenceElevationMasl: rnd() * 500 };
if (rnd() < 0.3) out = { ...out, geoAnchor: undefined };
if (rnd() < 0.5) out = { ...out, drawings2d: [] };
return out;
}
describe("ProjectDocument (Kern-Spiegel)", () => {
it("öffnet das Projekt unverändert", () => {
const d = new ProjectDocument(sampleProject);
expect(JSON.parse(d.json())).toEqual(plain(sampleProject));
d.close();
});
it("folgt beliebigen Änderungen über Ops", () => {
const d = new ProjectDocument(sampleProject);
let cur = sampleProject;
for (let seed = 1; seed <= 300; seed++) {
cur = mutate(cur, seed);
d.sync(cur);
const got = JSON.parse(d.json()) as Record<string, unknown>;
const want = plain(cur) as Record<string, unknown>;
const bad = [...new Set([...Object.keys(got), ...Object.keys(want)])].filter((k) => JSON.stringify(got[k]) !== JSON.stringify(want[k]));
expect(bad, `seed ${seed}`).toEqual([]);
}
d.close();
});
it("heilt sich bei doppelten ids (Neuöffnen)", () => {
const d = new ProjectDocument(sampleProject);
const dup = { ...sampleProject, walls: [...sampleProject.walls, sampleProject.walls[0]] };
d.sync(dup);
expect(JSON.parse(d.json())).toEqual(plain(dup));
d.close();
});
});
+119
View File
@@ -0,0 +1,119 @@
// Projekt-Dokument im Rust-Kern ([K4], Stufe S1): spiegelt das TS-Projekt über Ops nach Rust
// (`document.rs`). TS bleibt vorerst Quelle der Änderungen (`setProject` → `diffToOps`); Rust hält
// den Stand, auf dem die Ableitungen später direkt rechnen, ohne dass TS das Projekt je Aufruf
// flach neu sammeln muss. Bei jedem Widerspruch (abgelehnte Ops, Fehler) wird das Dokument aus dem
// vollen Projekt neu geöffnet — der Spiegel heilt sich selbst.
import type { Project } from "../model/types";
import { core } from "../engine/coreWasm";
import { diffToOps } from "./ops";
/** Abfragen des Kerns (`queries.rs`). */
export type QueryName =
| "kennzahlen"
| "siteCheck"
| "buildingFootprint"
| "modelTop"
| "shadows"
| "materialMaps"
| "slabs"
| "columnMeshes"
| "walls"
| "openingMeshes"
| "stairSlabs"
| "stairMeshes"
| "stairRailings"
| "roofMeshes"
| "roofCutLayers"
| "scheduleRows"
| "scheduleCsv"
| "pickGeometry"
| "model3dParts";
export class ProjectDocument {
private handle = 0;
private mirror: Project;
constructor(project: Project) {
this.mirror = project;
this.reopen(project);
}
/** Handle im Kern (0 = nicht offen). */
get id(): number {
return this.handle;
}
private reopen(project: Project): void {
if (this.handle) core().doc_close(this.handle);
this.handle = core().doc_open(JSON.stringify(project));
if (!this.handle) console.warn("Projekt-Dokument im Kern konnte nicht geöffnet werden.");
}
/** Bringt das Dokument auf den Stand `next` (über die Differenz als Ops). */
sync(next: Project): void {
if (next === this.mirror) return;
const ops = diffToOps(this.mirror, next);
this.mirror = next;
if (ops.length === 0) return;
if (!this.handle) {
this.reopen(next);
return;
}
const problems = core().doc_apply(this.handle, JSON.stringify(ops));
if (problems) {
console.warn("Kern-Dokument weicht ab, wird neu geöffnet:", problems);
this.reopen(next);
}
}
/**
* Abfrage auf dem typisierten Projekt im Kern (`queries.rs`); `args` Zahlen, `text` ein Text
* (z. B. eine id). Das Ergebnis ist JSON; Fehler im Kern (Projekt passt nicht zum Modell,
* Rechenfehler) werfen mit der Meldung des Kerns.
*/
query<T>(name: QueryName, args: number[] = [], text = ""): T {
return JSON.parse(this.queryJson(name, args, text)) as T;
}
/** Wie {@link query}, aber das JSON als Text (spart das Parsen, wenn es nur weitergereicht wird). */
queryJson(name: QueryName, args: number[] = [], text = ""): string {
if (!this.handle) throw new Error(`Kern-Abfrage „${name}": Dokument nicht offen`);
const out = core().doc_query_args(this.handle, name, Float64Array.from(args), text);
if (out.startsWith("\u0001")) throw new Error(out.slice(1));
return out;
}
/** Datei-Text des Projekts (Formatversion vorn, Einrückung 2). */
serialize(): string {
return this.handle ? core().doc_serialize(this.handle) : "";
}
/** Boden-Spuren der 2D-Zeichnungen als LineList-Vertices `[px, py, pz, r, g, b]`. */
drawingLineVerts(): Float32Array {
return this.handle ? core().doc_drawing_line_verts(this.handle) : new Float32Array(0);
}
/** Das Dokument als JSON-Text (Test, Export). */
json(): string {
return this.handle ? core().doc_json(this.handle) : "";
}
close(): void {
if (this.handle) core().doc_close(this.handle);
this.handle = 0;
}
}
let shared: ProjectDocument | null = null;
/**
* Das gemeinsame Kern-Dokument, auf den Stand von `project` gebracht. Der Abgleich läuft über die
* Differenz zum zuletzt gesehenen Projekt (Referenzvergleich, unveränderte Teile kosten nichts);
* wechselt ein Aufrufer zwischen sehr verschiedenen Projekten, ist es nur langsamer, nie falsch.
*/
export function documentFor(project: Project): ProjectDocument {
if (!shared) shared = new ProjectDocument(project);
else shared.sync(project);
return shared;
}