54 lines
2.0 KiB
Rust
54 lines
2.0 KiB
Rust
use crate::*;
|
|
pub use color::Rgba8;
|
|
|
|
use ndarray::{Array1, Array3, ArrayViewMut3};
|
|
|
|
pub trait Draw<T> {
|
|
fn draw(&mut self, item: T, color: color::Rgba8, thickness: usize);
|
|
}
|
|
|
|
impl Draw<Aabb2<usize>> for Array3<u8> {
|
|
fn draw(&mut self, item: Aabb2<usize>, color: color::Rgba8, thickness: usize) {
|
|
item.draw(self, color, thickness)
|
|
}
|
|
}
|
|
|
|
pub trait Drawable<Canvas> {
|
|
fn draw(&self, canvas: &mut Canvas, color: color::Rgba8, thickness: usize);
|
|
}
|
|
|
|
/// Implementing Drawable for Aabb2 with Array3<u8> as the canvas type
|
|
/// Assuming Array3<u8> is a 3D array representing an image with RGB/RGBA channels
|
|
impl Drawable<ArrayViewMut3<'_, u8>> for Aabb2<usize> {
|
|
fn draw(&self, canvas: &mut ArrayViewMut3<u8>, color: color::Rgba8, thickness: usize) {
|
|
use itertools::Itertools;
|
|
// let (height, width, channels) = canvas.dim();
|
|
let color = Array1::from_vec(vec![color.r, color.g, color.b, color.a]);
|
|
self.corners()
|
|
.iter()
|
|
.zip(self.padding(thickness).corners())
|
|
.tuple_windows()
|
|
.for_each(|((a, b), (c, d))| {
|
|
let bbox = Aabb2::from_vertices([*a, b, *c, d]).expect("Invalid bounding box");
|
|
use crate::roi::RoiMut;
|
|
let mut out = canvas.roi_mut(bbox).expect("Failed to get ROI");
|
|
out.lanes_mut(ndarray::Axis(2))
|
|
.into_iter()
|
|
.for_each(|mut pixel| {
|
|
pixel.assign(&color);
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
impl Drawable<Array3<u8>> for Aabb2<usize> {
|
|
fn draw(&self, canvas: &mut Array3<u8>, color: color::Rgba8, thickness: usize) {
|
|
use itertools::Itertools;
|
|
// let (height, width, channels) = canvas.dim();
|
|
let color = Array1::from_vec(vec![color.r, color.g, color.b, color.a]);
|
|
let pixel_size = canvas.dim().2;
|
|
let color = color.slice(ndarray::s![..pixel_size]);
|
|
let [x1y1, x2y1, x1y2, x2y2] = self.corners();
|
|
}
|
|
}
|