use super::{CameraUniform, ScreenPrimitiveGpu};
#[derive(Clone, Copy, Debug)]
pub(super) struct Certificate {
eligible: bool,
empty: bool,
anchor_maximum: [f64; 2],
corner_maximum: [f64; 2],
minimum_area_ratio: f64,
minimum_nonzero_edge: f64,
maximum_edge: f64,
}
impl Certificate {
#[cfg(test)]
pub(super) fn edge_separation_ratio(self, uniform: CameraUniform) -> f64 {
let mut error = 0.0_f64;
for axis in 0..2 {
let translation = if axis == 0 {
uniform.world_to_screen_x[3]
} else {
uniform.world_to_screen_y[3]
};
let magnitude = self.anchor_maximum[axis]
+ f64::from(uniform.camera_center[axis]).abs()
+ f64::from(translation).abs()
+ self.corner_maximum[axis]
+ f64::from(uniform.screen_to_clip[axis + 2]).abs()
/ f64::from(uniform.screen_to_clip[axis]).abs()
+ 1.0;
error = error.max(128.0 * f64::from(f32::EPSILON) * magnitude);
}
self.minimum_nonzero_edge / error
}
pub(super) fn new(instances: &[ScreenPrimitiveGpu]) -> Self {
let mut result = Self::empty();
for instance in instances {
result.include(instance);
}
result
}
pub(super) fn empty() -> Self {
Self {
eligible: true,
empty: true,
anchor_maximum: [0.0; 2],
corner_maximum: [0.0; 2],
minimum_area_ratio: f64::INFINITY,
minimum_nonzero_edge: f64::INFINITY,
maximum_edge: 0.0,
}
}
pub(super) fn coordinate_maxima(self) -> ([f64; 2], [f64; 2]) {
(self.anchor_maximum, self.corner_maximum)
}
#[inline]
pub(super) fn include(&mut self, instance: &ScreenPrimitiveGpu) {
self.empty = false;
let in_domain = |value: f32| {
value == 0.0
|| (value.is_finite()
&& (2.0_f32.powi(-16)..=2.0_f32.powi(20)).contains(&value.abs()))
};
self.eligible &= instance
.anchor
.into_iter()
.chain(instance.corners.into_iter().flatten())
.all(in_domain);
for axis in 0..2 {
self.anchor_maximum[axis] =
self.anchor_maximum[axis].max(f64::from(instance.anchor[axis]).abs());
for corner in instance.corners {
self.corner_maximum[axis] =
self.corner_maximum[axis].max(f64::from(corner[axis]).abs());
}
}
for indices in [[0, 1, 2], [0, 2, 3]] {
let points = indices.map(|index| instance.corners[index].map(f64::from));
let first = [points[1][0] - points[0][0], points[1][1] - points[0][1]];
let second = [points[2][0] - points[0][0], points[2][1] - points[0][1]];
let positive = first[0] * second[1];
let negative = first[1] * second[0];
let lower_area = (positive - negative).abs()
- 64.0 * f64::EPSILON * (positive.abs() + negative.abs());
let perimeter = first.into_iter().chain(second).map(f64::abs).sum::<f64>();
self.minimum_area_ratio = self.minimum_area_ratio.min(lower_area / (perimeter + 1.0));
self.eligible &= lower_area > 0.0 && lower_area.is_finite();
for edge in first.into_iter().chain(second).map(f64::abs) {
self.maximum_edge = self.maximum_edge.max(edge);
if edge > 0.0 {
self.minimum_nonzero_edge = self.minimum_nonzero_edge.min(edge);
}
}
}
}
pub(super) fn accepts(self, uniform: CameraUniform) -> bool {
if !self.eligible
|| !uniform.sources_are_portable()
|| uniform.world_to_screen_x[..3] != [1.0, 0.0, 0.0]
|| uniform.world_to_screen_y[..3] != [0.0, -1.0, 0.0]
{
return false;
}
if self.empty {
return true;
}
if uniform.camera_center[0] != uniform.world_to_screen_x[3]
|| uniform.camera_center[1] != -uniform.world_to_screen_y[3]
{
return false;
}
let mut maximum_error = 0.0_f64;
for axis in 0..2 {
let center = f64::from(uniform.camera_center[axis]).abs();
let translation = f64::from(if axis == 0 {
uniform.world_to_screen_x[3]
} else {
uniform.world_to_screen_y[3]
})
.abs();
let scale = f64::from(uniform.screen_to_clip[axis]).abs();
let offset = f64::from(uniform.screen_to_clip[axis + 2]).abs();
if !(8.0..=65_536.0).contains(¢er)
|| !(2.0_f64.powi(-20)..=1.0).contains(&scale)
|| !(0.25..=4.0).contains(&offset)
{
return false;
}
let magnitude = self.anchor_maximum[axis]
+ center
+ translation
+ self.corner_maximum[axis]
+ offset / scale
+ 1.0;
maximum_error = maximum_error.max(128.0 * f64::from(f32::EPSILON) * magnitude);
}
if self.minimum_nonzero_edge <= 2.0 * maximum_error {
return false;
}
let uncertainty = 2.0 * maximum_error
+ 8.0 * maximum_error * maximum_error
+ 64.0 * f64::from(f32::EPSILON) * self.maximum_edge;
self.minimum_area_ratio > uncertainty
}
}