Quaternions are now Transmutable.
This implements Transmutable for Quaternions and creates a test and example for this conversion.
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
use sigils::{Zero, Number};
|
||||
use sigils::{Zero, Number, Real};
|
||||
use sigils::vector::{Vector, Vector2, Vector3, Vector4};
|
||||
use sigils::quaternion::Quaternion;
|
||||
|
||||
use ::byte_sized::ByteSized;
|
||||
use ::converter::Converter;
|
||||
@ -502,3 +503,49 @@ impl<T> Transmutable for Vector4<T> where T: Number + ByteSized + Transmutable
|
||||
vec
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Transmutable for Quaternion<T>
|
||||
where T: Real + ByteSized + Transmutable
|
||||
{
|
||||
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
||||
{
|
||||
let byte_size: usize;
|
||||
let num_bytes: usize;
|
||||
|
||||
// Determine the number of bytes requires to
|
||||
// represent a Quaternion.
|
||||
byte_size = T::get_byte_size();
|
||||
num_bytes = byte_size * 4usize;
|
||||
|
||||
// Make sure that there is enough space to store
|
||||
// the bytes from this type.
|
||||
assert!(buffer.len() >= num_bytes);
|
||||
|
||||
// Convert this to bytes and add it to the buffer.
|
||||
self.scalar.to_bytes(&mut buffer[0..byte_size], endianess);
|
||||
self.vector.to_bytes(&mut buffer[byte_size..num_bytes], endianess);
|
||||
}
|
||||
|
||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> Quaternion<T>
|
||||
{
|
||||
let byte_size: usize;
|
||||
let num_bytes: usize;
|
||||
let mut quat: Quaternion<T>;
|
||||
|
||||
// Determine the number of bytes requires to
|
||||
// represent a Quaternion.
|
||||
quat = Quaternion::<T>::zero();
|
||||
byte_size = T::get_byte_size();
|
||||
num_bytes = byte_size * 4usize;
|
||||
|
||||
// Make sure that there is enough data to read
|
||||
// the bytes for this type.
|
||||
assert!(buffer.len() >= num_bytes);
|
||||
|
||||
// Convert the given bytes to this type and return it.
|
||||
quat.scalar = T::from_bytes(&buffer[0..byte_size], endianess);
|
||||
quat.vector =
|
||||
Vector3::<T>::from_bytes(&buffer[byte_size..num_bytes], endianess);
|
||||
quat
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user