From 4392c7fca245b8702932fbc5e0ed011bc0c6f00c Mon Sep 17 00:00:00 2001 From: konsumlamm Date: Fri, 21 Jun 2019 20:56:21 +0200 Subject: refactor maths.rs to math.rs --- webhogg/game_server/src/math.rs | 369 ++++++++++++++++++++++++++++++++++++++ webhogg/game_server/src/maths.rs | 370 --------------------------------------- 2 files changed, 369 insertions(+), 370 deletions(-) create mode 100644 webhogg/game_server/src/math.rs delete mode 100644 webhogg/game_server/src/maths.rs diff --git a/webhogg/game_server/src/math.rs b/webhogg/game_server/src/math.rs new file mode 100644 index 0000000..6d9f1e1 --- /dev/null +++ b/webhogg/game_server/src/math.rs @@ -0,0 +1,369 @@ +#[derive(Clone, Copy, Debug)] +pub struct Vec2 { + pub x: f32, + pub y: f32, +} + +impl std::ops::Add for Vec2 { + type Output = Self; + fn add(self, other: Self) -> Self { + Self { + x: self.x + other.x, + y: self.y + other.y + } + } +} + +impl std::ops::AddAssign for Vec2 { + fn add_assign(&mut self, other: Vec2) { + self.x += other.x; + self.y += other.y; + } +} + +impl std::ops::Sub for Vec2 { + type Output = Self; + fn sub(self, other: Self) -> Self { + Self { + x: self.x - other.x, + y: self.y - other.y + } + } +} + +impl std::ops::SubAssign for Vec2 { + fn sub_assign(&mut self, other: Vec2) { + self.x -= other.x; + self.y -= other.y; + } +} + +impl std::ops::Neg for Vec2 { + type Output = Self; + fn neg(self) -> Self { + Self { + x: -self.x, + y: -self.y + } + } +} + +impl std::ops::Mul for Vec2 { + type Output = Self; + fn mul(self, scale: f32) -> Self { + Self { + x: self.x * scale, + y: self.y * scale + } + } +} + +impl std::ops::Div for Vec2 { + type Output = Self; + fn div(self, scale: f32) -> Self { + Self { + x: self.x / scale, + y: self.y / scale + } + } +} + +impl std::ops::Div for Vec2 { + type Output = Self; + fn div(self, scale: Vec2) -> Self { + Self { + x: self.x / scale.x, + y: self.y / scale.y + } + } +} + + +impl std::cmp::PartialOrd for Vec2 { + fn partial_cmp(&self, other: &Self) -> Option { + if self.x <= other.x && self.y <= other.y { + Some(std::cmp::Ordering::Less) + } else if self.x > other.x && self.y > other.y { + Some(std::cmp::Ordering::Greater) + } else { + None + } + } +} + +impl std::cmp::PartialEq for Vec2 { + fn eq(&self, other: &Self) -> bool { + f32::abs(self.x - other.x) < 1e-8 + && f32::abs(self.y - other.y) < 1e-8 + } +} + +impl std::cmp::Eq for Vec2 {} + +impl Vec2 { + pub fn norm(&self) -> f32 { + f32::hypot(self.x, self.y) + } + + pub fn norm2(&self) -> f32 { + self.dot(self) + } + + pub fn dot(&self, other: &Vec2) -> f32 { + self.x * other.x + self.y * other.y + } + + pub fn normalized(&self) -> Vec2 { + let len = self.norm(); + Vec2 { + x: self.x / len, + y: self.y / len, + } + } +} + +#[derive(Clone, Copy, Debug)] +pub struct AABox { + pub pos: Vec2, + /// the size may not be smaller than zero + pub size: Vec2, +} + +impl std::ops::Add for AABox { + type Output = Self; + fn add(self, other: Vec2) -> Self { + Self { + pos: self.pos + other, + size: self.size, + } + } +} + +impl std::ops::AddAssign for AABox { + fn add_assign(&mut self, other: Vec2) { + self.pos += other + } +} + +impl std::ops::Sub for AABox { + type Output = Self; + fn sub(self, other: Vec2) -> Self { + Self { + pos: self.pos - other, + size: self.size + } + } +} + +impl std::ops::SubAssign for AABox { + fn sub_assign(&mut self, other: Vec2) { + self.pos -= other + } +} + +impl std::cmp::PartialEq for AABox { + fn eq(&self, other: &Self) -> bool { + self.pos == other.pos + && self.size == other.size + } +} + +impl std::cmp::Eq for AABox {} + +#[derive(Clone, Copy, Debug)] +pub struct RBox { + /// origin + pub pos: Vec2, + /// Vector1 + pub v1: Vec2, + /// Vector2 + pub v2: Vec2, +} + +impl RBox { + pub fn new(pos: Vec2, orientation: Vec2, width: f32) -> Self { + let scale = width / orientation.norm(); + let orth = Vec2 {x: orientation.x / scale, y: -orientation.y / scale}; + Self { + pos: pos, + v1: orientation, + v2: orth, + } + } +} + +impl std::ops::Add for RBox { + type Output = Self; + fn add(self, other: Vec2) -> Self { + Self { + pos: self.pos + other, + v1: self.v1, + v2: self.v2, + } + } +} + +impl std::ops::AddAssign for RBox { + fn add_assign(&mut self, other: Vec2) { + self.pos += other + } +} + +impl std::ops::Sub for RBox { + type Output = Self; + fn sub(self, other: Vec2) -> Self { + Self { + pos: self.pos - other, + v1: self.v1, + v2: self.v2, + } + } +} + +impl std::ops::SubAssign for RBox { + fn sub_assign(&mut self, other: Vec2) { + self.pos -= other + } +} + +impl std::cmp::PartialEq for RBox { + fn eq(&self, other: &Self) -> bool { + self.pos == other.pos + && self.v1 == other.v1 + && self.v2 == other.v2 + } +} + +impl std::cmp::Eq for RBox {} + +#[cfg(test)] +mod tests { + // Note this useful idiom: importing names from outer (for mod tests) scope. + use super::*; + + #[test] + fn test_less_vec2() { + let a = Vec2{x: 1.0, y: 7.5}; + let b = Vec2{x: -3.0, y: 2.5}; + + assert!(b < a); + } + + #[test] + fn test_less_vec2_fail() { + let a = Vec2{x: 1.0, y: 7.5}; + let b = Vec2{x: 3.0, y: 2.5}; + + assert!(!(a < b)); + } + + #[test] + fn test_greater_vec2() { + let a = Vec2{x: 1.0, y: 7.5}; + let b = Vec2{x: -3.0, y: 2.5}; + + assert!(a > b); + } + + #[test] + fn test_greater_vec2_fail() { + let a = Vec2{x: 1.0, y: 7.5}; + let b = Vec2{x: 3.0, y: 2.5}; + + assert!(!(a > b)); + } + + #[test] + fn test_add_vec2() { + let a = Vec2{x: 1.0, y: 7.5}; + let b = Vec2{x: -3.0, y: 2.5}; + let c = Vec2{x: -2.0, y: 10.0}; + + assert_eq!(a + b, c); + } + + #[test] + fn test_neg_vec2() { + let a = Vec2{x: 1.0, y: 7.5}; + let b = Vec2{x: -1.0, y: -7.5}; + + assert_eq!(-a, b); + } + + #[test] + fn test_sub_vec2() { + let a = Vec2{x: 1.0, y: 7.5}; + let b = Vec2{x: -3.0, y: 2.5}; + let c = Vec2{x: 4.0, y: 5.0}; + + assert_eq!(a - b, c); + } + + #[test] + fn test_norm_vec2() { + let a = Vec2{x: 2.0, y: 2.0}; + + assert!(f32::abs(a.norm() - 2.0) < 1e8); + } + + #[test] + fn test_norm2_vec2() { + let a = Vec2{x: 1.0, y: 2.0}; + + assert!(f32::abs(a.norm2() - 5.0) < 1e8); + } + + #[test] + fn test_normalized_vec2() { + let a = Vec2{x: 2.0, y: -2.0}; + let b = Vec2{x: std::f32::consts::FRAC_1_SQRT_2, y: -std::f32::consts::FRAC_1_SQRT_2}; + + assert_eq!(a.normalized(), b); + } + + #[test] + fn test_add_aabox_vec2() { + let a = Vec2{x: 1.0, y: 7.5}; + let b = Vec2{x: -3.0, y: 2.5}; + let mut aa_box = AABox{pos: a, size: b}; + let bb_box = AABox{pos: a + b,size: b}; + aa_box += b; + + assert_eq!(aa_box, bb_box); + } + + #[test] + fn test_sub_aabox_vec2() { + let a = Vec2{x: 1.0, y: 7.5}; + let b = Vec2{x: -3.0, y: 2.5}; + let mut aa_box = AABox{pos: a, size: b}; + let bb_box = AABox{pos: a - b,size: b}; + aa_box -= b; + + assert_eq!(aa_box, bb_box); + } + + #[test] + fn test_add_rbox_vec2() { + let a = Vec2{x: 1.0, y: 7.5}; + let b = Vec2{x: -3.0, y: 2.5}; + let c = Vec2{x: -3.0, y: 2.5}; + let mut aa_box = RBox{pos: a, v1: b, v2: c}; + let bb_box = RBox{pos: a + b, v1: b, v2: c}; + aa_box += b; + + assert_eq!(aa_box, bb_box); + } + + #[test] + fn test_sub_rbox_vec2() { + let a = Vec2{x: 1.0, y: 7.5}; + let b = Vec2{x: -3.0, y: 2.5}; + let c = Vec2{x: -3.0, y: 2.5}; + let mut aa_box = RBox{pos: a, v1: b, v2: c}; + let bb_box = RBox{pos: a - b, v1: b, v2: c}; + aa_box -= b; + + assert_eq!(aa_box, bb_box); + } +} diff --git a/webhogg/game_server/src/maths.rs b/webhogg/game_server/src/maths.rs deleted file mode 100644 index b9303af..0000000 --- a/webhogg/game_server/src/maths.rs +++ /dev/null @@ -1,370 +0,0 @@ -#[derive(Clone, Copy, Debug)] -pub struct Vec2 { - pub x: f32, - pub y: f32, -} - -impl std::ops::Add for Vec2 { - type Output = Self; - fn add(self, other: Self) -> Self { - Self { - x: self.x + other.x, - y: self.y + other.y - } - } -} - -impl std::ops::AddAssign for Vec2 { - fn add_assign(&mut self, other: Vec2) { - self.x += other.x; - self.y += other.y; - } -} - -impl std::ops::Sub for Vec2 { - type Output = Self; - fn sub(self, other: Self) -> Self { - Self { - x: self.x - other.x, - y: self.y - other.y - } - } -} - -impl std::ops::SubAssign for Vec2 { - fn sub_assign(&mut self, other: Vec2) { - self.x -= other.x; - self.y -= other.y; - } -} - -impl std::ops::Neg for Vec2 { - type Output = Self; - fn neg(self) -> Self { - Self { - x: -self.x, - y: -self.y - } - } -} - -impl std::ops::Mul for Vec2 { - type Output = Self; - fn mul(self, scale: f32) -> Self { - Self { - x: self.x * scale, - y: self.y * scale - } - } -} - -impl std::ops::Div for Vec2 { - type Output = Self; - fn div(self, scale: f32) -> Self { - Self { - x: self.x / scale, - y: self.y / scale - } - } -} - -impl std::ops::Div for Vec2 { - type Output = Self; - fn div(self, scale: Vec2) -> Self { - Self { - x: self.x / scale.x, - y: self.y / scale.y - } - } -} - - -impl std::cmp::PartialOrd for Vec2 { - fn partial_cmp(&self, other: &Self) -> Option { - if self.x <= other.x && self.y <= other.y { - Some(std::cmp::Ordering::Less) - } else if self.x > other.x && self.y > other.y { - Some(std::cmp::Ordering::Greater) - } else { - None - } - } -} - -impl std::cmp::PartialEq for Vec2 { - fn eq(&self, other: &Self) -> bool { - f32::abs(self.x - other.x) < 1e-8 - && f32::abs(self.y - other.y) < 1e-8 - } -} - -impl std::cmp::Eq for Vec2 {} - -impl Vec2 { - pub fn distance(&self) -> f32 { - f32::sqrt(self.distance2()) - } - - pub fn distance2(&self) -> f32 { - self.scalar(self) - } - - pub fn scalar(&self, other: &Vec2) -> f32 { - self.x * other.x + self.y * other.y - } - - pub fn norm(&self) -> Vec2 { - let len = self.distance(); - Vec2 { - x: self.x / len, - y: self.y / len, - } - } -} - -#[derive(Clone, Copy, Debug)] -pub struct AABox { - pub pos: Vec2, - /// the size may not be smaller than zero - pub size: Vec2, -} - -impl std::ops::Add for AABox { - type Output = Self; - fn add(self, other: Vec2) -> Self { - Self { - pos: self.pos + other, - size: self.size, - } - } -} - -impl std::ops::AddAssign for AABox { - fn add_assign(&mut self, other: Vec2) { - self.pos += other - } -} - -impl std::ops::Sub for AABox { - type Output = Self; - fn sub(self, other: Vec2) -> Self { - Self { - pos: self.pos + other, - size: self.size - } - } -} - -impl std::ops::SubAssign for AABox { - fn sub_assign(&mut self, other: Vec2) { - self.pos -= other - } -} - -impl std::cmp::PartialEq for AABox { - fn eq(&self, other: &Self) -> bool { - self.pos == other.pos - && self.size == other.size - } -} - -impl std::cmp::Eq for AABox {} - -#[derive(Clone, Copy, Debug)] -pub struct RBox { - /// origin - pub pos: Vec2, - /// Vwctor1 - pub v1: Vec2, - /// Vector2 - pub v2: Vec2, -} - -impl RBox { - pub fn new(pos: Vec2, orientation: Vec2, width: f32) -> Self { - let scale = width / orientation.distance(); - let orth = Vec2 {x: orientation.x / scale, y: -orientation.y / scale}; - Self { - pos: pos, - v1: orientation, - v2: orth, - } - } -} - -impl std::ops::Add for RBox { - type Output = Self; - fn add(self, other: Vec2) -> Self { - Self { - pos: self.pos + other, - v1: self.v1, - v2: self.v2, - } - } -} - -impl std::ops::AddAssign for RBox { - fn add_assign(&mut self, other: Vec2) { - self.pos += other - } -} - -impl std::ops::Sub for RBox { - type Output = Self; - fn sub(self, other: Vec2) -> Self { - Self { - pos: self.pos + other, - v1: self.v1 + other, - v2: self.v2, - } - } -} - -impl std::ops::SubAssign for RBox { - fn sub_assign(&mut self, other: Vec2) { - self.pos -= other - } -} - -impl std::cmp::PartialEq for RBox { - fn eq(&self, other: &Self) -> bool { - self.pos == other.pos - && self.v1 == other.v1 - && self.v1 == self.v2 - } -} - -impl std::cmp::Eq for RBox {} - -#[cfg(test)] -mod tests { - // Note this useful idiom: importing names from outer (for mod tests) scope. - use super::*; - - #[test] - fn test_less_vec2() { - let a = Vec2{x: 1.0, y: 7.5}; - let b = Vec2{x: -3.0, y: 2.5}; - - assert!(b < a); - } - - #[test] - fn test_less_vec2_fail() { - let a = Vec2{x: 1.0, y: 7.5}; - let b = Vec2{x: 3.0, y: 2.5}; - - assert!(!(a < b)); - } - - #[test] - fn test_greater_vec2() { - let a = Vec2{x: 1.0, y: 7.5}; - let b = Vec2{x: -3.0, y: 2.5}; - - assert!(a > b); - } - - #[test] - fn test_greater_vec2_fail() { - let a = Vec2{x: 1.0, y: 7.5}; - let b = Vec2{x: 3.0, y: 2.5}; - - assert!(!(a > b)); - } - - - #[test] - fn test_add_vec2() { - let a = Vec2{x: 1.0, y: 7.5}; - let b = Vec2{x: -3.0, y: 2.5}; - let c = Vec2{x: -2.0, y: 10.0}; - - assert_eq!(a + b, c); - } - - #[test] - fn test_neg_vec2() { - let a = Vec2{x: 1.0, y: 7.5}; - let b = Vec2{x: -1.0, y: -7.5}; - - assert_eq!(-a, b); - } - - #[test] - fn test_sub_vec2() { - let a = Vec2{x: 1.0, y: 7.5}; - let b = Vec2{x: -3.0, y: 2.5}; - let c = Vec2{x: 4.0, y: 5.0}; - - assert_eq!(a - b, c); - } - - #[test] - fn test_distance_vec2() { - let a = Vec2{x: 2.0, y: 2.0}; - - assert!(f32::abs(a.distance() - 2.0) < 1e8); - } - - #[test] - fn test_distance2_vec2() { - let a = Vec2{x: 1.0, y: 2.0}; - - assert!(f32::abs(a.distance2() - 5.0) < 1e8); - } - - #[test] - fn test_norm_vec2() { - let a = Vec2{x: 2.0, y: -2.0}; - let b = Vec2{x: std::f32::consts::FRAC_1_SQRT_2, y: -std::f32::consts::FRAC_1_SQRT_2}; - - assert_eq!(a.norm(), b); - } - - #[test] - fn test_add_aabox_vec2() { - let a = Vec2{x: 1.0, y: 7.5}; - let b = Vec2{x: -3.0, y: 2.5}; - let mut aa_box = AABox{pos: a, size: b}; - let bb_box = AABox{pos: a + b,size: b}; - aa_box += b; - - assert_eq!(aa_box, bb_box); - } - - #[test] - fn test_sub_aabox_vec2() { - let a = Vec2{x: 1.0, y: 7.5}; - let b = Vec2{x: -3.0, y: 2.5}; - let mut aa_box = AABox{pos: a, size: b}; - let bb_box = AABox{pos: a - b,size: b}; - aa_box -= b; - - assert_eq!(aa_box, bb_box); - } - - #[test] - fn test_add_rbox_vec2() { - let a = Vec2{x: 1.0, y: 7.5}; - let b = Vec2{x: -3.0, y: 2.5}; - let c = Vec2{x: -3.0, y: 2.5}; - let mut aa_box = RBox{pos: a, v1: b, v2: c}; - let bb_box = RBox{pos: a + b, v1: b, v2: c}; - aa_box += b; - - assert_eq!(aa_box, bb_box); - } - - #[test] - fn test_sub_rbox_vec2() { - let a = Vec2{x: 1.0, y: 7.5}; - let b = Vec2{x: -3.0, y: 2.5}; - let c = Vec2{x: -3.0, y: 2.5}; - let mut aa_box = RBox{pos: a, v1: b, v2: c}; - let bb_box = RBox{pos: a - b, v1: b, v2: c}; - aa_box -= b; - - assert_eq!(aa_box, bb_box); - } -} -- cgit v1.2.3