Fixed up some tests and wrote some documentation.

Also, .swp files are now ignored.
This commit is contained in:
Jason Travis Smith 2015-10-04 23:33:37 -04:00
parent 0160780bd1
commit e410e69e2f
4 changed files with 121 additions and 3 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
target target
Cargo.lock Cargo.lock
*.swp

View File

@ -11,3 +11,6 @@ pub mod vector;
pub use self::vector::Vector; pub use self::vector::Vector;
pub use self::vector::Vector2;
pub use self::vector::Vector3;
pub use self::vector::Vector4;

View File

@ -10,15 +10,52 @@ pub trait Vector<T> : Clone where T: Number
{ {
// Creation functions. // Creation functions.
/// Create a Vector from a single value. /// Create a Vector from a single value.
///
///```
/// // Create a new Vector3<f32> where all
/// // the components are 3.4.
/// use sigils::vector::*;
///
/// let vector = Vector3::<f32>::from_value(3.4f32);
/// # assert_eq!(vector.x, 3.4f32);
/// # assert_eq!(vector.y, 3.4f32);
/// # assert_eq!(vector.z, 3.4f32);
///```
fn from_value(val: T) -> Self; fn from_value(val: T) -> Self;
/// Create a zero Vector. All components will be set to zero. /// Create a zero Vector. All components will be set to zero.
///
/// ```
/// // Create a new Vector4<u8> where all
/// // the components are 0.
/// use sigils::vector::*;
///
/// let vector = Vector4::<u8>::zero();
/// # assert_eq!(vector.x, 0u8);
/// # assert_eq!(vector.y, 0u8);
/// # assert_eq!(vector.z, 0u8);
/// # assert_eq!(vector.w, 0u8);
/// ```
// TODO: Should Vectors implement the Zero trait
// instead of defining a function for it?
fn zero() -> Self fn zero() -> Self
{ {
Self::from_value(T::zero()) Self::from_value(T::zero())
} }
/// Create an identity Vector. All components will be set to one. /// Create an identity Vector. All components will be set to one.
///
/// ```
/// // Create a new Vector2<i32> where all
/// // the components are 1.
/// use sigils::vector::*;
///
/// let vector = Vector2::<i32>::identity();
/// # assert_eq!(vector.x, 1i32);
/// # assert_eq!(vector.y, 1i32);
/// ```
// TODO: Should Vectors implement the One trait
// instead of defining an identity function?
fn identity() -> Self fn identity() -> Self
{ {
Self::from_value(T::one()) Self::from_value(T::one())
@ -27,19 +64,81 @@ pub trait Vector<T> : Clone where T: Number
// Scalar operations that result in a new Vector. // Scalar operations that result in a new Vector.
/// Add a scalar value to this Vector and return a new Vector. /// Add a scalar value to this Vector and return a new Vector.
///
/// ```
/// // Create a new Vector2<i32> where all
/// // the components are 1. Then add 1 to it.
/// use sigils::vector::*;
///
/// let vector = Vector2::<i32>::identity();
/// let vector_two = vector.add_scalar(1i32);
/// # assert_eq!(vector_two.x, 2i32);
/// # assert_eq!(vector_two.y, 2i32);
/// ```
fn add_scalar(&self, scalar: T) -> Self; fn add_scalar(&self, scalar: T) -> Self;
/// Subtract a scalar value from this Vector and return a new Vector. /// Subtract a scalar value from this Vector and return a new Vector.
///
/// ```
/// // Create a new Vector4<f64> where all
/// // the components are 1. Then subtract 1 from it.
/// use sigils::vector::*;
///
/// let vector = Vector4::<f64>::identity();
/// let vector_two = vector.sub_scalar(1.0f64);
/// # assert_eq!(vector_two.x, 0f64);
/// # assert_eq!(vector_two.y, 0f64);
/// # assert_eq!(vector_two.z, 0f64);
/// # assert_eq!(vector_two.w, 0f64);
/// ```
fn sub_scalar(&self, scalar: T) -> Self; fn sub_scalar(&self, scalar: T) -> Self;
/// Multiply this Vector by a scalar value and return a new Vector. /// Multiply this Vector by a scalar value and return a new Vector.
///
/// ```
/// // Create a new Vector4<f64> where all
/// // the components are 5. Then multiply it by 5.
/// use sigils::vector::*;
///
/// let vector = Vector4::<f32>::from_value(5f32);
/// let vector_two = vector.mul_scalar(5f32);
/// # assert_eq!(vector_two.x, 25f32);
/// # assert_eq!(vector_two.y, 25f32);
/// # assert_eq!(vector_two.z, 25f32);
/// # assert_eq!(vector_two.w, 25f32);
/// ```
fn mul_scalar(&self, scalar: T) -> Self; fn mul_scalar(&self, scalar: T) -> Self;
/// Divide this Vector by a scalar value and return a new Vector. /// Divide this Vector by a scalar value and return a new Vector.
///
/// ```
/// // Create a new Vector3<u8> where all
/// // the components are 25. Then divide it by 5.
/// use sigils::vector::*;
///
/// let vector = Vector3::<u8>::from_value(25u8);
/// let vector_two = vector.div_scalar(5u8);
/// # assert_eq!(vector_two.x, 5u8);
/// # assert_eq!(vector_two.y, 5u8);
/// # assert_eq!(vector_two.z, 5u8);
/// ```
fn div_scalar(&self, scalar: T) -> Self; fn div_scalar(&self, scalar: T) -> Self;
/// Divide this Vector by a scalar value, take the remainder, /// Divide this Vector by a scalar value, take the remainder,
/// and return a new Vector. /// and return a new Vector.
///
/// ```
/// // Create a new Vector3<isize> where all
/// // the components are 25. Then divide it by 7
/// // and take the remainder.
/// use sigils::vector::*;
///
/// let vector = Vector3::<isize>::from_value(25isize);
/// let vector_two = vector.rem_scalar(7isize);
/// # assert_eq!(vector_two.x, 4isize);
/// # assert_eq!(vector_two.y, 4isize);
/// # assert_eq!(vector_two.z, 4isize);
/// ```
fn rem_scalar(&self, scalar: T) -> Self; fn rem_scalar(&self, scalar: T) -> Self;
@ -94,6 +193,7 @@ pub trait Vector<T> : Clone where T: Number
/// Divide this Vector by a Vector and take the remainder. /// Divide this Vector by a Vector and take the remainder.
fn rem_vector_self(&mut self, vector: &Self); fn rem_vector_self(&mut self, vector: &Self);
// TODO: Look at defining negation
// Basic Vector functions. // Basic Vector functions.
/// Get the sum of all the components of the Vector. /// Get the sum of all the components of the Vector.
@ -168,6 +268,10 @@ macro_rules! define_vector
{ {
($structName: ident <$T: ident> {$($field: ident),+}) => ($structName: ident <$T: ident> {$($field: ident),+}) =>
{ {
/// This structure was defined with the macro
/// define_vector.
///
/// Please excuse the lack of comments.
#[derive(PartialEq, Eq, Copy, Clone, Hash)] #[derive(PartialEq, Eq, Copy, Clone, Hash)]
pub struct $structName<T> where T: Number pub struct $structName<T> where T: Number
{ {
@ -323,11 +427,13 @@ define_vector!(Vector4<T> {x, y, z, w});
// Vector structure sizes. // Vector structure sizes.
impl<T> Vector2<T> where T: Number impl<T> Vector2<T> where T: Number
{ {
/// Create a unit Vector in the X direction.
pub fn unit_x() -> Vector2<T> pub fn unit_x() -> Vector2<T>
{ {
Vector2::new(T::one(), T::zero()) Vector2::new(T::one(), T::zero())
} }
/// Create a unit Vector in the Y direction.
pub fn unit_y() -> Vector2<T> pub fn unit_y() -> Vector2<T>
{ {
Vector2::new(T::zero(), T::one()) Vector2::new(T::zero(), T::one())
@ -341,21 +447,26 @@ impl<T> Vector2<T> where T: Number
impl<T> Vector3<T> where T: Number impl<T> Vector3<T> where T: Number
{ {
/// Create a unit Vector in the X direction.
pub fn unit_x() -> Vector3<T> pub fn unit_x() -> Vector3<T>
{ {
Vector3::new(T::one(), T::zero(), T::zero()) Vector3::new(T::one(), T::zero(), T::zero())
} }
/// Create a unit Vector in the Y direction.
pub fn unit_y() -> Vector3<T> pub fn unit_y() -> Vector3<T>
{ {
Vector3::new(T::zero(), T::one(), T::zero()) Vector3::new(T::zero(), T::one(), T::zero())
} }
/// Create a unit Vector in the Z direction.
pub fn unit_z() -> Vector3<T> pub fn unit_z() -> Vector3<T>
{ {
Vector3::new(T::zero(), T::zero(), T::one()) Vector3::new(T::zero(), T::zero(), T::one())
} }
/// Calculate the cross product between this and another Vector.
/// It will return a Vector perpendicular to both of the input Vectors.
pub fn cross(&self, vector: &Vector3<T>) -> Vector3<T> pub fn cross(&self, vector: &Vector3<T>) -> Vector3<T>
{ {
Vector3::new((self.y * vector.z) - (self.z * vector.y), Vector3::new((self.y * vector.z) - (self.z * vector.y),
@ -366,21 +477,25 @@ impl<T> Vector3<T> where T: Number
impl<T> Vector4<T> where T: Number impl<T> Vector4<T> where T: Number
{ {
/// Create a unit Vector in the X direction.
pub fn unit_x() -> Vector4<T> pub fn unit_x() -> Vector4<T>
{ {
Vector4::new(T::one(), T::zero(), T::zero(), T::zero()) Vector4::new(T::one(), T::zero(), T::zero(), T::zero())
} }
/// Create a unit Vector in the Y direction.
pub fn unit_y() -> Vector4<T> pub fn unit_y() -> Vector4<T>
{ {
Vector4::new(T::zero(), T::one(), T::zero(), T::zero()) Vector4::new(T::zero(), T::one(), T::zero(), T::zero())
} }
/// Create a unit Vector in the Z direction.
pub fn unit_z() -> Vector4<T> pub fn unit_z() -> Vector4<T>
{ {
Vector4::new(T::zero(), T::zero(), T::one(), T::zero()) Vector4::new(T::zero(), T::zero(), T::one(), T::zero())
} }
/// Create a unit Vector in the W direction.
pub fn unit_w() -> Vector4<T> pub fn unit_w() -> Vector4<T>
{ {
Vector4::new(T::zero(), T::zero(), T::zero(), T::one()) Vector4::new(T::zero(), T::zero(), T::zero(), T::one())

View File

@ -1,13 +1,12 @@
extern crate sigils; extern crate sigils;
use sigils::Vector; use sigils::vector::*;
#[test] #[test]
fn vector_creation() fn vector_creation()
{ {
let v: Vector<f32>; let v: Vector3<f32> = Vector3::<f32>::from_value(1.0f32);
v = Vector::new();
assert_eq!(v.x, 1.0f32); assert_eq!(v.x, 1.0f32);
} }