From 0a2b75c9ef723efef328efc97932a44d5bd760e1 Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Fri, 20 Dec 2024 16:22:11 +0100 Subject: [PATCH 1/8] implement manhattan distance for integer vec --- codegen/templates/vec.rs.tera | 17 +++++++++++++++++ src/i16/i16vec2.rs | 8 ++++++++ src/i16/i16vec3.rs | 8 ++++++++ src/i16/i16vec4.rs | 11 +++++++++++ src/i32/ivec2.rs | 8 ++++++++ src/i32/ivec3.rs | 8 ++++++++ src/i32/ivec4.rs | 11 +++++++++++ src/i64/i64vec2.rs | 8 ++++++++ src/i64/i64vec3.rs | 8 ++++++++ src/i64/i64vec4.rs | 11 +++++++++++ src/i8/i8vec2.rs | 8 ++++++++ src/i8/i8vec3.rs | 8 ++++++++ src/i8/i8vec4.rs | 11 +++++++++++ src/u16/u16vec2.rs | 8 ++++++++ src/u16/u16vec3.rs | 8 ++++++++ src/u16/u16vec4.rs | 11 +++++++++++ src/u32/uvec2.rs | 8 ++++++++ src/u32/uvec3.rs | 8 ++++++++ src/u32/uvec4.rs | 11 +++++++++++ src/u64/u64vec2.rs | 8 ++++++++ src/u64/u64vec3.rs | 8 ++++++++ src/u64/u64vec4.rs | 11 +++++++++++ src/u8/u8vec2.rs | 8 ++++++++ src/u8/u8vec3.rs | 8 ++++++++ src/u8/u8vec4.rs | 11 +++++++++++ 25 files changed, 233 insertions(+) diff --git a/codegen/templates/vec.rs.tera b/codegen/templates/vec.rs.tera index 468cf321..5f3e854e 100644 --- a/codegen/templates/vec.rs.tera +++ b/codegen/templates/vec.rs.tera @@ -2166,6 +2166,23 @@ impl {{ self_t }} { {% endif %} {% endif %} +{% if not is_float %} + + /// Computes the [manhattan distance] between two points. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + #[must_use] + pub fn manhattan_distance(self, other: Self) -> {{ scalar_t }} { + {% for c in components %} + {% if not loop.first %} + {% endif %} + self.{{c}}.abs_diff(other.{{c}}) + {% endfor %} + } + +{% endif %} + + {% if is_signed and dim == 2 %} /// Returns a vector that is equal to `self` rotated by 90 degrees. #[inline] diff --git a/src/i16/i16vec2.rs b/src/i16/i16vec2.rs index 0e58cfe3..2f1f34db 100644 --- a/src/i16/i16vec2.rs +++ b/src/i16/i16vec2.rs @@ -377,6 +377,14 @@ impl I16Vec2 { Self::new(self.x.rem_euclid(rhs.x), self.y.rem_euclid(rhs.y)) } + /// Computes the [manhattan distance] between two points. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + pub fn manhattan_distance(self, other: Self) -> i16 { + self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + } + /// Returns a vector that is equal to `self` rotated by 90 degrees. #[inline] #[must_use] diff --git a/src/i16/i16vec3.rs b/src/i16/i16vec3.rs index b435754f..8c5d05bd 100644 --- a/src/i16/i16vec3.rs +++ b/src/i16/i16vec3.rs @@ -441,6 +441,14 @@ impl I16Vec3 { ) } + /// Computes the [manhattan distance] between two points. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + pub fn manhattan_distance(self, other: Self) -> i16 { + self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/i16/i16vec4.rs b/src/i16/i16vec4.rs index 5b2bfa4f..7425740f 100644 --- a/src/i16/i16vec4.rs +++ b/src/i16/i16vec4.rs @@ -473,6 +473,17 @@ impl I16Vec4 { ) } + /// Computes the [manhattan distance] between two points. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + pub fn manhattan_distance(self, other: Self) -> i16 { + self.x.abs_diff(other.x) + + self.y.abs_diff(other.y) + + self.z.abs_diff(other.z) + + self.w.abs_diff(other.w) + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/i32/ivec2.rs b/src/i32/ivec2.rs index 90f45cfb..2cb5a8e4 100644 --- a/src/i32/ivec2.rs +++ b/src/i32/ivec2.rs @@ -377,6 +377,14 @@ impl IVec2 { Self::new(self.x.rem_euclid(rhs.x), self.y.rem_euclid(rhs.y)) } + /// Computes the [manhattan distance] between two points. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + pub fn manhattan_distance(self, other: Self) -> i32 { + self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + } + /// Returns a vector that is equal to `self` rotated by 90 degrees. #[inline] #[must_use] diff --git a/src/i32/ivec3.rs b/src/i32/ivec3.rs index 2c171029..fcf873d3 100644 --- a/src/i32/ivec3.rs +++ b/src/i32/ivec3.rs @@ -441,6 +441,14 @@ impl IVec3 { ) } + /// Computes the [manhattan distance] between two points. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + pub fn manhattan_distance(self, other: Self) -> i32 { + self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/i32/ivec4.rs b/src/i32/ivec4.rs index a69f6ca0..067c1464 100644 --- a/src/i32/ivec4.rs +++ b/src/i32/ivec4.rs @@ -473,6 +473,17 @@ impl IVec4 { ) } + /// Computes the [manhattan distance] between two points. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + pub fn manhattan_distance(self, other: Self) -> i32 { + self.x.abs_diff(other.x) + + self.y.abs_diff(other.y) + + self.z.abs_diff(other.z) + + self.w.abs_diff(other.w) + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/i64/i64vec2.rs b/src/i64/i64vec2.rs index 9a4c554b..bbecd8eb 100644 --- a/src/i64/i64vec2.rs +++ b/src/i64/i64vec2.rs @@ -377,6 +377,14 @@ impl I64Vec2 { Self::new(self.x.rem_euclid(rhs.x), self.y.rem_euclid(rhs.y)) } + /// Computes the [manhattan distance] between two points. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + pub fn manhattan_distance(self, other: Self) -> i64 { + self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + } + /// Returns a vector that is equal to `self` rotated by 90 degrees. #[inline] #[must_use] diff --git a/src/i64/i64vec3.rs b/src/i64/i64vec3.rs index dc46af03..2d98a484 100644 --- a/src/i64/i64vec3.rs +++ b/src/i64/i64vec3.rs @@ -441,6 +441,14 @@ impl I64Vec3 { ) } + /// Computes the [manhattan distance] between two points. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + pub fn manhattan_distance(self, other: Self) -> i64 { + self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/i64/i64vec4.rs b/src/i64/i64vec4.rs index ce35d831..05f9c6fe 100644 --- a/src/i64/i64vec4.rs +++ b/src/i64/i64vec4.rs @@ -473,6 +473,17 @@ impl I64Vec4 { ) } + /// Computes the [manhattan distance] between two points. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + pub fn manhattan_distance(self, other: Self) -> i64 { + self.x.abs_diff(other.x) + + self.y.abs_diff(other.y) + + self.z.abs_diff(other.z) + + self.w.abs_diff(other.w) + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/i8/i8vec2.rs b/src/i8/i8vec2.rs index 398760c5..dc5f68e4 100644 --- a/src/i8/i8vec2.rs +++ b/src/i8/i8vec2.rs @@ -377,6 +377,14 @@ impl I8Vec2 { Self::new(self.x.rem_euclid(rhs.x), self.y.rem_euclid(rhs.y)) } + /// Computes the [manhattan distance] between two points. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + pub fn manhattan_distance(self, other: Self) -> i8 { + self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + } + /// Returns a vector that is equal to `self` rotated by 90 degrees. #[inline] #[must_use] diff --git a/src/i8/i8vec3.rs b/src/i8/i8vec3.rs index 382b9a85..b430a0e6 100644 --- a/src/i8/i8vec3.rs +++ b/src/i8/i8vec3.rs @@ -441,6 +441,14 @@ impl I8Vec3 { ) } + /// Computes the [manhattan distance] between two points. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + pub fn manhattan_distance(self, other: Self) -> i8 { + self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/i8/i8vec4.rs b/src/i8/i8vec4.rs index 2eb4531c..3cfe5a70 100644 --- a/src/i8/i8vec4.rs +++ b/src/i8/i8vec4.rs @@ -473,6 +473,17 @@ impl I8Vec4 { ) } + /// Computes the [manhattan distance] between two points. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + pub fn manhattan_distance(self, other: Self) -> i8 { + self.x.abs_diff(other.x) + + self.y.abs_diff(other.y) + + self.z.abs_diff(other.z) + + self.w.abs_diff(other.w) + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/u16/u16vec2.rs b/src/u16/u16vec2.rs index 67d74bef..9f3d4b96 100644 --- a/src/u16/u16vec2.rs +++ b/src/u16/u16vec2.rs @@ -305,6 +305,14 @@ impl U16Vec2 { self.dot(self) } + /// Computes the [manhattan distance] between two points. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + pub fn manhattan_distance(self, other: Self) -> u16 { + self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/u16/u16vec3.rs b/src/u16/u16vec3.rs index 43e00234..0f474df4 100644 --- a/src/u16/u16vec3.rs +++ b/src/u16/u16vec3.rs @@ -354,6 +354,14 @@ impl U16Vec3 { self.dot(self) } + /// Computes the [manhattan distance] between two points. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + pub fn manhattan_distance(self, other: Self) -> u16 { + self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/u16/u16vec4.rs b/src/u16/u16vec4.rs index 84121fde..09a857bf 100644 --- a/src/u16/u16vec4.rs +++ b/src/u16/u16vec4.rs @@ -378,6 +378,17 @@ impl U16Vec4 { self.dot(self) } + /// Computes the [manhattan distance] between two points. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + pub fn manhattan_distance(self, other: Self) -> u16 { + self.x.abs_diff(other.x) + + self.y.abs_diff(other.y) + + self.z.abs_diff(other.z) + + self.w.abs_diff(other.w) + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/u32/uvec2.rs b/src/u32/uvec2.rs index ca91ef06..0462e58e 100644 --- a/src/u32/uvec2.rs +++ b/src/u32/uvec2.rs @@ -305,6 +305,14 @@ impl UVec2 { self.dot(self) } + /// Computes the [manhattan distance] between two points. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + pub fn manhattan_distance(self, other: Self) -> u32 { + self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/u32/uvec3.rs b/src/u32/uvec3.rs index 28fe407a..0e1c15ec 100644 --- a/src/u32/uvec3.rs +++ b/src/u32/uvec3.rs @@ -354,6 +354,14 @@ impl UVec3 { self.dot(self) } + /// Computes the [manhattan distance] between two points. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + pub fn manhattan_distance(self, other: Self) -> u32 { + self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/u32/uvec4.rs b/src/u32/uvec4.rs index d997f5b5..6d17e7bd 100644 --- a/src/u32/uvec4.rs +++ b/src/u32/uvec4.rs @@ -378,6 +378,17 @@ impl UVec4 { self.dot(self) } + /// Computes the [manhattan distance] between two points. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + pub fn manhattan_distance(self, other: Self) -> u32 { + self.x.abs_diff(other.x) + + self.y.abs_diff(other.y) + + self.z.abs_diff(other.z) + + self.w.abs_diff(other.w) + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/u64/u64vec2.rs b/src/u64/u64vec2.rs index fba69e0c..056bc0d9 100644 --- a/src/u64/u64vec2.rs +++ b/src/u64/u64vec2.rs @@ -305,6 +305,14 @@ impl U64Vec2 { self.dot(self) } + /// Computes the [manhattan distance] between two points. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + pub fn manhattan_distance(self, other: Self) -> u64 { + self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/u64/u64vec3.rs b/src/u64/u64vec3.rs index 3607882a..342f3e19 100644 --- a/src/u64/u64vec3.rs +++ b/src/u64/u64vec3.rs @@ -354,6 +354,14 @@ impl U64Vec3 { self.dot(self) } + /// Computes the [manhattan distance] between two points. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + pub fn manhattan_distance(self, other: Self) -> u64 { + self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/u64/u64vec4.rs b/src/u64/u64vec4.rs index 0772b525..48cd54f5 100644 --- a/src/u64/u64vec4.rs +++ b/src/u64/u64vec4.rs @@ -378,6 +378,17 @@ impl U64Vec4 { self.dot(self) } + /// Computes the [manhattan distance] between two points. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + pub fn manhattan_distance(self, other: Self) -> u64 { + self.x.abs_diff(other.x) + + self.y.abs_diff(other.y) + + self.z.abs_diff(other.z) + + self.w.abs_diff(other.w) + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/u8/u8vec2.rs b/src/u8/u8vec2.rs index cc6decff..314ff5e1 100644 --- a/src/u8/u8vec2.rs +++ b/src/u8/u8vec2.rs @@ -305,6 +305,14 @@ impl U8Vec2 { self.dot(self) } + /// Computes the [manhattan distance] between two points. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + pub fn manhattan_distance(self, other: Self) -> u8 { + self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/u8/u8vec3.rs b/src/u8/u8vec3.rs index 752c12fa..13ab9657 100644 --- a/src/u8/u8vec3.rs +++ b/src/u8/u8vec3.rs @@ -354,6 +354,14 @@ impl U8Vec3 { self.dot(self) } + /// Computes the [manhattan distance] between two points. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + pub fn manhattan_distance(self, other: Self) -> u8 { + self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/u8/u8vec4.rs b/src/u8/u8vec4.rs index 8dbe4fc8..05947a15 100644 --- a/src/u8/u8vec4.rs +++ b/src/u8/u8vec4.rs @@ -378,6 +378,17 @@ impl U8Vec4 { self.dot(self) } + /// Computes the [manhattan distance] between two points. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + pub fn manhattan_distance(self, other: Self) -> u8 { + self.x.abs_diff(other.x) + + self.y.abs_diff(other.y) + + self.z.abs_diff(other.z) + + self.w.abs_diff(other.w) + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] From 41c0ef7b55c4e59d310d7f0a0c4909504ccc320e Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Fri, 20 Dec 2024 16:28:45 +0100 Subject: [PATCH 2/8] implement chebyshev distance for integer vectors --- codegen/templates/vec.rs.tera | 14 ++++++++++++++ src/i16/i16vec2.rs | 14 ++++++++++++++ src/i16/i16vec3.rs | 18 ++++++++++++++++++ src/i16/i16vec4.rs | 19 +++++++++++++++++++ src/i32/ivec2.rs | 14 ++++++++++++++ src/i32/ivec3.rs | 18 ++++++++++++++++++ src/i32/ivec4.rs | 19 +++++++++++++++++++ src/i64/i64vec2.rs | 14 ++++++++++++++ src/i64/i64vec3.rs | 18 ++++++++++++++++++ src/i64/i64vec4.rs | 19 +++++++++++++++++++ src/i8/i8vec2.rs | 14 ++++++++++++++ src/i8/i8vec3.rs | 18 ++++++++++++++++++ src/i8/i8vec4.rs | 19 +++++++++++++++++++ src/u16/u16vec2.rs | 14 ++++++++++++++ src/u16/u16vec3.rs | 18 ++++++++++++++++++ src/u16/u16vec4.rs | 19 +++++++++++++++++++ src/u32/uvec2.rs | 14 ++++++++++++++ src/u32/uvec3.rs | 18 ++++++++++++++++++ src/u32/uvec4.rs | 19 +++++++++++++++++++ src/u64/u64vec2.rs | 14 ++++++++++++++ src/u64/u64vec3.rs | 18 ++++++++++++++++++ src/u64/u64vec4.rs | 19 +++++++++++++++++++ src/u8/u8vec2.rs | 14 ++++++++++++++ src/u8/u8vec3.rs | 18 ++++++++++++++++++ src/u8/u8vec4.rs | 19 +++++++++++++++++++ 25 files changed, 422 insertions(+) diff --git a/codegen/templates/vec.rs.tera b/codegen/templates/vec.rs.tera index 5f3e854e..bc79f7df 100644 --- a/codegen/templates/vec.rs.tera +++ b/codegen/templates/vec.rs.tera @@ -2180,6 +2180,20 @@ impl {{ self_t }} { {% endfor %} } + /// Computes the [chebyshev distance] between two points. + /// + /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance + #[inline] + #[must_use] + pub fn chebyshev_distance(self, other: Self) -> {{ scalar_t }} { + // Note: the compiler will eventually optimize out the loop + [ + {% for c in components %} + self.{{c}}.abs_diff(other.{{c}}), + {% endfor %} + ].into_iter().max().unwrap() + } + {% endif %} diff --git a/src/i16/i16vec2.rs b/src/i16/i16vec2.rs index 2f1f34db..38c15650 100644 --- a/src/i16/i16vec2.rs +++ b/src/i16/i16vec2.rs @@ -381,10 +381,24 @@ impl I16Vec2 { /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] + #[must_use] pub fn manhattan_distance(self, other: Self) -> i16 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) } + /// Computes the [chebyshev distance] between two points. + /// + /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance + #[inline] + #[must_use] + pub fn chebyshev_distance(self, other: Self) -> i16 { + // Note: the compiler will eventually optimize out the loop + [self.x.abs_diff(other.x), self.y.abs_diff(other.y)] + .into_iter() + .max() + .unwrap() + } + /// Returns a vector that is equal to `self` rotated by 90 degrees. #[inline] #[must_use] diff --git a/src/i16/i16vec3.rs b/src/i16/i16vec3.rs index 8c5d05bd..afa455a4 100644 --- a/src/i16/i16vec3.rs +++ b/src/i16/i16vec3.rs @@ -445,10 +445,28 @@ impl I16Vec3 { /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] + #[must_use] pub fn manhattan_distance(self, other: Self) -> i16 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) } + /// Computes the [chebyshev distance] between two points. + /// + /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance + #[inline] + #[must_use] + pub fn chebyshev_distance(self, other: Self) -> i16 { + // Note: the compiler will eventually optimize out the loop + [ + self.x.abs_diff(other.x), + self.y.abs_diff(other.y), + self.z.abs_diff(other.z), + ] + .into_iter() + .max() + .unwrap() + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/i16/i16vec4.rs b/src/i16/i16vec4.rs index 7425740f..2550a21c 100644 --- a/src/i16/i16vec4.rs +++ b/src/i16/i16vec4.rs @@ -477,6 +477,7 @@ impl I16Vec4 { /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] + #[must_use] pub fn manhattan_distance(self, other: Self) -> i16 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) @@ -484,6 +485,24 @@ impl I16Vec4 { + self.w.abs_diff(other.w) } + /// Computes the [chebyshev distance] between two points. + /// + /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance + #[inline] + #[must_use] + pub fn chebyshev_distance(self, other: Self) -> i16 { + // Note: the compiler will eventually optimize out the loop + [ + self.x.abs_diff(other.x), + self.y.abs_diff(other.y), + self.z.abs_diff(other.z), + self.w.abs_diff(other.w), + ] + .into_iter() + .max() + .unwrap() + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/i32/ivec2.rs b/src/i32/ivec2.rs index 2cb5a8e4..1a639079 100644 --- a/src/i32/ivec2.rs +++ b/src/i32/ivec2.rs @@ -381,10 +381,24 @@ impl IVec2 { /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] + #[must_use] pub fn manhattan_distance(self, other: Self) -> i32 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) } + /// Computes the [chebyshev distance] between two points. + /// + /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance + #[inline] + #[must_use] + pub fn chebyshev_distance(self, other: Self) -> i32 { + // Note: the compiler will eventually optimize out the loop + [self.x.abs_diff(other.x), self.y.abs_diff(other.y)] + .into_iter() + .max() + .unwrap() + } + /// Returns a vector that is equal to `self` rotated by 90 degrees. #[inline] #[must_use] diff --git a/src/i32/ivec3.rs b/src/i32/ivec3.rs index fcf873d3..8a8d723a 100644 --- a/src/i32/ivec3.rs +++ b/src/i32/ivec3.rs @@ -445,10 +445,28 @@ impl IVec3 { /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] + #[must_use] pub fn manhattan_distance(self, other: Self) -> i32 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) } + /// Computes the [chebyshev distance] between two points. + /// + /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance + #[inline] + #[must_use] + pub fn chebyshev_distance(self, other: Self) -> i32 { + // Note: the compiler will eventually optimize out the loop + [ + self.x.abs_diff(other.x), + self.y.abs_diff(other.y), + self.z.abs_diff(other.z), + ] + .into_iter() + .max() + .unwrap() + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/i32/ivec4.rs b/src/i32/ivec4.rs index 067c1464..bd069f5a 100644 --- a/src/i32/ivec4.rs +++ b/src/i32/ivec4.rs @@ -477,6 +477,7 @@ impl IVec4 { /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] + #[must_use] pub fn manhattan_distance(self, other: Self) -> i32 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) @@ -484,6 +485,24 @@ impl IVec4 { + self.w.abs_diff(other.w) } + /// Computes the [chebyshev distance] between two points. + /// + /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance + #[inline] + #[must_use] + pub fn chebyshev_distance(self, other: Self) -> i32 { + // Note: the compiler will eventually optimize out the loop + [ + self.x.abs_diff(other.x), + self.y.abs_diff(other.y), + self.z.abs_diff(other.z), + self.w.abs_diff(other.w), + ] + .into_iter() + .max() + .unwrap() + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/i64/i64vec2.rs b/src/i64/i64vec2.rs index bbecd8eb..97fdf3d4 100644 --- a/src/i64/i64vec2.rs +++ b/src/i64/i64vec2.rs @@ -381,10 +381,24 @@ impl I64Vec2 { /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] + #[must_use] pub fn manhattan_distance(self, other: Self) -> i64 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) } + /// Computes the [chebyshev distance] between two points. + /// + /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance + #[inline] + #[must_use] + pub fn chebyshev_distance(self, other: Self) -> i64 { + // Note: the compiler will eventually optimize out the loop + [self.x.abs_diff(other.x), self.y.abs_diff(other.y)] + .into_iter() + .max() + .unwrap() + } + /// Returns a vector that is equal to `self` rotated by 90 degrees. #[inline] #[must_use] diff --git a/src/i64/i64vec3.rs b/src/i64/i64vec3.rs index 2d98a484..f2eec297 100644 --- a/src/i64/i64vec3.rs +++ b/src/i64/i64vec3.rs @@ -445,10 +445,28 @@ impl I64Vec3 { /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] + #[must_use] pub fn manhattan_distance(self, other: Self) -> i64 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) } + /// Computes the [chebyshev distance] between two points. + /// + /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance + #[inline] + #[must_use] + pub fn chebyshev_distance(self, other: Self) -> i64 { + // Note: the compiler will eventually optimize out the loop + [ + self.x.abs_diff(other.x), + self.y.abs_diff(other.y), + self.z.abs_diff(other.z), + ] + .into_iter() + .max() + .unwrap() + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/i64/i64vec4.rs b/src/i64/i64vec4.rs index 05f9c6fe..902e18db 100644 --- a/src/i64/i64vec4.rs +++ b/src/i64/i64vec4.rs @@ -477,6 +477,7 @@ impl I64Vec4 { /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] + #[must_use] pub fn manhattan_distance(self, other: Self) -> i64 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) @@ -484,6 +485,24 @@ impl I64Vec4 { + self.w.abs_diff(other.w) } + /// Computes the [chebyshev distance] between two points. + /// + /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance + #[inline] + #[must_use] + pub fn chebyshev_distance(self, other: Self) -> i64 { + // Note: the compiler will eventually optimize out the loop + [ + self.x.abs_diff(other.x), + self.y.abs_diff(other.y), + self.z.abs_diff(other.z), + self.w.abs_diff(other.w), + ] + .into_iter() + .max() + .unwrap() + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/i8/i8vec2.rs b/src/i8/i8vec2.rs index dc5f68e4..36e2f7b5 100644 --- a/src/i8/i8vec2.rs +++ b/src/i8/i8vec2.rs @@ -381,10 +381,24 @@ impl I8Vec2 { /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] + #[must_use] pub fn manhattan_distance(self, other: Self) -> i8 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) } + /// Computes the [chebyshev distance] between two points. + /// + /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance + #[inline] + #[must_use] + pub fn chebyshev_distance(self, other: Self) -> i8 { + // Note: the compiler will eventually optimize out the loop + [self.x.abs_diff(other.x), self.y.abs_diff(other.y)] + .into_iter() + .max() + .unwrap() + } + /// Returns a vector that is equal to `self` rotated by 90 degrees. #[inline] #[must_use] diff --git a/src/i8/i8vec3.rs b/src/i8/i8vec3.rs index b430a0e6..117ef163 100644 --- a/src/i8/i8vec3.rs +++ b/src/i8/i8vec3.rs @@ -445,10 +445,28 @@ impl I8Vec3 { /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] + #[must_use] pub fn manhattan_distance(self, other: Self) -> i8 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) } + /// Computes the [chebyshev distance] between two points. + /// + /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance + #[inline] + #[must_use] + pub fn chebyshev_distance(self, other: Self) -> i8 { + // Note: the compiler will eventually optimize out the loop + [ + self.x.abs_diff(other.x), + self.y.abs_diff(other.y), + self.z.abs_diff(other.z), + ] + .into_iter() + .max() + .unwrap() + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/i8/i8vec4.rs b/src/i8/i8vec4.rs index 3cfe5a70..873d56d1 100644 --- a/src/i8/i8vec4.rs +++ b/src/i8/i8vec4.rs @@ -477,6 +477,7 @@ impl I8Vec4 { /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] + #[must_use] pub fn manhattan_distance(self, other: Self) -> i8 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) @@ -484,6 +485,24 @@ impl I8Vec4 { + self.w.abs_diff(other.w) } + /// Computes the [chebyshev distance] between two points. + /// + /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance + #[inline] + #[must_use] + pub fn chebyshev_distance(self, other: Self) -> i8 { + // Note: the compiler will eventually optimize out the loop + [ + self.x.abs_diff(other.x), + self.y.abs_diff(other.y), + self.z.abs_diff(other.z), + self.w.abs_diff(other.w), + ] + .into_iter() + .max() + .unwrap() + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/u16/u16vec2.rs b/src/u16/u16vec2.rs index 9f3d4b96..d58f8b27 100644 --- a/src/u16/u16vec2.rs +++ b/src/u16/u16vec2.rs @@ -309,10 +309,24 @@ impl U16Vec2 { /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] + #[must_use] pub fn manhattan_distance(self, other: Self) -> u16 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) } + /// Computes the [chebyshev distance] between two points. + /// + /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance + #[inline] + #[must_use] + pub fn chebyshev_distance(self, other: Self) -> u16 { + // Note: the compiler will eventually optimize out the loop + [self.x.abs_diff(other.x), self.y.abs_diff(other.y)] + .into_iter() + .max() + .unwrap() + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/u16/u16vec3.rs b/src/u16/u16vec3.rs index 0f474df4..ab3009fe 100644 --- a/src/u16/u16vec3.rs +++ b/src/u16/u16vec3.rs @@ -358,10 +358,28 @@ impl U16Vec3 { /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] + #[must_use] pub fn manhattan_distance(self, other: Self) -> u16 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) } + /// Computes the [chebyshev distance] between two points. + /// + /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance + #[inline] + #[must_use] + pub fn chebyshev_distance(self, other: Self) -> u16 { + // Note: the compiler will eventually optimize out the loop + [ + self.x.abs_diff(other.x), + self.y.abs_diff(other.y), + self.z.abs_diff(other.z), + ] + .into_iter() + .max() + .unwrap() + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/u16/u16vec4.rs b/src/u16/u16vec4.rs index 09a857bf..9288a54b 100644 --- a/src/u16/u16vec4.rs +++ b/src/u16/u16vec4.rs @@ -382,6 +382,7 @@ impl U16Vec4 { /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] + #[must_use] pub fn manhattan_distance(self, other: Self) -> u16 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) @@ -389,6 +390,24 @@ impl U16Vec4 { + self.w.abs_diff(other.w) } + /// Computes the [chebyshev distance] between two points. + /// + /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance + #[inline] + #[must_use] + pub fn chebyshev_distance(self, other: Self) -> u16 { + // Note: the compiler will eventually optimize out the loop + [ + self.x.abs_diff(other.x), + self.y.abs_diff(other.y), + self.z.abs_diff(other.z), + self.w.abs_diff(other.w), + ] + .into_iter() + .max() + .unwrap() + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/u32/uvec2.rs b/src/u32/uvec2.rs index 0462e58e..0a194a94 100644 --- a/src/u32/uvec2.rs +++ b/src/u32/uvec2.rs @@ -309,10 +309,24 @@ impl UVec2 { /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] + #[must_use] pub fn manhattan_distance(self, other: Self) -> u32 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) } + /// Computes the [chebyshev distance] between two points. + /// + /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance + #[inline] + #[must_use] + pub fn chebyshev_distance(self, other: Self) -> u32 { + // Note: the compiler will eventually optimize out the loop + [self.x.abs_diff(other.x), self.y.abs_diff(other.y)] + .into_iter() + .max() + .unwrap() + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/u32/uvec3.rs b/src/u32/uvec3.rs index 0e1c15ec..905a5fca 100644 --- a/src/u32/uvec3.rs +++ b/src/u32/uvec3.rs @@ -358,10 +358,28 @@ impl UVec3 { /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] + #[must_use] pub fn manhattan_distance(self, other: Self) -> u32 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) } + /// Computes the [chebyshev distance] between two points. + /// + /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance + #[inline] + #[must_use] + pub fn chebyshev_distance(self, other: Self) -> u32 { + // Note: the compiler will eventually optimize out the loop + [ + self.x.abs_diff(other.x), + self.y.abs_diff(other.y), + self.z.abs_diff(other.z), + ] + .into_iter() + .max() + .unwrap() + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/u32/uvec4.rs b/src/u32/uvec4.rs index 6d17e7bd..4b8b8cae 100644 --- a/src/u32/uvec4.rs +++ b/src/u32/uvec4.rs @@ -382,6 +382,7 @@ impl UVec4 { /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] + #[must_use] pub fn manhattan_distance(self, other: Self) -> u32 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) @@ -389,6 +390,24 @@ impl UVec4 { + self.w.abs_diff(other.w) } + /// Computes the [chebyshev distance] between two points. + /// + /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance + #[inline] + #[must_use] + pub fn chebyshev_distance(self, other: Self) -> u32 { + // Note: the compiler will eventually optimize out the loop + [ + self.x.abs_diff(other.x), + self.y.abs_diff(other.y), + self.z.abs_diff(other.z), + self.w.abs_diff(other.w), + ] + .into_iter() + .max() + .unwrap() + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/u64/u64vec2.rs b/src/u64/u64vec2.rs index 056bc0d9..6cdca085 100644 --- a/src/u64/u64vec2.rs +++ b/src/u64/u64vec2.rs @@ -309,10 +309,24 @@ impl U64Vec2 { /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] + #[must_use] pub fn manhattan_distance(self, other: Self) -> u64 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) } + /// Computes the [chebyshev distance] between two points. + /// + /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance + #[inline] + #[must_use] + pub fn chebyshev_distance(self, other: Self) -> u64 { + // Note: the compiler will eventually optimize out the loop + [self.x.abs_diff(other.x), self.y.abs_diff(other.y)] + .into_iter() + .max() + .unwrap() + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/u64/u64vec3.rs b/src/u64/u64vec3.rs index 342f3e19..f33bfc07 100644 --- a/src/u64/u64vec3.rs +++ b/src/u64/u64vec3.rs @@ -358,10 +358,28 @@ impl U64Vec3 { /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] + #[must_use] pub fn manhattan_distance(self, other: Self) -> u64 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) } + /// Computes the [chebyshev distance] between two points. + /// + /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance + #[inline] + #[must_use] + pub fn chebyshev_distance(self, other: Self) -> u64 { + // Note: the compiler will eventually optimize out the loop + [ + self.x.abs_diff(other.x), + self.y.abs_diff(other.y), + self.z.abs_diff(other.z), + ] + .into_iter() + .max() + .unwrap() + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/u64/u64vec4.rs b/src/u64/u64vec4.rs index 48cd54f5..c9d89646 100644 --- a/src/u64/u64vec4.rs +++ b/src/u64/u64vec4.rs @@ -382,6 +382,7 @@ impl U64Vec4 { /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] + #[must_use] pub fn manhattan_distance(self, other: Self) -> u64 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) @@ -389,6 +390,24 @@ impl U64Vec4 { + self.w.abs_diff(other.w) } + /// Computes the [chebyshev distance] between two points. + /// + /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance + #[inline] + #[must_use] + pub fn chebyshev_distance(self, other: Self) -> u64 { + // Note: the compiler will eventually optimize out the loop + [ + self.x.abs_diff(other.x), + self.y.abs_diff(other.y), + self.z.abs_diff(other.z), + self.w.abs_diff(other.w), + ] + .into_iter() + .max() + .unwrap() + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/u8/u8vec2.rs b/src/u8/u8vec2.rs index 314ff5e1..e7db9251 100644 --- a/src/u8/u8vec2.rs +++ b/src/u8/u8vec2.rs @@ -309,10 +309,24 @@ impl U8Vec2 { /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] + #[must_use] pub fn manhattan_distance(self, other: Self) -> u8 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) } + /// Computes the [chebyshev distance] between two points. + /// + /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance + #[inline] + #[must_use] + pub fn chebyshev_distance(self, other: Self) -> u8 { + // Note: the compiler will eventually optimize out the loop + [self.x.abs_diff(other.x), self.y.abs_diff(other.y)] + .into_iter() + .max() + .unwrap() + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/u8/u8vec3.rs b/src/u8/u8vec3.rs index 13ab9657..674fcd0c 100644 --- a/src/u8/u8vec3.rs +++ b/src/u8/u8vec3.rs @@ -358,10 +358,28 @@ impl U8Vec3 { /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] + #[must_use] pub fn manhattan_distance(self, other: Self) -> u8 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) } + /// Computes the [chebyshev distance] between two points. + /// + /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance + #[inline] + #[must_use] + pub fn chebyshev_distance(self, other: Self) -> u8 { + // Note: the compiler will eventually optimize out the loop + [ + self.x.abs_diff(other.x), + self.y.abs_diff(other.y), + self.z.abs_diff(other.z), + ] + .into_iter() + .max() + .unwrap() + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] diff --git a/src/u8/u8vec4.rs b/src/u8/u8vec4.rs index 05947a15..69bbddc8 100644 --- a/src/u8/u8vec4.rs +++ b/src/u8/u8vec4.rs @@ -382,6 +382,7 @@ impl U8Vec4 { /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] + #[must_use] pub fn manhattan_distance(self, other: Self) -> u8 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) @@ -389,6 +390,24 @@ impl U8Vec4 { + self.w.abs_diff(other.w) } + /// Computes the [chebyshev distance] between two points. + /// + /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance + #[inline] + #[must_use] + pub fn chebyshev_distance(self, other: Self) -> u8 { + // Note: the compiler will eventually optimize out the loop + [ + self.x.abs_diff(other.x), + self.y.abs_diff(other.y), + self.z.abs_diff(other.z), + self.w.abs_diff(other.w), + ] + .into_iter() + .max() + .unwrap() + } + /// Casts all elements of `self` to `f32`. #[inline] #[must_use] From 08752930ecfc7110adcf0e606ab67a91167a11ca Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Sun, 22 Dec 2024 09:10:30 +0100 Subject: [PATCH 3/8] fix the return type --- codegen/README.md | 1 + codegen/templates/vec.rs.tera | 12 ++++++++++-- src/i16/i16vec2.rs | 4 ++-- src/i16/i16vec3.rs | 4 ++-- src/i16/i16vec4.rs | 4 ++-- src/i32/ivec2.rs | 4 ++-- src/i32/ivec3.rs | 4 ++-- src/i32/ivec4.rs | 4 ++-- src/i64/i64vec2.rs | 4 ++-- src/i64/i64vec3.rs | 4 ++-- src/i64/i64vec4.rs | 4 ++-- src/i8/i8vec2.rs | 4 ++-- src/i8/i8vec3.rs | 4 ++-- src/i8/i8vec4.rs | 4 ++-- 14 files changed, 35 insertions(+), 26 deletions(-) diff --git a/codegen/README.md b/codegen/README.md index d9055792..d1d299db 100644 --- a/codegen/README.md +++ b/codegen/README.md @@ -77,6 +77,7 @@ Each template starts with setting up a number of common variables based on the inputs from the `codegen` program. Commonly used variables are: * `self_t` - the name of the type being generated +* `unsigned_scalar_t` - the unsigned version of `scalar_t` (e.g. `u8`, `u16`, `u32`) * `inner_t` - the inner storage type used by this type (e.g. `__m128` or `core::storage::XYZ`) * `deref_t` - the type used by the `Deref` and `DerefMut` implementation - not diff --git a/codegen/templates/vec.rs.tera b/codegen/templates/vec.rs.tera index bc79f7df..b94c2319 100644 --- a/codegen/templates/vec.rs.tera +++ b/codegen/templates/vec.rs.tera @@ -44,6 +44,7 @@ {% elif scalar_t == "i8" %} {% set is_signed = true %} {% set is_float = false %} + {% set unsigned_scalar_t = "u8" %} {% set self_t = "I8Vec" ~ dim %} {% set opposite_signedness_t = "U8Vec" ~ dim %} {% set vec2_t = "I8Vec2" %} @@ -53,6 +54,7 @@ {% elif scalar_t == "u8" %} {% set is_signed = false %} {% set is_float = false %} + {% set unsigned_scalar_t = "u8" %} {% set self_t = "U8Vec" ~ dim %} {% set opposite_signedness_t = "I8Vec" ~ dim %} {% set vec2_t = "U8Vec2" %} @@ -62,6 +64,7 @@ {% elif scalar_t == "i16" %} {% set is_signed = true %} {% set is_float = false %} + {% set unsigned_scalar_t = "u16" %} {% set self_t = "I16Vec" ~ dim %} {% set opposite_signedness_t = "U16Vec" ~ dim %} {% set vec2_t = "I16Vec2" %} @@ -72,6 +75,7 @@ {% elif scalar_t == "u16" %} {% set is_signed = false %} {% set is_float = false %} + {% set unsigned_scalar_t = "u16" %} {% set self_t = "U16Vec" ~ dim %} {% set opposite_signedness_t = "I16Vec" ~ dim %} {% set vec2_t = "U16Vec2" %} @@ -82,6 +86,7 @@ {% elif scalar_t == "i32" %} {% set is_signed = true %} {% set is_float = false %} + {% set unsigned_scalar_t = "u32" %} {% set self_t = "IVec" ~ dim %} {% set opposite_signedness_t = "UVec" ~ dim %} {% set vec2_t = "IVec2" %} @@ -92,6 +97,7 @@ {% elif scalar_t == "u32" %} {% set is_signed = false %} {% set is_float = false %} + {% set unsigned_scalar_t = "u32" %} {% set self_t = "UVec" ~ dim %} {% set opposite_signedness_t = "IVec" ~ dim %} {% set vec2_t = "UVec2" %} @@ -102,6 +108,7 @@ {% elif scalar_t == "i64" %} {% set is_signed = true %} {% set is_float = false %} + {% set unsigned_scalar_t = "u64" %} {% set self_t = "I64Vec" ~ dim %} {% set opposite_signedness_t = "U64Vec" ~ dim %} {% set vec2_t = "I64Vec2" %} @@ -112,6 +119,7 @@ {% elif scalar_t == "u64" %} {% set is_signed = false %} {% set is_float = false %} + {% set unsigned_scalar_t = "u64" %} {% set self_t = "U64Vec" ~ dim %} {% set opposite_signedness_t = "I64Vec" ~ dim %} {% set vec2_t = "U64Vec2" %} @@ -2173,7 +2181,7 @@ impl {{ self_t }} { /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] - pub fn manhattan_distance(self, other: Self) -> {{ scalar_t }} { + pub fn manhattan_distance(self, other: Self) -> {{ unsigned_scalar_t }} { {% for c in components %} {% if not loop.first %} + {% endif %} self.{{c}}.abs_diff(other.{{c}}) @@ -2185,7 +2193,7 @@ impl {{ self_t }} { /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance #[inline] #[must_use] - pub fn chebyshev_distance(self, other: Self) -> {{ scalar_t }} { + pub fn chebyshev_distance(self, other: Self) -> {{ unsigned_scalar_t }} { // Note: the compiler will eventually optimize out the loop [ {% for c in components %} diff --git a/src/i16/i16vec2.rs b/src/i16/i16vec2.rs index 38c15650..7847e982 100644 --- a/src/i16/i16vec2.rs +++ b/src/i16/i16vec2.rs @@ -382,7 +382,7 @@ impl I16Vec2 { /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] - pub fn manhattan_distance(self, other: Self) -> i16 { + pub fn manhattan_distance(self, other: Self) -> u16 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) } @@ -391,7 +391,7 @@ impl I16Vec2 { /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance #[inline] #[must_use] - pub fn chebyshev_distance(self, other: Self) -> i16 { + pub fn chebyshev_distance(self, other: Self) -> u16 { // Note: the compiler will eventually optimize out the loop [self.x.abs_diff(other.x), self.y.abs_diff(other.y)] .into_iter() diff --git a/src/i16/i16vec3.rs b/src/i16/i16vec3.rs index afa455a4..ad85ab3e 100644 --- a/src/i16/i16vec3.rs +++ b/src/i16/i16vec3.rs @@ -446,7 +446,7 @@ impl I16Vec3 { /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] - pub fn manhattan_distance(self, other: Self) -> i16 { + pub fn manhattan_distance(self, other: Self) -> u16 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) } @@ -455,7 +455,7 @@ impl I16Vec3 { /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance #[inline] #[must_use] - pub fn chebyshev_distance(self, other: Self) -> i16 { + pub fn chebyshev_distance(self, other: Self) -> u16 { // Note: the compiler will eventually optimize out the loop [ self.x.abs_diff(other.x), diff --git a/src/i16/i16vec4.rs b/src/i16/i16vec4.rs index 2550a21c..e867d3b7 100644 --- a/src/i16/i16vec4.rs +++ b/src/i16/i16vec4.rs @@ -478,7 +478,7 @@ impl I16Vec4 { /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] - pub fn manhattan_distance(self, other: Self) -> i16 { + pub fn manhattan_distance(self, other: Self) -> u16 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) @@ -490,7 +490,7 @@ impl I16Vec4 { /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance #[inline] #[must_use] - pub fn chebyshev_distance(self, other: Self) -> i16 { + pub fn chebyshev_distance(self, other: Self) -> u16 { // Note: the compiler will eventually optimize out the loop [ self.x.abs_diff(other.x), diff --git a/src/i32/ivec2.rs b/src/i32/ivec2.rs index 1a639079..bf890662 100644 --- a/src/i32/ivec2.rs +++ b/src/i32/ivec2.rs @@ -382,7 +382,7 @@ impl IVec2 { /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] - pub fn manhattan_distance(self, other: Self) -> i32 { + pub fn manhattan_distance(self, other: Self) -> u32 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) } @@ -391,7 +391,7 @@ impl IVec2 { /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance #[inline] #[must_use] - pub fn chebyshev_distance(self, other: Self) -> i32 { + pub fn chebyshev_distance(self, other: Self) -> u32 { // Note: the compiler will eventually optimize out the loop [self.x.abs_diff(other.x), self.y.abs_diff(other.y)] .into_iter() diff --git a/src/i32/ivec3.rs b/src/i32/ivec3.rs index 8a8d723a..35bc8763 100644 --- a/src/i32/ivec3.rs +++ b/src/i32/ivec3.rs @@ -446,7 +446,7 @@ impl IVec3 { /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] - pub fn manhattan_distance(self, other: Self) -> i32 { + pub fn manhattan_distance(self, other: Self) -> u32 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) } @@ -455,7 +455,7 @@ impl IVec3 { /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance #[inline] #[must_use] - pub fn chebyshev_distance(self, other: Self) -> i32 { + pub fn chebyshev_distance(self, other: Self) -> u32 { // Note: the compiler will eventually optimize out the loop [ self.x.abs_diff(other.x), diff --git a/src/i32/ivec4.rs b/src/i32/ivec4.rs index bd069f5a..0fc30ab6 100644 --- a/src/i32/ivec4.rs +++ b/src/i32/ivec4.rs @@ -478,7 +478,7 @@ impl IVec4 { /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] - pub fn manhattan_distance(self, other: Self) -> i32 { + pub fn manhattan_distance(self, other: Self) -> u32 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) @@ -490,7 +490,7 @@ impl IVec4 { /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance #[inline] #[must_use] - pub fn chebyshev_distance(self, other: Self) -> i32 { + pub fn chebyshev_distance(self, other: Self) -> u32 { // Note: the compiler will eventually optimize out the loop [ self.x.abs_diff(other.x), diff --git a/src/i64/i64vec2.rs b/src/i64/i64vec2.rs index 97fdf3d4..8265c553 100644 --- a/src/i64/i64vec2.rs +++ b/src/i64/i64vec2.rs @@ -382,7 +382,7 @@ impl I64Vec2 { /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] - pub fn manhattan_distance(self, other: Self) -> i64 { + pub fn manhattan_distance(self, other: Self) -> u64 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) } @@ -391,7 +391,7 @@ impl I64Vec2 { /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance #[inline] #[must_use] - pub fn chebyshev_distance(self, other: Self) -> i64 { + pub fn chebyshev_distance(self, other: Self) -> u64 { // Note: the compiler will eventually optimize out the loop [self.x.abs_diff(other.x), self.y.abs_diff(other.y)] .into_iter() diff --git a/src/i64/i64vec3.rs b/src/i64/i64vec3.rs index f2eec297..4940eddc 100644 --- a/src/i64/i64vec3.rs +++ b/src/i64/i64vec3.rs @@ -446,7 +446,7 @@ impl I64Vec3 { /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] - pub fn manhattan_distance(self, other: Self) -> i64 { + pub fn manhattan_distance(self, other: Self) -> u64 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) } @@ -455,7 +455,7 @@ impl I64Vec3 { /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance #[inline] #[must_use] - pub fn chebyshev_distance(self, other: Self) -> i64 { + pub fn chebyshev_distance(self, other: Self) -> u64 { // Note: the compiler will eventually optimize out the loop [ self.x.abs_diff(other.x), diff --git a/src/i64/i64vec4.rs b/src/i64/i64vec4.rs index 902e18db..9909adcb 100644 --- a/src/i64/i64vec4.rs +++ b/src/i64/i64vec4.rs @@ -478,7 +478,7 @@ impl I64Vec4 { /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] - pub fn manhattan_distance(self, other: Self) -> i64 { + pub fn manhattan_distance(self, other: Self) -> u64 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) @@ -490,7 +490,7 @@ impl I64Vec4 { /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance #[inline] #[must_use] - pub fn chebyshev_distance(self, other: Self) -> i64 { + pub fn chebyshev_distance(self, other: Self) -> u64 { // Note: the compiler will eventually optimize out the loop [ self.x.abs_diff(other.x), diff --git a/src/i8/i8vec2.rs b/src/i8/i8vec2.rs index 36e2f7b5..025ee15f 100644 --- a/src/i8/i8vec2.rs +++ b/src/i8/i8vec2.rs @@ -382,7 +382,7 @@ impl I8Vec2 { /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] - pub fn manhattan_distance(self, other: Self) -> i8 { + pub fn manhattan_distance(self, other: Self) -> u8 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) } @@ -391,7 +391,7 @@ impl I8Vec2 { /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance #[inline] #[must_use] - pub fn chebyshev_distance(self, other: Self) -> i8 { + pub fn chebyshev_distance(self, other: Self) -> u8 { // Note: the compiler will eventually optimize out the loop [self.x.abs_diff(other.x), self.y.abs_diff(other.y)] .into_iter() diff --git a/src/i8/i8vec3.rs b/src/i8/i8vec3.rs index 117ef163..d61a0019 100644 --- a/src/i8/i8vec3.rs +++ b/src/i8/i8vec3.rs @@ -446,7 +446,7 @@ impl I8Vec3 { /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] - pub fn manhattan_distance(self, other: Self) -> i8 { + pub fn manhattan_distance(self, other: Self) -> u8 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) } @@ -455,7 +455,7 @@ impl I8Vec3 { /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance #[inline] #[must_use] - pub fn chebyshev_distance(self, other: Self) -> i8 { + pub fn chebyshev_distance(self, other: Self) -> u8 { // Note: the compiler will eventually optimize out the loop [ self.x.abs_diff(other.x), diff --git a/src/i8/i8vec4.rs b/src/i8/i8vec4.rs index 873d56d1..ef5f559a 100644 --- a/src/i8/i8vec4.rs +++ b/src/i8/i8vec4.rs @@ -478,7 +478,7 @@ impl I8Vec4 { /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] - pub fn manhattan_distance(self, other: Self) -> i8 { + pub fn manhattan_distance(self, other: Self) -> u8 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) @@ -490,7 +490,7 @@ impl I8Vec4 { /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance #[inline] #[must_use] - pub fn chebyshev_distance(self, other: Self) -> i8 { + pub fn chebyshev_distance(self, other: Self) -> u8 { // Note: the compiler will eventually optimize out the loop [ self.x.abs_diff(other.x), From 5bb890a8b442cb12542a4fa922461de25c79d044 Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Sun, 22 Dec 2024 10:05:18 +0100 Subject: [PATCH 4/8] add tests --- tests/vec2.rs | 42 +++++++++++++++++++++++++++++--- tests/vec3.rs | 66 +++++++++++++++++++++++++++++++++++++++++++++++--- tests/vec4.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 163 insertions(+), 12 deletions(-) diff --git a/tests/vec2.rs b/tests/vec2.rs index e97e308e..bc0c0be1 100644 --- a/tests/vec2.rs +++ b/tests/vec2.rs @@ -734,6 +734,40 @@ macro_rules! impl_vec2_signed_integer_tests { assert_eq!($vec3::ONE.signum(), $vec3::ONE); assert_eq!((-$vec3::ONE).signum(), -$vec3::ONE); }); + + glam_test!(test_manhattan_distance, { + assert_eq!($vec2::new(5, 2).manhattan_distance($vec2::new(23, 16)), 32); + assert_eq!($vec2::new(30, 11).manhattan_distance($vec2::new(30, 11)), 0); + assert_eq!( + $vec2::new(-8, -23).manhattan_distance($vec2::new(12, 7)), + 50 + ); + }); + + glam_test!(test_chebyshev_distance, { + assert_eq!($vec2::new(5, 2).chebyshev_distance($vec2::new(23, 16)), 18); + assert_eq!($vec2::new(30, 11).chebyshev_distance($vec2::new(30, 11)), 0); + assert_eq!( + $vec2::new(-8, -23).chebyshev_distance($vec2::new(12, 7)), + 30 + ); + }); + }; +} + +macro_rules! impl_vec2_unsigned_integer_tests { + ($t:ident, $new:ident, $vec2:ident, $vec3:ident, $mask:ident, $masknew:ident) => { + impl_vec2_tests!($t, $new, $vec2, $vec3, $mask, $masknew); + + glam_test!(test_manhattan_distance, { + assert_eq!($vec2::new(5, 2).manhattan_distance($vec2::new(23, 16)), 32); + assert_eq!($vec2::new(30, 11).manhattan_distance($vec2::new(30, 11)), 0); + }); + + glam_test!(test_chebyshev_distance, { + assert_eq!($vec2::new(5, 2).chebyshev_distance($vec2::new(23, 16)), 18); + assert_eq!($vec2::new(30, 11).chebyshev_distance($vec2::new(30, 11)), 0); + }); }; } @@ -1727,7 +1761,7 @@ mod u8vec2 { ); }); - impl_vec2_tests!(u8, u8vec2, U8Vec2, U8Vec3, BVec2, bvec2); + impl_vec2_unsigned_integer_tests!(u8, u8vec2, U8Vec2, U8Vec3, BVec2, bvec2); impl_vec2_eq_hash_tests!(u8, u8vec2); impl_vec2_scalar_shift_op_tests!(U8Vec2, 0, 2); @@ -2020,7 +2054,7 @@ mod u16vec2 { ); }); - impl_vec2_tests!(u16, u16vec2, U16Vec2, U16Vec3, BVec2, bvec2); + impl_vec2_unsigned_integer_tests!(u16, u16vec2, U16Vec2, U16Vec3, BVec2, bvec2); impl_vec2_eq_hash_tests!(u16, u16vec2); impl_vec2_scalar_shift_op_tests!(U16Vec2, 0, 2); @@ -2295,7 +2329,7 @@ mod uvec2 { ); }); - impl_vec2_tests!(u32, uvec2, UVec2, UVec3, BVec2, bvec2); + impl_vec2_unsigned_integer_tests!(u32, uvec2, UVec2, UVec3, BVec2, bvec2); impl_vec2_eq_hash_tests!(u32, uvec2); impl_vec2_scalar_shift_op_tests!(UVec2, 0, 2); @@ -2443,7 +2477,7 @@ mod u64vec2 { ); }); - impl_vec2_tests!(u64, u64vec2, U64Vec2, U64Vec3, BVec2, bvec2); + impl_vec2_unsigned_integer_tests!(u64, u64vec2, U64Vec2, U64Vec3, BVec2, bvec2); impl_vec2_eq_hash_tests!(u64, u64vec2); impl_vec2_scalar_shift_op_tests!(U64Vec2, 0, 2); diff --git a/tests/vec3.rs b/tests/vec3.rs index 4f4ad052..40507204 100644 --- a/tests/vec3.rs +++ b/tests/vec3.rs @@ -849,6 +849,64 @@ macro_rules! impl_vec3_signed_integer_tests { assert_eq!($vec3::ONE.signum(), $vec3::ONE); assert_eq!((-$vec3::ONE).signum(), -$vec3::ONE); }); + + glam_test!(test_manhattan_distance, { + assert_eq!( + $vec3::new(3, 27, 98).manhattan_distance($vec3::new(20, 65, 97)), + 56 + ); + assert_eq!( + $vec3::new(8, 12, 12).manhattan_distance($vec3::new(24, 56, 2)), + 70 + ); + assert_eq!( + $vec3::new(-23, 2, -99).manhattan_distance($vec3::new(22, -12, 24)), + 182 + ); + }); + + glam_test!(test_chebyshev_distance, { + assert_eq!( + $vec3::new(3, 27, 98).chebyshev_distance($vec3::new(20, 65, 97)), + 38 + ); + assert_eq!( + $vec3::new(8, 12, 12).chebyshev_distance($vec3::new(24, 56, 2)), + 44 + ); + assert_eq!( + $vec3::new(-23, 2, -99).chebyshev_distance($vec3::new(22, -12, 24)), + 123 + ); + }); + }; +} + +macro_rules! impl_vec3_unsigned_integer_tests { + ($t:ident, $new:ident, $vec3:ident, $mask:ident, $masknew:ident) => { + impl_vec3_tests!($t, $new, $vec3, $mask, $masknew); + + glam_test!(test_manhattan_distance, { + assert_eq!( + $vec3::new(3, 27, 98).manhattan_distance($vec3::new(20, 65, 97)), + 56 + ); + assert_eq!( + $vec3::new(8, 12, 12).manhattan_distance($vec3::new(24, 56, 2)), + 70 + ); + }); + + glam_test!(test_chebyshev_distance, { + assert_eq!( + $vec3::new(3, 27, 98).chebyshev_distance($vec3::new(20, 65, 97)), + 38 + ); + assert_eq!( + $vec3::new(8, 12, 12).chebyshev_distance($vec3::new(24, 56, 2)), + 44 + ); + }); }; } @@ -2073,7 +2131,7 @@ mod u8vec3 { ); }); - impl_vec3_tests!(u8, u8vec3, U8Vec3, BVec3, bvec3); + impl_vec3_unsigned_integer_tests!(u8, u8vec3, U8Vec3, BVec3, bvec3); impl_vec3_eq_hash_tests!(u8, u8vec3); impl_vec3_scalar_shift_op_tests!(U8Vec3, 0, 2); @@ -2376,7 +2434,7 @@ mod u16vec3 { ); }); - impl_vec3_tests!(u16, u16vec3, U16Vec3, BVec3, bvec3); + impl_vec3_unsigned_integer_tests!(u16, u16vec3, U16Vec3, BVec3, bvec3); impl_vec3_eq_hash_tests!(u16, u16vec3); impl_vec3_scalar_shift_op_tests!(U16Vec3, 0, 2); @@ -2658,7 +2716,7 @@ mod uvec3 { ); }); - impl_vec3_tests!(u32, uvec3, UVec3, BVec3, bvec3); + impl_vec3_unsigned_integer_tests!(u32, uvec3, UVec3, BVec3, bvec3); impl_vec3_eq_hash_tests!(u32, uvec3); impl_vec3_scalar_shift_op_tests!(UVec3, 0, 2); @@ -2805,7 +2863,7 @@ mod u64vec3 { ); }); - impl_vec3_tests!(u64, u64vec3, U64Vec3, BVec3, bvec3); + impl_vec3_unsigned_integer_tests!(u64, u64vec3, U64Vec3, BVec3, bvec3); impl_vec3_eq_hash_tests!(u64, u64vec3); impl_vec3_scalar_shift_op_tests!(U64Vec3, 0, 2); diff --git a/tests/vec4.rs b/tests/vec4.rs index 1abdaf21..f2f5e9e3 100644 --- a/tests/vec4.rs +++ b/tests/vec4.rs @@ -967,6 +967,65 @@ macro_rules! impl_vec4_signed_integer_tests { assert_eq!($vec4::ONE.signum(), $vec4::ONE); assert_eq!((-$vec4::ONE).signum(), -$vec4::ONE); }); + + glam_test!(test_manhattan_distance, { + assert_eq!( + $vec4::new(41, 8, 21, 87).manhattan_distance($vec4::new(49, 48, 28, 40)), + 102 + ); + assert_eq!( + $vec4::new(19, 16, 100, 74).manhattan_distance($vec4::new(14, 55, 115, 48)), + 85 + ); + + assert_eq!( + $vec4::new(26, 2, 24, -22).manhattan_distance($vec4::new(26, 23, 6, 23)), + 84 + ); + }); + + glam_test!(test_chebyshev_distance, { + assert_eq!( + $vec4::new(41, 8, 21, 87).chebyshev_distance($vec4::new(49, 48, 28, 40)), + 47 + ); + assert_eq!( + $vec4::new(119, 16, 100, 74).chebyshev_distance($vec4::new(14, 55, 115, 48)), + 105 + ); + assert_eq!( + $vec4::new(26, 2, 24, -22).chebyshev_distance($vec4::new(26, 23, 6, 23)), + 45 + ); + }); + }; +} + +macro_rules! impl_vec4_unsigned_integer_tests { + ($t:ident, $new:ident, $vec4:ident, $vec3:ident, $vec2:ident, $mask:ident, $masknew:ident) => { + impl_vec4_tests!($t, $new, $vec4, $vec3, $vec2, $mask, $masknew); + + glam_test!(test_manhattan_distance, { + assert_eq!( + $vec4::new(41, 8, 21, 87).manhattan_distance($vec4::new(49, 48, 128, 40)), + 202 + ); + assert_eq!( + $vec4::new(19, 16, 179, 174).manhattan_distance($vec4::new(14, 55, 115, 148)), + 134 + ); + }); + + glam_test!(test_chebyshev_distance, { + assert_eq!( + $vec4::new(41, 8, 21, 87).chebyshev_distance($vec4::new(49, 48, 128, 40)), + 107 + ); + assert_eq!( + $vec4::new(119, 16, 179, 174).chebyshev_distance($vec4::new(14, 55, 115, 148)), + 105 + ); + }); }; } @@ -2324,7 +2383,7 @@ mod u8vec4 { ); }); - impl_vec4_tests!(u8, u8vec4, U8Vec4, U8Vec3, U8Vec2, BVec4, bvec4); + impl_vec4_unsigned_integer_tests!(u8, u8vec4, U8Vec4, U8Vec3, U8Vec2, BVec4, bvec4); impl_vec4_eq_hash_tests!(u8, u8vec4); impl_vec4_scalar_shift_op_tests!(U8Vec4, 0, 2); @@ -2666,7 +2725,7 @@ mod u16vec4 { ); }); - impl_vec4_tests!(u16, u16vec4, U16Vec4, U16Vec3, U16Vec2, BVec4, bvec4); + impl_vec4_unsigned_integer_tests!(u16, u16vec4, U16Vec4, U16Vec3, U16Vec2, BVec4, bvec4); impl_vec4_eq_hash_tests!(u16, u16vec4); impl_vec4_scalar_shift_op_tests!(U16Vec4, 0, 2); @@ -2980,7 +3039,7 @@ mod uvec4 { ); }); - impl_vec4_tests!(u32, uvec4, UVec4, UVec3, UVec2, BVec4, bvec4); + impl_vec4_unsigned_integer_tests!(u32, uvec4, UVec4, UVec3, UVec2, BVec4, bvec4); impl_vec4_eq_hash_tests!(u32, uvec4); impl_vec4_scalar_shift_op_tests!(UVec4, 0, 2); @@ -3171,7 +3230,7 @@ mod u64vec4 { ); }); - impl_vec4_tests!(u64, u64vec4, U64Vec4, U64Vec3, U64Vec2, BVec4, bvec4); + impl_vec4_unsigned_integer_tests!(u64, u64vec4, U64Vec4, U64Vec3, U64Vec2, BVec4, bvec4); impl_vec4_eq_hash_tests!(u64, u64vec4); impl_vec4_scalar_shift_op_tests!(U64Vec4, 0, 2); From 5b17a67d157807ac406b5a848c00ad5f37448238 Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Sun, 22 Dec 2024 10:08:08 +0100 Subject: [PATCH 5/8] add a note about overflow --- codegen/templates/vec.rs.tera | 3 +++ src/i16/i16vec2.rs | 3 +++ src/i16/i16vec3.rs | 3 +++ src/i16/i16vec4.rs | 3 +++ src/i32/ivec2.rs | 3 +++ src/i32/ivec3.rs | 3 +++ src/i32/ivec4.rs | 3 +++ src/i64/i64vec2.rs | 3 +++ src/i64/i64vec3.rs | 3 +++ src/i64/i64vec4.rs | 3 +++ src/i8/i8vec2.rs | 3 +++ src/i8/i8vec3.rs | 3 +++ src/i8/i8vec4.rs | 3 +++ src/u16/u16vec2.rs | 3 +++ src/u16/u16vec3.rs | 3 +++ src/u16/u16vec4.rs | 3 +++ src/u32/uvec2.rs | 3 +++ src/u32/uvec3.rs | 3 +++ src/u32/uvec4.rs | 3 +++ src/u64/u64vec2.rs | 3 +++ src/u64/u64vec3.rs | 3 +++ src/u64/u64vec4.rs | 3 +++ src/u8/u8vec2.rs | 3 +++ src/u8/u8vec3.rs | 3 +++ src/u8/u8vec4.rs | 3 +++ 25 files changed, 75 insertions(+) diff --git a/codegen/templates/vec.rs.tera b/codegen/templates/vec.rs.tera index b94c2319..75740930 100644 --- a/codegen/templates/vec.rs.tera +++ b/codegen/templates/vec.rs.tera @@ -2178,6 +2178,9 @@ impl {{ self_t }} { /// Computes the [manhattan distance] between two points. /// + /// # Overflow + /// This method may overflow if the result is greater than [`{{ unsigned_scalar_t }}::MAX`]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] diff --git a/src/i16/i16vec2.rs b/src/i16/i16vec2.rs index 7847e982..716e08e4 100644 --- a/src/i16/i16vec2.rs +++ b/src/i16/i16vec2.rs @@ -379,6 +379,9 @@ impl I16Vec2 { /// Computes the [manhattan distance] between two points. /// + /// # Overflow + /// This method may overflow if the result is greater than [`u16::MAX`]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] diff --git a/src/i16/i16vec3.rs b/src/i16/i16vec3.rs index ad85ab3e..94f15b07 100644 --- a/src/i16/i16vec3.rs +++ b/src/i16/i16vec3.rs @@ -443,6 +443,9 @@ impl I16Vec3 { /// Computes the [manhattan distance] between two points. /// + /// # Overflow + /// This method may overflow if the result is greater than [`u16::MAX`]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] diff --git a/src/i16/i16vec4.rs b/src/i16/i16vec4.rs index e867d3b7..4a3ccc44 100644 --- a/src/i16/i16vec4.rs +++ b/src/i16/i16vec4.rs @@ -475,6 +475,9 @@ impl I16Vec4 { /// Computes the [manhattan distance] between two points. /// + /// # Overflow + /// This method may overflow if the result is greater than [`u16::MAX`]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] diff --git a/src/i32/ivec2.rs b/src/i32/ivec2.rs index bf890662..c3b43687 100644 --- a/src/i32/ivec2.rs +++ b/src/i32/ivec2.rs @@ -379,6 +379,9 @@ impl IVec2 { /// Computes the [manhattan distance] between two points. /// + /// # Overflow + /// This method may overflow if the result is greater than [`u32::MAX`]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] diff --git a/src/i32/ivec3.rs b/src/i32/ivec3.rs index 35bc8763..0f3699e1 100644 --- a/src/i32/ivec3.rs +++ b/src/i32/ivec3.rs @@ -443,6 +443,9 @@ impl IVec3 { /// Computes the [manhattan distance] between two points. /// + /// # Overflow + /// This method may overflow if the result is greater than [`u32::MAX`]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] diff --git a/src/i32/ivec4.rs b/src/i32/ivec4.rs index 0fc30ab6..ac035294 100644 --- a/src/i32/ivec4.rs +++ b/src/i32/ivec4.rs @@ -475,6 +475,9 @@ impl IVec4 { /// Computes the [manhattan distance] between two points. /// + /// # Overflow + /// This method may overflow if the result is greater than [`u32::MAX`]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] diff --git a/src/i64/i64vec2.rs b/src/i64/i64vec2.rs index 8265c553..9bdeb2a7 100644 --- a/src/i64/i64vec2.rs +++ b/src/i64/i64vec2.rs @@ -379,6 +379,9 @@ impl I64Vec2 { /// Computes the [manhattan distance] between two points. /// + /// # Overflow + /// This method may overflow if the result is greater than [`u64::MAX`]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] diff --git a/src/i64/i64vec3.rs b/src/i64/i64vec3.rs index 4940eddc..62f15357 100644 --- a/src/i64/i64vec3.rs +++ b/src/i64/i64vec3.rs @@ -443,6 +443,9 @@ impl I64Vec3 { /// Computes the [manhattan distance] between two points. /// + /// # Overflow + /// This method may overflow if the result is greater than [`u64::MAX`]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] diff --git a/src/i64/i64vec4.rs b/src/i64/i64vec4.rs index 9909adcb..2cfce2eb 100644 --- a/src/i64/i64vec4.rs +++ b/src/i64/i64vec4.rs @@ -475,6 +475,9 @@ impl I64Vec4 { /// Computes the [manhattan distance] between two points. /// + /// # Overflow + /// This method may overflow if the result is greater than [`u64::MAX`]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] diff --git a/src/i8/i8vec2.rs b/src/i8/i8vec2.rs index 025ee15f..d711de8d 100644 --- a/src/i8/i8vec2.rs +++ b/src/i8/i8vec2.rs @@ -379,6 +379,9 @@ impl I8Vec2 { /// Computes the [manhattan distance] between two points. /// + /// # Overflow + /// This method may overflow if the result is greater than [`u8::MAX`]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] diff --git a/src/i8/i8vec3.rs b/src/i8/i8vec3.rs index d61a0019..7049ba03 100644 --- a/src/i8/i8vec3.rs +++ b/src/i8/i8vec3.rs @@ -443,6 +443,9 @@ impl I8Vec3 { /// Computes the [manhattan distance] between two points. /// + /// # Overflow + /// This method may overflow if the result is greater than [`u8::MAX`]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] diff --git a/src/i8/i8vec4.rs b/src/i8/i8vec4.rs index ef5f559a..133ab92f 100644 --- a/src/i8/i8vec4.rs +++ b/src/i8/i8vec4.rs @@ -475,6 +475,9 @@ impl I8Vec4 { /// Computes the [manhattan distance] between two points. /// + /// # Overflow + /// This method may overflow if the result is greater than [`u8::MAX`]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] diff --git a/src/u16/u16vec2.rs b/src/u16/u16vec2.rs index d58f8b27..49302546 100644 --- a/src/u16/u16vec2.rs +++ b/src/u16/u16vec2.rs @@ -307,6 +307,9 @@ impl U16Vec2 { /// Computes the [manhattan distance] between two points. /// + /// # Overflow + /// This method may overflow if the result is greater than [`u16::MAX`]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] diff --git a/src/u16/u16vec3.rs b/src/u16/u16vec3.rs index ab3009fe..9f86dbcb 100644 --- a/src/u16/u16vec3.rs +++ b/src/u16/u16vec3.rs @@ -356,6 +356,9 @@ impl U16Vec3 { /// Computes the [manhattan distance] between two points. /// + /// # Overflow + /// This method may overflow if the result is greater than [`u16::MAX`]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] diff --git a/src/u16/u16vec4.rs b/src/u16/u16vec4.rs index 9288a54b..2ae8b89e 100644 --- a/src/u16/u16vec4.rs +++ b/src/u16/u16vec4.rs @@ -380,6 +380,9 @@ impl U16Vec4 { /// Computes the [manhattan distance] between two points. /// + /// # Overflow + /// This method may overflow if the result is greater than [`u16::MAX`]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] diff --git a/src/u32/uvec2.rs b/src/u32/uvec2.rs index 0a194a94..c164616f 100644 --- a/src/u32/uvec2.rs +++ b/src/u32/uvec2.rs @@ -307,6 +307,9 @@ impl UVec2 { /// Computes the [manhattan distance] between two points. /// + /// # Overflow + /// This method may overflow if the result is greater than [`u32::MAX`]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] diff --git a/src/u32/uvec3.rs b/src/u32/uvec3.rs index 905a5fca..aaec8c6c 100644 --- a/src/u32/uvec3.rs +++ b/src/u32/uvec3.rs @@ -356,6 +356,9 @@ impl UVec3 { /// Computes the [manhattan distance] between two points. /// + /// # Overflow + /// This method may overflow if the result is greater than [`u32::MAX`]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] diff --git a/src/u32/uvec4.rs b/src/u32/uvec4.rs index 4b8b8cae..9199f5da 100644 --- a/src/u32/uvec4.rs +++ b/src/u32/uvec4.rs @@ -380,6 +380,9 @@ impl UVec4 { /// Computes the [manhattan distance] between two points. /// + /// # Overflow + /// This method may overflow if the result is greater than [`u32::MAX`]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] diff --git a/src/u64/u64vec2.rs b/src/u64/u64vec2.rs index 6cdca085..07e9bc36 100644 --- a/src/u64/u64vec2.rs +++ b/src/u64/u64vec2.rs @@ -307,6 +307,9 @@ impl U64Vec2 { /// Computes the [manhattan distance] between two points. /// + /// # Overflow + /// This method may overflow if the result is greater than [`u64::MAX`]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] diff --git a/src/u64/u64vec3.rs b/src/u64/u64vec3.rs index f33bfc07..815a7552 100644 --- a/src/u64/u64vec3.rs +++ b/src/u64/u64vec3.rs @@ -356,6 +356,9 @@ impl U64Vec3 { /// Computes the [manhattan distance] between two points. /// + /// # Overflow + /// This method may overflow if the result is greater than [`u64::MAX`]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] diff --git a/src/u64/u64vec4.rs b/src/u64/u64vec4.rs index c9d89646..60ea0404 100644 --- a/src/u64/u64vec4.rs +++ b/src/u64/u64vec4.rs @@ -380,6 +380,9 @@ impl U64Vec4 { /// Computes the [manhattan distance] between two points. /// + /// # Overflow + /// This method may overflow if the result is greater than [`u64::MAX`]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] diff --git a/src/u8/u8vec2.rs b/src/u8/u8vec2.rs index e7db9251..f28e5888 100644 --- a/src/u8/u8vec2.rs +++ b/src/u8/u8vec2.rs @@ -307,6 +307,9 @@ impl U8Vec2 { /// Computes the [manhattan distance] between two points. /// + /// # Overflow + /// This method may overflow if the result is greater than [`u8::MAX`]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] diff --git a/src/u8/u8vec3.rs b/src/u8/u8vec3.rs index 674fcd0c..b63df051 100644 --- a/src/u8/u8vec3.rs +++ b/src/u8/u8vec3.rs @@ -356,6 +356,9 @@ impl U8Vec3 { /// Computes the [manhattan distance] between two points. /// + /// # Overflow + /// This method may overflow if the result is greater than [`u8::MAX`]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] diff --git a/src/u8/u8vec4.rs b/src/u8/u8vec4.rs index 69bbddc8..c8b89a5a 100644 --- a/src/u8/u8vec4.rs +++ b/src/u8/u8vec4.rs @@ -380,6 +380,9 @@ impl U8Vec4 { /// Computes the [manhattan distance] between two points. /// + /// # Overflow + /// This method may overflow if the result is greater than [`u8::MAX`]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] From 183d9d186e522863b6092ac40da3e9e45a507a7a Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Sun, 22 Dec 2024 10:15:29 +0100 Subject: [PATCH 6/8] add `checked_manhattan_distance` --- codegen/templates/vec.rs.tera | 21 +++++++++++++++++++++ src/i16/i16vec2.rs | 17 +++++++++++++++++ src/i16/i16vec3.rs | 19 +++++++++++++++++++ src/i16/i16vec4.rs | 21 +++++++++++++++++++++ src/i32/ivec2.rs | 17 +++++++++++++++++ src/i32/ivec3.rs | 19 +++++++++++++++++++ src/i32/ivec4.rs | 21 +++++++++++++++++++++ src/i64/i64vec2.rs | 17 +++++++++++++++++ src/i64/i64vec3.rs | 19 +++++++++++++++++++ src/i64/i64vec4.rs | 21 +++++++++++++++++++++ src/i8/i8vec2.rs | 17 +++++++++++++++++ src/i8/i8vec3.rs | 19 +++++++++++++++++++ src/i8/i8vec4.rs | 21 +++++++++++++++++++++ src/u16/u16vec2.rs | 17 +++++++++++++++++ src/u16/u16vec3.rs | 19 +++++++++++++++++++ src/u16/u16vec4.rs | 21 +++++++++++++++++++++ src/u32/uvec2.rs | 17 +++++++++++++++++ src/u32/uvec3.rs | 19 +++++++++++++++++++ src/u32/uvec4.rs | 21 +++++++++++++++++++++ src/u64/u64vec2.rs | 17 +++++++++++++++++ src/u64/u64vec3.rs | 19 +++++++++++++++++++ src/u64/u64vec4.rs | 21 +++++++++++++++++++++ src/u8/u8vec2.rs | 17 +++++++++++++++++ src/u8/u8vec3.rs | 19 +++++++++++++++++++ src/u8/u8vec4.rs | 21 +++++++++++++++++++++ 25 files changed, 477 insertions(+) diff --git a/codegen/templates/vec.rs.tera b/codegen/templates/vec.rs.tera index 75740930..ff7ee3af 100644 --- a/codegen/templates/vec.rs.tera +++ b/codegen/templates/vec.rs.tera @@ -2181,6 +2181,8 @@ impl {{ self_t }} { /// # Overflow /// This method may overflow if the result is greater than [`{{ unsigned_scalar_t }}::MAX`]. /// + /// See also [`checked_manhattan_distance`][{{ self_t }}::checked_manhattan_distance]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] @@ -2191,6 +2193,25 @@ impl {{ self_t }} { {% endfor %} } + /// Computes the [manhattan distance] between two points. + /// + /// This will returns [`None`] if the result is greater than [`{{ unsigned_scalar_t }}::MAX`]. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + #[must_use] + pub fn checked_manhattan_distance(self, other: Self) -> Option<{{ unsigned_scalar_t }}> { + {% for c in components %} + {% if loop.first %} + let d = self.{{c}}.abs_diff(other.{{c}}); + {% else %} + let d = d.checked_add(self.{{c}}.abs_diff(other.{{c}}))?; + {% endif %} + {% endfor %} + + d + } + /// Computes the [chebyshev distance] between two points. /// /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance diff --git a/src/i16/i16vec2.rs b/src/i16/i16vec2.rs index 716e08e4..1fe2ed08 100644 --- a/src/i16/i16vec2.rs +++ b/src/i16/i16vec2.rs @@ -382,6 +382,8 @@ impl I16Vec2 { /// # Overflow /// This method may overflow if the result is greater than [`u16::MAX`]. /// + /// See also [`checked_manhattan_distance`][I16Vec2::checked_manhattan_distance]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] @@ -389,6 +391,21 @@ impl I16Vec2 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) } + /// Computes the [manhattan distance] between two points. + /// + /// This will returns [`None`] if the result is greater than [`u16::MAX`]. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + #[must_use] + pub fn checked_manhattan_distance(self, other: Self) -> Option { + let d = self.x.abs_diff(other.x); + + let d = d.checked_add(self.y.abs_diff(other.y))?; + + d + } + /// Computes the [chebyshev distance] between two points. /// /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance diff --git a/src/i16/i16vec3.rs b/src/i16/i16vec3.rs index 94f15b07..e3f6a008 100644 --- a/src/i16/i16vec3.rs +++ b/src/i16/i16vec3.rs @@ -446,6 +446,8 @@ impl I16Vec3 { /// # Overflow /// This method may overflow if the result is greater than [`u16::MAX`]. /// + /// See also [`checked_manhattan_distance`][I16Vec3::checked_manhattan_distance]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] @@ -453,6 +455,23 @@ impl I16Vec3 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) } + /// Computes the [manhattan distance] between two points. + /// + /// This will returns [`None`] if the result is greater than [`u16::MAX`]. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + #[must_use] + pub fn checked_manhattan_distance(self, other: Self) -> Option { + let d = self.x.abs_diff(other.x); + + let d = d.checked_add(self.y.abs_diff(other.y))?; + + let d = d.checked_add(self.z.abs_diff(other.z))?; + + d + } + /// Computes the [chebyshev distance] between two points. /// /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance diff --git a/src/i16/i16vec4.rs b/src/i16/i16vec4.rs index 4a3ccc44..b8c9f7de 100644 --- a/src/i16/i16vec4.rs +++ b/src/i16/i16vec4.rs @@ -478,6 +478,8 @@ impl I16Vec4 { /// # Overflow /// This method may overflow if the result is greater than [`u16::MAX`]. /// + /// See also [`checked_manhattan_distance`][I16Vec4::checked_manhattan_distance]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] @@ -488,6 +490,25 @@ impl I16Vec4 { + self.w.abs_diff(other.w) } + /// Computes the [manhattan distance] between two points. + /// + /// This will returns [`None`] if the result is greater than [`u16::MAX`]. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + #[must_use] + pub fn checked_manhattan_distance(self, other: Self) -> Option { + let d = self.x.abs_diff(other.x); + + let d = d.checked_add(self.y.abs_diff(other.y))?; + + let d = d.checked_add(self.z.abs_diff(other.z))?; + + let d = d.checked_add(self.w.abs_diff(other.w))?; + + d + } + /// Computes the [chebyshev distance] between two points. /// /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance diff --git a/src/i32/ivec2.rs b/src/i32/ivec2.rs index c3b43687..672f147b 100644 --- a/src/i32/ivec2.rs +++ b/src/i32/ivec2.rs @@ -382,6 +382,8 @@ impl IVec2 { /// # Overflow /// This method may overflow if the result is greater than [`u32::MAX`]. /// + /// See also [`checked_manhattan_distance`][IVec2::checked_manhattan_distance]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] @@ -389,6 +391,21 @@ impl IVec2 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) } + /// Computes the [manhattan distance] between two points. + /// + /// This will returns [`None`] if the result is greater than [`u32::MAX`]. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + #[must_use] + pub fn checked_manhattan_distance(self, other: Self) -> Option { + let d = self.x.abs_diff(other.x); + + let d = d.checked_add(self.y.abs_diff(other.y))?; + + d + } + /// Computes the [chebyshev distance] between two points. /// /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance diff --git a/src/i32/ivec3.rs b/src/i32/ivec3.rs index 0f3699e1..28e3063e 100644 --- a/src/i32/ivec3.rs +++ b/src/i32/ivec3.rs @@ -446,6 +446,8 @@ impl IVec3 { /// # Overflow /// This method may overflow if the result is greater than [`u32::MAX`]. /// + /// See also [`checked_manhattan_distance`][IVec3::checked_manhattan_distance]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] @@ -453,6 +455,23 @@ impl IVec3 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) } + /// Computes the [manhattan distance] between two points. + /// + /// This will returns [`None`] if the result is greater than [`u32::MAX`]. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + #[must_use] + pub fn checked_manhattan_distance(self, other: Self) -> Option { + let d = self.x.abs_diff(other.x); + + let d = d.checked_add(self.y.abs_diff(other.y))?; + + let d = d.checked_add(self.z.abs_diff(other.z))?; + + d + } + /// Computes the [chebyshev distance] between two points. /// /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance diff --git a/src/i32/ivec4.rs b/src/i32/ivec4.rs index ac035294..90785682 100644 --- a/src/i32/ivec4.rs +++ b/src/i32/ivec4.rs @@ -478,6 +478,8 @@ impl IVec4 { /// # Overflow /// This method may overflow if the result is greater than [`u32::MAX`]. /// + /// See also [`checked_manhattan_distance`][IVec4::checked_manhattan_distance]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] @@ -488,6 +490,25 @@ impl IVec4 { + self.w.abs_diff(other.w) } + /// Computes the [manhattan distance] between two points. + /// + /// This will returns [`None`] if the result is greater than [`u32::MAX`]. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + #[must_use] + pub fn checked_manhattan_distance(self, other: Self) -> Option { + let d = self.x.abs_diff(other.x); + + let d = d.checked_add(self.y.abs_diff(other.y))?; + + let d = d.checked_add(self.z.abs_diff(other.z))?; + + let d = d.checked_add(self.w.abs_diff(other.w))?; + + d + } + /// Computes the [chebyshev distance] between two points. /// /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance diff --git a/src/i64/i64vec2.rs b/src/i64/i64vec2.rs index 9bdeb2a7..623d1170 100644 --- a/src/i64/i64vec2.rs +++ b/src/i64/i64vec2.rs @@ -382,6 +382,8 @@ impl I64Vec2 { /// # Overflow /// This method may overflow if the result is greater than [`u64::MAX`]. /// + /// See also [`checked_manhattan_distance`][I64Vec2::checked_manhattan_distance]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] @@ -389,6 +391,21 @@ impl I64Vec2 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) } + /// Computes the [manhattan distance] between two points. + /// + /// This will returns [`None`] if the result is greater than [`u64::MAX`]. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + #[must_use] + pub fn checked_manhattan_distance(self, other: Self) -> Option { + let d = self.x.abs_diff(other.x); + + let d = d.checked_add(self.y.abs_diff(other.y))?; + + d + } + /// Computes the [chebyshev distance] between two points. /// /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance diff --git a/src/i64/i64vec3.rs b/src/i64/i64vec3.rs index 62f15357..13154080 100644 --- a/src/i64/i64vec3.rs +++ b/src/i64/i64vec3.rs @@ -446,6 +446,8 @@ impl I64Vec3 { /// # Overflow /// This method may overflow if the result is greater than [`u64::MAX`]. /// + /// See also [`checked_manhattan_distance`][I64Vec3::checked_manhattan_distance]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] @@ -453,6 +455,23 @@ impl I64Vec3 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) } + /// Computes the [manhattan distance] between two points. + /// + /// This will returns [`None`] if the result is greater than [`u64::MAX`]. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + #[must_use] + pub fn checked_manhattan_distance(self, other: Self) -> Option { + let d = self.x.abs_diff(other.x); + + let d = d.checked_add(self.y.abs_diff(other.y))?; + + let d = d.checked_add(self.z.abs_diff(other.z))?; + + d + } + /// Computes the [chebyshev distance] between two points. /// /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance diff --git a/src/i64/i64vec4.rs b/src/i64/i64vec4.rs index 2cfce2eb..b878b34d 100644 --- a/src/i64/i64vec4.rs +++ b/src/i64/i64vec4.rs @@ -478,6 +478,8 @@ impl I64Vec4 { /// # Overflow /// This method may overflow if the result is greater than [`u64::MAX`]. /// + /// See also [`checked_manhattan_distance`][I64Vec4::checked_manhattan_distance]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] @@ -488,6 +490,25 @@ impl I64Vec4 { + self.w.abs_diff(other.w) } + /// Computes the [manhattan distance] between two points. + /// + /// This will returns [`None`] if the result is greater than [`u64::MAX`]. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + #[must_use] + pub fn checked_manhattan_distance(self, other: Self) -> Option { + let d = self.x.abs_diff(other.x); + + let d = d.checked_add(self.y.abs_diff(other.y))?; + + let d = d.checked_add(self.z.abs_diff(other.z))?; + + let d = d.checked_add(self.w.abs_diff(other.w))?; + + d + } + /// Computes the [chebyshev distance] between two points. /// /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance diff --git a/src/i8/i8vec2.rs b/src/i8/i8vec2.rs index d711de8d..aef6fba0 100644 --- a/src/i8/i8vec2.rs +++ b/src/i8/i8vec2.rs @@ -382,6 +382,8 @@ impl I8Vec2 { /// # Overflow /// This method may overflow if the result is greater than [`u8::MAX`]. /// + /// See also [`checked_manhattan_distance`][I8Vec2::checked_manhattan_distance]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] @@ -389,6 +391,21 @@ impl I8Vec2 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) } + /// Computes the [manhattan distance] between two points. + /// + /// This will returns [`None`] if the result is greater than [`u8::MAX`]. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + #[must_use] + pub fn checked_manhattan_distance(self, other: Self) -> Option { + let d = self.x.abs_diff(other.x); + + let d = d.checked_add(self.y.abs_diff(other.y))?; + + d + } + /// Computes the [chebyshev distance] between two points. /// /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance diff --git a/src/i8/i8vec3.rs b/src/i8/i8vec3.rs index 7049ba03..8194d3a1 100644 --- a/src/i8/i8vec3.rs +++ b/src/i8/i8vec3.rs @@ -446,6 +446,8 @@ impl I8Vec3 { /// # Overflow /// This method may overflow if the result is greater than [`u8::MAX`]. /// + /// See also [`checked_manhattan_distance`][I8Vec3::checked_manhattan_distance]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] @@ -453,6 +455,23 @@ impl I8Vec3 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) } + /// Computes the [manhattan distance] between two points. + /// + /// This will returns [`None`] if the result is greater than [`u8::MAX`]. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + #[must_use] + pub fn checked_manhattan_distance(self, other: Self) -> Option { + let d = self.x.abs_diff(other.x); + + let d = d.checked_add(self.y.abs_diff(other.y))?; + + let d = d.checked_add(self.z.abs_diff(other.z))?; + + d + } + /// Computes the [chebyshev distance] between two points. /// /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance diff --git a/src/i8/i8vec4.rs b/src/i8/i8vec4.rs index 133ab92f..f320e539 100644 --- a/src/i8/i8vec4.rs +++ b/src/i8/i8vec4.rs @@ -478,6 +478,8 @@ impl I8Vec4 { /// # Overflow /// This method may overflow if the result is greater than [`u8::MAX`]. /// + /// See also [`checked_manhattan_distance`][I8Vec4::checked_manhattan_distance]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] @@ -488,6 +490,25 @@ impl I8Vec4 { + self.w.abs_diff(other.w) } + /// Computes the [manhattan distance] between two points. + /// + /// This will returns [`None`] if the result is greater than [`u8::MAX`]. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + #[must_use] + pub fn checked_manhattan_distance(self, other: Self) -> Option { + let d = self.x.abs_diff(other.x); + + let d = d.checked_add(self.y.abs_diff(other.y))?; + + let d = d.checked_add(self.z.abs_diff(other.z))?; + + let d = d.checked_add(self.w.abs_diff(other.w))?; + + d + } + /// Computes the [chebyshev distance] between two points. /// /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance diff --git a/src/u16/u16vec2.rs b/src/u16/u16vec2.rs index 49302546..0330784a 100644 --- a/src/u16/u16vec2.rs +++ b/src/u16/u16vec2.rs @@ -310,6 +310,8 @@ impl U16Vec2 { /// # Overflow /// This method may overflow if the result is greater than [`u16::MAX`]. /// + /// See also [`checked_manhattan_distance`][U16Vec2::checked_manhattan_distance]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] @@ -317,6 +319,21 @@ impl U16Vec2 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) } + /// Computes the [manhattan distance] between two points. + /// + /// This will returns [`None`] if the result is greater than [`u16::MAX`]. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + #[must_use] + pub fn checked_manhattan_distance(self, other: Self) -> Option { + let d = self.x.abs_diff(other.x); + + let d = d.checked_add(self.y.abs_diff(other.y))?; + + d + } + /// Computes the [chebyshev distance] between two points. /// /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance diff --git a/src/u16/u16vec3.rs b/src/u16/u16vec3.rs index 9f86dbcb..145e38bd 100644 --- a/src/u16/u16vec3.rs +++ b/src/u16/u16vec3.rs @@ -359,6 +359,8 @@ impl U16Vec3 { /// # Overflow /// This method may overflow if the result is greater than [`u16::MAX`]. /// + /// See also [`checked_manhattan_distance`][U16Vec3::checked_manhattan_distance]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] @@ -366,6 +368,23 @@ impl U16Vec3 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) } + /// Computes the [manhattan distance] between two points. + /// + /// This will returns [`None`] if the result is greater than [`u16::MAX`]. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + #[must_use] + pub fn checked_manhattan_distance(self, other: Self) -> Option { + let d = self.x.abs_diff(other.x); + + let d = d.checked_add(self.y.abs_diff(other.y))?; + + let d = d.checked_add(self.z.abs_diff(other.z))?; + + d + } + /// Computes the [chebyshev distance] between two points. /// /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance diff --git a/src/u16/u16vec4.rs b/src/u16/u16vec4.rs index 2ae8b89e..418cf012 100644 --- a/src/u16/u16vec4.rs +++ b/src/u16/u16vec4.rs @@ -383,6 +383,8 @@ impl U16Vec4 { /// # Overflow /// This method may overflow if the result is greater than [`u16::MAX`]. /// + /// See also [`checked_manhattan_distance`][U16Vec4::checked_manhattan_distance]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] @@ -393,6 +395,25 @@ impl U16Vec4 { + self.w.abs_diff(other.w) } + /// Computes the [manhattan distance] between two points. + /// + /// This will returns [`None`] if the result is greater than [`u16::MAX`]. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + #[must_use] + pub fn checked_manhattan_distance(self, other: Self) -> Option { + let d = self.x.abs_diff(other.x); + + let d = d.checked_add(self.y.abs_diff(other.y))?; + + let d = d.checked_add(self.z.abs_diff(other.z))?; + + let d = d.checked_add(self.w.abs_diff(other.w))?; + + d + } + /// Computes the [chebyshev distance] between two points. /// /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance diff --git a/src/u32/uvec2.rs b/src/u32/uvec2.rs index c164616f..890f1e36 100644 --- a/src/u32/uvec2.rs +++ b/src/u32/uvec2.rs @@ -310,6 +310,8 @@ impl UVec2 { /// # Overflow /// This method may overflow if the result is greater than [`u32::MAX`]. /// + /// See also [`checked_manhattan_distance`][UVec2::checked_manhattan_distance]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] @@ -317,6 +319,21 @@ impl UVec2 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) } + /// Computes the [manhattan distance] between two points. + /// + /// This will returns [`None`] if the result is greater than [`u32::MAX`]. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + #[must_use] + pub fn checked_manhattan_distance(self, other: Self) -> Option { + let d = self.x.abs_diff(other.x); + + let d = d.checked_add(self.y.abs_diff(other.y))?; + + d + } + /// Computes the [chebyshev distance] between two points. /// /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance diff --git a/src/u32/uvec3.rs b/src/u32/uvec3.rs index aaec8c6c..01b14c0a 100644 --- a/src/u32/uvec3.rs +++ b/src/u32/uvec3.rs @@ -359,6 +359,8 @@ impl UVec3 { /// # Overflow /// This method may overflow if the result is greater than [`u32::MAX`]. /// + /// See also [`checked_manhattan_distance`][UVec3::checked_manhattan_distance]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] @@ -366,6 +368,23 @@ impl UVec3 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) } + /// Computes the [manhattan distance] between two points. + /// + /// This will returns [`None`] if the result is greater than [`u32::MAX`]. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + #[must_use] + pub fn checked_manhattan_distance(self, other: Self) -> Option { + let d = self.x.abs_diff(other.x); + + let d = d.checked_add(self.y.abs_diff(other.y))?; + + let d = d.checked_add(self.z.abs_diff(other.z))?; + + d + } + /// Computes the [chebyshev distance] between two points. /// /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance diff --git a/src/u32/uvec4.rs b/src/u32/uvec4.rs index 9199f5da..d203b3f3 100644 --- a/src/u32/uvec4.rs +++ b/src/u32/uvec4.rs @@ -383,6 +383,8 @@ impl UVec4 { /// # Overflow /// This method may overflow if the result is greater than [`u32::MAX`]. /// + /// See also [`checked_manhattan_distance`][UVec4::checked_manhattan_distance]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] @@ -393,6 +395,25 @@ impl UVec4 { + self.w.abs_diff(other.w) } + /// Computes the [manhattan distance] between two points. + /// + /// This will returns [`None`] if the result is greater than [`u32::MAX`]. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + #[must_use] + pub fn checked_manhattan_distance(self, other: Self) -> Option { + let d = self.x.abs_diff(other.x); + + let d = d.checked_add(self.y.abs_diff(other.y))?; + + let d = d.checked_add(self.z.abs_diff(other.z))?; + + let d = d.checked_add(self.w.abs_diff(other.w))?; + + d + } + /// Computes the [chebyshev distance] between two points. /// /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance diff --git a/src/u64/u64vec2.rs b/src/u64/u64vec2.rs index 07e9bc36..ea287975 100644 --- a/src/u64/u64vec2.rs +++ b/src/u64/u64vec2.rs @@ -310,6 +310,8 @@ impl U64Vec2 { /// # Overflow /// This method may overflow if the result is greater than [`u64::MAX`]. /// + /// See also [`checked_manhattan_distance`][U64Vec2::checked_manhattan_distance]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] @@ -317,6 +319,21 @@ impl U64Vec2 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) } + /// Computes the [manhattan distance] between two points. + /// + /// This will returns [`None`] if the result is greater than [`u64::MAX`]. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + #[must_use] + pub fn checked_manhattan_distance(self, other: Self) -> Option { + let d = self.x.abs_diff(other.x); + + let d = d.checked_add(self.y.abs_diff(other.y))?; + + d + } + /// Computes the [chebyshev distance] between two points. /// /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance diff --git a/src/u64/u64vec3.rs b/src/u64/u64vec3.rs index 815a7552..acf62000 100644 --- a/src/u64/u64vec3.rs +++ b/src/u64/u64vec3.rs @@ -359,6 +359,8 @@ impl U64Vec3 { /// # Overflow /// This method may overflow if the result is greater than [`u64::MAX`]. /// + /// See also [`checked_manhattan_distance`][U64Vec3::checked_manhattan_distance]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] @@ -366,6 +368,23 @@ impl U64Vec3 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) } + /// Computes the [manhattan distance] between two points. + /// + /// This will returns [`None`] if the result is greater than [`u64::MAX`]. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + #[must_use] + pub fn checked_manhattan_distance(self, other: Self) -> Option { + let d = self.x.abs_diff(other.x); + + let d = d.checked_add(self.y.abs_diff(other.y))?; + + let d = d.checked_add(self.z.abs_diff(other.z))?; + + d + } + /// Computes the [chebyshev distance] between two points. /// /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance diff --git a/src/u64/u64vec4.rs b/src/u64/u64vec4.rs index 60ea0404..6eb27dc3 100644 --- a/src/u64/u64vec4.rs +++ b/src/u64/u64vec4.rs @@ -383,6 +383,8 @@ impl U64Vec4 { /// # Overflow /// This method may overflow if the result is greater than [`u64::MAX`]. /// + /// See also [`checked_manhattan_distance`][U64Vec4::checked_manhattan_distance]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] @@ -393,6 +395,25 @@ impl U64Vec4 { + self.w.abs_diff(other.w) } + /// Computes the [manhattan distance] between two points. + /// + /// This will returns [`None`] if the result is greater than [`u64::MAX`]. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + #[must_use] + pub fn checked_manhattan_distance(self, other: Self) -> Option { + let d = self.x.abs_diff(other.x); + + let d = d.checked_add(self.y.abs_diff(other.y))?; + + let d = d.checked_add(self.z.abs_diff(other.z))?; + + let d = d.checked_add(self.w.abs_diff(other.w))?; + + d + } + /// Computes the [chebyshev distance] between two points. /// /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance diff --git a/src/u8/u8vec2.rs b/src/u8/u8vec2.rs index f28e5888..7ed33c77 100644 --- a/src/u8/u8vec2.rs +++ b/src/u8/u8vec2.rs @@ -310,6 +310,8 @@ impl U8Vec2 { /// # Overflow /// This method may overflow if the result is greater than [`u8::MAX`]. /// + /// See also [`checked_manhattan_distance`][U8Vec2::checked_manhattan_distance]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] @@ -317,6 +319,21 @@ impl U8Vec2 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) } + /// Computes the [manhattan distance] between two points. + /// + /// This will returns [`None`] if the result is greater than [`u8::MAX`]. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + #[must_use] + pub fn checked_manhattan_distance(self, other: Self) -> Option { + let d = self.x.abs_diff(other.x); + + let d = d.checked_add(self.y.abs_diff(other.y))?; + + d + } + /// Computes the [chebyshev distance] between two points. /// /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance diff --git a/src/u8/u8vec3.rs b/src/u8/u8vec3.rs index b63df051..92670877 100644 --- a/src/u8/u8vec3.rs +++ b/src/u8/u8vec3.rs @@ -359,6 +359,8 @@ impl U8Vec3 { /// # Overflow /// This method may overflow if the result is greater than [`u8::MAX`]. /// + /// See also [`checked_manhattan_distance`][U8Vec3::checked_manhattan_distance]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] @@ -366,6 +368,23 @@ impl U8Vec3 { self.x.abs_diff(other.x) + self.y.abs_diff(other.y) + self.z.abs_diff(other.z) } + /// Computes the [manhattan distance] between two points. + /// + /// This will returns [`None`] if the result is greater than [`u8::MAX`]. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + #[must_use] + pub fn checked_manhattan_distance(self, other: Self) -> Option { + let d = self.x.abs_diff(other.x); + + let d = d.checked_add(self.y.abs_diff(other.y))?; + + let d = d.checked_add(self.z.abs_diff(other.z))?; + + d + } + /// Computes the [chebyshev distance] between two points. /// /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance diff --git a/src/u8/u8vec4.rs b/src/u8/u8vec4.rs index c8b89a5a..e0cee0ca 100644 --- a/src/u8/u8vec4.rs +++ b/src/u8/u8vec4.rs @@ -383,6 +383,8 @@ impl U8Vec4 { /// # Overflow /// This method may overflow if the result is greater than [`u8::MAX`]. /// + /// See also [`checked_manhattan_distance`][U8Vec4::checked_manhattan_distance]. + /// /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry #[inline] #[must_use] @@ -393,6 +395,25 @@ impl U8Vec4 { + self.w.abs_diff(other.w) } + /// Computes the [manhattan distance] between two points. + /// + /// This will returns [`None`] if the result is greater than [`u8::MAX`]. + /// + /// [manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry + #[inline] + #[must_use] + pub fn checked_manhattan_distance(self, other: Self) -> Option { + let d = self.x.abs_diff(other.x); + + let d = d.checked_add(self.y.abs_diff(other.y))?; + + let d = d.checked_add(self.z.abs_diff(other.z))?; + + let d = d.checked_add(self.w.abs_diff(other.w))?; + + d + } + /// Computes the [chebyshev distance] between two points. /// /// [chebyshev distance]: https://en.wikipedia.org/wiki/Chebyshev_distance From c72224ee13300583065ee867fb040643b05d04b1 Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Sun, 22 Dec 2024 10:29:12 +0100 Subject: [PATCH 7/8] fix checked_manhattan_distance --- codegen/templates/vec.rs.tera | 10 +++++----- src/i16/i16vec2.rs | 5 +---- src/i16/i16vec3.rs | 6 +----- src/i16/i16vec4.rs | 7 +------ src/i32/ivec2.rs | 5 +---- src/i32/ivec3.rs | 6 +----- src/i32/ivec4.rs | 7 +------ src/i64/i64vec2.rs | 5 +---- src/i64/i64vec3.rs | 6 +----- src/i64/i64vec4.rs | 7 +------ src/i8/i8vec2.rs | 5 +---- src/i8/i8vec3.rs | 6 +----- src/i8/i8vec4.rs | 7 +------ src/u16/u16vec2.rs | 5 +---- src/u16/u16vec3.rs | 6 +----- src/u16/u16vec4.rs | 7 +------ src/u32/uvec2.rs | 5 +---- src/u32/uvec3.rs | 6 +----- src/u32/uvec4.rs | 7 +------ src/u64/u64vec2.rs | 5 +---- src/u64/u64vec3.rs | 6 +----- src/u64/u64vec4.rs | 7 +------ src/u8/u8vec2.rs | 5 +---- src/u8/u8vec3.rs | 6 +----- src/u8/u8vec4.rs | 7 +------ 25 files changed, 29 insertions(+), 125 deletions(-) diff --git a/codegen/templates/vec.rs.tera b/codegen/templates/vec.rs.tera index ff7ee3af..83f1bd79 100644 --- a/codegen/templates/vec.rs.tera +++ b/codegen/templates/vec.rs.tera @@ -2202,14 +2202,14 @@ impl {{ self_t }} { #[must_use] pub fn checked_manhattan_distance(self, other: Self) -> Option<{{ unsigned_scalar_t }}> { {% for c in components %} - {% if loop.first %} + {%- if loop.first -%} let d = self.{{c}}.abs_diff(other.{{c}}); - {% else %} + {%- elif loop.last -%} + d.checked_add(self.{{c}}.abs_diff(other.{{c}})) + {%- else -%} let d = d.checked_add(self.{{c}}.abs_diff(other.{{c}}))?; - {% endif %} + {%- endif -%} {% endfor %} - - d } /// Computes the [chebyshev distance] between two points. diff --git a/src/i16/i16vec2.rs b/src/i16/i16vec2.rs index 1fe2ed08..45160836 100644 --- a/src/i16/i16vec2.rs +++ b/src/i16/i16vec2.rs @@ -400,10 +400,7 @@ impl I16Vec2 { #[must_use] pub fn checked_manhattan_distance(self, other: Self) -> Option { let d = self.x.abs_diff(other.x); - - let d = d.checked_add(self.y.abs_diff(other.y))?; - - d + d.checked_add(self.y.abs_diff(other.y)) } /// Computes the [chebyshev distance] between two points. diff --git a/src/i16/i16vec3.rs b/src/i16/i16vec3.rs index e3f6a008..b29a2146 100644 --- a/src/i16/i16vec3.rs +++ b/src/i16/i16vec3.rs @@ -464,12 +464,8 @@ impl I16Vec3 { #[must_use] pub fn checked_manhattan_distance(self, other: Self) -> Option { let d = self.x.abs_diff(other.x); - let d = d.checked_add(self.y.abs_diff(other.y))?; - - let d = d.checked_add(self.z.abs_diff(other.z))?; - - d + d.checked_add(self.z.abs_diff(other.z)) } /// Computes the [chebyshev distance] between two points. diff --git a/src/i16/i16vec4.rs b/src/i16/i16vec4.rs index b8c9f7de..de137d66 100644 --- a/src/i16/i16vec4.rs +++ b/src/i16/i16vec4.rs @@ -499,14 +499,9 @@ impl I16Vec4 { #[must_use] pub fn checked_manhattan_distance(self, other: Self) -> Option { let d = self.x.abs_diff(other.x); - let d = d.checked_add(self.y.abs_diff(other.y))?; - let d = d.checked_add(self.z.abs_diff(other.z))?; - - let d = d.checked_add(self.w.abs_diff(other.w))?; - - d + d.checked_add(self.w.abs_diff(other.w)) } /// Computes the [chebyshev distance] between two points. diff --git a/src/i32/ivec2.rs b/src/i32/ivec2.rs index 672f147b..5743abb8 100644 --- a/src/i32/ivec2.rs +++ b/src/i32/ivec2.rs @@ -400,10 +400,7 @@ impl IVec2 { #[must_use] pub fn checked_manhattan_distance(self, other: Self) -> Option { let d = self.x.abs_diff(other.x); - - let d = d.checked_add(self.y.abs_diff(other.y))?; - - d + d.checked_add(self.y.abs_diff(other.y)) } /// Computes the [chebyshev distance] between two points. diff --git a/src/i32/ivec3.rs b/src/i32/ivec3.rs index 28e3063e..e89ef0bb 100644 --- a/src/i32/ivec3.rs +++ b/src/i32/ivec3.rs @@ -464,12 +464,8 @@ impl IVec3 { #[must_use] pub fn checked_manhattan_distance(self, other: Self) -> Option { let d = self.x.abs_diff(other.x); - let d = d.checked_add(self.y.abs_diff(other.y))?; - - let d = d.checked_add(self.z.abs_diff(other.z))?; - - d + d.checked_add(self.z.abs_diff(other.z)) } /// Computes the [chebyshev distance] between two points. diff --git a/src/i32/ivec4.rs b/src/i32/ivec4.rs index 90785682..1da0f928 100644 --- a/src/i32/ivec4.rs +++ b/src/i32/ivec4.rs @@ -499,14 +499,9 @@ impl IVec4 { #[must_use] pub fn checked_manhattan_distance(self, other: Self) -> Option { let d = self.x.abs_diff(other.x); - let d = d.checked_add(self.y.abs_diff(other.y))?; - let d = d.checked_add(self.z.abs_diff(other.z))?; - - let d = d.checked_add(self.w.abs_diff(other.w))?; - - d + d.checked_add(self.w.abs_diff(other.w)) } /// Computes the [chebyshev distance] between two points. diff --git a/src/i64/i64vec2.rs b/src/i64/i64vec2.rs index 623d1170..8c110460 100644 --- a/src/i64/i64vec2.rs +++ b/src/i64/i64vec2.rs @@ -400,10 +400,7 @@ impl I64Vec2 { #[must_use] pub fn checked_manhattan_distance(self, other: Self) -> Option { let d = self.x.abs_diff(other.x); - - let d = d.checked_add(self.y.abs_diff(other.y))?; - - d + d.checked_add(self.y.abs_diff(other.y)) } /// Computes the [chebyshev distance] between two points. diff --git a/src/i64/i64vec3.rs b/src/i64/i64vec3.rs index 13154080..86188699 100644 --- a/src/i64/i64vec3.rs +++ b/src/i64/i64vec3.rs @@ -464,12 +464,8 @@ impl I64Vec3 { #[must_use] pub fn checked_manhattan_distance(self, other: Self) -> Option { let d = self.x.abs_diff(other.x); - let d = d.checked_add(self.y.abs_diff(other.y))?; - - let d = d.checked_add(self.z.abs_diff(other.z))?; - - d + d.checked_add(self.z.abs_diff(other.z)) } /// Computes the [chebyshev distance] between two points. diff --git a/src/i64/i64vec4.rs b/src/i64/i64vec4.rs index b878b34d..bb78b6b1 100644 --- a/src/i64/i64vec4.rs +++ b/src/i64/i64vec4.rs @@ -499,14 +499,9 @@ impl I64Vec4 { #[must_use] pub fn checked_manhattan_distance(self, other: Self) -> Option { let d = self.x.abs_diff(other.x); - let d = d.checked_add(self.y.abs_diff(other.y))?; - let d = d.checked_add(self.z.abs_diff(other.z))?; - - let d = d.checked_add(self.w.abs_diff(other.w))?; - - d + d.checked_add(self.w.abs_diff(other.w)) } /// Computes the [chebyshev distance] between two points. diff --git a/src/i8/i8vec2.rs b/src/i8/i8vec2.rs index aef6fba0..284aac0f 100644 --- a/src/i8/i8vec2.rs +++ b/src/i8/i8vec2.rs @@ -400,10 +400,7 @@ impl I8Vec2 { #[must_use] pub fn checked_manhattan_distance(self, other: Self) -> Option { let d = self.x.abs_diff(other.x); - - let d = d.checked_add(self.y.abs_diff(other.y))?; - - d + d.checked_add(self.y.abs_diff(other.y)) } /// Computes the [chebyshev distance] between two points. diff --git a/src/i8/i8vec3.rs b/src/i8/i8vec3.rs index 8194d3a1..400ac933 100644 --- a/src/i8/i8vec3.rs +++ b/src/i8/i8vec3.rs @@ -464,12 +464,8 @@ impl I8Vec3 { #[must_use] pub fn checked_manhattan_distance(self, other: Self) -> Option { let d = self.x.abs_diff(other.x); - let d = d.checked_add(self.y.abs_diff(other.y))?; - - let d = d.checked_add(self.z.abs_diff(other.z))?; - - d + d.checked_add(self.z.abs_diff(other.z)) } /// Computes the [chebyshev distance] between two points. diff --git a/src/i8/i8vec4.rs b/src/i8/i8vec4.rs index f320e539..5ba0ce76 100644 --- a/src/i8/i8vec4.rs +++ b/src/i8/i8vec4.rs @@ -499,14 +499,9 @@ impl I8Vec4 { #[must_use] pub fn checked_manhattan_distance(self, other: Self) -> Option { let d = self.x.abs_diff(other.x); - let d = d.checked_add(self.y.abs_diff(other.y))?; - let d = d.checked_add(self.z.abs_diff(other.z))?; - - let d = d.checked_add(self.w.abs_diff(other.w))?; - - d + d.checked_add(self.w.abs_diff(other.w)) } /// Computes the [chebyshev distance] between two points. diff --git a/src/u16/u16vec2.rs b/src/u16/u16vec2.rs index 0330784a..47987c96 100644 --- a/src/u16/u16vec2.rs +++ b/src/u16/u16vec2.rs @@ -328,10 +328,7 @@ impl U16Vec2 { #[must_use] pub fn checked_manhattan_distance(self, other: Self) -> Option { let d = self.x.abs_diff(other.x); - - let d = d.checked_add(self.y.abs_diff(other.y))?; - - d + d.checked_add(self.y.abs_diff(other.y)) } /// Computes the [chebyshev distance] between two points. diff --git a/src/u16/u16vec3.rs b/src/u16/u16vec3.rs index 145e38bd..5632671b 100644 --- a/src/u16/u16vec3.rs +++ b/src/u16/u16vec3.rs @@ -377,12 +377,8 @@ impl U16Vec3 { #[must_use] pub fn checked_manhattan_distance(self, other: Self) -> Option { let d = self.x.abs_diff(other.x); - let d = d.checked_add(self.y.abs_diff(other.y))?; - - let d = d.checked_add(self.z.abs_diff(other.z))?; - - d + d.checked_add(self.z.abs_diff(other.z)) } /// Computes the [chebyshev distance] between two points. diff --git a/src/u16/u16vec4.rs b/src/u16/u16vec4.rs index 418cf012..60c94aea 100644 --- a/src/u16/u16vec4.rs +++ b/src/u16/u16vec4.rs @@ -404,14 +404,9 @@ impl U16Vec4 { #[must_use] pub fn checked_manhattan_distance(self, other: Self) -> Option { let d = self.x.abs_diff(other.x); - let d = d.checked_add(self.y.abs_diff(other.y))?; - let d = d.checked_add(self.z.abs_diff(other.z))?; - - let d = d.checked_add(self.w.abs_diff(other.w))?; - - d + d.checked_add(self.w.abs_diff(other.w)) } /// Computes the [chebyshev distance] between two points. diff --git a/src/u32/uvec2.rs b/src/u32/uvec2.rs index 890f1e36..1752654d 100644 --- a/src/u32/uvec2.rs +++ b/src/u32/uvec2.rs @@ -328,10 +328,7 @@ impl UVec2 { #[must_use] pub fn checked_manhattan_distance(self, other: Self) -> Option { let d = self.x.abs_diff(other.x); - - let d = d.checked_add(self.y.abs_diff(other.y))?; - - d + d.checked_add(self.y.abs_diff(other.y)) } /// Computes the [chebyshev distance] between two points. diff --git a/src/u32/uvec3.rs b/src/u32/uvec3.rs index 01b14c0a..98b5c9b2 100644 --- a/src/u32/uvec3.rs +++ b/src/u32/uvec3.rs @@ -377,12 +377,8 @@ impl UVec3 { #[must_use] pub fn checked_manhattan_distance(self, other: Self) -> Option { let d = self.x.abs_diff(other.x); - let d = d.checked_add(self.y.abs_diff(other.y))?; - - let d = d.checked_add(self.z.abs_diff(other.z))?; - - d + d.checked_add(self.z.abs_diff(other.z)) } /// Computes the [chebyshev distance] between two points. diff --git a/src/u32/uvec4.rs b/src/u32/uvec4.rs index d203b3f3..077d4aff 100644 --- a/src/u32/uvec4.rs +++ b/src/u32/uvec4.rs @@ -404,14 +404,9 @@ impl UVec4 { #[must_use] pub fn checked_manhattan_distance(self, other: Self) -> Option { let d = self.x.abs_diff(other.x); - let d = d.checked_add(self.y.abs_diff(other.y))?; - let d = d.checked_add(self.z.abs_diff(other.z))?; - - let d = d.checked_add(self.w.abs_diff(other.w))?; - - d + d.checked_add(self.w.abs_diff(other.w)) } /// Computes the [chebyshev distance] between two points. diff --git a/src/u64/u64vec2.rs b/src/u64/u64vec2.rs index ea287975..5461fc9d 100644 --- a/src/u64/u64vec2.rs +++ b/src/u64/u64vec2.rs @@ -328,10 +328,7 @@ impl U64Vec2 { #[must_use] pub fn checked_manhattan_distance(self, other: Self) -> Option { let d = self.x.abs_diff(other.x); - - let d = d.checked_add(self.y.abs_diff(other.y))?; - - d + d.checked_add(self.y.abs_diff(other.y)) } /// Computes the [chebyshev distance] between two points. diff --git a/src/u64/u64vec3.rs b/src/u64/u64vec3.rs index acf62000..92391ff4 100644 --- a/src/u64/u64vec3.rs +++ b/src/u64/u64vec3.rs @@ -377,12 +377,8 @@ impl U64Vec3 { #[must_use] pub fn checked_manhattan_distance(self, other: Self) -> Option { let d = self.x.abs_diff(other.x); - let d = d.checked_add(self.y.abs_diff(other.y))?; - - let d = d.checked_add(self.z.abs_diff(other.z))?; - - d + d.checked_add(self.z.abs_diff(other.z)) } /// Computes the [chebyshev distance] between two points. diff --git a/src/u64/u64vec4.rs b/src/u64/u64vec4.rs index 6eb27dc3..3f3ebb22 100644 --- a/src/u64/u64vec4.rs +++ b/src/u64/u64vec4.rs @@ -404,14 +404,9 @@ impl U64Vec4 { #[must_use] pub fn checked_manhattan_distance(self, other: Self) -> Option { let d = self.x.abs_diff(other.x); - let d = d.checked_add(self.y.abs_diff(other.y))?; - let d = d.checked_add(self.z.abs_diff(other.z))?; - - let d = d.checked_add(self.w.abs_diff(other.w))?; - - d + d.checked_add(self.w.abs_diff(other.w)) } /// Computes the [chebyshev distance] between two points. diff --git a/src/u8/u8vec2.rs b/src/u8/u8vec2.rs index 7ed33c77..4a93faa4 100644 --- a/src/u8/u8vec2.rs +++ b/src/u8/u8vec2.rs @@ -328,10 +328,7 @@ impl U8Vec2 { #[must_use] pub fn checked_manhattan_distance(self, other: Self) -> Option { let d = self.x.abs_diff(other.x); - - let d = d.checked_add(self.y.abs_diff(other.y))?; - - d + d.checked_add(self.y.abs_diff(other.y)) } /// Computes the [chebyshev distance] between two points. diff --git a/src/u8/u8vec3.rs b/src/u8/u8vec3.rs index 92670877..ecaa95a0 100644 --- a/src/u8/u8vec3.rs +++ b/src/u8/u8vec3.rs @@ -377,12 +377,8 @@ impl U8Vec3 { #[must_use] pub fn checked_manhattan_distance(self, other: Self) -> Option { let d = self.x.abs_diff(other.x); - let d = d.checked_add(self.y.abs_diff(other.y))?; - - let d = d.checked_add(self.z.abs_diff(other.z))?; - - d + d.checked_add(self.z.abs_diff(other.z)) } /// Computes the [chebyshev distance] between two points. diff --git a/src/u8/u8vec4.rs b/src/u8/u8vec4.rs index e0cee0ca..7593e3a9 100644 --- a/src/u8/u8vec4.rs +++ b/src/u8/u8vec4.rs @@ -404,14 +404,9 @@ impl U8Vec4 { #[must_use] pub fn checked_manhattan_distance(self, other: Self) -> Option { let d = self.x.abs_diff(other.x); - let d = d.checked_add(self.y.abs_diff(other.y))?; - let d = d.checked_add(self.z.abs_diff(other.z))?; - - let d = d.checked_add(self.w.abs_diff(other.w))?; - - d + d.checked_add(self.w.abs_diff(other.w)) } /// Computes the [chebyshev distance] between two points. From d438ee876b09cae804b19c29106e79b5fa0bdc8d Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Sun, 22 Dec 2024 10:33:55 +0100 Subject: [PATCH 8/8] add tests for checked_manhattan_distance --- tests/vec2.rs | 34 ++++++++++++++++++++++++++++++++++ tests/vec3.rs | 40 ++++++++++++++++++++++++++++++++++++++++ tests/vec4.rs | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) diff --git a/tests/vec2.rs b/tests/vec2.rs index bc0c0be1..9c4a63b4 100644 --- a/tests/vec2.rs +++ b/tests/vec2.rs @@ -742,6 +742,25 @@ macro_rules! impl_vec2_signed_integer_tests { $vec2::new(-8, -23).manhattan_distance($vec2::new(12, 7)), 50 ); + + assert_eq!( + $vec2::new(5, 2).checked_manhattan_distance($vec2::new(23, 16)), + Some(32) + ); + assert_eq!( + $vec2::new(30, 11).checked_manhattan_distance($vec2::new(30, 11)), + Some(0) + ); + assert_eq!( + $vec2::new(-8, -23).checked_manhattan_distance($vec2::new(12, 7)), + Some(50) + ); + + assert_eq!( + $vec2::new($t::MIN, $t::MIN) + .checked_manhattan_distance($vec2::new($t::MAX, $t::MAX)), + None + ); }); glam_test!(test_chebyshev_distance, { @@ -762,6 +781,21 @@ macro_rules! impl_vec2_unsigned_integer_tests { glam_test!(test_manhattan_distance, { assert_eq!($vec2::new(5, 2).manhattan_distance($vec2::new(23, 16)), 32); assert_eq!($vec2::new(30, 11).manhattan_distance($vec2::new(30, 11)), 0); + + assert_eq!( + $vec2::new(5, 2).checked_manhattan_distance($vec2::new(23, 16)), + Some(32) + ); + assert_eq!( + $vec2::new(30, 11).checked_manhattan_distance($vec2::new(30, 11)), + Some(0) + ); + + assert_eq!( + $vec2::new($t::MIN, $t::MIN) + .checked_manhattan_distance($vec2::new($t::MAX, $t::MAX)), + None + ); }); glam_test!(test_chebyshev_distance, { diff --git a/tests/vec3.rs b/tests/vec3.rs index 40507204..05cbc95b 100644 --- a/tests/vec3.rs +++ b/tests/vec3.rs @@ -863,6 +863,28 @@ macro_rules! impl_vec3_signed_integer_tests { $vec3::new(-23, 2, -99).manhattan_distance($vec3::new(22, -12, 24)), 182 ); + + assert_eq!( + $vec3::new(3, 27, 98).checked_manhattan_distance($vec3::new(20, 65, 97)), + Some(56) + ); + assert_eq!( + $vec3::new(8, 12, 12).checked_manhattan_distance($vec3::new(24, 56, 2)), + Some(70) + ); + assert_eq!( + $vec3::new(-23, 2, -99).checked_manhattan_distance($vec3::new(22, -12, 24)), + Some(182) + ); + + assert_eq!( + $vec3::new($t::MIN, $t::MIN, $t::MIN).checked_manhattan_distance($vec3::new( + $t::MAX, + $t::MAX, + $t::MAX + )), + None + ); }); glam_test!(test_chebyshev_distance, { @@ -895,6 +917,24 @@ macro_rules! impl_vec3_unsigned_integer_tests { $vec3::new(8, 12, 12).manhattan_distance($vec3::new(24, 56, 2)), 70 ); + + assert_eq!( + $vec3::new(3, 27, 98).checked_manhattan_distance($vec3::new(20, 65, 97)), + Some(56) + ); + assert_eq!( + $vec3::new(8, 12, 12).checked_manhattan_distance($vec3::new(24, 56, 2)), + Some(70) + ); + + assert_eq!( + $vec3::new($t::MIN, $t::MIN, $t::MIN).checked_manhattan_distance($vec3::new( + $t::MAX, + $t::MAX, + $t::MAX + )), + None + ); }); glam_test!(test_chebyshev_distance, { diff --git a/tests/vec4.rs b/tests/vec4.rs index f2f5e9e3..75227516 100644 --- a/tests/vec4.rs +++ b/tests/vec4.rs @@ -982,6 +982,26 @@ macro_rules! impl_vec4_signed_integer_tests { $vec4::new(26, 2, 24, -22).manhattan_distance($vec4::new(26, 23, 6, 23)), 84 ); + + assert_eq!( + $vec4::new(41, 8, 21, 87).checked_manhattan_distance($vec4::new(49, 48, 28, 40)), + Some(102) + ); + assert_eq!( + $vec4::new(19, 16, 100, 74).checked_manhattan_distance($vec4::new(14, 55, 115, 48)), + Some(85) + ); + + assert_eq!( + $vec4::new(26, 2, 24, -22).checked_manhattan_distance($vec4::new(26, 23, 6, 23)), + Some(84) + ); + + assert_eq!( + $vec4::new($t::MIN, $t::MIN, $t::MIN, $t::MIN) + .checked_manhattan_distance($vec4::new($t::MAX, $t::MAX, $t::MAX, $t::MAX)), + None + ); }); glam_test!(test_chebyshev_distance, { @@ -1014,6 +1034,22 @@ macro_rules! impl_vec4_unsigned_integer_tests { $vec4::new(19, 16, 179, 174).manhattan_distance($vec4::new(14, 55, 115, 148)), 134 ); + + assert_eq!( + $vec4::new(41, 8, 21, 87).checked_manhattan_distance($vec4::new(49, 48, 128, 40)), + Some(202) + ); + assert_eq!( + $vec4::new(19, 16, 179, 174) + .checked_manhattan_distance($vec4::new(14, 55, 115, 148)), + Some(134) + ); + + assert_eq!( + $vec4::new($t::MIN, $t::MIN, $t::MIN, $t::MIN) + .checked_manhattan_distance($vec4::new($t::MAX, $t::MAX, $t::MAX, $t::MAX)), + None + ); }); glam_test!(test_chebyshev_distance, {