Skip to content

Commit b21f9ee

Browse files
committed
Refactor 4D vectors with new macros
1 parent 5b4821f commit b21f9ee

File tree

2 files changed

+28
-113
lines changed

2 files changed

+28
-113
lines changed

godot-core/src/builtin/vectors/vector4.rs

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use godot_ffi as sys;
99
use sys::{ffi_methods, GodotFfi};
1010

1111
use crate::builtin::math::{FloatExt, GlamConv, GlamType};
12-
use crate::builtin::{real, RVec4, Vector4i};
12+
use crate::builtin::{inner, real, RVec4, Vector4Axis, Vector4i};
1313

1414
use std::fmt;
1515

@@ -40,23 +40,17 @@ pub struct Vector4 {
4040
}
4141

4242
impl_vector_operators!(Vector4, real, (x, y, z, w));
43-
impl_common_vector_fns!(Vector4, real);
44-
impl_float_vector_glam_fns!(Vector4, real);
45-
impl_float_vector_component_fns!(Vector4, real, (x, y, z, w));
46-
impl_swizzle_trait_for_vector4x!(Vector4, real);
4743

48-
impl Vector4 {
49-
/// Returns a `Vector4` with the given components.
50-
pub const fn new(x: real, y: real, z: real, w: real) -> Self {
51-
Self { x, y, z, w }
52-
}
44+
impl_vector_consts!(Vector4, real);
45+
impl_float_vector_consts!(Vector4);
5346

54-
/// Returns a new `Vector4` with all components set to `v`.
55-
pub const fn splat(v: real) -> Self {
56-
Self::new(v, v, v, v)
57-
}
47+
impl_vector_fns!(Vector4, RVec4, real, (x, y, z, w));
48+
impl_float_vector_fns!(Vector4, (x, y, z, w));
49+
impl_vector4x_fns!(Vector4, real);
50+
impl_vector3_vector4_fns!(Vector4, (x, y, z, w));
5851

59-
/// Constructs a new `Vector3` from a [`Vector3i`][crate::builtin::Vector3i].
52+
impl Vector4 {
53+
/// Constructs a new `Vector4` from a [`Vector4i`][crate::builtin::Vector4i].
6054
pub const fn from_vector4i(v: Vector4i) -> Self {
6155
Self {
6256
x: v.x as real,
@@ -66,27 +60,10 @@ impl Vector4 {
6660
}
6761
}
6862

69-
/// Zero vector, a vector with all components set to `0.0`.
70-
pub const ZERO: Self = Self::splat(0.0);
71-
72-
/// One vector, a vector with all components set to `1.0`.
73-
pub const ONE: Self = Self::splat(1.0);
74-
75-
/// Infinity vector, a vector with all components set to `real::INFINITY`.
76-
pub const INF: Self = Self::splat(real::INFINITY);
77-
78-
/// Converts the corresponding `glam` type to `Self`.
79-
fn from_glam(v: RVec4) -> Self {
80-
Self::new(v.x, v.y, v.z, v.w)
81-
}
82-
83-
/// Converts `self` to the corresponding `glam` type.
84-
fn to_glam(self) -> RVec4 {
85-
RVec4::new(self.x, self.y, self.z, self.w)
86-
}
87-
88-
pub fn coords(&self) -> (real, real, real, real) {
89-
(self.x, self.y, self.z, self.w)
63+
#[doc(hidden)]
64+
#[inline]
65+
pub fn as_inner(&self) -> inner::InnerVector4 {
66+
inner::InnerVector4::from_outer(self)
9067
}
9168
}
9269

godot-core/src/builtin/vectors/vector4i.rs

Lines changed: 15 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
use godot_ffi as sys;
99
use sys::{ffi_methods, GodotFfi};
1010

11-
use crate::builtin::math::{FloatExt, GlamConv, GlamType};
12-
use crate::builtin::{real, RVec4, Vector4, Vector4Axis};
11+
use crate::builtin::math::{GlamConv, GlamType};
12+
use crate::builtin::{inner, real, RVec4, Vector4, Vector4Axis};
1313

1414
use std::fmt;
1515

@@ -39,67 +39,18 @@ pub struct Vector4i {
3939
}
4040

4141
impl_vector_operators!(Vector4i, i32, (x, y, z, w));
42-
impl_integer_vector_glam_fns!(Vector4i, real);
43-
impl_integer_vector_component_fns!(Vector4i, real, (x, y, z, w));
44-
impl_common_vector_fns!(Vector4i, i32);
45-
impl_swizzle_trait_for_vector4x!(Vector4i, i32);
4642

47-
impl Vector4i {
48-
/// Returns a `Vector4i` with the given components.
49-
pub const fn new(x: i32, y: i32, z: i32, w: i32) -> Self {
50-
Self { x, y, z, w }
51-
}
52-
53-
/// Axis of the vector's highest value. [`None`] if at least two components are equal.
54-
pub fn max_axis(self) -> Option<Vector4Axis> {
55-
use Vector4Axis::*;
56-
57-
let mut max_axis = X;
58-
let mut previous = None;
59-
let mut max_value = self.x;
60-
61-
let components = [(Y, self.y), (Z, self.z), (W, self.w)];
62-
63-
for (axis, value) in components {
64-
if value >= max_value {
65-
max_axis = axis;
66-
previous = Some(max_value);
67-
max_value = value;
68-
}
69-
}
70-
71-
(Some(max_value) != previous).then_some(max_axis)
72-
}
73-
74-
/// Axis of the vector's highest value. [`None`] if at least two components are equal.
75-
pub fn min_axis(self) -> Option<Vector4Axis> {
76-
use Vector4Axis::*;
77-
78-
let mut min_axis = X;
79-
let mut previous = None;
80-
let mut min_value = self.x;
81-
82-
let components = [(Y, self.y), (Z, self.z), (W, self.w)];
43+
impl_vector_consts!(Vector4i, i32);
44+
impl_integer_vector_consts!(Vector4i);
8345

84-
for (axis, value) in components {
85-
if value <= min_value {
86-
min_axis = axis;
87-
previous = Some(min_value);
88-
min_value = value;
89-
}
90-
}
91-
92-
(Some(min_value) != previous).then_some(min_axis)
93-
}
94-
95-
/// Constructs a new `Vector4i` with all components set to `v`.
96-
pub const fn splat(v: i32) -> Self {
97-
Self::new(v, v, v, v)
98-
}
46+
impl_vector_fns!(Vector4i, glam::IVec4, i32, (x, y, z, w));
47+
impl_vector4x_fns!(Vector4i, i32);
9948

49+
impl Vector4i {
10050
/// Constructs a new `Vector4i` from a [`Vector4`]. The floating point coordinates will be
10151
/// truncated.
102-
pub const fn from_vector3(v: Vector4) -> Self {
52+
#[inline]
53+
pub const fn from_vector4(v: Vector4) -> Self {
10354
Self {
10455
x: v.x as i32,
10556
y: v.y as i32,
@@ -108,24 +59,9 @@ impl Vector4i {
10859
}
10960
}
11061

111-
/// Zero vector, a vector with all components set to `0`.
112-
pub const ZERO: Self = Self::splat(0);
113-
114-
/// One vector, a vector with all components set to `1`.
115-
pub const ONE: Self = Self::splat(1);
116-
117-
/// Converts the corresponding `glam` type to `Self`.
118-
fn from_glam(v: glam::IVec4) -> Self {
119-
Self::new(v.x, v.y, v.z, v.w)
120-
}
121-
122-
/// Converts `self` to the corresponding `glam` type.
123-
fn to_glam(self) -> glam::IVec4 {
124-
glam::IVec4::new(self.x, self.y, self.z, self.w)
125-
}
126-
12762
/// Converts `self` to the corresponding [`real`] `glam` type.
128-
fn to_glam_real(self) -> RVec4 {
63+
#[inline]
64+
pub fn to_glam_real(self) -> RVec4 {
12965
RVec4::new(
13066
self.x as real,
13167
self.y as real,
@@ -134,8 +70,10 @@ impl Vector4i {
13470
)
13571
}
13672

137-
pub fn coords(&self) -> (i32, i32, i32, i32) {
138-
(self.x, self.y, self.z, self.w)
73+
#[doc(hidden)]
74+
#[inline]
75+
pub fn as_inner(&self) -> inner::InnerVector4i {
76+
inner::InnerVector4i::from_outer(self)
13977
}
14078
}
14179

0 commit comments

Comments
 (0)