Skip to content

Commit

Permalink
Require &mut Instance in Insert::insert
Browse files Browse the repository at this point in the history
This makes calling it more convenient (as all code already has a `&mut
Instance` available directly, and with this change no longer has to
access the field).

It also has the advantage of not exposing `Instance` internals to all
code calling `insert`.
  • Loading branch information
hannobraun committed Feb 12, 2024
1 parent a78d90a commit 4d098e6
Show file tree
Hide file tree
Showing 35 changed files with 166 additions and 203 deletions.
8 changes: 4 additions & 4 deletions crates/fj-core/src/algorithms/approx/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ mod tests {
fn approx_line_on_flat_surface() {
let mut core = Instance::new();

let curve = Curve::new().insert(&mut core.services);
let curve = Curve::new().insert(&mut core);
let (surface_path, boundary) =
SurfacePath::line_from_points([[1., 1.], [2., 1.]]);
let boundary = CurveBoundary::from(boundary);
Expand All @@ -205,7 +205,7 @@ mod tests {
fn approx_line_on_curved_surface_but_not_along_curve() {
let mut core = Instance::new();

let curve = Curve::new().insert(&mut core.services);
let curve = Curve::new().insert(&mut core);
let (surface_path, boundary) =
SurfacePath::line_from_points([[1., 1.], [2., 1.]]);
let boundary = CurveBoundary::from(boundary);
Expand All @@ -226,7 +226,7 @@ mod tests {
let mut core = Instance::new();

let global_path = GlobalPath::circle_from_radius(1.);
let curve = Curve::new().insert(&mut core.services);
let curve = Curve::new().insert(&mut core);
let surface_path = SurfacePath::line_from_points_with_coords([
([0.], [0., 1.]),
([TAU], [TAU, 1.]),
Expand Down Expand Up @@ -259,7 +259,7 @@ mod tests {
fn approx_circle_on_flat_surface() {
let mut core = Instance::new();

let curve = Curve::new().insert(&mut core.services);
let curve = Curve::new().insert(&mut core);
let surface_path =
SurfacePath::circle_from_center_and_radius([0., 0.], 1.);
let boundary = CurveBoundary::from([[0.], [TAU]]);
Expand Down
3 changes: 1 addition & 2 deletions crates/fj-core/src/operations/build/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ pub trait BuildCycle {
.map(Into::into)
.circular_tuple_windows()
.map(|(start, end)| {
HalfEdge::line_segment([start, end], None, core)
.insert(&mut core.services)
HalfEdge::line_segment([start, end], None, core).insert(core)
});

Cycle::new(edges)
Expand Down
8 changes: 4 additions & 4 deletions crates/fj-core/src/operations/build/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use crate::{
pub trait BuildFace {
/// Build a face with an empty exterior, no interiors, and no color
fn unbound(surface: Handle<Surface>, core: &mut Instance) -> Face {
let exterior = Cycle::empty().insert(&mut core.services);
let region = Region::new(exterior, [], None).insert(&mut core.services);
let exterior = Cycle::empty().insert(core);
let region = Region::new(exterior, [], None).insert(core);
Face::new(surface, region)
}

Expand All @@ -32,7 +32,7 @@ pub trait BuildFace {
core: &mut Instance,
) -> Polygon<3> {
let (surface, points_surface) = Surface::plane_from_points(points);
let surface = surface.insert(&mut core.services);
let surface = surface.insert(core);

let face = Face::polygon(surface, points_surface, core);

Expand Down Expand Up @@ -70,7 +70,7 @@ pub trait BuildFace {
Ps: IntoIterator<Item = P>,
Ps::IntoIter: Clone + ExactSizeIterator,
{
let region = Region::polygon(points, core).insert(&mut core.services);
let region = Region::polygon(points, core).insert(core);
Face::new(surface, region)
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/fj-core/src/operations/build/half_edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ pub trait BuildHalfEdge {
boundary: impl Into<CurveBoundary<Point<1>>>,
core: &mut Instance,
) -> HalfEdge {
let curve = Curve::new().insert(&mut core.services);
let start_vertex = Vertex::new().insert(&mut core.services);
let curve = Curve::new().insert(core);
let start_vertex = Vertex::new().insert(core);

HalfEdge::new(path, boundary, curve, start_vertex)
}
Expand Down
7 changes: 3 additions & 4 deletions crates/fj-core/src/operations/build/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
pub trait BuildRegion {
/// Build an empty region
fn empty(core: &mut Instance) -> Region {
let exterior = Cycle::empty().insert(&mut core.services);
let exterior = Cycle::empty().insert(core);
let interiors = [];
let color = None;

Expand All @@ -27,8 +27,7 @@ pub trait BuildRegion {
radius: impl Into<Scalar>,
core: &mut Instance,
) -> Region {
let exterior =
Cycle::circle(center, radius, core).insert(&mut core.services);
let exterior = Cycle::circle(center, radius, core).insert(core);
Region::new(exterior, [], None)
}

Expand All @@ -39,7 +38,7 @@ pub trait BuildRegion {
Ps: IntoIterator<Item = P>,
Ps::IntoIter: Clone + ExactSizeIterator,
{
let exterior = Cycle::polygon(points, core).insert(&mut core.services);
let exterior = Cycle::polygon(points, core).insert(core);
Region::new(exterior, [], None)
}
}
Expand Down
11 changes: 5 additions & 6 deletions crates/fj-core/src/operations/build/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub trait BuildShell {
.into_iter()
.enumerate()
.map(|(index, position)| {
let vertex = Vertex::new().insert(&mut core.services);
let vertex = Vertex::new().insert(core);
let position = position.into();

(index, (vertex, position))
Expand All @@ -57,7 +57,7 @@ pub trait BuildShell {
let (surface, _) = Surface::plane_from_points(
[a_pos, b_pos, c_pos].map(Clone::clone),
);
let surface = surface.insert(&mut core.services);
let surface = surface.insert(core);

let curves_and_boundaries =
[[a, b], [b, c], [c, a]].map(|vertices| {
Expand All @@ -68,8 +68,7 @@ pub trait BuildShell {
.get(&vertices.clone().reverse())
.cloned()
.unwrap_or_else(|| {
let curve =
Curve::new().insert(&mut core.services);
let curve = Curve::new().insert(core);
let boundary =
CurveBoundary::<Point<1>>::from([
[0.],
Expand Down Expand Up @@ -262,8 +261,8 @@ pub trait BuildShell {
core,
);

let triangles = [abc, bad, dac, cbd]
.map(|triangle| triangle.insert(&mut core.services));
let triangles =
[abc, bad, dac, cbd].map(|triangle| triangle.insert(core));
let shell =
Shell::new(triangles.iter().map(|triangle| triangle.face.clone()));

Expand Down
2 changes: 1 addition & 1 deletion crates/fj-core/src/operations/build/solid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub trait BuildSolid {
points: [impl Into<Point<3>>; 4],
core: &mut Instance,
) -> Tetrahedron {
let shell = Shell::tetrahedron(points, core).insert(&mut core.services);
let shell = Shell::tetrahedron(points, core).insert(core);
let solid = Solid::empty().add_shells([shell.shell.clone()], core);

Tetrahedron { solid, shell }
Expand Down
6 changes: 3 additions & 3 deletions crates/fj-core/src/operations/holes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ impl AddHole for Shell {
path: impl Into<Vector<3>>,
core: &mut Instance,
) -> Self {
let entry = HalfEdge::circle(location.position, radius, core)
.insert(&mut core.services);
let entry =
HalfEdge::circle(location.position, radius, core).insert(core);
let hole = Region::empty(core)
.update_exterior(
|_, core| Cycle::empty().add_half_edges([entry.clone()], core),
Expand Down Expand Up @@ -94,7 +94,7 @@ impl AddHole for Shell {
let radius = radius.into();

let entry = HalfEdge::circle(entry_location.position, radius, core)
.insert(&mut core.services);
.insert(core);

let path = {
let point = |location: &HoleLocation| {
Expand Down
20 changes: 10 additions & 10 deletions crates/fj-core/src/operations/insert/insert_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::{
Vertex,
},
operations::build::{Polygon, TetrahedronShell},
services::Services,
storage::Handle,
Instance,
};

use super::{IsInsertedNo, IsInsertedYes};
Expand All @@ -28,7 +28,7 @@ pub trait Insert: Sized {
/// specific reason to do so, and you are handling validation errors in a
/// non-standard way.
#[must_use]
fn insert(self, services: &mut Services) -> Self::Inserted;
fn insert(self, core: &mut Instance) -> Self::Inserted;
}

macro_rules! impl_insert {
Expand All @@ -37,10 +37,10 @@ macro_rules! impl_insert {
impl Insert for $ty {
type Inserted = Handle<Self>;

fn insert(self, services: &mut Services) -> Self::Inserted {
let handle = services.objects.$store.reserve();
fn insert(self, core: &mut Instance) -> Self::Inserted {
let handle = core.services.objects.$store.reserve();
let object = (handle.clone(), self).into();
services.insert_object(object);
core.services.insert_object(object);
handle
}
}
Expand Down Expand Up @@ -70,17 +70,17 @@ where
{
type Inserted = Self;

fn insert(self, _: &mut Services) -> Self::Inserted {
fn insert(self, _: &mut Instance) -> Self::Inserted {
self
}
}

impl<const D: usize> Insert for Polygon<D, IsInsertedNo> {
type Inserted = Polygon<D, IsInsertedYes>;

fn insert(self, services: &mut Services) -> Self::Inserted {
fn insert(self, core: &mut Instance) -> Self::Inserted {
Polygon {
face: self.face.insert(services),
face: self.face.insert(core),
half_edges: self.half_edges,
vertices: self.vertices,
}
Expand All @@ -90,9 +90,9 @@ impl<const D: usize> Insert for Polygon<D, IsInsertedNo> {
impl Insert for TetrahedronShell<IsInsertedNo> {
type Inserted = TetrahedronShell<IsInsertedYes>;

fn insert(self, services: &mut Services) -> Self::Inserted {
fn insert(self, core: &mut Instance) -> Self::Inserted {
TetrahedronShell {
shell: self.shell.insert(services),
shell: self.shell.insert(core),
abc: self.abc,
bad: self.bad,
dac: self.dac,
Expand Down
14 changes: 7 additions & 7 deletions crates/fj-core/src/operations/replace/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl ReplaceCurve for Cycle {
replacement_happened |= half_edge.was_updated();
half_edges.push(
half_edge
.map_updated(|updated| updated.insert(&mut core.services))
.map_updated(|updated| updated.insert(core))
.into_inner(),
);
}
Expand Down Expand Up @@ -92,15 +92,15 @@ impl ReplaceCurve for Region {
replacement_happened |= cycle.was_updated();
interiors.push(
cycle
.map_updated(|updated| updated.insert(&mut core.services))
.map_updated(|updated| updated.insert(core))
.into_inner(),
);
}

if replacement_happened {
ReplaceOutput::Updated(Region::new(
exterior
.map_updated(|updated| updated.insert(&mut core.services))
.map_updated(|updated| updated.insert(core))
.into_inner(),
interiors,
self.color(),
Expand All @@ -127,7 +127,7 @@ impl ReplaceCurve for Sketch {
replacement_happened |= region.was_updated();
regions.push(
region
.map_updated(|updated| updated.insert(&mut core.services))
.map_updated(|updated| updated.insert(core))
.into_inner(),
);
}
Expand All @@ -153,7 +153,7 @@ impl ReplaceCurve for Face {
ReplaceOutput::Updated(Face::new(
self.surface().clone(),
region
.map_updated(|updated| updated.insert(&mut core.services))
.map_updated(|updated| updated.insert(core))
.into_inner(),
))
} else {
Expand All @@ -176,7 +176,7 @@ impl ReplaceCurve for Shell {
let face = face.replace_curve(original, replacement.clone(), core);
replacement_happened |= face.was_updated();
faces.push(
face.map_updated(|updated| updated.insert(&mut core.services))
face.map_updated(|updated| updated.insert(core))
.into_inner(),
);
}
Expand Down Expand Up @@ -205,7 +205,7 @@ impl ReplaceCurve for Solid {
replacement_happened |= shell.was_updated();
shells.push(
shell
.map_updated(|updated| updated.insert(&mut core.services))
.map_updated(|updated| updated.insert(core))
.into_inner(),
);
}
Expand Down
12 changes: 6 additions & 6 deletions crates/fj-core/src/operations/replace/half_edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ impl ReplaceHalfEdge for Region {
replacement_happened |= cycle.was_updated();
interiors.push(
cycle
.map_updated(|updated| updated.insert(&mut core.services))
.map_updated(|updated| updated.insert(core))
.into_inner(),
);
}

if replacement_happened {
ReplaceOutput::Updated(Region::new(
exterior
.map_updated(|updated| updated.insert(&mut core.services))
.map_updated(|updated| updated.insert(core))
.into_inner(),
interiors,
self.color(),
Expand All @@ -100,7 +100,7 @@ impl ReplaceHalfEdge for Sketch {
replacement_happened |= region.was_updated();
regions.push(
region
.map_updated(|updated| updated.insert(&mut core.services))
.map_updated(|updated| updated.insert(core))
.into_inner(),
);
}
Expand Down Expand Up @@ -128,7 +128,7 @@ impl ReplaceHalfEdge for Face {
ReplaceOutput::Updated(Face::new(
self.surface().clone(),
region
.map_updated(|updated| updated.insert(&mut core.services))
.map_updated(|updated| updated.insert(core))
.into_inner(),
))
} else {
Expand All @@ -152,7 +152,7 @@ impl ReplaceHalfEdge for Shell {
face.replace_half_edge(original, replacements.clone(), core);
replacement_happened |= face.was_updated();
faces.push(
face.map_updated(|updated| updated.insert(&mut core.services))
face.map_updated(|updated| updated.insert(core))
.into_inner(),
);
}
Expand Down Expand Up @@ -181,7 +181,7 @@ impl ReplaceHalfEdge for Solid {
replacement_happened |= shell.was_updated();
shells.push(
shell
.map_updated(|updated| updated.insert(&mut core.services))
.map_updated(|updated| updated.insert(core))
.into_inner(),
);
}
Expand Down
Loading

0 comments on commit 4d098e6

Please sign in to comment.