loam_physics/collision/
vector_ops.rs1use std::ops::{Add, Mul, Neg, Sub};
2
3use glam::{Vec2, Vec3, Vec4};
4
5pub trait VectorOps:
6 Copy
7 + Add<Output = Self>
8 + Sub<Output = Self>
9 + Neg<Output = Self>
10 + Mul<f32, Output = Self>
11 + PartialEq
12{
13 fn zero() -> Self;
14 fn dot(self, rhs: Self) -> f32;
15 fn is_finite(self) -> bool;
16
17 fn length_squared(self) -> f32 {
18 self.dot(self)
19 }
20
21 fn length(self) -> f32 {
22 self.length_squared().sqrt()
23 }
24
25 fn normalize_or(self, fallback: Self) -> Self {
26 let l2 = self.length_squared();
27 if l2 > 1e-12 {
28 self * (1.0 / l2.sqrt())
29 } else {
30 fallback
31 }
32 }
33}
34
35impl VectorOps for Vec2 {
36 fn zero() -> Self {
37 Vec2::ZERO
38 }
39 fn dot(self, rhs: Self) -> f32 {
40 Vec2::dot(self, rhs)
41 }
42 fn is_finite(self) -> bool {
43 Vec2::is_finite(self)
44 }
45}
46
47impl VectorOps for Vec3 {
48 fn zero() -> Self {
49 Vec3::ZERO
50 }
51 fn dot(self, rhs: Self) -> f32 {
52 Vec3::dot(self, rhs)
53 }
54 fn is_finite(self) -> bool {
55 Vec3::is_finite(self)
56 }
57}
58
59impl VectorOps for Vec4 {
60 fn zero() -> Self {
61 Vec4::ZERO
62 }
63 fn dot(self, rhs: Self) -> f32 {
64 Vec4::dot(self, rhs)
65 }
66 fn is_finite(self) -> bool {
67 Vec4::is_finite(self)
68 }
69}