#[cfg(feature = "num-traits")]
use num_traits::Float;
use super::{Vec2, Vec3A, Vec3Mask, Vec4};
use core::{fmt, ops::*};
#[cfg(feature = "std")]
use std::iter::{Product, Sum};
const ZERO: Vec3 = const_vec3!([0.0; 3]);
const ONE: Vec3 = const_vec3!([1.0; 3]);
const X_AXIS: Vec3 = const_vec3!([1.0, 0.0, 0.0]);
const Y_AXIS: Vec3 = const_vec3!([0.0, 1.0, 0.0]);
const Z_AXIS: Vec3 = const_vec3!([0.0, 0.0, 1.0]);
#[derive(Clone, Copy, PartialEq, PartialOrd, Default)]
#[repr(C)]
pub struct Vec3 {
pub x: f32,
pub y: f32,
pub z: f32,
}
#[inline]
pub fn vec3(x: f32, y: f32, z: f32) -> Vec3 {
Vec3::new(x, y, z)
}
impl Vec3 {
#[inline]
pub fn new(x: f32, y: f32, z: f32) -> Self {
Self { x, y, z }
}
#[inline]
pub const fn zero() -> Self {
ZERO
}
#[inline]
pub const fn one() -> Self {
ONE
}
#[inline]
pub const fn unit_x() -> Self {
X_AXIS
}
#[inline]
pub const fn unit_y() -> Self {
Y_AXIS
}
#[inline]
pub const fn unit_z() -> Self {
Z_AXIS
}
#[inline]
pub fn splat(v: f32) -> Self {
Self { x: v, y: v, z: v }
}
#[inline]
pub fn extend(self, w: f32) -> Vec4 {
Vec4::new(self.x, self.y, self.z, w)
}
#[inline]
pub fn truncate(self) -> Vec2 {
Vec2::new(self.x, self.y)
}
#[deprecated(since = "0.10.0", note = "please use `.x` instead")]
#[inline]
pub fn x(self) -> f32 {
self.x
}
#[deprecated(since = "0.10.0", note = "please use `.y` instead")]
#[inline]
pub fn y(self) -> f32 {
self.y
}
#[deprecated(since = "0.10.0", note = "please use `.z` instead")]
#[inline]
pub fn z(self) -> f32 {
self.z
}
#[deprecated(since = "0.10.0", note = "please use `.x` instead")]
#[inline]
pub fn x_mut(&mut self) -> &mut f32 {
&mut self.x
}
#[deprecated(since = "0.10.0", note = "please use `.y` instead")]
#[inline]
pub fn y_mut(&mut self) -> &mut f32 {
&mut self.y
}
#[deprecated(since = "0.10.0", note = "please use `.z` instead")]
#[inline]
pub fn z_mut(&mut self) -> &mut f32 {
&mut self.z
}
#[deprecated(since = "0.10.0", note = "please use `.x` instead")]
#[inline]
pub fn set_x(&mut self, x: f32) {
self.x = x;
}
#[deprecated(since = "0.10.0", note = "please use `.y` instead")]
#[inline]
pub fn set_y(&mut self, y: f32) {
self.y = y;
}
#[deprecated(since = "0.10.0", note = "please use `.z` instead")]
#[inline]
pub fn set_z(&mut self, z: f32) {
self.z = z;
}
#[inline]
pub fn dot(self, other: Self) -> f32 {
(self.x * other.x) + (self.y * other.y) + (self.z * other.z)
}
#[inline]
#[allow(dead_code)]
pub(crate) fn dot_as_vec3(self, other: Self) -> Self {
let dot = self.dot(other);
Vec3::new(dot, dot, dot)
}
#[inline]
pub fn cross(self, other: Self) -> Self {
Self {
x: self.y * other.z - other.y * self.z,
y: self.z * other.x - other.z * self.x,
z: self.x * other.y - other.x * self.y,
}
}
#[inline]
pub fn length(self) -> f32 {
self.dot(self).sqrt()
}
#[inline]
pub fn length_squared(self) -> f32 {
self.dot(self)
}
#[inline]
pub fn length_recip(self) -> f32 {
self.length().recip()
}
#[inline]
pub fn distance(self, other: Vec3) -> f32 {
(self - other).length()
}
#[inline]
pub fn distance_squared(self, other: Vec3) -> f32 {
(self - other).length_squared()
}
#[inline]
pub fn normalize(self) -> Self {
self * self.length_recip()
}
#[inline]
pub fn min(self, other: Self) -> Self {
Self {
x: self.x.min(other.x),
y: self.y.min(other.y),
z: self.z.min(other.z),
}
}
#[inline]
pub fn max(self, other: Self) -> Self {
Self {
x: self.x.max(other.x),
y: self.y.max(other.y),
z: self.z.max(other.z),
}
}
#[inline]
pub fn min_element(self) -> f32 {
self.x.min(self.y.min(self.z))
}
#[inline]
pub fn max_element(self) -> f32 {
self.x.max(self.y.max(self.z))
}
#[inline]
pub fn cmpeq(self, other: Self) -> Vec3Mask {
Vec3Mask::new(
self.x.eq(&other.x),
self.y.eq(&other.y),
self.z.eq(&other.z),
)
}
#[inline]
pub fn cmpne(self, other: Self) -> Vec3Mask {
Vec3Mask::new(
self.x.ne(&other.x),
self.y.ne(&other.y),
self.z.ne(&other.z),
)
}
#[inline]
pub fn cmpge(self, other: Self) -> Vec3Mask {
Vec3Mask::new(
self.x.ge(&other.x),
self.y.ge(&other.y),
self.z.ge(&other.z),
)
}
#[inline]
pub fn cmpgt(self, other: Self) -> Vec3Mask {
Vec3Mask::new(
self.x.gt(&other.x),
self.y.gt(&other.y),
self.z.gt(&other.z),
)
}
#[inline]
pub fn cmple(self, other: Self) -> Vec3Mask {
Vec3Mask::new(
self.x.le(&other.x),
self.y.le(&other.y),
self.z.le(&other.z),
)
}
#[inline]
pub fn cmplt(self, other: Self) -> Vec3Mask {
Vec3Mask::new(
self.x.lt(&other.x),
self.y.lt(&other.y),
self.z.lt(&other.z),
)
}
#[inline]
pub fn from_slice_unaligned(slice: &[f32]) -> Self {
Self::new(slice[0], slice[1], slice[2])
}
#[inline]
pub fn write_to_slice_unaligned(self, slice: &mut [f32]) {
let a = self.as_ref();
slice[0] = a[0];
slice[1] = a[1];
slice[2] = a[2];
}
#[inline]
#[allow(dead_code)]
pub(crate) fn mul_add(self, a: Self, b: Self) -> Self {
Self {
x: (self.x * a.x) + b.x,
y: (self.y * a.y) + b.y,
z: (self.z * a.z) + b.z,
}
}
#[inline]
pub fn abs(self) -> Self {
Self {
x: self.x.abs(),
y: self.y.abs(),
z: self.z.abs(),
}
}
#[inline]
pub fn round(self) -> Self {
Self {
x: self.x.round(),
y: self.y.round(),
z: self.z.round(),
}
}
#[inline]
pub fn floor(self) -> Self {
Self {
x: self.x.floor(),
y: self.y.floor(),
z: self.z.floor(),
}
}
#[inline]
pub fn ceil(self) -> Self {
Self {
x: self.x.ceil(),
y: self.y.ceil(),
z: self.z.ceil(),
}
}
#[inline]
pub fn is_nan(self) -> Vec3Mask {
Vec3Mask::new(self.x.is_nan(), self.y.is_nan(), self.z.is_nan())
}
#[inline]
pub fn signum(self) -> Self {
Self {
x: self.x.signum(),
y: self.y.signum(),
z: self.z.signum(),
}
}
#[inline]
pub fn recip(self) -> Self {
Self {
x: self.x.recip(),
y: self.y.recip(),
z: self.z.recip(),
}
}
#[inline]
pub fn lerp(self, other: Self, s: f32) -> Self {
self + ((other - self) * s)
}
#[inline]
pub fn is_normalized(self) -> bool {
is_normalized!(self)
}
#[inline]
pub fn abs_diff_eq(self, other: Self, max_abs_diff: f32) -> bool {
abs_diff_eq!(self, other, max_abs_diff)
}
#[inline]
pub fn angle_between(self, other: Self) -> f32 {
crate::f32::funcs::scalar_acos(self.dot(other) / (self.dot(self) * other.dot(other)).sqrt())
}
}
impl AsRef<[f32; 3]> for Vec3 {
#[inline]
fn as_ref(&self) -> &[f32; 3] {
unsafe { &*(self as *const Vec3 as *const [f32; 3]) }
}
}
impl AsMut<[f32; 3]> for Vec3 {
#[inline]
fn as_mut(&mut self) -> &mut [f32; 3] {
unsafe { &mut *(self as *mut Vec3 as *mut [f32; 3]) }
}
}
impl fmt::Debug for Vec3 {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_tuple("Vec3")
.field(&self.x)
.field(&self.y)
.field(&self.z)
.finish()
}
}
impl fmt::Display for Vec3 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
}
}
impl Div<Vec3> for Vec3 {
type Output = Self;
#[inline]
fn div(self, other: Self) -> Self {
Self {
x: self.x / other.x,
y: self.y / other.y,
z: self.z / other.z,
}
}
}
impl DivAssign<Vec3> for Vec3 {
#[inline]
fn div_assign(&mut self, other: Self) {
self.x /= other.x;
self.y /= other.y;
self.z /= other.z;
}
}
impl Div<f32> for Vec3 {
type Output = Self;
#[inline]
fn div(self, other: f32) -> Self {
Self {
x: self.x / other,
y: self.y / other,
z: self.z / other,
}
}
}
impl DivAssign<f32> for Vec3 {
#[inline]
fn div_assign(&mut self, other: f32) {
self.x /= other;
self.y /= other;
self.z /= other;
}
}
impl Div<Vec3> for f32 {
type Output = Vec3;
#[inline]
fn div(self, other: Vec3) -> Vec3 {
Vec3 {
x: self / other.x,
y: self / other.y,
z: self / other.z,
}
}
}
impl Mul<Vec3> for Vec3 {
type Output = Self;
#[inline]
fn mul(self, other: Self) -> Self {
Self {
x: self.x * other.x,
y: self.y * other.y,
z: self.z * other.z,
}
}
}
impl MulAssign<Vec3> for Vec3 {
#[inline]
fn mul_assign(&mut self, other: Self) {
self.x *= other.x;
self.y *= other.y;
self.z *= other.z;
}
}
impl Mul<f32> for Vec3 {
type Output = Self;
#[inline]
fn mul(self, other: f32) -> Self {
Self {
x: self.x * other,
y: self.y * other,
z: self.z * other,
}
}
}
impl MulAssign<f32> for Vec3 {
#[inline]
fn mul_assign(&mut self, other: f32) {
self.x *= other;
self.y *= other;
self.z *= other;
}
}
impl Mul<Vec3> for f32 {
type Output = Vec3;
#[inline]
fn mul(self, other: Vec3) -> Vec3 {
Vec3 {
x: self * other.x,
y: self * other.y,
z: self * other.z,
}
}
}
impl Add for Vec3 {
type Output = Self;
#[inline]
fn add(self, other: Self) -> Self {
Self {
x: self.x + other.x,
y: self.y + other.y,
z: self.z + other.z,
}
}
}
impl AddAssign for Vec3 {
#[inline]
fn add_assign(&mut self, other: Self) {
self.x += other.x;
self.y += other.y;
self.z += other.z;
}
}
impl Sub for Vec3 {
type Output = Self;
#[inline]
fn sub(self, other: Self) -> Self {
Self {
x: self.x - other.x,
y: self.y - other.y,
z: self.z - other.z,
}
}
}
impl SubAssign for Vec3 {
#[inline]
fn sub_assign(&mut self, other: Self) {
self.x -= other.x;
self.y -= other.y;
self.z -= other.z;
}
}
impl Neg for Vec3 {
type Output = Self;
#[inline]
fn neg(self) -> Self {
Self {
x: -self.x,
y: -self.y,
z: -self.z,
}
}
}
impl Index<usize> for Vec3 {
type Output = f32;
#[inline]
fn index(&self, index: usize) -> &Self::Output {
&self.as_ref()[index]
}
}
impl IndexMut<usize> for Vec3 {
#[inline]
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.as_mut()[index]
}
}
impl From<(f32, f32, f32)> for Vec3 {
#[inline]
fn from(t: (f32, f32, f32)) -> Self {
Self::new(t.0, t.1, t.2)
}
}
impl From<Vec3> for (f32, f32, f32) {
#[inline]
fn from(v: Vec3) -> Self {
(v.x, v.y, v.z)
}
}
impl From<[f32; 3]> for Vec3 {
#[inline]
fn from(a: [f32; 3]) -> Self {
Self::new(a[0], a[1], a[2])
}
}
impl From<Vec3> for [f32; 3] {
#[inline]
fn from(v: Vec3) -> Self {
[v.x, v.y, v.z]
}
}
impl From<Vec3A> for Vec3 {
#[inline]
fn from(v: Vec3A) -> Self {
#[cfg(vec3a_sse2)]
{
let (x, y, z) = v.into();
Self { x, y, z }
}
#[cfg(vec3a_f32)]
{
v.0
}
}
}
impl From<Vec3> for Vec2 {
#[inline]
fn from(v: Vec3) -> Self {
Vec2 { x: v.x, y: v.y }
}
}
#[test]
fn test_vec3_private() {
assert_eq!(
vec3(1.0, 1.0, 1.0).mul_add(vec3(0.5, 2.0, -4.0), vec3(-1.0, -1.0, -1.0)),
vec3(-0.5, 1.0, -5.0)
);
}
#[cfg(feature = "std")]
impl<'a> Sum<&'a Self> for Vec3 {
fn sum<I>(iter: I) -> Self
where
I: Iterator<Item = &'a Self>,
{
iter.fold(ZERO, |a, &b| Self::add(a, b))
}
}
#[cfg(feature = "std")]
impl<'a> Product<&'a Self> for Vec3 {
fn product<I>(iter: I) -> Self
where
I: Iterator<Item = &'a Self>,
{
iter.fold(ONE, |a, &b| Self::mul(a, b))
}
}