1use{
2 std::{fmt,ops},
3 crate::math_f64::*,
4};
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)]
26pub struct Mat4 {
27 pub v: [f32; 16],
28}
29
30impl Default for Mat4{
31 fn default()->Self{
32 Self{v:[1.,0.,0.,0., 0.,1.,0.,0., 0.,0.,1.,0., 0.,0.,0.,1.]}
33 }
34}
35
36#[derive(Copy, Clone, Debug, PartialEq)]
37pub enum Vec2Index{
38 X,
39 Y
40}
41
42#[derive(Clone, Copy, Default, PartialEq, Debug)]
43pub struct Transform {
44 pub orientation: Quat,
45 pub position: Vec3
46}
47
48impl Transform {
49 pub fn to_mat4(&self) -> Mat4 {
50 let q = self.orientation;
51 let t = self.position;
52 Mat4 {v: [
53 (1.0 - 2.0 * q.b * q.b - 2.0 * q.c * q.c),
54 (2.0 * q.a * q.b - 2.0 * q.c * q.d),
55 (2.0 * q.a * q.c + 2.0 * q.b * q.d),
56 0.0,
57 (2.0 * q.a * q.b + 2.0 * q.c * q.d),
58 (1.0 - 2.0 * q.a * q.a - 2.0 * q.c * q.c),
59 (2.0 * q.b * q.c - 2.0 * q.a * q.d),
60 0.0,
61 (2.0 * q.a * q.c - 2.0 * q.b * q.d),
62 (2.0 * q.b * q.c + 2.0 * q.a * q.d),
63 (1.0 - 2.0 * q.a * q.a - 2.0 * q.b * q.b),
64 0.0,
65 t.x,
66 t.y,
67 t.z,
68 1.0
69 ]}
70 }
71
72 pub fn from_lerp(a: Transform, b: Transform, f: f32) -> Self {
73 Transform {
74 orientation: Quat::from_slerp(a.orientation, b.orientation, f),
75 position: Vec3::from_lerp(a.position, b.position, f)
76 }
77 }
78
79 pub fn from_slerp_orientation(a: Transform, b: Transform, f: f32) -> Self {
80 Transform {
81 orientation: Quat::from_slerp(a.orientation, b.orientation, f),
82 position: b.position
83 }
84 }
85}
86
87#[derive(Clone, Copy, Default, Debug, PartialEq)]
88pub struct Vec2 {
89 pub x: f32,
90 pub y: f32,
91}
92
93
94impl Vec2 {
95 pub fn new() -> Vec2 {
96 Vec2::default()
97 }
98
99 pub fn index(&self, index:Vec2Index)->f32{
100 match index{
101 Vec2Index::X=>self.x,
102 Vec2Index::Y=>self.y
103 }
104 }
105
106 pub fn set_index(&mut self, index:Vec2Index, v: f32){
107 match index{
108 Vec2Index::X=>{self.x = v},
109 Vec2Index::Y=>{self.y = v}
110 }
111 }
112
113
114 pub fn from_index_pair(index:Vec2Index, a: f32, b:f32)->Self{
115 match index{
116 Vec2Index::X=>{Self{x:a,y:b}},
117 Vec2Index::Y=>{Self{x:b,y:a}}
118 }
119 }
120
121
122 pub fn into_dvec2(self)->DVec2{
123 DVec2{x:self.x as f64, y:self.y as f64}
124 }
125
126 pub fn all(x: f32) -> Vec2 {
127 Vec2 {x, y: x}
128 }
129
130 pub fn from_lerp(a: Vec2, b: Vec2, f: f32) -> Vec2 {
131 let nf = 1.0 - f;
132 Vec2{
133 x: nf * a.x + f * b.x,
134 y: nf * a.y + f * b.y,
135 }
136 }
137
138 pub fn distance(&self, other: &Vec2) -> f32 {
139 let dx = self.x - other.x;
140 let dy = self.y - other.y;
141 (dx * dx + dy * dy).sqrt()
142 }
143
144 pub fn length(&self) -> f32 {
145 (self.x * self.x + self.y * self.y).sqrt()
146 }
147
148 pub fn to_vec3(&self) -> Vec3 {
149 Vec3 {x: self.x, y: self.y, z: 0.0}
150 }
151}
152
153
154impl fmt::Display for Vec2 {
155 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
157 write!(f,"vec2({},{})",self.x, self.y)
158 }
159}
160
161impl fmt::Display for Vec3 {
162 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
164 write!(f,"vec3({},{},{})",self.x, self.y, self.z)
165 }
166}
167
168pub fn vec2(x: f32, y: f32) -> Vec2 {Vec2 {x, y}}
169pub fn vec3(x: f32, y: f32, z: f32) -> Vec3 {Vec3 {x, y, z}}
170pub fn vec4(x: f32, y: f32, z: f32, w: f32) -> Vec4 {Vec4 {x, y, z, w}}
171
172const TORAD: f32 = 0.017453292;
173const TODEG: f32 = 57.29578;
174
175#[derive(Clone, Copy, Default, PartialEq, Debug)]
181pub struct Vec3 {
182 pub x: f32,
183 pub y: f32,
184 pub z: f32
185}
186
187impl Vec3 {
188
189 pub fn from_lerp(a: Vec3, b: Vec3, f: f32) -> Vec3 {
190 Vec3 {
191 x: (b.x - a.x) * f + a.x,
192 y: (b.y - a.y) * f + a.y,
193 z: (b.z - a.z) * f + a.z
194 }
195 }
196
197 pub fn all(x: f32) -> Vec3 {
198 Vec3 {x, y: x, z: x}
199 }
200
201 pub fn to_vec2(&self) -> Vec2 {
202 Vec2 {x: self.x, y: self.y}
203 }
204
205 pub fn scale(&self, f: f32) -> Vec3 {
206 Vec3 {x: self.x * f, y: self.y * f, z: self.z * f}
207 }
208
209 pub fn cross(a: Vec3, b: Vec3) -> Vec3 {
210 Vec3 {
211 x: a.y * b.z - a.z * b.y,
212 y: a.z * b.x - a.x * b.z,
213 z: a.x * b.y - a.y * b.x
214 }
215 }
216
217 pub fn dot(&self, other: Vec3) -> f32 {
218 self.x * other.x + self.y * other.y + self.z * other.z
219 }
220
221 pub fn normalize(&self) -> Vec3 {
222 let sz = self.x * self.x + self.y * self.y + self.z * self.z;
223 if sz > 0.0 {
224 let sr = 1.0 / sz.sqrt();
225 return Vec3 {
226 x: self.x * sr,
227 y: self.y * sr,
228 z: self.z * sr
229 };
230 }
231 Vec3::default()
232 }
233}
234
235#[derive(Clone, Copy, Default, Debug, PartialEq)]
243pub struct Plane {
244 pub a: f32,
245 pub b: f32,
246 pub c: f32,
247 pub d: f32
248}
249
250impl Plane {
251 pub fn from_point_normal(p: Vec3, normal: Vec3) -> Self {
252 let n = normal.normalize();
253 Self {
254 a: n.x,
255 b: n.y,
256 c: n.z,
257 d: -p.dot(n)
258 }
259 }
260
261 pub fn from_points(p1: Vec3, p2: Vec3, p3: Vec3) -> Self {
262 let normal = Vec3::cross(p2 - p1, p3 - p1);
263 Self::from_point_normal(p1, normal)
264 }
265
266 pub fn intersect_line(&self, v1: Vec3, v2: Vec3) -> Vec3 {
267 let diff = v1 - v2;
268 let denom = self.a * diff.x + self.b * diff.y + self.c * diff.z;
269 if denom == 0.0 {
270 return (v1 * v2) * 0.5
271 }
272 let u = (self.a * v1.x + self.b * v1.y + self.c * v1.z + self.d) / denom;
273 v1 + (v2 - v1) * u
274 }
275}
276
277
278
279#[derive(Clone, Copy, Default, Debug,PartialEq)]
280pub struct Vec4 {
281 pub x: f32,
282 pub y: f32,
283 pub z: f32,
284 pub w: f32
285}
286
287impl fmt::Display for Vec4 {
288 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
289 write!(
290 f,
291 "vec4({}, {}, {}, {})",
292 PrettyPrintedF32(self.x),
293 PrettyPrintedF32(self.y),
294 PrettyPrintedF32(self.z),
295 PrettyPrintedF32(self.w),
296 )
297 }
298}
299
300impl Vec4 {
301 pub fn all(v: f32) -> Self {
302 Self {x: v, y: v, z: v, w: v}
303 }
304
305 pub fn to_vec3(&self) -> Vec3 {
306 Vec3 {x: self.x, y: self.y, z: self.z}
307 }
308
309 pub fn dot(&self, other: Vec4) -> f32 {
310 self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w
311 }
312
313 pub fn from_lerp(a: Vec4, b: Vec4, f: f32) -> Vec4 {
314 let nf = 1.0 - f;
315 Vec4 {
316 x: nf * a.x + f * b.x,
317 y: nf * a.y + f * b.y,
318 z: nf * a.z + f * b.z,
319 w: nf * a.w + f * b.w,
320 }
321 }
322
323 pub fn is_equal_enough(&self, other: &Vec4, epsilon:f32) -> bool {
324 (self.x - other.x).abs() < epsilon
325 && (self.y - other.y).abs() < epsilon
326 && (self.z - other.z).abs() < epsilon
327 && (self.w - other.w).abs() < epsilon
328 }
329
330
331 pub fn from_hsva(hsv: Vec4) -> Vec4 {
332 fn mix(x: f32, y: f32, t: f32) -> f32 {x + (y - x) * t}
333 fn clamp(x: f32, mi: f32, ma: f32) -> f32 {if x < mi {mi} else if x > ma {ma} else {x}}
334 fn fract(x: f32) -> f32 {x.fract()}
335 fn abs(x: f32) -> f32 {x.abs()}
336 Vec4 {
337 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),
338 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),
339 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),
340 w: 1.0
341 }
342 }
343
344 pub fn to_hsva(&self) -> Vec4 {
345 let pc = self.y < self.z; let p0 = if pc {self.z} else {self.y}; let p1 = if pc {self.y} else {self.z}; let p2 = if pc {-1.0} else {0.0}; let p3 = if pc {2.0 / 3.0} else {-1.0 / 3.0}; let qc = self.x < p0; let q0 = if qc {p0} else {self.x}; let q1 = p1;
354 let q2 = if qc {p3} else {p2}; let q3 = if qc {self.x} else {p0}; let d = q0 - q3.min(q1);
358 let e = 1.0e-10;
359 Vec4 {
360 x: (q2 + (q3 - q1) / (6.0 * d + e)).abs(),
361 y: d / (q0 + e),
362 z: q0,
363 w: self.w
364 }
365 }
366
367
368 pub fn from_u32(val: u32) -> Vec4 {
369 Vec4 {
370 x: ((val >> 24) & 0xff) as f32 / 255.0,
371 y: ((val >> 16) & 0xff) as f32 / 255.0,
372 z: ((val >> 8) & 0xff) as f32 / 255.0,
373 w: (val & 0xff) as f32 / 255.0,
374 }
375 }
376
377 pub fn to_u32(&self) -> u32 {
378 let r = (self.x * 255.0) as u8 as u32;
379 let g = (self.y * 255.0) as u8 as u32;
380 let b = (self.z * 255.0) as u8 as u32;
381 let a = (self.w * 255.0) as u8 as u32;
382 (r<<24)|(g<<16)|(b<<8)|a
383 }
384
385 pub fn xy(&self) -> Vec2 {
386 Vec2{x:self.x, y:self.y}
387 }
388
389 pub fn zw(&self) -> Vec2 {
390 Vec2{x:self.z, y:self.w}
391 }
392
393}
394
395impl From<(DVec2,DVec2)> for Vec4{
396 fn from(other:(DVec2,DVec2))->Vec4{
397 vec4(other.0.x as f32, other.0.y as f32, other.1.x as f32, other.1.y as f32)
398 }
399}
400
401
402#[derive(Clone, Copy, Default, Debug, PartialEq)]
403pub struct Quat {
404 pub a: f32,
405 pub b: f32,
406 pub c: f32,
407 pub d: f32
408}
409
410impl Quat {
411 pub fn dot(&self, other: Quat) -> f32 {
412 self.a * other.a + self.b * other.b + self.c * other.c + self.d * other.d
413 }
414
415 pub fn neg(&self) -> Quat {
416 Quat {a: -self.a, b: -self.b, c: -self.c, d: -self.d}
417 }
418
419 pub fn get_angle_with(&self, other: Quat) -> f32 {
420 let dot = self.dot(other);
421 (2.0 * dot * dot - 1.0).acos() * TODEG
422 }
423
424 pub fn from_slerp(n: Quat, mut m: Quat, t: f32) -> Quat {
425 let mut cosom = n.dot(m);
427 if cosom < 0.0 {
429 cosom = -cosom;
430 m = m.neg();
431 }
432 let (scale0, scale1) = if 1.0 - cosom > 0.000001 {
434 let omega = cosom.acos();
436 let sinom = omega.sin();
437 (((1.0 - t) * omega).sin() / sinom, (t * omega).sin() / sinom)
438 } else {
439 (1.0 - t, t)
440 };
441 (Quat {
443 a: scale0 * n.a + scale1 * m.a,
444 b: scale0 * n.b + scale1 * m.b,
445 c: scale0 * n.c + scale1 * m.c,
446 d: scale0 * m.d + scale1 * m.d
447 }).normalized()
448 }
449
450 pub fn length(self) -> f32 {
451 self.dot(self).sqrt()
452 }
453
454 pub fn normalized(&mut self) -> Quat {
455 let len = self.length();
456 Quat {
457 a: self.a / len,
458 b: self.b / len,
459 c: self.c / len,
460 d: self.d / len,
461 }
462 }
463
464}
465
466impl Mat4 {
473 pub fn identity() -> Mat4 {
474 Mat4 {v: [
475 1.0,
476 0.0,
477 0.0,
478 0.0,
479 0.0,
480 1.0,
481 0.0,
482 0.0,
483 0.0,
484 0.0,
485 1.0,
486 0.0,
487 0.0,
488 0.0,
489 0.0,
490 1.0
491 ]}
492 }
493
494
495
496 pub fn txyz_s_ry_rx_txyz(t1: Vec3, s: f32, ry: f32, rx: f32, t2: Vec3) -> Mat4 {
497
498 let cx = f32::cos(rx * TORAD);
499 let cy = f32::cos(ry * TORAD);
500 let sx = f32::sin(rx * TORAD);
502 let sy = f32::sin(ry * TORAD);
503 let m0 = s * (cy);
552 let m1 = s * (0.0);
553 let m2 = s * (sy);
554
555 let m4 = s * (-sx * -sy);
556 let m5 = s * (cx);
557 let m6 = s * (-sx * cy);
558
559 let m8 = s * (-sy * cx);
560 let m9 = s * (sx);
561 let m10 = s * (cx * cy);
562
563 Mat4 {v: [
577 m0,
578 m4,
579 m8,
580 0.0,
581 m1,
582 m5,
583 m9,
584 0.0,
585 m2,
586 m6,
587 m10,
588 0.0,
589 t2.x + (m0 * t1.x + m1 * t1.y + m2 * t1.z),
590 t2.y + (m4 * t1.x + m5 * t1.y + m6 * t1.z),
591 t2.z + (m8 * t1.x + m9 * t1.y + m10 * t1.z),
592 1.0
593 ]}
594 }
595
596 pub fn perspective(fov_y: f32, aspect: f32, near: f32, far: f32) -> Mat4 {
597 let f = 1.0 / f32::tan(fov_y * TORAD / 2.0);
598 let nf = 1.0 / (near - far);
599 Mat4 {v: [
600 f / aspect,
601 0.0,
602 0.0,
603 0.0,
604 0.0,
605 f,
606 0.0,
607 0.0,
608 0.0,
609 0.0,
610 (far + near) * nf,
611 -1.0,
612 0.0,
613 0.0,
614 (2.0 * far * near) * nf,
615 0.0
616 ]}
617 }
618
619 pub fn translation(x: f32, y: f32, z: f32) -> Mat4 {
620 Mat4 {v: [
621 1.0,
622 0.0,
623 0.0,
624 0.0,
625 0.0,
626 1.0,
627 0.0,
628 0.0,
629 0.0,
630 0.0,
631 1.0,
632 0.0,
633 x,
634 y,
635 z,
636 1.0
637 ]}
638
639 }
640
641 pub fn scaled_translation(s: f32, x: f32, y: f32, z: f32) -> Mat4 {
642 Mat4 {v: [
643 s,
644 0.0,
645 0.0,
646 0.0,
647 0.0,
648 s,
649 0.0,
650 0.0,
651 0.0,
652 0.0,
653 s,
654 0.0,
655 x,
656 y,
657 z,
658 1.0
659 ]}
660
661 }
662
663 pub fn rotation(rx: f32, ry: f32, rz: f32) -> Mat4 {
664 const TORAD: f32 = 0.017453292;
665 let cx = f32::cos(rx * TORAD);
666 let cy = f32::cos(ry * TORAD);
667 let cz = f32::cos(rz * TORAD);
668 let sx = f32::sin(rx * TORAD);
669 let sy = f32::sin(ry * TORAD);
670 let sz = f32::sin(rz * TORAD);
671 let m0 = cy * cz + sx * sy * sz;
672 let m1 = -sz * cy + cz * sx * sy;
673 let m2 = sy * cx;
674 let m4 = sz * cx;
675 let m5 = cx * cz;
676 let m6 = -sx;
677 let m8 = -sy * cz + cy * sx * sz;
678 let m9 = sy * sz + cy * sx * cz;
679 let m10 = cx * cy;
680 Mat4 {v: [
681 m0,
682 m4,
683 m8,
684 0.0,
685 m1,
686 m5,
687 m9,
688 0.0,
689 m2,
690 m6,
691 m10,
692 0.0,
693 0.0,
694 0.0,
695 0.0,
696 1.0
697 ]}
698 }
699
700 pub fn ortho(left: f32, right: f32, top: f32, bottom: f32, near: f32, far: f32, scalex: f32, scaley: f32) -> Mat4 {
701 let lr = 1.0 / (left - right);
702 let bt = 1.0 / (bottom - top);
703 let nf = 1.0 / (near - far);
704 Mat4 {v: [
711 -2.0 * lr * scalex,
712 0.0,
713 0.0,
714 0.0,
715 0.0,
716 -2.0 * bt * scaley,
717 0.0,
718 0.0,
719 0.0,
720 0.0,
721 -1.0 * nf,
722 0.0,
723 (left + right) * lr,
724 (top + bottom) * bt,
725 0.5 + (far + near) * nf,
726 1.0
727 ]}
728 }
729
730 pub fn transform_vec4(&self, v: Vec4) -> Vec4 {
731 let m = &self.v;
732 Vec4 {
733 x: m[0] * v.x + m[4] * v.y + m[8] * v.z + m[12] * v.w,
734 y: m[1] * v.x + m[5] * v.y + m[9] * v.z + m[13] * v.w,
735 z: m[2] * v.x + m[6] * v.y + m[10] * v.z + m[14] * v.w,
736 w: m[3] * v.x + m[7] * v.y + m[11] * v.z + m[15] * v.w
737 }
738 }
739
740 pub fn mul(a: &Mat4, b: &Mat4) -> Mat4 {
741 let a = &a.v;
743 let b = &b.v;
744 fn d(i: &[f32; 16], x: usize, y: usize) -> f32 {i[x + 4 * y]}
745 Mat4 {
746 v: [
747 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),
748 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),
749 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),
750 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),
751 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),
752 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),
753 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),
754 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),
755 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),
756 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),
757 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),
758 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),
759 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),
760 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),
761 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),
762 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),
763 ]
764 }
765 }
766
767 pub fn invert(&self) -> Mat4 {
768 let a = &self.v;
769 let a00 = a[0];
770 let a01 = a[1];
771 let a02 = a[2];
772 let a03 = a[3];
773 let a10 = a[4];
774 let a11 = a[5];
775 let a12 = a[6];
776 let a13 = a[7];
777 let a20 = a[8];
778 let a21 = a[9];
779 let a22 = a[10];
780 let a23 = a[11];
781 let a30 = a[12];
782 let a31 = a[13];
783 let a32 = a[14];
784 let a33 = a[15];
785
786 let b00 = a00 * a11 - a01 * a10;
787 let b01 = a00 * a12 - a02 * a10;
788 let b02 = a00 * a13 - a03 * a10;
789 let b03 = a01 * a12 - a02 * a11;
790 let b04 = a01 * a13 - a03 * a11;
791 let b05 = a02 * a13 - a03 * a12;
792 let b06 = a20 * a31 - a21 * a30;
793 let b07 = a20 * a32 - a22 * a30;
794 let b08 = a20 * a33 - a23 * a30;
795 let b09 = a21 * a32 - a22 * a31;
796 let b10 = a21 * a33 - a23 * a31;
797 let b11 = a22 * a33 - a23 * a32;
798
799 let det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
801
802 if det == 0.0 {
803 return Mat4::identity();
804 }
805
806 let idet = 1.0 / det;
807 Mat4 {
808 v: [
809 (a11 * b11 - a12 * b10 + a13 * b09) * idet,
810 (a02 * b10 - a01 * b11 - a03 * b09) * idet,
811 (a31 * b05 - a32 * b04 + a33 * b03) * idet,
812 (a22 * b04 - a21 * b05 - a23 * b03) * idet,
813 (a12 * b08 - a10 * b11 - a13 * b07) * idet,
814 (a00 * b11 - a02 * b08 + a03 * b07) * idet,
815 (a32 * b02 - a30 * b05 - a33 * b01) * idet,
816 (a20 * b05 - a22 * b02 + a23 * b01) * idet,
817 (a10 * b10 - a11 * b08 + a13 * b06) * idet,
818 (a01 * b08 - a00 * b10 - a03 * b06) * idet,
819 (a30 * b04 - a31 * b02 + a33 * b00) * idet,
820 (a21 * b02 - a20 * b04 - a23 * b00) * idet,
821 (a11 * b07 - a10 * b09 - a12 * b06) * idet,
822 (a00 * b09 - a01 * b07 + a02 * b06) * idet,
823 (a31 * b01 - a30 * b03 - a32 * b00) * idet,
824 (a20 * b03 - a21 * b01 + a22 * b00) * idet,
825 ]
826 }
827 }
828}
829
830
831impl ops::Add<Vec2> for Vec2 {
834 type Output = Vec2;
835 fn add(self, rhs: Vec2) -> Vec2 {
836 Vec2 {x: self.x + rhs.x, y: self.y + rhs.y}
837 }
838}
839
840impl ops::Sub<Vec2> for Vec2 {
841 type Output = Vec2;
842 fn sub(self, rhs: Vec2) -> Vec2 {
843 Vec2 {x: self.x - rhs.x, y: self.y - rhs.y}
844 }
845}
846
847impl ops::Mul<Vec2> for Vec2 {
848 type Output = Vec2;
849 fn mul(self, rhs: Vec2) -> Vec2 {
850 Vec2 {x: self.x * rhs.x, y: self.y * rhs.y}
851 }
852}
853
854impl ops::Div<Vec2> for Vec2 {
855 type Output = Vec2;
856 fn div(self, rhs: Vec2) -> Vec2 {
857 Vec2 {x: self.x / rhs.x, y: self.y / rhs.y}
858 }
859}
860
861
862impl ops::Add<Vec2> for f32 {
863 type Output = Vec2;
864 fn add(self, rhs: Vec2) -> Vec2 {
865 Vec2 {x: self + rhs.x, y: self + rhs.y}
866 }
867}
868
869impl ops::Sub<Vec2> for f32 {
870 type Output = Vec2;
871 fn sub(self, rhs: Vec2) -> Vec2 {
872 Vec2 {x: self -rhs.x, y: self -rhs.y}
873 }
874}
875
876impl ops::Mul<Vec2> for f32 {
877 type Output = Vec2;
878 fn mul(self, rhs: Vec2) -> Vec2 {
879 Vec2 {x: self *rhs.x, y: self *rhs.y}
880 }
881}
882
883impl ops::Div<Vec2> for f32 {
884 type Output = Vec2;
885 fn div(self, rhs: Vec2) -> Vec2 {
886 Vec2 {x: self / rhs.x, y: self / rhs.y}
887 }
888}
889
890
891impl ops::Add<f32> for Vec2 {
892 type Output = Vec2;
893 fn add(self, rhs: f32) -> Vec2 {
894 Vec2 {x: self.x + rhs, y: self.y + rhs}
895 }
896}
897
898impl ops::Sub<f32> for Vec2 {
899 type Output = Vec2;
900 fn sub(self, rhs: f32) -> Vec2 {
901 Vec2 {x: self.x - rhs, y: self.y - rhs}
902 }
903}
904
905impl ops::Mul<f32> for Vec2 {
906 type Output = Vec2;
907 fn mul(self, rhs: f32) -> Vec2 {
908 Vec2 {x: self.x * rhs, y: self.y * rhs}
909 }
910}
911
912impl ops::Div<f32> for Vec2 {
913 type Output = Vec2;
914 fn div(self, rhs: f32) -> Vec2 {
915 Vec2 {x: self.x / rhs, y: self.y / rhs}
916 }
917}
918
919impl ops::AddAssign<Vec2> for Vec2 {
920 fn add_assign(&mut self, rhs: Vec2) {
921 self.x = self.x + rhs.x;
922 self.y = self.y + rhs.y;
923 }
924}
925
926impl ops::SubAssign<Vec2> for Vec2 {
927 fn sub_assign(&mut self, rhs: Vec2) {
928 self.x = self.x - rhs.x;
929 self.y = self.y - rhs.y;
930 }
931}
932
933impl ops::MulAssign<Vec2> for Vec2 {
934 fn mul_assign(&mut self, rhs: Vec2) {
935 self.x = self.x * rhs.x;
936 self.y = self.y * rhs.y;
937 }
938}
939
940impl ops::DivAssign<Vec2> for Vec2 {
941 fn div_assign(&mut self, rhs: Vec2) {
942 self.x = self.x / rhs.x;
943 self.y = self.y / rhs.y;
944 }
945}
946
947
948impl ops::AddAssign<f32> for Vec2 {
949 fn add_assign(&mut self, rhs: f32) {
950 self.x = self.x + rhs;
951 self.y = self.y + rhs;
952 }
953}
954
955impl ops::SubAssign<f32> for Vec2 {
956 fn sub_assign(&mut self, rhs: f32) {
957 self.x = self.x - rhs;
958 self.y = self.y - rhs;
959 }
960}
961
962impl ops::MulAssign<f32> for Vec2 {
963 fn mul_assign(&mut self, rhs: f32) {
964 self.x = self.x * rhs;
965 self.y = self.y * rhs;
966 }
967}
968
969impl ops::DivAssign<f32> for Vec2 {
970 fn div_assign(&mut self, rhs: f32) {
971 self.x = self.x / rhs;
972 self.y = self.y / rhs;
973 }
974}
975
976impl ops::Neg for Vec2 {
977 type Output = Vec2;
978 fn neg(self) -> Self { Vec2{x:-self.x, y:-self.y}}
979}
980
981impl ops::Neg for Vec3{
982 type Output = Vec3;
983 fn neg(self) -> Self { Vec3{x:-self.x, y:-self.y, z:-self.z}}
984}
985
986impl ops::Neg for Vec4 {
987 type Output = Vec4;
988 fn neg(self) -> Self { Vec4{x:-self.x, y:-self.y, z:-self.z, w:-self.w}}
989}
990
991
992impl ops::Add<Vec3> for Vec3 {
995 type Output = Vec3;
996 fn add(self, rhs: Vec3) -> Vec3 {
997 Vec3 {x: self.x + rhs.x, y: self.y + rhs.y, z: self.z + rhs.z}
998 }
999}
1000
1001impl ops::Sub<Vec3> for Vec3 {
1002 type Output = Vec3;
1003 fn sub(self, rhs: Vec3) -> Vec3 {
1004 Vec3 {x: self.x - rhs.x, y: self.y - rhs.y, z: self.z - rhs.z}
1005 }
1006}
1007
1008impl ops::Mul<Vec3> for Vec3 {
1009 type Output = Vec3;
1010 fn mul(self, rhs: Vec3) -> Vec3 {
1011 Vec3 {x: self.x * rhs.x, y: self.y * rhs.y, z: self.z * rhs.z}
1012 }
1013}
1014
1015impl ops::Div<Vec3> for Vec3 {
1016 type Output = Vec3;
1017 fn div(self, rhs: Vec3) -> Vec3 {
1018 Vec3 {x: self.x / rhs.x, y: self.y / rhs.y, z: self.z / rhs.z}
1019 }
1020}
1021
1022impl ops::Add<Vec3> for f32 {
1023 type Output = Vec3;
1024 fn add(self, rhs: Vec3) -> Vec3 {
1025 Vec3 {x: self + rhs.x, y: self + rhs.y, z: self + rhs.z}
1026 }
1027}
1028
1029impl ops::Sub<Vec3> for f32 {
1030 type Output = Vec3;
1031 fn sub(self, rhs: Vec3) -> Vec3 {
1032 Vec3 {x: self -rhs.x, y: self -rhs.y, z: self -rhs.z}
1033 }
1034}
1035
1036impl ops::Mul<Vec3> for f32 {
1037 type Output = Vec3;
1038 fn mul(self, rhs: Vec3) -> Vec3 {
1039 Vec3 {x: self *rhs.x, y: self *rhs.y, z: self *rhs.z}
1040 }
1041}
1042
1043impl ops::Div<Vec3> for f32 {
1044 type Output = Vec3;
1045 fn div(self, rhs: Vec3) -> Vec3 {
1046 Vec3 {x: self / rhs.x, y: self / rhs.y, z: self / rhs.z}
1047 }
1048}
1049
1050
1051impl ops::Add<f32> for Vec3 {
1052 type Output = Vec3;
1053 fn add(self, rhs: f32) -> Vec3 {
1054 Vec3 {x: self.x + rhs, y: self.y + rhs, z: self.z + rhs}
1055 }
1056}
1057
1058impl ops::Sub<f32> for Vec3 {
1059 type Output = Vec3;
1060 fn sub(self, rhs: f32) -> Vec3 {
1061 Vec3 {x: self.x - rhs, y: self.y - rhs, z: self.z - rhs}
1062 }
1063}
1064
1065impl ops::Mul<f32> for Vec3 {
1066 type Output = Vec3;
1067 fn mul(self, rhs: f32) -> Vec3 {
1068 Vec3 {x: self.x * rhs, y: self.y * rhs, z: self.z * rhs}
1069 }
1070}
1071
1072impl ops::Div<f32> for Vec3 {
1073 type Output = Vec3;
1074 fn div(self, rhs: f32) -> Vec3 {
1075 Vec3 {x: self.x / rhs, y: self.y / rhs, z: self.z / rhs}
1076 }
1077}
1078
1079
1080impl ops::AddAssign<Vec3> for Vec3 {
1081 fn add_assign(&mut self, rhs: Vec3) {
1082 self.x = self.x + rhs.x;
1083 self.y = self.y + rhs.y;
1084 self.z = self.z + rhs.z;
1085 }
1086}
1087
1088impl ops::SubAssign<Vec3> for Vec3 {
1089 fn sub_assign(&mut self, rhs: Vec3) {
1090 self.x = self.x - rhs.x;
1091 self.y = self.y - rhs.y;
1092 self.z = self.z - rhs.z;
1093 }
1094}
1095
1096impl ops::MulAssign<Vec3> for Vec3 {
1097 fn mul_assign(&mut self, rhs: Vec3) {
1098 self.x = self.x * rhs.x;
1099 self.y = self.y * rhs.y;
1100 self.z = self.z * rhs.z;
1101 }
1102}
1103
1104impl ops::DivAssign<Vec3> for Vec3 {
1105 fn div_assign(&mut self, rhs: Vec3) {
1106 self.x = self.x / rhs.x;
1107 self.y = self.y / rhs.y;
1108 self.z = self.z / rhs.z;
1109 }
1110}
1111
1112
1113impl ops::AddAssign<f32> for Vec3 {
1114 fn add_assign(&mut self, rhs: f32) {
1115 self.x = self.x + rhs;
1116 self.y = self.y + rhs;
1117 self.z = self.z + rhs;
1118 }
1119}
1120
1121impl ops::SubAssign<f32> for Vec3 {
1122 fn sub_assign(&mut self, rhs: f32) {
1123 self.x = self.x - rhs;
1124 self.y = self.y - rhs;
1125 self.z = self.z - rhs;
1126 }
1127}
1128
1129impl ops::MulAssign<f32> for Vec3 {
1130 fn mul_assign(&mut self, rhs: f32) {
1131 self.x = self.x * rhs;
1132 self.y = self.y * rhs;
1133 self.z = self.z * rhs;
1134 }
1135}
1136
1137impl ops::DivAssign<f32> for Vec3 {
1138 fn div_assign(&mut self, rhs: f32) {
1139 self.x = self.x / rhs;
1140 self.y = self.y / rhs;
1141 self.z = self.z / rhs;
1142 }
1143}
1144
1145impl ops::Add<Vec4> for Vec4 {
1148 type Output = Vec4;
1149 fn add(self, rhs: Vec4) -> Vec4 {
1150 Vec4 {x: self.x + rhs.x, y: self.y + rhs.y, z: self.z + rhs.z, w: self.w + rhs.w}
1151 }
1152}
1153
1154impl ops::Sub<Vec4> for Vec4 {
1155 type Output = Vec4;
1156 fn sub(self, rhs: Vec4) -> Vec4 {
1157 Vec4 {x: self.x - rhs.x, y: self.y - rhs.y, z: self.z - rhs.z, w: self.w - rhs.w}
1158 }
1159}
1160
1161impl ops::Mul<Vec4> for Vec4 {
1162 type Output = Vec4;
1163 fn mul(self, rhs: Vec4) -> Vec4 {
1164 Vec4 {x: self.x * rhs.x, y: self.y * rhs.y, z: self.z * rhs.z, w: self.w * rhs.w}
1165 }
1166}
1167
1168impl ops::Div<Vec4> for Vec4 {
1169 type Output = Vec4;
1170 fn div(self, rhs: Vec4) -> Vec4 {
1171 Vec4 {x: self.x / rhs.x, y: self.y / rhs.y, z: self.z / rhs.z, w: self.w / rhs.w}
1172 }
1173}
1174
1175impl ops::Add<Vec4> for f32 {
1176 type Output = Vec4;
1177 fn add(self, rhs: Vec4) -> Vec4 {
1178 Vec4 {x: self + rhs.x, y: self + rhs.y, z: self + rhs.z, w: self + rhs.z}
1179 }
1180}
1181
1182impl ops::Sub<Vec4> for f32 {
1183 type Output = Vec4;
1184 fn sub(self, rhs: Vec4) -> Vec4 {
1185 Vec4 {x: self -rhs.x, y: self -rhs.y, z: self -rhs.z, w: self -rhs.z}
1186 }
1187}
1188
1189impl ops::Mul<Vec4> for f32 {
1190 type Output = Vec4;
1191 fn mul(self, rhs: Vec4) -> Vec4 {
1192 Vec4 {x: self *rhs.x, y: self *rhs.y, z: self *rhs.z, w: self *rhs.z}
1193 }
1194}
1195
1196impl ops::Div<Vec4> for f32 {
1197 type Output = Vec4;
1198 fn div(self, rhs: Vec4) -> Vec4 {
1199 Vec4 {x: self / rhs.x, y: self / rhs.y, z: self / rhs.z, w: self / rhs.z}
1200 }
1201}
1202
1203
1204impl ops::Add<f32> for Vec4 {
1205 type Output = Vec4;
1206 fn add(self, rhs: f32) -> Vec4 {
1207 Vec4 {x: self.x + rhs, y: self.y + rhs, z: self.z + rhs, w: self.w + rhs}
1208 }
1209}
1210
1211impl ops::Sub<f32> for Vec4 {
1212 type Output = Vec4;
1213 fn sub(self, rhs: f32) -> Vec4 {
1214 Vec4 {x: self.x - rhs, y: self.y - rhs, z: self.z - rhs, w: self.w - rhs}
1215 }
1216}
1217
1218impl ops::Mul<f32> for Vec4 {
1219 type Output = Vec4;
1220 fn mul(self, rhs: f32) -> Vec4 {
1221 Vec4 {x: self.x * rhs, y: self.y * rhs, z: self.z * rhs, w: self.w * rhs}
1222 }
1223}
1224
1225impl ops::Div<f32> for Vec4 {
1226 type Output = Vec4;
1227 fn div(self, rhs: f32) -> Vec4 {
1228 Vec4 {x: self.x / rhs, y: self.y / rhs, z: self.z / rhs, w: self.w / rhs}
1229 }
1230}
1231
1232impl ops::AddAssign<Vec4> for Vec4 {
1233 fn add_assign(&mut self, rhs: Vec4) {
1234 self.x = self.x + rhs.x;
1235 self.y = self.y + rhs.y;
1236 self.z = self.z + rhs.z;
1237 self.w = self.w + rhs.w;
1238 }
1239}
1240
1241impl ops::SubAssign<Vec4> for Vec4 {
1242 fn sub_assign(&mut self, rhs: Vec4) {
1243 self.x = self.x - rhs.x;
1244 self.y = self.y - rhs.y;
1245 self.z = self.z - rhs.z;
1246 self.w = self.w - rhs.w;
1247 }
1248}
1249
1250impl ops::MulAssign<Vec4> for Vec4 {
1251 fn mul_assign(&mut self, rhs: Vec4) {
1252 self.x = self.x * rhs.x;
1253 self.y = self.y * rhs.y;
1254 self.z = self.z * rhs.z;
1255 self.w = self.w * rhs.w;
1256 }
1257}
1258
1259impl ops::DivAssign<Vec4> for Vec4 {
1260 fn div_assign(&mut self, rhs: Vec4) {
1261 self.x = self.x / rhs.x;
1262 self.y = self.y / rhs.y;
1263 self.z = self.z / rhs.z;
1264 self.w = self.w / rhs.w;
1265 }
1266}
1267
1268
1269impl ops::AddAssign<f32> for Vec4 {
1270 fn add_assign(&mut self, rhs: f32) {
1271 self.x = self.x + rhs;
1272 self.y = self.y + rhs;
1273 self.z = self.z + rhs;
1274 self.w = self.w + rhs;
1275 }
1276}
1277
1278impl ops::SubAssign<f32> for Vec4 {
1279 fn sub_assign(&mut self, rhs: f32) {
1280 self.x = self.x - rhs;
1281 self.y = self.y - rhs;
1282 self.z = self.z - rhs;
1283 self.w = self.w - rhs;
1284 }
1285}
1286
1287impl ops::MulAssign<f32> for Vec4 {
1288 fn mul_assign(&mut self, rhs: f32) {
1289 self.x = self.x * rhs;
1290 self.y = self.y * rhs;
1291 self.z = self.z * rhs;
1292 self.w = self.w * rhs;
1293 }
1294}
1295
1296impl ops::DivAssign<f32> for Vec4 {
1297 fn div_assign(&mut self, rhs: f32) {
1298 self.x = self.x / rhs;
1299 self.y = self.y / rhs;
1300 self.z = self.z / rhs;
1301 self.w = self.w / rhs;
1302 }
1303}
1304
1305