Skip to content

Commit 77d371a

Browse files
committed
Add basis type to Polar and PolarVec
Was already added to Spherical and SphericalVec earlier.
1 parent 6c6a3c2 commit 77d371a

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

core/src/math/angle.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ pub struct Angle(f32);
2727

2828
/// Tag type for a polar coordinate space
2929
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
30-
pub struct Polar;
30+
pub struct Polar<B>(PhantomData<B>);
3131

3232
/// Tag type for a spherical coordinate space.
3333
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
3434
pub struct Spherical<B>(PhantomData<B>);
3535

3636
/// A polar coordinate vector, with radius and azimuth components.
37-
pub type PolarVec = Vector<[f32; 2], Polar>;
37+
pub type PolarVec<B = ()> = Vector<[f32; 2], Polar<B>>;
3838

3939
/// A spherical coordinate vector, with radius, azimuth, and altitude
4040
/// (elevation) components.
41-
pub type SphericalVec<B> = Vector<[f32; 3], Spherical<B>>;
41+
pub type SphericalVec<B = ()> = Vector<[f32; 3], Spherical<B>>;
4242

4343
//
4444
// Free fns and consts
@@ -115,7 +115,7 @@ pub fn atan2(y: f32, x: f32) -> Angle {
115115
}
116116

117117
/// Returns a polar coordinate vector with azimuth `az` and radius `r`.
118-
pub const fn polar(r: f32, az: Angle) -> PolarVec {
118+
pub const fn polar<B>(r: f32, az: Angle) -> PolarVec<B> {
119119
Vector::new([r, az.to_rads()])
120120
}
121121

@@ -264,7 +264,7 @@ impl Angle {
264264
}
265265
}
266266

267-
impl PolarVec {
267+
impl<B> PolarVec<B> {
268268
/// Returns the radial component of `self`.
269269
#[inline]
270270
pub fn r(&self) -> f32 {
@@ -278,8 +278,8 @@ impl PolarVec {
278278

279279
/// Returns `self` converted to the equivalent Cartesian 2-vector.
280280
///
281-
/// Let the components of self be `(r, az)`. Then the `x` component of the
282-
/// result equals `r * cos(az)`, and the `y` component equals `r * sin(az)`.
281+
/// Let the components of `self` be `(r, az)`. Then the result `(x, y)`
282+
/// equals `(r * cos(az), r * sin(az))`.
283283
///
284284
/// ```text
285285
/// +y
@@ -296,13 +296,15 @@ impl PolarVec {
296296
/// use retrofire_core::assert_approx_eq;
297297
/// use retrofire_core::math::{vec2, polar, degs};
298298
///
299+
/// let vec2 = vec2::<f32, ()>;
300+
///
299301
/// assert_approx_eq!(polar(2.0, degs(0.0)).to_cart(), vec2(2.0, 0.0));
300302
/// assert_approx_eq!(polar(3.0, degs(90.0)).to_cart(), vec2(0.0, 3.0));
301303
/// assert_approx_eq!(polar(4.0, degs(-180.0)).to_cart(), vec2(-4.0, 0.0));
302304
///
303305
/// ```
304306
#[cfg(feature = "fp")]
305-
pub fn to_cart(&self) -> Vec2 {
307+
pub fn to_cart(&self) -> Vec2<B> {
306308
let (y, x) = self.az().sin_cos();
307309
vec2(x, y) * self.r()
308310
}
@@ -343,7 +345,7 @@ impl<B> SphericalVec<B> {
343345
}
344346

345347
#[cfg(feature = "fp")]
346-
impl Vec2 {
348+
impl<B> Vec2<B> {
347349
/// Returns `self` converted into the equivalent polar coordinate vector.
348350
///
349351
/// The `r` component of the result equals `self.len()`.
@@ -364,6 +366,8 @@ impl Vec2 {
364366
/// use retrofire_core::assert_approx_eq;
365367
/// use retrofire_core::math::{vec2, degs};
366368
///
369+
/// let vec2 = vec2::<f32, ()>;
370+
///
367371
/// // A non-negative x and zero y maps to zero azimuth
368372
/// assert_eq!(vec2(0.0, 0.0).to_polar().az(), degs(0.0));
369373
/// assert_eq!(vec2(1.0, 0.0).to_polar().az(), degs(0.0));
@@ -377,7 +381,7 @@ impl Vec2 {
377381
/// // A negative x and zero y maps to straight angle azimuth
378382
/// assert_approx_eq!(vec2(-1.0, 0.0).to_polar().az(), degs(180.0));
379383
/// ```
380-
pub fn to_polar(&self) -> PolarVec {
384+
pub fn to_polar(&self) -> PolarVec<B> {
381385
let r = self.len();
382386
let az = atan2(self.y(), self.x());
383387
polar(r, az)
@@ -532,21 +536,21 @@ impl Rem for Angle {
532536
}
533537

534538
#[cfg(feature = "fp")]
535-
impl From<PolarVec> for Vec2 {
539+
impl<B> From<PolarVec<B>> for Vec2<B> {
536540
/// Converts a polar vector into the equivalent Cartesian vector.
537541
///
538542
/// See [PolarVec::to_cart] for more information.
539-
fn from(p: PolarVec) -> Self {
543+
fn from(p: PolarVec<B>) -> Self {
540544
p.to_cart()
541545
}
542546
}
543547

544548
#[cfg(feature = "fp")]
545-
impl From<Vec2> for PolarVec {
549+
impl<B> From<Vec2<B>> for PolarVec<B> {
546550
/// Converts a Cartesian 2-vector into the equivalent polar vector.
547551
///
548552
/// See [Vec2::to_polar] for more information.
549-
fn from(v: Vec2) -> Self {
553+
fn from(v: Vec2<B>) -> Self {
550554
v.to_polar()
551555
}
552556
}
@@ -572,13 +576,13 @@ impl<B> From<Vec3<B>> for SphericalVec<B> {
572576
}
573577

574578
#[cfg(test)]
575-
#[allow(unused)]
579+
#[allow(unused, nonstandard_style)]
576580
mod tests {
577581
use core::f32::consts::{PI, TAU};
578582

579583
use crate::{
580584
assert_approx_eq,
581-
math::{Lerp, Vary},
585+
math::{self, Lerp, Vary, Vec2, Vec3},
582586
};
583587

584588
use super::*;
@@ -695,6 +699,9 @@ mod tests {
695699
assert_approx_eq!(i.next(), None);
696700
}
697701

702+
const vec2: fn(f32, f32) -> Vec2 = math::vec2;
703+
const vec3: fn(f32, f32, f32) -> Vec3 = math::vec3;
704+
698705
#[cfg(feature = "fp")]
699706
#[test]
700707
fn polar_to_cartesian_zero_r() {
@@ -773,7 +780,6 @@ mod tests {
773780
#[cfg(feature = "fp")]
774781
#[test]
775782
fn cartesian_to_spherical_zero_alt() {
776-
let vec3 = vec3::<f32, ()>;
777783
assert_approx_eq!(
778784
vec3(0.0, 0.0, 0.0).to_spherical(),
779785
spherical(0.0, degs(0.0), degs(0.0))
@@ -796,7 +802,6 @@ mod tests {
796802
#[test]
797803
fn cartesian_to_spherical() {
798804
use core::f32::consts::SQRT_2;
799-
let vec3 = vec3::<f32, ()>;
800805
assert_approx_eq!(
801806
vec3(SQRT_3, 0.0, 1.0).to_spherical(),
802807
spherical(2.0, degs(30.0), degs(0.0))

core/src/math/mat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::render::{NdcToScreen, ViewToProj};
1515

1616
use super::{
1717
float::f32,
18-
point::{Point2, Point2u, Point3, pt3},
18+
point::{Point2, Point2u, Point3},
1919
space::{Linear, Proj3, Real},
2020
vec::{ProjVec3, Vec2, Vec3, Vector},
2121
};

0 commit comments

Comments
 (0)