simple_term_renderer/
math.rs

1/*
2
3    MIT License
4    
5    Copyright (c) 2022 Siandfrance
6    
7    Permission is hereby granted, free of charge, to any person obtaining a copy
8    of this software and associated documentation files (the "Software"), to deal
9    in the Software without restriction, including without limitation the rights
10    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11    copies of the Software, and to permit persons to whom the Software is
12    furnished to do so, subject to the following conditions:
13    
14    The above copyright notice and this permission notice shall be included in all
15    copies or substantial portions of the Software.
16    
17    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23    SOFTWARE.
24
25*/
26
27
28use std::ops::{Add, Sub, AddAssign, SubAssign, Mul, MulAssign, Div, DivAssign};
29
30
31pub trait Number: Sized + Add<Output = Self> + Sub<Output = Self> + Mul<Output = Self> + Div<Output = Self>
32        + Copy + Clone + PartialEq {}
33
34impl Number for i32 {}
35impl Number for f32 {}
36
37
38#[derive(Debug, Copy, Clone, PartialEq, Hash)]
39pub struct Vector2<N: Number> {
40    pub x: N,
41    pub y: N
42}
43
44
45impl<N: Number> Vector2<N> {
46
47    pub const fn new(x: N, y: N) -> Self {
48        Self {
49            x: x,
50            y: y
51        }
52    }
53
54
55    pub fn dot(a: Self, b: Self) -> N {
56        a.x * b.x + a.y * b.y
57    }
58
59
60    pub fn det(a: Self, b: Self) -> N {
61        a.x * b.y - a.y * b.x
62    }
63
64
65    pub fn length_sq(&self) -> N {
66        self.x * self.x + self.y * self.y
67    }
68}
69
70
71
72impl<N: Number> Add for Vector2<N> {
73    type Output = Self;
74
75    fn add(self, rhs: Self) -> Self::Output {
76        Self::new(self.x + rhs.x, self.y + rhs.y)
77    }
78}
79
80
81impl<N: Number> AddAssign for Vector2<N> {
82
83    fn add_assign(&mut self, rhs: Self) {
84        *self = *self + rhs;
85    }
86}
87
88
89impl<N: Number> Sub for Vector2<N> {
90    type Output = Self;
91
92    fn sub(self, rhs: Self) -> Self::Output {
93        Self::new(self.x - rhs.x, self.y - rhs.y)
94    }
95}
96
97
98impl<N: Number> SubAssign for Vector2<N> {
99
100    fn sub_assign(&mut self, rhs: Self) {
101        *self = *self + rhs;
102    }
103}
104
105
106impl<N: Number> Mul<N> for Vector2<N> {
107    type Output = Self;
108
109    fn mul(self, rhs: N) -> Self::Output {
110        Self::new(self.x * rhs, self.y * rhs)
111    }
112}
113
114
115impl<N: Number> MulAssign<N> for Vector2<N> {
116
117    fn mul_assign(&mut self, rhs: N) {
118        *self = *self * rhs;
119    }
120}
121
122
123impl<N: Number> Div<N> for Vector2<N> {
124    type Output = Self;
125
126    fn div(self, rhs: N) -> Self::Output {
127        Self::new(self.x / rhs, self.y / rhs)
128    }
129}
130
131
132impl<N: Number> DivAssign<N> for Vector2<N> {
133
134    fn div_assign(&mut self, rhs: N) {
135        *self = *self / rhs;
136    }
137}
138
139
140#[macro_export]
141macro_rules! vec2 {
142    ($x:expr, $y:expr) => {Vec2::new($x as i32, $y as i32)};
143}
144
145
146pub type Vec2 = Vector2<i32>;
147
148
149impl Vec2 {
150    pub const ZERO: Vec2 = Vec2::new(0, 0);
151    pub const UNIT_X: Vec2 = Vec2::new(1, 0);
152    pub const UNIT_Y: Vec2 = Vec2::new(0, 1);
153    pub const ONE: Vec2 = Vec2::new(1, 1);
154}
155
156
157impl Eq for Vec2 {}
158
159
160
161impl AsRef<Vec2> for (u32, u32) {
162
163
164    fn as_ref(&self) -> &Vec2 {
165        if self.0 > i32::MAX as u32 || self.1 > i32::MAX as u32 {
166            panic!("Cannot convert {:?} to Vec2, integeroverflow", self);
167        }
168        unsafe {
169            let ptr: *const (u32, u32) = self;
170            &*(ptr as *const Vec2)
171        }
172    }
173}
174
175
176impl AsRef<Vec2> for (i32, i32) {
177
178
179    fn as_ref(&self) -> &Vec2 {
180        unsafe {
181            let ptr: *const (i32, i32) = self;
182            &*(ptr as *const Vec2)
183        }
184    }
185}
186
187
188impl AsMut<Vec2> for (u32, u32) {
189
190
191    fn as_mut(&mut self) -> &mut Vec2 {
192        if self.0 > i32::MAX as u32 || self.1 > i32::MAX as u32 {
193            panic!("Cannot convert {:?} to Vec2, integeroverflow", self);
194        }
195        unsafe {
196            let ptr: *mut (u32, u32) = self;
197            &mut *(ptr as *mut Vec2)
198        }
199    }
200}
201
202
203impl AsMut<Vec2> for (i32, i32) {
204
205
206    fn as_mut(&mut self) -> &mut Vec2 {
207        unsafe {
208            let ptr: *mut (i32, i32) = self;
209            &mut *(ptr as *mut Vec2)
210        }
211    }
212}
213
214
215impl Into<Vec2> for (i32, i32) {
216
217
218    fn into(self) -> Vec2 {
219        Vec2::new(self.0, self.1)
220    }
221}
222
223
224impl Into<Vec2> for (u32, u32) {
225
226
227    fn into(self) -> Vec2 {
228        Vec2::new(self.0 as i32, self.1 as i32)
229    }
230}
231
232
233impl AsRef<Vec2> for (usize, usize) {
234
235
236    fn as_ref(&self) -> &Vec2 {
237        if self.0 > i32::MAX as usize || self.1 > i32::MAX as usize {
238            panic!("Cannot convert {:?} to Vec2, integeroverflow", self);
239        }
240        unsafe {
241            let ptr: *const (usize, usize) = self;
242            &*(ptr as *const Vec2)
243        }
244    }
245}
246
247
248impl AsRef<Vec2> for (isize, isize) {
249
250
251    fn as_ref(&self) -> &Vec2 {
252        unsafe {
253            let ptr: *const (isize, isize) = self;
254            &*(ptr as *const Vec2)
255        }
256    }
257}
258
259
260impl AsMut<Vec2> for (usize, usize) {
261
262
263    fn as_mut(&mut self) -> &mut Vec2 {
264        if self.0 > i32::MAX as usize || self.1 > i32::MAX as usize {
265            panic!("Cannot convert {:?} to Vec2, integeroverflow", self);
266        }
267        unsafe {
268            let ptr: *mut (usize, usize) = self;
269            &mut *(ptr as *mut Vec2)
270        }
271    }
272}
273
274
275impl AsMut<Vec2> for (isize, isize) {
276
277
278    fn as_mut(&mut self) -> &mut Vec2 {
279        unsafe {
280            let ptr: *mut (isize, isize) = self;
281            &mut *(ptr as *mut Vec2)
282        }
283    }
284}
285
286
287impl Into<Vec2> for (isize, isize) {
288
289
290    fn into(self) -> Vec2 {
291        Vec2::new(self.0 as i32, self.1 as i32)
292    }
293}
294
295
296impl Into<Vec2> for (usize, usize) {
297
298
299    fn into(self) -> Vec2 {
300        Vec2::new(self.0 as i32, self.1 as i32)
301    }
302}
303
304
305impl AsRef<Vec2> for Vec2 {
306
307    fn as_ref(&self) -> &Vec2 {
308        self
309    }
310}
311
312
313impl AsMut<Vec2> for Vec2 {
314
315    fn as_mut(&mut self) -> &mut Vec2 {
316        self
317    }
318}
319
320impl From<Vec2f> for Vec2 {
321
322    fn from(value: Vec2f) -> Self {
323        Vec2::new(value.x as i32, value.y as i32)
324    }
325}
326
327
328
329#[derive(Debug, Copy, Clone, PartialEq, Hash)]
330pub struct Vector3<N: Number> {
331    pub x: N,
332    pub y: N,
333    pub z: N
334}
335
336
337impl<N: Number> Vector3<N> {
338
339    pub const fn new(x: N, y: N, z: N) -> Self {
340        Self {
341            x: x,
342            y: y,
343            z: z
344        }
345    }
346
347
348    pub fn dot(a: Self, b: Self) -> N {
349        a.x * b.x + a.y * b.y + a.z * b.z
350    }
351
352
353    pub fn length_sq(&self) -> N {
354        self.x * self.x + self.y * self.y + self.z * self.z
355    }
356}
357
358
359
360impl<N: Number> Add for Vector3<N> {
361    type Output = Self;
362
363    fn add(self, rhs: Self) -> Self::Output {
364        Self::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
365    }
366}
367
368
369impl<N: Number> AddAssign for Vector3<N> {
370
371    fn add_assign(&mut self, rhs: Self) {
372        *self = *self + rhs;
373    }
374}
375
376
377impl<N: Number> Sub for Vector3<N> {
378    type Output = Self;
379
380    fn sub(self, rhs: Self) -> Self::Output {
381        Self::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
382    }
383}
384
385
386impl<N: Number> SubAssign for Vector3<N> {
387
388    fn sub_assign(&mut self, rhs: Self) {
389        *self = *self + rhs;
390    }
391}
392
393
394impl<N: Number> Mul<N> for Vector3<N> {
395    type Output = Self;
396
397    fn mul(self, rhs: N) -> Self::Output {
398        Self::new(self.x * rhs, self.y * rhs, self.z * rhs)
399    }
400}
401
402
403impl<N: Number> MulAssign<N> for Vector3<N> {
404
405    fn mul_assign(&mut self, rhs: N) {
406        *self = *self * rhs;
407    }
408}
409
410
411impl<N: Number> Div<N> for Vector3<N> {
412    type Output = Self;
413
414    fn div(self, rhs: N) -> Self::Output {
415        Self::new(self.x / rhs, self.y / rhs, self.z / rhs)
416    }
417}
418
419
420impl<N: Number> DivAssign<N> for Vector3<N> {
421
422    fn div_assign(&mut self, rhs: N) {
423        *self = *self / rhs;
424    }
425}
426
427
428#[macro_export]
429macro_rules! vec3 {
430    ($x:expr, $y:expr, $z:expr) => {Vec3::new($x as i32, $y as i32, $z as i32)};
431}
432
433
434pub type Vec3 = Vector3<i32>;
435
436
437impl Vec3 {
438    pub const ZERO  : Vec3 = Vec3::new(0, 0, 0);
439    pub const UNIT_X: Vec3 = Vec3::new(1, 0, 0);
440    pub const UNIT_Y: Vec3 = Vec3::new(0, 1, 0);
441    pub const UNIT_Z: Vec3 = Vec3::new(0, 0, 1);
442    pub const ONE   : Vec3 = Vec3::new(1, 1, 1);
443}
444
445
446impl AsRef<Vec3> for (u32, u32, u32) {
447
448
449    fn as_ref(&self) -> &Vec3 {
450        if self.0 > i32::MAX as u32 || self.1 > i32::MAX as u32 || self.2 > i32::MAX as u32 {
451            panic!("Cannot convert {:?} to Vec3, integeroverflow", self);
452        }
453        unsafe {
454            let ptr: *const (u32, u32, u32) = self;
455            &*(ptr as *const Vec3)
456        }
457    }
458}
459
460
461impl AsRef<Vec3> for (i32, i32, i32) {
462
463
464    fn as_ref(&self) -> &Vec3 {
465        unsafe {
466            let ptr: *const (i32, i32, i32) = self;
467            &*(ptr as *const Vec3)
468        }
469    }
470}
471
472
473impl AsMut<Vec3> for (u32, u32, u32) {
474
475
476    fn as_mut(&mut self) -> &mut Vec3 {
477        if self.0 > i32::MAX as u32 || self.1 > i32::MAX as u32 || self.2 > i32::MAX as u32 {
478            panic!("Cannot convert {:?} to Vec3, integeroverflow", self);
479        }
480        unsafe {
481            let ptr: *mut (u32, u32, u32) = self;
482            &mut *(ptr as *mut Vec3)
483        }
484    }
485}
486
487
488impl AsMut<Vec3> for (i32, i32, i32) {
489
490
491    fn as_mut(&mut self) -> &mut Vec3 {
492        unsafe {
493            let ptr: *mut (i32, i32, i32) = self;
494            &mut *(ptr as *mut Vec3)
495        }
496    }
497}
498
499
500impl Into<Vec3> for (i32, i32, i32) {
501
502
503    fn into(self) -> Vec3 {
504        Vec3::new(self.0, self.1, self.2)
505    }
506}
507
508
509impl Into<Vec3> for (u32, u32, u32) {
510
511
512    fn into(self) -> Vec3 {
513        Vec3::new(self.0 as i32, self.1 as i32, self.2 as i32)
514    }
515}
516
517
518impl AsRef<Vec3> for (usize, usize, usize) {
519
520
521    fn as_ref(&self) -> &Vec3 {
522        if self.0 > i32::MAX as usize || self.1 > i32::MAX as usize || self.2 > i32::MAX as usize {
523            panic!("Cannot convert {:?} to Vec3, integeroverflow", self);
524        }
525        unsafe {
526            let ptr: *const (usize, usize, usize) = self;
527            &*(ptr as *const Vec3)
528        }
529    }
530}
531
532
533impl AsRef<Vec3> for (isize, isize, isize) {
534
535
536    fn as_ref(&self) -> &Vec3 {
537        unsafe {
538            let ptr: *const (isize, isize, isize) = self;
539            &*(ptr as *const Vec3)
540        }
541    }
542}
543
544
545impl AsMut<Vec3> for (usize, usize, usize) {
546
547
548    fn as_mut(&mut self) -> &mut Vec3 {
549        if self.0 > i32::MAX as usize || self.1 > i32::MAX as usize || self.2 > i32::MAX as usize {
550            panic!("Cannot convert {:?} to Vec3, integeroverflow", self);
551        }
552        unsafe {
553            let ptr: *mut (usize, usize, usize) = self;
554            &mut *(ptr as *mut Vec3)
555        }
556    }
557}
558
559
560impl AsMut<Vec3> for (isize, isize, isize) {
561
562
563    fn as_mut(&mut self) -> &mut Vec3 {
564        unsafe {
565            let ptr: *mut (isize, isize, isize) = self;
566            &mut *(ptr as *mut Vec3)
567        }
568    }
569}
570
571
572impl Into<Vec3> for (isize, isize, isize) {
573
574
575    fn into(self) -> Vec3 {
576        Vec3::new(self.0 as i32, self.1 as i32, self.2 as i32)
577    }
578}
579
580
581impl Into<Vec3> for (usize, usize, usize) {
582
583
584    fn into(self) -> Vec3 {
585        Vec3::new(self.0 as i32, self.1 as i32, self.2 as i32)
586    }
587}
588
589
590impl AsRef<Vec3> for Vec3 {
591
592    fn as_ref(&self) -> &Vec3 {
593        self
594    }
595}
596
597
598impl AsMut<Vec3> for Vec3 {
599
600    fn as_mut(&mut self) -> &mut Vec3 {
601        self
602    }
603}
604
605
606impl From<Vec3f> for Vec3 {
607
608    fn from(value: Vec3f) -> Self {
609        Vec3::new(value.x as i32, value.y as i32, value.z as i32)
610    }
611}
612
613
614#[macro_export]
615macro_rules! vec2f {
616    ($x:expr, $y:expr) => {Vec2f::new($x as f32, $y as f32)};
617}
618
619
620#[macro_export]
621macro_rules! vec3f {
622    ($x:expr, $y:expr, $z:expr) => {Vec3f::new($x as f32, $y as f32, $z as f32)};
623}
624
625
626pub type Vec2f = Vector2<f32>;
627pub type Vec3f = Vector3<f32>;
628
629
630impl Vec2f {
631    pub const ZERO  : Vec2f = Vec2f::new(0.0, 0.0);
632    pub const UNIT_X: Vec2f = Vec2f::new(1.0, 0.0);
633    pub const UNIT_Y: Vec2f = Vec2f::new(0.0, 1.0);
634    pub const ONE   : Vec2f = Vec2f::new(1.0, 1.0);
635
636
637    pub fn length(&self) -> f32 {
638        self.length_sq().sqrt()
639    }
640}
641
642impl Eq for Vec2f {}
643
644
645impl From<Vec2> for Vec2f {
646
647    fn from(value: Vec2) -> Self {
648        Vec2f::new(value.x as f32, value.y as f32)
649    }
650}
651
652
653
654impl Vec3f {
655    pub const ZERO  : Vec3f = Vec3f::new(0.0, 0.0, 0.0);
656    pub const UNIT_X: Vec3f = Vec3f::new(1.0, 0.0, 0.0);
657    pub const UNIT_Y: Vec3f = Vec3f::new(0.0, 1.0, 0.0);
658    pub const UNIT_Z: Vec3f = Vec3f::new(0.0, 0.0, 1.0);
659    pub const ONE   : Vec3f = Vec3f::new(1.0, 1.0, 1.0);
660
661    pub fn length(&self) -> f32 {
662        self.length_sq().sqrt()
663    }
664}
665
666
667impl Eq for Vec3f {}
668
669
670impl From<Vec3> for Vec3f {
671
672    fn from(value: Vec3) -> Self {
673        Vec3f::new(value.x as f32, value.y as f32, value.z as f32)
674    }
675}