Skip to content

Commit

Permalink
Separate building and inserting objects
Browse files Browse the repository at this point in the history
This provides more flexibility, making it possible to use the partial
object and builder infrastructure, without automatically opting into
validation. This can be useful, especially in unit tests.
  • Loading branch information
hannobraun committed Nov 9, 2022
1 parent 7758f81 commit 3f2aa6e
Show file tree
Hide file tree
Showing 30 changed files with 220 additions and 139 deletions.
13 changes: 9 additions & 4 deletions crates/fj-kernel/src/algorithms/approx/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ mod tests {
use crate::{
algorithms::approx::{path::RangeOnPath, Approx, ApproxPoint},
builder::CurveBuilder,
insert::Insert,
objects::{Curve, Objects, Surface},
partial::HasPartial,
path::GlobalPath,
Expand All @@ -215,7 +216,8 @@ mod tests {
let curve = Curve::partial()
.with_surface(Some(surface))
.update_as_line_from_points([[1., 1.], [2., 1.]])
.build(&objects)?;
.build(&objects)?
.insert(&objects)?;
let range = RangeOnPath::from([[0.], [1.]]);

let approx = (&curve, range).approx(1.);
Expand All @@ -236,7 +238,8 @@ mod tests {
let curve = Curve::partial()
.with_surface(Some(surface))
.update_as_line_from_points([[1., 1.], [1., 2.]])
.build(&objects)?;
.build(&objects)?
.insert(&objects)?;
let range = RangeOnPath::from([[0.], [1.]]);

let approx = (&curve, range).approx(1.);
Expand All @@ -255,7 +258,8 @@ mod tests {
let curve = Curve::partial()
.with_surface(Some(surface.clone()))
.update_as_line_from_points([[0., 1.], [1., 1.]])
.build(&objects)?;
.build(&objects)?
.insert(&objects)?;

let range = RangeOnPath::from([[0.], [TAU]]);
let tolerance = 1.;
Expand Down Expand Up @@ -287,7 +291,8 @@ mod tests {
let curve = Curve::partial()
.with_surface(Some(surface))
.update_as_circle_from_radius(1.)
.build(&objects)?;
.build(&objects)?
.insert(&objects)?;

let range = RangeOnPath::from([[0.], [TAU]]);
let tolerance = 1.;
Expand Down
16 changes: 10 additions & 6 deletions crates/fj-kernel/src/algorithms/intersect/face_face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ mod tests {
use crate::{
algorithms::intersect::CurveFaceIntersection,
builder::{CurveBuilder, FaceBuilder},
insert::Insert,
objects::{Curve, Face, Objects},
partial::HasPartial,
validate::ValidationError,
};

use super::FaceFaceIntersection;
Expand Down Expand Up @@ -122,12 +124,14 @@ mod tests {

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

let expected_curves = surfaces.try_map_ext(|surface| {
Curve::partial()
.with_surface(Some(surface))
.update_as_line_from_points([[0., 0.], [1., 0.]])
.build(&objects)
})?;
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 expected_intervals =
CurveFaceIntersection::from_intervals([[[-1.], [1.]]]);
assert_eq!(
Expand Down
25 changes: 17 additions & 8 deletions crates/fj-kernel/src/algorithms/intersect/face_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ mod tests {
use crate::{
algorithms::intersect::{face_point::FacePointIntersection, Intersect},
builder::FaceBuilder,
insert::Insert,
iter::ObjectIters,
objects::{Face, Objects},
partial::HasPartial,
Expand All @@ -150,7 +151,8 @@ mod tests {
let face = Face::partial()
.with_surface(surface)
.with_exterior_polygon_from_points([[0., 0.], [1., 1.], [0., 2.]])
.build(&objects)?;
.build(&objects)?
.insert(&objects)?;
let point = Point::from([2., 1.]);

let intersection = (&face, &point).intersect();
Expand All @@ -167,7 +169,8 @@ mod tests {
let face = Face::partial()
.with_surface(surface)
.with_exterior_polygon_from_points([[0., 0.], [2., 1.], [0., 2.]])
.build(&objects)?;
.build(&objects)?
.insert(&objects)?;
let point = Point::from([1., 1.]);

let intersection = (&face, &point).intersect();
Expand All @@ -187,7 +190,8 @@ mod tests {
let face = Face::partial()
.with_surface(surface)
.with_exterior_polygon_from_points([[4., 2.], [0., 4.], [0., 0.]])
.build(&objects)?;
.build(&objects)?
.insert(&objects)?;
let point = Point::from([1., 2.]);

let intersection = (&face, &point).intersect();
Expand All @@ -212,7 +216,8 @@ mod tests {
[3., 0.],
[3., 4.],
])
.build(&objects)?;
.build(&objects)?
.insert(&objects)?;
let point = Point::from([1., 1.]);

let intersection = (&face, &point).intersect();
Expand All @@ -238,7 +243,8 @@ mod tests {
[3., 1.],
[0., 2.],
])
.build(&objects)?;
.build(&objects)?
.insert(&objects)?;
let point = Point::from([1., 1.]);

let intersection = (&face, &point).intersect();
Expand All @@ -265,7 +271,8 @@ mod tests {
[4., 0.],
[4., 5.],
])
.build(&objects)?;
.build(&objects)?
.insert(&objects)?;
let point = Point::from([1., 1.]);

let intersection = (&face, &point).intersect();
Expand All @@ -285,7 +292,8 @@ mod tests {
let face = Face::partial()
.with_surface(surface)
.with_exterior_polygon_from_points([[0., 0.], [2., 0.], [0., 1.]])
.build(&objects)?;
.build(&objects)?
.insert(&objects)?;
let point = Point::from([1., 0.]);

let intersection = (&face, &point).intersect();
Expand Down Expand Up @@ -314,7 +322,8 @@ mod tests {
let face = Face::partial()
.with_surface(surface)
.with_exterior_polygon_from_points([[0., 0.], [1., 0.], [0., 1.]])
.build(&objects)?;
.build(&objects)?
.insert(&objects)?;
let point = Point::from([1., 0.]);

let intersection = (&face, &point).intersect();
Expand Down
10 changes: 9 additions & 1 deletion crates/fj-kernel/src/algorithms/intersect/ray_face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ mod tests {
transform::TransformObject,
},
builder::FaceBuilder,
insert::Insert,
iter::ObjectIters,
objects::{Face, Objects},
partial::HasPartial,
Expand All @@ -174,6 +175,7 @@ mod tests {
[-1., 1.],
])
.build(&objects)?
.insert(&objects)?
.translate([-1., 0., 0.], &objects)?;

assert_eq!((&ray, &face).intersect(), None);
Expand All @@ -196,6 +198,7 @@ mod tests {
[-1., 1.],
])
.build(&objects)?
.insert(&objects)?
.translate([1., 0., 0.], &objects)?;

assert_eq!(
Expand All @@ -221,6 +224,7 @@ mod tests {
[-1., 1.],
])
.build(&objects)?
.insert(&objects)?
.translate([0., 0., 2.], &objects)?;

assert_eq!((&ray, &face).intersect(), None);
Expand All @@ -243,6 +247,7 @@ mod tests {
[-1., 1.],
])
.build(&objects)?
.insert(&objects)?
.translate([1., 1., 0.], &objects)?;

let edge = face
Expand Down Expand Up @@ -276,6 +281,7 @@ mod tests {
[-1., 1.],
])
.build(&objects)?
.insert(&objects)?
.translate([1., 1., 1.], &objects)?;

let vertex = face
Expand Down Expand Up @@ -306,7 +312,8 @@ mod tests {
[1., 1.],
[-1., 1.],
])
.build(&objects)?;
.build(&objects)?
.insert(&objects)?;

assert_eq!(
(&ray, &face).intersect(),
Expand All @@ -332,6 +339,7 @@ mod tests {
[-1., 1.],
])
.build(&objects)?
.insert(&objects)?
.translate([0., 0., 1.], &objects)?;

assert_eq!((&ray, &face).intersect(), None);
Expand Down
7 changes: 5 additions & 2 deletions crates/fj-kernel/src/algorithms/intersect/surface_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ mod tests {
use crate::{
algorithms::transform::TransformObject,
builder::CurveBuilder,
insert::Insert,
objects::{Curve, Objects},
partial::HasPartial,
};
Expand Down Expand Up @@ -124,11 +125,13 @@ mod tests {
let expected_xy = Curve::partial()
.with_surface(Some(xy.clone()))
.update_as_u_axis()
.build(&objects)?;
.build(&objects)?
.insert(&objects)?;
let expected_xz = Curve::partial()
.with_surface(Some(xz.clone()))
.update_as_u_axis()
.build(&objects)?;
.build(&objects)?
.insert(&objects)?;

assert_eq!(
SurfaceSurfaceIntersection::compute([xy, xz], &objects)?,
Expand Down
6 changes: 4 additions & 2 deletions crates/fj-kernel/src/algorithms/reverse/face.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
insert::Insert,
objects::{Face, Objects},
partial::HasPartial,
storage::Handle,
Expand All @@ -15,10 +16,11 @@ impl Reverse for Handle<Face> {
.map(|cycle| cycle.clone().reverse(objects))
.collect::<Result<Vec<_>, _>>()?;

Face::partial()
Ok(Face::partial()
.with_exterior(exterior)
.with_interiors(interiors)
.with_color(self.color())
.build(objects)
.build(objects)?
.insert(objects)?)
}
}
23 changes: 17 additions & 6 deletions crates/fj-kernel/src/algorithms/sweep/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use fj_math::{Line, Scalar, Vector};

use crate::{
algorithms::{reverse::Reverse, transform::TransformObject},
insert::Insert,
objects::{
Curve, Cycle, Face, GlobalEdge, HalfEdge, Objects, SurfaceVertex,
Vertex,
Expand Down Expand Up @@ -176,10 +177,11 @@ impl Sweep for (Handle<HalfEdge>, Color) {
objects.cycles.insert(Cycle::new(edges))?
};

Face::partial()
Ok(Face::partial()
.with_exterior(cycle)
.with_color(color)
.build(objects)
.build(objects)?
.insert(objects)?)
}
}

Expand All @@ -191,6 +193,7 @@ mod tests {
use crate::{
algorithms::{reverse::Reverse, sweep::Sweep},
builder::HalfEdgeBuilder,
insert::Insert,
objects::{Cycle, Face, HalfEdge, Objects, SurfaceVertex, Vertex},
partial::HasPartial,
};
Expand All @@ -204,7 +207,8 @@ mod tests {
objects.surfaces.xy_plane(),
[[0., 0.], [1., 0.]],
)
.build(&objects)?;
.build(&objects)?
.insert(&objects)?;

let face =
(half_edge, Color::default()).sweep([0., 0., 1.], &objects)?;
Expand All @@ -218,7 +222,8 @@ mod tests {
surface.clone(),
[[0., 0.], [1., 0.]],
)
.build(&objects)?;
.build(&objects)?
.insert(&objects)?;
let side_up = HalfEdge::partial()
.with_surface(surface.clone())
.with_back_vertex(Vertex::partial().with_surface_form(
Expand All @@ -228,7 +233,8 @@ mod tests {
SurfaceVertex::partial().with_position(Some([1., 1.])),
))
.update_as_line_segment()
.build(&objects)?;
.build(&objects)?
.insert(&objects)?;
let top = HalfEdge::partial()
.with_surface(surface.clone())
.with_back_vertex(Vertex::partial().with_surface_form(
Expand All @@ -239,6 +245,7 @@ mod tests {
))
.update_as_line_segment()
.build(&objects)?
.insert(&objects)?
.reverse(&objects)?;
let side_down =
HalfEdge::partial()
Expand All @@ -251,13 +258,17 @@ mod tests {
))
.update_as_line_segment()
.build(&objects)?
.insert(&objects)?
.reverse(&objects)?;

let cycle = objects
.cycles
.insert(Cycle::new([bottom, side_up, top, side_down]))?;

Face::partial().with_exterior(cycle).build(&objects)?
Face::partial()
.with_exterior(cycle)
.build(&objects)?
.insert(&objects)?
};

assert_eq!(face, expected_face);
Expand Down
Loading

0 comments on commit 3f2aa6e

Please sign in to comment.