Skip to content

Commit

Permalink
Refactor 4D vectors with new macros
Browse files Browse the repository at this point in the history
  • Loading branch information
joriskleiber committed May 29, 2024
1 parent 2c04101 commit 2323e19
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 113 deletions.
49 changes: 13 additions & 36 deletions godot-core/src/builtin/vectors/vector4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use sys::{ffi_methods, GodotFfi};

use crate::builtin::math::{FloatExt, GlamConv, GlamType};
use crate::builtin::meta::impl_godot_as_self;
use crate::builtin::{real, RVec4, Vector4i};
use crate::builtin::{inner, real, RVec4, Vector4Axis, Vector4i};

use std::fmt;

Expand Down Expand Up @@ -41,23 +41,17 @@ pub struct Vector4 {
}

impl_vector_operators!(Vector4, real, (x, y, z, w));
impl_common_vector_fns!(Vector4, real);
impl_float_vector_glam_fns!(Vector4, real);
impl_float_vector_component_fns!(Vector4, real, (x, y, z, w));
impl_swizzle_trait_for_vector4x!(Vector4, real);

impl Vector4 {
/// Returns a `Vector4` with the given components.
pub const fn new(x: real, y: real, z: real, w: real) -> Self {
Self { x, y, z, w }
}
impl_vector_consts!(Vector4, real);
impl_float_vector_consts!(Vector4);

/// Returns a new `Vector4` with all components set to `v`.
pub const fn splat(v: real) -> Self {
Self::new(v, v, v, v)
}
impl_vector_fns!(Vector4, RVec4, real, (x, y, z, w));
impl_float_vector_fns!(Vector4, (x, y, z, w));
impl_vector4x_fns!(Vector4, real);
impl_vector3_vector4_fns!(Vector4, (x, y, z, w));

/// Constructs a new `Vector3` from a [`Vector3i`][crate::builtin::Vector3i].
impl Vector4 {
/// Constructs a new `Vector4` from a [`Vector4i`][crate::builtin::Vector4i].
pub const fn from_vector4i(v: Vector4i) -> Self {
Self {
x: v.x as real,
Expand All @@ -67,27 +61,10 @@ impl Vector4 {
}
}

/// Zero vector, a vector with all components set to `0.0`.
pub const ZERO: Self = Self::splat(0.0);

/// One vector, a vector with all components set to `1.0`.
pub const ONE: Self = Self::splat(1.0);

/// Infinity vector, a vector with all components set to `real::INFINITY`.
pub const INF: Self = Self::splat(real::INFINITY);

/// Converts the corresponding `glam` type to `Self`.
fn from_glam(v: RVec4) -> Self {
Self::new(v.x, v.y, v.z, v.w)
}

/// Converts `self` to the corresponding `glam` type.
fn to_glam(self) -> RVec4 {
RVec4::new(self.x, self.y, self.z, self.w)
}

pub fn coords(&self) -> (real, real, real, real) {
(self.x, self.y, self.z, self.w)
#[doc(hidden)]
#[inline]
pub fn as_inner(&self) -> inner::InnerVector4 {
inner::InnerVector4::from_outer(self)
}
}

Expand Down
92 changes: 15 additions & 77 deletions godot-core/src/builtin/vectors/vector4i.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
use godot_ffi as sys;
use sys::{ffi_methods, GodotFfi};

use crate::builtin::math::{FloatExt, GlamConv, GlamType};
use crate::builtin::math::{GlamConv, GlamType};
use crate::builtin::meta::impl_godot_as_self;
use crate::builtin::{real, RVec4, Vector4, Vector4Axis};
use crate::builtin::{inner, real, RVec4, Vector4, Vector4Axis};

use std::fmt;

Expand Down Expand Up @@ -40,67 +40,18 @@ pub struct Vector4i {
}

impl_vector_operators!(Vector4i, i32, (x, y, z, w));
impl_integer_vector_glam_fns!(Vector4i, real);
impl_integer_vector_component_fns!(Vector4i, real, (x, y, z, w));
impl_common_vector_fns!(Vector4i, i32);
impl_swizzle_trait_for_vector4x!(Vector4i, i32);

impl Vector4i {
/// Returns a `Vector4i` with the given components.
pub const fn new(x: i32, y: i32, z: i32, w: i32) -> Self {
Self { x, y, z, w }
}

/// Axis of the vector's highest value. [`None`] if at least two components are equal.
pub fn max_axis(self) -> Option<Vector4Axis> {
use Vector4Axis::*;

let mut max_axis = X;
let mut previous = None;
let mut max_value = self.x;

let components = [(Y, self.y), (Z, self.z), (W, self.w)];

for (axis, value) in components {
if value >= max_value {
max_axis = axis;
previous = Some(max_value);
max_value = value;
}
}

(Some(max_value) != previous).then_some(max_axis)
}

/// Axis of the vector's highest value. [`None`] if at least two components are equal.
pub fn min_axis(self) -> Option<Vector4Axis> {
use Vector4Axis::*;

let mut min_axis = X;
let mut previous = None;
let mut min_value = self.x;

let components = [(Y, self.y), (Z, self.z), (W, self.w)];
impl_vector_consts!(Vector4i, i32);
impl_integer_vector_consts!(Vector4i);

for (axis, value) in components {
if value <= min_value {
min_axis = axis;
previous = Some(min_value);
min_value = value;
}
}

(Some(min_value) != previous).then_some(min_axis)
}

/// Constructs a new `Vector4i` with all components set to `v`.
pub const fn splat(v: i32) -> Self {
Self::new(v, v, v, v)
}
impl_vector_fns!(Vector4i, glam::IVec4, i32, (x, y, z, w));
impl_vector4x_fns!(Vector4i, i32);

impl Vector4i {
/// Constructs a new `Vector4i` from a [`Vector4`]. The floating point coordinates will be
/// truncated.
pub const fn from_vector3(v: Vector4) -> Self {
#[inline]
pub const fn from_vector4(v: Vector4) -> Self {
Self {
x: v.x as i32,
y: v.y as i32,
Expand All @@ -109,24 +60,9 @@ impl Vector4i {
}
}

/// Zero vector, a vector with all components set to `0`.
pub const ZERO: Self = Self::splat(0);

/// One vector, a vector with all components set to `1`.
pub const ONE: Self = Self::splat(1);

/// Converts the corresponding `glam` type to `Self`.
fn from_glam(v: glam::IVec4) -> Self {
Self::new(v.x, v.y, v.z, v.w)
}

/// Converts `self` to the corresponding `glam` type.
fn to_glam(self) -> glam::IVec4 {
glam::IVec4::new(self.x, self.y, self.z, self.w)
}

/// Converts `self` to the corresponding [`real`] `glam` type.
fn to_glam_real(self) -> RVec4 {
#[inline]
pub fn to_glam_real(self) -> RVec4 {
RVec4::new(
self.x as real,
self.y as real,
Expand All @@ -135,8 +71,10 @@ impl Vector4i {
)
}

pub fn coords(&self) -> (i32, i32, i32, i32) {
(self.x, self.y, self.z, self.w)
#[doc(hidden)]
#[inline]
pub fn as_inner(&self) -> inner::InnerVector4i {
inner::InnerVector4i::from_outer(self)
}
}

Expand Down

0 comments on commit 2323e19

Please sign in to comment.