Skip to content

Commit

Permalink
Merge pull request #1521 from hannobraun/simplify
Browse files Browse the repository at this point in the history
Demote `Vertex` from its status as an object, embed it in `HalfEdge`
  • Loading branch information
hannobraun authored Jan 18, 2023
2 parents ceea642 + 71dda8a commit 71d59e2
Show file tree
Hide file tree
Showing 17 changed files with 233 additions and 284 deletions.
7 changes: 4 additions & 3 deletions crates/fj-kernel/src/algorithms/intersect/face_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub enum FacePointIntersection {
PointIsOnEdge(Handle<HalfEdge>),

/// The point is coincident with a vertex
PointIsOnVertex(Handle<Vertex>),
PointIsOnVertex(Vertex),
}

#[cfg(test)]
Expand Down Expand Up @@ -346,10 +346,11 @@ mod tests {
.find(|vertex| {
vertex.surface_form().position() == Point::from([1., 0.])
})
.unwrap();
.unwrap()
.clone();
assert_eq!(
intersection,
Some(FacePointIntersection::PointIsOnVertex(vertex.clone()))
Some(FacePointIntersection::PointIsOnVertex(vertex))
);
}
}
7 changes: 4 additions & 3 deletions crates/fj-kernel/src/algorithms/intersect/ray_face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ pub enum RayFaceIntersection {
RayHitsEdge(Handle<HalfEdge>),

/// The ray hits a vertex
RayHitsVertex(Handle<Vertex>),
RayHitsVertex(Vertex),
}

#[cfg(test)]
Expand Down Expand Up @@ -291,10 +291,11 @@ mod tests {
.find(|vertex| {
vertex.surface_form().position() == Point::from([-1., -1.])
})
.unwrap();
.unwrap()
.clone();
assert_eq!(
(&ray, &face).intersect(),
Some(RayFaceIntersection::RayHitsVertex(vertex.clone()))
Some(RayFaceIntersection::RayHitsVertex(vertex))
);
}

Expand Down
17 changes: 5 additions & 12 deletions crates/fj-kernel/src/algorithms/sweep/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ impl Sweep for (Handle<HalfEdge>, Color) {
curve.clone(),
surface_vertex,
)
.insert(objects)
})
};

Expand Down Expand Up @@ -138,7 +137,6 @@ impl Sweep for (Handle<HalfEdge>, Color) {
.collect::<[_; 2]>()
.map(|(vertex, surface_form)| {
Vertex::new(vertex.position(), curve.clone(), surface_form)
.insert(objects)
});

HalfEdge::new(vertices, global).insert(objects)
Expand Down Expand Up @@ -236,10 +234,8 @@ mod tests {
{
let [back, front] = &mut side_up.vertices;

back.write().surface_form =
bottom.vertices[1].read().surface_form.clone();
back.surface_form = bottom.vertices[1].surface_form.clone();

let mut front = front.write();
let mut front = front.surface_form.write();
front.position = Some([1., 1.].into());
front.surface = surface.clone();
Expand All @@ -257,13 +253,12 @@ mod tests {
{
let [back, front] = &mut top.vertices;

let mut back = back.write();
let mut back = back.surface_form.write();
back.position = Some([0., 1.].into());
back.surface = surface.clone();

front.write().surface_form =
side_up.vertices[1].read().surface_form.clone();
front.surface_form =
side_up.vertices[1].surface_form.clone();
}

top.infer_global_form();
Expand All @@ -283,10 +278,8 @@ mod tests {

let [back, front] = &mut side_down.vertices;

back.write().surface_form =
bottom.vertices[0].read().surface_form.clone();
front.write().surface_form =
top.vertices[1].read().surface_form.clone();
back.surface_form = bottom.vertices[0].surface_form.clone();
front.surface_form = top.vertices[1].surface_form.clone();

side_down.infer_global_form();
side_down.update_as_line_segment();
Expand Down
6 changes: 2 additions & 4 deletions crates/fj-kernel/src/algorithms/sweep/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{

use super::{Sweep, SweepCache};

impl Sweep for (Handle<Vertex>, Handle<Surface>) {
impl Sweep for (Vertex, Handle<Surface>) {
type Swept = Handle<HalfEdge>;

fn sweep_with_cache(
Expand Down Expand Up @@ -112,7 +112,6 @@ impl Sweep for (Handle<Vertex>, Handle<Surface>) {
curve.clone(),
surface_form,
)
.insert(objects)
});

// And finally, creating the output `Edge` is just a matter of
Expand Down Expand Up @@ -188,8 +187,7 @@ mod tests {
..Default::default()
}),
}
.build(&mut services.objects)
.insert(&mut services.objects);
.build(&mut services.objects);

let half_edge = (vertex, surface.clone())
.sweep([0., 0., 1.], &mut services.objects);
Expand Down
21 changes: 6 additions & 15 deletions crates/fj-kernel/src/builder/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,23 +99,21 @@ impl CycleBuilder for PartialCycle {

{
let shared_surface_vertex =
new_half_edge.read().back().read().surface_form.clone();
new_half_edge.read().back().surface_form.clone();

let mut last_half_edge = last_half_edge.write();

last_half_edge.front_mut().write().surface_form =
shared_surface_vertex;
last_half_edge.front_mut().surface_form = shared_surface_vertex;
last_half_edge.infer_global_form();
}

{
let shared_surface_vertex =
first_half_edge.read().back().read().surface_form.clone();
first_half_edge.read().back().surface_form.clone();

let mut new_half_edge = new_half_edge.write();

new_half_edge.front_mut().write().surface_form =
shared_surface_vertex;
new_half_edge.front_mut().surface_form = shared_surface_vertex;
new_half_edge.replace_surface(self.surface.clone());
new_half_edge.infer_global_form();
}
Expand All @@ -130,13 +128,8 @@ impl CycleBuilder for PartialCycle {
) -> Partial<HalfEdge> {
let mut half_edge = self.add_half_edge();

half_edge
.write()
.back_mut()
.write()
.surface_form
.write()
.position = Some(point.into());
half_edge.write().back_mut().surface_form.write().position =
Some(point.into());

half_edge
}
Expand All @@ -150,7 +143,6 @@ impl CycleBuilder for PartialCycle {
half_edge
.write()
.back_mut()
.write()
.surface_form
.write()
.global_form
Expand Down Expand Up @@ -198,7 +190,6 @@ impl CycleBuilder for PartialCycle {
half_edge
.write()
.back_mut()
.write()
.surface_form
.write()
.global_form
Expand Down
29 changes: 10 additions & 19 deletions crates/fj-kernel/src/builder/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl HalfEdgeBuilder for PartialHalfEdge {
let surface = surface.into();

for vertex in &mut self.vertices {
vertex.write().replace_surface(surface.clone());
vertex.replace_surface(surface.clone());
}
}

Expand All @@ -72,15 +72,15 @@ impl HalfEdgeBuilder for PartialHalfEdge {

let mut surface_vertex = {
let [vertex, _] = &mut self.vertices;
vertex.write().surface_form.clone()
vertex.surface_form.clone()
};
surface_vertex.write().position =
Some(path.point_from_path_coords(a_curve));

for (vertex, point_curve) in
self.vertices.each_mut_ext().zip_ext([a_curve, b_curve])
{
let mut vertex = vertex.write();
let mut vertex = vertex;
vertex.position = Some(point_curve);
vertex.surface_form = surface_vertex.clone();
}
Expand All @@ -95,7 +95,6 @@ impl HalfEdgeBuilder for PartialHalfEdge {
}
let points_surface = self.vertices.each_ref_ext().map(|vertex| {
vertex
.read()
.surface_form
.read()
.position
Expand Down Expand Up @@ -123,7 +122,6 @@ impl HalfEdgeBuilder for PartialHalfEdge {
for (vertex, point_curve) in
self.vertices.each_mut_ext().zip_ext([a_curve, b_curve])
{
let mut vertex = vertex.write();
vertex.position = Some(point_curve);
vertex.surface_form.write().position =
Some(path.point_from_path_coords(point_curve));
Expand All @@ -140,7 +138,6 @@ impl HalfEdgeBuilder for PartialHalfEdge {
let surface = surface.into();

for (vertex, point) in self.vertices.each_mut_ext().zip_ext(points) {
let mut vertex = vertex.write();
vertex.curve.write().surface = surface.clone();

let mut surface_form = vertex.surface_form.write();
Expand All @@ -154,7 +151,6 @@ impl HalfEdgeBuilder for PartialHalfEdge {
fn update_as_line_segment(&mut self) {
let points_surface = self.vertices.each_ref_ext().map(|vertex| {
vertex
.read()
.surface_form
.read()
.position
Expand All @@ -167,7 +163,7 @@ impl HalfEdgeBuilder for PartialHalfEdge {

for (vertex, position) in self.vertices.each_mut_ext().zip_ext([0., 1.])
{
vertex.write().position = Some([position].into());
vertex.position = Some([position].into());
}

self.infer_global_form();
Expand All @@ -176,10 +172,10 @@ impl HalfEdgeBuilder for PartialHalfEdge {
fn infer_global_form(&mut self) -> Partial<GlobalEdge> {
self.global_form.write().curve =
self.curve().read().global_form.clone();
self.global_form.write().vertices =
self.vertices.each_ref_ext().map(|vertex| {
vertex.read().surface_form.read().global_form.clone()
});
self.global_form.write().vertices = self
.vertices
.each_ref_ext()
.map(|vertex| vertex.surface_form.read().global_form.clone());

self.global_form.clone()
}
Expand All @@ -202,13 +198,8 @@ impl HalfEdgeBuilder for PartialHalfEdge {
.iter_mut()
.zip(other.read().vertices.iter().rev())
{
this.write()
.surface_form
.write()
.global_form
.write()
.position =
other.read().surface_form.read().global_form.read().position;
this.surface_form.write().global_form.write().position =
other.surface_form.read().global_form.read().position;
}
}
}
Expand Down
16 changes: 6 additions & 10 deletions crates/fj-kernel/src/builder/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@ impl FaceBuilder for PartialFace {

fn update_surface_as_plane(&mut self) -> Partial<Surface> {
let mut exterior = self.exterior.write();
let mut vertices = exterior.half_edges.iter().map(|half_edge| {
half_edge.read().back().read().surface_form.clone()
});
let mut vertices = exterior
.half_edges
.iter()
.map(|half_edge| half_edge.read().back().surface_form.clone());

let vertices = {
let array = [
Expand Down Expand Up @@ -164,12 +165,7 @@ impl FaceBuilder for PartialFace {
MaybeSurfacePath::UndefinedLine => {
let points_surface =
half_edge.vertices.each_ref_ext().map(|vertex| {
vertex
.read()
.surface_form
.read()
.position
.expect(
vertex.surface_form.read().position.expect(
"Can't infer curve without surface points",
)
});
Expand All @@ -182,7 +178,7 @@ impl FaceBuilder for PartialFace {
.each_mut_ext()
.zip_ext(points_curve)
{
vertex.write().position = Some(point);
vertex.position = Some(point);
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions crates/fj-kernel/src/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use crate::{
objects::{
Curve, Cycle, Face, GlobalCurve, GlobalEdge, GlobalVertex, HalfEdge,
Objects, Shell, Sketch, Solid, Surface, SurfaceVertex, Vertex,
Objects, Shell, Sketch, Solid, Surface, SurfaceVertex,
},
services::{Service, ServiceObjectsExt},
storage::Handle,
Expand Down Expand Up @@ -46,5 +46,4 @@ impl_insert!(
Solid, solids;
SurfaceVertex, surface_vertices;
Surface, surfaces;
Vertex, vertices;
);
13 changes: 5 additions & 8 deletions crates/fj-kernel/src/objects/full/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ use crate::{
/// A half-edge
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct HalfEdge {
vertices: [Handle<Vertex>; 2],
vertices: [Vertex; 2],
global_form: Handle<GlobalEdge>,
}

impl HalfEdge {
/// Create an instance of `HalfEdge`
pub fn new(
vertices: [Handle<Vertex>; 2],
global_form: Handle<GlobalEdge>,
) -> Self {
pub fn new(vertices: [Vertex; 2], global_form: Handle<GlobalEdge>) -> Self {
Self {
vertices,
global_form,
Expand All @@ -31,18 +28,18 @@ impl HalfEdge {
}

/// Access the vertices that bound the half-edge on the curve
pub fn vertices(&self) -> &[Handle<Vertex>; 2] {
pub fn vertices(&self) -> &[Vertex; 2] {
&self.vertices
}

/// Access the vertex at the back of the half-edge
pub fn back(&self) -> &Handle<Vertex> {
pub fn back(&self) -> &Vertex {
let [back, _] = self.vertices();
back
}

/// Access the vertex at the front of the half-edge
pub fn front(&self) -> &Handle<Vertex> {
pub fn front(&self) -> &Vertex {
let [_, front] = self.vertices();
front
}
Expand Down
3 changes: 1 addition & 2 deletions crates/fj-kernel/src/objects/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::any::Any;
use crate::{
objects::{
Curve, Cycle, Face, GlobalCurve, GlobalEdge, GlobalVertex, HalfEdge,
Objects, Shell, Sketch, Solid, Surface, SurfaceVertex, Vertex,
Objects, Shell, Sketch, Solid, Surface, SurfaceVertex,
},
storage::{Handle, ObjectId},
validate::{Validate, ValidationError},
Expand Down Expand Up @@ -120,7 +120,6 @@ object!(
Solid, "solid", solids;
Surface, "surface", surfaces;
SurfaceVertex, "surface vertex", surface_vertices;
Vertex, "vertex", vertices;
);

/// The form that an object can take
Expand Down
Loading

0 comments on commit 71d59e2

Please sign in to comment.