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.
This commit is contained in:
Tanish Manku
2026-11-19 15:14:28 -07:00
parent c6e80bea8f
commit bd7d94c8ea
3 changed files with 25 additions and 7 deletions
+10 -6
View File
@@ -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<Item = &'a Entity>, m: &Mat3, at: Vec2) -> Option<Vec2> {
/// 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<Item = &'a Entity>, m: &Mat3, at: Vec2) -> Result<Option<Vec2>> {
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<Item = &'a Entity>, 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<Option<Vec2>> {
/// The key point of objects `hs` in the drawing nearest `at`.
pub(crate) fn key_point_of(d: &Drawing, hs: &[Handle], at: Vec2) -> Result<Vec2> {
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."))
}
+1 -1
View File
@@ -224,7 +224,7 @@ fn source_key_point(d: &Drawing, h: Handle, at: Vec2, tol: f64) -> Result<Vec2>
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.
+14
View File
@@ -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<String> = 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}");
}