[K4] S3 Split und Segment löschen im Kern (cmd_split.rs); Segment löschen ersetzt jetzt an Ort und Stelle

cutSegment lieferte ein Element mit neuer id als replace: der Kern hängte es an, das Original blieb stehen. Jetzt behält das Element id und Attribute. TS-Rechnung, Kernel-Fassaden und k2_*-Bindungen für Split/Segment löschen gelöscht. Gleichheit (einmalig, Test gelöscht): 2808 Split- und 9000 Segment-Fälle.
This commit is contained in:
Claude committed 2026-10-29 14:05:42 +00:00
1 parent 4c45331e67
commit ddff1ede18
11 files changed
+528 -513

No files matched your search

+2 -105
View File
@@ -2,52 +2,16 @@
//!
//! Flache `f64`-Puffer statt JSON:
//! - Punktliste `pts`: `[x0, y0, x1, y1, …]`
//! - Liste von Punktlisten: `[n, (n_pts, x0, y0, …)…]`
//! - „kein Ergebnis" (TS `null`): leerer Puffer.
//!
//! Kaputte Eingaben (ungerade Längen, falsche Zählungen, ungültige Indizes) ergeben ein
//! leeres Ergebnis statt eines Panics.
//! Kaputte Eingaben (ungerade Längen) ergeben ein leeres Ergebnis statt eines Panics.
//! (Split/Segment löschen rechnen im Kern selbst: `cmd_split.rs`.)
use kernel2d::{self as k, TangentTarget, Vec2};
use wasm_bindgen::prelude::wasm_bindgen;
// --- Lesen -------------------------------------------------------------------
/// Sequentieller Leser mit Zählungs-Prüfung.
struct Reader<'a> {
buf: &'a [f64],
i: usize,
}
impl<'a> Reader<'a> {
fn new(buf: &'a [f64]) -> Self {
Reader { buf, i: 0 }
}
fn f(&mut self) -> Option<f64> {
let v = *self.buf.get(self.i)?;
self.i += 1;
Some(v)
}
fn count(&mut self) -> Option<usize> {
count(self.f()?)
}
fn pts(&mut self, n: usize) -> Option<Vec<Vec2>> {
let end = self.i.checked_add(n.checked_mul(2)?)?;
let slice = self.buf.get(self.i..end)?;
self.i = end;
Some(slice.chunks_exact(2).map(|c| Vec2::new(c[0], c[1])).collect())
}
}
/// Nicht-negative ganze Zahl (Zählung/Index) oder None.
fn count(v: f64) -> Option<usize> {
if v.is_finite() && v >= 0.0 && v.fract() == 0.0 && v < 1e9 {
Some(v as usize)
} else {
None
}
}
fn pts(buf: &[f64]) -> Option<Vec<Vec2>> {
if buf.len() % 2 != 0 {
return None;
@@ -55,12 +19,6 @@ fn pts(buf: &[f64]) -> Option<Vec<Vec2>> {
Some(buf.chunks_exact(2).map(|c| Vec2::new(c[0], c[1])).collect())
}
fn pt_lists(buf: &[f64]) -> Option<Vec<Vec<Vec2>>> {
let mut r = Reader::new(buf);
let n = r.count()?;
(0..n).map(|_| r.count().and_then(|m| r.pts(m))).collect()
}
fn tangent_target(buf: &[f64]) -> Option<TangentTarget> {
match *buf {
[kind, ax, ay, bx, by] if kind == 0.0 => {
@@ -86,25 +44,6 @@ fn enc_pts(pts: &[Vec2]) -> Vec<f64> {
out
}
fn enc_pt_lists(lists: &[Vec<Vec2>]) -> Vec<f64> {
let mut out = vec![lists.len() as f64];
for l in lists {
out.push(l.len() as f64);
push_pts(&mut out, l);
}
out
}
/// `[n_pts, xy…, bulge…]` (bulge-Anzahl = Rest des Puffers).
fn enc_pts_bulge(pts: &[Vec2], bulge: &[f64]) -> Vec<f64> {
let mut out = vec![pts.len() as f64];
push_pts(&mut out, pts);
out.extend_from_slice(bulge);
out
}
const EMPTY_LIST: [f64; 1] = [0.0];
// --- Punkt/Strecke, Fläche, Kreise ---------------------------------------------
#[wasm_bindgen]
@@ -171,54 +110,12 @@ pub fn k2_fillet_corner(cx: f64, cy: f64, p1x: f64, p1y: f64, p2x: f64, p2y: f64
}
// --- Split / Join / Segment löschen / Bruch ----------------------------------------
#[wasm_bindgen]
pub fn k2_split_polyline_at_param(p: &[f64], closed: bool, edge_index: f64, t: f64) -> Vec<f64> {
let (Some(p), Some(e)) = (pts(p), count(edge_index)) else { return EMPTY_LIST.to_vec() };
enc_pt_lists(&k::split_polyline_at_param(&p, closed, e, t))
}
/// Zwei Ringe oder leere Liste (TS `null`).
#[wasm_bindgen]
pub fn k2_split_closed_by_chord(p: &[f64], i: f64, ti: f64, j: f64, tj: f64) -> Vec<f64> {
let (Some(p), Some(i), Some(j)) = (pts(p), count(i), count(j)) else { return EMPTY_LIST.to_vec() };
match k::split_closed_by_chord(&p, i, ti, j, tj) {
Some((a, b)) => enc_pt_lists(&[a, b]),
None => EMPTY_LIST.to_vec(),
}
}
#[wasm_bindgen]
pub fn k2_remove_segment(p: &[f64], closed: bool, edge_index: f64) -> Vec<f64> {
let (Some(p), Some(e)) = (pts(p), count(edge_index)) else { return Vec::new() };
enc_pts(&k::remove_segment(&p, closed, e).pts)
}
#[wasm_bindgen]
pub fn k2_remove_segment_with_bulge(p: &[f64], bulge: &[f64], closed: bool, edge_index: f64) -> Vec<f64> {
let (Some(p), Some(e)) = (pts(p), count(edge_index)) else { return enc_pts_bulge(&[], &[]) };
let r = k::remove_segment_with_bulge(&p, bulge, closed, e);
enc_pts_bulge(&r.pts, &r.bulge)
}
#[wasm_bindgen]
pub fn k2_split_at_intersections(target: &[f64], closed: bool, others: &[f64]) -> Vec<f64> {
let (Some(target), Some(others)) = (pts(target), pt_lists(others)) else { return EMPTY_LIST.to_vec() };
enc_pt_lists(&k::split_at_intersections(&target, closed, &others))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn kaputte_puffer_ergeben_leere_ergebnisse() {
assert_eq!(k2_split_polyline_at_param(&[0.0, 0.0, 1.0, 0.0], false, -1.0, 0.5), vec![0.0]);
assert_eq!(k2_split_polyline_at_param(&[0.0, 0.0, 1.0, 0.0], true, 7.0, 0.5), vec![1.0, 2.0, 0.0, 0.0, 1.0, 0.0]);
assert!(k2_tangent_circle_centers(&[3.0], &[0.0, 0.0, 0.0, 1.0, 0.0], 1.0).is_empty());
}
}