Merge Analyse-Module nach Rust (Schatten, Baurecht, Aussenbemassung, Kennzahlen); TODO.md [K1]/[K2] nachgefuehrt, CLAUDE.md: hoechstens 2 Agents, Modell je Aufgabe
CLI-Gegenprobe identisch: Parzelle 320 m2, zulaessig 66 m2, Verstoss 2.296 m2, Sonnenhoehe auf 6 Stellen. Claude-Session: https://claude.ai/code/session_01NTYBZBvLYCtugMJoKXaB67
This commit is contained in:
19 files changed
+1548
-387
No files matched your search
+41
-21
@@ -5,11 +5,15 @@
|
||||
// ihr Volumen wird abgezogen.
|
||||
// • Ausnützungs-/Überbauungsziffer nur mit `project.parcelArea` oder der Parzellengrenze (`siteRules`).
|
||||
// • Raumprogramm (`project.roomProgram`, Soll) gegen die gezeichneten Räume.
|
||||
// Alles rein und ohne Wasm — exakt für Prismen/Kegelstümpfe/Kreise, Näherung sonst (Fasen/Rundungen).
|
||||
// Exakt für Prismen/Kegelstümpfe/Kreise, Näherung sonst (Fasen/Rundungen). Raumflächen je
|
||||
// Geschoss und Kategorie, Volumen und Parzellenfläche rechnet der Rust-Kern
|
||||
// (`src-tauri/core/src/kennzahlen.rs`); die Zusammenfassung nach SIA 416 (NF/NGF/GF) bleibt bei
|
||||
// `geometry/roomArea.ts::balance`, die auch Raumstempel, Bilanz, Raumliste und IFC nutzen.
|
||||
|
||||
import type { ExtrudedSolid, Project } from "../model/types";
|
||||
import { balance, polygonArea } from "../geometry/roomArea";
|
||||
import type { SiaCategory } from "../geometry/roomArea";
|
||||
import { SIA_CATEGORIES, balance, polygonArea } from "../geometry/roomArea";
|
||||
import { core } from "../engine/coreWasm";
|
||||
import { FlatWriter } from "../geometry/coreFlat";
|
||||
|
||||
export interface LevelKennzahlen {
|
||||
levelId: string;
|
||||
@@ -37,41 +41,57 @@ export interface Kennzahlen {
|
||||
|
||||
/** Volumen eines Extrusionskörpers (m³): Prisma, Kegel(stumpf) über `taper`, Kreis. */
|
||||
export function solidVolume(s: ExtrudedSolid): number {
|
||||
const area = s.circle ? Math.PI * s.circle.r * s.circle.r : polygonArea(s.points);
|
||||
const t = Math.min(1, Math.max(0, s.taper ?? 0));
|
||||
const k = 1 - t; // Massstab der Deckfläche
|
||||
return (area * s.height * (1 + k + k * k)) / 3;
|
||||
return core().kz_solid_volume(new FlatWriter().pts(s.circle ? [] : s.points).done(), s.circle ? s.circle.r : Number.NaN, s.height, s.taper ?? 0);
|
||||
}
|
||||
|
||||
const finite = (n: number): number => (Number.isFinite(n) ? n : 0);
|
||||
|
||||
/** Körper flach (`web/kennzahlen.rs`). */
|
||||
function encodeSolids(solids: ExtrudedSolid[]): Float64Array {
|
||||
const w = new FlatWriter().num(solids.length);
|
||||
for (const s of solids) {
|
||||
w.num(s.operation === "subtract" ? 1 : 0, s.circle ? s.circle.r : Number.NaN, s.height, s.taper ?? 0).ring(s.circle ? [] : s.points);
|
||||
}
|
||||
return w.done();
|
||||
}
|
||||
|
||||
export function computeKennzahlen(project: Project): Kennzahlen {
|
||||
const floors = project.drawingLevels
|
||||
.filter((l) => l.kind === "floor")
|
||||
.slice()
|
||||
.sort((a, b) => (a.baseElevation ?? 0) - (b.baseElevation ?? 0));
|
||||
const rooms = project.rooms ?? [];
|
||||
const levels: LevelKennzahlen[] = floors.map((l) => {
|
||||
const mine = rooms.filter((r) => r.floorId === l.id);
|
||||
const b = balance(mine.map((r) => ({ category: r.siaCategory as SiaCategory, area: finite(polygonArea(r.boundary)) })));
|
||||
return { levelId: l.id, name: l.name, gf: b.byKey.GF, agf: b.byKey.NGF, rooms: mine.length };
|
||||
const levelIndex = new Map<string, number>();
|
||||
floors.forEach((l, i) => levelIndex.has(l.id) || levelIndex.set(l.id, i));
|
||||
const rooms = (project.rooms ?? []).filter((r) => levelIndex.has(r.floorId));
|
||||
const rw = new FlatWriter().num(rooms.length);
|
||||
for (const r of rooms) {
|
||||
const k = SIA_CATEGORIES.indexOf(r.siaCategory);
|
||||
// Unbekannte Kategorie → Index ausserhalb, zählt im Kern nicht.
|
||||
rw.num(levelIndex.get(r.floorId)!, k < 0 ? SIA_CATEGORIES.length : k).ring(r.boundary);
|
||||
}
|
||||
const parcelRing = project.siteRules?.parcel ?? [];
|
||||
const sums = core().kz_area_sums(
|
||||
floors.length,
|
||||
rw.done(),
|
||||
encodeSolids(project.extrudedSolids ?? []),
|
||||
project.parcelArea ?? Number.NaN,
|
||||
new FlatWriter().pts(Array.isArray(parcelRing) ? parcelRing : []).done(),
|
||||
);
|
||||
const gv = sums[0];
|
||||
const parcel = Number.isNaN(sums[1]) ? null : sums[1];
|
||||
const levels: LevelKennzahlen[] = floors.map((l, i) => {
|
||||
// Kategorie-Summen als Einträge der SIA-416-Bilanz → GF und NGF.
|
||||
const b = balance(SIA_CATEGORIES.map((category, k) => ({ category, area: sums[2 + i * SIA_CATEGORIES.length + k] })));
|
||||
return { levelId: l.id, name: l.name, gf: b.byKey.GF, agf: b.byKey.NGF, rooms: rooms.filter((r) => r.floorId === l.id).length };
|
||||
});
|
||||
const gf = levels.reduce((s, l) => s + l.gf, 0);
|
||||
const agf = levels.reduce((s, l) => s + l.agf, 0);
|
||||
let gv = 0;
|
||||
for (const s of project.extrudedSolids ?? []) {
|
||||
const v = finite(solidVolume(s));
|
||||
gv += s.operation === "subtract" ? -v : v;
|
||||
}
|
||||
const footprint = levels.find((l) => l.gf > 0)?.gf ?? 0;
|
||||
// Parzellenfläche: eingegebene Zahl, sonst aus der Parzellengrenze des Baurechts.
|
||||
const fromRules = project.siteRules?.parcel && project.siteRules.parcel.length >= 3 ? finite(polygonArea(project.siteRules.parcel)) : 0;
|
||||
const parcel = project.parcelArea && project.parcelArea > 0 ? project.parcelArea : fromRules > 0 ? fromRules : null;
|
||||
return {
|
||||
levels,
|
||||
gf,
|
||||
agf,
|
||||
gv: Math.max(0, gv),
|
||||
gv,
|
||||
footprint,
|
||||
az: parcel ? agf / parcel : null,
|
||||
uez: parcel ? footprint / parcel : null,
|
||||
|
||||
@@ -38,6 +38,11 @@ describe("zulässige Grundfläche", () => {
|
||||
it("Uhrzeigersinn der Parzelle spielt keine Rolle", () => {
|
||||
expect(multiPolygonArea(allowedArea({ ...base, parcel: parcel.slice().reverse() }))).toBeCloseTo(22 * 12, 6);
|
||||
});
|
||||
it("einspringende Ecke: Abstand als Kreisbogen um die Ecke", () => {
|
||||
// L-Parzelle, 2 m rundum: scharfe Innen-L 156 m² plus (8..10)² ausserhalb des Kreises um (10, 10).
|
||||
const l = [{ x: 0, y: 0 }, { x: 20, y: 0 }, { x: 20, y: 10 }, { x: 10, y: 10 }, { x: 10, y: 20 }, { x: 0, y: 20 }];
|
||||
expect(multiPolygonArea(allowedArea({ parcel: l, defaultSetback: 2 }))).toBeCloseTo(160 - Math.PI, 3);
|
||||
});
|
||||
it("ungültige Parzelle → keine Fläche, kein Absturz", () => {
|
||||
expect(allowedArea({ parcel: rect(0, 0, 1, 1).slice(0, 2), defaultSetback: 1 })).toEqual([]);
|
||||
expect(multiPolygonArea(allowedArea({ parcel, defaultSetback: Number.NaN }))).toBeCloseTo(600, 6);
|
||||
|
||||
+48
-105
@@ -1,123 +1,65 @@
|
||||
// Baurecht der Parzelle ([63]): zulässige Grundfläche aus Grenzabständen und Baulinien, Prüfung
|
||||
// des Modells (Grundfläche ausserhalb, Gesamthöhe).
|
||||
//
|
||||
// • Grenzabstand: gesperrt ist alles, was näher als d_i an Kante i liegt (Streifen um die Kante
|
||||
// mit gerundeten Enden) — so stimmen auch konkave Parzellen und einspringende Ecken.
|
||||
// • Grenzabstand: gesperrt ist alles, was näher als d_i an Kante i liegt (Puffer der Kante mit
|
||||
// gerundeten Enden) — so stimmen auch konkave Parzellen und einspringende Ecken.
|
||||
// • Baulinie: Halbebene auf der Seite des Parzellen-Schwerpunkts.
|
||||
// • Gebäude-Grundfläche: Wände aller Geschosse und freie Körper (ohne Abzugskörper). Vorsprünge
|
||||
// (Dachüberstand, Balkone) und ihre Sonderregeln zählen nicht.
|
||||
// Gerechnet wird im Rust-Kern (`src-tauri/core/src/site_rules.rs`); hier wird aus dem Projekt
|
||||
// gesammelt (Grundflächen-Teile, Modellhöhe) und die Datenform übersetzt.
|
||||
|
||||
import * as polygonClipping from "../geometry/polygonBool";
|
||||
import type { MultiPolygon, Pair, Polygon as PcPolygon } from "../geometry/polygonBool";
|
||||
import type { Project, SiteRules, Vec2 } from "../model/types";
|
||||
import { getFloor, getWallType, wallTypeThickness } from "../model/types";
|
||||
import { wallVerticalExtent } from "../model/wall";
|
||||
import { wallCorners } from "../model/geometry";
|
||||
import { roofBaseElevation, roofGeometry } from "../geometry/roof";
|
||||
import { polygonArea } from "../geometry/roomArea";
|
||||
import { core } from "../engine/coreWasm";
|
||||
import { FlatReader, FlatWriter } from "../geometry/coreFlat";
|
||||
|
||||
/** Polygone mit Ringen: `[polygon][ring]`, Ring 0 aussen, weitere Löcher; Ringe offen. */
|
||||
export type Area = Vec2[][][];
|
||||
|
||||
const ring = (pts: Vec2[]): Pair[] => [...pts.map((p): Pair => [p.x, p.y]), [pts[0].x, pts[0].y]];
|
||||
const fromPc = (mp: MultiPolygon): Area => mp.map((poly) => poly.map((r) => r.slice(0, -1).map(([x, y]) => ({ x, y }))));
|
||||
const toPc = (a: Area): MultiPolygon => a.map((poly) => poly.map(ring));
|
||||
|
||||
/** Fläche einer Mehrfach-Fläche (Löcher abgezogen). */
|
||||
export function multiPolygonArea(a: Area): number {
|
||||
let s = 0;
|
||||
for (const poly of a) poly.forEach((r, i) => (s += (i === 0 ? 1 : -1) * polygonArea(r)));
|
||||
return s;
|
||||
return core().site_multi_polygon_area(new FlatWriter().polygons(a).done());
|
||||
}
|
||||
|
||||
/** Streifen um die Strecke a–b mit Halbbreite d und Halbkreis-Enden (Polygon-Näherung). */
|
||||
function capsule(a: Vec2, b: Vec2, d: number, segs = 12): Vec2[] {
|
||||
const ang = Math.atan2(b.y - a.y, b.x - a.x);
|
||||
const pts: Vec2[] = [];
|
||||
// Halbkreis um b (von rechts nach links der Richtung), dann um a; Polygon-Ecken AUSSERHALB
|
||||
// des Kreises (Radius / cos), damit der gesperrte Streifen nie zu schmal gerät.
|
||||
const r = d / Math.cos(Math.PI / (2 * segs));
|
||||
for (let i = 0; i <= segs; i++) {
|
||||
const t = ang - Math.PI / 2 + (Math.PI * i) / segs;
|
||||
const rr = i === 0 || i === segs ? d : r;
|
||||
pts.push({ x: b.x + rr * Math.cos(t), y: b.y + rr * Math.sin(t) });
|
||||
}
|
||||
for (let i = 0; i <= segs; i++) {
|
||||
const t = ang + Math.PI / 2 + (Math.PI * i) / segs;
|
||||
const rr = i === 0 || i === segs ? d : r;
|
||||
pts.push({ x: a.x + rr * Math.cos(t), y: a.y + rr * Math.sin(t) });
|
||||
}
|
||||
return pts;
|
||||
}
|
||||
|
||||
function centroid(pts: Vec2[]): Vec2 {
|
||||
let a = 0, cx = 0, cy = 0;
|
||||
for (let i = 0; i < pts.length; i++) {
|
||||
const p = pts[i], q = pts[(i + 1) % pts.length];
|
||||
const c = p.x * q.y - q.x * p.y;
|
||||
a += c; cx += (p.x + q.x) * c; cy += (p.y + q.y) * c;
|
||||
}
|
||||
if (Math.abs(a) < 1e-12) return pts[0];
|
||||
return { x: cx / (3 * a), y: cy / (3 * a) };
|
||||
}
|
||||
|
||||
/** Grosses Rechteck = Halbebene der Baulinie a–b auf der Seite von `inside`. */
|
||||
function halfPlane(a: Vec2, b: Vec2, inside: Vec2, size: number): PcPolygon | null {
|
||||
const dx = b.x - a.x, dy = b.y - a.y;
|
||||
const len = Math.hypot(dx, dy);
|
||||
if (!(len > 1e-9)) return null;
|
||||
const ux = dx / len, uy = dy / len;
|
||||
// Normale zur Innenseite.
|
||||
let nx = -uy, ny = ux;
|
||||
if ((inside.x - a.x) * nx + (inside.y - a.y) * ny < 0) { nx = -nx; ny = -ny; }
|
||||
const p0 = { x: a.x - ux * size, y: a.y - uy * size };
|
||||
const p1 = { x: a.x + ux * size, y: a.y + uy * size };
|
||||
return [ring([p0, p1, { x: p1.x + nx * size, y: p1.y + ny * size }, { x: p0.x + nx * size, y: p0.y + ny * size }])];
|
||||
}
|
||||
|
||||
const validParcel = (r: SiteRules) =>
|
||||
Array.isArray(r.parcel) && r.parcel.length >= 3 && r.parcel.every((p) => Number.isFinite(p.x) && Number.isFinite(p.y)) && polygonArea(r.parcel) > 1e-9;
|
||||
|
||||
/** Grenzabstand der Kante i (ungültig/negativ → 0). */
|
||||
export function setbackOf(r: SiteRules, i: number): number {
|
||||
const v = r.setbacks?.[i] ?? r.defaultSetback;
|
||||
return Number.isFinite(v) && v > 0 ? v : 0;
|
||||
/** Baurecht flach (`web/site_rules.rs`): Parzelle, Grenzabstand je Kante (Rohwert), Baulinien. */
|
||||
function encodeRules(r: SiteRules): Float64Array {
|
||||
const parcel = Array.isArray(r.parcel) ? r.parcel : [];
|
||||
const w = new FlatWriter().ring(parcel).num(parcel.length);
|
||||
for (let i = 0; i < parcel.length; i++) w.num(r.setbacks?.[i] ?? r.defaultSetback);
|
||||
const lines = r.buildingLines ?? [];
|
||||
w.num(lines.length);
|
||||
for (const l of lines) w.num(l.a.x, l.a.y, l.b.x, l.b.y);
|
||||
return w.done();
|
||||
}
|
||||
|
||||
/** Zulässige Grundfläche (leer bei ungültiger Parzelle). */
|
||||
export function allowedArea(r: SiteRules): Area {
|
||||
if (!validParcel(r)) return [];
|
||||
const n = r.parcel.length;
|
||||
let mp: MultiPolygon = [[ring(r.parcel)]];
|
||||
const strips: PcPolygon[] = [];
|
||||
for (let i = 0; i < n; i++) {
|
||||
const d = setbackOf(r, i);
|
||||
if (d > 0) strips.push([ring(capsule(r.parcel[i], r.parcel[(i + 1) % n], d))]);
|
||||
return new FlatReader(core().site_allowed_area(encodeRules(r))).polygons();
|
||||
}
|
||||
|
||||
/** Teile der Gebäude-Grundfläche (Wände aller Geschosse, freie Körper ohne Abzugskörper). */
|
||||
function footprintParts(project: Project): Area {
|
||||
const parts: Area = [];
|
||||
for (const w of project.walls) {
|
||||
if (Math.hypot(w.end.x - w.start.x, w.end.y - w.start.y) < 1e-9) continue;
|
||||
parts.push([wallCorners(w.start, w.end, wallTypeThickness(getWallType(project, w)))]);
|
||||
}
|
||||
if (strips.length > 0) mp = polygonClipping.difference(mp, ...strips);
|
||||
const c = centroid(r.parcel);
|
||||
const xs = r.parcel.map((p) => p.x), ys = r.parcel.map((p) => p.y);
|
||||
const size = 10 * (Math.max(...xs) - Math.min(...xs) + Math.max(...ys) - Math.min(...ys) + 1);
|
||||
for (const line of r.buildingLines ?? []) {
|
||||
const h = halfPlane(line.a, line.b, c, size);
|
||||
if (h && mp.length > 0) mp = polygonClipping.intersection(mp, h);
|
||||
for (const s of project.extrudedSolids ?? []) {
|
||||
if (s.operation === "subtract" || s.points.length < 3) continue;
|
||||
parts.push([s.points]);
|
||||
}
|
||||
return fromPc(mp);
|
||||
return parts;
|
||||
}
|
||||
|
||||
/** Grundfläche des Gebäudes (Wände aller Geschosse, freie Körper ohne Abzugskörper). */
|
||||
export function buildingFootprint(project: Project): Area {
|
||||
const parts: PcPolygon[] = [];
|
||||
for (const w of project.walls) {
|
||||
if (Math.hypot(w.end.x - w.start.x, w.end.y - w.start.y) < 1e-9) continue;
|
||||
parts.push([ring(wallCorners(w.start, w.end, wallTypeThickness(getWallType(project, w))))]);
|
||||
}
|
||||
for (const s of project.extrudedSolids ?? []) {
|
||||
if (s.operation === "subtract" || s.points.length < 3) continue;
|
||||
parts.push([ring(s.points)]);
|
||||
}
|
||||
const parts = footprintParts(project);
|
||||
if (parts.length === 0) return [];
|
||||
return fromPc(polygonClipping.union(parts[0], ...parts.slice(1)));
|
||||
return new FlatReader(core().poly_union(new FlatWriter().polygons(parts).done())).polygons();
|
||||
}
|
||||
|
||||
/** Höchster Punkt des Modells über ±0.00 (Wände, freie Körper, Dächer); `null` ohne Bauteile. */
|
||||
@@ -152,23 +94,24 @@ export interface SiteCheck {
|
||||
heightExceeded: boolean;
|
||||
}
|
||||
|
||||
/** Prüft das Modell gegen `project.siteRules`; `null` ohne (gültiges) Baurecht. */
|
||||
/** Prüft das Modell gegen `project.siteRules`; `null` ohne (gültiges) Baurecht. Rechnung im Rust-Kern (`site_rules.rs`). */
|
||||
export function checkSiteRules(project: Project): SiteCheck | null {
|
||||
const r = project.siteRules;
|
||||
if (!r || !validParcel(r)) return null;
|
||||
const allowed = allowedArea(r);
|
||||
const fp = buildingFootprint(project);
|
||||
const violations = fp.length === 0 ? [] : allowed.length === 0 ? fp : fromPc(polygonClipping.difference(toPc(fp), toPc(allowed)));
|
||||
// Kleinste Splitter (Rundung an gemeinsamen Kanten) nicht als Verstoss melden.
|
||||
const real = violations.filter((poly) => polygonArea(poly[0]) > 1e-4);
|
||||
if (!r) return null;
|
||||
const maxTop = modelTop(project);
|
||||
return {
|
||||
parcelArea: polygonArea(r.parcel),
|
||||
allowed,
|
||||
allowedArea: multiPolygonArea(allowed),
|
||||
violations: real,
|
||||
violationArea: multiPolygonArea(real),
|
||||
maxTop,
|
||||
heightExceeded: r.maxHeight !== undefined && maxTop !== null && maxTop > r.maxHeight + 1e-6,
|
||||
};
|
||||
const out = core().site_check(
|
||||
encodeRules(r),
|
||||
new FlatWriter().polygons(footprintParts(project)).done(),
|
||||
r.maxHeight ?? Number.NaN,
|
||||
maxTop ?? Number.NaN,
|
||||
);
|
||||
const rd = new FlatReader(out);
|
||||
if (rd.num() !== 1) return null;
|
||||
const parcelArea = rd.num();
|
||||
const allowedA = rd.num();
|
||||
const violationArea = rd.num();
|
||||
const heightExceeded = rd.num() === 1;
|
||||
const allowed = rd.polygons();
|
||||
const violations = rd.polygons();
|
||||
return { parcelArea, allowed, allowedArea: allowedA, violations, violationArea, maxTop, heightExceeded };
|
||||
}
|
||||
+40
-104
@@ -5,9 +5,11 @@
|
||||
// • `shadowOfPrism`/`shadowPolygons`: Boden-Schatten (z = 0) von Wänden, freien Körpern und
|
||||
// Dächern als Vereinigung konvexer Hüllen (Prisma ∪ verschobenes Prisma). Modell: x = Ost,
|
||||
// y = Nord, Höhen über dem Geländenullpunkt (Meter).
|
||||
// Gerechnet wird im Rust-Kern (`src-tauri/core/src/sun.rs`); hier wird nur aus dem Projekt
|
||||
// gesammelt und die Datenform übersetzt.
|
||||
|
||||
import * as polygonClipping from "../geometry/polygonBool";
|
||||
import type { Pair, Polygon as PcPolygon } from "../geometry/polygonBool";
|
||||
import { core } from "../engine/coreWasm";
|
||||
import { FlatReader, FlatWriter, pointsOf } from "../geometry/coreFlat";
|
||||
import type { Project, Vec2 } from "../model/types";
|
||||
import { wallVerticalExtent } from "../model/wall";
|
||||
import { roofBaseElevation, roofGeometry } from "../geometry/roof";
|
||||
@@ -21,25 +23,9 @@ export interface SunPosition {
|
||||
elevation: number;
|
||||
}
|
||||
|
||||
const RAD = Math.PI / 180;
|
||||
|
||||
export function sunPosition(latDeg: number, lonDeg: number, when: Date): SunPosition {
|
||||
const jd = when.getTime() / 86400000 + 2440587.5;
|
||||
const n = jd - 2451545.0;
|
||||
const L = (280.46 + 0.9856474 * n) % 360;
|
||||
const g = ((357.528 + 0.9856003 * n) % 360) * RAD;
|
||||
const lambda = (L + 1.915 * Math.sin(g) + 0.02 * Math.sin(2 * g)) * RAD;
|
||||
const eps = (23.439 - 0.0000004 * n) * RAD;
|
||||
const ra = Math.atan2(Math.cos(eps) * Math.sin(lambda), Math.cos(lambda));
|
||||
const dec = Math.asin(Math.sin(eps) * Math.sin(lambda));
|
||||
// Greenwich-Sternzeit → Stundenwinkel.
|
||||
const gmst = (280.46061837 + 360.98564736629 * n) % 360;
|
||||
const ha = (((gmst + lonDeg) * RAD - ra) % (2 * Math.PI) + 3 * Math.PI) % (2 * Math.PI) - Math.PI;
|
||||
const lat = latDeg * RAD;
|
||||
const sinEl = Math.sin(lat) * Math.sin(dec) + Math.cos(lat) * Math.cos(dec) * Math.cos(ha);
|
||||
const el = Math.asin(sinEl);
|
||||
const az = Math.atan2(-Math.sin(ha) * Math.cos(dec), Math.sin(dec) * Math.cos(lat) - Math.cos(dec) * Math.sin(lat) * Math.cos(ha));
|
||||
return { azimuth: (((az / RAD) % 360) + 360) % 360, elevation: el / RAD };
|
||||
const r = core().sun_position(latDeg, lonDeg, when.getTime());
|
||||
return { azimuth: r[0], elevation: r[1] };
|
||||
}
|
||||
|
||||
/** Breite/Länge des Projekts aus `geoAnchor` (LV95), sonst Zürich als Platzhalter. */
|
||||
@@ -52,50 +38,23 @@ export function projectLatLon(project: Project): { lat: number; lon: number } {
|
||||
return { lat: 47.3769, lon: 8.5417 };
|
||||
}
|
||||
|
||||
function hull(points: Vec2[]): Vec2[] {
|
||||
const p = points.slice().sort((a, b) => a.x - b.x || a.y - b.y);
|
||||
if (p.length < 3) return p;
|
||||
const cross = (o: Vec2, a: Vec2, b: Vec2) => (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
|
||||
const lower: Vec2[] = [];
|
||||
for (const q of p) {
|
||||
while (lower.length >= 2 && cross(lower[lower.length - 2], lower[lower.length - 1], q) <= 0) lower.pop();
|
||||
lower.push(q);
|
||||
}
|
||||
const upper: Vec2[] = [];
|
||||
for (const q of p.slice().reverse()) {
|
||||
while (upper.length >= 2 && cross(upper[upper.length - 2], upper[upper.length - 1], q) <= 0) upper.pop();
|
||||
upper.push(q);
|
||||
}
|
||||
return lower.slice(0, -1).concat(upper.slice(0, -1));
|
||||
}
|
||||
|
||||
/** Verschiebung des Schattens je Meter Höhe (Richtung von der Sonne weg). */
|
||||
function shadowShift(sun: SunPosition): Vec2 | null {
|
||||
if (sun.elevation <= 0.5) return null;
|
||||
const k = 1 / Math.tan(sun.elevation * RAD);
|
||||
const az = sun.azimuth * RAD;
|
||||
return { x: -Math.sin(az) * k, y: -Math.cos(az) * k };
|
||||
}
|
||||
|
||||
/** Boden-Schatten eines Prismas `footprint` von `zBottom` bis `zTop` (leer bei Nacht). */
|
||||
export function shadowOfPrism(footprint: Vec2[], zBottom: number, zTop: number, sun: SunPosition): Vec2[] {
|
||||
const k = shadowShift(sun);
|
||||
if (!k || zTop <= zBottom || footprint.length < 3) return [];
|
||||
const pts: Vec2[] = [];
|
||||
for (const p of footprint) {
|
||||
pts.push({ x: p.x + k.x * zBottom, y: p.y + k.y * zBottom });
|
||||
pts.push({ x: p.x + k.x * zTop, y: p.y + k.y * zTop });
|
||||
}
|
||||
return hull(pts);
|
||||
return pointsOf(core().sun_shadow_of_prism(new FlatWriter().pts(footprint).done(), zBottom, zTop, sun.azimuth, sun.elevation));
|
||||
}
|
||||
|
||||
/** Ein Schatten-Polygon: `[0]` Aussenring, weitere Ringe sind Löcher (Höfe). Ringe offen (ohne Wiederholung des Startpunkts). */
|
||||
export type ShadowPolygon = Vec2[][];
|
||||
|
||||
/** Vereinigter Schatten aller Wände, Körper und Dächer (leer bei Nacht). */
|
||||
/**
|
||||
* Vereinigter Schatten aller Wände, Körper und Dächer (leer bei Nacht). Hier werden nur die
|
||||
* Körper gesammelt — Wände und freie Körper als Prismen, Dächer als Punktwolke (Traufe auf
|
||||
* Traufhöhe, Traufe/First/Grate auf Firsthöhe); Schatten und Vereinigung rechnet der Rust-Kern.
|
||||
*/
|
||||
export function shadowPolygons(project: Project, sun: SunPosition): ShadowPolygon[] {
|
||||
if (shadowShift(sun) === null) return [];
|
||||
const parts: Vec2[][] = [];
|
||||
const prisms = new FlatWriter();
|
||||
const prismCount = prisms.slot();
|
||||
let nPrisms = 0;
|
||||
for (const w of project.walls) {
|
||||
const { zBottom, zTop } = wallVerticalExtent(project, w);
|
||||
const dx = w.end.x - w.start.x;
|
||||
@@ -105,13 +64,13 @@ export function shadowPolygons(project: Project, sun: SunPosition): ShadowPolygo
|
||||
const t = wallTypeThickness(getWallType(project, w)) / 2;
|
||||
const nx = (-dy / len) * t;
|
||||
const ny = (dx / len) * t;
|
||||
const fp = [
|
||||
prisms.num(zBottom, zTop).ring([
|
||||
{ x: w.start.x + nx, y: w.start.y + ny },
|
||||
{ x: w.end.x + nx, y: w.end.y + ny },
|
||||
{ x: w.end.x - nx, y: w.end.y - ny },
|
||||
{ x: w.start.x - nx, y: w.start.y - ny },
|
||||
];
|
||||
parts.push(shadowOfPrism(fp, zBottom, zTop, sun));
|
||||
]);
|
||||
nPrisms++;
|
||||
}
|
||||
for (const s of project.extrudedSolids ?? []) {
|
||||
if (s.operation === "subtract") continue;
|
||||
@@ -122,61 +81,38 @@ export function shadowPolygons(project: Project, sun: SunPosition): ShadowPolygo
|
||||
/* Geschoss fehlt → 0 */
|
||||
}
|
||||
const z0 = base + (s.baseOffset ?? 0);
|
||||
parts.push(shadowOfPrism(s.points, z0, z0 + s.height, sun));
|
||||
prisms.num(z0, z0 + s.height).ring(s.points);
|
||||
nPrisms++;
|
||||
}
|
||||
const clouds = new FlatWriter();
|
||||
const cloudCount = clouds.slot();
|
||||
let nClouds = 0;
|
||||
for (const r of project.roofs ?? []) {
|
||||
const eavesZ = roofBaseElevation(project, r);
|
||||
const geo = roofGeometry(r, eavesZ);
|
||||
const k = shadowShift(sun)!;
|
||||
const pts: Vec2[] = geo.eaves.map((p) => ({ x: p.x + k.x * eavesZ, y: p.y + k.y * eavesZ }));
|
||||
const top = eavesZ + geo.ridgeHeight;
|
||||
for (const [a, b] of geo.ridges) for (const p of [a, b]) pts.push({ x: p.x + k.x * top, y: p.y + k.y * top });
|
||||
for (const [, b] of geo.hips) pts.push({ x: b.x + k.x * top, y: b.y + k.y * top });
|
||||
// Traufkante auf Traufhöhe + Firstpunkte → Hülle (Dach ist konvex genug für die Studie).
|
||||
for (const p of geo.eaves) pts.push({ x: p.x + k.x * top, y: p.y + k.y * top });
|
||||
parts.push(hull(pts));
|
||||
const pts: number[] = [];
|
||||
for (const p of geo.eaves) pts.push(p.x, p.y, eavesZ, p.x, p.y, top);
|
||||
for (const [a, b] of geo.ridges) pts.push(a.x, a.y, top, b.x, b.y, top);
|
||||
for (const [, b] of geo.hips) pts.push(b.x, b.y, top);
|
||||
clouds.num(pts.length / 3, ...pts);
|
||||
nClouds++;
|
||||
}
|
||||
const polys: PcPolygon[] = parts
|
||||
.filter((p) => p.length >= 3)
|
||||
.map((p) => [[...p.map((q): Pair => [q.x, q.y]), [p[0].x, p[0].y] as Pair]]);
|
||||
if (polys.length === 0) return [];
|
||||
const union = polygonClipping.union(polys[0], ...polys.slice(1));
|
||||
return union.map((poly) => poly.map((ring) => ring.slice(0, -1).map(([x, y]) => ({ x, y }))));
|
||||
const out = core().sun_shadow_union(
|
||||
prisms.set(prismCount, nPrisms).done(),
|
||||
clouds.set(cloudCount, nClouds).done(),
|
||||
sun.azimuth,
|
||||
sun.elevation,
|
||||
);
|
||||
return new FlatReader(out).polygons();
|
||||
}
|
||||
|
||||
/**
|
||||
* Zerlegt ein Polygon mit Löchern in lochfreie Teile gleicher Gesamtfläche (für Ausgaben, die
|
||||
* nur einfache Polygone füllen, z. B. das Plan-Primitiv). Jedes Loch wird von einer senkrechten
|
||||
* Schnittlinie durch seinen Mittelpunkt geteilt; in den Streifen dazwischen berührt jeder
|
||||
* Lochrest den Streifenrand und ist damit kein Loch mehr.
|
||||
* nur einfache Polygone füllen, z. B. das Plan-Primitiv); Rechnung im Rust-Kern (`sun.rs`).
|
||||
*/
|
||||
export function withoutHoles(poly: ShadowPolygon): Vec2[][] {
|
||||
const outer = poly[0];
|
||||
if (!outer || outer.length < 3) return [];
|
||||
if (poly.length === 1) return [outer];
|
||||
const toPc = (ring: Vec2[]): Pair[] => [...ring.map((q): Pair => [q.x, q.y]), [ring[0].x, ring[0].y]];
|
||||
const pc: PcPolygon = poly.filter((r) => r.length >= 3).map(toPc);
|
||||
let minX = Infinity, maxX = -Infinity, minY = Infinity, maxY = -Infinity;
|
||||
for (const p of outer) {
|
||||
minX = Math.min(minX, p.x); maxX = Math.max(maxX, p.x);
|
||||
minY = Math.min(minY, p.y); maxY = Math.max(maxY, p.y);
|
||||
}
|
||||
const cuts = poly
|
||||
.slice(1)
|
||||
.filter((r) => r.length >= 3)
|
||||
.map((r) => {
|
||||
const xs = r.map((p) => p.x);
|
||||
return (Math.min(...xs) + Math.max(...xs)) / 2;
|
||||
})
|
||||
.sort((a, b) => a - b);
|
||||
const xs = [minX - 1, ...cuts, maxX + 1];
|
||||
const out: Vec2[][] = [];
|
||||
for (let i = 0; i + 1 < xs.length; i++) {
|
||||
if (xs[i + 1] - xs[i] < 1e-9) continue;
|
||||
const slab: PcPolygon = [[[xs[i], minY - 1], [xs[i + 1], minY - 1], [xs[i + 1], maxY + 1], [xs[i], maxY + 1], [xs[i], minY - 1]]];
|
||||
for (const piece of polygonClipping.intersection(pc, slab)) {
|
||||
if (piece[0].length >= 4) out.push(piece[0].slice(0, -1).map(([x, y]) => ({ x, y })));
|
||||
}
|
||||
}
|
||||
return out;
|
||||
if (!poly[0] || poly[0].length < 3) return [];
|
||||
if (poly.length === 1) return [poly[0]];
|
||||
return new FlatReader(core().sun_without_holes(new FlatWriter().polygons([poly]).done())).polygons().map((p) => p[0]);
|
||||
}
|
||||
+48
-151
@@ -8,11 +8,11 @@
|
||||
// schiefe Wände fehlen in den Ketten. Ergebnis sind gewöhnliche Bemassungen (Drawing2D), danach
|
||||
// frei bearbeitbar.
|
||||
|
||||
import * as polygonClipping from "./polygonBool";
|
||||
import type { Pair, Polygon as PcPolygon } from "./polygonBool";
|
||||
import type { Drawing2D, Project, Vec2, Wall } from "../model/types";
|
||||
import type { Drawing2D, Project, Wall } from "../model/types";
|
||||
import { getWallType, wallTypeThickness } from "../model/types";
|
||||
import { openingJambs } from "./opening";
|
||||
import { core } from "../engine/coreWasm";
|
||||
import { FlatReader, FlatWriter } from "./coreFlat";
|
||||
|
||||
export type DimSide = "south" | "east" | "north" | "west";
|
||||
export type DimChain = "openings" | "steps" | "total";
|
||||
@@ -23,85 +23,22 @@ export interface AutoDimension {
|
||||
drawing: Drawing2D;
|
||||
}
|
||||
|
||||
/** Aussennormalen der Seiten (Modell: x = Ost, y = Nord). */
|
||||
const SIDES: { side: DimSide; n: Vec2 }[] = [
|
||||
{ side: "south", n: { x: 0, y: -1 } },
|
||||
{ side: "east", n: { x: 1, y: 0 } },
|
||||
{ side: "north", n: { x: 0, y: 1 } },
|
||||
{ side: "west", n: { x: -1, y: 0 } },
|
||||
];
|
||||
/** Reihenfolge wie im Rust-Kern (`auto_dimensions.rs`: SIDES, CHAIN_*). */
|
||||
const SIDES: DimSide[] = ["south", "east", "north", "west"];
|
||||
const CHAINS: DimChain[] = ["openings", "steps", "total"];
|
||||
|
||||
const EPS = 1e-6;
|
||||
const dot = (a: Vec2, b: Vec2) => a.x * b.x + a.y * b.y;
|
||||
|
||||
/** Abstand der Ketten von der äussersten Kante in Papier-mm (1:100 → 1.0 / 1.7 / 2.4 m). */
|
||||
const CHAIN_MM = { openings: 10, steps: 17, total: 24 } as const;
|
||||
|
||||
function wallRect(w: Wall, t: number, extStart: number, extEnd: number): Vec2[] {
|
||||
const dx = w.end.x - w.start.x, dy = w.end.y - w.start.y;
|
||||
const len = Math.hypot(dx, dy);
|
||||
const ux = dx / len, uy = dy / len;
|
||||
const nx = -uy * (t / 2), ny = ux * (t / 2);
|
||||
const a = { x: w.start.x - ux * extStart, y: w.start.y - uy * extStart };
|
||||
const b = { x: w.end.x + ux * extEnd, y: w.end.y + uy * extEnd };
|
||||
return [
|
||||
{ x: a.x + nx, y: a.y + ny }, { x: b.x + nx, y: b.y + ny },
|
||||
{ x: b.x - nx, y: b.y - ny }, { x: a.x - nx, y: a.y - ny },
|
||||
];
|
||||
}
|
||||
|
||||
/** Liegt `p` auf einer anderen Wandachse (Ende oder dazwischen)? Dann ist das Wandende verbunden. */
|
||||
function connected(p: Vec2, self: Wall, walls: Wall[]): boolean {
|
||||
for (const o of walls) {
|
||||
if (o === self) continue;
|
||||
const dx = o.end.x - o.start.x, dy = o.end.y - o.start.y;
|
||||
const l2 = dx * dx + dy * dy;
|
||||
if (l2 < EPS) continue;
|
||||
const t = Math.max(0, Math.min(1, ((p.x - o.start.x) * dx + (p.y - o.start.y) * dy) / l2));
|
||||
if (Math.hypot(o.start.x + t * dx - p.x, o.start.y + t * dy - p.y) < 1e-3) return true;
|
||||
function thicknessOf(project: Project, w: Wall): number {
|
||||
try {
|
||||
return wallTypeThickness(getWallType(project, w));
|
||||
} catch {
|
||||
return 0.3; // verwaister Wandtyp
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Aussenringe der vereinigten Wand-Grundfläche (gegen den Uhrzeigersinn). */
|
||||
function outerRings(project: Project, walls: Wall[]): Vec2[][] {
|
||||
const parts: PcPolygon[] = [];
|
||||
for (const w of walls) {
|
||||
if (Math.hypot(w.end.x - w.start.x, w.end.y - w.start.y) < EPS) continue;
|
||||
let t = 0.3;
|
||||
try {
|
||||
t = wallTypeThickness(getWallType(project, w));
|
||||
} catch {
|
||||
/* verwaister Wandtyp */
|
||||
}
|
||||
const r = wallRect(w, t, connected(w.start, w, walls) ? t / 2 : 0, connected(w.end, w, walls) ? t / 2 : 0);
|
||||
parts.push([[...r.map((p): Pair => [p.x, p.y]), [r[0].x, r[0].y]]]);
|
||||
}
|
||||
if (parts.length === 0) return [];
|
||||
const rings: Vec2[][] = [];
|
||||
for (const poly of polygonClipping.union(parts[0], ...parts.slice(1))) {
|
||||
const ring = poly[0].slice(0, -1).map(([x, y]) => ({ x, y }));
|
||||
let a = 0;
|
||||
for (let i = 0; i < ring.length; i++) {
|
||||
const p = ring[i], q = ring[(i + 1) % ring.length];
|
||||
a += p.x * q.y - q.x * p.y;
|
||||
}
|
||||
rings.push(a < 0 ? ring.reverse() : ring);
|
||||
}
|
||||
return rings;
|
||||
}
|
||||
|
||||
/** Werte zusammenfassen, die näher als 1 mm beieinander liegen (sortiert). */
|
||||
function uniqueSorted(vals: number[]): number[] {
|
||||
const s = vals.slice().sort((a, b) => a - b);
|
||||
const out: number[] = [];
|
||||
for (const v of s) if (out.length === 0 || v - out[out.length - 1] > 1e-3) out.push(v);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Erzeugt die Aussenbemassung eines Geschosses. `scaleDenominator` bestimmt den Abstand der
|
||||
* Ketten (Papier-mm → Modell-Meter), `categoryCode` die Ebene der neuen Bemassungen.
|
||||
* Ketten (Papier-mm → Modell-Meter), `categoryCode` die Ebene der neuen Bemassungen. Die Ketten
|
||||
* rechnet der Rust-Kern aus Wandachsen/-dicken und Öffnungs-Leibungen.
|
||||
*/
|
||||
export function autoExteriorDimensions(
|
||||
project: Project,
|
||||
@@ -111,82 +48,42 @@ export function autoExteriorDimensions(
|
||||
idPrefix = "autodim",
|
||||
): AutoDimension[] {
|
||||
const walls = project.walls.filter((w) => w.floorId === floorId);
|
||||
const rings = outerRings(project, walls);
|
||||
if (rings.length === 0) return [];
|
||||
const toM = (mm: number) => (mm / 1000) * scaleDenominator;
|
||||
if (walls.length === 0) return [];
|
||||
const ww = new FlatWriter().num(walls.length);
|
||||
for (const w of walls) ww.num(w.start.x, w.start.y, w.end.x, w.end.y, thicknessOf(project, w));
|
||||
const index = new Map<string, number>();
|
||||
walls.forEach((w, i) => index.has(w.id) || index.set(w.id, i));
|
||||
const ow = new FlatWriter();
|
||||
const count = ow.slot();
|
||||
let nOpenings = 0;
|
||||
for (const o of project.openings ?? []) {
|
||||
const i = index.get(o.hostWallId);
|
||||
if (i === undefined) continue;
|
||||
const j = openingJambs(walls[i], o);
|
||||
if (!j) continue;
|
||||
ow.num(i, j.jambStart.x, j.jambStart.y, j.jambEnd.x, j.jambEnd.y);
|
||||
nOpenings++;
|
||||
}
|
||||
ow.set(count, nOpenings);
|
||||
|
||||
const rd = new FlatReader(core().autodim_chains(ww.done(), ow.done(), scaleDenominator));
|
||||
const out: AutoDimension[] = [];
|
||||
|
||||
for (const { side, n } of SIDES) {
|
||||
// Laufrichtung so, dass die linke Normale = Aussennormale ist (positiver Abstand = aussen).
|
||||
const u: Vec2 = { x: n.y, y: -n.x };
|
||||
let sMax = -Infinity, tMin = Infinity, tMax = -Infinity;
|
||||
const corners: number[] = [];
|
||||
// Kanten, die nach `n` schauen: Aussennormale (rechte Normale eines CCW-Rings) ≈ n.
|
||||
const faces: { s: number; t0: number; t1: number }[] = [];
|
||||
for (const ring of rings) {
|
||||
for (const p of ring) {
|
||||
sMax = Math.max(sMax, dot(p, n));
|
||||
tMin = Math.min(tMin, dot(p, u));
|
||||
tMax = Math.max(tMax, dot(p, u));
|
||||
}
|
||||
for (let i = 0; i < ring.length; i++) {
|
||||
const a = ring[i], b = ring[(i + 1) % ring.length];
|
||||
const dx = b.x - a.x, dy = b.y - a.y;
|
||||
const len = Math.hypot(dx, dy);
|
||||
if (len < EPS) continue;
|
||||
const outward = { x: dy / len, y: -dx / len };
|
||||
if (dot(outward, n) < 1 - 1e-6) continue; // nur achsparallele, nach n schauende Kanten
|
||||
const ta = dot(a, u), tb = dot(b, u);
|
||||
faces.push({ s: dot(a, n), t0: Math.min(ta, tb), t1: Math.max(ta, tb) });
|
||||
corners.push(ta, tb);
|
||||
}
|
||||
}
|
||||
if (faces.length === 0) continue;
|
||||
|
||||
// Öffnungen in Wänden, deren Aussenseite auf einer dieser Kanten liegt.
|
||||
const jambs: number[] = [];
|
||||
for (const o of project.openings ?? []) {
|
||||
const w = walls.find((x) => x.id === o.hostWallId);
|
||||
if (!w) continue;
|
||||
const dx = w.end.x - w.start.x, dy = w.end.y - w.start.y;
|
||||
const len = Math.hypot(dx, dy);
|
||||
if (len < EPS || Math.abs(dot({ x: dx / len, y: dy / len }, n)) > 1e-6) continue; // Wand quer zu n
|
||||
let t = 0.3;
|
||||
try {
|
||||
t = wallTypeThickness(getWallType(project, w));
|
||||
} catch {
|
||||
/* verwaister Wandtyp */
|
||||
}
|
||||
const outerS = dot(w.start, n) + t / 2;
|
||||
const j = openingJambs(w, o);
|
||||
if (!j) continue;
|
||||
const ja = dot(j.jambStart, u), jb = dot(j.jambEnd, u);
|
||||
const onFace = faces.some((f) => Math.abs(f.s - outerS) < 1e-3 && Math.min(ja, jb) >= f.t0 - 1e-3 && Math.max(ja, jb) <= f.t1 + 1e-3);
|
||||
if (onFace) jambs.push(ja, jb);
|
||||
}
|
||||
|
||||
const chains: { chain: DimChain; ts: number[] }[] = [];
|
||||
const total = [tMin, tMax];
|
||||
const steps = uniqueSorted(corners);
|
||||
if (jambs.length > 0) chains.push({ chain: "openings", ts: uniqueSorted([...steps, ...jambs]) });
|
||||
if (steps.length > 2) chains.push({ chain: "steps", ts: steps });
|
||||
chains.push({ chain: "total", ts: total });
|
||||
|
||||
for (const { chain, ts } of chains) {
|
||||
if (ts.length < 2) continue;
|
||||
const pts = ts.map((t) => ({ x: u.x * t + n.x * sMax, y: u.y * t + n.y * sMax }));
|
||||
out.push({
|
||||
side,
|
||||
chain,
|
||||
drawing: {
|
||||
id: `${idPrefix}-${floorId}-${side}-${chain}`,
|
||||
type: "drawing2d",
|
||||
levelId: floorId,
|
||||
categoryCode,
|
||||
geom: { shape: "dimension", pts, offset: toM(CHAIN_MM[chain]) },
|
||||
} as Drawing2D,
|
||||
});
|
||||
}
|
||||
for (let n = rd.count(); n > 0; n--) {
|
||||
const side = SIDES[rd.num()];
|
||||
const chain = CHAINS[rd.num()];
|
||||
const offset = rd.num();
|
||||
const pts = rd.ring();
|
||||
out.push({
|
||||
side,
|
||||
chain,
|
||||
drawing: {
|
||||
id: `${idPrefix}-${floorId}-${side}-${chain}`,
|
||||
type: "drawing2d",
|
||||
levelId: floorId,
|
||||
categoryCode,
|
||||
geom: { shape: "dimension", pts, offset },
|
||||
} as Drawing2D,
|
||||
});
|
||||
}
|
||||
return out;
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
// Flache Puffer für den Rust-Kern (`src-tauri/core`): Schreiben/Lesen von Zahlen, Punktringen und
|
||||
// Mehrfach-Polygonen (`flat.rs`: `[nPoly, (nRing, (nPkt, x, y, …)…)…]`, Ringe offen). Punkte als
|
||||
// `Vec2`; Aufbau je Kern-Funktion steht bei der Rust-Bindung (`src-tauri/core/src/web/*.rs`).
|
||||
|
||||
import type { Vec2 } from "../model/types";
|
||||
|
||||
/** Mehrfach-Polygon: `[polygon][ring]`, Ring 0 aussen, weitere Löcher; Ringe offen. */
|
||||
export type FlatPolygons = Vec2[][][];
|
||||
|
||||
export class FlatWriter {
|
||||
private buf: number[] = [];
|
||||
|
||||
num(...v: number[]): this {
|
||||
for (const x of v) this.buf.push(x);
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Platz für eine erst später bekannte Zahl (z. B. Anzahl); Wert mit `set` nachtragen. */
|
||||
slot(): number {
|
||||
this.buf.push(0);
|
||||
return this.buf.length - 1;
|
||||
}
|
||||
|
||||
set(slot: number, v: number): this {
|
||||
this.buf[slot] = v;
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Nur die Punkte: `x0, y0, x1, y1, …`. */
|
||||
pts(pts: readonly Vec2[]): this {
|
||||
for (const p of pts) this.buf.push(p.x, p.y);
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Anzahl, dann die Punkte. */
|
||||
ring(pts: readonly Vec2[]): this {
|
||||
this.buf.push(pts.length);
|
||||
return this.pts(pts);
|
||||
}
|
||||
|
||||
/** Mehrfach-Polygon in `flat`-Kodierung. */
|
||||
polygons(polys: FlatPolygons): this {
|
||||
this.buf.push(polys.length);
|
||||
for (const poly of polys) {
|
||||
this.buf.push(poly.length);
|
||||
for (const r of poly) this.ring(r);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
done(): Float64Array {
|
||||
return Float64Array.from(this.buf);
|
||||
}
|
||||
}
|
||||
|
||||
/** Liest Zahlen der Reihe nach; fehlende Werte → NaN bzw. Anzahl 0. */
|
||||
export class FlatReader {
|
||||
private i = 0;
|
||||
constructor(private readonly buf: Float64Array) {}
|
||||
|
||||
num(): number {
|
||||
return this.i < this.buf.length ? this.buf[this.i++] : Number.NaN;
|
||||
}
|
||||
|
||||
count(): number {
|
||||
const n = this.num();
|
||||
return Number.isInteger(n) && n > 0 ? n : 0;
|
||||
}
|
||||
|
||||
pts(n: number): Vec2[] {
|
||||
const out: Vec2[] = [];
|
||||
for (let k = 0; k < n; k++) out.push({ x: this.num(), y: this.num() });
|
||||
return out;
|
||||
}
|
||||
|
||||
ring(): Vec2[] {
|
||||
return this.pts(this.count());
|
||||
}
|
||||
|
||||
polygons(): FlatPolygons {
|
||||
const out: FlatPolygons = [];
|
||||
for (let p = this.count(); p > 0; p--) {
|
||||
const poly: Vec2[][] = [];
|
||||
for (let r = this.count(); r > 0; r--) poly.push(this.ring());
|
||||
out.push(poly);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
/** `x0, y0, x1, y1, …` → Punkte. */
|
||||
export function pointsOf(buf: Float64Array): Vec2[] {
|
||||
return new FlatReader(buf).pts(buf.length >> 1);
|
||||
}
|
||||
Reference in new issue
Block a user