Merge branch 'worktree-agent-a2a66834ac0cb027d' into claude/practical-noether-1hb2s1
This commit is contained in:
14 files changed
+452
-21
No files matched your search
@@ -315,6 +315,97 @@ pub fn clip_prims(prims: Vec<Primitive>, s: Option<&Strip>) -> Vec<Primitive> {
|
||||
out
|
||||
}
|
||||
|
||||
/// [176] Grundriss-Unterlage: Primitive eines Segments in dessen Ansichtskoordinaten (x = u, y = Tiefe hinter
|
||||
/// der Ebene) auf den ganzen Streifen schneiden (u-Bereich und Winkelhalbierende) und in die Abwicklung
|
||||
/// schieben. Flächen und Linien geometrisch, alles andere nach Anker.
|
||||
pub fn clip_plan_prims(prims: Vec<Primitive>, s: &Strip) -> Vec<Primitive> {
|
||||
let g = |h: &[f64; 3], x: f64, y: f64| h[0] * x + h[1] * y - h[2];
|
||||
let mut out = Vec::with_capacity(prims.len());
|
||||
for mut q in prims {
|
||||
let keep = match &mut q {
|
||||
Primitive::Polygon { pts, no_stroke_edges, .. } => {
|
||||
let p2: Vec<[f64; 2]> = pts.iter().map(|p| [p.x, p.y]).collect();
|
||||
match clip_poly_u(&p2, no_stroke_edges.as_deref(), s.lo, s.hi) {
|
||||
Some((mut cur, nse)) => {
|
||||
let mut flags = vec![false; cur.len()];
|
||||
for &k in nse.as_deref().unwrap_or(&[]) {
|
||||
if let Some(f) = flags.get_mut(k as usize) {
|
||||
*f = true;
|
||||
}
|
||||
}
|
||||
for h in &s.half {
|
||||
if cur.iter().all(|q| g(h, q[0], q[1]) <= 0.0) {
|
||||
continue;
|
||||
}
|
||||
(cur, flags) = clip_ring(&cur, &flags, &|q| g(h, q[0], q[1]), &|a, b, t| lerp2(a, b, t), true);
|
||||
if cur.len() < 3 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if cur.len() < 3 {
|
||||
false
|
||||
} else {
|
||||
let nse: Vec<f64> = flags.iter().enumerate().filter(|(_, f)| **f).map(|(i, _)| i as f64).collect();
|
||||
*pts = cur.into_iter().map(|p| Vec2 { x: p[0], y: p[1] }).collect();
|
||||
*no_stroke_edges = (!nse.is_empty()).then_some(nse);
|
||||
true
|
||||
}
|
||||
}
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
Primitive::Line { a, b, .. } => match clip_line_to_u(&[[a.x, a.y], [b.x, b.y]], s.lo, s.hi).first() {
|
||||
Some(l) if l.len() == 2 => {
|
||||
let (mut p, mut r) = (l[0], l[1]);
|
||||
let mut ok = true;
|
||||
for h in &s.half {
|
||||
let (gp, gr) = (g(h, p[0], p[1]), g(h, r[0], r[1]));
|
||||
if gp > 0.0 && gr > 0.0 {
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
if gp > 0.0 {
|
||||
p = lerp2(&p, &r, gp / (gp - gr));
|
||||
} else if gr > 0.0 {
|
||||
r = lerp2(&p, &r, gp / (gp - gr));
|
||||
}
|
||||
}
|
||||
if ok {
|
||||
*a = Vec2 { x: p[0], y: p[1] };
|
||||
*b = Vec2 { x: r[0], y: r[1] };
|
||||
}
|
||||
ok
|
||||
}
|
||||
_ => false,
|
||||
},
|
||||
other => {
|
||||
let (u, w) = anchor_pt(other);
|
||||
u >= s.lo - 1e-9 && u <= s.hi + 1e-9 && s.half.iter().all(|h| g(h, u, w) <= 1e-9)
|
||||
}
|
||||
};
|
||||
if keep {
|
||||
shift_prim(&mut q, s.offset);
|
||||
out.push(q);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Anker (x, y) eines Primitivs.
|
||||
fn anchor_pt(q: &Primitive) -> (f64, f64) {
|
||||
match q {
|
||||
Primitive::Polygon { pts, .. } => {
|
||||
let n = pts.len().max(1) as f64;
|
||||
(pts.iter().map(|p| p.x).sum::<f64>() / n, pts.iter().map(|p| p.y).sum::<f64>() / n)
|
||||
}
|
||||
Primitive::Line { a, b, .. } => (0.5 * (a.x + b.x), 0.5 * (a.y + b.y)),
|
||||
Primitive::Arc { center, .. } | Primitive::DrawingCircle { center, .. } | Primitive::DrawingArc { center, .. } => (center.x, center.y),
|
||||
Primitive::Text { at, .. } | Primitive::DrawingText { at, .. } => (at.x, at.y),
|
||||
Primitive::DrawingTextPath { path_pts, .. } => path_pts.first().map_or((0.0, 0.0), |p| (p.x, p.y)),
|
||||
Primitive::DrawingImage { min, max, .. } => (0.5 * (min.x + max.x), 0.5 * (min.y + max.y)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Ganze Gruppe (z. B. ein Baum) behalten, wenn ihre Mitte im Streifen liegt; verschoben.
|
||||
pub fn keep_group(prims: Vec<Primitive>, s: Option<&Strip>) -> Vec<Primitive> {
|
||||
let Some(s) = s else { return prims };
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
use super::prim::Primitive;
|
||||
use crate::jsmath::hypot2;
|
||||
use crate::jsnum::{atan2, max, min};
|
||||
use super::section::LevelSpec;
|
||||
use crate::model::Vec2;
|
||||
|
||||
/// Abstand (Meter) zwischen tiefster Schnitt-/Ansichtskante und Grundriss-Oberkante.
|
||||
@@ -147,14 +148,38 @@ fn as_reference(pr: Primitive) -> Primitive {
|
||||
/// bei `section_min_y` liegt (`planUnderlayPrimitives`): gedimmt, ohne Auswahl-Ids, dazu die
|
||||
/// Schnittlinie. Ohne gültige Linie leer.
|
||||
pub fn underlay_primitives(line: Option<[Vec2; 2]>, direction_sign: Option<f64>, plan: impl IntoIterator<Item = Primitive>, section_min_y: f64) -> Vec<Primitive> {
|
||||
underlay_for_level(&LevelSpec { line, direction_sign, ..Default::default() }, plan, section_min_y)
|
||||
}
|
||||
|
||||
/// Wie [`underlay_primitives`] für eine ganze Schnittebene; mit Knickpunkten ([176]) je Segment mit dessen
|
||||
/// Ebene abgebildet, auf den Streifen zugeschnitten und abgewickelt (wie der Schnitt selbst).
|
||||
pub fn underlay_for_level(level: &LevelSpec, plan: impl IntoIterator<Item = Primitive>, section_min_y: f64) -> Vec<Primitive> {
|
||||
let (line, direction_sign) = (level.line, level.direction_sign);
|
||||
let Some(line) = line else { return vec![] };
|
||||
let Some(base) = section_map(line, direction_sign) else { return vec![] };
|
||||
let mut mapped = Vec::new();
|
||||
let mut top = f64::NEG_INFINITY;
|
||||
for pr in plan {
|
||||
let Some(q) = map_primitive(&base, pr) else { continue };
|
||||
max_y(&q, &mut top);
|
||||
mapped.push(q);
|
||||
let bent = super::section::unfold::segments(level);
|
||||
let plan: Vec<Primitive> = plan.into_iter().collect();
|
||||
match &bent {
|
||||
None => {
|
||||
for pr in plan {
|
||||
let Some(q) = map_primitive(&base, pr) else { continue };
|
||||
max_y(&q, &mut top);
|
||||
mapped.push(q);
|
||||
}
|
||||
}
|
||||
Some(segs) => {
|
||||
for (seg, strip) in segs {
|
||||
let (Some(l), Some(strip)) = (seg.line, strip) else { continue };
|
||||
let Some(m) = section_map(l, direction_sign) else { continue };
|
||||
let part: Vec<Primitive> = plan.iter().cloned().filter_map(|pr| map_primitive(&m, pr)).collect();
|
||||
for q in super::section::unfold::clip_plan_prims(part, strip) {
|
||||
max_y(&q, &mut top);
|
||||
mapped.push(q);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Die Schnittlinie (w = 0) liegt immer mit in der Unterlage.
|
||||
if 0.0 > top {
|
||||
@@ -165,7 +190,11 @@ pub fn underlay_primitives(line: Option<[Vec2; 2]>, direction_sign: Option<f64>,
|
||||
let shift_map = SectionMap { p0: o, u: Vec2 { x: 1.0, y: 0.0 }, n: Vec2 { x: 0.0, y: 1.0 }, theta: 0.0, dy: shift };
|
||||
let mut out: Vec<Primitive> = mapped.into_iter().filter_map(|q| map_primitive(&shift_map, q)).map(as_reference).collect();
|
||||
let [l0, l1] = line;
|
||||
let len = hypot2(l1.x - l0.x, l1.y - l0.y);
|
||||
// Länge der (abgewickelten) Linie: Summe der Segmente.
|
||||
let len = match &bent {
|
||||
Some(segs) => segs.iter().filter_map(|(s, _)| s.line).map(|[a, b]| hypot2(b.x - a.x, b.y - a.y)).sum(),
|
||||
None => hypot2(l1.x - l0.x, l1.y - l0.y),
|
||||
};
|
||||
out.push(Primitive::Line {
|
||||
a: Vec2 { x: 0.0, y: shift },
|
||||
b: Vec2 { x: len, y: shift },
|
||||
@@ -206,11 +235,11 @@ pub fn plan_out_primitives(out: &super::domains::PlanOut) -> Result<Vec<Primitiv
|
||||
|
||||
/// Argumente aus dem Abfrage-Text: die `planDomains`-Argumente des Geschosses plus `level` (Schnitt/
|
||||
/// Ansicht) und `minY` (tiefste Kante des Schnitts).
|
||||
pub fn parse_args(text: &str) -> Result<(Option<[Vec2; 2]>, Option<f64>, f64), String> {
|
||||
pub fn parse_args(text: &str) -> Result<(super::section::LevelSpec, f64), String> {
|
||||
let v: serde_json::Value = serde_json::from_str(text).map_err(|e| format!("planUnderlay: {e}"))?;
|
||||
let level = super::section::LevelSpec::from_json(v.get("level").unwrap_or(&serde_json::Value::Null));
|
||||
let min_y = v.get("minY").and_then(|x| x.as_f64()).ok_or("planUnderlay: minY fehlt")?;
|
||||
Ok((level.line, level.direction_sign, min_y))
|
||||
Ok((level, min_y))
|
||||
}
|
||||
|
||||
/// Plan-Puffer (eine Gruppe) der Unterlage.
|
||||
@@ -311,4 +340,23 @@ mod tests {
|
||||
// Blick nach −x: θ = −90°, das Muster dreht um +90°.
|
||||
assert!((hatch.angle - 100.0).abs() < 1e-9, "{}", hatch.angle);
|
||||
}
|
||||
|
||||
/// [176] Geknickte Schnittlinie: die Unterlage folgt den Segmenten (je Segment abgewickelt, nichts doppelt).
|
||||
#[test]
|
||||
fn geknickte_linie_wickelt_die_unterlage_ab() {
|
||||
let lv = LevelSpec { line: Some([v(0.0, 0.0), v(6.0, 6.0)]), bends: vec![v(6.0, 0.0)], direction_sign: Some(1.0), ..Default::default() };
|
||||
// Ein Strich im ersten Streifen (u = 1, Tiefe 2), einer im zweiten (u = 3 ab Knick, Tiefe −2).
|
||||
let plan = vec![line(v(1.0, 2.0), v(1.0, 2.5)), line(v(8.0, 3.0), v(8.0, 3.5))];
|
||||
let out = underlay_for_level(&lv, plan.clone(), 0.0);
|
||||
let xs: Vec<f64> = out.iter().filter_map(|q| if let Primitive::Line { a, cls, .. } = q { (cls == "t").then_some(a.x) } else { None }).collect();
|
||||
assert_eq!(xs.len(), 2, "jeder Strich genau einmal: {xs:?}");
|
||||
assert!((xs[0] - 1.0).abs() < 1e-9 && (xs[1] - 9.0).abs() < 1e-9, "{xs:?}");
|
||||
// Abgewickelte Linie = Summe der Segmente (6 + 6).
|
||||
let Primitive::Line { a, b, cls, .. } = out.last().unwrap() else { panic!() };
|
||||
assert_eq!(cls, "section-underlay-line");
|
||||
assert!((b.x - a.x - 12.0).abs() < 1e-9);
|
||||
// Ohne Knick bleibt alles beim Alten.
|
||||
let gerade = LevelSpec { bends: vec![], ..lv };
|
||||
assert_eq!(underlay_for_level(&gerade, plan.clone(), 0.0).len(), underlay_primitives(gerade.line, Some(1.0), plan, 0.0).len());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user