1use std::ops::{Add, AddAssign, Div, Mul, Sub, SubAssign};
2
3use bytemuck::{Pod, Zeroable};
4use num_traits::ToPrimitive;
5
6#[repr(C)]
7#[derive(Clone, Copy, Default, Pod, Zeroable, Debug)]
8pub struct Vector2 {
9 pub x: f32,
10 pub y: f32,
11}
12
13impl Vector2 {
14 pub fn new<T: ToPrimitive>(x: T, y: T) -> Self {
15 Self {
16 x: x.to_f32().unwrap_or(0.0),
17 y: y.to_f32().unwrap_or(0.0),
18 }
19 }
20
21 pub fn length(&self) -> f32 {
22 (self.x * self.x + self.y * self.y).sqrt()
23 }
24
25 pub fn normalize(&self) -> Self {
26 let length = self.length();
27 Self {
28 x: self.x / length,
29 y: self.y / length,
30 }
31 }
32
33 pub fn dot(&self, other: &Self) -> f32 {
34 self.x * other.x + self.y * other.y
35 }
36
37 pub fn angle(&self, other: &Self) -> f32 {
38 let dot = self.dot(other);
39 let len1 = self.length();
40 let len2 = other.length();
41 (dot / (len1 * len2)).acos()
42 }
43
44 pub fn into_vector3(&self) -> Vector3 {
45 Vector3 {
46 x: self.x,
47 y: self.y,
48 z: 0.0,
49 }
50 }
51
52 pub const ZERO: Self = Self { x: 0.0, y: 0.0 };
53 pub const ONE: Self = Self { x: 1.0, y: 1.0 };
54 pub const UP: Self = Self { x: 0.0, y: 1.0 };
55 pub const DOWN: Self = Self { x: 0.0, y: -1.0 };
56 pub const LEFT: Self = Self { x: -1.0, y: 0.0 };
57 pub const RIGHT: Self = Self { x: 1.0, y: 0.0 };
58}
59
60impl From<[f32; 2]> for Vector2 {
61 fn from(array: [f32; 2]) -> Self {
62 Self {
63 x: array[0],
64 y: array[1],
65 }
66 }
67}
68
69impl From<(f32, f32)> for Vector2 {
70 fn from(tuple: (f32, f32)) -> Self {
71 Self {
72 x: tuple.0,
73 y: tuple.1,
74 }
75 }
76}
77
78impl From<Vector3> for Vector2 {
79 fn from(vector: Vector3) -> Self {
80 Self {
81 x: vector.x,
82 y: vector.y,
83 }
84 }
85}
86
87impl From<(f32, f32, f32)> for Vector2 {
88 fn from(tuple: (f32, f32, f32)) -> Self {
89 Self {
90 x: tuple.0,
91 y: tuple.1,
92 }
93 }
94}
95
96impl From<(u32, u32)> for Vector2 {
97 fn from(tuple: (u32, u32)) -> Self {
98 Self {
99 x: tuple.0 as f32,
100 y: tuple.1 as f32,
101 }
102 }
103}
104
105impl From<(i32, i32)> for Vector2 {
106 fn from(tuple: (i32, i32)) -> Self {
107 Self {
108 x: tuple.0 as f32,
109 y: tuple.1 as f32,
110 }
111 }
112}
113
114impl Add for Vector2 {
115 type Output = Self;
116
117 fn add(self, other: Self) -> Self {
118 Self {
119 x: self.x + other.x,
120 y: self.y + other.y,
121 }
122 }
123}
124
125impl Sub for Vector2 {
126 type Output = Self;
127
128 fn sub(self, other: Self) -> Self {
129 Self {
130 x: self.x - other.x,
131 y: self.y - other.y,
132 }
133 }
134}
135
136impl Sub<f32> for Vector2 {
137 type Output = Self;
138
139 fn sub(self, scalar: f32) -> Self {
140 Self {
141 x: self.x - scalar,
142 y: self.y - scalar,
143 }
144 }
145}
146
147impl Mul<Vector2> for f32 {
148 type Output = Vector2;
149
150 fn mul(self, vector: Vector2) -> Vector2 {
151 Vector2 {
152 x: self * vector.x,
153 y: self * vector.y,
154 }
155 }
156}
157
158impl Div<Vector2> for f32 {
159 type Output = Vector2;
160
161 fn div(self, vector: Vector2) -> Vector2 {
162 Vector2 {
163 x: self / vector.x,
164 y: self / vector.y,
165 }
166 }
167}
168
169impl Mul<f32> for Vector2 {
170 type Output = Self;
171
172 fn mul(self, scalar: f32) -> Self {
173 Self {
174 x: self.x * scalar,
175 y: self.y * scalar,
176 }
177 }
178}
179
180impl Div<f32> for Vector2 {
181 type Output = Self;
182
183 fn div(self, scalar: f32) -> Self {
184 Self {
185 x: self.x / scalar,
186 y: self.y / scalar,
187 }
188 }
189}
190
191impl Div<Vector2> for Vector2 {
192 type Output = Self;
193
194 fn div(self, other: Self) -> Self {
195 Self {
196 x: self.x / other.x,
197 y: self.y / other.y,
198 }
199 }
200}
201
202impl AddAssign for Vector2 {
203 fn add_assign(&mut self, other: Self) {
204 *self = *self + other;
205 }
206}
207
208impl SubAssign for Vector2 {
209 fn sub_assign(&mut self, other: Self) {
210 *self = *self - other;
211 }
212}
213
214impl PartialEq for Vector2 {
215 fn eq(&self, other: &Self) -> bool {
216 self.x == other.x && self.y == other.y
217 }
218}
219
220impl Eq for Vector2 {}
221
222#[repr(C)]
223#[derive(Debug, Clone, Copy, Pod, Zeroable)]
224pub struct Vector3 {
225 pub x: f32,
226 pub y: f32,
227 pub z: f32,
228}
229
230impl Vector3 {
231 pub fn new<T: ToPrimitive>(x: T, y: T, z: T) -> Self {
232 Self {
233 x: x.to_f32().unwrap_or(0.0),
234 y: y.to_f32().unwrap_or(0.0),
235 z: z.to_f32().unwrap_or(0.0),
236 }
237 }
238
239 pub fn cross(&self, other: &Self) -> Self {
240 Self {
241 x: self.y * other.z - self.z * other.y,
242 y: self.z * other.x - self.x * other.z,
243 z: self.x * other.y - self.y * other.x,
244 }
245 }
246
247 pub fn dot(&self, other: &Self) -> f32 {
248 self.x * other.x + self.y * other.y + self.z * other.z
249 }
250
251 pub fn length(&self) -> f32 {
252 self.dot(self).sqrt()
253 }
254
255 pub fn normalize(&self) -> Self {
256 let length = self.length();
257 Self {
258 x: self.x / length,
259 y: self.y / length,
260 z: self.z / length,
261 }
262 }
263
264 pub const ZERO: Self = Self {
265 x: 0.0,
266 y: 0.0,
267 z: 0.0,
268 };
269
270 pub const ONE: Self = Self {
271 x: 1.0,
272 y: 1.0,
273 z: 1.0,
274 };
275
276 pub const UP: Self = Self {
277 x: 0.0,
278 y: 1.0,
279 z: 0.0,
280 };
281
282 pub const DOWN: Self = Self {
283 x: 0.0,
284 y: -1.0,
285 z: 0.0,
286 };
287
288 pub const LEFT: Self = Self {
289 x: -1.0,
290 y: 0.0,
291 z: 0.0,
292 };
293
294 pub const RIGHT: Self = Self {
295 x: 1.0,
296 y: 0.0,
297 z: 0.0,
298 };
299
300 pub const FORWARD: Self = Self {
301 x: 0.0,
302 y: 0.0,
303 z: 1.0,
304 };
305}
306
307impl Default for Vector3 {
308 fn default() -> Self {
309 Self {
310 x: 0.0,
311 y: 0.0,
312 z: 0.0,
313 }
314 }
315}
316
317impl From<[f32; 3]> for Vector3 {
318 fn from(array: [f32; 3]) -> Self {
319 Self {
320 x: array[0],
321 y: array[1],
322 z: array[2],
323 }
324 }
325}
326
327impl From<(f32, f32, f32)> for Vector3 {
328 fn from(tuple: (f32, f32, f32)) -> Self {
329 Self {
330 x: tuple.0,
331 y: tuple.1,
332 z: tuple.2,
333 }
334 }
335}
336
337impl From<(u32, u32, u32)> for Vector3 {
338 fn from(tuple: (u32, u32, u32)) -> Self {
339 Self {
340 x: tuple.0 as f32,
341 y: tuple.1 as f32,
342 z: tuple.2 as f32,
343 }
344 }
345}
346
347impl From<(i32, i32, i32)> for Vector3 {
348 fn from(tuple: (i32, i32, i32)) -> Self {
349 Self {
350 x: tuple.0 as f32,
351 y: tuple.1 as f32,
352 z: tuple.2 as f32,
353 }
354 }
355}
356
357impl From<Vector2> for Vector3 {
358 fn from(vector: Vector2) -> Self {
359 Self {
360 x: vector.x,
361 y: vector.y,
362 z: 0.0,
363 }
364 }
365}
366
367impl From<(f32, f32)> for Vector3 {
368 fn from(tuple: (f32, f32)) -> Self {
369 Self {
370 x: tuple.0,
371 y: tuple.1,
372 z: 0.0,
373 }
374 }
375}
376
377impl Add for Vector3 {
378 type Output = Self;
379
380 fn add(self, other: Self) -> Self {
381 Self {
382 x: self.x + other.x,
383 y: self.y + other.y,
384 z: self.z + other.z,
385 }
386 }
387}
388
389impl Sub for Vector3 {
390 type Output = Self;
391
392 fn sub(self, other: Self) -> Self {
393 Self {
394 x: self.x - other.x,
395 y: self.y - other.y,
396 z: self.z - other.z,
397 }
398 }
399}
400
401impl Mul<f32> for Vector3 {
402 type Output = Self;
403
404 fn mul(self, scalar: f32) -> Self {
405 Self {
406 x: self.x * scalar,
407 y: self.y * scalar,
408 z: self.z * scalar,
409 }
410 }
411}
412
413impl Div<f32> for Vector3 {
414 type Output = Self;
415
416 fn div(self, scalar: f32) -> Self {
417 Self {
418 x: self.x / scalar,
419 y: self.y / scalar,
420 z: self.z / scalar,
421 }
422 }
423}
424
425impl PartialEq for Vector3 {
426 fn eq(&self, other: &Self) -> bool {
427 self.x == other.x && self.y == other.y && self.z == other.z
428 }
429}
430
431impl Eq for Vector3 {}
432
433impl AddAssign for Vector3 {
434 fn add_assign(&mut self, other: Self) {
435 *self = *self + other;
436 }
437}
438
439impl SubAssign for Vector3 {
440 fn sub_assign(&mut self, other: Self) {
441 *self = *self - other;
442 }
443}
444
445#[repr(C)]
446#[derive(Debug, Clone, Copy, Pod, Zeroable)]
447pub struct Vector4 {
448 pub x: f32,
449 pub y: f32,
450 pub z: f32,
451 pub w: f32,
452}
453
454impl Vector4 {
455 pub fn new<T: ToPrimitive>(x: T, y: T, z: T, w: T) -> Self {
456 Self {
457 x: x.to_f32().unwrap_or(0.0),
458 y: y.to_f32().unwrap_or(0.0),
459 z: z.to_f32().unwrap_or(0.0),
460 w: w.to_f32().unwrap_or(1.0), }
462 }
463
464 pub fn length(&self) -> f32 {
465 (self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w).sqrt()
466 }
467
468 pub fn normalize(&self) -> Self {
469 let length = self.length();
470 Self {
471 x: self.x / length,
472 y: self.y / length,
473 z: self.z / length,
474 w: self.w / length,
475 }
476 }
477
478 pub fn dot(&self, other: &Self) -> f32 {
479 self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w
480 }
481
482 pub fn cross(&self, other: &Self) -> Self {
483 Self {
484 x: self.y * other.z - self.z * other.y,
485 y: self.z * other.x - self.x * other.z,
486 z: self.x * other.y - self.y * other.x,
487 w: 0.0, }
489 }
490
491 pub const ZERO: Self = Self {
492 x: 0.0,
493 y: 0.0,
494 z: 0.0,
495 w: 1.0, };
497
498 pub const ONE: Self = Self {
499 x: 1.0,
500 y: 1.0,
501 z: 1.0,
502 w: 1.0, };
504
505 pub const UP: Self = Self {
506 x: 0.0,
507 y: 1.0,
508 z: 0.0,
509 w: 1.0, };
511
512 pub const DOWN: Self = Self {
513 x: 0.0,
514 y: -1.0,
515 z: 0.0,
516 w: 1.0, };
518}
519
520impl Default for Vector4 {
521 fn default() -> Self {
522 Self {
523 x: 0.0,
524 y: 0.0,
525 z: 0.0,
526 w: 1.0, }
528 }
529}
530
531impl PartialEq for Vector4 {
532 fn eq(&self, other: &Self) -> bool {
533 self.x == other.x && self.y == other.y && self.z == other.z && self.w == other.w
534 }
535}
536
537impl Eq for Vector4 {}
538
539#[repr(C)]
540#[derive(Clone, Copy, Pod, Zeroable)]
541pub struct Vector2I {
542 pub x: i32,
543 pub y: i32,
544}
545
546#[allow(dead_code)]
547impl Vector2I {
548 pub fn new<T: ToPrimitive>(x: T, y: T) -> Self {
549 Self {
550 x: x.to_i32().unwrap_or(0),
551 y: y.to_i32().unwrap_or(0),
552 }
553 }
554
555 pub const ZERO: Self = Self { x: 0, y: 0 };
556 pub const ONE: Self = Self { x: 1, y: 1 };
557 pub const UP: Self = Self { x: 0, y: 1 };
558 pub const DOWN: Self = Self { x: 0, y: -1 };
559 pub const LEFT: Self = Self { x: -1, y: 0 };
560 pub const RIGHT: Self = Self { x: 1, y: 0 };
561 pub const FORWARD: Self = Self { x: 0, y: 1 };
562}
563
564#[allow(dead_code)]
565impl Vector2I {
566 pub fn length(&self) -> f32 {
567 ((self.x * self.x + self.y * self.y) as f32).sqrt()
568 }
569
570 pub fn normalize(&self) -> Self {
571 let length = self.length();
572 Self {
573 x: (self.x as f32 / length) as i32,
574 y: (self.y as f32 / length) as i32,
575 }
576 }
577}
578
579impl From<(i32, i32)> for Vector2I {
580 fn from(tuple: (i32, i32)) -> Self {
581 Self {
582 x: tuple.0,
583 y: tuple.1,
584 }
585 }
586}
587
588impl From<(u32, u32)> for Vector2I {
589 fn from(tuple: (u32, u32)) -> Self {
590 Self {
591 x: tuple.0 as i32,
592 y: tuple.1 as i32,
593 }
594 }
595}
596
597impl From<(f32, f32)> for Vector2I {
598 fn from(tuple: (f32, f32)) -> Self {
599 Self {
600 x: tuple.0 as i32,
601 y: tuple.1 as i32,
602 }
603 }
604}
605
606#[repr(C)]
607#[derive(Clone, Copy, Pod, Zeroable)]
608pub struct Vector3I {
609 pub x: i32,
610 pub y: i32,
611 pub z: i32,
612}
613
614#[allow(dead_code)]
615impl Vector3I {
616 pub fn new<T: ToPrimitive>(x: T, y: T, z: T) -> Self {
617 Self {
618 x: x.to_i32().unwrap_or(0),
619 y: y.to_i32().unwrap_or(0),
620 z: z.to_i32().unwrap_or(0),
621 }
622 }
623
624 pub const ZERO: Self = Self { x: 0, y: 0, z: 0 };
625 pub const ONE: Self = Self { x: 1, y: 1, z: 1 };
626 pub const UP: Self = Self { x: 0, y: 1, z: 0 };
627 pub const DOWN: Self = Self { x: 0, y: -1, z: 0 };
628 pub const LEFT: Self = Self { x: -1, y: 0, z: 0 };
629 pub const RIGHT: Self = Self { x: 1, y: 0, z: 0 };
630}
631
632#[allow(dead_code)]
633impl Vector3I {
634 pub fn length(&self) -> f32 {
635 ((self.x * self.x + self.y * self.y + self.z * self.z) as f32).sqrt()
636 }
637
638 pub fn normalize(&self) -> Self {
639 let length = self.length();
640 Self {
641 x: (self.x as f32 / length) as i32,
642 y: (self.y as f32 / length) as i32,
643 z: (self.z as f32 / length) as i32,
644 }
645 }
646}
647
648impl From<(i32, i32, i32)> for Vector3I {
649 fn from(tuple: (i32, i32, i32)) -> Self {
650 Self {
651 x: tuple.0,
652 y: tuple.1,
653 z: tuple.2,
654 }
655 }
656}
657
658impl From<(u32, u32, u32)> for Vector3I {
659 fn from(tuple: (u32, u32, u32)) -> Self {
660 Self {
661 x: tuple.0 as i32,
662 y: tuple.1 as i32,
663 z: tuple.2 as i32,
664 }
665 }
666}
667
668impl From<(f32, f32, f32)> for Vector3I {
669 fn from(tuple: (f32, f32, f32)) -> Self {
670 Self {
671 x: tuple.0 as i32,
672 y: tuple.1 as i32,
673 z: tuple.2 as i32,
674 }
675 }
676}