[K3d] Plan in Rust: Primitive-Typ (JSON wie TS) und Attribut-/Schraffur-Auflösung als Grundlage

This commit is contained in:
karim committed 2026-10-28 02:31:54 +01:00
1 parent af0e0fc49b
commit 976acbee88
5 files changed
+369

No files matched your search

+3
View File
@@ -117,6 +117,9 @@ truck (`trucksolid`) bleibt eigenes WASM-Modul (Panic-Isolation), wird vom Kern
- ○ **[K3d] Aufräumen:** TS-Primitive-Erzeugung löschen, SVG/PDF/DXF-Export lesen den Puffer. Hängt an [K4]: Rust braucht das
typisierte Modell (Bauteil-Typen, Schraffuren, Linienstile, Kategorien) und einen Plan-Puffer v2 mit Pick-IDs + vollem `Primitive`-Umfang
(Text, Schraffur-Optionen, Zickzack/Motiv); die Verbraucher von `Primitive[]` (SVG/PDF/DXF, Auswahl, Overlay) lesen dann den Puffer.
Stand 28.10.: Rust-Grundlage `core/src/plan/{prim,style}.rs` (Primitive-Typ mit TS-gleicher JSON-Form, Auflösung By-Layer/By-Object,
`resolve_hatch`). Offen: Overrides-Engine, Raum-Stempel/richText, Domänen (Decken, Dächer, Räume, Wände+Öffnungen, Treppen, Stützen,
Körper, Kontext, Schnittlinien), Orchestrator (drawings2d bleibt vorerst TS), Plan-Puffer v2 (GPU-Chunk-Cache braucht Inhalts-Hash statt Objekt-Identität).
Nachteile (bewusst): Übergang mit doppelter Datenhaltung, Format muss TS/Rust gleich bleiben
(Versionsnummer + Hin-und-zurück-Tests), Text-Layout bleibt bis [K4] in TS.
- ◐ **[K4] Modell und Ops in Rust** [2]; TS bekommt die Typen generiert (z. B. `ts-rs`). · XL
+1
View File
@@ -37,6 +37,7 @@ pub mod wall_mesh_cut;
pub mod jsnum;
pub mod hatch;
pub mod hatch_flat;
pub mod plan;
pub mod plan_buf;
pub mod project_format;
pub mod queries;
+5
View File
@@ -0,0 +1,5 @@
//! Plan-Erzeugung auf dem Projekt-Dokument ([K3d]/[K4]): Rust-Gegenstück zu `src/plan/generatePlan.ts`
//! und `generatePlan/*.ts`.
pub mod prim;
pub mod style;
+198
View File
@@ -0,0 +1,198 @@
//! Plan-Primitive ([K3d]/[K4]): das Austauschformat zwischen Plan-Erzeugung und allen Verbrauchern
//! (Gegenstück zu `Primitive` in `src/plan/generatePlan/shared.ts`). Die JSON-Form ist
//! Zeichen für Zeichen die der TS-Objekte (camelCase, fehlende Felder fehlen).
use crate::model::*;
use serde::Serialize;
/// Aufgelöste Schraffur eines Polygons (`HatchRender`).
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct HatchRender {
pub pattern: HatchPattern,
pub scale: f64,
pub angle: f64,
#[serde(skip_serializing_if = "Option::is_none")]
pub origin: Option<Vec2>,
pub color: String,
pub line_weight: f64,
pub dash: Option<Vec<f64>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub kind: Option<HatchStyleKind>,
#[serde(skip_serializing_if = "Option::is_none")]
pub lines: Option<HatchStyleLines>,
#[serde(skip_serializing_if = "Option::is_none")]
pub image: Option<HatchStyleImage>,
#[serde(skip_serializing_if = "Option::is_none")]
pub gradient: Option<HatchStyleGradient>,
#[serde(skip_serializing_if = "Option::is_none")]
pub seed: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub density: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub length_min: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub length_max: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub motif: Option<HatchStyleMotif>,
}
/// Zeile unter dem Raum-Stempel.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct StampLine {
pub text: String,
pub align: Align,
}
/// `dash?: number[] | null`: fehlt (`None`), `null` (`Some(None)`) oder Muster.
pub type Dash = Option<Option<Vec<f64>>>;
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(tag = "kind")]
pub enum Primitive {
#[serde(rename = "polygon", rename_all = "camelCase")]
Polygon {
pts: Vec<Vec2>,
fill: String,
stroke: String,
stroke_width_mm: f64,
hatch: HatchRender,
#[serde(skip_serializing_if = "Option::is_none")]
greyed: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
opacity: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
no_stroke_edges: Option<Vec<f64>>,
#[serde(skip_serializing_if = "Option::is_none")]
wall_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
drawing_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
ceiling_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
opening_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
stair_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
room_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
extruded_solid_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
context_object_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
column_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
roof_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
section_line_id: Option<String>,
},
#[serde(rename = "line", rename_all = "camelCase")]
Line {
a: Vec2,
b: Vec2,
cls: String,
weight_mm: f64,
#[serde(skip_serializing_if = "Option::is_none")]
dash: Dash,
#[serde(skip_serializing_if = "Option::is_none")]
zigzag: Option<LineStyleZigzag>,
#[serde(skip_serializing_if = "Option::is_none")]
motif: Option<LineStyleMotif>,
#[serde(skip_serializing_if = "Option::is_none")]
color: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
drawing_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
opening_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
stair_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
greyed: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
opacity: Option<f64>,
},
#[serde(rename = "arc", rename_all = "camelCase")]
Arc {
center: Vec2,
from: Vec2,
to: Vec2,
r: f64,
cls: String,
weight_mm: f64,
#[serde(skip_serializing_if = "Option::is_none")]
dash: Dash,
#[serde(skip_serializing_if = "Option::is_none")]
opening_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
stair_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
greyed: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
opacity: Option<f64>,
},
#[serde(rename = "text", rename_all = "camelCase")]
Text {
at: Vec2,
doc: RichTextDoc,
extra_lines: Vec<StampLine>,
base_pt: f64,
color: String,
#[serde(skip_serializing_if = "Option::is_none")]
room_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
greyed: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
opacity: Option<f64>,
},
#[serde(rename = "drawingText", rename_all = "camelCase")]
DrawingText {
at: Vec2,
text: String,
height_m: f64,
angle: f64,
color: String,
#[serde(skip_serializing_if = "Option::is_none")]
marks: Option<Marks>,
#[serde(skip_serializing_if = "Option::is_none")]
wrap_width: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
doc: Option<RichTextDoc>,
#[serde(skip_serializing_if = "Option::is_none")]
drawing_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
greyed: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
opacity: Option<f64>,
},
}
impl Primitive {
/// Setzt `greyed` (für Gruppen, deren Darstellungsmodus gedimmt ist).
pub fn with_greyed(mut self, g: bool) -> Self {
match &mut self {
Primitive::Polygon { greyed, .. } | Primitive::Line { greyed, .. } | Primitive::Arc { greyed, .. } | Primitive::Text { greyed, .. } | Primitive::DrawingText { greyed, .. } => *greyed = Some(g),
}
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::plan::style::no_hatch;
#[test]
fn line_dash_null_und_fehlend_unterscheiden_sich() {
let mk = |dash: Dash| Primitive::Line { a: Vec2 { x: 0.0, y: 0.0 }, b: Vec2 { x: 1.0, y: 0.0 }, cls: "c".into(), weight_mm: 0.1, dash, zigzag: None, motif: None, color: None, drawing_id: None, opening_id: None, stair_id: None, greyed: None, opacity: None };
assert!(!serde_json::to_string(&mk(None)).unwrap().contains("dash"));
assert!(serde_json::to_string(&mk(Some(None))).unwrap().contains("\"dash\":null"));
assert!(serde_json::to_string(&mk(Some(Some(vec![0.4])))).unwrap().contains("\"dash\":[0.4]"));
}
#[test]
fn polygon_tragt_kind_und_camel_case() {
let p = Primitive::Polygon { pts: vec![], fill: "#fff".into(), stroke: "#000".into(), stroke_width_mm: 0.2, hatch: no_hatch(), greyed: None, opacity: None, no_stroke_edges: None, wall_id: Some("w".into()), drawing_id: None, ceiling_id: None, opening_id: None, stair_id: None, room_id: None, extruded_solid_id: None, context_object_id: None, column_id: None, roof_id: None, section_line_id: None };
let s = serde_json::to_string(&p).unwrap();
assert!(s.contains("\"kind\":\"polygon\"") && s.contains("strokeWidthMm") && s.contains("\"wallId\":\"w\""));
}
}
+162
View File
@@ -0,0 +1,162 @@
//! Attribut-Auflösung (By-Layer / By-Object) und Schraffur-Auflösung des Grundriss-Generators —
//! Gegenstück zu `src/plan/generatePlan/shared.ts`.
use super::prim::HatchRender;
use crate::model::*;
pub const POCHE_STROKE: &str = "#1a1a1a";
pub const MONO_INK: &str = "#111111";
pub const LAYER_LINE_MM: f64 = 0.02;
pub const SYMBOL_HAIRLINE_MM: f64 = 0.02;
pub const OVERHEAD_DASH: [f64; 2] = [0.4, 0.25];
pub const ROOF_EAVES_MM: f64 = 0.13;
pub const ROOF_RIDGE_MM: f64 = 0.25;
pub const ROOF_HIP_MM: f64 = 0.18;
pub const ROOF_BREAK_MM: f64 = 0.09;
pub const ROOF_BREAK_DASH: [f64; 2] = [0.12, 0.08];
pub const WALL_FALLBACK_MM: f64 = 0.18;
pub const HATCH_PAPER: &str = "#f0f0f0";
pub const HATCH_INK: &str = "#0f0f0f";
/// Detailgrad der Plan-Darstellung.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Detail {
Grob,
Mittel,
Fein,
}
impl Detail {
pub fn parse(s: &str) -> Detail {
match s {
"grob" => Detail::Grob,
"fein" => Detail::Fein,
_ => Detail::Mittel,
}
}
/// Stärke der Wand-Umrisslinie als Faktor auf die Ebenen-lw.
pub fn outline_factor(self) -> f64 {
match self {
Detail::Grob => 1.6,
Detail::Mittel => 1.0,
Detail::Fein => 0.6,
}
}
/// Stärke der Schichtfugen.
pub fn layer_factor(self) -> f64 {
match self {
Detail::Fein => 0.7,
_ => 1.0,
}
}
}
/// Schraffur-Platzhalter „ohne" (für reine Umriss-/Sammelflächen).
pub fn no_hatch() -> HatchRender {
HatchRender {
pattern: HatchPattern::None,
scale: 1.0,
angle: 0.0,
origin: None,
color: POCHE_STROKE.into(),
line_weight: LAYER_LINE_MM,
dash: None,
kind: None,
lines: None,
image: None,
gradient: None,
seed: None,
density: None,
length_min: None,
length_max: None,
motif: None,
}
}
/// Neutrale Poché-Füllfarbe für ein Muster: „solid" → Tinte, sonst Papier.
pub fn poche_fill(p: HatchPattern) -> &'static str {
if p == HatchPattern::Solid {
HATCH_INK
} else {
HATCH_PAPER
}
}
/// `resolveHatch`: `None`, wenn Schraffur oder ihr Linienstil fehlen (TS wirft dort — Aufrufer fängt).
pub fn resolve_hatch(p: &Project, hatch_id: &str, wall_angle_deg: Option<f64>, foreground: Option<&str>, placement: Option<(Option<f64>, Option<f64>, Option<Vec2>)>) -> Option<HatchRender> {
let h = p.hatch(hatch_id)?;
let ls = match h.line_style_id.as_deref().filter(|s| !s.is_empty()) {
Some(id) => Some(p.line_style(id)?),
None => None,
};
let (p_angle, p_scale, p_origin) = placement.unwrap_or((None, None, None));
let base_angle = p_angle.unwrap_or(h.angle);
let angle = match wall_angle_deg {
Some(w) if h.relative_to_wall == Some(true) => base_angle - w,
_ => base_angle,
};
Some(HatchRender {
pattern: h.pattern,
scale: h.scale * p_scale.unwrap_or(1.0),
angle,
origin: p_origin,
color: foreground.unwrap_or(HATCH_INK).to_string(),
line_weight: ls.map_or(0.13, |l| l.weight),
dash: ls.and_then(|l| l.dash.clone()),
kind: h.kind,
lines: h.lines,
image: h.image.clone(),
gradient: h.gradient.clone(),
seed: h.seed,
density: h.density,
length_min: h.length_min,
length_max: h.length_max,
motif: h.motif.clone(),
})
}
/// Vordergrundfarbe der Schraffur: Override ?? (Quelle „layer" ⇒ Kategorie-Farbe) ?? Bauteil.
pub fn resolve_foreground<'a>(comp: &'a Component, override_: Option<&'a str>, category: Option<&'a LayerCategory>, source: Option<AttributeSource>) -> Option<&'a str> {
if let Some(o) = override_ {
return Some(o);
}
if source == Some(AttributeSource::Layer) {
return category.map(|c| c.color.as_str()).or(comp.foreground.as_deref());
}
comp.foreground.as_deref()
}
/// Hintergrund-/Füllfarbe (Poché-Override), analog [`resolve_foreground`].
pub fn resolve_background<'a>(comp: &'a Component, override_: Option<&'a str>, category: Option<&'a LayerCategory>, source: Option<AttributeSource>) -> Option<&'a str> {
if let Some(o) = override_ {
return Some(o);
}
if source == Some(AttributeSource::Layer) {
return category.map(|c| c.color.as_str()).or(comp.background.as_deref());
}
comp.background.as_deref()
}
/// Schraffur-Referenz: Override ?? (Quelle „layer" ⇒ Kategorie-Schraffur) ?? Rückfall.
pub fn resolve_hatch_id<'a>(override_: Option<&'a str>, source: Option<AttributeSource>, category: Option<&'a LayerCategory>, fallback: Option<&'a str>) -> Option<&'a str> {
if let Some(o) = override_ {
return Some(o);
}
if source == Some(AttributeSource::Layer) {
return category.and_then(|c| c.hatch.as_deref()).or(fallback);
}
fallback
}
/// Strichstärke: Override ?? (Quelle „layer" ⇒ Kategorie-lw) ?? Rückfall.
pub fn resolve_stroke_weight(override_: Option<f64>, source: Option<AttributeSource>, category: Option<&LayerCategory>, fallback: f64) -> f64 {
if let Some(o) = override_ {
return o;
}
if source == Some(AttributeSource::Layer) {
return category.map_or(fallback, |c| c.lw);
}
fallback
}