use eframe::egui;
use log::{Level, log_enabled};
pub fn rotate_aabb(rect: &egui::Rect, rot: egui::emath::Rot2, origin: egui::Vec2) -> egui::Rect {
let a = origin + rot * (rect.left_top() - origin.to_pos2());
let b = origin + rot * (rect.right_top() - origin.to_pos2());
let c = origin + rot * (rect.left_bottom() - origin.to_pos2());
let d = origin + rot * (rect.right_bottom() - origin.to_pos2());
egui::Rect::from_min_max(
a.min(b).min(c).min(d).to_pos2(),
a.max(b).max(c).max(d).to_pos2(),
)
}
pub fn quantized_intersection(
rect: &egui::Rect,
region_of_interest: &egui::Rect,
quantum: f32,
) -> egui::Rect {
let offset = rect.min.to_vec2();
let rect = rect.translate(-offset);
let crop_rect = region_of_interest.translate(-offset);
let intersection = rect.intersect(crop_rect);
let min = if intersection.min.to_vec2().length() > rect.min.to_vec2().length() {
let x = (intersection.min.x / quantum).floor() * quantum;
let y = (intersection.min.y / quantum).floor() * quantum;
egui::Pos2::new(x, y).max(rect.min)
} else {
intersection.min
};
let max = if intersection.max.to_vec2().length() < rect.max.to_vec2().length() {
let x = (intersection.max.x / quantum).ceil() * quantum;
let y = (intersection.max.y / quantum).ceil() * quantum;
egui::Pos2::new(x, y).min(rect.max)
} else {
intersection.max
};
egui::Rect::from_min_max(min, max).translate(offset)
}
pub fn debug_paint(painter: &egui::Painter, rect: egui::Rect, color: egui::Color32, label: &str) {
if !log_enabled!(Level::Trace) {
return;
}
painter.debug_rect(rect, color, label);
}