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

Separate object construction from store insertion #1282

Merged
merged 12 commits into from
Oct 27, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ impl SurfaceSurfaceIntersection {

let curves = surfaces_and_planes.map(|(surface, plane)| {
let path = SurfacePath::Line(plane.project_line(&line));
let global_form = GlobalCurve::new(objects);
let global_form = objects.global_curves.insert(GlobalCurve);

Curve::new(surface, path, global_form, objects)
objects
.curves
.insert(Curve::new(surface, path, global_form))
});

Some(Self {
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/algorithms/reverse/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ impl Reverse for Handle<Cycle> {

edges.reverse();

Cycle::new(surface, edges, objects)
objects.cycles.insert(Cycle::new(surface, edges))
}
}
4 changes: 3 additions & 1 deletion crates/fj-kernel/src/algorithms/reverse/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ impl Reverse for Handle<HalfEdge> {
[b, a]
};

HalfEdge::new(vertices, self.global_form().clone(), objects)
objects
.half_edges
.insert(HalfEdge::new(vertices, self.global_form().clone()))
}
}
51 changes: 25 additions & 26 deletions crates/fj-kernel/src/algorithms/sweep/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,11 @@ impl Sweep for (Handle<HalfEdge>, Color) {
points_curve_and_surface,
));

Curve::new(
objects.curves.insert(Curve::new(
surface.clone(),
path,
edge.curve().global_form().clone(),
objects,
)
))
};

let vertices = {
Expand All @@ -64,24 +63,26 @@ impl Sweep for (Handle<HalfEdge>, Color) {

vertices.each_ref_ext().zip_ext(points_surface).map(
|(vertex, point_surface)| {
let surface_vertex = SurfaceVertex::new(
point_surface,
surface.clone(),
vertex.global_form().clone(),
objects,
let surface_vertex = objects.surface_vertices.insert(
SurfaceVertex::new(
point_surface,
surface.clone(),
vertex.global_form().clone(),
),
);

Vertex::new(
objects.vertices.insert(Vertex::new(
vertex.position(),
curve.clone(),
surface_vertex,
objects,
)
))
},
)
};

HalfEdge::new(vertices, edge.global_form().clone(), objects)
objects
.half_edges
.insert(HalfEdge::new(vertices, edge.global_form().clone()))
};

let side_edges =
Expand Down Expand Up @@ -117,30 +118,30 @@ impl Sweep for (Handle<HalfEdge>, Color) {
points_curve_and_surface,
));

Curve::new(surface.clone(), path, global, objects)
objects
.curves
.insert(Curve::new(surface.clone(), path, global))
};

let global = GlobalEdge::new(
let global = objects.global_edges.insert(GlobalEdge::new(
curve.global_form().clone(),
surface_vertices
.clone()
.map(|surface_vertex| surface_vertex.global_form().clone()),
objects,
);
));

let vertices = bottom_vertices
.each_ref_ext()
.zip_ext(surface_vertices)
.map(|(vertex, surface_form)| {
Vertex::new(
objects.vertices.insert(Vertex::new(
vertex.position(),
curve.clone(),
surface_form,
objects,
)
))
});

HalfEdge::new(vertices, global, objects)
objects.half_edges.insert(HalfEdge::new(vertices, global))
};

let cycle = {
Expand Down Expand Up @@ -170,7 +171,7 @@ impl Sweep for (Handle<HalfEdge>, Color) {
i += 1;
}

Cycle::new(surface, edges, objects)
objects.cycles.insert(Cycle::new(surface, edges))
};

Ok(Face::builder(objects)
Expand Down Expand Up @@ -247,11 +248,9 @@ mod tests {
.build(&objects)?
.reverse(&objects);

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

Face::builder(&objects).with_exterior(cycle).build()
};
Expand Down
31 changes: 18 additions & 13 deletions crates/fj-kernel/src/algorithms/sweep/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,11 @@ impl Sweep for (Handle<Vertex>, Handle<Surface>) {
let curve = {
let line = Line::from_points(points_surface);

Curve::new(
objects.curves.insert(Curve::new(
surface.clone(),
SurfacePath::Line(line),
edge_global.curve().clone(),
objects,
)
))
};

let vertices_surface = {
Expand All @@ -102,23 +101,28 @@ impl Sweep for (Handle<Vertex>, Handle<Surface>) {

[
vertex.surface_form().clone(),
SurfaceVertex::new(position, surface, global_form, objects),
objects.surface_vertices.insert(SurfaceVertex::new(
position,
surface,
global_form,
)),
]
};

// And now the vertices. Again, nothing wild here.
let vertices = vertices_surface.map(|surface_form| {
Vertex::new(
objects.vertices.insert(Vertex::new(
[surface_form.position().v],
curve.clone(),
surface_form,
objects,
)
))
});

// And finally, creating the output `Edge` is just a matter of
// assembling the pieces we've already created.
Ok(HalfEdge::new(vertices, edge_global, objects))
Ok(objects
.half_edges
.insert(HalfEdge::new(vertices, edge_global)))
}
}

Expand All @@ -131,22 +135,23 @@ impl Sweep for Handle<GlobalVertex> {
cache: &mut SweepCache,
objects: &Objects,
) -> Result<Self::Swept, ValidationError> {
let curve = GlobalCurve::new(objects);
let curve = objects.global_curves.insert(GlobalCurve);

let a = self.clone();
let b = cache
.global_vertex
.entry(self.id())
.or_insert_with(|| {
GlobalVertex::from_position(
objects.global_vertices.insert(GlobalVertex::from_position(
self.position() + path.into(),
objects,
)
))
})
.clone();

let vertices = [a, b];
let global_edge = GlobalEdge::new(curve, vertices.clone(), objects);
let global_edge = objects
.global_edges
.insert(GlobalEdge::new(curve, vertices.clone()));

// The vertices of the returned `GlobalEdge` are in normalized order,
// which means the order can't be relied upon by the caller. Return the
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/algorithms/transform/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl TransformObject for Handle<GlobalCurve> {
// All we need to do here is create a new `GlobalCurve` instance, to
// make sure the transformed `GlobalCurve` has a different identity than
// the original one.
Ok(GlobalCurve::new(objects))
Ok(objects.global_curves.insert(GlobalCurve))
}
}

Expand Down
4 changes: 3 additions & 1 deletion crates/fj-kernel/src/builder/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ impl<'a> FaceBuilder<'a> {
.expect("Can't build `Face` without exterior cycle");
let color = self.color.unwrap_or_default();

Face::new(exterior, self.interiors, color, self.objects)
self.objects
.faces
.insert(Face::new(exterior, self.interiors, color))
}
}
6 changes: 4 additions & 2 deletions crates/fj-kernel/src/builder/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,9 @@ impl<'a> ShellBuilder<'a> {
}

Face::builder(self.objects)
.with_exterior(Cycle::new(surface, edges, self.objects))
.with_exterior(
self.objects.cycles.insert(Cycle::new(surface, edges)),
)
.build()
};

Expand All @@ -271,6 +273,6 @@ impl<'a> ShellBuilder<'a> {

/// Build the [`Shell`]
pub fn build(self) -> Handle<Shell> {
Shell::new(self.faces, self.objects)
self.objects.shells.insert(Shell::new(self.faces))
}
}
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/builder/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ impl<'a> SketchBuilder<'a> {

/// Build the [`Sketch`]
pub fn build(self) -> Handle<Sketch> {
Sketch::new(self.faces, self.objects)
self.objects.sketches.insert(Sketch::new(self.faces))
}
}
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/builder/solid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ impl<'a> SolidBuilder<'a> {

/// Build the [`Solid`]
pub fn build(self) -> Handle<Solid> {
Solid::new(self.shells, self.objects)
self.objects.solids.insert(Solid::new(self.shells))
}
}
20 changes: 14 additions & 6 deletions crates/fj-kernel/src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ mod tests {
fn global_curve() {
let objects = Objects::new();

let object = GlobalCurve::new(&objects);
let object = objects.global_curves.insert(GlobalCurve);

assert_eq!(0, object.curve_iter().count());
assert_eq!(0, object.cycle_iter().count());
Expand All @@ -462,7 +462,9 @@ mod tests {
fn global_vertex() {
let objects = Objects::new();

let object = GlobalVertex::from_position([0., 0., 0.], &objects);
let object = objects
.global_vertices
.insert(GlobalVertex::from_position([0., 0., 0.]));

assert_eq!(0, object.curve_iter().count());
assert_eq!(0, object.cycle_iter().count());
Expand Down Expand Up @@ -593,10 +595,16 @@ mod tests {
.with_surface(Some(surface.clone()))
.as_u_axis()
.build(&objects)?;
let global_vertex = GlobalVertex::from_position([0., 0., 0.], &objects);
let surface_vertex =
SurfaceVertex::new([0., 0.], surface, global_vertex, &objects);
let object = Vertex::new([0.], curve, surface_vertex, &objects);
let global_vertex = objects
.global_vertices
.insert(GlobalVertex::from_position([0., 0., 0.]));
let surface_vertex = objects
.surface_vertices
.insert(SurfaceVertex::new([0., 0.], surface, global_vertex));
let object =
objects
.vertices
.insert(Vertex::new([0.], curve, surface_vertex));

assert_eq!(1, object.curve_iter().count());
assert_eq!(0, object.cycle_iter().count());
Expand Down
16 changes: 4 additions & 12 deletions crates/fj-kernel/src/objects/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
storage::{Handle, HandleWrapper},
};

use super::{Objects, Surface};
use super::Surface;

/// A curve, defined in local surface coordinates
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
Expand All @@ -19,13 +19,12 @@ impl Curve {
surface: Handle<Surface>,
path: SurfacePath,
global_form: impl Into<HandleWrapper<GlobalCurve>>,
objects: &Objects,
) -> Handle<Self> {
objects.curves.insert(Self {
) -> Self {
Self {
surface,
path,
global_form: global_form.into(),
})
}
}

/// Access the path that defines this curve
Expand All @@ -47,10 +46,3 @@ impl Curve {
/// A curve, defined in global (3D) coordinates
#[derive(Clone, Copy, Debug)]
pub struct GlobalCurve;

impl GlobalCurve {
/// Construct a new instance of `Handle` and add it to the store
pub fn new(objects: &Objects) -> Handle<Self> {
objects.global_curves.insert(GlobalCurve)
}
}
9 changes: 4 additions & 5 deletions crates/fj-kernel/src/objects/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use pretty_assertions::assert_eq;

use crate::{path::SurfacePath, storage::Handle};

use super::{HalfEdge, Objects, Surface};
use super::{HalfEdge, Surface};

/// A cycle of connected half-edges
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
Expand All @@ -23,8 +23,7 @@ impl Cycle {
pub fn new(
surface: Handle<Surface>,
half_edges: impl IntoIterator<Item = Handle<HalfEdge>>,
objects: &Objects,
) -> Handle<Self> {
) -> Self {
let half_edges = half_edges.into_iter().collect::<Vec<_>>();

// Verify, that the curves of all edges are defined in the correct
Expand Down Expand Up @@ -65,10 +64,10 @@ impl Cycle {
}
}

objects.cycles.insert(Self {
Self {
surface,
half_edges,
})
}
}

/// Access the surface that this cycle is in
Expand Down
Loading