Add Interval::union()

This commit is contained in:
Matthew Gordon 2020-01-10 16:46:17 -05:00
parent 5f6733fdb0
commit da2208f3f8
1 changed files with 34 additions and 3 deletions

View File

@ -44,6 +44,13 @@ impl<T: RealField> Interval<T> {
max: self.max.min(b.max),
}
}
pub fn union(self, b: Self) -> Self {
Interval {
min: self.min.min(b.min),
max: self.max.max(b.max),
}
}
}
pub struct BoundingBox<T: RealField> {
@ -91,6 +98,11 @@ impl<T: RealField> IntersectP<T> for BoundingBox<T> {
mod tests {
use super::*;
use itertools::{Itertools, MinMaxResult};
use quickcheck::TestResult;
use quickcheck_macros::quickcheck;
mod interval {
use super::*;
@ -211,6 +223,28 @@ mod tests {
assert!(target.min == result.min);
assert!(target.max == result.max);
}
#[quickcheck]
fn union_with_self_yields_self(a: f64, b: f64) -> bool {
let target = Interval::new(a, b);
let result = target.union(target);
result.min == target.min && result.max == target.max
}
#[quickcheck]
fn union_yields_min_and_max(a: f64, b: f64, c: f64, d: f64) -> bool {
let values = vec![a, b, c, d];
if let MinMaxResult::MinMax(&min, &max) =
values.iter().minmax_by(|a, b| a.partial_cmp(b).unwrap())
{
let target1 = Interval::new(a, b);
let target2 = Interval::new(c, d);
let result = target1.union(target2);
result.min == min && result.max == max
} else {
false
}
}
}
mod bounding_box {
@ -218,9 +252,6 @@ mod tests {
use nalgebra::Vector3;
use quickcheck::TestResult;
use quickcheck_macros::quickcheck;
#[test]
fn from_corners_with_same_point_yields_degenerate_intervals() {
let test_point = Point3::new(0f64, 1f64, 2f64);