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

Simplify functions that previously had to return Result<_, ValidationError> #1393

Merged
merged 12 commits into from
Nov 25, 2022
37 changes: 16 additions & 21 deletions crates/fj-kernel/src/algorithms/approx/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,66 +211,63 @@ mod tests {
use super::CurveApprox;

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

let surface =
PartialSurface::from_axes(GlobalPath::x_axis(), [0., 0., 1.])
.build(&objects)?
.insert(&mut objects)?;
.build(&objects)
.insert(&mut objects);
let mut curve = PartialCurve {
surface: Some(surface),
..Default::default()
};
curve.update_as_line_from_points([[1., 1.], [2., 1.]]);
let curve = curve.build(&mut objects)?.insert(&mut objects)?;
let curve = curve.build(&mut objects).insert(&mut objects);
let range = RangeOnPath::from([[0.], [1.]]);

let approx = (&curve, range).approx(1.);

assert_eq!(approx, CurveApprox::empty());
Ok(())
}

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

let surface = PartialSurface::from_axes(
GlobalPath::circle_from_radius(1.),
[0., 0., 1.],
)
.build(&objects)?
.insert(&mut objects)?;
.build(&objects)
.insert(&mut objects);
let mut curve = PartialCurve {
surface: Some(surface),
..Default::default()
};
curve.update_as_line_from_points([[1., 1.], [1., 2.]]);
let curve = curve.build(&mut objects)?.insert(&mut objects)?;
let curve = curve.build(&mut objects).insert(&mut objects);
let range = RangeOnPath::from([[0.], [1.]]);

let approx = (&curve, range).approx(1.);

assert_eq!(approx, CurveApprox::empty());
Ok(())
}

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

let path = GlobalPath::circle_from_radius(1.);
let surface = PartialSurface::from_axes(path, [0., 0., 1.])
.build(&objects)?
.insert(&mut objects)?;
.build(&objects)
.insert(&mut 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(&mut objects)?.insert(&mut objects)?;
let curve = curve.build(&mut objects).insert(&mut objects);

let range = RangeOnPath::from([[0.], [TAU]]);
let tolerance = 1.;
Expand All @@ -289,23 +286,22 @@ mod tests {
})
.collect::<Vec<_>>();
assert_eq!(approx.points, expected_approx);
Ok(())
}

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

let surface =
PartialSurface::from_axes(GlobalPath::x_axis(), [0., 0., 1.])
.build(&objects)?
.insert(&mut objects)?;
.build(&objects)
.insert(&mut objects);
let mut curve = PartialCurve {
surface: Some(surface),
..Default::default()
};
curve.update_as_circle_from_radius(1.);
let curve = curve.build(&mut objects)?.insert(&mut objects)?;
let curve = curve.build(&mut objects).insert(&mut objects);

let range = RangeOnPath::from([[0.], [TAU]]);
let tolerance = 1.;
Expand All @@ -323,6 +319,5 @@ mod tests {
})
.collect::<Vec<_>>();
assert_eq!(approx.points, expected_approx);
Ok(())
}
}
28 changes: 12 additions & 16 deletions crates/fj-kernel/src/algorithms/intersect/curve_edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ mod tests {
use super::CurveEdgeIntersection;

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

let surface = objects.surfaces.xy_plane();
Expand All @@ -93,10 +93,10 @@ mod tests {
..Default::default()
};
curve.update_as_u_axis();
let curve = curve.build(&mut objects)?;
let curve = curve.build(&mut objects);
let half_edge = HalfEdge::partial()
.update_as_line_segment_from_points(surface, [[1., -1.], [1., 1.]])
.build(&mut objects)?;
.build(&mut objects);

let intersection = CurveEdgeIntersection::compute(&curve, &half_edge);

Expand All @@ -106,11 +106,10 @@ mod tests {
point_on_curve: Point::from([1.])
})
);
Ok(())
}

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

let surface = objects.surfaces.xy_plane();
Expand All @@ -119,13 +118,13 @@ mod tests {
..Default::default()
};
curve.update_as_u_axis();
let curve = curve.build(&mut objects)?;
let curve = curve.build(&mut objects);
let half_edge = HalfEdge::partial()
.update_as_line_segment_from_points(
surface,
[[-1., -1.], [-1., 1.]],
)
.build(&mut objects)?;
.build(&mut objects);

let intersection = CurveEdgeIntersection::compute(&curve, &half_edge);

Expand All @@ -135,11 +134,10 @@ mod tests {
point_on_curve: Point::from([-1.])
})
);
Ok(())
}

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

let surface = objects.surfaces.xy_plane();
Expand All @@ -148,22 +146,21 @@ mod tests {
..Default::default()
};
curve.update_as_u_axis();
let curve = curve.build(&mut objects)?;
let curve = curve.build(&mut objects);
let half_edge = HalfEdge::partial()
.update_as_line_segment_from_points(
surface,
[[-1., -1.], [1., -1.]],
)
.build(&mut objects)?;
.build(&mut objects);

let intersection = CurveEdgeIntersection::compute(&curve, &half_edge);

assert!(intersection.is_none());
Ok(())
}

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

let surface = objects.surfaces.xy_plane();
Expand All @@ -172,10 +169,10 @@ mod tests {
..Default::default()
};
curve.update_as_u_axis();
let curve = curve.build(&mut objects)?;
let curve = curve.build(&mut objects);
let half_edge = HalfEdge::partial()
.update_as_line_segment_from_points(surface, [[-1., 0.], [1., 0.]])
.build(&mut objects)?;
.build(&mut objects);

let intersection = CurveEdgeIntersection::compute(&curve, &half_edge);

Expand All @@ -185,6 +182,5 @@ mod tests {
points_on_curve: [Point::from([-1.]), Point::from([1.]),]
})
);
Ok(())
}
}
7 changes: 3 additions & 4 deletions crates/fj-kernel/src/algorithms/intersect/curve_face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ mod tests {
use super::CurveFaceIntersection;

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

let surface = objects.surfaces.xy_plane();
Expand All @@ -169,7 +169,7 @@ mod tests {
..Default::default()
};
curve.update_as_line_from_points([[-3., 0.], [-2., 0.]]);
let curve = curve.build(&mut objects)?;
let curve = curve.build(&mut objects);

#[rustfmt::skip]
let exterior = [
Expand All @@ -190,12 +190,11 @@ mod tests {
.with_surface(surface)
.with_exterior_polygon_from_points(exterior)
.with_interior_polygon_from_points(interior)
.build(&mut objects)?;
.build(&mut objects);

let expected =
CurveFaceIntersection::from_intervals([[[1.], [2.]], [[4.], [5.]]]);
assert_eq!(CurveFaceIntersection::compute(&curve, &face), expected);
Ok(())
}

#[test]
Expand Down
53 changes: 23 additions & 30 deletions crates/fj-kernel/src/algorithms/intersect/face_face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::{
objects::{Curve, Face, Objects},
services::Service,
storage::Handle,
validate::ValidationError,
};

use super::{CurveFaceIntersection, SurfaceSurfaceIntersection};
Expand All @@ -32,13 +31,13 @@ impl FaceFaceIntersection {
pub fn compute(
faces: [&Face; 2],
objects: &mut Service<Objects>,
) -> Result<Option<Self>, ValidationError> {
) -> Option<Self> {
let surfaces = faces.map(|face| face.surface().clone());

let intersection_curves =
match SurfaceSurfaceIntersection::compute(surfaces, objects)? {
match SurfaceSurfaceIntersection::compute(surfaces, objects) {
Some(intersection) => intersection.intersection_curves,
None => return Ok(None),
None => return None,
};

let curve_face_intersections = intersection_curves
Expand All @@ -54,19 +53,18 @@ impl FaceFaceIntersection {
};

if intersection_intervals.is_empty() {
return Ok(None);
return None;
}

Ok(Some(Self {
Some(Self {
intersection_curves,
intersection_intervals,
}))
})
}
}

#[cfg(test)]
mod tests {
use fj_interop::ext::ArrayExt;
use pretty_assertions::assert_eq;

use crate::{
Expand All @@ -76,13 +74,12 @@ mod tests {
objects::{Face, Objects},
partial::{HasPartial, PartialCurve},
services::State,
validate::ValidationError,
};

use super::FaceFaceIntersection;

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

#[rustfmt::skip]
Expand All @@ -93,23 +90,21 @@ mod tests {
[1., 2.],
];
let [a, b] = [objects.surfaces.xy_plane(), objects.surfaces.xz_plane()]
.try_map_ext(|surface| {
.map(|surface| {
Face::partial()
.with_surface(surface)
.with_exterior_polygon_from_points(points)
.build(&mut objects)
})?;
});

let intersection =
FaceFaceIntersection::compute([&a, &b], &mut objects)?;
FaceFaceIntersection::compute([&a, &b], &mut objects);

assert!(intersection.is_none());

Ok(())
}

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

#[rustfmt::skip]
Expand All @@ -121,25 +116,24 @@ mod tests {
];
let surfaces =
[objects.surfaces.xy_plane(), objects.surfaces.xz_plane()];
let [a, b] = surfaces.clone().try_map_ext(|surface| {
let [a, b] = surfaces.clone().map(|surface| {
Face::partial()
.with_surface(surface)
.with_exterior_polygon_from_points(points)
.build(&mut objects)
})?;
});

let intersection =
FaceFaceIntersection::compute([&a, &b], &mut objects)?;

let expected_curves =
surfaces.try_map_ext(|surface| -> Result<_, ValidationError> {
let mut curve = PartialCurve {
surface: Some(surface),
..Default::default()
};
curve.update_as_line_from_points([[0., 0.], [1., 0.]]);
Ok(curve.build(&mut objects)?.insert(&mut objects)?)
})?;
FaceFaceIntersection::compute([&a, &b], &mut objects);

let expected_curves = surfaces.map(|surface| {
let mut curve = PartialCurve {
surface: Some(surface),
..Default::default()
};
curve.update_as_line_from_points([[0., 0.], [1., 0.]]);
curve.build(&mut objects).insert(&mut objects)
});
let expected_intervals =
CurveFaceIntersection::from_intervals([[[-1.], [1.]]]);
assert_eq!(
Expand All @@ -149,6 +143,5 @@ mod tests {
intersection_intervals: expected_intervals
})
);
Ok(())
}
}
Loading