1mod contours;
15mod drawing;
16mod math;
17mod normalize;
18mod poly_algos;
19mod shapes;
20
21pub use contours::{RetrievalMode, find_contours};
22pub use drawing::{FillIter, Painter, Rgb, draw_line, draw_polygon, fill_rect, stroke_rect};
23pub use math::Vec2;
24pub use normalize::{IMAGENET_MEAN, IMAGENET_STD_DEV, normalize_image};
25pub use poly_algos::{convex_hull, min_area_rect, simplify_polygon, simplify_polyline};
26pub use shapes::{
27 BoundingRect, Coord, Line, LineF, Point, PointF, Polygon, PolygonF, Polygons, Rect, RectF,
28 RotatedRect, bounding_rect,
29};
30
31#[cfg(test)]
32mod tests {
33 use std::fmt::Display;
34
35 use rten_tensor::{MatrixLayout, NdTensorView, NdTensorViewMut};
36
37 use super::{Coord, Point, Rect};
38
39 pub fn border_points(rect: Rect, omit_corners: bool) -> Vec<Point> {
45 let mut points = Vec::new();
46
47 let left_range = if omit_corners {
48 rect.top() + 1..rect.bottom() - 1
49 } else {
50 rect.top()..rect.bottom()
51 };
52
53 for y in left_range.clone() {
55 points.push(Point::from_yx(y, rect.left()));
56 }
57
58 for x in rect.left() + 1..rect.right() - 1 {
60 points.push(Point::from_yx(rect.bottom() - 1, x));
61 }
62
63 for y in left_range.rev() {
65 points.push(Point::from_yx(y, rect.right() - 1));
66 }
67
68 for x in (rect.left() + 1..rect.right() - 1).rev() {
70 points.push(Point::from_yx(rect.top(), x));
71 }
72
73 points
74 }
75
76 #[allow(dead_code)]
78 pub fn plot_points<T: Copy>(mut grid: NdTensorViewMut<T, 2>, points: &[Point], value: T) {
79 for point in points {
80 grid[point.coord()] = value;
81 }
82 }
83
84 #[allow(dead_code)]
87 fn plot_point_indices<T: std::ops::AddAssign + Copy + Default>(
88 mut grid: NdTensorViewMut<T, 2>,
89 points: &[Point],
90 step: T,
91 ) {
92 let mut value = T::default();
93 value += step;
94 for point in points {
95 grid[point.coord()] = value;
96 value += step;
97 }
98 }
99
100 pub fn points_from_coords<T: Coord>(coords: &[[T; 2]]) -> Vec<Point<T>> {
102 coords.iter().map(|[y, x]| Point::from_yx(*y, *x)).collect()
103 }
104
105 pub fn points_from_n_coords<T: Coord, const N: usize>(coords: [[T; 2]; N]) -> [Point<T>; N] {
107 coords.map(|[y, x]| Point::from_yx(y, x))
108 }
109
110 #[allow(dead_code)]
112 pub fn print_grid<T: Display>(grid: NdTensorView<T, 2>) {
113 for y in 0..grid.rows() {
114 for x in 0..grid.cols() {
115 print!("{:2} ", grid[[y, x]]);
116 }
117 println!();
118 }
119 println!();
120 }
121}