Skip to content

Commit

Permalink
Merge pull request #2016 from hannobraun/approx
Browse files Browse the repository at this point in the history
Make cleanups in `CurveApproxCache`
  • Loading branch information
hannobraun authored Sep 8, 2023
2 parents 5e36cc7 + b7d7fc3 commit 7b904ac
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
25 changes: 25 additions & 0 deletions crates/fj-core/src/algorithms/approx/curve/approx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,31 @@ impl CurveApprox {

self
}

/// Merge the provided segment into the approximation
pub fn merge(
&mut self,
new_segment: CurveApproxSegment,
) -> CurveApproxSegment {
// We assume that approximated curve segments never overlap, unless they
// are completely congruent. As a consequence of this, we don't have to
// do any merging with existing segments here.
//
// For now, this is a valid assumption, as it matches the uses of this
// method, due to documented limitations elsewhere in the system.

let mut existing_segment = None;
for segment in self.segments.iter().cloned() {
if segment.boundary == new_segment.boundary {
existing_segment = Some(segment);
}
}

existing_segment.unwrap_or_else(|| {
self.segments.push(new_segment.clone());
new_segment
})
}
}

#[cfg(test)]
Expand Down
31 changes: 2 additions & 29 deletions crates/fj-core/src/algorithms/approx/curve/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,36 +55,9 @@ impl CurveApproxCache {

new_segment.normalize();

let existing_approx = self.inner.remove(&curve);
let (approx, segment) = match existing_approx {
let (approx, segment) = match self.inner.remove(&curve) {
Some(mut existing_approx) => {
// An approximation for this curve already exists. We need to
// merge the new segment into it.

// We assume that approximated curve segments never overlap,
// unless they are completely congruent. As a consequence of
// this, we don't have to do any merging with existing segments
// here.
//
// For now, this is a valid assumption, as it matches the uses
// of this method, due to documented limitations elsewhere in
// the system.

let mut existing_segment = None;
for segment in existing_approx.segments.iter().cloned() {
if segment.boundary == new_segment.boundary {
existing_segment = Some(segment);
}
}

let segment = match existing_segment {
Some(segment) => segment,
None => {
existing_approx.segments.push(new_segment.clone());
new_segment
}
};

let segment = existing_approx.merge(new_segment);
(existing_approx, segment)
}
None => {
Expand Down

0 comments on commit 7b904ac

Please sign in to comment.