Skip to content

Commit

Permalink
Merge pull request #2392 from hannobraun/geometry
Browse files Browse the repository at this point in the history
Add infrastructure for defining vertex geometry
  • Loading branch information
hannobraun authored Jun 20, 2024
2 parents fac3276 + d19b12e commit 0d25b1e
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 12 deletions.
7 changes: 3 additions & 4 deletions crates/fj-core/src/geometry/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ pub struct CurveGeom {
///
/// Having multiple redundant definitions is undesirable. However, we can't
/// just use one global definition in 3D, as we need the local 2D
/// definitions to approximate and triangulate curves, and we currently
/// don't have the tools to project a global definition into a local
/// context.
/// definitions to triangulate faces, and we currently don't have the tools
/// to project a global definition into a local context.
///
/// Eventually, it should be possible to define the geometry of a curve
/// once, either locally or globally, and then convert that single
Expand All @@ -41,7 +40,7 @@ impl CurveGeom {
}
}

/// The geometric definition of a curve in 2D surface coordinates
/// The geometric definition of a curve, in 2D surface coordinates
#[derive(Clone, Debug)]
pub struct LocalCurveGeom {
/// The path that defines the curve on its surface
Expand Down
31 changes: 25 additions & 6 deletions crates/fj-core/src/geometry/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ use fj_math::Vector;

use crate::{
storage::Handle,
topology::{Curve, HalfEdge, Surface, Topology},
topology::{Curve, HalfEdge, Surface, Topology, Vertex},
};

use super::{CurveGeom, GlobalPath, HalfEdgeGeom, LocalCurveGeom, SurfaceGeom};
use super::{
vertex::LocalVertexGeom, CurveGeom, GlobalPath, HalfEdgeGeom,
LocalCurveGeom, SurfaceGeom, VertexGeom,
};

/// Geometric data that is associated with topological objects
pub struct Geometry {
curve: BTreeMap<Handle<Curve>, CurveGeom>,
half_edge: BTreeMap<Handle<HalfEdge>, HalfEdgeGeom>,
surface: BTreeMap<Handle<Surface>, SurfaceGeom>,
vertex: BTreeMap<Handle<Vertex>, VertexGeom>,

space_2d: Handle<Surface>,

Expand All @@ -29,6 +33,7 @@ impl Geometry {
curve: BTreeMap::new(),
half_edge: BTreeMap::new(),
surface: BTreeMap::new(),
vertex: BTreeMap::new(),

space_2d: topology.surfaces.space_2d(),

Expand Down Expand Up @@ -103,11 +108,20 @@ impl Geometry {
self.surface.insert(surface, geometry);
}

pub(crate) fn define_vertex_inner(
&mut self,
vertex: Handle<Vertex>,
curve: Handle<Curve>,
geometry: LocalVertexGeom,
) {
self.vertex
.entry(vertex)
.or_default()
.definitions
.insert(curve, geometry);
}

/// # Access the geometry of the provided curve
///
/// ## Panics
///
/// Panics, if the geometry of the curve is not defined.
pub fn of_curve(&self, curve: &Handle<Curve>) -> Option<&CurveGeom> {
self.curve.get(curve)
}
Expand All @@ -134,6 +148,11 @@ impl Geometry {
.expect("Expected geometry of surface to be defined")
}

/// # Access the geometry of the provided vertex
pub fn of_vertex(&self, vertex: &Handle<Vertex>) -> Option<&VertexGeom> {
self.vertex.get(vertex)
}

/// Access the geometry of the xy-plane
pub fn xy_plane(&self) -> &SurfaceGeom {
self.of_surface(&self.xy_plane)
Expand Down
2 changes: 2 additions & 0 deletions crates/fj-core/src/geometry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod geometry;
mod half_edge;
mod path;
mod surface;
mod vertex;

pub use self::{
boundary::{CurveBoundary, CurveBoundaryElement},
Expand All @@ -14,4 +15,5 @@ pub use self::{
half_edge::HalfEdgeGeom,
path::{GlobalPath, SurfacePath},
surface::SurfaceGeom,
vertex::{LocalVertexGeom, VertexGeom},
};
34 changes: 34 additions & 0 deletions crates/fj-core/src/geometry/vertex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::collections::BTreeMap;

use fj_math::Point;

use crate::{storage::Handle, topology::Curve};

/// The geometric definition of a vertex
#[derive(Clone, Debug, Default)]
pub struct VertexGeom {
/// # The redundant local definitions of the vertex geometry
///
/// ## Implementation Note
///
/// Having multiple redundant definitions is undesirable. However, we can't
/// just use one global definition in 3D, as we need the local 1D
/// definitions to approximate curves, and we currently don't have the tools
/// to project a global definition into a local context.
///
/// Eventually, it should be possible to define the geometry of a vertex
/// once, either locally or globally, and then convert that single
/// definition into (other) local contexts, as needed. There currently is no
/// issue to track that specifically, but there is the following issue,
/// which is a prerequisite for making the required tooling practical:
///
/// <https://github.com/hannobraun/fornjot/issues/2118>
pub definitions: BTreeMap<Handle<Curve>, LocalVertexGeom>,
}

/// The geometric definition of a vertex, in 1D curve coordinates
#[derive(Clone, Debug)]
pub struct LocalVertexGeom {
/// The position of the vertex, in 1-dimensional curve coordinates
pub position: Point<1>,
}
54 changes: 52 additions & 2 deletions crates/fj-core/src/layers/geometry.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//! Layer infrastructure for [`Geometry`]
use crate::{
geometry::{Geometry, HalfEdgeGeom, LocalCurveGeom, SurfaceGeom},
geometry::{
Geometry, HalfEdgeGeom, LocalCurveGeom, LocalVertexGeom, SurfaceGeom,
},
storage::Handle,
topology::{Curve, HalfEdge, Surface},
topology::{Curve, HalfEdge, Surface, Vertex},
};

use super::{Command, Event, Layer};
Expand Down Expand Up @@ -57,6 +59,24 @@ impl Layer<Geometry> {
let mut events = Vec::new();
self.process(DefineSurface { surface, geometry }, &mut events);
}

/// Define the geometry of the provided vertex
pub fn define_vertex(
&mut self,
vertex: Handle<Vertex>,
curve: Handle<Curve>,
geometry: LocalVertexGeom,
) {
let mut events = Vec::new();
self.process(
DefineVertex {
vertex,
curve,
geometry,
},
&mut events,
);
}
}

/// Define the geometry of a curve
Expand Down Expand Up @@ -138,3 +158,33 @@ impl Event<Geometry> for DefineSurface {
state.define_surface_inner(self.surface.clone(), self.geometry);
}
}

/// Define the geometry of a curve
pub struct DefineVertex {
vertex: Handle<Vertex>,
curve: Handle<Curve>,
geometry: LocalVertexGeom,
}

impl Command<Geometry> for DefineVertex {
type Result = ();
type Event = Self;

fn decide(
self,
_: &Geometry,
events: &mut Vec<Self::Event>,
) -> Self::Result {
events.push(self);
}
}

impl Event<Geometry> for DefineVertex {
fn evolve(&self, state: &mut Geometry) {
state.define_vertex_inner(
self.vertex.clone(),
self.curve.clone(),
self.geometry.clone(),
);
}
}

0 comments on commit 0d25b1e

Please sign in to comment.