// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors, // refer to the Cargo.toml file at the top-level directory of this distribution. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! Angle units for type-safe, self-documenting code. use std::fmt; use std::f64; use std::ops::*; use rand::{Rand, Rng}; use rand::distributions::range::SampleRange; use rust_num::{Float, Zero}; use rust_num::traits::cast; use approx::ApproxEq; use num::BaseFloat; /// An angle, in radians #[derive(Copy, Clone, PartialEq, PartialOrd, RustcEncodable, RustcDecodable)] pub struct Rad { pub s: S } /// An angle, in degrees #[derive(Copy, Clone, PartialEq, PartialOrd, RustcEncodable, RustcDecodable)] pub struct Deg { pub s: S } /// Create a new angle, in radians #[inline] pub fn rad(s: S) -> Rad { Rad { s: s } } /// Create a new angle, in degrees #[inline] pub fn deg(s: S) -> Deg { Deg { s: s } } impl From> for Deg where S: BaseFloat { #[inline] fn from(r: Rad) -> Deg { Deg::new(r.s * cast(180.0 / f64::consts::PI).unwrap()) } } impl From> for Rad where S: BaseFloat { #[inline] fn from(d: Deg) -> Rad { Rad::new(d.s * cast(f64::consts::PI / 180.0).unwrap()) } } /// Operations on angles. pub trait Angle where Self: Copy + Clone, Self: PartialEq + PartialOrd, // FIXME: Ugly type signatures - blocked by rust-lang/rust#24092 Self: ApproxEq::Unitless>, Self: Neg, Self: Add, Self: Sub, Self: Div::Unitless>, Self: Rem::Unitless>, Self: Mul<::Unitless, Output = Self>, Self: Div<::Unitless, Output = Self>, Self: Rem<::Unitless, Output = Self>, { type Unitless: BaseFloat; /// Create an angle from a unitless value. fn new(value: Self::Unitless) -> Self; /// Return the angle, normalized to the range `[0, full_turn)`. fn normalize(self) -> Self; /// Return the angle rotated by half a turn #[inline] fn opposite(self) -> Self { Self::normalize(self + Self::turn_div_2()) } /// Returns the interior bisector of the two angles #[inline] fn bisect(self, other: Self) -> Self { let half = cast(0.5f64).unwrap(); Self::normalize((self - other) * half + self) } fn zero() -> Self; fn full_turn() -> Self; fn turn_div_2() -> Self; fn turn_div_3() -> Self; fn turn_div_4() -> Self; fn turn_div_6() -> Self; #[inline] fn equiv(&self, other: &Self) -> bool { self.normalize() == other.normalize() } fn sin(self) -> Self::Unitless; fn cos(self) -> Self::Unitless; fn tan(self) -> Self::Unitless; fn sin_cos(self) -> (Self::Unitless, Self::Unitless); #[inline] fn cot(self) -> Self::Unitless { Self::tan(self).recip() } #[inline] fn sec(self) -> Self::Unitless { Self::cos(self).recip() } #[inline] fn csc(self) -> Self::Unitless { Self::sin(self).recip() } fn asin(a: Self::Unitless) -> Self; fn acos(a: Self::Unitless) -> Self; fn atan(a: Self::Unitless) -> Self; fn atan2(a: Self::Unitless, b: Self::Unitless) -> Self; } macro_rules! impl_angle { ($Angle:ident, $fmt:expr, $full_turn:expr, $hi:expr) => { impl Angle for $Angle { type Unitless = S; #[inline] fn new(value: S) -> $Angle { $Angle { s: value } } #[inline] fn zero() -> $Angle { $Angle::new(S::zero()) } #[inline] fn normalize(self) -> Self { let tmp = self % Self::full_turn().s; if tmp < Self::zero() { tmp + Self::full_turn() } else { tmp } } #[inline] fn full_turn() -> $Angle { $Angle::new(cast($full_turn).unwrap()) } #[inline] fn turn_div_2() -> $Angle { let factor: S = cast(2).unwrap(); $Angle::full_turn() / factor } #[inline] fn turn_div_3() -> $Angle { let factor: S = cast(3).unwrap(); $Angle::full_turn() / factor } #[inline] fn turn_div_4() -> $Angle { let factor: S = cast(4).unwrap(); $Angle::full_turn() / factor } #[inline] fn turn_div_6() -> $Angle { let factor: S = cast(6).unwrap(); $Angle::full_turn() / factor } #[inline] fn sin(self) -> S { let rad: Rad = self.into(); rad.s.sin() } #[inline] fn cos(self) -> S { let rad: Rad = self.into(); rad.s.cos() } #[inline] fn tan(self) -> S { let rad: Rad = self.into(); rad.s.tan() } #[inline] fn sin_cos(self) -> (S, S) { let rad: Rad = self.into(); rad.s.sin_cos() } #[inline] fn asin(a: S) -> $Angle { Rad::new(a.asin()).into() } #[inline] fn acos(a: S) -> $Angle { Rad::new(a.acos()).into() } #[inline] fn atan(a: S) -> $Angle { Rad::new(a.atan()).into() } #[inline] fn atan2(a: S, b: S) -> $Angle { Rad::new(a.atan2(b)).into() } } impl Neg for $Angle { type Output = $Angle; #[inline] fn neg(self) -> $Angle { $Angle::new(-self.s) } } impl<'a, S: BaseFloat> Neg for &'a $Angle { type Output = $Angle; #[inline] fn neg(self) -> $Angle { $Angle::new(-self.s) } } impl_binary_operator!( Add<$Angle > for $Angle { fn add(lhs, rhs) -> $Angle { $Angle::new(lhs.s + rhs.s) } }); impl_binary_operator!( Sub<$Angle > for $Angle { fn sub(lhs, rhs) -> $Angle { $Angle::new(lhs.s - rhs.s) } }); impl_binary_operator!( Div<$Angle > for $Angle { fn div(lhs, rhs) -> S { lhs.s / rhs.s } }); impl_binary_operator!( Rem<$Angle > for $Angle { fn rem(lhs, rhs) -> S { lhs.s % rhs.s } }); impl_binary_operator!( Mul for $Angle { fn mul(lhs, scalar) -> $Angle { $Angle::new(lhs.s * scalar) } }); impl_binary_operator!( Div for $Angle { fn div(lhs, scalar) -> $Angle { $Angle::new(lhs.s / scalar) } }); impl_binary_operator!( Rem for $Angle { fn rem(lhs, scalar) -> $Angle { $Angle::new(lhs.s % scalar) } }); impl ApproxEq for $Angle { type Epsilon = S; #[inline] fn approx_eq_eps(&self, other: &$Angle, epsilon: &S) -> bool { self.s.approx_eq_eps(&other.s, epsilon) } } impl Rand for $Angle { #[inline] fn rand(rng: &mut R) -> $Angle { $Angle::new(rng.gen_range(cast(-$hi).unwrap(), cast($hi).unwrap())) } } impl fmt::Debug for $Angle { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, $fmt, self.s) } } } } impl_angle!(Rad, "{:?} rad", f64::consts::PI * 2.0, f64::consts::PI); impl_angle!(Deg, "{:?}°", 360, 180);