-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangle.rs
More file actions
837 lines (751 loc) · 22 KB
/
angle.rs
File metadata and controls
837 lines (751 loc) · 22 KB
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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
//! Angular quantities, including scalar angles and angular vectors.
use core::{
f32::consts::{PI, TAU},
fmt::{self, Debug, Display},
marker::PhantomData,
ops::{Add, Div, Mul, Neg, Rem, Sub},
};
use crate::math::{Affine, ApproxEq, Linear, Vector, vary::ZDiv};
#[cfg(feature = "fp")]
use crate::math::{Vec2, Vec3, float::f32, vec2, vec3};
//
// Types
//
/// A scalar angular quantity.
///
/// Prevents confusion between degrees and radians by requiring the use of
/// one of the named constructors to create an `Angle`, as well as one of
/// the named getter methods to obtain the angle as a raw `f32` value.
#[derive(Copy, Clone, Default, PartialEq)]
#[repr(transparent)]
pub struct Angle(f32);
/// Tag type for a polar coordinate space
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub struct Polar<B>(PhantomData<B>);
/// Tag type for a spherical coordinate space.
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub struct Spherical<B>(PhantomData<B>);
/// A polar coordinate vector, with radius and azimuth components.
pub type PolarVec<B = ()> = Vector<[f32; 2], Polar<B>>;
/// A spherical coordinate vector, with radius, azimuth, and altitude
/// (elevation) components.
pub type SphericalVec<B = ()> = Vector<[f32; 3], Spherical<B>>;
//
// Free fns and consts
//
/// Returns an angle of `a` radians.
pub const fn rads(a: f32) -> Angle {
Angle(a)
}
/// Returns an angle of `a` degrees.
pub const fn degs(a: f32) -> Angle {
Angle(a * RADS_PER_DEG)
}
/// Returns an angle of `a` turns.
pub const fn turns(a: f32) -> Angle {
Angle(a * RADS_PER_TURN)
}
/// Returns the arcsine of `x` as an `Angle`.
///
/// The return value is in the range [-90°, 90°].
///
/// # Examples
/// ```
/// use retrofire_core::assert_approx_eq;
/// use retrofire_core::math::{degs, asin};
///
/// assert_approx_eq!(asin(1.0), degs(90.0));
/// assert_approx_eq!(asin(-1.0), degs(-90.0));
/// ```
/// # Panics
/// If `x` is outside the range [-1.0, 1.0].
#[cfg(feature = "fp")]
pub fn asin(x: f32) -> Angle {
assert!(-1.0 <= x && x <= 1.0);
Angle(f32::asin(x))
}
/// Returns the arccosine of `x` as an `Angle`.
///
/// The return value is in the range [-90°, 90°].
///
/// # Examples
/// ```
/// use retrofire_core::assert_approx_eq;
/// use retrofire_core::math::{acos, degs};
///
/// assert_approx_eq!(acos(1.0), degs(0.0));
/// ```
/// # Panics
/// If `x` is outside the range [-1.0, 1.0].
#[cfg(feature = "fp")]
pub fn acos(x: f32) -> Angle {
Angle(f32::acos(x))
}
/// Returns the four-quadrant arctangent of `y` and `x` as an `Angle`.
///
/// The returned angle is equal to [`y.atan2(x)`][f32::atan2].
///
/// # Examples
/// ```
/// use retrofire_core::math::{atan2, degs};
///
/// assert_eq!(atan2(0.0, 1.0), degs(0.0));
/// assert_eq!(atan2(2.0, 2.0), degs(45.0));
/// assert_eq!(atan2(3.0, 0.0), degs(90.0));
/// ```
#[cfg(feature = "fp")]
pub fn atan2(y: f32, x: f32) -> Angle {
Angle(f32::atan2(y, x))
}
/// Returns a polar coordinate vector with azimuth `az` and radius `r`.
pub const fn polar<B>(r: f32, az: Angle) -> PolarVec<B> {
Vector::new([r, az.to_rads()])
}
/// Returns a spherical coordinate vector with azimuth `az`,
/// altitude `alt`, and radius `r`.
///
/// An altitude of +90° corresponds to straight up and -90° to straight down.
pub const fn spherical<B>(r: f32, az: Angle, alt: Angle) -> SphericalVec<B> {
Vector::new([r, az.to_rads(), alt.to_rads()])
}
const RADS_PER_DEG: f32 = PI / 180.0;
const RADS_PER_TURN: f32 = TAU;
//
// Inherent impls
//
impl Angle {
/// A zero degree angle.
pub const ZERO: Self = Self(0.0);
/// A 90 degree angle.
pub const RIGHT: Self = Self(RADS_PER_TURN / 4.0);
/// A 180 degree angle.
pub const STRAIGHT: Self = Self(RADS_PER_TURN / 2.0);
/// A 360 degree angle.
pub const FULL: Self = Self(RADS_PER_TURN);
/// Returns the value of `self` in radians.
/// # Examples
/// ```
/// use std::f32;
/// use retrofire_core::math::degs;
///
/// assert_eq!(degs(90.0).to_rads(), f32::consts::FRAC_PI_2);
/// ```
pub const fn to_rads(self) -> f32 {
self.0
}
/// Returns the value of `self` in degrees.
/// # Examples
/// ```
/// use retrofire_core::math::turns;
///
/// assert_eq!(turns(2.0).to_degs(), 720.0);
pub fn to_degs(self) -> f32 {
self.0 / RADS_PER_DEG
}
/// Returns the value of `self` in turns.
/// # Examples
/// ```
/// use retrofire_core::math::degs;
///
/// assert_eq!(degs(180.0).to_turns(), 0.5);
/// ```
pub fn to_turns(self) -> f32 {
self.0 / RADS_PER_TURN
}
/// Returns the minimum of `self` and `other`.
pub fn min(self, other: Self) -> Self {
Self(self.0.min(other.0))
}
/// Returns the maximum of `self` and `other`.
pub fn max(self, other: Self) -> Self {
Self(self.0.max(other.0))
}
/// Returns `self` clamped to the range `min..=max`.
///
/// # Examples
/// ```
/// use retrofire_core::math::degs;
///
/// let (min, max) = (degs(0.0), degs(45.0));
///
/// assert_eq!(degs(100.0).clamp(min, max), max);
/// assert_eq!(degs(30.0).clamp(min, max), degs(30.0));
/// assert_eq!(degs(-10.0).clamp(min, max), min);
/// ```
#[must_use]
pub fn clamp(self, min: Self, max: Self) -> Self {
Self(self.0.clamp(min.0, max.0))
}
}
#[cfg(feature = "fp")]
impl Angle {
/// Returns the sine of `self`.
/// # Examples
/// ```
/// use retrofire_core::assert_approx_eq;
/// use retrofire_core::math::degs;
///
/// assert_approx_eq!(degs(30.0).sin(), 0.5)
/// ```
pub fn sin(self) -> f32 {
f32::sin(self.0)
}
/// Returns the cosine of `self`.
/// # Examples
/// ```
/// use retrofire_core::assert_approx_eq;
/// use retrofire_core::math::degs;
///
/// assert_approx_eq!(degs(60.0).cos(), 0.5)
/// ```
pub fn cos(self) -> f32 {
f32::cos(self.0)
}
/// Simultaneously computes the sine and cosine of `self`.
/// # Examples
/// ```
/// use retrofire_core::assert_approx_eq;
/// use retrofire_core::math::degs;
///
/// let (sin, cos) = degs(90.0).sin_cos();
/// assert_approx_eq!(sin, 1.0);
/// assert_approx_eq!(cos, 0.0);
/// ```
pub fn sin_cos(self) -> (f32, f32) {
(self.sin(), self.cos())
}
/// Returns the tangent of `self`.
/// # Examples
/// ```
/// use retrofire_core::math::degs;
/// assert_eq!(degs(45.0).tan(), 1.0)
/// ```
pub fn tan(self) -> f32 {
f32::tan(self.0)
}
/// Returns `self` "wrapped around" to the range `min..max`.
///
/// # Examples
/// ```
/// use retrofire_core::assert_approx_eq;
/// use retrofire_core::math::{degs, turns};
///
/// // 400 (mod 360) = 40
/// assert_approx_eq!(degs(400.0).wrap(turns(0.0), turns(1.0)), degs(40.0))
/// ```
#[must_use]
pub fn wrap(self, min: Self, max: Self) -> Self {
Self(min.0 + f32::rem_euclid(self.0 - min.0, max.0 - min.0))
}
}
impl<B> PolarVec<B> {
/// Returns the radial component of `self`.
#[inline]
pub fn r(&self) -> f32 {
self.0[0]
}
/// Returns the azimuthal component of `self`.
#[inline]
pub fn az(&self) -> Angle {
rads(self.0[1])
}
/// Returns `self` converted to the equivalent Cartesian 2-vector.
///
/// Let the components of `self` be `(r, az)`. Then the result `(x, y)`
/// equals `(r * cos(az), r * sin(az))`.
///
/// ```text
/// +y
/// ^ ^
/// | +r /
/// | /
/// | /_ +az
/// | / \
/// +----------> +x
/// ```
///
/// # Examples
/// ```
/// use retrofire_core::assert_approx_eq;
/// use retrofire_core::math::{vec2, polar, degs};
///
/// let vec2 = vec2::<f32, ()>;
///
/// assert_approx_eq!(polar(2.0, degs(0.0)).to_cart(), vec2(2.0, 0.0));
/// assert_approx_eq!(polar(3.0, degs(90.0)).to_cart(), vec2(0.0, 3.0));
/// assert_approx_eq!(polar(4.0, degs(-180.0)).to_cart(), vec2(-4.0, 0.0));
///
/// ```
#[cfg(feature = "fp")]
pub fn to_cart(&self) -> Vec2<B> {
let (y, x) = self.az().sin_cos();
vec2(x, y) * self.r()
}
}
impl<B> SphericalVec<B> {
/// Returns the radial component of `self`.
#[inline]
pub fn r(&self) -> f32 {
self.0[0]
}
/// Returns the azimuthal component of `self`.
#[inline]
pub fn az(&self) -> Angle {
rads(self.0[1])
}
/// Returns the altitude (elevation) component of `self`.
#[inline]
pub fn alt(&self) -> Angle {
rads(self.0[2])
}
/// Returns `self` converted to the equivalent Cartesian 3-vector.
///
/// # Examples
/// TODO examples
#[cfg(feature = "fp")]
pub fn to_cart(&self) -> Vec3<B> {
let (sin_alt, cos_alt) = self.alt().sin_cos();
let (sin_az, cos_az) = self.az().sin_cos();
let x = cos_az * cos_alt;
let z = sin_az * cos_alt;
let y = sin_alt;
self.r() * vec3(x, y, z)
}
}
#[cfg(feature = "fp")]
impl<B> Vec2<B> {
/// Returns `self` converted into the equivalent polar coordinate vector.
///
/// The `r` component of the result equals `self.len()`.
///
/// The `az` component equals the angle between the vector and the x-axis
/// in the range (-180°, 180°] such that positive `y` maps to positive `az`.
/// ```text
/// +y
/// ^ ^
/// | /
/// | /_ +az
/// | / \
/// +----------> +x
/// ```
///
/// # Examples
/// ```
/// use retrofire_core::assert_approx_eq;
/// use retrofire_core::math::{vec2, degs};
///
/// let vec2 = vec2::<f32, ()>;
///
/// // A non-negative x and zero y maps to zero azimuth
/// assert_eq!(vec2(0.0, 0.0).to_polar().az(), degs(0.0));
/// assert_eq!(vec2(1.0, 0.0).to_polar().az(), degs(0.0));
///
/// // A zero x and positive y maps to right angle azimuth
/// assert_eq!(vec2(0.0, 1.0).to_polar().az(), degs(90.0));
///
/// // A zero x and negative y maps to negative right angle azimuth
/// assert_eq!(vec2(0.0, -1.0).to_polar().az(), degs(-90.0));
///
/// // A negative x and zero y maps to straight angle azimuth
/// assert_approx_eq!(vec2(-1.0, 0.0).to_polar().az(), degs(180.0));
/// ```
pub fn to_polar(&self) -> PolarVec<B> {
let r = self.len();
let az = atan2(self.y(), self.x());
polar(r, az)
}
}
#[cfg(feature = "fp")]
impl<B> Vec3<B> {
/// Converts `self` into the equivalent spherical coordinate vector.
///
/// The `r` component of the result equals `self.len()`.
///
/// The `az` component is the angle between `self` and the xy-plane in the
/// range (-180°, 180°] such that positive `z` maps to positive `az`.
///
/// The `alt` component is the angle between `self` and the xz-plane in the
/// range [-90°, 90°] such that positive `y` maps to positive `alt`.
///
/// # Examples
/// ```
/// use retrofire_core::math::{vec3, spherical, degs};
///
/// // The positive x-axis lies at zero azimuth and altitude
/// assert_eq!(
/// vec3(2.0, 0.0, 0.0).to_spherical(),
/// spherical::<()>(2.0, degs(0.0), degs(0.0))
/// );
/// // The positive y-axis lies at 90° altitude
/// assert_eq!(
/// vec3(0.0, 2.0, 0.0).to_spherical(),
/// spherical::<()>(2.0, degs(0.0), degs(90.0))
/// );
/// // The positive z axis lies at 90° azimuth
/// assert_eq!(
/// vec3(0.0, 0.0, 2.0).to_spherical(),
/// spherical::<()>(2.0, degs(90.0), degs(0.0))
/// );
/// ```
pub fn to_spherical(&self) -> SphericalVec<B> {
let [x, y, z] = self.0;
let az = atan2(z, x);
let alt = atan2(y, f32::sqrt(x * x + z * z));
let r = self.len();
spherical(r, az, alt)
}
}
//
// Local trait impls
//
impl ApproxEq for Angle {
// TODO Should this account for wraparound?
fn approx_eq_eps(&self, other: &Self, eps: &Self) -> bool {
self.0.approx_eq_eps(&other.0, &eps.0)
}
fn relative_epsilon() -> Self {
Self(f32::relative_epsilon())
}
}
impl Affine for Angle {
type Space = ();
type Diff = Self;
const DIM: usize = 1;
#[inline]
fn add(&self, other: &Self) -> Self {
*self + *other
}
#[inline]
fn sub(&self, other: &Self) -> Self {
*self - *other
}
}
impl Linear for Angle {
type Scalar = f32;
#[inline]
fn zero() -> Self {
Self::ZERO
}
#[inline]
fn mul(&self, scalar: f32) -> Self {
*self * scalar
}
}
impl ZDiv for Angle {}
//
// Foreign trait impls
//
impl<B> Default for SphericalVec<B> {
fn default() -> Self {
Self::new([1.0, 0.0, 0.0])
}
}
impl<B> Default for PolarVec<B> {
fn default() -> Self {
Self::new([1.0, 0.0])
}
}
impl Display for Angle {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (val, unit) = if f.alternate() {
(self.to_rads() / PI, "𝜋 rad")
} else {
(self.to_degs(), "°")
};
Display::fmt(&val, f)?;
f.write_str(unit)
}
}
impl Debug for Angle {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Angle(")?;
Display::fmt(self, f)?;
f.write_str(")")
}
}
impl Add for Angle {
type Output = Self;
fn add(self, rhs: Self) -> Self {
Self(self.0 + rhs.0)
}
}
impl Sub for Angle {
type Output = Self;
fn sub(self, rhs: Self) -> Self {
Self(self.0 - rhs.0)
}
}
impl Neg for Angle {
type Output = Self;
fn neg(self) -> Self {
Self(-self.0)
}
}
impl Mul<f32> for Angle {
type Output = Self;
fn mul(self, rhs: f32) -> Self {
Self(self.0 * rhs)
}
}
impl Div<f32> for Angle {
type Output = Self;
fn div(self, rhs: f32) -> Self {
Self(self.0 / rhs)
}
}
impl Rem for Angle {
type Output = Self;
fn rem(self, rhs: Self) -> Self {
Self(self.0 % rhs.0)
}
}
impl Mul<Angle> for f32 {
type Output = Angle;
fn mul(self, rhs: Angle) -> Self::Output {
rhs * self
}
}
#[cfg(feature = "fp")]
impl<B> From<PolarVec<B>> for Vec2<B> {
/// Converts a polar vector into the equivalent Cartesian vector.
///
/// See [PolarVec::to_cart] for more information.
fn from(p: PolarVec<B>) -> Self {
p.to_cart()
}
}
#[cfg(feature = "fp")]
impl<B> From<Vec2<B>> for PolarVec<B> {
/// Converts a Cartesian 2-vector into the equivalent polar vector.
///
/// See [Vec2::to_polar] for more information.
fn from(v: Vec2<B>) -> Self {
v.to_polar()
}
}
#[cfg(feature = "fp")]
impl<B> From<SphericalVec<B>> for Vec3<B> {
/// Converts a spherical coordinate vector to a Euclidean 3-vector.
///
/// See [SphericalVec::to_cart] for more information.
fn from(v: SphericalVec<B>) -> Self {
v.to_cart()
}
}
#[cfg(feature = "fp")]
impl<B> From<Vec3<B>> for SphericalVec<B> {
/// Converts a Cartesian 3-vector into the equivalent spherical vector.
///
/// See [Vec3::to_spherical] for more information.
fn from(v: Vec3<B>) -> Self {
v.to_spherical()
}
}
#[cfg(test)]
#[allow(unused, nonstandard_style)]
mod tests {
use core::f32::consts::{PI, TAU};
use crate::{
assert_approx_eq,
math::{self, Lerp, Vary, Vec2, Vec3},
};
use super::*;
const SQRT_3: f32 = 1.7320508;
#[test]
fn rads_to_degs() {
assert_eq!(rads(PI).to_degs(), 180.0);
}
#[test]
fn rads_to_turns() {
assert_eq!(rads(PI).to_turns(), 0.5);
}
#[test]
fn degs_to_rads() {
assert_eq!(degs(180.0).to_rads(), PI);
}
#[test]
fn degs_to_turns() {
assert_eq!(degs(360.0).to_turns(), 1.0);
}
#[test]
fn turns_to_rads() {
assert_eq!(turns(1.0).to_rads(), TAU);
}
#[test]
fn turns_to_degs() {
assert_eq!(turns(1.0).to_degs(), 360.0);
}
#[test]
fn clamping() {
let min = degs(-45.0);
let max = degs(45.0);
assert_eq!(degs(60.0).clamp(min, max), max);
assert_eq!(degs(10.0).clamp(min, max), degs(10.0));
assert_eq!(degs(-50.0).clamp(min, max), min);
}
#[cfg(feature = "fp")]
#[test]
fn trig_functions() {
assert_eq!(degs(0.0).sin(), 0.0);
assert_eq!(degs(0.0).cos(), 1.0);
assert_approx_eq!(degs(30.0).sin(), 0.5);
assert_approx_eq!(degs(60.0).cos(), 0.5);
let (sin, cos) = degs(90.0).sin_cos();
assert_approx_eq!(sin, 1.0);
assert_approx_eq!(cos, 0.0);
assert_approx_eq!(degs(-45.0).tan(), -1.0);
assert_approx_eq!(degs(0.0).tan(), 0.0);
assert_approx_eq!(degs(45.0).tan(), 1.0);
assert_approx_eq!(degs(135.0).tan(), -1.0);
assert_approx_eq!(degs(225.0).tan(), 1.0);
assert_approx_eq!(degs(315.0).tan(), -1.0);
}
// TODO Micromath requires large epsilon here
#[cfg(all(feature = "fp", not(feature = "mm")))]
#[test]
fn inverse_trig_functions() {
assert_approx_eq!(asin(-1.0), degs(-90.0));
assert_approx_eq!(asin(0.0), degs(0.0));
assert_approx_eq!(asin(0.5), degs(30.0));
assert_approx_eq!(asin(1.0), degs(90.0));
assert_approx_eq!(acos(-1.0), degs(180.0));
assert_approx_eq!(acos(0.0), degs(90.0));
assert_approx_eq!(acos(0.5), degs(60.0));
assert_approx_eq!(acos(1.0), degs(0.0));
assert_approx_eq!(atan2(0.0, 1.0), degs(0.0));
assert_approx_eq!(atan2(1.0, SQRT_3), degs(30.0));
assert_approx_eq!(atan2(1.0, -1.0), degs(135.0));
assert_approx_eq!(atan2(-SQRT_3, -1.0), degs(-120.0));
assert_approx_eq!(atan2(-1.0, 1.0), degs(-45.0));
}
#[cfg(feature = "fp")]
#[test]
fn wrapping() {
use crate::assert_approx_eq;
let a = degs(540.0).wrap(Angle::ZERO, Angle::FULL);
assert_approx_eq!(a, degs(180.0));
let a = degs(225.0).wrap(-Angle::STRAIGHT, Angle::STRAIGHT);
assert_approx_eq!(a, degs(-135.0));
}
#[test]
fn lerping() {
let a = degs(30.0).lerp(°s(60.0), 0.2);
assert_eq!(a, degs(36.0));
}
#[test]
fn varying() {
let mut i = degs(45.0).vary(degs(15.0), Some(4));
assert_approx_eq!(i.next(), Some(degs(45.0)));
assert_approx_eq!(i.next(), Some(degs(60.0)));
assert_approx_eq!(i.next(), Some(degs(75.0)));
assert_approx_eq!(i.next(), Some(degs(90.0)));
assert_approx_eq!(i.next(), None);
}
const vec2: fn(f32, f32) -> Vec2 = math::vec2;
const vec3: fn(f32, f32, f32) -> Vec3 = math::vec3;
#[cfg(feature = "fp")]
#[test]
fn polar_to_cartesian_zero_r() {
assert_eq!(polar(0.0, degs(0.0)).to_cart(), vec2(0.0, 0.0));
assert_eq!(polar(0.0, degs(30.0)).to_cart(), vec2(0.0, 0.0));
assert_eq!(polar(0.0, degs(-120.0)).to_cart(), vec2(0.0, 0.0));
}
#[cfg(feature = "fp")]
#[test]
fn polar_to_cartesian_zero_az() {
assert_eq!(polar(2.0, degs(0.0)).to_cart(), vec2(2.0, 0.0));
assert_eq!(polar(-3.0, degs(0.0)).to_cart(), vec2(-3.0, 0.0));
}
#[cfg(feature = "fp")]
#[test]
fn polar_to_cartesian() {
assert_approx_eq!(polar(2.0, degs(60.0)).to_cart(), vec2(1.0, SQRT_3));
assert_approx_eq!(
polar(3.0, degs(-90.0)).to_cart(),
vec2(0.0, -3.0),
eps = 1e-6
);
assert_approx_eq!(polar(4.0, degs(270.0)).to_cart(), vec2(0.0, -4.0));
assert_approx_eq!(
polar(5.0, turns(1.25)).to_cart(),
vec2(0.0, 5.0),
eps = 2e-6
);
}
#[cfg(feature = "fp")]
#[test]
fn cartesian_to_polar_zero_y() {
assert_approx_eq!(vec2(0.0, 0.0).to_polar(), polar(0.0, degs(0.0)));
assert_eq!(vec2(1.0, 0.0).to_polar(), polar(1.0, degs(0.0)));
}
#[cfg(feature = "fp")]
#[test]
fn cartesian_to_polar() {
assert_approx_eq!(vec2(SQRT_3, 1.0).to_polar(), polar(2.0, degs(30.0)));
assert_eq!(vec2(0.0, 2.0).to_polar(), polar(2.0, degs(90.0)));
assert_approx_eq!(vec2(-3.0, 0.0).to_polar(), polar(3.0, degs(180.0)));
assert_eq!(vec2(0.0, -4.0).to_polar(), polar(4.0, degs(-90.0)));
}
#[cfg(feature = "fp")]
#[test]
fn spherical_to_cartesian() {
let spherical = spherical::<()>;
assert_eq!(
spherical(0.0, degs(0.0), degs(0.0)).to_cart(),
vec3(0.0, 0.0, 0.0)
);
assert_eq!(
spherical(1.0, degs(0.0), degs(0.0)).to_cart(),
vec3(1.0, 0.0, 0.0)
);
assert_approx_eq!(
spherical(2.0, degs(60.0), degs(0.0)).to_cart(),
vec3(1.0, 0.0, SQRT_3)
);
assert_approx_eq!(
spherical(2.0, degs(90.0), degs(0.0)).to_cart(),
vec3(0.0, 0.0, 2.0)
);
assert_approx_eq!(
spherical(3.0, degs(123.0), degs(90.0)).to_cart(),
vec3(0.0, 3.0, 0.0)
);
}
#[cfg(feature = "fp")]
#[test]
fn cartesian_to_spherical_zero_alt() {
assert_approx_eq!(
vec3(0.0, 0.0, 0.0).to_spherical(),
spherical(0.0, degs(0.0), degs(0.0))
);
assert_eq!(
vec3(1.0, 0.0, 0.0).to_spherical(),
spherical(1.0, degs(0.0), degs(0.0))
);
assert_approx_eq!(
vec3(1.0, SQRT_3, 0.0).to_spherical(),
spherical(2.0, degs(0.0), degs(60.0))
);
assert_eq!(
vec3(0.0, 2.0, 0.0).to_spherical(),
spherical(2.0, degs(0.0), degs(90.0))
);
}
#[cfg(feature = "fp")]
#[test]
fn cartesian_to_spherical() {
use core::f32::consts::SQRT_2;
assert_approx_eq!(
vec3(SQRT_3, 0.0, 1.0).to_spherical(),
spherical(2.0, degs(30.0), degs(0.0))
);
assert_approx_eq!(
vec3(1.0, SQRT_2, 1.0).to_spherical(),
spherical(2.0, degs(45.0), degs(45.0))
);
assert_approx_eq!(
vec3(0.0, 0.0, 3.0).to_spherical(),
spherical(3.0, degs(90.0), degs(0.0))
);
}
}