Debian Codegraph @

src/angle.rs

raw symbols SCIP
1 //! Traits for working with angular values, such as for in hues.
2
3 use craterust-analyzer cargo palette 0.7.6 crate/::{
4 bool_maskrust-analyzer cargo palette 0.7.6 bool_mask/::HasBoolMaskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#,
5 numrust-analyzer cargo palette 0.7.6 num/::{Realrust-analyzer cargo palette 0.7.6 num/Real#, Roundrust-analyzer cargo palette 0.7.6 num/Round#},
6 };
7
8 #[cfg(feature = "wide")]
9 mod widerust-analyzer cargo palette 0.7.6 angle/wide/;
10
11 /// Represents types that can express half of a rotation (i.e. 180 degrees).
12 pub trait HalfRotationrust-analyzer cargo palette 0.7.6 angle/HalfRotation# {
13 /// Return a value that represents half of a rotation (i.e. 180 degrees).
14 #[must_use]
15 fn half_rotationrust-analyzer cargo palette 0.7.6 angle/HalfRotation#half_rotation().() -> Self;
16 }
17
18 /// Represents types that can express a full rotation (i.e. 360 degrees).
19 pub trait FullRotationrust-analyzer cargo palette 0.7.6 angle/FullRotation# {
20 /// Return a value that represents a full rotation (i.e. 360 degrees).
21 #[must_use]
22 fn full_rotationrust-analyzer cargo palette 0.7.6 angle/FullRotation#full_rotation().() -> Self;
23 }
24
25 /// Angle values that are real numbers and can represent both radians and
26 /// degrees.
27 pub trait RealAnglerust-analyzer cargo palette 0.7.6 angle/RealAngle#: Realrust-analyzer cargo palette 0.7.6 num/Real# {
28 /// Consider `self` to be radians and convert it to degrees.
29 #[must_use]
30 fn radians_to_degreesrust-analyzer cargo palette 0.7.6 angle/RealAngle#radians_to_degrees().(self) -> Self;
31
32 /// Consider `self` to be degrees and convert it to radians.
33 #[must_use]
34 fn degrees_to_radiansrust-analyzer cargo palette 0.7.6 angle/RealAngle#degrees_to_radians().(self) -> Self;
35 }
36
37 /// Angular equality, where 0 degrees and 360 degrees are equal.
38 pub trait AngleEqrust-analyzer cargo palette 0.7.6 angle/AngleEq#: HasBoolMaskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask# {
39 /// Check if `self` and `other` represent the same angle on a circle.
40 #[must_use]
41 fn angle_eqrust-analyzer cargo palette 0.7.6 angle/AngleEq#angle_eq().(&self, other: &Self) -> Self::Maskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#Mask#;
42 }
43
44 /// Angle types that can represent the full circle using positive and negative
45 /// values.
46 pub trait SignedAnglerust-analyzer cargo palette 0.7.6 angle/SignedAngle# {
47 /// Normalize `self` to a range corresponding to -180 to 180 degrees.
48 #[must_use]
49 fn normalize_signed_anglerust-analyzer cargo palette 0.7.6 angle/SignedAngle#normalize_signed_angle().(self) -> Self;
50 }
51
52 /// Angle types that can represent the full circle as positive values.
53 pub trait UnsignedAnglerust-analyzer cargo palette 0.7.6 angle/UnsignedAngle# {
54 /// Normalize `self` to a range corresponding to 0 to 360 degrees.
55 #[must_use]
56 fn normalize_unsigned_anglerust-analyzer cargo palette 0.7.6 angle/UnsignedAngle#normalize_unsigned_angle().(self) -> Self;
57 }
58
59 /// Performs value-to-value conversion between angle types. See also [`IntoAngle`].
60 pub trait FromAnglerust-analyzer cargo palette 0.7.6 angle/FromAngle#<T> {
61 /// Performs a conversion from `angle`.
62 fn from_anglerust-analyzer cargo palette 0.7.6 angle/FromAngle#from_angle().(angle: T) -> Self;
63 }
64
65 impl<T> FromAnglerust-analyzer cargo palette 0.7.6 angle/FromAngle#<T> for T {
66 #[inline]
67 fn from_anglerust-analyzer cargo palette 0.7.6 angle/impl#[T][`FromAngle<Self>`]from_angle().(angle: Selfrust-analyzer cargo palette 0.7.6 angle/impl#[T][`FromAngle<Self>`]) -> Selfrust-analyzer cargo palette 0.7.6 angle/impl#[T][`FromAngle<Self>`] {
68 angle
69 }
70 }
71
72 /// Performs value-to-value conversion between angle types. See also [`IntoAngle`].
73 pub trait IntoAnglerust-analyzer cargo palette 0.7.6 angle/IntoAngle#<T> {
74 /// Performs a conversion into `T`.
75 fn into_anglerust-analyzer cargo palette 0.7.6 angle/IntoAngle#into_angle().(self) -> T;
76 }
77
78 impl<T, U> IntoAnglerust-analyzer cargo palette 0.7.6 angle/IntoAngle#<U> for T
79 where
80 U: FromAnglerust-analyzer cargo palette 0.7.6 angle/FromAngle#<T>,
81 {
82 #[inline]
83 fn into_anglerust-analyzer cargo palette 0.7.6 angle/impl#[T][`IntoAngle<U>`]into_angle().(self) -> U {
84 U::from_anglerust-analyzer cargo palette 0.7.6 angle/FromAngle#from_angle().(self)
85 }
86 }
87
88 macro_rules! impl_angle_floatrust-analyzer cargo palette 0.7.6 angle/impl_angle_float! {
89 ($($ty: ident),+) => {
90 $(
91 impl HalfRotation for $ty {
92 #[inline]
93 fn half_rotation() -> Self {
94 180.0
95 }
96 }
97
98 impl FullRotation for $ty {
99 #[inline]
100 fn full_rotation() -> Self {
101 360.0
102 }
103 }
104
105 impl RealAngle for $ty {
106 #[inline]
107 fn degrees_to_radians(self) -> Self {
108 self.to_radians()
109 }
110
111 #[inline]
112 fn radians_to_degrees(self) -> Self {
113 self.to_degrees()
114 }
115 }
116
117 impl AngleEq for $ty {
118 #[inline]
119 fn angle_eq(&self, other: &Self) -> bool {
120 self.normalize_unsigned_angle() == other.normalize_unsigned_angle()
121 }
122 }
123
124 impl SignedAngle for $ty {
125 #[inline]
126 fn normalize_signed_angle(self) -> Self {
127 self - Round::ceil(((self + 180.0) / 360.0) - 1.0) * 360.0
128 }
129 }
130
131 impl UnsignedAngle for $ty {
132 #[inline]
133 fn normalize_unsigned_angle(self) -> Self {
134 self - (Round::floor(self / 360.0) * 360.0)
135 }
136 }
137 )+
138 };
139 }
140
141 macro_rules! impl_from_angle_floatrust-analyzer cargo palette 0.7.6 angle/impl_from_angle_float! {
142 ($ty: ident to $other_ty: ident) => {
143 impl FromAngle<$other_ty> for $ty {
144 #[inline]
145 fn from_angle(angle: $other_ty) -> Self {
146 angle as $ty
147 }
148 }
149 };
150 }
151
152 macro_rules! impl_from_angle_u8rust-analyzer cargo palette 0.7.6 angle/impl_from_angle_u8! {
153 ($($float_ty: ident),*) => {
154 $(
155 impl FromAngle<u8> for $float_ty {
156 #[inline]
157 fn from_angle(angle: u8) -> Self {
158 (angle as $float_ty / 256.0) * Self::full_rotation()
159 }
160 }
161
162 impl FromAngle<$float_ty> for u8 {
163 #[inline]
164 fn from_angle(angle: $float_ty) -> Self {
165 let normalized = angle.normalize_unsigned_angle() / $float_ty::full_rotation();
166 let rounded = (normalized * 256.0).round();
167
168 if rounded > 255.5 {
169 0
170 } else {
171 rounded as u8
172 }
173 }
174 }
175 )*
176 };
177 }
178
179 impl_angle_floatrust-analyzer cargo palette 0.7.6 angle/impl_angle_float!!(f32, f64);
180 impl_from_angle_floatrust-analyzer cargo palette 0.7.6 angle/impl_from_angle_float!!(f32 to f64);
181 impl_from_angle_floatrust-analyzer cargo palette 0.7.6 angle/impl_from_angle_float!!(f64 to f32);
182 impl_from_angle_u8rust-analyzer cargo palette 0.7.6 angle/impl_from_angle_u8!!(f32, f64);
183
184 impl HalfRotationrust-analyzer cargo palette 0.7.6 angle/HalfRotation# for u8 {
185 #[inline]
186 fn half_rotationrust-analyzer cargo palette 0.7.6 angle/impl#[u8][HalfRotation]half_rotation().() -> Selfrust-analyzer cargo palette 0.7.6 angle/impl#[u8][HalfRotation] {
187 128
188 }
189 }
190
191 impl AngleEqrust-analyzer cargo palette 0.7.6 angle/AngleEq# for u8 {
192 #[inline]
193 fn angle_eqrust-analyzer cargo palette 0.7.6 angle/impl#[u8][AngleEq]angle_eq().(&self, other: &Selfrust-analyzer cargo palette 0.7.6 angle/impl#[u8][AngleEq]) -> bool {
194 self rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().other
195 }
196 }
197
198 impl UnsignedAnglerust-analyzer cargo palette 0.7.6 angle/UnsignedAngle# for u8 {
199 #[inline]
200 fn normalize_unsigned_anglerust-analyzer cargo palette 0.7.6 angle/impl#[u8][UnsignedAngle]normalize_unsigned_angle().(self) -> Selfrust-analyzer cargo palette 0.7.6 angle/impl#[u8][UnsignedAngle] {
201 self
202 }
203 }
204
205 #[cfg(test)]
206 mod testrust-analyzer cargo palette 0.7.6 angle/test/ {
207 use craterust-analyzer cargo palette 0.7.6 crate/::RgbHuerust-analyzer cargo palette 0.7.6 hues/RgbHue#;
208
209 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
210 fn f32_to_u8rust-analyzer cargo palette 0.7.6 angle/test/f32_to_u8().() {
211 let hue_f32 = RgbHuerust-analyzer cargo palette 0.7.6 hues/RgbHue#::newrust-analyzer cargo palette 0.7.6 hues/impl#[`RgbHue<T>`]new().(180.0f32);
212 let hue_u8 = hue_f32.into_formatrust-analyzer cargo palette 0.7.6 hues/impl#[`RgbHue<T>`]into_format().::<u8>();
213 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(hue_u8, RgbHuerust-analyzer cargo palette 0.7.6 hues/RgbHue#::newrust-analyzer cargo palette 0.7.6 hues/impl#[`RgbHue<T>`]new().(128u8));
214 }
215
216 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
217 fn u8_to_f32rust-analyzer cargo palette 0.7.6 angle/test/u8_to_f32().() {
218 let hue_f32 = RgbHuerust-analyzer cargo palette 0.7.6 hues/RgbHue#::newrust-analyzer cargo palette 0.7.6 hues/impl#[`RgbHue<T>`]new().(128u8);
219 let hue_u8 = hue_f32.into_formatrust-analyzer cargo palette 0.7.6 hues/impl#[`RgbHue<T>`]into_format().::<f32>();
220 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(hue_u8, RgbHuerust-analyzer cargo palette 0.7.6 hues/RgbHue#::newrust-analyzer cargo palette 0.7.6 hues/impl#[`RgbHue<T>`]new().(180.0f32));
221 }
222 }