Skip to content

Commit

Permalink
Make center and half_size private in Aabb
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Oom committed Mar 27, 2024
1 parent 65e409a commit 9de2b00
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
12 changes: 10 additions & 2 deletions geometry/src/aabb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use super::aap::Aap;

#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
pub struct Aabb {
pub center: Vector3<f32>,
pub half_size: Vector3<f32>,
center: Vector3<f32>,
half_size: Vector3<f32>,
}

impl Aabb {
Expand All @@ -32,6 +32,14 @@ impl Aabb {
}
}

pub fn center(&self) -> Vector3<f32> {
self.center
}

pub fn half_size(&self) -> Vector3<f32> {
self.half_size
}

pub fn size(&self) -> Vector3<f32> {
2.0 * self.half_size
}
Expand Down
35 changes: 13 additions & 22 deletions geometry/src/algorithms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,12 @@ pub fn intersect_triangle_aabb(triangle: &Triangle, aabb: &Aabb) -> bool {
const U1: Vector3<f32> = Vector3::new(0., 1., 0.);
const U2: Vector3<f32> = Vector3::new(0., 0., 1.);

let v0 = triangle.v0 - aabb.center;
let v1 = triangle.v1 - aabb.center;
let v2 = triangle.v2 - aabb.center;
let center = aabb.center();
let half_size = aabb.half_size();

let v0 = triangle.v0 - center;
let v1 = triangle.v1 - center;
let v2 = triangle.v2 - center;

let f0 = v1 - v0;
let f1 = v2 - v1;
Expand All @@ -274,9 +277,9 @@ pub fn intersect_triangle_aabb(triangle: &Triangle, aabb: &Aabb) -> bool {
let p0 = v0.dot(&axis);
let p1 = v1.dot(&axis);
let p2 = v2.dot(&axis);
let r = aabb.half_size.x * U0.dot(&axis).abs()
+ aabb.half_size.y * U1.dot(&axis).abs()
+ aabb.half_size.z * U2.dot(&axis).abs();
let r = half_size.x * U0.dot(&axis).abs()
+ half_size.y * U1.dot(&axis).abs()
+ half_size.z * U2.dot(&axis).abs();
(-p0.max(p1.max(p2))).max(p0.min(p1.min(p2))) > r
};

Expand Down Expand Up @@ -337,10 +340,7 @@ mod tests_intersect_triangle_aabb {
v1: Vector3::new(2., 1., 1.),
v2: Vector3::new(1., 2., 1.),
};
let aabb = Aabb {
center: Vector3::new(1., 1., 1.),
half_size: Vector3::new(1., 1., 1.),
};
let aabb = Aabb::from_extents(&Vector3::new(0., 0., 0.), &Vector3::new(2., 2., 2.));

assert_eq!(intersect_triangle_aabb(&triangle, &aabb), true);
}
Expand All @@ -352,10 +352,7 @@ mod tests_intersect_triangle_aabb {
v1: Vector3::new(2., 1., 2.),
v2: Vector3::new(1., 2., 2.),
};
let aabb = Aabb {
center: Vector3::new(1., 1., 1.),
half_size: Vector3::new(1., 1., 1.),
};
let aabb = Aabb::from_extents(&Vector3::new(0., 0., 0.), &Vector3::new(2., 2., 2.));

assert_eq!(intersect_triangle_aabb(&triangle, &aabb), true);
}
Expand All @@ -367,10 +364,7 @@ mod tests_intersect_triangle_aabb {
v1: Vector3::new(11., 10., 10.),
v2: Vector3::new(10., 11., 10.),
};
let aabb = Aabb {
center: Vector3::new(1., 1., 1.),
half_size: Vector3::new(1., 1., 1.),
};
let aabb = Aabb::from_extents(&Vector3::new(0., 0., 0.), &Vector3::new(2., 2., 2.));

assert_eq!(intersect_triangle_aabb(&triangle, &aabb), false);
}
Expand Down Expand Up @@ -410,10 +404,7 @@ mod tests {

let actual = triangles_bounding_box(&triangles);

let expected = Aabb {
center: Vector3::new(0., 0., 0.),
half_size: Vector3::new(1., 1., 1.),
};
let expected = Aabb::from_extents(&Vector3::new(-1., -1., -1.), &Vector3::new(1., 1., 1.));
assert_eq!(actual, expected);
}
}
Expand Down

0 comments on commit 9de2b00

Please sign in to comment.