makepad_math/
math_f32.rs

1use{
2    std::{fmt,ops},
3    crate::math_f64::*,
4    makepad_micro_serde::*,
5//    crate::colorhex::*
6};
7
8
9pub struct PrettyPrintedF32(pub f32);
10
11impl fmt::Display for PrettyPrintedF32 {
12    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
13        if self.0.abs().fract() < 0.00000001 {
14            write!(f, "{}.0", self.0)
15        } else {
16            write!(f, "{}", self.0)
17        }
18    }
19}
20
21pub const VF00:Vec4 = Vec4{x:1.0,y:0.0,z:0.0,w:1.0};
22pub const V0F0:Vec4 = Vec4{x:0.0,y:1.0,z:0.0,w:1.0};
23pub const V00F:Vec4 = Vec4{x:0.0,y:0.0,z:1.0,w:1.0};
24
25#[derive(Clone, Copy, PartialEq, Debug)]
26#[repr(C)]
27pub struct Mat4 {
28    pub v: [f32; 16],
29}
30
31impl Default for Mat4{
32    fn default()->Self{
33        Self{v:[1.,0.,0.,0., 0.,1.,0.,0., 0.,0.,1.,0., 0.,0.,0.,1.]}
34    }
35}
36
37#[derive(Copy, Clone, Debug, PartialEq)]
38pub enum Vec2Index{
39    X,
40    Y
41}
42
43#[repr(C)]
44#[derive(Clone, Copy, Default, PartialEq, Debug, SerBin, DeBin)]
45pub struct Pose {
46    pub orientation: Quat,
47    pub position: Vec3
48}
49
50impl Pose {
51    pub fn new(orientation:Quat, position:Vec3)->Self{
52        Self{
53            orientation,
54            position
55        }
56    }
57    
58    pub fn transform_vec3(&self, v:&Vec3)->Vec3{
59        let r0 = self.orientation.rotate_vec3(v);
60        r0 + self.position
61    }
62    pub fn multiply(a:&Pose, b:&Pose)->Self{
63        Self{
64            orientation: Quat::multiply(&b.orientation, &a.orientation),
65            position: a.transform_vec3(&b.position)
66        }
67    }
68    pub fn invert(&self)->Self{
69        let orientation = self.orientation.invert();
70        let neg_pos = self.position.scale(-1.0);
71        Self{
72            orientation,
73            position: orientation.rotate_vec3(&neg_pos),
74        }
75    }
76    
77    pub fn to_mat4(&self) -> Mat4 {
78        
79        let q = self.orientation;
80        let t = self.position;/*
81        Mat4 {v: [
82            (1.0 - 2.0 * q.y * q.y - 2.0 * q.z * q.z),
83            (2.0 * q.x * q.y - 2.0 * q.z * q.w),
84            (2.0 * q.x * q.z + 2.0 * q.y * q.w),
85            t.x,
86            (2.0 * q.x * q.y + 2.0 * q.z * q.w),
87            (1.0 - 2.0 * q.x * q.x - 2.0 * q.z * q.z),
88            (2.0 * q.y * q.z - 2.0 * q.x * q.w),
89            t.y,
90            (2.0 * q.x * q.z - 2.0 * q.y * q.w),
91            (2.0 * q.y * q.z + 2.0 * q.x * q.w),
92            (1.0 - 2.0 * q.x * q.x - 2.0 * q.y * q.y),
93            t.z,
94            0.0,
95            0.0,
96            0.0,
97            1.0
98        ]}*/
99        Mat4 {v: [
100            (1.0 - 2.0 * q.y * q.y - 2.0 * q.z * q.z),
101            (2.0 * q.x * q.y + 2.0 * q.z * q.w),
102            (2.0 * q.x * q.z - 2.0 * q.y * q.w),
103            0.0,
104            (2.0 * q.x * q.y - 2.0 * q.z * q.w),
105            (1.0 - 2.0 * q.x * q.x - 2.0 * q.z * q.z),
106            (2.0 * q.y * q.z + 2.0 * q.x * q.w),
107            0.0,
108            (2.0 * q.x * q.z + 2.0 * q.y * q.w),
109            (2.0 * q.y * q.z - 2.0 * q.x * q.w),
110            (1.0 - 2.0 * q.x * q.x - 2.0 * q.y * q.y),
111            0.0,
112            t.x,
113            t.y,
114            t.z,
115            1.0
116        ]}
117    }
118    
119    pub fn from_lerp(a: Pose, b: Pose, f: f32) -> Self {
120        Pose {
121            orientation: Quat::from_slerp(a.orientation, b.orientation, f),
122            position: Vec3::from_lerp(a.position, b.position, f)
123        }
124    }
125    
126    pub fn from_slerp_orientation(a: Pose, b: Pose, f: f32) -> Self {
127        Pose {
128            orientation: Quat::from_slerp(a.orientation, b.orientation, f),
129            position: b.position
130        }
131    }
132}
133
134#[repr(C)]
135#[derive(Clone, Copy, Default, Debug, PartialEq, SerBin, DeBin)]
136pub struct Vec2 {
137    pub x: f32,
138    pub y: f32,
139}
140
141
142impl Vec2 {
143
144    pub fn new() -> Vec2 {
145        Vec2::default()
146    }
147    
148    pub fn index(&self, index:Vec2Index)->f32{
149        match index{
150            Vec2Index::X=>self.x,
151            Vec2Index::Y=>self.y
152        }
153    }
154
155    pub fn set_index(&mut self, index:Vec2Index, v: f32){
156        match index{
157            Vec2Index::X=>{self.x = v},
158            Vec2Index::Y=>{self.y = v}
159        }
160    }
161
162
163    pub fn from_index_pair(index:Vec2Index, a: f32, b:f32)->Self{
164        match index{
165            Vec2Index::X=>{Self{x:a,y:b}},
166            Vec2Index::Y=>{Self{x:b,y:a}}
167        }
168    }
169    
170
171    pub fn into_dvec2(self)->DVec2{
172        DVec2{x:self.x as f64, y:self.y as f64}
173    }
174    
175    pub fn all(x: f32) -> Vec2 {
176        Vec2 {x, y: x}
177    }
178    
179    pub fn from_lerp(a: Vec2, b: Vec2, f: f32) -> Vec2 {
180        let nf = 1.0 - f;
181        Vec2{
182            x: nf * a.x + f * b.x,
183            y: nf * a.y + f * b.y,
184        }
185    }
186    
187    pub fn distance(&self, other: &Vec2) -> f32 {
188        let dx = self.x - other.x;
189        let dy = self.y - other.y;
190        (dx * dx + dy * dy).sqrt()
191    }
192    
193    pub fn angle_in_radians(&self) -> f32 {
194        self.y.atan2(self.x)
195    }
196    
197    pub fn angle_in_degrees(&self) -> f32 {
198        self.y.atan2(self.x) * (360.0 / (2. * std::f32::consts::PI))
199    }
200
201
202    pub fn length(&self) -> f32 {
203        (self.x * self.x + self.y * self.y).sqrt()
204    }
205
206    pub fn lengthsquared(&self) -> f32 {
207        self.x * self.x + self.y * self.y
208    }
209    pub fn normalize(&self) -> Vec2
210    {
211        let l  = self.length();
212        if l == 0.0 {return vec2(0.,0.);}
213        return vec2(self.x/l, self.y/l);
214    }
215    pub fn normalize_to_x(&self) -> Vec2
216    {
217        let l  = self.x;
218        if l == 0.0 {return vec2(1.,0.);}
219        return vec2(1., self.y/l);
220    }
221   
222    pub fn normalize_to_y(&self) -> Vec2
223    {
224        let l  = self.y;
225        if l == 0.0 {return vec2(1.,0.);}
226        return vec2(self.x/l, 1.);
227    }
228    pub fn to_vec3(&self) -> Vec3 {
229        Vec3 {x: self.x, y: self.y, z: 0.0}
230    }
231}
232
233
234impl fmt::Display for Vec2 {
235    // This trait requires `fmt` with this exact signature.
236    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
237        write!(f,"vec2({},{})",self.x, self.y)
238    }
239}
240
241impl fmt::Display for Vec3 {
242    // This trait requires `fmt` with this exact signature.
243    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
244        write!(f,"vec3({},{},{})",self.x, self.y, self.z)
245    }
246}
247
248pub const fn vec2(x: f32, y: f32) -> Vec2 {Vec2 {x, y}}
249pub const fn vec3(x: f32, y: f32, z: f32) -> Vec3 {Vec3 {x, y, z}}
250pub const fn vec4(x: f32, y: f32, z: f32, w: f32) -> Vec4 {Vec4 {x, y, z, w}}
251
252const TORAD: f32 = 0.017453292;
253const TODEG: f32 = 57.29578;
254
255/*
256pub fn vec2(x:f32, y:f32)->Vec2{
257    Vec2{x:x, y:y}
258}*/
259
260#[repr(C)]
261#[derive(Clone, Copy, Default, PartialEq, Debug, SerBin, DeBin)]
262pub struct Vec3 {
263    pub x: f32,
264    pub y: f32,
265    pub z: f32
266}
267
268impl Vec3 {
269    
270    pub fn from_lerp(a: Vec3, b: Vec3, f: f32) -> Vec3 {
271        Vec3 {
272            x: (b.x - a.x) * f + a.x,
273            y: (b.y - a.y) * f + a.y,
274            z: (b.z - a.z) * f + a.z
275        }
276    }
277    
278    pub fn zero(&mut self)
279    {
280        self.x = 0.0;
281        self.y = 0.0;
282        self.z = 0.0;
283    }
284    
285    pub const fn all(x: f32) -> Vec3 {
286        Vec3 {x, y: x, z: x}
287    }
288    
289    pub const fn to_vec2(&self) -> Vec2 {
290        Vec2 {x: self.x, y: self.y}
291    }
292        
293    pub const fn to_vec4(&self) -> Vec4 {
294        Vec4 {x: self.x, y: self.y, z: self.z, w: 1.0}
295    }
296        
297    pub fn scale(&self, f: f32) -> Vec3 {
298        Vec3 {x: self.x * f, y: self.y * f, z: self.z * f}
299    }
300    
301    pub fn cross(a: Vec3, b: Vec3) -> Vec3 {
302        Vec3 {
303            x: a.y * b.z - a.z * b.y,
304            y: a.z * b.x - a.x * b.z,
305            z: a.x * b.y - a.y * b.x
306        }
307    }
308    
309    pub fn dot(&self, other: Vec3) -> f32 {
310        self.x * other.x + self.y * other.y + self.z * other.z
311    }
312    
313    pub fn normalize(&self) -> Vec3 {
314        let sz = self.x * self.x + self.y * self.y + self.z * self.z;
315        if sz > 0.0 {
316            let sr = 1.0 / sz.sqrt();
317            return Vec3 {
318                x: self.x * sr,
319                y: self.y * sr,
320                z: self.z * sr
321            };
322        }
323        Vec3::default()
324    }
325    
326    pub fn length(&self) -> f32 {
327        let sz = self.x * self.x + self.y * self.y + self.z * self.z;
328        sz.sqrt()
329    }
330}
331
332/*
333pub fn vec3(x:f32, y:f32, z:f32)->Vec3{
334    Vec3{x:x, y:y, z:z}
335}*/
336
337
338// equation ax + by + cz + d = 0
339#[derive(Clone, Copy, Default, Debug, PartialEq)]
340pub struct Plane {
341    pub a: f32,
342    pub b: f32,
343    pub c: f32,
344    pub d: f32
345}
346
347impl Plane {
348    pub fn from_point_normal(p: Vec3, normal: Vec3) -> Self {
349        let n = normal.normalize();
350        Self {
351            a: n.x,
352            b: n.y,
353            c: n.z,
354            d: -p.dot(n)
355        }
356    }
357    
358    pub fn from_points(p1: Vec3, p2: Vec3, p3: Vec3) -> Self {
359        let normal = Vec3::cross(p2 - p1, p3 - p1);
360        Self::from_point_normal(p1, normal)
361    }
362    
363    pub fn intersect_line(&self, v1: Vec3, v2: Vec3) -> Vec3 {
364        let diff = v1 - v2;
365        let denom = self.a * diff.x + self.b * diff.y + self.c * diff.z;
366        if denom == 0.0 {
367            return (v1 * v2) * 0.5
368        }
369        let u = (self.a * v1.x + self.b * v1.y + self.c * v1.z + self.d) / denom;
370        v1 + (v2 - v1) * u
371    }
372}
373
374#[repr(C)]
375#[derive(Clone, Copy, Default, Debug,PartialEq, SerBin, DeBin)]
376pub struct Vec4 {
377    pub x: f32,
378    pub y: f32,
379    pub z: f32,
380    pub w: f32
381}
382
383impl fmt::Display for Vec4 {
384    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
385        write!(
386            f,
387            "vec4({}, {}, {}, {})",
388            PrettyPrintedF32(self.x),
389            PrettyPrintedF32(self.y),
390            PrettyPrintedF32(self.z),
391            PrettyPrintedF32(self.w),
392        )
393    }
394}
395
396impl Vec4 {
397    pub const R: Vec4 = Vec4 {x: 1.0, y: 0.0, z: 0.0, w: 1.0};
398    pub const G: Vec4 = Vec4 {x: 0.0, y: 1.0, z: 0.0, w: 1.0};
399    pub const B: Vec4 = Vec4 {x: 0.0, y: 0.0, z: 1.0, w: 1.0};
400
401    
402    pub const fn all(v: f32) -> Self {
403        Self {x: v, y: v, z: v, w: v}
404    }
405    
406    pub const fn to_vec3(&self) -> Vec3 {
407        Vec3 {x: self.x, y: self.y, z: self.z}
408    }
409    
410    pub fn dot(&self, other: Vec4) -> f32 {
411        self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w
412    }
413    
414    pub fn from_lerp(a: Vec4, b: Vec4, f: f32) -> Vec4 {
415        let nf = 1.0 - f;
416        Vec4 {
417            x: nf * a.x + f * b.x,
418            y: nf * a.y + f * b.y,
419            z: nf * a.z + f * b.z,
420            w: nf * a.w + f * b.w,
421        }
422    }
423    
424    pub fn is_equal_enough(&self, other: &Vec4, epsilon:f32) -> bool {
425        (self.x - other.x).abs() < epsilon
426            && (self.y - other.y).abs() < epsilon
427            && (self.z - other.z).abs() < epsilon
428            && (self.w - other.w).abs() < epsilon
429    }
430    
431    
432    pub fn from_hsva(hsv: Vec4) -> Vec4 {
433        fn mix(x: f32, y: f32, t: f32) -> f32 {x + (y - x) * t}
434        fn clamp(x: f32, mi: f32, ma: f32) -> f32 {if x < mi {mi} else if x > ma {ma} else {x}}
435        fn fract(x: f32) -> f32 {x.fract()}
436        fn abs(x: f32) -> f32 {x.abs()}
437        Vec4 {
438            x: hsv.z * mix(1.0, clamp(abs(fract(hsv.x + 1.0) * 6.0 - 3.0) - 1.0, 0.0, 1.0), hsv.y),
439            y: hsv.z * mix(1.0, clamp(abs(fract(hsv.x + 2.0 / 3.0) * 6.0 - 3.0) - 1.0, 0.0, 1.0), hsv.y),
440            z: hsv.z * mix(1.0, clamp(abs(fract(hsv.x + 1.0 / 3.0) * 6.0 - 3.0) - 1.0, 0.0, 1.0), hsv.y),
441            w: 1.0
442        }
443    }
444    
445    pub fn to_hsva(&self) -> Vec4 {
446        let pc = self.y < self.z; //step(c[2],c[1])
447        let p0 = if pc {self.z} else {self.y}; //mix(c[2],c[1],pc)
448        let p1 = if pc {self.y} else {self.z}; //mix(c[1],c[2],pc)
449        let p2 = if pc {-1.0} else {0.0}; //mix(-1,0,pc)
450        let p3 = if pc {2.0 / 3.0} else {-1.0 / 3.0}; //mix(2/3,-1/3,pc)
451        
452        let qc = self.x < p0; //step(p0, c[0])
453        let q0 = if qc {p0} else {self.x}; //mix(p0, c[0], qc)
454        let q1 = p1;
455        let q2 = if qc {p3} else {p2}; //mix(p3, p2, qc)
456        let q3 = if qc {self.x} else {p0}; //mix(c[0], p0, qc)
457        
458        let d = q0 - q3.min(q1);
459        let e = 1.0e-10;
460        Vec4 {
461            x: (q2 + (q3 - q1) / (6.0 * d + e)).abs(),
462            y: d / (q0 + e),
463            z: q0,
464            w: self.w
465        }
466    }
467    
468    
469    pub fn from_u32(val: u32) -> Vec4 {
470        Vec4 {
471            x: ((val >> 24) & 0xff) as f32 / 255.0,
472            y: ((val >> 16) & 0xff) as f32 / 255.0,
473            z: ((val >> 8) & 0xff) as f32 / 255.0,
474            w: (val & 0xff) as f32 / 255.0,
475        }
476    }
477    
478    pub fn to_u32(&self) -> u32 {
479        let r = (self.x * 255.0) as u8 as u32;
480        let g = (self.y * 255.0) as u8 as u32;
481        let b = (self.z * 255.0) as u8 as u32;
482        let a = (self.w * 255.0) as u8 as u32;
483        (r<<24)|(g<<16)|(b<<8)|a
484    }
485
486    pub const fn xy(&self) -> Vec2 {
487        Vec2{x:self.x, y:self.y}
488    }
489
490    pub const fn zw(&self) -> Vec2 {
491        Vec2{x:self.z, y:self.w}
492    }
493
494}
495
496impl From<(DVec2,DVec2)> for Vec4{
497    fn from(other:(DVec2,DVec2))->Vec4{
498        vec4(other.0.x as f32, other.0.y as f32, other.1.x as f32, other.1.y as f32)
499    }
500}
501
502
503#[repr(C)]
504#[derive(Copy, Clone, Debug, Default, PartialEq)]
505pub struct CameraFov {
506    pub angle_left: f32,
507    pub angle_right: f32,
508    pub angle_up: f32,
509    pub angle_down: f32,
510}
511
512#[repr(C)]
513#[derive(Clone, Copy, Debug, PartialEq, SerBin, DeBin)]
514pub struct Quat {
515    pub x: f32,
516    pub y: f32,
517    pub z: f32,
518    pub w: f32
519}
520
521impl Default for Quat{
522    fn default()->Self{Self{x:0.0,y:0.0,z:0.0,w:1.0}}
523}
524
525impl Quat {
526    pub fn multiply(a:&Quat, b:&Quat)->Self{
527        Self{
528            x:(b.w * a.x) + (b.x * a.w) + (b.y * a.z) - (b.z * a.y),
529            y:(b.w * a.y) - (b.x * a.z) + (b.y * a.w) + (b.z * a.x),
530            z:(b.w * a.z) + (b.x * a.y) - (b.y * a.x) + (b.z * a.w),
531            w:(b.w * a.w) - (b.x * a.x) - (b.y * a.y) - (b.z * a.z)
532        }
533    }
534        
535    pub fn invert(&self)->Self{
536        Self{
537            x: -self.x,
538            y: -self.y,
539            z: -self.z,
540            w: self.w,
541        }
542    }
543        
544    pub fn rotate_vec3(&self, v:&Vec3)->Vec3{
545        let q = Quat{x:v.x, y:v.y, z:v.z, w:0.0};
546        let aq = Quat::multiply(&q, self);
547        let ainv = self.invert();
548        let aqainv = Quat::multiply(&ainv, &aq);
549        Vec3{x: aqainv.x, y: aqainv.y, z: aqainv.z}
550    }
551    
552    pub fn dot(&self, other: Quat) -> f32 {
553        self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w
554    }
555    
556    pub fn neg(&self) -> Quat {
557        Quat {x: -self.x, y: -self.y, z: -self.z, w: -self.w}
558    }
559    
560    pub fn get_angle_with(&self, other: Quat) -> f32 {
561        let dot = self.dot(other);
562        (2.0 * dot * dot - 1.0).acos() * TODEG
563    }
564    
565    pub fn from_slerp(n: Quat, mut m: Quat, t: f32) -> Quat {
566        // calc cosine
567        let mut cosom = n.dot(m);
568        // adjust signs (if necessary)
569        if cosom < 0.0 {
570            cosom = -cosom;
571            m = m.neg();
572        }
573        // calculate coefficients
574        let (scale0, scale1) = if 1.0 - cosom > 0.000001 {
575            // standard case (slerp)
576            let omega = cosom.acos();
577            let sinom = omega.sin();
578            (((1.0 - t) * omega).sin() / sinom, (t * omega).sin() / sinom)
579        } else {
580            (1.0 - t, t)
581        };
582        // calculate final values
583        (Quat {
584            x: scale0 * n.x + scale1 * m.x,
585            y: scale0 * n.y + scale1 * m.y,
586            z: scale0 * n.z + scale1 * m.z,
587            w: scale0 * m.w + scale1 * m.w
588        }).normalized()
589    }
590    
591    pub fn length(self) -> f32 {
592        self.dot(self).sqrt()
593    }
594    
595    pub fn normalized(&mut self) -> Quat {
596        let len = self.length();
597        Quat {
598            x: self.x / len,
599            y: self.y / len,
600            z: self.z / len,
601            w: self.w / len,
602        }
603    }
604    
605    pub fn look_rotation(forward: Vec3, up:Vec3)->Self{
606        let forward = forward.normalize();
607        let up = up.normalize();
608        let v2 = forward;
609        let v0 = Vec3::cross(up, forward).normalize();
610        let v1 = Vec3::cross(v2, v0);
611        
612        let num = (v0.x + v1.y) + v2.z;
613        if num > 0.0{
614            let num = (num+1.0).sqrt();
615            let numh = 0.5 / num;
616            return Quat{
617                x: (v1.z - v2.y) * numh,
618                y: (v2.x - v0.z) * numh,
619                z: (v0.y - v1.x) * numh,
620                w: num * 0.5,
621            }
622        }
623        if (v0.x >= v1.y) && (v0.x >= v2.z){
624            let num = (((1.0+v0.x) - v1.y) - v2.z).sqrt();
625            let numh = 0.5 / num;
626            return Quat{
627                x: 0.5 * num,
628                y: (v0.y + v1.x) * numh,
629                z: (v0.z + v2.x) * numh,
630                w: (v1.z - v2.y) * numh
631            }
632        }
633        if v1.y > v2.z{
634            let num = ((((1.0+v1.y) - v0.x) - v2.z)).sqrt();
635            let numh = 0.5 / num;
636            return Quat{
637                x: (v1.x + v0.y) * numh,
638                y: 0.5 * num,
639                z: (v2.y + v1.z) * numh,
640                w: (v2.x - v0.z) * numh
641            }
642        }
643        let num = (((1.0 + v2.z) - v0.x) - v1.y).sqrt();
644        let numh = 0.5 / num;
645        Quat{
646            x: (v2.x + v0.z) * numh,
647            y: (v2.y + v1.z) * numh,
648            z: 0.5 * num,
649            w: (v0.y - v1.x) * numh
650        }
651    }
652    
653}
654
655/*
656pub fn vec4(x:f32, y:f32, z:f32, w:f32)->Vec4{
657    Vec4{x:x, y:y, z:z, w:w}
658}*/
659
660
661impl Mat4 {
662    pub const fn identity() -> Mat4 {
663        Mat4 {v: [
664            1.0,
665            0.0,
666            0.0,
667            0.0,
668            0.0,
669            1.0,
670            0.0,
671            0.0,
672            0.0,
673            0.0,
674            1.0,
675            0.0,
676            0.0,
677            0.0,
678            0.0,
679            1.0
680        ]}
681    }
682    
683    pub fn transpose(&self) -> Mat4 {
684        Mat4 {v: [
685            self.v[0],
686            self.v[4],
687            self.v[8],
688            self.v[12],
689            self.v[1],
690            self.v[5],
691            self.v[9],
692            self.v[13],
693            self.v[2],
694            self.v[6],
695            self.v[10],
696            self.v[14],
697            self.v[3],
698            self.v[7],
699            self.v[11],
700            self.v[15],
701        ]}
702    }
703    
704    pub fn txyz_s_ry_rx_txyz(t1: Vec3, s: f32, ry: f32, rx: f32, t2: Vec3) -> Mat4 {
705        
706        let cx = f32::cos(rx * TORAD);
707        let cy = f32::cos(ry * TORAD);
708        //let cz = f32::cos(r.z * TORAD);
709        let sx = f32::sin(rx * TORAD);
710        let sy = f32::sin(ry * TORAD);
711        //let sz = f32::sin(r.z * TORAD);
712        // y first, then x, then z
713        
714        // Y
715        // |  cy,  0,  sy  |
716        // |  0,   1,  0  |
717        // | -sy,  0,  cy  |
718        
719        // X:
720        // |  1,  0,  0  |
721        // |  0,  cx, -sx  |
722        // |  0,  sx,  cx  |
723        
724        // Z:
725        // |  cz, -sz,  0  |
726        // |  sz,  cz,  0  |
727        // |  0,    0,  1  |
728        
729        // X * Y
730        // | cy,           0,    sy |
731        // | -sx*-sy,     cx,   -sx*cy  |
732        // | -sy * cx,    sx,  cx*cy  |
733        
734        // Z * X * Y
735        // | cz * cy + -sz * -sx *-sy,   -sz * cx,    sy *cz + -sz * -sx * cy |
736        // | sz * cy + -sx*-sy * cz,     sz * cx,   sy * sz + cz * -sz * cy  |
737        // | -sy * cx,    sx,  cx*cy  |
738        
739        
740        // Y * X * Z
741        // | c*c,  c, s*s   |
742        // |   0,  c,  -s   |
743        // |  -s,  c*s, c*c |
744        
745        /*       
746        let m0 = s * (cz * cy + (-sz) * (-sx) *(-sy));
747        let m1 = s * (-sz * cx);
748        let m2 = s * (sy *cz + (-sz) * (-sx) * cy);
749        
750        let m4 = s * (sz * cy + (-sx)*(-sy) * cz);
751        let m5 = s * (sz * cx);
752        let m6 = s * (sy * sz + cz * (-sx) * cy);
753        
754        let m8 = s * (-sy*cx);
755        let m9 = s * (sx);
756        let m10 = s * (cx * cy);
757        */
758        
759        let m0 = s * (cy);
760        let m1 = s * (0.0);
761        let m2 = s * (sy);
762        
763        let m4 = s * (-sx * -sy);
764        let m5 = s * (cx);
765        let m6 = s * (-sx * cy);
766        
767        let m8 = s * (-sy * cx);
768        let m9 = s * (sx);
769        let m10 = s * (cx * cy);
770        
771        /*
772        let m0 = s * (cy * cz + sx * sy * sz);
773        let m1 = s * (-sz * cy + cz * sx * sy);
774        let m2 = s * (sy * cx);
775        
776        let m4 = s * (sz * cx);
777        let m5 = s * (cx * cz);
778        let m6 = s * (-sx);
779        
780        let m8 = s * (-sy * cz + cy * sx * sz);
781        let m9 = s * (sy * sz + cy * sx * cz);
782        let m10 = s * (cx * cy);
783        */
784        Mat4 {v: [
785            m0,
786            m4,
787            m8,
788            0.0,
789            m1,
790            m5,
791            m9,
792            0.0,
793            m2,
794            m6,
795            m10,
796            0.0,
797            t2.x + (m0 * t1.x + m1 * t1.y + m2 * t1.z),
798            t2.y + (m4 * t1.x + m5 * t1.y + m6 * t1.z),
799            t2.z + (m8 * t1.x + m9 * t1.y + m10 * t1.z),
800            1.0
801        ]}
802    }
803    
804    pub fn perspective(fov_y: f32, aspect: f32, near: f32, far: f32) -> Mat4 {
805        let f = 1.0 / f32::tan(fov_y * TORAD / 2.0);
806        let nf = 1.0 / (near - far);
807        Mat4 {v: [
808            f / aspect,
809            0.0,
810            0.0,
811            0.0,
812            0.0,
813            f,
814            0.0,
815            0.0,
816            0.0,
817            0.0,
818            (far + near) * nf,
819            -1.0,
820            0.0,
821            0.0,
822            (2.0 * far * near) * nf,
823            0.0
824        ]}
825    }
826    
827    pub fn from_camera_fov(fov:&CameraFov, near: f32, far: f32) -> Mat4 {
828        let tan_left = fov.angle_left.tan();
829        let tan_right = fov.angle_right.tan();
830        let tan_down = fov.angle_down.tan();
831        let tan_up = fov.angle_up.tan();
832        
833        let tan_height = tan_up - tan_down;
834        let tan_width = tan_right - tan_left;
835        
836        if far <= near{
837            Mat4 {v: [
838                2.0 / tan_width,
839                0.0,
840                0.0,
841                0.0,
842                
843                0.0,
844                2.0 / tan_height,
845                0.0,
846                0.0,
847                
848                (tan_right + tan_left) / tan_width,
849                (tan_up + tan_down) / tan_height,
850                -1.0,
851                -1.0,
852                
853                0.0,
854                0.0,
855                - 2.0 * near,
856                0.0,
857            ]}
858        }
859        else{
860            Mat4 {v: [
861                2.0 / tan_width,
862                0.0,
863                0.0,
864                0.0,
865                                
866                0.0,
867                2.0 / tan_height,
868                0.0,
869                0.0,
870                                
871                (tan_right + tan_left) / tan_width,
872                (tan_up + tan_down) / tan_height,
873                -(far + near) / (far - near),
874                -1.0,
875                                
876                0.0,
877                0.0,
878                -(far * 2.0 * near)/ (far - near),
879                0.0,
880            ]}
881        }
882    }
883    
884    pub const fn translation(v:Vec3) -> Mat4 {
885        Mat4 {v: [
886            1.0,
887            0.0,
888            0.0,
889            0.0,
890            0.0,
891            1.0,
892            0.0,
893            0.0,
894            0.0,
895            0.0,
896            1.0,
897            0.0,
898            v.x,
899            v.y,
900            v.z,
901            1.0
902        ]}
903        
904    }
905    
906    pub const fn nonuniform_scaled_translation(s:Vec3,  t:Vec3) -> Mat4 {
907        Mat4 {v: [
908            s.x,
909            0.0,
910            0.0,
911            0.0,
912            0.0,
913            s.y,
914            0.0,
915            0.0,
916            0.0,
917            0.0,
918            s.z,
919            0.0,
920            t.x,
921            t.y,
922            t.z,
923            1.0
924        ]}
925    }
926    
927    pub const fn scaled_translation(s:f32,  t:Vec3) -> Mat4 {
928        Mat4 {v: [
929            s,
930            0.0,
931            0.0,
932            0.0,
933            0.0,
934            s,
935            0.0,
936            0.0,
937            0.0,
938            0.0,
939            s,
940            0.0,
941            t.x,
942            t.y,
943            t.z,
944            1.0
945        ]}
946    }
947        
948    pub const fn scale(s: f32) -> Mat4 {
949        Mat4 {v: [
950            s,
951            0.0,
952            0.0,
953            0.0,
954            0.0,
955            s,
956            0.0,
957            0.0,
958            0.0,
959            0.0,
960            s,
961            0.0,
962            0.0,
963            0.0,
964            0.0,
965            1.0
966        ]}
967                
968    }
969    
970    pub fn rotation(r:Vec3) -> Mat4 {
971        //const TORAD: f32 = 0.017453292;
972        let cx = f32::cos(r.x);
973        let cy = f32::cos(r.y);
974        let cz = f32::cos(r.z);
975        let sx = f32::sin(r.x);
976        let sy = f32::sin(r.y);
977        let sz = f32::sin(r.z);
978        let m0 = cy * cz + sx * sy * sz;
979        let m1 = -sz * cy + cz * sx * sy;
980        let m2 = sy * cx;
981        let m4 = sz * cx;
982        let m5 = cx * cz;
983        let m6 = -sx;
984        let m8 = -sy * cz + cy * sx * sz;
985        let m9 = sy * sz + cy * sx * cz;
986        let m10 = cx * cy;
987        Mat4 {v: [
988            m0,
989            m4,
990            m8,
991            0.0,
992            m1,
993            m5,
994            m9,
995            0.0,
996            m2,
997            m6,
998            m10,
999            0.0,
1000            0.0,
1001            0.0,
1002            0.0,
1003            1.0
1004        ]}
1005    }
1006    
1007    pub fn ortho(left: f32, right: f32, top: f32, bottom: f32, near: f32, far: f32, scalex: f32, scaley: f32) -> Mat4 {
1008        let lr = 1.0 / (left - right);
1009        let bt = 1.0 / (bottom - top);
1010        let nf = 1.0 / (near - far);
1011        /*return Mat4{v:[
1012            -2.0 * lr * scalex, 0.0, 0.0, (left+right) * lr,
1013            0.0, -2.0 * bt * scaley, 0.0, (top+bottom) * bt,
1014            0.0, 0.0, 2.0 * nf, (far + near) * nf,
1015            0.0, 0.0, 0.0, 1.0
1016        ]}*/
1017        Mat4 {v: [
1018            -2.0 * lr * scalex,
1019            0.0,
1020            0.0,
1021            0.0,
1022            0.0,
1023            -2.0 * bt * scaley,
1024            0.0,
1025            0.0,
1026            0.0,
1027            0.0,
1028            -1.0 * nf,
1029            0.0,
1030            (left + right) * lr,
1031            (top + bottom) * bt,
1032            0.5 + (far + near) * nf,
1033            1.0
1034        ]}
1035    }
1036    
1037    pub fn transform_vec4(&self, v: Vec4) -> Vec4 {
1038        let m = &self.v;
1039        Vec4 {
1040            x: m[0] * v.x + m[4] * v.y + m[8] * v.z + m[12] * v.w,
1041            y: m[1] * v.x + m[5] * v.y + m[9] * v.z + m[13] * v.w,
1042            z: m[2] * v.x + m[6] * v.y + m[10] * v.z + m[14] * v.w,
1043            w: m[3] * v.x + m[7] * v.y + m[11] * v.z + m[15] * v.w
1044        }
1045    }
1046    
1047    pub fn mul(a: &Mat4, b: &Mat4) -> Mat4 {
1048        // this is probably stupid. Programmed JS for too long.
1049        let a = &a.v;
1050        let b = &b.v;
1051        #[inline]
1052        fn d(i: &[f32; 16], x: usize, y: usize) -> f32 {i[x + 4 * y]}
1053        Mat4 {
1054            v: [
1055                d(a, 0, 0) * d(b, 0, 0) + d(a, 1, 0) * d(b, 0, 1) + d(a, 2, 0) * d(b, 0, 2) + d(a, 3, 0) * d(b, 0, 3),
1056                d(a, 0, 0) * d(b, 1, 0) + d(a, 1, 0) * d(b, 1, 1) + d(a, 2, 0) * d(b, 1, 2) + d(a, 3, 0) * d(b, 1, 3),
1057                d(a, 0, 0) * d(b, 2, 0) + d(a, 1, 0) * d(b, 2, 1) + d(a, 2, 0) * d(b, 2, 2) + d(a, 3, 0) * d(b, 2, 3),
1058                d(a, 0, 0) * d(b, 3, 0) + d(a, 1, 0) * d(b, 3, 1) + d(a, 2, 0) * d(b, 3, 2) + d(a, 3, 0) * d(b, 3, 3),
1059                d(a, 0, 1) * d(b, 0, 0) + d(a, 1, 1) * d(b, 0, 1) + d(a, 2, 1) * d(b, 0, 2) + d(a, 3, 1) * d(b, 0, 3),
1060                d(a, 0, 1) * d(b, 1, 0) + d(a, 1, 1) * d(b, 1, 1) + d(a, 2, 1) * d(b, 1, 2) + d(a, 3, 1) * d(b, 1, 3),
1061                d(a, 0, 1) * d(b, 2, 0) + d(a, 1, 1) * d(b, 2, 1) + d(a, 2, 1) * d(b, 2, 2) + d(a, 3, 1) * d(b, 2, 3),
1062                d(a, 0, 1) * d(b, 3, 0) + d(a, 1, 1) * d(b, 3, 1) + d(a, 2, 1) * d(b, 3, 2) + d(a, 3, 1) * d(b, 3, 3),
1063                d(a, 0, 2) * d(b, 0, 0) + d(a, 1, 2) * d(b, 0, 1) + d(a, 2, 2) * d(b, 0, 2) + d(a, 3, 2) * d(b, 0, 3),
1064                d(a, 0, 2) * d(b, 1, 0) + d(a, 1, 2) * d(b, 1, 1) + d(a, 2, 2) * d(b, 1, 2) + d(a, 3, 2) * d(b, 1, 3),
1065                d(a, 0, 2) * d(b, 2, 0) + d(a, 1, 2) * d(b, 2, 1) + d(a, 2, 2) * d(b, 2, 2) + d(a, 3, 2) * d(b, 2, 3),
1066                d(a, 0, 2) * d(b, 3, 0) + d(a, 1, 2) * d(b, 3, 1) + d(a, 2, 2) * d(b, 3, 2) + d(a, 3, 2) * d(b, 3, 3),
1067                d(a, 0, 3) * d(b, 0, 0) + d(a, 1, 3) * d(b, 0, 1) + d(a, 2, 3) * d(b, 0, 2) + d(a, 3, 3) * d(b, 0, 3),
1068                d(a, 0, 3) * d(b, 1, 0) + d(a, 1, 3) * d(b, 1, 1) + d(a, 2, 3) * d(b, 1, 2) + d(a, 3, 3) * d(b, 1, 3),
1069                d(a, 0, 3) * d(b, 2, 0) + d(a, 1, 3) * d(b, 2, 1) + d(a, 2, 3) * d(b, 2, 2) + d(a, 3, 3) * d(b, 2, 3),
1070                d(a, 0, 3) * d(b, 3, 0) + d(a, 1, 3) * d(b, 3, 1) + d(a, 2, 3) * d(b, 3, 2) + d(a, 3, 3) * d(b, 3, 3),
1071            ]
1072        }
1073    }
1074    
1075    pub fn invert(&self) -> Mat4 {
1076        let a = &self.v;
1077        let a00 = a[0];
1078        let a01 = a[1];
1079        let a02 = a[2];
1080        let a03 = a[3];
1081        let a10 = a[4];
1082        let a11 = a[5];
1083        let a12 = a[6];
1084        let a13 = a[7];
1085        let a20 = a[8];
1086        let a21 = a[9];
1087        let a22 = a[10];
1088        let a23 = a[11];
1089        let a30 = a[12];
1090        let a31 = a[13];
1091        let a32 = a[14];
1092        let a33 = a[15];
1093        
1094        let b00 = a00 * a11 - a01 * a10;
1095        let b01 = a00 * a12 - a02 * a10;
1096        let b02 = a00 * a13 - a03 * a10;
1097        let b03 = a01 * a12 - a02 * a11;
1098        let b04 = a01 * a13 - a03 * a11;
1099        let b05 = a02 * a13 - a03 * a12;
1100        let b06 = a20 * a31 - a21 * a30;
1101        let b07 = a20 * a32 - a22 * a30;
1102        let b08 = a20 * a33 - a23 * a30;
1103        let b09 = a21 * a32 - a22 * a31;
1104        let b10 = a21 * a33 - a23 * a31;
1105        let b11 = a22 * a33 - a23 * a32;
1106        
1107        // Calculate the determinant
1108        let det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
1109        
1110        if det == 0.0 {
1111            return Mat4::identity();
1112        }
1113        
1114        let idet = 1.0 / det;
1115        Mat4 {
1116            v: [
1117                (a11 * b11 - a12 * b10 + a13 * b09) * idet,
1118                (a02 * b10 - a01 * b11 - a03 * b09) * idet,
1119                (a31 * b05 - a32 * b04 + a33 * b03) * idet,
1120                (a22 * b04 - a21 * b05 - a23 * b03) * idet,
1121                (a12 * b08 - a10 * b11 - a13 * b07) * idet,
1122                (a00 * b11 - a02 * b08 + a03 * b07) * idet,
1123                (a32 * b02 - a30 * b05 - a33 * b01) * idet,
1124                (a20 * b05 - a22 * b02 + a23 * b01) * idet,
1125                (a10 * b10 - a11 * b08 + a13 * b06) * idet,
1126                (a01 * b08 - a00 * b10 - a03 * b06) * idet,
1127                (a30 * b04 - a31 * b02 + a33 * b00) * idet,
1128                (a21 * b02 - a20 * b04 - a23 * b00) * idet,
1129                (a11 * b07 - a10 * b09 - a12 * b06) * idet,
1130                (a00 * b09 - a01 * b07 + a02 * b06) * idet,
1131                (a31 * b01 - a30 * b03 - a32 * b00) * idet,
1132                (a20 * b03 - a21 * b01 + a22 * b00) * idet,
1133            ]
1134        }
1135    }
1136}
1137
1138
1139//------ Vec2 operators
1140
1141impl ops::Add<Vec2> for Vec2 {
1142    type Output = Vec2;
1143    fn add(self, rhs: Vec2) -> Vec2 {
1144        Vec2 {x: self.x + rhs.x, y: self.y + rhs.y}
1145    }
1146}
1147
1148impl ops::Sub<Vec2> for Vec2 {
1149    type Output = Vec2;
1150    fn sub(self, rhs: Vec2) -> Vec2 {
1151        Vec2 {x: self.x - rhs.x, y: self.y - rhs.y}
1152    }
1153}
1154
1155impl ops::Mul<Vec2> for Vec2 {
1156    type Output = Vec2;
1157    fn mul(self, rhs: Vec2) -> Vec2 {
1158        Vec2 {x: self.x * rhs.x, y: self.y * rhs.y}
1159    }
1160}
1161
1162impl ops::Div<Vec2> for Vec2 {
1163    type Output = Vec2;
1164    fn div(self, rhs: Vec2) -> Vec2 {
1165        Vec2 {x: self.x / rhs.x, y: self.y / rhs.y}
1166    }
1167}
1168
1169
1170impl ops::Add<Vec2> for f32 {
1171    type Output = Vec2;
1172    fn add(self, rhs: Vec2) -> Vec2 {
1173        Vec2 {x: self + rhs.x, y: self + rhs.y}
1174    }
1175}
1176
1177impl ops::Sub<Vec2> for f32 {
1178    type Output = Vec2;
1179    fn sub(self, rhs: Vec2) -> Vec2 {
1180        Vec2 {x: self -rhs.x, y: self -rhs.y}
1181    }
1182}
1183
1184impl ops::Mul<Vec2> for f32 {
1185    type Output = Vec2;
1186    fn mul(self, rhs: Vec2) -> Vec2 {
1187        Vec2 {x: self *rhs.x, y: self *rhs.y}
1188    }
1189}
1190
1191impl ops::Div<Vec2> for f32 {
1192    type Output = Vec2;
1193    fn div(self, rhs: Vec2) -> Vec2 {
1194        Vec2 {x: self / rhs.x, y: self / rhs.y}
1195    }
1196}
1197
1198
1199impl ops::Add<f32> for Vec2 {
1200    type Output = Vec2;
1201    fn add(self, rhs: f32) -> Vec2 {
1202        Vec2 {x: self.x + rhs, y: self.y + rhs}
1203    }
1204}
1205
1206impl ops::Sub<f32> for Vec2 {
1207    type Output = Vec2;
1208    fn sub(self, rhs: f32) -> Vec2 {
1209        Vec2 {x: self.x - rhs, y: self.y - rhs}
1210    }
1211}
1212
1213impl ops::Mul<f32> for Vec2 {
1214    type Output = Vec2;
1215    fn mul(self, rhs: f32) -> Vec2 {
1216        Vec2 {x: self.x * rhs, y: self.y * rhs}
1217    }
1218}
1219
1220impl ops::Div<f32> for Vec2 {
1221    type Output = Vec2;
1222    fn div(self, rhs: f32) -> Vec2 {
1223        Vec2 {x: self.x / rhs, y: self.y / rhs}
1224    }
1225}
1226
1227impl ops::AddAssign<Vec2> for Vec2 {
1228    fn add_assign(&mut self, rhs: Vec2) {
1229        self.x = self.x + rhs.x;
1230        self.y = self.y + rhs.y;
1231    }
1232}
1233
1234impl ops::SubAssign<Vec2> for Vec2 {
1235    fn sub_assign(&mut self, rhs: Vec2) {
1236        self.x = self.x - rhs.x;
1237        self.y = self.y - rhs.y;
1238    }
1239}
1240
1241impl ops::MulAssign<Vec2> for Vec2 {
1242    fn mul_assign(&mut self, rhs: Vec2) {
1243        self.x = self.x * rhs.x;
1244        self.y = self.y * rhs.y;
1245    }
1246}
1247
1248impl ops::DivAssign<Vec2> for Vec2 {
1249    fn div_assign(&mut self, rhs: Vec2) {
1250        self.x = self.x / rhs.x;
1251        self.y = self.y / rhs.y;
1252    }
1253}
1254
1255
1256impl ops::AddAssign<f32> for Vec2 {
1257    fn add_assign(&mut self, rhs: f32) {
1258        self.x = self.x + rhs;
1259        self.y = self.y + rhs;
1260    }
1261}
1262
1263impl ops::SubAssign<f32> for Vec2 {
1264    fn sub_assign(&mut self, rhs: f32) {
1265        self.x = self.x - rhs;
1266        self.y = self.y - rhs;
1267    }
1268}
1269
1270impl ops::MulAssign<f32> for Vec2 {
1271    fn mul_assign(&mut self, rhs: f32) {
1272        self.x = self.x * rhs;
1273        self.y = self.y * rhs;
1274    }
1275}
1276
1277impl ops::DivAssign<f32> for Vec2 {
1278    fn div_assign(&mut self, rhs: f32) {
1279        self.x = self.x / rhs;
1280        self.y = self.y / rhs;
1281    }
1282}
1283
1284impl ops::Neg for Vec2 {
1285    type Output = Vec2;
1286    fn neg(self) -> Self { Vec2{x:-self.x, y:-self.y}}
1287}
1288
1289impl ops::Neg for Vec3{
1290    type Output = Vec3;
1291    fn neg(self) -> Self { Vec3{x:-self.x, y:-self.y, z:-self.z}}
1292}
1293
1294impl ops::Neg for Vec4 {
1295    type Output = Vec4;
1296    fn neg(self) -> Self { Vec4{x:-self.x, y:-self.y, z:-self.z, w:-self.w}}
1297}
1298
1299
1300//------ Vec3 operators
1301
1302impl ops::Add<Vec3> for Vec3 {
1303    type Output = Vec3;
1304    fn add(self, rhs: Vec3) -> Vec3 {
1305        Vec3 {x: self.x + rhs.x, y: self.y + rhs.y, z: self.z + rhs.z}
1306    }
1307}
1308
1309impl ops::Sub<Vec3> for Vec3 {
1310    type Output = Vec3;
1311    fn sub(self, rhs: Vec3) -> Vec3 {
1312        Vec3 {x: self.x - rhs.x, y: self.y - rhs.y, z: self.z - rhs.z}
1313    }
1314}
1315
1316impl ops::Mul<Vec3> for Vec3 {
1317    type Output = Vec3;
1318    fn mul(self, rhs: Vec3) -> Vec3 {
1319        Vec3 {x: self.x * rhs.x, y: self.y * rhs.y, z: self.z * rhs.z}
1320    }
1321}
1322
1323impl ops::Div<Vec3> for Vec3 {
1324    type Output = Vec3;
1325    fn div(self, rhs: Vec3) -> Vec3 {
1326        Vec3 {x: self.x / rhs.x, y: self.y / rhs.y, z: self.z / rhs.z}
1327    }
1328}
1329
1330impl ops::Add<Vec3> for f32 {
1331    type Output = Vec3;
1332    fn add(self, rhs: Vec3) -> Vec3 {
1333        Vec3 {x: self + rhs.x, y: self + rhs.y, z: self + rhs.z}
1334    }
1335}
1336
1337impl ops::Sub<Vec3> for f32 {
1338    type Output = Vec3;
1339    fn sub(self, rhs: Vec3) -> Vec3 {
1340        Vec3 {x: self -rhs.x, y: self -rhs.y, z: self -rhs.z}
1341    }
1342}
1343
1344impl ops::Mul<Vec3> for f32 {
1345    type Output = Vec3;
1346    fn mul(self, rhs: Vec3) -> Vec3 {
1347        Vec3 {x: self *rhs.x, y: self *rhs.y, z: self *rhs.z}
1348    }
1349}
1350
1351impl ops::Div<Vec3> for f32 {
1352    type Output = Vec3;
1353    fn div(self, rhs: Vec3) -> Vec3 {
1354        Vec3 {x: self / rhs.x, y: self / rhs.y, z: self / rhs.z}
1355    }
1356}
1357
1358
1359impl ops::Add<f32> for Vec3 {
1360    type Output = Vec3;
1361    fn add(self, rhs: f32) -> Vec3 {
1362        Vec3 {x: self.x + rhs, y: self.y + rhs, z: self.z + rhs}
1363    }
1364}
1365
1366impl ops::Sub<f32> for Vec3 {
1367    type Output = Vec3;
1368    fn sub(self, rhs: f32) -> Vec3 {
1369        Vec3 {x: self.x - rhs, y: self.y - rhs, z: self.z - rhs}
1370    }
1371}
1372
1373impl ops::Mul<f32> for Vec3 {
1374    type Output = Vec3;
1375    fn mul(self, rhs: f32) -> Vec3 {
1376        Vec3 {x: self.x * rhs, y: self.y * rhs, z: self.z * rhs}
1377    }
1378}
1379
1380impl ops::Div<f32> for Vec3 {
1381    type Output = Vec3;
1382    fn div(self, rhs: f32) -> Vec3 {
1383        Vec3 {x: self.x / rhs, y: self.y / rhs, z: self.z / rhs}
1384    }
1385}
1386
1387
1388impl ops::AddAssign<Vec3> for Vec3 {
1389    fn add_assign(&mut self, rhs: Vec3) {
1390        self.x = self.x + rhs.x;
1391        self.y = self.y + rhs.y;
1392        self.z = self.z + rhs.z;
1393    }
1394}
1395
1396impl ops::SubAssign<Vec3> for Vec3 {
1397    fn sub_assign(&mut self, rhs: Vec3) {
1398        self.x = self.x - rhs.x;
1399        self.y = self.y - rhs.y;
1400        self.z = self.z - rhs.z;
1401    }
1402}
1403
1404impl ops::MulAssign<Vec3> for Vec3 {
1405    fn mul_assign(&mut self, rhs: Vec3) {
1406        self.x = self.x * rhs.x;
1407        self.y = self.y * rhs.y;
1408        self.z = self.z * rhs.z;
1409    }
1410}
1411
1412impl ops::DivAssign<Vec3> for Vec3 {
1413    fn div_assign(&mut self, rhs: Vec3) {
1414        self.x = self.x / rhs.x;
1415        self.y = self.y / rhs.y;
1416        self.z = self.z / rhs.z;
1417    }
1418}
1419
1420
1421impl ops::AddAssign<f32> for Vec3 {
1422    fn add_assign(&mut self, rhs: f32) {
1423        self.x = self.x + rhs;
1424        self.y = self.y + rhs;
1425        self.z = self.z + rhs;
1426    }
1427}
1428
1429impl ops::SubAssign<f32> for Vec3 {
1430    fn sub_assign(&mut self, rhs: f32) {
1431        self.x = self.x - rhs;
1432        self.y = self.y - rhs;
1433        self.z = self.z - rhs;
1434    }
1435}
1436
1437impl ops::MulAssign<f32> for Vec3 {
1438    fn mul_assign(&mut self, rhs: f32) {
1439        self.x = self.x * rhs;
1440        self.y = self.y * rhs;
1441        self.z = self.z * rhs;
1442    }
1443}
1444
1445impl ops::DivAssign<f32> for Vec3 {
1446    fn div_assign(&mut self, rhs: f32) {
1447        self.x = self.x / rhs;
1448        self.y = self.y / rhs;
1449        self.z = self.z / rhs;
1450    }
1451}
1452
1453//------ Vec4 operators
1454
1455impl ops::Add<Vec4> for Vec4 {
1456    type Output = Vec4;
1457    fn add(self, rhs: Vec4) -> Vec4 {
1458        Vec4 {x: self.x + rhs.x, y: self.y + rhs.y, z: self.z + rhs.z, w: self.w + rhs.w}
1459    }
1460}
1461
1462impl ops::Sub<Vec4> for Vec4 {
1463    type Output = Vec4;
1464    fn sub(self, rhs: Vec4) -> Vec4 {
1465        Vec4 {x: self.x - rhs.x, y: self.y - rhs.y, z: self.z - rhs.z, w: self.w - rhs.w}
1466    }
1467}
1468
1469impl ops::Mul<Vec4> for Vec4 {
1470    type Output = Vec4;
1471    fn mul(self, rhs: Vec4) -> Vec4 {
1472        Vec4 {x: self.x * rhs.x, y: self.y * rhs.y, z: self.z * rhs.z, w: self.w * rhs.w}
1473    }
1474}
1475
1476impl ops::Div<Vec4> for Vec4 {
1477    type Output = Vec4;
1478    fn div(self, rhs: Vec4) -> Vec4 {
1479        Vec4 {x: self.x / rhs.x, y: self.y / rhs.y, z: self.z / rhs.z, w: self.w / rhs.w}
1480    }
1481}
1482
1483impl ops::Add<Vec4> for f32 {
1484    type Output = Vec4;
1485    fn add(self, rhs: Vec4) -> Vec4 {
1486        Vec4 {x: self + rhs.x, y: self + rhs.y, z: self + rhs.z, w: self + rhs.z}
1487    }
1488}
1489
1490impl ops::Sub<Vec4> for f32 {
1491    type Output = Vec4;
1492    fn sub(self, rhs: Vec4) -> Vec4 {
1493        Vec4 {x: self -rhs.x, y: self -rhs.y, z: self -rhs.z, w: self -rhs.z}
1494    }
1495}
1496
1497impl ops::Mul<Vec4> for f32 {
1498    type Output = Vec4;
1499    fn mul(self, rhs: Vec4) -> Vec4 {
1500        Vec4 {x: self *rhs.x, y: self *rhs.y, z: self *rhs.z, w: self *rhs.z}
1501    }
1502}
1503
1504impl ops::Div<Vec4> for f32 {
1505    type Output = Vec4;
1506    fn div(self, rhs: Vec4) -> Vec4 {
1507        Vec4 {x: self / rhs.x, y: self / rhs.y, z: self / rhs.z, w: self / rhs.z}
1508    }
1509}
1510
1511
1512impl ops::Add<f32> for Vec4 {
1513    type Output = Vec4;
1514    fn add(self, rhs: f32) -> Vec4 {
1515        Vec4 {x: self.x + rhs, y: self.y + rhs, z: self.z + rhs, w: self.w + rhs}
1516    }
1517}
1518
1519impl ops::Sub<f32> for Vec4 {
1520    type Output = Vec4;
1521    fn sub(self, rhs: f32) -> Vec4 {
1522        Vec4 {x: self.x - rhs, y: self.y - rhs, z: self.z - rhs, w: self.w - rhs}
1523    }
1524}
1525
1526impl ops::Mul<f32> for Vec4 {
1527    type Output = Vec4;
1528    fn mul(self, rhs: f32) -> Vec4 {
1529        Vec4 {x: self.x * rhs, y: self.y * rhs, z: self.z * rhs, w: self.w * rhs}
1530    }
1531}
1532
1533impl ops::Div<f32> for Vec4 {
1534    type Output = Vec4;
1535    fn div(self, rhs: f32) -> Vec4 {
1536        Vec4 {x: self.x / rhs, y: self.y / rhs, z: self.z / rhs, w: self.w / rhs}
1537    }
1538}
1539
1540impl ops::AddAssign<Vec4> for Vec4 {
1541    fn add_assign(&mut self, rhs: Vec4) {
1542        self.x = self.x + rhs.x;
1543        self.y = self.y + rhs.y;
1544        self.z = self.z + rhs.z;
1545        self.w = self.w + rhs.w;
1546    }
1547}
1548
1549impl ops::SubAssign<Vec4> for Vec4 {
1550    fn sub_assign(&mut self, rhs: Vec4) {
1551        self.x = self.x - rhs.x;
1552        self.y = self.y - rhs.y;
1553        self.z = self.z - rhs.z;
1554        self.w = self.w - rhs.w;
1555    }
1556}
1557
1558impl ops::MulAssign<Vec4> for Vec4 {
1559    fn mul_assign(&mut self, rhs: Vec4) {
1560        self.x = self.x * rhs.x;
1561        self.y = self.y * rhs.y;
1562        self.z = self.z * rhs.z;
1563        self.w = self.w * rhs.w;
1564    }
1565}
1566
1567impl ops::DivAssign<Vec4> for Vec4 {
1568    fn div_assign(&mut self, rhs: Vec4) {
1569        self.x = self.x / rhs.x;
1570        self.y = self.y / rhs.y;
1571        self.z = self.z / rhs.z;
1572        self.w = self.w / rhs.w;
1573    }
1574}
1575
1576
1577impl ops::AddAssign<f32> for Vec4 {
1578    fn add_assign(&mut self, rhs: f32) {
1579        self.x = self.x + rhs;
1580        self.y = self.y + rhs;
1581        self.z = self.z + rhs;
1582        self.w = self.w + rhs;
1583    }
1584}
1585
1586impl ops::SubAssign<f32> for Vec4 {
1587    fn sub_assign(&mut self, rhs: f32) {
1588        self.x = self.x - rhs;
1589        self.y = self.y - rhs;
1590        self.z = self.z - rhs;
1591        self.w = self.w - rhs;
1592    }
1593}
1594
1595impl ops::MulAssign<f32> for Vec4 {
1596    fn mul_assign(&mut self, rhs: f32) {
1597        self.x = self.x * rhs;
1598        self.y = self.y * rhs;
1599        self.z = self.z * rhs;
1600        self.w = self.w * rhs;
1601    }
1602}
1603
1604impl ops::DivAssign<f32> for Vec4 {
1605    fn div_assign(&mut self, rhs: f32) {
1606        self.x = self.x / rhs;
1607        self.y = self.y / rhs;
1608        self.z = self.z / rhs;
1609        self.w = self.w / rhs;
1610    }
1611}
1612
1613