diff --git a/crates/fj-kernel/src/algorithms/approx/curve.rs b/crates/fj-kernel/src/algorithms/approx/curve.rs deleted file mode 100644 index 1bf0bb0db..000000000 --- a/crates/fj-kernel/src/algorithms/approx/curve.rs +++ /dev/null @@ -1,76 +0,0 @@ -//! Curve approximation -//! -//! Since curves are infinite (even circles have an infinite coordinate space, -//! even though they connect to themselves in global coordinates), a range must -//! be provided to approximate them. The approximation then returns points -//! within that range. -//! -//! The boundaries of the range are not included in the approximation. This is -//! done, to give the caller (who knows the boundary anyway) more options on how -//! to further process the approximation. - -use std::collections::BTreeMap; - -use crate::{ - objects::GlobalCurve, - storage::{Handle, ObjectId}, -}; - -use super::{path::RangeOnPath, ApproxPoint}; - -/// A cache for results of an approximation -#[derive(Default)] -pub struct CurveCache { - inner: BTreeMap<(ObjectId, RangeOnPath), GlobalCurveApprox>, -} - -impl CurveCache { - /// Create an empty cache - pub fn new() -> Self { - Self::default() - } - - /// Insert the approximation of a [`GlobalCurve`] - pub fn insert( - &mut self, - handle: Handle, - range: RangeOnPath, - approx: GlobalCurveApprox, - ) -> GlobalCurveApprox { - self.inner.insert((handle.id(), range), approx.clone()); - approx - } - - /// Access the approximation for the given [`GlobalCurve`], if available - pub fn get( - &self, - handle: Handle, - range: RangeOnPath, - ) -> Option { - if let Some(approx) = self.inner.get(&(handle.id(), range)) { - return Some(approx.clone()); - } - if let Some(approx) = self.inner.get(&(handle.id(), range.reverse())) { - // If we have a cache entry for the reverse range, we need to use - // that too! - return Some(approx.clone().reverse()); - } - - None - } -} - -/// An approximation of a [`GlobalCurve`] -#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)] -pub struct GlobalCurveApprox { - /// The points that approximate the curve - pub points: Vec>, -} - -impl GlobalCurveApprox { - /// Reverse the order of the approximation - pub fn reverse(mut self) -> Self { - self.points.reverse(); - self - } -} diff --git a/crates/fj-kernel/src/algorithms/approx/cycle.rs b/crates/fj-kernel/src/algorithms/approx/cycle.rs index 02c404311..51b364830 100644 --- a/crates/fj-kernel/src/algorithms/approx/cycle.rs +++ b/crates/fj-kernel/src/algorithms/approx/cycle.rs @@ -7,7 +7,8 @@ use fj_math::Segment; use crate::objects::{Cycle, Surface}; use super::{ - curve::CurveCache, edge::HalfEdgeApprox, Approx, ApproxPoint, Tolerance, + edge::{CurveCache, HalfEdgeApprox}, + Approx, ApproxPoint, Tolerance, }; impl Approx for (&Cycle, &Surface) { diff --git a/crates/fj-kernel/src/algorithms/approx/edge.rs b/crates/fj-kernel/src/algorithms/approx/edge.rs index bc0eb1757..87a2aa3c0 100644 --- a/crates/fj-kernel/src/algorithms/approx/edge.rs +++ b/crates/fj-kernel/src/algorithms/approx/edge.rs @@ -5,17 +5,15 @@ //! approximations are usually used to build cycle approximations, and this way, //! the caller doesn't have to call with duplicate vertices. +use std::collections::BTreeMap; + use crate::{ geometry::path::{GlobalPath, SurfacePath}, - objects::{Curve, HalfEdge, Surface}, - storage::Handle, + objects::{Curve, GlobalCurve, HalfEdge, Surface}, + storage::{Handle, ObjectId}, }; -use super::{ - curve::{CurveCache, GlobalCurveApprox}, - path::RangeOnPath, - Approx, ApproxPoint, Tolerance, -}; +use super::{path::RangeOnPath, Approx, ApproxPoint, Tolerance}; impl Approx for (&Handle, &Surface) { type Approximation = HalfEdgeApprox; @@ -180,6 +178,63 @@ fn approx_edge( GlobalCurveApprox { points } } +/// A cache for results of an approximation +#[derive(Default)] +pub struct CurveCache { + inner: BTreeMap<(ObjectId, RangeOnPath), GlobalCurveApprox>, +} + +impl CurveCache { + /// Create an empty cache + pub fn new() -> Self { + Self::default() + } + + /// Insert the approximation of a [`GlobalCurve`] + pub fn insert( + &mut self, + handle: Handle, + range: RangeOnPath, + approx: GlobalCurveApprox, + ) -> GlobalCurveApprox { + self.inner.insert((handle.id(), range), approx.clone()); + approx + } + + /// Access the approximation for the given [`GlobalCurve`], if available + pub fn get( + &self, + handle: Handle, + range: RangeOnPath, + ) -> Option { + if let Some(approx) = self.inner.get(&(handle.id(), range)) { + return Some(approx.clone()); + } + if let Some(approx) = self.inner.get(&(handle.id(), range.reverse())) { + // If we have a cache entry for the reverse range, we need to use + // that too! + return Some(approx.clone().reverse()); + } + + None + } +} + +/// An approximation of a [`GlobalCurve`] +#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)] +pub struct GlobalCurveApprox { + /// The points that approximate the curve + pub points: Vec>, +} + +impl GlobalCurveApprox { + /// Reverse the order of the approximation + pub fn reverse(mut self) -> Self { + self.points.reverse(); + self + } +} + #[cfg(test)] mod tests { use std::{f64::consts::TAU, ops::Deref}; diff --git a/crates/fj-kernel/src/algorithms/approx/face.rs b/crates/fj-kernel/src/algorithms/approx/face.rs index aa99ea0a9..b81ff580b 100644 --- a/crates/fj-kernel/src/algorithms/approx/face.rs +++ b/crates/fj-kernel/src/algorithms/approx/face.rs @@ -12,7 +12,7 @@ use crate::{ }; use super::{ - curve::CurveCache, cycle::CycleApprox, Approx, ApproxPoint, Tolerance, + cycle::CycleApprox, edge::CurveCache, Approx, ApproxPoint, Tolerance, }; impl Approx for &FaceSet { diff --git a/crates/fj-kernel/src/algorithms/approx/mod.rs b/crates/fj-kernel/src/algorithms/approx/mod.rs index 85dcb1d0a..cc8ae0208 100644 --- a/crates/fj-kernel/src/algorithms/approx/mod.rs +++ b/crates/fj-kernel/src/algorithms/approx/mod.rs @@ -1,6 +1,5 @@ //! Approximation of objects -pub mod curve; pub mod cycle; pub mod edge; pub mod face; diff --git a/crates/fj-kernel/src/algorithms/approx/shell.rs b/crates/fj-kernel/src/algorithms/approx/shell.rs index f3262c1d2..942d13a0f 100644 --- a/crates/fj-kernel/src/algorithms/approx/shell.rs +++ b/crates/fj-kernel/src/algorithms/approx/shell.rs @@ -4,7 +4,7 @@ use std::collections::BTreeSet; use crate::objects::Shell; -use super::{curve::CurveCache, face::FaceApprox, Approx, Tolerance}; +use super::{edge::CurveCache, face::FaceApprox, Approx, Tolerance}; impl Approx for &Shell { type Approximation = BTreeSet; diff --git a/crates/fj-kernel/src/algorithms/approx/sketch.rs b/crates/fj-kernel/src/algorithms/approx/sketch.rs index 4cf8e89df..0510cc167 100644 --- a/crates/fj-kernel/src/algorithms/approx/sketch.rs +++ b/crates/fj-kernel/src/algorithms/approx/sketch.rs @@ -4,7 +4,7 @@ use std::collections::BTreeSet; use crate::objects::Sketch; -use super::{curve::CurveCache, face::FaceApprox, Approx, Tolerance}; +use super::{edge::CurveCache, face::FaceApprox, Approx, Tolerance}; impl Approx for &Sketch { type Approximation = BTreeSet; diff --git a/crates/fj-kernel/src/algorithms/approx/solid.rs b/crates/fj-kernel/src/algorithms/approx/solid.rs index 2077c5666..8995057c7 100644 --- a/crates/fj-kernel/src/algorithms/approx/solid.rs +++ b/crates/fj-kernel/src/algorithms/approx/solid.rs @@ -4,7 +4,7 @@ use std::collections::BTreeSet; use crate::objects::Solid; -use super::{curve::CurveCache, face::FaceApprox, Approx, Tolerance}; +use super::{edge::CurveCache, face::FaceApprox, Approx, Tolerance}; impl Approx for &Solid { type Approximation = BTreeSet;