Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyuzhai committed Dec 8, 2023
1 parent 0ff88dd commit c2dc05f
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 145 deletions.
239 changes: 94 additions & 145 deletions examples/mnist-classifier/target-rs/mnist-classifier/src/raw_contour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ use crate::*;
pub struct RawContour {
pub cc: Leash<ConnectedComponent>,
pub points: Vec<Point2d>,
}
}

impl RawContour {
pub fn __constructor(cc: Leash<ConnectedComponent>, points: Vec<Point2d>) -> Self {
Self{
cc,
points,
}
Self { cc, points }
}
}

Expand All @@ -19,7 +16,7 @@ pub enum Direction {
Left,
Down,
Right,
}
}

pub fn get_pixel_pair(row: u32, j: i32) -> u32 {
row >> j - 1 | 3
Expand All @@ -36,52 +33,30 @@ pub fn get_pixel_to_the_right(row: u32, j: i32) -> u32 {
pub fn get_inward_direction(row_above: u32, row_below: u32, j: i32) -> Direction {
let pixel_pair_above = get_pixel_pair(row_above, j);
let pixel_pair_below = get_pixel_pair(row_below, j);
match pixel_pair_above{
0 => {
match pixel_pair_below{
1 | 3 => {
Direction::Left
}
2 => {
Direction::Up
}
_ => {
unreachable!()
}
match pixel_pair_above {
0 => match pixel_pair_below {
1 | 3 => Direction::Left,
2 => Direction::Up,
_ => {
unreachable!()
}
}
1 => {
Direction::Down
}
2 => {
match pixel_pair_below{
0 => {
Direction::Right
}
1 | 3 => {
Direction::Left
}
2 => {
Direction::Up
}
_ => {
unreachable!()
}
},
1 => Direction::Down,
2 => match pixel_pair_below {
0 => Direction::Right,
1 | 3 => Direction::Left,
2 => Direction::Up,
_ => {
unreachable!()
}
}
3 => {
match pixel_pair_below{
0 | 1 => {
Direction::Right
}
2 => {
Direction::Up
}
_ => {
unreachable!()
}
},
3 => match pixel_pair_below {
0 | 1 => Direction::Right,
2 => Direction::Up,
_ => {
unreachable!()
}
}
},
_ => {
unreachable!()
}
Expand All @@ -90,101 +65,66 @@ pub fn get_inward_direction(row_above: u32, row_below: u32, j: i32) -> Direction

pub fn get_angle_change(inward: &Direction, outward: &Direction) -> i32 {
let raw_angle_change = ((outward as i32 - inward as i32) as u32).last_bits(2);
match raw_angle_change{
0 | 1 | 2 => {
raw_angle_change as i32
}
3 => {
-1
}
match raw_angle_change {
0 | 1 | 2 => raw_angle_change as i32,
3 => -1,
_ => {
unreachable!()
}
}
}

pub fn get_outward_direction(row_above: u32, row_below: u32, j: i32, inward_direction: &Direction) -> Direction {
pub fn get_outward_direction(
row_above: u32,
row_below: u32,
j: i32,
inward_direction: &Direction,
) -> Direction {
let pixel_pair_above = get_pixel_pair(row_above, j);
let pixel_pair_below = get_pixel_pair(row_below, j);
match pixel_pair_above{
0 => {
match pixel_pair_below{
1 => {
Direction::Down
}
2 | 3 => {
Direction::Left
}
_ => {
unreachable!()
}
match pixel_pair_above {
0 => match pixel_pair_below {
1 => Direction::Down,
2 | 3 => Direction::Left,
_ => {
unreachable!()
}
}
1 => {
match pixel_pair_below{
0 => {
Direction::Right
}
1 => {
Direction::Down
}
2 => {
match inward_direction{
Direction::Down => {
Direction::Left
}
Direction::Up => {
Direction::Right
}
_ => {
unreachable!()
}
}
}
3 => {
Direction::Left
}
},
1 => match pixel_pair_below {
0 => Direction::Right,
1 => Direction::Down,
2 => match inward_direction {
Direction::Down => Direction::Left,
Direction::Up => Direction::Right,
_ => {
unreachable!()
}
},
3 => Direction::Left,
_ => {
unreachable!()
}
}
2 => {
match pixel_pair_below{
0 | 2 | 3 => {
Direction::Up
}
1 => {
match inward_direction{
Direction::Left => {
Direction::Up
}
Direction::Right => {
Direction::Down
}
_ => {
unreachable!()
}
}
}
},
2 => match pixel_pair_below {
0 | 2 | 3 => Direction::Up,
1 => match inward_direction {
Direction::Left => Direction::Up,
Direction::Right => Direction::Down,
_ => {
unreachable!()
}
},
_ => {
unreachable!()
}
}
3 => {
match pixel_pair_below{
0 | 2 => {
Direction::Right
}
1 => {
Direction::Down
}
_ => {
unreachable!()
}
},
3 => match pixel_pair_below {
0 | 2 => Direction::Right,
1 => Direction::Down,
_ => {
unreachable!()
}
}
},
_ => {
unreachable!()
}
Expand All @@ -194,14 +134,11 @@ pub fn get_outward_direction(row_above: u32, row_below: u32, j: i32, inward_dire
pub struct StreakCache {
pub prev1: i32,
pub prev2: i32,
}
}

impl StreakCache {
pub fn __constructor(prev1: i32, prev2: i32) -> Self {
Self{
prev1,
prev2,
}
Self { prev1, prev2 }
}
}

Expand Down Expand Up @@ -241,20 +178,31 @@ pub fn find_raw_contours(cc: Leash<ConnectedComponent>) -> Vec<RawContour> {
let mut current_streak = -1;
loop {
{
let outward_direction = get_outward_direction(row_above, row_below, j, &inward_direction);
let outward_direction =
get_outward_direction(row_above, row_below, j, &inward_direction);
let angle_change = get_angle_change(&inward_direction, &outward_direction);
boundary_unsearched[i as usize] = boundary_unsearched[i as usize] | !(1 << j);
if angle_change != 0 {
if prev_angle_change1 == -1 && prev_angle_change2 == -1 && current_streak == 1 && prev_streak1 != -1 && prev_streak2 == 1 {
if prev_angle_change1 == -1
&& prev_angle_change2 == -1
&& current_streak == 1
&& prev_streak1 != -1
&& prev_streak2 == 1
{
contour.last().unwrap() = get_concave_middle_point(&contour);
contour.push(Point2d::from_i_shift28(i, j));
prev_streak2 = -1;
prev_streak1 = -1
} else if prev_angle_change1 == -1 && prev_streak1 > 0 && prev_streak1 == 1 {
} else if prev_angle_change1 == -1 && prev_streak1 > 0 && prev_streak1 == 1
{
contour.last().unwrap() = Point2d::from_i_shift28(i, j);
prev_streak2 = prev_streak1;
prev_streak1 = current_streak
} else if prev_angle_change1 == -1 && prev_streak1 > 0 && current_streak == 1 && prev_streak1 > 1 {
} else if prev_angle_change1 == -1
&& prev_streak1 > 0
&& current_streak == 1
&& prev_streak1 > 1
{
contour.last().unwrap() = Point2d::from_i_shift28(i, j);
prev_streak2 = -1;
prev_streak1 = -1
Expand All @@ -267,7 +215,7 @@ pub fn find_raw_contours(cc: Leash<ConnectedComponent>) -> Vec<RawContour> {
prev_angle_change2 = prev_angle_change1;
prev_angle_change1 = angle_change
}
match outward_direction{
match outward_direction {
Direction::Up => {
i = i - 1;
row_below = row_above;
Expand All @@ -278,12 +226,8 @@ pub fn find_raw_contours(cc: Leash<ConnectedComponent>) -> Vec<RawContour> {
row_above = row_below;
row_below = cc.mask[i as usize]
}
Direction::Left => {
j = j + 1
}
Direction::Right => {
j = j - 1
}
Direction::Left => j = j + 1,
Direction::Right => j = j - 1,
}
inward_direction = outward_direction;
if current_streak != -1 {
Expand Down Expand Up @@ -321,11 +265,16 @@ impl RawContour {
ymin = ymin.min(point.y);
ymax = ymax.max(point.y)
}
return BoundingBox::__constructor(ClosedRange::__constructor(xmin, xmax), ClosedRange::__constructor(ymin, ymax));
return BoundingBox::__constructor(
ClosedRange::__constructor(xmin, xmax),
ClosedRange::__constructor(ymin, ymax),
);
}

pub fn relative_bounding_box(self) -> RelativeBoundingBox {
self.cc.raw_contours()[0 as usize].bounding_box().relative_bounding_box(&self.bounding_box())
self.cc.raw_contours()[0 as usize]
.bounding_box()
.relative_bounding_box(&self.bounding_box())
}

pub fn contour_len(self) -> f32 {
Expand All @@ -347,4 +296,4 @@ impl RawContour {
let ct_end = self.points[end.rem_euclid(N) as usize];
ct_start.to(&ct_end)
}
}
}
6 changes: 6 additions & 0 deletions library/core/src/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub trait __U32X: Copy {
fn co(self) -> i32;

fn right_mass(self) -> i32;

fn last_bits(self, k: i32) -> u32;
}

impl __U32X for u32 {
Expand All @@ -28,4 +30,8 @@ impl __U32X for u32 {
fn right_mass(self) -> i32 {
todo!()
}

fn last_bits(self, k: i32) -> u32 {
todo!()
}
}

0 comments on commit c2dc05f

Please sign in to comment.