use std::cmp;
use nalgebra::{Scalar, Vector3};
pub trait MinMax {
fn infimum(&self, other: &Self) -> Self;
fn supremum(&self, other: &Self) -> Self;
}
macro_rules! impl_minmax_for_primitive_type {
($type:tt) => {
impl MinMax for $type {
fn infimum(&self, other: &Self) -> Self {
cmp::min(*self, *other)
}
fn supremum(&self, other: &Self) -> Self {
cmp::max(*self, *other)
}
}
};
}
impl_minmax_for_primitive_type! {u8}
impl_minmax_for_primitive_type! {u16}
impl_minmax_for_primitive_type! {u32}
impl_minmax_for_primitive_type! {u64}
impl_minmax_for_primitive_type! {i8}
impl_minmax_for_primitive_type! {i16}
impl_minmax_for_primitive_type! {i32}
impl_minmax_for_primitive_type! {i64}
impl_minmax_for_primitive_type! {bool}
impl MinMax for f32 {
fn infimum(&self, other: &Self) -> Self {
if *self < *other {
*self
} else {
*other
}
}
fn supremum(&self, other: &Self) -> Self {
if *self > *other {
*self
} else {
*other
}
}
}
impl MinMax for f64 {
fn infimum(&self, other: &Self) -> Self {
if *self < *other {
*self
} else {
*other
}
}
fn supremum(&self, other: &Self) -> Self {
if *self > *other {
*self
} else {
*other
}
}
}
impl<T: MinMax + Scalar> MinMax for Vector3<T> {
fn infimum(&self, other: &Self) -> Self {
Vector3::new(
self.x.infimum(&other.x),
self.y.infimum(&other.y),
self.z.infimum(&other.z),
)
}
fn supremum(&self, other: &Self) -> Self {
Vector3::new(
self.x.supremum(&other.x),
self.y.supremum(&other.y),
self.z.supremum(&other.z),
)
}
}