-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
140 additions
and
60 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
Sources/SwiftSCAD/Operations/Alignment/Geometry+Aligned.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,39 @@ | ||
import Foundation | ||
|
||
public extension Geometry2D { | ||
/// Aligns the geometry according to specified alignment criteria. | ||
/// | ||
/// This method adjusts the position of the geometry so that its bounding box aligns to the coordinate system's origin. Usage of multiple alignment parameters allows for compound alignments, such as aligning to the center along the X-axis and to the top along the Y-axis. | ||
/// | ||
/// - Parameter alignment: A list of alignment criteria specifying how the geometry should be aligned. Each alignment option targets a specific axis. If more than one alignment is passed for the same axis, the last one is used. | ||
/// - Returns: A new geometry that is the result of applying the specified alignments to the original geometry. | ||
/// | ||
/// Example: | ||
/// ``` | ||
/// let square = Rectangle([10, 10]) | ||
/// .aligned(at: .centerX, .bottom) | ||
/// ``` | ||
/// This example centers the square along the X-axis and aligns its bottom edge with the Y=0 line. | ||
func aligned(at alignment: GeometryAlignment2D...) -> any Geometry2D { | ||
Align2D(content: self, alignment: .init(merging: alignment)) | ||
} | ||
} | ||
|
||
public extension Geometry3D { | ||
/// Aligns the geometry according to specified alignment criteria. | ||
/// | ||
/// This method adjusts the position of the geometry so that its bounding box aligns to the coordinate system's origin. Usage of multiple alignment parameters allows for compound alignments, such as aligning to the center along the X-axis and to the top along the Z-axis. | ||
/// | ||
/// - Parameter alignment: A variable list of alignment criteria specifying how the geometry should be aligned. Each alignment option targets a specific axis. If more than one alignment is passed for the same axis, the last one is used. | ||
/// - Returns: A new geometry that is the result of applying the specified alignments to the original geometry. | ||
/// | ||
/// Example: | ||
/// ``` | ||
/// let box = Box([10, 10, 5]) | ||
/// .aligned(at: .centerX, .bottom) | ||
/// ``` | ||
/// This example centers the square along the X-axis and aligns its bottom edge with the Z=0 line. | ||
func aligned(at alignment: GeometryAlignment3D...) -> any Geometry3D { | ||
Align3D(content: self, alignment: .init(merging: alignment)) | ||
} | ||
} |
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
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
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
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,71 @@ | ||
import Foundation | ||
|
||
public extension Geometry2D { | ||
/// Rotates the geometry around a specified pivot point in 2D space. | ||
/// | ||
/// This method rotates the geometry by the specified angle in the 2D plane. The rotation occurs around a pivot point, which is defined by alignment options. The pivot is determined based on the bounding box of the geometry. | ||
/// | ||
/// - Parameters: | ||
/// - angle: The angle to rotate the geometry. Defaults to `0°`. | ||
/// - pivot: A list of alignment options that specify the pivot point for the rotation. For example, `.center` can be used to rotate the geometry around its center, while `.top` can rotate it around the top boundary. | ||
/// | ||
/// - Returns: A new geometry that is the result of applying the specified rotation and pivot adjustments. | ||
/// | ||
/// Example: | ||
/// ``` | ||
/// Rectangle([10, 8]) | ||
/// .rotated(45°, around: .center) | ||
/// ``` | ||
/// This example rotates the square 45 degrees around its center. | ||
/// | ||
func rotated( | ||
_ angle: Angle = 0°, | ||
around pivot: GeometryAlignment2D... | ||
) -> any Geometry2D { | ||
measuringBounds { _, box in | ||
let alignment = pivot.merged | ||
self | ||
.translated(alignment.offset(for: box)) | ||
.rotated(angle) | ||
.translated(-alignment.offset(for: box)) | ||
} | ||
} | ||
} | ||
|
||
public extension Geometry3D { | ||
/// Rotates the geometry around a specified pivot point in 3D space. | ||
/// | ||
/// This method rotates the geometry by the specified angles along the X, Y, and Z axes. When using multiple axes, the geometry is rotated around the axes in order (first X, then Y, then Z). | ||
/// The rotation occurs around a pivot point, which is defined by alignment options. The pivot is determined based on the bounding box of the geometry. | ||
/// | ||
/// - Parameters: | ||
/// - x: The angle to rotate around the X-axis. Defaults to `0°`. | ||
/// - y: The angle to rotate around the Y-axis. Defaults to `0°`. | ||
/// - z: The angle to rotate around the Z-axis. Defaults to `0°`. | ||
/// - pivot: A list of alignment options that specify the pivot point for the rotation. For example, `.center` can be used to rotate the geometry around its center, while `.top` can rotate it around the top boundary. | ||
/// | ||
/// - Returns: A new geometry that is the result of applying the specified rotation and pivot adjustments. | ||
/// | ||
/// Example: | ||
/// ``` | ||
/// Box([10, 10, 5]) | ||
/// .rotated(x: 90°, around: .center) | ||
/// ``` | ||
/// This example rotates the box 90 degrees around its center. | ||
/// | ||
func rotated( | ||
x: Angle = 0°, | ||
y: Angle = 0°, | ||
z: Angle = 0°, | ||
around pivot: GeometryAlignment3D... | ||
) -> any Geometry3D { | ||
measuringBounds { _, box in | ||
let alignment = pivot.merged | ||
self | ||
.translated(alignment.offset(for: box)) | ||
.rotated(x: x, y: y, z: z) | ||
.translated(-alignment.offset(for: box)) | ||
} | ||
|
||
} | ||
} |
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,15 @@ | ||
import Foundation | ||
|
||
public enum AxisAlignment: Equatable, Sendable { | ||
case min | ||
case mid | ||
case max | ||
|
||
internal var factor: Double { | ||
switch self { | ||
case .min: 0.0 | ||
case .mid: 0.5 | ||
case .max: 1.0 | ||
} | ||
} | ||
} |
38 changes: 0 additions & 38 deletions
38
...erations/Alignment/Alignment+Public.swift → ...Alignment/GeometryAlignment+Presets.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
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