Skip to content

Commit

Permalink
Cache GlobalCurve approximations
Browse files Browse the repository at this point in the history
  • Loading branch information
hannobraun committed Sep 14, 2022
1 parent 17bbf59 commit d64c0fb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
8 changes: 6 additions & 2 deletions crates/fj-kernel/src/algorithms/approx/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,22 @@ impl Approx for (&GlobalCurve, RangeOnCurve) {
fn approx_with_cache(
self,
tolerance: Tolerance,
_: &mut ApproxCache,
cache: &mut ApproxCache,
) -> Self::Approximation {
let (curve, range) = self;

if let Some(approx) = cache.global_curve(curve) {
return approx;
}

let points = match curve.path() {
GlobalPath::Circle(circle) => {
approx_circle(&circle, range, tolerance)
}
GlobalPath::Line(_) => vec![],
};

GlobalCurveApprox { points }
cache.insert_global_curve(curve, GlobalCurveApprox { points })
}
}

Expand Down
26 changes: 24 additions & 2 deletions crates/fj-kernel/src/algorithms/approx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ pub mod tolerance;
use std::{
any::Any,
cmp::Ordering,
collections::BTreeMap,
fmt::Debug,
hash::{Hash, Hasher},
rc::Rc,
};

use fj_math::Point;

use crate::objects::Curve;
use crate::objects::{Curve, GlobalCurve};

use self::curve::GlobalCurveApprox;
pub use self::tolerance::{InvalidTolerance, Tolerance};

/// Approximate an object
Expand All @@ -47,13 +49,33 @@ pub trait Approx: Sized {

/// A cache for results of an approximation
#[derive(Default)]
pub struct ApproxCache;
pub struct ApproxCache {
global_curves: BTreeMap<GlobalCurve, GlobalCurveApprox>,
}

impl ApproxCache {
/// Create an empty cache
pub fn new() -> Self {
Self::default()
}

/// Insert the approximation of a [`GlobalCurve`]
pub fn insert_global_curve(
&mut self,
object: &GlobalCurve,
approx: GlobalCurveApprox,
) -> GlobalCurveApprox {
self.global_curves.insert(*object, approx.clone());
approx
}

/// Access the approximation for the given [`GlobalCurve`], if available
pub fn global_curve(
&self,
object: &GlobalCurve,
) -> Option<GlobalCurveApprox> {
self.global_curves.get(object).cloned()
}
}

/// A point from an approximation, with local and global forms
Expand Down

0 comments on commit d64c0fb

Please sign in to comment.