Skip to content

Commit

Permalink
improve skeleton bbox estimation
Browse files Browse the repository at this point in the history
  • Loading branch information
necocen committed Jan 24, 2025
1 parent 2fda6a2 commit e482036
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
37 changes: 25 additions & 12 deletions src/tiles/skeleton.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,25 +270,38 @@ impl Skeleton {
return inherited_bbox;
}

let points = [self.anchor1, self.anchor2, self.anchor3, self.anchor4];
let axis2 = self.anchor2 - self.anchor1;
let axis4 = self.anchor4 - self.anchor1;
let p5 = self.anchor1 + axis2 - axis4 / 2;
let p6 = self.anchor1 - axis2 / 2 + axis4 / 4;
let p7 = self.anchor1 + axis2 + axis4 / 2;

let points = [
self.anchor1,
self.anchor2,
self.anchor3,
self.anchor4,
p5,
p6,
p7,
];
let mut min_x = f32::INFINITY;
let mut min_y = f32::INFINITY;
let mut max_x = f32::NEG_INFINITY;
let mut max_y = f32::NEG_INFINITY;

for p in points.iter() {
let x = p.x.to_f32();
let y = p.y.to_f32();
min_x = min_x.min(x);
min_y = min_y.min(y);
max_x = max_x.max(x);
max_y = max_y.max(y);
for p in points {
let p = p.to_vec2();
min_x = min_x.min(p.x);
min_y = min_y.min(p.y);
max_x = max_x.max(p.x);
max_y = max_y.max(p.y);
}

let expanded_min_x = min_x - (max_x - min_x) * 0.5;
let expanded_min_y = min_y - (max_y - min_y) * 0.5;
let expanded_max_x = max_x + (max_x - min_x) * 0.5;
let expanded_max_y = max_y + (max_y - min_y) * 0.5;
let expanded_min_x = min_x - (max_x - min_x) * 0.25;
let expanded_min_y = min_y - (max_y - min_y) * 0.25;
let expanded_max_x = max_x + (max_x - min_x) * 0.25;
let expanded_max_y = max_y + (max_y - min_y) * 0.25;

Aabb::new(
expanded_min_x,
Expand Down
19 changes: 17 additions & 2 deletions src/utils/hex_value.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ops::{Add, AddAssign, Mul, Neg, Sub, SubAssign};
use std::ops::{Add, AddAssign, Div, Mul, Neg, Sub, SubAssign};

use super::Angle;

Expand Down Expand Up @@ -164,6 +164,17 @@ impl Mul<i32> for HexValue {
}
}

impl Div<i32> for HexValue {
type Output = Self;

fn div(self, rhs: i32) -> Self {
Self {
rational: self.rational / rhs,
irrational: self.irrational / rhs,
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -234,11 +245,15 @@ mod tests {
// 減算
assert_eq!(a - b, HexValue::new(-2, -2));

// 否定
//
assert_eq!(-a, HexValue::new(-1, -2));

// 乗算(スカラー)
assert_eq!(a * 3, HexValue::new(3, 6));

// 除算(スカラー)
assert_eq!(a / 2, HexValue::new(0, 1));
assert_eq!((a + b) / 2, HexValue::new(2, 3));
}

#[test]
Expand Down
13 changes: 12 additions & 1 deletion src/utils/hex_vec.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ops::{Add, AddAssign, Mul, Neg, Sub, SubAssign};
use std::ops::{Add, AddAssign, Div, Mul, Neg, Sub, SubAssign};

use super::{Angle, HexValue};

Expand Down Expand Up @@ -123,6 +123,17 @@ impl Mul<i32> for HexVec {
}
}

impl Div<i32> for HexVec {
type Output = Self;

fn div(self, rhs: i32) -> Self {
Self {
x: self.x / rhs,
y: self.y / rhs,
}
}
}

impl std::fmt::Display for HexVec {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "({}, {})", self.x, self.y)
Expand Down

0 comments on commit e482036

Please sign in to comment.