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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
use std::fmt::{self, Debug, Formatter};
use std::mem::MaybeUninit;
use std::ops::Deref;
use std::os::raw::c_int;
use std::str::FromStr;

use geo_types::{Line, LineString, MultiLineString};
#[cfg(feature = "use-serde")]
use serde::{Deserialize, Serialize};

use h3ron_h3_sys::H3Index;

use crate::index::{index_from_str, Index};
use crate::iter::CellBoundaryIter;
use crate::to_geo::{ToLine, ToLineString, ToMultiLineString};
use crate::{Error, FromH3Index, H3Cell, ToCoordinate};

/// H3 Index representing an directed H3 edge
#[derive(PartialOrd, PartialEq, Clone, Hash, Eq, Ord, Copy)]
#[cfg_attr(feature = "use-serde", derive(Serialize, Deserialize))]
#[repr(transparent)]
pub struct H3DirectedEdge(H3Index);

impl Debug for H3DirectedEdge {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "H3DirectedEdge({})", self.to_string())
    }
}

/// convert to index including validation
impl TryFrom<u64> for H3DirectedEdge {
    type Error = Error;

    fn try_from(h3index: H3Index) -> Result<Self, Self::Error> {
        let index = Self::new(h3index);
        index.validate()?;
        Ok(index)
    }
}

impl H3DirectedEdge {
    /// Gets the unidirectional edge from `origin_cell` to `destination_cell`
    pub fn from_cells(origin_cell: H3Cell, destination_cell: H3Cell) -> Result<Self, Error> {
        origin_cell.directed_edge_to(destination_cell)
    }

    pub fn is_edge_valid(&self) -> bool {
        self.validate().is_ok()
    }

    /// Gets the average length of an edge in kilometers at `resolution`.
    /// This is the length of the cell boundary segment represented by the edge.
    pub fn edge_length_avg_km(resolution: u8) -> Result<f64, Error> {
        let mut edge_length: f64 = 0.0;
        Error::check_returncode(unsafe {
            h3ron_h3_sys::getHexagonEdgeLengthAvgKm(c_int::from(resolution), &mut edge_length)
        })
        .map(|_| edge_length)
    }

    /// Gets the average length of an edge in meters at `resolution`.
    /// This is the length of the cell boundary segment represented by the edge.
    pub fn edge_length_avg_m(resolution: u8) -> Result<f64, Error> {
        let mut edge_length: f64 = 0.0;
        Error::check_returncode(unsafe {
            h3ron_h3_sys::getHexagonEdgeLengthAvgM(c_int::from(resolution), &mut edge_length)
        })
        .map(|_| edge_length)
    }

    /// The approximate distance between the centroids of two neighboring cells
    /// at the given `resolution`.
    ///
    /// Based on the approximate edge length. See [`H3DirectedEdge::cell_centroid_distance_m`] for a
    /// more exact variant of this function.
    pub fn cell_centroid_distance_avg_m_at_resolution(resolution: u8) -> Result<f64, Error> {
        Self::edge_length_avg_m(resolution).map(cell_centroid_distance_m_by_edge_length)
    }

    /// The approximate distance between the centroids of two neighboring cells
    /// at the given `resolution`.
    ///
    /// Based on the exact edge length. See [`H3DirectedEdge::cell_centroid_distance_avg_m_at_resolution`]
    /// for a resolution based variant.
    pub fn cell_centroid_distance_m(&self) -> Result<f64, Error> {
        self.length_m().map(cell_centroid_distance_m_by_edge_length)
    }

    /// Retrieves the destination H3 Cell of `self`
    ///
    /// # Returns
    /// If the built index is invalid, returns an Error.
    pub fn destination_cell(&self) -> Result<H3Cell, Error> {
        let mut cell_h3index: H3Index = 0;
        Error::check_returncode(unsafe {
            h3ron_h3_sys::getDirectedEdgeDestination(self.h3index(), &mut cell_h3index)
        })
        .map(|_| H3Cell::new(cell_h3index))
    }

    /// Retrieves the origin H3 Cell of `self`
    ///
    /// # Returns
    /// If the built index is invalid, returns an Error.
    pub fn origin_cell(&self) -> Result<H3Cell, Error> {
        let mut cell_h3index: H3Index = 0;
        Error::check_returncode(unsafe {
            h3ron_h3_sys::getDirectedEdgeOrigin(self.h3index(), &mut cell_h3index)
        })
        .map(|_| H3Cell::new(cell_h3index))
    }

    /// Retrieves a `H3EdgeCells` of the origin and destination cell of the
    /// edge.
    ///
    /// # Returns
    /// If the built indexes are invalid, returns an Error.
    pub fn cells(&self) -> Result<H3EdgeCells, Error> {
        let mut out: [H3Index; 2] = [0, 0];
        Error::check_returncode(unsafe {
            h3ron_h3_sys::directedEdgeToCells(self.h3index(), out.as_mut_ptr())
        })?;
        let res = H3EdgeCells {
            origin: H3Cell::new(out[0]),
            destination: H3Cell::new(out[1]),
        };
        Ok(res)
    }

    /// Retrieves the corresponding edge in the reversed direction.
    ///
    /// # Returns
    /// If the built edge is invalid, returns an Error.
    pub fn reversed(&self) -> Result<Self, Error> {
        let edge_cells = self.cells()?;
        edge_cells.destination.directed_edge_to(edge_cells.origin)
    }

    /// Retrieves the [`LineString`] which forms the boundary between
    /// two cells.
    pub fn boundary_linestring(&self) -> Result<LineString<f64>, Error> {
        let cb = unsafe {
            let mut mu = MaybeUninit::<h3ron_h3_sys::CellBoundary>::uninit();
            Error::check_returncode(h3ron_h3_sys::directedEdgeToBoundary(
                self.0,
                mu.as_mut_ptr(),
            ))?;
            mu.assume_init()
        };
        Ok(CellBoundaryIter::new(&cb, false).collect())
    }

    /// Retrieves the exact length of `self` in meters
    /// This is the length of the cell boundary segment represented by the edge.
    pub fn length_m(&self) -> Result<f64, Error> {
        let mut length: f64 = 0.0;
        Error::check_returncode(unsafe { h3ron_h3_sys::edgeLengthM(self.h3index(), &mut length) })
            .map(|_| length)
    }

    /// Retrieves the exact length of `self` in kilometers
    /// This is the length of the cell boundary segment represented by the edge.
    pub fn length_km(&self) -> Result<f64, Error> {
        let mut length: f64 = 0.0;
        Error::check_returncode(unsafe { h3ron_h3_sys::edgeLengthKm(self.h3index(), &mut length) })
            .map(|_| length)
    }

    /// Retrieves the exact length of `self` in radians
    /// This is the length of the cell boundary segment represented by the edge.
    pub fn length_rads(&self) -> Result<f64, Error> {
        let mut length: f64 = 0.0;
        Error::check_returncode(unsafe {
            h3ron_h3_sys::edgeLengthRads(self.h3index(), &mut length)
        })
        .map(|_| length)
    }
}

impl FromH3Index for H3DirectedEdge {
    fn from_h3index(h3index: H3Index) -> Self {
        Self::new(h3index)
    }
}

impl Index for H3DirectedEdge {
    fn h3index(&self) -> H3Index {
        self.0
    }

    fn new(h3index: H3Index) -> Self {
        Self(h3index)
    }

    fn validate(&self) -> Result<(), Error> {
        if unsafe { h3ron_h3_sys::isValidDirectedEdge(self.h3index()) == 0 } {
            Err(Error::DirectedEdgeInvalid)
        } else {
            Ok(())
        }
    }
}

impl ToString for H3DirectedEdge {
    fn to_string(&self) -> String {
        format!("{:x}", self.0)
    }
}

impl FromStr for H3DirectedEdge {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        index_from_str(s)
    }
}

impl ToLine for H3DirectedEdge {
    type Error = Error;

    /// Create a line geometry from the origin index to the destination index
    ///
    /// ```
    /// use h3ron::{H3DirectedEdge, Index};
    /// use h3ron::to_geo::{ToLine, ToCoordinate};
    ///
    /// let edge = H3DirectedEdge::new(0x149283080ddbffff);
    /// let l = edge.to_line().unwrap();
    /// assert_eq!(l.start, edge.origin_cell().unwrap().to_coordinate().unwrap());
    /// assert_eq!(l.end, edge.destination_cell().unwrap().to_coordinate().unwrap());
    /// ```
    fn to_line(&self) -> Result<Line<f64>, Self::Error> {
        let edge_cells = self.cells()?;
        Ok(Line::new(
            edge_cells.origin.to_coordinate()?,
            edge_cells.destination.to_coordinate()?,
        ))
    }
}

impl ToLineString for H3DirectedEdge {
    type Error = Error;

    /// Create a linestring from the origin index to the destination index
    ///
    /// ```
    /// use h3ron::{H3DirectedEdge, Index};
    /// use h3ron::to_geo::{ToLineString, ToCoordinate};
    ///
    /// let edge = H3DirectedEdge::new(0x149283080ddbffff);
    /// let ls = edge.to_linestring().unwrap();
    /// assert_eq!(ls.0.len(), 2);
    /// assert_eq!(ls.0[0], edge.origin_cell().unwrap().to_coordinate().unwrap());
    /// assert_eq!(ls.0[1], edge.destination_cell().unwrap().to_coordinate().unwrap());
    /// ```
    fn to_linestring(&self) -> Result<LineString<f64>, Self::Error> {
        Ok(self.to_line()?.into())
    }
}

/// converts `&[H3Edge]` slices to `MultiLineString` while attempting
/// to combine consequent `H3Edge` values into a single `LineString<f64>`
impl ToMultiLineString for &[H3DirectedEdge] {
    type Error = Error;

    fn to_multilinestring(&self) -> Result<MultiLineString<f64>, Self::Error> {
        let cell_tuples = self
            .iter()
            .map(|edge| match (edge.origin_cell(), edge.destination_cell()) {
                (Ok(origin_cell), Ok(destination_cell)) => Ok((origin_cell, destination_cell)),
                (Err(e), _) | (_, Err(e)) => Err(e),
            })
            .collect::<Result<Vec<_>, _>>()?;
        celltuples_to_multlinestring(cell_tuples)
    }
}

impl Deref for H3DirectedEdge {
    type Target = H3Index;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

/// avoid repeated calculations by using this constant of the
/// result of `3.0_f64.sqrt()`.
const F64_SQRT_3: f64 = 1.7320508075688772_f64;

/// the height of two equilateral triangles with a shared side calculated using
/// the `edge_length`.
///     .
///    /_\
///    \ /
///     `
///
/// For one triangle:  `h = (edge_length / 2.0) * 3.0.sqrt()`
#[inline(always)]
fn cell_centroid_distance_m_by_edge_length(edge_length: f64) -> f64 {
    edge_length * F64_SQRT_3
}

/// convert an iterator of subsequent H3Cell-tuples `(origin_cell, destination_cell)` generated
/// from `H3DirectedEdge` values to a multilinestring
fn celltuples_to_multlinestring<I>(iter: I) -> Result<MultiLineString<f64>, Error>
where
    I: IntoIterator<Item = (H3Cell, H3Cell)>,
{
    let mut linestrings = vec![];
    let mut last_destination_cell: Option<H3Cell> = None;
    let mut coordinates = vec![];
    for (origin_cell, destination_cell) in iter {
        if coordinates.is_empty() {
            coordinates.push(origin_cell.to_coordinate()?);
        } else if last_destination_cell != Some(origin_cell) {
            // create a new linestring
            linestrings.push(LineString::from(std::mem::take(&mut coordinates)));
            coordinates.push(origin_cell.to_coordinate()?);
        }
        coordinates.push(destination_cell.to_coordinate()?);
        last_destination_cell = Some(destination_cell);
    }
    if !coordinates.is_empty() {
        linestrings.push(LineString::from(coordinates));
    }
    Ok(MultiLineString(linestrings))
}

impl ToMultiLineString for Vec<H3DirectedEdge> {
    type Error = Error;

    fn to_multilinestring(&self) -> Result<MultiLineString<f64>, Self::Error> {
        self.as_slice().to_multilinestring()
    }
}

pub struct H3EdgeCells {
    /// origin cell of the edge
    pub origin: H3Cell,

    /// destination cell of the edge
    pub destination: H3Cell,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[should_panic(expected = "DirectedEdgeInvalid")]
    #[test]
    fn checks_both_validity() {
        let edge = H3DirectedEdge::new(0x149283080ddbffff);
        assert!(edge.validate().is_ok());
        let edge = H3DirectedEdge::new(0x89283080ddbffff_u64);
        edge.validate().unwrap();
    }

    #[test]
    fn debug_hexadecimal() {
        let edge = H3DirectedEdge::new(0x149283080ddbffff);
        assert_eq!(
            format!("{:?}", edge),
            "H3DirectedEdge(149283080ddbffff)".to_string()
        );
    }

    #[test]
    fn reversed() {
        let edge = H3DirectedEdge::new(0x149283080ddbffff);
        let rev_edge = edge.reversed().unwrap();
        assert_ne!(edge, rev_edge);
        assert_eq!(
            edge.origin_cell().unwrap(),
            rev_edge.destination_cell().unwrap()
        );
        assert_eq!(
            edge.destination_cell().unwrap(),
            rev_edge.origin_cell().unwrap()
        );
    }

    #[test]
    fn boundary_linestring() {
        let edge = H3DirectedEdge::new(0x149283080ddbffff);
        let boundary_ls = edge.boundary_linestring().unwrap();
        assert_eq!(boundary_ls.0.len(), 2);
        dbg!(&boundary_ls);

        let ls = edge.to_linestring().unwrap();
        assert_eq!(ls.0.len(), 2);
        dbg!(&ls);
        assert_ne!(ls, boundary_ls);
    }

    #[test]
    fn test_cell_centroid_distance_m() {
        let edge = H3DirectedEdge::new(0x149283080ddbffff);
        assert!(edge.length_m().unwrap() < edge.cell_centroid_distance_m().unwrap());
        assert!((2.0 * edge.length_m().unwrap()) > edge.cell_centroid_distance_m().unwrap());
    }
}