gemath 0.1.0

Type-safe game math with type-level units/spaces, typed angles, and explicit fallible ops (plus optional geometry/collision).
Documentation
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
#![cfg(feature = "vec2")]

use gemath::vec2::*;
use gemath::{Degrees, Radians};

#[cfg(test)]
mod tests {
    use super::*;
    // `libm` implementations can differ slightly from `std` in edge accuracy.
    const EPSILON: f32 = if cfg!(feature = "libm") { 1e-6 } else { 1e-7 };

    #[test]
    fn test_vec2_new() {
        let v = Vec2f32::new(1.0, 2.0);
        assert!((v.x - 1.0).abs() < EPSILON);
        assert!((v.y - 2.0).abs() < EPSILON);
    }

    #[test]
    fn test_vec2_zero() {
        let v = Vec2f32::ZERO;
        assert!((v.x - 0.0).abs() < EPSILON);
        assert!((v.y - 0.0).abs() < EPSILON);
    }

    #[test]
    fn test_vec2_default() {
        let v: Vec2f32 = Default::default();
        assert!((v.x - 0.0).abs() < EPSILON);
        assert!((v.y - 0.0).abs() < EPSILON);
    }

    #[test]
    fn test_vec2_checked_div_scalar() {
        let v = Vec2f32::new(2.0, 4.0);
        assert_eq!(v.checked_div_scalar(2.0), Some(Vec2f32::new(1.0, 2.0)));
        assert_eq!(v.checked_div_scalar(0.0), None);
        assert_eq!(v.checked_div_scalar(-0.0), None);
        assert_eq!(v.checked_div_scalar(f32::NAN), None);
        assert_eq!(v.checked_div_scalar(f32::INFINITY), None);
    }

    #[test]
    fn test_vec2_eq() {
        let v1 = Vec2f32::new(1.0, 2.0);
        let v2 = Vec2f32::new(1.0, 2.0);
        let v3 = Vec2f32::new(2.0, 1.0);
        assert_eq!(v1, v2);
        assert_ne!(v1, v3);
    }

    #[test]
    fn test_vec2_clone_copy() {
        let v1 = Vec2f32::new(1.0, 2.0);
        let v2 = v1; // Copy
        let mut v3 = v1;
        v3.x = 3.0;
        assert!((v1.x - 1.0).abs() < EPSILON); // v1 should be unchanged
        assert!((v2.x - 1.0).abs() < EPSILON);
        assert!((v3.x - 3.0).abs() < EPSILON);
    }

    #[test]
    fn test_vec2_add() {
        let v1 = Vec2f32::new(1.0, 2.0);
        let v2 = Vec2f32::new(3.0, 4.0);
        let result = v1 + v2;
        assert!((result.x - 4.0).abs() < EPSILON);
        assert!((result.y - 6.0).abs() < EPSILON);

        let mut v3 = Vec2f32::new(1.0, 2.0);
        v3 += v2;
        assert!((v3.x - 4.0).abs() < EPSILON);
        assert!((v3.y - 6.0).abs() < EPSILON);
    }

    #[test]
    fn test_vec2_sub() {
        let v1 = Vec2f32::new(3.0, 4.0);
        let v2 = Vec2f32::new(1.0, 2.0);
        let result = v1 - v2;
        assert!((result.x - 2.0).abs() < EPSILON);
        assert!((result.y - 2.0).abs() < EPSILON);

        let mut v3 = Vec2f32::new(3.0, 4.0);
        v3 -= v2;
        assert!((v3.x - 2.0).abs() < EPSILON);
        assert!((v3.y - 2.0).abs() < EPSILON);
    }

    #[test]
    fn test_vec2_mul_scalar() {
        let v1 = Vec2f32::new(1.0, 2.0);
        let scalar = 3.0;
        let result1 = v1 * scalar;
        assert!((result1.x - 3.0).abs() < EPSILON);
        assert!((result1.y - 6.0).abs() < EPSILON);

        let result2 = scalar * v1;
        assert!((result2.x - 3.0).abs() < EPSILON);
        assert!((result2.y - 6.0).abs() < EPSILON);

        let mut v2 = Vec2f32::new(1.0, 2.0);
        v2 *= scalar;
        assert!((v2.x - 3.0).abs() < EPSILON);
        assert!((v2.y - 6.0).abs() < EPSILON);
    }

    #[test]
    fn test_vec2_div_scalar() {
        let v1 = Vec2f32::new(3.0, 6.0);
        let scalar = 3.0;
        let result = v1 / scalar;
        assert!((result.x - 1.0).abs() < EPSILON);
        assert!((result.y - 2.0).abs() < EPSILON);

        let mut v2 = Vec2f32::new(3.0, 6.0);
        v2 /= scalar;
        assert!((v2.x - 1.0).abs() < EPSILON);
        assert!((v2.y - 2.0).abs() < EPSILON);
    }

    #[test]
    fn test_vec2_mul_hadamard() {
        let v1 = Vec2f32::new(1.0, 2.0);
        let v2 = Vec2f32::new(3.0, 4.0);
        let result = v1 * v2;
        assert!((result.x - 3.0).abs() < EPSILON);
        assert!((result.y - 8.0).abs() < EPSILON);

        let mut v3 = Vec2f32::new(1.0, 2.0);
        v3 *= v2;
        assert!((v3.x - 3.0).abs() < EPSILON);
        assert!((v3.y - 8.0).abs() < EPSILON);
    }

    #[test]
    fn test_vec2_div_hadamard() {
        let v1 = Vec2f32::new(3.0, 8.0);
        let v2 = Vec2f32::new(3.0, 4.0);
        let result = v1 / v2;
        assert!((result.x - 1.0).abs() < EPSILON);
        assert!((result.y - 2.0).abs() < EPSILON);

        let mut v3 = Vec2f32::new(3.0, 8.0);
        v3 /= v2;
        assert!((v3.x - 1.0).abs() < EPSILON);
        assert!((v3.y - 2.0).abs() < EPSILON);
    }

    #[test]
    fn test_vec2_neg() {
        let v1 = Vec2f32::new(1.0, -2.0);
        let result = -v1;
        assert!((result.x - (-1.0)).abs() < EPSILON);
        assert!((result.y - 2.0).abs() < EPSILON);
    }

    #[test]
    fn test_vec2_dot() {
        let v1 = Vec2f32::new(1.0, 2.0);
        let v2 = Vec2f32::new(3.0, 4.0);
        let result = v1.dot(v2);
        // 1*3 + 2*4 = 3 + 8 = 11
        assert!((result - 11.0).abs() < EPSILON);

        let v3 = Vec2f32::new(-1.0, 0.5);
        let v4 = Vec2f32::new(2.0, -4.0);
        let result2 = v3.dot(v4);
        // -1*2 + 0.5*(-4) = -2 - 2 = -4
        assert!((result2 - (-4.0)).abs() < EPSILON);

        // Dot product with zero vector
        assert!((Vec2f32::ZERO.dot(v1) - 0.0).abs() < EPSILON);
        assert!((v1.dot(Vec2f32::ZERO) - 0.0).abs() < EPSILON);
    }

    #[test]
    fn test_vec2_cross() {
        let v1 = Vec2f32::new(1.0, 0.0);
        let v2 = Vec2f32::new(0.0, 1.0);
        assert!((v1.cross(v2) - 1.0).abs() < EPSILON);

        let v3 = Vec2f32::new(1.0, 1.0);
        let v4 = Vec2f32::new(-1.0, 1.0);
        assert!((v3.cross(v4) - 2.0).abs() < EPSILON);

        assert!((v1.cross(v1)).abs() < EPSILON); // Cross product with self is 0
    }

    #[test]
    fn test_vec2_length_squared() {
        let v1 = Vec2f32::new(3.0, 4.0);
        // 3*3 + 4*4 = 9 + 16 = 25
        assert!((v1.length_squared() - 25.0).abs() < EPSILON);
        assert!((Vec2f32::ZERO.length_squared() - 0.0).abs() < EPSILON);
    }

    #[test]
    fn test_vec2_length() {
        let v1 = Vec2f32::new(3.0, 4.0);
        assert!((v1.length() - 5.0).abs() < EPSILON);
        assert!((Vec2f32::ZERO.length() - 0.0).abs() < EPSILON);
        let v2 = Vec2f32::new(1.0, 0.0);
        assert!((v2.length() - 1.0).abs() < EPSILON);
    }

    #[test]
    fn test_vec2_normalize() {
        let v1 = Vec2f32::new(3.0, 4.0);
        let norm_v1 = v1.normalize();
        assert!((norm_v1.length() - 1.0).abs() < EPSILON);
        assert!((norm_v1.x - 0.6).abs() < EPSILON);
        assert!((norm_v1.y - 0.8).abs() < EPSILON);

        let v_zero = Vec2f32::ZERO;
        assert_eq!(v_zero.normalize(), Vec2f32::ZERO);
    }

    #[test]
    fn test_vec2_try_normalize() {
        let v1 = Vec2f32::new(3.0, 4.0);
        let norm_v1_opt = v1.try_normalize();
        assert!(norm_v1_opt.is_some());
        let norm_v1 = norm_v1_opt.unwrap();
        assert!((norm_v1.length() - 1.0).abs() < EPSILON);
        assert!((norm_v1.x - 0.6).abs() < EPSILON);
        assert!((norm_v1.y - 0.8).abs() < EPSILON);

        let v_zero = Vec2f32::ZERO;
        assert!(v_zero.try_normalize().is_none());
    }

    #[test]
    fn test_vec2_reflect() {
        // Reflect along y-axis
        let v1 = Vec2f32::new(1.0, 1.0);
        let n1 = Vec2f32::new(0.0, -1.0); // Normal pointing down
        let r1 = v1.reflect(n1);
        assert_eq!(r1, Vec2f32::new(1.0, -1.0));

        // Reflect along x-axis
        let v2 = Vec2f32::new(1.0, 1.0);
        let n2 = Vec2f32::new(-1.0, 0.0); // Normal pointing left
        let r2 = v2.reflect(n2);
        assert_eq!(r2, Vec2f32::new(-1.0, 1.0));

        // Reflect at a 45 degree angle
        let v3 = Vec2f32::new(1.0, 0.0); // Coming in along x-axis
        let n3 = Vec2f32::new(-1.0, -1.0).normalize(); // Normal pointing down-left
        let r3 = v3.reflect(n3);
        // Expected: (0.0, -1.0) - reflected vector should go down along y-axis
        assert!((r3.x - 0.0).abs() < EPSILON);
        assert!((r3.y - (-1.0)).abs() < EPSILON);

        // Incident vector is parallel to normal (points away)
        let v4 = Vec2f32::new(0.0, 1.0);
        let n4 = Vec2f32::new(0.0, 1.0);
        let r4 = v4.reflect(n4);
        assert_eq!(r4, Vec2f32::new(0.0, -1.0));

        // Incident vector is anti-parallel to normal (points into surface)
        let v5 = Vec2f32::new(0.0, -1.0);
        let n5 = Vec2f32::new(0.0, 1.0);
        let r5 = v5.reflect(n5);
        assert_eq!(r5, Vec2f32::new(0.0, 1.0));
    }

    #[test]
    fn test_vec2_refract() {
        // Case 1: Standard refraction (e.g., air to glass)
        // Incident I = (0.5, -sqrt(3)/2), Normal N = (0,1), eta = 2/3
        // Expected R based on C++ gb_math formula: (1/3, (-3*sqrt(3) + 2*sqrt(6))/9 )
        let i1 = Vec2f32::new(0.5, -(3.0_f32.sqrt()) / 2.0);
        let n1 = Vec2f32::new(0.0, 1.0);
        let eta1 = 2.0 / 3.0;
        let r1 = i1.refract(n1, eta1);
        let expected_r1_x = 1.0 / 3.0;
        let expected_r1_y = (-3.0 * 3.0_f32.sqrt() + 2.0 * 6.0_f32.sqrt()) / 9.0;
        assert!(
            (r1.x - expected_r1_x).abs() < EPSILON,
            "Case 1: R.x mismatch. Got {}, expected {}",
            r1.x,
            expected_r1_x
        );
        assert!(
            (r1.y - expected_r1_y).abs() < EPSILON,
            "Case 1: R.y mismatch. Got {}, expected {}",
            r1.y,
            expected_r1_y
        );

        // Case 2: Total Internal Reflection (TIR) (e.g., glass to air, steep angle)
        // Incident I = (sqrt(3)/2, -0.5), Normal N = (0,1), eta = 1.5
        // Expected R = Vec2f32::ZERO (k = -0.6875)
        let i2 = Vec2f32::new((3.0_f32.sqrt()) / 2.0, -0.5);
        let n2 = Vec2f32::new(0.0, 1.0);
        let eta2 = 1.5;
        let r2 = i2.refract(n2, eta2);
        assert!(
            (r2.x - Vec2f32::ZERO.x).abs() < EPSILON,
            "Case 2: R.x mismatch (TIR)"
        );
        assert!(
            (r2.y - Vec2f32::ZERO.y).abs() < EPSILON,
            "Case 2: R.y mismatch (TIR)"
        );

        // Case 3: Normal Incidence (incident vector anti-parallel to normal)
        // Incident I = (0,-1), Normal N = (0,1), eta = 2/3
        // Expected R = Vec2f32::ZERO (using C++ formula: dotNI = -1, k = 1, N_coeff = -2/3, R = (0,-2/3) - (0,-2/3) = (0,0))
        let i3 = Vec2f32::new(0.0, -1.0);
        let n3 = Vec2f32::new(0.0, 1.0);
        let eta3 = 2.0 / 3.0;
        let r3 = i3.refract(n3, eta3);
        let expected_r3 = Vec2f32::ZERO; // Corrected based on C++ formula
        assert!(
            (r3.x - expected_r3.x).abs() < EPSILON,
            "Case 3: R.x mismatch (Normal Incidence). Got {}, expected {}",
            r3.x,
            expected_r3.x
        );
        assert!(
            (r3.y - expected_r3.y).abs() < EPSILON,
            "Case 3: R.y mismatch (Normal Incidence). Got {}, expected {}",
            r3.y,
            expected_r3.y
        );

        // Case 4a: Grazing angle, no TIR
        // Incident I = (1,0), Normal N = (0,1), eta = 0.5
        // Expected R = (0.5, 0.0) (using C++ formula: dotNI = 0, k=0.75, N_coeff = 0, R = (0.5,0) - (0) = (0.5,0) )
        let i4a = Vec2f32::new(1.0, 0.0);
        let n4a = Vec2f32::new(0.0, 1.0);
        let eta4a = 0.5;
        let r4a = i4a.refract(n4a, eta4a);
        let expected_r4a = Vec2f32::new(0.5, 0.0); // Corrected
        assert!(
            (r4a.x - expected_r4a.x).abs() < EPSILON,
            "Case 4a: R.x mismatch (Grazing, no TIR). Got {}, expected {}",
            r4a.x,
            expected_r4a.x
        );
        assert!(
            (r4a.y - expected_r4a.y).abs() < EPSILON,
            "Case 4a: R.y mismatch (Grazing, no TIR). Got {}, expected {}",
            r4a.y,
            expected_r4a.y
        );

        // Case 4b: Grazing angle, with TIR
        let i4b = Vec2f32::new(1.0, 0.0);
        let n4b = Vec2f32::new(0.0, 1.0);
        let eta4b = 1.5;
        let r4b = i4b.refract(n4b, eta4b);
        assert!(
            (r4b.x - Vec2f32::ZERO.x).abs() < EPSILON,
            "Case 4b: R.x mismatch (Grazing, TIR)"
        );
        assert!(
            (r4b.y - Vec2f32::ZERO.y).abs() < EPSILON,
            "Case 4b: R.y mismatch (Grazing, TIR)"
        );
    }

    #[test]
    fn test_vec2_try_refract() {
        // Case 1: Standard refraction (e.g., air to glass)
        let i1 = Vec2f32::new(0.5, -(3.0_f32.sqrt()) / 2.0);
        let n1 = Vec2f32::new(0.0, 1.0);
        let eta1 = 2.0 / 3.0;
        let r1_opt = i1.try_refract(n1, eta1);
        assert!(r1_opt.is_some(), "Case 1: Expected Some");
        if let Some(r1) = r1_opt {
            let expected_r1_x = 1.0 / 3.0;
            let expected_r1_y = (-3.0 * 3.0_f32.sqrt() + 2.0 * 6.0_f32.sqrt()) / 9.0;
            let expected_r1 = Vec2f32::new(expected_r1_x, expected_r1_y);
            assert!(
                (r1.x - expected_r1.x).abs() < EPSILON,
                "Case 1: R.x mismatch. Got {}, expected {}",
                r1.x,
                expected_r1.x
            );
            assert!(
                (r1.y - expected_r1.y).abs() < EPSILON,
                "Case 1: R.y mismatch. Got {}, expected {}",
                r1.y,
                expected_r1.y
            );
        }

        // Case 2: Total Internal Reflection (TIR) (e.g., glass to air, steep angle)
        let i2 = Vec2f32::new((3.0_f32.sqrt()) / 2.0, -0.5);
        let n2 = Vec2f32::new(0.0, 1.0);
        let eta2 = 1.5;
        let r2_opt = i2.try_refract(n2, eta2);
        assert!(r2_opt.is_none(), "Case 2: Expected None (TIR)");

        // Case 3: Normal Incidence
        let i3 = Vec2f32::new(0.0, -1.0);
        let n3 = Vec2f32::new(0.0, 1.0);
        let eta3 = 2.0 / 3.0;
        let r3_opt = i3.try_refract(n3, eta3);
        assert!(r3_opt.is_some(), "Case 3: Expected Some (Normal Incidence)");
        if let Some(r3) = r3_opt {
            let expected_r3 = Vec2f32::ZERO; // Corrected
            assert!(
                (r3.x - expected_r3.x).abs() < EPSILON,
                "Case 3: R.x mismatch. Got {}",
                r3.x
            );
            assert!(
                (r3.y - expected_r3.y).abs() < EPSILON,
                "Case 3: R.y mismatch. Got {}",
                r3.y
            );
        }

        // Case 4a: Grazing angle, no TIR
        let i4a = Vec2f32::new(1.0, 0.0);
        let n4a = Vec2f32::new(0.0, 1.0);
        let eta4a = 0.5;
        let r4a_opt = i4a.try_refract(n4a, eta4a);
        assert!(
            r4a_opt.is_some(),
            "Case 4a: Expected Some (Grazing, no TIR)"
        );
        if let Some(r4a) = r4a_opt {
            let expected_r4a = Vec2f32::new(0.5, 0.0); // Corrected
            assert!(
                (r4a.x - expected_r4a.x).abs() < EPSILON,
                "Case 4a: R.x mismatch. Got {}",
                r4a.x
            );
            assert!(
                (r4a.y - expected_r4a.y).abs() < EPSILON,
                "Case 4a: R.y mismatch. Got {}",
                r4a.y
            );
        }

        // Case 4b: Grazing angle, with TIR
        let i4b = Vec2f32::new(1.0, 0.0);
        let n4b = Vec2f32::new(0.0, 1.0);
        let eta4b = 1.5;
        let r4b_opt = i4b.try_refract(n4b, eta4b);
        assert!(r4b_opt.is_none(), "Case 4b: Expected None (Grazing, TIR)");
    }

    #[test]
    fn test_vec2_yx() {
        let v = Vec2f32::new(1.0, 2.0);
        let swapped = v.yx();
        assert_eq!(swapped, Vec2f32::new(2.0, 1.0));
    }

    #[test]
    fn test_vec2_from_array() {
        let arr = [1.0, 2.0];
        let v = Vec2f32::from_array(arr);
        assert_eq!(v, Vec2f32::new(1.0, 2.0));
    }

    #[test]
    fn test_vec2_reflect_incident() {
        // Test case similar to reflect, but using reflect_incident
        let i = Vec2f32::new(1.0, 1.0);
        let n = Vec2f32::new(0.0, -1.0); // Normal pointing down
        let r = Vec2f32::reflect_incident(i, n);
        assert_eq!(r, Vec2f32::new(1.0, -1.0));

        let i2 = Vec2f32::new(1.0, 0.0);
        let n2 = Vec2f32::new(-1.0, -1.0).normalize();
        let r2 = Vec2f32::reflect_incident(i2, n2);
        assert!((r2.x - 0.0).abs() < EPSILON);
        assert!((r2.y - (-1.0)).abs() < EPSILON);
    }

    #[test]
    fn test_vec2_refract_gl() {
        // Using GLSL definition: i * eta - n * (eta * dot(i, n) + sqrt(k))
        // Assuming i and n are normalized
        // Case 1: No TIR (Air to Glass-like, n1=1, n2=1.5, eta = 1/1.5 = 0.666)
        let i1 = Vec2f32::new(0.70710678, -0.70710678).normalize(); // 45 deg incident
        let n1 = Vec2f32::new(0.0, 1.0); // Normal along Y
        let eta1 = 1.0 / 1.5; // Ratio of indices (n1/n2)
        let r1 = Vec2f32::refract_gl(i1, n1, eta1);
        // Expected: dot_ni = -0.70710678 * 1.5 = -0.47140452
        // k = 1.0 - eta1*eta1*(1.0 - dot_ni*dot_ni)
        // k = 1.0 - (0.666^2) * (1.0 - (-0.70710678)^2) = 1.0 - 0.444 * (1.0 - 0.5) = 1.0 - 0.444 * 0.5 = 1.0 - 0.222 = 0.778
        // sqrt(k) = 0.882043
        // r1 = i1 * eta1 - n1 * (eta1 * dot_ni + sqrt(k))
        // r1.x = 0.70710678 * 0.666 = 0.470903
        // r1.y = -0.70710678 * 0.666 - 1.0 * (0.666 * (-0.70710678) + 0.882043)
        //      = -0.470903 - ( -0.470903 + 0.882043)
        //      = -0.470903 - 0.411139 = -0.882043
        // This is approximately (0.4709, -0.8820)
        // Let's use a less direct check for now by verifying the angle or comparing with a known good calculation
        // For eta = 2/3, i=(sin(pi/4), -cos(pi/4)), n=(0,1) => refraction angle arcsin(eta*sin(pi/4)) = arcsin( (2/3) * (1/sqrt(2))) = arcsin(sqrt(2)/3) ~ 28.12 deg
        // Resulting vector (sin(28.12deg), -cos(28.12deg)) = (0.4714, -0.8819)
        assert!(
            (r1.x - 0.47140452).abs() < 1e-5,
            "GL refract R1.x: got {}, expected {}",
            r1.x,
            0.47140452
        );
        assert!(
            (r1.y - (-0.88192126)).abs() < 1e-5,
            "GL refract R1.y: got {}, expected {}",
            r1.y,
            -0.88192126
        );

        // Case 2: Total Internal Reflection (Glass to Air-like, n1=1.5, n2=1, eta = 1.5/1 = 1.5)
        let i2 = Vec2f32::new(0.70710678, -0.70710678).normalize(); // 45 deg incident
        let n2 = Vec2f32::new(0.0, 1.0); // Normal along Y
        let eta2 = 1.5; // Ratio of indices (n1/n2)
        let r2 = Vec2f32::refract_gl(i2, n2, eta2);
        // Critical angle sin_crit = 1/eta = 1/1.5 = 0.666. Angle is 41.81 deg. Our incident angle 45deg > 41.81deg => TIR
        assert_eq!(r2, Vec2f32::ZERO, "GL refract R2 (TIR)");

        // Case 3: Normal incidence
        let i3 = Vec2f32::new(0.0, -1.0);
        let n3 = Vec2f32::new(0.0, 1.0);
        let eta3 = 2.0 / 3.0;
        let r3 = Vec2f32::refract_gl(i3, n3, eta3);
        // dot_ni = -1. k = 1 - eta*eta*(1-1) = 1. sqrt(k) = 1
        // r = i*eta - n*(eta*(-1) + 1) = i*eta - n*(-eta+1)
        // r.x = 0
        // r.y = (-1)*eta - 1*(-eta+1) = -eta + eta - 1 = -1
        // Expected: (0.0, -1.0) * eta / eta, essentially (0, -1) ?  The formula implies this is wrong.
        // GLSL Refract should give (0, -1) for normal incidence if i and n are normalized.
        // It means the vector passes through without changing direction, only speed if eta != 1.
        // But the function returns the direction vector.
        // The returned vector must have the same direction as i: (0,-1).
        // Let's trace: i=(0,-1), n=(0,1), eta. dot_ni = -1. k = 1 - eta*eta*(1 - (-1)*(-1)) = 1. sqrt(k) = 1.
        // r = i*eta - n*(eta*(-1) + 1) = (0, -eta) - (0,1)*(-eta+1) = (0, -eta) - (0, -eta+1) = (0, -eta - (-eta+1)) = (0,-1)
        assert!(
            (r3.x - 0.0).abs() < EPSILON,
            "GL refract R3.x: got {}, expected 0.0",
            r3.x
        );
        assert!(
            (r3.y - (-1.0)).abs() < EPSILON,
            "GL refract R3.y: got {}, expected -1.0",
            r3.y
        );
    }

    #[test]
    fn test_vec2_try_refract_gl() {
        // Case 1: No TIR
        let i1 = Vec2f32::new(0.70710678, -0.70710678).normalize();
        let n1 = Vec2f32::new(0.0, 1.0);
        let eta1 = 1.0 / 1.5;
        let r1_opt = Vec2f32::try_refract_gl(i1, n1, eta1);
        assert!(r1_opt.is_some(), "Try GL refract R1: Expected Some");
        if let Some(r1) = r1_opt {
            assert!((r1.x - 0.47140452).abs() < 1e-5);
            assert!((r1.y - (-0.88192126)).abs() < 1e-5);
        }

        // Case 2: Total Internal Reflection
        let i2 = Vec2f32::new(0.70710678, -0.70710678).normalize();
        let n2 = Vec2f32::new(0.0, 1.0);
        let eta2 = 1.5;
        let r2_opt = Vec2f32::try_refract_gl(i2, n2, eta2);
        assert!(r2_opt.is_none(), "Try GL refract R2 (TIR): Expected None");
    }

    #[test]
    fn test_vec2_aspect_ratio() {
        let v1 = Vec2f32::new(16.0, 9.0);
        assert!((v1.aspect_ratio() - 16.0 / 9.0).abs() < EPSILON);

        let v2 = Vec2f32::new(4.0, 0.0);
        assert!((v2.aspect_ratio() - 0.0).abs() < EPSILON); // y is ~0

        let v3 = Vec2f32::new(0.0, 5.0);
        assert!((v3.aspect_ratio() - 0.0).abs() < EPSILON);

        let v4 = Vec2f32::new(10.0, 0.00001); // y very close to zero
        assert!((v4.aspect_ratio() - 0.0).abs() < EPSILON);

        let v5 = Vec2f32::new(10.0, -0.00001); // y very close to zero (negative)
        assert!((v5.aspect_ratio() - 0.0).abs() < EPSILON);
    }

    #[test]
    fn test_vec2_lerp() {
        let a = Vec2f32::new(1.0, 2.0);
        let b = Vec2f32::new(5.0, 6.0);

        let r1 = a.lerp(b, 0.0); // Should be a
        assert_eq!(r1, a);

        let r2 = a.lerp(b, 1.0); // Should be b
        assert_eq!(r2, b);

        let r3 = a.lerp(b, 0.5); // Midpoint
        assert_eq!(r3, Vec2f32::new(3.0, 4.0));

        let r4 = a.lerp(b, 0.25);
        assert_eq!(r4, Vec2f32::new(2.0, 3.0));
    }

    #[test]
    fn test_vec2_angle_between() {
        let v1 = Vec2f32::new(1.0, 0.0);
        let v2 = Vec2f32::new(0.0, 1.0);
        assert!((v1.angle_between(v2) - std::f32::consts::FRAC_PI_2).abs() < EPSILON);
        let v3 = Vec2f32::new(1.0, 0.0);
        let v4 = Vec2f32::new(1.0, 0.0);
        assert!((v3.angle_between(v4)).abs() < EPSILON);
    }

    #[test]
    fn test_vec2_project_onto() {
        let v = Vec2f32::new(2.0, 2.0);
        let onto = Vec2f32::new(1.0, 0.0);
        let proj = v.project_onto(onto);
        assert!((proj.x - 2.0).abs() < EPSILON);
        assert!((proj.y - 0.0).abs() < EPSILON);
    }

    #[test]
    fn test_vec2_perp() {
        let v = Vec2f32::new(1.0, 2.0);
        let perp = v.perp();
        assert_eq!(perp, Vec2f32::new(-2.0, 1.0));
    }

    #[test]
    fn test_vec2_normalize_or_zero() {
        let v = Vec2f32::new(3.0, 4.0);
        let n = v.normalize_or_zero();
        assert!((n.length() - 1.0).abs() < EPSILON);
        let zero = Vec2f32::ZERO;
        assert_eq!(zero.normalize_or_zero(), Vec2f32::ZERO);
    }

    #[test]
    fn test_vec2_distance() {
        let v1 = Vec2f32::new(0.0, 0.0);
        let v2 = Vec2f32::new(3.0, 4.0);
        assert!((v1.distance(v2) - 5.0).abs() < EPSILON);
    }

    #[test]
    fn test_vec2_clamp() {
        let v = Vec2f32::new(5.0, -2.0);
        let min = Vec2f32::new(0.0, 0.0);
        let max = Vec2f32::new(4.0, 4.0);
        let clamped = v.clamp(min, max);
        assert_eq!(clamped, Vec2f32::new(4.0, 0.0));
    }

    #[test]
    fn test_vec2_min_max() {
        let v1 = Vec2f32::new(1.0, 5.0);
        let v2 = Vec2f32::new(3.0, 2.0);
        assert_eq!(v1.min(v2), Vec2f32::new(1.0, 2.0));
        assert_eq!(v1.max(v2), Vec2f32::new(3.0, 5.0));
    }

    #[test]
    fn test_vec2_is_nan_is_finite() {
        let v = Vec2f32::new(f32::NAN, 1.0);
        assert!(v.is_nan());
        let v2 = Vec2f32::new(1.0, 2.0);
        assert!(v2.is_finite());
        let v3 = Vec2f32::new(f32::INFINITY, 1.0);
        assert!(!v3.is_finite());
    }

    #[test]
    fn test_angle_types_conversion() {
        let deg = Degrees(180.0);
        let rad = deg.to_radians();
        assert!((rad.0 - std::f32::consts::PI).abs() < EPSILON);
        let deg2 = rad.to_degrees();
        assert!((deg2.0 - 180.0).abs() < EPSILON);
        // From/Into
        let rad2: Radians = Degrees(90.0).into();
        assert!((rad2.0 - std::f32::consts::FRAC_PI_2).abs() < EPSILON);
        let deg3: Degrees = Radians(std::f32::consts::PI).into();
        assert!((deg3.0 - 180.0).abs() < EPSILON);
    }

    #[test]
    fn test_vec2_rotate() {
        let v = Vec2::<(), ()>::new(1.0, 0.0);
        let rotated = v.rotate(Radians(std::f32::consts::FRAC_PI_2));
        assert!((rotated.x).abs() < EPSILON);
        assert!((rotated.y - 1.0).abs() < EPSILON);
        // Rotate by 180 degrees
        let rotated2 = v.rotate(Degrees(180.0).to_radians());
        assert!((rotated2.x + 1.0).abs() < EPSILON);
        assert!(rotated2.y.abs() < EPSILON);
    }
}
// --- Compile-time (const) tests/examples for Vec2 ---
const _CONST_V0: Vec2f32 = Vec2f32::new(1.0, 2.0);
const _CONST_V1: Vec2f32 = Vec2f32::new(3.0, 4.0);
// const CONST_ADD: Vec2f32 = Vec2f32::new(1.0, 2.0) + Vec2f32::new(3.0, 4.0); // Not allowed on stable
const _CONST_DOT: f32 = Vec2f32::new(1.0, 2.0).dot(Vec2f32::new(3.0, 4.0));
const _CONST_CROSS: f32 = Vec2f32::new(1.0, 2.0).cross(Vec2f32::new(3.0, 4.0));
const _CONST_YX: Vec2f32 = Vec2f32::new(1.0, 2.0).yx();
const _CONST_FROM_ARRAY: Vec2f32 = Vec2f32::from_array([5.0, 6.0]);
const _CONST_LEN_SQ: f32 = Vec2f32::new(3.0, 4.0).length_squared();

const _: () = {
    // Compile-time assertions for const-everything
    assert!(_CONST_V0.x == 1.0 && _CONST_V0.y == 2.0);
    assert!(_CONST_V1.x == 3.0 && _CONST_V1.y == 4.0);
    // assert!(CONST_ADD.x == 4.0 && CONST_ADD.y == 6.0); // Not allowed on stable
    assert!(_CONST_DOT == 11.0);
    assert!(_CONST_CROSS == -2.0);
    assert!(_CONST_YX.x == 2.0 && _CONST_YX.y == 1.0);
    assert!(_CONST_FROM_ARRAY.x == 5.0 && _CONST_FROM_ARRAY.y == 6.0);
    assert!(_CONST_LEN_SQ == 25.0);
};

// --- Compile-time (const) tests/examples for Vec2<Unit> type-level units ---
const _CONST_METERS: Vec2Meters = Vec2Meters::new(1.0, 2.0);
const _CONST_PIXELS: Vec2Pixels = Vec2Pixels::new(10.0, 20.0);

const fn _make_vec2_meters() -> Vec2Meters {
    Vec2Meters::new(3.0, 4.0)
}
const fn _make_vec2_pixels() -> Vec2Pixels {
    Vec2Pixels::new(30.0, 40.0)
}

const _CONST_METERS2: Vec2Meters = _make_vec2_meters();
const _CONST_PIXELS2: Vec2Pixels = _make_vec2_pixels();

// Compile-time type safety: the following line would fail to compile if uncommented
// const _FAIL: Vec2Meters = Vec2Pixels::new(1.0, 2.0); // error: mismatched types

// Runtime test for conversion
#[test]
fn test_vec2_meters_to_pixels() {
    let meters: Vec2<Meters, ()> = Vec2::new(2.0, 3.0);
    let pixels = meters.to_pixels(100.0);
    assert_eq!(pixels, Vec2::<Pixels, ()>::new(200.0, 300.0));
}

// --- Compile-time (const) tests/examples for Vec2<Unit, Space> phantom coordinate spaces ---
const _CONST_WORLD: Vec2<(), World> = Vec2::new(1.0, 2.0);
const _CONST_LOCAL: Vec2<(), Local> = Vec2::new(3.0, 4.0);
const _CONST_SCREEN: Vec2<(), Screen> = Vec2::new(5.0, 6.0);

// Compile-time type safety: the following line would fail to compile if uncommented
// const FAIL: Vec2World = Vec2Local::new(1.0, 2.0); // error: mismatched types
// const FAIL2: Vec2Local = Vec2World::new(1.0, 2.0);
// const FAIL3: Vec2World = Vec2World::new(1.0, 2.0) + Vec2Local::new(3.0, 4.0);
// const FAIL4: Vec2World = _CONST_WORLD + _CONST_LOCAL;

#[test]
fn test_vec2_spaces_addition() {
    let world = Vec2World::new(1.0, 2.0);
    let local = Vec2Local::new(3.0, 4.0);
    let screen = Vec2Screen::new(5.0, 6.0);
    assert_eq!(world + _CONST_WORLD, Vec2World::new(2.0, 4.0));
    assert_eq!(local + _CONST_LOCAL, Vec2Local::new(6.0, 8.0));
    assert_eq!(screen + _CONST_SCREEN, Vec2Screen::new(10.0, 12.0));
}