Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion node-graph/nodes/math/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core_types::Context;
use core_types::context::{CloneVarArgs, ExtractAll};
use core_types::list::{Bundle, Item};
use core_types::list::{Bundle, Item, List};
use core_types::registry::types::{Fraction, Percentage, PixelSize};
use core_types::transform::Footprint;
use core_types::{Color, Ctx, OwnedContextImpl, num_traits};
Expand Down Expand Up @@ -721,6 +721,45 @@ fn binary_gcd<T: num_traits::int::PrimInt + std::ops::ShrAssign<i32> + std::ops:
a << shift
}

/// Adds together all the numbers in the input list, producing their total.
#[node_macro::node(category("Math: Numeric"))]
fn sum(_: impl Ctx, values: List<f64>) -> Item<f64> {
Item::new_from_element(values.iter_element_values().sum())
}

/// Averages all the numbers in the input list. An empty list gives 0.
#[node_macro::node(category("Math: Numeric"))]
fn average(_: impl Ctx, values: List<f64>) -> Item<f64> {
let count = values.len();
let average = if count == 0 { 0. } else { values.iter_element_values().sum::<f64>() / count as f64 };

Item::new_from_element(average)
}

/// Gives the smallest number in the input list. An empty list gives 0.
#[node_macro::node(category("Math: Numeric"))]
fn minimum(_: impl Ctx, values: List<f64>) -> Item<f64> {
Item::new_from_element(values.iter_element_values().copied().reduce(f64::min).unwrap_or_default())
}

/// Gives the largest number in the input list. An empty list gives 0.
#[node_macro::node(category("Math: Numeric"))]
fn maximum(_: impl Ctx, values: List<f64>) -> Item<f64> {
Item::new_from_element(values.iter_element_values().copied().reduce(f64::max).unwrap_or_default())
}

/// Outputs true if at least one value in the input list is true. An empty list gives false.
#[node_macro::node(category("Math: Logic"))]
fn any(_: impl Ctx, values: List<bool>) -> Item<bool> {
Item::new_from_element(values.iter_element_values().any(|&value| value))
}

/// Outputs true only if every value in the input list is true. An empty list gives true.
#[node_macro::node(category("Math: Logic"))]
fn all(_: impl Ctx, values: List<bool>) -> Item<bool> {
Item::new_from_element(values.iter_element_values().all(|&value| value))
}
Comment thread
Keavon marked this conversation as resolved.

/// The less-than operation (`<`) compares two values and returns true if the first value is less than the second, or false if it is not.
/// If enabled with *Or Equal*, the less-than-or-equal operation (`<=`) is used instead.
#[node_macro::node(category("Math: Logic"))]
Expand Down
Loading