From 0af524740045013d808cd847fd070d8c80431b51 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 16 Nov 2022 15:09:05 +0100 Subject: [PATCH 1/3] Add `Get` trait --- crates/fj-kernel/src/get.rs | 15 +++++++++++++++ crates/fj-kernel/src/lib.rs | 1 + 2 files changed, 16 insertions(+) create mode 100644 crates/fj-kernel/src/get.rs diff --git a/crates/fj-kernel/src/get.rs b/crates/fj-kernel/src/get.rs new file mode 100644 index 000000000..43b41c2b0 --- /dev/null +++ b/crates/fj-kernel/src/get.rs @@ -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 { + /// Access the referenced object + fn get(&self) -> Handle; +} diff --git a/crates/fj-kernel/src/lib.rs b/crates/fj-kernel/src/lib.rs index 87091d3a1..9eebf0122 100644 --- a/crates/fj-kernel/src/lib.rs +++ b/crates/fj-kernel/src/lib.rs @@ -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; From 9ad3c35c6c8e497306303ddf1b463c954b667b48 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 16 Nov 2022 15:24:29 +0100 Subject: [PATCH 2/3] Implement `Get` for all first-order object refs It's likely that many objects will want to implement `Get` for objects they reference indirectly (through another object they reference), but this is a good start. --- crates/fj-kernel/src/objects/curve.rs | 13 +++++++++++++ crates/fj-kernel/src/objects/edge.rs | 17 ++++++++++++++++- crates/fj-kernel/src/objects/vertex.rs | 26 +++++++++++++++++++++++++- 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/crates/fj-kernel/src/objects/curve.rs b/crates/fj-kernel/src/objects/curve.rs index 2d1b439ef..5b9663ef7 100644 --- a/crates/fj-kernel/src/objects/curve.rs +++ b/crates/fj-kernel/src/objects/curve.rs @@ -1,5 +1,6 @@ use crate::{ geometry::path::SurfacePath, + get::Get, storage::{Handle, HandleWrapper}, }; @@ -43,6 +44,18 @@ impl Curve { } } +impl Get for Curve { + fn get(&self) -> Handle { + self.surface().clone() + } +} + +impl Get for Curve { + fn get(&self) -> Handle { + self.global_form().clone() + } +} + /// A curve, defined in global (3D) coordinates #[derive(Clone, Copy, Debug)] pub struct GlobalCurve; diff --git a/crates/fj-kernel/src/objects/edge.rs b/crates/fj-kernel/src/objects/edge.rs index ea20d4e7d..3ae07f724 100644 --- a/crates/fj-kernel/src/objects/edge.rs +++ b/crates/fj-kernel/src/objects/edge.rs @@ -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}; @@ -57,6 +60,12 @@ impl HalfEdge { } } +impl Get for HalfEdge { + fn get(&self) -> Handle { + self.global_form().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()); @@ -115,6 +124,12 @@ impl GlobalEdge { } } +impl Get for GlobalEdge { + fn get(&self) -> Handle { + self.curve().clone() + } +} + /// The vertices of a [`GlobalEdge`] /// /// Since [`GlobalEdge`] is the single global representation of an edge in diff --git a/crates/fj-kernel/src/objects/vertex.rs b/crates/fj-kernel/src/objects/vertex.rs index 62a6db6a4..ea55c5e3c 100644 --- a/crates/fj-kernel/src/objects/vertex.rs +++ b/crates/fj-kernel/src/objects/vertex.rs @@ -1,6 +1,6 @@ use fj_math::Point; -use crate::storage::Handle; +use crate::{get::Get, storage::Handle}; use super::{Curve, Surface}; @@ -56,6 +56,18 @@ impl Vertex { } } +impl Get for Vertex { + fn get(&self) -> Handle { + self.curve().clone() + } +} + +impl Get for Vertex { + fn get(&self) -> Handle { + self.surface_form().clone() + } +} + /// A vertex, defined in surface (2D) coordinates #[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)] pub struct SurfaceVertex { @@ -95,6 +107,18 @@ impl SurfaceVertex { } } +impl Get for SurfaceVertex { + fn get(&self) -> Handle { + self.surface().clone() + } +} + +impl Get for SurfaceVertex { + fn get(&self) -> Handle { + self.global_form().clone() + } +} + /// A vertex, defined in global (3D) coordinates /// /// This struct exists to distinguish between vertices and points at the type From 44028e273332c2805ee13a25b34492666834f1e0 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 16 Nov 2022 15:35:59 +0100 Subject: [PATCH 3/3] Add some second-order implementations of `Get` --- crates/fj-kernel/src/objects/edge.rs | 6 ++++++ crates/fj-kernel/src/objects/vertex.rs | 20 +++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/crates/fj-kernel/src/objects/edge.rs b/crates/fj-kernel/src/objects/edge.rs index 3ae07f724..bdc84d25a 100644 --- a/crates/fj-kernel/src/objects/edge.rs +++ b/crates/fj-kernel/src/objects/edge.rs @@ -66,6 +66,12 @@ impl Get for HalfEdge { } } +impl Get for HalfEdge { + fn get(&self) -> Handle { + 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()); diff --git a/crates/fj-kernel/src/objects/vertex.rs b/crates/fj-kernel/src/objects/vertex.rs index ea55c5e3c..d7b14adab 100644 --- a/crates/fj-kernel/src/objects/vertex.rs +++ b/crates/fj-kernel/src/objects/vertex.rs @@ -2,7 +2,7 @@ use fj_math::Point; use crate::{get::Get, storage::Handle}; -use super::{Curve, Surface}; +use super::{Curve, GlobalCurve, Surface}; /// A vertex /// @@ -68,6 +68,24 @@ impl Get for Vertex { } } +impl Get for Vertex { + fn get(&self) -> Handle { + self.curve().surface().clone() + } +} + +impl Get for Vertex { + fn get(&self) -> Handle { + self.curve().global_form().clone() + } +} + +impl Get for Vertex { + fn get(&self) -> Handle { + self.surface_form().global_form().clone() + } +} + /// A vertex, defined in surface (2D) coordinates #[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)] pub struct SurfaceVertex {