From 57cc30b6cc5af192884f6cea05a0d7d0ae42292f Mon Sep 17 00:00:00 2001 From: Simon Desloges Date: Thu, 15 Jul 2021 22:28:38 -0400 Subject: [PATCH 1/3] Add a document method that returns a layer's bounding box --- core/document/src/document.rs | 5 +++++ core/document/src/layers/mod.rs | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/core/document/src/document.rs b/core/document/src/document.rs index 93b471be7b..4087c98036 100644 --- a/core/document/src/document.rs +++ b/core/document/src/document.rs @@ -227,6 +227,11 @@ impl Document { Ok(()) } + pub fn layer_bounding_box(&self, path: &[LayerId]) -> Result<[DVec2; 2], DocumentError> { + let layer = self.layer(path)?; + Ok(layer.bounding_box(self.root.transform * layer.transform, layer.style)) + } + fn mark_as_dirty(&mut self, path: &[LayerId]) -> Result<(), DocumentError> { let mut root = &mut self.root; root.cache_dirty = true; diff --git a/core/document/src/layers/mod.rs b/core/document/src/layers/mod.rs index b9f294a4e9..b8c6205859 100644 --- a/core/document/src/layers/mod.rs +++ b/core/document/src/layers/mod.rs @@ -100,6 +100,13 @@ impl LayerDataTypes { } } } + + pub fn bounding_box(&self, transform: glam::DAffine2, style: style::PathStyle) -> [DVec2; 2] { + use kurbo::Shape; + let bez_path = self.to_kurbo_path(transform, style); + let bbox = bez_path.bounding_box(); + [DVec2::new(bbox.x0, bbox.y0), DVec2::new(bbox.x1, bbox.y1)] + } } #[derive(Serialize, Deserialize)] @@ -168,6 +175,10 @@ impl Layer { self.data.to_kurbo_path(self.transform, self.style) } + pub fn bounding_box(&self, transform: glam::DAffine2, style: style::PathStyle) -> [DVec2; 2] { + self.data.bounding_box(transform, style) + } + pub fn as_folder_mut(&mut self) -> Result<&mut Folder, DocumentError> { match &mut self.data { LayerDataTypes::Folder(f) => Ok(f), From d8d4ff1d6ea08750ecf98681237ceb2f944ef9ed Mon Sep 17 00:00:00 2001 From: Simon Desloges Date: Thu, 15 Jul 2021 22:48:17 -0400 Subject: [PATCH 2/3] Moved `use` to the top of the file --- core/document/src/layers/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/document/src/layers/mod.rs b/core/document/src/layers/mod.rs index b8c6205859..5d1d1e960e 100644 --- a/core/document/src/layers/mod.rs +++ b/core/document/src/layers/mod.rs @@ -6,6 +6,7 @@ pub use ellipse::Ellipse; pub mod line; use glam::{DMat2, DVec2}; use kurbo::BezPath; +use kurbo::Shape as KurboShape; pub use line::Line; pub mod rect; @@ -102,7 +103,6 @@ impl LayerDataTypes { } pub fn bounding_box(&self, transform: glam::DAffine2, style: style::PathStyle) -> [DVec2; 2] { - use kurbo::Shape; let bez_path = self.to_kurbo_path(transform, style); let bbox = bez_path.bounding_box(); [DVec2::new(bbox.x0, bbox.y0), DVec2::new(bbox.x1, bbox.y1)] From 33613df9b310f3860996c3957623669b399e2210 Mon Sep 17 00:00:00 2001 From: Simon Desloges Date: Thu, 15 Jul 2021 23:11:34 -0400 Subject: [PATCH 3/3] Add layer local bounding box method --- core/document/src/document.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/core/document/src/document.rs b/core/document/src/document.rs index 4087c98036..17b1baacbe 100644 --- a/core/document/src/document.rs +++ b/core/document/src/document.rs @@ -227,11 +227,16 @@ impl Document { Ok(()) } - pub fn layer_bounding_box(&self, path: &[LayerId]) -> Result<[DVec2; 2], DocumentError> { + pub fn layer_axis_aligned_bounding_box(&self, path: &[LayerId]) -> Result<[DVec2; 2], DocumentError> { let layer = self.layer(path)?; Ok(layer.bounding_box(self.root.transform * layer.transform, layer.style)) } + pub fn layer_local_bounding_box(&self, path: &[LayerId]) -> Result<[DVec2; 2], DocumentError> { + let layer = self.layer(path)?; + Ok(layer.bounding_box(layer.transform, layer.style)) + } + fn mark_as_dirty(&mut self, path: &[LayerId]) -> Result<(), DocumentError> { let mut root = &mut self.root; root.cache_dirty = true;