From 1ec41db825768008ef6841236f9ef214f5877de9 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 15 Jul 2022 11:27:22 +0200 Subject: [PATCH 01/19] Make argument name more specific --- crates/fj-math/src/transform.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/fj-math/src/transform.rs b/crates/fj-math/src/transform.rs index dc089e930..7da9ba089 100644 --- a/crates/fj-math/src/transform.rs +++ b/crates/fj-math/src/transform.rs @@ -18,11 +18,11 @@ impl Transform { } /// Construct a translation - pub fn translation(vector: impl Into>) -> Self { - let vector = vector.into(); + pub fn translation(offset: impl Into>) -> Self { + let offset = offset.into(); Self(nalgebra::Transform::from_matrix_unchecked( - nalgebra::OMatrix::new_translation(&vector.to_na()), + nalgebra::OMatrix::new_translation(&offset.to_na()), )) } From 4f1a1808c54198bcd814dd34c6e91f70e30260ef Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 15 Jul 2022 11:30:45 +0200 Subject: [PATCH 02/19] Add `TransformExt` This is a new, more convenient API for transforming objects. The trait is not implemented for any objects yet. I will do this over the next few commits. --- crates/fj-kernel/src/algorithms/mod.rs | 2 +- crates/fj-kernel/src/algorithms/transform.rs | 29 +++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/mod.rs b/crates/fj-kernel/src/algorithms/mod.rs index ce99e9b3b..ba69b3cfe 100644 --- a/crates/fj-kernel/src/algorithms/mod.rs +++ b/crates/fj-kernel/src/algorithms/mod.rs @@ -15,6 +15,6 @@ pub use self::{ approx::{CycleApprox, FaceApprox, InvalidTolerance, Tolerance}, reverse::reverse_face, sweep::sweep, - transform::transform_shape, + transform::{transform_shape, TransformExt}, triangulate::triangulate, }; diff --git a/crates/fj-kernel/src/algorithms/transform.rs b/crates/fj-kernel/src/algorithms/transform.rs index 41a9dd28d..9ae7de4e5 100644 --- a/crates/fj-kernel/src/algorithms/transform.rs +++ b/crates/fj-kernel/src/algorithms/transform.rs @@ -1,4 +1,4 @@ -use fj_math::Transform; +use fj_math::{Transform, Vector}; use crate::{ local::Local, @@ -7,6 +7,33 @@ use crate::{ }, }; +/// Extension trait that provides transformation methods for objects +/// +/// # Implementation Note +/// +/// So far, a general `transform` method is available, along some convenience +/// methods for more specific transformations. +/// +/// More convenience methods can be added as required. The only reason this +/// hasn't been done so far, is that no one has put in the work yet. +pub trait TransformExt: Sized { + /// Transform the object + #[must_use] + fn transform(self, transform: &Transform) -> Self; + + /// Translate the object + #[must_use] + fn translate(self, offset: impl Into>) -> Self { + self.transform(&Transform::translation(offset)) + } + + /// Rotate the object + #[must_use] + fn rotate(self, axis_angle: impl Into>) -> Self { + self.transform(&Transform::rotation(axis_angle)) + } +} + /// Transform a shape pub fn transform_shape(faces: &mut Vec, transform: &Transform) { for face in faces { From 52a4daf6082cb6620d86819e9fa7c9d017ed49f3 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 15 Jul 2022 11:35:45 +0200 Subject: [PATCH 03/19] Move `Curve` transform code to `TransformExt` --- crates/fj-kernel/src/algorithms/transform.rs | 13 ++++++++++++- crates/fj-kernel/src/objects/curve.rs | 13 +------------ crates/fj-kernel/src/objects/surface.rs | 2 ++ 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/transform.rs b/crates/fj-kernel/src/algorithms/transform.rs index 9ae7de4e5..f46f881dc 100644 --- a/crates/fj-kernel/src/algorithms/transform.rs +++ b/crates/fj-kernel/src/algorithms/transform.rs @@ -3,7 +3,7 @@ use fj_math::{Transform, Vector}; use crate::{ local::Local, objects::{ - Cycle, CyclesInFace, Edge, Face, FaceBRep, GlobalVertex, Vertex, + Curve, Cycle, CyclesInFace, Edge, Face, FaceBRep, GlobalVertex, Vertex, }, }; @@ -34,6 +34,17 @@ pub trait TransformExt: Sized { } } +impl TransformExt for Curve<3> { + fn transform(self, transform: &Transform) -> Self { + match self { + Self::Circle(curve) => { + Self::Circle(transform.transform_circle(&curve)) + } + Self::Line(curve) => Self::Line(transform.transform_line(&curve)), + } + } +} + /// Transform a shape pub fn transform_shape(faces: &mut Vec, transform: &Transform) { for face in faces { diff --git a/crates/fj-kernel/src/objects/curve.rs b/crates/fj-kernel/src/objects/curve.rs index 848c7a3ee..e808dd039 100644 --- a/crates/fj-kernel/src/objects/curve.rs +++ b/crates/fj-kernel/src/objects/curve.rs @@ -1,6 +1,6 @@ use std::fmt; -use fj_math::{Circle, Line, Point, Transform, Vector}; +use fj_math::{Circle, Line, Point, Vector}; /// A one-dimensional shape /// @@ -112,17 +112,6 @@ impl Curve<3> { direction: Vector::unit_z(), }) } - - /// Create a new instance that is transformed by `transform` - #[must_use] - pub fn transform(self, transform: &Transform) -> Self { - match self { - Self::Circle(curve) => { - Self::Circle(transform.transform_circle(&curve)) - } - Self::Line(curve) => Self::Line(transform.transform_line(&curve)), - } - } } impl fmt::Display for Curve { diff --git a/crates/fj-kernel/src/objects/surface.rs b/crates/fj-kernel/src/objects/surface.rs index 6fa770fd3..363b4691a 100644 --- a/crates/fj-kernel/src/objects/surface.rs +++ b/crates/fj-kernel/src/objects/surface.rs @@ -1,5 +1,7 @@ use fj_math::{Line, Point, Transform, Vector}; +use crate::algorithms::TransformExt; + use super::Curve; /// A two-dimensional shape From 3c50f099adf897d466b6c80b688871c065dbb418 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 15 Jul 2022 11:37:56 +0200 Subject: [PATCH 04/19] Implement `TransformExt` for `Cycle` --- crates/fj-kernel/src/algorithms/transform.rs | 47 +++++++++++--------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/transform.rs b/crates/fj-kernel/src/algorithms/transform.rs index f46f881dc..1351bbb2a 100644 --- a/crates/fj-kernel/src/algorithms/transform.rs +++ b/crates/fj-kernel/src/algorithms/transform.rs @@ -45,6 +45,30 @@ impl TransformExt for Curve<3> { } } +impl TransformExt for Cycle { + fn transform(self, transform: &Transform) -> Self { + let edges = self + .edges + .into_iter() + .map(|edge| { + let curve_local = edge.curve.local(); + let curve_canonical = edge.curve().transform(transform); + + let vertices = edge + .vertices + .map(|vertex| transform_vertex(&vertex, transform)); + + Edge { + curve: Local::new(curve_local, curve_canonical), + vertices, + } + }) + .collect(); + + Self { edges } + } +} + /// Transform a shape pub fn transform_shape(faces: &mut Vec, transform: &Transform) { for face in faces { @@ -86,28 +110,7 @@ pub fn transform_cycles( cycles: &CyclesInFace, transform: &Transform, ) -> CyclesInFace { - let cycles = cycles.as_local().map(|cycle| { - let edges = cycle - .edges - .iter() - .map(|edge| { - let curve_local = edge.curve.local(); - let curve_canonical = edge.curve().transform(transform); - - let vertices = edge - .clone() - .vertices - .map(|vertex| transform_vertex(&vertex, transform)); - - Edge { - curve: Local::new(curve_local, curve_canonical), - vertices, - } - }) - .collect(); - - Cycle { edges } - }); + let cycles = cycles.as_local().map(|cycle| cycle.transform(transform)); CyclesInFace::new(cycles) } From 7d8f49e4f29683b585d1c39a3ae39376bfc5fe8c Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 15 Jul 2022 11:42:25 +0200 Subject: [PATCH 05/19] Implement `TransformExt` for `Edge` --- crates/fj-kernel/src/algorithms/transform.rs | 29 +++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/transform.rs b/crates/fj-kernel/src/algorithms/transform.rs index 1351bbb2a..1ead2e75d 100644 --- a/crates/fj-kernel/src/algorithms/transform.rs +++ b/crates/fj-kernel/src/algorithms/transform.rs @@ -50,25 +50,28 @@ impl TransformExt for Cycle { let edges = self .edges .into_iter() - .map(|edge| { - let curve_local = edge.curve.local(); - let curve_canonical = edge.curve().transform(transform); - - let vertices = edge - .vertices - .map(|vertex| transform_vertex(&vertex, transform)); - - Edge { - curve: Local::new(curve_local, curve_canonical), - vertices, - } - }) + .map(|edge| edge.transform(transform)) .collect(); Self { edges } } } +impl TransformExt for Edge { + fn transform(self, transform: &Transform) -> Self { + let curve = Local::new( + self.curve.local(), + self.curve.global().transform(transform), + ); + + let vertices = self + .vertices + .map(|vertex| transform_vertex(&vertex, transform)); + + Self { curve, vertices } + } +} + /// Transform a shape pub fn transform_shape(faces: &mut Vec, transform: &Transform) { for face in faces { From 3b39a3f05461c114a5daa12ccef73b64f5ee126d Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 15 Jul 2022 11:44:58 +0200 Subject: [PATCH 06/19] Derive `Copy` for `Edge` --- crates/fj-kernel/src/algorithms/sweep.rs | 2 +- crates/fj-kernel/src/iter.rs | 2 +- crates/fj-kernel/src/objects/edge.rs | 2 +- crates/fj-kernel/src/validation/mod.rs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/sweep.rs b/crates/fj-kernel/src/algorithms/sweep.rs index 02fb0a400..76262e20c 100644 --- a/crates/fj-kernel/src/algorithms/sweep.rs +++ b/crates/fj-kernel/src/algorithms/sweep.rs @@ -52,7 +52,7 @@ pub fn sweep( } create_continuous_side_face( - edge.clone(), + edge, path, tolerance, color, diff --git a/crates/fj-kernel/src/iter.rs b/crates/fj-kernel/src/iter.rs index cbf7c1ed0..1b9c73e19 100644 --- a/crates/fj-kernel/src/iter.rs +++ b/crates/fj-kernel/src/iter.rs @@ -168,7 +168,7 @@ impl ObjectIters for Edge { } fn edge_iter(&self) -> Iter { - Iter::from_object(self.clone()) + Iter::from_object(*self) } fn face_iter(&self) -> Iter { diff --git a/crates/fj-kernel/src/objects/edge.rs b/crates/fj-kernel/src/objects/edge.rs index f05832c90..2431160ef 100644 --- a/crates/fj-kernel/src/objects/edge.rs +++ b/crates/fj-kernel/src/objects/edge.rs @@ -7,7 +7,7 @@ use crate::local::Local; use super::{Curve, GlobalVertex, Surface, Vertex}; /// An edge of a shape -#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)] +#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)] pub struct Edge { /// Access the curve that defines the edge's geometry /// diff --git a/crates/fj-kernel/src/validation/mod.rs b/crates/fj-kernel/src/validation/mod.rs index 506188bea..be232e360 100644 --- a/crates/fj-kernel/src/validation/mod.rs +++ b/crates/fj-kernel/src/validation/mod.rs @@ -158,7 +158,7 @@ mod tests { let edge = Edge { curve, vertices }; let result = validate( - edge.clone(), + edge, &ValidationConfig { identical_max_distance: deviation * 2., ..ValidationConfig::default() From 9616e2d43dc027494ee9c12790983e9aba1f9faf Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 15 Jul 2022 11:45:55 +0200 Subject: [PATCH 07/19] Save allocation --- crates/fj-kernel/src/algorithms/transform.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/transform.rs b/crates/fj-kernel/src/algorithms/transform.rs index 1ead2e75d..aed78663a 100644 --- a/crates/fj-kernel/src/algorithms/transform.rs +++ b/crates/fj-kernel/src/algorithms/transform.rs @@ -46,14 +46,12 @@ impl TransformExt for Curve<3> { } impl TransformExt for Cycle { - fn transform(self, transform: &Transform) -> Self { - let edges = self - .edges - .into_iter() - .map(|edge| edge.transform(transform)) - .collect(); + fn transform(mut self, transform: &Transform) -> Self { + for edge in &mut self.edges { + *edge = edge.transform(transform); + } - Self { edges } + self } } From 4ec8daee383757dad7f2374020c8b9464ed2b8cb Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 15 Jul 2022 11:49:46 +0200 Subject: [PATCH 08/19] Implement `TransformExt` for `Face` --- crates/fj-kernel/src/algorithms/sweep.rs | 4 +- crates/fj-kernel/src/algorithms/transform.rs | 56 ++++++++++---------- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/sweep.rs b/crates/fj-kernel/src/algorithms/sweep.rs index 76262e20c..e31909a6a 100644 --- a/crates/fj-kernel/src/algorithms/sweep.rs +++ b/crates/fj-kernel/src/algorithms/sweep.rs @@ -9,7 +9,7 @@ use crate::{ }, }; -use super::{reverse_face, transform::transform_face, CycleApprox, Tolerance}; +use super::{reverse_face, CycleApprox, Tolerance, TransformExt}; /// Create a solid by sweeping a sketch pub fn sweep( @@ -86,7 +86,7 @@ fn create_top_face( target: &mut Vec, ) { let translation = Transform::translation(path); - let mut face = transform_face(face, &translation); + let mut face = face.clone().transform(&translation); if is_sweep_along_negative_direction { face = reverse_face(&face); diff --git a/crates/fj-kernel/src/algorithms/transform.rs b/crates/fj-kernel/src/algorithms/transform.rs index aed78663a..f97603bcc 100644 --- a/crates/fj-kernel/src/algorithms/transform.rs +++ b/crates/fj-kernel/src/algorithms/transform.rs @@ -70,43 +70,45 @@ impl TransformExt for Edge { } } -/// Transform a shape -pub fn transform_shape(faces: &mut Vec, transform: &Transform) { - for face in faces { - *face = transform_face(face, transform); - } -} +impl TransformExt for Face { + fn transform(self, transform: &Transform) -> Self { + match self { + Self::Face(face) => { + let surface = face.surface.transform(transform); -pub fn transform_face(face: &Face, transform: &Transform) -> Face { - match face { - Face::Face(face) => { - let surface = face.surface.transform(transform); + let exteriors = transform_cycles(&face.exteriors, transform); + let interiors = transform_cycles(&face.interiors, transform); - let exteriors = transform_cycles(&face.exteriors, transform); - let interiors = transform_cycles(&face.interiors, transform); + let color = face.color; - let color = face.color; + Self::Face(FaceBRep { + surface, + exteriors, + interiors, + color, + }) + } + Self::Triangles(triangles) => { + let mut target = Vec::new(); - Face::Face(FaceBRep { - surface, - exteriors, - interiors, - color, - }) - } - Face::Triangles(triangles) => { - let mut target = Vec::new(); + for (triangle, color) in triangles { + let triangle = transform.transform_triangle(&triangle); + target.push((triangle, color)); + } - for &(triangle, color) in triangles { - let triangle = transform.transform_triangle(&triangle); - target.push((triangle, color)); + Self::Triangles(target) } - - Face::Triangles(target) } } } +/// Transform a shape +pub fn transform_shape(faces: &mut Vec, transform: &Transform) { + for face in faces { + *face = face.clone().transform(transform); + } +} + pub fn transform_cycles( cycles: &CyclesInFace, transform: &Transform, From 24b8a60a80447661795a9cf2f45ec6ab0cf3111c Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 15 Jul 2022 11:51:16 +0200 Subject: [PATCH 09/19] Refactor --- crates/fj-kernel/src/algorithms/sweep.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/sweep.rs b/crates/fj-kernel/src/algorithms/sweep.rs index e31909a6a..c184ce3f8 100644 --- a/crates/fj-kernel/src/algorithms/sweep.rs +++ b/crates/fj-kernel/src/algorithms/sweep.rs @@ -85,8 +85,7 @@ fn create_top_face( is_sweep_along_negative_direction: bool, target: &mut Vec, ) { - let translation = Transform::translation(path); - let mut face = face.clone().transform(&translation); + let mut face = face.clone().translate(path); if is_sweep_along_negative_direction { face = reverse_face(&face); From d71e32e14e01b3ab07f67f405f915548cdbde1b3 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 15 Jul 2022 11:56:12 +0200 Subject: [PATCH 10/19] Move clone that was hidden inside function outside --- crates/fj-kernel/src/algorithms/sweep.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/sweep.rs b/crates/fj-kernel/src/algorithms/sweep.rs index c184ce3f8..3c33c1ad1 100644 --- a/crates/fj-kernel/src/algorithms/sweep.rs +++ b/crates/fj-kernel/src/algorithms/sweep.rs @@ -32,7 +32,7 @@ pub fn sweep( &mut target, ); create_top_face( - &face, + face.clone(), path, is_sweep_along_negative_direction, &mut target, @@ -80,12 +80,12 @@ fn create_bottom_faces( } fn create_top_face( - face: &Face, + face: Face, path: Vector<3>, is_sweep_along_negative_direction: bool, target: &mut Vec, ) { - let mut face = face.clone().translate(path); + let mut face = face.translate(path); if is_sweep_along_negative_direction { face = reverse_face(&face); From 0c400cb2dfd28828b4845b8f6fd7ff76e91a9c4d Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 15 Jul 2022 11:58:36 +0200 Subject: [PATCH 11/19] Implement `TransformExt` for `GlobalVertex` --- crates/fj-kernel/src/algorithms/transform.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/transform.rs b/crates/fj-kernel/src/algorithms/transform.rs index f97603bcc..bb842fad4 100644 --- a/crates/fj-kernel/src/algorithms/transform.rs +++ b/crates/fj-kernel/src/algorithms/transform.rs @@ -102,6 +102,13 @@ impl TransformExt for Face { } } +impl TransformExt for GlobalVertex { + fn transform(self, transform: &Transform) -> Self { + let position = transform.transform_point(&self.position()); + Self::from_position(position) + } +} + /// Transform a shape pub fn transform_shape(faces: &mut Vec, transform: &Transform) { for face in faces { @@ -119,8 +126,5 @@ pub fn transform_cycles( } pub fn transform_vertex(vertex: &Vertex, transform: &Transform) -> Vertex { - let position = transform.transform_point(&vertex.global().position()); - let global = GlobalVertex::from_position(position); - - Vertex::new(vertex.position(), global) + Vertex::new(vertex.position(), vertex.global().transform(transform)) } From 52fcdca4f734bfe024fefd386f254b605e6bab8a Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 15 Jul 2022 12:01:39 +0200 Subject: [PATCH 12/19] Add `Sketch::into_faces` --- crates/fj-kernel/src/objects/sketch.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/fj-kernel/src/objects/sketch.rs b/crates/fj-kernel/src/objects/sketch.rs index 0fc182a35..5fd1ac522 100644 --- a/crates/fj-kernel/src/objects/sketch.rs +++ b/crates/fj-kernel/src/objects/sketch.rs @@ -22,4 +22,9 @@ impl Sketch { pub fn faces(&self) -> impl Iterator { self.faces.iter() } + + /// Convert the sketch into a list of faces + pub fn into_faces(self) -> Vec { + self.faces + } } From 3aa3bafc65c145976ee398b8285e0d49af7b1391 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 15 Jul 2022 12:01:53 +0200 Subject: [PATCH 13/19] Implement `TransformExt` for `Sketch` --- crates/fj-kernel/src/algorithms/transform.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/fj-kernel/src/algorithms/transform.rs b/crates/fj-kernel/src/algorithms/transform.rs index bb842fad4..634478639 100644 --- a/crates/fj-kernel/src/algorithms/transform.rs +++ b/crates/fj-kernel/src/algorithms/transform.rs @@ -3,7 +3,8 @@ use fj_math::{Transform, Vector}; use crate::{ local::Local, objects::{ - Curve, Cycle, CyclesInFace, Edge, Face, FaceBRep, GlobalVertex, Vertex, + Curve, Cycle, CyclesInFace, Edge, Face, FaceBRep, GlobalVertex, Sketch, + Vertex, }, }; @@ -109,6 +110,14 @@ impl TransformExt for GlobalVertex { } } +impl TransformExt for Sketch { + fn transform(self, transform: &Transform) -> Self { + let mut faces = self.into_faces(); + transform_shape(&mut faces, transform); + Self::from_faces(faces) + } +} + /// Transform a shape pub fn transform_shape(faces: &mut Vec, transform: &Transform) { for face in faces { From 4623c4d4780bde46be77c56e7fffb923490a5bc4 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 15 Jul 2022 12:04:07 +0200 Subject: [PATCH 14/19] Move `Surface` transform code to `TransformExt` --- .../src/algorithms/intersection/surface_surface.rs | 5 ++++- crates/fj-kernel/src/algorithms/transform.rs | 12 +++++++++++- crates/fj-kernel/src/objects/surface.rs | 10 ---------- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/intersection/surface_surface.rs b/crates/fj-kernel/src/algorithms/intersection/surface_surface.rs index c64e4351b..6e02b4cb9 100644 --- a/crates/fj-kernel/src/algorithms/intersection/surface_surface.rs +++ b/crates/fj-kernel/src/algorithms/intersection/surface_surface.rs @@ -130,7 +130,10 @@ fn project_line_into_plane( mod tests { use fj_math::Transform; - use crate::objects::{Curve, Surface}; + use crate::{ + algorithms::TransformExt, + objects::{Curve, Surface}, + }; use super::surface_surface; diff --git a/crates/fj-kernel/src/algorithms/transform.rs b/crates/fj-kernel/src/algorithms/transform.rs index 634478639..74354af58 100644 --- a/crates/fj-kernel/src/algorithms/transform.rs +++ b/crates/fj-kernel/src/algorithms/transform.rs @@ -4,7 +4,7 @@ use crate::{ local::Local, objects::{ Curve, Cycle, CyclesInFace, Edge, Face, FaceBRep, GlobalVertex, Sketch, - Vertex, + Surface, Vertex, }, }; @@ -118,6 +118,16 @@ impl TransformExt for Sketch { } } +impl TransformExt for Surface { + fn transform(self, transform: &Transform) -> Self { + match self { + Self::SweptCurve(surface) => { + Self::SweptCurve(surface.transform(transform)) + } + } + } +} + /// Transform a shape pub fn transform_shape(faces: &mut Vec, transform: &Transform) { for face in faces { diff --git a/crates/fj-kernel/src/objects/surface.rs b/crates/fj-kernel/src/objects/surface.rs index 363b4691a..bfd0ab0d0 100644 --- a/crates/fj-kernel/src/objects/surface.rs +++ b/crates/fj-kernel/src/objects/surface.rs @@ -54,16 +54,6 @@ impl Surface { } } - /// Transform the surface - #[must_use] - pub fn transform(self, transform: &Transform) -> Self { - match self { - Self::SweptCurve(surface) => { - Self::SweptCurve(surface.transform(transform)) - } - } - } - /// Convert a point in surface coordinates to model coordinates pub fn point_from_surface_coords( &self, From d17a1e092db94acca5c9752f72f9f2ace8cd225b Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 15 Jul 2022 12:07:02 +0200 Subject: [PATCH 15/19] Implement `TransformExt` for `Vertex` --- crates/fj-kernel/src/algorithms/transform.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/transform.rs b/crates/fj-kernel/src/algorithms/transform.rs index 74354af58..d188d1ce0 100644 --- a/crates/fj-kernel/src/algorithms/transform.rs +++ b/crates/fj-kernel/src/algorithms/transform.rs @@ -63,9 +63,7 @@ impl TransformExt for Edge { self.curve.global().transform(transform), ); - let vertices = self - .vertices - .map(|vertex| transform_vertex(&vertex, transform)); + let vertices = self.vertices.map(|vertex| vertex.transform(transform)); Self { curve, vertices } } @@ -128,6 +126,12 @@ impl TransformExt for Surface { } } +impl TransformExt for Vertex { + fn transform(self, transform: &Transform) -> Self { + Self::new(self.position(), self.global().transform(transform)) + } +} + /// Transform a shape pub fn transform_shape(faces: &mut Vec, transform: &Transform) { for face in faces { @@ -143,7 +147,3 @@ pub fn transform_cycles( CyclesInFace::new(cycles) } - -pub fn transform_vertex(vertex: &Vertex, transform: &Transform) -> Vertex { - Vertex::new(vertex.position(), vertex.global().transform(transform)) -} From a113216338adf0e64a1181b65e6c36df2333e043 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 15 Jul 2022 12:07:48 +0200 Subject: [PATCH 16/19] Remove unnecessary `pub` --- crates/fj-kernel/src/algorithms/transform.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/fj-kernel/src/algorithms/transform.rs b/crates/fj-kernel/src/algorithms/transform.rs index d188d1ce0..2cf5465c5 100644 --- a/crates/fj-kernel/src/algorithms/transform.rs +++ b/crates/fj-kernel/src/algorithms/transform.rs @@ -139,7 +139,7 @@ pub fn transform_shape(faces: &mut Vec, transform: &Transform) { } } -pub fn transform_cycles( +fn transform_cycles( cycles: &CyclesInFace, transform: &Transform, ) -> CyclesInFace { From e99a8bf33577373418824471c6c4a998179970b4 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 15 Jul 2022 12:08:30 +0200 Subject: [PATCH 17/19] Make function name more explicit --- crates/fj-kernel/src/algorithms/mod.rs | 2 +- crates/fj-kernel/src/algorithms/transform.rs | 4 ++-- crates/fj-operations/src/transform.rs | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/mod.rs b/crates/fj-kernel/src/algorithms/mod.rs index ba69b3cfe..4d11381cd 100644 --- a/crates/fj-kernel/src/algorithms/mod.rs +++ b/crates/fj-kernel/src/algorithms/mod.rs @@ -15,6 +15,6 @@ pub use self::{ approx::{CycleApprox, FaceApprox, InvalidTolerance, Tolerance}, reverse::reverse_face, sweep::sweep, - transform::{transform_shape, TransformExt}, + transform::{transform_faces, TransformExt}, triangulate::triangulate, }; diff --git a/crates/fj-kernel/src/algorithms/transform.rs b/crates/fj-kernel/src/algorithms/transform.rs index 2cf5465c5..66fc33336 100644 --- a/crates/fj-kernel/src/algorithms/transform.rs +++ b/crates/fj-kernel/src/algorithms/transform.rs @@ -111,7 +111,7 @@ impl TransformExt for GlobalVertex { impl TransformExt for Sketch { fn transform(self, transform: &Transform) -> Self { let mut faces = self.into_faces(); - transform_shape(&mut faces, transform); + transform_faces(&mut faces, transform); Self::from_faces(faces) } } @@ -133,7 +133,7 @@ impl TransformExt for Vertex { } /// Transform a shape -pub fn transform_shape(faces: &mut Vec, transform: &Transform) { +pub fn transform_faces(faces: &mut Vec, transform: &Transform) { for face in faces { *face = face.clone().transform(transform); } diff --git a/crates/fj-operations/src/transform.rs b/crates/fj-operations/src/transform.rs index f3b74fbd6..53b09f4ff 100644 --- a/crates/fj-operations/src/transform.rs +++ b/crates/fj-operations/src/transform.rs @@ -1,6 +1,6 @@ use fj_interop::debug::DebugInfo; use fj_kernel::{ - algorithms::{transform_shape, Tolerance}, + algorithms::{transform_faces, Tolerance}, objects::Face, validation::{validate, Validated, ValidationConfig, ValidationError}, }; @@ -20,7 +20,7 @@ impl Shape for fj::Transform { .compute_brep(config, tolerance, debug_info)? .into_inner(); - transform_shape(&mut shape, &make_transform(self)); + transform_faces(&mut shape, &make_transform(self)); validate(shape, config) } From 130d00e03c9e5309ee5befb52dabe4f420ceacc0 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 15 Jul 2022 12:10:00 +0200 Subject: [PATCH 18/19] Rename `TransformExt` to `TransformObject` --- .../algorithms/intersection/surface_surface.rs | 2 +- crates/fj-kernel/src/algorithms/mod.rs | 2 +- crates/fj-kernel/src/algorithms/sweep.rs | 2 +- crates/fj-kernel/src/algorithms/transform.rs | 18 +++++++++--------- crates/fj-kernel/src/objects/surface.rs | 2 +- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/intersection/surface_surface.rs b/crates/fj-kernel/src/algorithms/intersection/surface_surface.rs index 6e02b4cb9..430da8ea7 100644 --- a/crates/fj-kernel/src/algorithms/intersection/surface_surface.rs +++ b/crates/fj-kernel/src/algorithms/intersection/surface_surface.rs @@ -131,7 +131,7 @@ mod tests { use fj_math::Transform; use crate::{ - algorithms::TransformExt, + algorithms::TransformObject, objects::{Curve, Surface}, }; diff --git a/crates/fj-kernel/src/algorithms/mod.rs b/crates/fj-kernel/src/algorithms/mod.rs index 4d11381cd..402d5ab9f 100644 --- a/crates/fj-kernel/src/algorithms/mod.rs +++ b/crates/fj-kernel/src/algorithms/mod.rs @@ -15,6 +15,6 @@ pub use self::{ approx::{CycleApprox, FaceApprox, InvalidTolerance, Tolerance}, reverse::reverse_face, sweep::sweep, - transform::{transform_faces, TransformExt}, + transform::{transform_faces, TransformObject}, triangulate::triangulate, }; diff --git a/crates/fj-kernel/src/algorithms/sweep.rs b/crates/fj-kernel/src/algorithms/sweep.rs index 3c33c1ad1..0f5c4b108 100644 --- a/crates/fj-kernel/src/algorithms/sweep.rs +++ b/crates/fj-kernel/src/algorithms/sweep.rs @@ -9,7 +9,7 @@ use crate::{ }, }; -use super::{reverse_face, CycleApprox, Tolerance, TransformExt}; +use super::{reverse_face, CycleApprox, Tolerance, TransformObject}; /// Create a solid by sweeping a sketch pub fn sweep( diff --git a/crates/fj-kernel/src/algorithms/transform.rs b/crates/fj-kernel/src/algorithms/transform.rs index 66fc33336..a908de602 100644 --- a/crates/fj-kernel/src/algorithms/transform.rs +++ b/crates/fj-kernel/src/algorithms/transform.rs @@ -17,7 +17,7 @@ use crate::{ /// /// More convenience methods can be added as required. The only reason this /// hasn't been done so far, is that no one has put in the work yet. -pub trait TransformExt: Sized { +pub trait TransformObject: Sized { /// Transform the object #[must_use] fn transform(self, transform: &Transform) -> Self; @@ -35,7 +35,7 @@ pub trait TransformExt: Sized { } } -impl TransformExt for Curve<3> { +impl TransformObject for Curve<3> { fn transform(self, transform: &Transform) -> Self { match self { Self::Circle(curve) => { @@ -46,7 +46,7 @@ impl TransformExt for Curve<3> { } } -impl TransformExt for Cycle { +impl TransformObject for Cycle { fn transform(mut self, transform: &Transform) -> Self { for edge in &mut self.edges { *edge = edge.transform(transform); @@ -56,7 +56,7 @@ impl TransformExt for Cycle { } } -impl TransformExt for Edge { +impl TransformObject for Edge { fn transform(self, transform: &Transform) -> Self { let curve = Local::new( self.curve.local(), @@ -69,7 +69,7 @@ impl TransformExt for Edge { } } -impl TransformExt for Face { +impl TransformObject for Face { fn transform(self, transform: &Transform) -> Self { match self { Self::Face(face) => { @@ -101,14 +101,14 @@ impl TransformExt for Face { } } -impl TransformExt for GlobalVertex { +impl TransformObject for GlobalVertex { fn transform(self, transform: &Transform) -> Self { let position = transform.transform_point(&self.position()); Self::from_position(position) } } -impl TransformExt for Sketch { +impl TransformObject for Sketch { fn transform(self, transform: &Transform) -> Self { let mut faces = self.into_faces(); transform_faces(&mut faces, transform); @@ -116,7 +116,7 @@ impl TransformExt for Sketch { } } -impl TransformExt for Surface { +impl TransformObject for Surface { fn transform(self, transform: &Transform) -> Self { match self { Self::SweptCurve(surface) => { @@ -126,7 +126,7 @@ impl TransformExt for Surface { } } -impl TransformExt for Vertex { +impl TransformObject for Vertex { fn transform(self, transform: &Transform) -> Self { Self::new(self.position(), self.global().transform(transform)) } diff --git a/crates/fj-kernel/src/objects/surface.rs b/crates/fj-kernel/src/objects/surface.rs index bfd0ab0d0..a6e33252e 100644 --- a/crates/fj-kernel/src/objects/surface.rs +++ b/crates/fj-kernel/src/objects/surface.rs @@ -1,6 +1,6 @@ use fj_math::{Line, Point, Transform, Vector}; -use crate::algorithms::TransformExt; +use crate::algorithms::TransformObject; use super::Curve; From 656175c757f5bcc806c4248da9b0ba01ca632d5a Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 15 Jul 2022 12:10:24 +0200 Subject: [PATCH 19/19] Simplify doc comment --- crates/fj-kernel/src/algorithms/transform.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/fj-kernel/src/algorithms/transform.rs b/crates/fj-kernel/src/algorithms/transform.rs index a908de602..bbcf117a9 100644 --- a/crates/fj-kernel/src/algorithms/transform.rs +++ b/crates/fj-kernel/src/algorithms/transform.rs @@ -8,7 +8,7 @@ use crate::{ }, }; -/// Extension trait that provides transformation methods for objects +/// Transform an object /// /// # Implementation Note ///