use crate::raster2::{Point2I, Vector2I};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RoiPoint {
pub local: Point2I,
pub parent: Point2I,
}
#[derive(Debug, Clone)]
pub struct RoiOverlap {
a: RasterRoi,
b: RasterRoi,
i: RasterRoi,
}
impl RoiOverlap {
pub fn a(&self) -> &RasterRoi {
&self.a
}
pub fn b(&self) -> &RasterRoi {
&self.b
}
pub fn new(a: RasterRoi, b: RasterRoi) -> Self {
let i = a.intersection(&b);
Self { a, b, i }
}
pub fn iter_intersection_a(&self) -> RoiIterator<'_> {
let offset = self.i.min - self.a.min;
RoiIterator::new(&self.i, offset)
}
pub fn iter_intersection_b(&self) -> RoiIterator<'_> {
let offset = self.i.min - self.b.min;
RoiIterator::new(&self.i, offset)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct RasterRoi {
pub min: Point2I,
pub max: Point2I,
}
impl RasterRoi {
pub fn is_empty(&self) -> bool {
self.min.x == self.max.x || self.min.y == self.max.y
}
pub fn empty() -> Self {
Self {
min: Point2I::new(0, 0),
max: Point2I::new(0, 0),
}
}
pub fn new(min: Point2I, max: Point2I) -> Self {
assert!(min.x <= max.x && min.y <= max.y, "Invalid ROI bounds");
Self { min, max }
}
pub fn from_bounds(min_x: i32, min_y: i32, max_x: i32, max_y: i32) -> Self {
Self {
min: Point2I::new(min_x, min_y),
max: Point2I::new(max_x, max_y),
}
}
pub fn extent(&self) -> Vector2I {
self.max - self.min
}
pub fn contains_indices(&self, x: i32, y: i32) -> bool {
x >= self.min.x && x < self.max.x && y >= self.min.y && y < self.max.y
}
pub fn out_to_in(&self, outside: Point2I) -> Point2I {
Point2I::from(outside - self.min)
}
pub fn in_to_out(&self, inside: Point2I) -> Point2I {
Point2I::from(inside + self.min.coords)
}
pub fn expand_to_contain(&mut self, outside: Point2I) {
if self.is_empty() {
self.min = outside;
self.max = self.min + Vector2I::new(1, 1);
} else {
self.min.x = self.min.x.min(outside.x);
self.min.y = self.min.y.min(outside.y);
self.max.x = self.max.x.max(outside.x + 1);
self.max.y = self.max.y.max(outside.y + 1);
}
}
pub fn union(&self, other: &RasterRoi) -> Self {
if self.is_empty() {
return *other;
}
if other.is_empty() {
return *self;
}
Self {
min: Point2I::new(self.min.x.min(other.min.x), self.min.y.min(other.min.y)),
max: Point2I::new(self.max.x.max(other.max.x), self.max.y.max(other.max.y)),
}
}
pub fn intersects(&self, other: &RasterRoi) -> bool {
if self.is_empty() || other.is_empty() {
return false;
}
self.min.x < other.max.x
&& self.max.x > other.min.x
&& self.min.y < other.max.y
&& self.max.y > other.min.y
}
pub fn intersection(&self, other: &RasterRoi) -> Self {
if !self.intersects(other) {
return Self::empty();
}
Self {
min: Point2I::new(self.min.x.max(other.min.x), self.min.y.max(other.min.y)),
max: Point2I::new(self.max.x.min(other.max.x), self.max.y.min(other.max.y)),
}
}
pub fn iter_points(&self) -> RoiIterator<'_> {
RoiIterator::new(self, Vector2I::default())
}
pub fn expanded(&self, padding: u32) -> RasterRoi {
let padding = padding as i32;
let offset = Vector2I::new(padding, padding);
let new_min = self.min - offset;
let new_max = self.max + offset;
RasterRoi::new(new_min, new_max)
}
}
impl Default for RasterRoi {
fn default() -> Self {
Self::empty()
}
}
pub struct RoiIterator<'a> {
pub roi: &'a RasterRoi,
pub x: i32,
pub y: i32,
pub offset: Vector2I,
}
impl<'a> RoiIterator<'a> {
pub fn new(roi: &'a RasterRoi, offset: Vector2I) -> Self {
Self {
roi,
x: 0,
y: 0,
offset,
}
}
}
impl<'a> Iterator for RoiIterator<'a> {
type Item = RoiPoint;
fn next(&mut self) -> Option<Self::Item> {
let extent = self.roi.extent();
if self.y >= extent.y {
return None;
}
let roi = Point2I::new(self.x, self.y);
let parent = self.roi.in_to_out(roi);
let point = RoiPoint {
local: roi + self.offset,
parent,
};
self.x += 1;
if self.x >= extent.x {
self.x = 0;
self.y += 1;
}
Some(point)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::image::{ImageBuffer, Luma};
use crate::raster2::RasterMask;
use crate::raster2::roi_mask::RoiMask;
use imageproc::definitions::Image;
use std::collections::HashMap;
fn make_test_setup() -> (HashMap<Point2I, usize>, Image<Luma<usize>>, RasterRoi) {
let mut map = HashMap::new();
let mut image = ImageBuffer::new(60, 20);
let mut c = 1;
let mut roi = RasterRoi::empty();
let corner = Vector2I::new(12, 8);
for x in 0..24 {
for y in 0..7 {
let inside = Point2I::new(x, y);
map.insert(inside, c);
let outside = inside + corner;
roi.expand_to_contain(outside);
image.put_pixel(outside.x as u32, outside.y as u32, Luma([c]));
c += 1;
}
}
(map, image, roi)
}
#[test]
fn roi_iterator_forward() {
let (map, image, roi) = make_test_setup();
for p in roi.iter_points() {
let expected = map.get(&p.local);
assert!(expected.is_some(), "Point {:?} not found in map", p.local);
let expected = expected.unwrap();
let actual = image.get_pixel(p.parent.x as u32, p.parent.y as u32)[0];
assert_eq!(*expected, actual, "Mismatch at point {:?}", p);
}
}
#[test]
fn roi_mask_offset_matching() {
let (map, image, roi) = make_test_setup();
let mask = RasterMask::empty(roi.extent().x as u32 + 4, roi.extent().y as u32 + 4);
let offset = Vector2I::new(-2, -2);
let roi_mask = RoiMask::new_resized(mask, roi, roi.expanded(2));
let mut expected_map = HashMap::new();
for (kp, v) in map.iter() {
expected_map.insert(kp - offset, *v);
}
assert_eq!(*expected_map.get(&Point2I::new(2, 2)).unwrap(), 1);
assert!(!expected_map.contains_key(&Point2I::new(0, 0)));
for p in roi_mask.iter_shared_points() {
let expected = expected_map.get(&p.local);
assert!(expected.is_some(), "Point {:?} not found in map", p.local);
let expected = expected.unwrap();
let actual = image.get_pixel(p.parent.x as u32, p.parent.y as u32)[0];
assert_eq!(*expected, actual, "Mismatch at point {:?}", p);
}
}
#[test]
fn roi_expand() {
let mut roi = RasterRoi::empty();
assert!(roi.is_empty());
roi.expand_to_contain(Point2I::new(1, 1));
assert!(!roi.is_empty());
assert_eq!(roi.min, Point2I::new(1, 1));
assert_eq!(roi.max, Point2I::new(2, 2));
roi.expand_to_contain(Point2I::new(0, 0));
assert_eq!(roi.min, Point2I::new(0, 0));
assert_eq!(roi.max, Point2I::new(2, 2));
roi.expand_to_contain(Point2I::new(3, 3));
assert_eq!(roi.min, Point2I::new(0, 0));
assert_eq!(roi.max, Point2I::new(4, 4));
}
#[test]
fn test_raster_roi_empty() {
let roi = RasterRoi::empty();
assert!(roi.is_empty());
}
}