Skip to content

Commit

Permalink
Add common AffineTransform protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasf committed Feb 27, 2024
1 parent 377a590 commit 1eea16d
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 2 deletions.
17 changes: 17 additions & 0 deletions Sources/SwiftSCAD/Values/Transforms/AffineTransform.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Foundation

protocol AffineTransform {
associatedtype Vector: SwiftSCAD.Vector

static var identity: Self { get }

init(_ values: [[Double]])
subscript(_ row: Int, _ column: Int) -> Double { get set }
func setting(row: Int, column: Int, to: Double) -> Self
func applying(_ function: (_ row: Int, _ column: Int, _ value: Double) -> Double) -> Self
static func linearInterpolation(_ from: Self, _ to: Self, factor: Double) -> Self

var inverse: Self { get }
func concatenated(with: Self) -> Self
func apply(to point: Vector) -> Vector
}
3 changes: 2 additions & 1 deletion Sources/SwiftSCAD/Values/Transforms/AffineTransform2D.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import simd
#endif

/// An `AffineTransform2D` represents a 2D affine transformation using a 3x3 matrix.
public struct AffineTransform2D: Equatable {
public struct AffineTransform2D: AffineTransform, Equatable {
typealias Vector = Vector2D
private var matrix: Matrix3x3

internal init(_ matrix: Matrix3x3) {
Expand Down
15 changes: 14 additions & 1 deletion Sources/SwiftSCAD/Values/Transforms/AffineTransform3D.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import simd
#endif

/// An `AffineTransform3D` represents a 3D affine transformation using a 4x4 matrix.
public struct AffineTransform3D: Equatable {
public struct AffineTransform3D: AffineTransform, Equatable {
private var matrix: Matrix4x4

private init(_ matrix: Matrix4x4) {
Expand Down Expand Up @@ -61,6 +61,19 @@ public struct AffineTransform3D: Equatable {
AffineTransform3D(other.matrix * matrix)
}

/// Creates a new `AffineTransform3D` by setting a value at the given row and column indices.
///
/// - Parameters:
/// - row: The row index (0 to 3).
/// - column: The column index (0 to 3).
/// - value: The value to set at the specified row and column.
/// - Returns: A new `AffineTransform3D` with the specified value set.
public func setting(row: Int, column: Int, to value: Double) -> Self {
var transform = self
transform[row, column] = value
return transform
}

public var inverse: AffineTransform3D {
.init(matrix.inverse)
}
Expand Down

0 comments on commit 1eea16d

Please sign in to comment.