Vector types are now Transmutable.

A Vector can now be converted to and from bytes of data.
I'm not entirely sure that the structure bytes do not have to
be handled for endianess, but the underlying basic types are
correctly handled.

Tests and and example have also be created. The tests lib file was split
into a converter test file and a transmutable test file.
This commit is contained in:
Jason Travis Smith
2016-01-07 05:10:50 -05:00
parent 8075e68607
commit aa4bb78f66
5 changed files with 274 additions and 8 deletions

View File

@ -1,5 +1,5 @@
use sigils::{Zero, Number};
use sigils::vector::{Vector, Vector2};
use sigils::vector::{Vector, Vector2, Vector3, Vector4};
use ::byte_sized::ByteSized;
use ::converter::Converter;
@ -408,3 +408,97 @@ impl<T> Transmutable for Vector2<T> where T: Number + ByteSized + Transmutable
vec
}
}
impl<T> Transmutable for Vector3<T> where T: Number + 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 Vector3.
byte_size = T::get_byte_size();
num_bytes = byte_size * self.get_size() as usize;
// 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.x.to_bytes(&mut buffer[0..byte_size], endianess);
self.y.to_bytes(&mut buffer[byte_size..(byte_size*2)], endianess);
self.z.to_bytes(&mut buffer[(byte_size*2)..num_bytes], endianess);
}
fn from_bytes(buffer: &[u8], endianess: Endianess) -> Vector3<T>
{
let byte_size: usize;
let num_bytes: usize;
let mut vec: Vector3<T>;
// Determine the number of bytes requires to
// represent a Vector3.
vec = Vector3::<T>::zero();
byte_size = T::get_byte_size();
num_bytes = byte_size * vec.get_size() as usize;
// 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.
vec.x = T::from_bytes(&buffer[0..byte_size], endianess);
vec.y = T::from_bytes(&buffer[byte_size..(byte_size*2)], endianess);
vec.z = T::from_bytes(&buffer[(byte_size*2)..num_bytes], endianess);
vec
}
}
impl<T> Transmutable for Vector4<T> where T: Number + 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 Vector4.
byte_size = T::get_byte_size();
num_bytes = byte_size * self.get_size() as usize;
// 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.x.to_bytes(&mut buffer[0..byte_size], endianess);
self.y.to_bytes(&mut buffer[byte_size..(byte_size*2)], endianess);
self.z.to_bytes(&mut buffer[(byte_size*2)..(byte_size*3)], endianess);
self.w.to_bytes(&mut buffer[(byte_size*3)..num_bytes], endianess);
}
fn from_bytes(buffer: &[u8], endianess: Endianess) -> Vector4<T>
{
let byte_size: usize;
let num_bytes: usize;
let mut vec: Vector4<T>;
// Determine the number of bytes requires to
// represent a Vector4.
vec = Vector4::<T>::zero();
byte_size = T::get_byte_size();
num_bytes = byte_size * vec.get_size() as usize;
// 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.
vec.x = T::from_bytes(&buffer[0..byte_size], endianess);
vec.y = T::from_bytes(&buffer[byte_size..(byte_size*2)], endianess);
vec.z = T::from_bytes(&buffer[(byte_size*2)..(byte_size*3)], endianess);
vec.w = T::from_bytes(&buffer[(byte_size*3)..num_bytes], endianess);
vec
}
}