Skip to content

Commit

Permalink
Renaming types from Periodic -> Parametric
Browse files Browse the repository at this point in the history
  • Loading branch information
LuizZak committed Jul 8, 2024
1 parent 4e96baf commit 218c733
Show file tree
Hide file tree
Showing 37 changed files with 655 additions and 1,087 deletions.
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
import Geometria

/// A boolean periodic type generator that takes as input periodic types and
/// produces other periodic types based on [boolean algebra] of the input
/// A boolean parametric type generator that takes as input parametric types and
/// produces other parametric types based on [boolean algebra] of the input
/// geometries.
///
/// [boolean algebra]: https://en.wikipedia.org/wiki/Boolean_algebra
public protocol Boolean2Periodic where T1.Vector == T2.Vector {
/// The vector type associated with productions from this boolean periodic
public protocol Boolean2Parametric where T1.Vector == T2.Vector {
/// The vector type associated with productions from this boolean parametric
/// generator.
typealias Vector = T1.Vector

associatedtype T1: Periodic2Geometry
associatedtype T2: Periodic2Geometry
associatedtype T1: ParametricClip2Geometry
associatedtype T2: ParametricClip2Geometry

typealias Scalar = Vector.Scalar
typealias Period = Vector.Scalar

/// The simplex type produced by this periodic geometry.
typealias Simplex = Periodic2GeometrySimplex<Vector>
/// The simplex type produced by this parametric geometry.
typealias Simplex = Parametric2GeometrySimplex<Vector>

/// Creates a new instance of this boolean periodic type with two input
/// periodic geometries.
/// Creates a new instance of this boolean parametric type with two input
/// parametric geometries.
///
/// The tolerance specified is used to differentiate intersections that are
/// too close together. Specifying a tolerance of `Scalar.infinity` results
/// in no intersection being ignored, except for exactly duplicated ones,
/// which are always ignored.
init(_ lhs: T1, _ rhs: T2, tolerance: Scalar) where T1.Vector == Vector, T2.Vector == Vector

/// Generates the simplexes for this boolean periodic.
/// Generates the simplexes for this boolean parametric.
///
/// More than one simplex collection may be generated, depending on the input
/// shapes and the underlying operation being applied.
func allSimplexes() -> [[Simplex]]
}

extension Boolean2Periodic {
/// Creates a new instance of this boolean periodic type with two input
/// periodic geometries.
extension Boolean2Parametric {
/// Creates a new instance of this boolean parametric type with two input
/// parametric geometries.
///
/// The tolerance is set to `Scalar.leastNonzeroMagnitude`.
public init(_ lhs: T1, _ rhs: T2) where T1.Vector == Vector, T2.Vector == Vector {
Expand Down
62 changes: 62 additions & 0 deletions Sources/GeometriaClipping/2D/Boolean/Intersection2Parametric.swift
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]
}
}
Loading

0 comments on commit 218c733

Please sign in to comment.