octoon_math/
vec.rs

1use std::ops::{Add, Sub, Mul, Div, Rem, Neg, Not, BitAnd, BitOr, BitXor};
2use std::ops::{AddAssign, SubAssign, MulAssign, DivAssign};
3use std::{f32, f64};
4use std::fmt::Debug;
5
6use crate::consts::*;
7
8pub trait Vec:
9	Debug + Copy + Clone
10	+ Add<Self, Output = Self>
11	+ Sub<Self, Output = Self>
12	+ Mul<Self, Output = Self>
13	+ Div<Self, Output = Self>
14	+ Rem<Self, Output = Self>
15	+ AddAssign<Self>
16	+ SubAssign<Self>
17	+ MulAssign<Self>
18	+ DivAssign<Self>
19	+ Neg<Output = Self>
20	+ Cmp
21	+ One + Two + Zero + OneHalf
22{
23}
24
25impl Vec for f32 {}
26impl Vec for f64 {}
27
28pub trait Math: Sized
29{
30	fn abs(self) -> Self;
31	fn recip(self) -> Self;
32	fn sqrt(self) -> Self;
33	fn rsqrt(self) -> Self;
34	fn sin(self) -> Self;
35	fn cos(self) -> Self;
36	fn tan(self) -> Self;
37	fn sincos(self) -> (Self, Self);
38	fn acos(self) -> Self;
39	fn asin(self) -> Self;
40	fn atan(self) -> Self;
41	fn exp(self) -> Self;
42	fn exp2(self) -> Self;
43	fn log(self, rhs: Self) -> Self;
44	fn log2(self) -> Self;
45	fn log10(self) -> Self;
46	fn to_radians(self) -> Self;
47	fn to_degrees(self) -> Self;
48	fn min(self, rhs: Self) -> Self;
49	fn max(self, rhs: Self) -> Self;
50	fn saturate(self) -> Self;
51	fn snorm2unorm(self) -> Self;
52	fn unorm2snorm(self) -> Self;
53	fn clamp(self, minval: Self, maxval: Self) -> Self;
54}
55
56impl Math for f32 
57{
58	#[inline(always)] fn abs(self) -> Self { f32::abs(self) }
59	#[inline(always)] fn recip(self) -> Self { f32::recip(self) }
60	#[inline(always)] fn sqrt(self) -> Self { f32::sqrt(self) }
61	#[inline(always)] fn rsqrt(self) -> Self { f32::recip(f32::sqrt(self)) }
62	#[inline(always)] fn sin(self) -> Self { f32::sin(self) }
63	#[inline(always)] fn cos(self) -> Self { f32::cos(self) }
64	#[inline(always)] fn tan(self) -> Self { f32::tan(self) }
65	#[inline(always)] fn sincos(self) -> (f32, f32) { f32::sin_cos(self) }
66	#[inline(always)] fn acos(self) -> Self { f32::acos(self) }
67	#[inline(always)] fn asin(self) -> Self { f32::asin(self) }
68	#[inline(always)] fn atan(self) -> Self { f32::atan(self) }
69	#[inline(always)] fn exp(self) -> Self { f32::exp(self) }
70	#[inline(always)] fn exp2(self) -> Self { f32::exp2(self) }
71	#[inline(always)] fn log(self, y:f32) -> Self { f32::log(self, y) }
72	#[inline(always)] fn log2(self) -> Self { f32::log2(self) }
73	#[inline(always)] fn log10(self) -> Self { f32::log10(self) }
74	#[inline(always)] fn to_radians(self) -> Self { f32::to_radians(self) }
75	#[inline(always)] fn to_degrees(self) -> Self { f32::to_degrees(self) }
76	#[inline(always)] fn min(self, y: f32) -> Self { f32::min(self, y) }
77	#[inline(always)] fn max(self, y: f32) -> Self { f32::max(self, y) }
78	#[inline(always)] fn saturate(self) -> Self { f32::min(1.0, f32::max(0.0, self)) }
79	#[inline(always)] fn snorm2unorm(self) -> Self { self * 0.5 + 0.5 }	
80	#[inline(always)] fn unorm2snorm(self) -> Self { self * 2.0 + 1.0 }
81	#[inline(always)] fn clamp(self, minval: f32, maxval: f32) -> Self { f32::min(maxval, f32::max(minval, self)) }
82}
83
84impl Math for f64 
85{
86	#[inline(always)] fn abs(self) -> Self { f64::abs(self) }
87	#[inline(always)] fn recip(self) -> Self { f64::recip(self) }
88	#[inline(always)] fn sqrt(self) -> Self { f64::sqrt(self) }
89	#[inline(always)] fn rsqrt(self) -> Self { f64::recip(f64::sqrt(self)) }
90	#[inline(always)] fn sin(self) -> Self { f64::sin(self) }
91	#[inline(always)] fn cos(self) -> Self { f64::cos(self) }
92	#[inline(always)] fn tan(self) -> Self { f64::tan(self) }
93	#[inline(always)] fn sincos(self) -> (f64, f64) { f64::sin_cos(self) }
94	#[inline(always)] fn acos(self) -> Self { f64::acos(self) }
95	#[inline(always)] fn asin(self) -> Self { f64::asin(self) }
96	#[inline(always)] fn atan(self) -> Self { f64::atan(self) }
97	#[inline(always)] fn exp(self) -> Self { f64::exp(self) }
98	#[inline(always)] fn exp2(self) -> Self { f64::exp2(self) }
99	#[inline(always)] fn log(self, y:f64) -> Self { f64::log(self, y) }
100	#[inline(always)] fn log2(self) -> Self { f64::log2(self) }
101	#[inline(always)] fn log10(self) -> Self { f64::log10(self) }
102	#[inline(always)] fn to_radians(self) -> Self { f64::to_radians(self) }
103	#[inline(always)] fn to_degrees(self) -> Self { f64::to_degrees(self) }
104	#[inline(always)] fn min(self, y: f64) -> Self { f64::min(self, y) }
105	#[inline(always)] fn max(self, y: f64) -> Self { f64::max(self, y) }
106	#[inline(always)] fn saturate(self) -> Self { f64::min(1.0, f64::max(0.0, self)) }
107	#[inline(always)] fn snorm2unorm(self) -> Self { self * 0.5 + 0.5 }	
108	#[inline(always)] fn unorm2snorm(self) -> Self { self * 2.0 + 1.0 }
109	#[inline(always)] fn clamp(self, minval: f64, maxval: f64) -> Self { f64::min(maxval, f64::max(minval, self)) }
110}
111
112pub trait Interpolation<T> 
113{
114	fn lerp(self, rhs: Self, t:T) -> Self; 
115}
116
117impl Interpolation<f32> for f32
118{
119	#[inline(always)]
120    fn lerp(self, b: Self, t: f32) -> Self 
121    {
122        return self * (1.0 - t) + b * t;
123    }
124}
125
126impl Interpolation<f64> for f64
127{
128	#[inline(always)]
129    fn lerp(self, b: Self, t: Self) -> Self 
130    {
131        return self * (1.0 - t) + b * t;
132    }
133}
134
135pub trait Cmp 
136{
137    type Bool: Copy
138               + Not<Output = Self::Bool>
139               + BitAnd<Self::Bool, Output = Self::Bool>
140               + BitOr<Self::Bool, Output = Self::Bool>
141               + BitXor<Self::Bool, Output = Self::Bool>;
142
143    fn eq(self, rhs: Self) -> bool;
144    fn ne(self, rhs: Self) -> bool;
145    fn gt(self, rhs: Self) -> bool;
146    fn lt(self, rhs: Self) -> bool;
147    fn ge(self, rhs: Self) -> bool;
148    fn le(self, rhs: Self) -> bool;
149}
150
151impl Cmp for f32 
152{
153    type Bool = bool;
154
155    #[inline(always)] fn eq(self, rhs: Self) -> bool { self == rhs }
156    #[inline(always)] fn ne(self, rhs: Self) -> bool { self != rhs }
157    #[inline(always)] fn gt(self, rhs: Self) -> bool { self > rhs }
158    #[inline(always)] fn lt(self, rhs: Self) -> bool { self < rhs }
159    #[inline(always)] fn ge(self, rhs: Self) -> bool { self >= rhs }
160    #[inline(always)] fn le(self, rhs: Self) -> bool { self <= rhs }
161}
162
163impl Cmp for f64 
164{
165    type Bool = bool;
166
167    #[inline(always)] fn eq(self, rhs: Self) -> bool { self == rhs }
168    #[inline(always)] fn ne(self, rhs: Self) -> bool { self != rhs }
169    #[inline(always)] fn gt(self, rhs: Self) -> bool { self > rhs }
170    #[inline(always)] fn lt(self, rhs: Self) -> bool { self < rhs }
171    #[inline(always)] fn ge(self, rhs: Self) -> bool { self >= rhs }
172    #[inline(always)] fn le(self, rhs: Self) -> bool { self <= rhs }
173}