-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Renaming types from Periodic -> Parametric
- Loading branch information
Showing
37 changed files
with
655 additions
and
1,087 deletions.
There are no files selected for viewing
28 changes: 14 additions & 14 deletions
28
...lipping/2D/Boolean/Boolean2Periodic.swift → ...pping/2D/Boolean/Boolean2Parametric.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
Sources/GeometriaClipping/2D/Boolean/Intersection2Parametric.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/// A Union boolean parametric that joins two shapes into a single shape, if they | ||
/// intersect in space. | ||
public struct Intersection2Parametric<T1: ParametricClip2Geometry, T2: ParametricClip2Geometry>: Boolean2Parametric | ||
where T1.Vector == T2.Vector, T1.Vector: Hashable | ||
{ | ||
public let lhs: T1, rhs: T2 | ||
public let tolerance: Scalar | ||
|
||
public init(_ lhs: T1, _ rhs: T2, tolerance: T1.Scalar) where T1.Vector == T2.Vector { | ||
self.lhs = lhs | ||
self.rhs = rhs | ||
self.tolerance = tolerance | ||
} | ||
|
||
public func allSimplexes() -> [[Simplex]] { | ||
typealias State = GeometriaClipping.State<T1, T2> | ||
|
||
let lookup: IntersectionLookup<T1, T2> = .init( | ||
intersectionsOfSelfShape: lhs, | ||
otherShape: rhs, | ||
tolerance: tolerance | ||
) | ||
|
||
// If no intersections have been found, check if one of the shapes is | ||
// contained within the other | ||
guard !lookup.intersections.isEmpty else { | ||
if lookup.isSelfWithinOther() { | ||
return [rhs.allSimplexes()] | ||
} | ||
if lookup.isOtherWithinSelf() { | ||
return [lhs.allSimplexes()] | ||
} | ||
|
||
return [] | ||
} | ||
|
||
var state = State.onLhs(lhs.startPeriod, rhs.startPeriod) | ||
if lookup.isInsideOther(selfPeriod: state.lhsPeriod) { | ||
state = lookup.next(state).flipped() | ||
} else { | ||
state = lookup.previous(state).flipped() | ||
} | ||
|
||
var result: [Simplex] = [] | ||
var visited: Set<State> = [] | ||
|
||
while visited.insert(state).inserted { | ||
let next = lookup.next(state) | ||
|
||
// Append simplex | ||
let simplex = lookup.clampedSimplexesRange(state, next) | ||
result.append(contentsOf: simplex) | ||
|
||
state = next.flipped() | ||
} | ||
|
||
// Re-normalize the simplex periods | ||
result = result.normalized(startPeriod: .zero, endPeriod: 1) | ||
|
||
return [result] | ||
} | ||
} |
Oops, something went wrong.