Skip to content

Commit

Permalink
Add serde support to DQ
Browse files Browse the repository at this point in the history
  • Loading branch information
chinedufn committed Dec 17, 2020
1 parent ae09712 commit 0498ee9
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/geometry/dual_quaternion.rs
@@ -1,6 +1,9 @@
use crate::{Quaternion, RealField, Scalar, SimdRealField};
use simba::simd::SimdValue;

#[cfg(feature = "serde-serialize")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};

/// A dual quaternion.
///
/// NOTE:
Expand Down Expand Up @@ -78,3 +81,46 @@ where
}
}
}

#[cfg(feature = "serde-serialize")]
impl<N: SimdRealField> Serialize for DualQuaternion<N>
where
N: Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
[
self.rot.coords.w,
self.rot.coords.x,
self.rot.coords.y,
self.rot.coords.z,
self.trans.coords.w,
self.trans.coords.x,
self.trans.coords.y,
self.trans.coords.z,
]
.serialize(serializer)
}
}

type Quats<N> = [N; 8];

#[cfg(feature = "serde-serialize")]
impl<'a, N: SimdRealField> Deserialize<'a> for DualQuaternion<N>
where
N: Deserialize<'a>,
{
fn deserialize<Des>(deserializer: Des) -> Result<Self, Des::Error>
where
Des: Deserializer<'a>,
{
let dq = Quats::<N>::deserialize(deserializer)?;

Ok(Self {
rot: Quaternion::new(dq[0], dq[1], dq[2], dq[3]),
trans: Quaternion::new(dq[4], dq[5], dq[6], dq[7]),
})
}
}

0 comments on commit 0498ee9

Please sign in to comment.