diff --git a/src/core/domains/box2d.rs b/src/core/domains/box2d.rs index 81927a6..43057b7 100644 --- a/src/core/domains/box2d.rs +++ b/src/core/domains/box2d.rs @@ -5,24 +5,31 @@ use crate::{ #[derive(Clone, Copy)] pub struct Box2d { - width: i32, - height: i32, + pmin: Point2d, + pmax: Point2d, } impl Box2d { - pub fn new(width: i32, height: i32) -> Box2d { + pub fn new(pmin: Point2d, pmax: Point2d) -> Box2d { Box2d { - width: width, - height: height, + pmin: pmin, + pmax: pmax, + } + } + + pub fn new_from_dimension(width: i32, height: i32) -> Box2d { + Box2d { + pmin: Point2d::new(0, 0), + pmax: Point2d::new(width - 1, height - 1), } } pub fn width(&self) -> i32 { - self.width + self.pmax.x() - self.pmin.x() + 1 } pub fn height(&self) -> i32 { - self.height + self.pmax.y() - self.pmin.y() + 1 } } @@ -30,13 +37,16 @@ impl Domain for Box2d { type Point = Point2d; fn has(&self, p: &Self::Point) -> bool { - p.x() >= 0 && p.y() >= 0 && p.x() < self.width && p.y() < self.height() + p.x() >= self.pmin.x() + && p.y() >= self.pmin.y() + && p.x() <= self.pmax.x() + && p.y() <= self.pmax.y() } } impl SizedDomain for Box2d { fn size(&self) -> i32 { - self.width * self.height + self.width() * self.height() } } @@ -45,9 +55,9 @@ impl ShapedDomain for Box2d { fn shape(&self, i: usize) -> Result { if i == 0 { - Ok(self.width) + Ok(self.width()) } else if i == 1 { - Ok(self.height) + Ok(self.height()) } else { Err(format!( "Index i ({}) out of range (should be in [0 - {}[)", @@ -69,7 +79,7 @@ impl IntoIterator for Box2d { impl PartialEq for Box2d { fn eq(&self, other: &Self) -> bool { - self.width == other.width && self.height == other.height + self.pmin == other.pmin && self.pmax == other.pmax } } diff --git a/src/core/images/image2d.rs b/src/core/images/image2d.rs index 5cd5dc7..52147ca 100644 --- a/src/core/images/image2d.rs +++ b/src/core/images/image2d.rs @@ -49,7 +49,7 @@ impl Image for Image2d { type ReturnType<'a> = &'a T where Self: 'a; fn domain(&self) -> Self::Domain { - Self::Domain::new(self.width, self.height) + Self::Domain::new_from_dimension(self.width, self.height) } fn at_point<'a>(&'a self, p: &::Point) -> Self::ReturnType<'a> { diff --git a/src/morpho/tos.rs b/src/morpho/tos.rs index fcbbc5e..a52a3e8 100644 --- a/src/morpho/tos.rs +++ b/src/morpho/tos.rs @@ -64,7 +64,7 @@ mod internal { T: Ord + Display + Copy + Default + Bounded, { let domain = img.domain(); - let new_domain = Box2d::new(domain.width() * 2 - 1, domain.height() * 2 - 1); + let new_domain = Box2d::new_from_dimension(domain.width() * 2 - 1, domain.height() * 2 - 1); let mut res = Image2d::>::new_from_domain(&new_domain); // Immerse original pixels (2F) diff --git a/src/tests/core/domains/box2d.rs b/src/tests/core/domains/box2d.rs index 38cceeb..73617b8 100644 --- a/src/tests/core/domains/box2d.rs +++ b/src/tests/core/domains/box2d.rs @@ -2,7 +2,7 @@ use crate::Box2d; #[test] fn test_creation_and_attributes() { - let domain = Box2d::new(7, 3); + let domain = Box2d::new_from_dimension(7, 3); assert_eq!(domain.width(), 7); assert_eq!(domain.height(), 3); } @@ -13,7 +13,7 @@ fn test_iter() { "(0, 0)", "(1, 0)", "(2, 0)", "(3, 0)", "(0, 1)", "(1, 1)", "(2, 1)", "(3, 1)", ]; - let domain = Box2d::new(4, 2); + let domain = Box2d::new_from_dimension(4, 2); let mut i = 0; for p in domain { let formatted = String::from(format!("{}", p).as_str()); diff --git a/src/tests/core/images/constant_image.rs b/src/tests/core/images/constant_image.rs index 61cb392..387d158 100644 --- a/src/tests/core/images/constant_image.rs +++ b/src/tests/core/images/constant_image.rs @@ -5,7 +5,10 @@ use crate::{ #[test] fn test_constant_image_creation_and_access() { - let img = ConstantImage::::new_from_domain_with_value(&Box2d::new(3, 6), 27); + let img = ConstantImage::::new_from_domain_with_value( + &Box2d::new_from_dimension(3, 6), + 27, + ); let domain = img.domain(); assert_eq!(domain.width(), 3); @@ -19,8 +22,8 @@ fn test_constant_image_creation_and_access() { #[test] fn test_constant_image_change_value() { let ref_img: ConstantImage = - ConstantImage::new_from_domain_with_value(&Box2d::new(2, 2), 7); - let img = ConstantImage::new_from_domain_with_value(&Box2d::new(2, 2), 7); + ConstantImage::new_from_domain_with_value(&Box2d::new_from_dimension(2, 2), 7); + let img = ConstantImage::new_from_domain_with_value(&Box2d::new_from_dimension(2, 2), 7); let img_changed_value = as ChangeValueImage>::change_value(&img); assert!(img_changed_value == ref_img); diff --git a/src/tests/core/images/views/subdomain_view.rs b/src/tests/core/images/views/subdomain_view.rs index 095a523..17c1a17 100644 --- a/src/tests/core/images/views/subdomain_view.rs +++ b/src/tests/core/images/views/subdomain_view.rs @@ -4,7 +4,7 @@ use crate::{traits::Image, views::SubdomainView, Box2d, Image2d}; fn test_subdomain_box2d() { const REF_VAL: [u8; 4] = [1, 2, 4, 5]; let img = Image2d::new_from_vec(3, 3, Vec::::from([1, 2, 3, 4, 5, 6, 7, 8, 9])); - let new_domain = Box2d::new(2, 2); + let new_domain = Box2d::new_from_dimension(2, 2); let view = SubdomainView::new(&img, new_domain); let mut i = 0; diff --git a/src/tests/morpho/union_find.rs b/src/tests/morpho/union_find.rs index 1e5dfac..0cb2d82 100644 --- a/src/tests/morpho/union_find.rs +++ b/src/tests/morpho/union_find.rs @@ -8,7 +8,7 @@ fn test_compressed_union_find() { let p1 = Point2d::new(0, 0); let p2 = Point2d::new(1, 0); let p3 = Point2d::new(1, 1); - let mut uf = CompressedUnionFind::>::new(Box2d::new(3, 3)); + let mut uf = CompressedUnionFind::>::new(Box2d::new_from_dimension(3, 3)); uf.make_set(&p1); uf.make_set(&p2); uf.make_set(&p3);