Skip to content

Commit

Permalink
Replacing usage of swift-numerics' isApproximatelyEqual with a faster…
Browse files Browse the repository at this point in the history
… albeit less accurate alternative
  • Loading branch information
LuizZak committed Aug 2, 2024
1 parent 058638b commit d60195d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extension Simplex2Graph {
edge.totalWinding =
contourTree
.queryPoint(center)
.filter({ $0.contour.contains(center) && $0.index != geometry.shapeIndex })
.filter({ $0.index != geometry.shapeIndex && $0.contour.contains(center) })
.reduce(edge.winding.value, { $0 + $1.contour.winding.value })
}

Expand Down
4 changes: 4 additions & 0 deletions Sources/GeometriaClipping/2D/Parametric2GeometrySimplex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public enum Parametric2GeometrySimplex<Vector: Vector2Real>: Parametric2Simplex,
}
}

@inlinable
public func compute(at period: Period) -> Vector {
switch self {
case .lineSegment2(let lineSegment):
Expand All @@ -71,6 +72,7 @@ public enum Parametric2GeometrySimplex<Vector: Vector2Real>: Parametric2Simplex,
}
}

@inlinable
public func isOnSurface(_ vector: Vector, toleranceSquared: Scalar) -> Bool {
switch self {
case .lineSegment2(let lineSegment):
Expand All @@ -81,6 +83,7 @@ public enum Parametric2GeometrySimplex<Vector: Vector2Real>: Parametric2Simplex,
}
}

@inlinable
public func intersectsHorizontalLine(start: Vector, tolerance: Scalar) -> Bool {
switch self {
case .lineSegment2(let lineSegment):
Expand All @@ -97,6 +100,7 @@ public enum Parametric2GeometrySimplex<Vector: Vector2Real>: Parametric2Simplex,
}
}

@inlinable
public func closestPeriod(to vector: Vector) -> Period {
switch self {
case .lineSegment2(let lineSegment):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public enum ParametricClip2Intersection<Period: Hashable & FloatingPoint> {

@inlinable
static func areApproximatelyEqual(_ lhs: Atom, _ rhs: Atom, tolerance: Period) -> Bool {
let lead = lhs.`self`.isApproximatelyEqual(to: rhs.`self`, absoluteTolerance: tolerance)
let trail = lhs.`other`.isApproximatelyEqual(to: rhs.`other`, absoluteTolerance: tolerance)
let lead = lhs.`self`.isApproximatelyEqualFast(to: rhs.`self`, tolerance: tolerance)
let trail = lhs.`other`.isApproximatelyEqualFast(to: rhs.`other`, tolerance: tolerance)

return lead && trail
}
Expand Down
12 changes: 6 additions & 6 deletions Sources/GeometriaClipping/2D/Simplexes/CircleArc2Simplex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,33 +135,33 @@ public struct CircleArc2Simplex<Vector: Vector2Real>: Parametric2Simplex, Equata
// •-s-->
// (
// e
if self.start.x > point.x && self.start.y.isApproximatelyEqual(to: point.y, absoluteTolerance: tolerance) {
if self.start.x > point.x && self.start.y.isApproximatelyEqualFast(to: point.y, tolerance: tolerance) {
return true
}

// s
// (
// •--e->
if self.end.x > point.x && self.end.y.isApproximatelyEqual(to: point.y, absoluteTolerance: tolerance) {
if self.end.x > point.x && self.end.y.isApproximatelyEqualFast(to: point.y, tolerance: tolerance) {
return false
}
} else if self.start.y > end.y {
// •--e->
// (
// s
if self.end.x > point.x && self.end.y.isApproximatelyEqual(to: point.y, absoluteTolerance: tolerance) {
if self.end.x > point.x && self.end.y.isApproximatelyEqualFast(to: point.y, tolerance: tolerance) {
return true
}

// e
// (
// •-s--->
if self.start.x > point.x && self.start.y.isApproximatelyEqual(to: point.y, absoluteTolerance: tolerance) {
if self.start.x > point.x && self.start.y.isApproximatelyEqualFast(to: point.y, tolerance: tolerance) {
return false
}
} else if
self.start.y.isApproximatelyEqual(to: self.end.y, absoluteTolerance: tolerance)
&& self.start.y.isApproximatelyEqual(to: point.y, absoluteTolerance: tolerance)
self.start.y.isApproximatelyEqualFast(to: self.end.y, tolerance: tolerance)
&& self.start.y.isApproximatelyEqualFast(to: point.y, tolerance: tolerance)
{
// s--•--e->
if self.start.x < point.x && self.end.x > point.x {
Expand Down
6 changes: 6 additions & 0 deletions Sources/GeometriaClipping/Utils/FloatingPoint+Ext.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extension FloatingPoint {
@inlinable
func isApproximatelyEqualFast(to other: Self, tolerance: Self) -> Bool {
(self - other).magnitude <= tolerance
}
}

0 comments on commit d60195d

Please sign in to comment.