//! Öffnungs-Einbauteile im 3D (Glas, Rahmen, Sprossen, Bank, Kasten): Öffnungen als flacher Puffer an //! `openings3d`, Kästen zu Netzen, Füllungen von Formöffnungen und Türzargen (`emit_opening_meshes()`, //! `wall_opening_meshes()`). Aufgerufen von `parts()` (`mod.rs`) und dem inkrementellen Cache. use super::*; /// Farbe je Bauteil-Klasse (Index = `Part` in `openings3d.rs`). pub(crate) const PART_RGB: [Rgb; 7] = [ [0.62, 0.76, 0.85], [0.85, 0.85, 0.83], [0.88, 0.88, 0.9], [0.8, 0.8, 0.78], [0.25, 0.27, 0.3], [0.93, 0.9, 0.85], [0.9, 0.89, 0.86], ]; /// Dreiecke eines Öffnungs-Quaders (Ecken-Bits: bit0 = Achse, bit1 = Höhe, bit2 = quer). const OPENING_BOX_TRIS: [u32; 36] = [0, 1, 3, 0, 3, 2, 4, 5, 7, 4, 7, 6, 0, 1, 5, 0, 5, 4, 2, 3, 7, 2, 7, 6, 0, 2, 6, 0, 6, 4, 1, 3, 7, 1, 7, 5]; fn opt(v: Option) -> f64 { v.unwrap_or(f64::NAN) } fn flag(b: bool) -> f64 { if b { 1.0 } else { 0.0 } } fn join_code(j: Option) -> f64 { match j { Some(FrameCornerJoin::Horizontal) => 1.0, Some(FrameCornerJoin::Gehrung) => 2.0, _ => 0.0, } } fn opening_kind_code(k: Option) -> f64 { match k { None => 0.0, Some(OpeningKind::Dreh) => 1.0, Some(OpeningKind::Kipp) => 2.0, Some(OpeningKind::Drehkipp) => 3.0, Some(OpeningKind::Fest) => 4.0, Some(OpeningKind::Schiebe) => 5.0, Some(OpeningKind::Unknown) => 6.0, } } #[allow(clippy::too_many_arguments)] fn write_common(w: &mut FlatWriter, frame_depth: Option, inset: Option, side: f64, join: Option, frame_width: Option, transom: Option, rebate: Option) { w.nums(&[opt(frame_depth), opt(inset), side, join_code(join), opt(frame_width), opt(transom), opt(rebate)]); } fn glazing_panes_of(wt: &WindowType) -> f64 { match wt.glazing_panes { Some(p) if p != 0.0 => p, _ => match wt.glazing { WindowTypeGlazing::Dreifach => 3.0, WindowTypeGlazing::Zweifach => 2.0, _ => 1.0, }, } } fn write_opening(w: &mut FlatWriter, p: &Project, op: &Opening) { if op.kind == ElementKind::Window { let Some(wt) = p.window_type(op) else { w.num(0.0); return; }; w.num(1.0); let side = match wt.inset_face { Some(WindowTypeInsetFace::Innen) => 1.0, Some(WindowTypeInsetFace::Mitte) => 2.0, _ => 0.0, }; write_common(w, wt.frame_depth, wt.inset_from_face.or_else(|| crate::opening_detail::default_frame_inset(p, op)), side, wt.frame_corner_join, wt.frame_width, wt.transom_height, wt.rebate_depth); let sturz = wt.shading.as_ref().is_some_and(|s| s.mount == Some(WindowShadingMount::Sturz)); let rails = sturz && wt.shading.as_ref().is_some_and(|s| s.kind != WindowShadingKind::Markise); let shading = wt.shading.as_ref().filter(|s| s.create).map(|s| &s.r#box); w.nums(&[ glazing_panes_of(wt), opt(wt.mullion_rows), opt(wt.mullion_cols), match wt.sill_board { Some(WindowTypeSillBoard::Keine) => 1.0, Some(WindowTypeSillBoard::Innen) => 2.0, Some(WindowTypeSillBoard::Beide) => 3.0, _ => 0.0, }, flag(shading.is_some()), shading.map_or(0.0, |b| b.width), shading.map_or(0.0, |b| b.height), shading.map_or(0.0, |b| b.depth), flag(sturz), flag(rails), op.wing_count.unwrap_or(wt.wing_count), opening_kind_code(Some(wt.kind)), ]); let sashes = wt.sashes.as_deref().unwrap_or(&[]); w.num(sashes.len() as f64); for s in sashes { w.nums(&[ if s.kind == SashDefKind::Fluegel { 0.0 } else { 1.0 }, flag(s.auto_width == Some(false)), opt(s.width), opt(s.post_width), opening_kind_code(s.opening), flag(s.hinge_side != Some(SashDefHingeSide::Right)), ]); } let transom = wt.transom_sashes.as_deref().unwrap_or(&[]); let bottom = wt.bottom_sashes.as_deref().unwrap_or(&[]); for (k, list) in [transom, bottom].into_iter().enumerate() { if k == 1 { // [95] Brüstungsfeld: Höhe bis Riegelmitte, dann seine Einteilung. w.num(wt.bottom_panel_height.filter(|v| v.is_finite()).unwrap_or(0.0)); } w.num(list.len() as f64); for s in list { w.nums(&[ if s.kind == SashDefKind::Fluegel { 0.0 } else { 1.0 }, flag(s.auto_width == Some(false)), opt(s.width), opt(s.post_width), opening_kind_code(s.opening), flag(s.hinge_side != Some(SashDefHingeSide::Right)), ]); } } return; } let Some(dt) = p.door_type(op) else { w.num(0.0); return; }; w.num(1.0); let side = match dt.inset_face { Some(DoorTypeInsetFace::Innen) => 1.0, Some(DoorTypeInsetFace::Mitte) => 2.0, _ => 0.0, }; write_common(w, dt.frame_depth, dt.inset_from_face.or_else(|| crate::opening_detail::default_frame_inset(p, op)), side, dt.frame_corner_join, dt.frame_width, dt.transom_height, dt.rebate_depth); w.nums(&[ flag(dt.threshold == Some(true)), dt.leaf_count, match dt.leaf_style { DoorTypeLeafStyle::Glas => 1.0, DoorTypeLeafStyle::Kassette => 2.0, _ => 0.0, }, opt(dt.glazing_ratio), flag(dt.frame_kind != Some(DoorTypeFrameKind::Blockrahmen)), opt(dt.panel_cols), opt(dt.panel_rows), match dt.kind { DoorTypeKind::Schiebe => 1.0, DoorTypeKind::Hebeschiebe => 2.0, DoorTypeKind::Pendel => 3.0, DoorTypeKind::Tasche => 4.0, _ => 0.0, }, // Aufschlagseite wie im Grundriss (linke Normale), hier entlang der rechten Normalen. -(if matches!(op.swing, None | Some(SwingSide::Left)) { 1.0 } else { -1.0 }) * (if matches!(op.opening_dir, None | Some(OpeningOpeningDir::In)) { 1.0 } else { -1.0 }), flag(op.hinge == Some(OpeningHinge::End)), ]); } /// Öffnungs-Einbauteile als Quader-Meshes (`emitOpeningMeshes`); `detail`: 0 grob, 1 mittel, 2 fein. /// Bogenwände ([T2]): je Wand auf der abgewickelten Ersatzwand gerechnet und auf den Bogen gelegt — breite Öffnungen /// folgen dem Bogen ([216], [`follows_arc`]), schmale bleiben gerade (Sehne zwischen den Laibungen), Öffnungen mit /// Schichteinzug liegen gerade auf ihrer Sehne (`reveal_zone`); die Enden sitzen radial in der Laibung. pub fn emit_opening_meshes(p: &Project, detail: f64) -> Vec { emit_opening_meshes_with(p, detail, &|_| true) } /// Wie [`emit_opening_meshes`], nur für die Öffnungen mit `keep` ([180] Klassen-Filter: die Teile einer /// Wand werden gemeinsam gerechnet, darum wirkt der Filter auf die Eingabe). pub fn emit_opening_meshes_with(p: &Project, detail: f64, keep: &dyn Fn(&Opening) -> bool) -> Vec { let arcs: Vec = p.walls.iter().enumerate().filter(|(_, w)| crate::wall_arc::WallArc::of_wall(w).is_some()).map(|(i, _)| i).collect(); if arcs.is_empty() { return emit_opening_meshes_plain(p, detail, keep); } let mut q = p.clone(); let arc_ids: std::collections::HashSet<&str> = arcs.iter().map(|&i| p.walls[i].id.as_str()).collect(); q.openings = p.openings.as_ref().map(|v| v.iter().filter(|o| keep(o) && !arc_ids.contains(o.host_wall_id.as_str())).cloned().collect()); let mut out = emit_opening_meshes_plain(&q, detail, &|_| true); for &i in &arcs { let Some((arc, v)) = crate::wall_arc::unrolled_wall(&p.walls[i]) else { continue }; q.walls = vec![v]; let own: Vec = p.openings.as_deref().unwrap_or(&[]).iter().filter(|o| o.host_wall_id == p.walls[i].id && keep(o)).cloned().collect(); // [106]/[216] Lage nach [`arc_placement`]: mit Schichteinzug gerade auf der Sehne über `reveal_zone` (wie die // Teilwand mit dem Loch, `wall_arc::chord_walls`), breite folgen dem Bogen, schmale bleiben gerade. let (mut zoned, mut follow, mut straight) = (Vec::new(), Vec::new(), Vec::new()); for o in own { match arc_placement(p, &p.walls[i], &arc, &o) { ArcPlacement::Zone(sa, sb) => zoned.push((o, sa, sb)), ArcPlacement::Follow => follow.push(o), ArcPlacement::Chord => straight.push(o), } } let total = p.wall_thickness(&p.walls[i], 0.2); let off = crate::joins::reference_offset(super::wall_reference(&p.walls[i]), total); let r_out = arc.r_max(off - total / 2.0, off + total / 2.0); q.openings = Some(straight); for mut m in emit_opening_meshes_plain(&q, detail, &|_| true) { for c in m.positions.chunks_exact_mut(3) { let w = arc.point(c[0], c[1]); c[0] = w.x; c[1] = w.y; } out.push(m); } if !follow.is_empty() { let stations = arc_stations(&arc, r_out, p.openings.as_deref().unwrap_or(&[]).iter().filter(|o| o.host_wall_id == p.walls[i].id)); q.openings = Some(follow); for m in emit_opening_meshes_plain(&q, detail, &|_| true) { out.extend(bend_on_arc(m, &arc, &stations)); } } for (o, sa, sb) in zoned { let (pa, u, chord) = crate::wall_arc::chord_of(&arc, sa, sb); q.openings = Some(vec![o]); for mut m in emit_opening_meshes_plain(&q, detail, &|_| true) { for c in m.positions.chunks_exact_mut(3) { let t = (c[0] - sa) / (sb - sa) * chord; let (x, y) = (pa.x + u.x * t - u.y * c[1], pa.y + u.y * t + u.x * c[1]); c[0] = x; c[1] = y; } out.push(m); } } } out } /// [216] Lage der Einbauteile einer Öffnung in einer Bogenwand — EINE Entscheidung für 3D, Ansicht und Schnitt. #[derive(Clone, Copy, Debug, PartialEq)] pub(crate) enum ArcPlacement { /// Mit Schichteinzug [106]: Wandloch und Teile gerade auf der Sehne der Achse über `[sa, sb]` (Bogenlänge, /// `wall_arc::reveal_zone`); Bogenlänge `s` liegt auf der Sehne linear bei `(s − sa)·Länge/(sb − sa)`. Zone(f64, f64), /// Ohne Schichteinzug und Pfeilhöhe > [`BEND_SAGITTA`]: Teile folgen dem Bogen (Punkt `s`, Querlage `n` auf /// `arc.point(s, n)`). Follow, /// Schmal: gerade zwischen den Laibungen (Sehne von `from` nach `to`). Chord, } /// [216] Wie liegen die Einbauteile von `o` in der Bogenwand `wall` (Bogen `arc`)? Siehe [`ArcPlacement`]. pub(crate) fn arc_placement(p: &Project, wall: &Wall, arc: &crate::wall_arc::WallArc, o: &Opening) -> ArcPlacement { let zones = arc_zones(p, wall); let own = match crate::opening_detail::opening_reveal(p, wall, o) { Some(r) => { let ms = r.sides[..2].iter().flatten().map(|a| a.shift).fold(0.0f64, f64::max); Some(crate::wall_arc::reveal_zone(r.from, r.to, ms, arc.length())) } None => None, }; // Die (über die Geschosse vereinigte) Zone, die die Öffnung enthält — auch ohne eigenen Einzug. let (from, to) = own.unwrap_or((o.position, o.position + o.width)); if let Some(&(sa, sb)) = zones.iter().find(|(a, b)| *a <= from + 1e-6 && *b >= to - 1e-6) { return ArcPlacement::Zone(sa, sb); } if let Some((sa, sb)) = own { return ArcPlacement::Zone(sa, sb); } let total = p.wall_thickness(wall, 0.2); let off = crate::joins::reference_offset(super::wall_reference(wall), total); let r_out = arc.r_max(off - total / 2.0, off + total / 2.0); if follows_arc(r_out, o.width) { ArcPlacement::Follow } else { ArcPlacement::Chord } } /// [216] Wirksame Sehnen-Zonen einer Bogenwand (Bogenlänge, sortiert, disjunkt): je Öffnung mit Schichteinzug [106] ihre /// Zone (`wall_arc::reveal_zone`), vereinigt über alle deckungsgleichen Bogenwände (gleicher Mittelpunkt, Bänder überlappen /// radial — die Erkerwände der Geschosse übereinander). So fluchten die Sehnen übereinanderliegender Geschosse (keine /// waagrechte Kante an der Geschossgrenze, Decke dazwischen auf derselben Flucht). Öffnungen ohne Einzug, die eine Zone /// teilweise überdecken, erweitern sie (sie liegen dann ganz auf der Sehne). EINE Quelle für Wand (3D, Schnitt, Ansicht, /// Grundriss), Einbauteile und Deckenanschluss. pub(crate) fn arc_zones(p: &Project, wall: &Wall) -> Vec<(f64, f64)> { use crate::wall_arc::{merge_zones, reveal_zone, WallArc, REVEAL_MARGIN}; let Some(a) = WallArc::of_wall(wall) else { return Vec::new() }; let len = a.length(); let t0 = p.wall_thickness(wall, 0.2); // Bereich [sa, sb] einer anderen Bogenwand `b` in Bogenlänge der eigenen Achse (`None`: liegt nicht auf ihr). let map = |b: &WallArc, sa: f64, sb: f64| -> Option<(f64, f64)> { let (x, y) = (a.local(b.point(sa, 0.0)).0, a.local(b.point(sb, 0.0)).0); let (x, y) = (x.min(y), x.max(y)); let want = (sb - sa).abs() * a.r / b.r; ((y - x - want).abs() < 1e-6 && y > 1e-9 && x < len - 1e-9).then_some((x, y)) }; let mut zones: Vec<(f64, f64)> = Vec::new(); let mut plain: Vec<(f64, f64)> = Vec::new(); for w in &p.walls { let Some(b) = WallArc::of_wall(w) else { continue }; let d = (b.center.x - a.center.x).hypot(b.center.y - a.center.y); if d > 1e-3 || (b.r - a.r).abs() > (t0 + p.wall_thickness(w, 0.2)) / 2.0 { continue; } let bl = b.length(); for o in p.openings_of_wall(&w.id) { match crate::opening_detail::opening_reveal(p, w, o) { Some(r) => { let ms = r.sides[..2].iter().flatten().map(|a| a.shift).fold(0.0f64, f64::max); let (sa, sb) = reveal_zone(r.from, r.to, ms, bl); zones.extend(map(&b, sa, sb)); } None => { let (from, to) = (o.position.clamp(0.0, bl), (o.position + o.width).clamp(0.0, bl)); if to > from + 1e-9 { plain.extend(map(&b, (from - REVEAL_MARGIN).max(0.0), (to + REVEAL_MARGIN).min(bl))); } } } } } if zones.is_empty() { return zones; } let mut zones = merge_zones(zones); for _ in 0..16 { let mut grown = false; for &(x, y) in &plain { for z in zones.iter_mut() { if x < z.1 - 1e-9 && y > z.0 + 1e-9 && (x < z.0 - 1e-9 || y > z.1 + 1e-9) { *z = (z.0.min(x), z.1.max(y)); grown = true; } } } if !grown { break; } zones = merge_zones(zones); } zones.into_iter().map(|(x, y)| (x.max(0.0), y.min(len))).filter(|(x, y)| y - x > 1e-6).collect() } /// [216] Pfeilhöhe (m), ab der die Einbauteile einer Öffnung in der Bogenwand dem Bogen folgen; darunter bleiben sie /// gerade zwischen den Leibungen (der Versatz zur Wandflucht ist dann kleiner als ein Zentimeter). pub(crate) const BEND_SAGITTA: f64 = 0.01; /// Folgen die Einbauteile einer Öffnung der Breite `width` dem Bogen mit (Aussen-)Radius `r`? Pfeilhöhe der Sehne /// grösser als [`BEND_SAGITTA`] (bei r = 2 m ab ≈ 0,4 m Breite). pub(crate) fn follows_arc(r: f64, width: f64) -> bool { if !(r > 1e-9 && width > 0.0) { return false; } let half = (width / 2.0).min(r); r - (r * r - half * half).max(0.0).sqrt() > BEND_SAGITTA } /// Teilstellen (Bogenlänge, aufsteigend) einer Bogenwand für gebogene Einbauteile: die gleichmässige Teilung der Wand /// ([`crate::wall_arc::WallArc::stations`], wie `chord_walls`) und die Laibungen der Öffnungen `openings` — so liegen /// Rahmen, Glas und Fensterbank auf denselben Sehnen wie die Wandflächen. fn arc_stations<'a>(arc: &crate::wall_arc::WallArc, r_max: f64, openings: impl Iterator) -> Vec { let len = arc.length(); let mut st = arc.stations(0.0, len, r_max); for o in openings { let from = o.position.min(len).max(0.0); let to = (o.position + o.width).min(len).max(from); st.extend([from, to].into_iter().filter(|s| s.is_finite())); } st.retain(|s| s.is_finite()); st.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)); st.dedup_by(|a, b| (*a - *b).abs() < 1e-6); st } /// Netz der abgewickelten Wand (x = Bogenlänge, y = Querlage links, z) an den Ebenen x = `cuts` zerschneiden: jedes /// Dreieck, das eine Teilstelle quert, wird in konvexe Stücke geteilt und neu aufgefächert. Schnittpunkte auf einer Kante /// werden einmal angelegt (Schlüssel Kante + Teilstelle) und aus den Kanten-Enden in fester Reihenfolge gerechnet, die /// Nachbardreiecke teilen sie (keine T-Stösse, geschlossene Körper bleiben geschlossen). pub(crate) fn slice_x(positions: &[f64], indices: &[u32], cuts: &[f64]) -> (Vec, Vec) { const EPS: f64 = 1e-9; let mut pos: Vec<[f64; 3]> = positions.chunks_exact(3).map(|c| [c[0], c[1], c[2]]).collect(); let n0 = pos.len() as u32; let mut cache: std::collections::HashMap<(u32, u32, usize), u32> = std::collections::HashMap::new(); let mut out: Vec = Vec::with_capacity(indices.len()); // Polygon-Ecke: Index in `pos` und die Original-Kante, auf der sie liegt (Ecke: (i, i)). #[derive(Clone, Copy)] struct Corner { id: u32, on: (u32, u32), } let edge_of = |a: Corner, b: Corner| -> Option<(u32, u32)> { let ea = if a.on.0 == a.on.1 { None } else { Some(a.on) }; let eb = if b.on.0 == b.on.1 { None } else { Some(b.on) }; match (ea, eb) { (None, None) => Some((a.on.0.min(b.on.0), a.on.0.max(b.on.0))), (Some(e), None) if e.0 == b.on.0 || e.1 == b.on.0 => Some(e), (None, Some(e)) if e.0 == a.on.0 || e.1 == a.on.0 => Some(e), (Some(e), Some(f)) if e == f => Some(e), _ => None, } }; for t in indices.chunks_exact(3) { if t.iter().any(|&k| k >= n0) { continue; } let xs = [pos[t[0] as usize][0], pos[t[1] as usize][0], pos[t[2] as usize][0]]; let (lo, hi) = (xs[0].min(xs[1]).min(xs[2]), xs[0].max(xs[1]).max(xs[2])); let mut polys: Vec> = vec![t.iter().map(|&k| Corner { id: k, on: (k, k) }).collect()]; for (ci, &c) in cuts.iter().enumerate() { if !(c > lo + EPS && c < hi - EPS) { continue; } let mut next = Vec::with_capacity(polys.len() * 2); for poly in polys { let side = |pos: &[[f64; 3]], k: &Corner| { let d = pos[k.id as usize][0] - c; if d.abs() <= EPS { 0 } else if d < 0.0 { -1 } else { 1 } }; if poly.iter().all(|k| side(&pos, k) <= 0) || poly.iter().all(|k| side(&pos, k) >= 0) { next.push(poly); continue; } let (mut left, mut right) = (Vec::new(), Vec::new()); for j in 0..poly.len() { let (a, b) = (poly[j], poly[(j + 1) % poly.len()]); let (sa, sb) = (side(&pos, &a), side(&pos, &b)); if sa <= 0 { left.push(a); } if sa >= 0 { right.push(a); } if sa * sb < 0 { let Some(e) = edge_of(a, b) else { continue }; let id = *cache.entry((e.0, e.1, ci)).or_insert_with(|| { let (p0, p1) = (pos[e.0 as usize], pos[e.1 as usize]); let (a0, a1) = if p0[0] <= p1[0] { (p0, p1) } else { (p1, p0) }; let f = (c - a0[0]) / (a1[0] - a0[0]); pos.push([c, a0[1] + (a1[1] - a0[1]) * f, a0[2] + (a1[2] - a0[2]) * f]); pos.len() as u32 - 1 }); let k = Corner { id, on: e }; left.push(k); right.push(k); } } next.extend([left, right].into_iter().filter(|q| q.len() >= 3)); } polys = next; } for poly in polys { for j in 1..poly.len() - 1 { out.extend([poly[0].id, poly[j].id, poly[j + 1].id]); } } } (pos.into_iter().flatten().collect(), out) } /// [216] Einbauteil der abgewickelten Wand dem Bogen nach legen: an den Teilstellen `stations` zerschnitten /// ([`slice_x`]) und stückweise linear abgebildet (zwischen zwei Teilstellen auf der Sehne der Versatzlinie, wie die /// Sehnen-Stücke der Wand) — Rahmen, Flügel, Glas und Fensterbank laufen in gleichem Abstand zur Wandflucht. fn bend_on_arc(mut m: RMesh, arc: &crate::wall_arc::WallArc, stations: &[f64]) -> Option { if stations.len() < 2 { return None; } let (positions, indices) = slice_x(&m.positions, &m.indices, stations); if indices.is_empty() { return None; } let last = stations.len() - 2; let map = |x: f64, y: f64| -> [f64; 2] { let k = stations.partition_point(|s| *s <= x).saturating_sub(1).min(last); let (s0, s1) = (stations[k], stations[k + 1]); let (a, b) = (arc.point(s0, y), arc.point(s1, y)); let t = if s1 - s0 > 1e-12 { (x - s0) / (s1 - s0) } else { 0.0 }; [a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t] }; m.positions = positions .chunks_exact(3) .flat_map(|c| { let w = map(c[0], c[1]); [w[0], w[1], c[2]] }) .collect(); m.indices = indices; // Die Sehnen-Facetten glatt, die Kanten der Teile hart. m.crease_angle.get_or_insert(RING_CREASE_DEG); Some(m) } fn emit_opening_meshes_plain(p: &Project, detail: f64, keep: &dyn Fn(&Opening) -> bool) -> Vec { let mut by_wall = p.openings_by_wall(); for v in by_wall.values_mut() { v.retain(|o| keep(o)); } let per: Vec = p.walls.iter().map(|wall| wall_opening_meshes(p, wall, by_wall.get(wall.id.as_str()).map_or(&[][..], |v| v.as_slice()), detail)).collect(); let mut out = assemble_opening_meshes(per); out.extend(surface_window_meshes_with(p, keep)); out } /// Öffnungs-Einbauteile EINER Wand ([58]: der 3D-Cache behält sie je Wand): die Quader je Durchgang /// (`openings3d::wall_mesh_passes`), Türzargen und Formöffnungen. #[derive(Clone, Debug, Default)] pub(crate) struct WallOpeningMeshes { pub passes: [Vec; 3], pub zargen: Vec, pub shaped: Vec, } /// Reihenfolge des ganzen Modells: alle Wände je Durchgang, dann alle Zargen, dann alle Formöffnungen /// (ohne die Dach-/Deckenfenster, s. [`surface_window_meshes_with`]). pub(crate) fn assemble_opening_meshes(per: Vec) -> Vec { let mut lists: [Vec; 5] = Default::default(); for m in per { let [a, b, c] = m.passes; for (k, v) in [a, b, c, m.zargen, m.shaped].into_iter().enumerate() { lists[k].extend(v); } } lists.into_iter().flatten().collect() } /// Dach-/Deckenfenster ([103]) der Öffnungen mit `keep` ([180] Klassen-Filter). pub(crate) fn surface_window_meshes_with(p: &Project, keep: &dyn Fn(&Opening) -> bool) -> Vec { let mut out = Vec::new(); for op in p.openings.as_deref().unwrap_or(&[]).iter().filter(|o| keep(o)) { if let Some(f) = crate::opening_shape::host_frame(p, op) { for (part, positions) in crate::opening_shape::surface_window_boxes(p, op, &f) { out.push(box_mesh(positions, PART_RGB[part as usize])); } } } out } /// Einbauteile der Öffnungen `openings` (in Projekt-Reihenfolge) der Wand `wall`. Formöffnungen ohne Füllkörper: den /// Umriss schneidet der Wandkörper ([216], `wall_body`; auch bei Bogenwänden, hier auf der abgewickelten Ersatzwand). pub(crate) fn wall_opening_meshes(p: &Project, wall: &Wall, openings: &[&Opening], detail: f64) -> WallOpeningMeshes { let body_ops = super::wall_body::body_openings(openings); let mut shaped: Vec = Vec::new(); let mut w = FlatWriter::new(); w.num(detail).num(1.0); // [105] Anhang: Rahmen-Detail je (Wand, Öffnung); Türzargen als eigene Quader. let mut details: Vec = Vec::new(); let mut n_details = 0.0; let mut zargen: Vec = Vec::new(); let wi = 0usize; { w.nums(&[wall.start.x, wall.start.y, wall.end.x, wall.end.y]); let slot = w.slot(); let mut count = 0.0; let wr = wall_ref(p, wall); let (zb, zt) = p.wall_vertical_extent(wall); let mut thickness: Option = None; for &op in openings { let Some((from, to)) = opening_geom::opening_interval(&wr, op.position, op.width) else { continue }; let o_bottom = zb + op.sill_height.max(0.0); let o_top = p.opening_top_under_ceilings(wall, op, o_bottom, zt.min(o_bottom + op.height)); let t = *thickness.get_or_insert_with(|| p.wall_thickness(wall, 0.2)); // Formöffnung ([G3]): Füllkörper, Rahmen und Glas entlang des Umrisses statt der Quader-Teile. if crate::opening_shape::is_shaped(op) { let with_fill = !body_ops.iter().any(|b| std::ptr::eq(*b, op)); shaped.extend(shaped_wall_opening_meshes_with(p, wall, &wr, op, [from, to, o_bottom, o_top], with_fill)); continue; } w.nums(&[from, to, o_bottom, o_top, flag(op.kind != ElementKind::Window), t]); write_opening(&mut w, p, op); let dframe = crate::opening_detail::door_frame_of(p, op); if let Some(f) = crate::opening_detail::frame_of(p, op).filter(|f| f.detailed || dframe.is_some()) { let s4 = |s: crate::opening_detail::Sides| [s.left, s.right, s.top, s.bottom]; details.extend([wi as f64, count]); details.extend(s4(f.sides)); details.extend(s4(f.ext)); details.extend(s4(f.rebate)); details.extend([f.sash_width, f.sash_depth, f.sash_overlap, f.mullion_width, f.transom_width, f.sill_profile, f.shading_profile, flag(dframe.is_some())]); details.extend(dframe.as_ref().map_or([f64::NAN; 4], |df| door_leaf_in_falz(&wr, op, df))); n_details += 1.0; } if let (Some(df), true) = (dframe.as_ref(), detail > 0.0) { door_frame_boxes(&mut zargen, wall, &wr, op, df, (from, to, o_bottom, o_top)); } count += 1.0; } w.set(slot, count); } w.num(n_details); w.nums(&details); let passes = crate::openings3d::opening_mesh_passes_flat(&w.done()).map(|buf| box_meshes_of_flat(&buf)); WallOpeningMeshes { passes, zargen, shaped } } /// Quader aus dem flachen Puffer von `openings3d` (`[n, je Quader: Bauteil, 24 Koordinaten]`). fn box_meshes_of_flat(buf: &[f64]) -> Vec { let n = buf.first().copied().unwrap_or(0.0).max(0.0) as usize; let mut out = Vec::new(); let mut i = 1; for _ in 0..n { if i + 25 > buf.len() { break; } let part = buf[i] as usize; out.push(RMesh { crease_angle: None, positions: buf[i + 1..i + 25].to_vec(), indices: OPENING_BOX_TRIS.to_vec(), kind: "extrusion", color: PART_RGB.get(part).copied().unwrap_or(PART_RGB[1]), vertex_colors: None, material_index: None, owner: Owner::None, }); i += 25; } out } /// Überstand der Schneidkörper (m) bei Rahmen-Ring und Türschwelle. const OVER_MESH: f64 = 0.01; /// Knickwinkel (Grad) des Rahmen-Rings: die Facetten des Bogens glatt. const RING_CREASE_DEG: f64 = 20.0; /// Prisma über dem Umriss `ring` (Öffnungs-Koordinaten) quer von `n0` bis `n1`, abgebildet mit `map(u, v, n)`, als /// Mesh-Eingabe für [`mesh_of`] bzw. direkt als `RMesh` (Platte, Glas). fn prism_mesh(ring: &[crate::opening_shape::P], n0: f64, n1: f64, map: &impl Fn(f64, f64, f64) -> [f64; 3], color: Rgb) -> Option { let m = crate::solid_bool::Mesh::prism(ring, n0, n1); if m.is_empty() { return None; } let positions: Vec = m.positions.chunks_exact(3).flat_map(|c| map(c[0], c[1], c[2])).collect(); Some(RMesh { crease_angle: None, positions, indices: m.indices, kind: "extrusion", color, vertex_colors: None, material_index: None, owner: Owner::None }) } fn mesh_of(m: &RMesh) -> crate::solid_bool::Mesh { crate::solid_bool::Mesh { positions: m.positions.clone(), indices: m.indices.clone() } } fn box_mesh(positions: [f64; 24], color: Rgb) -> RMesh { RMesh { crease_angle: None, positions: positions.to_vec(), indices: OPENING_BOX_TRIS.to_vec(), kind: "extrusion", color, vertex_colors: None, material_index: None, owner: Owner::None } } /// Schichten einer Wand für die Füllkörper einer Formöffnung ([G3]): Dicke (auf `total` skaliert) /// und Farbe, aussen zuerst; ohne Aufbau eine Schicht in Wandfarbe. Dazu je Schicht der /// Schichteinzug [106] (nur gestapelte Schichten wie die Wandbänder): Rücksprung je Seite /// `[links, rechts, oben, unten]` und Zusatzstücke `(seite, [x0, x1, o0, o1])`. struct FillBand { thickness: f64, color: Rgb, shift: [f64; 4], pieces: Vec<(usize, [f64; 4])>, } fn shaped_fill_bands(p: &Project, wall: &Wall, op: &Opening, total: f64) -> Vec { let (bands, stacked) = resolve_wall_bands(p, &wall_material_color_maps(p), wall, true); let sum: f64 = bands.iter().map(|b| b.thickness.max(0.0)).sum(); if !(sum > 1e-9) || bands.iter().all(|b| b.thickness <= 1e-9) { return vec![FillBand { thickness: total, color: WALL_RGB, shift: [0.0; 4], pieces: Vec::new() }]; } let reveal = if stacked { crate::opening_detail::opening_reveal(p, wall, op).filter(|r| r.thick.len() == bands.len()) } else { None }; let mut list: Vec = bands.iter().map(|b| FillBand { thickness: b.thickness.max(0.0) * total / sum, color: b.color, shift: [0.0; 4], pieces: Vec::new() }).collect(); for (k, side) in reveal.iter().flat_map(|r| r.sides.iter().enumerate()) { for a in side { if let Some(fb) = list.get_mut(a.layer) { fb.shift[k] = a.shift; fb.pieces.extend(a.pieces.iter().map(|pc| (k, [pc[0], pc[1], pc[2] * total / sum, pc[3] * total / sum]))); } } } list.retain(|b| b.thickness > 1e-9); list } /// Wandkörper einer Formöffnung als Vierecke in Öffnungskoordinaten mit Lage quer `[n0, n1]` (aussen = /// +total/2) und Farbe: Füllkörper je Schicht zwischen dem (geweiteten) Rechteck-Loch und dem um den /// Schichteinzug versetzten Umriss, Zusatzstücke [106] entlang des Umrisses (je Kante nach ihrer Seite). fn shaped_wall_fill(p: &Project, wall: &Wall, op: &Opening, total: f64, w: f64, h: f64, with_fill: bool) -> Vec<([crate::opening_shape::P; 4], f64, f64, Rgb)> { use crate::opening_shape as os; let Some(poly) = os::opening_outline(op, w, h) else { return Vec::new() }; let half = total / 2.0; let bands = shaped_fill_bands(p, wall, op, total); let mut out = Vec::new(); let mut n_hi = half; for b in &bands { // [216] Mit Körper der Wand ([`super::wall_body`]) füllt der Wandkörper selbst bis zum Umriss. if with_fill { for q in os::layer_fill_quads(&poly, w, h, b.shift) { out.push((q, n_hi - b.thickness, n_hi, b.color)); } } n_hi -= b.thickness; } // [216] Mit Körper der Wand (Anzeige) laufen die Zusatzstücke als je ein Körper je Kantenzug ([`reveal_meshes`]). if !with_fill { return out; } for b in &bands { for &(side, [x0, x1, o0, o1]) in &b.pieces { if !(x1 - x0 > 1e-9 && o1 - o0 > 1e-9) { continue; } let (outer, inner) = (os::inset(&poly, x0), os::inset(&poly, x1)); for (i, q) in os::ring_quads(&outer, &inner).into_iter().enumerate() { if os::edge_side(&poly, i) == side { out.push((q, half - o1, half - o0, b.color)); } } } } out } /// [216] Zusatzstücke des Schichteinzugs [106] einer Formöffnung für die Anzeige: je Stück und Seite EIN Körper über dem /// zusammenhängenden Kantenzug dieser Seite (Aussen- und Innenumriss des Stücks als ein Polygon), statt eines Quaders je /// Umriss-Kante. Die Quader zeigten im 3D auf Leibung und Sturz ein Stein-/Streifenmuster aus lauter Stossfugen. Glatt /// schattiert ([`RING_CREASE_DEG`]); schlägt ein Zug fehl (entartet), bleiben seine Quader. fn reveal_meshes(p: &Project, wall: &Wall, op: &Opening, total: f64, w: f64, h: f64, map: &impl Fn(f64, f64, f64) -> [f64; 3]) -> Vec { use crate::opening_shape as os; let Some(poly) = os::opening_outline(op, w, h) else { return Vec::new() }; let half = total / 2.0; let mut out = Vec::new(); for b in shaped_fill_bands(p, wall, op, total) { for &(side, [x0, x1, o0, o1]) in &b.pieces { if !(x1 - x0 > 1e-9 && o1 - o0 > 1e-9) { continue; } let (outer, inner) = (os::inset(&poly, x0), os::inset(&poly, x1)); let n = outer.len().min(inner.len()).min(poly.len()); let on_side: Vec = (0..n).map(|i| os::edge_side(&poly, i) == side).collect(); let (n0, n1) = (half - o1, half - o0); let boxes = |i: usize, out: &mut Vec| { let q = os::ring_quads(&outer, &inner)[i]; out.push(box_mesh(os::quad_box(&q, n0, n1, map), b.color)); }; if on_side.iter().all(|s| *s) { // Ganzer Umriss auf einer Seite (Rund): kein Anfang des Kantenzugs. (0..n).for_each(|i| boxes(i, &mut out)); continue; } for start in (0..n).filter(|&i| on_side[i] && !on_side[(i + n - 1) % n]) { let len = (0..n).take_while(|k| on_side[(start + k) % n]).count(); let mut ring: Vec = (0..=len).map(|k| outer[(start + k) % n]).collect(); ring.extend((0..=len).rev().map(|k| inner[(start + k) % n])); match prism_mesh(&ring, n0, n1, map, b.color) { Some(mut m) => { m.crease_angle = Some(RING_CREASE_DEG); out.push(m); } None => (0..len).for_each(|k| boxes((start + k) % n, &mut out)), } } } } out } /// Anzahl der Wandkörper (Füllkörper je Schicht, Zusatzstücke) einer Formöffnung — sie stehen in /// [`shaped_wall_opening_meshes`] vor Rahmen und Glas. pub(crate) fn shaped_fill_count(p: &Project, wall: &Wall, op: &Opening, w: f64, h: f64) -> usize { shaped_wall_fill(p, wall, op, p.wall_thickness(wall, 0.2), w, h, true).len() } /// Formöffnung in der Wand ([G3]) als Quader: Füllkörper (je Wandschicht, mit Schichteinzug [106]) zwischen dem /// Rechteck-Loch und dem Umriss, Rahmen-Ring und Glas (bzw. Türblatt) entlang des Umrisses. Rahmenlage /// quer zur Wand wie bei den Rechteck-Öffnungen (`openings3d::frame_normal_range`). Nicht für Formen: /// Anschlag, Fensterbank, Storenkasten, Flügel-/Sprossenteilung (bleiben beim Rechteck). pub(crate) fn shaped_wall_opening_meshes(p: &Project, wall: &Wall, wr: &WallRef, op: &Opening, rect: [f64; 4]) -> Vec { shaped_wall_opening_meshes_with(p, wall, wr, op, rect, true) } /// Wie [`shaped_wall_opening_meshes`]; `with_fill = false`: ohne die Füllkörper zwischen Rechteck und Umriss (der /// Wandkörper deckt sie, [216]); die Zahl der Wandkörper vor Rahmen und Glas ist dann nur die der Zusatzstücke. pub(crate) fn shaped_wall_opening_meshes_with(p: &Project, wall: &Wall, wr: &WallRef, op: &Opening, [from, to, zb, zt]: [f64; 4], with_fill: bool) -> Vec { use crate::opening_shape as os; let (w, h) = (to - from, zt - zb); let Some(poly) = os::opening_outline(op, w, h) else { return Vec::new() }; let (dx, dy) = (wall.end.x - wall.start.x, wall.end.y - wall.start.y); let len = hypot2(dx, dy); if len < 1e-9 { return Vec::new(); } let (ux, uy) = (dx / len, dy / len); // Quer-Achse wie `openings3d` (rechte Normale); die Wandmitte liegt bei −ref_off. let shift = -wr.ref_off; let map = |u: f64, v: f64, n: f64| -> [f64; 3] { let (s, t) = (from + u, n + shift); [wall.start.x + ux * s + uy * t, wall.start.y + uy * s - ux * t, zb + v] }; let half = wr.total / 2.0; // Wandkörper je Schicht (aussen = +half) in deren Farbe. let mut out: Vec = shaped_wall_fill(p, wall, op, wr.total, w, h, with_fill).into_iter().map(|(q, n0, n1, rgb)| box_mesh(os::quad_box(&q, n0, n1, &map), rgb)).collect(); if !with_fill { out.extend(reveal_meshes(p, wall, op, wr.total, w, h, &map)); } let door = op.kind == ElementKind::Door; let ty = if door { p.door_type(op).map(|t| (t.frame_depth, t.inset_from_face.or_else(|| crate::opening_detail::default_frame_inset(p, op)), t.inset_face == Some(DoorTypeInsetFace::Innen), t.inset_face == Some(DoorTypeInsetFace::Mitte), t.frame_width)) } else { p.window_type(op).map(|t| (t.frame_depth, t.inset_from_face.or_else(|| crate::opening_detail::default_frame_inset(p, op)), t.inset_face == Some(WindowTypeInsetFace::Innen), t.inset_face == Some(WindowTypeInsetFace::Mitte), t.frame_width)) }; let glass = PART_RGB[0]; let Some((fd, inset_face, inner, mid, fw)) = ty else { // Ohne Typ: eine Vollscheibe mittig (wie das Rechteck-Fenster ohne Typ). if !door { let g = crate::openings3d::GLASS_HALF_THICK; if !with_fill { if let Some(m) = prism_mesh(&poly, -g, g, &map, glass) { out.push(m); return out; } } for q in os::strip_quads(&poly) { out.push(box_mesh(os::quad_box(&q, -g, g, &map), glass)); } } return out; }; let depth = crate::openings3d::across_wall_depth(fd.unwrap_or(f64::NAN).max(0.0), wr.total); let (n_min, n_max) = crate::openings3d::frame_normal_range(half, depth, inset_face.unwrap_or(0.0), inner, mid); let fw = fw.filter(|v| *v > 0.0).unwrap_or(crate::openings3d::DEFAULT_FRAME_WIDTH).min(w.min(h) / 3.0); let inner_poly = os::inset(&poly, fw); let (inner_glass, color) = if door { let c = (n_min + n_max) / 2.0; let t = crate::openings3d::DOOR_LEAF_THICK / 2.0; ((c - t, c + t), PART_RGB[5]) } else { let n1 = n_max - 0.02; ((n1 - 2.0 * crate::openings3d::GLASS_HALF_THICK, n1), glass) }; // [216] Anzeige (Wandkörper nach dem Umriss): Rahmen als EIN Ring, Glas bzw. Blatt als EINE Platte — keine // Quader je Umriss-Kante (Stossfugen und Streifen im Bogen). Der Export behält die Quader (`with_fill`). if !with_fill { let ring = prism_mesh(&poly, n_min, n_max, &map, PART_RGB[1]).zip(prism_mesh(&inner_poly, n_min - OVER_MESH, n_max + OVER_MESH, &map, PART_RGB[1])); let frame = ring.and_then(|(outer, hole)| { let (a, b) = (crate::solid_bool::Solid::new(&mesh_of(&outer)).ok()?, crate::solid_bool::Solid::new(&mesh_of(&hole)).ok()?); let mut r = a.difference(&b).ok()?; // Tür: unten keine Schwelle (Kante 0 = Boden). if door && op.shape != Some(OpeningShape::Rund) { let q = os::ring_quads(&poly, &inner_poly)[0]; let ext = [[q[0][0] - OVER_MESH, q[0][1] - OVER_MESH], [q[1][0] + OVER_MESH, q[1][1] - OVER_MESH], [q[2][0] + OVER_MESH, q[2][1]], [q[3][0] - OVER_MESH, q[3][1]]]; let cut = prism_mesh(&ext, n_min - OVER_MESH, n_max + OVER_MESH, &map, PART_RGB[1])?; r = r.difference(&crate::solid_bool::Solid::new(&mesh_of(&cut)).ok()?).ok()?; } let m = r.to_mesh_raw(); (!m.is_empty()).then(|| RMesh { crease_angle: Some(RING_CREASE_DEG), positions: m.positions, indices: m.indices, kind: "extrusion", color: PART_RGB[1], vertex_colors: None, material_index: None, owner: Owner::None }) }); let plate = prism_mesh(&inner_poly, inner_glass.0, inner_glass.1, &map, color); if let (Some(frame), Some(plate)) = (frame, plate) { out.push(frame); out.push(plate); return out; } } for (k, q) in os::ring_quads(&poly, &inner_poly).into_iter().enumerate() { // Tür: unten keine Schwelle (Kante 0 = Boden). if door && k == 0 && op.shape != Some(OpeningShape::Rund) { continue; } out.push(box_mesh(os::quad_box(&q, n_min, n_max, &map), PART_RGB[1])); } for q in os::strip_quads(&inner_poly) { out.push(box_mesh(os::quad_box(&q, inner_glass.0, inner_glass.1, &map), color)); } out } /// Türzarge als Quader ([105]): Profil (Grundriss = Schnitt) an beiden Leibungen über die Höhe und /// am Sturz über die Breite; Ecken ohne Überlappung (Leibungsstück endet unter dem Sturzstück). /// Türblatt im Falz ([105]): Einzug ab Rohbaukante (seitlich, oben) und Lage quer zur Wand in der /// Normalen von `openings3d` (rechts der Achse, `t = −Versatz`). fn door_leaf_in_falz(wr: &WallRef, op: &Opening, df: &crate::opening_detail::DoorFrame) -> [f64; 4] { use crate::opening_detail as od; let innen = od::aufschlag_innen(op); let clear = od::profile_clear_x(&od::door_frame_profile(df, wr.total, innen)); let x = (clear - df.falz_width).max(0.0); let (d0, d1) = od::leaf_band(df, wr.total, innen); let face = wr.ref_off - wr.total / 2.0; let off_of = |d: f64| face + if innen { wr.total - d } else { d }; [x, x, -off_of(d0), -off_of(d1)] } fn door_frame_boxes(out: &mut Vec, wall: &Wall, wr: &WallRef, op: &Opening, df: &crate::opening_detail::DoorFrame, (from, to, zb, zt): (f64, f64, f64, f64)) { use crate::opening_detail as od; let (u, n) = wr.frame(); let face = wr.ref_off - wr.total / 2.0; let innen = od::aufschlag_innen(op); let off_of = |d: f64| face + if innen { wr.total - d } else { d }; let s = [wall.start.x, wall.start.y]; let (u, n) = ([u.x, u.y], [n.x, n.y]); for (part, poly) in od::door_frame_profile(df, wr.total, innen) { let rgb = if part == od::FramePart::Frame && df.steel { PART_RGB[4] } else { PART_RGB[1] }; for [x0, x1, d0, d1] in od::ortho_rects(&poly) { let (o0, o1) = (off_of(d0).min(off_of(d1)), off_of(d0).max(off_of(d1))); let boxes = [ od::axis_box(s, u, n, from + x0, from + x1, zb, zt - x1, o0, o1), od::axis_box(s, u, n, to - x1, to - x0, zb, zt - x1, o0, o1), od::axis_box(s, u, n, from + x0, to - x0, zt - x1, zt - x0, o0, o1), ]; for b in boxes.into_iter().flatten() { out.push(RMesh { crease_angle: None, positions: b.to_vec(), indices: OPENING_BOX_TRIS.to_vec(), kind: "extrusion", color: rgb, vertex_colors: None, material_index: None, owner: Owner::None }); } } } }