1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use core::ops::{Add, Sub, Mul, Div, Rem, Neg};
use crate::scalar::*;
macro_rules! implVecScalar {
($vecName:ident, $scalar:ident) => {
impl Mul<$vecName<$scalar>> for $scalar {
type Output = $vecName<$scalar>;
fn mul(self, rhs: $vecName<$scalar>) -> Self::Output {
$vecName::mulVF(&rhs, self)
}
}
}
}
macro_rules! implVector {
($vecName:ident, $($field:ident)*) => {
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct $vecName<T> { $(pub $field: T),* }
impl<T: Scalar> $vecName<T> {
pub fn new($($field:T),*) -> Self { Self { $($field: $field),* } }
pub fn zero() -> Self { Self { $($field: T::zero()),* } }
pub fn length(&self) -> T { Self::dot(&self, &self).tsqrt() }
pub fn dot (l: &Self, r: &Self) -> T { $(l.$field * r.$field +)* T::zero() }
pub fn addVV(l: &Self, r: &Self) -> Self { Self::new($(l.$field + r.$field),*) }
pub fn subVV(l: &Self, r: &Self) -> Self { Self::new($(l.$field - r.$field),*) }
pub fn mulVV(l: &Self, r: &Self) -> Self { Self::new($(l.$field * r.$field),*) }
pub fn divVV(l: &Self, r: &Self) -> Self { Self::new($(l.$field / r.$field),*) }
pub fn mulVF(l: &Self, r: T) -> Self { Self::new($(l.$field * r),*) }
pub fn divVF(l: &Self, r: T) -> Self { Self::new($(l.$field / r),*) }
pub fn remVV(l: &Self, r: &Self) -> Self { Self::new($(l.$field % r.$field),*) }
pub fn normalize(v: &Self) -> Self { let len = v.length(); *v / len }
pub fn distance (l: &Self, r: &Self) -> T { (*r - *l).length() }
pub fn min(l: &Self, r: &Self) -> Self { Self::new($(T::min(l.$field, r.$field)),*) }
pub fn max(l: &Self, r: &Self) -> Self { Self::new($(T::max(l.$field, r.$field)),*) }
}
impl<T> Add for $vecName<T> where T: Scalar {
type Output = $vecName<T>;
fn add(self, rhs: Self) -> Self::Output {
Self { $($field: self.$field + rhs.$field),* }
}
}
impl<T> Sub for $vecName<T> where T: Scalar {
type Output = $vecName<T>;
fn sub(self, rhs: Self) -> Self::Output {
Self { $($field: self.$field - rhs.$field),* }
}
}
impl<T> Mul for $vecName<T> where T: Scalar {
type Output = $vecName<T>;
fn mul(self, rhs: Self) -> Self::Output {
Self { $($field: self.$field * rhs.$field),* }
}
}
impl<T> Mul<T> for $vecName<T> where T:Scalar {
type Output = $vecName<T>;
fn mul(self, rhs: T) -> Self::Output {
Self { $($field: self.$field * rhs),* }
}
}
implVecScalar!($vecName, f32);
implVecScalar!($vecName, f64);
implVecScalar!($vecName, i32);
implVecScalar!($vecName, i64);
impl<T> Div for $vecName<T> where T:Scalar {
type Output = $vecName<T>;
fn div(self, rhs: Self) -> Self::Output {
Self { $($field: self.$field / rhs.$field),* }
}
}
impl<T> Div<T> for $vecName<T> where T:Scalar {
type Output = $vecName<T>;
fn div(self, rhs: T) -> Self::Output {
Self { $($field: self.$field / rhs),* }
}
}
impl<T> Rem for $vecName<T> where T: Scalar {
type Output = $vecName<T>;
fn rem(self, rhs: $vecName<T>) -> Self::Output {
Self { $($field: self.$field % rhs.$field),* }
}
}
impl<T> Rem<T> for $vecName<T> where T:Scalar {
type Output = $vecName<T>;
fn rem(self, rhs: T) -> Self::Output {
Self { $($field: self.$field % rhs),* }
}
}
impl<T: Scalar> Neg for $vecName<T> {
type Output = $vecName<T>;
fn neg(self) -> Self::Output {
Self { $($field: -self.$field),* }
}
}
};
}
pub trait CrossProduct {
fn cross(l: &Self, r: &Self) -> Self;
}
implVector!(Vector2, x y);
implVector!(Vector3, x y z);
implVector!(Vector4, x y z w);
impl<T> CrossProduct for Vector3<T> where T : Scalar {
fn cross(l: &Vector3<T>, r: &Vector3<T>) -> Vector3<T> {
Vector3::new(l.y * r.z - l.z * r.y, l.z * r.x - l.x * r.z, l.x * r.y - l.y * r.x)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
pub fn test() {
let f1 = Vector2 { x: 1.0, y: 2.0 };
let f2 = Vector2 { x: 3.0, y: 4.0 };
let out = f1 + f2;
assert_eq!(out.x, 4.0);
assert_eq!(out.y, 6.0);
let f22 : Vector2<f32> = 2.0 * f2;
let f23 : Vector2<f32> = f2 * 2.0;
assert_eq!(f22.x, 6.0);
assert_eq!(f22.y, 8.0);
assert_eq!(f23.x, f22.x);
assert_eq!(f23.y, f22.y);
}
}