Skip to content

Commit

Permalink
Merge pull request #900 from hannobraun/curve
Browse files Browse the repository at this point in the history
Replace `Curve` with `Curve`/`GlobalCurve`/`CurveKind`
  • Loading branch information
hannobraun committed Aug 1, 2022
2 parents 02da55c + e2ed8ae commit 203c5d6
Show file tree
Hide file tree
Showing 19 changed files with 225 additions and 164 deletions.
13 changes: 8 additions & 5 deletions crates/fj-kernel/src/algorithms/approx/curves.rs
Expand Up @@ -2,7 +2,10 @@ use std::cmp::max;

use fj_math::{Circle, Point, Scalar};

use crate::{local::Local, objects::Curve};
use crate::{
local::Local,
objects::{CurveKind, GlobalCurve},
};

use super::Tolerance;

Expand All @@ -21,13 +24,13 @@ use super::Tolerance;
/// The `approximate_between` methods of the curves then need to make sure to
/// only return points in between those vertices, not the vertices themselves.
pub fn approx_curve(
curve: &Curve<3>,
curve: &GlobalCurve,
tolerance: Tolerance,
out: &mut Vec<Local<Point<1>>>,
) {
match curve {
Curve::Circle(curve) => approx_circle(curve, tolerance, out),
Curve::Line(_) => {}
match curve.kind() {
CurveKind::Circle(curve) => approx_circle(curve, tolerance, out),
CurveKind::Line(_) => {}
}
}

Expand Down
8 changes: 2 additions & 6 deletions crates/fj-kernel/src/algorithms/approx/cycles.rs
Expand Up @@ -21,17 +21,13 @@ impl CycleApprox {

for edge in cycle.edges() {
let mut edge_points = Vec::new();
approx_curve(
edge.curve().global_form(),
tolerance,
&mut edge_points,
);
approx_curve(edge.curve().global(), tolerance, &mut edge_points);
approx_edge(*edge.vertices(), &mut edge_points);

points.extend(edge_points.into_iter().map(|point| {
let local = edge
.curve()
.local_form()
.kind()
.point_from_curve_coords(*point.local_form());
Local::new(local, *point.global_form())
}));
Expand Down
24 changes: 12 additions & 12 deletions crates/fj-kernel/src/algorithms/intersection/curve_edge.rs
@@ -1,10 +1,10 @@
use fj_math::{Point, Segment};

use crate::objects::{Curve, Edge};
use crate::objects::{CurveKind, Edge};

use super::LineSegmentIntersection;

/// The intersection between a [`Curve`] and an [`Edge`], in curve coordinates
/// The intersection between a curve and an [`Edge`], in curve coordinates
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub enum CurveEdgeIntersection {
/// The curve and edge intersect at a point
Expand All @@ -26,17 +26,17 @@ impl CurveEdgeIntersection {
/// # Panics
///
/// Currently, only intersections between lines and line segments can be
/// computed. Panics, if a different type of [`Curve`] or [`Edge`] is
/// computed. Panics, if a different type of curve or [`Edge`] is
/// passed.
pub fn compute(curve: &Curve<2>, edge: &Edge) -> Option<Self> {
pub fn compute(curve: &CurveKind<2>, edge: &Edge) -> Option<Self> {
let curve_as_line = match curve {
Curve::Line(line) => line,
CurveKind::Line(line) => line,
_ => todo!("Curve-edge intersection only supports lines"),
};

let edge_as_segment = {
let edge_curve_as_line = match edge.curve().local_form() {
Curve::Line(line) => line,
let edge_curve_as_line = match edge.curve().kind() {
CurveKind::Line(line) => line,
_ => {
todo!("Curve-edge intersection only supports line segments")
}
Expand Down Expand Up @@ -76,14 +76,14 @@ impl CurveEdgeIntersection {
mod tests {
use fj_math::Point;

use crate::objects::{Curve, Edge, Surface};
use crate::objects::{CurveKind, Edge, Surface};

use super::CurveEdgeIntersection;

#[test]
fn compute_edge_in_front_of_curve_origin() {
let surface = Surface::xy_plane();
let curve = Curve::u_axis();
let curve = CurveKind::u_axis();
let edge = Edge::build()
.line_segment_from_points(&surface, [[1., -1.], [1., 1.]]);

Expand All @@ -100,7 +100,7 @@ mod tests {
#[test]
fn compute_edge_behind_curve_origin() {
let surface = Surface::xy_plane();
let curve = Curve::u_axis();
let curve = CurveKind::u_axis();
let edge = Edge::build()
.line_segment_from_points(&surface, [[-1., -1.], [-1., 1.]]);

Expand All @@ -117,7 +117,7 @@ mod tests {
#[test]
fn compute_edge_parallel_to_curve() {
let surface = Surface::xy_plane();
let curve = Curve::u_axis();
let curve = CurveKind::u_axis();
let edge = Edge::build()
.line_segment_from_points(&surface, [[-1., -1.], [1., -1.]]);

Expand All @@ -129,7 +129,7 @@ mod tests {
#[test]
fn compute_edge_on_curve() {
let surface = Surface::xy_plane();
let curve = Curve::u_axis();
let curve = CurveKind::u_axis();
let edge = Edge::build()
.line_segment_from_points(&surface, [[-1., 0.], [1., 0.]]);

Expand Down
12 changes: 6 additions & 6 deletions crates/fj-kernel/src/algorithms/intersection/curve_face.rs
Expand Up @@ -4,10 +4,10 @@ use fj_math::Point;

use crate::{
algorithms::intersection::CurveEdgeIntersection,
objects::{Curve, Face},
objects::{CurveKind, Face},
};

/// The intersections between a [`Curve`] and a [`Face`], in curve coordinates
/// The intersections between a curve and a [`Face`], in curve coordinates
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct CurveFaceIntersectionList {
intervals: Vec<CurveFaceIntersection>,
Expand All @@ -27,8 +27,8 @@ impl CurveFaceIntersectionList {
Self { intervals }
}

/// Compute the intersections between a [`Curve`] and a [`Face`]
pub fn compute(curve: &Curve<2>, face: &Face) -> Self {
/// Compute the intersections between a curve and a [`Face`]
pub fn compute(curve: &CurveKind<2>, face: &Face) -> Self {
let edges = face.all_cycles().flat_map(|cycle| {
let edges: Vec<_> = cycle.edges().cloned().collect();
edges
Expand Down Expand Up @@ -142,13 +142,13 @@ pub type CurveFaceIntersection = [Point<1>; 2];
mod tests {
use fj_math::{Line, Point, Vector};

use crate::objects::{Curve, Face, Surface};
use crate::objects::{CurveKind, Face, Surface};

use super::CurveFaceIntersectionList;

#[test]
fn compute() {
let curve = Curve::Line(Line {
let curve = CurveKind::Line(Line {
origin: Point::from([-3., 0.]),
direction: Vector::from([1., 0.]),
});
Expand Down
22 changes: 11 additions & 11 deletions crates/fj-kernel/src/algorithms/intersection/surface_surface.rs
@@ -1,15 +1,15 @@
use fj_math::{Line, Point, Scalar, Vector};

use crate::objects::{Curve, Surface};
use crate::objects::{CurveKind, Surface};

/// The intersection between two surfaces
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct SurfaceSurfaceIntersection {
/// The intersection curves, in the coordinates of the input surfaces
pub local_intersection_curves: [Curve<2>; 2],
pub local_intersection_curves: [CurveKind<2>; 2],

/// The intersection curve, in global coordinates
pub global_intersection_curve: Curve<3>,
pub global_intersection_curve: CurveKind<3>,
}

impl SurfaceSurfaceIntersection {
Expand Down Expand Up @@ -50,7 +50,7 @@ impl SurfaceSurfaceIntersection {

let curve_a = project_line_into_plane(&line, &a_parametric);
let curve_b = project_line_into_plane(&line, &b_parametric);
let curve_global = Curve::Line(Line { origin, direction });
let curve_global = CurveKind::Line(Line { origin, direction });

Some(Self {
local_intersection_curves: [curve_a, curve_b],
Expand All @@ -70,7 +70,7 @@ impl PlaneParametric {
pub fn extract_from_surface(surface: &Surface) -> Self {
let Surface::SweptCurve(surface) = surface;
let line = match surface.curve {
Curve::Line(line) => line,
CurveKind::Line(line) => line,
_ => todo!("Only plane-plane intersection is currently supported."),
};

Expand Down Expand Up @@ -111,7 +111,7 @@ impl PlaneConstantNormal {
fn project_line_into_plane(
line: &Line<3>,
plane: &PlaneParametric,
) -> Curve<2> {
) -> CurveKind<2> {
let line_origin_relative_to_plane = line.origin - plane.origin;
let line_origin_in_plane = Vector::from([
plane
Expand All @@ -134,7 +134,7 @@ fn project_line_into_plane(
direction: line_direction_in_plane,
};

Curve::Line(line)
CurveKind::Line(line)
}

#[cfg(test)]
Expand All @@ -143,7 +143,7 @@ mod tests {

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

use super::SurfaceSurfaceIntersection;
Expand All @@ -163,9 +163,9 @@ mod tests {
None,
);

let expected_xy = Curve::u_axis();
let expected_xz = Curve::u_axis();
let expected_global = Curve::x_axis();
let expected_xy = CurveKind::u_axis();
let expected_xz = CurveKind::u_axis();
let expected_global = CurveKind::x_axis();

assert_eq!(
SurfaceSurfaceIntersection::compute(&xy, &xz),
Expand Down
17 changes: 7 additions & 10 deletions crates/fj-kernel/src/algorithms/reverse.rs
@@ -1,9 +1,6 @@
use fj_math::{Circle, Line, Point, Vector};

use crate::{
local::Local,
objects::{Curve, Cycle, Edge, Face},
};
use crate::objects::{Curve, CurveKind, Cycle, Edge, Face};

/// Reverse the direction of a face
pub fn reverse_face(face: &Face) -> Face {
Expand All @@ -28,25 +25,25 @@ fn reverse_local_coordinates_in_cycle<'r>(
cycles.into_iter().map(|cycle| {
let edges = cycle.edges().map(|edge| {
let curve = {
let local = match edge.curve().local_form() {
Curve::Circle(Circle { center, a, b }) => {
let local = match edge.curve().kind() {
CurveKind::Circle(Circle { center, a, b }) => {
let center = Point::from([center.u, -center.v]);

let a = Vector::from([a.u, -a.v]);
let b = Vector::from([b.u, -b.v]);

Curve::Circle(Circle { center, a, b })
CurveKind::Circle(Circle { center, a, b })
}
Curve::Line(Line { origin, direction }) => {
CurveKind::Line(Line { origin, direction }) => {
let origin = Point::from([origin.u, -origin.v]);
let direction =
Vector::from([direction.u, -direction.v]);

Curve::Line(Line { origin, direction })
CurveKind::Line(Line { origin, direction })
}
};

Local::new(local, *edge.curve().global_form())
Curve::new(local, *edge.curve().global())
};

Edge::new(curve, *edge.vertices())
Expand Down
12 changes: 6 additions & 6 deletions crates/fj-kernel/src/algorithms/sweep.rs
Expand Up @@ -3,10 +3,9 @@ use fj_math::{Point, Scalar, Transform, Triangle, Vector};

use crate::{
iter::ObjectIters,
local::Local,
objects::{
Curve, Cycle, Edge, Face, GlobalVertex, Sketch, Solid, Surface, Vertex,
VerticesOfEdge,
Curve, CurveKind, Cycle, Edge, Face, GlobalCurve, GlobalVertex, Sketch,
Solid, Surface, Vertex, VerticesOfEdge,
},
};

Expand Down Expand Up @@ -140,12 +139,13 @@ fn create_non_continuous_side_face(
let [a, b] = [&vertices[0], &vertices[1]];

let curve = {
let local = Curve::line_from_points([a.0, b.0]);
let local = CurveKind::line_from_points([a.0, b.0]);

let global = [a, b].map(|vertex| vertex.1.position());
let global = Curve::line_from_points(global);
let global =
GlobalCurve::from_kind(CurveKind::line_from_points(global));

Local::new(local, global)
Curve::new(local, global)
};

let vertices = VerticesOfEdge::from_vertices([
Expand Down
33 changes: 18 additions & 15 deletions crates/fj-kernel/src/algorithms/transform.rs
@@ -1,10 +1,8 @@
use fj_math::{Transform, Vector};

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

/// Transform an object
Expand Down Expand Up @@ -34,14 +32,12 @@ pub trait TransformObject: Sized {
}
}

impl TransformObject for Curve<3> {
impl TransformObject for Curve {
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)),
}
// Don't need to transform `self.kind`, as that's in local form.
let global = self.global().transform(transform);

Curve::new(*self.kind(), global)
}
}

Expand All @@ -54,9 +50,9 @@ impl TransformObject for Cycle {

impl TransformObject for Edge {
fn transform(self, transform: &Transform) -> Self {
let curve = Local::new(
*self.curve().local_form(),
self.curve().global_form().transform(transform),
let curve = Curve::new(
*self.curve().kind(),
self.curve().global().transform(transform),
);

let vertices =
Expand Down Expand Up @@ -93,6 +89,13 @@ impl TransformObject for Face {
}
}

impl TransformObject for GlobalCurve {
fn transform(self, transform: &Transform) -> Self {
let kind = self.kind().transform(transform);
GlobalCurve::from_kind(kind)
}
}

impl TransformObject for GlobalVertex {
fn transform(self, transform: &Transform) -> Self {
let position = transform.transform_point(&self.position());
Expand Down

0 comments on commit 203c5d6

Please sign in to comment.