port find bounding box

though it might not need to be ported, evaluate removing later.
This commit is contained in:
Henry Schimke
2023-03-21 11:59:56 -05:00
parent 5450f05d1b
commit c2ca7fa2b0

View File

@@ -1,6 +1,7 @@
use crate::common::BitMatrix; use crate::common::BitMatrix;
use crate::common::Result; use crate::common::Result;
use crate::point; use crate::point;
use crate::Point;
impl BitMatrix { impl BitMatrix {
pub fn Deflate( pub fn Deflate(
@@ -28,26 +29,23 @@ impl BitMatrix {
} }
pub fn getTopLeftOnBitWithPosition(&self, left: &mut u32, top: &mut u32) -> bool { pub fn getTopLeftOnBitWithPosition(&self, left: &mut u32, top: &mut u32) -> bool {
todo!() let Some(Point{x,y}) = self.getTopLeftOnBit() else {
// int bitsOffset = (int)std::distance(_bits.begin(), std::find_if(_bits.begin(), _bits.end(), isSet)); return false;
// if (bitsOffset == Size(_bits)) { };
// return false; *left = x as u32;
// } *top = y as u32;
// top = bitsOffset / _width;
// left = (bitsOffset % _width); true
// return true;
} }
pub fn getBottomRightOnBitWithPosition(&self, right: &mut u32, bottom: &mut u32) -> bool { pub fn getBottomRightOnBitWithPosition(&self, right: &mut u32, bottom: &mut u32) -> bool {
todo!() let Some(Point{x,y}) = self.getBottomRightOnBit() else {
// int bitsOffset = Size(_bits) - 1 - (int)std::distance(_bits.rbegin(), std::find_if(_bits.rbegin(), _bits.rend(), isSet)); return false;
// if (bitsOffset < 0) { };
// return false; *right = x as u32;
// } *bottom = y as u32;
// bottom = bitsOffset / _width; true
// right = (bitsOffset % _width);
// return true;
} }
pub fn findBoundingBox( pub fn findBoundingBox(
@@ -58,26 +56,47 @@ impl BitMatrix {
height: u32, height: u32,
minSize: u32, minSize: u32,
) -> (bool, u32, u32, u32, u32) { ) -> (bool, u32, u32, u32, u32) {
todo!() let mut left = left;
// int right, bottom; let mut top = top;
// if (!getTopLeftOnBit(left, top) || !getBottomRightOnBit(right, bottom) || bottom - top + 1 < minSize) let mut width = width;
// return false; let mut height = height;
// for (int y = top; y <= bottom; y++ ) { let mut right = 0;
// for (int x = 0; x < left; ++x) let mut bottom = 0;
// if (get(x, y)) { if (!self.getTopLeftOnBitWithPosition(&mut left, &mut top)
// left = x; || !self.getBottomRightOnBitWithPosition(&mut right, &mut bottom)
// break; || bottom - top + 1 < minSize)
// } {
// for (int x = _width-1; x > right; x--) return (false, left, top, width, height);
// if (get(x, y)) { }
// right = x;
// break;
// }
// }
// width = right - left + 1; for y in top..=bottom {
// height = bottom - top + 1; // for (int y = top; y <= bottom; y++ ) {
// return width >= minSize && height >= minSize; for x in 0..left {
// for (int x = 0; x < left; ++x){
if (self.get(x, y)) {
left = x;
break;
}
}
for x in (right..(self.width() - 1)).rev() {
// for (int x = _width-1; x > right; x--){
if (self.get(x, y)) {
right = x;
break;
}
}
}
width = right - left + 1;
height = bottom - top + 1;
(
width >= minSize && height >= minSize,
left,
top,
width,
height,
)
} }
} }