Skip to main content

ssruntime/eval/
matrix.rs

1use crate::eval::math::*;
2use core::ops::{Add, AddAssign, Div, DivAssign, Index, IndexMut, Mul, MulAssign, Sub, SubAssign};
3use wide::f32x4;
4
5#[repr(C)]
6#[derive(Debug, Clone, Copy, PartialEq, Default)]
7pub struct SsVector3 {
8    pub x: f32,
9    pub y: f32,
10    pub z: f32,
11}
12
13impl SsVector3 {
14    pub const ZERO: Self = Self::new(0.0, 0.0, 0.0);
15    pub const ONE: Self = Self::new(1.0, 1.0, 1.0);
16
17    #[inline]
18    #[must_use]
19    pub const fn new(x: f32, y: f32, z: f32) -> Self {
20        Self { x, y, z }
21    }
22
23    #[inline]
24    #[must_use]
25    pub fn dot(self, rhs: Self) -> f32 {
26        self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
27    }
28
29    #[inline]
30    #[must_use]
31    pub fn length_squared(self) -> f32 {
32        self.dot(self)
33    }
34}
35
36// --- Vector Operators ---
37impl Add for SsVector3 {
38    type Output = Self;
39    #[inline]
40    fn add(self, rhs: Self) -> Self {
41        Self::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
42    }
43}
44impl AddAssign for SsVector3 {
45    #[inline]
46    fn add_assign(&mut self, rhs: Self) {
47        self.x += rhs.x;
48        self.y += rhs.y;
49        self.z += rhs.z;
50    }
51}
52impl Sub for SsVector3 {
53    type Output = Self;
54    #[inline]
55    fn sub(self, rhs: Self) -> Self {
56        Self::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
57    }
58}
59impl SubAssign for SsVector3 {
60    #[inline]
61    fn sub_assign(&mut self, rhs: Self) {
62        self.x -= rhs.x;
63        self.y -= rhs.y;
64        self.z -= rhs.z;
65    }
66}
67impl Mul<f32> for SsVector3 {
68    type Output = Self;
69    #[inline]
70    fn mul(self, rhs: f32) -> Self {
71        Self::new(self.x * rhs, self.y * rhs, self.z * rhs)
72    }
73}
74impl MulAssign<f32> for SsVector3 {
75    #[inline]
76    fn mul_assign(&mut self, rhs: f32) {
77        self.x *= rhs;
78        self.y *= rhs;
79        self.z *= rhs;
80    }
81}
82impl Div<f32> for SsVector3 {
83    type Output = Self;
84    #[inline]
85    fn div(self, rhs: f32) -> Self {
86        let inv = 1.0 / rhs;
87        self * inv
88    }
89}
90impl DivAssign<f32> for SsVector3 {
91    #[inline]
92    fn div_assign(&mut self, rhs: f32) {
93        let inv = 1.0 / rhs;
94        *self *= inv;
95    }
96}
97
98impl Index<usize> for SsVector3 {
99    type Output = f32;
100    fn index(&self, index: usize) -> &Self::Output {
101        match index {
102            0 => &self.x,
103            1 => &self.y,
104            2 => &self.z,
105            _ => panic!("Index out of bounds for SsVector3"),
106        }
107    }
108}
109impl IndexMut<usize> for SsVector3 {
110    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
111        match index {
112            0 => &mut self.x,
113            1 => &mut self.y,
114            2 => &mut self.z,
115            _ => panic!("Index out of bounds for SsVector3"),
116        }
117    }
118}
119
120#[repr(C, align(16))]
121#[derive(Debug, Clone, Copy, PartialEq)]
122pub struct SsMatrix4x4 {
123    pub m: [f32; 16],
124}
125
126impl SsMatrix4x4 {
127    pub const IDENTITY: Self = Self { m: [1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0] };
128
129    #[inline]
130    #[must_use]
131    pub const fn identity() -> Self {
132        Self::IDENTITY
133    }
134
135    #[inline]
136    #[must_use]
137    pub const fn from_array(m: [f32; 16]) -> Self {
138        Self { m }
139    }
140
141    #[inline]
142    #[must_use]
143    pub fn create_scale(x: f32, y: f32, z: f32) -> Self {
144        let mut res = Self::IDENTITY;
145        res.m[0] = x;
146        res.m[5] = y;
147        res.m[10] = z;
148        res
149    }
150
151    #[inline]
152    #[must_use]
153    pub fn create_translation(x: f32, y: f32, z: f32) -> Self {
154        let mut res = Self::IDENTITY;
155        res.m[12] = x;
156        res.m[13] = y;
157        res.m[14] = z;
158        res
159    }
160
161    #[inline]
162    #[must_use]
163    pub fn create_rotation_x(radians: f32) -> Self {
164        let mut res = Self::IDENTITY;
165        let (s, c) = sincosf(radians);
166        res.m[5] = c;
167        res.m[6] = s;
168        res.m[9] = -s;
169        res.m[10] = c;
170        res
171    }
172
173    #[inline]
174    #[must_use]
175    pub fn create_rotation_y(radians: f32) -> Self {
176        let mut res = Self::IDENTITY;
177        let (s, c) = sincosf(radians);
178        res.m[0] = c;
179        res.m[2] = -s;
180        res.m[8] = s;
181        res.m[10] = c;
182        res
183    }
184
185    #[inline]
186    #[must_use]
187    pub fn create_rotation_z(radians: f32) -> Self {
188        let mut res = Self::IDENTITY;
189        let (s, c) = sincosf(radians);
190        res.m[0] = c;
191        res.m[1] = s;
192        res.m[4] = -s;
193        res.m[5] = c;
194        res
195    }
196
197    #[inline]
198    #[must_use]
199    pub fn load_columns(&self) -> (f32x4, f32x4, f32x4, f32x4) {
200        (f32x4::from([self.m[0], self.m[1], self.m[2], self.m[3]]), f32x4::from([self.m[4], self.m[5], self.m[6], self.m[7]]), f32x4::from([self.m[8], self.m[9], self.m[10], self.m[11]]), f32x4::from([self.m[12], self.m[13], self.m[14], self.m[15]]))
201    }
202
203    #[inline]
204    #[must_use]
205    pub fn from_columns(c0: f32x4, c1: f32x4, c2: f32x4, c3: f32x4) -> Self {
206        let mut res = [0.0f32; 16];
207        let a0: [f32; 4] = c0.into();
208        let a1: [f32; 4] = c1.into();
209        let a2: [f32; 4] = c2.into();
210        let a3: [f32; 4] = c3.into();
211        res[0..4].copy_from_slice(&a0);
212        res[4..8].copy_from_slice(&a1);
213        res[8..12].copy_from_slice(&a2);
214        res[12..16].copy_from_slice(&a3);
215        Self { m: res }
216    }
217
218    /// Calculates the world matrix the part's children inherit, and the one it draws
219    /// with, which adds its local scale. Returns `(inheritance_matrix, drawing_matrix)`.
220    #[inline]
221    #[must_use]
222    pub fn compute_world_matrix(state: &crate::runtime::framedata::ss::runtime::PartState, parent_world_matrix: Option<&Self>, parent_size: Option<(f32, f32)>) -> (Self, Self) {
223        let mut pos_x = state.position_x();
224        let mut pos_y = state.position_y();
225
226        if let Some((parent_size_x, parent_size_y)) = parent_size {
227            pos_x += parent_size_x * state.anchor_x();
228            pos_y += parent_size_y * state.anchor_y();
229        }
230        let pos_z = state.position_z();
231
232        let (mut col0, mut col1, mut col2, mut col3) =
233            if let Some(p) = parent_world_matrix { p.load_columns() } else { (f32x4::from([1.0, 0.0, 0.0, 0.0]), f32x4::from([0.0, 1.0, 0.0, 0.0]), f32x4::from([0.0, 0.0, 1.0, 0.0]), f32x4::from([0.0, 0.0, 0.0, 1.0])) };
234
235        // Translation
236        if pos_x != 0.0 || pos_y != 0.0 || pos_z != 0.0 {
237            col3 = col0.mul_add(f32x4::splat(pos_x), col1.mul_add(f32x4::splat(pos_y), col2.mul_add(f32x4::splat(pos_z), col3)));
238        }
239
240        // Rotation X
241        let rx = state.rotation_x() * core::f32::consts::PI / 180.0;
242        if rx != 0.0 {
243            let (sf, cf) = sincosf(rx);
244            let s = f32x4::splat(sf);
245            let c = f32x4::splat(cf);
246            let new_col1 = col1.mul_add(c, col2 * s);
247            let new_col2 = col1.mul_add(-s, col2 * c);
248            col1 = new_col1;
249            col2 = new_col2;
250        }
251
252        // Rotation Y
253        let ry = state.rotation_y() * core::f32::consts::PI / 180.0;
254        if ry != 0.0 {
255            let (sf, cf) = sincosf(ry);
256            let s = f32x4::splat(sf);
257            let c = f32x4::splat(cf);
258            let new_col0 = col0.mul_add(c, col2 * -s);
259            let new_col2 = col0.mul_add(s, col2 * c);
260            col0 = new_col0;
261            col2 = new_col2;
262        }
263
264        // Rotation Z
265        let rz = state.rotation_z() * core::f32::consts::PI / 180.0;
266        if rz != 0.0 {
267            let (sf, cf) = sincosf(rz);
268            let s = f32x4::splat(sf);
269            let c = f32x4::splat(cf);
270            let new_col0 = col0.mul_add(c, col1 * s);
271            let new_col1 = col0.mul_add(-s, col1 * c);
272            col0 = new_col0;
273            col1 = new_col1;
274        }
275
276        // Skew. SpriteStudio's Skew is a per-part Vertex-deform mode that does
277        // NOT inherit, so by default it is applied to local vertices instead of
278        // here (see crate::eval::SKEW_PROPAGATES and geometry::skew_local_tans).
279        // This branch is the propagate path, gated off by `SKEW_PROPAGATES`.
280        let skew_x = state.skew_x() * core::f32::consts::PI / 180.0;
281        let skew_y = state.skew_y() * core::f32::consts::PI / 180.0;
282        if crate::eval::SKEW_PROPAGATES && (skew_x != 0.0 || skew_y != 0.0) {
283            let sx = tanf(skew_x);
284            let sy = tanf(skew_y);
285            let new_col0 = col1.mul_add(f32x4::splat(sy), col0);
286            let new_col1 = col0.mul_add(f32x4::splat(sx), col1);
287            col0 = new_col0;
288            col1 = new_col1;
289        }
290
291        let sx = state.scale_x();
292        let sy = state.scale_y();
293
294        let inh_m = Self::from_columns(col0 * f32x4::splat(sx), col1 * f32x4::splat(sy), col2, col3);
295
296        let lsx = state.local_scale_x();
297        let lsy = state.local_scale_y();
298        let draw_m = Self::from_columns(col0 * f32x4::splat(sx * lsx), col1 * f32x4::splat(sy * lsy), col2, col3);
299
300        (inh_m, draw_m)
301    }
302
303    #[inline]
304    #[must_use]
305    pub fn multiply(&self, rhs: &Self) -> Self {
306        let mut res = [0.0f32; 16];
307
308        let a_col0 = f32x4::from([self.m[0], self.m[1], self.m[2], self.m[3]]);
309        let a_col1 = f32x4::from([self.m[4], self.m[5], self.m[6], self.m[7]]);
310        let a_col2 = f32x4::from([self.m[8], self.m[9], self.m[10], self.m[11]]);
311        let a_col3 = f32x4::from([self.m[12], self.m[13], self.m[14], self.m[15]]);
312
313        for j in 0..4 {
314            let offset = j * 4;
315            let b0j = f32x4::splat(rhs.m[offset]);
316            let b1j = f32x4::splat(rhs.m[offset + 1]);
317            let b2j = f32x4::splat(rhs.m[offset + 2]);
318            let b3j = f32x4::splat(rhs.m[offset + 3]);
319
320            let res_col_j = a_col0.mul_add(b0j, a_col1.mul_add(b1j, a_col2.mul_add(b2j, a_col3 * b3j)));
321            let res_arr: [f32; 4] = res_col_j.into();
322            res[offset..offset + 4].copy_from_slice(&res_arr);
323        }
324        Self { m: res }
325    }
326
327    #[inline]
328    #[must_use]
329    pub fn transform(&self, v: &SsVector3) -> SsVector3 {
330        let a_col0 = f32x4::from([self.m[0], self.m[1], self.m[2], self.m[3]]);
331        let a_col1 = f32x4::from([self.m[4], self.m[5], self.m[6], self.m[7]]);
332        let a_col2 = f32x4::from([self.m[8], self.m[9], self.m[10], self.m[11]]);
333        let a_col3 = f32x4::from([self.m[12], self.m[13], self.m[14], self.m[15]]);
334
335        let vx = f32x4::splat(v.x);
336        let vy = f32x4::splat(v.y);
337        let vz = f32x4::splat(v.z);
338        let vw = f32x4::splat(1.0);
339
340        let res_vec = a_col0.mul_add(vx, a_col1.mul_add(vy, a_col2.mul_add(vz, a_col3 * vw)));
341        let res_arr: [f32; 4] = res_vec.into();
342
343        SsVector3 { x: res_arr[0], y: res_arr[1], z: res_arr[2] }
344    }
345
346    /// Transforms four 2D vectors at once using SIMD, ignoring Z and W for performance.
347    /// `vx`, `vy` are f32x4 containing the x and y components of the four vectors.
348    /// Assumes a 2D affine transformation where z=0 and w=1.
349    #[inline]
350    #[must_use]
351    pub fn transform_v4_2d(&self, vx: f32x4, vy: f32x4) -> (f32x4, f32x4) {
352        let out_x = f32x4::splat(self.m[0]).mul_add(vx, f32x4::splat(self.m[4]).mul_add(vy, f32x4::splat(self.m[12])));
353        let out_y = f32x4::splat(self.m[1]).mul_add(vx, f32x4::splat(self.m[5]).mul_add(vy, f32x4::splat(self.m[13])));
354        (out_x, out_y)
355    }
356
357    #[must_use]
358    pub fn inverse(&self) -> Self {
359        let m = &self.m;
360
361        let s0 = m[0] * m[5] - m[1] * m[4];
362        let s1 = m[0] * m[6] - m[2] * m[4];
363        let s2 = m[0] * m[7] - m[3] * m[4];
364        let s3 = m[1] * m[6] - m[2] * m[5];
365        let s4 = m[1] * m[7] - m[3] * m[5];
366        let s5 = m[2] * m[7] - m[3] * m[6];
367
368        let c5 = m[10] * m[15] - m[11] * m[14];
369        let c4 = m[9] * m[15] - m[11] * m[13];
370        let c3 = m[9] * m[14] - m[10] * m[13];
371        let c2 = m[8] * m[15] - m[11] * m[12];
372        let c1 = m[8] * m[14] - m[10] * m[12];
373        let c0 = m[8] * m[13] - m[9] * m[12];
374
375        let det = s0 * c5 - s1 * c4 + s2 * c3 + s3 * c2 - s4 * c1 + s5 * c0;
376
377        if fabsf(det) < 1e-12 {
378            return Self::IDENTITY;
379        }
380
381        let inv_det = 1.0 / det;
382        let mut res = [0.0f32; 16];
383
384        res[0] = (m[5] * c5 - m[6] * c4 + m[7] * c3) * inv_det;
385        res[1] = (-m[1] * c5 + m[2] * c4 - m[3] * c3) * inv_det;
386        res[2] = (m[13] * s5 - m[14] * s4 + m[15] * s3) * inv_det;
387        res[3] = (-m[9] * s5 + m[10] * s4 - m[11] * s3) * inv_det;
388
389        res[4] = (-m[4] * c5 + m[6] * c2 - m[7] * c1) * inv_det;
390        res[5] = (m[0] * c5 - m[2] * c2 + m[3] * c1) * inv_det;
391        res[6] = (-m[12] * s5 + m[14] * s2 - m[15] * s1) * inv_det;
392        res[7] = (m[8] * s5 - m[10] * s2 + m[11] * s1) * inv_det;
393
394        res[8] = (m[4] * c4 - m[5] * c2 + m[7] * c0) * inv_det;
395        res[9] = (-m[0] * c4 + m[1] * c2 - m[3] * c0) * inv_det;
396        res[10] = (m[12] * s4 - m[13] * s2 + m[15] * s0) * inv_det;
397        res[11] = (-m[8] * s4 + m[9] * s2 - m[11] * s0) * inv_det;
398
399        res[12] = (-m[4] * c3 + m[5] * c1 - m[6] * c0) * inv_det;
400        res[13] = (m[0] * c3 - m[1] * c1 + m[2] * c0) * inv_det;
401        res[14] = (-m[12] * s3 + m[13] * s1 - m[14] * s0) * inv_det;
402        res[15] = (m[8] * s3 - m[9] * s1 + m[10] * s0) * inv_det;
403
404        Self { m: res }
405    }
406}
407
408// --- Matrix Traits ---
409impl Default for SsMatrix4x4 {
410    #[inline]
411    fn default() -> Self {
412        Self::IDENTITY
413    }
414}
415
416impl Mul for SsMatrix4x4 {
417    type Output = Self;
418    #[inline]
419    fn mul(self, rhs: Self) -> Self {
420        self.multiply(&rhs)
421    }
422}
423impl Mul<&SsMatrix4x4> for &SsMatrix4x4 {
424    type Output = SsMatrix4x4;
425    #[inline]
426    fn mul(self, rhs: &SsMatrix4x4) -> SsMatrix4x4 {
427        self.multiply(rhs)
428    }
429}
430impl MulAssign for SsMatrix4x4 {
431    #[inline]
432    fn mul_assign(&mut self, rhs: Self) {
433        *self = *self * rhs;
434    }
435}
436
437impl Mul<SsVector3> for SsMatrix4x4 {
438    type Output = SsVector3;
439    #[inline]
440    fn mul(self, rhs: SsVector3) -> SsVector3 {
441        self.transform(&rhs)
442    }
443}
444
445impl Mul<&SsVector3> for &SsMatrix4x4 {
446    type Output = SsVector3;
447    #[inline]
448    fn mul(self, rhs: &SsVector3) -> SsVector3 {
449        self.transform(rhs)
450    }
451}
452
453impl Index<usize> for SsMatrix4x4 {
454    type Output = f32;
455    #[inline]
456    fn index(&self, index: usize) -> &Self::Output {
457        &self.m[index]
458    }
459}
460impl IndexMut<usize> for SsMatrix4x4 {
461    #[inline]
462    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
463        &mut self.m[index]
464    }
465}
466
467impl AsRef<[f32; 16]> for SsMatrix4x4 {
468    #[inline]
469    fn as_ref(&self) -> &[f32; 16] {
470        &self.m
471    }
472}
473impl AsMut<[f32; 16]> for SsMatrix4x4 {
474    #[inline]
475    fn as_mut(&mut self) -> &mut [f32; 16] {
476        &mut self.m
477    }
478}
479
480// --- Conversion Traits ---
481impl From<[f32; 16]> for SsMatrix4x4 {
482    #[inline]
483    fn from(m: [f32; 16]) -> Self {
484        Self { m }
485    }
486}
487impl From<SsMatrix4x4> for [f32; 16] {
488    #[inline]
489    fn from(mat: SsMatrix4x4) -> [f32; 16] {
490        mat.m
491    }
492}
493
494impl From<[f32; 3]> for SsVector3 {
495    #[inline]
496    fn from(v: [f32; 3]) -> Self {
497        Self::new(v[0], v[1], v[2])
498    }
499}
500impl From<SsVector3> for [f32; 3] {
501    #[inline]
502    fn from(v: SsVector3) -> [f32; 3] {
503        [v.x, v.y, v.z]
504    }
505}
506
507#[cfg(test)]
508mod tests {
509    use super::*;
510
511    const EPSILON: f32 = 1e-5;
512
513    fn assert_f32_eq(a: f32, b: f32) {
514        assert!((a - b).abs() < EPSILON, "{} != {}", a, b);
515    }
516
517    fn assert_vec3_eq(a: SsVector3, b: SsVector3) {
518        assert_f32_eq(a.x, b.x);
519        assert_f32_eq(a.y, b.y);
520        assert_f32_eq(a.z, b.z);
521    }
522
523    fn assert_mat4_eq(a: &SsMatrix4x4, b: &SsMatrix4x4) {
524        for i in 0..16 {
525            assert_f32_eq(a.m[i], b.m[i]);
526        }
527    }
528
529    // --- SsVector3 Tests ---
530
531    #[test]
532    fn test_vector_creation_and_constants() {
533        let v = SsVector3::new(1.0, 2.0, 3.0);
534        assert_eq!(v.x, 1.0);
535        assert_eq!(v.y, 2.0);
536        assert_eq!(v.z, 3.0);
537
538        assert_eq!(SsVector3::ZERO.x, 0.0);
539        assert_eq!(SsVector3::ZERO.y, 0.0);
540        assert_eq!(SsVector3::ZERO.z, 0.0);
541
542        assert_eq!(SsVector3::ONE.x, 1.0);
543        assert_eq!(SsVector3::ONE.y, 1.0);
544        assert_eq!(SsVector3::ONE.z, 1.0);
545    }
546
547    #[test]
548    fn test_vector_math() {
549        let v1 = SsVector3::new(1.0, 2.0, 3.0);
550        let v2 = SsVector3::new(4.0, 5.0, 6.0);
551
552        // Dot product: 1*4 + 2*5 + 3*6 = 4 + 10 + 18 = 32
553        assert_f32_eq(v1.dot(v2), 32.0);
554
555        // Length squared: 1^2 + 2^2 + 3^2 = 14
556        assert_f32_eq(v1.length_squared(), 14.0);
557    }
558
559    #[test]
560    fn test_vector_operators() {
561        let v1 = SsVector3::new(1.0, 2.0, 3.0);
562        let v2 = SsVector3::new(4.0, 5.0, 6.0);
563
564        assert_vec3_eq(v1 + v2, SsVector3::new(5.0, 7.0, 9.0));
565        assert_vec3_eq(v1 - v2, SsVector3::new(-3.0, -3.0, -3.0));
566        assert_vec3_eq(v1 * 2.0, SsVector3::new(2.0, 4.0, 6.0));
567        assert_vec3_eq(v1 / 2.0, SsVector3::new(0.5, 1.0, 1.5));
568
569        let mut v3 = v1;
570        v3 += v2;
571        assert_vec3_eq(v3, SsVector3::new(5.0, 7.0, 9.0));
572
573        let mut v4 = v1;
574        v4 -= v2;
575        assert_vec3_eq(v4, SsVector3::new(-3.0, -3.0, -3.0));
576
577        let mut v5 = v1;
578        v5 *= 3.0;
579        assert_vec3_eq(v5, SsVector3::new(3.0, 6.0, 9.0));
580
581        let mut v6 = v1;
582        v6 /= 2.0;
583        assert_vec3_eq(v6, SsVector3::new(0.5, 1.0, 1.5));
584    }
585
586    #[test]
587    fn test_vector_indexing() {
588        let mut v = SsVector3::new(1.0, 2.0, 3.0);
589        assert_eq!(v[0], 1.0);
590        assert_eq!(v[1], 2.0);
591        assert_eq!(v[2], 3.0);
592
593        v[1] = 5.0;
594        assert_eq!(v.y, 5.0);
595    }
596
597    // --- SsMatrix4x4 Tests ---
598
599    #[test]
600    fn test_matrix_identity() {
601        let id = SsMatrix4x4::identity();
602        assert_f32_eq(id.m[0], 1.0);
603        assert_f32_eq(id.m[5], 1.0);
604        assert_f32_eq(id.m[10], 1.0);
605        assert_f32_eq(id.m[15], 1.0);
606
607        assert_f32_eq(id.m[1], 0.0);
608    }
609
610    #[test]
611    fn test_matrix_create_scale() {
612        let s = SsMatrix4x4::create_scale(2.0, 3.0, 4.0);
613        assert_f32_eq(s.m[0], 2.0);
614        assert_f32_eq(s.m[5], 3.0);
615        assert_f32_eq(s.m[10], 4.0);
616        assert_f32_eq(s.m[15], 1.0);
617    }
618
619    #[test]
620    fn test_matrix_create_translation() {
621        let t = SsMatrix4x4::create_translation(5.0, 6.0, 7.0);
622        assert_f32_eq(t.m[12], 5.0);
623        assert_f32_eq(t.m[13], 6.0);
624        assert_f32_eq(t.m[14], 7.0);
625        assert_f32_eq(t.m[15], 1.0);
626    }
627
628    #[test]
629    fn test_matrix_create_rotation_x() {
630        let r = SsMatrix4x4::create_rotation_x(core::f32::consts::FRAC_PI_2); // 90 degrees
631        // cos(90) = 0, sin(90) = 1
632        assert_f32_eq(r.m[5], 0.0);
633        assert_f32_eq(r.m[6], 1.0);
634        assert_f32_eq(r.m[9], -1.0);
635        assert_f32_eq(r.m[10], 0.0);
636    }
637
638    #[test]
639    fn test_matrix_create_rotation_y() {
640        let r = SsMatrix4x4::create_rotation_y(core::f32::consts::FRAC_PI_2); // 90 degrees
641        // cos(90) = 0, sin(90) = 1
642        assert_f32_eq(r.m[0], 0.0);
643        assert_f32_eq(r.m[2], -1.0);
644        assert_f32_eq(r.m[8], 1.0);
645        assert_f32_eq(r.m[10], 0.0);
646    }
647
648    #[test]
649    fn test_matrix_create_rotation_z() {
650        let r = SsMatrix4x4::create_rotation_z(core::f32::consts::FRAC_PI_2); // 90 degrees
651        // cos(90) = 0, sin(90) = 1
652        assert_f32_eq(r.m[0], 0.0);
653        assert_f32_eq(r.m[1], 1.0);
654        assert_f32_eq(r.m[4], -1.0);
655        assert_f32_eq(r.m[5], 0.0);
656    }
657
658    #[test]
659    fn test_matrix_multiplication() {
660        let t = SsMatrix4x4::create_translation(1.0, 2.0, 3.0);
661        let s = SsMatrix4x4::create_scale(2.0, 2.0, 2.0);
662
663        // Scale then Translate (T * S)
664        // Vector v will be scaled by 2, then translated by (1, 2, 3)
665        let ts = t * s;
666
667        let v = SsVector3::new(1.0, 1.0, 1.0);
668        let res = ts * v;
669
670        // (1*2)+1 = 3, (1*2)+2 = 4, (1*2)+3 = 5
671        assert_vec3_eq(res, SsVector3::new(3.0, 4.0, 5.0));
672    }
673
674    #[test]
675    fn test_matrix_transform_vector() {
676        let t = SsMatrix4x4::create_translation(5.0, -2.0, 1.0);
677        let v = SsVector3::new(1.0, 1.0, 1.0);
678
679        let transformed = t.transform(&v);
680        assert_vec3_eq(transformed, SsVector3::new(6.0, -1.0, 2.0));
681
682        // Test operator overloading
683        let transformed2 = t * v;
684        assert_vec3_eq(transformed2, SsVector3::new(6.0, -1.0, 2.0));
685    }
686
687    #[test]
688    fn test_matrix_inverse() {
689        // Identity inverse is Identity
690        let id = SsMatrix4x4::identity();
691        let id_inv = id.inverse();
692        assert_mat4_eq(&id, &id_inv);
693
694        // Translation inverse
695        let t = SsMatrix4x4::create_translation(2.0, 3.0, 4.0);
696        let t_inv = t.inverse();
697        let expected_t_inv = SsMatrix4x4::create_translation(-2.0, -3.0, -4.0);
698        assert_mat4_eq(&t_inv, &expected_t_inv);
699
700        // Scale inverse
701        let s = SsMatrix4x4::create_scale(2.0, 4.0, 5.0);
702        let s_inv = s.inverse();
703        let expected_s_inv = SsMatrix4x4::create_scale(0.5, 0.25, 0.2);
704        assert_mat4_eq(&s_inv, &expected_s_inv);
705
706        // T * S inverse
707        let ts = t * s;
708        let ts_inv = ts.inverse();
709
710        // A * A^-1 = I
711        let identity_check = ts * ts_inv;
712        assert_mat4_eq(&identity_check, &SsMatrix4x4::identity());
713    }
714
715    #[test]
716    fn test_compute_world_matrix() {
717        let mut state = crate::runtime::framedata::ss::runtime::PartState::default();
718        state.set_position_x(10.0);
719        state.set_position_y(20.0);
720        state.set_scale_x(2.0);
721        state.set_scale_y(3.0);
722        state.set_local_scale_x(0.5);
723        state.set_local_scale_y(2.0);
724
725        // No parent
726        let (inh, draw) = SsMatrix4x4::compute_world_matrix(&state, None, None);
727
728        let expected_inh = SsMatrix4x4::create_translation(10.0, 20.0, 0.0) * SsMatrix4x4::create_scale(2.0, 3.0, 1.0);
729        let expected_draw = SsMatrix4x4::create_translation(10.0, 20.0, 0.0) * SsMatrix4x4::create_scale(1.0, 6.0, 1.0); // 2*0.5, 3*2
730
731        assert_mat4_eq(&inh, &expected_inh);
732        assert_mat4_eq(&draw, &expected_draw);
733
734        // With Parent and Anchor
735        state.set_anchor_x(0.5);
736        state.set_anchor_y(0.5);
737        let parent_mat = SsMatrix4x4::create_translation(100.0, 100.0, 0.0);
738
739        let (inh2, draw2) = SsMatrix4x4::compute_world_matrix(&state, Some(&parent_mat), Some((50.0, 50.0)));
740
741        // Final translation: parent(100,100) + pos(10,20) + anchor(25,25) = (135, 145)
742        let expected_inh2 = parent_mat * SsMatrix4x4::create_translation(35.0, 45.0, 0.0) * SsMatrix4x4::create_scale(2.0, 3.0, 1.0);
743        let expected_draw2 = parent_mat * SsMatrix4x4::create_translation(35.0, 45.0, 0.0) * SsMatrix4x4::create_scale(1.0, 6.0, 1.0);
744
745        assert_mat4_eq(&inh2, &expected_inh2);
746        assert_mat4_eq(&draw2, &expected_draw2);
747    }
748
749    #[test]
750    fn test_compute_world_matrix_rotation() {
751        let mut state = crate::runtime::framedata::ss::runtime::PartState::default();
752        state.set_position_x(10.0);
753        state.set_position_y(20.0);
754        state.set_position_z(5.0);
755        state.set_rotation_x(30.0);
756        state.set_rotation_y(45.0);
757        state.set_rotation_z(60.0);
758        state.set_scale_x(2.0);
759        state.set_scale_y(3.0);
760        state.set_local_scale_x(1.5);
761        state.set_local_scale_y(0.5);
762
763        let (inh, draw) = SsMatrix4x4::compute_world_matrix(&state, None, None);
764
765        // Expected matrices using scalar math functions
766        let mut expected_m = SsMatrix4x4::identity();
767        expected_m *= SsMatrix4x4::create_translation(10.0, 20.0, 5.0);
768        expected_m *= SsMatrix4x4::create_rotation_x(30.0 * core::f32::consts::PI / 180.0);
769        expected_m *= SsMatrix4x4::create_rotation_y(45.0 * core::f32::consts::PI / 180.0);
770        expected_m *= SsMatrix4x4::create_rotation_z(60.0 * core::f32::consts::PI / 180.0);
771
772        let expected_inh = expected_m * SsMatrix4x4::create_scale(2.0, 3.0, 1.0);
773        let expected_draw = expected_m * SsMatrix4x4::create_scale(2.0 * 1.5, 3.0 * 0.5, 1.0);
774
775        assert_mat4_eq(&inh, &expected_inh);
776        assert_mat4_eq(&draw, &expected_draw);
777    }
778
779    #[test]
780    fn test_matrix_multiplication_consistency() {
781        let mut m1 = SsMatrix4x4::identity();
782        m1.m[0] = 2.0;
783        m1.m[5] = 3.0;
784        m1.m[10] = 4.0;
785        m1.m[12] = 10.0;
786        m1.m[13] = 20.0;
787        m1.m[14] = 30.0;
788
789        let mut m2 = SsMatrix4x4::identity();
790        m2.m[0] = 0.5;
791        m2.m[5] = 0.5;
792        m2.m[10] = 0.5;
793        m2.m[12] = -5.0;
794        m2.m[13] = -10.0;
795        m2.m[14] = -15.0;
796
797        let res = m1 * m2;
798
799        // Manual calculation check for Column-Major
800        // res[0] = m1[0]*m2[0] + m1[4]*m2[1] + m1[8]*m2[2] + m1[12]*m2[3]
801        //        = 2.0 * 0.5 + 0 + 0 + 10 * 0 = 1.0
802        assert_f32_eq(res.m[0], 1.0);
803        assert_f32_eq(res.m[5], 1.5);
804        assert_f32_eq(res.m[10], 2.0);
805
806        // Translation X: res[12] = m1[0]*m2[12] + m1[4]*m2[13] + m1[8]*m2[14] + m1[12]*m2[15]
807        //                        = 2.0 * -5.0 + 0 + 0 + 10.0 * 1.0 = -10 + 10 = 0
808        assert_f32_eq(res.m[12], 0.0);
809        // Translation Y: res[13] = m1[1]*m2[12] + m1[5]*m2[13] + m1[9]*m2[14] + m1[13]*m2[15]
810        //                        = 0 + 3.0 * -10.0 + 0 + 20.0 * 1.0 = -10.0
811        assert_f32_eq(res.m[13], -10.0);
812    }
813
814    #[test]
815    fn test_compute_world_matrix_skew() {
816        let mut state = crate::runtime::framedata::ss::runtime::PartState::default();
817        state.set_scale_x(1.0);
818        state.set_scale_y(1.0);
819        state.set_skew_x(30.0);
820        state.set_skew_y(45.0);
821
822        let (inh, _) = SsMatrix4x4::compute_world_matrix(&state, None, None);
823
824        let tan30 = tanf(30.0f32 * core::f32::consts::PI / 180.0);
825        let tan45 = tanf(45.0f32 * core::f32::consts::PI / 180.0);
826
827        if crate::eval::SKEW_PROPAGATES {
828            // Propagate mode: Skew is folded into the world matrix so it
829            // inherits down to children.
830            //   col0' = col0 + col1 * tan(sy) = [1, tan45, 0, 0]
831            //   col1' = col1 + col0 * tan(sx) = [tan30, 1, 0, 0]
832            assert_f32_eq(inh.m[0], 1.0);
833            assert_f32_eq(inh.m[1], tan45);
834            assert_f32_eq(inh.m[4], tan30);
835            assert_f32_eq(inh.m[5], 1.0);
836        } else {
837            // Non-propagate (SpriteStudio default): Skew is a per-part local
838            // vertex deform, so it must NOT touch the world matrix — otherwise
839            // it would inherit to children, which SpriteStudio does not do.
840            let _ = (tan30, tan45);
841            assert_f32_eq(inh.m[0], 1.0);
842            assert_f32_eq(inh.m[1], 0.0);
843            assert_f32_eq(inh.m[4], 0.0);
844            assert_f32_eq(inh.m[5], 1.0);
845        }
846    }
847}