irox_carto/geo/
ellipse.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
// SPDX-License-Identifier: MIT
// Copyright 2025 IROX Contributors
//

use irox_units::units::length::{Length, LengthUnits};

use crate::geo::ellipsoid::Ellipsoid;
use crate::geo::EllipticalShape;

#[allow(unused_imports)]
use irox_tools::f64::FloatExt;

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Ellipse {
    semi_major_axis: Length,
    inverse_flattening: f64,
    name: &'static str,
}

impl Ellipse {
    #[must_use]
    pub const fn named(
        name: &'static str,
        semi_major_axis: Length,
        inverse_flattening: f64,
    ) -> Ellipse {
        Ellipse {
            semi_major_axis,
            inverse_flattening,
            name,
        }
    }
    #[must_use]
    pub const fn new(semi_major_axis: Length, inverse_flattening: f64) -> Ellipse {
        Ellipse {
            semi_major_axis,
            inverse_flattening,
            name: "unnamed",
        }
    }

    #[must_use]
    pub const fn new_meters(semi_major_axis_meters: f64, inverse_flattening: f64) -> Ellipse {
        Self::new(
            Length::new(semi_major_axis_meters, LengthUnits::Meters),
            inverse_flattening,
        )
    }

    #[must_use]
    pub const fn new_sphere(radius: Length) -> Ellipse {
        Ellipse {
            semi_major_axis: radius,
            inverse_flattening: 0.0,
            name: "unnamed",
        }
    }

    #[must_use]
    pub const fn new_sphere_meters(radius_meters: f64) -> Ellipse {
        Self::new_sphere(Length::new(radius_meters, LengthUnits::Meters))
    }

    #[must_use]
    pub const fn semi_major_axis_a(&self) -> Length {
        self.semi_major_axis
    }

    #[must_use]
    pub const fn inverse_flattening(&self) -> f64 {
        self.inverse_flattening
    }

    #[must_use]
    pub const fn name(&self) -> &'static str {
        self.name
    }

    #[must_use]
    pub fn semi_minor_axis_b(&self) -> Length {
        self.semi_major_axis * (1.0 - self.flattening_f())
    }

    #[must_use]
    pub fn flattening_f(&self) -> f64 {
        1.0 / self.inverse_flattening
    }

    #[must_use]
    pub fn first_eccentricity_squared(&self) -> f64 {
        let f = self.flattening_f();
        f * (2.0 - f)
    }

    #[must_use]
    pub fn first_eccentricity(&self) -> f64 {
        self.first_eccentricity_squared().sqrt()
    }

    #[must_use]
    pub fn second_eccentricity_squared(&self) -> f64 {
        self.first_eccentricity_squared() / (1.0 - self.first_eccentricity_squared())
    }

    #[must_use]
    pub fn second_eccentricity(&self) -> f64 {
        self.first_eccentricity_squared().sqrt()
    }

    #[must_use]
    pub fn as_ellipsoid(&self) -> Ellipsoid {
        Ellipsoid::from(*self)
    }
    #[must_use]
    pub fn as_elliptical_shape(&self) -> EllipticalShape {
        EllipticalShape::Ellipse(*self)
    }
}

impl From<Ellipsoid> for Ellipse {
    fn from(value: Ellipsoid) -> Self {
        Ellipse::new(value.semi_major_axis, value.inverse_flattening)
    }
}