[27] Punktwolken: Import-Dialog, Anzeige im 3D-Viewport, Gelände-Panel
Menü „Punktwolke importieren …“ und Drag & Drop (.las/.laz/.e57/.ply/.xyz/.pts), Dialog mit Voxel/Budget und Bericht; Lage über geoAnchor (LV95) bzw. Projekt-Nullpunkt mit Hinweis. Wolken im UI-Store (kein Projektfeld, nach Neuladen weg); Abgleich mit render3d über pointCloudSync. TODO [27] nachgeführt. Claude-Session: https://claude.ai/code/session_01NTYBZBvLYCtugMJoKXaB67
This commit is contained in:
18 files changed
+1090
-1
No files matched your search
@@ -0,0 +1,56 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { looksLikeLv95, placePointCloud } from "./pointCloudPlacement";
|
||||
|
||||
const lv95Cloud = {
|
||||
origin: [2_600_100, 1_200_050, 450] as [number, number, number],
|
||||
bboxMin: [2_600_090, 1_200_040, 440] as [number, number, number],
|
||||
bboxMax: [2_600_120, 1_200_070, 470] as [number, number, number],
|
||||
};
|
||||
|
||||
describe("placePointCloud", () => {
|
||||
it("passt eine LV95-Wolke über den Standort-Bezug ein (Höhe aus dem Anker)", () => {
|
||||
const p = placePointCloud({ ...lv95Cloud, epsg: 2056 }, { e: 2_600_000, n: 1_200_000 }, 445);
|
||||
expect(p.mode).toBe("georef");
|
||||
expect(p.offset).toEqual([100, 50, 5]);
|
||||
expect(p.heightFromAnchor).toBe(true);
|
||||
expect(p.epsgAssumed).toBe(false);
|
||||
});
|
||||
|
||||
it("ohne Ankerhöhe: tiefster Punkt auf 0", () => {
|
||||
const p = placePointCloud({ ...lv95Cloud, epsg: 2056 }, { e: 2_600_000, n: 1_200_000 });
|
||||
expect(p.offset).toEqual([100, 50, 10]);
|
||||
expect(p.heightFromAnchor).toBe(false);
|
||||
});
|
||||
|
||||
it("ohne EPSG, aber mit LV95-Koordinaten: LV95 angenommen", () => {
|
||||
const p = placePointCloud({ ...lv95Cloud, epsg: null }, { e: 2_600_000, n: 1_200_000 }, 440);
|
||||
expect(p.mode).toBe("georef");
|
||||
expect(p.epsgAssumed).toBe(true);
|
||||
});
|
||||
|
||||
it("anderes EPSG mit Bezug: nicht georeferenziert, sondern zentriert", () => {
|
||||
const p = placePointCloud({ ...lv95Cloud, epsg: 4326 }, { e: 2_600_000, n: 1_200_000 });
|
||||
expect(p.mode).toBe("centered");
|
||||
});
|
||||
|
||||
it("ohne Bezug: Landeskoordinaten werden mittig auf den Nullpunkt gelegt", () => {
|
||||
const p = placePointCloud({ ...lv95Cloud, epsg: 2056 }, null);
|
||||
expect(p.mode).toBe("centered");
|
||||
// Mitte (2 600 105 / 1 200 055) → 0, tiefster Punkt 440 → 0.
|
||||
expect(p.offset).toEqual([-5, -5, 10]);
|
||||
});
|
||||
|
||||
it("lokale Koordinaten bleiben wie in der Datei", () => {
|
||||
const p = placePointCloud(
|
||||
{ origin: [3, 4, 0], bboxMin: [-5, -5, -1], bboxMax: [12, 9, 6], epsg: null },
|
||||
{ e: 2_600_000, n: 1_200_000 },
|
||||
);
|
||||
expect(p.mode).toBe("local");
|
||||
expect(p.offset).toEqual([3, 4, 0]);
|
||||
});
|
||||
|
||||
it("erkennt LV95-Bereich", () => {
|
||||
expect(looksLikeLv95(lv95Cloud.bboxMin, lv95Cloud.bboxMax)).toBe(true);
|
||||
expect(looksLikeLv95([600_000, 200_000, 0], [600_100, 200_100, 0])).toBe(false); // LV03
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,68 @@
|
||||
// Lage einer importierten Punktwolke im Projekt ([27] Stufe 1). Rein rechnend, ohne UI.
|
||||
//
|
||||
// Der Leser liefert `xyz` (f32) relativ zu `origin` (f64, Dateikoordinaten). Hier entsteht der
|
||||
// Modellversatz dieses Ursprungs (f64, Modell-Meter x = Ost, y = Nord, z = Höhe); render3d bildet
|
||||
// `versatz + xyz` in f64 und rundet erst danach auf f32 (LV95-fest, s. `render3d/src/points.rs`).
|
||||
//
|
||||
// • Projekt hat einen Standort-Bezug (`geoAnchor`) und die Wolke liegt in LV95 (EPSG 2056, oder
|
||||
// ohne EPSG-Angabe mit LV95-typischen Koordinaten): lagerichtig einpassen. Höhe: mit
|
||||
// `geoAnchor.height` (m ü. M. des Bezugspunkts) absolut, sonst tiefster Punkt auf 0 + Hinweis.
|
||||
// • Sonst: Ursprung der Wolke = Projekt-Nullpunkt. Kleine (lokale) Koordinaten bleiben wie in
|
||||
// der Datei; grosse (Landeskoordinaten ohne Bezug) werden mit der Mitte auf den Nullpunkt und
|
||||
// dem tiefsten Punkt auf 0 gelegt. Immer mit Hinweis.
|
||||
|
||||
export type PointCloudPlacementMode = "georef" | "local" | "centered";
|
||||
|
||||
export interface PointCloudPlacement {
|
||||
/** Modell-Meter des Wolken-Ursprungs (x = Ost, y = Nord, z = Höhe); Punkt = offset + xyz. */
|
||||
offset: [number, number, number];
|
||||
mode: PointCloudPlacementMode;
|
||||
/** Höhe absolut übernommen (sonst tiefster Punkt auf 0 bzw. Dateihöhe). */
|
||||
heightFromAnchor: boolean;
|
||||
/** Ohne EPSG-Angabe wurde LV95 aus den Koordinaten erschlossen. */
|
||||
epsgAssumed: boolean;
|
||||
}
|
||||
|
||||
export interface PointCloudPlacementInput {
|
||||
origin: [number, number, number];
|
||||
bboxMin: [number, number, number];
|
||||
bboxMax: [number, number, number];
|
||||
epsg: number | null;
|
||||
}
|
||||
|
||||
/** LV95-Bereich der Schweiz (grosszügig): E 2,45–2,85 Mio., N 1,05–1,32 Mio. m. */
|
||||
export function looksLikeLv95(min: [number, number, number], max: [number, number, number]): boolean {
|
||||
return min[0] >= 2_450_000 && max[0] <= 2_850_000 && min[1] >= 1_050_000 && max[1] <= 1_320_000;
|
||||
}
|
||||
|
||||
/** Ab dieser Ausdehnung vom Nullpunkt gelten Koordinaten als „Landeskoordinaten" (nicht lokal). */
|
||||
const LOCAL_LIMIT_M = 50_000;
|
||||
|
||||
export function placePointCloud(
|
||||
cloud: PointCloudPlacementInput,
|
||||
geoOrigin: { e: number; n: number } | null,
|
||||
geoHeight?: number,
|
||||
): PointCloudPlacement {
|
||||
const [ox, oy, oz] = cloud.origin;
|
||||
const isLv95 = cloud.epsg === 2056;
|
||||
const assumeLv95 = cloud.epsg === null && looksLikeLv95(cloud.bboxMin, cloud.bboxMax);
|
||||
if (geoOrigin && (isLv95 || assumeLv95)) {
|
||||
const heightFromAnchor = geoHeight !== undefined && Number.isFinite(geoHeight);
|
||||
return {
|
||||
offset: [ox - geoOrigin.e, oy - geoOrigin.n, heightFromAnchor ? oz - geoHeight : oz - cloud.bboxMin[2]],
|
||||
mode: "georef",
|
||||
heightFromAnchor,
|
||||
epsgAssumed: assumeLv95,
|
||||
};
|
||||
}
|
||||
const far = [...cloud.bboxMin, ...cloud.bboxMax].some((v) => Math.abs(v) > LOCAL_LIMIT_M);
|
||||
if (!far) return { offset: [ox, oy, oz], mode: "local", heightFromAnchor: false, epsgAssumed: false };
|
||||
const cx = (cloud.bboxMin[0] + cloud.bboxMax[0]) / 2;
|
||||
const cy = (cloud.bboxMin[1] + cloud.bboxMax[1]) / 2;
|
||||
return {
|
||||
offset: [ox - cx, oy - cy, oz - cloud.bboxMin[2]],
|
||||
mode: "centered",
|
||||
heightFromAnchor: false,
|
||||
epsgAssumed: false,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
import { beforeEach, describe, expect, it } from "vitest";
|
||||
import type { PointCloudData, PointCloudReport } from "../io/pointCloud";
|
||||
import {
|
||||
addPointCloud,
|
||||
clearPointClouds,
|
||||
defaultColorMode,
|
||||
getPointClouds,
|
||||
removePointCloud,
|
||||
resetPointCloudStoreForTests,
|
||||
updatePointCloud,
|
||||
} from "./pointCloudStore";
|
||||
|
||||
function report(p: Partial<PointCloudReport> = {}): PointCloudReport {
|
||||
return {
|
||||
format: "las",
|
||||
version: "1.4",
|
||||
pointFormat: 3,
|
||||
pointsDeclared: 2,
|
||||
pointsRead: 2,
|
||||
pointsKept: 2,
|
||||
pointsInvalid: 0,
|
||||
voxel: 0,
|
||||
bboxMin: [0, 0, 0],
|
||||
bboxMax: [1, 1, 1],
|
||||
origin: [0, 0, 0],
|
||||
hasColor: false,
|
||||
hasIntensity: false,
|
||||
hasClassification: false,
|
||||
intensityRange: null,
|
||||
classCounts: [],
|
||||
epsg: null,
|
||||
truncated: false,
|
||||
warnings: [],
|
||||
...p,
|
||||
};
|
||||
}
|
||||
|
||||
function cloud(p: Partial<PointCloudReport> = {}): PointCloudData {
|
||||
return {
|
||||
origin: [0, 0, 0],
|
||||
xyz: new Float32Array(6),
|
||||
rgb: null,
|
||||
intensity: null,
|
||||
classification: null,
|
||||
report: report(p),
|
||||
};
|
||||
}
|
||||
|
||||
describe("pointCloudStore", () => {
|
||||
beforeEach(() => resetPointCloudStoreForTests());
|
||||
|
||||
it("wählt den Farbmodus nach vorhandenen Attributen", () => {
|
||||
expect(defaultColorMode(report({ hasColor: true, hasIntensity: true }))).toBe("rgb");
|
||||
expect(defaultColorMode(report({ hasIntensity: true }))).toBe("intensity");
|
||||
expect(defaultColorMode(report({ hasClassification: true }))).toBe("class");
|
||||
expect(defaultColorMode(report())).toBe("height");
|
||||
});
|
||||
|
||||
it("fügt hinzu, ändert und entfernt", () => {
|
||||
const a = addPointCloud("a.las", cloud({ hasColor: true }));
|
||||
const b = addPointCloud("b.xyz", cloud());
|
||||
expect(getPointClouds().map((c) => c.id)).toEqual([a, b]);
|
||||
expect(getPointClouds()[0]).toMatchObject({ visible: true, colorMode: "rgb", pointSize: 2 });
|
||||
|
||||
const before = getPointClouds();
|
||||
updatePointCloud(a, { visible: false, pointSize: 99 });
|
||||
expect(getPointClouds()).not.toBe(before); // neue Referenz → React rendert neu
|
||||
expect(getPointClouds()[0]).toMatchObject({ visible: false, pointSize: 10 });
|
||||
updatePointCloud(a, { pointSize: Number.NaN, colorMode: "quatsch" as never });
|
||||
expect(getPointClouds()[0]).toMatchObject({ pointSize: 10, colorMode: "rgb" });
|
||||
|
||||
removePointCloud(a);
|
||||
expect(getPointClouds().map((c) => c.id)).toEqual([b]);
|
||||
const same = getPointClouds();
|
||||
removePointCloud(12345);
|
||||
expect(getPointClouds()).toBe(same);
|
||||
clearPointClouds();
|
||||
expect(getPointClouds()).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,157 @@
|
||||
// Punktwolken im UI-Zustand ([27] Stufe 1). Bewusst KEIN `Project`-Feld: die Punkte (Millionen,
|
||||
// zig MB) gehören nicht ins .obp, und das Modellfeld (`Project.pointClouds`, Vorschlag in TODO.md
|
||||
// [27]) kommt erst mit dem Asset-Speicher. Bis dahin leben die Wolken nur in dieser Sitzung —
|
||||
// nach dem Neuladen sind sie weg (bekannt, s. TODO).
|
||||
//
|
||||
// Kleiner externer Store (Muster `busyStore.ts`, `useSyncExternalStore`), weil Viewport (Anzeige),
|
||||
// Lage-Panel (Liste/Einstellungen) und der Import-Dialog in getrennten Teilbäumen sitzen.
|
||||
|
||||
import { useSyncExternalStore } from "react";
|
||||
import type { PointCloudData, PointCloudReport } from "../io/pointCloud";
|
||||
|
||||
export type PointColorMode = "rgb" | "intensity" | "class" | "height";
|
||||
|
||||
export const POINT_COLOR_MODES: readonly PointColorMode[] = ["rgb", "intensity", "class", "height"];
|
||||
|
||||
export interface PointCloudEntry {
|
||||
/** Laufnummer (auch die id im Renderer). */
|
||||
id: number;
|
||||
name: string;
|
||||
data: PointCloudData;
|
||||
visible: boolean;
|
||||
colorMode: PointColorMode;
|
||||
/** Punktgrösse in Bildschirm-Pixeln. */
|
||||
pointSize: number;
|
||||
}
|
||||
|
||||
export const DEFAULT_POINT_SIZE = 2;
|
||||
export const MIN_POINT_SIZE = 1;
|
||||
export const MAX_POINT_SIZE = 10;
|
||||
|
||||
/** Ist der Farbmodus mit den Attributen der Wolke möglich? (Höhe immer.) */
|
||||
export function colorModeAvailable(report: PointCloudReport, mode: PointColorMode): boolean {
|
||||
if (mode === "rgb") return report.hasColor;
|
||||
if (mode === "intensity") return report.hasIntensity;
|
||||
if (mode === "class") return report.hasClassification;
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Voreinstellung: gemessene Farbe, sonst Intensität, sonst Klassen, sonst Höhe. */
|
||||
export function defaultColorMode(report: PointCloudReport): PointColorMode {
|
||||
return POINT_COLOR_MODES.find((m) => colorModeAvailable(report, m)) ?? "height";
|
||||
}
|
||||
|
||||
let clouds: readonly PointCloudEntry[] = [];
|
||||
let nextId = 1;
|
||||
const listeners = new Set<() => void>();
|
||||
|
||||
function emit(): void {
|
||||
for (const l of listeners) l();
|
||||
}
|
||||
|
||||
function subscribe(l: () => void): () => void {
|
||||
listeners.add(l);
|
||||
return () => listeners.delete(l);
|
||||
}
|
||||
|
||||
export function getPointClouds(): readonly PointCloudEntry[] {
|
||||
return clouds;
|
||||
}
|
||||
|
||||
/** Fügt eine gelesene Wolke hinzu (sichtbar, Voreinstellungen aus dem Bericht); liefert die id. */
|
||||
export function addPointCloud(name: string, data: PointCloudData): number {
|
||||
const id = nextId++;
|
||||
clouds = [
|
||||
...clouds,
|
||||
{ id, name, data, visible: true, colorMode: defaultColorMode(data.report), pointSize: DEFAULT_POINT_SIZE },
|
||||
];
|
||||
emit();
|
||||
return id;
|
||||
}
|
||||
|
||||
/** Ändert Sichtbarkeit/Farbmodus/Punktgrösse/Name (Unbekanntes wird ignoriert, Grösse geklemmt). */
|
||||
export function updatePointCloud(
|
||||
id: number,
|
||||
patch: Partial<Pick<PointCloudEntry, "visible" | "colorMode" | "pointSize" | "name">>,
|
||||
): void {
|
||||
let changed = false;
|
||||
clouds = clouds.map((c) => {
|
||||
if (c.id !== id) return c;
|
||||
changed = true;
|
||||
const next = { ...c, ...patch };
|
||||
if (!Number.isFinite(next.pointSize)) next.pointSize = c.pointSize;
|
||||
next.pointSize = Math.min(MAX_POINT_SIZE, Math.max(MIN_POINT_SIZE, next.pointSize));
|
||||
if (!POINT_COLOR_MODES.includes(next.colorMode)) next.colorMode = c.colorMode;
|
||||
return next;
|
||||
});
|
||||
if (changed) emit();
|
||||
}
|
||||
|
||||
export function removePointCloud(id: number): void {
|
||||
const next = clouds.filter((c) => c.id !== id);
|
||||
if (next.length === clouds.length) return;
|
||||
clouds = next;
|
||||
emit();
|
||||
}
|
||||
|
||||
export function clearPointClouds(): void {
|
||||
if (clouds.length === 0) return;
|
||||
clouds = [];
|
||||
emit();
|
||||
}
|
||||
|
||||
export function usePointClouds(): readonly PointCloudEntry[] {
|
||||
return useSyncExternalStore(subscribe, getPointClouds, getPointClouds);
|
||||
}
|
||||
|
||||
// ── Import-Anfrage (Menü, Drag & Drop, Panel-Knopf → Dialog) ────────────────────────────────
|
||||
|
||||
let pendingFile: File | null = null;
|
||||
const importListeners = new Set<() => void>();
|
||||
let fileInput: HTMLInputElement | null = null;
|
||||
|
||||
function emitImport(): void {
|
||||
for (const l of importListeners) l();
|
||||
}
|
||||
|
||||
/** Öffnet den Import-Dialog für `file` (Drag & Drop, Datei-Auswahl). */
|
||||
export function requestPointCloudImport(file: File): void {
|
||||
pendingFile = file;
|
||||
emitImport();
|
||||
}
|
||||
|
||||
/** Schliesst den Import-Dialog. */
|
||||
export function closePointCloudImport(): void {
|
||||
if (pendingFile === null) return;
|
||||
pendingFile = null;
|
||||
emitImport();
|
||||
}
|
||||
|
||||
export function usePointCloudImportRequest(): File | null {
|
||||
return useSyncExternalStore(
|
||||
(l) => {
|
||||
importListeners.add(l);
|
||||
return () => importListeners.delete(l);
|
||||
},
|
||||
() => pendingFile,
|
||||
() => pendingFile,
|
||||
);
|
||||
}
|
||||
|
||||
/** Der Dialog-Host meldet sein verstecktes Datei-Feld an (oder ab mit `null`). */
|
||||
export function registerPointCloudFileInput(el: HTMLInputElement | null): void {
|
||||
fileInput = el;
|
||||
}
|
||||
|
||||
/** Datei-Auswahl öffnen (Menü „Punktwolke importieren …"). Synchron im Klick, sonst blockt WebKit. */
|
||||
export function openPointCloudFileDialog(): void {
|
||||
fileInput?.click();
|
||||
}
|
||||
|
||||
/** Nur für Tests: Zustand zurücksetzen. */
|
||||
export function resetPointCloudStoreForTests(): void {
|
||||
clouds = [];
|
||||
nextId = 1;
|
||||
pendingFile = null;
|
||||
fileInput = null;
|
||||
}
|
||||
Reference in new issue
Block a user