[K4] Merge: Griffe und Objekt-Info im Kern (grips.rs, selection_info.rs), 3D-Griffhöhen
Claude-Session: https://claude.ai/code/session_01NTYBZBvLYCtugMJoKXaB67
This commit is contained in:
46 files changed
+3352
-2427
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -15,6 +15,30 @@ pub fn to_fixed(x: f64, d: usize) -> String {
|
||||
if ax >= 1e21 {
|
||||
return format!("{}{}", if neg { "-" } else { "" }, ax);
|
||||
}
|
||||
// Schnellweg: Rust rundet `{:.N}` korrekt (zum geraden Nachbarn nur bei exakten Halbwerten); ohne
|
||||
// exakten Halbwert ist das Ergebnis dasselbe wie in JS, nur ohne die teure lange Formatierung.
|
||||
if !exact_half(ax, d) {
|
||||
let s = format!("{:.*}", d, ax);
|
||||
return if neg { format!("-{s}") } else { s };
|
||||
}
|
||||
to_fixed_wide(ax, d, neg)
|
||||
}
|
||||
|
||||
/// Ob `ax` (endlich, ≥ 0) bei `d` Nachkommastellen genau auf der Mitte zwischen zwei Ergebnissen liegt:
|
||||
/// `ax · 10^d · 2` ist eine ungerade ganze Zahl, d. h. das niedrigste gesetzte Bit von `ax` ist 2^-(d+1).
|
||||
fn exact_half(ax: f64, d: usize) -> bool {
|
||||
if ax == 0.0 {
|
||||
return false;
|
||||
}
|
||||
let bits = ax.to_bits();
|
||||
let exp = ((bits >> 52) & 0x7ff) as i64;
|
||||
let frac = bits & ((1u64 << 52) - 1);
|
||||
let (m, e) = if exp == 0 { (frac, -1074) } else { (frac | (1u64 << 52), exp - 1075) };
|
||||
e + m.trailing_zeros() as i64 + d as i64 + 1 == 0
|
||||
}
|
||||
|
||||
/// Langsamer, exakter Weg (für exakte Halbwerte): Ziffern mit 25 Stellen Reserve, dann aufrunden.
|
||||
fn to_fixed_wide(ax: f64, d: usize, neg: bool) -> String {
|
||||
// 25 Stellen mehr als nötig: ein exakter Halbwert zeigt sich als '5' gefolgt von Nullen.
|
||||
let wide = format!("{:.*}", d + 25, ax);
|
||||
let (int_part, frac) = wide.split_once('.').expect("Nachkommastellen");
|
||||
@@ -119,5 +143,30 @@ mod tests {
|
||||
assert_eq!(to_fixed(123.456, 2), "123.46");
|
||||
assert_eq!(to_fixed(2.675, 2), "2.67"); // 2.675 ist binär 2.67499999… — JS: "2.67"
|
||||
assert_eq!(to_fixed(1e-9, 6), "0.000000");
|
||||
assert_eq!(to_fixed(0.25, 1), "0.3");
|
||||
assert_eq!(to_fixed(-0.25, 1), "-0.3");
|
||||
assert_eq!(to_fixed(-0.0, 2), "0.00");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn schnellweg_gleich_dem_genauen_weg() {
|
||||
let mut x: u64 = 0x2545_f491_4f6c_dd1d;
|
||||
for i in 0..200_000 {
|
||||
x ^= x << 13;
|
||||
x ^= x >> 7;
|
||||
x ^= x << 17;
|
||||
let v = match i % 4 {
|
||||
0 => (x % 1_000_000) as f64 / 1000.0,
|
||||
1 => f64::from_bits(x) % 1e6,
|
||||
2 => (x % 4096) as f64 / 64.0,
|
||||
_ => (x as f64 / u64::MAX as f64 - 0.5) * 100.0,
|
||||
};
|
||||
if !v.is_finite() {
|
||||
continue;
|
||||
}
|
||||
for d in 0..8 {
|
||||
assert_eq!(to_fixed(v, d), to_fixed_wide(v.abs(), d, v < 0.0), "{v} {d}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -85,6 +85,8 @@ pub mod ugrid;
|
||||
pub mod wasm_alloc;
|
||||
pub mod text_layout;
|
||||
pub mod plan_text;
|
||||
pub mod grips;
|
||||
pub mod selection_info;
|
||||
|
||||
#[cfg(feature = "web")]
|
||||
mod web;
|
||||
@@ -98,6 +98,27 @@ pub fn run_root(root: &serde_json::Map<String, serde_json::Value>, name: &str, t
|
||||
let m: crate::groups::Membership = serde_json::from_str(text).map_err(|e| format!("selectionFilterPatch: {e}"))?;
|
||||
json(&crate::selection_filter::filter_patch(root, &m))
|
||||
}
|
||||
// Griff-Änderungen ([K4] S3, `grips.rs`): `text` = JSON der Anfrage; Antwort `{ops, notice}` (ohne Commit).
|
||||
"gripOps" => {
|
||||
let req: crate::grips::Request = serde_json::from_str(text).map_err(|e| format!("gripOps: {e}"))?;
|
||||
Ok(crate::grips::result_json(&crate::grips::run(root, &req)?))
|
||||
}
|
||||
// Objekt-Info der Auswahl ([K4] S3, `selection_info.rs`): `text` = JSON der gewählten ids je Art; Antwort
|
||||
// `Selection` (TS) oder null, Zahlen mit Nicht-endlich-Marken.
|
||||
"selectionInfo" => {
|
||||
let a: crate::selection_info::SelArgs = serde_json::from_str(text).map_err(|e| format!("selectionInfo: {e}"))?;
|
||||
crate::selection_info::derive(root, &a).map(crate::selection_info::J::into_string)
|
||||
}
|
||||
// Bounding-Boxen von Zeichnungen (Ausrichten/Verteilen): `text` = JSON der ids.
|
||||
"drawingBBoxes" => {
|
||||
let ids: Vec<String> = serde_json::from_str(text).map_err(|e| format!("drawingBBoxes: {e}"))?;
|
||||
Ok(crate::selection_info::drawing_bboxes(root, &ids).into_string())
|
||||
}
|
||||
// UK/OK von Wänden und Decken (3D-Griffe): `text` = `{walls: […], ceilings: […]}` (ganze Elemente).
|
||||
"verticalExtents" => {
|
||||
let a: crate::selection_info::ExtentArgs = serde_json::from_str(text).map_err(|e| format!("verticalExtents: {e}"))?;
|
||||
Ok(crate::selection_info::vertical_extents(root, &a).into_string())
|
||||
}
|
||||
_ => Err(format!("Unbekannte Wurzel-Abfrage: {name}")),
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,16 +0,0 @@
|
||||
//! Bindungen der 2D-Zeichnungen im Grundriss (`crate::plan::drawings`). Die Plan-Primitive selbst
|
||||
//! kommen über die Dokument-Abfragen (`planDomains`, `drawingPrims`, `drawingLines`).
|
||||
|
||||
use wasm_bindgen::prelude::wasm_bindgen;
|
||||
|
||||
/// Lage der Masszahl eines Mass-Segments `[baseX, baseY, readX, readY, upX, upY, angle]`
|
||||
/// (`dimTextFrame`, geteilt von Plan und Masszahl-Griff); leer bei NaN/∞.
|
||||
#[wasm_bindgen]
|
||||
pub fn dim_text_frame(ax: f64, ay: f64, bx: f64, by: f64, offset: f64, text_height: f64) -> Vec<f64> {
|
||||
if ![ax, ay, bx, by, offset, text_height].iter().all(|v| v.is_finite()) {
|
||||
return Vec::new();
|
||||
}
|
||||
let v = |x, y| crate::model::Vec2 { x, y };
|
||||
let (base, read, up, angle) = crate::plan::drawings::dim_text_frame(v(ax, ay), v(bx, by), offset, text_height);
|
||||
vec![base.x, base.y, read.x, read.y, up.x, up.y, angle]
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
//! Bindungen der Griff-Geometrie (`crate::grips`): Griff-Änderung im Dokument rechnen und gleich als
|
||||
//! Benutzer-Änderung anwenden (ein Aufruf je Mausschritt, keine Op-Übergabe hin und zurück), Griff-
|
||||
//! und Kanten-Listen für die Anzeige als flache Puffer.
|
||||
|
||||
use super::document::with_doc;
|
||||
use serde_json::{json, Value};
|
||||
use wasm_bindgen::prelude::wasm_bindgen;
|
||||
|
||||
/// Griff-Änderung `req_json` (s. `crate::grips::run`) auf dem Dokument `h` rechnen und mit Verlauf
|
||||
/// anwenden (`key` = coalesceKey, `now_ms` = Zeit für das Koaleszenz-Fenster, wie `doc_commit`).
|
||||
/// Antwort: `{"ops":[…],"notice":…}` (angewendet; leer = nichts zu tun) oder `{"problems":[…]}`
|
||||
/// (zurückgewiesen, Dokument unverändert); Fehler als Text mit vorangestelltem `\u{1}`.
|
||||
#[wasm_bindgen]
|
||||
pub fn doc_grip_commit(h: u32, req_json: &str, key: &str, now_ms: f64) -> String {
|
||||
let req: crate::grips::Request = match serde_json::from_str(req_json) {
|
||||
Ok(v) => v,
|
||||
Err(e) => return format!("\u{1}Griff-Anfrage: {e}"),
|
||||
};
|
||||
let key = (!key.is_empty()).then_some(key);
|
||||
let now = if now_ms.is_finite() { now_ms } else { 0.0 };
|
||||
let out = with_doc(h, |d| -> Result<String, String> {
|
||||
let res = crate::grips::run(d.root(), &req)?;
|
||||
let text = crate::grips::result_json(&res);
|
||||
if res.ops.is_empty() {
|
||||
return Ok(text);
|
||||
}
|
||||
// Nicht endliche Ergebnisse stehen als Marken in den Ops (die Prüfung weist sie zurück).
|
||||
let marks = text.contains("\\u0001");
|
||||
match d.commit(res.ops, key, now, marks) {
|
||||
Ok(()) => Ok(text),
|
||||
Err(problems) => Ok(json!({ "problems": problems }).to_string()),
|
||||
}
|
||||
});
|
||||
match out {
|
||||
Some(Ok(t)) => t,
|
||||
Some(Err(e)) => format!("\u{1}{e}"),
|
||||
None => "\u{1}Dokument nicht offen".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Editier-Griffe einer 2D-Zeichnung (JSON) beim Massstab 1:`scale`: `[n, x0, y0, …]`; `[-1]` = Textspalte
|
||||
/// (Rahmen-Ecken rechnet die Hülle mit der Schrift-Metrik), `[]` bei kaputtem JSON.
|
||||
#[wasm_bindgen]
|
||||
pub fn grip_vertices(drawing_json: &str, scale: f64) -> Vec<f64> {
|
||||
let Ok(d) = serde_json::from_str::<Value>(drawing_json) else { return vec![] };
|
||||
let g = d.get("geom").unwrap_or(&d);
|
||||
match crate::grips::drawing_vertices(g, scale) {
|
||||
None => vec![-1.0],
|
||||
Some(v) => {
|
||||
let mut out = Vec::with_capacity(1 + 2 * v.len());
|
||||
out.push(v.len() as f64);
|
||||
for p in v {
|
||||
out.push(p.0);
|
||||
out.push(p.1);
|
||||
}
|
||||
out
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Editier-Griffe einer Polylinie aus flachen Puffern (grosse Kataster-Umrisse ohne JSON): `pts = [x0, y0, …]`,
|
||||
/// `bulge` je Segment (fehlend/NaN = gerade) → `[x0, y0, …]` (Eckpunkte, dann je Segment die Bogen-Mitte).
|
||||
#[wasm_bindgen]
|
||||
pub fn grip_vertices_polyline(pts: &[f64], bulge: &[f64], closed: bool) -> Vec<f64> {
|
||||
let verts: Vec<(f64, f64)> = pts.chunks_exact(2).map(|c| (c[0], c[1])).collect();
|
||||
crate::grips::polyline_vertices(verts, closed, |i| bulge.get(i).copied().unwrap_or(0.0)).into_iter().flat_map(|p| [p.0, p.1]).collect()
|
||||
}
|
||||
|
||||
/// Kanten-Griffe eines Rings (`closed`) bzw. einer Kette aus `pts = [x0, y0, …]`; `bulge` je Segment
|
||||
/// nur bei `use_bulge` (Polylinie: Bogen-Segmente ohne Kanten-Griff). Antwort je Kante
|
||||
/// `[a, b, mitteX, mitteY, normaleX, normaleY]`.
|
||||
#[wasm_bindgen]
|
||||
pub fn edge_grips(pts: &[f64], closed: bool, bulge: &[f64], use_bulge: bool) -> Vec<f64> {
|
||||
let verts: Vec<(f64, f64)> = pts.chunks_exact(2).map(|c| (c[0], c[1])).collect();
|
||||
let edges = crate::grips::edge_grips(&verts, closed, use_bulge.then_some(bulge));
|
||||
let mut out = Vec::with_capacity(6 * edges.len());
|
||||
for (a, b, m, n) in edges {
|
||||
out.extend([a as f64, b as f64, m.0, m.1, n.0, n.1]);
|
||||
}
|
||||
out
|
||||
}
|
||||
@@ -32,13 +32,13 @@ mod snap;
|
||||
mod plan_svg;
|
||||
mod plan_dxf;
|
||||
mod plan_print_svg;
|
||||
mod drawings;
|
||||
mod layers;
|
||||
mod pens;
|
||||
mod layout_model;
|
||||
mod parametric_walls;
|
||||
mod reorder;
|
||||
mod text_layout;
|
||||
mod grips;
|
||||
|
||||
use wasm_bindgen::prelude::wasm_bindgen;
|
||||
|
||||
|
||||
@@ -21,12 +21,6 @@ pub fn stair_cut_flat(geometry: &[f64], cut_rise: f64) -> Vec<f64> {
|
||||
s::cut(geometry, cut_rise)
|
||||
}
|
||||
|
||||
/// `[minX, minY, maxX, maxY]` aller Tritte + Podest.
|
||||
#[wasm_bindgen]
|
||||
pub fn stair_bbox_flat(geometry: &[f64]) -> Vec<f64> {
|
||||
s::bbox(geometry)
|
||||
}
|
||||
|
||||
/// Vorgabe-Stufenzahl aus Steighöhe und Lauflänge.
|
||||
#[wasm_bindgen]
|
||||
pub fn stair_default_step_count(total_rise: f64, run_length: f64) -> u32 {
|
||||
|
||||
Reference in new issue
Block a user