Skip to content

Commit b2a1560

Browse files
Merge pull request #108 from busstoptaktik/work
Improve the coordinate tree
2 parents 76dd01c + 5095449 commit b2a1560

File tree

10 files changed

+708
-702
lines changed

10 files changed

+708
-702
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- CoordinateSet: xyz(), set_xyz(), xyzt(), set_xyzt() methods
13+
- Vector space operators (Add, Sub, Mul, Div) for all built
14+
in coordinate tuple types (Coor4D, Coor3D, Coor2D, Coor32)
1315

1416
### Changed
1517

18+
- `CoordinateTuple` trait now requires implementation of the constructor
19+
method `new(fill: f64)`, returning an object of `dim()` copies of `fill`.
20+
1621
### Removed
1722

1823
- 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

+17-70
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,38 @@
1-
use crate::math::angular;
2-
use std::ops::{Add, Div, Index, IndexMut, Mul, Sub};
1+
use super::*;
32

43
/// Generic 3D coordinate tuple, with no fixed interpretation of the elements
54
#[derive(Debug, Default, PartialEq, Copy, Clone)]
65
pub struct Coor3D(pub [f64; 3]);
76

8-
// ----- O P E R A T O R T R A I T S -------------------------------------------------
9-
10-
impl Index<usize> for Coor3D {
11-
type Output = f64;
12-
fn index(&self, i: usize) -> &Self::Output {
13-
&self.0[i]
14-
}
15-
}
16-
17-
impl IndexMut<usize> for Coor3D {
18-
fn index_mut(&mut self, i: usize) -> &mut Self::Output {
19-
&mut self.0[i]
7+
impl CoordinateTuple for Coor3D {
8+
fn new(fill: f64) -> Self {
9+
Coor3D([fill; 3])
2010
}
21-
}
2211

23-
impl Add for Coor3D {
24-
type Output = Self;
25-
fn add(self, other: Self) -> Self {
26-
Coor3D([
27-
self.0[0] + other.0[0],
28-
self.0[1] + other.0[1],
29-
self.0[2] + other.0[2],
30-
])
12+
fn dim(&self) -> usize {
13+
3
3114
}
32-
}
3315

34-
impl Add<&Coor3D> for Coor3D {
35-
type Output = Self;
36-
fn add(self, other: &Self) -> Self {
37-
Coor3D([
38-
self.0[0] + other.0[0],
39-
self.0[1] + other.0[1],
40-
self.0[2] + other.0[2],
41-
])
16+
fn nth_unchecked(&self, n: usize) -> f64 {
17+
self.0[n]
4218
}
43-
}
4419

45-
impl Sub for Coor3D {
46-
type Output = Self;
47-
fn sub(self, other: Self) -> Self {
48-
Coor3D([
49-
self.0[0] - other.0[0],
50-
self.0[1] - other.0[1],
51-
self.0[2] - other.0[2],
52-
])
53-
}
54-
}
55-
56-
impl Mul for Coor3D {
57-
type Output = Self;
58-
fn mul(self, other: Self) -> Self {
59-
Coor3D([
60-
self.0[0] * other.0[0],
61-
self.0[1] * other.0[1],
62-
self.0[2] * other.0[2],
63-
])
64-
}
65-
}
66-
67-
impl Div for Coor3D {
68-
type Output = Self;
69-
fn div(self, other: Self) -> Self {
70-
Coor3D([
71-
self.0[0] / other.0[0],
72-
self.0[1] / other.0[1],
73-
self.0[2] / other.0[2],
74-
])
20+
fn set_nth_unchecked(&mut self, n: usize, value: f64) {
21+
self.0[n] = value;
7522
}
7623
}
7724

7825
// ----- C O N S T R U C T O R S ---------------------------------------------
7926

8027
/// Constructors
8128
impl Coor3D {
82-
/// A `Coor3D` from latitude/longitude/height/time, with the angular input in degrees
29+
/// A `Coor3D` from latitude/longitude/height, with the angular input in degrees
8330
#[must_use]
8431
pub fn geo(latitude: f64, longitude: f64, height: f64) -> Coor3D {
8532
Coor3D([longitude.to_radians(), latitude.to_radians(), height])
8633
}
8734

88-
/// A `Coor3D` from longitude/latitude/height/time, with the angular input in seconds
35+
/// A `Coor3D` from longitude/latitude/height, with the angular input in seconds
8936
/// of arc. Mostly for handling grid shift elements.
9037
#[must_use]
9138
pub fn arcsec(longitude: f64, latitude: f64, height: f64) -> Coor3D {
@@ -96,7 +43,7 @@ impl Coor3D {
9643
])
9744
}
9845

99-
/// A `Coor3D` from longitude/latitude/height/time, with the angular input in degrees
46+
/// A `Coor3D` from longitude/latitude/height, with the angular input in degrees
10047
#[must_use]
10148
pub fn gis(longitude: f64, latitude: f64, height: f64) -> Coor3D {
10249
Coor3D([longitude.to_radians(), latitude.to_radians(), height])
@@ -108,7 +55,7 @@ impl Coor3D {
10855
Coor3D([first, second, third])
10956
}
11057

111-
/// A `Coor3D` from latitude/longitude/height/time,
58+
/// A `Coor3D` from latitude/longitude/height,
11259
/// with the angular input in the ISO-6709 DDDMM.mmmmm format
11360
#[must_use]
11461
pub fn iso_dm(latitude: f64, longitude: f64, height: f64) -> Coor3D {
@@ -117,7 +64,7 @@ impl Coor3D {
11764
Coor3D([longitude.to_radians(), latitude.to_radians(), height])
11865
}
11966

120-
/// A `Coor3D` from latitude/longitude/height/time, with
67+
/// A `Coor3D` from latitude/longitude/height, with
12168
/// the angular input in the ISO-6709 DDDMMSS.sssss format
12269
#[must_use]
12370
pub fn iso_dms(latitude: f64, longitude: f64, height: f64) -> Coor3D {
@@ -129,7 +76,7 @@ impl Coor3D {
12976
/// A `Coor3D` consisting of 3 `NaN`s
13077
#[must_use]
13178
pub fn nan() -> Coor3D {
132-
Coor3D([f64::NAN, f64::NAN, f64::NAN])
79+
Coor3D::new(f64::NAN) //([f64::NAN, f64::NAN, f64::NAN])
13380
}
13481

13582
/// A `Coor3D` consisting of 3 `0`s
@@ -172,7 +119,7 @@ impl Coor3D {
172119
#[cfg(test)]
173120
mod tests {
174121
use super::*;
175-
use crate::prelude::*;
122+
use std::ops::{Add, Div, Mul};
176123

177124
#[test]
178125
fn distances() {

src/coordinate/coor4d.rs

+11-71
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,24 @@
1-
use crate::math::angular;
2-
use std::ops::{Add, Div, Mul, Sub};
1+
use crate::coordinate::*;
32

43
/// Generic 4D coordinate tuple, with no fixed interpretation of the elements
54
#[derive(Debug, Default, PartialEq, Copy, Clone)]
65
pub struct Coor4D(pub [f64; 4]);
76

8-
// ----- O P E R A T O R T R A I T S -------------------------------------------------
9-
10-
use std::ops::{Index, IndexMut};
11-
12-
impl Index<usize> for Coor4D {
13-
type Output = f64;
14-
fn index(&self, i: usize) -> &Self::Output {
15-
&self.0[i]
16-
}
17-
}
18-
19-
impl IndexMut<usize> for Coor4D {
20-
fn index_mut(&mut self, i: usize) -> &mut Self::Output {
21-
&mut self.0[i]
22-
}
23-
}
24-
25-
impl Add for Coor4D {
26-
type Output = Self;
27-
fn add(self, other: Self) -> Self {
28-
Coor4D([
29-
self.0[0] + other.0[0],
30-
self.0[1] + other.0[1],
31-
self.0[2] + other.0[2],
32-
self.0[3] + other.0[3],
33-
])
34-
}
35-
}
36-
37-
impl Add<&Coor4D> for Coor4D {
38-
type Output = Self;
39-
fn add(self, other: &Self) -> Self {
40-
Coor4D([
41-
self.0[0] + other.0[0],
42-
self.0[1] + other.0[1],
43-
self.0[2] + other.0[2],
44-
self.0[3] + other.0[3],
45-
])
7+
impl CoordinateTuple for Coor4D {
8+
fn new(fill: f64) -> Self {
9+
Coor4D([fill; 4])
4610
}
47-
}
4811

49-
impl Sub for Coor4D {
50-
type Output = Self;
51-
fn sub(self, other: Self) -> Self {
52-
Coor4D([
53-
self.0[0] - other.0[0],
54-
self.0[1] - other.0[1],
55-
self.0[2] - other.0[2],
56-
self.0[3] - other.0[3],
57-
])
12+
fn dim(&self) -> usize {
13+
4
5814
}
59-
}
6015

61-
impl Mul for Coor4D {
62-
type Output = Self;
63-
fn mul(self, other: Self) -> Self {
64-
Coor4D([
65-
self.0[0] * other.0[0],
66-
self.0[1] * other.0[1],
67-
self.0[2] * other.0[2],
68-
self.0[3] * other.0[3],
69-
])
16+
fn nth_unchecked(&self, n: usize) -> f64 {
17+
self.0[n]
7018
}
71-
}
7219

73-
impl Div for Coor4D {
74-
type Output = Self;
75-
fn div(self, other: Self) -> Self {
76-
Coor4D([
77-
self.0[0] / other.0[0],
78-
self.0[1] / other.0[1],
79-
self.0[2] / other.0[2],
80-
self.0[3] / other.0[3],
81-
])
20+
fn set_nth_unchecked(&mut self, n: usize, value: f64) {
21+
self.0[n] = value;
8222
}
8323
}
8424

@@ -158,7 +98,7 @@ impl Coor4D {
15898
#[cfg(test)]
15999
mod tests {
160100
use super::*;
161-
use crate::prelude::*;
101+
use std::ops::{Add, Div, Mul};
162102

163103
#[test]
164104
fn distances() {

0 commit comments

Comments
 (0)