Split CanvasRasterizer image into many small tiles

This commit is contained in:
2025-06-23 21:35:01 +02:00
parent f2556f7125
commit 61669e15bd
3 changed files with 169 additions and 44 deletions

View File

@ -1,5 +1,6 @@
use core::f32;
use egui::{Color32, ColorImage, Pos2, Rect, Vec2, emath::TSTransform, epaint::Vertex};
use std::ops::Range;
pub trait BlendFn {
fn blend(a: Color32, b: Color32) -> Color32;
@ -88,7 +89,7 @@ pub fn rasterize_onto<'a, Blend: BlendFn>(
/// A bounding box, measured in pixels.
#[derive(Debug, PartialEq, Eq)]
struct PxBoundingBox {
pub struct PxBoundingBox {
pub x_from: usize,
pub y_from: usize,
pub x_to: usize,
@ -104,9 +105,42 @@ impl PxBoundingBox {
y_to: self.y_to.min(other.y_to),
}
}
pub fn union(&self, other: &PxBoundingBox) -> PxBoundingBox {
PxBoundingBox {
x_from: self.x_from.min(other.x_from),
y_from: self.y_from.min(other.y_from),
x_to: self.x_to.max(other.x_to),
y_to: self.y_to.max(other.y_to),
}
}
pub fn x_range(&self) -> Range<usize> {
self.x_from..self.x_to
}
pub fn y_range(&self) -> Range<usize> {
self.y_from..self.y_to
}
/// Test whether two boxes do NOT overlap
pub fn overlaps_with(&self, other: &PxBoundingBox) -> bool {
!self.is_disjoint_from(other)
}
pub fn is_disjoint_from(&self, other: &PxBoundingBox) -> bool {
false
|| self.x_from > other.x_to
|| self.y_from > other.y_to
|| other.x_from > self.x_to
|| other.y_from > self.y_to
}
}
fn triangle_bounding_box(triangle: &[&Vertex; 3], point_to_pixel: TSTransform) -> PxBoundingBox {
pub fn triangle_bounding_box(
triangle: &[&Vertex; 3],
point_to_pixel: TSTransform,
) -> PxBoundingBox {
// calculate bounding box in point coords
let mut rect = Rect::NOTHING;
for vertex in triangle {