From b9513257b88dfe4a0243af5d6088fb6486dd5384 Mon Sep 17 00:00:00 2001 From: Chinedu Francis Nwafili Date: Fri, 18 Dec 2020 12:19:38 -0500 Subject: [PATCH] Serialize DQ to [N;8] Closes #816 --- src/geometry/dual_quaternion.rs | 36 ++++++++++++++++++++++++++++++++- tests/core/matrixcompare.rs | 10 +++++++-- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/geometry/dual_quaternion.rs b/src/geometry/dual_quaternion.rs index ce49244f..ce9f7284 100644 --- a/src/geometry/dual_quaternion.rs +++ b/src/geometry/dual_quaternion.rs @@ -1,4 +1,6 @@ use crate::{Quaternion, SimdRealField}; +#[cfg(feature = "serde-serialize")] +use serde::{Deserialize, Deserializer, Serialize, Serializer}; /// A dual quaternion. /// @@ -27,7 +29,6 @@ use crate::{Quaternion, SimdRealField}; /// See https://github.com/dimforge/nalgebra/issues/487 #[repr(C)] #[derive(Debug, Default, Eq, PartialEq, Copy, Clone)] -#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] pub struct DualQuaternion { /// The real component of the quaternion pub real: Quaternion, @@ -80,3 +81,36 @@ where *self = self.normalize(); } } + +#[cfg(feature = "serde-serialize")] +impl Serialize for DualQuaternion +where + N: Serialize, +{ + fn serialize(&self, serializer: S) -> Result<::Ok, ::Error> + where + S: Serializer, + { + self.as_ref().serialize(serializer) + } +} + +#[cfg(feature = "serde-serialize")] +impl<'a, N: SimdRealField> Deserialize<'a> for DualQuaternion +where + N: Deserialize<'a>, +{ + fn deserialize(deserializer: Des) -> Result + where + Des: Deserializer<'a>, + { + type Dq = [N; 8]; + + let dq: Dq = Dq::::deserialize(deserializer)?; + + Ok(Self { + real: Quaternion::new(dq[3], dq[0], dq[1], dq[2]), + dual: Quaternion::new(dq[7], dq[4], dq[5], dq[6]), + }) + } +} diff --git a/tests/core/matrixcompare.rs b/tests/core/matrixcompare.rs index df112173..cdd93ea3 100644 --- a/tests/core/matrixcompare.rs +++ b/tests/core/matrixcompare.rs @@ -4,9 +4,15 @@ //! The tests here only check that the necessary trait implementations are correctly implemented, //! in addition to some sanity checks with example input. -use nalgebra::{DMatrix, MatrixMN, U4, U5}; +use nalgebra::{MatrixMN, U4, U5}; -use matrixcompare::{assert_matrix_eq, DenseAccess}; +#[cfg(feature = "arbitrary")] +use nalgebra::DMatrix; + +use matrixcompare::assert_matrix_eq; + +#[cfg(feature = "arbitrary")] +use matrixcompare::DenseAccess; #[cfg(feature = "arbitrary")] quickcheck! {