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

//!
//! `Ellipse` struct, describes an ellipse using two `CircularDimension`
//! axes and an optional `CompassDirection` orientation of the first axis
//!

use core::fmt::{Display, Formatter};

use crate::shapes::CircularDimension;
use crate::units::compass::CompassDirection;

///
/// A discrete measurement of an Ellipse.  An Ellipse is a circle with two
/// [`CircularDimension`]s offset by 90° to each other.
/// The `Ellipse::first_axis` orientation is indicated by `Ellipse::orientation`
/// and the `Ellipse::second_axis` is oriented orthogonally to the first.
#[derive(Debug, Copy, Clone, PartialEq, Default)]
pub struct Ellipse {
    first_axis: CircularDimension,
    second_axis: CircularDimension,
    orientation: Option<CompassDirection>,
}

impl Display for Ellipse {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        match self.orientation {
            Some(o) => {
                write!(f, "{} / {} {}", self.first_axis, self.second_axis, o)
            }
            None => {
                write!(f, "{} / {}", self.first_axis, self.second_axis)
            }
        }
    }
}

impl Ellipse {
    #[must_use]
    pub fn new(first_axis: CircularDimension, second_axis: CircularDimension) -> Ellipse {
        Ellipse {
            first_axis,
            second_axis,
            orientation: None,
        }
    }

    #[must_use]
    pub fn semi_major_axis(&self) -> CircularDimension {
        if self.first_axis > self.second_axis {
            return self.first_axis.as_radius();
        }
        self.second_axis.as_radius()
    }

    #[must_use]
    pub fn semi_minor_axis(&self) -> CircularDimension {
        if self.first_axis > self.second_axis {
            return self.second_axis.as_radius();
        }
        self.first_axis.as_radius()
    }

    #[must_use]
    pub fn major_axis(&self) -> CircularDimension {
        if self.first_axis > self.second_axis {
            return self.first_axis.as_diameter();
        }
        self.second_axis.as_diameter()
    }

    #[must_use]
    pub fn minor_axis(&self) -> CircularDimension {
        if self.first_axis > self.second_axis {
            return self.second_axis.as_diameter();
        }
        self.first_axis.as_diameter()
    }

    #[must_use]
    pub fn orientation(&self) -> Option<CompassDirection> {
        self.orientation
    }

    #[must_use]
    pub fn with_orientation(self, orientation: CompassDirection) -> Ellipse {
        Ellipse {
            first_axis: self.first_axis,
            second_axis: self.second_axis,
            orientation: Some(orientation),
        }
    }
}

impl From<CircularDimension> for Ellipse {
    fn from(value: CircularDimension) -> Self {
        Ellipse {
            first_axis: value,
            second_axis: value,
            orientation: None,
        }
    }
}

impl From<&CircularDimension> for Ellipse {
    fn from(value: &CircularDimension) -> Self {
        Ellipse {
            first_axis: *value,
            second_axis: *value,
            orientation: None,
        }
    }
}