From e64c3b0fbecca351e0a078fefc4273a0f0b8e6ad Mon Sep 17 00:00:00 2001 From: Jason Travis Smith Date: Thu, 7 Jan 2016 17:42:23 -0500 Subject: [PATCH] Added Debug, Display, and Default to Vectors and Quaternions. This also adds an example for Vectors and Quaternions. --- examples/quaternion.rs | 14 ++++++++++ examples/vector.rs | 22 +++++++++++++++ src/number.rs | 3 +- src/quaternion.rs | 29 ++++++++++++++++++++ src/vector.rs | 62 +++++++++++++++++++++++++++++++++++++++++- 5 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 examples/quaternion.rs create mode 100644 examples/vector.rs diff --git a/examples/quaternion.rs b/examples/quaternion.rs new file mode 100644 index 0000000..5d94d72 --- /dev/null +++ b/examples/quaternion.rs @@ -0,0 +1,14 @@ +extern crate sigils; + + + +use sigils::quaternion::{Quaternion}; + + +pub fn main() +{ + let quat: Quaternion; + + quat = Quaternion::::default(); + println!("Quaternion: {}", quat); +} diff --git a/examples/vector.rs b/examples/vector.rs new file mode 100644 index 0000000..3857d64 --- /dev/null +++ b/examples/vector.rs @@ -0,0 +1,22 @@ +extern crate sigils; + + + +use sigils::vector::{Vector2, Vector3, Vector4}; + + +pub fn main() +{ + let vec2: Vector2; + let vec3: Vector3; + let vec4: Vector4; + + vec2 = Vector2::::default(); + println!("Vector2: {}", vec2); + + vec3 = Vector3::::default(); + println!("Vector3: {}", vec3); + + vec4 = Vector4::::default(); + println!("Vector4: {}", vec4); +} diff --git a/src/number.rs b/src/number.rs index d977059..4dad830 100644 --- a/src/number.rs +++ b/src/number.rs @@ -1,4 +1,5 @@ use std::cmp::PartialEq; +use std::fmt::{Debug, Display}; use std::mem::size_of; use std::ops::{Add, Sub, Mul, Div, Rem}; use std::str::FromStr; @@ -12,7 +13,7 @@ use ::bounded::Bounded; /// a number. pub trait Number: Zero + One + Add + Sub + Mul + Div + Rem + - PartialEq + Copy + Clone + PartialEq + Copy + Clone + Debug + Display { type StrRadixError; diff --git a/src/quaternion.rs b/src/quaternion.rs index ddd71ac..8dc3036 100644 --- a/src/quaternion.rs +++ b/src/quaternion.rs @@ -3,6 +3,7 @@ //! //! [1]: https://en.wikipedia.org/wiki/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 ::zero::Zero; @@ -275,6 +276,34 @@ impl Quaternion where T: Real // TODO: Add binary operations for editing self. } +// Implement the Debug trait for the Quaternion. +impl Debug for Quaternion 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 Display for Quaternion 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 Default for Quaternion where T: Real +{ + fn default() -> Quaternion + { + Quaternion::identity() + } +} + // Implement the Zero and One traits for the Quaternion. impl Zero for Quaternion where T: Real { diff --git a/src/vector.rs b/src/vector.rs index 800483e..7624a54 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -1,6 +1,7 @@ //! This module defines 2, 3, and 4 component [Vector][1] structures. //! //! [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 ::zero::Zero; @@ -10,11 +11,13 @@ use ::real::Real; use ::trig::{acos, atan2}; + /// A trait that defines the minimum set of /// functions a [Vector][1] must implement. /// /// [1]: https://en.wikipedia.org/wiki/Vector_(mathematics_and_physics) -pub trait Vector : Clone where T: Number +pub trait Vector: Debug + Display + Clone + Default + Zero + One + where T: Number { // Creation functions. /// Create a Vector from a single value. @@ -638,6 +641,63 @@ macro_rules! define_vector } } + // Implement the Debug trait for the Vector. + impl Debug for $structName 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 Display for $structName 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 Default for $structName where T: Number + { + fn default() -> $structName + { + $structName::identity() + } + } + // Implement the Zero and One traits for the Vector. impl Zero for $structName where T: Number {