From 62c7f88f5b600cc88732b8a14855d430cb78cb50 Mon Sep 17 00:00:00 2001 From: James Lindsay <78500760+0HyperCube@users.noreply.github.com> Date: Sat, 28 Jun 2025 03:37:37 +0100 Subject: [PATCH] Fix incorrectly calculating nonzero-area bounding boxes for empty raster images (#2772) No bounding box for empty rasters Co-authored-by: Keavon Chambers --- node-graph/gcore/src/raster_types.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/node-graph/gcore/src/raster_types.rs b/node-graph/gcore/src/raster_types.rs index 0c34ba0c5..4fb1fc4d5 100644 --- a/node-graph/gcore/src/raster_types.rs +++ b/node-graph/gcore/src/raster_types.rs @@ -57,6 +57,10 @@ impl Raster { let RasterStorage::Cpu(cpu) = self.data else { unreachable!() }; cpu } + pub fn is_empty(&self) -> bool { + let data = self.data(); + data.height == 0 || data.width == 0 + } } impl Default for Raster { fn default() -> Self { @@ -93,6 +97,10 @@ impl Raster { let RasterStorage::Gpu(gpu) = &self.data else { unreachable!() }; gpu.clone() } + pub fn is_empty(&self) -> bool { + let data = self.data(); + data.width() == 0 || data.height() == 0 + } } #[cfg(feature = "wgpu")] impl Deref for Raster { @@ -104,9 +112,23 @@ impl Deref for Raster { } pub type RasterDataTable = Instances>; -impl BoundingBox for RasterDataTable { +// TODO: Make this not dupliated +impl BoundingBox for RasterDataTable { fn bounding_box(&self, transform: DAffine2, _include_stroke: bool) -> Option<[DVec2; 2]> { self.instance_ref_iter() + .filter(|instance| !instance.instance.is_empty()) // Eliminate empty images + .flat_map(|instance| { + let transform = transform * *instance.transform; + (transform.matrix2.determinant() != 0.).then(|| (transform * Quad::from_box([DVec2::ZERO, DVec2::ONE])).bounding_box()) + }) + .reduce(Quad::combine_bounds) + } +} + +impl BoundingBox for RasterDataTable { + fn bounding_box(&self, transform: DAffine2, _include_stroke: bool) -> Option<[DVec2; 2]> { + self.instance_ref_iter() + .filter(|instance| !instance.instance.is_empty()) // Eliminate empty images .flat_map(|instance| { let transform = transform * *instance.transform; (transform.matrix2.determinant() != 0.).then(|| (transform * Quad::from_box([DVec2::ZERO, DVec2::ONE])).bounding_box())