Skip to content

Commit

Permalink
refactor: remove Copy on Interval
Browse files Browse the repository at this point in the history
  • Loading branch information
Walther committed Jun 22, 2024
1 parent 52aa91f commit b5ea84e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion clovers/benches/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn new_from_intervals(bencher: divan::Bencher) {
(ab, cd)
})
.counter(1u32)
.bench_values(|(ab, cd)| black_box(Interval::new_from_intervals(ab, cd)))
.bench_values(|(ab, cd)| black_box(Interval::new_from_intervals(&ab, &cd)))
}

#[divan::bench]
Expand Down
20 changes: 10 additions & 10 deletions clovers/src/aabb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ impl AABB {
#[must_use]
pub fn surrounding_box(box0: &AABB, box1: &AABB) -> AABB {
AABB {
x: Interval::new_from_intervals(box0.x, box1.x),
y: Interval::new_from_intervals(box0.y, box1.y),
z: Interval::new_from_intervals(box0.z, box1.z),
x: Interval::new_from_intervals(&box0.x, &box1.x),
y: Interval::new_from_intervals(&box0.y, &box1.y),
z: Interval::new_from_intervals(&box0.z, &box1.z),
}
}

Expand All @@ -100,17 +100,17 @@ impl AABB {
// TODO: refactor
let delta = EPSILON_RECT_THICKNESS;
let new_x: Interval = if self.x.size() >= delta {
self.x
self.x.clone()
} else {
self.x.expand(delta)
};
let new_y: Interval = if self.y.size() >= delta {
self.y
self.y.clone()
} else {
self.y.expand(delta)
};
let new_z: Interval = if self.z.size() >= delta {
self.z
self.z.clone()
} else {
self.z.expand(delta)
};
Expand All @@ -121,11 +121,11 @@ impl AABB {
/// Returns the interval of the given axis.
// TODO: this api is kind of annoying
#[must_use]
pub fn axis(&self, n: usize) -> Interval {
pub fn axis(&self, n: usize) -> &Interval {
match n {
0 => self.x,
1 => self.y,
2 => self.z,
0 => &self.x,
1 => &self.y,
2 => &self.z,
_ => panic!("AABB::axis called with invalid parameter: {n:?}"),
}
}
Expand Down
6 changes: 3 additions & 3 deletions clovers/src/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::ops::Add;
use crate::Float;

/// An interval structure.
#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde-derive", derive(serde::Serialize, serde::Deserialize))]
pub struct Interval {
/// Smallest value of the interval. Must be kept in order
Expand All @@ -27,7 +27,7 @@ impl Interval {
/// Constructs a new interval from two intervals
// TODO: explanation, clarification
#[must_use]
pub fn new_from_intervals(a: Interval, b: Interval) -> Self {
pub fn new_from_intervals(a: &Interval, b: &Interval) -> Self {
Interval {
min: a.min.min(b.min),
max: a.max.max(b.max),
Expand All @@ -42,7 +42,7 @@ impl Interval {

/// Returns the size of the interval
#[must_use]
pub fn size(self) -> Float {
pub fn size(&self) -> Float {
self.max - self.min
}
}
Expand Down

0 comments on commit b5ea84e

Please sign in to comment.