Skip to main content

trackball/
fixed.rs

1use crate::float;
2use core::f32::consts::FRAC_PI_4;
3use glam::Vec2;
4
5/// Fixed quantity wrt field of view.
6///
7///   * Implements [`Default`] and can be created with `Fixed::default()` returning
8///     `Fixed::Ver(FRAC_PI_4)`.
9///   * Implements `From<f32>` and can be created with `f32::into()` returning `Fixed::Ver()`.
10#[derive(Debug, Clone, Copy, PartialEq)]
11#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
12#[cfg_attr(
13    feature = "rkyv-08",
14    derive(rkyv_08::Archive, rkyv_08::Serialize, rkyv_08::Deserialize),
15    rkyv(crate = rkyv_08)
16)]
17pub enum Fixed {
18    /// Fixed horizontal field of view aka Vert- scaling.
19    Hor(f32),
20    /// Fixed vertical field of view aka Hor+ scaling.
21    Ver(f32),
22    /// Fixed unit per pixel on focus plane at distance from eye of one aka Pixel-based scaling.
23    Upp(f32),
24}
25
26impl Default for Fixed {
27    fn default() -> Self {
28        FRAC_PI_4.into()
29    }
30}
31
32impl From<f32> for Fixed {
33    fn from(fov: f32) -> Self {
34        Self::Ver(fov)
35    }
36}
37
38impl Fixed {
39    /// Converts to fixed horizontal field of view wrt maximum position in screen space.
40    #[must_use]
41    pub fn to_hor(self, max: Vec2) -> Self {
42        let two = 2.0;
43        Self::Hor(match self {
44            Self::Hor(fov) => fov,
45            Self::Ver(fov) => float::atan(max.x / max.y * float::tan(fov / two)) * two,
46            Self::Upp(upp) => float::atan(max.x / two * upp) * two,
47        })
48    }
49    /// Converts to fixed vertical field of view wrt maximum position in screen space.
50    #[must_use]
51    pub fn to_ver(self, max: Vec2) -> Self {
52        let two = 2.0;
53        Self::Ver(match self {
54            Self::Hor(fov) => float::atan(max.y / max.x * float::tan(fov / two)) * two,
55            Self::Ver(fov) => fov,
56            Self::Upp(upp) => float::atan(max.y / two * upp) * two,
57        })
58    }
59    /// Converts to fixed unit per pixel on focus plane at distance from eye of one wrt maximum
60    /// position in screen space.
61    #[must_use]
62    pub fn to_upp(self, max: Vec2) -> Self {
63        let two = 2.0;
64        Self::Upp(match self {
65            Self::Hor(fov) => float::tan(fov / two) * two / max.x,
66            Self::Ver(fov) => float::tan(fov / two) * two / max.y,
67            Self::Upp(upp) => upp,
68        })
69    }
70    /// Maximum position in camera space and unit per pixel on focus plane wrt distance between
71    /// eye and target and maximum position in screen space.
72    #[must_use]
73    pub fn max_and_upp(self, zat: f32, max: Vec2) -> (Vec2, f32) {
74        let two = 2.0;
75        match self {
76            Self::Hor(fov) => {
77                let x = zat * float::tan(fov / two);
78                let y = max.y / max.x * x;
79                (Vec2::new(x, y), x * two / max.x)
80            }
81            Self::Ver(fov) => {
82                let y = zat * float::tan(fov / two);
83                let x = max.x / max.y * y;
84                (Vec2::new(x, y), y * two / max.y)
85            }
86            Self::Upp(upp) => {
87                let upp = upp * zat;
88                (max / two * upp, upp)
89            }
90        }
91    }
92    /// Underlying quantity.
93    #[must_use]
94    pub const fn into_inner(self) -> f32 {
95        match self {
96            Self::Hor(fov) | Self::Ver(fov) => fov,
97            Self::Upp(upp) => upp,
98        }
99    }
100}