//! [207] Situationsplan / Schwarzplan: eigenes Gebäude schwarz, Kontext grau (bzw. schwarz), Klassen, Planzeichen. use super::*; use crate::plan::domains::{plan_domains_buf, PlanArgs}; use crate::standards::{derived_categories, model_views, SITUATION_CATEGORY, SYMBOL_CATEGORY}; use serde_json::{json, Value}; /// Würfel 10 × 6 × 8 m an (x0, y0), Dreiecke wie beim Import (aussen, ohne Boden-Duplikate). fn box_mesh(id: &str, category: &str, x0: f64, y0: f64) -> Value { let (x1, y1) = (x0 + 10.0, y0 + 6.0); let positions = [x0, y0, 0.0, x1, y0, 0.0, x1, y1, 0.0, x0, y1, 0.0, x0, y0, 8.0, x1, y0, 8.0, x1, y1, 8.0, x0, y1, 8.0]; let indices = [0, 2, 1, 0, 3, 2, 4, 5, 6, 4, 6, 7, 0, 1, 5, 0, 5, 4, 1, 2, 6, 1, 6, 5, 2, 3, 7, 2, 7, 6, 3, 0, 4, 3, 4, 7]; json!({"type": "importedMesh", "id": id, "name": id, "categoryCode": category, "positions": positions.to_vec(), "indices": indices.to_vec()}) } fn project() -> Project { let mut v: Value = serde_json::from_str(include_str!("../../tests/fixtures/sample_project.json")).unwrap(); v["layers"].as_array_mut().unwrap().extend(derived_categories()); v["layers"].as_array_mut().unwrap().push(json!({"code": "12", "name": "Gebäude", "color": "#888888", "lw": 0.18, "visible": true, "locked": false})); // Untergeschoss mit einer Wand ausserhalb des Hauses: zählt nicht zum Gebäude über dem Terrain. v["drawingLevels"].as_array_mut().unwrap().push(json!({"id": "ug", "name": "UG", "kind": "floor", "visible": true, "locked": false, "baseElevation": -2.7})); v["walls"].as_array_mut().unwrap().push(json!({"id": "w-ug", "type": "wall", "floorId": "ug", "categoryCode": "20", "start": {"x": 20, "y": 0}, "end": {"x": 25, "y": 0}, "wallTypeId": "aw", "height": 2.6})); v["modelViewOptions"] = Value::Array(model_views()); v["context"] = json!([box_mesh("nachbar-1", "12", 12.0, -1.0), box_mesh("nachbar-2", "12", -14.0, 5.0)]); serde_json::from_value(v).unwrap() } fn codes(p: &Project) -> Vec { p.flat_categories().iter().map(|c| c.code.clone()).collect() } fn plan(p: &Project, show: &[String], model_view: &str) -> Vec { let a = PlanArgs::parse(1.0, &json!({"floor": "eg", "show": show, "global": true, "scale": 500, "modelView": model_view}).to_string()).unwrap(); let buf = plan_domains_buf(p, &a).unwrap(); crate::plan::buf::View::parse(&buf).unwrap().prims().unwrap() } fn without(codes: &[String], code: &str) -> Vec { codes.iter().filter(|c| *c != code).cloned().collect() } fn fills<'a>(ps: &'a [Primitive], color: &str) -> Vec<&'a Primitive> { ps.iter().filter(|q| matches!(q, Primitive::Polygon { fill, .. } if fill == color)).collect() } fn ring_area(pts: &[Vec2]) -> f64 { (0..pts.len()).map(|i| (pts[i].x * pts[(i + 1) % pts.len()].y - pts[(i + 1) % pts.len()].x * pts[i].y) / 2.0).sum::().abs() } #[test] fn eigenes_gebaeude_schwarz_ohne_bauteile() { let p = project(); let ps = plan(&p, &codes(&p), "std-mv-situation-wbw"); // Keine Wand-Poché, keine Raumstempel, keine Schnittmarken. assert!(!ps.iter().any(|q| matches!(q, Primitive::Polygon { wall_id: Some(_), .. })), "Wände entfallen"); assert!(!ps.iter().any(|q| matches!(q, Primitive::Text { room_id: Some(_), .. })), "Raumstempel entfallen"); let black: Vec<_> = fills(&ps, "#000000").into_iter().filter(|q| matches!(q, Primitive::Polygon { context_object_id: None, stroke_width_mm, .. } if *stroke_width_mm == 0.1)).collect(); assert_eq!(black.len(), 1, "ein schwarzer Umriss (UG-Wand zählt nicht)"); let Primitive::Polygon { pts, .. } = black[0] else { unreachable!() }; assert!(pts.iter().all(|q| q.x < 6.0), "nur das Haus über dem Terrain: {pts:?}"); let a = ring_area(pts); assert!(a > 15.0 && a < 40.0, "Fläche des Hauses: {a}"); } #[test] fn kontext_grau_und_schwarzplan_schwarz() { let p = project(); let wbw = plan(&p, &codes(&p), "std-mv-situation-wbw"); let grey: Vec<_> = wbw.iter().filter(|q| matches!(q, Primitive::Polygon { context_object_id: Some(_), fill, .. } if fill == "#b8b8b8")).collect(); assert_eq!(grey.len(), 2, "zwei Nachbarn grau"); let Primitive::Polygon { pts, context_object_id, .. } = grey[0] else { unreachable!() }; assert_eq!(context_object_id.as_deref(), Some("nachbar-1")); assert!((ring_area(pts) - 60.0).abs() < 1e-6, "Grundfläche aus der Aufsicht: {}", ring_area(pts)); let sp = plan(&p, &codes(&p), "std-mv-schwarzplan"); let black_ctx = sp.iter().filter(|q| matches!(q, Primitive::Polygon { context_object_id: Some(_), fill, .. } if fill == "#000000")).count(); assert_eq!(black_ctx, 2, "Schwarzplan: Nachbarn schwarz"); } #[test] fn alles_auf_klassen() { let p = project(); let all = codes(&p); let own = |ps: &[Primitive]| ps.iter().filter(|q| matches!(q, Primitive::Polygon { context_object_id: None, fill, stroke_width_mm, .. } if fill == "#000000" && *stroke_width_mm == 0.1)).count(); let ctx = |ps: &[Primitive]| ps.iter().filter(|q| matches!(q, Primitive::Polygon { context_object_id: Some(_), .. })).count(); let north = |ps: &[Primitive]| ps.iter().any(|q| matches!(q, Primitive::DrawingText { text, .. } if text == "N")); let base = plan(&p, &all, "std-mv-situation-wbw"); assert!(own(&base) == 1 && ctx(&base) == 2 && north(&base)); let no_own = plan(&p, &without(&all, SITUATION_CATEGORY), "std-mv-situation-wbw"); assert!(own(&no_own) == 0 && ctx(&no_own) == 2 && north(&no_own), "Klasse 83 schaltet das eigene Gebäude"); let no_ctx = plan(&p, &without(&all, "12"), "std-mv-situation-wbw"); assert!(own(&no_ctx) == 1 && ctx(&no_ctx) == 0, "Klasse der Netze schaltet den Kontext"); let no_sym = plan(&p, &without(&all, SYMBOL_CATEGORY), "std-mv-situation-wbw"); assert!(own(&no_sym) == 1 && !north(&no_sym), "Klasse 84 schaltet Nordpfeil und Massstabsbalken"); } #[test] fn gewoehnlicher_grundriss_bleibt() { let p = project(); let ps = plan(&p, &codes(&p), "mittel"); assert!(ps.iter().any(|q| matches!(q, Primitive::Polygon { wall_id: Some(_), .. }))); assert!(fills(&ps, "#b8b8b8").is_empty()); assert!(!ps.iter().any(|q| matches!(q, Primitive::DrawingText { text, .. } if text == "N"))); } #[test] fn dachaufsicht_zaehlt_zum_gebaeude() { let p = project(); let Some(r) = p.roofs.as_deref().and_then(|r| r.first()) else { panic!("Fixture hat ein Dach") }; let g = crate::roof::roof_geometry(&r.params(), p.roof_base_elevation(r)); let polys = own_polygons(&p); let (min_x, max_x) = g.eaves.iter().fold((f64::INFINITY, f64::NEG_INFINITY), |a, q| (a.0.min(q[0]), a.1.max(q[0]))); let xs: Vec = polys.iter().flat_map(|q| q[0].iter().map(|r| r[0])).collect(); assert!(xs.iter().cloned().fold(f64::INFINITY, f64::min) <= min_x + 1e-6 && xs.iter().cloned().fold(f64::NEG_INFINITY, f64::max) >= max_x - 1e-6, "Umriss deckt die Traufe"); } #[test] fn netz_flaeche_ohne_senkrechte() { // Nur senkrechte Dreiecke: keine Fläche in der Aufsicht. let pos = [0.0, 0.0, 0.0, 5.0, 0.0, 0.0, 5.0, 0.0, 3.0]; assert!(mesh_polygons(&pos, &[0.0, 1.0, 2.0]).is_empty()); // Ungültige Indizes und NaN: ohne Panic leer. assert!(mesh_polygons(&[0.0, 0.0, 0.0, f64::NAN, 0.0, 0.0, 1.0, 1.0, 0.0], &[0.0, 1.0, 2.0, 0.0, 9.0, 1.0]).is_empty()); } #[test] fn massstabsbalken_rund_und_im_papiermass() { assert_eq!(scalebar_total(500.0), 20.0); assert_eq!(scalebar_total(1000.0), 50.0); assert_eq!(scalebar_total(200.0), 10.0); let s = symbols(500.0, 40.0, -10.0); // Vier Balkenfelder + zwei Spitzenhälften, alle unter der Lage (Papierabstand 12 mm = 6 m). let polys: Vec<_> = s.iter().filter_map(|q| if let Primitive::Polygon { pts, .. } = q { Some(pts) } else { None }).collect(); assert_eq!(polys.len(), 6); assert!(polys.iter().flat_map(|p| p.iter()).all(|q| q.y < -10.0), "unter der Oberkante der Lage"); } #[test] fn baurecht_nur_parzellengrenze() { let mut p = project(); p.site_rules = Some(serde_json::from_value(json!({"parcel": [{"x": -8, "y": -8}, {"x": 16, "y": -8}, {"x": 16, "y": 12}, {"x": -8, "y": 12}], "setbacks": [3, 3, 3, 3], "defaultSetback": 3, "show": true})).unwrap()); let mut all = codes(&p); all.push("01".into()); let ps = plan(&p, &all, "std-mv-situation-wbw"); let cls = |c: &str| ps.iter().filter(|q| matches!(q, Primitive::Line { cls, .. } if cls == c)).count(); assert!(cls("site-parcel") >= 4, "Parzellengrenze bleibt"); assert_eq!(cls("site-allowed"), 0, "kein Baubereich"); let normal = plan(&p, &all, "mittel"); assert!(normal.iter().filter(|q| matches!(q, Primitive::Line { cls, .. } if cls == "site-allowed")).count() > 0, "im gewöhnlichen Grundriss schon"); } fn poly2d(id: &str, code: &str, pts: &[(f64, f64)]) -> Value { json!({"id": id, "type": "drawing2d", "levelId": "eg", "categoryCode": code, "geom": {"shape": "polyline", "closed": true, "pts": pts.iter().map(|(x, y)| json!({"x": x, "y": y})).collect::>()}}) } fn rect_pts(x0: f64, y0: f64, x1: f64, y1: f64) -> [(f64, f64); 4] { [(x0, y0), (x1, y0), (x1, y1), (x0, y1)] } /// Kataster: Haus mit Innenhof (zwei geschlossene Polylinien), Nachbarhaus, Gewässer auf Klasse „17". fn cadastre_project() -> Project { let p = project(); let mut v = serde_json::to_value(&p).unwrap(); v["context"] = json!([]); v["layers"].as_array_mut().unwrap().push(json!({"code": "17", "name": "Gewässer", "color": "#909090", "lw": 0.13, "visible": true, "locked": false})); let d = v["drawings2d"].as_array_mut().unwrap(); d.push(poly2d("k-haus", "12", &rect_pts(30.0, -20.0, 60.0, 0.0))); d.push(poly2d("k-hof", "12", &rect_pts(40.0, -14.0, 50.0, -6.0))); d.push(poly2d("k-nachbar", "12", &rect_pts(70.0, -20.0, 80.0, -10.0))); d.push(poly2d("k-see", "17", &rect_pts(30.0, 10.0, 60.0, 30.0))); serde_json::from_value(v).unwrap() } #[test] fn kataster_gebaeude_gefuellt_hof_als_loch() { let p = cadastre_project(); let ps = plan(&p, &codes(&p), "std-mv-situation-wbw"); let grey: Vec<&Primitive> = fills(&ps, "#b8b8b8").into_iter().filter(|q| matches!(q, Primitive::Polygon { context_object_id: None, .. })).collect(); assert_eq!(grey.len(), 2, "Haus mit Hof und Nachbar, je eine Fläche"); // Haus: 30 x 20 minus Hof 10 x 8 = 520 m2; der Hof ist ein Loch (Brückenring). let areas: Vec = grey.iter().map(|q| if let Primitive::Polygon { pts, .. } = q { ring_area(pts) } else { 0.0 }).collect(); assert!(areas.iter().any(|a| (a - 520.0).abs() < 0.5), "Haus abzüglich Hof: {areas:?}"); assert!(areas.iter().any(|a| (a - 100.0).abs() < 1e-6), "Nachbarhaus: {areas:?}"); // Brückenkanten ohne Strich, kein weisser Hof darüber. assert!(grey.iter().any(|q| matches!(q, Primitive::Polygon { no_stroke_edges: Some(e), .. } if !e.is_empty())), "Brücke zum Hof"); assert!(fills(&ps, "#ffffff").iter().all(|q| matches!(q, Primitive::Polygon { stroke_width_mm, .. } if *stroke_width_mm != 0.1)), "keine weisse Fläche über dem Hof"); // Gewässer hellblau. let water = fills(&ps, "#cfe3f1"); assert_eq!(water.len(), 1); let Primitive::Polygon { pts, .. } = water[0] else { unreachable!() }; assert!((ring_area(pts) - 600.0).abs() < 1e-6); } #[test] fn kataster_schwarzplan_und_klassen() { let p = cadastre_project(); let all = codes(&p); let sp = plan(&p, &all, "std-mv-schwarzplan"); let black = sp.iter().filter(|q| matches!(q, Primitive::Polygon { context_object_id: None, fill, stroke_width_mm, .. } if fill == "#000000" && *stroke_width_mm == 0.1)).count(); assert_eq!(black, 3, "eigenes Gebäude + Haus + Nachbar schwarz"); let no12 = plan(&p, &without(&all, "12"), "std-mv-situation-wbw"); assert!(fills(&no12, "#b8b8b8").is_empty(), "Klasse Gebäude schaltet die Kataster-Flächen"); let no17 = plan(&p, &without(&all, "17"), "std-mv-situation-wbw"); assert!(fills(&no17, "#cfe3f1").is_empty(), "Klasse Gewässer schaltet das Gewässer"); let normal = plan(&p, &all, "mittel"); assert!(fills(&normal, "#b8b8b8").is_empty() && fills(&normal, "#cfe3f1").is_empty()); } /// [207] Gewässer: hellblau mit feiner Parallelschraffur; in Tusche bleibt die Schraffur (Fläche weiss, Linien Tinte), /// sonst wäre das Wasser auf dem Papier unsichtbar. #[test] fn kataster_gewaesser_mit_schraffur_auch_in_tusche() { use crate::plan::extras::Post; let p = cadastre_project(); let ps = plan(&p, &codes(&p), "std-mv-situation-wbw"); let water = fills(&ps, "#cfe3f1"); let Primitive::Polygon { hatch, .. } = water[0] else { unreachable!() }; assert_eq!(hatch.pattern, HatchPattern::Diagonal); assert_eq!(hatch.angle, 90.0, "waagrechte Linien (Musterwinkel 0 = senkrecht)"); assert!(hatch.line_weight <= 0.1, "fein: {}", hatch.line_weight); // Abstand 1.2 mm auf dem Papier bei 1:500: 8 · scale / 90 m = 0.6 m. assert!((8.0 * hatch.scale / 90.0 - 0.6).abs() < 1e-9, "Abstand {}", 8.0 * hatch.scale / 90.0); let mut q = water[0].clone(); Post { mono: true, lw: 1.0, sketch: 0.0, keep: Default::default() }.apply(&mut q); let Primitive::Polygon { fill, hatch, .. } = &q else { unreachable!() }; assert_eq!(fill, "#ffffff"); assert_eq!(hatch.pattern, HatchPattern::Diagonal); assert_eq!(hatch.color, crate::plan::style::MONO_INK, "Schraffur in Tusche"); // Gebäude behalten keine Schraffur. assert!(fills(&ps, "#b8b8b8").iter().all(|q| matches!(q, Primitive::Polygon { hatch, .. } if hatch.pattern == HatchPattern::None))); } /// [207] Gefüllte Kataster-Flächen tragen ihren Umriss selbst (wie die Kontextnetze): die Zeichnungs-Linien der /// Ringe liegen nicht noch einmal darüber. Im gewöhnlichen Grundriss bleiben sie. #[test] fn kataster_umrisse_nicht_doppelt() { let p = cadastre_project(); let all = codes(&p); let kat_lines = |ps: &[Primitive]| ps.iter().filter(|q| matches!(q, Primitive::Line { drawing_id: Some(id), .. } if id.starts_with("k-"))).count(); assert_eq!(kat_lines(&plan(&p, &all, "std-mv-situation-wbw")), 0); assert_eq!(kat_lines(&plan(&p, &all, "std-mv-schwarzplan")), 0); assert!(kat_lines(&plan(&p, &all, "mittel")) > 0, "gewöhnlicher Grundriss zeichnet sie"); // Schwarzplan: Umriss der gefüllten Gebäude schwarz wie die Füllung. let sp = plan(&p, &all, "std-mv-schwarzplan"); assert!(sp.iter().filter(|q| matches!(q, Primitive::Polygon { fill, .. } if fill == "#000000")).all(|q| matches!(q, Primitive::Polygon { stroke, .. } if stroke == "#000000"))); } /// [299] Kataster-Gebäude unter dem eigenen Fussabdruck entfallen (das Projektgebäude ersetzt es), andere bleiben. #[test] fn kataster_gebaeude_unter_eigenem_haus_entfaellt() { let mut v = serde_json::to_value(cadastre_project()).unwrap(); // Kataster-Fläche, die das Haus (x < 6) deckt. v["drawings2d"].as_array_mut().unwrap().push(poly2d("k-eigen", "12", &rect_pts(-1.0, -1.0, 5.0, 5.0))); let p: Project = serde_json::from_value(v).unwrap(); let ps = plan(&p, &codes(&p), "std-mv-situation-wbw"); let grey: Vec<&Primitive> = fills(&ps, "#b8b8b8").into_iter().filter(|q| matches!(q, Primitive::Polygon { context_object_id: None, .. })).collect(); assert_eq!(grey.len(), 2, "eigenes Kataster-Gebäude entfällt, Haus mit Hof und Nachbar bleiben"); assert!(grey.iter().all(|q| matches!(q, Primitive::Polygon { drawing_id: Some(id), .. } if id != "k-eigen"))); let sp = plan(&p, &codes(&p), "std-mv-schwarzplan"); let black = sp.iter().filter(|q| matches!(q, Primitive::Polygon { context_object_id: None, fill, stroke_width_mm, .. } if fill == "#000000" && *stroke_width_mm == 0.1)).count(); assert_eq!(black, 3, "Schwarzplan: eigenes Gebäude + zwei Kataster-Flächen"); } /// [299] Nordpfeil und Massstabsbalken stehen dicht unter Haus und Parzelle, nicht unter dem fernsten Kontext. #[test] fn planzeichen_dicht_an_der_lage() { let mut p = cadastre_project(); p.site_rules = Some(serde_json::from_value(json!({"parcel": [{"x": -8, "y": -8}, {"x": 16, "y": -8}, {"x": 16, "y": 12}, {"x": -8, "y": 12}], "setbacks": [3, 3, 3, 3], "defaultSetback": 3, "show": true})).unwrap()); let ps = plan(&p, &codes(&p), "std-mv-situation-wbw"); // Kataster-Haus reicht bis y = -20, Parzelle bis y = -8: die Zeichen liegen knapp unter der Parzelle (12 mm = 6 m Abstand). let ys: Vec = ps.iter().filter_map(|q| if let Primitive::Polygon { pts, stroke_width_mm, fill, .. } = q { (*stroke_width_mm == 0.18 && (fill == "#000000" || fill == "#ffffff")).then(|| pts.iter().map(|p| p.y).fold(f64::MAX, f64::min)) } else { None }).collect(); assert!(!ys.is_empty()); assert!(ys.iter().all(|y| *y < -8.0 && *y > -20.0), "unter der Parzelle, über dem fernen Kataster-Haus: {ys:?}"); } /// [321] Rampe (Decke mit Gefälle als Rampe) zählt nicht zum Gebäude der Situation. #[test] fn rampe_ist_kein_gebaeude() { let p = project(); let mut v = serde_json::to_value(&p).unwrap(); v["ceilings"] = json!([{"id": "c-rampe", "type": "ceiling", "floorId": "eg", "categoryCode": "30", "outline": [{"x": 30, "y": 2}, {"x": 36, "y": 2}, {"x": 36, "y": 5}, {"x": 30, "y": 5}], "wallTypeId": "", "ceilingTypeId": "", "top": {"mode": "floor", "floorId": "eg", "offset": 0}, "slope": {"from": {"x": 30, "y": 3.5}, "to": {"x": 36, "y": 3.5}, "rise": -0.36, "ramp": true}}]); let p: Project = serde_json::from_value(v).unwrap(); assert!(!own_polygons(&p).iter().flatten().flatten().any(|q| q[0] > 29.0), "Rampe nicht gefüllt"); // Ohne Rampen-Kennzeichen wäre sie Gebäude. let mut v = serde_json::to_value(&p).unwrap(); v["ceilings"][0]["slope"]["ramp"] = json!(false); let p2: Project = serde_json::from_value(v).unwrap(); assert!(own_polygons(&p2).iter().flatten().flatten().any(|q| q[0] > 29.0)); } /// [321] Planzeichen stehen nicht auf Linien der Lage: eine senkrechte Linie durch den Balken verschiebt sie. #[test] fn planzeichen_weichen_linien_aus() { let mut o = crate::plan::stamp_place::Obstacles::default(); let free = placed_symbols(500.0, 40.0, -10.0, &o); let b0 = symbols_box(&free); let x = (b0[0] + b0[2]) / 2.0; o.seg([x, -200.0], [x, 200.0]); let moved = symbols_box(&placed_symbols(500.0, 40.0, -10.0, &o)); assert!(moved[2] < x || moved[0] > x, "Zeichen links oder rechts der Linie: {moved:?} x={x}"); }