Skip to content

Commit 1cdb36d

Browse files
patowenRalith
authored andcommitted
Replace to_f32 and to_f64 with cast
This requires the direct import of the simba library, which used to be indirectly imported via nalgebra, because nalgebra does not expose the SupersetOf trait.
1 parent 9f5cf0d commit 1cdb36d

File tree

4 files changed

+17
-31
lines changed

4 files changed

+17
-31
lines changed

common/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ tracing-subscriber = { version = "0.3.15", default-features = false, features =
2323
rand = "0.9.0"
2424
rand_pcg = "0.9.0"
2525
rand_distr = "0.5.0"
26+
simba = "0.9.0"
2627

2728
[dev-dependencies]
2829
approx = "0.5.1"

common/src/dodeca.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -524,16 +524,16 @@ mod data {
524524
LazyLock::new(|| Vertex::VALUES.map(|vertex| vertex.dual_to_node().parity()));
525525

526526
pub static SIDE_NORMALS_F32: LazyLock<[MVector<f32>; Side::VALUES.len()]> =
527-
LazyLock::new(|| SIDE_NORMALS_F64.map(|n| n.to_f32()));
527+
LazyLock::new(|| SIDE_NORMALS_F64.map(|n| n.cast()));
528528

529529
pub static REFLECTIONS_F32: LazyLock<[MIsometry<f32>; Side::VALUES.len()]> =
530-
LazyLock::new(|| REFLECTIONS_F64.map(|n| n.to_f32()));
530+
LazyLock::new(|| REFLECTIONS_F64.map(|n| n.cast()));
531531

532532
pub static DUAL_TO_NODE_F32: LazyLock<[MIsometry<f32>; Vertex::VALUES.len()]> =
533-
LazyLock::new(|| DUAL_TO_NODE_F64.map(|n| n.to_f32()));
533+
LazyLock::new(|| DUAL_TO_NODE_F64.map(|n| n.cast()));
534534

535535
pub static NODE_TO_DUAL_F32: LazyLock<[MIsometry<f32>; Vertex::VALUES.len()]> =
536-
LazyLock::new(|| NODE_TO_DUAL_F64.map(|n| n.to_f32()));
536+
LazyLock::new(|| NODE_TO_DUAL_F64.map(|n| n.cast()));
537537

538538
pub static DUAL_TO_CHUNK_FACTOR_F32: LazyLock<f32> =
539539
LazyLock::new(|| *DUAL_TO_CHUNK_FACTOR_F64 as f32);

common/src/math.rs

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
99
use na::{RealField, Scalar};
1010
use serde::{Deserialize, Serialize};
11+
use simba::scalar::SupersetOf;
1112
use std::ops::*;
1213

1314
/// A stack-allocated 4-dimensional column-vector in Minkowski space. Such
@@ -106,6 +107,12 @@ impl<N: RealField + Copy> MVector<N> {
106107
self.0 * na::RowVector4::new(other.x, other.y, other.z, -other.w)
107108
}
108109

110+
/// Cast the components of `self` to another type.
111+
#[inline]
112+
pub fn cast<N2: RealField + Copy + SupersetOf<N>>(self) -> MVector<N2> {
113+
MVector(self.0.cast())
114+
}
115+
109116
/// The column vector with components `[0, 0, 0, 0]`.
110117
#[inline]
111118
pub fn zero() -> Self {
@@ -158,20 +165,6 @@ impl<N: RealField + Copy> MVector<N> {
158165
}
159166
}
160167

161-
impl MVector<f32> {
162-
/// Casts the components to an `f64`
163-
pub fn to_f64(self) -> MVector<f64> {
164-
MVector(self.0.cast::<f64>())
165-
}
166-
}
167-
168-
impl MVector<f64> {
169-
/// Casts the components to an `f32`
170-
pub fn to_f32(self) -> MVector<f32> {
171-
MVector(self.0.cast::<f32>())
172-
}
173-
}
174-
175168
impl<N: Scalar> Index<usize> for MVector<N> {
176169
type Output = N;
177170
#[inline]
@@ -465,19 +458,11 @@ impl<N: RealField + Copy> MIsometry<N> {
465458
// orientation components.
466459
normalized_translation_component * normalized_orientation_component
467460
}
468-
}
469461

470-
impl MIsometry<f32> {
471-
/// Casts the components to an `f64`
472-
pub fn to_f64(self) -> MIsometry<f64> {
473-
MIsometry(self.0.cast::<f64>())
474-
}
475-
}
476-
477-
impl MIsometry<f64> {
478-
/// Casts the components to an `f32`
479-
pub fn to_f32(self) -> MIsometry<f32> {
480-
MIsometry(self.0.cast::<f32>())
462+
/// Cast the components of `self` to another type.
463+
#[inline]
464+
pub fn cast<N2: RealField + Copy + SupersetOf<N>>(self) -> MIsometry<N2> {
465+
MIsometry(self.0.cast())
481466
}
482467
}
483468

common/src/worldgen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl NodeState {
123123
}
124124

125125
pub fn up_direction(&self) -> MVector<f32> {
126-
self.surface.normal().to_f32()
126+
self.surface.normal().cast()
127127
}
128128
}
129129

0 commit comments

Comments
 (0)