|
1 | 1 | import simd |
2 | 2 |
|
3 | | - |
4 | | -public extension float3x3 { |
5 | | - |
6 | | - // MARK: - Identity |
7 | | - |
8 | | - /// Returns the identity matrix |
9 | | - static let identity = matrix_identity_float3x3 |
10 | | - |
11 | | - // MARK: - Translate |
12 | | - |
13 | | - /// Returns a translation matrix |
14 | | - /// - Parameter value: the translation value |
15 | | - /// - Returns: a new translation matrix |
16 | | - static func translate(value: SIMD2<Float32>) -> float3x3 { |
17 | | - float3x3( |
18 | | - [1, 0, 0], |
19 | | - [0, 1, 0], |
20 | | - [value.x, value.y, 1] |
21 | | - ) |
22 | | - } |
23 | | - |
24 | | - // MARK: - Rotate |
25 | | - |
26 | | - /// Returns a transformation matrix that rotates around the x and then y axes |
27 | | - /// - Parameter angle: angle |
28 | | - /// - Returns: a new rotation matrix |
29 | | - static func rotate(angle: Angle) -> float3x3 { |
30 | | - let (sin: sx, cos: cx) = sincos(angle) |
31 | | - return float3x3( |
32 | | - [cx, -sx, 0], |
33 | | - [sx, cx, 0], |
34 | | - [0, 0, 1] |
35 | | - ) |
36 | | - } |
37 | | - |
38 | | - // MARK: - Scale |
39 | | - |
40 | | - /// Returns a scaling matrix |
41 | | - /// - Parameter value: the scaling value |
42 | | - /// - Returns: a new scaling matrix |
43 | | - static func scale(value: SIMD2<Float32>) -> float3x3 { |
44 | | - float3x3( |
45 | | - [value.x, 0, 0], |
46 | | - [0, value.y, 0], |
47 | | - [0, 0, 1] |
48 | | - ) |
49 | | - } |
50 | | - |
51 | | - // MARK: - Shear |
52 | | - |
53 | | - /// Returns a shearing matrix along the x-axis |
54 | | - /// - Parameter sx: The shearing value along the x-axis |
55 | | - /// - Returns: A new shearing matrix |
56 | | - static func shear(x: Float32) -> float3x3 { |
57 | | - float3x3( |
58 | | - [1, 0, 0], |
59 | | - [x, 1, 0], |
60 | | - [0, 0, 1] |
61 | | - ) |
| 3 | +extension float3x3 { |
| 4 | + |
| 5 | + // MARK: - Identity |
| 6 | + |
| 7 | + /// Returns the identity matrix |
| 8 | + public static let identity = matrix_identity_float3x3 |
| 9 | + |
| 10 | + // MARK: - Translate |
| 11 | + |
| 12 | + /// Returns a translation matrix |
| 13 | + /// - Parameter value: the translation value |
| 14 | + /// - Returns: a new translation matrix |
| 15 | + public static func translate(value: SIMD2<Float32>) -> float3x3 { |
| 16 | + float3x3( |
| 17 | + [1, 0, 0], |
| 18 | + [0, 1, 0], |
| 19 | + [value.x, value.y, 1] |
| 20 | + ) |
| 21 | + } |
| 22 | + |
| 23 | + // MARK: - Rotate |
| 24 | + |
| 25 | + /// Returns a transformation matrix that rotates around the x and then y axes |
| 26 | + /// - Parameter angle: angle |
| 27 | + /// - Returns: a new rotation matrix |
| 28 | + public static func rotate(angle: Angle) -> float3x3 { |
| 29 | + let (sin:sx, cos:cx) = sincos(angle) |
| 30 | + return float3x3( |
| 31 | + [cx, -sx, 0], |
| 32 | + [sx, cx, 0], |
| 33 | + [0, 0, 1] |
| 34 | + ) |
| 35 | + } |
| 36 | + |
| 37 | + // MARK: - Scale |
| 38 | + |
| 39 | + /// Returns a scaling matrix |
| 40 | + /// - Parameter value: the scaling value |
| 41 | + /// - Returns: a new scaling matrix |
| 42 | + public static func scale(value: SIMD2<Float32>) -> float3x3 { |
| 43 | + float3x3( |
| 44 | + [value.x, 0, 0], |
| 45 | + [0, value.y, 0], |
| 46 | + [0, 0, 1] |
| 47 | + ) |
| 48 | + } |
| 49 | + |
| 50 | + // MARK: - Shear |
| 51 | + |
| 52 | + /// Returns a shearing matrix along the x-axis |
| 53 | + /// - Parameter sx: The shearing value along the x-axis |
| 54 | + /// - Returns: A new shearing matrix |
| 55 | + public static func shear(x: Float32) -> float3x3 { |
| 56 | + float3x3( |
| 57 | + [1, 0, 0], |
| 58 | + [x, 1, 0], |
| 59 | + [0, 0, 1] |
| 60 | + ) |
| 61 | + } |
| 62 | + |
| 63 | + /// Returns a shearing matrix along the y-axis |
| 64 | + /// - Parameter sy: The shearing value along the y-axis |
| 65 | + /// - Returns: A new shearing matrix |
| 66 | + public static func shear(y: Float32) -> float3x3 { |
| 67 | + float3x3( |
| 68 | + [1, y, 0], |
| 69 | + [0, 1, 0], |
| 70 | + [0, 0, 1] |
| 71 | + ) |
| 72 | + } |
| 73 | + |
| 74 | + // MARK: - Matrix Operations |
| 75 | + |
| 76 | + /// Returns a scaling matrix to fit the original size within the bounding size, maintaining the aspect ratio |
| 77 | + /// - Parameters: |
| 78 | + /// - originalSize: The original size of the object |
| 79 | + /// - boundingSize: The bounding size to fit the object into |
| 80 | + /// - Returns: A new scaling matrix |
| 81 | + public static func aspectFitScale( |
| 82 | + originalSize: SIMD2<Float32>, |
| 83 | + boundingSize: SIMD2<Float32> |
| 84 | + ) -> float3x3 { |
| 85 | + var newSize = boundingSize |
| 86 | + let mW = newSize.x / originalSize.x |
| 87 | + let mH = newSize.y / originalSize.y |
| 88 | + |
| 89 | + if mH < mW { |
| 90 | + newSize.x = newSize.y / originalSize.y * originalSize.x |
| 91 | + } else if mW < mH { |
| 92 | + newSize.y = newSize.x / originalSize.x * originalSize.y |
62 | 93 | } |
63 | 94 |
|
64 | | - /// Returns a shearing matrix along the y-axis |
65 | | - /// - Parameter sy: The shearing value along the y-axis |
66 | | - /// - Returns: A new shearing matrix |
67 | | - static func shear(y: Float32) -> float3x3 { |
68 | | - float3x3( |
69 | | - [1, y, 0], |
70 | | - [0, 1, 0], |
71 | | - [0, 0, 1] |
72 | | - ) |
| 95 | + return .scale(value: newSize / originalSize) |
| 96 | + } |
| 97 | + |
| 98 | + /// Returns a scaling matrix to fill the original size within the bounding size, maintaining the aspect ratio and cropping excess |
| 99 | + /// - Parameters: |
| 100 | + /// - originalSize: The original size of the object |
| 101 | + /// - boundingSize: The bounding size to fill the object into |
| 102 | + /// - Returns: A new scaling matrix |
| 103 | + public static func aspectFillScale( |
| 104 | + originalSize: SIMD2<Float32>, |
| 105 | + boundingSize: SIMD2<Float32> |
| 106 | + ) -> float3x3 { |
| 107 | + var newSize = boundingSize |
| 108 | + let mW = newSize.x / originalSize.x |
| 109 | + let mH = newSize.y / originalSize.y |
| 110 | + |
| 111 | + if mH > mW { |
| 112 | + newSize.x = newSize.y / originalSize.y * originalSize.x |
| 113 | + } else if mW > mH { |
| 114 | + newSize.y = newSize.x / originalSize.x * originalSize.y |
73 | 115 | } |
74 | 116 |
|
75 | | - // MARK: - Matrix Operations |
76 | | - |
77 | | - /// Returns a scaling matrix to fit the original size within the bounding size, maintaining the aspect ratio |
78 | | - /// - Parameters: |
79 | | - /// - originalSize: The original size of the object |
80 | | - /// - boundingSize: The bounding size to fit the object into |
81 | | - /// - Returns: A new scaling matrix |
82 | | - static func aspectFitScale( |
83 | | - originalSize: SIMD2<Float32>, |
84 | | - boundingSize: SIMD2<Float32> |
85 | | - ) -> float3x3 { |
86 | | - var newSize = boundingSize |
87 | | - let mW = newSize.x / originalSize.x |
88 | | - let mH = newSize.y / originalSize.y |
89 | | - |
90 | | - if mH < mW { |
91 | | - newSize.x = newSize.y / originalSize.y * originalSize.x |
92 | | - } else if mW < mH { |
93 | | - newSize.y = newSize.x / originalSize.x * originalSize.y |
94 | | - } |
95 | | - |
96 | | - return .scale(value: newSize / originalSize) |
97 | | - } |
98 | | - |
99 | | - /// Returns a scaling matrix to fill the original size within the bounding size, maintaining the aspect ratio and cropping excess |
100 | | - /// - Parameters: |
101 | | - /// - originalSize: The original size of the object |
102 | | - /// - boundingSize: The bounding size to fill the object into |
103 | | - /// - Returns: A new scaling matrix |
104 | | - static func aspectFillScale( |
105 | | - originalSize: SIMD2<Float32>, |
106 | | - boundingSize: SIMD2<Float32> |
107 | | - ) -> float3x3 { |
108 | | - var newSize = boundingSize |
109 | | - let mW = newSize.x / originalSize.x |
110 | | - let mH = newSize.y / originalSize.y |
111 | | - |
112 | | - if mH > mW { |
113 | | - newSize.x = newSize.y / originalSize.y * originalSize.x |
114 | | - } else if mW > mH { |
115 | | - newSize.y = newSize.x / originalSize.x * originalSize.y |
116 | | - } |
117 | | - |
118 | | - return .scale(value: newSize / originalSize) |
119 | | - } |
120 | | - |
121 | | - /// Returns a scaling matrix to fill the original size exactly within the bounding size, without maintaining the aspect ratio |
122 | | - /// - Parameters: |
123 | | - /// - originalSize: The original size of the object |
124 | | - /// - boundingSize: The bounding size to fill the object into |
125 | | - /// - Returns: A new scaling matrix |
126 | | - static func fillScale( |
127 | | - originalSize: SIMD2<Float32>, |
128 | | - boundingSize: SIMD2<Float32> |
129 | | - ) -> float3x3 { |
130 | | - .scale(value: boundingSize / originalSize) |
131 | | - } |
| 117 | + return .scale(value: newSize / originalSize) |
| 118 | + } |
| 119 | + |
| 120 | + /// Returns a scaling matrix to fill the original size exactly within the bounding size, without maintaining the aspect ratio |
| 121 | + /// - Parameters: |
| 122 | + /// - originalSize: The original size of the object |
| 123 | + /// - boundingSize: The bounding size to fill the object into |
| 124 | + /// - Returns: A new scaling matrix |
| 125 | + public static func fillScale( |
| 126 | + originalSize: SIMD2<Float32>, |
| 127 | + boundingSize: SIMD2<Float32> |
| 128 | + ) -> float3x3 { |
| 129 | + .scale(value: boundingSize / originalSize) |
| 130 | + } |
132 | 131 |
|
133 | 132 | } |
134 | 133 |
|
135 | 134 | // MARK: - Codable |
136 | 135 |
|
137 | | -extension float3x3: Codable { |
138 | | - private enum CodingKey: String, Swift.CodingKey { |
139 | | - case column1, column2, column3 |
140 | | - } |
141 | | - |
142 | | - /// Initializes a `float3x3` instance from a decoder |
143 | | - /// - Parameter decoder: The decoder to read data from |
144 | | - /// - Throws: An error if reading from the decoder fails |
145 | | - public init(from decoder: Decoder) throws { |
146 | | - let values = try decoder.container(keyedBy: CodingKey.self) |
147 | | - let c1 = try values.decode(SIMD3<Float>.self, forKey: .column1) |
148 | | - let c2 = try values.decode(SIMD3<Float>.self, forKey: .column2) |
149 | | - let c3 = try values.decode(SIMD3<Float>.self, forKey: .column3) |
150 | | - |
151 | | - self.init(c1, c2, c3) |
152 | | - } |
153 | | - |
154 | | - /// Encodes a `float3x3` instance into an encoder |
155 | | - /// - Parameter encoder: The encoder to write data to |
156 | | - /// - Throws: An error if encoding fails |
157 | | - public func encode(to encoder: Encoder) throws { |
158 | | - var container = encoder.container(keyedBy: CodingKey.self) |
159 | | - try container.encode(self.columns.0, forKey: .column1) |
160 | | - try container.encode(self.columns.1, forKey: .column2) |
161 | | - try container.encode(self.columns.2, forKey: .column3) |
162 | | - } |
| 136 | +extension float3x3: @retroactive Decodable, @retroactive Encodable { |
| 137 | + private enum CodingKey: String, Swift.CodingKey { |
| 138 | + case column1, column2, column3 |
| 139 | + } |
| 140 | + |
| 141 | + /// Initializes a `float3x3` instance from a decoder |
| 142 | + /// - Parameter decoder: The decoder to read data from |
| 143 | + /// - Throws: An error if reading from the decoder fails |
| 144 | + public init(from decoder: Decoder) throws { |
| 145 | + let values = try decoder.container(keyedBy: CodingKey.self) |
| 146 | + let c1 = try values.decode(SIMD3<Float>.self, forKey: .column1) |
| 147 | + let c2 = try values.decode(SIMD3<Float>.self, forKey: .column2) |
| 148 | + let c3 = try values.decode(SIMD3<Float>.self, forKey: .column3) |
| 149 | + |
| 150 | + self.init(c1, c2, c3) |
| 151 | + } |
| 152 | + |
| 153 | + /// Encodes a `float3x3` instance into an encoder |
| 154 | + /// - Parameter encoder: The encoder to write data to |
| 155 | + /// - Throws: An error if encoding fails |
| 156 | + public func encode(to encoder: Encoder) throws { |
| 157 | + var container = encoder.container(keyedBy: CodingKey.self) |
| 158 | + try container.encode(self.columns.0, forKey: .column1) |
| 159 | + try container.encode(self.columns.1, forKey: .column2) |
| 160 | + try container.encode(self.columns.2, forKey: .column3) |
| 161 | + } |
163 | 162 |
|
164 | 163 | } |
0 commit comments