Skip to content

Commit

Permalink
Merge pull request #1522 from hannobraun/simplify
Browse files Browse the repository at this point in the history
Move `Curve` reference from `Vertex` to `HalfEdge`
  • Loading branch information
hannobraun authored Jan 18, 2023
2 parents 71d59e2 + a06b0d5 commit cb92f20
Show file tree
Hide file tree
Showing 15 changed files with 161 additions and 218 deletions.
7 changes: 6 additions & 1 deletion crates/fj-kernel/src/algorithms/reverse/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ impl Reverse for Handle<HalfEdge> {
[b, a]
};

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

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

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

let side_edges = bottom_edge.vertices().clone().map(|vertex| {
Expand Down Expand Up @@ -136,10 +133,10 @@ impl Sweep for (Handle<HalfEdge>, Color) {
.zip(surface_vertices)
.collect::<[_; 2]>()
.map(|(vertex, surface_form)| {
Vertex::new(vertex.position(), curve.clone(), surface_form)
Vertex::new(vertex.position(), surface_form)
});

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

let cycle = {
Expand Down Expand Up @@ -229,7 +226,7 @@ mod tests {
};
let side_up = {
let mut side_up = PartialHalfEdge::default();
side_up.curve().write().surface = surface.clone();
side_up.curve.write().surface = surface.clone();

{
let [back, front] = &mut side_up.vertices;
Expand All @@ -248,7 +245,7 @@ mod tests {
};
let top = {
let mut top = PartialHalfEdge::default();
top.curve().write().surface = surface.clone();
top.curve.write().surface = surface.clone();

{
let [back, front] = &mut top.vertices;
Expand All @@ -274,7 +271,7 @@ mod tests {
};
let side_down = {
let mut side_down = PartialHalfEdge::default();
side_down.curve().write().surface = surface;
side_down.curve.write().surface = surface;

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

Expand Down
22 changes: 9 additions & 13 deletions crates/fj-kernel/src/algorithms/sweep/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,12 @@ impl Sweep for (Vertex, Handle<Surface>) {

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

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

Expand Down Expand Up @@ -153,15 +149,16 @@ impl Sweep for Handle<GlobalVertex> {

#[cfg(test)]
mod tests {
use fj_math::Point;
use pretty_assertions::assert_eq;

use crate::{
algorithms::sweep::Sweep,
builder::{CurveBuilder, HalfEdgeBuilder},
insert::Insert,
partial::{
Partial, PartialCurve, PartialHalfEdge, PartialObject,
PartialSurfaceVertex, PartialVertex,
Partial, PartialCurve, PartialGlobalVertex, PartialHalfEdge,
PartialObject, PartialSurfaceVertex, PartialVertex,
},
services::Services,
};
Expand All @@ -176,15 +173,14 @@ mod tests {
..Default::default()
};
curve.update_as_u_axis();
let curve = curve
.build(&mut services.objects)
.insert(&mut services.objects);
let vertex = PartialVertex {
position: Some([0.].into()),
curve: Partial::from(curve),
surface_form: Partial::from_partial(PartialSurfaceVertex {
position: Some(Point::from([0., 0.])),
surface: Partial::from(surface.clone()),
..Default::default()
global_form: Partial::from_partial(PartialGlobalVertex {
position: Some(Point::from([0., 0., 0.])),
}),
}),
}
.build(&mut services.objects);
Expand Down
6 changes: 5 additions & 1 deletion crates/fj-kernel/src/algorithms/transform/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ impl TransformObject for HalfEdge {
objects: &mut Service<Objects>,
cache: &mut TransformCache,
) -> Self {
let curve = self
.curve()
.clone()
.transform_with_cache(transform, objects, cache);
let vertices = self.vertices().clone().map(|vertex| {
vertex.transform_with_cache(transform, objects, cache)
});
Expand All @@ -22,7 +26,7 @@ impl TransformObject for HalfEdge {
.clone()
.transform_with_cache(transform, objects, cache);

Self::new(vertices, global_form)
Self::new(curve, vertices, global_form)
}
}

Expand Down
6 changes: 1 addition & 5 deletions crates/fj-kernel/src/algorithms/transform/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,12 @@ impl TransformObject for Vertex {
// coordinates and thus transforming the curve takes care of it.
let position = self.position();

let curve = self
.curve()
.clone()
.transform_with_cache(transform, objects, cache);
let surface_form = self
.surface_form()
.clone()
.transform_with_cache(transform, objects, cache);

Self::new(position, curve, surface_form)
Self::new(position, surface_form)
}
}

Expand Down
26 changes: 13 additions & 13 deletions crates/fj-kernel/src/builder/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@ impl HalfEdgeBuilder for PartialHalfEdge {
fn replace_surface(&mut self, surface: impl Into<Partial<Surface>>) {
let surface = surface.into();

self.curve.write().surface = surface.clone();

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

fn update_as_circle_from_radius(&mut self, radius: impl Into<Scalar>) {
let mut curve = self.curve();
let path = curve.write().update_as_circle_from_radius(radius);
let path = self.curve.write().update_as_circle_from_radius(radius);

let [a_curve, b_curve] =
[Scalar::ZERO, Scalar::TAU].map(|coord| Point::from([coord]));
Expand Down Expand Up @@ -107,8 +108,8 @@ impl HalfEdgeBuilder for PartialHalfEdge {
angle_rad,
);

let mut curve = self.curve();
let path = curve
let path = self
.curve
.write()
.update_as_circle_from_center_and_radius(arc.center, arc.radius);

Expand Down Expand Up @@ -137,9 +138,9 @@ impl HalfEdgeBuilder for PartialHalfEdge {
) {
let surface = surface.into();

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

for (vertex, point) in self.vertices.each_mut_ext().zip_ext(points) {
let mut surface_form = vertex.surface_form.write();
surface_form.position = Some(point.into());
surface_form.surface = surface.clone();
Expand All @@ -157,7 +158,7 @@ impl HalfEdgeBuilder for PartialHalfEdge {
.expect("Can't infer line segment without surface position")
});

self.curve()
self.curve
.write()
.update_as_line_from_points(points_surface);

Expand All @@ -170,8 +171,7 @@ 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().curve = self.curve.read().global_form.clone();
self.global_form.write().vertices = self
.vertices
.each_ref_ext()
Expand All @@ -181,13 +181,13 @@ impl HalfEdgeBuilder for PartialHalfEdge {
}

fn update_from_other_edge(&mut self, other: &Partial<HalfEdge>) {
let global_curve = other.read().curve().read().global_form.clone();
self.curve().write().global_form = global_curve.clone();
let global_curve = other.read().curve.read().global_form.clone();
self.curve.write().global_form = global_curve.clone();
self.global_form.write().curve = global_curve;

self.curve().write().path = other
self.curve.write().path = other
.read()
.curve()
.curve
.read()
.path
.as_ref()
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/builder/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl FaceBuilder for PartialFace {
for half_edge in &mut self.exterior.write().half_edges {
let mut half_edge = half_edge.write();

let mut curve = half_edge.curve();
let mut curve = half_edge.curve.clone();
let mut curve = curve.write();

if let Some(path) = &mut curve.path {
Expand Down
2 changes: 0 additions & 2 deletions crates/fj-kernel/src/builder/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ pub trait VertexBuilder {
impl VertexBuilder for PartialVertex {
fn replace_surface(&mut self, surface: impl Into<Partial<Surface>>) {
let surface = surface.into();

self.curve.write().surface = surface.clone();
self.surface_form.write().surface = surface;
}
}
Expand Down
11 changes: 8 additions & 3 deletions crates/fj-kernel/src/objects/full/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,28 @@ use crate::{
/// A half-edge
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct HalfEdge {
curve: Handle<Curve>,
vertices: [Vertex; 2],
global_form: Handle<GlobalEdge>,
}

impl HalfEdge {
/// Create an instance of `HalfEdge`
pub fn new(vertices: [Vertex; 2], global_form: Handle<GlobalEdge>) -> Self {
pub fn new(
curve: Handle<Curve>,
vertices: [Vertex; 2],
global_form: Handle<GlobalEdge>,
) -> Self {
Self {
curve,
vertices,
global_form,
}
}

/// Access the curve that defines the half-edge's geometry
pub fn curve(&self) -> &Handle<Curve> {
let [vertex, _] = self.vertices();
vertex.curve()
&self.curve
}

/// Access the vertices that bound the half-edge on the curve
Expand Down
13 changes: 1 addition & 12 deletions crates/fj-kernel/src/objects/full/vertex.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use fj_math::Point;

use crate::{
objects::{Curve, Surface},
storage::Handle,
};
use crate::{objects::Surface, storage::Handle};

/// A vertex
///
Expand All @@ -15,22 +12,19 @@ use crate::{
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct Vertex {
position: Point<1>,
curve: Handle<Curve>,
surface_form: Handle<SurfaceVertex>,
}

impl Vertex {
/// Construct an instance of `Vertex`
pub fn new(
position: impl Into<Point<1>>,
curve: Handle<Curve>,
surface_form: Handle<SurfaceVertex>,
) -> Self {
let position = position.into();

Self {
position,
curve,
surface_form,
}
}
Expand All @@ -40,11 +34,6 @@ impl Vertex {
self.position
}

/// Access the curve that the vertex is defined in
pub fn curve(&self) -> &Handle<Curve> {
&self.curve
}

/// Access the surface form of this vertex
pub fn surface_form(&self) -> &Handle<SurfaceVertex> {
&self.surface_form
Expand Down
Loading

0 comments on commit cb92f20

Please sign in to comment.