Merge pull request #817 from chinedufn/dq-serde

Serialize DQ to [N;8]
This commit is contained in:
Sébastien Crozet 2020-12-20 15:51:17 +01:00 committed by GitHub
commit c1eeaf0cf1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 3 deletions

View File

@ -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<N: SimdRealField> {
/// The real component of the quaternion
pub real: Quaternion<N>,
@ -80,3 +81,36 @@ where
*self = self.normalize();
}
}
#[cfg(feature = "serde-serialize")]
impl<N: SimdRealField> Serialize for DualQuaternion<N>
where
N: Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where
S: Serializer,
{
self.as_ref().serialize(serializer)
}
}
#[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>,
{
type Dq<N> = [N; 8];
let dq: Dq<N> = Dq::<N>::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]),
})
}
}

View File

@ -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! {