Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide more complete and convenient transform API #822

Merged
merged 19 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ fn project_line_into_plane(
mod tests {
use fj_math::Transform;

use crate::objects::{Curve, Surface};
use crate::{
algorithms::TransformObject,
objects::{Curve, Surface},
};

use super::surface_surface;

Expand Down
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/algorithms/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ pub use self::{
approx::{CycleApprox, FaceApprox, InvalidTolerance, Tolerance},
reverse::reverse_face,
sweep::sweep,
transform::transform_shape,
transform::{transform_faces, TransformObject},
triangulate::triangulate,
};
11 changes: 5 additions & 6 deletions crates/fj-kernel/src/algorithms/sweep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
},
};

use super::{reverse_face, transform::transform_face, CycleApprox, Tolerance};
use super::{reverse_face, CycleApprox, Tolerance, TransformObject};

/// Create a solid by sweeping a sketch
pub fn sweep(
Expand All @@ -32,7 +32,7 @@ pub fn sweep(
&mut target,
);
create_top_face(
&face,
face.clone(),
path,
is_sweep_along_negative_direction,
&mut target,
Expand All @@ -52,7 +52,7 @@ pub fn sweep(
}

create_continuous_side_face(
edge.clone(),
edge,
path,
tolerance,
color,
Expand Down Expand Up @@ -80,13 +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<Face>,
) {
let translation = Transform::translation(path);
let mut face = transform_face(face, &translation);
let mut face = face.translate(path);

if is_sweep_along_negative_direction {
face = reverse_face(&face);
Expand Down
179 changes: 123 additions & 56 deletions crates/fj-kernel/src/algorithms/transform.rs
Original file line number Diff line number Diff line change
@@ -1,82 +1,149 @@
use fj_math::Transform;
use fj_math::{Transform, Vector};

use crate::{
local::Local,
objects::{
Cycle, CyclesInFace, Edge, Face, FaceBRep, GlobalVertex, Vertex,
Curve, Cycle, CyclesInFace, Edge, Face, FaceBRep, GlobalVertex, Sketch,
Surface, Vertex,
},
};

/// Transform a shape
pub fn transform_shape(faces: &mut Vec<Face>, transform: &Transform) {
for face in faces {
*face = transform_face(face, transform);
/// Transform an object
///
/// # 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 TransformObject: Sized {
/// Transform the object
#[must_use]
fn transform(self, transform: &Transform) -> Self;

/// Translate the object
#[must_use]
fn translate(self, offset: impl Into<Vector<3>>) -> Self {
self.transform(&Transform::translation(offset))
}

/// Rotate the object
#[must_use]
fn rotate(self, axis_angle: impl Into<Vector<3>>) -> Self {
self.transform(&Transform::rotation(axis_angle))
}
}

impl TransformObject 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)),
}
}
}

impl TransformObject for Cycle {
fn transform(mut self, transform: &Transform) -> Self {
for edge in &mut self.edges {
*edge = edge.transform(transform);
}

self
}
}

pub fn transform_face(face: &Face, transform: &Transform) -> Face {
match face {
Face::Face(face) => {
let surface = face.surface.transform(transform);
impl TransformObject for Edge {
fn transform(self, transform: &Transform) -> Self {
let curve = Local::new(
self.curve.local(),
self.curve.global().transform(transform),
);

let exteriors = transform_cycles(&face.exteriors, transform);
let interiors = transform_cycles(&face.interiors, transform);
let vertices = self.vertices.map(|vertex| vertex.transform(transform));

let color = face.color;
Self { curve, vertices }
}
}

Face::Face(FaceBRep {
surface,
exteriors,
interiors,
color,
})
}
Face::Triangles(triangles) => {
let mut target = Vec::new();
impl TransformObject for Face {
fn transform(self, transform: &Transform) -> Self {
match self {
Self::Face(face) => {
let surface = face.surface.transform(transform);

for &(triangle, color) in triangles {
let triangle = transform.transform_triangle(&triangle);
target.push((triangle, color));
let exteriors = transform_cycles(&face.exteriors, transform);
let interiors = transform_cycles(&face.interiors, transform);

let color = face.color;

Self::Face(FaceBRep {
surface,
exteriors,
interiors,
color,
})
}
Self::Triangles(triangles) => {
let mut target = Vec::new();

for (triangle, color) in triangles {
let triangle = transform.transform_triangle(&triangle);
target.push((triangle, color));
}

Face::Triangles(target)
Self::Triangles(target)
}
}
}
}

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();
impl TransformObject for GlobalVertex {
fn transform(self, transform: &Transform) -> Self {
let position = transform.transform_point(&self.position());
Self::from_position(position)
}
}

Cycle { edges }
});
impl TransformObject for Sketch {
fn transform(self, transform: &Transform) -> Self {
let mut faces = self.into_faces();
transform_faces(&mut faces, transform);
Self::from_faces(faces)
}
}

CyclesInFace::new(cycles)
impl TransformObject for Surface {
fn transform(self, transform: &Transform) -> Self {
match self {
Self::SweptCurve(surface) => {
Self::SweptCurve(surface.transform(transform))
}
}
}
}

pub fn transform_vertex(vertex: &Vertex, transform: &Transform) -> Vertex {
let position = transform.transform_point(&vertex.global().position());
let global = GlobalVertex::from_position(position);
impl TransformObject for Vertex {
fn transform(self, transform: &Transform) -> Self {
Self::new(self.position(), self.global().transform(transform))
}
}

/// Transform a shape
pub fn transform_faces(faces: &mut Vec<Face>, transform: &Transform) {
for face in faces {
*face = face.clone().transform(transform);
}
}

Vertex::new(vertex.position(), global)
fn transform_cycles(
cycles: &CyclesInFace,
transform: &Transform,
) -> CyclesInFace {
let cycles = cycles.as_local().map(|cycle| cycle.transform(transform));

CyclesInFace::new(cycles)
}
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl ObjectIters for Edge {
}

fn edge_iter(&self) -> Iter<Edge> {
Iter::from_object(self.clone())
Iter::from_object(*self)
}

fn face_iter(&self) -> Iter<Face> {
Expand Down
13 changes: 1 addition & 12 deletions crates/fj-kernel/src/objects/curve.rs
Original file line number Diff line number Diff line change
@@ -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
///
Expand Down Expand Up @@ -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<const D: usize> fmt::Display for Curve<D> {
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/objects/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down
5 changes: 5 additions & 0 deletions crates/fj-kernel/src/objects/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ impl Sketch {
pub fn faces(&self) -> impl Iterator<Item = &Face> {
self.faces.iter()
}

/// Convert the sketch into a list of faces
pub fn into_faces(self) -> Vec<Face> {
self.faces
}
}
12 changes: 2 additions & 10 deletions crates/fj-kernel/src/objects/surface.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use fj_math::{Line, Point, Transform, Vector};

use crate::algorithms::TransformObject;

use super::Curve;

/// A two-dimensional shape
Expand Down Expand Up @@ -52,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,
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/validation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions crates/fj-math/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ impl Transform {
}

/// Construct a translation
pub fn translation(vector: impl Into<Vector<3>>) -> Self {
let vector = vector.into();
pub fn translation(offset: impl Into<Vector<3>>) -> 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()),
))
}

Expand Down
4 changes: 2 additions & 2 deletions crates/fj-operations/src/transform.rs
Original file line number Diff line number Diff line change
@@ -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},
};
Expand All @@ -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)
}
Expand Down