Skip to content

Commit

Permalink
Merge pull request #1338 from hannobraun/partial
Browse files Browse the repository at this point in the history
Simplify `PartialCurve`
  • Loading branch information
hannobraun authored Nov 11, 2022
2 parents b64c84b + 7e5500a commit d6cde48
Show file tree
Hide file tree
Showing 17 changed files with 211 additions and 196 deletions.
48 changes: 26 additions & 22 deletions crates/fj-kernel/src/algorithms/approx/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ mod tests {
algorithms::approx::{path::RangeOnPath, Approx, ApproxPoint},
builder::CurveBuilder,
insert::Insert,
objects::{Curve, Objects, Surface},
partial::HasPartial,
objects::{Objects, Surface},
partial::PartialCurve,
path::GlobalPath,
};

Expand All @@ -213,11 +213,12 @@ mod tests {
let surface = objects
.surfaces
.insert(Surface::new(GlobalPath::x_axis(), [0., 0., 1.]))?;
let curve = Curve::partial()
.with_surface(Some(surface))
.update_as_line_from_points([[1., 1.], [2., 1.]])
.build(&objects)?
.insert(&objects)?;
let mut curve = PartialCurve {
surface: Some(surface),
..Default::default()
};
curve.update_as_line_from_points([[1., 1.], [2., 1.]]);
let curve = curve.build(&objects)?.insert(&objects)?;
let range = RangeOnPath::from([[0.], [1.]]);

let approx = (&curve, range).approx(1.);
Expand All @@ -235,11 +236,12 @@ mod tests {
GlobalPath::circle_from_radius(1.),
[0., 0., 1.],
))?;
let curve = Curve::partial()
.with_surface(Some(surface))
.update_as_line_from_points([[1., 1.], [1., 2.]])
.build(&objects)?
.insert(&objects)?;
let mut curve = PartialCurve {
surface: Some(surface),
..Default::default()
};
curve.update_as_line_from_points([[1., 1.], [1., 2.]]);
let curve = curve.build(&objects)?.insert(&objects)?;
let range = RangeOnPath::from([[0.], [1.]]);

let approx = (&curve, range).approx(1.);
Expand All @@ -255,11 +257,12 @@ mod tests {
let path = GlobalPath::circle_from_radius(1.);
let surface =
objects.surfaces.insert(Surface::new(path, [0., 0., 1.]))?;
let curve = Curve::partial()
.with_surface(Some(surface.clone()))
.update_as_line_from_points([[0., 1.], [1., 1.]])
.build(&objects)?
.insert(&objects)?;
let mut curve = PartialCurve {
surface: Some(surface.clone()),
..Default::default()
};
curve.update_as_line_from_points([[0., 1.], [1., 1.]]);
let curve = curve.build(&objects)?.insert(&objects)?;

let range = RangeOnPath::from([[0.], [TAU]]);
let tolerance = 1.;
Expand Down Expand Up @@ -288,11 +291,12 @@ mod tests {
let surface = objects
.surfaces
.insert(Surface::new(GlobalPath::x_axis(), [0., 0., 1.]))?;
let curve = Curve::partial()
.with_surface(Some(surface))
.update_as_circle_from_radius(1.)
.build(&objects)?
.insert(&objects)?;
let mut curve = PartialCurve {
surface: Some(surface),
..Default::default()
};
curve.update_as_circle_from_radius(1.);
let curve = curve.build(&objects)?.insert(&objects)?;

let range = RangeOnPath::from([[0.], [TAU]]);
let tolerance = 1.;
Expand Down
44 changes: 26 additions & 18 deletions crates/fj-kernel/src/algorithms/intersect/curve_edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ mod tests {

use crate::{
builder::{CurveBuilder, HalfEdgeBuilder},
objects::{Curve, HalfEdge, Objects},
partial::HasPartial,
objects::{HalfEdge, Objects},
partial::{HasPartial, PartialCurve},
};

use super::CurveEdgeIntersection;
Expand All @@ -87,10 +87,12 @@ mod tests {
let objects = Objects::new();

let surface = objects.surfaces.xy_plane();
let curve = Curve::partial()
.with_surface(Some(surface.clone()))
.update_as_u_axis()
.build(&objects)?;
let mut curve = PartialCurve {
surface: Some(surface.clone()),
..Default::default()
};
curve.update_as_u_axis();
let curve = curve.build(&objects)?;
let half_edge = HalfEdge::partial()
.update_as_line_segment_from_points(surface, [[1., -1.], [1., 1.]])
.build(&objects)?;
Expand All @@ -111,10 +113,12 @@ mod tests {
let objects = Objects::new();

let surface = objects.surfaces.xy_plane();
let curve = Curve::partial()
.with_surface(Some(surface.clone()))
.update_as_u_axis()
.build(&objects)?;
let mut curve = PartialCurve {
surface: Some(surface.clone()),
..Default::default()
};
curve.update_as_u_axis();
let curve = curve.build(&objects)?;
let half_edge = HalfEdge::partial()
.update_as_line_segment_from_points(
surface,
Expand All @@ -138,10 +142,12 @@ mod tests {
let objects = Objects::new();

let surface = objects.surfaces.xy_plane();
let curve = Curve::partial()
.with_surface(Some(surface.clone()))
.update_as_u_axis()
.build(&objects)?;
let mut curve = PartialCurve {
surface: Some(surface.clone()),
..Default::default()
};
curve.update_as_u_axis();
let curve = curve.build(&objects)?;
let half_edge = HalfEdge::partial()
.update_as_line_segment_from_points(
surface,
Expand All @@ -160,10 +166,12 @@ mod tests {
let objects = Objects::new();

let surface = objects.surfaces.xy_plane();
let curve = Curve::partial()
.with_surface(Some(surface.clone()))
.update_as_u_axis()
.build(&objects)?;
let mut curve = PartialCurve {
surface: Some(surface.clone()),
..Default::default()
};
curve.update_as_u_axis();
let curve = curve.build(&objects)?;
let half_edge = HalfEdge::partial()
.update_as_line_segment_from_points(surface, [[-1., 0.], [1., 0.]])
.build(&objects)?;
Expand Down
14 changes: 8 additions & 6 deletions crates/fj-kernel/src/algorithms/intersect/curve_face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ where
mod tests {
use crate::{
builder::{CurveBuilder, FaceBuilder},
objects::{Curve, Face, Objects},
partial::HasPartial,
objects::{Face, Objects},
partial::{HasPartial, PartialCurve},
};

use super::CurveFaceIntersection;
Expand All @@ -163,10 +163,12 @@ mod tests {

let surface = objects.surfaces.xy_plane();

let curve = Curve::partial()
.with_surface(Some(surface.clone()))
.update_as_line_from_points([[-3., 0.], [-2., 0.]])
.build(&objects)?;
let mut curve = PartialCurve {
surface: Some(surface.clone()),
..Default::default()
};
curve.update_as_line_from_points([[-3., 0.], [-2., 0.]]);
let curve = curve.build(&objects)?;

#[rustfmt::skip]
let exterior = [
Expand Down
15 changes: 8 additions & 7 deletions crates/fj-kernel/src/algorithms/intersect/face_face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ mod tests {
algorithms::intersect::CurveFaceIntersection,
builder::{CurveBuilder, FaceBuilder},
insert::Insert,
objects::{Curve, Face, Objects},
partial::HasPartial,
objects::{Face, Objects},
partial::{HasPartial, PartialCurve},
validate::ValidationError,
};

Expand Down Expand Up @@ -129,11 +129,12 @@ mod tests {

let expected_curves =
surfaces.try_map_ext(|surface| -> Result<_, ValidationError> {
Ok(Curve::partial()
.with_surface(Some(surface))
.update_as_line_from_points([[0., 0.], [1., 0.]])
.build(&objects)?
.insert(&objects)?)
let mut curve = PartialCurve {
surface: Some(surface),
..Default::default()
};
curve.update_as_line_from_points([[0., 0.], [1., 0.]]);
Ok(curve.build(&objects)?.insert(&objects)?)
})?;
let expected_intervals =
CurveFaceIntersection::from_intervals([[[-1.], [1.]]]);
Expand Down
29 changes: 14 additions & 15 deletions crates/fj-kernel/src/algorithms/intersect/surface_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,8 @@ mod tests {
use pretty_assertions::assert_eq;

use crate::{
algorithms::transform::TransformObject,
builder::CurveBuilder,
insert::Insert,
objects::{Curve, Objects},
partial::HasPartial,
algorithms::transform::TransformObject, builder::CurveBuilder,
insert::Insert, objects::Objects, partial::PartialCurve,
};

use super::SurfaceSurfaceIntersection;
Expand All @@ -122,16 +119,18 @@ mod tests {
None,
);

let expected_xy = Curve::partial()
.with_surface(Some(xy.clone()))
.update_as_u_axis()
.build(&objects)?
.insert(&objects)?;
let expected_xz = Curve::partial()
.with_surface(Some(xz.clone()))
.update_as_u_axis()
.build(&objects)?
.insert(&objects)?;
let mut expected_xy = PartialCurve {
surface: Some(xy.clone()),
..Default::default()
};
expected_xy.update_as_u_axis();
let expected_xy = expected_xy.build(&objects)?.insert(&objects)?;
let mut expected_xz = PartialCurve {
surface: Some(xz.clone()),
..Default::default()
};
expected_xz.update_as_u_axis();
let expected_xz = expected_xz.build(&objects)?.insert(&objects)?;

assert_eq!(
SurfaceSurfaceIntersection::compute([xy, xz], &objects)?,
Expand Down
15 changes: 8 additions & 7 deletions crates/fj-kernel/src/algorithms/sweep/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,21 @@ mod tests {
algorithms::sweep::Sweep,
builder::{CurveBuilder, HalfEdgeBuilder},
insert::Insert,
objects::{Curve, HalfEdge, Objects, Vertex},
partial::HasPartial,
objects::{HalfEdge, Objects, Vertex},
partial::{HasPartial, PartialCurve},
};

#[test]
fn vertex_surface() -> anyhow::Result<()> {
let objects = Objects::new();

let surface = objects.surfaces.xz_plane();
let curve = Curve::partial()
.with_surface(Some(surface.clone()))
.update_as_u_axis()
.build(&objects)?
.insert(&objects)?;
let mut curve = PartialCurve {
surface: Some(surface.clone()),
..Default::default()
};
curve.update_as_u_axis();
let curve = curve.build(&objects)?.insert(&objects)?;
let vertex = Vertex::partial()
.with_position(Some([0.]))
.with_curve(curve)
Expand Down
13 changes: 7 additions & 6 deletions crates/fj-kernel/src/algorithms/transform/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,20 @@ impl TransformObject for PartialCurve {
objects: &Objects,
) -> Result<Self, ValidationError> {
let surface = self
.surface()
.surface
.map(|surface| surface.transform(transform, objects))
.transpose()?;
let global_form = self
.global_form()
.global_form
.map(|global_form| global_form.transform(transform, objects))
.transpose()?;

// Don't need to transform `self.path`, as that's defined in surface
// coordinates, and thus transforming `surface` takes care of it.
Ok(Self::default()
.with_surface(surface)
.with_path(self.path())
.with_global_form(global_form))
Ok(PartialCurve {
path: self.path,
surface,
global_form,
})
}
}
32 changes: 20 additions & 12 deletions crates/fj-kernel/src/builder/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,52 @@ use crate::{partial::PartialCurve, path::SurfacePath};
/// Builder API for [`PartialCurve`]
pub trait CurveBuilder {
/// Update partial curve to represent the u-axis
fn update_as_u_axis(self) -> Self;
fn update_as_u_axis(&mut self) -> &mut Self;

/// Update partial curve to represent the v-axis
fn update_as_v_axis(self) -> Self;
fn update_as_v_axis(&mut self) -> &mut Self;

/// Update partial curve as a circle, from the provided radius
fn update_as_circle_from_radius(self, radius: impl Into<Scalar>) -> Self;
fn update_as_circle_from_radius(
&mut self,
radius: impl Into<Scalar>,
) -> &mut Self;

/// Update partial curve as a line, from the provided points
fn update_as_line_from_points(
self,
&mut self,
points: [impl Into<Point<2>>; 2],
) -> Self;
) -> &mut Self;
}

impl CurveBuilder for PartialCurve {
fn update_as_u_axis(self) -> Self {
fn update_as_u_axis(&mut self) -> &mut Self {
let a = Point::origin();
let b = a + Vector::unit_u();

self.update_as_line_from_points([a, b])
}

fn update_as_v_axis(self) -> Self {
fn update_as_v_axis(&mut self) -> &mut Self {
let a = Point::origin();
let b = a + Vector::unit_v();

self.update_as_line_from_points([a, b])
}

fn update_as_circle_from_radius(self, radius: impl Into<Scalar>) -> Self {
self.with_path(Some(SurfacePath::circle_from_radius(radius)))
fn update_as_circle_from_radius(
&mut self,
radius: impl Into<Scalar>,
) -> &mut Self {
self.path = Some(SurfacePath::circle_from_radius(radius));
self
}

fn update_as_line_from_points(
self,
&mut self,
points: [impl Into<Point<2>>; 2],
) -> Self {
self.with_path(Some(SurfacePath::line_from_points(points)))
) -> &mut Self {
self.path = Some(SurfacePath::line_from_points(points));
self
}
}
Loading

0 comments on commit d6cde48

Please sign in to comment.