1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
use std::ops::Mul;
use bytemuck::{Pod, Zeroable};
use crate::vec::Vec2;
#[repr(C)]
#[derive(Debug, Clone, Copy, Pod, Zeroable)]
pub struct GpuTransform2d {
// col0 col1 col2 col3
// [m11, [m12, [m13, [0,
// m21, m22, m23, 0,
// 0, 0, 1, 0,
// 0] 0] 0] 1]
pub matrix: [[f32; 4]; 4],
}
impl From<Transform2d> for GpuTransform2d {
fn from(t: Transform2d) -> Self {
let [m11, m21, m12, m22, m13, m23] = t.matrix;
Self {
matrix: [
[m11, m21, 0. , 0. ],
[m12, m22, 0. , 0. ],
[0. , 0. , 1. , 0. ],
[m13, m23, 0. , 1. ],
],
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct Transform2d {
// represents
// [ m11 m12 m13 ]
// [ m21 m22 m23 ]
// [ 0 0 1 ]
// with indices
// [ 0 2 4 ]
// [ 1 3 5 ]
// [ N/A N/A N/A ]
pub matrix: [f32; 6],
}
impl AsRef<Transform2d> for Transform2d {
fn as_ref(&self) -> &Transform2d {
self
}
}
impl Default for Transform2d {
fn default() -> Self {
Self::identity()
}
}
impl Transform2d {
/// Returns the identity transform — no translation, rotation, or scale.
///
/// # Example
/// ```
/// use verdant::{transform::Transform2d, vec::Vec2};
///
/// let t = Transform2d::identity();
/// let p = Vec2::new(3., 4.);
/// assert_eq!(t.transform_point(p), p);
/// ```
pub fn identity() -> Self {
Self {
matrix: [
1., 0.,
0., 1.,
0., 0.,
]
}
}
/// Applies `self` first, then `other`, returning the composed transform.
///
/// # Example
/// ```
/// use verdant::{transform::Transform2d, vec::Vec2};
///
/// let t = Transform2d::translation(1., 0.).then(Transform2d::scaling(2., 2.));
/// let p = t.transform_point(Vec2::new(1., 0.));
/// assert_eq!(p, Vec2::new(4., 0.)); // translated first, then scaled
/// ```
pub fn then(self, other: Self) -> Self {
other.mul(self)
}
/// Returns a transform that translates by `(x, y)`.
///
/// # Example
/// ```
/// use verdant::{transform::Transform2d, vec::Vec2};
///
/// let t = Transform2d::translation(5., -3.);
/// assert_eq!(t.transform_point(Vec2::new(1., 1.)), Vec2::new(6., -2.));
/// ```
pub fn translation(x: f32, y: f32) -> Self {
Self {
matrix: [
1., 0.,
0., 1.,
x, y,
]
}
}
/// Applies an additional translation of `(x, y)` after `self` and returns the result.
///
/// # Example
/// ```
/// use verdant::{transform::Transform2d, vec::Vec2};
///
/// let mut t = Transform2d::scaling(2., 2.);
/// let t2 = t.translate(3., 1.);
/// assert_eq!(t2.transform_point(Vec2::new(1., 1.)), Vec2::new(5., 3.));
/// ```
pub fn translate(&mut self, x: f32, y: f32) -> Self {
*self = self.then(Self::translation(x, y));
*self
}
/// Returns a transform that rotates by `rad` radians, counter-clockwise.
///
/// # Example
/// ```
/// use verdant::{transform::Transform2d, vec::Vec2};
///
/// let t = Transform2d::rotation_rad(std::f32::consts::FRAC_PI_2);
/// let p = t.transform_point(Vec2::new(1., 0.));
/// assert!((p.x - 0.).abs() < 1e-6);
/// assert!((p.y - 1.).abs() < 1e-6);
/// ```
pub fn rotation_rad(rad: f32) -> Self {
let (sin, cos) = rad.sin_cos();
Self {
matrix: [
cos, sin,
-sin, cos,
0., 0.,
]
}
}
/// Applies an additional counter-clockwise rotation of `rad` radians after `self` and returns the result.
///
/// # Example
/// ```
/// use verdant::{transform::Transform2d, vec::Vec2};
///
/// let mut t = Transform2d::translation(1., 0.);
/// let t2 = t.rotate_rad(std::f32::consts::FRAC_PI_2);
/// let p = t2.transform_point(Vec2::new(0., 0.));
/// assert!((p.x - 0.).abs() < 1e-6);
/// assert!((p.y - 1.).abs() < 1e-6);
/// ```
pub fn rotate_rad(&mut self, rad: f32) -> Self {
*self = self.then(Self::rotation_rad(rad));
*self
}
/// Returns a transform that rotates by `deg` degrees, counter-clockwise.
///
/// # Example
/// ```
/// use verdant::{transform::Transform2d, vec::Vec2};
///
/// let t = Transform2d::rotation_deg(90.);
/// let p = t.transform_point(Vec2::new(1., 0.));
/// assert!((p.x - 0.).abs() < 1e-6);
/// assert!((p.y - 1.).abs() < 1e-6);
/// ```
pub fn rotation_deg(deg: f32) -> Self {
let (sin, cos) = deg.to_radians().sin_cos();
Self {
matrix: [
cos, sin,
-sin, cos,
0., 0.,
]
}
}
/// Applies an additional counter-clockwise rotation of `deg` degrees after `self` and returns the result.
///
/// # Example
/// ```
/// use verdant::{transform::Transform2d, vec::Vec2};
///
/// let mut t = Transform2d::translation(1., 0.);
/// let t2 = t.rotate_deg(90.);
/// let p = t2.transform_point(Vec2::new(0., 0.));
/// assert!((p.x - 0.).abs() < 1e-6);
/// assert!((p.y - 1.).abs() < 1e-6);
/// ```
pub fn rotate_deg(&mut self, deg: f32) -> Self {
*self = self.then(Self::rotation_deg(deg));
*self
}
/// Returns a transform that scales by `sx` horizontally and `sy` vertically.
///
/// # Example
/// ```
/// use verdant::{transform::Transform2d, vec::Vec2};
///
/// let t = Transform2d::scaling(2., 3.);
/// assert_eq!(t.transform_point(Vec2::new(1., 1.)), Vec2::new(2., 3.));
/// ```
pub fn scaling(sx: f32, sy: f32) -> Self {
Self {
matrix: [
sx, 0.,
0., sy,
0., 0.,
]
}
}
/// Applies an additional scale of `sx` horizontally and `sy` vertically after `self` and returns the result.
///
/// # Example
/// ```
/// use verdant::{transform::Transform2d, vec::Vec2};
///
/// let t = Transform2d::translation(1., 1.).scale(2., 3.);
/// assert_eq!(t.transform_point(Vec2::new(0., 0.)), Vec2::new(2., 3.));
/// ```
pub fn scale(&mut self, sx: f32, sy: f32) -> Self {
*self = self.then(Transform2d::scaling(sx, sy));
*self
}
/// Applies this transform to a 2D point.
///
/// # Example
/// ```
/// use verdant::{transform::Transform2d, vec::Vec2};
///
/// let t = Transform2d::translation(1., 2.);
/// assert_eq!(t.transform_point(Vec2::new(3., 4.)), Vec2::new(4., 6.));
/// ```
pub fn transform_point(self, p: impl Into<Vec2>) -> Vec2 {
let p = p.into();
let [m11, m21, m12, m22, m13, m23] = self.matrix;
Vec2::new(
m11 * p.x + m12 * p.y + m13,
m21 * p.x + m22 * p.y + m23,
)
}
/// Returns the scale factors encoded in this transform as a [`Vec2`],
/// extracted from the column magnitudes of the rotation/scale portion of the matrix.
///
/// # Example
/// ```
/// use verdant::{transform::Transform2d, vec::Vec2};
///
/// let t = Transform2d::scaling(2., 3.).then(Transform2d::rotation_deg(45.));
/// let s = t.get_scale();
/// assert!((s.x - 2.).abs() < 1e-6);
/// assert!((s.y - 3.).abs() < 1e-6);
/// ```
pub fn get_scale(self) -> Vec2 {
Vec2::new(
(self.matrix[0] * self.matrix[0] + self.matrix[1] * self.matrix[1]).sqrt(),
(self.matrix[2] * self.matrix[2] + self.matrix[3] * self.matrix[3]).sqrt(),
)
}
/// Returns the scale factors encoded in this transform as a [`Vec2`], clamped to a small
/// epsilon (1e-5) to prevent division by zero in internal rendering calculations.
///
/// # Example
/// ```
/// use verdant::{transform::Transform2d, vec::Vec2};
///
/// let t = Transform2d::scaling(0., 5.);
/// let s = t.get_safe_scale();
/// assert!((s.x - 1e-5).abs() < 1e-6); // clamped to epsilon
/// assert!((s.y - 5.).abs() < 1e-6);
pub fn get_safe_scale(self) -> Vec2 {
let s = self.get_scale();
Vec2::new(s.x.max(1e-5), s.y.max(1e-5))
}
/// Applies this transform to a directional 2D vector, ignoring any translation.
/// This is useful for scaling and rotating directional vectors or distances.
///
/// # Example
/// ```
/// use verdant::{transform::Transform2d, vec::Vec2};
/// ```
pub fn transform_vector(self, v: impl Into<Vec2>) -> Vec2 {
let v = v.into();
let [m11, m21, m12, m22, _, _] = self.matrix;
Vec2::new(
m11 * v.x + m12 * v.y,
m21 * v.x + m22 * v.y,
)
}
}
impl Mul for Transform2d {
type Output = Self;
/// Multiplies two transforms together, composing them into a single transform.
/// The result applies `other` first, then `self`.
///
/// # Example
/// ```
/// use verdant::{transform::Transform2d, vec::Vec2};
/// use std::ops::Mul;
///
/// let t = Transform2d::translation(2., 0.).mul(Transform2d::scaling(2., 2.));
/// let p = t.transform_point(Vec2::new(1., 0.));
/// assert_eq!(p, Vec2::new(4., 0.)); // scaled first, then translated
/// ```
fn mul(self, rhs: Self) -> Self::Output {
let [lm11, lm21, lm12, lm22, lm13, lm23] = self.matrix;
let [rm11, rm21, rm12, rm22, rm13, rm23] = rhs.matrix;
let m11 = lm11 * rm11 + lm12 * rm21;
let m21 = lm21 * rm11 + lm22 * rm21;
let m12 = lm11 * rm12 + lm12 * rm22;
let m22 = lm21 * rm12 + lm22 * rm22;
let m13 = lm11 * rm13 + lm12 * rm23 + lm13; // translation x
let m23 = lm21 * rm13 + lm22 * rm23 + lm23; // translation y
Self {
matrix: [
m11, m21,
m12, m22,
m13, m23,
]
}
}
}