Skip to content

Commit

Permalink
removed potentially unsound unsafe code
Browse files Browse the repository at this point in the history
  • Loading branch information
jsmith628 committed Mar 29, 2024
1 parent 5a1f9a5 commit 3235751
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/geometry/rotation_interpolation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ where
#[inline]
#[must_use]
pub fn slerp(&self, other: &Self, t: T) -> Self {
use std::mem::transmute;

//The best option here would be to use #[feature(specialization)], but until
//that's stabilized, this is the best we can do. Theoretically, the compiler should
Expand All @@ -124,21 +123,30 @@ where
//FIXME: this doesn't really work in 1D since we can't interp between -1 and 1
1 => self.clone(),

//NOTE: Not pretty, but without refactoring the API, this is the best we can do
//NOTE: This is safe because we directly check the dimension first
2 => unsafe {
let (self2d, other2d) = (
transmute::<&Self, &Rotation2<T>>(self),
transmute::<&Self, &Rotation2<T>>(other),
2 => {
let self2d = Rotation2::from_matrix_unchecked(
self.clone().into_inner().fixed_resize(T::zero())
);
transmute::<&Rotation2<T>, &Self>(&self2d.slerp_2d(other2d, t)).clone()
let other2d = Rotation2::from_matrix_unchecked(
other.clone().into_inner().fixed_resize(T::zero())
);

Self::from_matrix_unchecked(
self2d.slerp_2d(&other2d, t).into_inner().fixed_resize(T::zero())
)
},
3 => unsafe {
let (self3d, other3d) = (
transmute::<&Self, &Rotation3<T>>(self),
transmute::<&Self, &Rotation3<T>>(other),

3 => {
let self3d = Rotation3::from_matrix_unchecked(
self.clone().into_inner().fixed_resize(T::zero())
);
transmute::<&Rotation3<T>, &Self>(&self3d.slerp_3d(other3d, t)).clone()
let other3d = Rotation3::from_matrix_unchecked(
other.clone().into_inner().fixed_resize(T::zero())
);

Self::from_matrix_unchecked(
self3d.slerp_3d(&other3d, t).into_inner().fixed_resize(T::zero())
)
},

//the multiplication order matters here
Expand Down

0 comments on commit 3235751

Please sign in to comment.