Skip to content

Commit

Permalink
Merge pull request #1246 from hannobraun/vertex
Browse files Browse the repository at this point in the history
Integrate `Vertex` into centralized object storage
  • Loading branch information
hannobraun authored Oct 20, 2022
2 parents 9f263eb + 0668dc0 commit 8c33db9
Show file tree
Hide file tree
Showing 15 changed files with 92 additions and 58 deletions.
7 changes: 5 additions & 2 deletions crates/fj-kernel/src/algorithms/intersect/face_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
use fj_math::Point;

use crate::objects::{Face, HalfEdge, Vertex};
use crate::{
objects::{Face, HalfEdge, Vertex},
storage::Handle,
};

use super::{
ray_segment::RaySegmentIntersection, HorizontalRayToTheRight, Intersect,
Expand Down Expand Up @@ -123,7 +126,7 @@ pub enum FacePointIntersection {
PointIsOnEdge(HalfEdge),

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

#[cfg(test)]
Expand Down
3 changes: 2 additions & 1 deletion crates/fj-kernel/src/algorithms/intersect/ray_face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
algorithms::intersect::face_point::FacePointIntersection,
objects::{Face, HalfEdge, Vertex},
path::GlobalPath,
storage::Handle,
};

use super::{HorizontalRayToTheRight, Intersect};
Expand Down Expand Up @@ -136,7 +137,7 @@ pub enum RayFaceIntersection {
RayHitsEdge(HalfEdge),

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

#[cfg(test)]
Expand Down
44 changes: 29 additions & 15 deletions crates/fj-kernel/src/algorithms/sweep/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ impl Sweep for (HalfEdge, Color) {
vertex.position(),
curve.clone(),
surface_vertex,
objects,
)
})
};
Expand Down Expand Up @@ -134,7 +135,12 @@ impl Sweep for (HalfEdge, Color) {
let vertices = [(a_vertex, a_surface), (b_vertex, b_surface)];

vertices.map(|(vertex, surface_form)| {
Vertex::new(vertex.position(), curve.clone(), surface_form)
Vertex::new(
vertex.position(),
curve.clone(),
surface_form,
objects,
)
})
};

Expand Down Expand Up @@ -207,11 +213,13 @@ mod tests {
.build(&objects);
let side_up = HalfEdge::partial()
.with_surface(Some(surface.clone()))
.with_back_vertex(Some(Vertex::partial().with_surface_form(
Some(bottom.front().surface_form().clone()),
)))
.with_back_vertex(Some(
Handle::<Vertex>::partial().with_surface_form(Some(
bottom.front().surface_form().clone(),
)),
))
.with_front_vertex(Some(
Vertex::partial().with_surface_form(Some(
Handle::<Vertex>::partial().with_surface_form(Some(
Handle::<SurfaceVertex>::partial()
.with_position(Some([1., 1.])),
)),
Expand All @@ -221,25 +229,31 @@ mod tests {
let top = HalfEdge::partial()
.with_surface(Some(surface.clone()))
.with_back_vertex(Some(
Vertex::partial().with_surface_form(Some(
Handle::<Vertex>::partial().with_surface_form(Some(
Handle::<SurfaceVertex>::partial()
.with_position(Some([0., 1.])),
)),
))
.with_front_vertex(Some(Vertex::partial().with_surface_form(
Some(side_up.front().surface_form().clone()),
)))
.with_front_vertex(Some(
Handle::<Vertex>::partial().with_surface_form(Some(
side_up.front().surface_form().clone(),
)),
))
.as_line_segment()
.build(&objects)
.reverse();
let side_down = HalfEdge::partial()
.with_surface(Some(surface.clone()))
.with_back_vertex(Some(Vertex::partial().with_surface_form(
Some(bottom.back().surface_form().clone()),
)))
.with_front_vertex(Some(Vertex::partial().with_surface_form(
Some(top.front().surface_form().clone()),
)))
.with_back_vertex(Some(
Handle::<Vertex>::partial().with_surface_form(Some(
bottom.back().surface_form().clone(),
)),
))
.with_front_vertex(Some(
Handle::<Vertex>::partial().with_surface_form(Some(
top.front().surface_form().clone(),
)),
))
.as_line_segment()
.build(&objects)
.reverse();
Expand Down
5 changes: 3 additions & 2 deletions crates/fj-kernel/src/algorithms/sweep/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{

use super::{Sweep, SweepCache};

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

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

Expand Down Expand Up @@ -173,7 +174,7 @@ mod tests {
.with_surface(Some(surface.clone()))
.as_u_axis()
.build(&objects);
let vertex = Vertex::partial()
let vertex = Handle::<Vertex>::partial()
.with_position(Some([0.]))
.with_curve(Some(curve))
.build(&objects);
Expand Down
9 changes: 7 additions & 2 deletions crates/fj-kernel/src/algorithms/validate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,14 @@ mod tests {
Point::from([Scalar::ZERO + deviation]),
curve.clone(),
a_surface,
&objects,
);
let b = Vertex::new(
Point::from([Scalar::ONE]),
curve.clone(),
b_surface,
&objects,
);
let b =
Vertex::new(Point::from([Scalar::ONE]), curve.clone(), b_surface);
let vertices = [a, b];

let global_edge = GlobalEdge::partial()
Expand Down
20 changes: 13 additions & 7 deletions crates/fj-kernel/src/builder/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@ impl<'a> ShellBuilder<'a> {

HalfEdge::partial()
.with_vertices(Some([
Vertex::partial().with_surface_form(Some(from)),
Vertex::partial().with_surface_form(Some(to)),
Handle::<Vertex>::partial()
.with_surface_form(Some(from)),
Handle::<Vertex>::partial()
.with_surface_form(Some(to)),
]))
.as_line_segment()
.build(self.objects)
Expand Down Expand Up @@ -128,8 +130,10 @@ impl<'a> ShellBuilder<'a> {
HalfEdge::partial()
.with_curve(Some(curve))
.with_vertices(Some([
Vertex::partial().with_surface_form(Some(from)),
Vertex::partial().with_surface_form(Some(to)),
Handle::<Vertex>::partial()
.with_surface_form(Some(from)),
Handle::<Vertex>::partial()
.with_surface_form(Some(to)),
]))
.as_line_segment()
.build(self.objects)
Expand All @@ -148,8 +152,10 @@ impl<'a> ShellBuilder<'a> {
let from = from.surface_form().clone();
let to = to.surface_form().clone();

let from = Vertex::partial().with_surface_form(Some(from));
let to = Vertex::partial().with_surface_form(Some(to));
let from = Handle::<Vertex>::partial()
.with_surface_form(Some(from));
let to =
Handle::<Vertex>::partial().with_surface_form(Some(to));

HalfEdge::partial()
.with_vertices(Some([from, to]))
Expand Down Expand Up @@ -231,7 +237,7 @@ impl<'a> ShellBuilder<'a> {
(vertex_b, surface_vertex_b),
]
.map(|(vertex, surface_form)| {
Vertex::partial()
Handle::<Vertex>::partial()
.with_position(Some(vertex.position()))
.with_surface_form(Some(surface_form))
});
Expand Down
8 changes: 4 additions & 4 deletions crates/fj-kernel/src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub trait ObjectIters<'r> {
}

/// Iterator over all vertices
fn vertex_iter(&'r self) -> Iter<&'r Vertex> {
fn vertex_iter(&'r self) -> Iter<&'r Handle<Vertex>> {
let mut iter = Iter::empty();

for object in self.referenced_objects() {
Expand Down Expand Up @@ -276,15 +276,15 @@ impl<'r> ObjectIters<'r> for Handle<Surface> {
}
}

impl<'r> ObjectIters<'r> for Vertex {
impl<'r> ObjectIters<'r> for Handle<Vertex> {
fn referenced_objects(&'r self) -> Vec<&'r dyn ObjectIters> {
vec![
self.curve() as &dyn ObjectIters,
self.global_form() as &dyn ObjectIters,
]
}

fn vertex_iter(&'r self) -> Iter<&'r Vertex> {
fn vertex_iter(&'r self) -> Iter<&'r Handle<Vertex>> {
Iter::from_object(self)
}
}
Expand Down Expand Up @@ -591,7 +591,7 @@ mod tests {
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);
let object = Vertex::new([0.], curve, surface_vertex, &objects);

assert_eq!(1, object.curve_iter().count());
assert_eq!(0, object.cycle_iter().count());
Expand Down
10 changes: 5 additions & 5 deletions crates/fj-kernel/src/objects/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::{Curve, GlobalCurve, GlobalVertex, Vertex};
/// A half-edge
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct HalfEdge {
vertices: [Vertex; 2],
vertices: [Handle<Vertex>; 2],
global_form: GlobalEdge,
}

Expand All @@ -29,7 +29,7 @@ impl HalfEdge {
/// were, the edge would have no length, and thus not be valid. (It is
/// perfectly fine for global forms of the the vertices to be coincident.
/// That would just mean, that ends of the edge connect to each other.)
pub fn new([a, b]: [Vertex; 2], global_form: GlobalEdge) -> Self {
pub fn new([a, b]: [Handle<Vertex>; 2], global_form: GlobalEdge) -> Self {
// Make sure `curve` and `vertices` match.
assert_eq!(
a.curve().id(),
Expand Down Expand Up @@ -84,18 +84,18 @@ impl HalfEdge {
}

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

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

/// Access the vertex at the front of the half-edge
pub fn front(&self) -> &Vertex {
pub fn front(&self) -> &Handle<Vertex> {
let [_, front] = self.vertices();
front
}
Expand Down
3 changes: 3 additions & 0 deletions crates/fj-kernel/src/objects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ pub struct Objects {

/// Store for surfaces
pub surfaces: Surfaces,

/// Store for vertices
pub vertices: Store<Vertex>,
}

impl Objects {
Expand Down
7 changes: 4 additions & 3 deletions crates/fj-kernel/src/objects/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ impl Vertex {
position: impl Into<Point<1>>,
curve: Handle<Curve>,
surface_form: Handle<SurfaceVertex>,
) -> Self {
objects: &Objects,
) -> Handle<Self> {
let position = position.into();

assert_eq!(
Expand All @@ -35,11 +36,11 @@ impl Vertex {
"Surface form of vertex must be defined on same surface as curve",
);

Self {
objects.vertices.insert(Self {
position,
curve,
surface_form,
}
})
}

/// Access the position of the vertex on the curve
Expand Down
8 changes: 4 additions & 4 deletions crates/fj-kernel/src/partial/maybe_partial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl MaybePartial<GlobalEdge> {

impl MaybePartial<HalfEdge> {
/// Access the back vertex
pub fn back(&self) -> Option<MaybePartial<Vertex>> {
pub fn back(&self) -> Option<MaybePartial<Handle<Vertex>>> {
match self {
Self::Full(full) => Some(full.back().clone().into()),
Self::Partial(partial) => {
Expand All @@ -127,7 +127,7 @@ impl MaybePartial<HalfEdge> {
}

/// Access the front vertex
pub fn front(&self) -> Option<MaybePartial<Vertex>> {
pub fn front(&self) -> Option<MaybePartial<Handle<Vertex>>> {
match self {
Self::Full(full) => Some(full.front().clone().into()),
Self::Partial(partial) => {
Expand All @@ -138,7 +138,7 @@ impl MaybePartial<HalfEdge> {
}

/// Access the vertices
pub fn vertices(&self) -> [Option<MaybePartial<Vertex>>; 2] {
pub fn vertices(&self) -> [Option<MaybePartial<Handle<Vertex>>>; 2] {
match self {
Self::Full(full) => {
full.vertices().clone().map(|vertex| Some(vertex.into()))
Expand Down Expand Up @@ -166,7 +166,7 @@ impl MaybePartial<Handle<SurfaceVertex>> {
}
}

impl MaybePartial<Vertex> {
impl MaybePartial<Handle<Vertex>> {
/// Access the surface form
pub fn surface_form(&self) -> Option<MaybePartial<Handle<SurfaceVertex>>> {
match self {
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/partial/objects/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl PartialCycle {

let [from, to] =
[(0., from), (1., to)].map(|(position, surface_form)| {
Vertex::partial()
Handle::<Vertex>::partial()
.with_curve(Some(curve.clone()))
.with_position(Some([position]))
.with_surface_form(Some(surface_form))
Expand Down
Loading

0 comments on commit 8c33db9

Please sign in to comment.