Skip to content

Commit

Permalink
[Clipping] Using lazy SpatialTree iteration in parametric intersectio…
Browse files Browse the repository at this point in the history
…n creation
  • Loading branch information
LuizZak committed Dec 9, 2024
1 parent 24f96bb commit 3276972
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,42 @@ public struct SpatialTree<Element: BoundableType>: SpatialTreeType where Element
return result
}

/// Performs a point query using a closure to be invoked for each element
/// found to intersect `point`.
@inlinable
public func lazyQueryPoint(
_ point: Vector,
_ closure: (Element) -> Void
) {
root.queryPoint(point) { (_, element) in
closure(element)
}
}

/// Performs a line query using a closure to be invoked for each element
/// found to intersect `line`.
@inlinable
public func lazyQueryLine<Line: LineFloatingPoint>(
_ line: Line,
_ closure: (Element) -> Void
) where Line.Vector == Vector {
root.queryLine(line) { (_, element) in
closure(element)
}
}

/// Performs a bound query using a closure to be invoked for each element
/// found to intersect `area`.
@inlinable
public func lazyQuery<Bounds: BoundableType>(
_ area: Bounds,
_ closure: (Element) -> Void
) where Bounds.Vector == Vector {
root.query(area) { (_, element) in
closure(element)
}
}

// MARK: - Mutation

@inlinable
Expand Down
22 changes: 13 additions & 9 deletions Sources/GeometriaClipping/2D/Graph/Simplex2Graph+Creation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -289,14 +289,17 @@ extension Simplex2Graph {
// MARK: Edge-vertex interferences
for node in nodes {
let nodeAABB = AABB2(center: node.location, size: .init(repeating: tolerance * 2))
let edgesNearNode = edgeTree.query(nodeAABB)

for edge in edgesNearNode where edge.start != node && edge.end != node {
edgeTree.lazyQuery(nodeAABB) { edge in
guard edge.start != node && edge.end != node else {
return
}

let (ratio, distanceSquared) = edge.closestRatio(to: node.location)

// Avoid attempts to split an edge at its end points.
guard ratio > 0 && ratio < 1 else {
continue
return
}

if distanceSquared.squareRoot() < tolerance {
Expand Down Expand Up @@ -414,12 +417,13 @@ extension Simplex2Graph {
var edgesToCheck: OrderedSet<OrderedSet<Edge>> = []

for edge in edges {
let coincident =
edgeTree
.query(edge)
.filter({ next in
next.coincidenceRelationship(with: edge, tolerance: tolerance) == .sameSpan
})
var coincident: [Edge] = []

edgeTree.lazyQuery(edge) { next in
if next.coincidenceRelationship(with: edge, tolerance: tolerance) == .sameSpan {
coincident.append(next)
}
}

guard !coincident.isEmpty else {
continue
Expand Down

0 comments on commit 3276972

Please sign in to comment.