From 3482c5b8a5d351f0eaa44333e0446a1f731a0749 Mon Sep 17 00:00:00 2001 From: celyk Date: Sun, 6 Jul 2025 18:15:27 +1000 Subject: [PATCH 1/4] Add vector length node --- node-graph/gmath-nodes/src/lib.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/node-graph/gmath-nodes/src/lib.rs b/node-graph/gmath-nodes/src/lib.rs index c4d1b9a1b..fe9812afb 100644 --- a/node-graph/gmath-nodes/src/lib.rs +++ b/node-graph/gmath-nodes/src/lib.rs @@ -612,6 +612,11 @@ fn dot_product(_: impl Ctx, vector_a: DVec2, vector_b: DVec2) -> f64 { vector_a.dot(vector_b) } +#[node_macro::node(category("Math: Vector"))] +fn length(_: impl Ctx, vector: DVec2) -> f64 { + vector.length() +} + #[cfg(test)] mod test { use super::*; @@ -625,6 +630,12 @@ mod test { assert_eq!(dot_product((), vector_a, vector_b), 11.); } + #[test] + pub fn length_function() { + let vector = DVec2::new(3., 4.); + assert_eq!(length((), vector), 5.); + } + #[test] fn test_basic_expression() { let result = math((), 0., "2 + 2".to_string(), 0.); From a337a52c1ef307d6bf8d310c26bd44458c699da7 Mon Sep 17 00:00:00 2001 From: celyk Date: Sun, 6 Jul 2025 18:52:18 +1000 Subject: [PATCH 2/4] Add normalize node --- node-graph/gmath-nodes/src/lib.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/node-graph/gmath-nodes/src/lib.rs b/node-graph/gmath-nodes/src/lib.rs index fe9812afb..b4d76432c 100644 --- a/node-graph/gmath-nodes/src/lib.rs +++ b/node-graph/gmath-nodes/src/lib.rs @@ -617,6 +617,11 @@ fn length(_: impl Ctx, vector: DVec2) -> f64 { vector.length() } +#[node_macro::node(category("Math: Vector"))] +fn normalize(_: impl Ctx, vector: DVec2) -> DVec2 { + vector.normalize() +} + #[cfg(test)] mod test { use super::*; From 592d6bfcbd7b4fb6644b0fea45908cd3852e99c0 Mon Sep 17 00:00:00 2001 From: celyk Date: Sun, 6 Jul 2025 19:10:18 +1000 Subject: [PATCH 3/4] Add comments --- node-graph/gmath-nodes/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/node-graph/gmath-nodes/src/lib.rs b/node-graph/gmath-nodes/src/lib.rs index b4d76432c..f76cba4aa 100644 --- a/node-graph/gmath-nodes/src/lib.rs +++ b/node-graph/gmath-nodes/src/lib.rs @@ -612,11 +612,13 @@ fn dot_product(_: impl Ctx, vector_a: DVec2, vector_b: DVec2) -> f64 { vector_a.dot(vector_b) } +/// Gets the length or magnitude of a vector. #[node_macro::node(category("Math: Vector"))] fn length(_: impl Ctx, vector: DVec2) -> f64 { vector.length() } +/// Scales the input vector to unit length while preserving it's direction. This is equivalent to dividing the input vector by it's own magnitude. #[node_macro::node(category("Math: Vector"))] fn normalize(_: impl Ctx, vector: DVec2) -> DVec2 { vector.normalize() From 7de13aca3e0fb8e46756d80b67cdc4a5ffb413e4 Mon Sep 17 00:00:00 2001 From: celyk Date: Sun, 6 Jul 2025 19:40:14 +1000 Subject: [PATCH 4/4] Make normalize node safer for the user --- node-graph/gmath-nodes/src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/node-graph/gmath-nodes/src/lib.rs b/node-graph/gmath-nodes/src/lib.rs index f76cba4aa..1019506bd 100644 --- a/node-graph/gmath-nodes/src/lib.rs +++ b/node-graph/gmath-nodes/src/lib.rs @@ -619,9 +619,11 @@ fn length(_: impl Ctx, vector: DVec2) -> f64 { } /// Scales the input vector to unit length while preserving it's direction. This is equivalent to dividing the input vector by it's own magnitude. +/// +/// Returns zero when the input vector is zero. #[node_macro::node(category("Math: Vector"))] fn normalize(_: impl Ctx, vector: DVec2) -> DVec2 { - vector.normalize() + vector.normalize_or_zero() } #[cfg(test)]