irox_units/shapes/
circular.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
// SPDX-License-Identifier: MIT
// Copyright 2023 IROX Contributors

//!
//! `CircularAspect` enum and `CircularDimension` struct, describes a circle by
//! radius or diameter with appropriate length units.
//!

use core::cmp::Ordering;
use core::fmt::{Display, Formatter};
use core::ops::{Div, DivAssign, Mul, MulAssign};

use crate::units::length::Length;

///
/// A way to describe a measurement of a Circle [`CircularAspect::Radius`] or
/// [`CircularAspect::Diameter`]
#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
pub enum CircularAspect {
    #[default]
    Radius,
    Diameter,
}

///
/// A discrete measurement of a Circle with a [`CircularAspect`] and a [`Length`]
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct CircularDimension {
    dimension_type: CircularAspect,
    dimension: Length,
}

impl Display for CircularDimension {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        write!(f, "+/- {}m", self.as_radius().dimension.as_meters().value())
    }
}

impl Default for CircularDimension {
    fn default() -> Self {
        CircularDimension {
            dimension: Length::new_meters(1.0),
            dimension_type: CircularAspect::Radius,
        }
    }
}

impl CircularDimension {
    #[must_use]
    pub const fn new(dimension_type: CircularAspect, dimension: Length) -> CircularDimension {
        CircularDimension {
            dimension,
            dimension_type,
        }
    }
    #[must_use]
    pub const fn new_radius(radius: Length) -> CircularDimension {
        CircularDimension::new(CircularAspect::Radius, radius)
    }
    #[must_use]
    pub const fn new_diameter(diameter: Length) -> CircularDimension {
        CircularDimension::new(CircularAspect::Diameter, diameter)
    }

    #[must_use]
    pub fn get_aspect(&self) -> CircularAspect {
        self.dimension_type
    }

    #[must_use]
    pub fn get_dimension(&self) -> Length {
        self.dimension
    }

    #[must_use]
    pub fn as_radius(self) -> CircularDimension {
        match self.dimension_type {
            CircularAspect::Radius => self,
            CircularAspect::Diameter => CircularDimension::new_radius(self.dimension / 2.0),
        }
    }

    #[must_use]
    pub fn as_diameter(self) -> CircularDimension {
        match self.dimension_type {
            CircularAspect::Radius => CircularDimension::new_diameter(self.dimension * 2.0),
            CircularAspect::Diameter => self,
        }
    }

    #[must_use]
    pub fn as_aspect(self, aspect: CircularAspect) -> CircularDimension {
        match aspect {
            CircularAspect::Radius => self.as_radius(),
            CircularAspect::Diameter => self.as_diameter(),
        }
    }
}

impl Mul<f64> for CircularDimension {
    type Output = CircularDimension;

    fn mul(self, rhs: f64) -> Self::Output {
        CircularDimension {
            dimension_type: self.dimension_type,
            dimension: self.dimension * rhs,
        }
    }
}

impl Mul<f64> for &CircularDimension {
    type Output = CircularDimension;

    fn mul(self, rhs: f64) -> Self::Output {
        CircularDimension {
            dimension_type: self.dimension_type,
            dimension: self.dimension * rhs,
        }
    }
}

impl MulAssign<f64> for CircularDimension {
    fn mul_assign(&mut self, rhs: f64) {
        self.dimension *= rhs
    }
}

impl Div<f64> for CircularDimension {
    type Output = CircularDimension;

    fn div(self, rhs: f64) -> Self::Output {
        CircularDimension {
            dimension_type: self.dimension_type,
            dimension: self.dimension / rhs,
        }
    }
}

impl Div<f64> for &CircularDimension {
    type Output = CircularDimension;

    fn div(self, rhs: f64) -> Self::Output {
        CircularDimension {
            dimension_type: self.dimension_type,
            dimension: self.dimension / rhs,
        }
    }
}

impl DivAssign<f64> for CircularDimension {
    fn div_assign(&mut self, rhs: f64) {
        self.dimension /= rhs
    }
}

impl PartialOrd<CircularDimension> for CircularDimension {
    fn partial_cmp(&self, other: &CircularDimension) -> Option<Ordering> {
        let dim = other.as_aspect(self.dimension_type).dimension;
        self.dimension.partial_cmp(&dim)
    }
}