vec_arith/
lib.rs

1// MIT License
2//
3// Copyright (c) 2019 Tobias Pfeiffer
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22
23#![feature(clamp)]
24use std::{
25	ops::*,
26	iter::FromIterator
27};
28
29pub use self::{
30	vec2::*,
31	vec3::*,
32	vec4::*,
33	quat32::*,
34	quat64::*,
35	mat2::*,
36	mat3::*,
37	mat4::*,
38	bezier1::*,
39	bezier2::*,
40	bezier3::*,
41	sphere::*,
42	cuboid::*,
43	plane::*,
44	cylinder::*,
45	triangle::*,
46	ops::*,
47	poly::*,
48	misc::*,
49	sdf::*
50};
51
52pub mod vec2 {
53	use super::*;
54	
55	#[derive(Copy, Clone, Debug, PartialOrd, PartialEq)]
56	pub struct Vec2<T>(pub T, pub T);
57	
58	impl<T> Vec2<T> {
59		#[inline]
60		pub fn map<F: FnMut(T) -> U, U>(self, mut f: F) -> Vec2<U> {
61			Vec2(f(self.0),
62				 f(self.1))
63		}
64	}
65	
66	impl<T: Copy> Vec2<T> {
67		#[inline] pub fn xx(self) -> Vec2<T> { Vec2(self.0, self.0) }
68		#[inline] pub fn xy(self) -> Vec2<T> { Vec2(self.0, self.1) }
69		#[inline] pub fn yx(self) -> Vec2<T> { Vec2(self.1, self.0) }
70		#[inline] pub fn yy(self) -> Vec2<T> { Vec2(self.1, self.1) }
71	}
72	
73	impl<T: Default> Default for Vec2<T> {
74		fn default() -> Self {
75			Self(T::default(),
76				 T::default())
77		}
78	}
79	
80	impl<T: Copy> From<T> for Vec2<T> {
81		fn from(v: T) -> Self {
82			Self(v, v)
83		}
84	}
85	
86	impl<T: Copy> From<[T; 2]> for Vec2<T> {
87		fn from(v: [T; 2]) -> Self {
88			Self(v[0], v[1])
89		}
90	}
91	
92	impl<T> From<(T, T)> for Vec2<T> {
93		fn from(v: (T, T)) -> Self {
94			Self(v.0, v.1)
95		}
96	}
97	
98	impl Vec2<f32> {
99		pub fn abs(self) -> Self {
100			Self(self.0.abs(),
101				 self.1.abs())
102		}
103		
104		pub fn powf(self, rhs: Self) -> Self {
105			Self(self.0.powf(rhs.0),
106				 self.1.powf(rhs.1))
107		}
108		
109		pub fn powi(self, rhs: Vec2<i32>) -> Self {
110			Self(self.0.powi(rhs.0),
111				 self.1.powi(rhs.1))
112		}
113		
114		pub fn sqrt(self) -> Self {
115			Self(self.0.sqrt(),
116				 self.1.sqrt())
117		}
118		
119		pub fn cbrt(self) -> Self {
120			Self(self.0.cbrt(),
121				 self.1.cbrt())
122		}
123		
124		pub fn log(self, rhs: Self) -> Self {
125			Self(self.0.log(rhs.0),
126				 self.1.log(rhs.1))
127		}
128		
129		pub fn len2(self) -> f32 {
130			self.dot(self)
131		}
132		
133		pub fn len(self) -> f32 {
134			self.dot(self).sqrt()
135		}
136		
137		pub fn normalize(self, len: f32) -> Self {
138			self * (len / self.len())
139		}
140		
141		pub fn distance2(self, other: Self) -> f32 {
142			(other - self).len2()
143		}
144		
145		pub fn distance(self, other: Self) -> f32 {
146			(other - self).len()
147		}
148		
149		pub fn angle(self, other: Self) -> f32 {
150			(self.dot(other) / (self.len() * other.len())).acos()
151		}
152	}
153	
154	impl Vec2<f64> {
155		pub fn abs(self) -> Self {
156			Self(self.0.abs(),
157				 self.1.abs())
158		}
159		
160		pub fn powf(self, rhs: Self) -> Self {
161			Self(self.0.powf(rhs.0),
162				 self.1.powf(rhs.1))
163		}
164		
165		pub fn powi(self, rhs: Vec2<i32>) -> Self {
166			Self(self.0.powi(rhs.0),
167				 self.1.powi(rhs.1))
168		}
169		
170		pub fn sqrt(self) -> Self {
171			Self(self.0.sqrt(),
172				 self.1.sqrt())
173		}
174		
175		pub fn cbrt(self) -> Self {
176			Self(self.0.cbrt(),
177				 self.1.cbrt())
178		}
179		
180		pub fn log(self, rhs: Self) -> Self {
181			Self(self.0.log(rhs.0),
182				 self.1.log(rhs.1))
183		}
184		
185		pub fn len2(self) -> f64 {
186			self.dot(self)
187		}
188		
189		pub fn len(self) -> f64 {
190			self.dot(self).sqrt()
191		}
192		
193		pub fn normalize(self, len: f64) -> Self {
194			self * (len / self.len())
195		}
196		
197		pub fn distance2(self, other: Self) -> f64 {
198			(other - self).len2()
199		}
200		
201		pub fn distance(self, other: Self) -> f64 {
202			(other - self).len()
203		}
204		
205		pub fn angle(self, other: Self) -> f64 {
206			self.dot(other) / (self.len() * other.len())
207		}
208	}
209	
210	impl Vec2<bool> {
211		pub fn and(self) -> bool {
212			self.0 && self.1
213		}
214		
215		pub fn or(self) -> bool {
216			self.0 || self.1
217		}
218		
219		pub fn xor(self) -> bool {
220			self.0 ^ self.1
221		}
222	}
223	
224	impl<T> Index<usize> for Vec2<T> {
225		type Output = T;
226		
227		fn index(&self, index: usize) -> &Self::Output {
228			match index {
229				0 => &self.0,
230				1 => &self.1,
231				_ => panic!()
232			}
233		}
234	}
235	
236	impl<T> IndexMut<usize> for Vec2<T> {
237		fn index_mut(&mut self, index: usize) -> &mut Self::Output {
238			match index {
239				0 => &mut self.0,
240				1 => &mut self.1,
241				_ => panic!()
242			}
243		}
244	}
245	
246	impl<T: PartialOrd + Copy> Vec2<T> {
247		pub fn min(self, other: Self) -> Self {
248			Self(if self.0 < other.0 { self.0 } else { other.0 },
249				 if self.1 < other.1 { self.1 } else { other.1 })
250		}
251		
252		pub fn mins(self, other: T) -> Self {
253			Self(if self.0 < other { self.0 } else { other },
254				 if self.1 < other { self.1 } else { other })
255		}
256		
257		pub fn max(self, other: Self) -> Self {
258			Self(if self.0 > other.0 { self.0 } else { other.0 },
259				 if self.1 > other.1 { self.1 } else { other.1 })
260		}
261		
262		pub fn maxs(self, other: T) -> Self {
263			Self(if self.0 > other { self.0 } else { other },
264				 if self.1 > other { self.1 } else { other })
265		}
266		
267		pub fn clamp(self, lower: Self, upper: Self) -> Self {
268			Self(if self.0 < lower.0 { lower.0 } else if self.0 > upper.0 { upper.0 } else { self.0 },
269				 if self.1 < lower.1 { lower.1 } else if self.1 > upper.1 { upper.1 } else { self.1 })
270		}
271		
272		pub fn clamps(self, lower: T, upper: T) -> Self {
273			Self(if self.0 < lower { lower } else if self.0 > upper { upper } else { self.0 },
274				 if self.1 < lower { lower } else if self.1 > upper { upper } else { self.1 })
275		}
276		
277		pub fn clampvs(self, lower: Self, upper: T) -> Self {
278			Self(if self.0 < lower.0 { lower.0 } else if self.0 > upper { upper } else { self.0 },
279				 if self.1 < lower.1 { lower.1 } else if self.1 > upper { upper } else { self.1 })
280		}
281		
282		pub fn clampsv(self, lower: T, upper: Self) -> Self {
283			Self(if self.0 < lower { lower } else if self.0 > upper.0 { upper.0 } else { self.0 },
284				 if self.1 < lower { lower } else if self.1 > upper.1 { upper.1 } else { self.1 })
285		}
286	}
287	
288	impl<T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Copy> Vec2<T> {
289		pub fn dot(self, other: Self) -> T {
290			self.0 * other.0 + self.1 * other.1
291		}
292		
293		pub fn mix(self, a: Self, b: Self) -> Self {
294			Self(a.0 + (b.0 - a.0) * self.0,
295				 a.1 + (b.1 - a.1) * self.1)
296		}
297		
298		pub fn mixs(self, a: T, b: T) -> Self {
299			Self(a + (b - a) * self.0,
300				 a + (b - a) * self.1)
301		}
302	}
303	
304	impl<T: Add> Add for Vec2<T> {
305		type Output = Vec2<T::Output>;
306		
307		fn add(self, rhs: Self) -> Self::Output {
308			Vec2(self.0 + rhs.0,
309				 self.1 + rhs.1)
310		}
311	}
312	
313	impl<T: Sub> Sub for Vec2<T> {
314		type Output = Vec2<T::Output>;
315		
316		fn sub(self, rhs: Self) -> Self::Output {
317			Vec2(self.0 - rhs.0,
318				 self.1 - rhs.1)
319		}
320	}
321	
322	impl<T: Mul> Mul for Vec2<T> {
323		type Output = Vec2<T::Output>;
324		
325		fn mul(self, rhs: Self) -> Self::Output {
326			Vec2(self.0 * rhs.0,
327				 self.1 * rhs.1)
328		}
329	}
330	
331	impl<T: Mul + Copy> Mul<T> for Vec2<T> {
332		type Output = Vec2<T::Output>;
333		
334		fn mul(self, rhs: T) -> Self::Output {
335			Vec2(self.0 * rhs,
336				 self.1 * rhs)
337		}
338	}
339	
340	impl<T: Div> Div for Vec2<T> {
341		type Output = Vec2<T::Output>;
342		
343		fn div(self, rhs: Self) -> Self::Output {
344			Vec2(self.0 / rhs.0,
345				 self.1 / rhs.1)
346		}
347	}
348	
349	impl<T: Div + Copy> Div<T> for Vec2<T> {
350		type Output = Vec2<T::Output>;
351		
352		fn div(self, rhs: T) -> Self::Output {
353			Vec2(self.0 / rhs,
354				 self.1 / rhs)
355		}
356	}
357	
358	impl<T: Rem> Rem for Vec2<T> {
359		type Output = Vec2<T::Output>;
360		
361		fn rem(self, rhs: Self) -> Self::Output {
362			Vec2(self.0 % rhs.0,
363				 self.1 % rhs.1)
364		}
365	}
366	
367	impl<T: Rem + Copy> Rem<T> for Vec2<T> {
368		type Output = Vec2<T::Output>;
369		
370		fn rem(self, rhs: T) -> Self::Output {
371			Vec2(self.0 % rhs,
372				 self.1 % rhs)
373		}
374	}
375	
376	impl<T: Neg> Neg for Vec2<T> {
377		type Output = Vec2<T::Output>;
378		
379		fn neg(self) -> Self::Output {
380			Vec2(-self.0,
381				 -self.1)
382		}
383	}
384	
385	impl<T: Not> Not for Vec2<T> {
386		type Output = Vec2<T::Output>;
387		
388		fn not(self) -> Self::Output {
389			Vec2(!self.0,
390				 !self.1)
391		}
392	}
393	
394	impl<T: BitAnd> BitAnd for Vec2<T> {
395		type Output = Vec2<T::Output>;
396		
397		fn bitand(self, rhs: Self) -> Self::Output {
398			Vec2(self.0 & rhs.0,
399				 self.1 & rhs.1)
400		}
401	}
402	
403	impl<T: BitAnd + Copy> BitAnd<T> for Vec2<T> {
404		type Output = Vec2<T::Output>;
405		
406		fn bitand(self, rhs: T) -> Self::Output {
407			Vec2(self.0 & rhs,
408				 self.1 & rhs)
409		}
410	}
411	
412	impl<T: BitOr> BitOr for Vec2<T> {
413		type Output = Vec2<T::Output>;
414		
415		fn bitor(self, rhs: Self) -> Self::Output {
416			Vec2(self.0 | rhs.0,
417				 self.1 | rhs.1)
418		}
419	}
420	
421	impl<T: BitOr + Copy> BitOr<T> for Vec2<T> {
422		type Output = Vec2<T::Output>;
423		
424		fn bitor(self, rhs: T) -> Self::Output {
425			Vec2(self.0 | rhs,
426				 self.1 | rhs)
427		}
428	}
429	
430	impl<T: BitXor> BitXor for Vec2<T> {
431		type Output = Vec2<T::Output>;
432		
433		fn bitxor(self, rhs: Self) -> Self::Output {
434			Vec2(self.0 ^ rhs.0,
435				 self.1 ^ rhs.1)
436		}
437	}
438	
439	impl<T: BitXor + Copy> BitXor<T> for Vec2<T> {
440		type Output = Vec2<T::Output>;
441		
442		fn bitxor(self, rhs: T) -> Self::Output {
443			Vec2(self.0 ^ rhs,
444				 self.1 ^ rhs)
445		}
446	}
447	
448	impl<T: Shl> Shl for Vec2<T> {
449		type Output = Vec2<T::Output>;
450		
451		fn shl(self, rhs: Self) -> Self::Output {
452			Vec2(self.0 << rhs.0,
453				 self.1 << rhs.1)
454		}
455	}
456	
457	impl<T: Shl + Copy> Shl<T> for Vec2<T> {
458		type Output = Vec2<T::Output>;
459		
460		fn shl(self, rhs: T) -> Self::Output {
461			Vec2(self.0 << rhs,
462				 self.1 << rhs)
463		}
464	}
465	
466	impl<T: Shr> Shr for Vec2<T> {
467		type Output = Vec2<T::Output>;
468		
469		fn shr(self, rhs: Self) -> Self::Output {
470			Vec2(self.0 >> rhs.0,
471				 self.1 >> rhs.1)
472		}
473	}
474	
475	impl<T: Shr + Copy> Shr<T> for Vec2<T> {
476		type Output = Vec2<T::Output>;
477		
478		fn shr(self, rhs: T) -> Self::Output {
479			Vec2(self.0 >> rhs,
480				 self.1 >> rhs)
481		}
482	}
483	
484	impl<T: AddAssign> AddAssign for Vec2<T> {
485		fn add_assign(&mut self, rhs: Self) {
486			self.0 += rhs.0;
487			self.1 += rhs.1;
488		}
489	}
490	
491	impl<T: SubAssign> SubAssign for Vec2<T> {
492		fn sub_assign(&mut self, rhs: Self) {
493			self.0 -= rhs.0;
494			self.1 -= rhs.1;
495		}
496	}
497	
498	impl<T: MulAssign> MulAssign for Vec2<T> {
499		fn mul_assign(&mut self, rhs: Self) {
500			self.0 *= rhs.0;
501			self.1 *= rhs.1;
502		}
503	}
504	
505	impl<T: MulAssign + Copy> MulAssign<T> for Vec2<T> {
506		fn mul_assign(&mut self, rhs: T) {
507			self.0 *= rhs;
508			self.1 *= rhs;
509		}
510	}
511	
512	impl<T: DivAssign> DivAssign for Vec2<T> {
513		fn div_assign(&mut self, rhs: Self) {
514			self.0 /= rhs.0;
515			self.1 /= rhs.1;
516		}
517	}
518	
519	impl<T: DivAssign + Copy> DivAssign<T> for Vec2<T> {
520		fn div_assign(&mut self, rhs: T) {
521			self.0 /= rhs;
522			self.1 /= rhs;
523		}
524	}
525	
526	impl<T: RemAssign> RemAssign for Vec2<T> {
527		fn rem_assign(&mut self, rhs: Self) {
528			self.0 %= rhs.0;
529			self.1 %= rhs.1;
530		}
531	}
532	
533	impl<T: RemAssign + Copy> RemAssign<T> for Vec2<T> {
534		fn rem_assign(&mut self, rhs: T) {
535			self.0 %= rhs;
536			self.1 %= rhs;
537		}
538	}
539	
540	impl<T: BitAndAssign> BitAndAssign for Vec2<T> {
541		fn bitand_assign(&mut self, rhs: Self) {
542			self.0 &= rhs.0;
543			self.1 &= rhs.1;
544		}
545	}
546	
547	impl<T: BitAndAssign + Copy> BitAndAssign<T> for Vec2<T> {
548		fn bitand_assign(&mut self, rhs: T) {
549			self.0 &= rhs;
550			self.1 &= rhs;
551		}
552	}
553	
554	impl<T: BitOrAssign> BitOrAssign for Vec2<T> {
555		fn bitor_assign(&mut self, rhs: Self) {
556			self.0 |= rhs.0;
557			self.1 |= rhs.1;
558		}
559	}
560	
561	impl<T: BitOrAssign + Copy> BitOrAssign<T> for Vec2<T> {
562		fn bitor_assign(&mut self, rhs: T) {
563			self.0 |= rhs;
564			self.1 |= rhs;
565		}
566	}
567	
568	impl<T: BitXorAssign> BitXorAssign for Vec2<T> {
569		fn bitxor_assign(&mut self, rhs: Self) {
570			self.0 ^= rhs.0;
571			self.1 ^= rhs.1;
572		}
573	}
574	
575	impl<T: BitXorAssign + Copy> BitXorAssign<T> for Vec2<T> {
576		fn bitxor_assign(&mut self, rhs: T) {
577			self.0 ^= rhs;
578			self.1 ^= rhs;
579		}
580	}
581	
582	impl<T: ShlAssign> ShlAssign for Vec2<T> {
583		fn shl_assign(&mut self, rhs: Self) {
584			self.0 <<= rhs.0;
585			self.1 <<= rhs.1;
586		}
587	}
588	
589	impl<T: ShlAssign + Copy> ShlAssign<T> for Vec2<T> {
590		fn shl_assign(&mut self, rhs: T) {
591			self.0 <<= rhs;
592			self.1 <<= rhs;
593		}
594	}
595	
596	impl<T: ShrAssign> ShrAssign for Vec2<T> {
597		fn shr_assign(&mut self, rhs: Self) {
598			self.0 >>= rhs.0;
599			self.1 >>= rhs.1;
600		}
601	}
602	
603	impl<T: ShrAssign + Copy> ShrAssign<T> for Vec2<T> {
604		fn shr_assign(&mut self, rhs: T) {
605			self.0 >>= rhs;
606			self.1 >>= rhs;
607		}
608	}
609	
610	impl<A> FromIterator<A> for Vec2<A> {
611		fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self {
612			let mut iter = iter.into_iter();
613			Vec2(iter.next().unwrap(),
614				 iter.next().unwrap())
615		}
616	}
617	
618	pub struct Iter<T>(Vec2<T>, usize);
619	
620	impl<T: Clone> Iterator for Iter<T> {
621		type Item = T;
622		
623		fn next(&mut self) -> Option<Self::Item> {
624			self.1 += 1;
625			match self.1 {
626				1 => Some((self.0).0.clone()),
627				2 => Some((self.0).1.clone()),
628				_ => None,
629			}
630		}
631	}
632	
633	impl<T: Clone> IntoIterator for Vec2<T> {
634		type Item = T;
635		type IntoIter = Iter<T>;
636		
637		#[inline]
638		fn into_iter(self) -> Self::IntoIter {
639			Iter(self, 0)
640		}
641	}
642	
643	pub struct IterRef<'a, T>(&'a Vec2<T>, usize);
644	
645	impl<'a, T> Iterator for IterRef<'a, T> {
646		type Item = &'a T;
647		
648		fn next(&mut self) -> Option<Self::Item> {
649			self.1 += 1;
650			match self.1 {
651				1 => Some(&(self.0).0),
652				2 => Some(&(self.0).1),
653				_ => None,
654			}
655		}
656	}
657	
658	impl<'a, T> IntoIterator for &'a Vec2<T> {
659		type Item = &'a T;
660		type IntoIter = IterRef<'a, T>;
661		
662		#[inline]
663		fn into_iter(self) -> Self::IntoIter {
664			IterRef(self, 0)
665		}
666	}
667	
668	pub struct IterMut<'a, T>(&'a mut Vec2<T>, usize);
669	
670	impl<'a, T> Iterator for IterMut<'a, T> {
671		type Item = &'a mut T;
672		
673		fn next(&mut self) -> Option<Self::Item> {
674			self.1 += 1;
675			match self.1 {
676				1 => Some(unsafe { ::std::mem::transmute(&mut (self.0).0) }),
677				2 => Some(unsafe { ::std::mem::transmute(&mut (self.0).1) }),
678				_ => None,
679			}
680		}
681	}
682	
683	impl<'a, T> IntoIterator for &'a mut Vec2<T> {
684		type Item = &'a mut T;
685		type IntoIter = IterMut<'a, T>;
686		
687		#[inline]
688		fn into_iter(self) -> Self::IntoIter {
689			IterMut(self, 0)
690		}
691	}
692}
693
694pub mod vec3 {
695	use super::*;
696	
697	#[derive(Copy, Clone, Debug, PartialOrd, PartialEq)]
698	pub struct Vec3<T>(pub T, pub T, pub T);
699	
700	impl<T> Vec3<T> {
701		#[inline]
702		pub fn map<F: FnMut(T) -> U, U>(self, mut f: F) -> Vec3<U> {
703			Vec3(f(self.0),
704				 f(self.1),
705				 f(self.2))
706		}
707	}
708	
709	impl<T: Copy> Vec3<T> {
710		#[inline] pub fn xx(self) -> Vec2<T> { Vec2(self.0, self.0) }
711		#[inline] pub fn xy(self) -> Vec2<T> { Vec2(self.0, self.1) }
712		#[inline] pub fn xz(self) -> Vec2<T> { Vec2(self.0, self.2) }
713		#[inline] pub fn yx(self) -> Vec2<T> { Vec2(self.1, self.0) }
714		#[inline] pub fn yy(self) -> Vec2<T> { Vec2(self.1, self.1) }
715		#[inline] pub fn yz(self) -> Vec2<T> { Vec2(self.1, self.2) }
716		#[inline] pub fn zx(self) -> Vec2<T> { Vec2(self.2, self.0) }
717		#[inline] pub fn zy(self) -> Vec2<T> { Vec2(self.2, self.1) }
718		#[inline] pub fn zz(self) -> Vec2<T> { Vec2(self.2, self.2) }
719		#[inline] pub fn xxx(self) -> Vec3<T> { Vec3(self.0, self.0, self.0) }
720		#[inline] pub fn xxy(self) -> Vec3<T> { Vec3(self.0, self.0, self.1) }
721		#[inline] pub fn xxz(self) -> Vec3<T> { Vec3(self.0, self.0, self.2) }
722		#[inline] pub fn xyx(self) -> Vec3<T> { Vec3(self.0, self.1, self.0) }
723		#[inline] pub fn xyy(self) -> Vec3<T> { Vec3(self.0, self.1, self.1) }
724		#[inline] pub fn xyz(self) -> Vec3<T> { Vec3(self.0, self.1, self.2) }
725		#[inline] pub fn xzx(self) -> Vec3<T> { Vec3(self.0, self.2, self.0) }
726		#[inline] pub fn xzy(self) -> Vec3<T> { Vec3(self.0, self.2, self.1) }
727		#[inline] pub fn xzz(self) -> Vec3<T> { Vec3(self.0, self.2, self.2) }
728		#[inline] pub fn yxx(self) -> Vec3<T> { Vec3(self.1, self.0, self.0) }
729		#[inline] pub fn yxy(self) -> Vec3<T> { Vec3(self.1, self.0, self.1) }
730		#[inline] pub fn yxz(self) -> Vec3<T> { Vec3(self.1, self.0, self.2) }
731		#[inline] pub fn yyx(self) -> Vec3<T> { Vec3(self.1, self.1, self.0) }
732		#[inline] pub fn yyy(self) -> Vec3<T> { Vec3(self.1, self.1, self.1) }
733		#[inline] pub fn yyz(self) -> Vec3<T> { Vec3(self.1, self.1, self.2) }
734		#[inline] pub fn yzx(self) -> Vec3<T> { Vec3(self.1, self.2, self.0) }
735		#[inline] pub fn yzy(self) -> Vec3<T> { Vec3(self.1, self.2, self.1) }
736		#[inline] pub fn yzz(self) -> Vec3<T> { Vec3(self.1, self.2, self.2) }
737		#[inline] pub fn zxx(self) -> Vec3<T> { Vec3(self.2, self.0, self.0) }
738		#[inline] pub fn zxy(self) -> Vec3<T> { Vec3(self.2, self.0, self.1) }
739		#[inline] pub fn zxz(self) -> Vec3<T> { Vec3(self.2, self.0, self.2) }
740		#[inline] pub fn zyx(self) -> Vec3<T> { Vec3(self.2, self.1, self.0) }
741		#[inline] pub fn zyy(self) -> Vec3<T> { Vec3(self.2, self.1, self.1) }
742		#[inline] pub fn zyz(self) -> Vec3<T> { Vec3(self.2, self.1, self.2) }
743		#[inline] pub fn zzx(self) -> Vec3<T> { Vec3(self.2, self.2, self.0) }
744		#[inline] pub fn zzy(self) -> Vec3<T> { Vec3(self.2, self.2, self.1) }
745		#[inline] pub fn zzz(self) -> Vec3<T> { Vec3(self.2, self.2, self.2) }
746	}
747	
748	impl<T: Default> Default for Vec3<T> {
749		fn default() -> Self {
750			Self(T::default(),
751				 T::default(),
752				 T::default())
753		}
754	}
755	
756	impl<T: Copy> From<T> for Vec3<T> {
757		fn from(v: T) -> Self {
758			Vec3(v, v, v)
759		}
760	}
761	
762	impl<T: Copy> From<[T; 3]> for Vec3<T> {
763		fn from(v: [T; 3]) -> Self {
764			Vec3(v[0], v[1], v[2])
765		}
766	}
767	
768	impl<T> From<(T, T, T)> for Vec3<T> {
769		fn from(v: (T, T, T)) -> Self {
770			Vec3(v.0, v.1, v.2)
771		}
772	}
773	
774	impl<T> From<(Vec2<T>, T)> for Vec3<T> {
775		fn from(v: (Vec2<T>, T)) -> Self {
776			Vec3((v.0).0, (v.0).1, v.1)
777		}
778	}
779	
780	impl<T> From<(T, Vec2<T>)> for Vec3<T> {
781		fn from(v: (T, Vec2<T>)) -> Self {
782			Vec3(v.0, (v.1).0, (v.1).1)
783		}
784	}
785	
786	impl Vec3<f32> {
787		pub fn abs(self) -> Self {
788			Self(self.0.abs(),
789				 self.1.abs(),
790				 self.2.abs())
791		}
792		
793		pub fn powf(self, rhs: Self) -> Self {
794			Self(self.0.powf(rhs.0),
795				 self.1.powf(rhs.1),
796				 self.2.powf(rhs.2))
797		}
798		
799		pub fn powi(self, rhs: Vec3<i32>) -> Self {
800			Self(self.0.powi(rhs.0),
801				 self.1.powi(rhs.1),
802				 self.2.powi(rhs.2))
803		}
804		
805		pub fn sqrt(self) -> Self {
806			Self(self.0.sqrt(),
807				 self.1.sqrt(),
808				 self.2.sqrt())
809		}
810		
811		pub fn cbrt(self) -> Self {
812			Self(self.0.cbrt(),
813				 self.1.cbrt(),
814				 self.2.cbrt())
815		}
816		
817		pub fn log(self, rhs: Self) -> Self {
818			Self(self.0.log(rhs.0),
819				 self.1.log(rhs.1),
820				 self.2.log(rhs.2))
821		}
822		
823		pub fn len2(self) -> f32 {
824			self.dot(self)
825		}
826		
827		pub fn len(self) -> f32 {
828			self.dot(self).sqrt()
829		}
830		
831		pub fn normalize(self, len: f32) -> Self {
832			self * (len / self.len())
833		}
834		
835		pub fn distance2(self, other: Self) -> f32 {
836			(other - self).len2()
837		}
838		
839		pub fn distance(self, other: Self) -> f32 {
840			(other - self).len()
841		}
842		
843		pub fn angle(self, other: Self) -> f32 {
844			self.dot(other) / (self.len() * other.len())
845		}
846	}
847	
848	impl Vec3<f64> {
849		pub fn abs(self) -> Self {
850			Self(self.0.abs(),
851				 self.1.abs(),
852				 self.2.abs())
853		}
854		
855		pub fn powf(self, rhs: Self) -> Self {
856			Self(self.0.powf(rhs.0),
857				 self.1.powf(rhs.1),
858				 self.2.powf(rhs.2))
859		}
860		
861		pub fn powi(self, rhs: Vec3<i32>) -> Self {
862			Self(self.0.powi(rhs.0),
863				 self.1.powi(rhs.1),
864				 self.2.powi(rhs.2))
865		}
866		
867		pub fn sqrt(self) -> Self {
868			Self(self.0.sqrt(),
869				 self.1.sqrt(),
870				 self.2.sqrt())
871		}
872		
873		pub fn cbrt(self) -> Self {
874			Self(self.0.cbrt(),
875				 self.1.cbrt(),
876				 self.2.cbrt())
877		}
878		
879		pub fn log(self, rhs: Self) -> Self {
880			Self(self.0.log(rhs.0),
881				 self.1.log(rhs.1),
882				 self.2.log(rhs.2))
883		}
884		
885		pub fn len2(self) -> f64 {
886			self.dot(self)
887		}
888		
889		pub fn len(self) -> f64 {
890			self.dot(self).sqrt()
891		}
892		
893		pub fn normalize(self, len: f64) -> Self {
894			self * (len / self.len())
895		}
896		
897		pub fn distance(self, other: Self) -> f64 {
898			(other - self).len()
899		}
900		
901		pub fn angle(self, other: Self) -> f64 {
902			(self.dot(other) / (self.len() * other.len())).acos()
903		}
904	}
905	
906	impl Vec3<bool> {
907		pub fn and(self) -> bool {
908			self.0 && self.1 && self.2
909		}
910		
911		pub fn or(self) -> bool {
912			self.0 || self.1 || self.2
913		}
914		
915		pub fn xor(self) -> bool {
916			self.0 ^ self.1 ^ self.2
917		}
918	}
919	
920	impl<T> Index<usize> for Vec3<T> {
921		type Output = T;
922		
923		fn index(&self, index: usize) -> &Self::Output {
924			match index {
925				0 => &self.0,
926				1 => &self.1,
927				2 => &self.2,
928				_ => panic!()
929			}
930		}
931	}
932	
933	impl<T> IndexMut<usize> for Vec3<T> {
934		fn index_mut(&mut self, index: usize) -> &mut Self::Output {
935			match index {
936				0 => &mut self.0,
937				1 => &mut self.1,
938				2 => &mut self.2,
939				_ => panic!()
940			}
941		}
942	}
943	
944	impl<T: PartialOrd + Copy> Vec3<T> {
945		pub fn min(self, other: Self) -> Self {
946			Self(if self.0 < other.0 { self.0 } else { other.0 },
947				 if self.1 < other.1 { self.1 } else { other.1 },
948				 if self.2 < other.2 { self.2 } else { other.2 })
949		}
950		
951		pub fn mins(self, other: T) -> Self {
952			Self(if self.0 < other { self.0 } else { other },
953				 if self.1 < other { self.1 } else { other },
954				 if self.2 < other { self.2 } else { other })
955		}
956		
957		pub fn max(self, other: Self) -> Self {
958			Self(if self.0 > other.0 { self.0 } else { other.0 },
959				 if self.1 > other.1 { self.1 } else { other.1 },
960				 if self.2 > other.2 { self.2 } else { other.2 })
961		}
962		
963		pub fn maxs(self, other: T) -> Self {
964			Self(if self.0 > other { self.0 } else { other },
965				 if self.1 > other { self.1 } else { other },
966				 if self.2 > other { self.2 } else { other })
967		}
968		
969		pub fn clamp(self, lower: Self, upper: Self) -> Self {
970			Self(if self.0 < lower.0 { lower.0 } else if self.0 > upper.0 { upper.0 } else { self.0 },
971				 if self.1 < lower.1 { lower.1 } else if self.1 > upper.1 { upper.1 } else { self.1 },
972				 if self.2 < lower.2 { lower.2 } else if self.2 > upper.2 { upper.2 } else { self.2 })
973		}
974		
975		pub fn clamps(self, lower: T, upper: T) -> Self {
976			Self(if self.0 < lower { lower } else if self.0 > upper { upper } else { self.0 },
977				 if self.1 < lower { lower } else if self.1 > upper { upper } else { self.1 },
978				 if self.2 < lower { lower } else if self.2 > upper { upper } else { self.2 })
979		}
980	}
981	
982	impl<T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Copy> Vec3<T> {
983		pub fn dot(self, other: Self) -> T {
984			self.0 * other.0 + self.1 * other.1 + self.2 * other.2
985		}
986		
987		pub fn cross(self, other: Self) -> Self {
988			Self(self.1 * other.2 - self.2 * other.1,
989				 self.2 * other.0 - self.0 * other.2,
990				 self.0 * other.1 - self.1 * other.0)
991		}
992		
993		pub fn mix(self, a: Self, b: Self) -> Self {
994			Self(a.0 + (b.0 - a.0) * self.0,
995				 a.1 + (b.1 - a.1) * self.1,
996				 a.2 + (b.2 - a.2) * self.2)
997		}
998	}
999	
1000	impl<T: Add> Add for Vec3<T> {
1001		type Output = Vec3<T::Output>;
1002		
1003		fn add(self, rhs: Self) -> Self::Output {
1004			Vec3(self.0 + rhs.0,
1005				 self.1 + rhs.1,
1006				 self.2 + rhs.2)
1007		}
1008	}
1009	
1010	impl<T: Sub> Sub for Vec3<T> {
1011		type Output = Vec3<T::Output>;
1012		
1013		fn sub(self, rhs: Self) -> Self::Output {
1014			Vec3(self.0 - rhs.0,
1015				 self.1 - rhs.1,
1016				 self.2 - rhs.2)
1017		}
1018	}
1019	
1020	impl<T: Mul> Mul for Vec3<T> {
1021		type Output = Vec3<T::Output>;
1022		
1023		fn mul(self, rhs: Self) -> Self::Output {
1024			Vec3(self.0 * rhs.0,
1025				 self.1 * rhs.1,
1026				 self.2 * rhs.2)
1027		}
1028	}
1029	
1030	impl<T: Mul + Copy> Mul<T> for Vec3<T> {
1031		type Output = Vec3<T::Output>;
1032		
1033		fn mul(self, rhs: T) -> Self::Output {
1034			Vec3(self.0 * rhs,
1035				 self.1 * rhs,
1036				 self.2 * rhs)
1037		}
1038	}
1039	
1040	impl<T: Div> Div for Vec3<T> {
1041		type Output = Vec3<T::Output>;
1042		
1043		fn div(self, rhs: Self) -> Self::Output {
1044			Vec3(self.0 / rhs.0,
1045				 self.1 / rhs.1,
1046				 self.2 / rhs.2)
1047		}
1048	}
1049	
1050	impl<T: Div + Copy> Div<T> for Vec3<T> {
1051		type Output = Vec3<T::Output>;
1052		
1053		fn div(self, rhs: T) -> Self::Output {
1054			Vec3(self.0 / rhs,
1055				 self.1 / rhs,
1056				 self.2 / rhs)
1057		}
1058	}
1059	
1060	impl<T: Rem> Rem for Vec3<T> {
1061		type Output = Vec3<T::Output>;
1062		
1063		fn rem(self, rhs: Self) -> Self::Output {
1064			Vec3(self.0 % rhs.0,
1065				 self.1 % rhs.1,
1066				 self.2 % rhs.2)
1067		}
1068	}
1069	
1070	impl<T: Rem + Copy> Rem<T> for Vec3<T> {
1071		type Output = Vec3<T::Output>;
1072		
1073		fn rem(self, rhs: T) -> Self::Output {
1074			Vec3(self.0 % rhs,
1075				 self.1 % rhs,
1076				 self.2 % rhs)
1077		}
1078	}
1079	
1080	impl<T: Neg> Neg for Vec3<T> {
1081		type Output = Vec3<T::Output>;
1082		
1083		fn neg(self) -> Self::Output {
1084			Vec3(-self.0,
1085				 -self.1,
1086				 -self.2)
1087		}
1088	}
1089	
1090	impl<T: Not> Not for Vec3<T> {
1091		type Output = Vec3<T::Output>;
1092		
1093		fn not(self) -> Self::Output {
1094			Vec3(!self.0,
1095				 !self.1,
1096				 !self.2)
1097		}
1098	}
1099	
1100	impl<T: BitAnd> BitAnd for Vec3<T> {
1101		type Output = Vec3<T::Output>;
1102		
1103		fn bitand(self, rhs: Self) -> Self::Output {
1104			Vec3(self.0 & rhs.0,
1105				 self.1 & rhs.1,
1106				 self.2 & rhs.2)
1107		}
1108	}
1109	
1110	impl<T: BitAnd + Copy> BitAnd<T> for Vec3<T> {
1111		type Output = Vec3<T::Output>;
1112		
1113		fn bitand(self, rhs: T) -> Self::Output {
1114			Vec3(self.0 & rhs,
1115				 self.1 & rhs,
1116				 self.2 & rhs)
1117		}
1118	}
1119	
1120	impl<T: BitOr> BitOr for Vec3<T> {
1121		type Output = Vec3<T::Output>;
1122		
1123		fn bitor(self, rhs: Self) -> Self::Output {
1124			Vec3(self.0 | rhs.0,
1125				 self.1 | rhs.1,
1126				 self.2 | rhs.2)
1127		}
1128	}
1129	
1130	impl<T: BitOr + Copy> BitOr<T> for Vec3<T> {
1131		type Output = Vec3<T::Output>;
1132		
1133		fn bitor(self, rhs: T) -> Self::Output {
1134			Vec3(self.0 | rhs,
1135				 self.1 | rhs,
1136				 self.2 | rhs)
1137		}
1138	}
1139	
1140	impl<T: BitXor> BitXor for Vec3<T> {
1141		type Output = Vec3<T::Output>;
1142		
1143		fn bitxor(self, rhs: Self) -> Self::Output {
1144			Vec3(self.0 ^ rhs.0,
1145				 self.1 ^ rhs.1,
1146				 self.2 ^ rhs.2)
1147		}
1148	}
1149	
1150	impl<T: BitXor + Copy> BitXor<T> for Vec3<T> {
1151		type Output = Vec3<T::Output>;
1152		
1153		fn bitxor(self, rhs: T) -> Self::Output {
1154			Vec3(self.0 ^ rhs,
1155				 self.1 ^ rhs,
1156				 self.2 ^ rhs)
1157		}
1158	}
1159	
1160	impl<T: Shl> Shl for Vec3<T> {
1161		type Output = Vec3<T::Output>;
1162		
1163		fn shl(self, rhs: Self) -> Self::Output {
1164			Vec3(self.0 << rhs.0,
1165				 self.1 << rhs.1,
1166				 self.2 << rhs.2)
1167		}
1168	}
1169	
1170	impl<T: Shl + Copy> Shl<T> for Vec3<T> {
1171		type Output = Vec3<T::Output>;
1172		
1173		fn shl(self, rhs: T) -> Self::Output {
1174			Vec3(self.0 << rhs,
1175				 self.1 << rhs,
1176				 self.2 << rhs)
1177		}
1178	}
1179	
1180	impl<T: Shr> Shr for Vec3<T> {
1181		type Output = Vec3<T::Output>;
1182		
1183		fn shr(self, rhs: Self) -> Self::Output {
1184			Vec3(self.0 >> rhs.0,
1185				 self.1 >> rhs.1,
1186				 self.2 >> rhs.2)
1187		}
1188	}
1189	
1190	impl<T: Shr + Copy> Shr<T> for Vec3<T> {
1191		type Output = Vec3<T::Output>;
1192		
1193		fn shr(self, rhs: T) -> Self::Output {
1194			Vec3(self.0 >> rhs,
1195				 self.1 >> rhs,
1196				 self.2 >> rhs)
1197		}
1198	}
1199	
1200	impl<T: AddAssign> AddAssign for Vec3<T> {
1201		fn add_assign(&mut self, rhs: Self) {
1202			self.0 += rhs.0;
1203			self.1 += rhs.1;
1204			self.2 += rhs.2;
1205		}
1206	}
1207	
1208	impl<T: SubAssign> SubAssign for Vec3<T> {
1209		fn sub_assign(&mut self, rhs: Self) {
1210			self.0 -= rhs.0;
1211			self.1 -= rhs.1;
1212			self.2 -= rhs.2;
1213		}
1214	}
1215	
1216	impl<T: MulAssign> MulAssign for Vec3<T> {
1217		fn mul_assign(&mut self, rhs: Self) {
1218			self.0 *= rhs.0;
1219			self.1 *= rhs.1;
1220			self.2 *= rhs.2;
1221		}
1222	}
1223	
1224	impl<T: MulAssign + Copy> MulAssign<T> for Vec3<T> {
1225		fn mul_assign(&mut self, rhs: T) {
1226			self.0 *= rhs;
1227			self.1 *= rhs;
1228			self.2 *= rhs;
1229		}
1230	}
1231	
1232	impl<T: DivAssign> DivAssign for Vec3<T> {
1233		fn div_assign(&mut self, rhs: Self) {
1234			self.0 /= rhs.0;
1235			self.1 /= rhs.1;
1236			self.2 /= rhs.2;
1237		}
1238	}
1239	
1240	impl<T: DivAssign + Copy> DivAssign<T> for Vec3<T> {
1241		fn div_assign(&mut self, rhs: T) {
1242			self.0 /= rhs;
1243			self.1 /= rhs;
1244			self.2 /= rhs;
1245		}
1246	}
1247	
1248	impl<T: RemAssign> RemAssign for Vec3<T> {
1249		fn rem_assign(&mut self, rhs: Self) {
1250			self.0 %= rhs.0;
1251			self.1 %= rhs.1;
1252			self.2 %= rhs.2;
1253		}
1254	}
1255	
1256	impl<T: RemAssign + Copy> RemAssign<T> for Vec3<T> {
1257		fn rem_assign(&mut self, rhs: T) {
1258			self.0 %= rhs;
1259			self.1 %= rhs;
1260			self.2 %= rhs;
1261		}
1262	}
1263	
1264	impl<T: BitAndAssign> BitAndAssign for Vec3<T> {
1265		fn bitand_assign(&mut self, rhs: Self) {
1266			self.0 &= rhs.0;
1267			self.1 &= rhs.1;
1268			self.2 &= rhs.2;
1269		}
1270	}
1271	
1272	impl<T: BitAndAssign + Copy> BitAndAssign<T> for Vec3<T> {
1273		fn bitand_assign(&mut self, rhs: T) {
1274			self.0 &= rhs;
1275			self.1 &= rhs;
1276			self.2 &= rhs;
1277		}
1278	}
1279	
1280	impl<T: BitOrAssign> BitOrAssign for Vec3<T> {
1281		fn bitor_assign(&mut self, rhs: Self) {
1282			self.0 |= rhs.0;
1283			self.1 |= rhs.1;
1284			self.2 |= rhs.2;
1285		}
1286	}
1287	
1288	impl<T: BitOrAssign + Copy> BitOrAssign<T> for Vec3<T> {
1289		fn bitor_assign(&mut self, rhs: T) {
1290			self.0 |= rhs;
1291			self.1 |= rhs;
1292			self.2 |= rhs;
1293		}
1294	}
1295	
1296	impl<T: BitXorAssign> BitXorAssign for Vec3<T> {
1297		fn bitxor_assign(&mut self, rhs: Self) {
1298			self.0 ^= rhs.0;
1299			self.1 ^= rhs.1;
1300			self.2 ^= rhs.2;
1301		}
1302	}
1303	
1304	impl<T: BitXorAssign + Copy> BitXorAssign<T> for Vec3<T> {
1305		fn bitxor_assign(&mut self, rhs: T) {
1306			self.0 ^= rhs;
1307			self.1 ^= rhs;
1308			self.2 ^= rhs;
1309		}
1310	}
1311	
1312	impl<T: ShlAssign> ShlAssign for Vec3<T> {
1313		fn shl_assign(&mut self, rhs: Self) {
1314			self.0 <<= rhs.0;
1315			self.1 <<= rhs.1;
1316			self.2 <<= rhs.2;
1317		}
1318	}
1319	
1320	impl<T: ShlAssign + Copy> ShlAssign<T> for Vec3<T> {
1321		fn shl_assign(&mut self, rhs: T) {
1322			self.0 <<= rhs;
1323			self.1 <<= rhs;
1324			self.2 <<= rhs;
1325		}
1326	}
1327	
1328	impl<T: ShrAssign> ShrAssign for Vec3<T> {
1329		fn shr_assign(&mut self, rhs: Self) {
1330			self.0 >>= rhs.0;
1331			self.1 >>= rhs.1;
1332			self.2 >>= rhs.2;
1333		}
1334	}
1335	
1336	impl<T: ShrAssign + Copy> ShrAssign<T> for Vec3<T> {
1337		fn shr_assign(&mut self, rhs: T) {
1338			self.0 >>= rhs;
1339			self.1 >>= rhs;
1340			self.2 >>= rhs;
1341		}
1342	}
1343	
1344	impl<A> FromIterator<A> for Vec3<A> {
1345		fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self {
1346			let mut iter = iter.into_iter();
1347			Vec3(iter.next().unwrap(),
1348				 iter.next().unwrap(),
1349				 iter.next().unwrap())
1350		}
1351	}
1352	
1353	pub struct Iter<T>(Vec3<T>, usize);
1354	
1355	impl<T: Clone> Iterator for Iter<T> {
1356		type Item = T;
1357		
1358		fn next(&mut self) -> Option<Self::Item> {
1359			self.1 += 1;
1360			match self.1 {
1361				1 => Some((self.0).0.clone()),
1362				2 => Some((self.0).1.clone()),
1363				3 => Some((self.0).2.clone()),
1364				_ => None,
1365			}
1366		}
1367	}
1368	
1369	impl<T: Clone> IntoIterator for Vec3<T> {
1370		type Item = T;
1371		type IntoIter = Iter<T>;
1372		
1373		#[inline]
1374		fn into_iter(self) -> Self::IntoIter {
1375			Iter(self, 0)
1376		}
1377	}
1378	
1379	pub struct IterRef<'a, T>(&'a Vec3<T>, usize);
1380	
1381	impl<'a, T> Iterator for IterRef<'a, T> {
1382		type Item = &'a T;
1383		
1384		fn next(&mut self) -> Option<Self::Item> {
1385			self.1 += 1;
1386			match self.1 {
1387				1 => Some(&(self.0).0),
1388				2 => Some(&(self.0).1),
1389				3 => Some(&(self.0).2),
1390				_ => None,
1391			}
1392		}
1393	}
1394	
1395	impl<'a, T> IntoIterator for &'a Vec3<T> {
1396		type Item = &'a T;
1397		type IntoIter = IterRef<'a, T>;
1398		
1399		#[inline]
1400		fn into_iter(self) -> Self::IntoIter {
1401			IterRef(self, 0)
1402		}
1403	}
1404	
1405	pub struct IterMut<'a, T>(&'a mut Vec3<T>, usize);
1406	
1407	impl<'a, T> Iterator for IterMut<'a, T> {
1408		type Item = &'a mut T;
1409		
1410		fn next(&mut self) -> Option<Self::Item> {
1411			self.1 += 1;
1412			match self.1 {
1413				1 => Some(unsafe { ::std::mem::transmute(&mut (self.0).0) }),
1414				2 => Some(unsafe { ::std::mem::transmute(&mut (self.0).1) }),
1415				3 => Some(unsafe { ::std::mem::transmute(&mut (self.0).2) }),
1416				_ => None,
1417			}
1418		}
1419	}
1420	
1421	impl<'a, T> IntoIterator for &'a mut Vec3<T> {
1422		type Item = &'a mut T;
1423		type IntoIter = IterMut<'a, T>;
1424		
1425		#[inline]
1426		fn into_iter(self) -> Self::IntoIter {
1427			IterMut(self, 0)
1428		}
1429	}
1430}
1431
1432pub mod vec4 {
1433	use super::*;
1434	
1435	#[derive(Copy, Clone, Debug, PartialOrd, PartialEq)]
1436	pub struct Vec4<T>(pub T, pub T, pub T, pub T);
1437	
1438	impl<T> Vec4<T> {
1439		#[inline]
1440		pub fn map<F: FnMut(T) -> U, U>(self, mut f: F) -> Vec4<U> {
1441			Vec4(f(self.0),
1442				 f(self.1),
1443				 f(self.2),
1444				 f(self.3))
1445		}
1446	}
1447	
1448	impl<T: Default> Default for Vec4<T> {
1449		fn default() -> Self {
1450			Self(T::default(),
1451				 T::default(),
1452				 T::default(),
1453				 T::default())
1454		}
1455	}
1456	
1457	impl<T> Vec4<T> {
1458		#[inline]
1459		pub fn xyz(self) -> Vec3<T> {
1460			Vec3(self.0, self.1, self.2)
1461		}
1462	}
1463	
1464	impl<T: Copy> From<T> for Vec4<T> {
1465		fn from(v: T) -> Self {
1466			Self(v, v, v, v)
1467		}
1468	}
1469	
1470	impl<T: Copy> From<[T; 4]> for Vec4<T> {
1471		fn from(v: [T; 4]) -> Self {
1472			Self(v[0], v[1], v[2], v[3])
1473		}
1474	}
1475	
1476	impl<T> From<(T, T, T, T)> for Vec4<T> {
1477		fn from(v: (T, T, T, T)) -> Self {
1478			Self(v.0, v.1, v.2, v.3)
1479		}
1480	}
1481	
1482	impl Vec4<f32> {
1483		pub fn abs(self) -> Self {
1484			Self(self.0.abs(),
1485				 self.1.abs(),
1486				 self.2.abs(),
1487				 self.3.abs())
1488		}
1489		
1490		pub fn powf(self, rhs: Self) -> Self {
1491			Self(self.0.powf(rhs.0),
1492				 self.1.powf(rhs.1),
1493				 self.2.powf(rhs.2),
1494				 self.3.powf(rhs.3))
1495		}
1496		
1497		pub fn powi(self, rhs: Vec4<i32>) -> Self {
1498			Self(self.0.powi(rhs.0),
1499				 self.1.powi(rhs.1),
1500				 self.2.powi(rhs.2),
1501				 self.3.powi(rhs.3))
1502		}
1503		
1504		pub fn sqrt(self) -> Self {
1505			Self(self.0.sqrt(),
1506				 self.1.sqrt(),
1507				 self.2.sqrt(),
1508				 self.3.sqrt())
1509		}
1510		
1511		pub fn cbrt(self) -> Self {
1512			Self(self.0.cbrt(),
1513				 self.1.cbrt(),
1514				 self.2.cbrt(),
1515				 self.3.cbrt())
1516		}
1517		
1518		pub fn log(self, rhs: Self) -> Self {
1519			Self(self.0.log(rhs.0),
1520				 self.1.log(rhs.1),
1521				 self.2.log(rhs.2),
1522				 self.3.log(rhs.3))
1523		}
1524		
1525		pub fn len2(self) -> f32 {
1526			self.dot(self)
1527		}
1528		
1529		pub fn len(self) -> f32 {
1530			self.dot(self).sqrt()
1531		}
1532		
1533		pub fn normalize(self, len: f32) -> Self {
1534			self * (len / self.len())
1535		}
1536		
1537		pub fn distance2(self, other: Self) -> f32 {
1538			(other - self).len2()
1539		}
1540		
1541		pub fn distance(self, other: Self) -> f32 {
1542			(other - self).len()
1543		}
1544		
1545		pub fn angle(self, other: Self) -> f32 {
1546			self.dot(other) / (self.len() * other.len())
1547		}
1548	}
1549	
1550	impl Vec4<f64> {
1551		pub fn abs(self) -> Self {
1552			Self(self.0.abs(),
1553				 self.1.abs(),
1554				 self.2.abs(),
1555				 self.3.abs())
1556		}
1557		
1558		pub fn powf(self, rhs: Self) -> Self {
1559			Self(self.0.powf(rhs.0),
1560				 self.1.powf(rhs.1),
1561				 self.2.powf(rhs.2),
1562				 self.3.powf(rhs.3))
1563		}
1564		
1565		pub fn powi(self, rhs: Vec4<i32>) -> Self {
1566			Self(self.0.powi(rhs.0),
1567				 self.1.powi(rhs.1),
1568				 self.2.powi(rhs.2),
1569				 self.3.powi(rhs.3))
1570		}
1571		
1572		pub fn sqrt(self) -> Self {
1573			Self(self.0.sqrt(),
1574				 self.1.sqrt(),
1575				 self.2.sqrt(),
1576				 self.3.sqrt())
1577		}
1578		
1579		pub fn cbrt(self) -> Self {
1580			Self(self.0.cbrt(),
1581				 self.1.cbrt(),
1582				 self.2.cbrt(),
1583				 self.3.cbrt())
1584		}
1585		
1586		pub fn log(self, rhs: Self) -> Self {
1587			Self(self.0.log(rhs.0),
1588				 self.1.log(rhs.1),
1589				 self.2.log(rhs.2),
1590				 self.2.log(rhs.3))
1591		}
1592		
1593		pub fn len2(self) -> f64 {
1594			self.dot(self)
1595		}
1596		
1597		pub fn len(self) -> f64 {
1598			self.dot(self).sqrt()
1599		}
1600		
1601		pub fn normalize(self, len: f64) -> Self {
1602			self * (len / self.len())
1603		}
1604		
1605		pub fn distance2(self, other: Self) -> f64 {
1606			(other - self).len2()
1607		}
1608		
1609		pub fn distance(self, other: Self) -> f64 {
1610			(other - self).len()
1611		}
1612		
1613		pub fn angle(self, other: Self) -> f64 {
1614			(self.dot(other) / (self.len() * other.len())).acos()
1615		}
1616	}
1617	
1618	impl Vec4<bool> {
1619		pub fn and(self) -> bool {
1620			self.0 && self.1 && self.2 && self.3
1621		}
1622		
1623		pub fn or(self) -> bool {
1624			self.0 || self.1 || self.2 || self.3
1625		}
1626		
1627		pub fn xor(self) -> bool {
1628			self.0 ^ self.1 ^ self.2 ^ self.3
1629		}
1630	}
1631	
1632	impl<T> Index<usize> for Vec4<T> {
1633		type Output = T;
1634		
1635		fn index(&self, index: usize) -> &Self::Output {
1636			match index {
1637				0 => &self.0,
1638				1 => &self.1,
1639				2 => &self.2,
1640				3 => &self.3,
1641				_ => panic!()
1642			}
1643		}
1644	}
1645	
1646	impl<T> IndexMut<usize> for Vec4<T> {
1647		fn index_mut(&mut self, index: usize) -> &mut Self::Output {
1648			match index {
1649				0 => &mut self.0,
1650				1 => &mut self.1,
1651				2 => &mut self.2,
1652				3 => &mut self.3,
1653				_ => panic!()
1654			}
1655		}
1656	}
1657	
1658	impl<T: PartialOrd + Copy> Vec4<T> {
1659		pub fn eq(self, other: Self) -> Vec4<bool> {
1660			Vec4(self.0 == other.0,
1661				 self.1 == other.1,
1662				 self.2 == other.2,
1663				 self.3 == other.3)
1664		}
1665		
1666		pub fn eqs(self, other: T) -> Vec4<bool> {
1667			Vec4(self.0 == other,
1668				 self.1 == other,
1669				 self.2 == other,
1670				 self.3 == other)
1671		}
1672		
1673		
1674		pub fn ne(self, other: Self) -> Vec4<bool> {
1675			Vec4(self.0 != other.0,
1676				 self.1 != other.1,
1677				 self.2 != other.2,
1678				 self.3 != other.3)
1679		}
1680		
1681		pub fn nes(self, other: T) -> Vec4<bool> {
1682			Vec4(self.0 != other,
1683				 self.1 != other,
1684				 self.2 != other,
1685				 self.3 != other)
1686		}
1687		
1688		pub fn gt(self, other: Self) -> Vec4<bool> {
1689			Vec4(self.0 > other.0,
1690				 self.1 > other.1,
1691				 self.2 > other.2,
1692				 self.3 > other.3)
1693		}
1694		
1695		pub fn gts(self, other: T) -> Vec4<bool> {
1696			Vec4(self.0 > other,
1697				 self.1 > other,
1698				 self.2 > other,
1699				 self.3 > other)
1700		}
1701		
1702		
1703		pub fn lt(self, other: Self) -> Vec4<bool> {
1704			Vec4(self.0 < other.0,
1705				 self.1 < other.1,
1706				 self.2 < other.2,
1707				 self.3 < other.3)
1708		}
1709		
1710		pub fn lts(self, other: T) -> Vec4<bool> {
1711			Vec4(self.0 < other,
1712				 self.1 < other,
1713				 self.2 < other,
1714				 self.3 < other)
1715		}
1716		
1717		pub fn ge(self, other: Self) -> Vec4<bool> {
1718			Vec4(self.0 >= other.0,
1719				 self.1 >= other.1,
1720				 self.2 >= other.2,
1721				 self.3 >= other.3)
1722		}
1723		
1724		pub fn ges(self, other: T) -> Vec4<bool> {
1725			Vec4(self.0 >= other,
1726				 self.1 >= other,
1727				 self.2 >= other,
1728				 self.3 >= other)
1729		}
1730		
1731		pub fn le(self, other: Self) -> Vec4<bool> {
1732			Vec4(self.0 <= other.0,
1733				 self.1 <= other.1,
1734				 self.2 <= other.2,
1735				 self.3 <= other.3)
1736		}
1737		
1738		pub fn les(self, other: T) -> Vec4<bool> {
1739			Vec4(self.0 <= other,
1740				 self.1 <= other,
1741				 self.2 <= other,
1742				 self.3 <= other)
1743		}
1744		
1745		pub fn min(self, other: Self) -> Self {
1746			Self(if self.0 < other.0 { self.0 } else { other.0 },
1747				 if self.1 < other.1 { self.1 } else { other.1 },
1748				 if self.2 < other.2 { self.2 } else { other.2 },
1749				 if self.3 < other.3 { self.3 } else { other.3 })
1750		}
1751		
1752		pub fn mins(self, other: T) -> Self {
1753			Self(if self.0 < other { self.0 } else { other },
1754				 if self.1 < other { self.1 } else { other },
1755				 if self.2 < other { self.2 } else { other },
1756				 if self.3 < other { self.3 } else { other })
1757		}
1758		
1759		pub fn max(self, other: Self) -> Self {
1760			Self(if self.0 > other.0 { self.0 } else { other.0 },
1761				 if self.1 > other.1 { self.1 } else { other.1 },
1762				 if self.2 > other.2 { self.2 } else { other.2 },
1763				 if self.3 > other.3 { self.3 } else { other.3 })
1764		}
1765		
1766		pub fn maxs(self, other: T) -> Self {
1767			Self(if self.0 > other { self.0 } else { other },
1768				 if self.1 > other { self.1 } else { other },
1769				 if self.2 > other { self.2 } else { other },
1770				 if self.3 > other { self.3 } else { other })
1771		}
1772		
1773		pub fn clamp(self, lower: Self, upper: Self) -> Self {
1774			Self(if self.0 < lower.0 { lower.0 } else if self.0 > upper.0 { upper.0 } else { self.0 },
1775				 if self.1 < lower.1 { lower.1 } else if self.1 > upper.1 { upper.1 } else { self.1 },
1776				 if self.2 < lower.2 { lower.2 } else if self.2 > upper.2 { upper.2 } else { self.2 },
1777				 if self.3 < lower.3 { lower.3 } else if self.3 > upper.3 { upper.3 } else { self.3 })
1778		}
1779		
1780		pub fn clamps(self, lower: T, upper: T) -> Self {
1781			Self(if self.0 < lower { lower } else if self.0 > upper { upper } else { self.0 },
1782				 if self.1 < lower { lower } else if self.1 > upper { upper } else { self.1 },
1783				 if self.2 < lower { lower } else if self.2 > upper { upper } else { self.2 },
1784				 if self.3 < lower { lower } else if self.3 > upper { upper } else { self.3 })
1785		}
1786	}
1787	
1788	impl<T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Copy> Vec4<T> {
1789		pub fn dot(self, other: Self) -> T {
1790			self.0 * other.0 + self.1 * other.1 + self.2 * other.2 + self.3 * other.3
1791		}
1792		
1793		pub fn mix(self, a: Self, b: Self) -> Self {
1794			Self(a.0 + (b.0 - a.0) * self.0,
1795				 a.1 + (b.1 - a.1) * self.1,
1796				 a.2 + (b.2 - a.2) * self.2,
1797				 a.3 + (b.3 - a.3) * self.3)
1798		}
1799	}
1800	
1801	impl<T: Add> Add for Vec4<T> {
1802		type Output = Vec4<T::Output>;
1803		
1804		fn add(self, rhs: Self) -> Self::Output {
1805			Vec4(self.0 + rhs.0,
1806				 self.1 + rhs.1,
1807				 self.2 + rhs.2,
1808				 self.3 + rhs.3)
1809		}
1810	}
1811	
1812	impl<T: Sub> Sub for Vec4<T> {
1813		type Output = Vec4<T::Output>;
1814		
1815		fn sub(self, rhs: Self) -> Self::Output {
1816			Vec4(self.0 - rhs.0,
1817				 self.1 - rhs.1,
1818				 self.2 - rhs.2,
1819				 self.3 - rhs.3)
1820		}
1821	}
1822	
1823	impl<T: Mul> Mul for Vec4<T> {
1824		type Output = Vec4<T::Output>;
1825		
1826		fn mul(self, rhs: Self) -> Self::Output {
1827			Vec4(self.0 * rhs.0,
1828				 self.1 * rhs.1,
1829				 self.2 * rhs.2,
1830				 self.3 * rhs.3)
1831		}
1832	}
1833	
1834	impl<T: Mul + Copy> Mul<T> for Vec4<T> {
1835		type Output = Vec4<T::Output>;
1836		
1837		fn mul(self, rhs: T) -> Self::Output {
1838			Vec4(self.0 * rhs,
1839				 self.1 * rhs,
1840				 self.2 * rhs,
1841				 self.3 * rhs)
1842		}
1843	}
1844	
1845	impl<T: Div> Div for Vec4<T> {
1846		type Output = Vec4<T::Output>;
1847		
1848		fn div(self, rhs: Self) -> Self::Output {
1849			Vec4(self.0 / rhs.0,
1850				 self.1 / rhs.1,
1851				 self.2 / rhs.2,
1852				 self.3 / rhs.3)
1853		}
1854	}
1855	
1856	impl<T: Div + Copy> Div<T> for Vec4<T> {
1857		type Output = Vec4<T::Output>;
1858		
1859		fn div(self, rhs: T) -> Self::Output {
1860			Vec4(self.0 / rhs,
1861				 self.1 / rhs,
1862				 self.2 / rhs,
1863				 self.3 / rhs)
1864		}
1865	}
1866	
1867	impl<T: Rem> Rem for Vec4<T> {
1868		type Output = Vec4<T::Output>;
1869		
1870		fn rem(self, rhs: Self) -> Self::Output {
1871			Vec4(self.0 % rhs.0,
1872				 self.1 % rhs.1,
1873				 self.2 % rhs.2,
1874				 self.3 % rhs.3)
1875		}
1876	}
1877	
1878	impl<T: Rem + Copy> Rem<T> for Vec4<T> {
1879		type Output = Vec4<T::Output>;
1880		
1881		fn rem(self, rhs: T) -> Self::Output {
1882			Vec4(self.0 % rhs,
1883				 self.1 % rhs,
1884				 self.2 % rhs,
1885				 self.3 % rhs)
1886		}
1887	}
1888	
1889	impl<T: Neg> Neg for Vec4<T> {
1890		type Output = Vec4<T::Output>;
1891		
1892		fn neg(self) -> Self::Output {
1893			Vec4(-self.0,
1894				 -self.1,
1895				 -self.2,
1896				 -self.3)
1897		}
1898	}
1899	
1900	impl<T: Not> Not for Vec4<T> {
1901		type Output = Vec4<T::Output>;
1902		
1903		fn not(self) -> Self::Output {
1904			Vec4(!self.0,
1905				 !self.1,
1906				 !self.2,
1907				 !self.3)
1908		}
1909	}
1910	
1911	impl<T: BitAnd> BitAnd for Vec4<T> {
1912		type Output = Vec4<T::Output>;
1913		
1914		fn bitand(self, rhs: Self) -> Self::Output {
1915			Vec4(self.0 & rhs.0,
1916				 self.1 & rhs.1,
1917				 self.2 & rhs.2,
1918				 self.3 & rhs.3)
1919		}
1920	}
1921	
1922	impl<T: BitAnd + Copy> BitAnd<T> for Vec4<T> {
1923		type Output = Vec4<T::Output>;
1924		
1925		fn bitand(self, rhs: T) -> Self::Output {
1926			Vec4(self.0 & rhs,
1927				 self.1 & rhs,
1928				 self.2 & rhs,
1929				 self.3 & rhs)
1930		}
1931	}
1932	
1933	impl<T: BitOr> BitOr for Vec4<T> {
1934		type Output = Vec4<T::Output>;
1935		
1936		fn bitor(self, rhs: Self) -> Self::Output {
1937			Vec4(self.0 | rhs.0,
1938				 self.1 | rhs.1,
1939				 self.2 | rhs.2,
1940				 self.3 | rhs.3)
1941		}
1942	}
1943	
1944	impl<T: BitOr + Copy> BitOr<T> for Vec4<T> {
1945		type Output = Vec4<T::Output>;
1946		
1947		fn bitor(self, rhs: T) -> Self::Output {
1948			Vec4(self.0 | rhs,
1949				 self.1 | rhs,
1950				 self.2 | rhs,
1951				 self.3 | rhs)
1952		}
1953	}
1954	
1955	impl<T: BitXor> BitXor for Vec4<T> {
1956		type Output = Vec4<T::Output>;
1957		
1958		fn bitxor(self, rhs: Self) -> Self::Output {
1959			Vec4(self.0 ^ rhs.0,
1960				 self.1 ^ rhs.1,
1961				 self.2 ^ rhs.2,
1962				 self.3 ^ rhs.3)
1963		}
1964	}
1965	
1966	impl<T: BitXor + Copy> BitXor<T> for Vec4<T> {
1967		type Output = Vec4<T::Output>;
1968		
1969		fn bitxor(self, rhs: T) -> Self::Output {
1970			Vec4(self.0 ^ rhs,
1971				 self.1 ^ rhs,
1972				 self.2 ^ rhs,
1973				 self.3 ^ rhs)
1974		}
1975	}
1976	
1977	impl<T: Shl> Shl for Vec4<T> {
1978		type Output = Vec4<T::Output>;
1979		
1980		fn shl(self, rhs: Self) -> Self::Output {
1981			Vec4(self.0 << rhs.0,
1982				 self.1 << rhs.1,
1983				 self.2 << rhs.2,
1984				 self.3 << rhs.3)
1985		}
1986	}
1987	
1988	impl<T: Shl + Copy> Shl<T> for Vec4<T> {
1989		type Output = Vec4<T::Output>;
1990		
1991		fn shl(self, rhs: T) -> Self::Output {
1992			Vec4(self.0 << rhs,
1993				 self.1 << rhs,
1994				 self.2 << rhs,
1995				 self.3 << rhs)
1996		}
1997	}
1998	
1999	impl<T: Shr> Shr for Vec4<T> {
2000		type Output = Vec4<T::Output>;
2001		
2002		fn shr(self, rhs: Self) -> Self::Output {
2003			Vec4(self.0 >> rhs.0,
2004				 self.1 >> rhs.1,
2005				 self.2 >> rhs.2,
2006				 self.3 >> rhs.3)
2007		}
2008	}
2009	
2010	impl<T: Shr + Copy> Shr<T> for Vec4<T> {
2011		type Output = Vec4<T::Output>;
2012		
2013		fn shr(self, rhs: T) -> Self::Output {
2014			Vec4(self.0 >> rhs,
2015				 self.1 >> rhs,
2016				 self.2 >> rhs,
2017				 self.3 >> rhs)
2018		}
2019	}
2020	
2021	impl<T: AddAssign> AddAssign for Vec4<T> {
2022		fn add_assign(&mut self, rhs: Self) {
2023			self.0 += rhs.0;
2024			self.1 += rhs.1;
2025			self.2 += rhs.2;
2026			self.3 += rhs.3;
2027		}
2028	}
2029	
2030	impl<T: SubAssign> SubAssign for Vec4<T> {
2031		fn sub_assign(&mut self, rhs: Self) {
2032			self.0 -= rhs.0;
2033			self.1 -= rhs.1;
2034			self.2 -= rhs.2;
2035			self.3 -= rhs.3;
2036		}
2037	}
2038	
2039	impl<T: MulAssign> MulAssign for Vec4<T> {
2040		fn mul_assign(&mut self, rhs: Self) {
2041			self.0 *= rhs.0;
2042			self.1 *= rhs.1;
2043			self.2 *= rhs.2;
2044			self.3 *= rhs.3;
2045		}
2046	}
2047	
2048	impl<T: MulAssign + Copy> MulAssign<T> for Vec4<T> {
2049		fn mul_assign(&mut self, rhs: T) {
2050			self.0 *= rhs;
2051			self.1 *= rhs;
2052			self.2 *= rhs;
2053			self.3 *= rhs;
2054		}
2055	}
2056	
2057	impl<T: DivAssign> DivAssign for Vec4<T> {
2058		fn div_assign(&mut self, rhs: Self) {
2059			self.0 /= rhs.0;
2060			self.1 /= rhs.1;
2061			self.2 /= rhs.2;
2062			self.3 /= rhs.3;
2063		}
2064	}
2065	
2066	impl<T: DivAssign + Copy> DivAssign<T> for Vec4<T> {
2067		fn div_assign(&mut self, rhs: T) {
2068			self.0 /= rhs;
2069			self.1 /= rhs;
2070			self.2 /= rhs;
2071			self.3 /= rhs;
2072		}
2073	}
2074	
2075	impl<T: RemAssign> RemAssign for Vec4<T> {
2076		fn rem_assign(&mut self, rhs: Self) {
2077			self.0 %= rhs.0;
2078			self.1 %= rhs.1;
2079			self.2 %= rhs.2;
2080			self.3 %= rhs.3;
2081		}
2082	}
2083	
2084	impl<T: RemAssign + Copy> RemAssign<T> for Vec4<T> {
2085		fn rem_assign(&mut self, rhs: T) {
2086			self.0 %= rhs;
2087			self.1 %= rhs;
2088			self.2 %= rhs;
2089			self.3 %= rhs;
2090		}
2091	}
2092	
2093	impl<T: BitAndAssign> BitAndAssign for Vec4<T> {
2094		fn bitand_assign(&mut self, rhs: Self) {
2095			self.0 &= rhs.0;
2096			self.1 &= rhs.1;
2097			self.2 &= rhs.2;
2098			self.3 &= rhs.3;
2099		}
2100	}
2101	
2102	impl<T: BitAndAssign + Copy> BitAndAssign<T> for Vec4<T> {
2103		fn bitand_assign(&mut self, rhs: T) {
2104			self.0 &= rhs;
2105			self.1 &= rhs;
2106			self.2 &= rhs;
2107			self.3 &= rhs;
2108		}
2109	}
2110	
2111	impl<T: BitOrAssign> BitOrAssign for Vec4<T> {
2112		fn bitor_assign(&mut self, rhs: Self) {
2113			self.0 |= rhs.0;
2114			self.1 |= rhs.1;
2115			self.2 |= rhs.2;
2116			self.3 |= rhs.3;
2117		}
2118	}
2119	
2120	impl<T: BitOrAssign + Copy> BitOrAssign<T> for Vec4<T> {
2121		fn bitor_assign(&mut self, rhs: T) {
2122			self.0 |= rhs;
2123			self.1 |= rhs;
2124			self.2 |= rhs;
2125			self.3 |= rhs;
2126		}
2127	}
2128	
2129	impl<T: BitXorAssign> BitXorAssign for Vec4<T> {
2130		fn bitxor_assign(&mut self, rhs: Self) {
2131			self.0 ^= rhs.0;
2132			self.1 ^= rhs.1;
2133			self.2 ^= rhs.2;
2134			self.3 ^= rhs.3;
2135		}
2136	}
2137	
2138	impl<T: BitXorAssign + Copy> BitXorAssign<T> for Vec4<T> {
2139		fn bitxor_assign(&mut self, rhs: T) {
2140			self.0 ^= rhs;
2141			self.1 ^= rhs;
2142			self.2 ^= rhs;
2143			self.3 ^= rhs;
2144		}
2145	}
2146	
2147	impl<T: ShlAssign> ShlAssign for Vec4<T> {
2148		fn shl_assign(&mut self, rhs: Self) {
2149			self.0 <<= rhs.0;
2150			self.1 <<= rhs.1;
2151			self.2 <<= rhs.2;
2152			self.3 <<= rhs.3;
2153		}
2154	}
2155	
2156	impl<T: ShlAssign + Copy> ShlAssign<T> for Vec4<T> {
2157		fn shl_assign(&mut self, rhs: T) {
2158			self.0 <<= rhs;
2159			self.1 <<= rhs;
2160			self.2 <<= rhs;
2161			self.3 <<= rhs;
2162		}
2163	}
2164	
2165	impl<T: ShrAssign> ShrAssign for Vec4<T> {
2166		fn shr_assign(&mut self, rhs: Self) {
2167			self.0 >>= rhs.0;
2168			self.1 >>= rhs.1;
2169			self.2 >>= rhs.2;
2170			self.3 >>= rhs.3;
2171		}
2172	}
2173	
2174	impl<T: ShrAssign + Copy> ShrAssign<T> for Vec4<T> {
2175		fn shr_assign(&mut self, rhs: T) {
2176			self.0 >>= rhs;
2177			self.1 >>= rhs;
2178			self.2 >>= rhs;
2179			self.3 >>= rhs;
2180		}
2181	}
2182	
2183	impl<A> FromIterator<A> for Vec4<A> {
2184		fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self {
2185			let mut iter = iter.into_iter();
2186			Vec4(iter.next().unwrap(),
2187				 iter.next().unwrap(),
2188				 iter.next().unwrap(),
2189				 iter.next().unwrap())
2190		}
2191	}
2192	
2193	pub struct Iter<T>(Vec4<T>, usize);
2194	
2195	impl<T: Clone> Iterator for Iter<T> {
2196		type Item = T;
2197		
2198		fn next(&mut self) -> Option<Self::Item> {
2199			self.1 += 1;
2200			match self.1 {
2201				1 => Some((self.0).0.clone()),
2202				2 => Some((self.0).1.clone()),
2203				3 => Some((self.0).2.clone()),
2204				4 => Some((self.0).3.clone()),
2205				_ => None,
2206			}
2207		}
2208	}
2209	
2210	impl<T: Clone> IntoIterator for Vec4<T> {
2211		type Item = T;
2212		type IntoIter = Iter<T>;
2213		
2214		#[inline]
2215		fn into_iter(self) -> Self::IntoIter {
2216			Iter(self, 0)
2217		}
2218	}
2219	
2220	pub struct IterRef<'a, T>(&'a Vec4<T>, usize);
2221	
2222	impl<'a, T> Iterator for IterRef<'a, T> {
2223		type Item = &'a T;
2224		
2225		fn next(&mut self) -> Option<Self::Item> {
2226			self.1 += 1;
2227			match self.1 {
2228				1 => Some(&(self.0).0),
2229				2 => Some(&(self.0).1),
2230				3 => Some(&(self.0).2),
2231				4 => Some(&(self.0).3),
2232				_ => None,
2233			}
2234		}
2235	}
2236	
2237	impl<'a, T> IntoIterator for &'a Vec4<T> {
2238		type Item = &'a T;
2239		type IntoIter = IterRef<'a, T>;
2240		
2241		#[inline]
2242		fn into_iter(self) -> Self::IntoIter {
2243			IterRef(self, 0)
2244		}
2245	}
2246	
2247	pub struct IterMut<'a, T>(&'a mut Vec4<T>, usize);
2248	
2249	impl<'a, T> Iterator for IterMut<'a, T> {
2250		type Item = &'a mut T;
2251		
2252		fn next(&mut self) -> Option<Self::Item> {
2253			self.1 += 1;
2254			match self.1 {
2255				1 => Some(unsafe { ::std::mem::transmute(&mut (self.0).0) }),
2256				2 => Some(unsafe { ::std::mem::transmute(&mut (self.0).1) }),
2257				3 => Some(unsafe { ::std::mem::transmute(&mut (self.0).2) }),
2258				4 => Some(unsafe { ::std::mem::transmute(&mut (self.0).3) }),
2259				_ => None,
2260			}
2261		}
2262	}
2263	
2264	impl<'a, T> IntoIterator for &'a mut Vec4<T> {
2265		type Item = &'a mut T;
2266		type IntoIter = IterMut<'a, T>;
2267		
2268		#[inline]
2269		fn into_iter(self) -> Self::IntoIter {
2270			IterMut(self, 0)
2271		}
2272	}
2273}
2274
2275pub mod quat32 {
2276	use super::*;
2277	
2278	pub type Quat32 = Vec4<f32>;
2279	
2280	impl Quat32 {
2281		pub fn from_rotation_x(angle: f32) -> Self {
2282			Self((angle * 0.5f32).sin(), 0f32, 0f32, (angle * 0.5f32).cos())
2283		}
2284		
2285		pub fn from_rotation_y(angle: f32) -> Self {
2286			Self(0f32, (angle * 0.5f32).sin(), 0f32, (angle * 0.5f32).cos())
2287		}
2288		
2289		pub fn from_rotation_z(angle: f32) -> Self {
2290			Self(0f32, 0f32, (angle * 0.5f32).sin(), (angle * 0.5f32).cos())
2291		}
2292		
2293		pub fn from_axis_angle(axis: Vec3<f32>, angle: f32) -> Self {
2294			let sin = (angle * 0.5f32).sin();
2295			let inv_len = 1f32 / axis.len();
2296			Self(axis.0 * inv_len * sin,
2297				 axis.1 * inv_len * sin,
2298				 axis.2 * inv_len * sin,
2299				 (angle * 0.5f32).cos())
2300		}
2301		
2302		pub fn to_axis_angle(self) -> (Vec3<f32>, f32) {
2303			let acos = self.3.acos();
2304			let inv_sqrt = 1f32 / (1f32 - self.3 * self.3).sqrt();
2305			(Vec3(self.0 * inv_sqrt, self.1 * inv_sqrt, self.3 * inv_sqrt), acos + acos)
2306		}
2307		
2308		pub fn rotate_x_local(self, angle: f32) -> Self {
2309			let sin = (angle * 0.5f32).sin();
2310			let cos = (angle * 0.5f32).cos();
2311			Self(self.0 * cos + self.3 * sin,
2312				 self.1 * cos - self.2 * sin,
2313				 self.2 * cos + self.1 * sin,
2314				 self.3 * cos - self.0 * sin)
2315		}
2316		
2317		pub fn rotate_y_local(self, angle: f32) -> Self {
2318			let sin = (angle * 0.5f32).sin();
2319			let cos = (angle * 0.5f32).cos();
2320			Self(self.0 * cos + self.2 * sin,
2321				 self.1 * cos + self.3 * sin,
2322				 self.2 * cos - self.0 * sin,
2323				 self.3 * cos - self.1 * sin)
2324		}
2325		
2326		pub fn rotate_z_local(self, angle: f32) -> Self {
2327			let sin = (angle * 0.5f32).sin();
2328			let cos = (angle * 0.5f32).cos();
2329			Self(self.0 * cos - self.1 * sin,
2330				 self.1 * cos + self.0 * sin,
2331				 self.2 * cos + self.3 * sin,
2332				 self.3 * cos - self.2 * sin)
2333		}
2334		
2335		pub fn rotate_x(self, angle: f32) -> Self {
2336			let sin = (angle * 0.5f32).sin();
2337			let cos = (angle * 0.5f32).cos();
2338			Self(self.3 * sin  + self.0 * cos,
2339				 self.1 * cos  + self.2 * sin,
2340				 self.2 * cos  - self.1 * sin,
2341				 self.3 * cos  - self.0 * sin)
2342		}
2343		
2344		pub fn rotate_y(self, angle: f32) -> Self {
2345			let sin = (angle * 0.5f32).sin();
2346			let cos = (angle * 0.5f32).cos();
2347			Self(self.0 * cos  - self.2 * sin,
2348				 self.3 * sin  + self.1 * cos,
2349				 self.0 * sin  + self.2 * cos,
2350				 self.3 * cos  - self.1 * sin)
2351		}
2352		
2353		pub fn rotate_z(self, angle: f32) -> Self {
2354			let sin = (angle * 0.5f32).sin();
2355			let cos = (angle * 0.5f32).cos();
2356			Self(self.0 * cos  + self.1 * sin,
2357				 self.1 * cos  - self.0 * sin,
2358				 self.3 * sin  + self.2 * cos,
2359				 self.3 * cos  - self.2 * sin)
2360		}
2361		
2362		pub fn rotate_axis(self, axis: Vec3<f32>, angle: f32) -> Self {
2363			let sin = (angle * 0.5f32).sin();
2364			let inv_len = 1f32 / axis.len();
2365			let rx = axis.0 * inv_len * sin;
2366			let ry = axis.1 * inv_len * sin;
2367			let rz = axis.2 * inv_len * sin;
2368			let rw = (angle * 0.5f32).cos();
2369			Self(self.3 * rx + self.0 * rw + self.1 * rz - self.2 * ry,
2370				 self.3 * ry + self.0 * rz + self.1 * rw - self.2 * rx,
2371				 self.3 * rz + self.0 * ry + self.1 * rx - self.2 * rw,
2372				 self.3 * rw + self.0 * rx + self.1 * ry - self.2 * rz)
2373		}
2374	}
2375}
2376
2377pub mod quat64 {
2378	use super::*;
2379	
2380	pub type Quat64 = Vec4<f64>;
2381	
2382	impl Quat64 {
2383		pub fn from_rotation_x(angle: f64) -> Self {
2384			Self((angle * 0.5f64).sin(), 0f64, 0f64, (angle * 0.5f64).cos())
2385		}
2386		
2387		pub fn from_rotation_y(angle: f64) -> Self {
2388			Self(0f64, (angle * 0.5f64).sin(), 0f64, (angle * 0.5f64).cos())
2389		}
2390		
2391		pub fn from_rotation_z(angle: f64) -> Self {
2392			Self(0f64, 0f64, (angle * 0.5f64).sin(), (angle * 0.5f64).cos())
2393		}
2394		
2395		pub fn from_axis_angle(axis: Vec3<f64>, angle: f64) -> Self {
2396			let sin = (angle * 0.5f64).sin();
2397			let inv_len = 1f64 / axis.len();
2398			Self(axis.0 * inv_len * sin,
2399				 axis.1 * inv_len * sin,
2400				 axis.2 * inv_len * sin,
2401				 (angle * 0.5f64).cos())
2402		}
2403		
2404		pub fn to_axis_angle(self) -> (Vec3<f64>, f64) {
2405			let acos = self.3.acos();
2406			let inv_sqrt = 1f64 / (1f64 - self.3 * self.3).sqrt();
2407			(Vec3(self.0 * inv_sqrt, self.1 * inv_sqrt, self.3 * inv_sqrt), acos + acos)
2408		}
2409		
2410		pub fn rotate_x_local(self, angle: f64) -> Self {
2411			let sin = (angle * 0.5f64).sin();
2412			let cos = (angle * 0.5f64).cos();
2413			Self(self.0 * cos + self.3 * sin,
2414				 self.1 * cos + self.2 * sin,
2415				 self.2 * cos - self.1 * sin,
2416				 self.3 * cos - self.0 * sin)
2417		}
2418		
2419		pub fn rotate_y_local(self, angle: f64) -> Self {
2420			let sin = (angle * 0.5f64).sin();
2421			let cos = (angle * 0.5f64).cos();
2422			Self(self.0 * cos + self.2 * sin,
2423				 self.1 * cos + self.3 * sin,
2424				 self.2 * cos - self.0 * sin,
2425				 self.3 * cos - self.1 * sin)
2426		}
2427		
2428		pub fn rotate_z_local(self, angle: f64) -> Self {
2429			let sin = (angle * 0.5f64).sin();
2430			let cos = (angle * 0.5f64).cos();
2431			Self(self.0 * cos + self.1 * sin,
2432				 self.1 * cos + self.0 * sin,
2433				 self.2 * cos - self.3 * sin,
2434				 self.3 * cos - self.2 * sin)
2435		}
2436		
2437		pub fn rotate_x(self, angle: f64) -> Self {
2438			let sin = (angle * 0.5f64).sin();
2439			let cos = (angle * 0.5f64).cos();
2440			Self(self.3 * sin  + self.0 * cos,
2441				 self.1 * cos  + self.2 * sin,
2442				 self.2 * cos  - self.1 * sin,
2443				 self.3 * cos  - self.0 * sin)
2444		}
2445		
2446		pub fn rotate_y(self, angle: f64) -> Self {
2447			let sin = (angle * 0.5f64).sin();
2448			let cos = (angle * 0.5f64).cos();
2449			Self(self.0 * cos  - self.2 * sin,
2450				 self.3 * sin  + self.1 * cos,
2451				 self.0 * sin  + self.2 * cos,
2452				 self.3 * cos  - self.1 * sin)
2453		}
2454		
2455		pub fn rotate_z(self, angle: f64) -> Self {
2456			let sin = (angle * 0.5f64).sin();
2457			let cos = (angle * 0.5f64).cos();
2458			Self(self.0 * cos  + self.1 * sin,
2459				 self.1 * cos  + self.0 * sin,
2460				 self.3 * sin  - self.2 * cos,
2461				 self.3 * cos  - self.2 * sin)
2462		}
2463		
2464		pub fn rotate_axis(self, axis: Vec3<f64>, angle: f64) -> Self {
2465			let sin = (angle * 0.5f64).sin();
2466			let inv_len = 1f64 / axis.len();
2467			let rx = axis.0 * inv_len * sin;
2468			let ry = axis.1 * inv_len * sin;
2469			let rz = axis.2 * inv_len * sin;
2470			let rw = (angle * 0.5f64).cos();
2471			Self(self.3 * rx + self.0 * rw + self.1 * rz - self.2 * ry,
2472				 self.3 * ry + self.0 * rz + self.1 * rw - self.2 * rx,
2473				 self.3 * rz + self.0 * ry + self.1 * rx - self.2 * rw,
2474				 self.3 * rw + self.0 * rx + self.1 * ry - self.2 * rz)
2475		}
2476	}
2477}
2478
2479pub mod mat2 {
2480	use super::*;
2481	
2482	#[derive(Copy, Clone, Debug, PartialOrd, PartialEq)]
2483	pub struct Mat2<T>(pub Vec2<T>, pub Vec2<T>);
2484	
2485	impl Default for Mat2<f32> {
2486		fn default() -> Self {
2487			Self(Vec2(1f32, 0f32),
2488				 Vec2(0f32, 1f32))
2489		}
2490	}
2491	
2492	impl Default for Mat2<f64> {
2493		fn default() -> Self {
2494			Self(Vec2(1f64, 0f64),
2495				 Vec2(0f64, 1f64))
2496		}
2497	}
2498	
2499	impl<T: Copy> From<[T; 4]> for Mat2<T> {
2500		fn from(v: [T; 4]) -> Self {
2501			Self(Vec2(v[0], v[1]),
2502				 Vec2(v[2], v[3]))
2503		}
2504	}
2505	
2506	impl<T: Copy> From<[[T; 2]; 2]> for Mat2<T> {
2507		fn from(v: [[T; 2]; 2]) -> Self {
2508			Self(Vec2::from(v[0]),
2509				 Vec2::from(v[1]))
2510		}
2511	}
2512	
2513	impl<T> Index<usize> for Mat2<T> {
2514		type Output = Vec2<T>;
2515		
2516		fn index(&self, index: usize) -> &Self::Output {
2517			match index {
2518				0 => &self.0,
2519				1 => &self.1,
2520				_ => panic!()
2521			}
2522		}
2523	}
2524	
2525	impl<T> IndexMut<usize> for Mat2<T> {
2526		fn index_mut(&mut self, index: usize) -> &mut Self::Output {
2527			match index {
2528				0 => &mut self.0,
2529				1 => &mut self.1,
2530				_ => panic!()
2531			}
2532		}
2533	}
2534}
2535
2536pub mod mat3 {
2537	use super::*;
2538	
2539	#[derive(Copy, Clone, Debug, PartialOrd, PartialEq)]
2540	pub struct Mat3<T>(pub Vec3<T>, pub Vec3<T>, pub Vec3<T>);
2541	
2542	impl Default for Mat3<f32> {
2543		fn default() -> Self {
2544			Self(Vec3(1f32, 0f32, 0f32),
2545				 Vec3(0f32, 1f32, 0f32),
2546				 Vec3(0f32, 0f32, 1f32))
2547		}
2548	}
2549	
2550	impl Default for Mat3<f64> {
2551		fn default() -> Self {
2552			Self(Vec3(1f64, 0f64, 0f64),
2553				 Vec3(0f64, 1f64, 0f64),
2554				 Vec3(0f64, 0f64, 1f64))
2555		}
2556	}
2557	
2558	impl<T: Copy> From<[T; 9]> for Mat3<T> {
2559		fn from(v: [T; 9]) -> Self {
2560			Self(Vec3(v[0], v[1], v[2]),
2561				 Vec3(v[3], v[4], v[5]),
2562				 Vec3(v[6], v[7], v[8]))
2563		}
2564	}
2565	
2566	impl<T: Copy> From<[[T; 3]; 3]> for Mat3<T> {
2567		fn from(v: [[T; 3]; 3]) -> Self {
2568			Self(Vec3::from(v[0]),
2569				 Vec3::from(v[1]),
2570				 Vec3::from(v[2]))
2571		}
2572	}
2573	
2574	impl<T> Index<usize> for Mat3<T> {
2575		type Output = Vec3<T>;
2576		
2577		fn index(&self, index: usize) -> &Self::Output {
2578			match index {
2579				0 => &self.0,
2580				1 => &self.1,
2581				2 => &self.2,
2582				_ => panic!()
2583			}
2584		}
2585	}
2586	
2587	impl<T> IndexMut<usize> for Mat3<T> {
2588		fn index_mut(&mut self, index: usize) -> &mut Self::Output {
2589			match index {
2590				0 => &mut self.0,
2591				1 => &mut self.1,
2592				2 => &mut self.2,
2593				_ => panic!()
2594			}
2595		}
2596	}
2597}
2598
2599pub mod mat4 {
2600	use super::*;
2601	
2602	#[derive(Copy, Clone, Debug, PartialOrd, PartialEq)]
2603	pub struct Mat4<T>(pub Vec4<T>, pub Vec4<T>, pub Vec4<T>, pub Vec4<T>);
2604	
2605	impl Default for Mat4<f32> {
2606		fn default() -> Self {
2607			Self(Vec4(1f32, 0f32, 0f32, 0f32),
2608				 Vec4(0f32, 1f32, 0f32, 0f32),
2609				 Vec4(0f32, 0f32, 1f32, 0f32),
2610				 Vec4(0f32, 0f32, 0f32, 1f32))
2611		}
2612	}
2613	
2614	impl Default for Mat4<f64> {
2615		fn default() -> Self {
2616			Self(Vec4(1f64, 0f64, 0f64, 0f64),
2617				 Vec4(0f64, 1f64, 0f64, 0f64),
2618				 Vec4(0f64, 0f64, 1f64, 0f64),
2619				 Vec4(0f64, 0f64, 0f64, 1f64))
2620		}
2621	}
2622	
2623	impl<T: Copy> From<[T; 16]> for Mat4<T> {
2624		fn from(v: [T; 16]) -> Self {
2625			Self(Vec4(v[0], v[1], v[2], v[3]),
2626				 Vec4(v[4], v[5], v[6], v[7]),
2627				 Vec4(v[8], v[9], v[10], v[11]),
2628				 Vec4(v[12], v[13], v[14], v[15]))
2629		}
2630	}
2631	
2632	impl<T: Copy> From<[[T; 4]; 4]> for Mat4<T> {
2633		fn from(v: [[T; 4]; 4]) -> Self {
2634			Self(Vec4::from(v[0]),
2635				 Vec4::from(v[1]),
2636				 Vec4::from(v[2]),
2637				 Vec4::from(v[3]))
2638		}
2639	}
2640	
2641	impl<T> Index<usize> for Mat4<T> {
2642		type Output = Vec4<T>;
2643		
2644		fn index(&self, index: usize) -> &Self::Output {
2645			match index {
2646				0 => &self.0,
2647				1 => &self.1,
2648				2 => &self.2,
2649				3 => &self.3,
2650				_ => panic!()
2651			}
2652		}
2653	}
2654	
2655	impl<T> IndexMut<usize> for Mat4<T> {
2656		fn index_mut(&mut self, index: usize) -> &mut Self::Output {
2657			match index {
2658				0 => &mut self.0,
2659				1 => &mut self.1,
2660				2 => &mut self.2,
2661				3 => &mut self.3,
2662				_ => panic!()
2663			}
2664		}
2665	}
2666	
2667	impl Mat4<f32> {
2668		pub fn from_translation(trans: Vec3<f32>) -> Self {
2669			let mut mat = Self::default();
2670			(mat.3).0 = trans.0;
2671			(mat.3).1 = trans.1;
2672			(mat.3).2 = trans.2;
2673			mat
2674		}
2675		
2676		pub fn from_rotation(quat: Quat32) -> Self {
2677			let mut mat = Self::default();
2678			let w2 = quat.3 * quat.3;
2679			let x2 = quat.0 * quat.0;
2680			let y2 = quat.1 * quat.1;
2681			let z2 = quat.2 * quat.2;
2682			let zw = quat.2 * quat.3;
2683			let xy = quat.0 * quat.1;
2684			let xz = quat.0 * quat.2;
2685			let yw = quat.1 * quat.3;
2686			let yz = quat.1 * quat.2;
2687			let xw = quat.0 * quat.3;
2688			(mat.0).0 = w2 + x2 - z2 - y2;
2689			(mat.0).1 = xy + zw + zw + xy;
2690			(mat.0).2 = xz - yw + xz - yw;
2691			(mat.1).0 = -zw + xy - zw + xy;
2692			(mat.1).1 = y2 - z2 + w2 - x2;
2693			(mat.1).2 = yz + yz + xw + xw;
2694			(mat.2).0 = yw + xz + xz + yw;
2695			(mat.2).1 = yz + yz - xw - xw;
2696			(mat.2).2 = z2 - y2 - x2 + w2;
2697			mat
2698		}
2699		
2700		pub fn from_rotation_axis_angle(axis: Vec3<f32>, angle: f32) -> Self {
2701			let axis = axis.normalize(1f32);
2702			let sin = angle.sin();
2703			let cos = angle.cos();
2704			let c = 1f32 - cos;
2705			let xyc = axis.0 * axis.1 * c;
2706			let xzc = axis.0 * axis.2 * c;
2707			let yzc = axis.1 * axis.2 * c;
2708			let xs = axis.0 * sin;
2709			let ys = axis.1 * sin;
2710			let zs = axis.2 * sin;
2711			Self(Vec4(cos + axis.0 * axis.0 * c, xyc + zs, xzc - ys, 0f32),
2712				 Vec4(xyc - zs, cos + axis.1 * axis.1 * c, yzc + xs, 0f32),
2713				 Vec4(xzc + ys, yzc - xs, cos + axis.2 * axis.2 * c, 0f32),
2714				 Vec4(0f32, 0f32, 0f32, 1f32))
2715		}
2716		
2717		pub fn from_scale(scale: Vec3<f32>) -> Self {
2718			let mut mat = Self::default();
2719			(mat.0).0 = scale.0;
2720			(mat.1).1 = scale.1;
2721			(mat.2).2 = scale.2;
2722			mat
2723		}
2724		
2725		pub fn from_transform(t: Vec3<f32>, q: Quat32, s: Vec3<f32>) -> Self {
2726			let mut mat = Self::default();
2727			let dqx = q.0 + q.0;
2728			let dqy = q.1 + q.1;
2729			let dqz = q.2 + q.2;
2730			let q00 = dqx * q.0;
2731			let q11 = dqy * q.1;
2732			let q22 = dqz * q.2;
2733			let q01 = dqx * q.1;
2734			let q02 = dqx * q.2;
2735			let q03 = dqx * q.3;
2736			let q12 = dqy * q.2;
2737			let q13 = dqy * q.3;
2738			let q23 = dqz * q.3;
2739			(mat.0).0 = s.0 - (q11 + q22) * s.0;
2740			(mat.0).1 = (q01 + q23) * s.0;
2741			(mat.0).2 = (q02 - q13) * s.0;
2742			(mat.0).3 = 0f32;
2743			(mat.1).0 = (q01 - q23) * s.1;
2744			(mat.1).1 = s.1 - (q22 + q00) * s.1;
2745			(mat.1).2 = (q12 + q03) * s.1;
2746			(mat.1).3 = 0f32;
2747			(mat.2).0 = (q02 + q13) * s.2;
2748			(mat.2).1 = (q12 - q03) * s.2;
2749			(mat.2).2 = s.2 - (q11 + q00) * s.2;
2750			(mat.2).3 = 0f32;
2751			(mat.3).0 = t.0;
2752			(mat.3).1 = t.1;
2753			(mat.3).2 = t.2;
2754			(mat.3).3 = 1f32;
2755			mat
2756		}
2757		
2758		pub fn from_perspective(y_fov: f32, aspect: f32, z_near: f32, z_far: f32, z_zero_to_one: bool) -> Self {
2759			let mut mat = Self::default();
2760			let h = (y_fov * 0.5f32).tan();
2761			(mat.0).0 = 1f32 / (h * aspect);
2762			(mat.1).1 = 1f32 / h;
2763			if z_far > 0f32 && (z_far == ::std::f32::INFINITY || z_far == ::std::f32::NEG_INFINITY) {
2764				(mat.2).2 = 1E-6f32 - 1f32;
2765				(mat.3).2 = (1E-6f32 - if z_zero_to_one { 1f32 } else { 2f32 }) * z_near;
2766			} else if z_near > 0f32 && (z_near == ::std::f32::INFINITY || z_near == ::std::f32::NEG_INFINITY) {
2767				(mat.2).2 = (if z_zero_to_one { 0f32 } else { 1f32 }) - 1E-6f32;
2768				(mat.3).2 = ((if z_zero_to_one { 1f32 } else { 2f32 }) - 1E-6f32) * z_far;
2769			} else {
2770				(mat.2).2 = (if z_zero_to_one { z_far } else { z_far + z_near }) / (z_near - z_far);
2771				(mat.3).2 = (if z_zero_to_one { z_far } else { z_far + z_far }) * z_near / (z_near - z_far);
2772			}
2773			(mat.2).3 = -1f32;
2774			(mat.3).3 = 0f32;
2775			mat
2776		}
2777		
2778		pub fn from_ortho(left: f32, right: f32, bottom: f32, top: f32, z_near: f32, z_far: f32, z_zero_to_one: bool) -> Self {
2779			let mut mat = Self::default();
2780			(mat.0).0 = 2f32 / (right - left);
2781			(mat.1).1 = 2f32 / (top - bottom);
2782			(mat.2).2 = (if z_zero_to_one { 1f32 } else { 2f32 }) / (z_near - z_far);
2783			(mat.3).0 = (right + left) / (left - right);
2784			(mat.3).1 = (top + bottom) / (bottom - top);
2785			(mat.3).2 = (if z_zero_to_one { z_near } else { z_far + z_near }) / (z_near - z_far);
2786			mat
2787		}
2788		
2789		pub fn from_ortho_symmetric(width: f32, height: f32, z_near: f32, z_far: f32, z_zero_to_one: bool) -> Self {
2790			let mut mat = Self::default();
2791			(mat.0).0 = 2f32 / width;
2792			(mat.1).1 = 2f32 / height;
2793			(mat.2).2 = (if z_zero_to_one { 1f32 } else { 2f32 }) / (z_near - z_far);
2794			(mat.3).2 = (if z_zero_to_one { z_near } else { z_far + z_near }) / (z_near - z_far);
2795			mat
2796		}
2797		
2798		pub fn rotate(self, axis: Vec3<f32>, angle: f32) -> Self {
2799			let sin = angle.sin();
2800			let cos = angle.cos();
2801			let c = 1f32 - cos;
2802			let m00 = axis.0 * axis.0 * c + cos;
2803			let m01 = axis.0 * axis.1 * c + axis.2 * sin;
2804			let m02 = axis.0 * axis.2 * c - axis.1 * sin;
2805			let m10 = axis.0 * axis.1 * c - axis.2 * sin;
2806			let m11 = axis.1 * axis.1 * c + cos;
2807			let m12 = axis.1 * axis.2 * c + axis.0 * sin;
2808			let m20 = axis.0 * axis.2 * c + axis.1 * sin;
2809			let m21 = axis.1 * axis.2 * c - axis.0 * sin;
2810			let m22 = axis.2 * axis.2 * c + cos;
2811			
2812			Self(Vec4((self.0).0 * m00 + (self.1).0 * m01 + (self.2).0 * m02,
2813					  (self.0).1 * m00 + (self.1).1 * m01 + (self.2).1 * m02,
2814					  (self.0).2 * m00 + (self.1).2 * m01 + (self.2).2 * m02,
2815					  (self.0).3 * m00 + (self.1).3 * m01 + (self.2).3 * m02),
2816				 Vec4((self.0).0 * m10 + (self.1).0 * m11 + (self.2).0 * m12,
2817					  (self.0).1 * m10 + (self.1).1 * m11 + (self.2).1 * m12,
2818					  (self.0).2 * m10 + (self.1).2 * m11 + (self.2).2 * m12,
2819					  (self.0).3 * m10 + (self.1).3 * m11 + (self.2).3 * m12),
2820				 Vec4((self.0).0 * m20 + (self.1).0 * m21 + (self.2).0 * m22,
2821					  (self.0).1 * m20 + (self.1).1 * m21 + (self.2).1 * m22,
2822					  (self.0).2 * m20 + (self.1).2 * m21 + (self.2).2 * m22,
2823					  (self.0).3 * m20 + (self.1).3 * m21 + (self.2).3 * m22),
2824				 self.3)
2825		}
2826		
2827		pub fn rotate_local(self, axis: Vec3<f32>, angle: f32) -> Self {
2828			let sin = angle.sin();
2829			let cos = angle.cos();
2830			let c = 1f32 - cos;
2831			let m00 = axis.0 * axis.0 * c + cos;
2832			let m01 = axis.0 * axis.1 * c + axis.2 * sin;
2833			let m02 = axis.0 * axis.2 * c - axis.1 * sin;
2834			let m10 = axis.0 * axis.1 * c - axis.2 * sin;
2835			let m11 = axis.1 * axis.1 * c + cos;
2836			let m12 = axis.1 * axis.2 * c + axis.0 * sin;
2837			let m20 = axis.0 * axis.2 * c + axis.1 * sin;
2838			let m21 = axis.1 * axis.2 * c - axis.0 * sin;
2839			let m22 = axis.2 * axis.2 * c + cos;
2840			
2841			Self(Vec4(m00 * (self.0).0 + m10 * (self.0).1 + m20 * (self.0).2,
2842					  m01 * (self.0).0 + m11 * (self.0).1 + m21 * (self.0).2,
2843					  m02 * (self.0).0 + m12 * (self.0).1 + m22 * (self.0).2,
2844					  (self.0).3),
2845				 Vec4(m00 * (self.1).0 + m10 * (self.1).1 + m20 * (self.1).2,
2846					  m01 * (self.1).0 + m11 * (self.1).1 + m21 * (self.1).2,
2847					  m02 * (self.1).0 + m12 * (self.1).1 + m22 * (self.1).2,
2848					  (self.1).3),
2849				 Vec4(m00 * (self.2).0 + m10 * (self.2).1 + m20 * (self.2).2,
2850					  m01 * (self.2).0 + m11 * (self.2).1 + m21 * (self.2).2,
2851					  m02 * (self.2).0 + m12 * (self.2).1 + m22 * (self.2).2,
2852					  (self.2).3),
2853				 Vec4(m00 * (self.3).0 + m10 * (self.3).1 + m20 * (self.3).2,
2854					  m01 * (self.3).0 + m11 * (self.3).1 + m21 * (self.3).2,
2855					  m02 * (self.3).0 + m12 * (self.3).1 + m22 * (self.3).2,
2856					  (self.3).3))
2857		}
2858		
2859		pub fn get_scale(&self) -> Vec3<f32> {
2860			Vec3(((self.0).0 * (self.0).0 + (self.0).1 * (self.0).1 + (self.0).2 * (self.0).2).sqrt(),
2861				 ((self.1).0 * (self.1).0 + (self.1).1 * (self.1).1 + (self.1).2 * (self.1).2).sqrt(),
2862				 ((self.2).0 * (self.2).0 + (self.2).1 * (self.2).1 + (self.2).2 * (self.2).2).sqrt())
2863		}
2864		
2865		pub fn get_rotation(&self) -> Quat32 {
2866			let tr = (self.0).0 + (self.1).1 + (self.2).2;
2867			if tr >= 0f32 {
2868				let t = (tr + 1f32).sqrt();
2869				let w = 0.5f32 * t;
2870				let t = 0.5f32 / t;
2871				Vec4(((self.1).2 - (self.2).1) * t,
2872					 ((self.2).0 - (self.0).2) * t,
2873					 ((self.0).1 - (self.1).0) * t,
2874					 w)
2875			} else if (self.0).0 >= (self.1).1 && (self.0).0 >= (self.2).2 {
2876				let t = ((self.0).0 - ((self.1).1 + (self.2).2) + 1f32).sqrt();
2877				let x = 0.5f32 * t;
2878				let t = 0.5f32 / t;
2879				Vec4(x,
2880					 ((self.1).0 + (self.0).1) * t,
2881					 ((self.0).2 + (self.2).0) * t,
2882					 ((self.1).2 - (self.2).1) * t)
2883			} else if (self.1).1 > (self.2).2 {
2884				let t = ((self.1).1 - ((self.2).2 + (self.0).0) + 1f32).sqrt();
2885				let y = 0.5f32 * t;
2886				let t = 0.5f32 / t;
2887				Vec4(((self.1).0 + (self.0).1) * t,
2888					 y,
2889					 ((self.2).1 + (self.1).2) * t,
2890					 ((self.2).0 - (self.0).2) * t)
2891			} else {
2892				let t = ((self.2).2 - ((self.0).0 + (self.1).1) + 1f32).sqrt();
2893				let z = 0.5f32 * t;
2894				let t = 0.5f32 / t;
2895				Vec4(((self.0).2 + (self.2).0) * t,
2896					 ((self.2).1 + (self.1).2) * t,
2897					 z,
2898					 ((self.0).1 - (self.1).0) * t)
2899			}
2900		}
2901		
2902		pub fn get_rotation_axis_angle(self) -> (Vec3<f32>, f32) {
2903			if ((self.1).0 - (self.0).1).abs() < 1E-4f32
2904				&& ((self.2).0 - (self.0).2).abs() < 1E-4f32
2905				&& ((self.2).1 - (self.1).2).abs() < 1E-4f32 {
2906				let xx = ((self.0).0 + 1f32) / 2f32;
2907				let yy = ((self.1).1 + 1f32) / 2f32;
2908				let zz = ((self.2).2 + 1f32) / 2f32;
2909				let xy = ((self.1).0 + (self.0).1) / 4f32;
2910				let xz = ((self.2).0 + (self.0).2) / 4f32;
2911				let yz = ((self.2).1 + (self.1).2) / 4f32;
2912				(if xx > yy && xx > zz {
2913					let x = xx.sqrt();
2914					Vec3(x, xy / x, xz / x)
2915				} else if yy > zz {
2916					let y = yy.sqrt();
2917					Vec3(xy / y, y, yz / y)
2918				} else {
2919					let z = zz.sqrt();
2920					Vec3(xz / z, yz / z, z)
2921				}, ::std::f32::consts::PI)
2922			} else {
2923				let s = (((self.1).2 - (self.2).1) * ((self.1).2 - (self.2).1)
2924					+ ((self.2).0 - (self.0).2) * ((self.2).0 - (self.0).2)
2925					+ ((self.0).1 - (self.1).0) * ((self.0).1 - (self.1).0)).sqrt();
2926				(Vec3(((self.1).2 - (self.2).1) / s,
2927					  ((self.1).2 - (self.2).1) / s,
2928					  ((self.0).1 - (self.1).0) / s),
2929				 (((self.0).0 + (self.1).1 + (self.2).2 - 1f32) / 2f32).acos())
2930			}
2931		}
2932		
2933		pub fn normalize3x3(self) -> Self {
2934			let inv_x_len = 1f32 / ((self.0).0 * (self.0).0 + (self.0).1 * (self.0).1 + (self.0).2 * (self.0).2).sqrt();
2935			let inv_y_len = 1f32 / ((self.1).0 * (self.1).0 + (self.1).1 * (self.1).1 + (self.1).2 * (self.1).2).sqrt();
2936			let inv_z_len = 1f32 / ((self.2).0 * (self.2).0 + (self.2).1 * (self.2).1 + (self.2).2 * (self.2).2).sqrt();
2937			let mut mat = Self(self.0 * inv_x_len,
2938							   self.1 * inv_y_len,
2939							   self.2 * inv_z_len,
2940							   self.3);
2941			(mat.0).3 = (self.0).3;
2942			(mat.1).3 = (self.1).3;
2943			(mat.2).3 = (self.2).3;
2944			mat
2945		}
2946	}
2947	
2948	impl Mat4<f64> {
2949		pub fn from_translation(trans: Vec3<f64>) -> Self {
2950			let mut mat = Self::default();
2951			(mat.3).0 = trans.0;
2952			(mat.3).1 = trans.1;
2953			(mat.3).2 = trans.2;
2954			mat
2955		}
2956		
2957		pub fn from_rotation(quat: Quat64) -> Self {
2958			let mut mat = Self::default();
2959			let w2 = quat.3 * quat.3;
2960			let x2 = quat.0 * quat.0;
2961			let y2 = quat.1 * quat.1;
2962			let z2 = quat.2 * quat.2;
2963			let zw = quat.2 * quat.3;
2964			let xy = quat.0 * quat.1;
2965			let xz = quat.0 * quat.2;
2966			let yw = quat.1 * quat.3;
2967			let yz = quat.1 * quat.2;
2968			let xw = quat.0 * quat.3;
2969			(mat.0).0 = w2 + x2 - z2 - y2;
2970			(mat.0).1 = xy + zw + zw + xy;
2971			(mat.0).2 = xz - yw + xz - yw;
2972			(mat.1).0 = -zw + xy - zw + xy;
2973			(mat.1).1 = y2 - z2 + w2 - x2;
2974			(mat.1).2 = yz + yz + xw + xw;
2975			(mat.2).0 = yw + xz + xz + yw;
2976			(mat.2).1 = yz + yz - xw - xw;
2977			(mat.2).2 = z2 - y2 - x2 + w2;
2978			mat
2979		}
2980		
2981		pub fn from_rotation_axis_angle(axis: Vec3<f64>, angle: f64) -> Self {
2982			let axis = axis.normalize(1f64);
2983			let sin = angle.sin();
2984			let cos = angle.cos();
2985			let c = 1f64 - cos;
2986			let xyc = axis.0 * axis.1 * c;
2987			let xzc = axis.0 * axis.2 * c;
2988			let yzc = axis.1 * axis.2 * c;
2989			let xs = axis.0 * sin;
2990			let ys = axis.1 * sin;
2991			let zs = axis.2 * sin;
2992			Self(Vec4(cos + axis.0 * axis.0 * c, xyc + zs, xzc - ys, 0f64),
2993				 Vec4(xyc - zs, cos + axis.1 * axis.1 * c, yzc + xs, 0f64),
2994				 Vec4(xzc + ys, yzc - xs, cos + axis.2 * axis.2 * c, 0f64),
2995				 Vec4(0f64, 0f64, 0f64, 1f64))
2996		}
2997		
2998		pub fn from_scale(scale: Vec3<f64>) -> Self {
2999			let mut mat = Self::default();
3000			(mat.0).0 = scale.0;
3001			(mat.1).1 = scale.1;
3002			(mat.2).2 = scale.2;
3003			mat
3004		}
3005		
3006		pub fn from_transform(t: Vec3<f64>, q: Quat64, s: Vec3<f64>) -> Self {
3007			let mut mat = Self::default();
3008			let dqx = q.0 + q.0;
3009			let dqy = q.1 + q.1;
3010			let dqz = q.2 + q.2;
3011			let q00 = dqx * q.0;
3012			let q11 = dqy * q.1;
3013			let q22 = dqz * q.2;
3014			let q01 = dqx * q.1;
3015			let q02 = dqx * q.2;
3016			let q03 = dqx * q.3;
3017			let q12 = dqy * q.2;
3018			let q13 = dqy * q.3;
3019			let q23 = dqz * q.3;
3020			(mat.0).0 = s.0 - (q11 + q22) * s.0;
3021			(mat.0).1 = (q01 + q23) * s.0;
3022			(mat.0).2 = (q02 - q13) * s.0;
3023			(mat.0).3 = 0f64;
3024			(mat.1).0 = (q01 - q23) * s.1;
3025			(mat.1).1 = s.1 - (q22 + q00) * s.1;
3026			(mat.1).2 = (q12 + q03) * s.1;
3027			(mat.1).3 = 0f64;
3028			(mat.2).0 = (q02 + q13) * s.2;
3029			(mat.2).1 = (q12 - q03) * s.2;
3030			(mat.2).2 = s.2 - (q11 + q00) * s.2;
3031			(mat.2).3 = 0f64;
3032			(mat.3).0 = t.0;
3033			(mat.3).1 = t.1;
3034			(mat.3).2 = t.2;
3035			(mat.3).3 = 1f64;
3036			mat
3037		}
3038		
3039		pub fn from_perspective(y_fov: f64, aspect: f64, z_near: f64, z_far: f64, z_zero_to_one: bool) -> Self {
3040			let mut mat = Self::default();
3041			let h = (y_fov * 0.5f64).tan();
3042			(mat.0).0 = 1f64 / (h * aspect);
3043			(mat.1).1 = 1f64 / h;
3044			if z_far > 0f64 && (z_far == ::std::f64::INFINITY || z_far == ::std::f64::NEG_INFINITY) {
3045				(mat.2).2 = 1E-6f64 - 1f64;
3046				(mat.3).2 = (1E-6f64 - if z_zero_to_one { 1f64 } else { 2f64 }) * z_near;
3047			} else if z_near > 0f64 && (z_near == ::std::f64::INFINITY || z_near == ::std::f64::NEG_INFINITY) {
3048				(mat.2).2 = (if z_zero_to_one { 0f64 } else { 1f64 }) - 1E-6f64;
3049				(mat.3).2 = ((if z_zero_to_one { 1f64 } else { 2f64 }) - 1E-6f64) * z_far;
3050			} else {
3051				(mat.2).2 = (if z_zero_to_one { z_far } else { z_far + z_near }) / (z_near - z_far);
3052				(mat.3).2 = (if z_zero_to_one { z_far } else { z_far + z_far }) * z_near / (z_near - z_far);
3053			}
3054			(mat.2).3 = -1f64;
3055			(mat.3).3 = 0f64;
3056			mat
3057		}
3058		
3059		pub fn from_ortho(left: f64, right: f64, bottom: f64, top: f64, z_near: f64, z_far: f64, z_zero_to_one: bool) -> Self {
3060			let mut mat = Self::default();
3061			(mat.0).0 = 2f64 / (right - left);
3062			(mat.1).1 = 2f64 / (top - bottom);
3063			(mat.2).2 = (if z_zero_to_one { 1f64 } else { 2f64 }) / (z_near - z_far);
3064			(mat.3).0 = (right + left) / (left - right);
3065			(mat.3).1 = (top + bottom) / (bottom - top);
3066			(mat.3).2 = (if z_zero_to_one { z_near } else { z_far + z_near }) / (z_near - z_far);
3067			mat
3068		}
3069		
3070		pub fn from_ortho_symmetric(width: f64, height: f64, z_near: f64, z_far: f64, z_zero_to_one: bool) -> Self {
3071			let mut mat = Self::default();
3072			(mat.0).0 = 2f64 / width;
3073			(mat.1).1 = 2f64 / height;
3074			(mat.2).2 = (if z_zero_to_one { 1f64 } else { 2f64 }) / (z_near - z_far);
3075			(mat.3).2 = (if z_zero_to_one { z_near } else { z_far + z_near }) / (z_near - z_far);
3076			mat
3077		}
3078		
3079		pub fn rotate(self, axis: Vec3<f64>, angle: f64) -> Self {
3080			let sin = angle.sin();
3081			let cos = angle.cos();
3082			let c = 1f64 - cos;
3083			let m00 = axis.0 * axis.0 * c + cos;
3084			let m01 = axis.0 * axis.1 * c + axis.2 * sin;
3085			let m02 = axis.0 * axis.2 * c - axis.1 * sin;
3086			let m10 = axis.0 * axis.1 * c - axis.2 * sin;
3087			let m11 = axis.1 * axis.1 * c + cos;
3088			let m12 = axis.1 * axis.2 * c + axis.0 * sin;
3089			let m20 = axis.0 * axis.2 * c + axis.1 * sin;
3090			let m21 = axis.1 * axis.2 * c - axis.0 * sin;
3091			let m22 = axis.2 * axis.2 * c + cos;
3092			
3093			Self(Vec4((self.0).0 * m00 + (self.1).0 * m01 + (self.2).0 * m02,
3094					  (self.0).1 * m00 + (self.1).1 * m01 + (self.2).1 * m02,
3095					  (self.0).2 * m00 + (self.1).2 * m01 + (self.2).2 * m02,
3096					  (self.0).3 * m00 + (self.1).3 * m01 + (self.2).3 * m02),
3097				 Vec4((self.0).0 * m10 + (self.1).0 * m11 + (self.2).0 * m12,
3098					  (self.0).1 * m10 + (self.1).1 * m11 + (self.2).1 * m12,
3099					  (self.0).2 * m10 + (self.1).2 * m11 + (self.2).2 * m12,
3100					  (self.0).3 * m10 + (self.1).3 * m11 + (self.2).3 * m12),
3101				 Vec4((self.0).0 * m20 + (self.1).0 * m21 + (self.2).0 * m22,
3102					  (self.0).1 * m20 + (self.1).1 * m21 + (self.2).1 * m22,
3103					  (self.0).2 * m20 + (self.1).2 * m21 + (self.2).2 * m22,
3104					  (self.0).3 * m20 + (self.1).3 * m21 + (self.2).3 * m22),
3105				 self.3)
3106		}
3107		
3108		pub fn rotate_local(self, axis: Vec3<f64>, angle: f64) -> Self {
3109			let sin = angle.sin();
3110			let cos = angle.cos();
3111			let c = 1f64 - cos;
3112			let m00 = axis.0 * axis.0 * c + cos;
3113			let m01 = axis.0 * axis.1 * c + axis.2 * sin;
3114			let m02 = axis.0 * axis.2 * c - axis.1 * sin;
3115			let m10 = axis.0 * axis.1 * c - axis.2 * sin;
3116			let m11 = axis.1 * axis.1 * c + cos;
3117			let m12 = axis.1 * axis.2 * c + axis.0 * sin;
3118			let m20 = axis.0 * axis.2 * c + axis.1 * sin;
3119			let m21 = axis.1 * axis.2 * c - axis.0 * sin;
3120			let m22 = axis.2 * axis.2 * c + cos;
3121			
3122			Self(Vec4(m00 * (self.0).0 + m10 * (self.0).1 + m20 * (self.0).2,
3123					  m01 * (self.0).0 + m11 * (self.0).1 + m21 * (self.0).2,
3124					  m02 * (self.0).0 + m12 * (self.0).1 + m22 * (self.0).2,
3125					  (self.0).3),
3126				 Vec4(m00 * (self.1).0 + m10 * (self.1).1 + m20 * (self.1).2,
3127					  m01 * (self.1).0 + m11 * (self.1).1 + m21 * (self.1).2,
3128					  m02 * (self.1).0 + m12 * (self.1).1 + m22 * (self.1).2,
3129					  (self.1).3),
3130				 Vec4(m00 * (self.2).0 + m10 * (self.2).1 + m20 * (self.2).2,
3131					  m01 * (self.2).0 + m11 * (self.2).1 + m21 * (self.2).2,
3132					  m02 * (self.2).0 + m12 * (self.2).1 + m22 * (self.2).2,
3133					  (self.2).3),
3134				 Vec4(m00 * (self.3).0 + m10 * (self.3).1 + m20 * (self.3).2,
3135					  m01 * (self.3).0 + m11 * (self.3).1 + m21 * (self.3).2,
3136					  m02 * (self.3).0 + m12 * (self.3).1 + m22 * (self.3).2,
3137					  (self.3).3))
3138		}
3139		
3140		pub fn get_scale(&self) -> Vec3<f64> {
3141			Vec3(((self.0).0 * (self.0).0 + (self.0).1 * (self.0).1 + (self.0).2 * (self.0).2).sqrt(),
3142				 ((self.1).0 * (self.1).0 + (self.1).1 * (self.1).1 + (self.1).2 * (self.1).2).sqrt(),
3143				 ((self.2).0 * (self.2).0 + (self.2).1 * (self.2).1 + (self.2).2 * (self.2).2).sqrt())
3144		}
3145		
3146		pub fn get_rotation(&self) -> Quat64 {
3147			let tr = (self.0).0 + (self.1).1 + (self.2).2;
3148			if tr >= 0f64 {
3149				let t = (tr + 1f64).sqrt();
3150				let w = 0.5f64 * t;
3151				let t = 0.5f64 / t;
3152				Vec4(((self.1).2 - (self.2).1) * t,
3153					 ((self.2).0 - (self.0).2) * t,
3154					 ((self.0).1 - (self.1).0) * t,
3155					 w)
3156			} else if (self.0).0 >= (self.1).1 && (self.0).0 >= (self.2).2 {
3157				let t = ((self.0).0 - ((self.1).1 + (self.2).2) + 1f64).sqrt();
3158				let x = 0.5f64 * t;
3159				let t = 0.5f64 / t;
3160				Vec4(x,
3161					 ((self.1).0 + (self.0).1) * t,
3162					 ((self.0).2 + (self.2).0) * t,
3163					 ((self.1).2 - (self.2).1) * t)
3164			} else if (self.1).1 > (self.2).2 {
3165				let t = ((self.1).1 - ((self.2).2 + (self.0).0) + 1f64).sqrt();
3166				let y = 0.5f64 * t;
3167				let t = 0.5f64 / t;
3168				Vec4(((self.1).0 + (self.0).1) * t,
3169					 y,
3170					 ((self.2).1 + (self.1).2) * t,
3171					 ((self.2).0 - (self.0).2) * t)
3172			} else {
3173				let t = ((self.2).2 - ((self.0).0 + (self.1).1) + 1f64).sqrt();
3174				let z = 0.5f64 * t;
3175				let t = 0.5f64 / t;
3176				Vec4(((self.0).2 + (self.2).0) * t,
3177					 ((self.2).1 + (self.1).2) * t,
3178					 z,
3179					 ((self.0).1 - (self.1).0) * t)
3180			}
3181		}
3182		
3183		pub fn get_rotation_axis_angle(self) -> (Vec3<f64>, f64) {
3184			if ((self.1).0 - (self.0).1).abs() < 1E-4f64
3185				&& ((self.2).0 - (self.0).2).abs() < 1E-4f64
3186				&& ((self.2).1 - (self.1).2).abs() < 1E-4f64 {
3187				let xx = ((self.0).0 + 1f64) / 2f64;
3188				let yy = ((self.1).1 + 1f64) / 2f64;
3189				let zz = ((self.2).2 + 1f64) / 2f64;
3190				let xy = ((self.1).0 + (self.0).1) / 4f64;
3191				let xz = ((self.2).0 + (self.0).2) / 4f64;
3192				let yz = ((self.2).1 + (self.1).2) / 4f64;
3193				(if xx > yy && xx > zz {
3194					let x = xx.sqrt();
3195					Vec3(x, xy / x, xz / x)
3196				} else if yy > zz {
3197					let y = yy.sqrt();
3198					Vec3(xy / y, y, yz / y)
3199				} else {
3200					let z = zz.sqrt();
3201					Vec3(xz / z, yz / z, z)
3202				}, ::std::f64::consts::PI)
3203			} else {
3204				let s = (((self.1).2 - (self.2).1) * ((self.1).2 - (self.2).1)
3205					+ ((self.2).0 - (self.0).2) * ((self.2).0 - (self.0).2)
3206					+ ((self.0).1 - (self.1).0) * ((self.0).1 - (self.1).0)).sqrt();
3207				(Vec3(((self.1).2 - (self.2).1) / s,
3208					  ((self.1).2 - (self.2).1) / s,
3209					  ((self.0).1 - (self.1).0) / s),
3210				 (((self.0).0 + (self.1).1 + (self.2).2 - 1f64) / 2f64).acos())
3211			}
3212		}
3213		
3214		pub fn normalize3x3(self) -> Self {
3215			let inv_x_len = 1f64 / ((self.0).0 * (self.0).0 + (self.0).1 * (self.0).1 + (self.0).2 * (self.0).2).sqrt();
3216			let inv_y_len = 1f64 / ((self.1).0 * (self.1).0 + (self.1).1 * (self.1).1 + (self.1).2 * (self.1).2).sqrt();
3217			let inv_z_len = 1f64 / ((self.2).0 * (self.2).0 + (self.2).1 * (self.2).1 + (self.2).2 * (self.2).2).sqrt();
3218			let mut mat = Self(self.0 * inv_x_len,
3219							   self.1 * inv_y_len,
3220							   self.2 * inv_z_len,
3221							   self.3);
3222			(mat.0).3 = (self.0).3;
3223			(mat.1).3 = (self.1).3;
3224			(mat.2).3 = (self.2).3;
3225			mat
3226		}
3227	}
3228	
3229	impl<T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T> + MulAssign + DivAssign + Copy> Mat4<T> {
3230		pub fn mul_component_wise(self, rhs: Self) -> Self {
3231			Self(self.0 * rhs.0,
3232				 self.1 * rhs.1,
3233				 self.2 * rhs.2,
3234				 self.3 * rhs.3)
3235		}
3236		
3237		pub fn div_component_wise(self, rhs: Self) -> Self {
3238			Self(self.0 / rhs.0,
3239				 self.1 / rhs.1,
3240				 self.2 / rhs.2,
3241				 self.3 / rhs.3)
3242		}
3243		
3244		pub fn mul_assign_component_wise(&mut self, rhs: Self) {
3245			self.0 *= rhs.0;
3246			self.1 *= rhs.1;
3247			self.2 *= rhs.2;
3248			self.3 *= rhs.3;
3249		}
3250		
3251		pub fn div_assign_component_wise(&mut self, rhs: Self) {
3252			self.0 /= rhs.0;
3253			self.1 /= rhs.1;
3254			self.2 /= rhs.2;
3255			self.3 /= rhs.3;
3256		}
3257		
3258		pub fn det(&self) -> T {
3259			((self.0).0 * (self.1).1 - (self.0).1 * (self.1).0) * ((self.2).2 * (self.3).3 - (self.2).3 * (self.3).2)
3260				+ ((self.0).2 * (self.1).0 - (self.0).0 * (self.1).2) * ((self.2).1 * (self.3).3 - (self.2).3 * (self.3).1)
3261				+ ((self.0).0 * (self.1).3 - (self.0).3 * (self.1).0) * ((self.2).1 * (self.3).2 - (self.2).2 * (self.3).1)
3262				+ ((self.0).1 * (self.1).2 - (self.0).2 * (self.1).1) * ((self.2).0 * (self.3).3 - (self.2).3 * (self.3).0)
3263				+ ((self.0).3 * (self.1).1 - (self.0).1 * (self.1).3) * ((self.2).0 * (self.3).2 - (self.2).2 * (self.3).0)
3264				+ ((self.0).2 * (self.1).3 - (self.0).3 * (self.1).2) * ((self.2).0 * (self.3).1 - (self.2).1 * (self.3).0)
3265		}
3266		
3267		pub fn transform(&self, vec: Vec4<T>) -> Vec4<T> {
3268			Vec4((self.0).0 * vec.0 + (self.1).0 * vec.1 + (self.2).0 * vec.2 + (self.3).0 * vec.3,
3269				 (self.0).1 * vec.0 + (self.1).1 * vec.1 + (self.2).1 * vec.2 + (self.3).1 * vec.3,
3270				 (self.0).2 * vec.0 + (self.1).2 * vec.1 + (self.2).2 * vec.2 + (self.3).2 * vec.3,
3271				 (self.0).3 * vec.0 + (self.1).3 * vec.1 + (self.2).3 * vec.2 + (self.3).3 * vec.3)
3272		}
3273		
3274		pub fn transform_pos(&self, vec: Vec3<T>) -> Vec3<T> {
3275			Vec3((self.0).0 * vec.0 + (self.1).0 * vec.1 + (self.2).0 * vec.2 + (self.3).0,
3276				 (self.0).1 * vec.0 + (self.1).1 * vec.1 + (self.2).1 * vec.2 + (self.3).1,
3277				 (self.0).2 * vec.0 + (self.1).2 * vec.1 + (self.2).2 * vec.2 + (self.3).2)
3278		}
3279		
3280		pub fn transform_dir(&self, vec: Vec3<T>) -> Vec3<T> {
3281			Vec3((self.0).0 * vec.0 + (self.1).0 * vec.1 + (self.2).0 * vec.2,
3282				 (self.0).1 * vec.0 + (self.1).1 * vec.1 + (self.2).1 * vec.2,
3283				 (self.0).2 * vec.0 + (self.1).2 * vec.1 + (self.2).2 * vec.2)
3284		}
3285		
3286		pub fn scale(self, scale: Vec3<T>) -> Self {
3287			Self(self.0 * scale.0,
3288				 self.1 * scale.1,
3289				 self.2 * scale.2,
3290				 self.3)
3291		}
3292		
3293		pub fn scale_local(self, scale: Vec3<T>) -> Self {
3294			Self(Vec4(scale.0 * (self.0).0, scale.1 * (self.0).1, scale.2 * (self.0).2, (self.0).3),
3295				 Vec4(scale.0 * (self.1).0, scale.1 * (self.1).1, scale.2 * (self.1).2, (self.1).3),
3296				 Vec4(scale.0 * (self.2).0, scale.1 * (self.2).1, scale.2 * (self.2).2, (self.2).3),
3297				 Vec4(scale.0 * (self.3).0, scale.1 * (self.3).1, scale.2 * (self.3).2, (self.3).3))
3298		}
3299		
3300		pub fn translate(self, t: Vec3<T>) -> Self {
3301			Self(self.0,
3302				 self.1,
3303				 self.3,
3304				 Vec4((self.0).0 * t.0 + (self.1).0 * t.1 + (self.2).0 * t.2 + (self.3).0,
3305					  (self.0).1 * t.0 + (self.1).1 * t.1 + (self.2).1 * t.2 + (self.3).1,
3306					  (self.0).2 * t.0 + (self.1).2 * t.1 + (self.2).2 * t.2 + (self.3).2,
3307					  (self.0).3 * t.0 + (self.1).3 * t.1 + (self.2).3 * t.2 + (self.3).3))
3308		}
3309		
3310		pub fn translate_local(self, t: Vec3<T>) -> Self {
3311			Self(Vec4((self.0).0 + t.0 * (self.0).3,
3312					  (self.0).1 + t.1 * (self.0).3,
3313					  (self.0).2 + t.2 * (self.0).3,
3314					  (self.0).3),
3315				 Vec4((self.1).0 + t.0 * (self.1).3,
3316					  (self.1).1 + t.1 * (self.1).3,
3317					  (self.1).2 + t.2 * (self.1).3,
3318					  (self.1).3),
3319				 Vec4((self.2).0 + t.0 * (self.2).3,
3320					  (self.2).1 + t.1 * (self.2).3,
3321					  (self.2).2 + t.2 * (self.2).3,
3322					  (self.2).3),
3323				 Vec4((self.3).0 + t.0 * (self.3).3,
3324					  (self.3).1 + t.1 * (self.3).3,
3325					  (self.3).2 + t.2 * (self.3).3,
3326					  (self.3).3))
3327		}
3328		
3329		pub fn get_translation(&self) -> Vec3<T> {
3330			Vec3((self.0).0, (self.1).0, (self.2).0)
3331		}
3332	}
3333	
3334	impl<T: Add<Output = U>, U> Add for Mat4<T> {
3335		type Output = Mat4<U>;
3336		
3337		fn add(self, rhs: Self) -> Self::Output {
3338			Mat4(self.0 + rhs.0,
3339				 self.1 + rhs.1,
3340				 self.2 + rhs.2,
3341				 self.3 + rhs.3)
3342		}
3343	}
3344	
3345	impl<T: Sub<Output = U>, U> Sub for Mat4<T> {
3346		type Output = Mat4<U>;
3347		
3348		fn sub(self, rhs: Self) -> Self::Output {
3349			Mat4(self.0 - rhs.0,
3350				 self.1 - rhs.1,
3351				 self.2 - rhs.2,
3352				 self.3 - rhs.3)
3353		}
3354	}
3355	
3356	impl<T: Add<Output = T> + Mul<Output = T> + Copy> Mul for Mat4<T> {
3357		type Output = Self;
3358		
3359		fn mul(self, rhs: Self) -> Self::Output {
3360			Mat4(Vec4((self.0).0 * (rhs.0).0 + (self.1).0 * (rhs.0).1 + (self.2).0 * (rhs.0).2 + (self.3).0 * (rhs.0).3,
3361					  (self.0).1 * (rhs.0).0 + (self.1).1 * (rhs.0).1 + (self.2).1 * (rhs.0).2 + (self.3).1 * (rhs.0).3,
3362					  (self.0).2 * (rhs.0).0 + (self.1).2 * (rhs.0).1 + (self.2).2 * (rhs.0).2 + (self.3).2 * (rhs.0).3,
3363					  (self.0).3 * (rhs.0).0 + (self.1).3 * (rhs.0).1 + (self.2).3 * (rhs.0).2 + (self.3).3 * (rhs.0).3),
3364				 Vec4((self.0).0 * (rhs.1).0 + (self.1).0 * (rhs.1).1 + (self.2).0 * (rhs.1).2 + (self.3).0 * (rhs.1).3,
3365					  (self.0).1 * (rhs.1).0 + (self.1).1 * (rhs.1).1 + (self.2).1 * (rhs.1).2 + (self.3).1 * (rhs.1).3,
3366					  (self.0).2 * (rhs.1).0 + (self.1).2 * (rhs.1).1 + (self.2).2 * (rhs.1).2 + (self.3).2 * (rhs.1).3,
3367					  (self.0).3 * (rhs.1).0 + (self.1).3 * (rhs.1).1 + (self.2).3 * (rhs.1).2 + (self.3).3 * (rhs.1).3),
3368				 Vec4((self.0).0 * (rhs.2).0 + (self.1).0 * (rhs.2).1 + (self.2).0 * (rhs.2).2 + (self.3).0 * (rhs.2).3,
3369					  (self.0).1 * (rhs.2).0 + (self.1).1 * (rhs.2).1 + (self.2).1 * (rhs.2).2 + (self.3).1 * (rhs.2).3,
3370					  (self.0).2 * (rhs.2).0 + (self.1).2 * (rhs.2).1 + (self.2).2 * (rhs.2).2 + (self.3).2 * (rhs.2).3,
3371					  (self.0).3 * (rhs.2).0 + (self.1).3 * (rhs.2).1 + (self.2).3 * (rhs.2).2 + (self.3).3 * (rhs.2).3),
3372				 Vec4((self.0).0 * (rhs.3).0 + (self.1).0 * (rhs.3).1 + (self.2).0 * (rhs.3).2 + (self.3).0 * (rhs.3).3,
3373					  (self.0).1 * (rhs.3).0 + (self.1).1 * (rhs.3).1 + (self.2).1 * (rhs.3).2 + (self.3).1 * (rhs.3).3,
3374					  (self.0).2 * (rhs.3).0 + (self.1).2 * (rhs.3).1 + (self.2).2 * (rhs.3).2 + (self.3).2 * (rhs.3).3,
3375					  (self.0).3 * (rhs.3).0 + (self.1).3 * (rhs.3).1 + (self.2).3 * (rhs.3).2 + (self.3).3 * (rhs.3).3))
3376		}
3377	}
3378	
3379	impl<T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T> + Neg<Output = T> + Copy> Div for Mat4<T> {
3380		type Output = Self;
3381		
3382		fn div(self, rhs: Self) -> Self::Output {
3383			self * (!rhs)
3384		}
3385	}
3386	
3387	impl<T: Neg<Output = U>, U> Neg for Mat4<T> {
3388		type Output = Mat4<U>;
3389		
3390		fn neg(self) -> Self::Output {
3391			Mat4(-self.0, -self.1, -self.2, -self.3)
3392		}
3393	}
3394	
3395	impl<T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T> + Neg<Output = T> + Copy> Not for Mat4<T> {
3396		type Output = Self;
3397		
3398		fn not(self) -> Self {
3399			let a = (self.0).0 * (self.1).1 - (self.0).1 * (self.1).0;
3400			let b = (self.0).0 * (self.1).2 - (self.0).2 * (self.1).0;
3401			let c = (self.0).0 * (self.1).3 - (self.0).3 * (self.1).0;
3402			let d = (self.0).1 * (self.1).2 - (self.0).2 * (self.1).1;
3403			let e = (self.0).1 * (self.1).3 - (self.0).3 * (self.1).1;
3404			let f = (self.0).2 * (self.1).3 - (self.0).3 * (self.1).2;
3405			let g = (self.2).0 * (self.3).1 - (self.2).1 * (self.3).0;
3406			let h = (self.2).0 * (self.3).2 - (self.2).2 * (self.3).0;
3407			let i = (self.2).0 * (self.3).3 - (self.2).3 * (self.3).0;
3408			let j = (self.2).1 * (self.3).2 - (self.2).2 * (self.3).1;
3409			let k = (self.2).1 * (self.3).3 - (self.2).3 * (self.3).1;
3410			let l = (self.2).2 * (self.3).3 - (self.2).3 * (self.3).2;
3411			let det = a * l - b * k + c * j + d * i - e * h + f * g;
3412			Self(Vec4(( (self.1).1 * l - (self.1).2 * k + (self.1).3 * j) / det,
3413					  (-(self.0).1 * l + (self.0).2 * k - (self.0).3 * j) / det,
3414					  ( (self.3).1 * f - (self.3).2 * e + (self.3).3 * d) / det,
3415					  (-(self.2).1 * f + (self.2).2 * e - (self.2).3 * d) / det),
3416				 Vec4((-(self.1).0 * l + (self.1).2 * i - (self.1).3 * h) / det,
3417					  ( (self.0).0 * l - (self.0).2 * i + (self.0).3 * h) / det,
3418					  (-(self.3).0 * f + (self.3).2 * c - (self.3).3 * b) / det,
3419					  ( (self.2).0 * f - (self.2).2 * c + (self.2).3 * b) / det),
3420				 Vec4(( (self.1).0 * k - (self.1).1 * i + (self.1).3 * g) / det,
3421					  (-(self.0).0 * k + (self.0).1 * i - (self.0).3 * g) / det,
3422					  ( (self.3).0 * e - (self.3).1 * c + (self.3).3 * a) / det,
3423					  (-(self.2).0 * e + (self.2).1 * c - (self.2).3 * a) / det),
3424				 Vec4((-(self.1).0 * j + (self.1).1 * h - (self.1).2 * g) / det,
3425					  ( (self.0).0 * j - (self.0).1 * h + (self.0).2 * g) / det,
3426					  (-(self.3).0 * d + (self.3).1 * b - (self.3).2 * a) / det,
3427					  ( (self.2).0 * d - (self.2).1 * b + (self.2).2 * a) / det))
3428		}
3429	}
3430	
3431	impl<T: AddAssign> AddAssign for Mat4<T> {
3432		fn add_assign(&mut self, rhs: Self) {
3433			self.0 += rhs.0;
3434			self.1 += rhs.1;
3435			self.2 += rhs.2;
3436			self.3 += rhs.3;
3437		}
3438	}
3439	
3440	impl<T: SubAssign> SubAssign for Mat4<T> {
3441		fn sub_assign(&mut self, rhs: Self) {
3442			self.0 -= rhs.0;
3443			self.1 -= rhs.1;
3444			self.2 -= rhs.2;
3445			self.3 -= rhs.3;
3446		}
3447	}
3448}
3449
3450pub mod bezier1 {
3451	use super::*;
3452	
3453	#[derive(Copy, Clone, Debug)]
3454	pub struct Bezier1(pub Vec2<f32>, pub Vec2<f32>);
3455	
3456	impl Bezier1 {
3457		/// https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection#Given_two_points_on_each_line
3458		pub fn intersections(self, l0: Vec2<f32>, l1: Vec2<f32>) -> u32 {
3459			let Bezier1(p0, p1) = self;
3460			let d0 = p1 - p0;
3461			let d1 = l1 - l0;
3462			let t0 = ((p0.0 - l0.0) * d1.1 - (p0.1 - l0.1) * d1.0)
3463				* ((p1.0 - l0.0) * d1.1 - (p1.1 - l0.1) * d1.0);
3464			let t1 = ((l0.0 - p0.0) * d0.1 - (l0.1 - p0.1) * d0.0)
3465				* ((l1.0 - p0.0) * d0.1 - (l1.1 - p0.1) * d0.0);
3466			match t0 >= 0.0 && t0 <= 1.0 && t1 >= 0.0 && t1 <= 1.0 {
3467				true  => 0,
3468				false => 1
3469			}
3470		}
3471		
3472		pub fn distance2(self, p: Vec2<f32>) -> f32 {
3473			let Bezier1(p0, p1) = self;
3474			let a = p - p0;
3475			let b = p1 - p0;
3476			let e = a.dot(b);
3477			a.len2() - e * e / b.len2()
3478		}
3479	}
3480	
3481	#[cfg(test)]
3482	mod tests {
3483		use super::*;
3484		
3485		#[test]
3486		fn intersections() {
3487			let b = Bezier1(Vec2(0.0, 0.0), Vec2(1.0, 0.0));
3488			assert_eq!(0, b.intersections(Vec2(0.0, 1.0), Vec2(1.0, 1.0)));
3489			assert_eq!(1, b.intersections(Vec2(0.0, -0.5), Vec2(1.0, 0.5)));
3490		}
3491		
3492		#[test]
3493		fn distance() {
3494			let b = Bezier1(Vec2(0.0, 0.0), Vec2(1.0, 0.0));
3495			assert_eq!(1.0, b.distance2(Vec2(0.5, 1.0)));
3496		}
3497	}
3498}
3499
3500pub mod bezier2 {
3501	use super::*;
3502	
3503	#[derive(Copy, Clone, Debug)]
3504	pub struct Bezier2(pub Vec2<f32>, pub Vec2<f32>, pub Vec2<f32>);
3505	
3506	impl Bezier2 {
3507		pub fn b(self, t: f32) -> Vec2<f32> {
3508			debug_assert!(t >= 0.0 && t <= 1.0);
3509			let Bezier2(p0, p1, p2) = self;
3510			let tinv = 1.0 - t;
3511			p0 * (tinv * tinv)
3512				+ p1 * 2.0 * t * tinv
3513				+ p2 * (t * t)
3514		}
3515		
3516		/// https://github.com/w8r/bezier-intersect/blob/master/src/quadratic.js
3517		pub fn intersections(self, p: Vec2<f32>, r: Vec2<f32>) -> usize {
3518			let Bezier2(p0, p1, p2) = self;
3519			
3520			/*
3521			// calculate coefficients
3522			let c0 = r.1 * (p0.0 - 2.0 * p1.0 + p2.0) - p.1 * (p0.0 - 2.0 * p1.0 + p2.0)
3523						+ p.0 * (p0.1 - 2.0 * p1.1 + p2.1) - r.0 * (p0.1 - 2.0 * p1.1 + p2.1);
3524			let c1 = r.1 * (2.0 * p1.0 - 2.0 * p0.0)  - p.1 * (2.0 * p1.0 - 2.0 * p0.0)
3525						+ p.0 * (2.0 * p1.1 - 2.0 * p0.1)  - r.0 * (2.0 * p1.1 - 2.0 * p0.1);
3526			let c2 = r.1 * p0.0 - p.1 * p0.0
3527				+ p.0 * p0.1 + r.0 * p0.1
3528				+ p.0 * (p.1 - r.1)
3529				+ p.1 * (r.0 - p.0);
3530			*/
3531			
3532			// calculate coefficients
3533			let c0 = p0 + p1 * -2.0 + p2;
3534			let c1 = p0 * -2.0 + p1 * 2.0;
3535			let c2 = p0;
3536			
3537			// Convert line to normal form: ax + by + c = 0
3538			let n = Vec2(p.1 - r.1, r.0 - p.0); // Find normal to line: negative inverse of original line's slope
3539			let cl = p.0 * r.1 - r.0 * p.1;      // Determine new c coefficient
3540			
3541			let min = p.0.min(r.0);
3542			let max = p.0.max(r.0);
3543			
3544			// solve quadratic equation
3545			quadratic_roots_iter(
3546				n.dot(c0),
3547				n.dot(c1),
3548				n.dot(c2) + cl)
3549				.filter(|t| *t >= 0.0 && *t <= 1.0) // check if point is on bezier
3550				.map(|t| self.b(t).0)                       // calculate point on bezier
3551				.filter(|b| *b >= min && *b <= max) // check if point is on line segment
3552				.count()                                           // count intersections
3553		}
3554		
3555		/// http://blog.gludion.com/2009/08/distance-to-quadratic-bezier-curve.html
3556		/// alternative: https://www.academia.edu/34616746/An_algorithm_for_computing_the_shortest_distance_between_a_point_and_quadratic_Bezier_curve
3557		pub fn distance2(self, p: Vec2<f32>) -> f32 {
3558			let Bezier2(p0, p1, p2) = self;
3559			
3560			let a = p1 - p0;
3561			let b = p2 - p1 - a;
3562			let c = p0 - p;
3563			
3564			cubic_roots_iter(
3565				b.dot(b),
3566				3.0 * a.dot(b),
3567				2.0 * a.dot(a) + c.dot(b),
3568				c.dot(a))
3569				.filter(|t| *t >= 0.0 && *t <= 1.0)
3570				.map(|t| p.distance2(self.b(t)))
3571				.fold(p.distance2(p0), f32::min)
3572				.min(p.distance2(p2))
3573		}
3574	}
3575	
3576	#[cfg(test)]
3577	mod tests {
3578		use super::*;
3579		
3580		#[test]
3581		fn intersections() {
3582			let b = Bezier2(Vec2(0.0, 0.0), Vec2(1.0, 0.0), Vec2(2.0, 0.0));
3583			assert_eq!(0, b.intersections(Vec2(0.0, 1.0), Vec2(1.0, 1.0)));
3584			assert_eq!(1, b.intersections(Vec2(0.0, -0.5), Vec2(1.0, 0.5)));
3585		}
3586		
3587		#[test]
3588		fn distance() {
3589			let b = Bezier2(Vec2(0.0, 0.0), Vec2(1.0, 0.0), Vec2(2.0, 0.0));
3590			assert_eq!(1.0, b.distance2(Vec2(0.5, 1.0)));
3591		}
3592	}
3593}
3594
3595pub mod bezier3 {
3596	use super::*;
3597	
3598	#[derive(Copy, Clone, Debug)]
3599	pub struct Bezier3(pub Vec2<f32>, pub Vec2<f32>, pub Vec2<f32>, pub Vec2<f32>);
3600	
3601	/// https://en.wikipedia.org/wiki/B%C3%A9zier_curve#Properties
3602	impl From<Bezier2> for Bezier3 {
3603		fn from(Bezier2(p0, p1, p2): Bezier2) -> Self {
3604			const FRAC_2_3: f32 = 2.0 / 3.0;
3605			Self(p0,
3606				 p0 * (1f32 - FRAC_2_3) + p1 * FRAC_2_3,
3607				 p2 * (1f32 - FRAC_2_3) + p1 * FRAC_2_3,
3608				 p2)
3609		}
3610	}
3611	
3612	impl Bezier3 {
3613		pub fn b(self, t: f32) -> Vec2<f32> {
3614			debug_assert!(t >= 0.0 && t <= 1.0);
3615			let Bezier3(p0, p1, p2, p3) = self;
3616			let t2 = t * t;
3617			let t3 = t * t2;
3618			let tinv = 1.0 - t;
3619			let tinv2 = tinv * tinv;
3620			let tinv3 = tinv * tinv2;
3621			p0 * tinv3
3622				+ p1 * 3.0 * tinv2 * t
3623				+ p2 * 3.0 * tinv2 * t2
3624				+ p3 * t3
3625		}
3626		
3627		/// https://github.com/w8r/bezier-intersect/blob/master/src/cubic.js
3628		pub fn intersections(self, p: Vec2<f32>, r: Vec2<f32>) -> usize {
3629			let Bezier3(p0, p1, p2, p3) = self;
3630			
3631			let min = p.min(r);
3632			let max = p.max(r);
3633			
3634			// calculate coefficients
3635			let c0 = p0 * -1.0 + p1 *  3.0 + p2 * -3.0 + p3;
3636			let c1 = p0 *  3.0 + p1 * -6.0 + p2 *  3.0;
3637			let c2 = p0 * -3.0 + p1 *  3.0;
3638			let c3 = p0;
3639			
3640			// Convert line to normal form: ax + by + c = 0
3641			let n = Vec2(p.1 - r.1, r.0 - p.0); // Find normal to line: negative inverse of original line's slope
3642			let cl = p.0 * r.1 - r.0 * p.1;      // Determine new c coefficient
3643			
3644			// ?Rotate each cubic coefficient using line for new coordinate system?
3645			// Find roots of rotated cubic
3646			cubic_roots_iter(
3647				n.dot(c0),
3648				n.dot(c1),
3649				n.dot(c2),
3650				n.dot(c3) + cl)
3651				.filter(|t| *t >= 0.0 && *t <= 1.0)            // check if t in [0;1] i.e. intersects Bezier
3652				.map(|t| self.b(t))                                    // calculate point on Bezier
3653				.filter(|b| b.0 >= min.0 && b.1 >= min.1
3654					&& b.0 <= max.0 && b.1 <= max.1)                          // check if point is on line segment
3655				.count()                                                      // count intersections
3656		}
3657		
3658		/// alternative: https://hal.inria.fr/file/index/docid/518379/filename/Xiao-DiaoChen2007c.pdf
3659		pub fn distance2(self, p: Vec2<f32>) -> f32 {
3660			const STEPS: usize = 1000;
3661			let mut distance = self.3.distance2(p);
3662			for i in 0..STEPS {
3663				distance = distance.min(self.b(1.0 / STEPS as f32 * i as f32).distance2(p));
3664			}
3665			distance
3666		}
3667	}
3668	
3669	#[cfg(test)]
3670	mod tests {
3671		use super::*;
3672		
3673		#[test]
3674		fn intersections() {
3675			let b = Bezier3(Vec2(0.0, 0.0), Vec2(1.0, 0.0), Vec2(2.0, 0.0), Vec2(3.0, 0.0));
3676			assert_eq!(0, b.intersections(Vec2(0.0, 1.0), Vec2(1.0, 1.0)));
3677			assert_eq!(1, b.intersections(Vec2(0.0, -0.5), Vec2(1.0, 0.5)));
3678		}
3679		
3680		#[test]
3681		fn distance() {
3682			let b = Bezier3(Vec2(0.0, 0.0), Vec2(1.0, 0.0), Vec2(2.0, 0.0), Vec2(3.0, 0.0));
3683			assert!((b.distance2(Vec2(0.5, 1.0)) - 1.0).abs() < 1e-4);
3684		}
3685	}
3686}
3687
3688pub mod sphere {
3689	use super::*;
3690	
3691	#[derive(Copy, Clone, Debug)]
3692	pub struct Sphere {
3693		pub radius: f32
3694	}
3695	
3696	impl Distance<Vec3<f32>> for Sphere {
3697		fn distance(&self, point: &Vec3<f32>) -> f32 {
3698			(*point).len() - self.radius
3699		}
3700	}
3701	
3702	#[cfg(test)]
3703	mod tests {
3704		use super::*;
3705		
3706		#[test]
3707		fn distance() {
3708			assert_eq!(1f32, Sphere { radius: 0.5 }.distance(&Vec3(0.0, 0.0, 1.5)))
3709		}
3710	}
3711}
3712
3713pub mod cuboid {
3714	use super::*;
3715	
3716	#[derive(Copy, Clone, Debug)]
3717	pub struct Cuboid(Vec3<f32>);
3718	
3719	impl Distance<Vec3<f32>> for Cuboid {
3720		fn distance(&self, p: &Vec3<f32>) -> f32 {
3721			let d = (*p).abs() - self.0;
3722			d.maxs(0f32).len() + d.0.max(d.1).max(d.2).min(0f32)
3723		}
3724	}
3725	
3726	#[cfg(test)]
3727	mod tests {
3728		use super::*;
3729		
3730		#[test]
3731		fn distance() {
3732			assert_eq!(1f32, Cuboid(Vec3(0.5, 0.5, 0.5)).distance(&Vec3(0.0, 0.0, 1.5)))
3733		}
3734	}
3735}
3736
3737pub mod plane {
3738	use super::*;
3739	
3740	#[derive(Copy, Clone, Debug)]
3741	pub struct Plane(Vec4<f32>);
3742	
3743	impl Distance<Vec3<f32>> for Plane {
3744		fn distance(&self, p: &Vec3<f32>) -> f32 {
3745			(*p).dot(self.0.xyz()) + (self.0).3
3746		}
3747	}
3748	
3749	#[cfg(test)]
3750	mod tests {
3751		use super::*;
3752		
3753		#[test]
3754		fn distance() {
3755			// TODO add test
3756		}
3757	}
3758}
3759
3760pub mod cylinder {
3761	use super::*;
3762	
3763	#[derive(Copy, Clone, Debug)]
3764	pub struct Cylinder(Vec2<f32>);
3765	
3766	impl Distance<Vec3<f32>> for Cylinder {
3767		fn distance(&self, p: &Vec3<f32>) -> f32 {
3768			let d = Vec2((*p).xz().len(), p.1).abs() - self.0;
3769			d.0.max(d.1).min(0.0) + d.maxs(0.0).len()
3770		}
3771	}
3772	
3773	#[cfg(test)]
3774	mod tests {
3775		use super::*;
3776		
3777		#[test]
3778		fn distance() {
3779			assert_eq!(1f32, Cylinder(Vec2(0.5, 0.5)).distance(&Vec3(0.0, 0.0, 1.5)))
3780		}
3781	}
3782}
3783
3784pub mod triangle {
3785	use super::*;
3786	
3787	#[derive(Copy, Clone, Debug)]
3788	pub struct Triangle {
3789		pub a: Vec3<f32>,
3790		pub b: Vec3<f32>,
3791		pub c: Vec3<f32>
3792	}
3793	
3794	impl Distance2<Vec3<f32>> for Triangle {
3795		fn distance2(&self, p: &Vec3<f32>) -> f32 {
3796			let Triangle { a, b, c } = *self;
3797			let p = *p;
3798			let ba = b - a;
3799			let pa = p - a;
3800			let cb = c - b;
3801			let pb = p - b;
3802			let ac = a - c;
3803			let pc = p - c;
3804			let nor = ba.cross(ac);
3805			
3806			if ba.cross(nor).dot(pa).sign() +
3807				cb.cross(nor).dot(pb).sign() +
3808				ac.cross(nor).dot(pc).sign() < 2 {
3809				(ba * (ba.dot(pa) / ba.dot(ba)).clamp(0f32, 1f32) - pa).len2()
3810					.min((cb * (cb.dot(pb) / cb.dot(cb)).clamp(0f32, 1f32) - pb).len2())
3811					.min((ac * (ac.dot(pc) / ac.dot(ac)).clamp(0f32, 1f32) - pc).len2())
3812			} else {
3813				nor.dot(pa) * nor.dot(pa) / nor.dot(nor)
3814			}
3815		}
3816	}
3817	
3818	#[cfg(test)]
3819	mod tests {
3820		use super::*;
3821		
3822		#[test]
3823		fn distance() {
3824			assert_eq!(1f32, Triangle {
3825				a: Vec3(1.0, 0.0, 0.0),
3826				b: Vec3(0.0, 0.0, 0.0),
3827				c: Vec3(0.0, 1.0, 0.0)
3828			}.distance(&Vec3(0.0, 0.0, 1.0)))
3829		}
3830	}
3831}
3832
3833pub mod quad {
3834	use super::*;
3835	
3836	#[derive(Copy, Clone, Debug)]
3837	pub struct Quad {
3838		pub a: Vec3<f32>,
3839		pub b: Vec3<f32>,
3840		pub c: Vec3<f32>,
3841		pub d: Vec3<f32>
3842	}
3843	
3844	impl Distance2<Vec3<f32>> for Quad {
3845		fn distance2(&self, p: &Vec3<f32>) -> f32 {
3846			let Quad { a, b, c, d } = *self;
3847			let p = *p;
3848			let ba = b - a;
3849			let pa = p - a;
3850			let cb = c - b;
3851			let pb = p - b;
3852			let dc = d - c;
3853			let pc = p - c;
3854			let ad = a - d;
3855			let pd = p - d;
3856			let nor = ba.cross(ad);
3857			
3858			if ba.cross(nor).dot(pa).sign() +
3859				cb.cross(nor).dot(pb).sign() +
3860				dc.cross(nor).dot(pc).sign() +
3861				ad.cross(nor).dot(pd).sign() < 3 {
3862				(ba * (ba.dot(pa) / ba.dot(ba)).clamp(0f32, 1f32) - pa).len2()
3863					.min((cb * (cb.dot(pb) / cb.dot(cb)).clamp(0f32, 1f32) - pb).len2())
3864					.min((dc * (dc.dot(pc) / dc.dot(dc)).clamp(0f32, 1f32) - pc).len2())
3865					.min((ad * (ad.dot(pd) / ad.dot(ad)).clamp(0f32, 1f32) - pd).len2())
3866			} else {
3867				nor.dot(pa) * nor.dot(pa) / nor.dot(nor)
3868			}
3869		}
3870	}
3871	
3872	#[cfg(test)]
3873	mod tests {
3874		use super::*;
3875		
3876		#[test]
3877		fn distance() {
3878			assert_eq!(1f32, Quad {
3879				a: Vec3(0.0, 0.0, 0.0),
3880				b: Vec3(1.0, 0.0, 0.0),
3881				c: Vec3(1.0, 1.0, 0.0),
3882				d: Vec3(0.0, 1.0, 0.0)
3883			}.distance(&Vec3(0.0, 0.0, 1.0)))
3884		}
3885	}
3886}
3887
3888pub mod ops {
3889	use super::*;
3890	
3891	pub trait Distance<T> {
3892		fn distance(&self, _: &T) -> f32;
3893	}
3894	
3895	pub trait Distance2<T> {
3896		fn distance2(&self, _: &T) -> f32;
3897	}
3898	
3899	impl<T: Distance2<U>, U> Distance<U> for T {
3900		#[inline]
3901		fn distance(&self, v: &U) -> f32 {
3902			self.distance2(v).sqrt()
3903		}
3904	}
3905	
3906	pub trait Intersections<T> {
3907		fn intersections(&self, _: &T) -> u32;
3908		
3909		#[inline]
3910		fn intersects(&self, v: &T) -> bool {
3911			self.intersections(v) > 0
3912		}
3913	}
3914}
3915
3916pub mod sdf {
3917	use super::*;
3918	
3919	#[inline]
3920	pub fn union(d1: f32, d2: f32) -> f32 {
3921		d1.min(d2)
3922	}
3923	
3924	#[inline]
3925	pub fn subtraction(d1: f32, d2: f32) -> f32 {
3926		(-d1).max(d2)
3927	}
3928	
3929	#[inline]
3930	pub fn intersection(d1: f32, d2: f32) -> f32 {
3931		d1.max(d2)
3932	}
3933	
3934	pub fn smooth_union(d1: f32, d2: f32, k: f32) -> f32 {
3935		let h = (0.5f32 + 0.5f32 * (d2 - d1) / k).clamp(0f32, 1f32);
3936		mix(d2, d1, h) - k * h * (1f32 - h)
3937	}
3938	
3939	pub fn smooth_subtraction(d1: f32, d2: f32, k: f32) -> f32 {
3940		let h = (0.5f32 - 0.5f32 * (d2 + d1) / k).clamp(0f32, 1f32);
3941		mix(d2, -d1, h) + k * h * (1f32 - h)
3942	}
3943	
3944	pub fn smooth_intersection(d1: f32, d2: f32, k: f32) -> f32 {
3945		let h = (0.5f32 - 0.5f32 * (d2 - d1) / k).clamp(0f32, 1f32);
3946		mix(d2, d1, h) + k * h * (1f32 - h)
3947	}
3948	
3949	pub struct Union<T: Distance<Vec3<f32>>, U: Distance<Vec3<f32>>>(T, U);
3950	
3951	impl<T: Distance<Vec3<f32>>, U: Distance<Vec3<f32>>> Distance<Vec3<f32>> for Union<T, U> {
3952		fn distance(&self, point: &Vec3<f32>) -> f32 {
3953			union(self.0.distance(point), self.1.distance(point))
3954		}
3955	}
3956	
3957	pub struct Subtraction<T: Distance<Vec3<f32>>, U: Distance<Vec3<f32>>>(T, U);
3958	
3959	impl<T: Distance<Vec3<f32>>, U: Distance<Vec3<f32>>> Distance<Vec3<f32>> for Subtraction<T, U> {
3960		fn distance(&self, point: &Vec3<f32>) -> f32 {
3961			subtraction(self.0.distance(point), self.1.distance(point))
3962		}
3963	}
3964	
3965	pub struct Intersection<T: Distance<Vec3<f32>>, U: Distance<Vec3<f32>>>(T, U);
3966	
3967	impl<T: Distance<Vec3<f32>>, U: Distance<Vec3<f32>>> Distance<Vec3<f32>> for Intersection<T, U> {
3968		fn distance(&self, point: &Vec3<f32>) -> f32 {
3969			intersection(self.0.distance(point), self.1.distance(point))
3970		}
3971	}
3972	
3973	pub struct SmoothUnion<T: Distance<Vec3<f32>>, U: Distance<Vec3<f32>>>(T, U, f32);
3974	
3975	impl<T: Distance<Vec3<f32>>, U: Distance<Vec3<f32>>> Distance<Vec3<f32>> for SmoothUnion<T, U> {
3976		fn distance(&self, point: &Vec3<f32>) -> f32 {
3977			smooth_union(self.0.distance(point), self.1.distance(point), self.2)
3978		}
3979	}
3980	
3981	pub struct SmoothSubtraction<T: Distance<Vec3<f32>>, U: Distance<Vec3<f32>>>(T, U, f32);
3982	
3983	impl<T: Distance<Vec3<f32>>, U: Distance<Vec3<f32>>> Distance<Vec3<f32>> for SmoothSubtraction<T, U> {
3984		fn distance(&self, point: &Vec3<f32>) -> f32 {
3985			smooth_subtraction(self.0.distance(point), self.1.distance(point), self.2)
3986		}
3987	}
3988	
3989	pub struct SmoothIntersection<T: Distance<Vec3<f32>>, U: Distance<Vec3<f32>>>(T, U, f32);
3990	
3991	impl<T: Distance<Vec3<f32>>, U: Distance<Vec3<f32>>> Distance<Vec3<f32>> for SmoothIntersection<T, U> {
3992		fn distance(&self, point: &Vec3<f32>) -> f32 {
3993			smooth_intersection(self.0.distance(point), self.1.distance(point), self.2)
3994		}
3995	}
3996	
3997	pub struct Round<T: Distance<Vec3<f32>>>(T, f32);
3998	
3999	impl<T: Distance<Vec3<f32>>> Distance<Vec3<f32>> for Round<T> {
4000		fn distance(&self, point: &Vec3<f32>) -> f32 {
4001			self.0.distance(point) - self.1
4002		}
4003	}
4004	
4005	pub struct Translation<T: Distance<Vec3<f32>>>(T, Vec3<f32>);
4006	
4007	impl<T: Distance<Vec3<f32>>> Distance<Vec3<f32>> for Translation<T> {
4008		#[inline]
4009		fn distance(&self, point: &Vec3<f32>) -> f32 {
4010			self.0.distance(&(*point + self.1))
4011		}
4012	}
4013	
4014	pub struct Rotation<T: Distance<Vec3<f32>>>(T, Quat32);
4015	
4016	impl<T: Distance<Vec3<f32>>> Distance<Vec3<f32>> for Rotation<T> {
4017		#[inline]
4018		fn distance(&self, point: &Vec3<f32>) -> f32 {
4019			self.0.distance(&Mat4::<f32>::from_rotation(self.1).transform_pos(*point))
4020		}
4021	}
4022	
4023	pub struct Scale<T: Distance<Vec3<f32>>>(T, f32);
4024	
4025	impl<T: Distance<Vec3<f32>>> Distance<Vec3<f32>> for Scale<T> {
4026		#[inline]
4027		fn distance(&self, point: &Vec3<f32>) -> f32 {
4028			self.0.distance(&(*point / self.1)) * self.1
4029		}
4030	}
4031}
4032
4033pub mod poly {
4034	const EPS: f32 = 1E-6;
4035	const FRAC_1_3: f32 = 1.0 / 3.0;
4036	
4037	pub fn quadratic_roots(c0: f32, c1: f32, c2: f32) -> ([f32; 2], usize) {
4038		if c0.abs() < EPS { return ([-c2 / c1, 0.0], 1) }
4039		let discriminant = c1 * c1 - (4.0 * c0 * c2);
4040		([
4041			 (-c1 + discriminant.sqrt()) / (2.0 * c0),
4042			 (-c1 - discriminant.sqrt()) / (2.0 * c0)
4043		 ], if discriminant > EPS {
4044			2
4045		} else if discriminant < EPS {
4046			0
4047		} else {
4048			1
4049		})
4050	}
4051	
4052	#[inline]
4053	pub fn quadratic_roots_iter(c0: f32, c1: f32, c2: f32) -> QuadraticRootsIter {
4054		let (roots, count) = quadratic_roots(c0, c1, c2);
4055		QuadraticRootsIter {
4056			roots,
4057			count,
4058			index: 0
4059		}
4060	}
4061	
4062	pub struct QuadraticRootsIter {
4063		roots: [f32; 2],
4064		count: usize,
4065		index: usize
4066	}
4067	
4068	impl Iterator for QuadraticRootsIter {
4069		type Item = f32;
4070		
4071		fn next(&mut self) -> Option<Self::Item> {
4072			if self.index < self.count {
4073				let v = Some(self.roots[self.index]);
4074				self.index += 1;
4075				v
4076			} else {
4077				None
4078			}
4079		}
4080	}
4081	
4082	/// https://github.com/nical/lyon/blob/master/geom/src/utils.rs
4083	pub fn cubic_roots(c0: f32, mut c1: f32, mut c2: f32, mut c3: f32) -> ([f32; 3], usize) {
4084		// check if it's a quadratic equation to avoid div by 0
4085		if c0.abs() < EPS {
4086			let (roots, count) = quadratic_roots(c1, c2, c3);
4087			return ([
4088						roots[0],
4089						roots[1],
4090						0.0
4091					], count)
4092		}
4093		
4094		c1 /= c0;
4095		c2 /= c0;
4096		c3 /= c0;
4097		
4098		let d0 = (3.0 * c2 - c1 * c1) / 9.0;
4099		let d1 = (9.0 * c1 * c2 - 27.0 * c3 - 2.0 * c1 * c1 * c1) / 54.0;
4100		let d = d0 * d0 * d0 + d1 * d1;
4101		
4102		if d > EPS {
4103			let p = d1 + d.sqrt();
4104			let m = d1 - d.sqrt();
4105			let s = p.signum() * p.abs().powf(FRAC_1_3);
4106			let t = m.signum() * m.abs().powf(FRAC_1_3);
4107			
4108			if (s - t).abs() < EPS && (s + t) > EPS {
4109				([
4110					 -c1 * FRAC_1_3 + s + t,
4111					 -c1 * FRAC_1_3 - (s + t) * 0.5,
4112					 0.0
4113				 ], 2)
4114			} else {
4115				([
4116					 -c1 * FRAC_1_3 + s + t,
4117					 0.0,
4118					 0.0
4119				 ], 1)
4120			}
4121		} else {
4122			let theta = (d1 / (-d0 * d0 * d0).sqrt()).acos();
4123			let d0 = 2.0 * (-d0).sqrt();
4124			([
4125				 d0 * (theta * FRAC_1_3).cos() - c1 * FRAC_1_3,
4126				 d0 * ((theta + 2.0 * ::std::f32::consts::PI) * FRAC_1_3).cos() - c1 * FRAC_1_3,
4127				 d0 * ((theta + 4.0 * ::std::f32::consts::PI) * FRAC_1_3).cos() - c1 * FRAC_1_3,
4128			 ], 3)
4129		}
4130	}
4131	
4132	#[inline]
4133	pub fn cubic_roots_iter(c0: f32, c1: f32, c2: f32, c3: f32) -> CubicRootsIter {
4134		let (roots, count) = cubic_roots(c0, c1, c2, c3);
4135		CubicRootsIter {
4136			roots,
4137			count,
4138			index: 0
4139		}
4140	}
4141	
4142	pub struct CubicRootsIter {
4143		roots: [f32; 3],
4144		count: usize,
4145		index: usize
4146	}
4147	
4148	impl Iterator for CubicRootsIter {
4149		type Item = f32;
4150		
4151		fn next(&mut self) -> Option<Self::Item> {
4152			if self.index < self.count {
4153				let v = Some(self.roots[self.index]);
4154				self.index += 1;
4155				v
4156			} else {
4157				None
4158			}
4159		}
4160	}
4161	
4162	#[cfg(test)]
4163	mod tests {
4164		use super::*;
4165		
4166		#[test]
4167		fn quadratic() {
4168			assert_eq!(([0.5, -3.0], 2), quadratic_roots(2.0, 5.0, -3.0));
4169		}
4170		
4171		#[test]
4172		fn cubic() {
4173			let (roots, count) = cubic_roots(2.0, -4.0, -22.0, 24.0);
4174			assert_eq!(3, count);
4175			assert!((roots[0] - 4.0).abs() < EPS);
4176			assert!((roots[1] + 3.0).abs() < EPS);
4177			assert!((roots[2] - 1.0).abs() < EPS);
4178		}
4179	}
4180}
4181
4182pub mod misc {
4183	#[inline]
4184	pub fn mix(x: f32, y: f32, a: f32) -> f32 {
4185		x * (1f32 - a) + y * a
4186	}
4187	
4188	pub trait Sign {
4189		fn sign(self) -> i32;
4190	}
4191	
4192	impl Sign for f32 {
4193		fn sign(self) -> i32 {
4194			if self < 0f32 {
4195				-1
4196			} else if self > 0f32 {
4197				1
4198			} else {
4199				0
4200			}
4201		}
4202	}
4203	
4204	impl Sign for f64 {
4205		fn sign(self) -> i32 {
4206			if self < 0f64 {
4207				-1
4208			} else if self > 0f64 {
4209				1
4210			} else {
4211				0
4212			}
4213		}
4214	}
4215	
4216	#[cfg(test)]
4217	mod tests {
4218		use super::*;
4219		
4220		#[test]
4221		fn test_mix() {
4222			assert_eq!(0.5, mix(0.0, 1.0, 0.5));
4223			assert_eq!(0.0, mix(-1.0, 1.0, 0.5));
4224		}
4225		
4226		#[test]
4227		fn sign() {
4228			assert_eq!(0, 0.0.sign());
4229			assert_eq!(-1, -1.5.sign());
4230			assert_eq!(1, 1.5.sign());
4231		}
4232	}
4233}