[109] [101] Schraffuren: Generator als eigenes Crate (hatchgen), render3d-Live-Schnitt/Druck-SVG/SVG-PlanView zeichnen Schichtmuster mit dem Kern-Generator, Putz/Keramik/Faserzement mit Schraffur (Punktfeld), Standards aktualisieren im Ressourcen-Manager

This commit is contained in:
Claude committed 2026-10-30 05:10:50 +00:00
1 parent 907ff65ce6
commit 23442fe7a2
38 files changed
+1004 -74

No files matched your search

File diff suppressed because it is too large. Load diff
-52
View File
@@ -1,52 +0,0 @@
//! `Math.hypot` wie in V8 (Kahan-Summe über die auf den Maximalwert skalierten Beträge). Die
//! 3D-Ableitungen (`stairs3d`, `drawing2d_3d`) lieferten in TS bitgenau diese Werte; `f64::hypot`
//! weicht in der letzten Stelle ab und würde Vergleiche alt/neu unnötig aufweichen.
pub fn hypot(v: &[f64]) -> f64 {
let mut max = 0.0f64;
let mut nan = false;
for &x in v {
let a = x.abs();
if a.is_infinite() {
return f64::INFINITY;
}
if a.is_nan() {
nan = true;
} else if a > max {
max = a;
}
}
if nan {
return f64::NAN;
}
if max == 0.0 {
return 0.0;
}
let (mut sum, mut comp) = (0.0f64, 0.0f64);
for &x in v {
let n = x.abs() / max;
let summand = n * n - comp;
let prelim = sum + summand;
comp = (prelim - sum) - summand;
sum = prelim;
}
sum.sqrt() * max
}
pub fn hypot2(a: f64, b: f64) -> f64 {
hypot(&[a, b])
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn werte() {
assert_eq!(hypot2(3.0, 4.0), 5.0);
assert_eq!(hypot2(0.0, 0.0), 0.0);
assert!(hypot2(f64::NAN, 1.0).is_nan());
assert_eq!(hypot2(f64::INFINITY, f64::NAN), f64::INFINITY);
assert_eq!(hypot(&[2.0, 3.0, 6.0]), 7.0);
}
}
-198
View File
@@ -1,198 +0,0 @@
//! Zahl-Semantik von JavaScript für bitgleiche Portierungen ([K3b] GPU-Kompilierung, Schraffur).
//!
//! - `Math.min/max` liefern bei NaN NaN (Rust `f64::min/max` ignorieren NaN).
//! - `Math.round` rundet halbe Werte Richtung +∞ (Rust `round` weg von 0).
//! - `x | 0` ist ToInt32 (Rest modulo 2³², Rust `as i32` sättigt).
//! - `Math.pow` (`x ** 3`) und Trigonometrie: im Web-Build (wasm32 + Feature "web") ruft der Kern `Math.cos/sin/tan/atan/atan2/acos`
//! der laufenden JS-Engine. Nur so sind die Werte bitgleich zur bisherigen TS-Fassung — V8
//! (glibc-abgeleitete sin/cos), JavaScriptCore (System-libm von macOS) und Rusts libm (musl)
//! unterscheiden sich in einzelnen Fällen in der letzten Stelle. Nativ (`cargo test`, Tauri)
//! gilt die Rust-Standardbibliothek.
#[cfg(all(feature = "web", target_arch = "wasm32"))]
mod trig {
use wasm_bindgen::prelude::wasm_bindgen;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = Math, js_name = cos)]
fn js_cos(x: f64) -> f64;
#[wasm_bindgen(js_namespace = Math, js_name = sin)]
fn js_sin(x: f64) -> f64;
#[wasm_bindgen(js_namespace = Math, js_name = atan2)]
fn js_atan2(y: f64, x: f64) -> f64;
#[wasm_bindgen(js_namespace = Math, js_name = acos)]
fn js_acos(x: f64) -> f64;
#[wasm_bindgen(js_namespace = Math, js_name = pow)]
fn js_pow(x: f64, y: f64) -> f64;
#[wasm_bindgen(js_namespace = Math, js_name = tan)]
fn js_tan(x: f64) -> f64;
#[wasm_bindgen(js_namespace = Math, js_name = atan)]
fn js_atan(x: f64) -> f64;
}
#[inline]
pub fn atan(x: f64) -> f64 {
js_atan(x)
}
#[inline]
pub fn tan(x: f64) -> f64 {
js_tan(x)
}
#[inline]
pub fn pow(x: f64, y: f64) -> f64 {
js_pow(x, y)
}
#[inline]
pub fn cos(x: f64) -> f64 {
js_cos(x)
}
#[inline]
pub fn sin(x: f64) -> f64 {
js_sin(x)
}
#[inline]
pub fn atan2(y: f64, x: f64) -> f64 {
js_atan2(y, x)
}
#[inline]
pub fn acos(x: f64) -> f64 {
js_acos(x)
}
}
#[cfg(not(all(feature = "web", target_arch = "wasm32")))]
mod trig {
#[inline]
pub fn cos(x: f64) -> f64 {
x.cos()
}
#[inline]
pub fn sin(x: f64) -> f64 {
x.sin()
}
#[inline]
pub fn atan2(y: f64, x: f64) -> f64 {
y.atan2(x)
}
#[inline]
pub fn acos(x: f64) -> f64 {
x.acos()
}
#[inline]
pub fn pow(x: f64, y: f64) -> f64 {
x.powf(y)
}
#[inline]
pub fn tan(x: f64) -> f64 {
x.tan()
}
#[inline]
pub fn atan(x: f64) -> f64 {
x.atan()
}
}
pub use trig::{acos, atan, atan2, cos, pow, sin, tan};
/// Endliche Zahl oder 0 (NaN/±∞ → 0; Summen der Kennzahlen/Flächen).
#[inline]
pub fn or_zero(v: f64) -> f64 {
if v.is_finite() {
v
} else {
0.0
}
}
/// `Math.min(a, b)` (NaN gewinnt; −0 < +0).
#[inline]
pub fn min(a: f64, b: f64) -> f64 {
if a.is_nan() || b.is_nan() {
f64::NAN
} else if a < b || (a == b && a.is_sign_negative()) {
a
} else {
b
}
}
/// `Math.max(a, b)` (NaN gewinnt; +0 > −0).
#[inline]
pub fn max(a: f64, b: f64) -> f64 {
if a.is_nan() || b.is_nan() {
f64::NAN
} else if a > b || (a == b && a.is_sign_positive()) {
a
} else {
b
}
}
/// `Math.round(x)`: halbe Werte Richtung +∞, −0 bleibt −0, Werte in (−0.5, 0) → −0.
#[inline]
pub fn round(x: f64) -> f64 {
if !x.is_finite() || x == 0.0 {
return x;
}
if x > 0.0 && x < 0.5 {
return 0.0;
}
if x < 0.0 && x >= -0.5 {
return -0.0;
}
let r = x.ceil();
if r - 0.5 > x {
r - 1.0
} else {
r
}
}
/// ToInt32 (`x | 0`).
#[inline]
pub fn to_int32(x: f64) -> i32 {
// Schneller Weg im i32-Bereich (`as` schneidet wie ToInt32 Richtung 0 ab; NaN → 0).
if (-2147483648.0..=2147483647.0).contains(&x) {
return x as i32;
}
if !x.is_finite() {
return 0;
}
let t = x.trunc();
let m = t.rem_euclid(4294967296.0);
(m as u64 as u32) as i32
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rundung_wie_js() {
assert_eq!(round(2.5), 3.0);
assert_eq!(round(-2.5), -2.0);
assert_eq!(round(-0.5).to_bits(), (-0.0f64).to_bits());
assert_eq!(round(0.49999999999999994), 0.0);
assert_eq!(round(-126.5), -126.0);
assert!(round(f64::NAN).is_nan());
}
#[test]
fn min_max_wie_js() {
assert!(min(1.0, f64::NAN).is_nan());
assert!(max(f64::NAN, 1.0).is_nan());
assert_eq!(max(0.0, -0.0).to_bits(), 0.0f64.to_bits());
assert_eq!(min(0.0, -0.0).to_bits(), (-0.0f64).to_bits());
assert_eq!(max(-1.0, min(1.0, 3.0)), 1.0);
}
#[test]
fn int32_wie_js() {
assert_eq!(to_int32(4294967296.0 + 5.0), 5);
assert_eq!(to_int32(2147483648.0), -2147483648);
assert_eq!(to_int32(-1.7), -1);
assert_eq!(to_int32(f64::NAN), 0);
assert_eq!(to_int32(-4294967297.0), -1);
}
}
+3 -3
View File
@@ -30,7 +30,7 @@ pub mod room_area;
pub mod room_boundary;
pub mod polygon_holes;
pub mod js_fmt;
pub mod jsmath;
pub use dossier_hatch::jsmath;
pub mod stairs3d;
pub mod mesh3d;
pub mod ceilings3d;
@@ -38,8 +38,8 @@ pub mod columns3d;
pub mod drawing2d_3d;
pub mod openings3d;
pub mod wall_mesh_cut;
pub mod jsnum;
pub mod hatch;
pub use dossier_hatch::jsnum;
pub use dossier_hatch::hatch;
pub mod hatch_flat;
pub mod plan;
pub mod plan_buf;
+3
View File
@@ -250,6 +250,9 @@ pub enum HatchPattern {
/// Naturstein ([109]): Diagonale in Doppellinien.
#[serde(rename = "stone")]
Stone,
/// Punktfeld ([109], Putz): feine Punkte im versetzten Raster.
#[serde(rename = "dots")]
Dots,
#[serde(other)]
#[ts(skip)]
Unknown,
+1
View File
@@ -2051,6 +2051,7 @@ fn write_hatch_words(out: &mut Vec<f64>, h: &HatchRender, color: f64) {
HatchPattern::Earth => 9.0,
HatchPattern::Gravel => 10.0,
HatchPattern::Stone => 11.0,
HatchPattern::Dots => 12.0,
_ => 2.0,
};
let kind = match h.kind {
+1 -1
View File
@@ -527,7 +527,7 @@ fn dash_seg(a: V2, b: V2, cycle: &[f64]) -> Vec<(V2, V2)> {
fn is_curve_pattern(p: Pattern) -> bool {
matches!(
p,
Pattern::Insulation | Pattern::InsulationHard | Pattern::InsulationWave | Pattern::Timber | Pattern::Earth | Pattern::Gravel | Pattern::Stone
Pattern::Insulation | Pattern::InsulationHard | Pattern::InsulationWave | Pattern::Timber | Pattern::Earth | Pattern::Gravel | Pattern::Stone | Pattern::Dots
)
}
+19 -4
View File
@@ -245,10 +245,18 @@ fn fill_parallel(poly: &[V2], angle: f64, spacing: f64, out: &mut Vec<(V2, V2)>)
}
}
/// Muster, die der Kern-Generator als Linien/Polylinien erzeugt (Schichtmuster, Streu-/Punktmuster, Doppellinien).
fn is_generated(p: Pattern) -> bool {
matches!(
p,
Pattern::Insulation | Pattern::InsulationHard | Pattern::InsulationWave | Pattern::Timber | Pattern::Earth | Pattern::Gravel | Pattern::Stone | Pattern::Dots
)
}
/// Schraffurlinien in Papier-mm (`hatchLines`).
fn hatch_lines(poly_mm: &[V2], h: &Hatch, mm_per_m: f64) -> Vec<(V2, V2)> {
let unit_mm = (1.0 / 90.0) * mm_per_m;
let tile_mm = (if h.pattern == Pattern::Insulation { 10.0 } else { 8.0 }) * h.scale * unit_mm;
let tile_mm = 8.0 * h.scale * unit_mm;
let spacing = jsnum::max(0.5, tile_mm);
let angle = (h.angle * PI) / 180.0;
let mut segs = Vec::new();
@@ -256,9 +264,6 @@ fn hatch_lines(poly_mm: &[V2], h: &Hatch, mm_per_m: f64) -> Vec<(V2, V2)> {
if h.pattern == Pattern::Crosshatch {
fill_parallel(poly_mm, angle + PI / 2.0, spacing, &mut segs);
}
if h.pattern == Pattern::Insulation {
fill_parallel(poly_mm, angle - PI / 4.0, spacing, &mut segs);
}
segs
}
@@ -351,6 +356,16 @@ fn entry(out: &mut Out, view: &View, i: usize, hatches: &[(i32, Hatch, i32)], m:
let (a, b) = (run[0], run[run.len() - 1]);
out.el(1, "line", &line_attrs(m.at(a.x, a.y), m.at(b.x, b.y), &hatch_color, pen, Some(&hdash)));
}
} else if is_generated(h.pattern) {
// Schicht-/Streumuster ([109]): dieselben Linien wie Szene und Chunk-Pfad (Kern-Generator).
for run in hatch::build_hatch_runs(&pts, &h) {
let p: Vec<V2> = run.iter().map(|q| m.at(q.x, q.y)).collect();
match p.len() {
0 | 1 => {}
2 => out.el(1, "line", &line_attrs(p[0], p[1], &hatch_color, pen, Some(&hdash))),
_ => out.el(1, "polyline", &polyline_attrs(&p, &hatch_color, pen, Some(&hdash))),
}
}
} else {
// Strichmuster der Schraffur (null und [] gleich: keins).
for (a, b) in hatch_lines(&pts_mm, &h, k) {
+6
View File
@@ -84,6 +84,12 @@ pub fn run_root(lk: &crate::document::Lookup, name: &str, text: &str) -> Result<
"penDeleteOps" => json(&crate::pens::delete_pen_ops(root, text)),
// Standard-Bibliothek ([100]–[102], `standards.rs`): fehlende Standard-Ressourcen als Ops.
"standardsMergeOps" => json(&crate::standards::merge_ops(root)),
// „Standards aktualisieren" ([109]): Abweichungen vom heutigen Standard / Ops für die gewählten (`text` = JSON-Liste `"<Liste>:<id>"`).
"standardsUpdatePlan" => json(&crate::standards::update_plan(root)),
"standardsUpdateOps" => {
let sel: Vec<String> = serde_json::from_str(text).map_err(|e| format!("standardsUpdateOps: {e}"))?;
json(&crate::standards::update_ops(root, &sel))
}
"penSeedOps" => {
let a: serde_json::Value = serde_json::from_str(text).map_err(|e| format!("penSeedOps: {e}"))?;
json(&crate::pens::seed_standard_ops(root, a["id"].as_str().unwrap_or(""), a["name"].as_str().unwrap_or("")))
+284 -4
View File
@@ -104,6 +104,14 @@ pub fn hatches() -> Vec<Value> {
// Holzfaser: Welle über die Dicke.
layer("std-h-holzfaser", "SIA Dämmung Holzfaser", "insulationWave", 1.0),
solid("std-h-gips", "SIA Gips", "#cfcfcf"),
// Putz (innen/aussen): feines, ruhiges Punktfeld entlang der Schicht (Teilung 2 cm). Eine Schraffur
// ohne Massstabsgrenze: in 1:100 und gröber wird sie ein feiner Grauton, wer Putz dort leer will,
// wählt „SIA Ohne Schraffur".
json!({"id": "std-h-putz", "name": "SIA Putz", "pattern": "dots", "scale": 0.4, "angle": 0, "relativeToWall": true, "color": INK, "lineWeight": 0.1}),
// Keramik (Fliesen, Platten): feines Kreuzraster 45°.
lines("std-h-keramik", "SIA Keramik", "crosshatch", 0.015, 45.0, false, 0.1, ""),
// Faserzement: dichte Diagonale 135° (Plattenwerkstoff, gegenläufig zu Backstein/Beton).
lines("std-h-faserzement", "SIA Faserzement", "diagonal", 0.02, 135.0, false, 0.1, ""),
// Estrich/Überzug: feines Punktfeld (punktierte Diagonale).
lines("std-h-estrich", "SIA Estrich", "diagonal", 0.025, 45.0, false, 0.1, "std-ls-schraffur-punkt"),
solid("std-h-stahl", "SIA Stahl", INK),
@@ -166,12 +174,12 @@ pub fn components() -> Vec<Value> {
Mat { id: "std-c-holzfaser", name: "Holzfaser", abbrev: "HF", fill: "#ffffff", elev: "#b08a5a", hatch: "std-h-holzfaser", lambda: Some(0.04), density: Some(140.0), rf: "RF3", tex: None, prio: 24.0, pattern: None },
Mat { id: "std-c-trittschall", name: "Trittschalldämmung", abbrev: "TSD", fill: "#ffffff", elev: "#efe6a8", hatch: "std-h-daemmung-weich", lambda: Some(0.035), density: Some(100.0), rf: "RF1", tex: None, prio: 19.0, pattern: None },
Mat { id: "std-c-gipskarton", name: "Gipskarton", abbrev: "GK", fill: "#cfcfcf", elev: "#f4f4f2", hatch: "std-h-gips", lambda: Some(0.25), density: Some(800.0), rf: "RF1", tex: Some(("Plaster001", 1.0)), prio: 12.0, pattern: None },
Mat { id: "std-c-innenputz", name: "Innenputz", abbrev: "IP", fill: "#ffffff", elev: "#f5f3ee", hatch: "std-h-ohne", lambda: Some(0.7), density: Some(1400.0), rf: "RF1", tex: Some(("Plaster001", 1.0)), prio: 10.0, pattern: None },
Mat { id: "std-c-aussenputz", name: "Aussenputz", abbrev: "AP", fill: "#ffffff", elev: "#e9e3d6", hatch: "std-h-ohne", lambda: Some(0.87), density: Some(1800.0), rf: "RF1", tex: Some(("Plaster001", 1.0)), prio: 10.0, pattern: None },
Mat { id: "std-c-innenputz", name: "Innenputz", abbrev: "IP", fill: "#ffffff", elev: "#f5f3ee", hatch: "std-h-putz", lambda: Some(0.7), density: Some(1400.0), rf: "RF1", tex: Some(("Plaster001", 1.0)), prio: 10.0, pattern: None },
Mat { id: "std-c-aussenputz", name: "Aussenputz", abbrev: "AP", fill: "#ffffff", elev: "#e9e3d6", hatch: "std-h-putz", lambda: Some(0.87), density: Some(1800.0), rf: "RF1", tex: Some(("Plaster001", 1.0)), prio: 10.0, pattern: None },
Mat { id: "std-c-zementestrich", name: "Zementestrich", abbrev: "ZE", fill: "#ffffff", elev: "#cfcac0", hatch: "std-h-estrich", lambda: Some(1.4), density: Some(2000.0), rf: "RF1", tex: Some(("Concrete048", 2.0)), prio: 30.0, pattern: None },
// Bodenbeläge ([100]): Muster im Grundriss (Oberseite der Decke) und im 3D.
Mat { id: "std-c-parkett", name: "Parkett", abbrev: "PA", fill: "#ffffff", elev: "#c9a06b", hatch: "std-h-holz", lambda: Some(0.13), density: Some(650.0), rf: "RF3", tex: Some(("Wood095", 1.0)), prio: 8.0, pattern: sp("runningBond", None, 0.6, Some(0.1), None) },
Mat { id: "std-c-keramik", name: "Keramikplatten", abbrev: "KP", fill: "#ffffff", elev: "#d8d4cc", hatch: "std-h-ohne", lambda: Some(1.3), density: Some(2300.0), rf: "RF1", tex: None, prio: 9.0, pattern: sp("grid", None, 0.3, Some(0.3), None) },
Mat { id: "std-c-keramik", name: "Keramikplatten", abbrev: "KP", fill: "#ffffff", elev: "#d8d4cc", hatch: "std-h-keramik", lambda: Some(1.3), density: Some(2300.0), rf: "RF1", tex: None, prio: 9.0, pattern: sp("grid", None, 0.3, Some(0.3), None) },
Mat { id: "std-c-glas", name: "Glas", abbrev: "GL", fill: "#d6ecf3", elev: "#a9c8d4", hatch: "std-h-ohne", lambda: Some(1.0), density: Some(2500.0), rf: "RF1", tex: None, prio: 70.0, pattern: None },
Mat { id: "std-c-stahl", name: "Stahl", abbrev: "ST", fill: INK, elev: "#7d8287", hatch: "std-h-stahl", lambda: Some(50.0), density: Some(7850.0), rf: "RF1", tex: Some(("Metal063", 1.0)), prio: 110.0, pattern: None },
Mat { id: "std-c-aluminium", name: "Aluminium", abbrev: "AL", fill: "#8c8c8c", elev: "#b9bcbf", hatch: "std-h-alu", lambda: Some(160.0), density: Some(2700.0), rf: "RF1", tex: Some(("Metal063", 1.0)), prio: 105.0, pattern: None },
@@ -185,7 +193,7 @@ pub fn components() -> Vec<Value> {
Mat { id: "std-c-boden-deckel", name: "Holzschalung Boden-Deckel", abbrev: "BD", fill: "#ffffff", elev: "#a9824f", hatch: "std-h-holz", lambda: Some(0.13), density: Some(450.0), rf: "RF3", tex: Some(("WoodSiding008", 1.0)), prio: 41.0, pattern: sp("boardBatten", Some("vertical"), 0.2, None, Some(0.06)) },
Mat { id: "std-c-schindeln", name: "Holzschindeln", abbrev: "SCH", fill: "#ffffff", elev: "#9c8566", hatch: "std-h-holz", lambda: Some(0.13), density: Some(450.0), rf: "RF3", tex: Some(("Wood095", 0.5)), prio: 41.0, pattern: sp("shingles", None, 0.1, Some(0.12), None) },
Mat { id: "std-c-trapezblech", name: "Trapezblech", abbrev: "TB", fill: INK, elev: "#9aa1a6", hatch: "std-h-stahl", lambda: Some(50.0), density: Some(7850.0), rf: "RF1", tex: Some(("CorrugatedSteel005", 1.0)), prio: 108.0, pattern: sp("trapezoid", Some("vertical"), 0.2, None, Some(0.06)) },
Mat { id: "std-c-faserzement", name: "Faserzementplatten", abbrev: "FZ", fill: "#ffffff", elev: "#a6a8a6", hatch: "std-h-ohne", lambda: Some(0.6), density: Some(1800.0), rf: "RF1", tex: None, prio: 42.0, pattern: sp("grid", None, 1.25, Some(0.625), None) },
Mat { id: "std-c-faserzement", name: "Faserzementplatten", abbrev: "FZ", fill: "#ffffff", elev: "#a6a8a6", hatch: "std-h-faserzement", lambda: Some(0.6), density: Some(1800.0), rf: "RF1", tex: None, prio: 42.0, pattern: sp("grid", None, 1.25, Some(0.625), None) },
Mat { id: "std-c-tonziegel", name: "Tonziegel", abbrev: "ZI", fill: "#ffffff", elev: "#b3583c", hatch: "std-h-backstein", lambda: Some(1.0), density: Some(1900.0), rf: "RF1", tex: Some(("RoofingTiles013A", 1.0)), prio: 15.0, pattern: sp("grid", Some("horizontal"), 0.21, Some(0.34), None) },
];
list.iter()
@@ -263,6 +271,11 @@ fn remap(v: &mut Value, key: &str, ids: &HashMap<String, String>) {
/// gleichem Namen (Gross-/Kleinschreibung egal); Verweise neuer Einträge (Schraffur → Linienstil, Bauteil →
/// Schraffur, Aufbau → Bauteil/Fugen-Linienstil) zeigen dann auf den vorhandenen Eintrag.
pub fn merge_ops(root: &Map<String, Value>) -> Vec<Value> {
merge_plan(root).0
}
/// Wie [`merge_ops`], dazu die Umleitung Standard-id → vorhandene id (gleichnamige Einträge).
fn merge_plan(root: &Map<String, Value>) -> (Vec<Value>, HashMap<String, String>) {
let mut ids: HashMap<String, String> = HashMap::new();
let mut ops = Vec::new();
for (key, items) in library() {
@@ -299,6 +312,175 @@ pub fn merge_ops(root: &Map<String, Value>) -> Vec<Value> {
ops.push(json!({"kind": "setField", "key": key, "value": add}));
}
}
(ops, ids)
}
// ── „Standards aktualisieren" ([109]) ────────────────────────────────────────
//
// Projekte, die mit älteren Standards angelegt wurden, behalten deren Definitionen („Standards laden"
// ergänzt nur). „Aktualisieren" gleicht Standard-Einträge (gleiche id wie ein Eintrag der Bibliothek) mit
// der heutigen Definition ab. Regel:
// - Verglichen werden nur die vom Standard geführten Felder (`managed`); Name und eigene Zusatzfelder
// bleiben unangetastet. Ein vom Standard geführtes Feld, das die neue Definition nicht mehr hat, wird
// entfernt (z. B. ein früherer `lineStyleId`).
// - Ob jemand einen Standard-Eintrag selbst geändert hat, lässt sich nicht erkennen (frühere Fassungen sind
// nicht gespeichert). Darum nennt der Plan jede Abweichung einzeln; vorgewählt sind alle Einträge,
// deren Name noch dem Standard entspricht, umbenannte (`renamed`) sind abgewählt. Gewählt wird im Dialog.
// - Verweise (Schraffur, Linienstil, Bauteil) werden nur gesetzt, wenn das Ziel im Projekt existiert bzw.
// gleichzeitig ergänzt wird; fehlende Standard-Einträge werden wie bei „Standards laden" angehängt.
type Valid<'a> = HashMap<&'a str, std::collections::HashSet<String>>;
/// Felder, die der Standard je Liste führt.
fn managed(key: &str) -> &'static [&'static str] {
match key {
"lineStyles" => &["color", "dash", "kind", "zigzag"],
"hatches" => &[
"pattern", "scale", "angle", "color", "lineWeight", "relativeToWall", "lineStyleId", "kind", "lines", "density", "lengthMin", "lengthMax", "seed",
],
"components" => &[
"color", "background", "hatchId", "viewHatchId", "joinPriority", "fireClass", "elevationColor", "standardId", "lambda", "density", "material", "surfacePattern",
"abbrev",
],
_ => &["layers"],
}
}
/// Zahlen gleich nach Wert (1 == 1.0), sonst strukturell.
fn same(a: &Value, b: &Value) -> bool {
match (a, b) {
(Value::Number(x), Value::Number(y)) => x.as_f64() == y.as_f64(),
(Value::Array(x), Value::Array(y)) => x.len() == y.len() && x.iter().zip(y).all(|(p, q)| same(p, q)),
(Value::Object(x), Value::Object(y)) => x.len() == y.len() && x.iter().all(|(k, v)| y.get(k).is_some_and(|w| same(v, w))),
_ => a == b,
}
}
/// Ziel eines Verweises im Projekt (nach Umleitung gleichnamiger Einträge), sonst `None`.
fn ref_target(ref_key: &str, id: &str, valid: &Valid, ids: &HashMap<String, String>) -> Option<String> {
let list = match ref_key {
"lineStyleId" | "jointLineStyleId" => "lineStyles",
"hatchId" | "viewHatchId" => "hatches",
_ => "components",
};
let id = ids.get(id).map(String::as_str).unwrap_or(id);
valid.get(list).is_some_and(|v| v.contains(id)).then(|| id.to_string())
}
/// Neue Fassung eines vorhandenen Standard-Eintrags (nur geführte Felder; Verweise auf fehlende Ziele
/// bleiben wie sie sind) und die Namen der geänderten Felder.
fn updated(key: &str, old: &Value, std: &Value, valid: &Valid, ids: &HashMap<String, String>) -> (Value, Vec<String>) {
let mut new = old.clone();
let mut fields = Vec::new();
for f in managed(key) {
let mut want = std.get(*f).cloned();
if let Some(Value::String(id)) = &want {
if matches!(*f, "lineStyleId" | "hatchId" | "viewHatchId") {
match ref_target(f, id, valid, ids) {
Some(ok) => want = Some(json!(ok)),
None => continue, // Ziel fehlt: Verweis bleibt
}
}
}
if *f == "layers" {
if let Some(Value::Array(ls)) = &mut want {
for l in ls {
for rk in ["componentId", "jointLineStyleId"] {
if let Some(id) = l[rk].as_str().map(String::from) {
match ref_target(rk, &id, valid, ids) {
Some(ok) => l[rk] = json!(ok),
None => return (old.clone(), Vec::new()),
}
}
}
}
}
}
let have = old.get(*f);
let differs = match (&want, have) {
(Some(w), Some(h)) => !same(w, h),
(None, None) => false,
(None, Some(h)) => !h.is_null(),
(Some(w), None) => !w.is_null(),
};
if differs {
fields.push((*f).to_string());
match want {
Some(w) => new[*f] = w,
None => {
if let Some(o) = new.as_object_mut() {
o.remove(*f);
}
}
}
}
}
(new, fields)
}
/// Eine Abweichung: (Liste, id, Name, geänderte Felder, umbenannt, neue Fassung).
type Change = (String, String, String, Vec<String>, bool, Value);
fn update_changes(root: &Map<String, Value>) -> (Vec<Change>, usize) {
let (merge, ids) = merge_plan(root);
let mut valid: Valid = HashMap::new();
let mut added = 0;
for (key, _) in library() {
let set = valid.entry(key).or_default();
if let Some(a) = root.get(key).and_then(Value::as_array) {
set.extend(a.iter().filter_map(|e| e["id"].as_str().map(String::from)));
}
for op in &merge {
let list = match op["kind"].as_str() {
Some("add") if op["collection"] == key => op["items"].as_array(),
Some("setField") if op["key"] == key => op["value"].as_array(),
_ => None,
};
for it in list.into_iter().flatten() {
added += 1;
if let Some(i) = it["id"].as_str() {
set.insert(i.to_string());
}
}
}
}
let mut out = Vec::new();
for (key, items) in library() {
let Some(existing) = root.get(key).and_then(Value::as_array) else { continue };
for std in &items {
let Some(id) = std["id"].as_str() else { continue };
let Some(old) = existing.iter().find(|e| e["id"] == id) else { continue };
let (new, fields) = updated(key, old, std, &valid, &ids);
if fields.is_empty() {
continue;
}
let name = old["name"].as_str().unwrap_or(id).to_string();
let renamed = name_key(old) != name_key(std);
out.push((key.to_string(), id.to_string(), name, fields, renamed, new));
}
}
(out, added)
}
/// Abweichungen der Standard-Einträge im Projekt von der heutigen Bibliothek: je Eintrag
/// `{collection, id, name, fields, renamed}`; dazu `added` = Anzahl fehlender Standard-Einträge.
pub fn update_plan(root: &Map<String, Value>) -> Value {
let (changes, added) = update_changes(root);
json!({
"added": added,
"changes": changes.iter().map(|c| json!({"collection": c.0, "id": c.1, "name": c.2, "fields": c.3, "renamed": c.4})).collect::<Vec<_>>(),
})
}
/// Ops für „Standards aktualisieren": fehlende Standards anhängen und die gewählten Einträge (`selected` =
/// `"<Liste>:<id>"`) ersetzen — ein Undo-Schritt. Unbekannte Auswahl wird ignoriert.
pub fn update_ops(root: &Map<String, Value>, selected: &[String]) -> Vec<Value> {
let mut ops = merge_ops(root);
for (key, id, _, _, _, new) in update_changes(root).0 {
if selected.contains(&format!("{key}:{id}")) {
ops.push(json!({"kind": "replace", "collection": key, "entity": new}));
}
}
ops
}
@@ -361,6 +543,104 @@ mod tests {
}
}
#[test]
fn putz_keramik_faserzement_haben_schraffur() {
let r = apply(&empty_root(), &merge_ops(&empty_root()));
let p: Project = serde_json::from_value(Value::Object(r)).unwrap();
for (comp, hatch) in [("std-c-innenputz", "std-h-putz"), ("std-c-aussenputz", "std-h-putz"), ("std-c-keramik", "std-h-keramik"), ("std-c-faserzement", "std-h-faserzement")] {
assert_eq!(p.components.iter().find(|c| c.id == comp).unwrap().hatch_id, hatch);
}
assert_eq!(p.hatches.iter().find(|h| h.id == "std-h-putz").unwrap().pattern, crate::model::HatchPattern::Dots);
}
fn alt_projekt() -> Map<String, Value> {
// Projekt mit Standards vom 29.10.: alte EPS-Schraffur, Putz ohne Schraffur, eine umbenannte und
// eine eigene Schraffur.
let mut root = apply(&empty_root(), &merge_ops(&empty_root()));
let hs = root.get_mut("hatches").unwrap().as_array_mut().unwrap();
hs.retain(|h| h["id"] != "std-h-putz");
for h in hs.iter_mut() {
if h["id"] == "std-h-eps" {
h["pattern"] = json!("insulation");
}
if h["id"] == "std-h-xps" {
h["name"] = json!("Meine XPS");
h["pattern"] = json!("diagonal");
}
}
hs.push(json!({"id": "mine", "name": "Eigene", "pattern": "diagonal", "scale": 1, "angle": 0, "color": "#000"}));
for c in root.get_mut("components").unwrap().as_array_mut().unwrap() {
if c["id"] == "std-c-innenputz" {
c["hatchId"] = json!("std-h-ohne");
}
}
root
}
fn apply_all(root: &Map<String, Value>, ops: &[Value]) -> Map<String, Value> {
let mut r = root.clone();
for op in ops {
match op["kind"].as_str().unwrap() {
"add" => r.get_mut(op["collection"].as_str().unwrap()).unwrap().as_array_mut().unwrap().extend(op["items"].as_array().unwrap().iter().cloned()),
"setField" => {
r.insert(op["key"].as_str().unwrap().into(), op["value"].clone());
}
"replace" => {
let list = r.get_mut(op["collection"].as_str().unwrap()).unwrap().as_array_mut().unwrap();
let i = list.iter().position(|e| e["id"] == op["entity"]["id"]).unwrap();
list[i] = op["entity"].clone();
}
k => panic!("{k}"),
}
}
r
}
#[test]
fn aktualisieren_listet_abweichungen_und_ersetzt_gewaehlte() {
let root = alt_projekt();
let plan = update_plan(&root);
assert_eq!(plan["added"], 1, "Putz-Schraffur fehlt");
let ch = plan["changes"].as_array().unwrap();
let find = |id: &str| ch.iter().find(|c| c["id"] == id);
assert_eq!(find("std-h-eps").unwrap()["fields"], json!(["pattern"]));
assert_eq!(find("std-h-eps").unwrap()["renamed"], false);
assert_eq!(find("std-h-xps").unwrap()["renamed"], true, "umbenannt: abgewählt");
assert!(find("std-h-xps").unwrap()["fields"].as_array().unwrap().iter().any(|f| f == "pattern"));
assert_eq!(find("std-c-innenputz").unwrap()["fields"], json!(["hatchId"]), "Verweis zeigt künftig auf die ergänzte Putz-Schraffur");
assert!(find("mine").is_none() && find("std-h-beton").is_none(), "Eigenes und Unverändertes nicht");
let ops = update_ops(&root, &["hatches:std-h-eps".to_string(), "components:std-c-innenputz".to_string()]);
let after = apply_all(&root, &ops);
let get = |k: &str, id: &str| after[k].as_array().unwrap().iter().find(|e| e["id"] == id).unwrap().clone();
assert_eq!(get("hatches", "std-h-eps")["pattern"], "insulationHard");
assert_eq!(get("hatches", "std-h-xps")["pattern"], "diagonal", "nicht gewählt: bleibt");
assert_eq!(get("components", "std-c-innenputz")["hatchId"], "std-h-putz");
assert!(after["hatches"].as_array().unwrap().iter().any(|h| h["id"] == "std-h-putz"));
let left = update_plan(&after);
assert_eq!(left["added"], 0);
assert!(left["changes"].as_array().unwrap().iter().all(|c| c["id"] == "std-h-xps"));
let all: Vec<String> = left["changes"].as_array().unwrap().iter().map(|c| format!("{}:{}", c["collection"].as_str().unwrap(), c["id"].as_str().unwrap())).collect();
let done = apply_all(&after, &update_ops(&after, &all));
assert!(update_plan(&done)["changes"].as_array().unwrap().is_empty());
let p: Project = serde_json::from_value(Value::Object(done)).unwrap();
assert!(crate::project_refs::dangling_refs(&p).is_empty());
}
#[test]
fn aktualisieren_entfernt_nicht_mehr_gefuehrte_felder_und_laesst_zusatzfelder() {
let mut root = apply(&empty_root(), &merge_ops(&empty_root()));
for h in root.get_mut("hatches").unwrap().as_array_mut().unwrap() {
if h["id"] == "std-h-eps" {
h["lineStyleId"] = json!("std-ls-voll");
h["myNote"] = json!("bleibt");
}
}
let after = apply_all(&root, &update_ops(&root, &["hatches:std-h-eps".to_string()]));
let eps = after["hatches"].as_array().unwrap().iter().find(|h| h["id"] == "std-h-eps").unwrap();
assert!(eps.get("lineStyleId").is_none());
assert_eq!(eps["myNote"], "bleibt");
}
#[test]
fn zweites_laden_verdoppelt_nichts() {
let once = apply(&empty_root(), &merge_ops(&empty_root()));
+9 -4
View File
@@ -42,10 +42,15 @@ fn pattern_id(p: HatchPattern) -> f64 {
HatchPattern::Solid => 1.0,
HatchPattern::Diagonal => 2.0,
HatchPattern::Crosshatch => 3.0,
HatchPattern::Insulation | HatchPattern::InsulationHard | HatchPattern::InsulationWave => 4.0,
// 3D-Schnittflächen (render3d) kennen nur die Grundmuster: nächstliegendes ([109]).
HatchPattern::Timber | HatchPattern::Earth => 3.0,
HatchPattern::Gravel | HatchPattern::Stone => 2.0,
// Ab 4: Muster-Code des Kern-Generators (render3d zeichnet die Linien selbst, [109]).
HatchPattern::Insulation => 4.0,
HatchPattern::InsulationHard => 6.0,
HatchPattern::InsulationWave => 7.0,
HatchPattern::Timber => 8.0,
HatchPattern::Earth => 9.0,
HatchPattern::Gravel => 10.0,
HatchPattern::Stone => 11.0,
HatchPattern::Dots => 12.0,
_ => 0.0,
}
}