cgmath/src/angle.rs

201 lines
6.7 KiB
Rust
Raw Normal View History

2014-05-26 17:10:04 +00:00
// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors,
2015-03-14 02:49:46 +00:00
// refer to the Cargo.toml file at the top-level directory of this distribution.
2013-09-04 02:20:53 +00:00
//
// 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.
2013-10-19 14:00:44 +00:00
use std::fmt;
use std::f64;
2017-04-26 11:11:29 +00:00
use std::iter;
2015-01-03 21:29:26 +00:00
use std::ops::*;
2013-09-04 02:20:53 +00:00
2015-03-15 02:53:57 +00:00
use rand::{Rand, Rng};
use rand::distributions::range::SampleRange;
use num_traits::cast;
2015-04-05 01:19:11 +00:00
use structure::*;
2016-04-19 10:51:40 +00:00
2014-01-09 00:26:50 +00:00
use approx::ApproxEq;
2015-04-05 01:19:11 +00:00
use num::BaseFloat;
2014-01-09 00:26:50 +00:00
/// An angle, in radians.
///
/// This type is marked as `#[repr(C)]`.
#[repr(C)]
2016-05-15 12:48:57 +00:00
#[derive(Copy, Clone, PartialEq, PartialOrd)]
2016-05-16 12:16:59 +00:00
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
2016-08-01 03:40:31 +00:00
pub struct Rad<S>(pub S);
/// An angle, in degrees.
///
/// This type is marked as `#[repr(C)]`.
#[repr(C)]
2016-05-15 12:48:57 +00:00
#[derive(Copy, Clone, PartialEq, PartialOrd)]
2016-05-16 12:16:59 +00:00
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
2016-08-01 03:40:31 +00:00
pub struct Deg<S>(pub S);
2013-09-04 02:20:53 +00:00
2015-05-06 08:10:26 +00:00
impl<S> From<Rad<S>> for Deg<S> where S: BaseFloat {
2015-04-05 01:32:12 +00:00
#[inline]
2016-08-01 03:40:31 +00:00
fn from(rad: Rad<S>) -> Deg<S> {
Deg(rad.0 * cast(180.0 / f64::consts::PI).unwrap())
2015-04-05 01:32:12 +00:00
}
}
2013-09-04 02:20:53 +00:00
2015-05-06 08:10:26 +00:00
impl<S> From<Deg<S>> for Rad<S> where S: BaseFloat {
2015-04-05 01:32:12 +00:00
#[inline]
2016-08-01 03:40:31 +00:00
fn from(deg: Deg<S>) -> Rad<S> {
Rad(deg.0 * cast(f64::consts::PI / 180.0).unwrap())
2015-04-05 01:32:12 +00:00
}
}
2013-09-04 02:20:53 +00:00
macro_rules! impl_angle {
($Angle:ident, $fmt:expr, $full_turn:expr, $hi:expr) => {
impl<S: BaseFloat> Zero for $Angle<S> {
#[inline]
fn zero() -> $Angle<S> {
2016-08-01 03:40:31 +00:00
$Angle(S::zero())
}
#[inline]
fn is_zero(&self) -> bool {
ulps_eq!(self, &Self::zero())
}
}
2017-04-26 11:11:29 +00:00
impl<S: BaseFloat> iter::Sum<$Angle<S>> for $Angle<S> {
#[inline]
fn sum<I: Iterator<Item=$Angle<S>>>(iter: I) -> $Angle<S> {
iter.fold($Angle::zero(), Add::add)
}
}
impl<'a, S: 'a + BaseFloat> iter::Sum<&'a $Angle<S>> for $Angle<S> {
#[inline]
fn sum<I: Iterator<Item=&'a $Angle<S>>>(iter: I) -> $Angle<S> {
iter.fold($Angle::zero(), Add::add)
}
}
impl<S: BaseFloat> Angle for $Angle<S> {
type Unitless = S;
2016-08-01 03:40:31 +00:00
#[inline] fn full_turn() -> $Angle<S> { $Angle(cast($full_turn).unwrap()) }
2016-08-01 03:40:31 +00:00
#[inline] fn sin(self) -> S { Rad::from(self).0.sin() }
#[inline] fn cos(self) -> S { Rad::from(self).0.cos() }
#[inline] fn tan(self) -> S { Rad::from(self).0.tan() }
#[inline] fn sin_cos(self) -> (S, S) { Rad::from(self).0.sin_cos() }
2016-08-01 03:40:31 +00:00
#[inline] fn asin(a: S) -> $Angle<S> { Rad(a.asin()).into() }
#[inline] fn acos(a: S) -> $Angle<S> { Rad(a.acos()).into() }
#[inline] fn atan(a: S) -> $Angle<S> { Rad(a.atan()).into() }
#[inline] fn atan2(a: S, b: S) -> $Angle<S> { Rad(a.atan2(b)).into() }
}
impl<S: BaseFloat> Neg for $Angle<S> {
type Output = $Angle<S>;
#[inline]
2016-08-01 03:40:31 +00:00
fn neg(self) -> $Angle<S> { $Angle(-self.0) }
}
impl<'a, S: BaseFloat> Neg for &'a $Angle<S> {
type Output = $Angle<S>;
#[inline]
2016-08-01 03:40:31 +00:00
fn neg(self) -> $Angle<S> { $Angle(-self.0) }
}
impl_operator!(<S: BaseFloat> Add<$Angle<S> > for $Angle<S> {
2016-08-01 03:40:31 +00:00
fn add(lhs, rhs) -> $Angle<S> { $Angle(lhs.0 + rhs.0) }
});
impl_operator!(<S: BaseFloat> Sub<$Angle<S> > for $Angle<S> {
2016-08-01 03:40:31 +00:00
fn sub(lhs, rhs) -> $Angle<S> { $Angle(lhs.0 - rhs.0) }
});
impl_operator!(<S: BaseFloat> Div<$Angle<S> > for $Angle<S> {
2016-08-01 03:40:31 +00:00
fn div(lhs, rhs) -> S { lhs.0 / rhs.0 }
});
impl_operator!(<S: BaseFloat> Rem<$Angle<S> > for $Angle<S> {
2016-08-01 03:40:31 +00:00
fn rem(lhs, rhs) -> $Angle<S> { $Angle(lhs.0 % rhs.0) }
});
2015-12-20 20:24:56 +00:00
impl_assignment_operator!(<S: BaseFloat> AddAssign<$Angle<S> > for $Angle<S> {
2016-08-01 03:40:31 +00:00
fn add_assign(&mut self, other) { self.0 += other.0; }
2015-12-20 20:24:56 +00:00
});
impl_assignment_operator!(<S: BaseFloat> SubAssign<$Angle<S> > for $Angle<S> {
2016-08-01 03:40:31 +00:00
fn sub_assign(&mut self, other) { self.0 -= other.0; }
2015-12-20 20:24:56 +00:00
});
impl_assignment_operator!(<S: BaseFloat> RemAssign<$Angle<S> > for $Angle<S> {
2016-08-01 03:40:31 +00:00
fn rem_assign(&mut self, other) { self.0 %= other.0; }
2015-12-20 20:24:56 +00:00
});
impl_operator!(<S: BaseFloat> Mul<S> for $Angle<S> {
2016-08-01 03:40:31 +00:00
fn mul(lhs, scalar) -> $Angle<S> { $Angle(lhs.0 * scalar) }
});
impl_operator!(<S: BaseFloat> Div<S> for $Angle<S> {
2016-08-01 03:40:31 +00:00
fn div(lhs, scalar) -> $Angle<S> { $Angle(lhs.0 / scalar) }
});
2015-12-20 20:24:56 +00:00
impl_assignment_operator!(<S: BaseFloat> MulAssign<S> for $Angle<S> {
2016-08-01 03:40:31 +00:00
fn mul_assign(&mut self, scalar) { self.0 *= scalar; }
2015-12-20 20:24:56 +00:00
});
impl_assignment_operator!(<S: BaseFloat> DivAssign<S> for $Angle<S> {
2016-08-01 03:40:31 +00:00
fn div_assign(&mut self, scalar) { self.0 /= scalar; }
2015-12-20 20:24:56 +00:00
});
impl<S: BaseFloat> ApproxEq for $Angle<S> {
type Epsilon = S::Epsilon;
#[inline]
fn default_epsilon() -> S::Epsilon {
S::default_epsilon()
}
#[inline]
fn default_max_relative() -> S::Epsilon {
S::default_max_relative()
}
#[inline]
fn default_max_ulps() -> u32 {
S::default_max_ulps()
}
#[inline]
fn relative_eq(&self, other: &Self, epsilon: S::Epsilon, max_relative: S::Epsilon) -> bool {
S::relative_eq(&self.0, &other.0, epsilon, max_relative)
}
#[inline]
fn ulps_eq(&self, other: &Self, epsilon: S::Epsilon, max_ulps: u32) -> bool {
S::ulps_eq(&self.0, &other.0, epsilon, max_ulps)
}
}
impl<S: BaseFloat + SampleRange> Rand for $Angle<S> {
#[inline]
fn rand<R: Rng>(rng: &mut R) -> $Angle<S> {
2016-08-01 03:40:31 +00:00
$Angle(rng.gen_range(cast(-$hi).unwrap(), cast($hi).unwrap()))
}
}
2015-12-29 10:50:43 +00:00
impl<S: fmt::Debug> fmt::Debug for $Angle<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2016-08-01 03:40:31 +00:00
write!(f, $fmt, self.0)
}
}
}
}
2013-09-04 02:20:53 +00:00
impl_angle!(Rad, "{:?} rad", f64::consts::PI * 2.0, f64::consts::PI);
impl_angle!(Deg, "{:?}°", 360, 180);