diff --git a/src-tauri/core/src/site_rules.rs b/src-tauri/core/src/site_rules.rs index df51244d..7f722ed9 100644 --- a/src-tauri/core/src/site_rules.rs +++ b/src-tauri/core/src/site_rules.rs @@ -260,7 +260,7 @@ use serde::Serialize; /// Mehrfach-Fläche mit `Vec2`-Punkten (JSON-Form wie `Area` in TS). pub type AreaOut = Vec>>; -fn area_out(polys: &[Polygon]) -> AreaOut { +pub fn area_out(polys: &[Polygon]) -> AreaOut { polys.iter().map(|p| p.iter().map(|r| r.iter().map(|q| Vec2 { x: q[0], y: q[1] }).collect()).collect()).collect() } diff --git a/src-tauri/core/src/sun.rs b/src-tauri/core/src/sun.rs index d4aa46f7..aee487e5 100644 --- a/src-tauri/core/src/sun.rs +++ b/src-tauri/core/src/sun.rs @@ -280,3 +280,65 @@ mod tests { assert_eq!(without_holes(&vec![square(0.0, 0.0, 3.0)]), vec![square(0.0, 0.0, 3.0)]); } } + +// ── Schatten aus dem Projekt-Dokument ([K4] S2) ───────────────────────────── + +use crate::jsmath::hypot2; +use crate::model::Project; + +/// Vereinigter Boden-Schatten aller Wände, freien Körper und Dächer des Projekts (leer bei Nacht). +/// Wände und Körper als Prismen, Dächer als Punktwolke (Traufe auf Traufhöhe, Traufe/First/Grate +/// auf Firsthöhe). +pub fn shadow_polygons_project(p: &Project, sun: SunPosition) -> Vec { + let mut prisms: Vec = Vec::new(); + for w in &p.walls { + let (z_bottom, z_top) = p.wall_vertical_extent(w); + let dx = w.end.x - w.start.x; + let dy = w.end.y - w.start.y; + let len = hypot2(dx, dy); + if len < 1e-9 { + continue; + } + let t = p.wall_thickness(w, 0.2) / 2.0; + let nx = (-dy / len) * t; + let ny = (dx / len) * t; + prisms.push(Prism { + footprint: vec![ + [w.start.x + nx, w.start.y + ny], + [w.end.x + nx, w.end.y + ny], + [w.end.x - nx, w.end.y - ny], + [w.start.x - nx, w.start.y - ny], + ], + z_bottom, + z_top, + }); + } + for s in p.extruded_solids.as_deref().unwrap_or(&[]) { + if s.operation.as_deref() == Some("subtract") { + continue; + } + let base = p.floor(&s.level_id).map_or(0.0, |f| f.base_elevation.unwrap_or(0.0)); + let z0 = base + s.base_offset.unwrap_or(0.0); + prisms.push(Prism { footprint: s.points.iter().map(|v| [v.x, v.y]).collect(), z_bottom: z0, z_top: z0 + s.height }); + } + let mut clouds: Vec> = Vec::new(); + for r in p.roofs.as_deref().unwrap_or(&[]) { + let eaves_z = p.roof_base_elevation(r); + let geo = crate::roof::roof_geometry(&r.params(), eaves_z); + let top = eaves_z + geo.ridge_height; + let mut pts: Vec<[f64; 3]> = Vec::new(); + for e in &geo.eaves { + pts.push([e[0], e[1], eaves_z]); + pts.push([e[0], e[1], top]); + } + for l in &geo.ridges { + pts.push([l[0][0], l[0][1], top]); + pts.push([l[1][0], l[1][1], top]); + } + for l in &geo.hips { + pts.push([l[1][0], l[1][1], top]); + } + clouds.push(pts); + } + shadow_union(&prisms, &clouds, sun) +} diff --git a/src-tauri/core/src/web/document.rs b/src-tauri/core/src/web/document.rs index a559a936..4fc4a1cb 100644 --- a/src-tauri/core/src/web/document.rs +++ b/src-tauri/core/src/web/document.rs @@ -67,7 +67,14 @@ pub fn doc_json(h: u32) -> String { /// Baurecht), `buildingFootprint`, `modelTop` (`null` ohne Bauteile). #[wasm_bindgen] pub fn doc_query(h: u32, name: &str) -> String { - use crate::{kennzahlen, site_rules}; + doc_query_args(h, name, &[]) +} + +/// Wie [`doc_query`] mit Zahlenargumenten. `shadows`: `[azimut, höhe]` → vereinigter Boden-Schatten. +#[wasm_bindgen] +pub fn doc_query_args(h: u32, name: &str, args: &[f64]) -> String { + use crate::{kennzahlen, site_rules, sun}; + let args = args.to_vec(); let name = name.to_owned(); with_doc(h, move |d| { let p = d.project().ok()?; @@ -76,6 +83,10 @@ pub fn doc_query(h: u32, name: &str) -> String { "siteCheck" => serde_json::to_string(&site_rules::check_project(&p)), "buildingFootprint" => serde_json::to_string(&site_rules::building_footprint(&p)), "modelTop" => serde_json::to_string(&site_rules::model_top(&p)), + "shadows" if args.len() == 2 => { + let polys = sun::shadow_polygons_project(&p, sun::SunPosition { azimuth: args[0], elevation: args[1] }); + serde_json::to_string(&site_rules::area_out(&polys)) + } _ => return None, }; json.ok() diff --git a/src/analysis/sun.ts b/src/analysis/sun.ts index 68ec4a65..c62489bf 100644 --- a/src/analysis/sun.ts +++ b/src/analysis/sun.ts @@ -5,16 +5,13 @@ // • `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. +// Gerechnet wird im Rust-Kern (`src-tauri/core/src/sun.rs`), die Schatten auf dem Projekt-Dokument. 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"; import { lv95ToWgs84 } from "../io/lv95"; -import { getFloor, getWallType, wallTypeThickness } from "../model/types"; +import { documentFor } from "../core/document"; export interface SunPosition { /** Grad von Nord im Uhrzeigersinn (Ost = 90, Süd = 180). */ @@ -47,64 +44,11 @@ export function shadowOfPrism(footprint: Vec2[], zBottom: number, zTop: number, export type ShadowPolygon = Vec2[][]; /** - * 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. + * Vereinigter Schatten aller Wände, Körper und Dächer (leer bei Nacht). Rechnet der Kern auf dem + * Projekt-Dokument (`sun.rs`): Wände und freie Körper als Prismen, Dächer als Punktwolke. */ export function shadowPolygons(project: Project, sun: SunPosition): ShadowPolygon[] { - 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; - const dy = w.end.y - w.start.y; - const len = Math.hypot(dx, dy); - if (len < 1e-9) continue; - const t = wallTypeThickness(getWallType(project, w)) / 2; - const nx = (-dy / len) * t; - const ny = (dx / len) * t; - 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 }, - ]); - nPrisms++; - } - for (const s of project.extrudedSolids ?? []) { - if (s.operation === "subtract") continue; - let base = 0; - try { - base = getFloor(project, s.levelId).baseElevation ?? 0; - } catch { - /* Geschoss fehlt → 0 */ - } - const z0 = base + (s.baseOffset ?? 0); - 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 top = eavesZ + geo.ridgeHeight; - 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 out = core().sun_shadow_union( - prisms.set(prismCount, nPrisms).done(), - clouds.set(cloudCount, nClouds).done(), - sun.azimuth, - sun.elevation, - ); - return new FlatReader(out).polygons(); + return documentFor(project).query("shadows", [sun.azimuth, sun.elevation]); } /** diff --git a/src/core/document.ts b/src/core/document.ts index d7e8825f..9df4ad03 100644 --- a/src/core/document.ts +++ b/src/core/document.ts @@ -49,8 +49,8 @@ export class ProjectDocument { * Abfrage auf dem typisierten Projekt im Kern. Das Ergebnis ist JSON; ein Fehler (Projekt passt * nicht zum Modell, unbekannte Abfrage) wirft. */ - query(name: "kennzahlen" | "siteCheck" | "buildingFootprint" | "modelTop"): T { - const text = this.handle ? core().doc_query(this.handle, name) : ""; + query(name: "kennzahlen" | "siteCheck" | "buildingFootprint" | "modelTop" | "shadows", args: number[] = []): T { + const text = this.handle ? core().doc_query_args(this.handle, name, Float64Array.from(args)) : ""; if (!text) throw new Error(`Kern-Abfrage „${name}": Projekt passt nicht zum Kern-Modell`); return JSON.parse(text) as T; }