From bd7d94c8ea4423df14669a622b0ba91d0a813596 Mon Sep 17 00:00:00 2001 From: Tanish Manku Date: Thu, 19 Nov 2026 15:13:49 -0700 Subject: [PATCH] Stack d1: #2368: refuse a key point search over more than 10,000 objects The search took the first 10,000 objects and could miss a nearer key point on a later one; now it says so and asks for a point. --- crates/engine/src/cmd/array.rs | 16 ++++++++++------ crates/engine/src/cmd/array/edit.rs | 2 +- crates/engine/tests/it/array_key_point.rs | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/crates/engine/src/cmd/array.rs b/crates/engine/src/cmd/array.rs index a3f50d937..fc0f556fa 100644 --- a/crates/engine/src/cmd/array.rs +++ b/crates/engine/src/cmd/array.rs @@ -252,13 +252,17 @@ fn bounds(d: &Drawing, hs: &[Handle]) -> Bounds2 { /// The centre of the first object (the point a non-rotated polar copy is moved by). /// The key point of `ents`, placed by `m`, nearest `at`: one of their grips (end points, -/// midpoints, centres, vertices, insertion points). `None` when they have none. -pub(crate) fn nearest_key_point<'a>(ents: impl Iterator, m: &Mat3, at: Vec2) -> Option { +/// midpoints, centres, vertices, insertion points). `None` when they have none. More than +/// [`MAX_KEY_OBJECTS`] objects are refused: searching only some could miss the nearest one. +pub(crate) fn nearest_key_point<'a>(ents: impl Iterator, m: &Mat3, at: Vec2) -> Result> { if !at.is_finite() { - return None; + return Ok(None); } let mut best: Option<(Vec2, f64)> = None; - for e in ents.take(MAX_KEY_OBJECTS) { + for (n, e) in ents.enumerate() { + if n >= MAX_KEY_OBJECTS { + return Err(other(format!("Too many objects to search for a key point (at most {MAX_KEY_OBJECTS}); specify a point."))); + } let mut k = e.kind.clone(); k.transform(m); for g in k.grips().into_iter().filter(|g| g.is_finite()) { @@ -268,7 +272,7 @@ pub(crate) fn nearest_key_point<'a>(ents: impl Iterator, m: & } } } - best.map(|b| b.0) + Ok(best.map(|b| b.0)) } /// `keyPoint` of a JSON form: absent or null is none; anything else must be a point, rather than @@ -282,7 +286,7 @@ pub(crate) fn key_point_param(cmd: &str, p: &Value) -> Result> { /// The key point of objects `hs` in the drawing nearest `at`. pub(crate) fn key_point_of(d: &Drawing, hs: &[Handle], at: Vec2) -> Result { - nearest_key_point(hs.iter().filter_map(|h| d.entity(*h)).map(|e| &**e), &Mat3::IDENTITY, at) + nearest_key_point(hs.iter().filter_map(|h| d.entity(*h)).map(|e| &**e), &Mat3::IDENTITY, at)? .ok_or_else(|| other("The objects have no key point; specify a point.")) } diff --git a/crates/engine/src/cmd/array/edit.rs b/crates/engine/src/cmd/array/edit.rs index 7b057d032..8732e0645 100644 --- a/crates/engine/src/cmd/array/edit.rs +++ b/crates/engine/src/cmd/array/edit.rs @@ -224,7 +224,7 @@ fn source_key_point(d: &Drawing, h: Handle, at: Vec2, tol: f64) -> Result let (ins, blk, p) = array_of(d, h)?; let src = d.block(&p.source).ok_or_else(|| other("The array's source block is missing."))?; let m = ins.transform(blk.base.xy()).then_before(item_place(&p, item)?).then_before(Mat3::translate(-src.base.xy())); - nearest_key_point(src.entities.iter().map(|e| e.as_ref()), &m, at).ok_or_else(|| other("The source objects have no key point; specify a point.")) + nearest_key_point(src.entities.iter().map(|e| e.as_ref()), &m, at)?.ok_or_else(|| other("The source objects have no key point; specify a point.")) } /// Move the base point of array `h` to world point `base` without moving the items. diff --git a/crates/engine/tests/it/array_key_point.rs b/crates/engine/tests/it/array_key_point.rs index 0dd8c133a..98671f41a 100644 --- a/crates/engine/tests/it/array_key_point.rs +++ b/crates/engine/tests/it/array_key_point.rs @@ -168,3 +168,17 @@ fn a_key_point_that_is_not_a_point_is_refused() { let c = s.execute("circle", &json!({"center": [50, 50], "radius": 1})).unwrap()["handle"].clone(); assert!(s.execute("arrayedit", &json!({"handle": h.hex(), "replace": {"handles": [c], "keyPoint": {}, "items": [[1, 0, 0]]}})).is_err()); } + +/// More objects than the key point search takes are refused rather than searched in part, which +/// could miss the nearest key point. +#[test] +fn a_key_point_of_too_many_objects_is_refused() { + let mut s = Session::new(); + // 10,001 lines; the last one ends nearest the key point. + let pts: Vec<[f64; 2]> = (0..10_002).map(|i| [f64::from(i), 0.0]).collect(); + let r = s.execute("line", &json!({ "points": pts })).unwrap(); + let hs: Vec = s.doc().unwrap().model.iter().map(|e| e.handle.hex()).collect(); + assert_eq!(hs.len(), 10_001, "{r}"); + let e = s.execute("arraypolar", &json!({"handles": hs, "center": [0, -50], "count": 2, "keyPoint": [10_001.2, 0]})).unwrap_err(); + assert!(e.to_string().contains("10000"), "{e}"); +}