Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement manhattan and chebyshev distance for integer vectors #593

Merged
merged 8 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions codegen/templates/vec.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -2166,6 +2166,37 @@ 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 %}
}

/// 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 %}


{% if is_signed and dim == 2 %}
/// Returns a vector that is equal to `self` rotated by 90 degrees.
#[inline]
Expand Down
22 changes: 22 additions & 0 deletions src/i16/i16vec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,28 @@ 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]
#[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]
Expand Down
26 changes: 26 additions & 0 deletions src/i16/i16vec3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,32 @@ impl I16Vec3 {
)
}

/// 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) -> 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]
Expand Down
30 changes: 30 additions & 0 deletions src/i16/i16vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,36 @@ impl I16Vec4 {
)
}

/// 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) -> 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)
}

/// 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]
Expand Down
22 changes: 22 additions & 0 deletions src/i32/ivec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,28 @@ 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]
#[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]
Expand Down
26 changes: 26 additions & 0 deletions src/i32/ivec3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,32 @@ impl IVec3 {
)
}

/// 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) -> 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]
Expand Down
30 changes: 30 additions & 0 deletions src/i32/ivec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,36 @@ impl IVec4 {
)
}

/// 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) -> 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)
}

/// 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]
Expand Down
22 changes: 22 additions & 0 deletions src/i64/i64vec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,28 @@ 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]
#[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]
Expand Down
26 changes: 26 additions & 0 deletions src/i64/i64vec3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,32 @@ impl I64Vec3 {
)
}

/// 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) -> 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]
Expand Down
30 changes: 30 additions & 0 deletions src/i64/i64vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,36 @@ impl I64Vec4 {
)
}

/// 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) -> 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)
}

/// 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]
Expand Down
22 changes: 22 additions & 0 deletions src/i8/i8vec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,28 @@ 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]
#[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]
Expand Down
26 changes: 26 additions & 0 deletions src/i8/i8vec3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,32 @@ impl I8Vec3 {
)
}

/// 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) -> 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]
Expand Down
Loading
Loading