From c7d1cf82f703eadb87f1a278ac7782f80b75b27b Mon Sep 17 00:00:00 2001 From: ripytide Date: Fri, 24 May 2024 14:46:50 +0100 Subject: [PATCH 1/5] refactor the `Rect` type --- examples/drawing.rs | 12 +-- examples/template_matching.rs | 16 +++- src/drawing/rect.rs | 91 ++++++++++++++++----- src/edges.rs | 15 +++- src/rect.rs | 147 +++++++++++----------------------- 5 files changed, 145 insertions(+), 136 deletions(-) diff --git a/examples/drawing.rs b/examples/drawing.rs index 47a7ea22..c5677357 100644 --- a/examples/drawing.rs +++ b/examples/drawing.rs @@ -44,18 +44,14 @@ fn main() { draw_line_segment_mut(&mut image, (20f32, 180f32), (20f32, 220f32), white); // Draw a hollow rect within bounds - draw_hollow_rect_mut(&mut image, Rect::at(60, 10).of_size(20, 20), white); + draw_hollow_rect_mut(&mut image, Rect{x: 60, y:10, width:20, height:20}, white); // Outside bounds - draw_hollow_rect_mut(&mut image, Rect::at(300, 10).of_size(20, 20), white); - // Partially outside bounds - draw_hollow_rect_mut(&mut image, Rect::at(90, -10).of_size(30, 20), white); + draw_hollow_rect_mut(&mut image, Rect{x: 300, y:10, width:20, height:20}, white); // Draw a filled rect within bounds - draw_filled_rect_mut(&mut image, Rect::at(130, 10).of_size(20, 20), white); + draw_filled_rect_mut(&mut image, Rect{x: 130, y:10, width:20, height:20}, white); // Outside bounds - draw_filled_rect_mut(&mut image, Rect::at(300, 10).of_size(20, 20), white); - // Partially outside bounds - draw_filled_rect_mut(&mut image, Rect::at(180, -10).of_size(30, 20), white); + draw_filled_rect_mut(&mut image, Rect{x: 300, y:10, width:20, height:20}, white); // Draw a hollow circle within bounds draw_hollow_circle_mut(&mut image, (100, 100), 15, white); diff --git a/examples/template_matching.rs b/examples/template_matching.rs index 320743a4..eed230ba 100644 --- a/examples/template_matching.rs +++ b/examples/template_matching.rs @@ -125,8 +125,12 @@ fn run_match_template( .unwrap(); // Show location the template was extracted from - let roi = Rect::at(args.template_x as i32, args.template_y as i32) - .of_size(args.template_w, args.template_h); + let roi = Rect { + x: args.template_x, + y: args.template_y, + width: args.template_w, + height: args.template_h, + }; draw_green_rect(&result_padded, roi) } @@ -174,8 +178,12 @@ fn main() { ); // Show location the template was extracted from - let roi = Rect::at(args.template_x as i32, args.template_y as i32) - .of_size(args.template_w, args.template_h); + let roi = Rect { + x: args.template_x, + y: args.template_y, + width: args.template_w, + height: args.template_h, + }; let image_with_roi = draw_green_rect(&image, roi); diff --git a/src/drawing/rect.rs b/src/drawing/rect.rs index ee2c99ea..1c087e3e 100644 --- a/src/drawing/rect.rs +++ b/src/drawing/rect.rs @@ -23,15 +23,15 @@ pub fn draw_hollow_rect_mut(canvas: &mut C, rect: Rect, color: C::Pixel) where C: Canvas, { - let left = rect.left() as f32; - let right = rect.right() as f32; - let top = rect.top() as f32; - let bottom = rect.bottom() as f32; - - draw_line_segment_mut(canvas, (left, top), (right, top), color); - draw_line_segment_mut(canvas, (left, bottom), (right, bottom), color); - draw_line_segment_mut(canvas, (left, top), (left, bottom), color); - draw_line_segment_mut(canvas, (right, top), (right, bottom), color); + let left_x = rect.left_x() as f32; + let right_x = rect.right_x() as f32; + let top_y = rect.top_y() as f32; + let bottom_y = rect.bottom_y() as f32; + + draw_line_segment_mut(canvas, (left_x, top_y), (right_x, top_y), color); + draw_line_segment_mut(canvas, (left_x, bottom_y), (right_x, bottom_y), color); + draw_line_segment_mut(canvas, (left_x, top_y), (left_x, bottom_y), color); + draw_line_segment_mut(canvas, (right_x, top_y), (right_x, bottom_y), color); } /// Draws a rectangle and its contents on an image. @@ -52,12 +52,17 @@ pub fn draw_filled_rect_mut(canvas: &mut C, rect: Rect, color: C::Pixel) where C: Canvas, { - let canvas_bounds = Rect::at(0, 0).of_size(canvas.width(), canvas.height()); + let canvas_bounds = Rect { + x: 0, + y: 0, + width: canvas.width(), + height: canvas.height(), + }; if let Some(intersection) = canvas_bounds.intersect(rect) { - for dy in 0..intersection.height() { - for dx in 0..intersection.width() { - let x = intersection.left() as u32 + dx; - let y = intersection.top() as u32 + dy; + for dy in 0..intersection.height { + for dx in 0..intersection.width { + let x = intersection.left_x() + dx; + let y = intersection.top_y() + dy; canvas.draw_pixel(x, y, color); } } @@ -82,7 +87,16 @@ mod tests { 1, 1, 4, 1, 4; 1, 1, 4, 4, 4); - let actual = draw_hollow_rect(&image, Rect::at(2, 2).of_size(3, 3), Luma([4u8])); + let actual = draw_hollow_rect( + &image, + Rect { + x: 2, + y: 2, + width: 3, + height: 3, + }, + Luma([4u8]), + ); assert_pixels_eq!(actual, expected); } @@ -97,7 +111,16 @@ mod tests { 1, 4, 4, 4, 1; 1, 1, 1, 1, 1); - let actual = draw_filled_rect(&image, Rect::at(1, 1).of_size(3, 3), Luma([4u8])); + let actual = draw_filled_rect( + &image, + Rect { + x: 1, + y: 1, + width: 3, + height: 3, + }, + Luma([4u8]), + ); assert_pixels_eq!(actual, expected); } @@ -111,10 +134,24 @@ mod tests { let mut image = Blend(RgbaImage::from_pixel(5, 5, white)); - draw_filled_rect_mut(&mut image, Rect::at(1, 1).of_size(3, 3), blue); draw_filled_rect_mut( &mut image, - Rect::at(2, 2).of_size(1, 1), + Rect { + x: 1, + y: 1, + width: 3, + height: 3, + }, + blue, + ); + draw_filled_rect_mut( + &mut image, + Rect { + x: 2, + y: 2, + width: 1, + height: 1, + }, semi_transparent_red, ); @@ -134,7 +171,16 @@ mod tests { // Draw an opaque rectangle over the central pixel as a sanity check that // we're blending in the correct direction only. - draw_filled_rect_mut(&mut image, Rect::at(2, 2).of_size(1, 1), blue); + draw_filled_rect_mut( + &mut image, + Rect { + x: 2, + y: 2, + width: 1, + height: 1, + }, + blue, + ); assert_eq!(*image.0.get_pixel(2, 2), blue); } } @@ -151,7 +197,12 @@ mod benches { fn bench_draw_filled_rect_mut_rgb(b: &mut Bencher) { let mut image = RgbImage::new(200, 200); let color = Rgb([120u8, 60u8, 47u8]); - let rect = Rect::at(50, 50).of_size(80, 90); + let rect = Rect { + x: 50, + y: 50, + width: 80, + height: 90, + }; b.iter(|| { draw_filled_rect_mut(&mut image, rect, color); black_box(&image); diff --git a/src/edges.rs b/src/edges.rs index 38ce3d9e..1b3975b5 100644 --- a/src/edges.rs +++ b/src/edges.rs @@ -163,9 +163,18 @@ mod benches { fn edge_detect_bench_image(width: u32, height: u32) -> GrayImage { let mut image = GrayImage::new(width, height); - let (w, h) = (width as i32, height as i32); - let large = Rect::at(w / 4, h / 4).of_size(width / 2, height / 2); - let small = Rect::at(9, 9).of_size(3, 3); + let large = Rect { + x: width / 4, + y: height / 4, + width: width / 2, + height: height / 2, + }; + let small = Rect { + x: 9, + y: 9, + width: 3, + height: 3, + }; draw_filled_rect_mut(&mut image, large, Luma([255])); draw_filled_rect_mut(&mut image, small, Luma([255])); diff --git a/src/rect.rs b/src/rect.rs index 5ac47732..89ae67c0 100644 --- a/src/rect.rs +++ b/src/rect.rs @@ -2,31 +2,36 @@ use std::cmp; -/// A rectangular region of non-zero width and height. +/// A rectangular region. +/// /// # Examples /// ``` /// use imageproc::rect::Rect; /// use imageproc::rect::Region; /// /// // Construct a rectangle with top-left corner at (4, 5), width 6 and height 7. -/// let rect = Rect::at(4, 5).of_size(6, 7); +/// let rect = Rect{x:4, y:5,width:6, height:7}; /// /// // Contains top-left point: -/// assert_eq!(rect.left(), 4); -/// assert_eq!(rect.top(), 5); -/// assert!(rect.contains(rect.left(), rect.top())); +/// assert_eq!(rect.left_x(), 4); +/// assert_eq!(rect.top_y(), 5); +/// assert!(rect.contains(rect.left_x(), rect.top_y())); /// /// // Contains bottom-right point, at (left + width - 1, top + height - 1): -/// assert_eq!(rect.right(), 9); -/// assert_eq!(rect.bottom(), 11); -/// assert!(rect.contains(rect.right(), rect.bottom())); +/// assert_eq!(rect.right_x(), 9); +/// assert_eq!(rect.bottom_y(), 11); +/// assert!(rect.contains(rect.right_x(), rect.bottom_y())); /// ``` #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct Rect { - left: i32, - top: i32, - width: u32, - height: u32, + /// The x-coordinate of the top-left corner of the rectangle. + pub x: u32, + /// The y-coordinate of the top-left corner of the rectangle. + pub y: u32, + /// The non-zero width of the rectangle + pub width: u32, + /// The non-zero height of the rectangle + pub height: u32, } /// A geometrical representation of a set of 2D points with coordinate type T. @@ -36,50 +41,32 @@ pub trait Region { } impl Rect { - /// Reduces possibility of confusing coordinates and dimensions - /// when specifying rects. - /// - /// See the [struct-level documentation](struct.Rect.html) for examples. - pub fn at(x: i32, y: i32) -> RectPosition { - RectPosition { left: x, top: y } - } - /// Smallest y-coordinate reached by rect. /// /// See the [struct-level documentation](struct.Rect.html) for examples. - pub fn top(&self) -> i32 { - self.top + pub fn top_y(&self) -> u32 { + self.y } /// Smallest x-coordinate reached by rect. /// /// See the [struct-level documentation](struct.Rect.html) for examples. - pub fn left(&self) -> i32 { - self.left + pub fn left_x(&self) -> u32 { + self.x } /// Greatest y-coordinate reached by rect. /// /// See the [struct-level documentation](struct.Rect.html) for examples. - pub fn bottom(&self) -> i32 { - self.top + (self.height as i32) - 1 + pub fn bottom_y(&self) -> u32 { + self.y + self.height - 1 } /// Greatest x-coordinate reached by rect. /// /// See the [struct-level documentation](struct.Rect.html) for examples. - pub fn right(&self) -> i32 { - self.left + (self.width as i32) - 1 - } - - /// Width of rect. - pub fn width(&self) -> u32 { - self.width - } - - /// Height of rect. - pub fn height(&self) -> u32 { - self.height + pub fn right_x(&self) -> u32 { + self.x + self.width - 1 } /// Returns the intersection of self and other, or none if they are are disjoint. @@ -90,76 +77,42 @@ impl Rect { /// use imageproc::rect::Region; /// /// // Intersecting a rectangle with itself - /// let r = Rect::at(4, 5).of_size(6, 7); + /// let r = Rect{x: 4, y: 5, width: 6, height: 7}; /// assert_eq!(r.intersect(r), Some(r)); /// /// // Intersecting overlapping but non-equal rectangles - /// let r = Rect::at(0, 0).of_size(5, 5); - /// let s = Rect::at(1, 4).of_size(10, 12); - /// let i = Rect::at(1, 4).of_size(4, 1); + /// let r = Rect{x: 0, y: 0, width:5, height:5}; + /// let s = Rect{x: 1, y: 4, width:10, height: 2}; + /// let i = Rect{x: 1, y: 4, width:4, height:1}; /// assert_eq!(r.intersect(s), Some(i)); /// /// // Intersecting disjoint rectangles - /// let r = Rect::at(0, 0).of_size(5, 5); - /// let s = Rect::at(10, 10).of_size(100, 12); + /// let r = Rect{x: 0, y: 0, width:5, height:5}; + /// let s = Rect{x: 10, y: 10, width: 100, height: 12}; /// assert_eq!(r.intersect(s), None); /// ``` pub fn intersect(&self, other: Rect) -> Option { - let left = cmp::max(self.left, other.left); - let top = cmp::max(self.top, other.top); - let right = cmp::min(self.right(), other.right()); - let bottom = cmp::min(self.bottom(), other.bottom()); + let left = cmp::max(self.x, other.x); + let top = cmp::max(self.y, other.y); + let right = cmp::min(self.right_x(), other.right_x()); + let bottom = cmp::min(self.bottom_y(), other.bottom_y()); if right < left || bottom < top { return None; } Some(Rect { - left, - top, + x: left, + y: top, width: (right - left) as u32 + 1, height: (bottom - top) as u32 + 1, }) } } -impl Region for Rect { - fn contains(&self, x: i32, y: i32) -> bool { - self.left <= x && x <= self.right() && self.top <= y && y <= self.bottom() - } -} - -impl Region for Rect { - fn contains(&self, x: f32, y: f32) -> bool { - self.left as f32 <= x - && x <= self.right() as f32 - && self.top as f32 <= y - && y <= self.bottom() as f32 - } -} - -/// Position of the top left of a rectangle. -/// Only used when building a [`Rect`](struct.Rect.html). -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -pub struct RectPosition { - left: i32, - top: i32, -} - -impl RectPosition { - /// Construct a rectangle from a position and size. Width and height - /// are required to be strictly positive. - /// - /// See the [`Rect`](struct.Rect.html) documentation for examples. - pub fn of_size(self, width: u32, height: u32) -> Rect { - assert!(width > 0, "width must be strictly positive"); - assert!(height > 0, "height must be strictly positive"); - Rect { - left: self.left, - top: self.top, - width, - height, - } +impl Region for Rect { + fn contains(&self, x: u32, y: u32) -> bool { + self.x <= x && x <= self.right_x() && self.y <= y && y <= self.bottom_y() } } @@ -167,25 +120,17 @@ impl RectPosition { mod tests { use super::{Rect, Region}; - #[test] - #[should_panic] - fn test_rejects_empty_rectangle() { - Rect::at(1, 2).of_size(0, 1); - } - #[test] fn test_contains_i32() { - let r = Rect::at(5, 5).of_size(6, 6); + let r = Rect { + x: 5, + y: 5, + width: 6, + height: 6, + }; assert!(r.contains(5, 5)); assert!(r.contains(10, 10)); assert!(!r.contains(10, 11)); assert!(!r.contains(11, 10)); } - - #[test] - fn test_contains_f32() { - let r = Rect::at(5, 5).of_size(6, 6); - assert!(r.contains(5f32, 5f32)); - assert!(!r.contains(10.1f32, 10f32)); - } } From 5a763462a3a07da5723eb9ca428ef16392f5f49e Mon Sep 17 00:00:00 2001 From: ripytide Date: Sat, 25 May 2024 12:28:43 +0100 Subject: [PATCH 2/5] fix clippy --- src/rect.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/rect.rs b/src/rect.rs index 89ae67c0..3c271b14 100644 --- a/src/rect.rs +++ b/src/rect.rs @@ -92,20 +92,20 @@ impl Rect { /// assert_eq!(r.intersect(s), None); /// ``` pub fn intersect(&self, other: Rect) -> Option { - let left = cmp::max(self.x, other.x); - let top = cmp::max(self.y, other.y); - let right = cmp::min(self.right_x(), other.right_x()); - let bottom = cmp::min(self.bottom_y(), other.bottom_y()); + let left_x = cmp::max(self.x, other.x); + let top_y = cmp::max(self.y, other.y); + let right_x = cmp::min(self.right_x(), other.right_x()); + let bottom_y = cmp::min(self.bottom_y(), other.bottom_y()); - if right < left || bottom < top { + if right_x < left_x || bottom_y < top_y { return None; } Some(Rect { - x: left, - y: top, - width: (right - left) as u32 + 1, - height: (bottom - top) as u32 + 1, + x: left_x, + y: top_y, + width: right_x - left_x + 1, + height: bottom_y - top_y + 1, }) } } From 9b4f32fd8880363d45afd43c2fcf03ce84e48a0c Mon Sep 17 00:00:00 2001 From: ripytide Date: Sun, 26 May 2024 16:08:41 +0100 Subject: [PATCH 3/5] switch to `image::Rect` --- examples/drawing.rs | 2 +- examples/template_matching.rs | 2 +- src/drawing/rect.rs | 8 +-- src/edges.rs | 2 +- src/lib.rs | 2 +- src/{rect.rs => rect_ext.rs} | 100 +++++++++++----------------------- 6 files changed, 41 insertions(+), 75 deletions(-) rename src/{rect.rs => rect_ext.rs} (53%) diff --git a/examples/drawing.rs b/examples/drawing.rs index c5677357..3a53406f 100644 --- a/examples/drawing.rs +++ b/examples/drawing.rs @@ -5,7 +5,7 @@ use imageproc::drawing::{ draw_cross_mut, draw_filled_circle_mut, draw_filled_rect_mut, draw_hollow_circle_mut, draw_hollow_rect_mut, draw_line_segment_mut, }; -use imageproc::rect::Rect; +use imageproc::rect_ext::Rect; use std::env; use std::path::Path; diff --git a/examples/template_matching.rs b/examples/template_matching.rs index eed230ba..2752ceca 100644 --- a/examples/template_matching.rs +++ b/examples/template_matching.rs @@ -4,7 +4,7 @@ use image::{open, GenericImage, GrayImage, Luma, Rgb, RgbImage}; use imageproc::definitions::Image; use imageproc::drawing::draw_hollow_rect_mut; use imageproc::map::map_pixels; -use imageproc::rect::Rect; +use imageproc::rect_ext::Rect; #[cfg(feature = "rayon")] use imageproc::template_matching::match_template_parallel; use imageproc::template_matching::{match_template, MatchTemplateMethod}; diff --git a/src/drawing/rect.rs b/src/drawing/rect.rs index 1c087e3e..2c0780c8 100644 --- a/src/drawing/rect.rs +++ b/src/drawing/rect.rs @@ -1,7 +1,7 @@ use crate::definitions::Image; use crate::drawing::line::draw_line_segment_mut; use crate::drawing::Canvas; -use crate::rect::Rect; +use crate::rect_ext::{Rect, RectExt}; use image::GenericImage; use std::f32; @@ -58,7 +58,7 @@ where width: canvas.width(), height: canvas.height(), }; - if let Some(intersection) = canvas_bounds.intersect(rect) { + if let Some(intersection) = canvas_bounds.intersect(&rect) { for dy in 0..intersection.height { for dx in 0..intersection.width { let x = intersection.left_x() + dx; @@ -73,7 +73,7 @@ where mod tests { use super::*; use crate::drawing::Blend; - use crate::rect::Rect; + use crate::rect_ext::Rect; use image::{GrayImage, Luma, Pixel, Rgba, RgbaImage}; #[test] @@ -189,7 +189,7 @@ mod tests { #[cfg(test)] mod benches { use super::*; - use crate::rect::Rect; + use crate::rect_ext::Rect; use image::{Rgb, RgbImage}; use test::{black_box, Bencher}; diff --git a/src/edges.rs b/src/edges.rs index 1b3975b5..d18ce8ef 100644 --- a/src/edges.rs +++ b/src/edges.rs @@ -157,7 +157,7 @@ fn hysteresis(input: &Image>, low_thresh: f32, high_thresh: f32) -> Im mod benches { use super::canny; use crate::drawing::draw_filled_rect_mut; - use crate::rect::Rect; + use crate::rect_ext::Rect; use ::test; use image::{GrayImage, Luma}; diff --git a/src/lib.rs b/src/lib.rs index ba42245b..da6e4552 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,7 +43,7 @@ pub mod pixelops; pub mod point; #[cfg(any(feature = "property-testing", test))] pub mod property_testing; -pub mod rect; +pub mod rect_ext; pub mod region_labelling; pub mod seam_carving; pub mod stats; diff --git a/src/rect.rs b/src/rect_ext.rs similarity index 53% rename from src/rect.rs rename to src/rect_ext.rs index 3c271b14..d244377b 100644 --- a/src/rect.rs +++ b/src/rect_ext.rs @@ -1,74 +1,19 @@ -//! Basic manipulation of rectangles. +//! Extension trait for `image::Rect` use std::cmp; -/// A rectangular region. -/// -/// # Examples -/// ``` -/// use imageproc::rect::Rect; -/// use imageproc::rect::Region; -/// -/// // Construct a rectangle with top-left corner at (4, 5), width 6 and height 7. -/// let rect = Rect{x:4, y:5,width:6, height:7}; -/// -/// // Contains top-left point: -/// assert_eq!(rect.left_x(), 4); -/// assert_eq!(rect.top_y(), 5); -/// assert!(rect.contains(rect.left_x(), rect.top_y())); -/// -/// // Contains bottom-right point, at (left + width - 1, top + height - 1): -/// assert_eq!(rect.right_x(), 9); -/// assert_eq!(rect.bottom_y(), 11); -/// assert!(rect.contains(rect.right_x(), rect.bottom_y())); -/// ``` -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -pub struct Rect { - /// The x-coordinate of the top-left corner of the rectangle. - pub x: u32, - /// The y-coordinate of the top-left corner of the rectangle. - pub y: u32, - /// The non-zero width of the rectangle - pub width: u32, - /// The non-zero height of the rectangle - pub height: u32, -} - -/// A geometrical representation of a set of 2D points with coordinate type T. -pub trait Region { - /// Whether this region contains the given point. - fn contains(&self, x: T, y: T) -> bool; -} +pub use image::math::Rect; -impl Rect { +/// Extension methods for [`Rect`] used by `imageproc`. +pub trait RectExt { /// Smallest y-coordinate reached by rect. - /// - /// See the [struct-level documentation](struct.Rect.html) for examples. - pub fn top_y(&self) -> u32 { - self.y - } - + fn top_y(&self) -> u32; /// Smallest x-coordinate reached by rect. - /// - /// See the [struct-level documentation](struct.Rect.html) for examples. - pub fn left_x(&self) -> u32 { - self.x - } - + fn bottom_y(&self) -> u32; /// Greatest y-coordinate reached by rect. - /// - /// See the [struct-level documentation](struct.Rect.html) for examples. - pub fn bottom_y(&self) -> u32 { - self.y + self.height - 1 - } - + fn left_x(&self) -> u32; /// Greatest x-coordinate reached by rect. - /// - /// See the [struct-level documentation](struct.Rect.html) for examples. - pub fn right_x(&self) -> u32 { - self.x + self.width - 1 - } - + fn right_x(&self) -> u32; /// Returns the intersection of self and other, or none if they are are disjoint. /// /// # Examples @@ -91,7 +36,28 @@ impl Rect { /// let s = Rect{x: 10, y: 10, width: 100, height: 12}; /// assert_eq!(r.intersect(s), None); /// ``` - pub fn intersect(&self, other: Rect) -> Option { + fn intersect(&self, other: &Self) -> Option + where + Self: std::marker::Sized; + /// Does `self` contain the given coordinates? + fn contains(&self, x: u32, y: u32) -> bool; +} + +impl RectExt for Rect { + fn top_y(&self) -> u32 { + self.y + } + fn left_x(&self) -> u32 { + self.x + } + fn bottom_y(&self) -> u32 { + self.y + self.height - 1 + } + fn right_x(&self) -> u32 { + self.x + self.width - 1 + } + + fn intersect(&self, other: &Rect) -> Option { let left_x = cmp::max(self.x, other.x); let top_y = cmp::max(self.y, other.y); let right_x = cmp::min(self.right_x(), other.right_x()); @@ -108,9 +74,7 @@ impl Rect { height: bottom_y - top_y + 1, }) } -} -impl Region for Rect { fn contains(&self, x: u32, y: u32) -> bool { self.x <= x && x <= self.right_x() && self.y <= y && y <= self.bottom_y() } @@ -118,7 +82,9 @@ impl Region for Rect { #[cfg(test)] mod tests { - use super::{Rect, Region}; + use crate::rect_ext::RectExt; + + use super::Rect; #[test] fn test_contains_i32() { From c67424abdf469a76b3d21f138a250f34818f1191 Mon Sep 17 00:00:00 2001 From: ripytide Date: Sun, 26 May 2024 16:13:08 +0100 Subject: [PATCH 4/5] fix tests --- examples/drawing.rs | 2 +- examples/template_matching.rs | 2 +- src/drawing/rect.rs | 6 +++--- src/edges.rs | 2 +- src/lib.rs | 2 +- src/{rect_ext.rs => rect.rs} | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) rename src/{rect_ext.rs => rect.rs} (98%) diff --git a/examples/drawing.rs b/examples/drawing.rs index 3a53406f..c5677357 100644 --- a/examples/drawing.rs +++ b/examples/drawing.rs @@ -5,7 +5,7 @@ use imageproc::drawing::{ draw_cross_mut, draw_filled_circle_mut, draw_filled_rect_mut, draw_hollow_circle_mut, draw_hollow_rect_mut, draw_line_segment_mut, }; -use imageproc::rect_ext::Rect; +use imageproc::rect::Rect; use std::env; use std::path::Path; diff --git a/examples/template_matching.rs b/examples/template_matching.rs index 2752ceca..eed230ba 100644 --- a/examples/template_matching.rs +++ b/examples/template_matching.rs @@ -4,7 +4,7 @@ use image::{open, GenericImage, GrayImage, Luma, Rgb, RgbImage}; use imageproc::definitions::Image; use imageproc::drawing::draw_hollow_rect_mut; use imageproc::map::map_pixels; -use imageproc::rect_ext::Rect; +use imageproc::rect::Rect; #[cfg(feature = "rayon")] use imageproc::template_matching::match_template_parallel; use imageproc::template_matching::{match_template, MatchTemplateMethod}; diff --git a/src/drawing/rect.rs b/src/drawing/rect.rs index 2c0780c8..d4121dfe 100644 --- a/src/drawing/rect.rs +++ b/src/drawing/rect.rs @@ -1,7 +1,7 @@ use crate::definitions::Image; use crate::drawing::line::draw_line_segment_mut; use crate::drawing::Canvas; -use crate::rect_ext::{Rect, RectExt}; +use crate::rect::{Rect, RectExt}; use image::GenericImage; use std::f32; @@ -73,7 +73,7 @@ where mod tests { use super::*; use crate::drawing::Blend; - use crate::rect_ext::Rect; + use crate::rect::Rect; use image::{GrayImage, Luma, Pixel, Rgba, RgbaImage}; #[test] @@ -189,7 +189,7 @@ mod tests { #[cfg(test)] mod benches { use super::*; - use crate::rect_ext::Rect; + use crate::rect::Rect; use image::{Rgb, RgbImage}; use test::{black_box, Bencher}; diff --git a/src/edges.rs b/src/edges.rs index d18ce8ef..1b3975b5 100644 --- a/src/edges.rs +++ b/src/edges.rs @@ -157,7 +157,7 @@ fn hysteresis(input: &Image>, low_thresh: f32, high_thresh: f32) -> Im mod benches { use super::canny; use crate::drawing::draw_filled_rect_mut; - use crate::rect_ext::Rect; + use crate::rect::Rect; use ::test; use image::{GrayImage, Luma}; diff --git a/src/lib.rs b/src/lib.rs index da6e4552..ba42245b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,7 +43,7 @@ pub mod pixelops; pub mod point; #[cfg(any(feature = "property-testing", test))] pub mod property_testing; -pub mod rect_ext; +pub mod rect; pub mod region_labelling; pub mod seam_carving; pub mod stats; diff --git a/src/rect_ext.rs b/src/rect.rs similarity index 98% rename from src/rect_ext.rs rename to src/rect.rs index d244377b..0fcf2498 100644 --- a/src/rect_ext.rs +++ b/src/rect.rs @@ -82,7 +82,7 @@ impl RectExt for Rect { #[cfg(test)] mod tests { - use crate::rect_ext::RectExt; + use crate::rect::RectExt; use super::Rect; From 2a485765df4ac15382ca64b0856897dbf7071c64 Mon Sep 17 00:00:00 2001 From: ripytide Date: Sun, 26 May 2024 16:15:18 +0100 Subject: [PATCH 5/5] fix doc-test --- src/rect.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/rect.rs b/src/rect.rs index 0fcf2498..90f937f2 100644 --- a/src/rect.rs +++ b/src/rect.rs @@ -18,23 +18,22 @@ pub trait RectExt { /// /// # Examples /// ``` - /// use imageproc::rect::Rect; - /// use imageproc::rect::Region; + /// use imageproc::rect::{Rect, RectExt}; /// /// // Intersecting a rectangle with itself /// let r = Rect{x: 4, y: 5, width: 6, height: 7}; - /// assert_eq!(r.intersect(r), Some(r)); + /// assert_eq!(r.intersect(&r), Some(r)); /// /// // Intersecting overlapping but non-equal rectangles /// let r = Rect{x: 0, y: 0, width:5, height:5}; /// let s = Rect{x: 1, y: 4, width:10, height: 2}; /// let i = Rect{x: 1, y: 4, width:4, height:1}; - /// assert_eq!(r.intersect(s), Some(i)); + /// assert_eq!(r.intersect(&s), Some(i)); /// /// // Intersecting disjoint rectangles /// let r = Rect{x: 0, y: 0, width:5, height:5}; /// let s = Rect{x: 10, y: 10, width: 100, height: 12}; - /// assert_eq!(r.intersect(s), None); + /// assert_eq!(r.intersect(&s), None); /// ``` fn intersect(&self, other: &Self) -> Option where