diff --git a/crates/engine/src/cmd/distortcmds.rs b/crates/engine/src/cmd/distortcmds.rs index c1ca7ac644..f5d38c0bba 100644 --- a/crates/engine/src/cmd/distortcmds.rs +++ b/crates/engine/src/cmd/distortcmds.rs @@ -214,6 +214,15 @@ fn uniform() -> WidthProfile { /// Width points closer than this (in t) are at the same place: a discontinuous point. const SAME_T: f64 = 1e-6; +/// The outgoing half of the discontinuous width point whose incoming half is point `a`: point `a` +/// and the next one are the two halves when they are the only points within 1e-6 (in t) of `a`'s +/// place. None when `a` is out of range or not such an incoming half. +pub fn width_point_pair(points: &[(f64, f64, f64)], a: usize) -> Option { + let t = points.get(a)?.0; + let at: Vec = (0..points.len()).filter(|i| (points[*i].0 - t).abs() <= SAME_T).collect(); + (at == [a, a + 1]).then_some(a + 1) +} + /// The stroke of `id` that width point commands edit (a compound path's for its members). fn stroke_mut(d: &mut Document, id: NodeId) -> Result<&mut vectorcraft_doc::StrokeLayer> { let id = stroke_owner(d, id); @@ -316,11 +325,10 @@ fn set_pair( single: Option, ) -> Result { const C: &str = "stroke.widthPoint.set"; - let at = points.get(i).map_or(f64::NAN, |q| q.0); - let halves: Vec = (0..points.len()).filter(|k| (points[*k].0 - at).abs() <= SAME_T).collect(); - if halves != [i, j] { + if width_point_pair(points, i) != Some(j) { return Err(bad(C, "`index` and `outIndex` must be the two halves of one discontinuous width point")); } + let at = points[i].0; let (old_in, old_out) = (points[i], points[j]); if (old_in.0 - inc.0).abs() > SAME_T { return Err(bad(C, "a discontinuous width point keeps its place")); diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 4f7532108d..5381a549de 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -28,7 +28,7 @@ use vectorcraft_tools::{PaintDefaults, Tool}; pub use cmd::clipboard::{Clipboard, SourceLayer}; pub use cmd::create::constrain_click; -pub use cmd::distortcmds::perspective_click; +pub use cmd::distortcmds::{perspective_click, width_point_pair}; pub use cmd::graph::{GraphTypeSubject, calculated_axes as graph_calculated_axes, graph_type_subject}; pub use cmd::params::{check_keys, check_params}; pub use cmd::pathops::auto_simplify_level; diff --git a/crates/ui-egui/src/dialogs/width_point.rs b/crates/ui-egui/src/dialogs/width_point.rs index 0c4c41c356..b0881b9082 100644 --- a/crates/ui-egui/src/dialogs/width_point.rs +++ b/crates/ui-egui/src/dialogs/width_point.rs @@ -21,6 +21,7 @@ use egui::vec2; use serde_json::{Value, json}; use vectorcraft_doc::Unit; use vectorcraft_engine::doc::NodeId; +use vectorcraft_engine::width_point_pair; use super::swatch_options::{grid, label}; use super::{DialogSpec, run_and_close}; @@ -30,10 +31,6 @@ use crate::{VectorcraftApp, widgets}; /// The dialog kind of Width Point Edit. pub const KIND: &str = "widthPoint"; -/// Width points closer than this (in t) are the two halves of one discontinuous point, as the -/// engine and the Width tool count them. -const SAME_T: f64 = 1e-6; - pub(super) const SPEC: DialogSpec = DialogSpec { heading: |_| tl!("Width Point Edit").into(), body, confirm, discard: Some("Delete"), min_width: 300.0, ..DialogSpec::FORM }; @@ -45,17 +42,12 @@ fn point(app: &VectorcraftApp, id: u64, index: usize) -> Option<(f64, f64, f64)> } /// The incoming and outgoing halves of the discontinuous point that width point `index` of `id` -/// belongs to, or none when it is a continuous point. As `stroke.widthPoint.set` pairs them, a -/// point `a` and the next one are the two halves when they are the only points within -/// [`SAME_T`] of `a`'s place, so either half finds the same pair. +/// belongs to, or none when it is a continuous point. The halves are paired by +/// [`width_point_pair`], as `stroke.widthPoint.set` pairs them, so either half finds the same pair. fn halves(app: &VectorcraftApp, id: u64, index: usize) -> Option<(usize, usize)> { let points = &app.session.active()?.doc.node(NodeId(id))?.appearance.stroke()?.profile.as_ref()?.points; points.get(index)?; - let pair = |a: usize| { - let t = points.get(a)?.0; - let at: Vec = (0..points.len()).filter(|i| (points[*i].0 - t).abs() <= SAME_T).collect(); - (at == [a, a + 1]).then_some((a, a + 1)) - }; + let pair = |a: usize| width_point_pair(points, a).map(|b| (a, b)); pair(index).or_else(|| index.checked_sub(1).and_then(pair)) }