Added Debug, Display, and Default to Vectors and Quaternions.

This also adds an example for Vectors and Quaternions.
This commit is contained in:
Jason Travis Smith 2016-01-07 17:42:23 -05:00
parent 9306a73f9a
commit e64c3b0fbe
5 changed files with 128 additions and 2 deletions

14
examples/quaternion.rs Normal file
View File

@ -0,0 +1,14 @@
extern crate sigils;
use sigils::quaternion::{Quaternion};
pub fn main()
{
let quat: Quaternion<f64>;
quat = Quaternion::<f64>::default();
println!("Quaternion: {}", quat);
}

22
examples/vector.rs Normal file
View File

@ -0,0 +1,22 @@
extern crate sigils;
use sigils::vector::{Vector2, Vector3, Vector4};
pub fn main()
{
let vec2: Vector2<f64>;
let vec3: Vector3<f64>;
let vec4: Vector4<f64>;
vec2 = Vector2::<f64>::default();
println!("Vector2: {}", vec2);
vec3 = Vector3::<f64>::default();
println!("Vector3: {}", vec3);
vec4 = Vector4::<f64>::default();
println!("Vector4: {}", vec4);
}

View File

@ -1,4 +1,5 @@
use std::cmp::PartialEq; use std::cmp::PartialEq;
use std::fmt::{Debug, Display};
use std::mem::size_of; use std::mem::size_of;
use std::ops::{Add, Sub, Mul, Div, Rem}; use std::ops::{Add, Sub, Mul, Div, Rem};
use std::str::FromStr; use std::str::FromStr;
@ -12,7 +13,7 @@ use ::bounded::Bounded;
/// a number. /// a number.
pub trait Number: Zero + One + Add<Output=Self> + Sub<Output=Self> + pub trait Number: Zero + One + Add<Output=Self> + Sub<Output=Self> +
Mul<Output=Self> + Div<Output=Self> + Rem<Output=Self> + Mul<Output=Self> + Div<Output=Self> + Rem<Output=Self> +
PartialEq + Copy + Clone PartialEq + Copy + Clone + Debug + Display
{ {
type StrRadixError; type StrRadixError;

View File

@ -3,6 +3,7 @@
//! //!
//! [1]: https://en.wikipedia.org/wiki/Quaternion //! [1]: https://en.wikipedia.org/wiki/Quaternion
//! [2]: https://en.wikipedia.org/wiki/Dual_quaternion //! [2]: https://en.wikipedia.org/wiki/Dual_quaternion
use std::fmt::{Error, Formatter, Debug, Display};
use std::ops::{Add, Sub, Mul, Div, Neg}; use std::ops::{Add, Sub, Mul, Div, Neg};
use ::zero::Zero; use ::zero::Zero;
@ -275,6 +276,34 @@ impl<T> Quaternion<T> where T: Real
// TODO: Add binary operations for editing self. // TODO: Add binary operations for editing self.
} }
// Implement the Debug trait for the Quaternion.
impl<T> Debug for Quaternion<T> where T: Real
{
fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error>
{
write!(formatter, "[{:?}, {:?}]", self.scalar, self.vector)
}
}
// Implement the Display trait for the Quaternion.
impl<T> Display for Quaternion<T> where T: Real
{
fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error>
{
write!(formatter, "[{}, {}]", self.scalar, self.vector)
}
}
// Implement a default value for Quaternions which
// returns the identity quaternion.
impl<T> Default for Quaternion<T> where T: Real
{
fn default() -> Quaternion<T>
{
Quaternion::identity()
}
}
// Implement the Zero and One traits for the Quaternion. // Implement the Zero and One traits for the Quaternion.
impl<T> Zero for Quaternion<T> where T: Real impl<T> Zero for Quaternion<T> where T: Real
{ {

View File

@ -1,6 +1,7 @@
//! This module defines 2, 3, and 4 component [Vector][1] structures. //! This module defines 2, 3, and 4 component [Vector][1] structures.
//! //!
//! [1]: https://en.wikipedia.org/wiki/Vector_(mathematics_and_physics) //! [1]: https://en.wikipedia.org/wiki/Vector_(mathematics_and_physics)
use std::fmt::{Error, Formatter, Debug, Display};
use std::ops::{Add, Sub, Mul, Div, Rem, Neg}; use std::ops::{Add, Sub, Mul, Div, Rem, Neg};
use ::zero::Zero; use ::zero::Zero;
@ -10,11 +11,13 @@ use ::real::Real;
use ::trig::{acos, atan2}; use ::trig::{acos, atan2};
/// A trait that defines the minimum set of /// A trait that defines the minimum set of
/// functions a [Vector][1] must implement. /// functions a [Vector][1] must implement.
/// ///
/// [1]: https://en.wikipedia.org/wiki/Vector_(mathematics_and_physics) /// [1]: https://en.wikipedia.org/wiki/Vector_(mathematics_and_physics)
pub trait Vector<T> : Clone where T: Number pub trait Vector<T>: Debug + Display + Clone + Default + Zero + One
where T: Number
{ {
// Creation functions. // Creation functions.
/// Create a Vector from a single value. /// Create a Vector from a single value.
@ -638,6 +641,63 @@ macro_rules! define_vector
} }
} }
// Implement the Debug trait for the Vector.
impl<T> Debug for $structName<T> where T: Number
{
fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error>
{
let mut count: u8;
// Use the size of the Vector to
// pretty up the printing.
count = self.get_size();
write!(formatter, "<");
$(
write!(formatter, "{:?}", self.$field);
if count > 0
{
write!(formatter, ", ");
}
count -= 1;
)*
write!(formatter, ">")
}
}
// Implement the Display trait for the Vector.
impl<T> Display for $structName<T> where T: Number
{
fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error>
{
let mut count: u8;
// Use the size of the Vector to
// pretty up the printing.
count = self.get_size();
write!(formatter, "<");
$(
write!(formatter, "{}", self.$field);
if count > 1
{
write!(formatter, ", ");
}
count -= 1;
)*
write!(formatter, ">")
}
}
// Implement the Default trait for the Vector.
impl<T> Default for $structName<T> where T: Number
{
fn default() -> $structName<T>
{
$structName::identity()
}
}
// Implement the Zero and One traits for the Vector. // Implement the Zero and One traits for the Vector.
impl<T> Zero for $structName<T> where T: Number impl<T> Zero for $structName<T> where T: Number
{ {