1use crate::{Rigid, float};
2use core::f32::consts::PI;
3use glam::{Quat, Vec3, camera::rh::view::look_to_quat};
4
5#[derive(Debug, Clone, Copy, PartialEq, Default)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8#[cfg_attr(
9 feature = "rkyv-08",
10 derive(rkyv_08::Archive, rkyv_08::Serialize, rkyv_08::Deserialize),
11 rkyv(crate = rkyv_08)
12)]
13pub struct Frame {
14 pos: Vec3,
16 rot: Quat,
18 zat: f32,
20}
21
22impl Frame {
23 #[must_use]
25 pub fn look_at(eye: Vec3, target: Vec3, up: Vec3) -> Self {
26 let (dir, zat) = (target - eye).normalize_and_length();
27 Self {
28 pos: target,
29 rot: look_to_quat(dir, up).inverse(),
30 zat,
31 }
32 }
33 #[must_use]
35 pub fn eye(&self) -> Vec3 {
36 self.pos + self.rot * Vec3::Z * self.zat
37 }
38 pub fn set_eye(&mut self, eye: Vec3, up: Vec3) {
40 *self = Self::look_at(eye, self.pos, up);
41 }
42 #[must_use]
44 pub const fn target(&self) -> Vec3 {
45 self.pos
46 }
47 pub fn set_target(&mut self, pos: Vec3) {
51 let eye = self.eye();
52 let (new_dir, zat) = (pos - eye).normalize_and_length();
53 let old_dir = (self.pos - eye).normalize();
54 let yaw = if old_dir.dot(new_dir) < float::sqrt(f32::EPSILON) - 1.0 {
56 Quat::from_axis_angle(self.yaw_axis(), PI)
57 } else {
58 Quat::IDENTITY
59 };
60 let old_dir = yaw * old_dir;
63 let axis = old_dir.cross(new_dir);
64 let arc = Quat::from_xyzw(axis.x, axis.y, axis.z, 1.0 + old_dir.dot(new_dir)).normalize();
65 let rot = (arc * yaw * self.rot).normalize();
66 *self = Self { pos, rot, zat }
67 }
68 #[must_use]
70 pub const fn distance(&self) -> f32 {
71 self.zat
72 }
73 pub const fn set_distance(&mut self, zat: f32) {
75 self.zat = zat;
76 }
77 pub fn scale(&mut self, rat: f32) {
79 self.zat *= rat;
80 }
81 pub fn local_scale_around(&mut self, rat: f32, pos: Vec3) {
83 self.local_slide(pos * (1.0 - rat));
84 self.scale(rat);
85 }
86 pub fn scale_around(&mut self, rat: f32, pos: Vec3) {
88 let pos = pos - self.pos;
89 self.slide(pos * (1.0 - rat));
90 self.scale(rat);
91 }
92 pub fn local_slide(&mut self, vec: Vec3) {
94 self.pos += self.rot * vec;
95 }
96 pub fn slide(&mut self, vec: Vec3) {
98 self.pos += vec;
99 }
100 pub fn local_orbit(&mut self, rot: Quat) {
102 self.rot *= rot;
103 }
104 pub fn local_orbit_around(&mut self, rot: Quat, pos: Vec3) {
106 self.local_slide(pos - rot * pos);
107 self.local_orbit(rot);
108 }
109 pub fn orbit(&mut self, rot: Quat) {
111 self.rot = rot * self.rot;
112 }
113 pub fn orbit_around(&mut self, rot: Quat, pos: Vec3) {
115 let pos = pos - self.pos;
116 self.slide(pos - rot * pos);
117 self.orbit(rot);
118 }
119 pub fn look_around(&mut self, pitch: f32, yaw: f32, yaw_axis: Vec3) {
123 let pitch = Quat::from_axis_angle(self.pitch_axis(), pitch);
124 let yaw = Quat::from_axis_angle(yaw_axis, yaw);
125 self.orbit_around(yaw * pitch, self.eye());
126 }
127 #[allow(clippy::unused_self)]
129 #[must_use]
130 pub const fn local_pitch_axis(&self) -> Vec3 {
131 Vec3::X
132 }
133 #[allow(clippy::unused_self)]
135 #[must_use]
136 pub const fn local_yaw_axis(&self) -> Vec3 {
137 Vec3::Y
138 }
139 #[allow(clippy::unused_self)]
141 #[must_use]
142 pub const fn local_roll_axis(&self) -> Vec3 {
143 Vec3::Z
144 }
145 #[must_use]
147 pub fn pitch_axis(&self) -> Vec3 {
148 self.rot * self.local_pitch_axis()
149 }
150 #[must_use]
152 pub fn yaw_axis(&self) -> Vec3 {
153 self.rot * self.local_yaw_axis()
154 }
155 #[must_use]
157 pub fn roll_axis(&self) -> Vec3 {
158 self.rot * self.local_roll_axis()
159 }
160 #[must_use]
169 pub fn lerp_slerp(&self, other: Self, t: f32) -> Self {
170 Self {
171 pos: self.pos.lerp(other.pos, t),
172 rot: self.rot.slerp(other.rot, t),
173 zat: float::mul_add(other.zat, t, self.zat * (1.0 - t)),
174 }
175 }
176 pub fn renormalize(&mut self) -> f32 {
178 let norm = self.rot.length();
179 self.rot /= norm;
180 norm
181 }
182 #[must_use]
184 pub fn view(&self) -> Rigid {
185 Rigid::from_rotation_translation(
186 self.rot,
188 self.eye(),
190 )
191 }
192 #[must_use]
196 pub fn inverse_view(&self) -> Rigid {
197 let rot = self.rot.inverse();
199 let eye = rot * self.pos + Vec3::Z * self.zat;
201 Rigid::from_rotation_translation(rot, -eye)
203 }
204 #[must_use]
206 pub fn abs_diff_eq(&self, other: Self, max_abs_diff: f32) -> bool {
207 self.pos.abs_diff_eq(other.pos, max_abs_diff)
208 && self.rot.abs_diff_eq(other.rot, max_abs_diff)
209 && (self.zat - other.zat).abs() <= max_abs_diff
210 }
211}