Implement Abomonation for remaining types

This commit is contained in:
Eduard Bopp 2017-08-14 14:32:02 +02:00
parent 916006f80a
commit e09af0ca82
6 changed files with 98 additions and 1 deletions

View File

@ -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<T> {
}
}
#[cfg(feature = "abomonation-serialize")]
impl<T: Abomonation> Abomonation for Unit<T> {
unsafe fn entomb(&self, writer: &mut Vec<u8>) {
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<T: NormedSpace> Unit<T> {
/// Normalize the given value and return it wrapped on a `Unit` structure.
#[inline]

View File

@ -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<N, D, A, R> =
@ -32,6 +35,29 @@ pub struct IsometryBase<N: Scalar, D: DimName, S, R> {
_noconstruct: PhantomData<N>
}
#[cfg(feature = "abomonation-serialize")]
impl<N, D, S, R> Abomonation for IsometryBase<N, D, S, R>
where N: Scalar,
D: DimName,
R: Abomonation,
TranslationBase<N, D, S>: Abomonation
{
unsafe fn entomb(&self, writer: &mut Vec<u8>) {
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<N, D: DimName, S, R> IsometryBase<N, D, S, R>
where N: Real,
S: OwnedStorage<N, D, U1>,

View File

@ -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<N, S>
}
}
#[cfg(feature = "abomonation-serialize")]
impl<N, S> Abomonation for QuaternionBase<N, S>
where N: Real,
S: Storage<N, U4, U1>,
ColumnVector<N, U4, S>: Abomonation
{
unsafe fn entomb(&self, writer: &mut Vec<u8>) {
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<N, S> Eq for QuaternionBase<N, S>
where N: Real + Eq,

View File

@ -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<N, D, A, R> =
@ -25,6 +28,24 @@ pub struct SimilarityBase<N: Scalar, D: DimName, S, R> {
scaling: N
}
#[cfg(feature = "abomonation-serialize")]
impl<N: Scalar, D: DimName, S, R> Abomonation for SimilarityBase<N, D, S, R>
where IsometryBase<N, D, S, R>: Abomonation
{
unsafe fn entomb(&self, writer: &mut Vec<u8>) {
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<N, D: DimName, S, R> SimilarityBase<N, D, S, R>
where N: Real,
S: OwnedStorage<N, D, U1>,

View File

@ -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;

View File

@ -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<f32>;
abomonate_translation3, Translation3<f64>;
abomonate_rotation3, Rotation3<f64>;
abomonate_isometry3, Isometry3<f32>;
abomonate_isometry_matrix3, IsometryMatrix3<f64>;
abomonate_similarity3, Similarity3<f32>;
abomonate_similarity_matrix3, SimilarityMatrix3<f32>;
abomonate_quaternion, Quaternion<f32>;
}
fn assert_encode_and_decode<T: Abomonation + PartialEq>(data: &T) {