[193] Gelände & Co.: Höhen-Befehle im Kern, Maus-Ziehen für Kontext-Elemente, Höhenlinien-Punkte als Griffe, Entf auf Ansichts-Schlagschatten
This commit is contained in:
24 files changed
+530
-71
No files matched your search
@@ -72,6 +72,7 @@ Neues Modul: `<modul>.rs` und `mod` in `lib.rs`, Bindungen in `web/<modul>.rs` u
|
||||
| `cmd_create.rs` | Zeichenbefehle für Bauteile: neue Elemente, Vorschau, Treffer |
|
||||
| `cmd_draw.rs`, `cmd_edit.rs`, `cmd_split.rs`, `cmd_paste.rs` | 2D zeichnen und bemassen, bearbeiten, teilen, einfügen |
|
||||
| `cmd_transform.rs`, `cmd_wall_edit.rs`, `cmd_solid.rs` | Transformieren; Wände bearbeiten; Körper, Gelände, Sonderbefehle |
|
||||
| `cmd_context.rs` | [193] Höhenversatz und Höhe je Höhenlinie von Gelände-/Umgebungs-Elementen (`contextOffset`, `contourZ`) |
|
||||
| `grips.rs`, `grip_drag.rs` | Griffe: Geometrie und Rechnung je Mausschritt |
|
||||
| `snap.rs`, `snap_model.rs` | Objektfang |
|
||||
| `selection_info.rs`, `selection_filter.rs` | Objekt-Info der Auswahl; Export „nur Auswahl" |
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
//! [193] Höhe von Gelände-/Umgebungs-Elementen als Befehle (vorher `src/state/contextHeight.ts`):
|
||||
//! `contextOffset` versetzt alle Höhen eines Elements um `dz` (Netz-z, Höhenlinien-z, Baugruben-Sohle),
|
||||
//! `contourZ` setzt die Höhe einer einzelnen Höhenlinie. Ergebnis: ein `Replace` des Elements in `context`
|
||||
//! (leer = Art ohne Höhe, keine Änderung oder ungültige Eingabe).
|
||||
|
||||
use crate::json_util::{arr, field, js_num, num_of, with};
|
||||
use crate::ops::{id_of, Op};
|
||||
use serde_json::{Map, Value};
|
||||
|
||||
fn element<'a>(root: &'a Map<String, Value>, id: &str) -> Option<&'a Value> {
|
||||
arr(root.get("context")).iter().find(|e| id_of(e) == Some(id))
|
||||
}
|
||||
|
||||
fn kind(e: &Value) -> &str {
|
||||
field(e, "type").and_then(Value::as_str).unwrap_or("")
|
||||
}
|
||||
|
||||
/// Alle Höhen von `e` um `dz` Meter versetzt; `None` bei Art ohne Höhe oder `dz` = 0/ungültig.
|
||||
pub fn height_offset(e: &Value, dz: f64) -> Option<Value> {
|
||||
if !dz.is_finite() || dz == 0.0 {
|
||||
return None;
|
||||
}
|
||||
match kind(e) {
|
||||
"terrainMesh" | "importedMesh" => {
|
||||
let pos: Vec<Value> = arr(field(e, "positions"))
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, v)| if i % 3 == 2 { js_num(num_of(Some(v)) + dz) } else { v.clone() })
|
||||
.collect();
|
||||
Some(with(e, "positions", Value::Array(pos)))
|
||||
}
|
||||
"contourSet" => {
|
||||
let cs: Vec<Value> = arr(field(e, "contours")).iter().map(|c| with(c, "z", js_num(num_of(field(c, "z")) + dz))).collect();
|
||||
Some(with(e, "contours", Value::Array(cs)))
|
||||
}
|
||||
"excavation" => Some(with(e, "bottom", js_num(num_of(field(e, "bottom")) + dz))),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Höhe der Höhenlinie `index` eines Höhenlinien-Satzes auf `z` setzen.
|
||||
pub fn contour_height(e: &Value, index: usize, z: f64) -> Option<Value> {
|
||||
if kind(e) != "contourSet" || !z.is_finite() {
|
||||
return None;
|
||||
}
|
||||
let cs = arr(field(e, "contours"));
|
||||
let c = cs.get(index)?;
|
||||
if num_of(field(c, "z")) == z {
|
||||
return None;
|
||||
}
|
||||
let mut out = cs.to_vec();
|
||||
out[index] = with(c, "z", js_num(z));
|
||||
Some(with(e, "contours", Value::Array(out)))
|
||||
}
|
||||
|
||||
/// Ops der Höhen-Befehle (`None` = fremder Befehl).
|
||||
pub fn ops(root: &Map<String, Value>, v: &Value) -> Option<Vec<Op>> {
|
||||
let cmd = v.get("cmd").and_then(Value::as_str)?;
|
||||
if cmd != "contextOffset" && cmd != "contourZ" {
|
||||
return None;
|
||||
}
|
||||
let num = |k: &str| v.get(k).and_then(Value::as_f64).unwrap_or(f64::NAN);
|
||||
let patched = v.get("id").and_then(Value::as_str).and_then(|id| element(root, id)).and_then(|e| {
|
||||
if cmd == "contextOffset" {
|
||||
height_offset(e, num("dz"))
|
||||
} else {
|
||||
let i = num("index");
|
||||
if i.is_finite() && i >= 0.0 { contour_height(e, i as usize, num("z")) } else { None }
|
||||
}
|
||||
});
|
||||
Some(patched.map(|entity| vec![Op::Replace { collection: "context".into(), entity }]).unwrap_or_default())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
fn root() -> Map<String, Value> {
|
||||
json!({"context":[
|
||||
{"id":"tm","type":"terrainMesh","positions":[0,0,5, 1,0,6, 0,1,7],"indices":[0,1,2]},
|
||||
{"id":"hl","type":"contourSet","contours":[{"z":500,"pts":[],"closed":false},{"z":501,"pts":[],"closed":false}]},
|
||||
{"id":"bg","type":"excavation","bottom":-3,"outline":[]},
|
||||
{"id":"im","type":"image"}]})
|
||||
.as_object()
|
||||
.unwrap()
|
||||
.clone()
|
||||
}
|
||||
|
||||
fn run(v: Value) -> Vec<Value> {
|
||||
ops(&root(), &v).unwrap().iter().map(|o| o.to_json()).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn versatz_je_art() {
|
||||
let o = run(json!({"cmd":"contextOffset","id":"tm","dz":-1.5}));
|
||||
assert_eq!(o[0]["entity"]["positions"], json!([0, 0, 3.5, 1, 0, 4.5, 0, 1, 5.5]));
|
||||
assert_eq!(o[0]["entity"]["indices"], json!([0, 1, 2]));
|
||||
let o = run(json!({"cmd":"contextOffset","id":"hl","dz":2}));
|
||||
assert_eq!(o[0]["entity"]["contours"][0]["z"], json!(502));
|
||||
assert_eq!(o[0]["entity"]["contours"][1]["z"], json!(503));
|
||||
let o = run(json!({"cmd":"contextOffset","id":"bg","dz":0.5}));
|
||||
assert_eq!(o[0]["entity"]["bottom"], json!(-2.5));
|
||||
assert_eq!(o[0]["collection"], "context");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keine_aenderung() {
|
||||
assert!(run(json!({"cmd":"contextOffset","id":"hl","dz":0})).is_empty());
|
||||
assert!(run(json!({"cmd":"contextOffset","id":"im","dz":1})).is_empty());
|
||||
assert!(run(json!({"cmd":"contextOffset","id":"nix","dz":1})).is_empty());
|
||||
assert!(run(json!({"cmd":"contextOffset","id":"hl"})).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hoehe_einer_linie() {
|
||||
let o = run(json!({"cmd":"contourZ","id":"hl","index":1,"z":510}));
|
||||
assert_eq!(o[0]["entity"]["contours"][0]["z"], json!(500));
|
||||
assert_eq!(o[0]["entity"]["contours"][1]["z"], json!(510));
|
||||
assert!(run(json!({"cmd":"contourZ","id":"hl","index":1,"z":501})).is_empty());
|
||||
assert!(run(json!({"cmd":"contourZ","id":"hl","index":7,"z":1})).is_empty());
|
||||
assert!(run(json!({"cmd":"contourZ","id":"tm","index":0,"z":1})).is_empty());
|
||||
}
|
||||
}
|
||||
@@ -35,6 +35,9 @@ pub fn ops(root: &Map<String, Value>, p: &Project, text: &str) -> Result<Vec<Op>
|
||||
if let Some(ops) = crate::cmd_create::ops(root, p, &v) {
|
||||
return Ok(ops);
|
||||
}
|
||||
if let Some(ops) = crate::cmd_context::ops(root, &v) {
|
||||
return Ok(ops);
|
||||
}
|
||||
Ok(match cmd {
|
||||
"transform" => crate::cmd_transform::transform_ops(root, p, &parse(&v, cmd)?),
|
||||
"align" => crate::cmd_transform::align_ops(root, &ids("ids"), &s("mode")),
|
||||
|
||||
@@ -306,6 +306,15 @@ pub fn run(lk: &Lookup, req: &Request) -> Result<GripResult, String> {
|
||||
})
|
||||
.into()
|
||||
}
|
||||
// [193] Höhenlinien-Punkt ziehen bzw. einfügen: flacher Index über alle Linien des Satzes (Punkt-Index bzw. Segment-Index).
|
||||
"contourGrip" => {
|
||||
let (index, (x, y)) = (r.n("index"), r.p("pt"));
|
||||
patch("context", r.s("id"), &|e| contour_edit(e, index, Some((x, y)), false)).into()
|
||||
}
|
||||
"contourInsert" => {
|
||||
let (seg, (x, y)) = (r.n("seg"), r.p("pt"));
|
||||
patch("context", r.s("id"), &|e| contour_edit(e, seg, Some((x, y)), true)).into()
|
||||
}
|
||||
"excavationEdge" => {
|
||||
let (a, b, (dx, dy)) = (r.n("a"), r.n("b"), r.p("delta"));
|
||||
patch("context", r.s("id"), &|e| Some(with(e, "outline", move_polygon_edge(field(e, "outline")?.as_array()?, a, b, dx, dy, true)?))).into()
|
||||
@@ -1347,6 +1356,39 @@ fn shift_edge_list(e: Value, field_name: &str, off: usize, shift: &EdgeShift) ->
|
||||
with(&e, field_name, Value::Array(v))
|
||||
}
|
||||
|
||||
/// [193] Punkt `flat` (flach über alle Linien eines Höhenlinien-Satzes) auf `at` setzen bzw. (`insert`) hinter dem
|
||||
/// Punkt `flat` einen Punkt einfügen (Segment von `flat` zum Nachfolger; geschlossen: der letzte schliesst zum Anfang,
|
||||
/// offen: der letzte hat kein Segment). Eine wahre Kurve (`curve`) fällt weg.
|
||||
fn contour_edit(e: &Value, flat: f64, at: Option<(f64, f64)>, insert: bool) -> Option<Value> {
|
||||
let (x, y) = at.filter(|(x, y)| x.is_finite() && y.is_finite())?;
|
||||
if field(e, "type").and_then(Value::as_str) != Some("contourSet") {
|
||||
return None;
|
||||
}
|
||||
let cs = arr(field(e, "contours"));
|
||||
let mut off = 0usize;
|
||||
for (ci, c) in cs.iter().enumerate() {
|
||||
let pts = arr(field(c, "pts"));
|
||||
let closed = truthy(field(c, "closed"));
|
||||
let count = pts.len();
|
||||
let has_seg = |i: usize| count >= 2 && (closed || i + 1 < count);
|
||||
if let Some(i) = index_in(flat - off as f64, count).filter(|i| !insert || has_seg(*i)) {
|
||||
let new_pts = if insert {
|
||||
let mut v = pts.to_vec();
|
||||
v.insert(i + 1, pt(x, y));
|
||||
Value::Array(v)
|
||||
} else {
|
||||
replace_at(pts, i as f64, &pt(x, y))
|
||||
};
|
||||
let nc = with_opt(&with(c, "pts", new_pts), vec![("curve", None)]);
|
||||
let mut out = cs.to_vec();
|
||||
out[ci] = nc;
|
||||
return Some(with(e, "contours", Value::Array(out)));
|
||||
}
|
||||
off += count;
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// [26] Ecke einfügen/löschen/aufräumen an einem Umriss-Element (s. `ringInsert`).
|
||||
fn ring_edit(e: &Value, kind: &str, flat: f64, edit: &RingEdit) -> Option<Value> {
|
||||
if truthy(field(e, "locked")) {
|
||||
@@ -1932,6 +1974,28 @@ mod tests {
|
||||
assert_eq!(e["outline"][2], json!({"x": 4, "y": 4}));
|
||||
}
|
||||
|
||||
/// [193] Höhenlinien-Satz: Punkt ziehen (flacher Index über alle Linien) und Punkt hinter einem Segment einfügen.
|
||||
#[test]
|
||||
fn hoehenlinien_griffe() {
|
||||
let r = root(json!({"context": [{"id": "hl", "type": "contourSet", "contours": [
|
||||
{"z": 1, "closed": false, "pts": [{"x": 0, "y": 0}, {"x": 2, "y": 0}, {"x": 4, "y": 0}]},
|
||||
{"z": 2, "closed": true, "curve": {"cx": 0, "cy": 0}, "pts": [{"x": 0, "y": 1}, {"x": 2, "y": 1}, {"x": 2, "y": 3}]}]}]}));
|
||||
let ent = |v: Value| run(&Lookup::plain(&r), &rq(v)).unwrap().ops.first().map(|o| o.to_json()["entity"].clone());
|
||||
let e = ent(json!({"op": "contourGrip", "id": "hl", "index": 4, "pt": [9, 9]})).unwrap();
|
||||
assert_eq!(e["contours"][1]["pts"][1], json!({"x": 9, "y": 9}));
|
||||
assert!(e["contours"][1].get("curve").is_none());
|
||||
assert_eq!(e["contours"][0]["pts"][1], json!({"x": 2, "y": 0}));
|
||||
assert!(ent(json!({"op": "contourGrip", "id": "hl", "index": 6, "pt": [9, 9]})).is_none());
|
||||
// Offene Linie: Segmente ab Punkt 0 und 1 (Punkt 2 hat keins); geschlossene: ab 3, 4, 5 (5 schliesst zum Anfang).
|
||||
assert!(ent(json!({"op": "contourInsert", "id": "hl", "seg": 2, "pt": [3, 1]})).is_none());
|
||||
let e = ent(json!({"op": "contourInsert", "id": "hl", "seg": 1, "pt": [3, 1]})).unwrap();
|
||||
assert_eq!(e["contours"][0]["pts"].as_array().unwrap().len(), 4);
|
||||
assert_eq!(e["contours"][0]["pts"][2], json!({"x": 3, "y": 1}));
|
||||
let e = ent(json!({"op": "contourInsert", "id": "hl", "seg": 5, "pt": [1, 2]})).unwrap();
|
||||
assert_eq!(e["contours"][1]["pts"][3], json!({"x": 1, "y": 2}));
|
||||
assert!(ent(json!({"op": "contourInsert", "id": "hl", "seg": 6, "pt": [1, 2]})).is_none());
|
||||
}
|
||||
|
||||
/// [26] Dach: Ecken und Kanten von Umriss und Hof über den flachen Index; schiefer Umriss bleibt schief.
|
||||
#[test]
|
||||
fn dach_griffe_umriss_und_hof() {
|
||||
|
||||
@@ -122,6 +122,7 @@ pub mod import_context;
|
||||
pub mod import_ifc;
|
||||
pub mod cmd_paste;
|
||||
pub mod cmd_split;
|
||||
pub mod cmd_context;
|
||||
pub mod cmd_wall_edit;
|
||||
pub mod snap_model;
|
||||
pub mod solid_specs;
|
||||
|
||||
Reference in new issue
Block a user