use super::{AxisSide, Projection};
use crate::layout::PixelRect;
#[cfg(test)]
thread_local! {
static PROJECT_CALLS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}
impl Projection {
#[cfg(test)]
pub(crate) fn test_projection_calls() -> usize {
PROJECT_CALLS.with(std::cell::Cell::get)
}
#[must_use]
pub fn new(x_range: (f64, f64), y_range: (f64, f64), pixel_box: (f64, f64, f64, f64)) -> Self {
Self {
x_range,
y_range,
pixel_box,
category_counts: [None; 2],
}
}
pub(crate) fn with_categories(mut self, counts: [Option<usize>; 2]) -> Self {
self.category_counts = counts;
self
}
pub(crate) fn category_counts(&self) -> [Option<usize>; 2] {
self.category_counts
}
pub fn project(&self, x: f64, y: f64) -> (f64, f64) {
#[cfg(test)]
PROJECT_CALLS.with(|calls| calls.set(calls.get() + 1));
let (x_min, x_max) = self.x_range;
let (y_min, y_max) = self.y_range;
let (x0, x1, y0, y1) = self.pixel_box;
let px = project_axis(x, (x_min, x_max), x0, x1, self.category_counts[0]);
let py = project_axis(y, (y_min, y_max), y0, y1, self.category_counts[1]);
(px, py)
}
#[expect(
clippy::float_cmp,
reason = "exact projected endpoint identity, not a tolerance"
)]
pub(crate) fn validate_span(
&self,
axis: AxisSide,
source: (f64, f64),
projected: (f64, f64),
nonzero: bool,
context: &str,
) -> Result<(), crate::Error> {
let range = match axis {
AxisSide::Bottom => self.x_range,
AxisSide::Left => self.y_range,
};
if nonzero
&& source.0.min(source.1) <= range.1
&& source.0.max(source.1) >= range.0
&& projected.0.is_finite()
&& projected.0 == projected.1
{
return Err(crate::Error::Render(format!(
"{context}: nonzero span endpoints collapse at 1/1024 CSS-pixel coordinate precision"
)));
}
Ok(())
}
#[must_use]
pub fn x_range(&self) -> (f64, f64) {
self.x_range
}
#[must_use]
pub fn y_range(&self) -> (f64, f64) {
self.y_range
}
#[must_use]
pub fn pixel_box(&self) -> (f64, f64, f64, f64) {
self.pixel_box
}
#[must_use]
pub fn panel_box(&self) -> PixelRect {
let (x0, x1, y0, y1) = self.pixel_box;
PixelRect::new(x0, x1, y0, y1)
}
#[must_use]
pub fn contains_data(&self, x: f64, y: f64) -> bool {
x.is_finite()
&& y.is_finite()
&& x >= self.x_range.0
&& x <= self.x_range.1
&& y >= self.y_range.0
&& y <= self.y_range.1
}
}
#[allow(clippy::float_cmp, reason = "exact domain endpoint identity")]
fn fraction(value: f64, range: (f64, f64)) -> f64 {
if !value.is_finite() || !range.0.is_finite() || !range.1.is_finite() || range.0 >= range.1 {
f64::NAN
} else if value == range.0 {
0.0
} else if value == range.1 {
1.0
} else {
let difference = range.1 - range.0;
let offset = value - range.0;
if difference.is_finite() && offset.is_finite() {
offset / difference
} else {
let scale = value.abs().max(range.0.abs()).max(range.1.abs());
((value / scale) - (range.0 / scale)) / ((range.1 / scale) - (range.0 / scale))
}
}
}
#[allow(
clippy::float_cmp,
reason = "exact semantic band-edge and midpoint identity"
)]
fn project_axis(
value: f64,
range: (f64, f64),
start: f64,
end: f64,
categories: Option<usize>,
) -> f64 {
if value < range.0 || value > range.1 {
return start + fraction(value, range) * (end - start);
}
let position = (|| {
let key = projection_key(value, range)?;
let (origin, span) = axis_quanta(start, end).ok()?;
if let Some(count) = categories {
let denominator = (count as u128).checked_mul(2)?;
let half_index = (fraction(value, range) * denominator as f64).round_ties_even();
let band_position = range.0 + half_index * (range.1 - range.0) / denominator as f64;
if value == band_position {
return ratio_position(origin, span, half_index as u128, denominator);
}
}
ratio_position(origin, span, u128::from(key), u128::from(u64::MAX))
})();
position.map_or(f64::NAN, |position| position as f64 / 1024.0)
}
pub(super) fn axis_quanta(start: f64, end: f64) -> Result<(i64, i64), crate::Error> {
let origin = crate::layout::kernel::nearest_spatial_quanta(start)?;
let end = crate::layout::kernel::nearest_spatial_quanta(end)?;
let span = end
.checked_sub(origin)
.filter(|span| *span >= 0)
.ok_or_else(|| crate::Error::Render("axis span overflow or reversed edges".into()))?;
Ok((origin, span))
}
#[allow(clippy::float_cmp, reason = "exact unit fraction endpoint")]
pub(super) fn projection_key(value: f64, range: (f64, f64)) -> Option<u64> {
let fraction = fraction(value, range);
if !fraction.is_finite() || !(0.0..=1.0).contains(&fraction) {
return None;
}
if fraction == 1.0 {
return Some(u64::MAX);
}
let bits = fraction.to_bits();
let exponent = ((bits >> 52) & 0x7ff) as i32;
let mantissa = (bits & ((1_u64 << 52) - 1)) | if exponent == 0 { 0 } else { 1_u64 << 52 };
let shift = (1075 - exponent.max(1)) as u32;
let numerator = u128::from(mantissa) * u128::from(u64::MAX);
if shift >= 128 {
return Some(0);
}
let quotient = numerator >> shift;
let remainder = numerator & ((1_u128 << shift) - 1);
let halfway = 1_u128 << (shift - 1);
u64::try_from(
quotient
+ u128::from(
remainder > halfway || (remainder == halfway && !quotient.is_multiple_of(2)),
),
)
.ok()
}
pub(super) fn ratio_position(
origin: i64,
span: i64,
numerator: u128,
denominator: u128,
) -> Option<i64> {
let product = u128::try_from(span).ok()?.checked_mul(numerator)?;
let quotient = product.checked_div(denominator)?;
let remainder = product % denominator;
let position = i128::from(origin).checked_add(i128::try_from(quotient).ok()?)?;
let complement = denominator - remainder;
i64::try_from(
position
+ i128::from(remainder > complement || (remainder == complement && position % 2 != 0)),
)
.ok()
}