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

Reuse cached curve approximation, if range is reversed #1557

Merged
merged 3 commits into from
Feb 2, 2023
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
19 changes: 18 additions & 1 deletion crates/fj-kernel/src/algorithms/approx/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,16 @@ impl CurveCache {
handle: Handle<GlobalCurve>,
range: RangeOnPath,
) -> Option<GlobalCurveApprox> {
self.inner.get(&(handle.id(), range)).cloned()
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
}
}

Expand All @@ -192,6 +201,14 @@ pub struct GlobalCurveApprox {
pub points: Vec<ApproxPoint<1>>,
}

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;
Expand Down
8 changes: 8 additions & 0 deletions crates/fj-kernel/src/algorithms/approx/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ pub struct RangeOnPath {
pub boundary: [Point<1>; 2],
}

impl RangeOnPath {
/// Reverse the direction of the range
pub fn reverse(self) -> Self {
let [a, b] = self.boundary;
Self { boundary: [b, a] }
}
}

impl<T> From<[T; 2]> for RangeOnPath
where
T: Into<Point<1>>,
Expand Down