Skip to content

Commit 7401ca7

Browse files
committed
Extend and use CoordinateTuple trait. dim in CoordinateSet
1 parent 704c202 commit 7401ca7

16 files changed

+350
-541
lines changed

examples/00-transformations.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ fn main() -> anyhow::Result<()> {
3737
// But since a coordinate tuple is really just an array of double
3838
// precision numbers, you may also generate it directly using plain
3939
// Rust syntax. Note that Coor2D, like f64, provides the to_radians
40-
// method. So compared to cph_raw above, we can use a slightly more
41-
// compact notation.
40+
// method, but it operates in place, so we need two steps in this case.
4241
let cph_direct = Coor2D([12., 55.]).to_radians();
4342
// The three versions of Copenhagen coordinates should be identical.
4443
assert_eq!(cph, cph_raw);
@@ -79,8 +78,8 @@ fn main() -> anyhow::Result<()> {
7978
// Note the use of `to_geo`, which transforms lon/lat in radians
8079
// to lat/lon in degrees. It is defined for Coor2D as well as for
8180
// arrays, vectors and slices of Coor2D
82-
for coord in data.to_geo() {
83-
println!(" {:?}", coord);
81+
for coord in data {
82+
println!(" {:?}", coord.to_geo());
8483
}
8584

8685
// To geo again, but using slices - in two different ways

examples/02-user_defined_macros.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,9 @@ fn main() -> anyhow::Result<()> {
4646

4747
// data.to_geo() transforms all elements in data from the internal GIS
4848
// format (lon/lat in radians) to lat/lon in degrees.
49-
data.to_geo();
5049
println!("Back to ed50:");
5150
for coord in data {
52-
println!(" {:?}", coord);
51+
println!(" {:?}", coord.to_geo());
5352
}
5453

5554
Ok(())

examples/06-user_defined_coordinate_types_and_containers.rs

+3
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ impl CoordinateSet for AbscissaCollection {
7575
fn len(&self) -> usize {
7676
4
7777
}
78+
fn dim(&self) -> usize {
79+
1
80+
}
7881
}
7982

8083
fn main() -> Result<(), anyhow::Error> {

examples/07-examples_from_ruminations.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,8 @@ fn rumination_000() -> Result<(), anyhow::Error> {
6161

6262
// [6] And go back, i.e. utm -> geo
6363
ctx.apply(utm32, Inv, &mut data)?;
64-
data.to_geo();
6564
for coord in data {
66-
println!("{:?}", coord);
65+
println!("{:?}", coord.to_geo());
6766
}
6867

6968
Ok(())

src/context/minimal.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,16 @@ mod tests {
143143
assert_eq!(steps[2], "addone inv");
144144

145145
let mut data = some_basic_coor2dinates();
146-
assert_eq!(data[0][0], 55.);
147-
assert_eq!(data[1][0], 59.);
146+
assert_eq!(data[0].x(), 55.);
147+
assert_eq!(data[1].x(), 59.);
148148

149149
assert_eq!(2, ctx.apply(op, Fwd, &mut data)?);
150-
assert_eq!(data[0][0], 56.);
151-
assert_eq!(data[1][0], 60.);
150+
assert_eq!(data[0].x(), 56.);
151+
assert_eq!(data[1].x(), 60.);
152152

153153
ctx.apply(op, Inv, &mut data)?;
154-
assert_eq!(data[0][0], 55.);
155-
assert_eq!(data[1][0], 59.);
154+
assert_eq!(data[0].x(), 55.);
155+
assert_eq!(data[1].x(), 59.);
156156

157157
let params = ctx.params(op, 1)?;
158158
let ellps = params.ellps(0);
@@ -168,8 +168,8 @@ mod tests {
168168
let op = ctx.op("geo:in | utm zone=32 | neu:out")?;
169169

170170
let mut data = some_basic_coor2dinates();
171-
assert_eq!(data[0][0], 55.);
172-
assert_eq!(data[1][0], 59.);
171+
assert_eq!(data[0].x(), 55.);
172+
assert_eq!(data[1].x(), 59.);
173173

174174
ctx.apply(op, Fwd, &mut data)?;
175175
let expected = [6098907.825005002, 691875.6321396609];

src/context/plain.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -312,16 +312,16 @@ mod tests {
312312

313313
// ...and it works as expected?
314314
let mut data = some_basic_coor2dinates();
315-
assert_eq!(data[0][0], 55.);
316-
assert_eq!(data[1][0], 59.);
315+
assert_eq!(data[0].x(), 55.);
316+
assert_eq!(data[1].x(), 59.);
317317

318318
ctx.apply(op, Fwd, &mut data)?;
319-
assert_eq!(data[0][0], 56.);
320-
assert_eq!(data[1][0], 60.);
319+
assert_eq!(data[0].x(), 56.);
320+
assert_eq!(data[1].x(), 60.);
321321

322322
ctx.apply(op, Inv, &mut data)?;
323-
assert_eq!(data[0][0], 55.);
324-
assert_eq!(data[1][0], 59.);
323+
assert_eq!(data[0].x(), 55.);
324+
assert_eq!(data[1].x(), 59.);
325325

326326
// Now test that the look-up functionality works in general
327327

@@ -344,8 +344,8 @@ mod tests {
344344
let mut data = some_basic_coor2dinates();
345345

346346
ctx.apply(op, Fwd, &mut data)?;
347-
assert_eq!(data[0][0], 57.);
348-
assert_eq!(data[1][0], 61.);
347+
assert_eq!(data[0].x(), 57.);
348+
assert_eq!(data[1].x(), 61.);
349349

350350
// 3 Console tests from stupid.md
351351
let op = ctx.op("stupid:bad");
@@ -354,14 +354,14 @@ mod tests {
354354
let op = ctx.op("stupid:addthree")?;
355355
let mut data = some_basic_coor2dinates();
356356
ctx.apply(op, Fwd, &mut data)?;
357-
assert_eq!(data[0][0], 58.);
358-
assert_eq!(data[1][0], 62.);
357+
assert_eq!(data[0].x(), 58.);
358+
assert_eq!(data[1].x(), 62.);
359359

360360
let op = ctx.op("stupid:addthree_one_by_one")?;
361361
let mut data = some_basic_coor2dinates();
362362
ctx.apply(op, Fwd, &mut data)?;
363-
assert_eq!(data[0][0], 58.);
364-
assert_eq!(data[1][0], 62.);
363+
assert_eq!(data[0].x(), 58.);
364+
assert_eq!(data[1].x(), 62.);
365365

366366
// Make sure we can access "sigil-less runtime defined resources"
367367
ctx.register_resource("foo", "bar");

src/coordinate/coor2d.rs

+7-77
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use super::*;
22
use crate::math::angular;
3-
use std::ops::{Index, IndexMut};
43

54
/// Generic 2D Coordinate tuple, with no fixed interpretation of the elements
65
#[derive(Debug, Default, PartialEq, Copy, Clone)]
76
pub struct Coor2D(pub [f64; 2]);
87

9-
// ----- O P E R A T O R T R A I T S -------------------------------------------------
8+
use std::ops::{Index, IndexMut};
109

1110
impl Index<usize> for Coor2D {
1211
type Output = f64;
@@ -21,34 +20,6 @@ impl IndexMut<usize> for Coor2D {
2120
}
2221
}
2322

24-
// ----- A N G U L A R U N I T S -------------------------------------------
25-
26-
impl AngularUnits for Coor2D {
27-
/// Transform the elements of a `Coor2D` from degrees to radians
28-
#[must_use]
29-
fn to_radians(self) -> Self {
30-
Coor2D([self[0].to_radians(), self[1].to_radians()])
31-
}
32-
33-
/// Transform the elements of a `Coor2D` from radians to degrees
34-
#[must_use]
35-
fn to_degrees(self) -> Self {
36-
Coor2D([self[0].to_degrees(), self[1].to_degrees()])
37-
}
38-
39-
/// Transform the elements of a `Coor2D` from radians to seconds of arc.
40-
#[must_use]
41-
fn to_arcsec(self) -> Self {
42-
Coor2D([self[0].to_degrees() * 3600., self[1].to_degrees() * 3600.])
43-
}
44-
45-
/// Transform the internal lon/lat-in-radians to lat/lon-in-degrees
46-
#[must_use]
47-
fn to_geo(self) -> Self {
48-
Coor2D([self[1].to_degrees(), self[0].to_degrees()])
49-
}
50-
}
51-
5223
// ----- C O N S T R U C T O R S ---------------------------------------------
5324

5425
/// Constructors
@@ -127,53 +98,13 @@ impl Coor2D {
12798
/// Multiply by a scalar
12899
#[must_use]
129100
pub fn scale(&self, factor: f64) -> Coor2D {
130-
Coor2D([self[0] * factor, self[1] * factor])
101+
Coor2D([self.x() * factor, self.y() * factor])
131102
}
132103

133104
/// Scalar product
134105
#[must_use]
135106
pub fn dot(&self, other: Coor2D) -> f64 {
136-
self[0] * other[0] + self[1] * other[1]
137-
}
138-
}
139-
140-
// ----- D I S T A N C E S ---------------------------------------------------
141-
142-
impl Coor2D {
143-
/// Euclidean distance between two points in the 2D plane.
144-
///
145-
/// Primarily used to compute the distance between two projected points
146-
/// in their projected plane. Typically, this distance will differ from
147-
/// the actual distance in the real world.
148-
///
149-
/// # See also:
150-
///
151-
/// [`distance`](crate::ellipsoid::Ellipsoid::distance)
152-
///
153-
/// # Examples
154-
///
155-
/// ```
156-
/// use geodesy::prelude::*;
157-
/// let t = 1000 as f64;
158-
/// let p0 = Coor2D::origin();
159-
/// let p1 = Coor2D::raw(t, t);
160-
/// assert_eq!(p0.hypot2(&p1), t.hypot(t));
161-
/// ```
162-
#[must_use]
163-
pub fn hypot2(&self, other: &Self) -> f64 {
164-
(self[0] - other[0]).hypot(self[1] - other[1])
165-
}
166-
}
167-
168-
impl From<Coor2D> for Coor4D {
169-
fn from(c: Coor2D) -> Self {
170-
Coor4D([c[0], c[1], 0.0, 0.0])
171-
}
172-
}
173-
174-
impl From<Coor4D> for Coor2D {
175-
fn from(xyzt: Coor4D) -> Self {
176-
Coor2D([xyzt[0], xyzt[1]])
107+
self.x() * other.x() + self.y() * other.y()
177108
}
178109
}
179110

@@ -197,16 +128,15 @@ mod tests {
197128
let c = Coor2D::raw(12., 55.).to_radians();
198129
let d = Coor2D::gis(12., 55.);
199130
assert_eq!(c, d);
200-
assert_eq!(d[0], 12f64.to_radians());
201-
let e = d.to_degrees();
202-
assert_eq!(e[0], c.to_degrees()[0]);
131+
assert_eq!(d.x(), 12f64.to_radians());
132+
assert_eq!(d.x().to_degrees(), c.x().to_degrees());
203133
}
204134

205135
#[test]
206136
fn array() {
207137
let b = Coor2D::raw(7., 8.);
208-
let c = [b[0], b[1], f64::NAN, f64::NAN];
209-
assert_eq!(b[0], c[0]);
138+
let c = [b.x(), b.y(), f64::NAN, f64::NAN];
139+
assert_eq!(b.x(), c[0]);
210140
}
211141

212142
#[test]

src/coordinate/coor32.rs

+2-70
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/// Tiny coordinate type: 2D, 32 bits, only one fourth the weight of a Coord.
22
/// Probably only useful for small scale world maps, without too much zoom.
3-
use super::*;
43
use crate::math::angular;
54
use std::ops::{Index, IndexMut};
65

@@ -23,34 +22,6 @@ impl IndexMut<usize> for Coor32 {
2322
}
2423
}
2524

26-
// ----- A N G U L A R U N I T S -------------------------------------------
27-
28-
impl AngularUnits for Coor32 {
29-
/// Transform the first two elements of a `Coor32` from degrees to radians
30-
#[must_use]
31-
fn to_radians(self) -> Self {
32-
Coor32([self[0].to_radians(), self[1].to_radians()])
33-
}
34-
35-
/// Transform the elements of a `Coor32` from radians to degrees
36-
#[must_use]
37-
fn to_degrees(self) -> Self {
38-
Coor32([self[0].to_degrees(), self[1].to_degrees()])
39-
}
40-
41-
/// Transform the elements of a `Coor32` from radians to seconds of arc.
42-
#[must_use]
43-
fn to_arcsec(self) -> Self {
44-
Coor32([self[0].to_degrees() * 3600., self[1].to_degrees() * 3600.])
45-
}
46-
47-
/// Transform the internal lon/lat-in-radians to lat/lon-in-degrees
48-
#[must_use]
49-
fn to_geo(self) -> Self {
50-
Coor32([self[1].to_degrees(), self[0].to_degrees()])
51-
}
52-
}
53-
5425
// ----- C O N S T R U C T O R S ---------------------------------------------
5526

5627
/// Constructors
@@ -139,51 +110,12 @@ impl Coor32 {
139110
}
140111
}
141112

142-
impl From<Coor32> for Coor4D {
143-
fn from(c: Coor32) -> Self {
144-
Coor4D([c[0] as f64, c[1] as f64, 0.0, f64::NAN])
145-
}
146-
}
147-
148-
impl From<Coor4D> for Coor32 {
149-
fn from(xyzt: Coor4D) -> Self {
150-
Coor32::raw(xyzt[0], xyzt[1])
151-
}
152-
}
153-
154-
// ----- D I S T A N C E S ---------------------------------------------------
155-
156-
impl Coor32 {
157-
/// Euclidean distance between two points in the 2D plane.
158-
///
159-
/// Primarily used to compute the distance between two projected points
160-
/// in their projected plane. Typically, this distance will differ from
161-
/// the actual distance in the real world.
162-
///
163-
/// # See also:
164-
///
165-
/// [`distance`](crate::ellipsoid::Ellipsoid::distance)
166-
///
167-
/// # Examples
168-
///
169-
/// ```
170-
/// use geodesy::prelude::*;
171-
/// let t = 1000.;
172-
/// let p0 = Coor32::origin();
173-
/// let p1 = Coor32::raw(t, t);
174-
/// assert_eq!(p0.hypot2(&p1), t.hypot(t));
175-
/// ```
176-
#[must_use]
177-
pub fn hypot2(&self, other: &Self) -> f64 {
178-
(self[0] as f64 - other[0] as f64).hypot(self[1] as f64 - other[1] as f64)
179-
}
180-
}
181-
182113
// ----- T E S T S ---------------------------------------------------
183114

184115
#[cfg(test)]
185116
mod tests {
186-
use super::*;
117+
use crate::prelude::*;
118+
187119
#[test]
188120
fn distances() {
189121
let lat = angular::dms_to_dd(55, 30, 36.);

0 commit comments

Comments
 (0)