Skip to content

Commit 2323e19

Browse files
committed
Refactor 4D vectors with new macros
1 parent 2c04101 commit 2323e19

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
@@ -10,7 +10,7 @@ use sys::{ffi_methods, GodotFfi};
1010

1111
use crate::builtin::math::{FloatExt, GlamConv, GlamType};
1212
use crate::builtin::meta::impl_godot_as_self;
13-
use crate::builtin::{real, RVec4, Vector4i};
13+
use crate::builtin::{inner, real, RVec4, Vector4Axis, Vector4i};
1414

1515
use std::fmt;
1616

@@ -41,23 +41,17 @@ pub struct Vector4 {
4141
}
4242

4343
impl_vector_operators!(Vector4, real, (x, y, z, w));
44-
impl_common_vector_fns!(Vector4, real);
45-
impl_float_vector_glam_fns!(Vector4, real);
46-
impl_float_vector_component_fns!(Vector4, real, (x, y, z, w));
47-
impl_swizzle_trait_for_vector4x!(Vector4, real);
4844

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

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

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

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

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

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

11-
use crate::builtin::math::{FloatExt, GlamConv, GlamType};
11+
use crate::builtin::math::{GlamConv, GlamType};
1212
use crate::builtin::meta::impl_godot_as_self;
13-
use crate::builtin::{real, RVec4, Vector4, Vector4Axis};
13+
use crate::builtin::{inner, real, RVec4, Vector4, Vector4Axis};
1414

1515
use std::fmt;
1616

@@ -40,67 +40,18 @@ pub struct Vector4i {
4040
}
4141

4242
impl_vector_operators!(Vector4i, i32, (x, y, z, w));
43-
impl_integer_vector_glam_fns!(Vector4i, real);
44-
impl_integer_vector_component_fns!(Vector4i, real, (x, y, z, w));
45-
impl_common_vector_fns!(Vector4i, i32);
46-
impl_swizzle_trait_for_vector4x!(Vector4i, i32);
4743

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

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

50+
impl Vector4i {
10151
/// Constructs a new `Vector4i` from a [`Vector4`]. The floating point coordinates will be
10252
/// truncated.
103-
pub const fn from_vector3(v: Vector4) -> Self {
53+
#[inline]
54+
pub const fn from_vector4(v: Vector4) -> Self {
10455
Self {
10556
x: v.x as i32,
10657
y: v.y as i32,
@@ -109,24 +60,9 @@ impl Vector4i {
10960
}
11061
}
11162

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

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

0 commit comments

Comments
 (0)