Skip to content

Commit

Permalink
Serialize DQ to [N;8]
Browse files Browse the repository at this point in the history
Closes #816
  • Loading branch information
chinedufn committed Dec 18, 2020
1 parent d8fa3ff commit b951325
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
36 changes: 35 additions & 1 deletion 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.
///
Expand Down Expand Up @@ -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>,
Expand Down Expand Up @@ -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]),
})
}
}
10 changes: 8 additions & 2 deletions tests/core/matrixcompare.rs
Expand Up @@ -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! {
Expand Down

0 comments on commit b951325

Please sign in to comment.