zenpsy_api/maps.rs
1//! Borrowed native maps; placement does not imply local causal attribution.
2use crate::{ErrorKind, MetricError, MetricResult, ScoreSpec, fail};
3use zenpixels::PlaneView;
4
5/// Placement of a regular native map grid in input-pixel coordinates.
6/// Cells may cover only a cropped interior or extend beyond the image.
7#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8#[non_exhaustive]
9pub struct MapGrid {
10 /// Input image width, not the number of map cells.
11 input_width: u32,
12 /// Input image height.
13 input_height: u32,
14 /// Left edge of cell column zero, possibly negative.
15 origin_x: i64,
16 /// Top edge of cell row zero, possibly negative.
17 origin_y: i64,
18 /// Cell width and horizontal step in input pixels; positive.
19 cell_width: u32,
20 /// Cell height and vertical step in input pixels; positive.
21 cell_height: u32,
22}
23impl MapGrid {
24 /// One cell per input pixel; empty input geometry is refused.
25 pub fn new(input_width: u32, input_height: u32) -> MetricResult<Self> {
26 if input_width == 0 || input_height == 0 {
27 return fail(
28 MetricError::new(ErrorKind::InvalidConfig).with_message("empty map geometry"),
29 );
30 }
31 Ok(Self {
32 input_width,
33 input_height,
34 origin_x: 0,
35 origin_y: 0,
36 cell_width: 1,
37 cell_height: 1,
38 })
39 }
40 /// Set native cell placement without resizing, padding or resampling values.
41 pub fn with_cells(mut self, x: i64, y: i64, width: u32, height: u32) -> MetricResult<Self> {
42 if width == 0 || height == 0 {
43 return fail(
44 MetricError::new(ErrorKind::InvalidConfig).with_message("zero map cell step"),
45 );
46 }
47 self.origin_x = x;
48 self.origin_y = y;
49 self.cell_width = width;
50 self.cell_height = height;
51 Ok(self)
52 }
53 /// Input image width.
54 pub const fn input_width(self) -> u32 {
55 self.input_width
56 }
57 /// Input image height.
58 pub const fn input_height(self) -> u32 {
59 self.input_height
60 }
61 /// Left edge of the first cell.
62 pub const fn origin_x(self) -> i64 {
63 self.origin_x
64 }
65 /// Top edge of the first cell.
66 pub const fn origin_y(self) -> i64 {
67 self.origin_y
68 }
69 /// Positive cell width and horizontal step.
70 pub const fn cell_width(self) -> u32 {
71 self.cell_width
72 }
73 /// Positive cell height and vertical step.
74 pub const fn cell_height(self) -> u32 {
75 self.cell_height
76 }
77}
78
79/// A borrowed native numeric map, including reduced grids and strided rows.
80/// The assessment owns its storage and descriptors. Undefined native samples
81/// (including NaN) are preserved; scalar validity rules do not apply to samples.
82#[derive(Clone, Copy, Debug)]
83#[non_exhaustive]
84pub struct MapView<'a> {
85 /// Stable map key, ordering and native unit. Unordered diagnostic features
86 /// need no quality direction or perfect value.
87 pub spec: &'a ScoreSpec,
88 /// Original samples, with active width, height and byte stride.
89 pub values: PlaneView<'a>,
90 /// Cell placement in the input image, independent of filter support.
91 pub grid: MapGrid,
92}
93impl<'a> MapView<'a> {
94 /// Borrow an existing map without copying its samples or metadata.
95 pub const fn new(spec: &'a ScoreSpec, values: PlaneView<'a>, grid: MapGrid) -> Self {
96 Self { spec, values, grid }
97 }
98}