Skip to content

Commit

Permalink
Add methods to reflect mesh polygons along a plane
Browse files Browse the repository at this point in the history
  • Loading branch information
zilmarinen committed Aug 30, 2024
1 parent eee79b3 commit 0100e5e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Sources/Mesh+CSG.swift
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,11 @@ public extension Mesh {
}
return edges
}

/// Reflects each polygon of the mesh along a plane.
/// - Parameter plane: The ``Plane`` against which the vertices are to be reflected.
/// - Returns: A ``Mesh`` representing the reflected mesh.
func reflect(along plane: Plane) -> Self { Self(polygons.map { $0.reflect(along: plane) }) }
}

private func boundsTest(
Expand Down
21 changes: 21 additions & 0 deletions Sources/Polygon+CSG.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,27 @@ public extension Polygon {
intersect(with: plane, edges: &edges)
return edges
}

/// Reflects each vertex of the polygon along a plane.
/// - Parameter plane: The ``Plane`` against which the vertices are to be reflected.
/// - Returns: A ``Polygon`` representing the reflected vertices.
func reflect(along plane: Plane) -> Self {
mapVertices {
let p = $0.position.project(onto: plane)
let d = $0.position - p

//https://math.stackexchange.com/questions/13261/how-to-get-a-reflection-vector
//𝑟=𝑑−2(𝑑⋅𝑛)𝑛
let n = plane.normal - 2.0 * plane.normal.dot($0.normal) * $0.normal

return Vertex(
p - d,
n,
$0.texcoord,
$0.color
)
}.inverted()
}
}

extension Polygon {
Expand Down

0 comments on commit 0100e5e

Please sign in to comment.