From fe505edeff7b929ba0cd7d1019fc2df0da3f30d1 Mon Sep 17 00:00:00 2001 From: Keavon Chambers Date: Sat, 7 Nov 2026 06:01:29 -0800 Subject: [PATCH] Make the stroke and path nodes' distances and angles typed lengths and angles --- .../nodes/src/core_types/bevel_v1.rs | 66 +++++++ .../nodes/src/core_types/decimate_v1.rs | 66 +++++++ .../nodes/src/core_types/jitter_points_v1.rs | 66 +++++++ .../migrations/nodes/src/core_types/mod.rs | 12 ++ .../nodes/src/core_types/offset_path_v1.rs | 66 +++++++ .../nodes/src/core_types/offset_points_v1.rs | 66 +++++++ .../nodes/src/core_types/round_corners_v1.rs | 96 ++++++++++ .../src/core_types/sample_polyline_v1.rs | 80 ++++++++ .../nodes/src/core_types/scatter_points_v2.rs | 66 +++++++ .../nodes/src/core_types/simplify_v1.rs | 66 +++++++ .../nodes/src/core_types/weld_v1.rs | 66 +++++++ .../migrations/nodes/src/graphene_core/mod.rs | 2 + .../nodes/src/graphene_core/pack_strips_v1.rs | 73 +++++++ .../nodes/src/graphene_core/stroke_v1.rs | 79 ++++++++ document/migrations/nodes/src/lib.rs | 1 + .../document/graph_operation/utility_types.rs | 5 +- .../document/node_graph/node_inspector.rs | 15 +- .../graph_modification_utils.rs | 18 +- .../common_functionality/stroke_options.rs | 3 +- .../src/dynamic_executor/test.rs | 4 +- node-graph/nodes/vector/src/vector_nodes.rs | 178 ++++++++---------- 21 files changed, 976 insertions(+), 118 deletions(-) create mode 100644 document/migrations/nodes/src/core_types/bevel_v1.rs create mode 100644 document/migrations/nodes/src/core_types/decimate_v1.rs create mode 100644 document/migrations/nodes/src/core_types/jitter_points_v1.rs create mode 100644 document/migrations/nodes/src/core_types/mod.rs create mode 100644 document/migrations/nodes/src/core_types/offset_path_v1.rs create mode 100644 document/migrations/nodes/src/core_types/offset_points_v1.rs create mode 100644 document/migrations/nodes/src/core_types/round_corners_v1.rs create mode 100644 document/migrations/nodes/src/core_types/sample_polyline_v1.rs create mode 100644 document/migrations/nodes/src/core_types/scatter_points_v2.rs create mode 100644 document/migrations/nodes/src/core_types/simplify_v1.rs create mode 100644 document/migrations/nodes/src/core_types/weld_v1.rs create mode 100644 document/migrations/nodes/src/graphene_core/pack_strips_v1.rs create mode 100644 document/migrations/nodes/src/graphene_core/stroke_v1.rs diff --git a/document/migrations/nodes/src/core_types/bevel_v1.rs b/document/migrations/nodes/src/core_types/bevel_v1.rs new file mode 100644 index 0000000000..596b092dd6 --- /dev/null +++ b/document/migrations/nodes/src/core_types/bevel_v1.rs @@ -0,0 +1,66 @@ +use migration_core::{Inputs, Node, literal, literal_of, migration}; +use serde::{Deserialize, Serialize}; + +const DISTANCE: usize = 1; + +/// Frozen mirror of the `TaggedValue` variants this migration reads and writes. +#[derive(Serialize, Deserialize)] +enum Tagged { + None, + Number(f64), + Length(TypedLength), +} + +#[derive(Serialize, Deserialize)] +struct TypedLength { + value: f64, + unit: LengthUnit, +} + +#[derive(Serialize, Deserialize)] +enum LengthUnit { + Pixel, +} + +/// The distance is a length, which keeps the number of pixels it held. A wired number still feeds it, read as pixels. +#[migration("core_types::vector::BevelNode"@1 => @2)] +pub fn bevel_v1(node: &Node, inputs: &mut Inputs) { + let Some(slot) = node.inputs().get(DISTANCE) else { return }; + let Some(Tagged::Number(pixels)) = literal_of::(&slot.input) else { return }; + + inputs.set( + DISTANCE as u32, + literal(Tagged::Length(TypedLength { + value: pixels, + unit: LengthUnit::Pixel, + })), + ); +} + +#[cfg(test)] +mod tests { + use super::*; + use migration_core::decl; + use migration_runner::testing::{Fixture, SourceContract, check_commutation, check_undo}; + + fn contract() -> SourceContract { + SourceContract::new(2, |random, slot| if slot == 0 { literal(Tagged::None) } else { literal(Tagged::Number(random.below(100) as f64)) }) + } + + #[test] + fn rewrites_history() { + check_commutation(&BevelV1, &contract()); + check_undo(&BevelV1, &contract()); + } + + #[test] + fn a_number_of_pixels_becomes_a_length_in_pixels() { + let mut fixture = Fixture::new(&BevelV1); + let number = |number: f64| literal(Tagged::Number(number)); + let bevel = fixture.add(decl!("core_types::vector::BevelNode"@1), vec![literal(Tagged::None), number(10.)]); + let migrated = fixture.migrate(&BevelV1).unwrap(); + + let distance = literal(Tagged::Length(TypedLength { value: 10., unit: LengthUnit::Pixel })); + assert_eq!(fixture.inputs(&migrated, bevel), [literal(Tagged::None), distance]); + } +} diff --git a/document/migrations/nodes/src/core_types/decimate_v1.rs b/document/migrations/nodes/src/core_types/decimate_v1.rs new file mode 100644 index 0000000000..f54724217d --- /dev/null +++ b/document/migrations/nodes/src/core_types/decimate_v1.rs @@ -0,0 +1,66 @@ +use migration_core::{Inputs, Node, literal, literal_of, migration}; +use serde::{Deserialize, Serialize}; + +const TOLERANCE: usize = 1; + +/// Frozen mirror of the `TaggedValue` variants this migration reads and writes. +#[derive(Serialize, Deserialize)] +enum Tagged { + None, + Number(f64), + Length(TypedLength), +} + +#[derive(Serialize, Deserialize)] +struct TypedLength { + value: f64, + unit: LengthUnit, +} + +#[derive(Serialize, Deserialize)] +enum LengthUnit { + Pixel, +} + +/// The tolerance is a length, which keeps the number of pixels it held. A wired number still feeds it, read as pixels. +#[migration("core_types::vector::DecimateNode"@1 => @2)] +pub fn decimate_v1(node: &Node, inputs: &mut Inputs) { + let Some(slot) = node.inputs().get(TOLERANCE) else { return }; + let Some(Tagged::Number(pixels)) = literal_of::(&slot.input) else { return }; + + inputs.set( + TOLERANCE as u32, + literal(Tagged::Length(TypedLength { + value: pixels, + unit: LengthUnit::Pixel, + })), + ); +} + +#[cfg(test)] +mod tests { + use super::*; + use migration_core::decl; + use migration_runner::testing::{Fixture, SourceContract, check_commutation, check_undo}; + + fn contract() -> SourceContract { + SourceContract::new(2, |random, slot| if slot == 0 { literal(Tagged::None) } else { literal(Tagged::Number(random.below(100) as f64)) }) + } + + #[test] + fn rewrites_history() { + check_commutation(&DecimateV1, &contract()); + check_undo(&DecimateV1, &contract()); + } + + #[test] + fn a_number_of_pixels_becomes_a_length_in_pixels() { + let mut fixture = Fixture::new(&DecimateV1); + let number = |number: f64| literal(Tagged::Number(number)); + let decimate = fixture.add(decl!("core_types::vector::DecimateNode"@1), vec![literal(Tagged::None), number(5.)]); + let migrated = fixture.migrate(&DecimateV1).unwrap(); + + let tolerance = literal(Tagged::Length(TypedLength { value: 5., unit: LengthUnit::Pixel })); + assert_eq!(fixture.inputs(&migrated, decimate), [literal(Tagged::None), tolerance]); + } +} diff --git a/document/migrations/nodes/src/core_types/jitter_points_v1.rs b/document/migrations/nodes/src/core_types/jitter_points_v1.rs new file mode 100644 index 0000000000..d0a575a13d --- /dev/null +++ b/document/migrations/nodes/src/core_types/jitter_points_v1.rs @@ -0,0 +1,66 @@ +use migration_core::{Inputs, Node, literal, literal_of, migration}; +use serde::{Deserialize, Serialize}; + +const MAX_DISTANCE: usize = 1; + +/// Frozen mirror of the `TaggedValue` variants this migration reads and writes. +#[derive(Serialize, Deserialize)] +enum Tagged { + None, + Number(f64), + Length(TypedLength), +} + +#[derive(Serialize, Deserialize)] +struct TypedLength { + value: f64, + unit: LengthUnit, +} + +#[derive(Serialize, Deserialize)] +enum LengthUnit { + Pixel, +} + +/// The max distance is a length, which keeps the number of pixels it held. A wired number still feeds it, read as pixels. +#[migration("core_types::vector::JitterPointsNode"@1 => @2)] +pub fn jitter_points_v1(node: &Node, inputs: &mut Inputs) { + let Some(slot) = node.inputs().get(MAX_DISTANCE) else { return }; + let Some(Tagged::Number(pixels)) = literal_of::(&slot.input) else { return }; + + inputs.set( + MAX_DISTANCE as u32, + literal(Tagged::Length(TypedLength { + value: pixels, + unit: LengthUnit::Pixel, + })), + ); +} + +#[cfg(test)] +mod tests { + use super::*; + use migration_core::decl; + use migration_runner::testing::{Fixture, SourceContract, check_commutation, check_undo}; + + fn contract() -> SourceContract { + SourceContract::new(4, |random, slot| if slot == 0 { literal(Tagged::None) } else { literal(Tagged::Number(random.below(100) as f64)) }) + } + + #[test] + fn rewrites_history() { + check_commutation(&JitterPointsV1, &contract()); + check_undo(&JitterPointsV1, &contract()); + } + + #[test] + fn a_number_of_pixels_becomes_a_length_in_pixels() { + let mut fixture = Fixture::new(&JitterPointsV1); + let number = |number: f64| literal(Tagged::Number(number)); + let jitter_points = fixture.add(decl!("core_types::vector::JitterPointsNode"@1), vec![literal(Tagged::None), number(5.), number(0.), number(1.)]); + let migrated = fixture.migrate(&JitterPointsV1).unwrap(); + + let max_distance = literal(Tagged::Length(TypedLength { value: 5., unit: LengthUnit::Pixel })); + assert_eq!(fixture.inputs(&migrated, jitter_points), [literal(Tagged::None), max_distance, number(0.), number(1.)]); + } +} diff --git a/document/migrations/nodes/src/core_types/mod.rs b/document/migrations/nodes/src/core_types/mod.rs new file mode 100644 index 0000000000..3784a1e1e8 --- /dev/null +++ b/document/migrations/nodes/src/core_types/mod.rs @@ -0,0 +1,12 @@ +//! Migrations of the nodes in `core_types`. + +mod bevel_v1; +mod decimate_v1; +mod jitter_points_v1; +mod offset_path_v1; +mod offset_points_v1; +mod round_corners_v1; +mod sample_polyline_v1; +mod scatter_points_v2; +mod simplify_v1; +mod weld_v1; diff --git a/document/migrations/nodes/src/core_types/offset_path_v1.rs b/document/migrations/nodes/src/core_types/offset_path_v1.rs new file mode 100644 index 0000000000..20310090f8 --- /dev/null +++ b/document/migrations/nodes/src/core_types/offset_path_v1.rs @@ -0,0 +1,66 @@ +use migration_core::{Inputs, Node, literal, literal_of, migration}; +use serde::{Deserialize, Serialize}; + +const DISTANCE: usize = 1; + +/// Frozen mirror of the `TaggedValue` variants this migration reads and writes. +#[derive(Serialize, Deserialize)] +enum Tagged { + None, + Number(f64), + Length(TypedLength), +} + +#[derive(Serialize, Deserialize)] +struct TypedLength { + value: f64, + unit: LengthUnit, +} + +#[derive(Serialize, Deserialize)] +enum LengthUnit { + Pixel, +} + +/// The distance is a length, which keeps the number of pixels it held. A wired number still feeds it, read as pixels. +#[migration("core_types::vector::OffsetPathNode"@1 => @2)] +pub fn offset_path_v1(node: &Node, inputs: &mut Inputs) { + let Some(slot) = node.inputs().get(DISTANCE) else { return }; + let Some(Tagged::Number(pixels)) = literal_of::(&slot.input) else { return }; + + inputs.set( + DISTANCE as u32, + literal(Tagged::Length(TypedLength { + value: pixels, + unit: LengthUnit::Pixel, + })), + ); +} + +#[cfg(test)] +mod tests { + use super::*; + use migration_core::decl; + use migration_runner::testing::{Fixture, SourceContract, check_commutation, check_undo}; + + fn contract() -> SourceContract { + SourceContract::new(4, |random, slot| if slot == 0 { literal(Tagged::None) } else { literal(Tagged::Number(random.below(100) as f64)) }) + } + + #[test] + fn rewrites_history() { + check_commutation(&OffsetPathV1, &contract()); + check_undo(&OffsetPathV1, &contract()); + } + + #[test] + fn a_number_of_pixels_becomes_a_length_in_pixels() { + let mut fixture = Fixture::new(&OffsetPathV1); + let number = |number: f64| literal(Tagged::Number(number)); + let offset_path = fixture.add(decl!("core_types::vector::OffsetPathNode"@1), vec![literal(Tagged::None), number(10.), number(0.), number(4.)]); + let migrated = fixture.migrate(&OffsetPathV1).unwrap(); + + let distance = literal(Tagged::Length(TypedLength { value: 10., unit: LengthUnit::Pixel })); + assert_eq!(fixture.inputs(&migrated, offset_path), [literal(Tagged::None), distance, number(0.), number(4.)]); + } +} diff --git a/document/migrations/nodes/src/core_types/offset_points_v1.rs b/document/migrations/nodes/src/core_types/offset_points_v1.rs new file mode 100644 index 0000000000..54d06c85c2 --- /dev/null +++ b/document/migrations/nodes/src/core_types/offset_points_v1.rs @@ -0,0 +1,66 @@ +use migration_core::{Inputs, Node, literal, literal_of, migration}; +use serde::{Deserialize, Serialize}; + +const DISTANCE: usize = 1; + +/// Frozen mirror of the `TaggedValue` variants this migration reads and writes. +#[derive(Serialize, Deserialize)] +enum Tagged { + None, + Number(f64), + Length(TypedLength), +} + +#[derive(Serialize, Deserialize)] +struct TypedLength { + value: f64, + unit: LengthUnit, +} + +#[derive(Serialize, Deserialize)] +enum LengthUnit { + Pixel, +} + +/// The distance is a length, which keeps the number of pixels it held. A wired number still feeds it, read as pixels. +#[migration("core_types::vector::OffsetPointsNode"@1 => @2)] +pub fn offset_points_v1(node: &Node, inputs: &mut Inputs) { + let Some(slot) = node.inputs().get(DISTANCE) else { return }; + let Some(Tagged::Number(pixels)) = literal_of::(&slot.input) else { return }; + + inputs.set( + DISTANCE as u32, + literal(Tagged::Length(TypedLength { + value: pixels, + unit: LengthUnit::Pixel, + })), + ); +} + +#[cfg(test)] +mod tests { + use super::*; + use migration_core::decl; + use migration_runner::testing::{Fixture, SourceContract, check_commutation, check_undo}; + + fn contract() -> SourceContract { + SourceContract::new(2, |random, slot| if slot == 0 { literal(Tagged::None) } else { literal(Tagged::Number(random.below(100) as f64)) }) + } + + #[test] + fn rewrites_history() { + check_commutation(&OffsetPointsV1, &contract()); + check_undo(&OffsetPointsV1, &contract()); + } + + #[test] + fn a_number_of_pixels_becomes_a_length_in_pixels() { + let mut fixture = Fixture::new(&OffsetPointsV1); + let number = |number: f64| literal(Tagged::Number(number)); + let offset_points = fixture.add(decl!("core_types::vector::OffsetPointsNode"@1), vec![literal(Tagged::None), number(10.)]); + let migrated = fixture.migrate(&OffsetPointsV1).unwrap(); + + let distance = literal(Tagged::Length(TypedLength { value: 10., unit: LengthUnit::Pixel })); + assert_eq!(fixture.inputs(&migrated, offset_points), [literal(Tagged::None), distance]); + } +} diff --git a/document/migrations/nodes/src/core_types/round_corners_v1.rs b/document/migrations/nodes/src/core_types/round_corners_v1.rs new file mode 100644 index 0000000000..64a3326f3d --- /dev/null +++ b/document/migrations/nodes/src/core_types/round_corners_v1.rs @@ -0,0 +1,96 @@ +use migration_core::{Inputs, Node, NodeInput, literal, literal_of, migration}; +use serde::{Deserialize, Serialize}; + +const RADIUS: usize = 1; +const MIN_ANGLE_THRESHOLD: usize = 4; + +/// Frozen mirror of the `TaggedValue` variants this migration reads and writes. +#[derive(Serialize, Deserialize)] +enum Tagged { + None, + Number(f64), + Length(TypedLength), + Angle(TypedAngle), +} + +#[derive(Serialize, Deserialize)] +struct TypedLength { + value: f64, + unit: LengthUnit, +} + +#[derive(Serialize, Deserialize)] +enum LengthUnit { + Pixel, +} + +#[derive(Serialize, Deserialize)] +struct TypedAngle { + value: f64, + unit: AngleUnit, +} + +#[derive(Serialize, Deserialize)] +enum AngleUnit { + Degree, +} + +/// The radius is a length and the minimum angle threshold is an angle, which keep the numbers of pixels and degrees they held. Wired numbers still feed them, read as pixels and degrees. +#[migration("core_types::vector::RoundCornersNode"@1 => @2)] +pub fn round_corners_v1(node: &Node, inputs: &mut Inputs) { + let number_at = |slot: usize| match node.inputs().get(slot).and_then(|slot| literal_of::(&slot.input)) { + Some(Tagged::Number(number)) => Some(number), + _ => None, + }; + + if let Some(pixels) = number_at(RADIUS) { + inputs.set(RADIUS as u32, length(pixels)); + } + if let Some(degrees) = number_at(MIN_ANGLE_THRESHOLD) { + inputs.set(MIN_ANGLE_THRESHOLD as u32, angle(degrees)); + } +} + +fn length(pixels: f64) -> NodeInput { + literal(Tagged::Length(TypedLength { + value: pixels, + unit: LengthUnit::Pixel, + })) +} + +fn angle(degrees: f64) -> NodeInput { + literal(Tagged::Angle(TypedAngle { + value: degrees, + unit: AngleUnit::Degree, + })) +} + +#[cfg(test)] +mod tests { + use super::*; + use migration_core::decl; + use migration_runner::testing::{Fixture, SourceContract, check_commutation, check_undo}; + + fn contract() -> SourceContract { + SourceContract::new(5, |random, slot| if slot == 0 { literal(Tagged::None) } else { literal(Tagged::Number(random.below(100) as f64)) }) + } + + #[test] + fn rewrites_history() { + check_commutation(&RoundCornersV1, &contract()); + check_undo(&RoundCornersV1, &contract()); + } + + #[test] + fn numbers_of_pixels_and_degrees_become_a_length_and_an_angle() { + let mut fixture = Fixture::new(&RoundCornersV1); + let number = |number: f64| literal(Tagged::Number(number)); + let round_corners = fixture.add( + decl!("core_types::vector::RoundCornersNode"@1), + vec![literal(Tagged::None), number(10.), number(0.5), number(100.), number(5.)], + ); + let migrated = fixture.migrate(&RoundCornersV1).unwrap(); + + assert_eq!(fixture.inputs(&migrated, round_corners), [literal(Tagged::None), length(10.), number(0.5), number(100.), angle(5.)]); + } +} diff --git a/document/migrations/nodes/src/core_types/sample_polyline_v1.rs b/document/migrations/nodes/src/core_types/sample_polyline_v1.rs new file mode 100644 index 0000000000..2933160e36 --- /dev/null +++ b/document/migrations/nodes/src/core_types/sample_polyline_v1.rs @@ -0,0 +1,80 @@ +use migration_core::{Inputs, Node, NodeInput, literal, literal_of, migration}; +use serde::{Deserialize, Serialize}; + +const SEPARATION: usize = 2; +const START_OFFSET: usize = 4; +const STOP_OFFSET: usize = 5; + +/// Frozen mirror of the `TaggedValue` variants this migration reads and writes. +#[derive(Serialize, Deserialize)] +enum Tagged { + None, + Number(f64), + Length(TypedLength), +} + +#[derive(Serialize, Deserialize)] +struct TypedLength { + value: f64, + unit: LengthUnit, +} + +#[derive(Serialize, Deserialize)] +enum LengthUnit { + Pixel, +} + +/// The separation and start and stop offsets are lengths, which keep the numbers of pixels they held. Wired numbers still feed them, read as pixels. +#[migration("core_types::vector::SamplePolylineNode"@1 => @2)] +pub fn sample_polyline_v1(node: &Node, inputs: &mut Inputs) { + let number_at = |slot: usize| match node.inputs().get(slot).and_then(|slot| literal_of::(&slot.input)) { + Some(Tagged::Number(number)) => Some(number), + _ => None, + }; + + for slot in [SEPARATION, START_OFFSET, STOP_OFFSET] { + if let Some(pixels) = number_at(slot) { + inputs.set(slot as u32, length(pixels)); + } + } +} + +fn length(pixels: f64) -> NodeInput { + literal(Tagged::Length(TypedLength { + value: pixels, + unit: LengthUnit::Pixel, + })) +} + +#[cfg(test)] +mod tests { + use super::*; + use migration_core::decl; + use migration_runner::testing::{Fixture, SourceContract, check_commutation, check_undo}; + + fn contract() -> SourceContract { + SourceContract::new(7, |random, slot| if slot == 0 { literal(Tagged::None) } else { literal(Tagged::Number(random.below(100) as f64)) }) + } + + #[test] + fn rewrites_history() { + check_commutation(&SamplePolylineV1, &contract()); + check_undo(&SamplePolylineV1, &contract()); + } + + #[test] + fn numbers_of_pixels_become_lengths_in_pixels() { + let mut fixture = Fixture::new(&SamplePolylineV1); + let number = |number: f64| literal(Tagged::Number(number)); + let sample_polyline = fixture.add( + decl!("core_types::vector::SamplePolylineNode"@1), + vec![literal(Tagged::None), number(0.), number(100.), number(100.), number(10.), number(20.), number(0.)], + ); + let migrated = fixture.migrate(&SamplePolylineV1).unwrap(); + + assert_eq!( + fixture.inputs(&migrated, sample_polyline), + [literal(Tagged::None), number(0.), length(100.), number(100.), length(10.), length(20.), number(0.)] + ); + } +} diff --git a/document/migrations/nodes/src/core_types/scatter_points_v2.rs b/document/migrations/nodes/src/core_types/scatter_points_v2.rs new file mode 100644 index 0000000000..77129e4558 --- /dev/null +++ b/document/migrations/nodes/src/core_types/scatter_points_v2.rs @@ -0,0 +1,66 @@ +use migration_core::{Inputs, Node, literal, literal_of, migration}; +use serde::{Deserialize, Serialize}; + +const SEPARATION: usize = 1; + +/// Frozen mirror of the `TaggedValue` variants this migration reads and writes. +#[derive(Serialize, Deserialize)] +enum Tagged { + None, + Number(f64), + Length(TypedLength), +} + +#[derive(Serialize, Deserialize)] +struct TypedLength { + value: f64, + unit: LengthUnit, +} + +#[derive(Serialize, Deserialize)] +enum LengthUnit { + Pixel, +} + +/// The separation is a length, which keeps the number of pixels it held. A wired number still feeds it, read as pixels. +#[migration("core_types::vector::ScatterPointsNode"@2 => @3)] +pub fn scatter_points_v2(node: &Node, inputs: &mut Inputs) { + let Some(slot) = node.inputs().get(SEPARATION) else { return }; + let Some(Tagged::Number(pixels)) = literal_of::(&slot.input) else { return }; + + inputs.set( + SEPARATION as u32, + literal(Tagged::Length(TypedLength { + value: pixels, + unit: LengthUnit::Pixel, + })), + ); +} + +#[cfg(test)] +mod tests { + use super::*; + use migration_core::decl; + use migration_runner::testing::{Fixture, SourceContract, check_commutation, check_undo}; + + fn contract() -> SourceContract { + SourceContract::new(3, |random, slot| if slot == 0 { literal(Tagged::None) } else { literal(Tagged::Number(random.below(100) as f64)) }) + } + + #[test] + fn rewrites_history() { + check_commutation(&ScatterPointsV2, &contract()); + check_undo(&ScatterPointsV2, &contract()); + } + + #[test] + fn a_number_of_pixels_becomes_a_length_in_pixels() { + let mut fixture = Fixture::new(&ScatterPointsV2); + let number = |number: f64| literal(Tagged::Number(number)); + let scatter_points = fixture.add(decl!("core_types::vector::ScatterPointsNode"@2), vec![literal(Tagged::None), number(10.), number(0.)]); + let migrated = fixture.migrate(&ScatterPointsV2).unwrap(); + + let separation = literal(Tagged::Length(TypedLength { value: 10., unit: LengthUnit::Pixel })); + assert_eq!(fixture.inputs(&migrated, scatter_points), [literal(Tagged::None), separation, number(0.)]); + } +} diff --git a/document/migrations/nodes/src/core_types/simplify_v1.rs b/document/migrations/nodes/src/core_types/simplify_v1.rs new file mode 100644 index 0000000000..6c77a003bd --- /dev/null +++ b/document/migrations/nodes/src/core_types/simplify_v1.rs @@ -0,0 +1,66 @@ +use migration_core::{Inputs, Node, literal, literal_of, migration}; +use serde::{Deserialize, Serialize}; + +const TOLERANCE: usize = 1; + +/// Frozen mirror of the `TaggedValue` variants this migration reads and writes. +#[derive(Serialize, Deserialize)] +enum Tagged { + None, + Number(f64), + Length(TypedLength), +} + +#[derive(Serialize, Deserialize)] +struct TypedLength { + value: f64, + unit: LengthUnit, +} + +#[derive(Serialize, Deserialize)] +enum LengthUnit { + Pixel, +} + +/// The tolerance is a length, which keeps the number of pixels it held. A wired number still feeds it, read as pixels. +#[migration("core_types::vector::SimplifyNode"@1 => @2)] +pub fn simplify_v1(node: &Node, inputs: &mut Inputs) { + let Some(slot) = node.inputs().get(TOLERANCE) else { return }; + let Some(Tagged::Number(pixels)) = literal_of::(&slot.input) else { return }; + + inputs.set( + TOLERANCE as u32, + literal(Tagged::Length(TypedLength { + value: pixels, + unit: LengthUnit::Pixel, + })), + ); +} + +#[cfg(test)] +mod tests { + use super::*; + use migration_core::decl; + use migration_runner::testing::{Fixture, SourceContract, check_commutation, check_undo}; + + fn contract() -> SourceContract { + SourceContract::new(2, |random, slot| if slot == 0 { literal(Tagged::None) } else { literal(Tagged::Number(random.below(100) as f64)) }) + } + + #[test] + fn rewrites_history() { + check_commutation(&SimplifyV1, &contract()); + check_undo(&SimplifyV1, &contract()); + } + + #[test] + fn a_number_of_pixels_becomes_a_length_in_pixels() { + let mut fixture = Fixture::new(&SimplifyV1); + let number = |number: f64| literal(Tagged::Number(number)); + let simplify = fixture.add(decl!("core_types::vector::SimplifyNode"@1), vec![literal(Tagged::None), number(5.)]); + let migrated = fixture.migrate(&SimplifyV1).unwrap(); + + let tolerance = literal(Tagged::Length(TypedLength { value: 5., unit: LengthUnit::Pixel })); + assert_eq!(fixture.inputs(&migrated, simplify), [literal(Tagged::None), tolerance]); + } +} diff --git a/document/migrations/nodes/src/core_types/weld_v1.rs b/document/migrations/nodes/src/core_types/weld_v1.rs new file mode 100644 index 0000000000..f6c986ee81 --- /dev/null +++ b/document/migrations/nodes/src/core_types/weld_v1.rs @@ -0,0 +1,66 @@ +use migration_core::{Inputs, Node, literal, literal_of, migration}; +use serde::{Deserialize, Serialize}; + +const DISTANCE: usize = 1; + +/// Frozen mirror of the `TaggedValue` variants this migration reads and writes. +#[derive(Serialize, Deserialize)] +enum Tagged { + None, + Number(f64), + Length(TypedLength), +} + +#[derive(Serialize, Deserialize)] +struct TypedLength { + value: f64, + unit: LengthUnit, +} + +#[derive(Serialize, Deserialize)] +enum LengthUnit { + Pixel, +} + +/// The distance is a length, which keeps the number of pixels it held. A wired number still feeds it, read as pixels. +#[migration("core_types::vector::WeldNode"@1 => @2)] +pub fn weld_v1(node: &Node, inputs: &mut Inputs) { + let Some(slot) = node.inputs().get(DISTANCE) else { return }; + let Some(Tagged::Number(pixels)) = literal_of::(&slot.input) else { return }; + + inputs.set( + DISTANCE as u32, + literal(Tagged::Length(TypedLength { + value: pixels, + unit: LengthUnit::Pixel, + })), + ); +} + +#[cfg(test)] +mod tests { + use super::*; + use migration_core::decl; + use migration_runner::testing::{Fixture, SourceContract, check_commutation, check_undo}; + + fn contract() -> SourceContract { + SourceContract::new(3, |random, slot| if slot == 0 { literal(Tagged::None) } else { literal(Tagged::Number(random.below(100) as f64)) }) + } + + #[test] + fn rewrites_history() { + check_commutation(&WeldV1, &contract()); + check_undo(&WeldV1, &contract()); + } + + #[test] + fn a_number_of_pixels_becomes_a_length_in_pixels() { + let mut fixture = Fixture::new(&WeldV1); + let number = |number: f64| literal(Tagged::Number(number)); + let weld = fixture.add(decl!("core_types::vector::WeldNode"@1), vec![literal(Tagged::None), number(0.1), number(0.)]); + let migrated = fixture.migrate(&WeldV1).unwrap(); + + let distance = literal(Tagged::Length(TypedLength { value: 0.1, unit: LengthUnit::Pixel })); + assert_eq!(fixture.inputs(&migrated, weld), [literal(Tagged::None), distance, number(0.)]); + } +} diff --git a/document/migrations/nodes/src/graphene_core/mod.rs b/document/migrations/nodes/src/graphene_core/mod.rs index fe28a1a318..6ce62393b7 100644 --- a/document/migrations/nodes/src/graphene_core/mod.rs +++ b/document/migrations/nodes/src/graphene_core/mod.rs @@ -1,5 +1,7 @@ //! Migrations of the nodes in `graphene_core`. +mod pack_strips_v1; mod passthrough_from_upload_texture_node; mod renames; +mod stroke_v1; mod vary_colors_from_assign_colors; diff --git a/document/migrations/nodes/src/graphene_core/pack_strips_v1.rs b/document/migrations/nodes/src/graphene_core/pack_strips_v1.rs new file mode 100644 index 0000000000..3ba533f241 --- /dev/null +++ b/document/migrations/nodes/src/graphene_core/pack_strips_v1.rs @@ -0,0 +1,73 @@ +use migration_core::{Inputs, Node, NodeInput, literal, literal_of, migration}; +use serde::{Deserialize, Serialize}; + +const SEPARATION: usize = 1; +const STRIP_MAX_LENGTH: usize = 2; + +/// Frozen mirror of the `TaggedValue` variants this migration reads and writes. +#[derive(Serialize, Deserialize)] +enum Tagged { + None, + Number(f64), + Length(TypedLength), +} + +#[derive(Serialize, Deserialize)] +struct TypedLength { + value: f64, + unit: LengthUnit, +} + +#[derive(Serialize, Deserialize)] +enum LengthUnit { + Pixel, +} + +/// The separation and strip max length are lengths, which keep the numbers of pixels they held. Wired numbers still feed them, read as pixels. +#[migration("graphene_core::vector::PackStripsNode"@1 => @2)] +pub fn pack_strips_v1(node: &Node, inputs: &mut Inputs) { + let number_at = |slot: usize| match node.inputs().get(slot).and_then(|slot| literal_of::(&slot.input)) { + Some(Tagged::Number(number)) => Some(number), + _ => None, + }; + + for slot in [SEPARATION, STRIP_MAX_LENGTH] { + if let Some(pixels) = number_at(slot) { + inputs.set(slot as u32, length(pixels)); + } + } +} + +fn length(pixels: f64) -> NodeInput { + literal(Tagged::Length(TypedLength { + value: pixels, + unit: LengthUnit::Pixel, + })) +} + +#[cfg(test)] +mod tests { + use super::*; + use migration_core::decl; + use migration_runner::testing::{Fixture, SourceContract, check_commutation, check_undo}; + + fn contract() -> SourceContract { + SourceContract::new(4, |random, slot| if slot == 0 { literal(Tagged::None) } else { literal(Tagged::Number(random.below(100) as f64)) }) + } + + #[test] + fn rewrites_history() { + check_commutation(&PackStripsV1, &contract()); + check_undo(&PackStripsV1, &contract()); + } + + #[test] + fn numbers_of_pixels_become_lengths_in_pixels() { + let mut fixture = Fixture::new(&PackStripsV1); + let number = |number: f64| literal(Tagged::Number(number)); + let pack_strips = fixture.add(decl!("graphene_core::vector::PackStripsNode"@1), vec![literal(Tagged::None), number(10.), number(1000.), number(0.)]); + let migrated = fixture.migrate(&PackStripsV1).unwrap(); + + assert_eq!(fixture.inputs(&migrated, pack_strips), [literal(Tagged::None), length(10.), length(1000.), number(0.)]); + } +} diff --git a/document/migrations/nodes/src/graphene_core/stroke_v1.rs b/document/migrations/nodes/src/graphene_core/stroke_v1.rs new file mode 100644 index 0000000000..22ff7b5ae0 --- /dev/null +++ b/document/migrations/nodes/src/graphene_core/stroke_v1.rs @@ -0,0 +1,79 @@ +use migration_core::{Inputs, Node, NodeInput, literal, literal_of, migration}; +use serde::{Deserialize, Serialize}; + +const WEIGHT: usize = 2; +const DASH_OFFSET: usize = 8; + +/// Frozen mirror of the `TaggedValue` variants this migration reads and writes. +#[derive(Serialize, Deserialize)] +enum Tagged { + None, + Number(f64), + Length(TypedLength), +} + +#[derive(Serialize, Deserialize)] +struct TypedLength { + value: f64, + unit: LengthUnit, +} + +#[derive(Serialize, Deserialize)] +enum LengthUnit { + Pixel, +} + +/// The weight and dash offset are lengths, which keep the numbers of pixels they held. Wired numbers still feed them, read as pixels. +#[migration("graphene_core::vector::StrokeNode"@1 => @2)] +pub fn stroke_v1(node: &Node, inputs: &mut Inputs) { + let number_at = |slot: usize| match node.inputs().get(slot).and_then(|slot| literal_of::(&slot.input)) { + Some(Tagged::Number(number)) => Some(number), + _ => None, + }; + + for slot in [WEIGHT, DASH_OFFSET] { + if let Some(pixels) = number_at(slot) { + inputs.set(slot as u32, length(pixels)); + } + } +} + +fn length(pixels: f64) -> NodeInput { + literal(Tagged::Length(TypedLength { + value: pixels, + unit: LengthUnit::Pixel, + })) +} + +#[cfg(test)] +mod tests { + use super::*; + use migration_core::decl; + use migration_runner::testing::{Fixture, SourceContract, check_commutation, check_undo}; + + fn contract() -> SourceContract { + SourceContract::new(9, |random, slot| if slot == 0 { literal(Tagged::None) } else { literal(Tagged::Number(random.below(100) as f64)) }) + } + + #[test] + fn rewrites_history() { + check_commutation(&StrokeV1, &contract()); + check_undo(&StrokeV1, &contract()); + } + + #[test] + fn numbers_of_pixels_become_lengths_in_pixels() { + let mut fixture = Fixture::new(&StrokeV1); + let number = |number: f64| literal(Tagged::Number(number)); + let stroke = fixture.add( + decl!("graphene_core::vector::StrokeNode"@1), + vec![literal(Tagged::None), number(0.), number(2.), number(0.), number(0.), number(0.), number(4.), number(0.), number(5.)], + ); + let migrated = fixture.migrate(&StrokeV1).unwrap(); + + assert_eq!( + fixture.inputs(&migrated, stroke), + [literal(Tagged::None), number(0.), length(2.), number(0.), number(0.), number(0.), number(4.), number(0.), length(5.)] + ); + } +} diff --git a/document/migrations/nodes/src/lib.rs b/document/migrations/nodes/src/lib.rs index 1a2e206c17..1ed395e45f 100644 --- a/document/migrations/nodes/src/lib.rs +++ b/document/migrations/nodes/src/lib.rs @@ -2,6 +2,7 @@ //! `cargo run migration new ` after bumping a node's `version(N)`. mod blending_nodes; +mod core_types; mod graphene_core; mod graphene_std; mod math_nodes; diff --git a/editor/src/messages/portfolio/document/graph_operation/utility_types.rs b/editor/src/messages/portfolio/document/graph_operation/utility_types.rs index 3609143115..a9ed9b6d52 100644 --- a/editor/src/messages/portfolio/document/graph_operation/utility_types.rs +++ b/editor/src/messages/portfolio/document/graph_operation/utility_types.rs @@ -15,6 +15,7 @@ use graph_craft::document::{NodeId, NodeInput}; use graph_craft::{ProtoNodeIdentifier, list}; use graphene_std::raster::BlendMode; use graphene_std::text::{Font, TypesettingConfig}; +use graphene_std::units::TypedLength; use graphene_std::vector::style::{GradientForm, GradientHueDirection, GradientInterpolation, GradientSettings, GradientSpace, GradientSpread, PaintOrder, Stroke}; use graphene_std::vector::{Gradient, GradientRamp, Vector, VectorModification, VectorModificationType}; use graphene_std::{Artboard, Color, Graphic}; @@ -871,7 +872,7 @@ impl<'a> ModifyInputsContext<'a> { let input_connector = InputConnector::node(stroke_node_id, graphene_std::vector::stroke::PaintInput); self.set_input_with_refresh(input_connector, NodeInput::value(color.map_or_else(TaggedValue::no_paint, TaggedValue::Color), false), true); let input_connector = InputConnector::node(stroke_node_id, graphene_std::vector::stroke::WeightInput); - self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::Number(stroke.weight), false), true); + self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::Length(TypedLength::pixels(stroke.weight)), false), true); let input_connector = InputConnector::node(stroke_node_id, graphene_std::vector::stroke::AlignInput); self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::StrokeAlign(stroke.align), false), false); let input_connector = InputConnector::node(stroke_node_id, graphene_std::vector::stroke::CapInput); @@ -883,7 +884,7 @@ impl<'a> ModifyInputsContext<'a> { let input_connector = InputConnector::node(stroke_node_id, graphene_std::vector::stroke::DashPatternInput); self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::DashPattern(stroke.dash_lengths), false), true); let input_connector = InputConnector::node(stroke_node_id, graphene_std::vector::stroke::DashOffsetInput); - self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::Number(stroke.dash_offset), false), true); + self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::Length(TypedLength::pixels(stroke.dash_offset)), false), true); } /// Update the transform value of the upstream Transform node based a change to its existing value and the given parent transform. diff --git a/editor/src/messages/portfolio/document/node_graph/node_inspector.rs b/editor/src/messages/portfolio/document/node_graph/node_inspector.rs index 6ac234aab3..bac66c0170 100644 --- a/editor/src/messages/portfolio/document/node_graph/node_inspector.rs +++ b/editor/src/messages/portfolio/document/node_graph/node_inspector.rs @@ -2968,10 +2968,10 @@ pub(crate) fn sample_polyline_inspector(node_id: NodeId, context: &mut NodeInspe let spacing = enum_choice::() .for_socket(ParameterWidgetsInfo::new(node_id, SpacingInput, true, context)) .inspector_row(); - let separation = number_widget(ParameterWidgetsInfo::new(node_id, SeparationInput, true, context), NumberInput::default().min(0.).unit(" px")); + let separation = length_widget(ParameterWidgetsInfo::new(node_id, SeparationInput, true, context), NumberInput::default().min(0.)); let quantity = number_widget(ParameterWidgetsInfo::new(node_id, QuantityInput, true, context), NumberInput::default().min(2.).int()); - let start_offset = number_widget(ParameterWidgetsInfo::new(node_id, StartOffsetInput, true, context), NumberInput::default().min(0.).unit(" px")); - let stop_offset = number_widget(ParameterWidgetsInfo::new(node_id, StopOffsetInput, true, context), NumberInput::default().min(0.).unit(" px")); + let start_offset = length_widget(ParameterWidgetsInfo::new(node_id, StartOffsetInput, true, context), NumberInput::default().min(0.)); + let stop_offset = length_widget(ParameterWidgetsInfo::new(node_id, StopOffsetInput, true, context), NumberInput::default().min(0.)); let adaptive_spacing = bool_widget(ParameterWidgetsInfo::new(node_id, AdaptiveSpacingInput, true, context), CheckboxInput::default().disabled(is_quantity)); vec![ @@ -3799,7 +3799,7 @@ pub fn stroke_inspector(node_id: NodeId, context: &mut NodeInspectorContext) -> ParameterWidgetsInfo::new(node_id, PaintInput, true, context), crate::messages::layout::utility_types::widgets::button_widgets::ColorInput::default(), ); - let weight = number_widget(ParameterWidgetsInfo::new(node_id, WeightInput, true, context), NumberInput::default().unit(" px").min(0.)); + let weight = length_widget(ParameterWidgetsInfo::new(node_id, WeightInput, true, context), NumberInput::default().min(0.)); let align = enum_choice::().for_socket(ParameterWidgetsInfo::new(node_id, AlignInput, true, context)).inspector_row(); let cap = enum_choice::().for_socket(ParameterWidgetsInfo::new(node_id, CapInput, true, context)).inspector_row(); let join = enum_choice::().for_socket(ParameterWidgetsInfo::new(node_id, JoinInput, true, context)).inspector_row(); @@ -3808,10 +3808,10 @@ pub fn stroke_inspector(node_id: NodeId, context: &mut NodeInspectorContext) -> ParameterWidgetsInfo::new(node_id, MiterLimitInput, true, context), NumberInput::default().min(0.).disabled(miter_limit_disabled), ); - let disabled_number_input = NumberInput::default().unit(" px").disabled(has_dash_lengths); + let disabled_number_input = NumberInput::default().disabled(has_dash_lengths); let dash_lengths = dash_pattern_widget(ParameterWidgetsInfo::new(node_id, DashPatternInput, true, context), TextInput::default().centered(true)); let number_input = disabled_number_input; - let dash_offset = number_widget(ParameterWidgetsInfo::new(node_id, DashOffsetInput, true, context), number_input); + let dash_offset = length_widget(ParameterWidgetsInfo::new(node_id, DashOffsetInput, true, context), number_input); vec![ color, @@ -3828,8 +3828,7 @@ pub fn stroke_inspector(node_id: NodeId, context: &mut NodeInspectorContext) -> pub fn offset_path_inspector(node_id: NodeId, context: &mut NodeInspectorContext) -> Vec { use graphene_std::vector::offset_path::*; - let number_input = NumberInput::default().unit(" px"); - let distance = number_widget(ParameterWidgetsInfo::new(node_id, DistanceInput, true, context), number_input); + let distance = length_widget(ParameterWidgetsInfo::new(node_id, DistanceInput, true, context), NumberInput::default()); let join = enum_choice::().for_socket(ParameterWidgetsInfo::new(node_id, JoinInput, true, context)).inspector_row(); diff --git a/editor/src/messages/tool/common_functionality/graph_modification_utils.rs b/editor/src/messages/tool/common_functionality/graph_modification_utils.rs index e7cc11caca..e8438cafd3 100644 --- a/editor/src/messages/tool/common_functionality/graph_modification_utils.rs +++ b/editor/src/messages/tool/common_functionality/graph_modification_utils.rs @@ -683,11 +683,8 @@ pub fn get_text<'a>( } pub fn get_stroke_width(layer: LayerNodeIdentifier, network_interface: &NodeNetworkInterface) -> Option { - if let TaggedValue::Number(width) = NodeGraphLayer::new(layer, network_interface).parameter_value(graphene_std::vector::stroke::WeightInput)? { - Some(*width) - } else { - None - } + let weight = NodeGraphLayer::new(layer, network_interface).parameter_value(graphene_std::vector::stroke::WeightInput)?; + weight.as_length_pixels(network_interface.pixels_per_inch()) } /// Subset of Stroke node inputs read for the control bar's stroke options popover. @@ -729,10 +726,10 @@ pub fn get_stroke_options(layer: LayerNodeIdentifier, network_interface: &NodeNe Some(TaggedValue::DashPattern(lengths)) => lengths.clone(), _ => Vec::new(), }; - let dash_offset = match parameters.value(stroke::DashOffsetInput) { - Some(TaggedValue::Number(value)) => *value, - _ => 0., - }; + let dash_offset = parameters + .value(stroke::DashOffsetInput) + .and_then(|value| value.as_length_pixels(network_interface.pixels_per_inch())) + .unwrap_or_default(); Some(StrokeOptionsState { align, @@ -817,10 +814,11 @@ pub fn get_stroke_paint_order(layer: LayerNodeIdentifier, network_interface: &No pub fn set_stroke_weight_for_selected_layers(weight: f64, document: &DocumentMessageHandler, responses: &mut VecDeque) { for layer in paintable_selected_layers(document) { if let Some(node_id) = get_stroke_id(layer, &document.network_interface) { + let input_connector = InputConnector::node(node_id, graphene_std::vector::stroke::WeightInput); responses.add(NodeGraphMessage::SetInputValue { node_id, input_index: graphene_std::vector::stroke::WeightInput::INDEX, - value: TaggedValue::Number(weight).into(), + value: length_in_unit_of_input(&document.network_interface, &input_connector, weight).into(), }); } else if weight > 0. { let color = Some(Color::BLACK); diff --git a/editor/src/messages/tool/common_functionality/stroke_options.rs b/editor/src/messages/tool/common_functionality/stroke_options.rs index de1948ffc0..65db3c750a 100644 --- a/editor/src/messages/tool/common_functionality/stroke_options.rs +++ b/editor/src/messages/tool/common_functionality/stroke_options.rs @@ -5,6 +5,7 @@ use crate::messages::tool::common_functionality::graph_modification_utils; use graph_craft::document::value::TaggedValue; use graphene_std::choice_type::ChoiceTypeStatic; use graphene_std::core_types::misc::parse_f64; +use graphene_std::units::TypedLength; use graphene_std::vector::style::{PaintOrder, StrokeAlign, StrokeCap, StrokeJoin}; /// All non-color stroke-related options surfaced in the control bar popover. @@ -221,5 +222,5 @@ pub fn apply_dash_lengths(drawing: &mut DrawingToolState, lengths: Vec, doc pub fn apply_dash_offset(drawing: &mut DrawingToolState, offset: f64, document: &DocumentMessageHandler, responses: &mut VecDeque) { drawing.dash_offset = Some(offset); - graph_modification_utils::set_parameter_for_selected_layers(document, graphene_std::vector::stroke::DashOffsetInput, TaggedValue::Number(offset), responses); + graph_modification_utils::set_parameter_for_selected_layers(document, graphene_std::vector::stroke::DashOffsetInput, TaggedValue::Length(TypedLength::pixels(offset)), responses); } diff --git a/node-graph/interpreted-executor/src/dynamic_executor/test.rs b/node-graph/interpreted-executor/src/dynamic_executor/test.rs index c874321300..aa00c485ce 100644 --- a/node-graph/interpreted-executor/src/dynamic_executor/test.rs +++ b/node-graph/interpreted-executor/src/dynamic_executor/test.rs @@ -104,13 +104,13 @@ fn rank_0_content_promotes_through_the_layer_coercion_path() { assert_eq!(stack.len(), 1, "The rank-0 content should contribute exactly one graphic to the stack"); } -/// Builds a network feeding the given content plus an f64 distance value into Offset Points, whose distance input is ranked `Item`. +/// Builds a network feeding the given content plus an f64 distance value into Offset Points, whose distance input is ranked `Item`. fn offset_points_network(content: TaggedValue) -> ProtoNetwork { let content_node = ProtoNode::value(ConstructionArgs::Value(content.into()), vec![NodeId(0)]); let distance_node = ProtoNode::value(ConstructionArgs::Value(TaggedValue::Number(10.).into()), vec![NodeId(1)]); let mut input_adapter_node = ProtoNode::value(ConstructionArgs::Nodes(vec![NodeId(1)]), vec![NodeId(2)]); - input_adapter_node.identifier = ProtoNodeIdentifier::new("input_adapter"); + input_adapter_node.identifier = ProtoNodeIdentifier::new("input_adapter"); let mut offset_points_node = ProtoNode::value(ConstructionArgs::Nodes(vec![NodeId(0), NodeId(2)]), vec![NodeId(3)]); offset_points_node.identifier = ProtoNodeIdentifier::new("core_types::vector::OffsetPointsNode"); diff --git a/node-graph/nodes/vector/src/vector_nodes.rs b/node-graph/nodes/vector/src/vector_nodes.rs index 126c133d2f..4af59e125b 100644 --- a/node-graph/nodes/vector/src/vector_nodes.rs +++ b/node-graph/nodes/vector/src/vector_nodes.rs @@ -8,6 +8,7 @@ use core_types::bounds::{BoundingBox, RenderBoundingBox}; use core_types::list::{ATTR_APPEARANCE, ATTR_ID, ATTR_PATHS, Item, ItemAttributeValues, List, ListDyn, NodeIdPath}; use core_types::math::spread::spread_factors; use core_types::transform::{Footprint, Transform}; +use core_types::units::{Angle, Length}; use core_types::uuid::NodeId; use core_types::{ ATTR_BLEND_MODE, ATTR_CLIPPING_MASK, ATTR_EDITOR_LAYER_PATH, ATTR_EDITOR_MERGED_LAYERS, ATTR_GRADIENT_FORM, ATTR_OPACITY, ATTR_OPACITY_FILL, ATTR_TRANSFORM, CloneVarArgs, Color, Context, Ctx, @@ -499,7 +500,7 @@ where } /// Applies a stroke style to the vector content, giving an appearance to the area within the outline of the geometry. -#[node_macro::node(category("Vector: Style"), icon("NodeStroke"), path(graphene_core::vector), inspector("stroke_inspector"))] +#[node_macro::node(category("Vector: Style"), icon("NodeStroke"), path(graphene_core::vector), inspector("stroke_inspector"), version(2))] fn stroke( _: impl Ctx, /// The content with vector paths to apply the stroke style to. @@ -509,10 +510,9 @@ fn stroke( #[default(Color::BLACK)] paint: Item, /// The stroke thickness. - #[unit(" px")] #[hard(0..)] #[default(2.)] - weight: Item, + weight: Item, /// The alignment of stroke to the path's centerline or (for closed shapes) the inside or outside of the shape. align: Item, /// The shape of the stroke at open endpoints. @@ -526,20 +526,19 @@ fn stroke( /// The stroke dash pattern. Each length forms a distance in a pattern where the first length is a dash, the second is a gap, and so on. If the list is an odd length, the pattern repeats with solid-gap roles reversed. dash_pattern: Item, /// The phase offset distance from the starting point of the dash pattern. - #[unit(" px")] - dash_offset: Item, + dash_offset: Item, ) -> Item where Item: VectorItemMut + Send, { let mut content = content; let (weight, align, cap, join, miter_limit, dash_offset) = ( - weight.into_element(), + weight.into_element().pixels(), align.into_element(), cap.into_element(), join.into_element(), miter_limit.into_element(), - dash_offset.into_element(), + dash_offset.into_element().pixels(), ); let dash_lengths = dash_pattern.into_element().clamped_lengths(); @@ -572,14 +571,13 @@ fn dash_pattern( Item::new_from_element(DashPattern::from(lengths)) } -#[node_macro::node(category("Vector: Path"), icon("NodeRoundCorners"), path(core_types::vector))] +#[node_macro::node(category("Vector: Path"), icon("NodeRoundCorners"), path(core_types::vector), version(2))] fn round_corners( _: impl Ctx, #[implementations(Graphic, Vector)] source: Item, - #[unit(" px")] #[hard(0..)] #[default(10.)] - radius: Item, + radius: Item, #[range] #[hard(0..1)] #[default(0.5)] @@ -589,14 +587,13 @@ fn round_corners( #[hard(0..100)] #[default(100.)] edge_length_limit: Item, - #[unit("°")] #[range] #[hard(0..180)] #[default(5.)] - min_angle_threshold: Item, + min_angle_threshold: Item, ) -> Item { V::map_vector_items(source, |source| { - let (radius, roundness, edge_length_limit, min_angle_threshold) = (*radius.element(), *roundness.element(), *edge_length_limit.element(), *min_angle_threshold.element()); + let (radius, roundness, edge_length_limit, min_angle_threshold) = (radius.element().pixels(), *roundness.element(), *edge_length_limit.element(), min_angle_threshold.element().radians()); let source_transform: DAffine2 = source.attribute_cloned_or_default(ATTR_TRANSFORM); if !source_transform.matrix2.determinant().is_normal() { return source; @@ -618,7 +615,7 @@ fn round_corners( document_bezpath.apply_affine(Affine::new(source_transform.to_cols_array())); // A subpath without corners to round passes through as it was - let Some(mut rounded_subpath) = round_subpath_corners(&document_bezpath, radius, roundness, edge_length_limit, min_angle_threshold.to_radians()) else { + let Some(mut rounded_subpath) = round_subpath_corners(&document_bezpath, radius, roundness, edge_length_limit, min_angle_threshold) else { rounded_subpaths.push(bezpath); continue; }; @@ -740,21 +737,20 @@ fn round_subpath_corners(subpath: &BezPath, radius: f64, roundness: f64, edge_le } /// Joins points lying within a distance of each other into one, connecting the paths they belong to. -#[node_macro::node(category("Vector: Path"), icon("NodeWeld"), path(core_types::vector))] +#[node_macro::node(category("Vector: Path"), icon("NodeWeld"), path(core_types::vector), version(2))] fn weld( _: impl Ctx, /// The vector with points to join. #[implementations(Graphic, Vector)] content: Item, /// How close points must be to join. - #[unit(" px")] #[default(0.1)] #[hard(0.0001..)] - distance: Item, + distance: Item, /// Which points may join: any within the distance, the two ends of each segment shorter than it, or only the ends of open path flows. method: Item, ) -> Item { - let (distance, method) = (*distance.element(), *method.element()); + let (distance, method) = (distance.element().pixels(), *method.element()); V::map_vector_items(content, |content| { let mut content = content; @@ -1173,7 +1169,7 @@ fn bilinear_interpolate(t: DVec2, quad: &[DVec2; 4]) -> DVec2 { tl * (1. - t.x) * (1. - t.y) + tr * t.x * (1. - t.y) + br * t.x * t.y + bl * (1. - t.x) * t.y } -#[node_macro::node(category("Transform"), icon("NodePackStrips"), path(graphene_core::vector), footprint_invariant)] +#[node_macro::node(category("Transform"), icon("NodePackStrips"), path(graphene_core::vector), footprint_invariant, version(2))] fn pack_strips( _: impl Ctx, #[implementations( @@ -1183,19 +1179,15 @@ fn pack_strips( List>, )] elements: List, - #[default(0.)] - #[unit(" px")] - separation: Item, - #[default(1000.)] - #[unit(" px")] - strip_max_length: Item, + #[default(0.)] separation: Item, + #[default(1000.)] strip_max_length: Item, strip_direction: Item, ) -> List where Graphic: From>, List: BoundingBox, { - let (separation, strip_max_length, strip_direction) = (separation.into_element(), strip_max_length.into_element(), strip_direction.into_element()); + let (separation, strip_max_length, strip_direction) = (separation.into_element().pixels(), strip_max_length.into_element().pixels(), strip_direction.into_element()); // Packs shapes using bounds with Best-Fit Decreasing Height (BFDH) algorithm: // - Sort shapes by cross-axis size (tallest first for rows, widest first for columns) @@ -1772,11 +1764,11 @@ fn mesh_weld_tolerance(polygons: &[Vec]) -> f64 { if diagonal.is_finite() && diagonal > 0. { (diagonal * 1e-6).max(1e-12) } else { 1e-6 } } -#[node_macro::node(category("Vector: Path"), icon("NodeOffsetPath"), path(core_types::vector), inspector("offset_path_inspector"))] +#[node_macro::node(category("Vector: Path"), icon("NodeOffsetPath"), path(core_types::vector), inspector("offset_path_inspector"), version(2))] fn offset_path( _: impl Ctx, #[implementations(Graphic, Vector)] content: Item, - distance: Item, + distance: Item, join: Item, #[hard(0..)] #[default(4.)] @@ -1784,7 +1776,7 @@ fn offset_path( ) -> Result, OffsetPathError> { V::try_map_vector_items(content, |content| { let mut content = content; - let (distance, join, miter_limit) = (*distance.element(), *join.element(), *miter_limit.element()); + let (distance, join, miter_limit) = (distance.element().pixels(), *join.element(), *miter_limit.element()); let transform_attribute: DAffine2 = content.attribute_cloned_or_default(ATTR_TRANSFORM); if !transform_attribute.matrix2.determinant().is_normal() { @@ -2118,30 +2110,25 @@ pub fn combine_paths(_: impl Ctx, content: List) -> Item { } /// Convert vector geometry into a polyline composed of evenly spaced points. -#[node_macro::node(category("Vector: Path"), icon("NodeSamplePolyline"), path(core_types::vector), inspector("sample_polyline_inspector"), memoize)] +#[node_macro::node(category("Vector: Path"), icon("NodeSamplePolyline"), path(core_types::vector), inspector("sample_polyline_inspector"), memoize, version(2))] fn sample_polyline( _: impl Ctx, #[implementations(Graphic, Vector)] content: Item, spacing: Item, #[default(100.)] #[hard(0..)] - #[unit(" px")] - separation: Item, + separation: Item, #[default(100)] #[hard(2..)] quantity: Item, - #[hard(0..)] - #[unit(" px")] - start_offset: Item, - #[hard(0..)] - #[unit(" px")] - stop_offset: Item, + #[hard(0..)] start_offset: Item, + #[hard(0..)] stop_offset: Item, adaptive_spacing: Item, ) -> Result, SampleCountError> { V::try_map_vector_items(content, |content| { let mut content = content; - let (spacing, separation, quantity) = (*spacing.element(), *separation.element(), *quantity.element()); - let (start_offset, stop_offset, adaptive_spacing) = (*start_offset.element(), *stop_offset.element(), *adaptive_spacing.element()); + let (spacing, separation, quantity) = (*spacing.element(), separation.element().pixels(), *quantity.element()); + let (start_offset, stop_offset, adaptive_spacing) = (start_offset.element().pixels(), stop_offset.element().pixels(), *adaptive_spacing.element()); let pathseg_perimeter = |segment: PathSeg| { if is_linear(segment) { @@ -2216,19 +2203,18 @@ fn tolerance_floor(bezpath: &BezPath) -> f64 { } /// Simplifies vector paths by reducing the number of curve segments while preserving the overall shape within the given tolerance. -#[node_macro::node(category("Vector: Path"), icon("NodeSimplify"), path(core_types::vector))] +#[node_macro::node(category("Vector: Path"), icon("NodeSimplify"), path(core_types::vector), version(2))] fn simplify( _: impl Ctx, /// The vector paths to simplify. #[implementations(Graphic, Vector)] content: Item, /// The maximum distance the simplified path may deviate from the original. - #[unit(" px")] #[hard(0..)] #[default(5.)] - tolerance: Item, + tolerance: Item, ) -> Item { - let tolerance = *tolerance.element(); + let tolerance = tolerance.element().pixels(); if tolerance <= 0. { return content; @@ -2271,21 +2257,20 @@ fn simplify( } /// Decimates vector paths into polylines by sampling any curves into line segments, then removing points that don't significantly contribute to the shape using the Ramer-Douglas-Peucker algorithm. -#[node_macro::node(category("Vector: Path"), icon("NodeDecimate"), path(core_types::vector))] +#[node_macro::node(category("Vector: Path"), icon("NodeDecimate"), path(core_types::vector), version(2))] fn decimate( _: impl Ctx, /// The vector paths to decimate. #[implementations(Graphic, Vector)] content: Item, /// The maximum distance a point can deviate from the simplified path before it is kept. - #[unit(" px")] #[hard(0..)] #[default(5.)] - tolerance: Item, + tolerance: Item, ) -> Item { V::map_vector_items(content, |content| { let mut content = content; - let tolerance = *tolerance.element(); + let tolerance = tolerance.element().pixels(); // Tolerance of 0 means no simplification is possible, so return immediately if tolerance <= 0. { @@ -2554,16 +2539,15 @@ fn evaluate_path( }) } -#[node_macro::node(category("Vector: Point"), icon("NodeScatterPoints"), path(core_types::vector), version(2), memoize)] +#[node_macro::node(category("Vector: Point"), icon("NodeScatterPoints"), path(core_types::vector), version(3), memoize)] fn scatter_points( _: impl Ctx, #[implementations(Graphic, Vector)] content: Item, - #[unit(" px")] #[default(10.)] #[range] #[hard(0.01..)] #[soft(1..100)] - separation: Item, + separation: Item, #[hard(0..)] seed: Item, ) -> Result, PoissonDiskError> { // One sampler for every vector in the content holds them all together to its limits on work @@ -2571,7 +2555,7 @@ fn scatter_points( V::try_map_vector_items(content, |content| { let mut content = content; - let (separation, seed) = (*separation.element(), *seed.element()); + let (separation, seed) = (separation.element().pixels(), *seed.element()); let content_paths = shared_paths_from_item_attributes(&content); let bezpaths: Vec<_> = content.element().stroke_bezpath_iter(&content_paths).collect(); @@ -2732,16 +2716,15 @@ fn apply_point_deltas(vector: &mut Vector, paths: &List, anchors: &[usize] } /// Perturbs the positions of anchor points in vector geometry by random amounts and directions. -#[node_macro::node(category("Vector: Point"), icon("NodeJitterPoints"), path(core_types::vector))] +#[node_macro::node(category("Vector: Point"), icon("NodeJitterPoints"), path(core_types::vector), version(2))] fn jitter_points( _: impl Ctx, /// The vector geometry with points to be jittered. #[implementations(Graphic, Vector)] content: Item, /// The maximum extent of the random distance each point can be offset. - #[unit(" px")] #[default(5.)] - max_distance: Item, + max_distance: Item, /// Seed used to determine unique variations on all randomized offsets. #[hard(0..)] seed: Item, @@ -2751,7 +2734,7 @@ fn jitter_points( ) -> Item { V::map_vector_items(content, |content| { let mut content = content; - let (max_distance, seed, along_normals) = (*max_distance.element(), *seed.element(), *along_normals.element()); + let (max_distance, seed, along_normals) = (max_distance.element().pixels(), *seed.element(), *along_normals.element()); let mut rng = rand::rngs::StdRng::seed_from_u64(seed as u64); let transform_attribute: DAffine2 = content.attribute_cloned_or_default(ATTR_TRANSFORM); @@ -2784,7 +2767,7 @@ fn jitter_points( /// Displaces anchor points along their normal direction (perpendicular to the path) by a set distance. /// Points with 0 or 3+ segment connections have no well-defined normal and are left in place. -#[node_macro::node(category("Vector: Point"), icon("NodeOffsetPoints"), path(core_types::vector))] +#[node_macro::node(category("Vector: Point"), icon("NodeOffsetPoints"), path(core_types::vector), version(2))] fn offset_points( _: impl Ctx, /// The vector geometry with points to be offset. @@ -2792,12 +2775,11 @@ fn offset_points( content: Item, /// The distance to offset each anchor point along its normal. Positive values move outward, negative values move inward. #[default(10.)] - #[unit(" px")] - distance: Item, + distance: Item, ) -> Item { V::map_vector_items(content, |content| { let mut content = content; - let distance = *distance.element(); + let distance = distance.element().pixels(); let transform_attribute: DAffine2 = content.attribute_cloned_or_default(ATTR_TRANSFORM); let inverse_linear = inverse_linear_or_repair(transform_attribute.matrix2); @@ -3800,18 +3782,17 @@ fn bevel_algorithm(vector: &mut Vector, paths: &mut List, transform: DAffi } } -#[node_macro::node(category("Vector: Path"), icon("NodeBevel"), path(core_types::vector))] +#[node_macro::node(category("Vector: Path"), icon("NodeBevel"), path(core_types::vector), version(2))] fn bevel( _: impl Ctx, #[implementations(Graphic, Vector)] source: Item, - #[unit(" px")] #[hard(0..)] #[default(10.)] - distance: Item, + distance: Item, ) -> Item { V::map_vector_items(source, |source| { let mut source = source; - let distance = *distance.element(); + let distance = distance.element().pixels(); let transform: DAffine2 = source.attribute_cloned_or_default(ATTR_TRANSFORM); edit_item_geometry(&mut source, |vector, paths| bevel_algorithm(vector, paths, transform, distance)); @@ -4029,6 +4010,10 @@ mod test { Item::new_from_element(value) } + fn pixels(pixels: f64) -> Item { + item(Length::from_pixels(pixels)) + } + /// The paths riding an item, standing in for the empty weave when the item carries none. fn item_paths(item: &Item) -> &List { paths_from_item_attributes(item).unwrap_or_else(|| no_paths()) @@ -4109,7 +4094,7 @@ mod test { // Offsetting closed triangles must not leave each subpath with a redundant start/end anchor (a Kurbo offset // contour returns to approximately, not exactly, its start; that near-coincident point must close, not duplicate). let delaunay = super::triangulate((), vector_item_from_points(&SQUARE_WITH_CENTER), item(false)).unwrap(); - let offset = super::offset_path((), delaunay, item(0.5), item(StrokeJoin::Miter), item(4.)).unwrap(); + let offset = super::offset_path((), delaunay, pixels(0.5), item(StrokeJoin::Miter), item(4.)).unwrap(); let paths = item_paths(&offset); let result = offset.element(); @@ -4446,10 +4431,10 @@ mod test { Footprint::default(), vector_item_from_bezpath(path), Item::new_from_element(PointSpacingType::Separation), - Item::new_from_element(30.), + pixels(30.), Item::new_from_element(0), - Item::new_from_element(0.), - Item::new_from_element(0.), + pixels(0.), + pixels(0.), Item::new_from_element(false), ) .unwrap(); @@ -4466,10 +4451,10 @@ mod test { Footprint::default(), vector_item_from_bezpath(path), Item::new_from_element(PointSpacingType::Separation), - Item::new_from_element(18.), + pixels(18.), Item::new_from_element(0), - Item::new_from_element(45.), - Item::new_from_element(10.), + pixels(45.), + pixels(10.), Item::new_from_element(true), ) .unwrap(); @@ -4484,7 +4469,7 @@ mod test { let poisson_points = super::scatter_points( Footprint::default(), vector_item_from_bezpath(ellipse_bezpath(DVec2::splat(-50.), DVec2::splat(50.))), - Item::new_from_element(10. * std::f64::consts::SQRT_2), + pixels(10. * std::f64::consts::SQRT_2), Item::new_from_element(0), ) .expect("the separation should fit the circle"); @@ -4798,7 +4783,7 @@ mod test { #[test] fn bevel_rect() { let source = Rect::new(0., 0., 100., 100.).to_path(DEFAULT_ACCURACY); - let beveled = super::bevel(Footprint::default(), vector_item_from_bezpath(source), Item::new_from_element(2_f64.sqrt() * 10.)); + let beveled = super::bevel(Footprint::default(), vector_item_from_bezpath(source), pixels(2_f64.sqrt() * 10.)); let paths = item_paths(&beveled); let beveled = beveled.element(); @@ -4827,7 +4812,7 @@ mod test { source.line_to(Point::ZERO); source.push(curve.as_path_el()); - let beveled = super::bevel((), vector_item_from_bezpath(source), Item::new_from_element(2_f64.sqrt() * 10.)); + let beveled = super::bevel((), vector_item_from_bezpath(source), pixels(2_f64.sqrt() * 10.)); let paths = item_paths(&beveled); let beveled = beveled.element(); @@ -4855,7 +4840,7 @@ mod test { let transform = DAffine2::from_scale_angle_translation(DVec2::splat(10.), 1., DVec2::new(99., 77.)); let vector_item = Vector::item_from_bezpath(&source).with_attribute(ATTR_TRANSFORM, transform); - let beveled = super::bevel((), vector_item, Item::new_from_element(2_f64.sqrt() * 100.)); + let beveled = super::bevel((), vector_item, pixels(2_f64.sqrt() * 100.)); let paths = item_paths(&beveled); let beveled = beveled.element(); @@ -4879,7 +4864,7 @@ mod test { source.line_to(Point::new(100., 100.)); source.line_to(Point::new(0., 100.)); - let beveled = super::bevel(Footprint::default(), vector_item_from_bezpath(source), Item::new_from_element(999.)); + let beveled = super::bevel(Footprint::default(), vector_item_from_bezpath(source), pixels(999.)); let paths = item_paths(&beveled); let beveled = beveled.element(); @@ -4904,7 +4889,7 @@ mod test { let subpath = BezPath::from_path_segments([line, point, curve].into_iter()); - let beveled_item = super::bevel(Footprint::default(), vector_item_from_bezpath(subpath), Item::new_from_element(5.)); + let beveled_item = super::bevel(Footprint::default(), vector_item_from_bezpath(subpath), pixels(5.)); let paths = item_paths(&beveled_item); let beveled = beveled_item.element(); @@ -4952,7 +4937,7 @@ mod test { } fn round_corners_of(bezpath: BezPath, transform: DAffine2) -> Vec { - let rounded = super::round_corners((), create_vector_item(bezpath, transform), item(10.), item(0.5), item(100.), item(5.)); + let rounded = super::round_corners((), create_vector_item(bezpath, transform), pixels(10.), item(0.5), item(100.), item(Angle::from_degrees(5.))); rounded.element().stroke_bezpath_iter(item_paths(&rounded)).collect() } @@ -5067,7 +5052,7 @@ mod test { // Sampling the welded diamond goes once around its closed contour, and once unwelded, along each open path on its own #[test] fn unwelded_paths_are_sampled_one_at_a_time() { - let sample = |content: Item| super::sample_polyline((), content, item(PointSpacingType::Quantity), item(100.), item(5), item(0.), item(0.), item(false)).unwrap(); + let sample = |content: Item| super::sample_polyline((), content, item(PointSpacingType::Quantity), pixels(100.), item(5), pixels(0.), pixels(0.), item(false)).unwrap(); let polylines = |sampled: Item| sampled.element().stroke_bezpath_iter(item_paths(&sampled)).collect::>(); let welded = polylines(sample(welded_diamond())); @@ -5132,7 +5117,7 @@ mod test { #[test] fn offset_path_reports_a_shape_too_large_to_clip() { let star = vector_item_from_bezpath(vector_types::vector::algorithms::shapes::star_polygon_bezpath(DVec2::ZERO, 5, 1e20, 5e19).unwrap()); - let offset = super::offset_path((), star, item(-1e18), item(StrokeJoin::Miter), item(4.)); + let offset = super::offset_path((), star, pixels(-1e18), item(StrokeJoin::Miter), item(4.)); assert!(matches!(offset, Err(OffsetPathError::TooLarge))); } @@ -5141,25 +5126,28 @@ mod test { fn collapsed_items_pass_through() { let collapsed = create_vector_item(Rect::new(0., 0., 100., 100.).to_path(DEFAULT_ACCURACY), DAffine2::from_scale(DVec2::new(0., 1.))); - assert_eq!(super::round_corners((), collapsed.clone(), item(10.), item(0.5), item(100.), item(5.)), collapsed); - assert_eq!(super::offset_path((), collapsed.clone(), item(5.), item(StrokeJoin::Miter), item(4.)).unwrap(), collapsed); - assert_eq!(super::simplify((), collapsed.clone(), item(5.)), collapsed); - assert_eq!(super::decimate((), collapsed.clone(), item(5.)), collapsed); - assert_eq!(super::bevel((), collapsed.clone(), item(10.)), collapsed); + assert_eq!( + super::round_corners((), collapsed.clone(), pixels(10.), item(0.5), item(100.), item(Angle::from_degrees(5.))), + collapsed + ); + assert_eq!(super::offset_path((), collapsed.clone(), pixels(5.), item(StrokeJoin::Miter), item(4.)).unwrap(), collapsed); + assert_eq!(super::simplify((), collapsed.clone(), pixels(5.)), collapsed); + assert_eq!(super::decimate((), collapsed.clone(), pixels(5.)), collapsed); + assert_eq!(super::bevel((), collapsed.clone(), pixels(10.)), collapsed); } // Kurbo drops a subpath that simplifies down to nothing, so one without length passes through #[test] fn simplify_keeps_a_subpath_without_length() { let point = vector_item_from_bezpath(ellipse_bezpath(DVec2::splat(5.), DVec2::splat(5.))); - let simplified = super::simplify((), point, item(5.)); + let simplified = super::simplify((), point, pixels(5.)); assert_eq!(simplified.element().stroke_bezpath_iter(item_paths(&simplified)).count(), 1); } #[test] fn decimate_with_a_tolerance_finer_than_float_precision() { let circle = vector_item_from_bezpath(ellipse_bezpath(DVec2::splat(-50.), DVec2::splat(50.))); - let decimated = super::decimate((), circle, item(1e-60)); + let decimated = super::decimate((), circle, pixels(1e-60)); assert!(decimated.element().point_count() > 4); } @@ -5203,7 +5191,7 @@ mod test { fn scatter_points_samples_each_vector_in_graphic_content() { let shapes = [ellipse_bezpath(DVec2::splat(-50.), DVec2::splat(50.)), ellipse_bezpath(DVec2::new(200., -40.), DVec2::new(300., 40.))]; let vectors: List = shapes.iter().cloned().map(vector_item_from_bezpath).collect(); - let scattered = super::scatter_points((), Item::new_from_element(Graphic::VectorList(vectors)), item(10.), item(0)).expect("the shapes should scatter"); + let scattered = super::scatter_points((), Item::new_from_element(Graphic::VectorList(vectors)), pixels(10.), item(0)).expect("the shapes should scatter"); let Graphic::VectorList(vectors) = scattered.element() else { panic!("the content should stay a vector list") @@ -5217,12 +5205,12 @@ mod test { #[test] fn scatter_points_on_empty_content_gives_no_points() { - let scattered = super::scatter_points((), vector_item_from_bezpath(BezPath::new()), item(10.), item(0)).expect("empty content should scatter nothing"); + let scattered = super::scatter_points((), vector_item_from_bezpath(BezPath::new()), pixels(10.), item(0)).expect("empty content should scatter nothing"); assert_eq!(scattered.element().point_count(), 0); } fn scattered_points(bezpath: BezPath, separation: f64, seed: i64) -> Vec<(DVec2, PointId)> { - let scattered = super::scatter_points((), vector_item_from_bezpath(bezpath), item(separation), item(seed)).expect("the separation should fit the shape"); + let scattered = super::scatter_points((), vector_item_from_bezpath(bezpath), pixels(separation), item(seed)).expect("the separation should fit the shape"); let points = &scattered.element().0; points.iter_element_values().copied().zip(points.iter_attribute_values::(ATTR_ID).unwrap().copied()).collect() } @@ -5290,7 +5278,7 @@ mod test { #[test] fn scatter_points_reports_a_separation_too_small_for_the_shape() { let circle = |center: DVec2, radius: f64| vector_item_from_bezpath(ellipse_bezpath(center - radius, center + radius)); - let scatter = |content, separation| super::scatter_points((), content, item(separation), item(0)); + let scatter = |content, separation| super::scatter_points((), content, pixels(separation), item(0)); assert!(matches!(scatter(circle(DVec2::ZERO, 100.), 0.15), Err(PoissonDiskError::SeparationTooSmall { .. }))); assert!(matches!(scatter(circle(DVec2::ZERO, 100.), 0.01), Err(PoissonDiskError::ExtentTooLarge))); @@ -5317,9 +5305,9 @@ mod test { let transform = DAffine2::from_cols(DVec2::new(1e160, 0.), DVec2::ZERO, DVec2::ZERO); let content = || create_vector_item(Rect::new(0., 0., 10., 10.).to_path(DEFAULT_ACCURACY), transform); - let jittered = super::jitter_points((), content(), item(5.), item(0), item(true)); + let jittered = super::jitter_points((), content(), pixels(5.), item(0), item(true)); assert!(jittered.element().positions().all(|position| position.is_finite())); - let offset = super::offset_points((), content(), item(10.)); + let offset = super::offset_points((), content(), pixels(10.)); assert!(offset.element().positions().all(|position| position.is_finite())); } @@ -5360,7 +5348,7 @@ mod test { push_path(&mut paths, CurveType::Linear, [0, 1, 2, 3, 1], []); push_path(&mut paths, CurveType::Linear, [4, 5, 6], []); - let beveled = super::bevel((), Item::new_from_element(vector).with_attribute(ATTR_PATHS, paths), item(10.)); + let beveled = super::bevel((), Item::new_from_element(vector).with_attribute(ATTR_PATHS, paths), pixels(10.)); assert!(beveled.element().segment_iter(item_paths(&beveled)).count() > 6); } @@ -5376,7 +5364,7 @@ mod test { #[test] fn sample_polyline_reports_too_many_points() { let line = || vector_item_from_bezpath(Line::new((0., 0.), (100., 0.)).to_path(DEFAULT_ACCURACY)); - let sample = |spacing, separation, quantity| super::sample_polyline((), line(), item(spacing), item(separation), item(quantity), item(0.), item(0.), item(false)); + let sample = |spacing, separation, quantity| super::sample_polyline((), line(), item(spacing), pixels(separation), item(quantity), pixels(0.), pixels(0.), item(false)); assert!(matches!(sample(PointSpacingType::Separation, 0., 100), Err(SampleCountError::SeparationTooSmall { limit: 10_000 }))); assert!(matches!(sample(PointSpacingType::Quantity, 100., 10_001), Err(SampleCountError::QuantityTooLarge { limit: 10_000 })));