Skip to content

Commit a111d53

Browse files
committed
Streamline the coordinate tree
- CoordinateSet trait moved to set.rs - Improved doc comments in coorXX.rs - Centralize (by macro) the implementation of indexing for CoorXX - Cleanup
1 parent a6410f8 commit a111d53

File tree

8 files changed

+257
-235
lines changed

8 files changed

+257
-235
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Changed
1515

16+
- `CoordinateTuple` trait now requires implementation of the constructor
17+
method `new(fill: f64)`, returning an object of `dim()` copies of `fill`.
18+
1619
### Removed
1720

1821
- CHANGES.md

src/coordinate/coor2d.rs

+19-23
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,36 @@ use crate::math::angular;
55
#[derive(Debug, Default, PartialEq, Copy, Clone)]
66
pub struct Coor2D(pub [f64; 2]);
77

8-
use std::ops::{Index, IndexMut};
8+
impl CoordinateTuple for Coor2D {
9+
fn new(fill: f64) -> Self {
10+
Coor2D([fill; 2])
11+
}
912

10-
impl Index<usize> for Coor2D {
11-
type Output = f64;
12-
fn index(&self, i: usize) -> &Self::Output {
13-
&self.0[i]
13+
fn dim(&self) -> usize {
14+
2
15+
}
16+
17+
fn nth_unchecked(&self, n: usize) -> f64 {
18+
self.0[n]
1419
}
15-
}
1620

17-
impl IndexMut<usize> for Coor2D {
18-
fn index_mut(&mut self, i: usize) -> &mut Self::Output {
19-
&mut self.0[i]
21+
fn set_nth_unchecked(&mut self, n: usize, value: f64) {
22+
self.0[n] = value;
2023
}
2124
}
2225

2326
// ----- C O N S T R U C T O R S ---------------------------------------------
2427

2528
/// Constructors
2629
impl Coor2D {
27-
/// A `Coor2D` from latitude/longitude/height/time, with the angular input in degrees,
28-
/// and height and time ignored.
30+
/// A `Coor2D` from latitude/longitude in degrees
2931
#[must_use]
3032
pub fn geo(latitude: f64, longitude: f64) -> Coor2D {
3133
Coor2D([longitude.to_radians(), latitude.to_radians()])
3234
}
3335

34-
/// A `Coor2D` from longitude/latitude/height/time, with the angular input in seconds
35-
/// of arc. Mostly for handling grid shift elements.
36+
/// A `Coor2D` from longitude/latitude in seconds of arc.
37+
/// Mostly for handling grid shift elements.
3638
#[must_use]
3739
pub fn arcsec(longitude: f64, latitude: f64) -> Coor2D {
3840
Coor2D([
@@ -41,33 +43,27 @@ impl Coor2D {
4143
])
4244
}
4345

44-
/// A `Coor2D` from longitude/latitude/height/time, with the angular input in degrees.
45-
/// and height and time ignored.
46+
/// A `Coor2D` from longitude/latitude in degrees.
4647
#[must_use]
4748
pub fn gis(longitude: f64, latitude: f64) -> Coor2D {
4849
Coor2D([longitude.to_radians(), latitude.to_radians()])
4950
}
5051

51-
/// A `Coor2D` from longitude/latitude/height/time, with the angular input in radians,
52-
/// and third and fourth arguments ignored.
52+
/// A `Coor2D` from e.g. longitude/latitude in radians,
5353
#[must_use]
5454
pub fn raw(first: f64, second: f64) -> Coor2D {
5555
Coor2D([first, second])
5656
}
5757

58-
/// A `Coor2D` from latitude/longitude/height/time, with
59-
/// the angular input in the ISO-6709 DDDMM.mmmmm format,
60-
/// and height and time ignored.
58+
/// A `Coor2D` from latitude/longitude in the ISO-6709 DDDMM.mmmmm format.
6159
#[must_use]
6260
pub fn iso_dm(latitude: f64, longitude: f64) -> Coor2D {
6361
let longitude = angular::iso_dm_to_dd(longitude);
6462
let latitude = angular::iso_dm_to_dd(latitude);
6563
Coor2D([longitude.to_radians(), latitude.to_radians()])
6664
}
6765

68-
/// A `Coor2D` from latitude/longitude/height/time, with the
69-
/// angular input in the ISO-6709 DDDMMSS.sssss format,
70-
/// and height and time ignored.
66+
/// A `Coor2D` from latitude/longitude in the ISO-6709 DDDMMSS.sssss format.
7167
#[must_use]
7268
pub fn iso_dms(latitude: f64, longitude: f64) -> Coor2D {
7369
let longitude = angular::iso_dms_to_dd(longitude);

src/coordinate/coor32.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1-
use crate::math::angular;
2-
use std::ops::{Index, IndexMut};
1+
use super::*;
32

43
/// Generic 2D Coordinate tuple, with no fixed interpretation of the elements.
54
/// A tiny coordinate type: Just one fourth the weight of a [`Coor4D`](crate::Coor4D).
65
/// Probably only useful for small scale world maps, without too much zoom.
76
#[derive(Debug, Default, PartialEq, Copy, Clone)]
87
pub struct Coor32(pub [f32; 2]);
98

10-
// ----- O P E R A T O R T R A I T S -------------------------------------------------
9+
impl CoordinateTuple for Coor32 {
10+
fn new(fill: f64) -> Self {
11+
Coor32([fill as f32; 2])
12+
}
1113

12-
impl Index<usize> for Coor32 {
13-
type Output = f32;
14-
fn index(&self, i: usize) -> &Self::Output {
15-
&self.0[i]
14+
fn dim(&self) -> usize {
15+
2
16+
}
17+
18+
fn nth_unchecked(&self, n: usize) -> f64 {
19+
self.0[n] as f64
1620
}
17-
}
1821

19-
impl IndexMut<usize> for Coor32 {
20-
fn index_mut(&mut self, i: usize) -> &mut Self::Output {
21-
&mut self.0[i]
22+
fn set_nth_unchecked(&mut self, n: usize, value: f64) {
23+
self.0[n] = value as f32;
2224
}
2325
}
2426

src/coordinate/coor3d.rs

+22-18
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
1-
use crate::math::angular;
2-
use std::ops::{Add, Div, Index, IndexMut, Mul, Sub};
1+
use super::*;
2+
use std::ops::{Add, Div, Mul, Sub};
33

44
/// Generic 3D coordinate tuple, with no fixed interpretation of the elements
55
#[derive(Debug, Default, PartialEq, Copy, Clone)]
66
pub struct Coor3D(pub [f64; 3]);
77

8-
// ----- O P E R A T O R T R A I T S -------------------------------------------------
8+
impl CoordinateTuple for Coor3D {
9+
fn new(fill: f64) -> Self {
10+
Coor3D([fill; 3])
11+
}
912

10-
impl Index<usize> for Coor3D {
11-
type Output = f64;
12-
fn index(&self, i: usize) -> &Self::Output {
13-
&self.0[i]
13+
fn dim(&self) -> usize {
14+
3
1415
}
15-
}
1616

17-
impl IndexMut<usize> for Coor3D {
18-
fn index_mut(&mut self, i: usize) -> &mut Self::Output {
19-
&mut self.0[i]
17+
fn nth_unchecked(&self, n: usize) -> f64 {
18+
self.0[n]
19+
}
20+
21+
fn set_nth_unchecked(&mut self, n: usize, value: f64) {
22+
self.0[n] = value;
2023
}
2124
}
2225

26+
// ----- O P E R A T O R T R A I T S -------------------------------------------------
27+
2328
impl Add for Coor3D {
2429
type Output = Self;
2530
fn add(self, other: Self) -> Self {
@@ -79,13 +84,13 @@ impl Div for Coor3D {
7984

8085
/// Constructors
8186
impl Coor3D {
82-
/// A `Coor3D` from latitude/longitude/height/time, with the angular input in degrees
87+
/// A `Coor3D` from latitude/longitude/height, with the angular input in degrees
8388
#[must_use]
8489
pub fn geo(latitude: f64, longitude: f64, height: f64) -> Coor3D {
8590
Coor3D([longitude.to_radians(), latitude.to_radians(), height])
8691
}
8792

88-
/// A `Coor3D` from longitude/latitude/height/time, with the angular input in seconds
93+
/// A `Coor3D` from longitude/latitude/height, with the angular input in seconds
8994
/// of arc. Mostly for handling grid shift elements.
9095
#[must_use]
9196
pub fn arcsec(longitude: f64, latitude: f64, height: f64) -> Coor3D {
@@ -96,7 +101,7 @@ impl Coor3D {
96101
])
97102
}
98103

99-
/// A `Coor3D` from longitude/latitude/height/time, with the angular input in degrees
104+
/// A `Coor3D` from longitude/latitude/height, with the angular input in degrees
100105
#[must_use]
101106
pub fn gis(longitude: f64, latitude: f64, height: f64) -> Coor3D {
102107
Coor3D([longitude.to_radians(), latitude.to_radians(), height])
@@ -108,7 +113,7 @@ impl Coor3D {
108113
Coor3D([first, second, third])
109114
}
110115

111-
/// A `Coor3D` from latitude/longitude/height/time,
116+
/// A `Coor3D` from latitude/longitude/height,
112117
/// with the angular input in the ISO-6709 DDDMM.mmmmm format
113118
#[must_use]
114119
pub fn iso_dm(latitude: f64, longitude: f64, height: f64) -> Coor3D {
@@ -117,7 +122,7 @@ impl Coor3D {
117122
Coor3D([longitude.to_radians(), latitude.to_radians(), height])
118123
}
119124

120-
/// A `Coor3D` from latitude/longitude/height/time, with
125+
/// A `Coor3D` from latitude/longitude/height, with
121126
/// the angular input in the ISO-6709 DDDMMSS.sssss format
122127
#[must_use]
123128
pub fn iso_dms(latitude: f64, longitude: f64, height: f64) -> Coor3D {
@@ -129,7 +134,7 @@ impl Coor3D {
129134
/// A `Coor3D` consisting of 3 `NaN`s
130135
#[must_use]
131136
pub fn nan() -> Coor3D {
132-
Coor3D([f64::NAN, f64::NAN, f64::NAN])
137+
Coor3D::new(f64::NAN) //([f64::NAN, f64::NAN, f64::NAN])
133138
}
134139

135140
/// A `Coor3D` consisting of 3 `0`s
@@ -172,7 +177,6 @@ impl Coor3D {
172177
#[cfg(test)]
173178
mod tests {
174179
use super::*;
175-
use crate::prelude::*;
176180

177181
#[test]
178182
fn distances() {

src/coordinate/coor4d.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
1-
use crate::math::angular;
1+
use crate::coordinate::*;
22
use std::ops::{Add, Div, Mul, Sub};
33

44
/// Generic 4D coordinate tuple, with no fixed interpretation of the elements
55
#[derive(Debug, Default, PartialEq, Copy, Clone)]
66
pub struct Coor4D(pub [f64; 4]);
77

8-
// ----- O P E R A T O R T R A I T S -------------------------------------------------
8+
impl CoordinateTuple for Coor4D {
9+
fn new(fill: f64) -> Self {
10+
Coor4D([fill; 4])
11+
}
912

10-
use std::ops::{Index, IndexMut};
13+
fn dim(&self) -> usize {
14+
4
15+
}
1116

12-
impl Index<usize> for Coor4D {
13-
type Output = f64;
14-
fn index(&self, i: usize) -> &Self::Output {
15-
&self.0[i]
17+
fn nth_unchecked(&self, n: usize) -> f64 {
18+
self.0[n]
1619
}
17-
}
1820

19-
impl IndexMut<usize> for Coor4D {
20-
fn index_mut(&mut self, i: usize) -> &mut Self::Output {
21-
&mut self.0[i]
21+
fn set_nth_unchecked(&mut self, n: usize, value: f64) {
22+
self.0[n] = value;
2223
}
2324
}
2425

26+
// ----- O P E R A T O R T R A I T S -------------------------------------------------
27+
2528
impl Add for Coor4D {
2629
type Output = Self;
2730
fn add(self, other: Self) -> Self {
@@ -158,7 +161,6 @@ impl Coor4D {
158161
#[cfg(test)]
159162
mod tests {
160163
use super::*;
161-
use crate::prelude::*;
162164

163165
#[test]
164166
fn distances() {

0 commit comments

Comments
 (0)