Added the basic trigonometric functions.

These were moved to their own trait and removed from the definition of
a Real number. This allows the definition of a trigonometric function
to call a different libc trig function depending on type.
This commit is contained in:
2017-06-27 18:19:33 -04:00
parent cbde731b74
commit 43a1e30808
14 changed files with 839 additions and 1094 deletions

View File

@ -9,6 +9,8 @@ use std::ops::{Add, Sub, Mul, Div, Neg};
use ::zero::Zero;
use ::one::One;
use ::real::Real;
use ::trig::Radian;
use ::trig::Trig;
use ::vector::{Vector, EuclideanVector, Vector3};
@ -28,7 +30,7 @@ use ::vector::{Vector, EuclideanVector, Vector3};
/// | jk = 1 | kj = -1 |
/// | ki = 1 | ji = -1 |
#[derive(Clone, Copy)]
pub struct Quaternion<T> where T: Real
pub struct Quaternion<T>
{
/// TODO: Describe this.
pub scalar: T,
@ -45,7 +47,7 @@ macro_rules! binary_operator_impl
{
($traitName: ident :: $funcName: ident, $structName: ident) =>
{
impl<T> $traitName<T> for $structName<T> where T: Real
impl<T> $traitName<T> for $structName<T> where T: Trig
{
type Output = $structName<T>;
@ -56,7 +58,7 @@ macro_rules! binary_operator_impl
}
}
impl<'a, T> $traitName<T> for &'a $structName<T> where T: Real
impl<'a, T> $traitName<T> for &'a $structName<T> where T: Trig
{
type Output = $structName<T>;
@ -67,7 +69,7 @@ macro_rules! binary_operator_impl
}
}
impl<'a, T> $traitName<&'a T> for $structName<T> where T: Real
impl<'a, T> $traitName<&'a T> for $structName<T> where T: Trig
{
type Output = $structName<T>;
@ -78,7 +80,7 @@ macro_rules! binary_operator_impl
}
}
impl<'a, 'b, T> $traitName<&'b T> for &'a $structName<T> where T: Real
impl<'a, 'b, T> $traitName<&'b T> for &'a $structName<T> where T: Trig
{
type Output = $structName<T>;
@ -93,7 +95,7 @@ macro_rules! binary_operator_impl
// Implement the Quaternion's methods.
impl<T> Quaternion<T> where T: Real
impl<T> Quaternion<T> where T: Trig
{
/// Create a new Quaternion from a given scalar and Vector.
///
@ -277,7 +279,7 @@ impl<T> Quaternion<T> where T: Real
}
// Implement the Debug trait for the Quaternion.
impl<T> Debug for Quaternion<T> where T: Real
impl<T> Debug for Quaternion<T> where T: Trig
{
fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error>
{
@ -286,7 +288,7 @@ impl<T> Debug for Quaternion<T> where T: Real
}
// Implement the Display trait for the Quaternion.
impl<T> Display for Quaternion<T> where T: Real
impl<T> Display for Quaternion<T> where T: Trig
{
fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error>
{
@ -296,7 +298,7 @@ impl<T> Display for Quaternion<T> where T: Real
// Implement a default value for Quaternions which
// returns the identity quaternion.
impl<T> Default for Quaternion<T> where T: Real
impl<T> Default for Quaternion<T> where T: Trig
{
fn default() -> Quaternion<T>
{
@ -305,7 +307,7 @@ impl<T> Default for Quaternion<T> where T: Real
}
// 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: Trig
{
fn zero() -> Quaternion<T>
{
@ -313,7 +315,7 @@ impl<T> Zero for Quaternion<T> where T: Real
}
}
impl<T> One for Quaternion<T> where T: Real
impl<T> One for Quaternion<T> where T: Trig
{
fn one() -> Quaternion<T>
{
@ -327,7 +329,7 @@ binary_operator_impl!(Mul::mul, Quaternion);
//binary_operator_impl!(Neg::neg, Quaternion);
// Implement Negating a Quaternion.
impl<T> Neg for Quaternion<T> where T: Real
impl<T> Neg for Quaternion<T> where T: Trig
{
type Output = Quaternion<T>;
@ -337,7 +339,7 @@ impl<T> Neg for Quaternion<T> where T: Real
}
}
impl<'a, T> Neg for &'a Quaternion<T> where T: Real
impl<'a, T> Neg for &'a Quaternion<T> where T: Trig
{
type Output = Quaternion<T>;
@ -348,7 +350,7 @@ impl<'a, T> Neg for &'a Quaternion<T> where T: Real
}
// Implement Adding Quaternions.
impl<T> Add<Quaternion<T>> for Quaternion<T> where T: Real
impl<T> Add<Quaternion<T>> for Quaternion<T> where T: Trig
{
type Output = Quaternion<T>;
@ -359,7 +361,7 @@ impl<T> Add<Quaternion<T>> for Quaternion<T> where T: Real
}
}
impl<'a, T> Add<Quaternion<T>> for &'a Quaternion<T> where T: Real
impl<'a, T> Add<Quaternion<T>> for &'a Quaternion<T> where T: Trig
{
type Output = Quaternion<T>;
@ -370,7 +372,7 @@ impl<'a, T> Add<Quaternion<T>> for &'a Quaternion<T> where T: Real
}
}
impl<'a, T> Add<&'a Quaternion<T>> for Quaternion<T> where T: Real
impl<'a, T> Add<&'a Quaternion<T>> for Quaternion<T> where T: Trig
{
type Output = Quaternion<T>;
@ -381,7 +383,7 @@ impl<'a, T> Add<&'a Quaternion<T>> for Quaternion<T> where T: Real
}
}
impl<'a, 'b, T> Add<&'b Quaternion<T>> for &'a Quaternion<T> where T: Real
impl<'a, 'b, T> Add<&'b Quaternion<T>> for &'a Quaternion<T> where T: Trig
{
type Output = Quaternion<T>;
@ -393,7 +395,7 @@ impl<'a, 'b, T> Add<&'b Quaternion<T>> for &'a Quaternion<T> where T: Real
}
// Implement subtracting Quaternions.
impl<T> Sub<Quaternion<T>> for Quaternion<T> where T: Real
impl<T> Sub<Quaternion<T>> for Quaternion<T> where T: Trig
{
type Output = Quaternion<T>;
@ -404,7 +406,7 @@ impl<T> Sub<Quaternion<T>> for Quaternion<T> where T: Real
}
}
impl<'a, T> Sub<Quaternion<T>> for &'a Quaternion<T> where T: Real
impl<'a, T> Sub<Quaternion<T>> for &'a Quaternion<T> where T: Trig
{
type Output = Quaternion<T>;
@ -415,7 +417,7 @@ impl<'a, T> Sub<Quaternion<T>> for &'a Quaternion<T> where T: Real
}
}
impl<'a, T> Sub<&'a Quaternion<T>> for Quaternion<T> where T: Real
impl<'a, T> Sub<&'a Quaternion<T>> for Quaternion<T> where T: Trig
{
type Output = Quaternion<T>;
@ -426,7 +428,7 @@ impl<'a, T> Sub<&'a Quaternion<T>> for Quaternion<T> where T: Real
}
}
impl<'a, 'b, T> Sub<&'b Quaternion<T>> for &'a Quaternion<T> where T: Real
impl<'a, 'b, T> Sub<&'b Quaternion<T>> for &'a Quaternion<T> where T: Trig
{
type Output = Quaternion<T>;
@ -438,7 +440,7 @@ impl<'a, 'b, T> Sub<&'b Quaternion<T>> for &'a Quaternion<T> where T: Real
}
// Implement Dividing Quaternions.
impl<T> Div<Quaternion<T>> for Quaternion<T> where T: Real
impl<T> Div<Quaternion<T>> for Quaternion<T> where T: Trig
{
type Output = Quaternion<T>;
@ -448,7 +450,7 @@ impl<T> Div<Quaternion<T>> for Quaternion<T> where T: Real
}
}
impl<'a, T> Div<Quaternion<T>> for &'a Quaternion<T> where T: Real
impl<'a, T> Div<Quaternion<T>> for &'a Quaternion<T> where T: Trig
{
type Output = Quaternion<T>;
@ -458,7 +460,7 @@ impl<'a, T> Div<Quaternion<T>> for &'a Quaternion<T> where T: Real
}
}
impl<'a, T> Div<&'a Quaternion<T>> for Quaternion<T> where T: Real
impl<'a, T> Div<&'a Quaternion<T>> for Quaternion<T> where T: Trig
{
type Output = Quaternion<T>;
@ -468,7 +470,7 @@ impl<'a, T> Div<&'a Quaternion<T>> for Quaternion<T> where T: Real
}
}
impl<'a, 'b, T> Div<&'b Quaternion<T>> for &'a Quaternion<T> where T: Real
impl<'a, 'b, T> Div<&'b Quaternion<T>> for &'a Quaternion<T> where T: Trig
{
type Output = Quaternion<T>;
@ -479,7 +481,7 @@ impl<'a, 'b, T> Div<&'b Quaternion<T>> for &'a Quaternion<T> where T: Real
}
// Implement multiplying Quaternions and Vectors.
impl<T> Mul<Quaternion<T>> for Quaternion<T> where T: Real
impl<T> Mul<Quaternion<T>> for Quaternion<T> where T: Trig
{
type Output = Quaternion<T>;
@ -492,7 +494,7 @@ impl<T> Mul<Quaternion<T>> for Quaternion<T> where T: Real
}
}
impl<'a, T> Mul<Quaternion<T>> for &'a Quaternion<T> where T: Real
impl<'a, T> Mul<Quaternion<T>> for &'a Quaternion<T> where T: Trig
{
type Output = Quaternion<T>;
@ -505,7 +507,7 @@ impl<'a, T> Mul<Quaternion<T>> for &'a Quaternion<T> where T: Real
}
}
impl<'a, T> Mul<&'a Quaternion<T>> for Quaternion<T> where T: Real
impl<'a, T> Mul<&'a Quaternion<T>> for Quaternion<T> where T: Trig
{
type Output = Quaternion<T>;
@ -518,7 +520,7 @@ impl<'a, T> Mul<&'a Quaternion<T>> for Quaternion<T> where T: Real
}
}
impl<'a, 'b, T> Mul<&'b Quaternion<T>> for &'a Quaternion<T> where T: Real
impl<'a, 'b, T> Mul<&'b Quaternion<T>> for &'a Quaternion<T> where T: Trig
{
type Output = Quaternion<T>;
@ -547,7 +549,7 @@ impl<'a, 'b, T> Mul<&'b Quaternion<T>> for &'a Quaternion<T> where T: Real
/// (SaZb + XaYb - YaXb + SbZa)k
fn multiply_quaternions<T>(sa: T, xa: T, ya: T, za: T,
sb: T, xb: T, yb: T, zb: T)
-> Quaternion<T> where T: Real
-> Quaternion<T> where T: Trig
{
let i: T;
let j: T;