diff --git a/Sources/SwiftSCAD/Values/Transforms/AffineTransform.swift b/Sources/SwiftSCAD/Values/Transforms/AffineTransform.swift new file mode 100644 index 0000000..83dae17 --- /dev/null +++ b/Sources/SwiftSCAD/Values/Transforms/AffineTransform.swift @@ -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 +} diff --git a/Sources/SwiftSCAD/Values/Transforms/AffineTransform2D.swift b/Sources/SwiftSCAD/Values/Transforms/AffineTransform2D.swift index 5f7ee1b..7a06c5e 100644 --- a/Sources/SwiftSCAD/Values/Transforms/AffineTransform2D.swift +++ b/Sources/SwiftSCAD/Values/Transforms/AffineTransform2D.swift @@ -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) { diff --git a/Sources/SwiftSCAD/Values/Transforms/AffineTransform3D.swift b/Sources/SwiftSCAD/Values/Transforms/AffineTransform3D.swift index 1d20ba8..f04e061 100644 --- a/Sources/SwiftSCAD/Values/Transforms/AffineTransform3D.swift +++ b/Sources/SwiftSCAD/Values/Transforms/AffineTransform3D.swift @@ -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) { @@ -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) } diff --git a/Sources/SwiftSCAD/Values/Transforms/BasicMatrix3x3.swift b/Sources/SwiftSCAD/Values/Transforms/Matrix/BasicMatrix3x3.swift similarity index 100% rename from Sources/SwiftSCAD/Values/Transforms/BasicMatrix3x3.swift rename to Sources/SwiftSCAD/Values/Transforms/Matrix/BasicMatrix3x3.swift diff --git a/Sources/SwiftSCAD/Values/Transforms/BasicMatrix4x4.swift b/Sources/SwiftSCAD/Values/Transforms/Matrix/BasicMatrix4x4.swift similarity index 100% rename from Sources/SwiftSCAD/Values/Transforms/BasicMatrix4x4.swift rename to Sources/SwiftSCAD/Values/Transforms/Matrix/BasicMatrix4x4.swift diff --git a/Sources/SwiftSCAD/Values/Transforms/Matrix.swift b/Sources/SwiftSCAD/Values/Transforms/Matrix/Matrix.swift similarity index 100% rename from Sources/SwiftSCAD/Values/Transforms/Matrix.swift rename to Sources/SwiftSCAD/Values/Transforms/Matrix/Matrix.swift