Skip to content

Commit edea817

Browse files
authored
Glam 0.30 (#65)
* Fix Vector4::ONE * Bump to glam 0.30 * Vector3::slerp() * manhattan_distance(), chebyshev_distance() * Matrix4::from_mat3_translation() * Coverage: swizzle_with
1 parent 10f1f84 commit edea817

File tree

10 files changed

+210
-18
lines changed

10 files changed

+210
-18
lines changed

CHANGELOG.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10-
## Breaking changes
10+
### Fixes
11+
12+
- `Vector4::ONE.w` was accidentally zero.
1113

14+
### Breaking changes
15+
16+
- Bumped `glam` to 0.30. [Changelog](https://github.com/bitshifter/glam-rs/blob/main/CHANGELOG.md#0300---2025-02-18).
1217
- Bumped `wasmtime` dependency to 29.x.
18+
- Shuffled trait bounds between `IntUnit` and `IntScalar` to support mapping integers to
19+
their unsigned counterparts (needed for `manhattan_distance()` etc.).
1320

1421
## [0.15.0] - 2024-10-12
1522

16-
## Breaking changes
23+
### Breaking changes
1724

1825
- Bumped `wasmtime` dependency to 25.0.
1926
- Bumped `encase` dependency to 0.10.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ unexpected_cfgs = { level = "warn", check-cfg = [
2424
] }
2525

2626
[dependencies]
27-
glam = { version = "0.29.0", default-features = false, features = [
27+
glam = { version = "0.30.0", default-features = false, features = [
2828
"bytemuck",
2929
"approx",
3030
] }

src/bindings/vec.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use core::ops::Neg;
44

5-
use crate::Scalar;
5+
use crate::{scalar::IntScalar, Scalar};
66

77
use super::*;
88

@@ -59,14 +59,16 @@ macro_rules! impl_signed_vector {
5959
};
6060
}
6161

62-
pub trait IntegerVector: Vector + core::hash::Hash {
62+
pub trait IntegerVector: Vector<Scalar: IntScalar> + core::hash::Hash {
6363
crate::interfaces::vector_integer_interface!(trait_decl);
64+
crate::interfaces::point_int_interface!(trait_decl);
6465
}
6566

6667
macro_rules! impl_integer_vector {
6768
($glam_ty:ty) => {
6869
impl IntegerVector for $glam_ty {
6970
crate::interfaces::vector_integer_interface!(trait_impl);
71+
crate::interfaces::point_int_interface!(trait_impl);
7072
}
7173
};
7274
}

src/forward.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,21 @@ macro_rules! forward_fn_assoc {
250250
///
251251
/// The `struct` context is the public API of `glamour`.
252252
macro_rules! forward_ty {
253+
($mode:tt Option<$thing:tt>) => {
254+
Option<crate::forward_ty!($mode $thing)>
255+
};
253256
(trait scalar) => {
254257
Self::Scalar
255258
};
256259
(struct scalar) => {
257260
T::Scalar
258261
};
262+
(trait uscalar) => {
263+
<Self::Scalar as crate::scalar::IntScalar>::Unsigned
264+
};
265+
(struct uscalar) => {
266+
<T::Scalar as crate::scalar::IntScalar>::Unsigned
267+
};
259268
(trait angle) => {
260269
Self::Scalar
261270
};
@@ -456,12 +465,18 @@ macro_rules! forward_arg {
456465
}
457466

458467
macro_rules! wrap_ret_val {
468+
(Option<$thing:tt> => $arg:expr) => {
469+
$arg
470+
};
459471
($arg:expr) => {
460472
$arg
461473
};
462474
( scalar => $arg:expr) => {
463475
$arg
464476
};
477+
( uscalar => $arg:expr) => {
478+
$arg
479+
};
465480
( angle => $arg:expr) => {
466481
crate::wrap($arg)
467482
};

src/impl_vectorlike.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ macro_rules! simdlike {
205205
/// All zeroes.
206206
pub const ZERO: Self = Self::new(T::Scalar::ZERO, T::Scalar::ZERO, T::Scalar::ZERO, T::Scalar::ZERO);
207207
/// All ones.
208-
pub const ONE: Self = Self::new(T::Scalar::ONE, T::Scalar::ONE, T::Scalar::ONE, T::Scalar::ZERO);
208+
pub const ONE: Self = Self::new(T::Scalar::ONE, T::Scalar::ONE, T::Scalar::ONE, T::Scalar::ONE);
209209

210210
/// Unit vector in the direction of the X axis.
211211
pub const X: Self = Self::new(
@@ -376,6 +376,9 @@ macro_rules! pointlike {
376376
impl<T: crate::FloatUnit> $base_type_name<T> {
377377
crate::interfaces::point_float_interface!(struct);
378378
}
379+
impl<T: crate::IntUnit<Scalar: crate::scalar::IntScalar>> $base_type_name<T> {
380+
crate::interfaces::point_int_interface!(struct);
381+
}
379382
};
380383
(@for_size $base_type_name:ident, 2) => {
381384
impl<T: crate::Unit> $base_type_name<T> {
@@ -397,6 +400,7 @@ macro_rules! pointlike {
397400
};
398401
}
399402
pub(crate) use pointlike;
403+
400404
macro_rules! sizelike {
401405
($base_type_name:ident, $n:tt) => {
402406
crate::impl_vectorlike::simdlike!($base_type_name, $n);

src/interfaces.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,22 @@ macro_rules! point_float_interface {
228228
}
229229
pub(crate) use point_float_interface;
230230

231+
/// Interface for all geometric point-like things with integer components.
232+
macro_rules! point_int_interface {
233+
($mode:tt) => {
234+
crate::interface! {
235+
$mode =>
236+
/// Computes the manhattan distance between two points
237+
fn manhattan_distance(self, other: Self) -> uscalar;
238+
/// Computes the manhattan distance between two points
239+
fn checked_manhattan_distance(self, other: Self) -> Option<uscalar>;
240+
/// Compute the chebyshev distance between two points.
241+
fn chebyshev_distance(self, other: Self) -> uscalar;
242+
}
243+
};
244+
}
245+
pub(crate) use point_int_interface;
246+
231247
macro_rules! simd2_base_interface {
232248
($mode:tt, $larger:tt) => {
233249
crate::interface! {
@@ -416,6 +432,8 @@ macro_rules! vector3_float_interface {
416432
fn any_orthonormal_vector(&self) -> Self;
417433
/// See (e.g.) [`glam::Vec3::any_orthonormal_pair()`].
418434
fn any_orthonormal_pair(&self) -> (Self, Self);
435+
/// Performs a spherical linear interpolation between `self` and `rhs` based on the value `s`.
436+
fn slerp(self, rhs: Self, s: scalar) -> Self;
419437
}
420438
};
421439
}
@@ -778,6 +796,9 @@ macro_rules! matrix4_base_interface {
778796
fn to_cols_array_2d(&self) -> [[scalar; 4]; 4];
779797
/// Creates a 4x4 matrix from a `[T; 16]` array stored in column major order.
780798
fn from_cols_array(array: ref_scalar_array_16) -> Self;
799+
/// Creates an affine transformation matrics from a 3x3 matrix (expressing scale, shear and rotation) and a
800+
/// translation vector.
801+
fn from_mat3_translation(mat3: mat3, translation: vec3) -> Self;
781802
}
782803
}
783804
}

src/point.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,27 @@ mod tests {
414414
assert_eq!(p4, point!(1.0, 2.0, 3.0, 4.0));
415415
}
416416

417+
#[test]
418+
fn manhattan_distance() {
419+
let a: Point3<i16> = point![1, 1, 1];
420+
let b: Point3<i16> = point![2, 2, 2];
421+
let d: u16 = a.manhattan_distance(b);
422+
assert_eq!(d, 3);
423+
424+
let a: Point3<i16> = point![0, 0, 0];
425+
let b: Point3<i16> = point![i16::MAX, i16::MAX, 2];
426+
let d: Option<u16> = a.checked_manhattan_distance(b);
427+
assert_eq!(d, None);
428+
}
429+
430+
#[test]
431+
fn chebyshev_distance() {
432+
let a: Point3<i16> = point![1, 1, 1];
433+
let b: Point3<i16> = point![2, 2, 3];
434+
let d: u16 = a.chebyshev_distance(b);
435+
assert_eq!(d, 2);
436+
}
437+
417438
#[test]
418439
fn gaslight_coverage() {
419440
extern crate alloc;

src/scalar.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,8 @@ pub unsafe trait FloatScalar:
109109
}
110110

111111
/// Integer scalar types.
112-
pub trait IntScalar:
113-
Scalar<Vec2: bindings::IntegerVector, Vec3: bindings::IntegerVector, Vec4: bindings::IntegerVector>
114-
{
112+
pub trait IntScalar: Scalar + num_traits::PrimInt {
113+
type Unsigned: IntScalar + num_traits::Unsigned;
115114
}
116115

117116
unsafe impl Scalar for f32 {
@@ -160,7 +159,9 @@ unsafe impl Scalar for i16 {
160159
type Vec4 = glam::I16Vec4;
161160
}
162161

163-
impl IntScalar for i16 {}
162+
impl IntScalar for i16 {
163+
type Unsigned = u16;
164+
}
164165

165166
impl SignedScalar for i16 {
166167
const NEG_ONE: Self = -1;
@@ -172,7 +173,9 @@ unsafe impl Scalar for i32 {
172173
type Vec4 = glam::IVec4;
173174
}
174175

175-
impl IntScalar for i32 {}
176+
impl IntScalar for i32 {
177+
type Unsigned = u32;
178+
}
176179

177180
impl SignedScalar for i32 {
178181
const NEG_ONE: Self = -1;
@@ -184,7 +187,9 @@ unsafe impl Scalar for i64 {
184187
type Vec4 = glam::I64Vec4;
185188
}
186189

187-
impl IntScalar for i64 {}
190+
impl IntScalar for i64 {
191+
type Unsigned = u64;
192+
}
188193

189194
impl SignedScalar for i64 {
190195
const NEG_ONE: Self = -1;
@@ -196,23 +201,29 @@ unsafe impl Scalar for u16 {
196201
type Vec4 = glam::U16Vec4;
197202
}
198203

199-
impl IntScalar for u16 {}
204+
impl IntScalar for u16 {
205+
type Unsigned = u16;
206+
}
200207

201208
unsafe impl Scalar for u32 {
202209
type Vec2 = glam::UVec2;
203210
type Vec3 = glam::UVec3;
204211
type Vec4 = glam::UVec4;
205212
}
206213

207-
impl IntScalar for u32 {}
214+
impl IntScalar for u32 {
215+
type Unsigned = u32;
216+
}
208217

209218
unsafe impl Scalar for u64 {
210219
type Vec2 = glam::U64Vec2;
211220
type Vec3 = glam::U64Vec3;
212221
type Vec4 = glam::U64Vec4;
213222
}
214223

215-
impl IntScalar for u64 {}
224+
impl IntScalar for u64 {
225+
type Unsigned = u64;
226+
}
216227

217228
#[cfg(test)]
218229
mod tests {

0 commit comments

Comments
 (0)