[193] Gelände: Höhenlinien-Punkt löschen (Kontextmenü) und Aufräumen beim Zurückziehen; Netzpunkte als Griffe, Punkt-Höhe, Netz auf Parzelle zuschneiden
This commit is contained in:
14 files changed
+347
-17
No files matched your search
@@ -53,19 +53,82 @@ pub fn contour_height(e: &Value, index: usize, z: f64) -> Option<Value> {
|
||||
Some(with(e, "contours", Value::Array(out)))
|
||||
}
|
||||
|
||||
/// Ops der Höhen-Befehle (`None` = fremder Befehl).
|
||||
/// Höhe des Netzpunkts `index` (Vertex-Nummer) eines Geländes/Netzes auf `z` setzen.
|
||||
pub fn mesh_point_height(e: &Value, index: usize, z: f64) -> Option<Value> {
|
||||
if !matches!(kind(e), "terrainMesh" | "importedMesh") || !z.is_finite() {
|
||||
return None;
|
||||
}
|
||||
let pos = arr(field(e, "positions"));
|
||||
let k = index.checked_mul(3)?.checked_add(2)?;
|
||||
if k >= pos.len() || num_of(Some(&pos[k])) == z {
|
||||
return None;
|
||||
}
|
||||
let mut out = pos.to_vec();
|
||||
out[k] = js_num(z);
|
||||
Some(with(e, "positions", Value::Array(out)))
|
||||
}
|
||||
|
||||
/// Netz auf ein Polygon (`poly` = flach x,y,…) zuschneiden: Dreiecke bleiben, deren Schwerpunkt im Polygon liegt; nicht
|
||||
/// mehr genutzte Punkte (samt `vertexColors`) fallen weg. Nichts übrig oder nichts abgeschnitten = keine Änderung.
|
||||
pub fn mesh_crop(e: &Value, poly: &[f64]) -> Option<Value> {
|
||||
if !matches!(kind(e), "terrainMesh" | "importedMesh") || poly.len() < 6 || poly.len() % 2 != 0 || poly.iter().any(|v| !v.is_finite()) {
|
||||
return None;
|
||||
}
|
||||
let pos: Vec<f64> = arr(field(e, "positions")).iter().map(|v| num_of(Some(v))).collect();
|
||||
let idx: Vec<usize> = arr(field(e, "indices")).iter().map(|v| num_of(Some(v))).map(|f| if f >= 0.0 && f.fract() == 0.0 { f as usize } else { usize::MAX }).collect();
|
||||
let nv = pos.len() / 3;
|
||||
if idx.len() < 3 || idx.iter().any(|&i| i >= nv) {
|
||||
return None;
|
||||
}
|
||||
let keep: Vec<&[usize]> = idx
|
||||
.chunks_exact(3)
|
||||
.filter(|t| {
|
||||
let (cx, cy) = t.iter().fold((0.0, 0.0), |a, &i| (a.0 + pos[3 * i] / 3.0, a.1 + pos[3 * i + 1] / 3.0));
|
||||
crate::plan::pick::point_in_polygon(cx, cy, poly)
|
||||
})
|
||||
.collect();
|
||||
if keep.is_empty() || keep.len() == idx.len() / 3 {
|
||||
return None;
|
||||
}
|
||||
let mut map = vec![usize::MAX; nv];
|
||||
let mut order: Vec<usize> = Vec::new();
|
||||
let mut new_idx: Vec<Value> = Vec::with_capacity(keep.len() * 3);
|
||||
for t in keep {
|
||||
for &i in t {
|
||||
if map[i] == usize::MAX {
|
||||
map[i] = order.len();
|
||||
order.push(i);
|
||||
}
|
||||
new_idx.push(js_num(map[i] as f64));
|
||||
}
|
||||
}
|
||||
let new_pos: Vec<Value> = order.iter().flat_map(|&i| (0..3).map(move |c| (i, c))).map(|(i, c)| js_num(pos[3 * i + c])).collect();
|
||||
let mut out = with(&with(e, "positions", Value::Array(new_pos)), "indices", Value::Array(new_idx));
|
||||
if let Some(vc) = field(e, "vertexColors").and_then(Value::as_array).filter(|c| c.len() == nv * 3) {
|
||||
let nc: Vec<Value> = order.iter().flat_map(|&i| vc[3 * i..3 * i + 3].iter().cloned()).collect();
|
||||
out = with(&out, "vertexColors", Value::Array(nc));
|
||||
}
|
||||
Some(out)
|
||||
}
|
||||
|
||||
/// Ops der Höhen-/Netz-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" {
|
||||
if !matches!(cmd, "contextOffset" | "contourZ" | "meshPointZ" | "meshCrop") {
|
||||
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 }
|
||||
let index = || {
|
||||
let i = num("index");
|
||||
(i.is_finite() && i >= 0.0).then_some(i as usize)
|
||||
};
|
||||
let patched = v.get("id").and_then(Value::as_str).and_then(|id| element(root, id)).and_then(|e| match cmd {
|
||||
"contextOffset" => height_offset(e, num("dz")),
|
||||
"contourZ" => contour_height(e, index()?, num("z")),
|
||||
"meshPointZ" => mesh_point_height(e, index()?, num("z")),
|
||||
_ => {
|
||||
let poly: Vec<f64> = v.get("poly").and_then(Value::as_array)?.iter().map(|x| x.as_f64().unwrap_or(f64::NAN)).collect();
|
||||
mesh_crop(e, &poly)
|
||||
}
|
||||
});
|
||||
Some(patched.map(|entity| vec![Op::Replace { collection: "context".into(), entity }]).unwrap_or_default())
|
||||
@@ -112,6 +175,29 @@ mod tests {
|
||||
assert!(run(json!({"cmd":"contextOffset","id":"hl"})).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn netzpunkt_hoehe_und_zuschnitt() {
|
||||
let o = run(json!({"cmd":"meshPointZ","id":"tm","index":1,"z":9}));
|
||||
assert_eq!(o[0]["entity"]["positions"], json!([0, 0, 5, 1, 0, 9, 0, 1, 7]));
|
||||
assert!(run(json!({"cmd":"meshPointZ","id":"tm","index":1,"z":6})).is_empty());
|
||||
assert!(run(json!({"cmd":"meshPointZ","id":"tm","index":3,"z":6})).is_empty());
|
||||
assert!(run(json!({"cmd":"meshPointZ","id":"hl","index":0,"z":6})).is_empty());
|
||||
// Zwei Dreiecke (Quadrat 0..2); Zuschnitt auf die linke Hälfte behält eines, verwirft Punkte und Farben.
|
||||
let mut r = root();
|
||||
r.insert("context".into(), json!([{"id":"q","type":"terrainMesh","positions":[0,0,1, 2,0,2, 2,2,3, 0,2,4],"indices":[0,1,2, 0,2,3],"vertexColors":[0,0,0, 1,1,1, 2,2,2, 3,3,3]}]));
|
||||
let cut = |poly: Value| ops(&r, &json!({"cmd":"meshCrop","id":"q","poly":poly})).unwrap().iter().map(|o| o.to_json()).collect::<Vec<_>>();
|
||||
let o = cut(json!([-1.0, -1.0, 1.5, -1.0, 1.5, 3.0, -1.0, 3.0]));
|
||||
// Schwerpunkte: (4/3, 2/3) und (2/3, 4/3) -> beide liegen links von 1.5: nichts abgeschnitten.
|
||||
assert!(o.is_empty());
|
||||
let o = cut(json!([-1.0, -1.0, 1.0, -1.0, 1.0, 3.0, -1.0, 3.0]));
|
||||
assert_eq!(o[0]["entity"]["indices"], json!([0, 1, 2]));
|
||||
assert_eq!(o[0]["entity"]["positions"], json!([0, 0, 1, 2, 2, 3, 0, 2, 4]));
|
||||
assert_eq!(o[0]["entity"]["vertexColors"], json!([0, 0, 0, 2, 2, 2, 3, 3, 3]));
|
||||
// ausserhalb von allem / kaputtes Polygon
|
||||
assert!(cut(json!([10, 10, 11, 10, 11, 11])).is_empty());
|
||||
assert!(cut(json!([0, 0, 1])).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hoehe_einer_linie() {
|
||||
let o = run(json!({"cmd":"contourZ","id":"hl","index":1,"z":510}));
|
||||
|
||||
@@ -317,6 +317,11 @@ pub fn run(lk: &Lookup, req: &Request) -> Result<GripResult, String> {
|
||||
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()
|
||||
}
|
||||
// [193] Gelände-Netz: Netzpunkt `index` (Vertex-Nummer) im Grundriss verschieben, die Höhe bleibt (Höhe: Befehl `meshPointZ`).
|
||||
"meshGrip" => {
|
||||
let (index, (x, y)) = (r.n("index"), r.p("pt"));
|
||||
patch("context", r.s("id"), &|e| mesh_grip(e, index, x, y)).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()
|
||||
@@ -375,7 +380,7 @@ pub fn run(lk: &Lookup, req: &Request) -> Result<GripResult, String> {
|
||||
Some("roof") => "roofs",
|
||||
Some("ceiling") => "ceilings",
|
||||
Some("room") => "rooms",
|
||||
Some("excavation") => "context",
|
||||
Some("excavation") | Some("contours") => "context",
|
||||
Some("polyline") => "drawings2d",
|
||||
_ => return Ok(GripResult::default()),
|
||||
};
|
||||
@@ -1475,6 +1480,58 @@ fn contour_edit(e: &Value, flat: f64, at: Option<(f64, f64)>, insert: bool) -> O
|
||||
None
|
||||
}
|
||||
|
||||
/// [193] Netzpunkt `index` eines `terrainMesh`/`importedMesh` auf `(x, y)` setzen (z unverändert).
|
||||
fn mesh_grip(e: &Value, index: f64, x: f64, y: f64) -> Option<Value> {
|
||||
if !(x.is_finite() && y.is_finite()) || !matches!(field(e, "type").and_then(Value::as_str), Some("terrainMesh" | "importedMesh")) {
|
||||
return None;
|
||||
}
|
||||
let pos = arr(field(e, "positions"));
|
||||
let i = index_in(index, pos.len() / 3)?;
|
||||
let mut v = pos.to_vec();
|
||||
v[3 * i] = js_num(x);
|
||||
v[3 * i + 1] = js_num(y);
|
||||
Some(with(e, "positions", Value::Array(v)))
|
||||
}
|
||||
|
||||
/// [193] Höhenlinien-Punkt löschen bzw. aufräumen (flacher Index über alle Linien des Satzes). Mindestens 2 Punkte
|
||||
/// (offen) bzw. 3 (geschlossen) bleiben. Aufräumen entfernt den Punkt nur, wenn er auf dem Nachbarpunkt oder (innen) auf
|
||||
/// der Geraden zwischen den Nachbarn liegt. Eine wahre Kurve (`curve`) fällt weg. Einfügen läuft über `contourInsert`.
|
||||
fn contour_ring_edit(e: &Value, flat: f64, edit: &RingEdit) -> Option<Value> {
|
||||
if field(e, "type").and_then(Value::as_str) != Some("contourSet") || matches!(edit, RingEdit::Insert(..)) {
|
||||
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, n) = (truthy(field(c, "closed")), pts.len());
|
||||
if let Some(i) = index_in(flat - off as f64, n) {
|
||||
if n <= if closed { 3 } else { 2 } {
|
||||
return None;
|
||||
}
|
||||
if let RingEdit::Tidy(tol) = *edit {
|
||||
let near = |a: usize, b: usize| {
|
||||
let (p, q) = (xy(&pts[a]), xy(&pts[b]));
|
||||
hypot2(p.0 - q.0, p.1 - q.1) < tol
|
||||
};
|
||||
let end = !closed && (i == 0 || i == n - 1);
|
||||
let hit = if end { near(i, if i == 0 { 1 } else { n - 2 }) } else { near(i, (i + n - 1) % n) || near(i, (i + 1) % n) || point_near_neighbors(pts, i, tol) };
|
||||
if !hit {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
let mut v = pts.to_vec();
|
||||
v.remove(i);
|
||||
let nc = with_opt(&with(c, "pts", Value::Array(v)), vec![("curve", None)]);
|
||||
let mut out = cs.to_vec();
|
||||
out[ci] = nc;
|
||||
return Some(with(e, "contours", Value::Array(out)));
|
||||
}
|
||||
off += n;
|
||||
}
|
||||
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")) {
|
||||
@@ -1496,6 +1553,7 @@ fn ring_edit(e: &Value, kind: &str, flat: f64, edit: &RingEdit) -> Option<Value>
|
||||
"excavation" => simple("outline", "outlineBulge"),
|
||||
"room" => simple("boundary", "boundaryBulge"),
|
||||
"polyline" => polyline_ring_edit(e, flat, edit),
|
||||
"contours" => contour_ring_edit(e, flat, edit),
|
||||
"roof" => {
|
||||
let outline = field(e, "outline")?.as_array()?;
|
||||
let holes: Vec<&Vec<Value>> = arr(field(e, "holes")).iter().filter_map(|h| h.as_array()).collect();
|
||||
@@ -2132,6 +2190,37 @@ mod tests {
|
||||
assert!(ent(json!({"op": "contourInsert", "id": "hl", "seg": 6, "pt": [1, 2]})).is_none());
|
||||
}
|
||||
|
||||
/// [193] Netzpunkt im Grundriss verschieben (z bleibt).
|
||||
#[test]
|
||||
fn netzpunkt_griff() {
|
||||
let r = root(json!({"context": [{"id": "tm", "type": "terrainMesh", "positions": [0, 0, 5, 1, 0, 6, 0, 1, 7], "indices": [0, 1, 2]}]}));
|
||||
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": "meshGrip", "id": "tm", "index": 1, "pt": [3, 4]})).unwrap();
|
||||
assert_eq!(e["positions"], json!([0, 0, 5, 3, 4, 6, 0, 1, 7]));
|
||||
assert!(ent(json!({"op": "meshGrip", "id": "tm", "index": 3, "pt": [3, 4]})).is_none());
|
||||
}
|
||||
|
||||
/// [193] Höhenlinien-Punkt löschen / aufräumen (Mindestpunktzahl offen 2, geschlossen 3).
|
||||
#[test]
|
||||
fn hoehenlinien_punkt_loeschen() {
|
||||
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}]},
|
||||
{"z": 3, "closed": false, "pts": [{"x": 0, "y": 5}, {"x": 5, "y": 5}]}]}]}));
|
||||
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": "ringDelete", "kind": "contours", "id": "hl", "index": 1})).unwrap();
|
||||
assert_eq!(e["contours"][0]["pts"].as_array().unwrap().len(), 2);
|
||||
assert_eq!(e["contours"][0]["pts"][1], json!({"x": 4, "y": 0}));
|
||||
// geschlossen mit 3 Punkten und offen mit 2: nicht löschbar
|
||||
assert!(ent(json!({"op": "ringDelete", "kind": "contours", "id": "hl", "index": 4})).is_none());
|
||||
assert!(ent(json!({"op": "ringDelete", "kind": "contours", "id": "hl", "index": 6})).is_none());
|
||||
assert!(ent(json!({"op": "ringDelete", "kind": "contours", "id": "hl", "index": 9})).is_none());
|
||||
// Aufräumen: Punkt 1 liegt auf der Geraden -> weg; ein Punkt abseits bliebe.
|
||||
let e = ent(json!({"op": "ringTidy", "kind": "contours", "id": "hl", "index": 1, "tol": 0.01})).unwrap();
|
||||
assert_eq!(e["contours"][0]["pts"].as_array().unwrap().len(), 2);
|
||||
assert!(ent(json!({"op": "ringTidy", "kind": "contours", "id": "hl", "index": 4, "tol": 0.01})).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() {
|
||||
|
||||
Reference in new issue
Block a user