vec3/
norm.rs

1use core::ops::{Div, Mul};
2use num_traits::real::Real;
3
4use super::{len, sdiv};
5
6/// # Example
7/// ```
8/// let mut v = vec3::new_one::<f32>();
9/// assert_eq!(vec3::norm(&mut v, &vec3::new_one()), 3_f32.sqrt());
10/// assert_eq!(v, [1_f32 / 3_f32.sqrt(); 3]);
11/// ```
12#[inline]
13pub fn norm<'out, T>(out: &'out mut [T; 3], b: &[T; 3]) -> T
14where
15    T: Real,
16    for<'a, 'b> &'a T: Mul<&'b T, Output = T> + Div<&'b T, Output = T>,
17{
18    let s = len(b);
19    sdiv(out, b, &s);
20    s
21}
22
23/// # Example
24/// ```
25/// let mut v = vec3::new_one::<f32>();
26/// assert_eq!(vec3::norm_mut(&mut v), 3_f32.sqrt());
27/// assert_eq!(v, [1_f32 / 3_f32.sqrt(); 3]);
28/// ```
29#[inline]
30pub fn norm_mut<'out, T>(out: &'out mut [T; 3]) -> T
31where
32    T: Real,
33    for<'a, 'b> &'a T: Mul<&'b T, Output = T> + Div<&'b T, Output = T>,
34{
35    let tmp = out.clone();
36    norm(out, &tmp)
37}