extern crate image;
use image::{ImageBuffer, Rgb};
use imageproc::drawing::draw_filled_rect_mut;
use imageproc::rect::Rect;
use crate::bucket::Bucket;
use crate::packing_box::PackingBox;
pub fn generate_visualization(placed_boxes: &[PackingBox], bins: &Vec<Bucket>) {
const BUFFER: i32 = 10;
let max_bin_width = bins.iter().map(|bin| bin.width).max().unwrap_or(0);
let max_bin_height = bins.iter().map(|bin| bin.height).max().unwrap_or(0);
let width = bins.len() as i32 * (max_bin_width + BUFFER) - BUFFER; let height = max_bin_height;
let mut img = ImageBuffer::<Rgb<u8>, Vec<u8>>::new(width as u32, height as u32);
for (i, bin) in bins.iter().enumerate() {
draw_filled_rect_mut(
&mut img,
Rect::at(i as i32 * (max_bin_width + BUFFER), 0)
.of_size(bin.width as u32, bin.height as u32),
Rgb([200, 200, 200]),
);
for box_item in placed_boxes
.iter()
.filter(|&b| b.bucketid == Some(bin.bucketid))
{
let (box_x1, box_x2, box_y1, box_y2) = box_item.get_coords();
let x_offset = i as i32 * (max_bin_width + BUFFER);
let color = Rgb([
rand::random::<u8>(),
rand::random::<u8>(),
rand::random::<u8>(),
]);
draw_filled_rect_mut(
&mut img,
Rect::at(box_x1 + x_offset, box_y1)
.of_size((box_x2 - box_x1) as u32, (box_y2 - box_y1) as u32),
color,
);
}
}
img.save::<&str>("output.png").unwrap();
}