Skip to content

Commit

Permalink
[GeometriaClipping] Optimizing a few hotspots
Browse files Browse the repository at this point in the history
  • Loading branch information
LuizZak committed Aug 17, 2024
1 parent cebf33e commit 46cc3f2
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ lint-results.html
docs/
.swiftpm/
*.profraw
perf/
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Geometria
import GeometriaAlgorithms
import OrderedCollections

@_specializeExtension
extension Simplex2Graph {
@usableFromInline
internal typealias GlobalIntersection = (lhs: Int, lhsPeriod: Vector.Scalar, rhs: Int, rhsPeriod: Vector.Scalar)
Expand Down Expand Up @@ -33,6 +34,8 @@ extension Simplex2Graph {
}

@inlinable
@_specialize(exported: true, kind: full, where Vector == Vector2D)
@_specialize(exported: true, kind: full, where Vector == Vector2F)
public static func fromParametricIntersections(
contours: [Contour],
tolerance: Scalar
Expand Down Expand Up @@ -349,6 +352,10 @@ extension Simplex2Graph {

var finalSet: Set<Node> = []
for neighbor in neighbors {
guard neighbor !== node else {
finalSet.insert(neighbor)
continue
}
if areClose(neighbor.location, node.location) || areIntersection(node, neighbor) {
finalSet.insert(neighbor)
}
Expand Down
18 changes: 11 additions & 7 deletions Sources/GeometriaClipping/2D/Graph/Simplex2Graph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public struct Simplex2Graph<Vector: Vector2Real & Hashable> {
}
}

public class Node: Hashable, CustomStringConvertible {
public final class Node: Hashable, CustomStringConvertible {
public typealias ShapeIndex = Int

public var id: Int
Expand Down Expand Up @@ -154,12 +154,16 @@ public struct Simplex2Graph<Vector: Vector2Real & Hashable> {

@inlinable
func references(shapeIndex: Int, period: Period) -> Bool {
let query = Kind.SharedGeometryEntry(
shapeIndex: shapeIndex,
period: period
)
switch kind {
case .geometry(shapeIndex, period):
return true

return geometries.contains(query)
case .sharedGeometry(let entries):
return entries.contains(where: { $0.shapeIndex == shapeIndex && $0.period == period })

default:
return false
}
}

public func hash(into hasher: inout Hasher) {
Expand Down Expand Up @@ -270,7 +274,7 @@ public struct Simplex2Graph<Vector: Vector2Real & Hashable> {
}
}

public class Edge: AbstractDirectedGraphEdge, Hashable, CustomStringConvertible {
public final class Edge: AbstractDirectedGraphEdge, Hashable, CustomStringConvertible {
/// A unique identifier assigned during graph generation, used to sort
/// edges by earliest generation.
public var id: Int
Expand Down
4 changes: 1 addition & 3 deletions Sources/GeometriaClipping/2D/Parametric2Contour.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,13 @@ public struct Parametric2Contour<Vector: Vector2Real> {
// is divisible by two, then the point is not contained within the
// contour.

let simplexes = allSimplexes()

let bounds = self.bounds
if !bounds.contains(point) {
return false
}

var intersections = 0
for simplex in simplexes {
for simplex in allSimplexes() {
if simplex.intersectsHorizontalLine(start: point, tolerance: .exp10(-13)) {
intersections += 1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class Subtraction2ParametricTests: XCTestCase {
}

measure {
_ = subtraction(inputs[0], inputs[1...])
_ = subtraction(inputs[0], Array(inputs[1...]))
}
}

Expand Down

0 comments on commit 46cc3f2

Please sign in to comment.