forked from dojoengine/origami
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec2.cairo
260 lines (218 loc) · 8.62 KB
/
vec2.cairo
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
use cubit::f128::types::fixed::{Fixed, FixedTrait, ONE_u128};
struct Vec2<T> {
x: T,
y: T
}
impl Vec2Copy<T, impl TCopy: Copy<T>> of Copy<Vec2<T>>;
impl Vec2Drop<T, impl TDrop: Drop<T>> of Drop<Vec2<T>>;
trait Vec2Trait<T> {
// Constructors
fn new(x: T, y: T) -> Vec2<T>;
fn splat(self: T) -> Vec2<T>;
// Masks
fn select(mask: Vec2<bool>, if_true: Vec2<T>, if_false: Vec2<T>) -> Vec2<T>;
// Math
fn dot<impl TMul: Mul<T>, impl TAdd: Add<T>>(self: Vec2<T>, rhs: Vec2<T>) -> T;
fn dot_into_vec<impl TMul: Mul<T>, impl TAdd: Add<T>>(self: Vec2<T>, rhs: Vec2<T>) -> Vec2<T>;
// Swizzles
fn xy(self: Vec2<T>) -> Vec2<T>;
fn xx(self: Vec2<T>) -> Vec2<T>;
fn yx(self: Vec2<T>) -> Vec2<T>;
fn yy(self: Vec2<T>) -> Vec2<T>;
}
impl Vec2Impl<T, impl TCopy: Copy<T>, impl TDrop: Drop<T>> of Vec2Trait<T> {
// Constructors
/// Creates a new vector.
#[inline(always)]
fn new(x: T, y: T) -> Vec2<T> {
Vec2 { x: x, y: y }
}
/// Creates a vector with all elements set to `v`.
#[inline(always)]
fn splat(self: T) -> Vec2<T> {
Vec2 { x: self, y: self }
}
// Masks
/// Creates a vector from the elements in `if_true` and `if_false`,
/// selecting which to use for each element of `self`.
///
/// A true element in the mask uses the corresponding element from
/// `if_true`, and false uses the element from `if_false`.
#[inline(always)]
fn select(mask: Vec2<bool>, if_true: Vec2<T>, if_false: Vec2<T>) -> Vec2<T> {
Vec2 {
x: if mask.x {
if_true.x
} else {
if_false.x
},
y: if mask.y {
if_true.y
} else {
if_false.y
},
}
}
// Math
/// Computes the dot product of `self` and `rhs` .
// #[inline(always)] is not allowed for functions with impl generic parameters.
fn dot<impl TMul: Mul<T>, impl TAdd: Add<T>>(self: Vec2<T>, rhs: Vec2<T>) -> T {
(self.x * rhs.x) + (self.y * rhs.y)
}
/// Returns a vector where every component is the dot product
/// of `self` and `rhs`.
fn dot_into_vec<impl TMul: Mul<T>, impl TAdd: Add<T>>(self: Vec2<T>, rhs: Vec2<T>) -> Vec2<T> {
Vec2Trait::splat(Vec2Trait::dot(self, rhs))
}
// Swizzles
/// Vec2<T> -> Vec2<T>
#[inline(always)]
fn xx(self: Vec2<T>) -> Vec2<T> {
Vec2 { x: self.x, y: self.x, }
}
#[inline(always)]
fn xy(self: Vec2<T>) -> Vec2<T> {
Vec2 { x: self.x, y: self.y, }
}
#[inline(always)]
fn yx(self: Vec2<T>) -> Vec2<T> {
Vec2 { x: self.y, y: self.x, }
}
#[inline(always)]
fn yy(self: Vec2<T>) -> Vec2<T> {
Vec2 { x: self.y, y: self.y, }
}
}
#[cfg(test)]
mod tests {
// Local imports
use super::{FixedTrait, ONE_u128, Vec2Trait};
#[test]
fn test_new() {
let var1_pos = FixedTrait::new(ONE_u128, false);
let var2_neg = FixedTrait::new(2 * ONE_u128, true);
// with Fixed type
let vec2 = Vec2Trait::new(var1_pos, var2_neg);
assert(vec2.x.mag == ONE_u128, 'invalid x.mag');
assert(vec2.x.sign == false, 'invalid x.sign');
assert(vec2.y.mag == 2 * ONE_u128, 'invalid y.mag');
assert(vec2.y.sign == true, 'invalid y.sign');
// with bool
let bvec2tf = Vec2Trait::new(true, false);
assert(bvec2tf.x == true, 'invalid new x');
assert(bvec2tf.y == false, 'invalid new y');
}
#[test]
fn test_splat() {
let var = FixedTrait::new(ONE_u128, false);
// with Fixed type
let vec2 = Vec2Trait::splat(var);
assert(vec2.x.mag == ONE_u128, 'invalid x.mag');
assert(vec2.x.sign == false, 'invalid x.sign');
assert(vec2.y.mag == ONE_u128, 'invalid y.mag');
assert(vec2.y.sign == false, 'invalid y.sign');
// with bool
let bvec2tt = Vec2Trait::splat(true);
assert(bvec2tt.x == true, 'invalid x');
assert(bvec2tt.y == true, 'invalid y');
}
#[test]
fn test_select() {
let var1_pos = FixedTrait::new(ONE_u128, false);
let var2_neg = FixedTrait::new(2 * ONE_u128, true);
let var3_neg = FixedTrait::new(3 * ONE_u128, true);
let var4_pos = FixedTrait::new(4 * ONE_u128, false);
let vec2a = Vec2Trait::new(var1_pos, var2_neg);
let vec2b = Vec2Trait::new(var3_neg, var4_pos);
let mask = Vec2Trait::new(true, false);
let vec2 = Vec2Trait::select(mask, vec2a, vec2b);
assert(vec2.x.mag == ONE_u128, 'invalid x.mag');
assert(vec2.x.sign == false, 'invalid x.sign');
assert(vec2.y.mag == 4 * ONE_u128, 'invalid y.mag');
assert(vec2.y.sign == false, 'invalid y.sign');
let mask = Vec2Trait::new(false, true);
let vec2 = Vec2Trait::select(mask, vec2a, vec2b);
assert(vec2.x.mag == 3 * ONE_u128, 'invalid x.mag');
assert(vec2.x.sign == true, 'invalid x.sign');
assert(vec2.y.mag == 2 * ONE_u128, 'invalid y.mag');
assert(vec2.y.sign == true, 'invalid y.sign');
}
#[test]
fn test_dot() {
let var1_pos = FixedTrait::new(ONE_u128, false);
let var2_neg = FixedTrait::new(2 * ONE_u128, true);
let var3_neg = FixedTrait::new(3 * ONE_u128, true);
let var4_pos = FixedTrait::new(4 * ONE_u128, false);
let vec2a = Vec2Trait::new(var1_pos, var2_neg);
let vec2b = Vec2Trait::new(var3_neg, var4_pos);
let a_dot_b = vec2a.dot(vec2b);
assert(a_dot_b.mag == 11 * ONE_u128, 'invalid mag');
assert(a_dot_b.sign == true, 'invalid sign');
let a_dot_b = Vec2Trait::dot(vec2a, vec2b); // alt syntax
assert(a_dot_b.mag == 11 * ONE_u128, 'invalid mag');
assert(a_dot_b.sign == true, 'invalid sign');
}
#[test]
fn test_dot_into_vec() {
let var1_pos = FixedTrait::new(ONE_u128, false);
let var2_neg = FixedTrait::new(2 * ONE_u128, true);
let var3_neg = FixedTrait::new(3 * ONE_u128, true);
let var4_pos = FixedTrait::new(4 * ONE_u128, false);
let vec2a = Vec2Trait::new(var1_pos, var2_neg);
let vec2b = Vec2Trait::new(var3_neg, var4_pos);
let vec2 = vec2a.dot_into_vec(vec2b);
assert(vec2.x.mag == 11 * ONE_u128, 'invalid x.mag');
assert(vec2.x.sign == true, 'invalid x.sign');
assert(vec2.y.mag == 11 * ONE_u128, 'invalid y.mag');
assert(vec2.y.sign == true, 'invalid y.sign');
let vec2 = Vec2Trait::dot_into_vec(vec2a, vec2b); // alt syntax
assert(vec2.x.mag == 11 * ONE_u128, 'invalid x.mag');
assert(vec2.x.sign == true, 'invalid x.sign');
assert(vec2.y.mag == 11 * ONE_u128, 'invalid y.mag');
assert(vec2.y.sign == true, 'invalid y.sign');
}
#[test]
fn test_xx() {
let var1_pos = FixedTrait::new(ONE_u128, false);
let var2_neg = FixedTrait::new(2 * ONE_u128, true);
let vec2 = Vec2Trait::new(var1_pos, var2_neg);
let vec2xx = vec2.xx();
assert(vec2xx.x.mag == ONE_u128, 'invalid x.mag');
assert(vec2xx.x.sign == false, 'invalid x.sign');
assert(vec2xx.y.mag == ONE_u128, 'invalid y.mag');
assert(vec2xx.y.sign == false, 'invalid y.sign');
}
#[test]
fn test_xy() {
let var1_pos = FixedTrait::new(ONE_u128, false);
let var2_neg = FixedTrait::new(2 * ONE_u128, true);
let vec2 = Vec2Trait::new(var1_pos, var2_neg);
let vec2xy = vec2.xy();
assert(vec2xy.x.mag == ONE_u128, 'invalid x.mag');
assert(vec2xy.x.sign == false, 'invalid x.sign');
assert(vec2xy.y.mag == 2 * ONE_u128, 'invalid xy.mag');
assert(vec2xy.y.sign == true, 'invalid y.sign');
}
#[test]
fn test_yx() {
let var1_pos = FixedTrait::new(ONE_u128, false);
let var2_neg = FixedTrait::new(2 * ONE_u128, true);
let vec2 = Vec2Trait::new(var1_pos, var2_neg);
let vec2yx = vec2.yx();
assert(vec2yx.x.mag == 2 * ONE_u128, 'invalid x.mag');
assert(vec2yx.x.sign == true, 'invalid x.sign');
assert(vec2yx.y.mag == ONE_u128, 'invalid y.mag');
assert(vec2yx.y.sign == false, 'invalid y.sign');
}
#[test]
fn test_yy() {
let var1_pos = FixedTrait::new(ONE_u128, false);
let var2_neg = FixedTrait::new(2 * ONE_u128, true);
let vec2 = Vec2Trait::new(var1_pos, var2_neg);
let vec2yy = vec2.yy();
assert(vec2yy.x.mag == 2 * ONE_u128, 'invalid x.mag');
assert(vec2yy.x.sign == true, 'invalid x.sign');
assert(vec2yy.y.mag == 2 * ONE_u128, 'invalid y.mag');
assert(vec2yy.y.sign == true, 'invalid y.sign');
}
}