From e09af0ca82e77f157e64de922f4695a40b6e2dcf Mon Sep 17 00:00:00 2001 From: Eduard Bopp Date: Mon, 14 Aug 2017 14:32:02 +0200 Subject: [PATCH] Implement Abomonation for remaining types --- src/core/unit.rs | 18 ++++++++++++++++++ src/geometry/isometry.rs | 26 ++++++++++++++++++++++++++ src/geometry/quaternion.rs | 21 +++++++++++++++++++++ src/geometry/similarity.rs | 21 +++++++++++++++++++++ src/lib.rs | 3 +++ tests/abomonation.rs | 10 +++++++++- 6 files changed, 98 insertions(+), 1 deletion(-) diff --git a/src/core/unit.rs b/src/core/unit.rs index 6051165d..0d8035ef 100644 --- a/src/core/unit.rs +++ b/src/core/unit.rs @@ -5,6 +5,9 @@ use approx::ApproxEq; #[cfg(feature = "serde-serialize")] use serde::{Serialize, Serializer, Deserialize, Deserializer}; +#[cfg(feature = "abomonation-serialize")] +use abomonation::Abomonation; + use alga::general::SubsetOf; use alga::linear::NormedSpace; @@ -36,6 +39,21 @@ impl<'de, T: Deserialize<'de>> Deserialize<'de> for Unit { } } +#[cfg(feature = "abomonation-serialize")] +impl Abomonation for Unit { + unsafe fn entomb(&self, writer: &mut Vec) { + self.value.entomb(writer); + } + + unsafe fn embalm(&mut self) { + self.value.embalm(); + } + + unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { + self.value.exhume(bytes) + } +} + impl Unit { /// Normalize the given value and return it wrapped on a `Unit` structure. #[inline] diff --git a/src/geometry/isometry.rs b/src/geometry/isometry.rs index 67d2e7e8..9ae55ed9 100644 --- a/src/geometry/isometry.rs +++ b/src/geometry/isometry.rs @@ -11,6 +11,9 @@ use core::storage::{Storage, OwnedStorage}; use core::allocator::{Allocator, OwnedAllocator}; use geometry::{TranslationBase, PointBase}; +#[cfg(feature = "abomonation-serialize")] +use abomonation::Abomonation; + /// An isometry that uses a data storage deduced from the allocator `A`. pub type OwnedIsometryBase = @@ -32,6 +35,29 @@ pub struct IsometryBase { _noconstruct: PhantomData } +#[cfg(feature = "abomonation-serialize")] +impl Abomonation for IsometryBase + where N: Scalar, + D: DimName, + R: Abomonation, + TranslationBase: Abomonation +{ + unsafe fn entomb(&self, writer: &mut Vec) { + self.rotation.entomb(writer); + self.translation.entomb(writer); + } + + unsafe fn embalm(&mut self) { + self.rotation.embalm(); + self.translation.embalm(); + } + + unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { + self.rotation.exhume(bytes) + .and_then(|bytes| self.translation.exhume(bytes)) + } +} + impl IsometryBase where N: Real, S: OwnedStorage, diff --git a/src/geometry/quaternion.rs b/src/geometry/quaternion.rs index 4a99acc3..97482ab6 100644 --- a/src/geometry/quaternion.rs +++ b/src/geometry/quaternion.rs @@ -5,6 +5,9 @@ use approx::ApproxEq; #[cfg(feature = "serde-serialize")] use serde::{Serialize, Serializer, Deserialize, Deserializer}; +#[cfg(feature = "abomonation-serialize")] +use abomonation::Abomonation; + use alga::general::Real; use core::{Unit, ColumnVector, OwnedColumnVector, MatrixSlice, MatrixSliceMut, SquareMatrix, @@ -56,6 +59,24 @@ impl<'de, N, S> Deserialize<'de> for QuaternionBase } } +#[cfg(feature = "abomonation-serialize")] +impl Abomonation for QuaternionBase + where N: Real, + S: Storage, + ColumnVector: Abomonation +{ + unsafe fn entomb(&self, writer: &mut Vec) { + self.coords.entomb(writer) + } + + unsafe fn embalm(&mut self) { + self.coords.embalm() + } + + unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { + self.coords.exhume(bytes) + } +} impl Eq for QuaternionBase where N: Real + Eq, diff --git a/src/geometry/similarity.rs b/src/geometry/similarity.rs index 43e4ec1f..bb7fd4f4 100644 --- a/src/geometry/similarity.rs +++ b/src/geometry/similarity.rs @@ -10,6 +10,9 @@ use core::storage::{Storage, OwnedStorage}; use core::allocator::{Allocator, OwnedAllocator}; use geometry::{PointBase, TranslationBase, IsometryBase}; +#[cfg(feature = "abomonation-serialize")] +use abomonation::Abomonation; + /// A similarity that uses a data storage deduced from the allocator `A`. pub type OwnedSimilarityBase = @@ -25,6 +28,24 @@ pub struct SimilarityBase { scaling: N } + +#[cfg(feature = "abomonation-serialize")] +impl Abomonation for SimilarityBase + where IsometryBase: Abomonation +{ + unsafe fn entomb(&self, writer: &mut Vec) { + self.isometry.entomb(writer) + } + + unsafe fn embalm(&mut self) { + self.isometry.embalm() + } + + unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { + self.isometry.exhume(bytes) + } +} + impl SimilarityBase where N: Real, S: OwnedStorage, diff --git a/src/lib.rs b/src/lib.rs index 0231242b..22421a02 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -89,13 +89,16 @@ an optimized set of tools for computer graphics and physics. Those features incl #[cfg(feature = "arbitrary")] extern crate quickcheck; + #[cfg(feature = "serde")] extern crate serde; #[cfg(feature = "serde")] #[macro_use] extern crate serde_derive; + #[cfg(feature = "abomonation")] extern crate abomonation; + extern crate num_traits as num; extern crate num_complex; extern crate rand; diff --git a/tests/abomonation.rs b/tests/abomonation.rs index 371a52b6..16d825c5 100644 --- a/tests/abomonation.rs +++ b/tests/abomonation.rs @@ -4,7 +4,10 @@ extern crate abomonation; use rand::random; use abomonation::{Abomonation, encode, decode}; -use nalgebra::{DMatrix, Matrix3x4, Point3, Translation3, Rotation3}; +use nalgebra::{ + DMatrix, Matrix3x4, Point3, Translation3, Rotation3, Isometry3, Quaternion, + IsometryMatrix3, Similarity3, SimilarityMatrix3 +}; #[test] fn abomonate_dmatrix() { @@ -25,6 +28,11 @@ test_abomonation! { abomonate_point3, Point3; abomonate_translation3, Translation3; abomonate_rotation3, Rotation3; + abomonate_isometry3, Isometry3; + abomonate_isometry_matrix3, IsometryMatrix3; + abomonate_similarity3, Similarity3; + abomonate_similarity_matrix3, SimilarityMatrix3; + abomonate_quaternion, Quaternion; } fn assert_encode_and_decode(data: &T) {