Refactor loading
This commit is contained in:
+1
-1
@@ -1,3 +1,3 @@
|
||||
pub mod int;
|
||||
pub mod segment;
|
||||
pub mod vec;
|
||||
pub mod vec;
|
||||
|
||||
+20
-9
@@ -11,15 +11,13 @@ use crate::math::{
|
||||
#[derive(Clone, Copy, PartialEq, Reflect)]
|
||||
pub struct FGSegment2d {
|
||||
point1: FGVec2,
|
||||
point2: FGVec2
|
||||
point2: FGVec2,
|
||||
}
|
||||
|
||||
impl FGSegment2d {
|
||||
#[inline]
|
||||
pub fn new(point1: FGVec2, point2: FGVec2) -> Self {
|
||||
Self {
|
||||
point1, point2
|
||||
}
|
||||
Self { point1, point2 }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@@ -41,7 +39,8 @@ impl FGSegment2d {
|
||||
/// Panics if a valid direction could not be computed, for example when the endpoints are coincident, NaN, or infinite.
|
||||
#[inline]
|
||||
pub fn direction(&self) -> FGVec2 {
|
||||
self.try_direction().expect("Failed to compute the direction of a line segment")
|
||||
self.try_direction()
|
||||
.expect("Failed to compute the direction of a line segment")
|
||||
}
|
||||
|
||||
/// Try to compute the normalized direction pointing from the first endpoint to the second endpoint.
|
||||
@@ -100,7 +99,10 @@ impl FGSegment2d {
|
||||
///
|
||||
/// Panics if a valid normal could not be computed, for example when the endpoints are coincident
|
||||
pub fn left_normal(&self) -> FGVec2 {
|
||||
self.point1.direction_to(self.point2).map(|o| { FGVec2::new(-o.y, o.x)}).expect("a valid normal could not be computed")
|
||||
self.point1
|
||||
.direction_to(self.point2)
|
||||
.map(|o| FGVec2::new(-o.y, o.x))
|
||||
.expect("a valid normal could not be computed")
|
||||
}
|
||||
|
||||
pub fn segment_intersection(&self, rhs: &FGSegment2d) -> Option<FGVec2> {
|
||||
@@ -150,9 +152,18 @@ mod tests {
|
||||
fn closest_point_projects_and_clamps_to_segment() {
|
||||
let segment = FGSegment2d::new(FGVec2::lit("-4", "0"), FGVec2::lit("4", "0"));
|
||||
|
||||
assert_eq!(segment.closest_point(FGVec2::lit("2", "1")), FGVec2::lit("2", "0"));
|
||||
assert_eq!(segment.closest_point(FGVec2::lit("-6", "1")), segment.point1());
|
||||
assert_eq!(segment.closest_point(FGVec2::lit("6", "1")), segment.point2());
|
||||
assert_eq!(
|
||||
segment.closest_point(FGVec2::lit("2", "1")),
|
||||
FGVec2::lit("2", "0")
|
||||
);
|
||||
assert_eq!(
|
||||
segment.closest_point(FGVec2::lit("-6", "1")),
|
||||
segment.point1()
|
||||
);
|
||||
assert_eq!(
|
||||
segment.closest_point(FGVec2::lit("6", "1")),
|
||||
segment.point2()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
+29
-10
@@ -2,7 +2,7 @@ use bevy::math::Vec2;
|
||||
use bevy::prelude::*;
|
||||
use fixed::types::I32F32;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::ops::{AddAssign, Add, Div, Mul, Sub};
|
||||
use std::ops::{Add, AddAssign, Div, Mul, Sub};
|
||||
|
||||
use super::int::{FGWide, FGi32};
|
||||
|
||||
@@ -29,7 +29,10 @@ impl FGVec2 {
|
||||
|
||||
#[inline(always)]
|
||||
pub const fn lit(x: &str, y: &str) -> Self {
|
||||
Self { x: FGi32::lit(x), y: FGi32::lit(y) }
|
||||
Self {
|
||||
x: FGi32::lit(x),
|
||||
y: FGi32::lit(y),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
@@ -213,7 +216,7 @@ impl AddAssign for FGVec2 {
|
||||
fn add_assign(&mut self, rhs: Self) {
|
||||
*self = Self {
|
||||
x: self.x + rhs.x,
|
||||
y: self.y + rhs.y
|
||||
y: self.y + rhs.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -251,8 +254,12 @@ mod is_normalized_tests {
|
||||
let x = mag * theta.cos();
|
||||
let y = mag * theta.sin();
|
||||
|
||||
let Some(fx) = FGi32::checked_from_num(x) else { continue };
|
||||
let Some(fy) = FGi32::checked_from_num(y) else { continue };
|
||||
let Some(fx) = FGi32::checked_from_num(x) else {
|
||||
continue;
|
||||
};
|
||||
let Some(fy) = FGi32::checked_from_num(y) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
let v = FGVec2 { x: fx, y: fy };
|
||||
|
||||
@@ -301,24 +308,36 @@ mod is_normalized_tests {
|
||||
for &mag in &[0.01, 1.0, 100.0, 10000.0] {
|
||||
let x = mag * theta.cos();
|
||||
let y = mag * theta.sin();
|
||||
let Some(fx) = FGi32::checked_from_num(x) else { continue };
|
||||
let Some(fy) = FGi32::checked_from_num(y) else { continue };
|
||||
let Some(fx) = FGi32::checked_from_num(x) else {
|
||||
continue;
|
||||
};
|
||||
let Some(fy) = FGi32::checked_from_num(y) else {
|
||||
continue;
|
||||
};
|
||||
let v = FGVec2 { x: fx, y: fy };
|
||||
|
||||
if let Some(normalized) = v.normalize() {
|
||||
assert!(
|
||||
normalized.is_normalized(),
|
||||
"normalized({}, {}) failed is_normalized(): length_squared = {:?}",
|
||||
x, y, normalized.length_squared()
|
||||
x,
|
||||
y,
|
||||
normalized.length_squared()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let non_unit = FGVec2 { x: FGi32::from_num(2.0), y: FGi32::from_num(0.0) };
|
||||
let non_unit = FGVec2 {
|
||||
x: FGi32::from_num(2.0),
|
||||
y: FGi32::from_num(0.0),
|
||||
};
|
||||
assert!(!non_unit.is_normalized());
|
||||
|
||||
let non_unit_small = FGVec2 { x: FGi32::from_num(0.5), y: FGi32::from_num(0.0) };
|
||||
let non_unit_small = FGVec2 {
|
||||
x: FGi32::from_num(0.5),
|
||||
y: FGi32::from_num(0.0),
|
||||
};
|
||||
assert!(!non_unit_small.is_normalized());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user