Skip to content

Commit

Permalink
Change internals of Box2d
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteesteban committed Feb 26, 2024
1 parent ece4e3a commit 16900f9
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 21 deletions.
34 changes: 22 additions & 12 deletions src/core/domains/box2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,48 @@ 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
}
}

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()
}
}

Expand All @@ -45,9 +55,9 @@ impl ShapedDomain for Box2d {

fn shape(&self, i: usize) -> Result<i32, String> {
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 - {}[)",
Expand All @@ -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
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/images/image2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl<T> Image for Image2d<T> {
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: &<Self::Domain as Domain>::Point) -> Self::ReturnType<'a> {
Expand Down
2 changes: 1 addition & 1 deletion src/morpho/tos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Range<T>>::new_from_domain(&new_domain);

// Immerse original pixels (2F)
Expand Down
4 changes: 2 additions & 2 deletions src/tests/core/domains/box2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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());
Expand Down
9 changes: 6 additions & 3 deletions src/tests/core/images/constant_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use crate::{

#[test]
fn test_constant_image_creation_and_access() {
let img = ConstantImage::<u8, Box2d>::new_from_domain_with_value(&Box2d::new(3, 6), 27);
let img = ConstantImage::<u8, Box2d>::new_from_domain_with_value(
&Box2d::new_from_dimension(3, 6),
27,
);

let domain = img.domain();
assert_eq!(domain.width(), 3);
Expand All @@ -19,8 +22,8 @@ fn test_constant_image_creation_and_access() {
#[test]
fn test_constant_image_change_value() {
let ref_img: ConstantImage<i64, Box2d> =
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 =
<ConstantImage<i32, Box2d> as ChangeValueImage<i64>>::change_value(&img);
assert!(img_changed_value == ref_img);
Expand Down
2 changes: 1 addition & 1 deletion src/tests/core/images/views/subdomain_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u8>::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;

Expand Down
2 changes: 1 addition & 1 deletion src/tests/morpho/union_find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Image2d<Point2d>>::new(Box2d::new(3, 3));
let mut uf = CompressedUnionFind::<Image2d<Point2d>>::new(Box2d::new_from_dimension(3, 3));
uf.make_set(&p1);
uf.make_set(&p2);
uf.make_set(&p3);
Expand Down

0 comments on commit 16900f9

Please sign in to comment.