1use std::f64;
19use std::fmt;
20use std::iter;
21use std::ops::*;
22
23use num_traits::{cast, Bounded};
24#[cfg(feature = "rand")]
25use rand::{
26 distributions::{uniform::SampleUniform, Distribution, Standard},
27 Rng,
28};
29
30use structure::*;
31
32use approx;
33use num::BaseFloat;
34
35#[repr(C)]
39#[derive(Copy, Clone, PartialEq, PartialOrd)]
40#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
41pub struct Rad<S>(pub S);
42
43#[repr(C)]
47#[derive(Copy, Clone, PartialEq, PartialOrd)]
48#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
49pub struct Deg<S>(pub S);
50
51impl<S> From<Rad<S>> for Deg<S>
52where
53 S: BaseFloat,
54{
55 #[inline]
56 fn from(rad: Rad<S>) -> Deg<S> {
57 Deg(rad.0 * cast(180.0 / f64::consts::PI).unwrap())
58 }
59}
60
61impl<S> From<Deg<S>> for Rad<S>
62where
63 S: BaseFloat,
64{
65 #[inline]
66 fn from(deg: Deg<S>) -> Rad<S> {
67 Rad(deg.0 * cast(f64::consts::PI / 180.0).unwrap())
68 }
69}
70
71macro_rules! impl_angle {
72 ($Angle:ident, $fmt:expr, $full_turn:expr, $hi:expr) => {
73 impl<S: BaseFloat> Zero for $Angle<S> {
74 #[inline]
75 fn zero() -> $Angle<S> {
76 $Angle(S::zero())
77 }
78
79 #[inline]
80 fn is_zero(&self) -> bool {
81 ulps_eq!(self, &Self::zero())
82 }
83 }
84
85 impl<S: BaseFloat> iter::Sum<$Angle<S>> for $Angle<S> {
86 #[inline]
87 fn sum<I: Iterator<Item=$Angle<S>>>(iter: I) -> $Angle<S> {
88 iter.fold($Angle::zero(), Add::add)
89 }
90 }
91
92 impl<'a, S: 'a + BaseFloat> iter::Sum<&'a $Angle<S>> for $Angle<S> {
93 #[inline]
94 fn sum<I: Iterator<Item=&'a $Angle<S>>>(iter: I) -> $Angle<S> {
95 iter.fold($Angle::zero(), Add::add)
96 }
97 }
98
99 impl<S: BaseFloat> Angle for $Angle<S> {
100 type Unitless = S;
101
102 #[inline] fn full_turn() -> $Angle<S> { $Angle(cast($full_turn).unwrap()) }
103
104 #[inline] fn sin(self) -> S { Rad::from(self).0.sin() }
105 #[inline] fn cos(self) -> S { Rad::from(self).0.cos() }
106 #[inline] fn tan(self) -> S { Rad::from(self).0.tan() }
107 #[inline] fn sin_cos(self) -> (S, S) { Rad::from(self).0.sin_cos() }
108
109 #[inline] fn asin(a: S) -> $Angle<S> { Rad(a.asin()).into() }
110 #[inline] fn acos(a: S) -> $Angle<S> { Rad(a.acos()).into() }
111 #[inline] fn atan(a: S) -> $Angle<S> { Rad(a.atan()).into() }
112 #[inline] fn atan2(a: S, b: S) -> $Angle<S> { Rad(a.atan2(b)).into() }
113 }
114
115 impl<S: BaseFloat> Neg for $Angle<S> {
116 type Output = $Angle<S>;
117
118 #[inline]
119 fn neg(self) -> $Angle<S> { $Angle(-self.0) }
120 }
121
122 impl<'a, S: BaseFloat> Neg for &'a $Angle<S> {
123 type Output = $Angle<S>;
124
125 #[inline]
126 fn neg(self) -> $Angle<S> { $Angle(-self.0) }
127 }
128
129 impl<S: Bounded> Bounded for $Angle<S> {
130 #[inline]
131 fn min_value() -> $Angle<S> {
132 $Angle(S::min_value())
133 }
134
135 #[inline]
136 fn max_value() -> $Angle<S> {
137 $Angle(S::max_value())
138 }
139 }
140
141 impl_operator!(<S: BaseFloat> Add<$Angle<S> > for $Angle<S> {
142 fn add(lhs, rhs) -> $Angle<S> { $Angle(lhs.0 + rhs.0) }
143 });
144 impl_operator!(<S: BaseFloat> Sub<$Angle<S> > for $Angle<S> {
145 fn sub(lhs, rhs) -> $Angle<S> { $Angle(lhs.0 - rhs.0) }
146 });
147 impl_operator!(<S: BaseFloat> Div<$Angle<S> > for $Angle<S> {
148 fn div(lhs, rhs) -> S { lhs.0 / rhs.0 }
149 });
150 impl_operator!(<S: BaseFloat> Rem<$Angle<S> > for $Angle<S> {
151 fn rem(lhs, rhs) -> $Angle<S> { $Angle(lhs.0 % rhs.0) }
152 });
153 impl_assignment_operator!(<S: BaseFloat> AddAssign<$Angle<S> > for $Angle<S> {
154 fn add_assign(&mut self, other) { self.0 += other.0; }
155 });
156 impl_assignment_operator!(<S: BaseFloat> SubAssign<$Angle<S> > for $Angle<S> {
157 fn sub_assign(&mut self, other) { self.0 -= other.0; }
158 });
159 impl_assignment_operator!(<S: BaseFloat> RemAssign<$Angle<S> > for $Angle<S> {
160 fn rem_assign(&mut self, other) { self.0 %= other.0; }
161 });
162
163 impl_operator!(<S: BaseFloat> Mul<S> for $Angle<S> {
164 fn mul(lhs, scalar) -> $Angle<S> { $Angle(lhs.0 * scalar) }
165 });
166 impl_operator!(<S: BaseFloat> Div<S> for $Angle<S> {
167 fn div(lhs, scalar) -> $Angle<S> { $Angle(lhs.0 / scalar) }
168 });
169 impl_assignment_operator!(<S: BaseFloat> MulAssign<S> for $Angle<S> {
170 fn mul_assign(&mut self, scalar) { self.0 *= scalar; }
171 });
172 impl_assignment_operator!(<S: BaseFloat> DivAssign<S> for $Angle<S> {
173 fn div_assign(&mut self, scalar) { self.0 /= scalar; }
174 });
175
176 impl<S: BaseFloat> approx::AbsDiffEq for $Angle<S> {
177 type Epsilon = S::Epsilon;
178
179 #[inline]
180 fn default_epsilon() -> S::Epsilon {
181 S::default_epsilon()
182 }
183
184 #[inline]
185 fn abs_diff_eq(&self, other: &Self, epsilon: S::Epsilon) -> bool {
186 S::abs_diff_eq(&self.0, &other.0, epsilon)
187 }
188 }
189
190 impl<S: BaseFloat> approx::RelativeEq for $Angle<S> {
191 #[inline]
192 fn default_max_relative() -> S::Epsilon {
193 S::default_max_relative()
194 }
195
196 #[inline]
197 fn relative_eq(&self, other: &Self, epsilon: S::Epsilon, max_relative: S::Epsilon) -> bool {
198 S::relative_eq(&self.0, &other.0, epsilon, max_relative)
199 }
200 }
201
202 impl<S: BaseFloat> approx::UlpsEq for $Angle<S> {
203 #[inline]
204 fn default_max_ulps() -> u32 {
205 S::default_max_ulps()
206 }
207
208 #[inline]
209 fn ulps_eq(&self, other: &Self, epsilon: S::Epsilon, max_ulps: u32) -> bool {
210 S::ulps_eq(&self.0, &other.0, epsilon, max_ulps)
211 }
212 }
213
214 #[cfg(feature = "rand")]
215 impl<S> Distribution<$Angle<S>> for Standard
216 where Standard: Distribution<S>,
217 S: BaseFloat + SampleUniform {
218 #[inline]
219 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $Angle<S> {
220 $Angle(rng.gen_range(cast::<_, S>(-$hi).unwrap() .. cast::<_, S>($hi).unwrap()))
221 }
222 }
223
224 impl<S: fmt::Debug> fmt::Debug for $Angle<S> {
225 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
226 write!(f, $fmt, self.0)
227 }
228 }
229 }
230}
231
232impl_angle!(Rad, "{:?} rad", f64::consts::PI * 2.0, f64::consts::PI);
233impl_angle!(Deg, "{:?}°", 360, 180);