Skip to content

Commit faa03b0

Browse files
authored
Merge pull request #1418 from godot-rust/perf/vector2i-no-ffi
Fix `Vector2i` using FFI; add functions to `Vector3i`/`Vector4i`
2 parents c37db08 + 52ba2e2 commit faa03b0

File tree

3 files changed

+89
-9
lines changed

3 files changed

+89
-9
lines changed

godot-codegen/src/special_cases/special_cases.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -676,14 +676,6 @@ pub fn is_builtin_method_exposed(builtin_ty: &TyName, godot_method_name: &str) -
676676
| ("PackedByteArray", "get_string_from_wchar")
677677
| ("PackedByteArray", "hex_encode")
678678

679-
// Vector2i
680-
| ("Vector2i", "clampi")
681-
| ("Vector2i", "distance_squared_to")
682-
| ("Vector2i", "distance_to")
683-
| ("Vector2i", "maxi")
684-
| ("Vector2i", "mini")
685-
| ("Vector2i", "snappedi")
686-
687679
=> true, _ => false
688680
}
689681
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ impl GlamConv for Vector2i {
124124
#[cfg(test)]
125125
mod test {
126126
use super::*;
127+
use crate::assert_eq_approx;
127128

128129
#[test]
129130
fn coord_min_max() {
@@ -161,4 +162,29 @@ mod test {
161162
assert_eq!(Vector2i::new(15, 15).max_axis(), None);
162163
assert_eq!(Vector2i::new(15, 15).min_axis(), None);
163164
}
165+
166+
#[test]
167+
fn distance() {
168+
let a = Vector2i::new(1, 2);
169+
let b = Vector2i::new(4, 6);
170+
171+
assert_eq!(a.distance_squared_to(b), 25);
172+
assert_eq_approx!(a.distance_to(b), 5.0);
173+
}
174+
175+
#[test]
176+
fn mini_maxi_clampi() {
177+
let v = Vector2i::new(10, -5);
178+
179+
assert_eq!(v.mini(3), Vector2i::new(3, -5));
180+
assert_eq!(v.maxi(-2), Vector2i::new(10, -2));
181+
assert_eq!(v.clampi(-3, 7), Vector2i::new(7, -3));
182+
}
183+
184+
#[test]
185+
fn snappedi() {
186+
let v = Vector2i::new(13, -8);
187+
188+
assert_eq!(v.snappedi(5), Vector2i::new(15, -10));
189+
}
164190
}

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

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,56 @@ macro_rules! inline_impl_integer_vector_fns {
538538
// Names of the components, for example `x, y`.
539539
$($comp:ident),*
540540
) => {
541+
/// Returns the distance between this vector and `to`.
542+
///
543+
/// Where possible, prefer [`distance_squared_to()`][Self::distance_squared_to] for precision and performance.
544+
#[inline]
545+
pub fn distance_to(self, to: Self) -> real {
546+
(to - self).length()
547+
}
548+
549+
/// Returns the squared distance between this vector and `to`.
550+
///
551+
/// Faster than [`distance_to()`][Self::distance_to], so prefer it if you need to compare distances, or need the squared distance
552+
/// in a formula.
553+
#[inline]
554+
pub fn distance_squared_to(self, to: Self) -> i32 {
555+
(to - self).length_squared() as i32
556+
}
557+
558+
/// Returns `self` with each component limited to a range defined by `min` and `max`.
559+
///
560+
/// # Panics
561+
/// If `min > max` on any axis.
562+
#[inline]
563+
pub fn clampi(self, min: i32, max: i32) -> Self {
564+
Self::new(
565+
$(
566+
self.$comp.clamp(min, max)
567+
),*
568+
)
569+
}
570+
571+
/// Returns a new vector with each component set to the minimum of `self` and `with`.
572+
#[inline]
573+
pub fn mini(self, with: i32) -> Self {
574+
Self::new(
575+
$(
576+
self.$comp.min(with)
577+
),*
578+
)
579+
}
580+
581+
/// Returns a new vector with each component set to the maximum of `self` and `with`.
582+
#[inline]
583+
pub fn maxi(self, with: i32) -> Self {
584+
Self::new(
585+
$(
586+
self.$comp.max(with)
587+
),*
588+
)
589+
}
590+
541591
/// A new vector with each component snapped to the closest multiple of the corresponding
542592
/// component in `step`.
543593
///
@@ -556,6 +606,15 @@ macro_rules! inline_impl_integer_vector_fns {
556606
)
557607
}
558608

609+
/// A new vector with each component snapped to the closest multiple of `step`.
610+
///
611+
/// # Panics
612+
/// On under- or overflow (see [`snapped()`][Self::snapped] for details).
613+
#[inline]
614+
pub fn snappedi(self, step: i32) -> Self {
615+
self.snapped(Self::splat(step))
616+
}
617+
559618
/// Converts to a vector with floating-point [`real`](type.real.html) components, using `as` casts.
560619
#[inline]
561620
pub const fn cast_float(self) -> $VectorFloat {
@@ -654,13 +713,16 @@ macro_rules! impl_float_vector_fns {
654713

655714
/// Returns the squared distance between this vector and `to`.
656715
///
657-
/// This method runs faster than [`Self::distance_to`], so prefer it if you need to compare vectors or need the squared distance for some formula.
716+
/// Faster than [`distance_to()`][Self::distance_to], so prefer it if you need to compare distances, or need the squared distance
717+
/// in a formula.
658718
#[inline]
659719
pub fn distance_squared_to(self, to: Self) -> real {
660720
(to - self).length_squared()
661721
}
662722

663723
/// Returns the distance between this vector and `to`.
724+
///
725+
/// Where possible, prefer [`distance_squared_to()`][Self::distance_squared_to] for performance reasons.
664726
#[inline]
665727
pub fn distance_to(self, to: Self) -> real {
666728
(to - self).length()

0 commit comments

Comments
 (0)