Skip to main content

trackball/
delta.rs

1// Generated `rkyv` resolver lacks docs of its variant fields.
2#![cfg_attr(feature = "rkyv-08", allow(missing_docs))]
3
4use crate::{Frame, float};
5use glam::{Quat, Vec3};
6
7/// Delta transform from initial to final [`Frame`].
8#[derive(Debug, Clone, Copy, PartialEq, Default)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10#[cfg_attr(
11    feature = "rkyv-08",
12    derive(rkyv_08::Archive, rkyv_08::Serialize, rkyv_08::Deserialize),
13    rkyv(crate = rkyv_08)
14)]
15#[non_exhaustive]
16pub enum Delta {
17    /// Yields frame as identity transform (default).
18    #[default]
19    Frame,
20    /// Orbits target around eye by pitch and yaw preserving roll attitude aka first person view.
21    ///
22    /// See [`Frame::look_around()`].
23    First {
24        /// Pitch angle.
25        pitch: f32,
26        /// Yaw angle.
27        yaw: f32,
28        /// Yaw axis.
29        yaw_axis: Vec3,
30    },
31    /// Tracks target which slides by vector in world space.
32    ///
33    /// Preserves eye position inclusive its roll attitude.
34    Track {
35        /// Vector in world space of a sliding target to track.
36        vector: Vec3,
37    },
38    /// Orbits eye by rotation in camera space around point in camera space.
39    ///
40    /// See [`Frame::local_orbit_around()`].
41    Orbit {
42        /// Rotation in camera space.
43        rotation: Quat,
44        /// Point in camera space.
45        point: Vec3,
46    },
47    /// Slides camera eye and target by vector in camera space.
48    ///
49    /// See [`Frame::local_slide()`].
50    Slide {
51        /// Vector in camera space.
52        vector: Vec3,
53    },
54    /// Scales distance between eye and point in camera space by ratio preserving target position.
55    ///
56    /// See [`Frame::local_scale_around()`].
57    Scale {
58        /// Scale ratio.
59        ratio: f32,
60        /// Point in camera space.
61        point: Vec3,
62    },
63}
64
65impl Delta {
66    /// Transforms from initial to final frame.
67    #[must_use]
68    pub fn transform(&self, frame: Frame) -> Frame {
69        let mut frame = frame;
70        match *self {
71            Self::Frame => {}
72            Self::First {
73                pitch,
74                yaw,
75                yaw_axis,
76            } => frame.look_around(pitch, yaw, yaw_axis),
77            Self::Track { vector } => frame.set_target(frame.target() + vector),
78            Self::Orbit { rotation, point } => frame.local_orbit_around(rotation, point),
79            Self::Slide { vector } => frame.local_slide(vector),
80            Self::Scale { ratio, point } => frame.local_scale_around(ratio, point),
81        }
82        frame
83    }
84    /// Inverses delta transform.
85    ///
86    /// Effectively swaps initial with final frame.
87    #[must_use]
88    pub fn inverse(&self) -> Self {
89        match *self {
90            Self::Frame => Self::Frame,
91            Self::First {
92                pitch,
93                yaw,
94                yaw_axis,
95            } => Self::First {
96                pitch: -pitch,
97                yaw: -yaw,
98                yaw_axis,
99            },
100            Self::Track { vector } => Self::Track { vector: -vector },
101            Self::Orbit { rotation, point } => Self::Orbit {
102                rotation: rotation.inverse(),
103                point,
104            },
105            Self::Slide { vector } => Self::Slide { vector: -vector },
106            // Point in camera space of final frame is scaled by ratio.
107            Self::Scale { ratio, point } => Self::Scale {
108                ratio: ratio.recip(),
109                point: point * ratio,
110            },
111        }
112    }
113    /// Interpolates delta transform to a fraction using linear interpolation for the translation
114    /// part, and spherical linear interpolation for the rotation part.
115    ///
116    /// # Arguments
117    ///
118    ///   * `self`: The delta transform to interpolate from.
119    ///   * `t`: The interpolation parameter between 0 and 1.
120    #[must_use]
121    pub fn lerp_slerp(&self, t: f32) -> Self {
122        match *self {
123            Self::Frame => Self::Frame,
124            Self::First {
125                pitch,
126                yaw,
127                yaw_axis,
128            } => Self::First {
129                pitch: pitch * t,
130                yaw: yaw * t,
131                yaw_axis,
132            },
133            Self::Track { vector } => Self::Track { vector: vector * t },
134            Self::Orbit { rotation, point } => Self::Orbit {
135                rotation: Quat::IDENTITY.slerp(rotation, t),
136                point,
137            },
138            Self::Slide { vector } => Self::Slide { vector: vector * t },
139            Self::Scale { ratio, point } => Self::Scale {
140                ratio: float::mul_add(ratio - 1.0, t, 1.0),
141                point,
142            },
143        }
144    }
145}