[58] projectToModel3d über Änderungs-Antworten: TS liest nur neue Wand-Einträge, Bench kalt/unverändert/nach Änderung
This commit is contained in:
4 files changed
+167
-8
No files matched your search
+14
-4
@@ -850,7 +850,16 @@ async function execute(project: Project, file: string, command: string, rest: st
|
||||
await time("planToRenderScene", () => planToRenderScene(plan));
|
||||
await time("planToSvgString", () => planToSvgString(plan, { scaleDenominator: scale }));
|
||||
}
|
||||
await time("projectToModel3d", () => projectToModel3d(project));
|
||||
{
|
||||
// Erstes Mal auf einem frischen Dokument (ohne Cache, [58]); die Zeile darunter misst die Wiederholung.
|
||||
const cold = structuredClone(project);
|
||||
withDocument(cold, () => {
|
||||
const t0 = performance.now();
|
||||
projectToModel3d(cold);
|
||||
rows.push(["projectToModel3d (kalt)", performance.now() - t0]);
|
||||
});
|
||||
}
|
||||
await time("projectToModel3d (unverändert)", () => projectToModel3d(project));
|
||||
if (sec) await time("Schnitt (Kern)", async () => generateSectionPlan((await computeSection(project, sec))!, false, { visibleCodes: allCodes }), Math.max(3, runs / 2));
|
||||
if (elev) await time("generateElevationPlan", () => generateElevationPlan(project, elev), Math.max(3, runs / 2));
|
||||
if (project.walls.length) {
|
||||
@@ -888,11 +897,12 @@ async function execute(project: Project, file: string, command: string, rest: st
|
||||
const r = doc.commit(cur, opsFor({ ...cur, walls: cur.walls.slice(0, 1) } as Project, 0.01 * ++k).slice(0, 1), undefined, Date.now());
|
||||
if ("project" in r) cur = r.project;
|
||||
}, runs * 5);
|
||||
// [58] Inkrementelle 3D-Ableitung: eine Änderung, dann das ganze 3D-Modell neu abfragen.
|
||||
// [58] Inkrementelle 3D-Ableitung (Cache im Dokument, Änderungs-Antworten): eine Änderung, dann das ganze
|
||||
// 3D-Modell neu abfragen; zum Vergleich die volle Abfrage ohne Cache-Weg in TS.
|
||||
lap("projectToModel3d (Dokument)", () => projectToModel3d(cur));
|
||||
lap(" davon Kern (model3dParts)", () => doc.queryJson("model3dParts", [1, 2, 0, 0, 0]));
|
||||
lap(" model3dParts voll: Kern (JSON)", () => doc.queryJson("model3dParts", [1, 2, 0, 0, 0]));
|
||||
const partsJson = doc.queryJson("model3dParts", [1, 2, 0, 0, 0]);
|
||||
lap(" davon JSON.parse", () => JSON.parse(partsJson));
|
||||
lap(" model3dParts voll: JSON.parse", () => JSON.parse(partsJson));
|
||||
lap("projectToModel3d nach 1 Wand-Änderung", () => {
|
||||
const w = cur.walls[++k % cur.walls.length];
|
||||
const r = doc.commit(cur, patchItem(cur, "walls", w.id, { height: (w.height ?? 2.6) + (k % 2 ? 0.01 : -0.01) }), undefined, Date.now());
|
||||
|
||||
@@ -50,6 +50,51 @@ export interface IfcFlatInput {
|
||||
elementData?: Float64Array;
|
||||
}
|
||||
|
||||
/** Änderungs-Antwort des 3D-Modells (Kern `model3d_cache::write_delta`). */
|
||||
interface Model3dDelta {
|
||||
mark: number;
|
||||
wallKeys: number[];
|
||||
wallNew: [number, unknown[]][];
|
||||
openingKeys: number[];
|
||||
openingNew: [number, unknown[], unknown[], unknown[], unknown[], unknown[]][];
|
||||
openingTail: unknown[];
|
||||
[rest: string]: unknown;
|
||||
}
|
||||
|
||||
/** Gelesene Einträge der letzten Antwort (Laufnummer → Wandkörper bzw. 5 Listen Einbauteile). */
|
||||
interface Model3dDeltaCache {
|
||||
mark: number;
|
||||
walls: Map<number, unknown[]>;
|
||||
openings: Map<number, unknown[][]>;
|
||||
}
|
||||
|
||||
/** Setzt die Teile aus Antwort und gehaltenen Einträgen zusammen; `null`, wenn ein Eintrag fehlt. */
|
||||
function assembleModel3dDelta(r: Model3dDelta, cache: Model3dDeltaCache | undefined): { parts: Record<string, unknown>; cache: Model3dDeltaCache } | null {
|
||||
const wallNew = new Map(r.wallNew);
|
||||
const walls: unknown[] = [];
|
||||
const wallMap = new Map<number, unknown[]>();
|
||||
for (const k of r.wallKeys) {
|
||||
const e = wallNew.get(k) ?? cache?.walls.get(k);
|
||||
if (!e) return null;
|
||||
for (const x of e) walls.push(x);
|
||||
wallMap.set(k, e);
|
||||
}
|
||||
const openNew = new Map(r.openingNew.map(([k, ...lists]) => [k, lists] as const));
|
||||
const openMap = new Map<number, unknown[][]>();
|
||||
const entries: unknown[][][] = [];
|
||||
for (const k of r.openingKeys) {
|
||||
const e = openNew.get(k) ?? cache?.openings.get(k);
|
||||
if (!e) return null;
|
||||
entries.push(e);
|
||||
openMap.set(k, e);
|
||||
}
|
||||
const openingMeshes: unknown[] = [];
|
||||
for (let l = 0; l < 5; l++) for (const e of entries) for (const x of e[l]) openingMeshes.push(x);
|
||||
for (const x of r.openingTail) openingMeshes.push(x);
|
||||
const { mark, wallKeys: _wk, wallNew: _wn, openingKeys: _ok, openingNew: _on, openingTail: _ot, ...rest } = r;
|
||||
return { parts: { ...rest, walls, openingMeshes }, cache: { mark, walls: wallMap, openings: openMap } };
|
||||
}
|
||||
|
||||
/** Ergebnis des Ladens einer Projektdatei ({@link ProjectDocument.loadFile}). */
|
||||
export type LoadFileResult =
|
||||
| { ok: true; project: Project; warnings: string[]; migratedFrom: number | null }
|
||||
@@ -93,6 +138,7 @@ export type QueryName =
|
||||
| "pickGeometry"
|
||||
| "explodeOffsets"
|
||||
| "model3dParts"
|
||||
| "model3dPartsDelta"
|
||||
| "projectSolidSpecs"
|
||||
| "planDomains"
|
||||
| "ifcExportData"
|
||||
@@ -118,6 +164,8 @@ export class ProjectDocument {
|
||||
private limits: [number, number] | null = null;
|
||||
/** Stände grosser Verlaufsschritte (Undo/Redo ohne Ops-Rückgabe, s. `stepStates.ts`). */
|
||||
private steps = new StepStates();
|
||||
/** Gelieferte Einträge des 3D-Modells je Argument-Satz ([58], {@link model3dParts}). */
|
||||
private m3d = new Map<string, Model3dDeltaCache>();
|
||||
|
||||
constructor(project: Project) {
|
||||
this.mirror = project;
|
||||
@@ -137,6 +185,7 @@ export class ProjectDocument {
|
||||
* Änderung, deren Aufruf den Panic auslöste, war noch nicht übernommen). Der Verlauf beginnt leer.
|
||||
*/
|
||||
restoreAfterRestart(): void {
|
||||
this.m3d.clear();
|
||||
const wasOpen = this.handle !== 0;
|
||||
this.handle = 0;
|
||||
this.steps.clear();
|
||||
@@ -144,6 +193,7 @@ export class ProjectDocument {
|
||||
}
|
||||
|
||||
private reopen(project: Project): void {
|
||||
this.m3d.clear();
|
||||
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.");
|
||||
@@ -159,6 +209,32 @@ export class ProjectDocument {
|
||||
return JSON.parse(this.queryJson(name, args, text)) as T;
|
||||
}
|
||||
|
||||
/**
|
||||
* Teile des 3D-Modells (`model3dParts` ohne Explosion, `args` = deren 5 Zahlen) über Änderungs-Antworten
|
||||
* ([58], Kern `model3d_cache.rs`): der Kern schreibt nur die Wände/Öffnungs-Einbauteile aus, die seit der
|
||||
* letzten Antwort für dieselben Argumente neu sind; die übrigen kommen aus den hier gehaltenen, schon
|
||||
* gelesenen Einträgen. Ergebnis wie `query("model3dParts", args)`. Die Einträge werden zwischen den
|
||||
* Ergebnissen geteilt — nicht verändern.
|
||||
*/
|
||||
model3dParts<T>(args: number[]): T {
|
||||
const a = [0, 0, 0, 0, 0].map((d, i) => args[i] ?? d);
|
||||
const key = a.join(",");
|
||||
let cache = this.m3d.get(key);
|
||||
for (let attempt = 0; attempt < 2; attempt++) {
|
||||
const r = JSON.parse(this.queryJson("model3dPartsDelta", [...a, cache?.mark ?? 0])) as Model3dDelta;
|
||||
const out = assembleModel3dDelta(r, cache);
|
||||
if (out) {
|
||||
this.m3d.delete(key);
|
||||
this.m3d.set(key, out.cache);
|
||||
if (this.m3d.size > 4) this.m3d.delete(this.m3d.keys().next().value!);
|
||||
return out.parts as T;
|
||||
}
|
||||
// Ein Eintrag fehlt (sollte nicht vorkommen): einmal alles neu anfordern.
|
||||
cache = undefined;
|
||||
}
|
||||
throw new Error("Kern-Abfrage „model3dPartsDelta“: Einträge fehlen");
|
||||
}
|
||||
|
||||
/** 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`);
|
||||
@@ -194,6 +270,7 @@ export class ProjectDocument {
|
||||
load(project: Project): void {
|
||||
this.mirror = project;
|
||||
this.steps.clear();
|
||||
this.m3d.clear();
|
||||
if (!this.handle || !core().doc_load_json(this.handle, JSON.stringify(project))) this.reopen(project);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* [58] Inkrementelle 3D-Ableitung: `ProjectDocument.model3dParts` (Änderungs-Antworten, nur neue Einträge
|
||||
* je Wand) ergibt nach Änderungen, Undo und Redo dasselbe wie die volle Abfrage `model3dParts`.
|
||||
*/
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { sampleProject } from "../model/sampleProject";
|
||||
import type { Project } from "../model/types";
|
||||
import { withDocument } from "../core/document";
|
||||
import { addItems, patchItem, removeItems } from "../core/opsBuild";
|
||||
import { projectToModel3d } from "./toWalls3d";
|
||||
|
||||
const ARGS = [
|
||||
[1, 2, 0, 0, 0],
|
||||
[1, 0, 0, 0, 0],
|
||||
[0, 1, 1, 0, 0],
|
||||
];
|
||||
|
||||
describe("3D-Modell inkrementell", () => {
|
||||
it("gleich wie voll nach Änderungen, Undo und Redo", () => {
|
||||
withDocument(sampleProject, (doc) => {
|
||||
let cur: Project = sampleProject;
|
||||
const check = (step: string) => {
|
||||
for (const a of ARGS) {
|
||||
expect(doc.model3dParts(a), `${step} ${a}`).toEqual(JSON.parse(doc.queryJson("model3dParts", a)));
|
||||
}
|
||||
};
|
||||
const commit = (ops: ReturnType<typeof patchItem>) => {
|
||||
const r = doc.commit(cur, ops, undefined, Date.now());
|
||||
if ("project" in r) cur = r.project;
|
||||
else throw new Error(r.problems.join("; "));
|
||||
};
|
||||
check("Start");
|
||||
const openings = cur.openings ?? [];
|
||||
expect(openings.length).toBeGreaterThan(0);
|
||||
for (let i = 0; i < 12; i++) {
|
||||
const w = cur.walls[(i * 5) % cur.walls.length];
|
||||
switch (i % 6) {
|
||||
case 0:
|
||||
commit(patchItem(cur, "walls", w.id, { height: (w.height ?? 2.6) + 0.1 }));
|
||||
break;
|
||||
case 1:
|
||||
commit(patchItem(cur, "walls", w.id, { end: { x: w.end.x + 0.05, y: w.end.y } }));
|
||||
break;
|
||||
case 2: {
|
||||
const o = (cur.openings ?? [])[i % openings.length];
|
||||
commit(patchItem(cur, "openings", o.id, { width: o.width * 0.9 }));
|
||||
break;
|
||||
}
|
||||
case 3:
|
||||
commit(addItems("walls", [{ ...w, id: `neu${i}`, start: { x: w.start.x, y: w.start.y + 0.4 } }]));
|
||||
break;
|
||||
case 4:
|
||||
commit(removeItems(cur, "walls", [w.id]));
|
||||
break;
|
||||
default: {
|
||||
const u = doc.undo(cur);
|
||||
if (u) cur = u;
|
||||
check(`Undo ${i}`);
|
||||
const r = doc.redo(cur);
|
||||
if (r) cur = r;
|
||||
}
|
||||
}
|
||||
check(`Schritt ${i}`);
|
||||
}
|
||||
// Die Fassade liefert dasselbe Modell wie ein frisch geöffnetes Dokument.
|
||||
const inc = JSON.stringify(projectToModel3d(cur));
|
||||
const copy = structuredClone(cur);
|
||||
const fresh = withDocument(copy, () => JSON.stringify(projectToModel3d(copy)));
|
||||
expect(inc).toBe(fresh);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -169,10 +169,10 @@ export function projectToModel3d(project: Project, opts: Model3dOptions = {}): R
|
||||
// Mit `voids` liefert der Kern zusätzlich die getroffenen Wände/Decken als truck-Spezifikationen.
|
||||
const explode = explodeGap(opts.explode);
|
||||
const voids = !!opts.voids && typeof window !== "undefined";
|
||||
const parts = documentFor(project).query<Model3dParts>(
|
||||
"model3dParts",
|
||||
[layered ? 1 : 0, DETAIL_CODE[detail] ?? 3, opts.forSection ? 1 : 0, voids ? 1 : 0, explode],
|
||||
);
|
||||
const args = [layered ? 1 : 0, DETAIL_CODE[detail] ?? 3, opts.forSection ? 1 : 0, voids ? 1 : 0, explode];
|
||||
// Ohne Explosion inkrementell ([58]): nur geänderte Wände reisen aus dem Kern und werden gelesen.
|
||||
const doc = documentFor(project);
|
||||
const parts = explode > 0 ? doc.query<Model3dParts>("model3dParts", args) : doc.model3dParts<Model3dParts>(args);
|
||||
// Die truck-Körper (asynchron, mit Cache) kommen zwischen Kontext-Meshes und Stützen.
|
||||
const meshes: RMesh[] = [...parts.contextMeshes];
|
||||
for (const m of emitExtrudedSolids(project, parts.floorOffsets)) meshes.push(m);
|
||||
|
||||
Reference in new issue
Block a user