Skip to content

Commit

Permalink
optimization to greatly speed up finding intersections between neighb…
Browse files Browse the repository at this point in the history
…ors in some cases.
  • Loading branch information
hfutrell committed Jan 31, 2019
1 parent 9ec1314 commit 168dfa6
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions BezierKit/Library/PathComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ public final class PathComponent: NSObject, NSCoding {
return intersections
}

private func neighborsIntersectOnlyTrivially(_ c1: BezierCurve, _ c2: BezierCurve) -> Bool {
let boundingBox = c1.boundingBox
guard boundingBox.intersection(c2.boundingBox).area == 0 else {
return false
}
for i in 1..<c2.points.count {
if boundingBox.contains(c2.points[i]) {
return false
}
}
return true
}

public func intersects(threshold: CGFloat = BezierKit.defaultIntersectionThreshold) -> [PathComponentIntersection] {
var intersections: [PathComponentIntersection] = []
self.bvh.intersects() { i1, i2 in
Expand All @@ -122,15 +135,22 @@ public final class PathComponent: NSObject, NSCoding {
// we are intersecting two distinct path elements
let c1 = self.curves[i1]
let c2 = self.curves[i2]
elementIntersections = c1.intersects(curve: c2, threshold: threshold).filter {
if i1 == Utils.mod(i2-1, self.curves.count) && $0.t1 == 1.0 {
return false // exclude intersections of i and i+1 at t=1
}
if $0.t1 == 0.0 || $0.t2 == 0.0 {
// use the intersection with the prior path element at t=1 instead
return false
let areNeighbors = i1 == Utils.mod(i2-1, self.curves.count)
if areNeighbors, neighborsIntersectOnlyTrivially(c1, c2) {
// optimize the very common case of element i intersecting i+1 at its endpoint
elementIntersections = []
}
else {
elementIntersections = c1.intersects(curve: c2, threshold: threshold).filter {
if areNeighbors, $0.t1 == 1.0 {
return false // exclude intersections of i and i+1 at t=1
}
if $0.t1 == 0.0 || $0.t2 == 0.0 {
// use the intersection with the prior path element at t=1 instead
return false
}
return true
}
return true
}
}
intersections += elementIntersections.map {
Expand Down

0 comments on commit 168dfa6

Please sign in to comment.