Skip to content

Commit

Permalink
Merge pull request #2169 from hannobraun/transform
Browse files Browse the repository at this point in the history
Make some cleanups in `transform`, move it to `operations`
  • Loading branch information
hannobraun authored Jan 17, 2024
2 parents 76b902c + b280a9c commit 659c712
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 42 deletions.
9 changes: 3 additions & 6 deletions crates/fj-core/src/algorithms/intersect/ray_face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,14 @@ mod tests {
use fj_math::Point;

use crate::{
algorithms::{
intersect::{
ray_face::RayFaceIntersection, HorizontalRayToTheRight,
Intersect,
},
transform::TransformObject,
algorithms::intersect::{
ray_face::RayFaceIntersection, HorizontalRayToTheRight, Intersect,
},
objects::{Cycle, Face},
operations::{
build::{BuildCycle, BuildFace},
insert::Insert,
transform::TransformObject,
update::{UpdateFace, UpdateRegion},
},
services::Services,
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-core/src/algorithms/intersect/surface_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ mod tests {
use pretty_assertions::assert_eq;

use crate::{
algorithms::transform::TransformObject, geometry::SurfacePath,
geometry::SurfacePath, operations::transform::TransformObject,
services::Services,
};

Expand Down
1 change: 0 additions & 1 deletion crates/fj-core/src/algorithms/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@
pub mod approx;
pub mod bounding_volume;
pub mod intersect;
pub mod transform;
pub mod triangulate;
1 change: 1 addition & 0 deletions crates/fj-core/src/operations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ pub mod replace;
pub mod reverse;
pub mod split;
pub mod sweep;
pub mod transform;
pub mod update;
5 changes: 3 additions & 2 deletions crates/fj-core/src/operations/sweep/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use fj_interop::Color;
use fj_math::Vector;

use crate::{
algorithms::transform::TransformObject,
objects::{Cycle, Face, Region, Surface},
operations::{insert::Insert, reverse::Reverse},
operations::{
insert::Insert, reverse::Reverse, transform::TransformObject,
},
services::Services,
storage::Handle,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{TransformCache, TransformObject};

impl TransformObject for Curve {
fn transform_with_cache(
self,
&self,
_: &Transform,
_: &mut Services,
_: &mut TransformCache,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{TransformCache, TransformObject};

impl TransformObject for Cycle {
fn transform_with_cache(
self,
&self,
transform: &Transform,
services: &mut Services,
cache: &mut TransformCache,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{TransformCache, TransformObject};

impl TransformObject for HalfEdge {
fn transform_with_cache(
self,
&self,
transform: &Transform,
services: &mut Services,
cache: &mut TransformCache,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,24 @@
use fj_math::Transform;

use crate::{
objects::{Face, Region},
operations::insert::Insert,
services::Services,
};
use crate::{objects::Face, services::Services};

use super::{TransformCache, TransformObject};

impl TransformObject for Face {
fn transform_with_cache(
self,
&self,
transform: &Transform,
services: &mut Services,
cache: &mut TransformCache,
) -> Self {
// Color does not need to be transformed.
let color = self.region().color();

let surface = self
.surface()
.clone()
.transform_with_cache(transform, services, cache);
let exterior = self
let region = self
.region()
.exterior()
.clone()
.transform_with_cache(transform, services, cache);
let interiors =
self.region().interiors().iter().cloned().map(|interior| {
interior.transform_with_cache(transform, services, cache)
});

let region = Region::new(exterior, interiors, color).insert(services);

Self::new(surface, region)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod curve;
mod cycle;
mod edge;
mod face;
mod region;
mod shell;
mod solid;
mod surface;
Expand Down Expand Up @@ -31,14 +32,18 @@ use crate::{
/// hasn't been done so far, is that no one has put in the work yet.
pub trait TransformObject: Sized {
/// Transform the object
fn transform(self, transform: &Transform, services: &mut Services) -> Self {
fn transform(
&self,
transform: &Transform,
services: &mut Services,
) -> Self {
let mut cache = TransformCache::default();
self.transform_with_cache(transform, services, &mut cache)
}

/// Transform the object using the provided cache
fn transform_with_cache(
self,
&self,
transform: &Transform,
services: &mut Services,
cache: &mut TransformCache,
Expand All @@ -48,7 +53,7 @@ pub trait TransformObject: Sized {
///
/// Convenience wrapper around [`TransformObject::transform`].
fn translate(
self,
&self,
offset: impl Into<Vector<3>>,
services: &mut Services,
) -> Self {
Expand All @@ -59,7 +64,7 @@ pub trait TransformObject: Sized {
///
/// Convenience wrapper around [`TransformObject::transform`].
fn rotate(
self,
&self,
axis_angle: impl Into<Vector<3>>,
services: &mut Services,
) -> Self {
Expand All @@ -72,12 +77,12 @@ where
T: Clone + Insert<Inserted = Handle<T>> + TransformObject + 'static,
{
fn transform_with_cache(
self,
&self,
transform: &Transform,
services: &mut Services,
cache: &mut TransformCache,
) -> Self {
if let Some(object) = cache.get(&self) {
if let Some(object) = cache.get(self) {
return object.clone();
}

Expand Down
25 changes: 25 additions & 0 deletions crates/fj-core/src/operations/transform/region.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::objects::Region;

use super::TransformObject;

impl TransformObject for Region {
fn transform_with_cache(
&self,
transform: &fj_math::Transform,
services: &mut crate::services::Services,
cache: &mut super::TransformCache,
) -> Self {
// Color does not need to be transformed.
let color = self.color();

let exterior = self
.exterior()
.clone()
.transform_with_cache(transform, services, cache);
let interiors = self.interiors().iter().cloned().map(|interior| {
interior.transform_with_cache(transform, services, cache)
});

Region::new(exterior, interiors, color)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{TransformCache, TransformObject};

impl TransformObject for Shell {
fn transform_with_cache(
self,
&self,
transform: &Transform,
services: &mut Services,
cache: &mut TransformCache,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{TransformCache, TransformObject};

impl TransformObject for Solid {
fn transform_with_cache(
self,
&self,
transform: &Transform,
services: &mut Services,
cache: &mut TransformCache,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{TransformCache, TransformObject};

impl TransformObject for Surface {
fn transform_with_cache(
self,
&self,
transform: &Transform,
_: &mut Services,
_: &mut TransformCache,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{TransformCache, TransformObject};

impl TransformObject for Vertex {
fn transform_with_cache(
self,
&self,
_: &Transform,
_: &mut Services,
_: &mut TransformCache,
Expand Down
5 changes: 3 additions & 2 deletions models/all/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use fj::{
core::{
algorithms::transform::TransformObject,
objects::Solid,
operations::{insert::Insert, merge::Merge},
operations::{
insert::Insert, merge::Merge, transform::TransformObject,
},
services::Services,
storage::Handle,
},
Expand Down

0 comments on commit 659c712

Please sign in to comment.