[63] Merge: Baurecht – Vorsprünge und zulässige Hülle im 3D

This commit is contained in:
Claude committed 2026-11-05 01:59:28 +00:00
commit 6d9cd803a6
16 files changed
+836 -10

No files matched your search

+25
View File
@@ -1984,6 +1984,21 @@ pub struct SiteRules {
#[serde(default, skip_serializing_if = "Option::is_none")]
#[ts(optional)]
pub show: Option<bool>,
/// Zulässige Hülle (zulässige Fläche bis zur Gesamthöhe) im 3D durchscheinend zeigen (Default: nein).
#[serde(rename = "show3d", default, skip_serializing_if = "Option::is_none")]
#[ts(optional)]
pub show3d: Option<bool>,
/// Vorsprünge ([63]): zulässige Tiefe (m) in den Grenzabstand bzw. über die Baulinie für
/// Dachüberstände und als Vorsprung markierte Bauteile (`siteProjection`). Fehlt ⇒ keine
/// Vorsprungsregel (Dächer und Decken werden nicht geprüft, wie bisher).
#[serde(rename = "projectionDepth", default, skip_serializing_if = "Option::is_none")]
#[ts(optional)]
pub projection_depth: Option<f64>,
/// Vorsprünge ([63]): höchster Anteil (0..1) der Fassadenlänge, den markierte Vorsprünge
/// (Balkone, Erker; nicht Dachüberstände) je Grenze/Baulinie einnehmen dürfen. Fehlt ⇒ unbegrenzt.
#[serde(rename = "projectionShare", default, skip_serializing_if = "Option::is_none")]
#[ts(optional)]
pub projection_share: Option<f64>,
#[serde(flatten)]
#[ts(skip)]
pub extra: Extra,
@@ -2276,6 +2291,11 @@ pub struct Ceiling {
/// Grafik-Kategorie (Ebene), z. B. "30" für Decken.
#[serde(rename = "categoryCode")]
pub category_code: String,
/// Vorsprung im Baurecht ([63]: Balkon, Erker): darf mit `SiteRules.projectionDepth`/
/// `projectionShare` in den Grenzabstand ragen. Fehlt ⇒ normales Bauteil.
#[serde(rename = "siteProjection", default, skip_serializing_if = "Option::is_none")]
#[ts(optional)]
pub site_projection: Option<bool>,
/// Objekt-Sperre, analog `Wall.locked`.
#[serde(default, skip_serializing_if = "Option::is_none")]
#[ts(optional)]
@@ -3596,6 +3616,11 @@ pub struct ExtrudedSolid {
#[serde(default, skip_serializing_if = "Option::is_none")]
#[ts(optional, type = "\"subtract\"")]
pub operation: Option<String>,
/// Vorsprung im Baurecht ([63]: Balkon, Erker): darf mit `SiteRules.projectionDepth`/
/// `projectionShare` in den Grenzabstand ragen. Fehlt ⇒ normales Bauteil.
#[serde(rename = "siteProjection", default, skip_serializing_if = "Option::is_none")]
#[ts(optional)]
pub site_projection: Option<bool>,
/// Gesetzt, wenn das Profil ein Kreis ist (aus einem `Drawing2D` mit
/// `shape:"circle"`) — `points` bleibt die Tessellierung, `toWalls3d.ts`
/// (emitExtrudedSolids) nutzt `circle` für die runde 3D-Extrusion.
+457 -4
View File
@@ -160,7 +160,7 @@ pub fn check_site_rules(r: &SiteRules, footprint_parts: &[Polygon], max_height:
polygon_bool::difference(&fp, &allowed)
};
// Kleinste Splitter (Rundung an gemeinsamen Kanten) nicht als Verstoss melden.
let real: Vec<Polygon> = violations.into_iter().filter(|p| p.first().is_some_and(|o| signed_area(o).abs() > 1e-4)).collect();
let real = drop_slivers(violations);
Some(SiteCheck {
parcel_area: signed_area(&r.parcel).abs(),
allowed_area: multi_polygon_area(&allowed),
@@ -171,6 +171,158 @@ pub fn check_site_rules(r: &SiteRules, footprint_parts: &[Polygon], max_height:
})
}
/// Splitter (Rundung an gemeinsamen Kanten) weglassen.
fn drop_slivers(v: Vec<Polygon>) -> Vec<Polygon> {
v.into_iter().filter(|p| p.first().is_some_and(|o| signed_area(o).abs() > 1e-4)).collect()
}
// ── Vorsprünge ([63]) ───────────────────────────────────────────────────────
//
// Dachüberstände und als Vorsprung markierte Bauteile (Balkone, Erker) dürfen bis `depth` in den
// Grenzabstand bzw. über die Baulinie ragen; markierte Bauteile zusätzlich höchstens auf `share`
// der Fassadenlänge je Grenze/Baulinie (Ausdehnung längs der Grenze, verglichen mit der
// Ausdehnung der Gebäude-Grundfläche längs derselben Richtung).
/// Vorsprungsregel: Tiefe (m, ≥ 0) und optionaler Höchstanteil der Fassadenlänge.
#[derive(Clone, Copy, Debug)]
pub struct ProjectionRules {
pub depth: f64,
pub share: Option<f64>,
}
/// Ein Vorsprung: Grundriss und ob der Fassadenanteil gilt (Balkon/Erker ja, Dachüberstand nein).
pub struct ProjectionPart {
pub poly: Polygon,
pub share_limited: bool,
}
/// Zulässige Fläche für Vorsprünge: Grenzabstände um `depth` verringert, Baulinien um `depth`
/// nach aussen geschoben.
pub fn projection_area(r: &SiteRules, depth: f64) -> Vec<Polygon> {
if !valid_parcel(&r.parcel) {
return vec![];
}
let d = if depth.is_finite() && depth > 0.0 { depth } else { 0.0 };
let c = centroid(&r.parcel);
let lines = r
.lines
.iter()
.map(|l| {
let (dx, dy) = (l.b[0] - l.a[0], l.b[1] - l.a[1]);
let len = dx.hypot(dy);
if !(len > 1e-9) {
return *l;
}
let (mut nx, mut ny) = (-dy / len, dx / len);
if (c[0] - l.a[0]) * nx + (c[1] - l.a[1]) * ny > 0.0 {
nx = -nx;
ny = -ny;
}
BuildingLine { a: [l.a[0] + nx * d, l.a[1] + ny * d], b: [l.b[0] + nx * d, l.b[1] + ny * d] }
})
.collect();
let setbacks = (0..r.parcel.len()).map(|i| (setback_of(r, i) - d).max(0.0)).collect();
allowed_area(&SiteRules { parcel: r.parcel.clone(), setbacks, lines })
}
fn seg_dist(p: [f64; 2], a: [f64; 2], b: [f64; 2], infinite: bool) -> f64 {
let (dx, dy) = (b[0] - a[0], b[1] - a[1]);
let l2 = dx * dx + dy * dy;
if !(l2 > 1e-18) {
return (p[0] - a[0]).hypot(p[1] - a[1]);
}
let mut t = ((p[0] - a[0]) * dx + (p[1] - a[1]) * dy) / l2;
if !infinite {
t = t.clamp(0.0, 1.0);
}
(p[0] - a[0] - t * dx).hypot(p[1] - a[1] - t * dy)
}
/// Ausdehnung [min, max] der Aussenringe längs der Einheitsrichtung `u`.
fn extent(polys: &[Polygon], u: [f64; 2]) -> Option<(f64, f64)> {
let mut it = polys.iter().filter_map(|p| p.first()).flatten().map(|q| q[0] * u[0] + q[1] * u[1]);
let first = it.next()?;
Some(it.fold((first, first), |(lo, hi), v| (lo.min(v), hi.max(v))))
}
/// Vorsprünge, die die Regel verletzen: Teile ausserhalb der zulässigen Fläche, die tiefer als
/// `depth` hineinragen, und markierte Vorsprünge einer Grenze, deren Länge zusammen den
/// Fassadenanteil übersteigt. `allowed` = zulässige Fläche, `main` = Gebäude-Grundfläche (vereinigt).
pub fn projection_violations(r: &SiteRules, allowed: &[Polygon], main: &[Polygon], parts: &[ProjectionPart], rules: ProjectionRules) -> Vec<Polygon> {
if !valid_parcel(&r.parcel) || parts.is_empty() {
return vec![];
}
let ext = projection_area(r, rules.depth);
let mut out: Vec<Polygon> = vec![];
let mut pieces: Vec<Polygon> = vec![];
for part in parts {
if part.poly.first().is_none_or(|o| o.len() < 3 || o.iter().any(|q| !(q[0].is_finite() && q[1].is_finite()))) {
continue;
}
let one = std::slice::from_ref(&part.poly);
let outside = drop_slivers(if allowed.is_empty() { one.to_vec() } else { polygon_bool::difference(one, allowed) });
if outside.is_empty() {
continue;
}
out.extend(drop_slivers(if ext.is_empty() { outside.clone() } else { polygon_bool::difference(&outside, &ext) }));
if part.share_limited && rules.share.is_some() && !ext.is_empty() {
pieces.extend(drop_slivers(polygon_bool::intersection(&outside, &ext)));
}
}
let Some(share) = rules.share else { return out };
// Grenzen: Parzellenkanten mit Abstand (Strecke) und Baulinien (Gerade).
let n = r.parcel.len();
let mut bounds: Vec<([f64; 2], [f64; 2], bool)> = (0..n).filter(|&i| setback_of(r, i) > 0.0).map(|i| (r.parcel[i], r.parcel[(i + 1) % n], false)).collect();
bounds.extend(r.lines.iter().map(|l| (l.a, l.b, true)));
let mut groups: Vec<Vec<Polygon>> = vec![vec![]; bounds.len()];
for piece in pieces {
let ring = &piece[0];
let c = centroid(ring);
let best = bounds
.iter()
.enumerate()
.map(|(k, &(a, b, inf))| (k, seg_dist(c, a, b, inf)))
.min_by(|x, y| x.1.total_cmp(&y.1));
if let Some((k, _)) = best {
groups[k].push(piece);
}
}
for (k, group) in groups.into_iter().enumerate() {
if group.is_empty() {
continue;
}
let (a, b, _) = bounds[k];
let len = (b[0] - a[0]).hypot(b[1] - a[1]);
if !(len > 1e-9) {
continue;
}
let u = [(b[0] - a[0]) / len, (b[1] - a[1]) / len];
let facade = extent(main, u).map_or(0.0, |(lo, hi)| hi - lo);
let mut iv: Vec<(f64, f64)> = group.iter().filter_map(|p| extent(std::slice::from_ref(p), u)).collect();
iv.sort_by(|x, y| x.0.total_cmp(&y.0));
let mut total = 0.0;
let mut cur: Option<(f64, f64)> = None;
for (lo, hi) in iv {
match cur {
Some((cl, ch)) if lo <= ch => cur = Some((cl, ch.max(hi))),
_ => {
if let Some((cl, ch)) = cur {
total += ch - cl;
}
cur = Some((lo, hi));
}
}
}
if let Some((cl, ch)) = cur {
total += ch - cl;
}
if total > share * facade + 1e-6 {
out.extend(group);
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
@@ -240,6 +392,70 @@ mod tests {
assert_eq!(multi_polygon_area(&[]), 0.0);
}
fn part(poly: Ring, share_limited: bool) -> ProjectionPart {
ProjectionPart { poly: vec![poly], share_limited }
}
fn area_of(v: &[Polygon]) -> f64 {
multi_polygon_area(v)
}
#[test]
fn vorsprung_flaeche_verringert_abstand_und_schiebt_baulinie() {
let mut r = rules(rect(0.0, 0.0, 30.0, 20.0), SB4.to_vec());
assert!(close(area_of(&projection_area(&r, 1.5)), 25.0 * 15.0));
assert!(close(area_of(&projection_area(&r, 0.0)), 22.0 * 12.0));
// Tiefe grösser als der Abstand: bis zur Grenze, nicht darüber.
assert!(close(area_of(&projection_area(&r, 9.0)), 600.0));
r.lines.push(BuildingLine { a: [30.0, 14.0], b: [0.0, 14.0] });
// Baulinie 1 m nach aussen (weg vom Schwerpunkt): y ≤ 15.
assert!(close(area_of(&projection_area(&r, 1.0)), 24.0 * 12.0));
assert!(projection_area(&rules(vec![], vec![]), 1.0).is_empty());
}
#[test]
fn vorsprung_tiefe() {
let r = rules(rect(0.0, 0.0, 30.0, 20.0), SB4.to_vec());
let allowed = allowed_area(&r);
let main = vec![vec![rect(6.0, 6.0, 24.0, 16.0)]];
let pr = ProjectionRules { depth: 1.5, share: None };
// Balkon 1.5 m tief: erlaubt.
assert!(projection_violations(&r, &allowed, &main, &[part(rect(10.0, 16.0, 14.0, 17.5), true)], pr).is_empty());
// Balkon 2 m tief: die letzten 0.5 m verletzen.
let v = projection_violations(&r, &allowed, &main, &[part(rect(10.0, 16.0, 14.0, 18.0), true)], pr);
assert!(close(area_of(&v), 2.0), "{}", area_of(&v));
// Dachüberstand ringsum 1 m: erlaubt; 2 m nach Norden: 0.5 m Streifen verletzt.
assert!(projection_violations(&r, &allowed, &main, &[part(rect(5.0, 5.0, 25.0, 17.0), false)], pr).is_empty());
let v = projection_violations(&r, &allowed, &main, &[part(rect(3.0, 3.0, 27.0, 18.0), false)], pr);
assert!(close(area_of(&v), 24.0 * 0.5), "{}", area_of(&v));
// Ganz innerhalb: nichts.
assert!(projection_violations(&r, &allowed, &main, &[part(rect(8.0, 8.0, 9.0, 9.0), true)], pr).is_empty());
// Unsinn: kein Panic, nichts gemeldet.
assert!(projection_violations(&r, &allowed, &main, &[part(vec![[f64::NAN, 0.0], [1.0, 0.0], [0.0, 1.0]], true)], pr).is_empty());
let nan = ProjectionRules { depth: f64::NAN, share: None };
let v = projection_violations(&r, &allowed, &main, &[part(rect(10.0, 16.0, 14.0, 17.0), true)], nan);
assert!(close(area_of(&v), 4.0));
}
#[test]
fn vorsprung_fassadenanteil() {
let r = rules(rect(0.0, 0.0, 30.0, 20.0), SB4.to_vec());
let allowed = allowed_area(&r);
// Fassade längs der Nordgrenze: 18 m.
let main = vec![vec![rect(6.0, 6.0, 24.0, 16.0)]];
let balkone = [part(rect(7.0, 16.0, 11.0, 17.0), true), part(rect(15.0, 16.0, 19.0, 17.0), true)];
let mit = |share| projection_violations(&r, &allowed, &main, &balkone, ProjectionRules { depth: 1.5, share: Some(share) });
// 8 m von 18 m = 0.44.
assert!(mit(0.5).is_empty());
assert!(close(area_of(&mit(0.4)), 8.0));
// Überlappende Vorsprünge zählen ihre Länge nur einmal.
let ueber = [part(rect(7.0, 16.0, 11.0, 17.0), true), part(rect(9.0, 16.0, 13.0, 17.0), true)];
let v = projection_violations(&r, &allowed, &main, &ueber, ProjectionRules { depth: 1.5, share: Some(6.0 / 18.0) });
assert!(v.is_empty());
// Dachüberstände zählen nicht zum Anteil.
let dach = [part(rect(5.0, 5.0, 25.0, 17.0), false)];
assert!(projection_violations(&r, &allowed, &main, &dach, ProjectionRules { depth: 1.5, share: Some(0.1) }).is_empty());
}
#[test]
fn pruefung() {
let r = rules(rect(0.0, 0.0, 30.0, 20.0), SB4.to_vec());
@@ -279,8 +495,13 @@ pub(crate) fn wall_corners(start: Vec2, end: Vec2, thickness: f64) -> Vec<[f64;
]
}
/// Teile der Gebäude-Grundfläche (Wände aller Geschosse, freie Körper ohne Abzugskörper).
/// Teile der Gebäude-Grundfläche (Wände aller Geschosse, freie Körper ohne Abzugskörper). Mit
/// `skip_projections` fehlen die als Vorsprung markierten Körper (sie prüft die Vorsprungsregel).
fn footprint_parts(p: &Project) -> Vec<Polygon> {
footprint_parts_with(p, false)
}
fn footprint_parts_with(p: &Project, skip_projections: bool) -> Vec<Polygon> {
let mut parts: Vec<Polygon> = Vec::new();
for w in &p.walls {
if hypot2(w.end.x - w.start.x, w.end.y - w.start.y) < 1e-9 {
@@ -293,7 +514,7 @@ fn footprint_parts(p: &Project) -> Vec<Polygon> {
}
}
for s in p.extruded_solids.as_deref().unwrap_or(&[]) {
if s.operation.as_deref() == Some("subtract") || s.points.len() < 3 {
if s.operation.as_deref() == Some("subtract") || s.points.len() < 3 || (skip_projections && s.site_projection == Some(true)) {
continue;
}
parts.push(vec![s.points.iter().map(|v| [v.x, v.y]).collect()]);
@@ -301,6 +522,37 @@ fn footprint_parts(p: &Project) -> Vec<Polygon> {
parts
}
/// Vorsprünge ([63]): Dach-Traufumrisse (mit Überstand; ohne Fassadenanteil), als Vorsprung
/// markierte Decken und Körper (mit Fassadenanteil).
fn projection_parts(p: &Project) -> Vec<ProjectionPart> {
let mut out = Vec::new();
for r in p.roofs.as_deref().unwrap_or(&[]) {
let g = crate::roof::roof_geometry(&r.params(), p.roof_base_elevation(r));
if g.eaves.len() >= 3 {
let mut poly = vec![g.eaves.clone()];
poly.extend(g.eaves_holes.iter().cloned());
out.push(ProjectionPart { poly, share_limited: false });
}
}
for c in p.ceilings.as_deref().unwrap_or(&[]) {
if c.site_projection == Some(true) && c.outline.len() >= 3 {
out.push(ProjectionPart { poly: vec![c.outline.iter().map(|v| [v.x, v.y]).collect()], share_limited: true });
}
}
for s in p.extruded_solids.as_deref().unwrap_or(&[]) {
if s.site_projection == Some(true) && s.operation.as_deref() != Some("subtract") && s.points.len() >= 3 {
out.push(ProjectionPart { poly: vec![s.points.iter().map(|v| [v.x, v.y]).collect()], share_limited: true });
}
}
out
}
/// Vorsprungsregel des Projekts; `None` ohne (gültige) Tiefe = altes Verhalten.
fn projection_rules(r: &crate::model::SiteRules) -> Option<ProjectionRules> {
let depth = r.projection_depth.filter(|d| d.is_finite())?;
Some(ProjectionRules { depth: depth.max(0.0), share: r.projection_share.filter(|v| v.is_finite()).map(|v| v.max(0.0)) })
}
/// Grundfläche des Gebäudes (vereinigt).
pub fn building_footprint(p: &Project) -> AreaOut {
let parts = footprint_parts(p);
@@ -393,7 +645,19 @@ pub fn check_project(p: &Project) -> Option<SiteCheckOut> {
let height_above_terrain = max_top.zip(terrain_min).map(|(t, g)| t - g);
// Mit Gelände zählt die Höhe über dem massgebenden Terrain, sonst über ±0.00.
let top_for_check = height_above_terrain.or(max_top);
let c = check_site_rules(&rules, &footprint_parts(p), r.max_height.unwrap_or(f64::NAN), top_for_check.unwrap_or(f64::NAN))?;
let proj = projection_rules(r);
let parts = footprint_parts_with(p, proj.is_some());
let mut c = check_site_rules(&rules, &parts, r.max_height.unwrap_or(f64::NAN), top_for_check.unwrap_or(f64::NAN))?;
if let Some(pr) = proj {
let main = if parts.is_empty() { vec![] } else { polygon_bool::union(&parts) };
let pv = projection_violations(&rules, &c.allowed, &main, &projection_parts(p), pr);
if !pv.is_empty() {
let mut all = std::mem::take(&mut c.violations);
all.extend(pv);
c.violations = drop_slivers(polygon_bool::union(&all));
c.violation_area = multi_polygon_area(&c.violations);
}
}
Some(SiteCheckOut {
parcel_area: c.parcel_area,
allowed: area_out(&c.allowed),
@@ -407,3 +671,192 @@ pub fn check_project(p: &Project) -> Option<SiteCheckOut> {
height_above_terrain,
})
}
// ── Zulässige Hülle im 3D ([63]) ───────────────────────────────────────────
/// Farbe der Hülle (RGBA, durchscheinend), ihrer Kanten und der Verstösse.
pub const ENVELOPE_RGBA: [f32; 4] = [0.25, 0.55, 0.95, 0.16];
pub const ENVELOPE_EDGE: [f32; 3] = [0.15, 0.4, 0.8];
pub const VIOLATION_RGBA: [f32; 4] = [0.9, 0.12, 0.08, 0.4];
/// Durchscheinende Körper für render3d (`set_ghost`): Dreiecke `[x y z r g b a]` und Kanten
/// `[x y z r g b]` (LineList), Welt = (x, Höhe, y) wie `drawing_line_verts`.
#[derive(Debug, Default)]
pub struct Envelope3d {
pub tris: Vec<f32>,
pub lines: Vec<f32>,
}
fn polys_in(a: &AreaOut) -> Vec<Polygon> {
a.iter().map(|p| p.iter().map(|r| r.iter().map(|q| [q.x, q.y]).collect()).collect()).collect()
}
/// Prismen der Mehrfach-Fläche von `z0` bis `z1`; `edge` = Kantenfarbe (None: keine Kanten).
fn push_prisms(out: &mut Envelope3d, polys: &[Polygon], z0: f64, z1: f64, rgba: [f32; 4], edge: Option<[f32; 3]>) {
if !(z0.is_finite() && z1.is_finite() && z1 > z0) {
return;
}
let (b, t) = (z0 as f32, z1 as f32);
let tris = &mut out.tris;
let mut tri = |p: [f64; 2], z: f32| tris.extend([p[0] as f32, z, p[1] as f32, rgba[0], rgba[1], rgba[2], rgba[3]]);
for poly in polys {
let Some(outer) = poly.first() else { continue };
if outer.len() < 3 {
continue;
}
let ring = crate::polygon_holes::merge_holes(outer, &poly[1..], 1e-4).ring;
let pts: Vec<_> = ring.iter().map(|q| crate::hatch::v2(q[0], q[1])).collect();
for k in crate::gpu_compile::triangulate(&pts).chunks_exact(3) {
for z in [b, t] {
for &i in k {
tri(ring[i as usize], z);
}
}
}
for r in poly {
let n = r.len();
if n < 3 {
continue;
}
for i in 0..n {
let (p, q) = (r[i], r[(i + 1) % n]);
for (v, z) in [(p, b), (q, b), (q, t), (p, b), (q, t), (p, t)] {
tri(v, z);
}
}
}
}
let Some(e) = edge else { return };
let mut line = |p: [f64; 2], z: f32| out.lines.extend([p[0] as f32, z, p[1] as f32, e[0], e[1], e[2]]);
for poly in polys {
for ring in poly {
let n = ring.len();
if n < 3 {
continue;
}
for i in 0..n {
let (o, p, q) = (ring[(i + n - 1) % n], ring[i], ring[(i + 1) % n]);
for z in [b, t] {
line(p, z);
line(q, z);
}
// Senkrechte Kante nur an echten Ecken (nicht in gerundeten Bögen).
let (ax, ay, bx, by) = (p[0] - o[0], p[1] - o[1], q[0] - p[0], q[1] - p[1]);
let turn = crate::jsnum::atan2(ax * by - ay * bx, ax * bx + ay * by).abs();
if turn > 0.2 {
line(p, b);
line(p, t);
}
}
}
}
}
/// Zulässige Hülle (zulässige Fläche vom massgebenden Terrain bzw. ±0.00 bis zur Gesamthöhe;
/// ohne Gesamthöhe bis zur Modellhöhe, mind. 3 m) und rote Verstösse (Grundfläche ausserhalb bis
/// zur Modellhöhe, Teil über der Gesamthöhe). Leer ohne Baurecht oder mit `show3d` ≠ true.
pub fn envelope3d(p: &Project) -> Envelope3d {
let mut out = Envelope3d::default();
let Some(r) = p.site_rules.as_ref().filter(|r| r.show3d == Some(true)) else { return out };
let Some(c) = check_project(p) else { return out };
let base = c.terrain_min.unwrap_or(0.0);
let top = match r.max_height {
Some(h) if h.is_finite() && h > 0.0 => base + h,
_ => c.max_top.map_or(base + 10.0, |t| t.max(base + 3.0)),
};
push_prisms(&mut out, &polys_in(&c.allowed), base, top, ENVELOPE_RGBA, Some(ENVELOPE_EDGE));
let model_top = c.max_top.unwrap_or(top);
push_prisms(&mut out, &polys_in(&c.violations), base, model_top.max(base + 0.1), VIOLATION_RGBA, None);
if c.height_exceeded {
// Teil über der Hülle (die Hülle steht auf `base`, die Höhe ist gegen das Terrain geprüft).
push_prisms(&mut out, &polys_in(&building_footprint(p)), top, model_top, VIOLATION_RGBA, None);
}
out
}
#[cfg(test)]
mod project_tests {
use super::*;
use serde_json::json;
fn project(extra: serde_json::Value) -> Project {
let mut v = json!({
"id": "p", "name": "P", "lineStyles": [], "hatches": [], "components": [], "wallTypes": [],
"drawingLevels": [{"id": "eg", "name": "EG", "kind": "floor", "visible": true, "locked": false, "floorHeight": 3.0, "cutHeight": 1.0, "baseElevation": 0}],
"layers": [], "walls": [], "openings": [], "ceilings": [], "stairs": [], "rooms": [], "drawings2d": [], "context": [],
"siteRules": {"parcel": [{"x": 0, "y": 0}, {"x": 30, "y": 0}, {"x": 30, "y": 20}, {"x": 0, "y": 20}], "defaultSetback": 4.0}
});
for (k, val) in extra.as_object().unwrap() {
if k == "siteRules" {
for (k2, v2) in val.as_object().unwrap() {
v["siteRules"][k2] = v2.clone();
}
} else {
v[k] = val.clone();
}
}
serde_json::from_value(v).unwrap()
}
fn rect(x0: f64, y0: f64, x1: f64, y1: f64) -> serde_json::Value {
json!([{"x": x0, "y": y0}, {"x": x1, "y": y0}, {"x": x1, "y": y1}, {"x": x0, "y": y1}])
}
fn solid(id: &str, pts: serde_json::Value, projection: bool) -> serde_json::Value {
let mut s = json!({"id": id, "type": "extrudedSolid", "levelId": "eg", "points": pts, "height": 3.0});
if projection {
s["siteProjection"] = json!(true);
}
s
}
fn roof(overhang: f64) -> serde_json::Value {
json!({"id": "r", "type": "roof", "floorId": "eg", "categoryCode": "35", "outline": rect(6.0, 6.0, 24.0, 16.0), "shape": "flach",
"pitchDeg": 0, "overhang": overhang, "ridgeAxis": "x", "thickness": 0.3})
}
#[test]
fn ohne_vorsprungsregel_wie_bisher() {
// Markierter Körper und Dachüberstand im Abstand: ohne Regel zählt der Körper als Grundfläche, das Dach nicht.
let p = project(json!({"extrudedSolids": [solid("h", rect(6.0, 6.0, 24.0, 16.0), false), solid("b", rect(10.0, 16.0, 14.0, 17.0), true)], "roofs": [roof(2.0)]}));
let c = check_project(&p).unwrap();
assert!((c.violation_area - 4.0).abs() < 1e-6, "{}", c.violation_area);
}
#[test]
fn vorsprungsregel_im_projekt() {
let solids = json!([solid("h", rect(6.0, 6.0, 24.0, 16.0), false), solid("b", rect(10.0, 16.0, 14.0, 17.0), true)]);
// Balkon 1 m und Dachüberstand 1 m bei zulässigen 1.5 m: keine Verstösse.
let p = project(json!({"extrudedSolids": solids, "roofs": [roof(1.0)], "siteRules": {"projectionDepth": 1.5}}));
let c = check_project(&p).unwrap();
assert!(c.violations.is_empty(), "{}", c.violation_area);
// Dachüberstand 2 m: nur nach Norden (Gebäude 6 m vor der Grenze, Abstand 4 m) ragt er
// 2 m in den Abstand, 0.5 m davon zu tief, auf 18 + 2·2 = 22 m Länge.
let p = project(json!({"extrudedSolids": solids, "roofs": [roof(2.0)], "siteRules": {"projectionDepth": 1.5}}));
let c = check_project(&p).unwrap();
assert!((c.violation_area - 22.0 * 0.5).abs() < 1e-3, "{}", c.violation_area);
// Anteil 0.2 der 18 m langen Fassade: Balkon 4 m ist zu lang.
let p = project(json!({"extrudedSolids": solids, "siteRules": {"projectionDepth": 1.5, "projectionShare": 0.2}}));
let c = check_project(&p).unwrap();
assert!((c.violation_area - 4.0).abs() < 1e-6, "{}", c.violation_area);
}
#[test]
fn huelle_3d() {
let solids = json!([solid("h", rect(6.0, 6.0, 24.0, 16.0), false)]);
// Ohne show3d: leer.
let p = project(json!({"extrudedSolids": solids, "siteRules": {"maxHeight": 12.0}}));
assert!(envelope3d(&p).tris.is_empty());
let p = project(json!({"extrudedSolids": solids, "siteRules": {"maxHeight": 12.0, "show3d": true}}));
let e = envelope3d(&p);
// Quader 22 × 12: 2 Deckel à 2 Dreiecke + 4 Seiten à 2 Dreiecke = 12 Dreiecke à 3 Ecken à 7 Werte.
assert_eq!(e.tris.len(), 12 * 3 * 7);
assert!(e.tris.chunks_exact(7).all(|v| v[1] == 0.0 || v[1] == 12.0));
assert!((e.tris[6] - ENVELOPE_RGBA[3]).abs() < 1e-6);
// Kanten: 4 Kanten × 2 Höhen + 4 Senkrechte = 12 Linien à 2 Ecken à 6 Werte.
assert_eq!(e.lines.len(), 12 * 2 * 6);
// Höhe überschritten und Grundfläche ausserhalb: rote Körper kommen dazu.
let solids = json!([solid("h", rect(2.0, 6.0, 24.0, 16.0), false)]);
let p = project(json!({"extrudedSolids": solids, "siteRules": {"maxHeight": 2.0, "show3d": true}}));
let e = envelope3d(&p);
assert!(e.tris.chunks_exact(7).any(|v| (v[6] - VIOLATION_RGBA[3]).abs() < 1e-6 && v[1] == 3.0));
assert!(e.tris.iter().all(|v| v.is_finite()));
}
}
+16
View File
@@ -31,3 +31,19 @@ pub fn site_multi_polygon_area(polys: &[f64]) -> f64 {
pub fn site_allowed_area(rules: &[f64]) -> Vec<f64> {
encode(&read_rules(rules).map(|r| site_rules::allowed_area(&r)).unwrap_or_default())
}
/// Zulässige Hülle im 3D ([63], `site_rules::envelope3d`) für render3d `set_ghost`:
/// `[nDreieckWerte, Dreiecke (x y z r g b a)…, Kanten (x y z r g b)…]` (Float32); leer bei Fehler.
#[wasm_bindgen]
pub fn doc_site_envelope(h: u32) -> Vec<f32> {
super::document::with_doc(h, |d| d.project().ok().map(|p| site_rules::envelope3d(&p)))
.flatten()
.map(|e| {
let mut out = Vec::with_capacity(1 + e.tris.len() + e.lines.len());
out.push(e.tris.len() as f32);
out.extend(e.tris);
out.extend(e.lines);
out
})
.unwrap_or_default()
}