1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
//! Camera framing for the cockpit's object miniatures.
/// Returns the camera distance and field of view. The original fits the
/// object sphere against the current scene's planes before drawing it with
/// the miniature camera.
#[must_use]
pub fn fit_sphere(radius: f32, scene_planes: [f32; 2]) -> Option<(f32, f32)> {
let radius = f64::from(radius);
let [near, far] = scene_planes.map(f64::from);
if !radius.is_finite()
|| radius <= 0.0
|| !near.is_finite()
|| near <= 0.0
|| !far.is_finite()
|| radius * 2.0 > far - near
{
return None;
}
// iron3d+0x41c65 fits the diameter into the current scene depth interval.
let distance = (2.0 * radius).clamp(near + radius, far - radius);
let fov = if distance == 2.0 * radius {
75.0_f32.to_radians()
} else {
// Twice the angle subtended by the sphere, plus the native 25% margin.
let sine = f64::from((radius / distance) as f32);
((1.0 - 2.0 * sine * sine).acos() * 1.25) as f32
};
Some((distance as f32, fov))
}
#[cfg(test)]
mod tests {
use super::fit_sphere;
#[test]
fn targeter_uses_native_fits_on_both_clip_planes() {
let planes = [0.5, 700.0];
for (radius, distance, angle) in [
(
f32::from_bits(0x4258_71df),
f32::from_bits(0x42d8_71df),
0x3fa7_8d36,
),
(0.25, 0.75, 0x3f59_7ee0),
(250.0, 450.0, 0x3fbc_7d6b),
] {
let (fit_distance, fit_angle) = fit_sphere(radius, planes).unwrap();
assert_eq!(fit_distance, distance);
assert!((fit_angle - f32::from_bits(angle)).abs() < 2e-7);
}
assert!(fit_sphere(349.75, planes).is_some());
assert!(fit_sphere(350.0, planes).is_none());
}
}
|