Quaternions are now Transmutable.
This implements Transmutable for Quaternions and creates a test and example for this conversion.
This commit is contained in:
89
examples/convert_quaternion.rs
Normal file
89
examples/convert_quaternion.rs
Normal file
@ -0,0 +1,89 @@
|
||||
#![feature(convert)]
|
||||
|
||||
extern crate alchemy;
|
||||
extern crate sigils;
|
||||
|
||||
|
||||
|
||||
use alchemy::F64_BYTES;
|
||||
use alchemy::{Endianess, Transmutable};
|
||||
use sigils::quaternion::Quaternion;
|
||||
|
||||
|
||||
|
||||
/// The size of 4 f64 Real numbers.
|
||||
/// This would be different if the contained type
|
||||
/// was something different, like an f32.
|
||||
const SIZE_OF_QUATERNION: usize = F64_BYTES * 4;
|
||||
|
||||
|
||||
|
||||
/// This just help pretty up the printing of an array of bytes.
|
||||
fn stringify_array(buffer: &[u8]) -> String
|
||||
{
|
||||
let mut result: String;
|
||||
let mut count: usize;
|
||||
|
||||
// Create a new string that starts with just
|
||||
// the array opening bracket.
|
||||
result = String::new();
|
||||
result.push_str("[");
|
||||
|
||||
// Loop through the buffer keeping track
|
||||
// of our place in it.
|
||||
count = 0usize;
|
||||
for byte in buffer
|
||||
{
|
||||
// Handle priting the last value differently.
|
||||
if count >= buffer.len() - 1
|
||||
{
|
||||
result.push_str(byte.to_string().as_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
result.push_str(byte.to_string().as_str());
|
||||
result.push_str(", ");
|
||||
}
|
||||
|
||||
// Mark that we are going to look at
|
||||
// the next byte in the array.
|
||||
count += 1;
|
||||
}
|
||||
|
||||
// Add the array closing bracket and
|
||||
// return the new String.
|
||||
result.push_str("]");
|
||||
result
|
||||
}
|
||||
|
||||
|
||||
pub fn main()
|
||||
{
|
||||
let quat: Quaternion<f64>;
|
||||
let final_quat: Quaternion<f64>;
|
||||
let endianess: Endianess;
|
||||
let mut buffer: [u8; SIZE_OF_QUATERNION];
|
||||
|
||||
// Initialize the variables.
|
||||
quat = Quaternion::<f64>::from_values(6.29f64, 1.9f64, 8.5f64, 7.11f64);
|
||||
buffer = [0u8; SIZE_OF_QUATERNION];
|
||||
endianess = Endianess::PLATFORM;
|
||||
|
||||
println!("Transmuting a Quaternion:");
|
||||
|
||||
println!("Converting the value [{1}, <{2}, {3}, {4}>] {0}",
|
||||
"into and out of an array of bytes.",
|
||||
quat.scalar, quat.vector.x, quat.vector.y, quat.vector.z);
|
||||
println!("Buffer starts as: {}", stringify_array(&buffer));
|
||||
|
||||
// Convert the Vector2 into an array of bytes.
|
||||
quat.to_bytes(&mut buffer, endianess);
|
||||
|
||||
println!("Buffer contains: {}", stringify_array(&buffer));
|
||||
|
||||
// Convert the array of bytes into a Vector2.
|
||||
final_quat = Quaternion::from_bytes(&buffer, endianess);
|
||||
println!("The buffer converts back to: [{}, <{}, {}, {}>]",
|
||||
final_quat.scalar, final_quat.vector.x,
|
||||
final_quat.vector.y, final_quat.vector.z);
|
||||
}
|
Reference in New Issue
Block a user