Skip to content

Commit

Permalink
Merge pull request #1359 from hannobraun/get
Browse files Browse the repository at this point in the history
Add infrastructure for abstracting over access to referenced objects
  • Loading branch information
hannobraun authored Nov 16, 2022
2 parents f8c813d + 44028e2 commit 0467dae
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 3 deletions.
15 changes: 15 additions & 0 deletions crates/fj-kernel/src/get.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! Infrastructure for abstracting over accessing referenced objects
use crate::storage::Handle;

/// Access a single referenced object
///
/// Object types implement this trait for the objects they reference. It can be
/// used by other generic infrastructure to abstract over object access.
///
/// This trait is specifically intended to access single objects, like *the*
/// curve that a vertex references, not *a* half-edge that a cycle references.
pub trait Get<T> {
/// Access the referenced object
fn get(&self) -> Handle<T>;
}
1 change: 1 addition & 0 deletions crates/fj-kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
pub mod algorithms;
pub mod builder;
pub mod geometry;
pub mod get;
pub mod insert;
pub mod iter;
pub mod objects;
Expand Down
13 changes: 13 additions & 0 deletions crates/fj-kernel/src/objects/curve.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
geometry::path::SurfacePath,
get::Get,
storage::{Handle, HandleWrapper},
};

Expand Down Expand Up @@ -43,6 +44,18 @@ impl Curve {
}
}

impl Get<Surface> for Curve {
fn get(&self) -> Handle<Surface> {
self.surface().clone()
}
}

impl Get<GlobalCurve> for Curve {
fn get(&self) -> Handle<GlobalCurve> {
self.global_form().clone()
}
}

/// A curve, defined in global (3D) coordinates
#[derive(Clone, Copy, Debug)]
pub struct GlobalCurve;
23 changes: 22 additions & 1 deletion crates/fj-kernel/src/objects/edge.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::fmt;

use crate::storage::{Handle, HandleWrapper};
use crate::{
get::Get,
storage::{Handle, HandleWrapper},
};

use super::{Curve, GlobalCurve, GlobalVertex, Surface, Vertex};

Expand Down Expand Up @@ -57,6 +60,18 @@ impl HalfEdge {
}
}

impl Get<GlobalEdge> for HalfEdge {
fn get(&self) -> Handle<GlobalEdge> {
self.global_form().clone()
}
}

impl Get<GlobalCurve> for HalfEdge {
fn get(&self) -> Handle<GlobalCurve> {
self.global_form().curve().clone()
}
}

impl fmt::Display for HalfEdge {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let [a, b] = self.vertices().clone().map(|vertex| vertex.position());
Expand Down Expand Up @@ -115,6 +130,12 @@ impl GlobalEdge {
}
}

impl Get<GlobalCurve> for GlobalEdge {
fn get(&self) -> Handle<GlobalCurve> {
self.curve().clone()
}
}

/// The vertices of a [`GlobalEdge`]
///
/// Since [`GlobalEdge`] is the single global representation of an edge in
Expand Down
46 changes: 44 additions & 2 deletions crates/fj-kernel/src/objects/vertex.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use fj_math::Point;

use crate::storage::Handle;
use crate::{get::Get, storage::Handle};

use super::{Curve, Surface};
use super::{Curve, GlobalCurve, Surface};

/// A vertex
///
Expand Down Expand Up @@ -56,6 +56,36 @@ impl Vertex {
}
}

impl Get<Curve> for Vertex {
fn get(&self) -> Handle<Curve> {
self.curve().clone()
}
}

impl Get<SurfaceVertex> for Vertex {
fn get(&self) -> Handle<SurfaceVertex> {
self.surface_form().clone()
}
}

impl Get<Surface> for Vertex {
fn get(&self) -> Handle<Surface> {
self.curve().surface().clone()
}
}

impl Get<GlobalCurve> for Vertex {
fn get(&self) -> Handle<GlobalCurve> {
self.curve().global_form().clone()
}
}

impl Get<GlobalVertex> for Vertex {
fn get(&self) -> Handle<GlobalVertex> {
self.surface_form().global_form().clone()
}
}

/// A vertex, defined in surface (2D) coordinates
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct SurfaceVertex {
Expand Down Expand Up @@ -95,6 +125,18 @@ impl SurfaceVertex {
}
}

impl Get<Surface> for SurfaceVertex {
fn get(&self) -> Handle<Surface> {
self.surface().clone()
}
}

impl Get<GlobalVertex> for SurfaceVertex {
fn get(&self) -> Handle<GlobalVertex> {
self.global_form().clone()
}
}

/// A vertex, defined in global (3D) coordinates
///
/// This struct exists to distinguish between vertices and points at the type
Expand Down

0 comments on commit 0467dae

Please sign in to comment.