Skip to content

Commit

Permalink
Merge pull request #2298 from hannobraun/geometry
Browse files Browse the repository at this point in the history
Make some cleanups in `BoundingVolume` code
  • Loading branch information
hannobraun authored Mar 26, 2024
2 parents 7f3edb0 + 2eaf27b commit 54eef76
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 21 deletions.
13 changes: 8 additions & 5 deletions crates/fj-core/src/algorithms/bounding_volume/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ use fj_math::Aabb;

use crate::{geometry::Geometry, topology::Cycle};

impl super::BoundingVolume<2> for Cycle {
fn aabb(&self, geometry: &Geometry) -> Option<Aabb<2>> {
impl super::BoundingVolume<2> for &Cycle {
fn aabb(self, geometry: &Geometry) -> Option<Aabb<2>> {
let cycle = self;

let mut aabb: Option<Aabb<2>> = None;

for edge in self.half_edges() {
let new_aabb =
edge.aabb(geometry).expect("`Edge` can always compute AABB");
for half_edge in cycle.half_edges() {
let new_aabb = half_edge
.aabb(geometry)
.expect("`HalfEdge` can always compute AABB");
aabb = Some(aabb.map_or(new_aabb, |aabb| aabb.merged(&new_aabb)));
}

Expand Down
4 changes: 2 additions & 2 deletions crates/fj-core/src/algorithms/bounding_volume/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::{
topology::Face,
};

impl super::BoundingVolume<3> for Face {
fn aabb(&self, geometry: &Geometry) -> Option<Aabb<3>> {
impl super::BoundingVolume<3> for &Face {
fn aabb(self, geometry: &Geometry) -> Option<Aabb<3>> {
self.region().exterior().aabb(geometry).map(|aabb2| {
let surface = geometry.of_surface(self.surface());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ use crate::{
topology::HalfEdge,
};

impl super::BoundingVolume<2> for Handle<HalfEdge> {
fn aabb(&self, geometry: &Geometry) -> Option<Aabb<2>> {
match geometry.of_half_edge(self).path {
impl super::BoundingVolume<2> for &Handle<HalfEdge> {
fn aabb(self, geometry: &Geometry) -> Option<Aabb<2>> {
let half_edge = self;

let half_edge = geometry.of_half_edge(half_edge);
let path = half_edge.path;

match path {
SurfacePath::Circle(circle) => {
// Just calculate the AABB of the whole circle. This is not the
// most precise, but it should do for now.
Expand All @@ -22,10 +27,8 @@ impl super::BoundingVolume<2> for Handle<HalfEdge> {
})
}
SurfacePath::Line(_) => {
let geometry = geometry.of_half_edge(self);

let points = geometry.boundary.inner.map(|point_curve| {
geometry.path.point_from_path_coords(point_curve)
let points = half_edge.boundary.inner.map(|point_curve| {
path.point_from_path_coords(point_curve)
});

Some(Aabb::<2>::from_points(points))
Expand Down
4 changes: 2 additions & 2 deletions crates/fj-core/src/algorithms/bounding_volume/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Compute a bounding volume for an object

mod cycle;
mod edge;
mod face;
mod half_edge;
mod shell;
mod solid;

Expand All @@ -15,5 +15,5 @@ pub trait BoundingVolume<const D: usize> {
/// Compute an axis-aligned bounding box (AABB)
///
/// Return `None`, if no AABB can be computed (if the object is empty).
fn aabb(&self, geometry: &Geometry) -> Option<Aabb<D>>;
fn aabb(self, geometry: &Geometry) -> Option<Aabb<D>>;
}
4 changes: 2 additions & 2 deletions crates/fj-core/src/algorithms/bounding_volume/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use fj_math::Aabb;

use crate::{geometry::Geometry, topology::Shell};

impl super::BoundingVolume<3> for Shell {
fn aabb(&self, geometry: &Geometry) -> Option<Aabb<3>> {
impl super::BoundingVolume<3> for &Shell {
fn aabb(self, geometry: &Geometry) -> Option<Aabb<3>> {
let mut aabb: Option<Aabb<3>> = None;

for face in self.faces() {
Expand Down
4 changes: 2 additions & 2 deletions crates/fj-core/src/algorithms/bounding_volume/solid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use fj_math::Aabb;

use crate::{geometry::Geometry, topology::Solid};

impl super::BoundingVolume<3> for Solid {
fn aabb(&self, geometry: &Geometry) -> Option<Aabb<3>> {
impl super::BoundingVolume<3> for &Solid {
fn aabb(self, geometry: &Geometry) -> Option<Aabb<3>> {
let mut aabb: Option<Aabb<3>> = None;

for shell in self.shells() {
Expand Down
2 changes: 1 addition & 1 deletion crates/fj/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Instance {
pub fn process_model<M>(&mut self, model: &M) -> Result
where
for<'r> (&'r M, Tolerance): Triangulate,
M: BoundingVolume<3>,
for<'r> &'r M: BoundingVolume<3>,
{
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer())
Expand Down

0 comments on commit 54eef76

Please sign in to comment.