#[derive(Debug, Clone)]
pub struct Plane {
data: Vec<u16>,
width: usize,
height: usize,
}
impl Plane {
#[must_use]
pub fn new(width: usize, height: usize) -> Self {
Self {
data: vec![0; width.saturating_mul(height)],
width,
height,
}
}
#[must_use]
pub fn width(&self) -> usize {
self.width
}
#[must_use]
pub fn height(&self) -> usize {
self.height
}
#[must_use]
pub fn get(&self, x: usize, y: usize) -> Option<u16> {
if x >= self.width || y >= self.height {
return None;
}
self.data.get(y * self.width + x).copied()
}
pub fn set(&mut self, x: usize, y: usize, value: u16) {
if x < self.width && y < self.height {
if let Some(slot) = self.data.get_mut(y * self.width + x) {
*slot = value;
}
}
}
#[must_use]
pub fn row(&self, y: usize) -> Option<&[u16]> {
if y >= self.height {
return None;
}
self.data.get(y * self.width..y * self.width + self.width)
}
#[must_use]
pub fn sample_clamped(&self, x: isize, y: isize) -> u16 {
if self.width == 0 || self.height == 0 {
return 0;
}
let cx = x.clamp(0, self.width as isize - 1) as usize;
let cy = y.clamp(0, self.height as isize - 1) as usize;
self.data.get(cy * self.width + cx).copied().unwrap_or(0)
}
#[must_use]
pub fn samples(&self) -> &[u16] {
&self.data
}
}
#[cfg(test)]
#[allow(
clippy::unwrap_used,
clippy::indexing_slicing,
clippy::panic,
reason = "tests operate on known-good values and assert shapes directly"
)]
mod tests {
use super::*;
#[test]
fn set_and_get_round_trip_within_bounds() {
let mut p = Plane::new(4, 3);
p.set(2, 1, 300);
assert_eq!(p.get(2, 1), Some(300));
assert_eq!(p.get(0, 0), Some(0));
assert_eq!(p.get(4, 0), None);
assert_eq!(p.get(0, 3), None);
}
#[test]
fn out_of_range_writes_are_dropped() {
let mut p = Plane::new(2, 2);
p.set(5, 5, 999);
assert_eq!(p.samples(), &[0, 0, 0, 0]);
}
#[test]
fn clamped_sampling_replicates_the_edge() {
let mut p = Plane::new(3, 2);
for y in 0..2 {
for x in 0..3 {
p.set(x, y, (10 * y + x) as u16);
}
}
assert_eq!(p.sample_clamped(1, 1), 11);
assert_eq!(p.sample_clamped(-4, -9), 0);
assert_eq!(p.sample_clamped(99, 99), 12);
assert_eq!(p.sample_clamped(99, 0), 2);
}
#[test]
fn row_returns_exactly_the_width() {
let mut p = Plane::new(3, 2);
p.set(0, 1, 7);
p.set(2, 1, 9);
assert_eq!(p.row(1), Some(&[7, 0, 9][..]));
assert_eq!(p.row(2), None);
}
}