Skip to content

Commit

Permalink
Add method to create a Path from a set of line segments
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Feb 11, 2024
1 parent a942b64 commit d4555d0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
27 changes: 27 additions & 0 deletions Sources/Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,33 @@ public extension Path {
)
}

/// Creates a path from a set of line segments.
/// - Parameter lineSegments: A set of``LineSegment`` to convert to a path.
init(_ lineSegments: Set<LineSegment>) {
var paths = lineSegments.map { Path([.point($0.start), .point($0.end)]) }
outer: while true {
for (i, p) in paths.enumerated() {
let matches = paths.enumerated().filter { j, q in
guard i != j, let p = p.points.last?.position, !q.points.isEmpty else { return false }
return p.isEqual(to: q.points.first!.position) || p.isEqual(to: q.points.last!.position)
}
if let (j, q) = matches.first, matches.count == 1 {
let points: [PathPoint]
if p.points.last!.position.isEqual(to: q.points.first!.position) {
points = p.points + q.points.dropFirst()
} else {
points = p.points + q.points.dropLast().reversed()
}
paths[i] = Path(points)
paths.remove(at: j)
continue outer
}
}
break
}
self.init(subpaths: paths)
}

/// An array of the subpaths that make up the path.
///
/// For paths without nested subpaths, this will return an array containing only `self`.
Expand Down
6 changes: 3 additions & 3 deletions Sources/Polygon+CSG.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public extension Polygon {
}

/// Clip polygon to the specified plane
/// - Parameter plane: The plane to clip the polygon to.
/// - Parameter plane: The ``Plane`` to clip the polygon to.
/// - Returns: An array of the polygon fragments that lie in front of the plane.
func clip(to plane: Plane) -> [Polygon] {
var id = 0
Expand All @@ -37,7 +37,7 @@ public extension Polygon {
}

/// Computes a set of edges where the polygon intersects a plane.
/// - Parameter plane: A ``Plane`` to test against the mesh.
/// - Parameter plane: The ``Plane`` to test against the polygon.
/// - Returns: A `Set` of ``LineSegment`` representing the polygon edges intersecting the plane.
func edges(intersecting plane: Plane) -> Set<LineSegment> {
var edges = Set<LineSegment>()
Expand Down Expand Up @@ -167,7 +167,7 @@ extension Polygon {
}
}

/// Return all intersections with the plane
/// Get all edges intersecting the plane
func intersect(with plane: Plane, edges: inout Set<LineSegment>) {
var wasFront = false, wasBack = false
for edge in undirectedEdges {
Expand Down

0 comments on commit d4555d0

Please sign in to comment.