pasture_algorithms/
minmax.rs

1use pasture_core::{
2    containers::{BorrowedBuffer, BorrowedBufferExt},
3    layout::{PointAttributeDefinition, PrimitiveType},
4    math::MinMax,
5};
6
7/// Returns the minimum and maximum value of the given point `attribute` within `buffer`. Returns `None` if `buffer` contains no points. For
8/// vector `PrimitiveType`s such as `Vector3<f64>`, the component-wise minimum and maximum is applied.
9///
10/// # Panics
11///
12/// If `attribute` is not part of the point layout of `buffer`, or the attribute within `buffer` is not of type `T`
13pub fn minmax_attribute<'a, T: PrimitiveType + MinMax + Copy, B: BorrowedBuffer<'a>>(
14    buffer: &'a B,
15    attribute: &PointAttributeDefinition,
16) -> Option<(T, T)> {
17    if !buffer
18        .point_layout()
19        .has_attribute_with_name(attribute.name())
20    {
21        panic!(
22            "Attribute {} not contained in PointLayout buffer ({})",
23            attribute,
24            buffer.point_layout()
25        );
26    }
27
28    let mut minmax = None;
29
30    if T::data_type() == attribute.datatype() {
31        for val in buffer.view_attribute::<T>(attribute) {
32            match minmax {
33                None => minmax = Some((val, val)),
34                Some((old_min, old_max)) => {
35                    minmax = Some((val.infimum(&old_min), val.supremum(&old_max)));
36                }
37            }
38        }
39    } else {
40        for val in buffer.view_attribute_with_conversion::<T>(attribute).ok()? {
41            match minmax {
42                None => minmax = Some((val, val)),
43                Some((old_min, old_max)) => {
44                    minmax = Some((val.infimum(&old_min), val.supremum(&old_max)));
45                }
46            }
47        }
48    }
49
50    minmax
51}