Skip to main content

cvmath/math/
dot.rs

1use crate::*;
2
3/// Types which can be dot producted.
4pub trait Dot: Copy {
5	type T;
6
7	fn dot(self, other: Self) -> Self::T;
8}
9
10/// Computes the dot product of `a` and `b`.
11///
12/// ```
13/// assert_eq!(12, cvmath::dot(cvmath::Vec3(1, 2, 3), cvmath::Vec3(4, -5, 6)));
14/// ```
15#[inline]
16pub fn dot<T: Dot>(a: T, b: T) -> T::T {
17	a.dot(b)
18}
19
20impl<T: Scalar> Dot for T {
21	type T = T;
22
23	#[inline]
24	fn dot(self, other: Self) -> Self::T {
25		self * other
26	}
27}
28
29macro_rules! impl_vec_dot {
30	($vec:ident) => {
31		impl<T: Scalar> Dot for $vec<T> {
32			type T = T;
33
34			#[inline]
35			fn dot(self, rhs: $vec<T>) -> T {
36				self.dot(rhs)
37			}
38		}
39	};
40}
41
42impl_vec_dot!(Vec2);
43impl_vec_dot!(Vec3);
44impl_vec_dot!(Vec4);
45
46impl<T: Scalar> Dot for Quat<T> {
47	type T = T;
48
49	#[inline]
50	fn dot(self, other: Quat<T>) -> T {
51		self.dot(other)
52	}
53}