Give `LocalForm` ownership of the canonical form

We're entering the last stretch of the work towards removing `Shape`.
Now, nothing should really depend on `Shape` any more, so it's a matter
of replacing `Handle`s where we can find them with direct ownership of
the previously referred to object.
This commit is contained in:
Hanno Braun 2022-06-28 17:50:45 +02:00
parent a17e400b2b
commit 68da99913a
16 changed files with 106 additions and 113 deletions

View File

@ -13,7 +13,7 @@ pub fn approx_edge(
// the same vertex would be understood to refer to very close, but distinct
// vertices.
let vertices = vertices.convert(|vertex| {
geometry::Point::new(*vertex.local(), vertex.canonical().get().point)
geometry::Point::new(*vertex.local(), vertex.canonical().point)
});
if let Some([a, b]) = vertices {
points.insert(0, a);
@ -49,8 +49,8 @@ mod test {
let c = Point::from([3., 5., 8.]);
let d = Point::from([5., 8., 13.]);
let v1 = Vertex::builder(&mut shape).build_from_point(a);
let v2 = Vertex::builder(&mut shape).build_from_point(d);
let v1 = Vertex::builder(&mut shape).build_from_point(a).get();
let v2 = Vertex::builder(&mut shape).build_from_point(d).get();
let vertices = VerticesOfEdge::from_vertices([
LocalForm::new(Point::from([0.]), v1),

View File

@ -166,17 +166,15 @@ fn create_non_continuous_side_face(
let [[a, b], [c, d]] = [vertices_bottom, vertices_top];
let vertices = if is_sweep_along_negative_direction {
if is_sweep_along_negative_direction {
[b, a, c, d]
} else {
[a, b, d, c]
};
vertices.map(|vertex| tmp.get_handle_or_insert(vertex))
}
};
let surface = {
let [a, b, _, c] = vertices.clone().map(|vertex| vertex.get().point);
let [a, b, _, c] = vertices.map(|vertex| vertex.point);
Surface::plane_from_points([a, b, c])
};
let surface = tmp.get_handle_or_insert(surface);
@ -201,16 +199,15 @@ fn create_non_continuous_side_face(
let curve = {
let local = Curve::line_from_points([a.0, b.0]);
let global = [a, b].map(|vertex| vertex.1.get().point);
let global = [a, b].map(|vertex| vertex.1.point);
let global = Curve::line_from_points(global);
let global = tmp.get_handle_or_insert(global);
LocalForm::new(local, global)
};
let vertices = VerticesOfEdge::from_vertices([
LocalForm::new(Point::from([0.]), a.1.clone()),
LocalForm::new(Point::from([1.]), b.1.clone()),
LocalForm::new(Point::from([0.]), a.1),
LocalForm::new(Point::from([1.]), b.1),
]);
let edge = {
@ -223,7 +220,6 @@ fn create_non_continuous_side_face(
curve: LocalForm::canonical_only(curve.canonical()),
vertices,
};
let global = tmp.get_handle_or_insert(global);
LocalForm::new(local, global)
};
@ -236,7 +232,6 @@ fn create_non_continuous_side_face(
let global =
Cycle::new(local.edges.iter().map(|edge| edge.canonical()));
let global = tmp.get_handle_or_insert(global);
LocalForm::new(local, global)
};
@ -257,8 +252,6 @@ fn create_continuous_side_face(
) {
let translation = Transform::translation(path);
let mut tmp = Shape::new();
let edge = tmp.merge(edge);
let cycle = Cycle::new(vec![edge]);
let approx = CycleApprox::new(&cycle, tolerance);

View File

@ -49,8 +49,6 @@ pub fn transform_cycles(
cycles: &CyclesInFace,
transform: &Transform,
) -> CyclesInFace {
let mut tmp = Shape::new();
let cycles = cycles.as_local_form().map(|cycle| {
let edges_local = cycle
.local()
@ -58,65 +56,62 @@ pub fn transform_cycles(
.iter()
.map(|edge| {
let curve_local = *edge.local().curve.local();
let curve_canonical = tmp
.merge(edge.canonical().get().curve().transform(transform));
let curve_canonical =
edge.canonical().curve().transform(transform);
let vertices = edge.canonical().get().vertices.map(|vertex| {
let point = vertex.canonical().get().point;
let vertices = edge.canonical().vertices.map(|vertex| {
let point = vertex.canonical().point;
let point = transform.transform_point(&point);
let local = *vertex.local();
let canonical = tmp.merge(Vertex { point });
let canonical = Vertex { point };
LocalForm::new(local, canonical)
});
let edge_local = Edge {
curve: LocalForm::new(curve_local, curve_canonical.clone()),
curve: LocalForm::new(curve_local, curve_canonical),
vertices: vertices.clone(),
};
let edge_canonical = tmp.merge(Edge {
let edge_canonical = Edge {
curve: LocalForm::canonical_only(curve_canonical),
vertices,
});
};
LocalForm::new(edge_local, edge_canonical)
})
.collect();
let edges_canonical = cycle
.canonical()
.get()
.edges
.iter()
.map(|edge| {
let edge = edge.canonical().get();
let edge = edge.canonical();
let curve = {
let curve = edge.curve().transform(transform);
let curve = tmp.merge(curve);
LocalForm::canonical_only(curve)
};
let vertices = edge.vertices.map(|vertex| {
let point = vertex.canonical().get().point;
let point = vertex.canonical().point;
let point = transform.transform_point(&point);
let local = *vertex.local();
let canonical = tmp.merge(Vertex { point });
let canonical = Vertex { point };
LocalForm::new(local, canonical)
});
let edge = tmp.merge(Edge { curve, vertices });
let edge = Edge { curve, vertices };
LocalForm::canonical_only(edge)
})
.collect();
let cycle_local = Cycle { edges: edges_local };
let cycle_canonical = tmp.merge(Cycle {
let cycle_canonical = Cycle {
edges: edges_canonical,
});
};
LocalForm::new(cycle_local, cycle_canonical)
});

View File

@ -51,21 +51,20 @@ impl<'r> EdgeBuilder<'r> {
a: Vector::from([radius, Scalar::ZERO]),
b: Vector::from([Scalar::ZERO, radius]),
});
let curve_canonical =
self.shape.get_handle_or_insert(Curve::Circle(Circle {
center: Point::origin(),
a: Vector::from([radius, Scalar::ZERO, Scalar::ZERO]),
b: Vector::from([Scalar::ZERO, radius, Scalar::ZERO]),
}));
let curve_canonical = Curve::Circle(Circle {
center: Point::origin(),
a: Vector::from([radius, Scalar::ZERO, Scalar::ZERO]),
b: Vector::from([Scalar::ZERO, radius, Scalar::ZERO]),
});
let edge_local = Edge {
curve: LocalForm::new(curve_local, curve_canonical.clone()),
curve: LocalForm::new(curve_local, curve_canonical),
vertices: VerticesOfEdge::none(),
};
let edge_canonical = self.shape.get_handle_or_insert(Edge {
let edge_canonical = Edge {
curve: LocalForm::canonical_only(curve_canonical),
vertices: VerticesOfEdge::none(),
});
};
LocalForm::new(edge_local, edge_canonical)
}
@ -90,12 +89,9 @@ impl<'r> EdgeBuilder<'r> {
) -> Handle<Edge<3>> {
let curve = {
let points = [a, b].map(|vertex| vertex.point);
let curve = Curve::Line(Line::from_points(points));
self.shape.get_handle_or_insert(curve)
Curve::Line(Line::from_points(points))
};
let [a, b] = [a, b].map(|vertex| self.shape.insert(vertex));
let vertices = [
LocalForm::new(Point::from([0.]), a),
LocalForm::new(Point::from([1.]), b),
@ -144,14 +140,15 @@ impl<'r> CycleBuilder<'r> {
let points_canonical = points
.map(|point| self.surface.point_from_surface_coords(point));
let edge_canonical = Edge::builder(self.shape)
.build_line_segment_from_points(points_canonical);
.build_line_segment_from_points(points_canonical)
.get();
let edge_local = Edge {
curve: LocalForm::new(
Curve::Line(Line::from_points(points)),
edge_canonical.get().curve.canonical(),
edge_canonical.curve.canonical(),
),
vertices: edge_canonical.get().vertices,
vertices: edge_canonical.vertices.clone(),
};
edges.push(LocalForm::new(edge_local, edge_canonical));
@ -162,8 +159,7 @@ impl<'r> CycleBuilder<'r> {
};
let edges_canonical = edges.into_iter().map(|edge| edge.canonical());
let canonical =
self.shape.get_handle_or_insert(Cycle::new(edges_canonical));
let canonical = Cycle::new(edges_canonical);
LocalForm::new(local, canonical)
}

View File

@ -463,8 +463,7 @@ mod tests {
let mut shape = Shape::new();
let cycle = Cycle::builder(Surface::xy_plane(), &mut shape)
.build_polygon([[0., 0.], [1., 0.], [0., 1.]])
.canonical()
.get();
.canonical();
assert_eq!(3, cycle.curve_iter().count());
assert_eq!(1, cycle.cycle_iter().count());

View File

@ -1,6 +1,6 @@
use crate::{
builder::CycleBuilder,
shape::{Handle, LocalForm, Shape},
shape::{LocalForm, Shape},
};
use super::{Edge, Surface};
@ -28,7 +28,7 @@ pub struct Cycle<const D: usize> {
impl Cycle<3> {
/// Construct a `Cycle`
pub fn new(edges: impl IntoIterator<Item = Handle<Edge<3>>>) -> Self {
pub fn new(edges: impl IntoIterator<Item = Edge<3>>) -> Self {
let edges = edges.into_iter().map(LocalForm::canonical_only).collect();
Self { edges }
@ -44,6 +44,6 @@ impl Cycle<3> {
/// This is a convenience method that saves the caller from dealing with the
/// [`Handle`]s.
pub fn edges(&self) -> impl Iterator<Item = Edge<3>> + '_ {
self.edges.iter().map(|handle| handle.canonical().get())
self.edges.iter().map(|handle| handle.canonical())
}
}

View File

@ -43,7 +43,7 @@ impl<const D: usize> Edge<D> {
/// This is a convenience method that saves the caller from dealing with the
/// [`Handle`].
pub fn curve(&self) -> Curve<3> {
self.curve.canonical().get()
self.curve.canonical()
}
/// Access the vertices that the edge refers to
@ -54,7 +54,7 @@ impl<const D: usize> Edge<D> {
self.vertices
.0
.as_ref()
.map(|[a, b]| [a.canonical().get(), b.canonical().get()])
.map(|[a, b]| [a.canonical(), b.canonical()])
}
}
@ -114,7 +114,7 @@ impl VerticesOfEdge {
[a.canonical(), b.canonical()]
};
return [a.clone(), b.clone()] == other || [b, a] == other;
return [a, b] == other || [b, a] == other;
}
}

View File

@ -211,7 +211,7 @@ impl CyclesInFace {
/// Access an iterator over the canonical forms of the cycles
pub fn as_canonical(&self) -> impl Iterator<Item = Cycle<3>> + '_ {
self.0.iter().map(|cycle| cycle.canonical().get())
self.0.iter().map(|cycle| cycle.canonical())
}
/// Access an iterator over local forms of the cycles

View File

@ -177,7 +177,7 @@ mod tests {
let vertex = Vertex { point };
let edge = Edge {
curve: LocalForm::canonical_only(curve),
curve: LocalForm::canonical_only(curve.get()),
vertices: VerticesOfEdge::none(),
};
@ -190,7 +190,7 @@ mod tests {
assert!(shape.get_handle(&vertex.get()).as_ref() == Some(&vertex));
assert!(shape.get_handle(&edge.get()).as_ref() == Some(&edge));
let cycle = Cycle::new(vec![edge]);
let cycle = Cycle::new(vec![edge.get()]);
assert!(shape.get_handle(&cycle).is_none());
let cycle = shape.insert(cycle);

View File

@ -1,6 +1,6 @@
use std::hash::{Hash, Hasher};
use super::{Handle, Object};
use super::Object;
/// A reference to an object, which includes a local form
///
@ -17,7 +17,7 @@ use super::{Handle, Object};
#[derive(Clone, Debug, Eq, Ord, PartialOrd)]
pub struct LocalForm<Local, Canonical: Object> {
local: Local,
canonical: Handle<Canonical>,
canonical: Canonical,
}
impl<Local, Canonical: Object> LocalForm<Local, Canonical> {
@ -25,7 +25,7 @@ impl<Local, Canonical: Object> LocalForm<Local, Canonical> {
///
/// It is the caller's responsibility to make sure that the local and
/// canonical forms passed to this method actually match.
pub fn new(local: Local, canonical: Handle<Canonical>) -> Self {
pub fn new(local: Local, canonical: Canonical) -> Self {
Self { local, canonical }
}
@ -35,7 +35,7 @@ impl<Local, Canonical: Object> LocalForm<Local, Canonical> {
}
/// Access the canonical form of the referenced object
pub fn canonical(&self) -> Handle<Canonical> {
pub fn canonical(&self) -> Canonical {
self.canonical.clone()
}
}
@ -46,8 +46,8 @@ impl<Canonical: Object> LocalForm<Canonical, Canonical> {
/// It's possible that an object's local and canonical forms are the same.
/// This is a convenience constructor that constructs a `LocalForm` instance
/// for such a situation.
pub fn canonical_only(canonical: Handle<Canonical>) -> Self {
Self::new(canonical.get(), canonical)
pub fn canonical_only(canonical: Canonical) -> Self {
Self::new(canonical.clone(), canonical)
}
}
@ -57,8 +57,7 @@ where
Canonical: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
self.local == other.local
&& self.canonical.get() == other.canonical.get()
self.local == other.local && self.canonical == other.canonical
}
}
@ -69,6 +68,6 @@ where
{
fn hash<H: Hasher>(&self, state: &mut H) {
self.local.hash(state);
self.canonical.get().hash(state);
self.canonical.hash(state);
}
}

View File

@ -44,12 +44,12 @@ impl Object for Edge<3> {
let vertices = self.vertices.convert(|vertex| {
let canonical = vertex.canonical();
let canonical = canonical.get().merge_into(shape);
LocalForm::new(*vertex.local(), canonical)
let canonical = canonical.merge_into(shape);
LocalForm::new(*vertex.local(), canonical.get())
});
shape.get_handle_or_insert(Edge {
curve: LocalForm::canonical_only(curve),
curve: LocalForm::canonical_only(curve.get()),
vertices: VerticesOfEdge::new(vertices),
})
}
@ -60,8 +60,8 @@ impl Object for Cycle<3> {
let mut edges = Vec::new();
for edge in self.edges {
let edge = edge.canonical();
let edge = edge.get().merge_into(shape);
edges.push(edge);
let edge = edge.merge_into(shape);
edges.push(edge.get());
}
shape.get_handle_or_insert(Cycle::new(edges))
@ -76,14 +76,20 @@ impl Object for Face {
let mut exts = Vec::new();
for cycle in face.exteriors.as_local_form() {
let merged = cycle.canonical().get().merge_into(shape);
exts.push(LocalForm::new(cycle.local().clone(), merged));
let merged = cycle.canonical().merge_into(shape);
exts.push(LocalForm::new(
cycle.local().clone(),
merged.get(),
));
}
let mut ints = Vec::new();
for cycle in face.interiors.as_local_form() {
let merged = cycle.canonical().get().merge_into(shape);
ints.push(LocalForm::new(cycle.local().clone(), merged));
let merged = cycle.canonical().merge_into(shape);
ints.push(LocalForm::new(
cycle.local().clone(),
merged.get(),
));
}
shape.get_handle_or_insert(Face::new(

View File

@ -19,7 +19,7 @@ pub fn validate_edge(
for vertex in edge.vertices.iter() {
let local = *vertex.local();
let local_as_canonical = edge.curve().point_from_curve_coords(local);
let canonical = vertex.canonical().get().point;
let canonical = vertex.canonical().point;
let distance = (local_as_canonical - canonical).magnitude();
if distance > max_distance {

View File

@ -232,18 +232,16 @@ mod tests {
#[test]
fn coherence_edge() {
let mut tmp = Shape::new();
let a = Point::from([0., 0., 0.]);
let b = Point::from([1., 0., 0.]);
let curve = {
let curve = tmp.insert(Curve::line_from_points([a, b]));
let curve = Curve::line_from_points([a, b]);
LocalForm::canonical_only(curve)
};
let a = tmp.insert(Vertex { point: a });
let b = tmp.insert(Vertex { point: b });
let a = Vertex { point: a };
let b = Vertex { point: b };
let deviation = Scalar::from_f64(0.25);
@ -279,15 +277,17 @@ mod tests {
// Trying to refer to edge that is not from the same shape. Should fail.
let edge = Edge::builder(&mut other)
.build_line_segment_from_points([[0., 0., 0.], [1., 0., 0.]]);
.build_line_segment_from_points([[0., 0., 0.], [1., 0., 0.]])
.get();
shape.insert(Cycle::new(vec![edge.clone()]));
let err =
validate(shape.clone(), &ValidationConfig::default()).unwrap_err();
assert!(err.missing_edge(&edge.get()));
assert!(err.missing_edge(&edge));
// Referring to edge that *is* from the same shape. Should work.
let edge = Edge::builder(&mut shape)
.build_line_segment_from_points([[0., 0., 0.], [1., 0., 0.]]);
.build_line_segment_from_points([[0., 0., 0.], [1., 0., 0.]])
.get();
shape.insert(Cycle::new(vec![edge]));
}
@ -296,27 +296,35 @@ mod tests {
let mut shape = Shape::new();
let mut other = Shape::new();
let curve = other.insert(Curve::x_axis());
let a = Vertex::builder(&mut other).build_from_point([1., 0., 0.]);
let b = Vertex::builder(&mut other).build_from_point([2., 0., 0.]);
let curve = Curve::x_axis();
let a = Vertex::builder(&mut other)
.build_from_point([1., 0., 0.])
.get();
let b = Vertex::builder(&mut other)
.build_from_point([2., 0., 0.])
.get();
let a = LocalForm::new(Point::from([1.]), a);
let b = LocalForm::new(Point::from([2.]), b);
// Shouldn't work. Nothing has been added to `shape`.
shape.insert(Edge {
curve: LocalForm::canonical_only(curve.clone()),
curve: LocalForm::canonical_only(curve),
vertices: VerticesOfEdge::from_vertices([a.clone(), b.clone()]),
});
let err =
validate(shape.clone(), &ValidationConfig::default()).unwrap_err();
assert!(err.missing_curve(&curve.get()));
assert!(err.missing_vertex(&a.canonical().get()));
assert!(err.missing_vertex(&b.canonical().get()));
assert!(err.missing_curve(&curve));
assert!(err.missing_vertex(&a.canonical()));
assert!(err.missing_vertex(&b.canonical()));
let curve = shape.insert(Curve::x_axis());
let a = Vertex::builder(&mut shape).build_from_point([1., 0., 0.]);
let b = Vertex::builder(&mut shape).build_from_point([2., 0., 0.]);
let curve = Curve::x_axis();
let a = Vertex::builder(&mut shape)
.build_from_point([1., 0., 0.])
.get();
let b = Vertex::builder(&mut shape)
.build_from_point([2., 0., 0.])
.get();
let a = LocalForm::new(Point::from([1.]), a);
let b = LocalForm::new(Point::from([2.]), b);
@ -349,7 +357,7 @@ mod tests {
let err =
validate(shape.clone(), &ValidationConfig::default()).unwrap_err();
assert!(err.missing_surface(&surface.get()));
assert!(err.missing_cycle(&cycle.canonical().get()));
assert!(err.missing_cycle(&cycle.canonical()));
let surface = shape.insert(Surface::xy_plane());
let cycle =

View File

@ -11,7 +11,7 @@ pub fn validate_edge(
let mut missing_vertices = HashSet::new();
if !curves.contains(&edge.curve()) {
missing_curve = Some(edge.curve.canonical().get());
missing_curve = Some(edge.curve.canonical());
}
for vertex in edge.vertices().into_iter().flatten() {
if !vertices.contains(&vertex) {

View File

@ -27,7 +27,7 @@ impl ToShape for fj::Circle {
let cycle_local = Cycle {
edges: vec![edge.clone()],
};
let cycle_canonical = tmp.insert(Cycle::new(vec![edge.canonical()]));
let cycle_canonical = Cycle::new(vec![edge.canonical()]);
let surface = tmp.insert(Surface::xy_plane());
let face = tmp

View File

@ -3,7 +3,7 @@ use fj_kernel::{
algorithms::Tolerance,
iter::ObjectIters,
objects::{Cycle, Edge, Face},
shape::{LocalForm, Shape},
shape::LocalForm,
validation::{validate, Validated, ValidationConfig, ValidationError},
};
use fj_math::Aabb;
@ -95,8 +95,6 @@ fn add_cycle(
cycle: LocalForm<Cycle<2>, Cycle<3>>,
reverse: bool,
) -> LocalForm<Cycle<2>, Cycle<3>> {
let mut tmp = Shape::new();
let mut edges = Vec::new();
for edge in cycle.local().edges.clone() {
let curve_local = *edge.local().curve.local();
@ -106,13 +104,12 @@ fn add_cycle(
curve_local
};
let curve_canonical = edge.canonical().get().curve();
let curve_canonical = edge.canonical().curve();
let curve_canonical = if reverse {
curve_canonical.reverse()
} else {
curve_canonical
};
let curve_canonical = tmp.insert(curve_canonical);
let vertices = if reverse {
edge.local().vertices.clone().reverse()
@ -121,13 +118,13 @@ fn add_cycle(
};
let edge_local = Edge {
curve: LocalForm::new(curve_local, curve_canonical.clone()),
curve: LocalForm::new(curve_local, curve_canonical),
vertices: vertices.clone(),
};
let edge_canonical = tmp.merge(Edge {
let edge_canonical = Edge {
curve: LocalForm::canonical_only(curve_canonical),
vertices,
});
};
edges.push(LocalForm::new(edge_local, edge_canonical));
}
@ -140,7 +137,7 @@ fn add_cycle(
edges: edges.clone(),
};
let cycle_canonical =
tmp.insert(Cycle::new(edges.into_iter().map(|edge| edge.canonical())));
Cycle::new(edges.into_iter().map(|edge| edge.canonical()));
LocalForm::new(cycle_local, cycle_canonical)
}