devela/num/geom/shape/angle/impl/
int.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// devela::num::geom::shape::angle::impl::int
//
//!
//
// IMPROVE IDEAS
// - make alternative methods that doesn't depnd on floating point operations,
//   and instead use integer scaling functions Int::scale.
// - maybe use NonExtreme for the signed representation.

#[cfg(all(not(feature = "std"), _float··))]
use crate::ExtFloat;
#[cfg(_float··)]
#[allow(unused_imports)]
use crate::{fsize, ExtFloatConst};
use crate::{Angle, AngleDirection, AngleKind};

/// impl `Angle` methods with an integer representation:
///
/// # TOC
/// - integer common methods
/// - signed integers specific methods
/// - unsigned integers specific methods
///
/// # Macro arguments
/// $t: the inner integer primitive type
/// $f: the associated floating point type
/// $tcap: the capability feature that enables the given integer implementation. E.g "_int_i8".
/// $fcap: the capability feature that enables the given floating implementation. E.g "_float_f32".
macro_rules! impl_angle {
    () => {
        impl_angle![sint
            i8:f32;"_int_i8":"_float_f32",
            i16:f32;"_int_i16":"_float_f32",
            i32:f32;"_int_i32":"_float_f32",
            i64:f64;"_int_i64":"_float_f64",
            i128:f64;"_int_i128":"_float_f64"
        ];
        #[cfg(target_pointer_width = "32")]
        impl_angle![sint isize:fsize;"_int_isize":"_float_f32"];
        #[cfg(target_pointer_width = "64")]
        impl_angle![sint isize:fsize;"_int_isize":"_float_f64"];

        impl_angle![uint
            u8:f32;"_int_u8":"_float_f32",
            u16:f32;"_int_u16":"_float_f32",
            u32:f32;"_int_u32":"_float_f32",
            u64:f64;"_int_u64":"_float_f64",
            u128:f64;"_int_u128":"_float_f64"
        ];
        #[cfg(target_pointer_width = "32")]
        impl_angle![uint usize:fsize;"_int_usize":"_float_f32"];
        #[cfg(target_pointer_width = "64")]
        impl_angle![uint usize:fsize;"_int_usize":"_float_f64"];
    };

    // integers common methods
    (int $($t:ty : $f:ty ; $tcap:literal : $fcap:literal),+) => {
        $( impl_angle![@int $t:$f ; $tcap:$fcap]; )+
    };
    (@int $t:ty : $f:ty ; $tcap:literal : $fcap:literal) => {
        #[doc = concat!("# Methods for angles represented using `", stringify!($t), "`.")]
        #[cfg(feature = $tcap )]
        #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = $tcap)))]
        impl Angle<$t> {
            /* private helpers */

            // Returns the inner value normalized as a float between -1 and 1
            const fn to_float_normalized(self) -> $f { self.0 as $f / <$t>::MAX as $f }
            // Returns the `value` associated to the full turn `unit`, scaled to the full $t range.
            #[cfg(any(feature = "std", feature = $fcap))]
            fn from_float_normalized(value: $f, unit: $f) -> $t {
                ((value / unit) * <$t>::MAX as $f).round() as $t
            }

            /* construct */

            /// Creates a normalized full positive angle at 0 degrees.
            pub const fn new_full() -> Self { Self(0) }

            /// Creates a normalized right positive angle at 90 degrees.
            pub const fn new_right() -> Self { Self(<$t>::MAX / 4) }

            /// Creates a normalized straight positive angle at 180 degrees.
            pub const fn new_straight() -> Self { Self(<$t>::MAX / 2) }

            /// Creates a new angle from a floating-point `radians` value.
            #[cfg(any(feature = "std", feature = $fcap))]
            #[cfg_attr(feature = "nightly_doc", doc(cfg(any(feature = "std", feature = $fcap))))]
            pub fn from_rad(radians: $f) -> Self {
                Self(Self::from_float_normalized(radians, <$f>::TAU))
            }

            /// Creates a new angle from a floating-point `degrees` value.
            #[cfg(any(feature = "std", feature = $fcap))]
            #[cfg_attr(feature = "nightly_doc", doc(cfg(any(feature = "std", feature = $fcap))))]
            pub fn from_deg(degrees: $f) -> Self {
                Self(Self::from_float_normalized(degrees, 360.0))
            }

            /// Creates a new angle from a `value` in a `custom_unit` which represents a full turn.
            #[cfg(any(feature = "std", feature = $fcap))]
            #[cfg_attr(feature = "nightly_doc", doc(cfg(any(feature = "std", feature = $fcap))))]
            pub fn from_custom(value: $f, custom_unit: $f) -> Self {
                Self(Self::from_float_normalized(value, custom_unit))
            }

            /* convert */

            /// Converts the angle to radians.
            #[must_use]
            #[cfg(any(feature = "std", _float··))]
            #[cfg_attr(feature = "nightly_doc", doc(cfg(any(feature = "std", _float··))))]
            pub const fn to_rad(self) -> $f { self.to_float_normalized() * <$f>::TAU }

            /// Converts the angle to degrees.
            #[must_use]
            pub const fn to_deg(self) -> $f { self.to_float_normalized() * 360.0 }

            /// Converts the angle to a `custom_unit` which represents a full turn.
            #[must_use]
            pub const fn to_custom(self, custom_unit: $f) -> $f {
                self.to_float_normalized() * custom_unit
            }

            /* normalize */

            /// Always returns `true` since integer representations are always normalized.
            #[must_use]
            pub const fn is_normalized(self) -> bool { true }

            /// Returns the angle normalized (no-op for integer representation).
            pub const fn normalize(self) -> Self { self }

            /// Sets the angle normalized (no-op for integer representation).
            pub fn set_normalized(&mut self) {}

            /// Returns `true` if the angle has the given `direction`.
            #[must_use ]
            pub const fn has_direction(self, direction: AngleDirection) -> bool {
                direction as i8 == self.direction() as i8
            }

            /* kind */

            /// Returns the kind of the normalized angle.
            pub const fn kind(self) -> AngleKind {
                let angle = self.positive().0;
                let right = <$t>::MAX / 4;
                let straight = <$t>::MAX / 2;
                use AngleKind as K;
                if angle == 0 { // 1 turn (0' or 360º)
                    K::Full
                } else if angle == right { // 1/4 turn (90º)
                    K::Right
                } else if angle == straight { // 1/2 turn (180º)
                    K::Straight
                //
                } else if angle < right { // < 1/4 turn (< 90º)
                    K::Acute
                } else if angle < straight { // < 1/2 turn (< 180º)
                    K::Obtuse
                } else { // < 1 turn (< 360º)
                    K::Reflex
                }
            }

            /// Returns `true` if the angle is of the given `kind`.
            #[must_use]
            pub const fn is_kind(self, kind: AngleKind) -> bool {
                let angle = self.positive().0;
                let right = <$t>::MAX / 4;
                let straight = <$t>::MAX / 2;

                use AngleKind as K;
                match kind {
                    K::Full => angle == 0,
                    K::Right => angle == right,
                    K::Straight => angle == straight,
                    //
                    K::Acute => angle > 0 && angle < right,
                    K::Obtuse => angle < right && angle < straight,
                    K::Reflex => angle > right,
                }
            }
        }
    };

    // signed integers specific methods
    (sint $($t:ty : $f:ty ; $tcap:literal : $fcap:literal),+) => {
        $( impl_angle![@sint $t:$f ; $tcap:$fcap]; )+
    };
    (@sint $t:ty : $f:ty ; $tcap:literal : $fcap:literal) => {
        impl_angle![int $t:$f ; $tcap:$fcap];

        #[doc = concat!("# Methods for angles represented using `", stringify!($t), "`, signed.")]
        #[cfg(feature = $tcap )]
        #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = $tcap)))]
        impl Angle<$t> {
            /* direction */

            /// Returns the angle direction.
            ///
            /// The direction will be `Undefined` if the angle kind is [`Full`][AngleKind::Full].
            pub const fn direction(self) -> AngleDirection {
                use AngleDirection as D;
                if self.0 == 0 {
                    D::Undefined
                } else if self.0 > 0 {
                    D::CounterClockwise
                } else {
                    D::Clockwise
                }
            }

            /// Returns a version of the angle with the given `direction`.
            ///
            /// An `Undefined` direction will be interpreted as counter-clockwise (positive).
            pub const fn with_direction(self, direction: AngleDirection) -> Self {
                use AngleDirection as D;
                match direction {
                    D::CounterClockwise | D::Undefined => Self(self.0.saturating_abs()),
                    D::Clockwise => Self(-self.0.saturating_abs()),
                }
            }

            /// Returns a version of the angle with the given `direction`.
            ///
            /// An `Undefined` direction will be interpreted as counter-clockwise (positive).
            pub fn set_direction(&mut self, direction: AngleDirection) {
                use AngleDirection as D;
                match direction {
                    D::CounterClockwise | D::Undefined => self.0 = self.0.saturating_abs(),
                    D::Clockwise => self.0 = -self.0.saturating_abs(),
                }
            }

            /// Returns a version of the angle with inverted direction.
            pub const fn invert_direction(self) -> Self {
                Self(self.0.saturating_neg())
            }

            /// Returns the negative version of the angle.
            pub const fn negative(self) -> Self { Self(-self.0.saturating_abs()) }

            /// Sets the angle as negative.
            pub fn set_negative(&mut self) { self.0 = -self.0.saturating_abs(); }

            /// Returns the positive version of the angle.
            pub const fn positive(self) -> Self { Self(self.0.saturating_abs()) }

            /// Sets the angle as positive.
            pub fn set_positive(&mut self) { self.0 = self.0.saturating_abs(); }
        }
    };

    // unsigned integers specific methods
    (uint $($t:ty : $f:ty ; $tcap:literal : $fcap:literal),+) => {
        $( impl_angle![@uint $t:$f ; $tcap:$fcap]; )+
    };
    (@uint $t:ty : $f:ty ; $tcap:literal : $fcap:literal) => {
        impl_angle![int $t:$f ; $tcap:$fcap];

        #[doc = concat!("# Methods for angles represented using `", stringify!($t), "`, unsigned.")]
        #[cfg(feature = $tcap )]
        #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = $tcap)))]
        impl Angle<$t> {
            /* direction */

            /// Returns the angle direction.
            ///
            /// For unsigned integers the direction is always `CounterClockwise`.
            pub const fn direction(self) -> AngleDirection { AngleDirection::CounterClockwise }

            /// Returns a version of the angle with the given `direction` (no-op for unsigned).
            ///
            /// Unsigned integers can only have `CounterClockwise` direction.
            pub const fn with_direction(self, _direction: AngleDirection) -> Self { self }

            /// Returns a version of the angle with the given `direction` (no-op for unsigned).
            ///
            /// Unsigned integers can only have `CounterClockwise` direction.
            pub const fn set_direction(self, _direction: AngleDirection) {}

            /// Returns a version of the angle with inverted direction (no-op for unsigned).
            ///
            /// Unsigned integers can only have `CounterClockwise` direction.
            pub const fn invert_direction(self) -> Self { self }

            /// Returns the negative version of the angle (no-op for unsigned).
            pub const fn negative(self) -> Self { self }

            /// Sets the angle as negative (no-op for unsigned).
            pub fn set_negative(&mut self) {}

            /// Returns the positive version of the angle (no-op for unsigned).
            pub const fn positive(self) -> Self { self }

            /// Sets the angle as positive (no-op for unsigned).
            pub fn set_positive(&mut self) {}

        }
    };
}
impl_angle!();