use super::types::CameraPose;
use indicatrix::optics::raytracer::{DEFAULT_FOV_DEG, DEFAULT_POSE};
use std::f32::consts::FRAC_PI_2;
pub const RESET_POSE: CameraPose = CameraPose {
yaw: DEFAULT_POSE.yaw,
pitch: DEFAULT_POSE.pitch,
distance: DEFAULT_POSE.distance,
};
#[must_use]
pub fn orbit_distance_bounds(bounding_radius: f64) -> (f32, f32) {
const MIN_FACTOR: f32 = 0.8;
const MAX_FACTOR: f32 = 16.0 / 3.0;
let radius = (bounding_radius as f32).max(0.01);
(radius * MIN_FACTOR, radius * MAX_FACTOR)
}
#[must_use]
pub fn fit_distance_for_radius(bounding_radius: f64) -> f32 {
const MARGIN: f32 = 1.15;
let half_fov_tan = (DEFAULT_FOV_DEG.to_radians() * 0.5).tan();
(bounding_radius as f32).max(0.01) / half_fov_tan * MARGIN
}
#[must_use]
pub fn wrap_pitch(pitch: f32) -> f32 {
use std::f32::consts::PI;
(pitch + PI).rem_euclid(2.0 * PI) - PI
}
#[must_use]
pub fn orbit_step(pose: CameraPose, dx: f32, dy: f32) -> CameraPose {
CameraPose {
yaw: dx.mul_add(-0.008, pose.yaw),
pitch: wrap_pitch(dy.mul_add(0.008, pose.pitch)),
distance: pose.distance,
}
}
#[must_use]
pub fn zoom_step(distance: f32, delta: f32, bounding_radius: f64) -> f32 {
let (min, max) = orbit_distance_bounds(bounding_radius);
delta.mul_add(-0.002, distance).clamp(min, max)
}
#[must_use]
pub fn standard_view(kind: i32, pose: CameraPose, bounding_radius: f64) -> CameraPose {
if kind == 5 {
let (min, max) = orbit_distance_bounds(bounding_radius);
return CameraPose {
distance: fit_distance_for_radius(bounding_radius).clamp(min, max),
..pose
};
}
let (yaw, pitch) = match kind {
1 => (0.0, FRAC_PI_2),
2 => (0.0, -FRAC_PI_2),
3 => (-FRAC_PI_2, 0.0),
4 => (FRAC_PI_2, 0.0),
_ => (0.0, 0.0),
};
CameraPose { yaw, pitch, ..pose }
}