Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add infrastructure for abstracting over access to referenced objects #1359

Merged
merged 3 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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