use crate::names;
use kurbo::{Affine, Point};
use pdfrum_object::{Dict, Resolve};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct FunctionBased {
pub domain: [f32; 4],
pub matrix: Affine,
}
impl FunctionBased {
pub(super) fn load(dict: &Dict, r: &impl Resolve) -> Self {
let domain = match dict.array(names::DOMAIN, r) {
Some(a) => [
a.number_at_or_zero(0),
a.number_at_or_zero(1),
a.number_at_or_zero(2),
a.number_at_or_zero(3),
],
None => [0.0, 1.0, 0.0, 1.0],
};
Self {
domain,
matrix: dict.matrix(names::MATRIX, r),
}
}
#[must_use]
pub fn contains(&self, p: Point) -> bool {
let x = p.x;
let y = p.y;
x >= f64::from(self.domain[0])
&& x <= f64::from(self.domain[1])
&& y >= f64::from(self.domain[2])
&& y <= f64::from(self.domain[3])
}
}
#[cfg(test)]
mod tests {
#![allow(
clippy::unreadable_literal,
clippy::float_cmp,
clippy::indexing_slicing,
clippy::cast_precision_loss,
clippy::cast_possible_truncation,
reason = "test fixtures quote oracle vectors verbatim and compare exactly"
)]
use super::FunctionBased;
use kurbo::{Affine, Point};
#[test]
fn the_domain_pairs_per_axis_not_per_corner() {
let s = FunctionBased {
domain: [0.0, 10.0, 100.0, 200.0],
matrix: Affine::IDENTITY,
};
assert!(s.contains(Point::new(5.0, 150.0)));
assert!(!s.contains(Point::new(5.0, 50.0)));
assert!(s.contains(Point::new(0.0, 100.0)));
assert!(s.contains(Point::new(10.0, 200.0)));
assert!(!s.contains(Point::new(10.001, 200.0)));
}
}