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
#![warn(missing_docs)]
#![warn(rustdoc::missing_doc_code_examples)]
#![doc = include_str!("../README.md")]

pub use traits::ExtendedNumOps;

#[cfg(feature = "tests")]
use serde::{Deserialize, Serialize};

mod traits;

/// A two-diemensional point, where the value of each coordinate must implement the
/// [ExtendedNumOps] trait.
///
/// Example:
/// ```
/// use simplify_polyline::*;
///
/// let point = Point::<f64> { x: 1.0, y: 1.0 };
/// ```
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "tests", derive(Serialize, Deserialize))]
pub struct Point<T: ExtendedNumOps> {
    /// The x coordinate value.
    pub x: T,
    /// The y coordinate value.
    pub y: T,
}

/// Creates a [Point] struct, used for input and output for [simplify]. A type should be specified
/// as the first argument that implements the [ExtendedNumOps] trait.
///
/// Example
/// ```
/// use simplify_polyline::*;
///
/// let point = point!(f64, 1.0, 1.0);
/// ```
#[macro_export]
macro_rules! point {
    ($t:ty, $x:expr,$y:expr) => {
        Point {
            x: $x as $t,
            y: $y as $t,
        }
    };
}

/// Creates a &[[Point]] array, used for  used for input and output for [simplify]. A type should
/// be specified as the first argument that implements the [ExtendedNumOps] trait. Point values
/// should be specified as length-2 tuples, where each value matches the input type, in (x, y)
/// order.
///
/// Example
/// ```
/// use simplify_polyline::*;
///
/// let points = points![f64, (1.0, 1.0), (2.0, 2.0), (3.0, 3.0)];
/// ```
#[macro_export]
macro_rules! points {
    ($t: ty, $(($x:expr,$y:expr)),*) => {
        &[$(Point { x: $x as $t, y: $y as $t }),*]
    };
}

fn get_sq_dist<T: ExtendedNumOps>(p1: &Point<T>, p2: &Point<T>) -> T {
    let dx = p2.x - p1.x;
    let dy = p2.y - p1.y;

    dx * dx + dy * dy
}

fn get_sq_seg_dist<T: ExtendedNumOps>(pt: &Point<T>, start: &Point<T>, end: &Point<T>) -> T {
    let (mut x, mut y, mut dx, mut dy) = (start.x, start.y, end.x - start.x, end.y - start.y);

    if !dx.is_zero() || !dy.is_zero() {
        let t = ((pt.x - x) * dx + (pt.y - y) * dy) / (dx * dx + dy * dy);

        if t > T::one() {
            x = end.x;
            y = end.y;
        } else if t > T::zero() {
            x += dx * t;
            y += dy * t;
        }
    }

    dx = pt.x - x;
    dy = pt.y - y;

    dx * dx + dy * dy
}

fn simplify_radial_dist<T: ExtendedNumOps>(points: &[Point<T>], tolerance: T) -> Vec<Point<T>> {
    let mut prev_point = points[0];
    let mut new_points = vec![prev_point];
    let mut point = prev_point;

    for pt in points.iter().skip(1) {
        point = *pt;
        if get_sq_dist(pt, &prev_point) > tolerance {
            new_points.push(*pt);
            prev_point = *pt;
        }
    }

    if prev_point != point {
        new_points.push(point);
    }

    new_points
}

fn simplify_dp_step<T: ExtendedNumOps>(
    points: &[Point<T>],
    first: usize,
    last: usize,
    tolerance: T,
    simplified: &mut Vec<Point<T>>,
) {
    let mut max_sq_dist = tolerance;
    let mut max_index = 0;

    for i in first + 1..last {
        let sq_dist = get_sq_seg_dist(&points[i], &points[first], &points[last]);
        if sq_dist > max_sq_dist {
            max_index = i;
            max_sq_dist = sq_dist;
        }
    }

    if max_sq_dist > tolerance {
        if (max_index - first) > 1 {
            simplify_dp_step(points, first, max_index, tolerance, simplified);
        }
        simplified.push(points[max_index]);
        if (last - max_index) > 1 {
            simplify_dp_step(points, max_index, last, tolerance, simplified);
        }
    }
}

fn simplify_douglas_peucker<T: ExtendedNumOps>(points: &[Point<T>], tolerance: T) -> Vec<Point<T>> {
    let mut simplified = vec![points[0]];
    simplify_dp_step(points, 0, points.len() - 1, tolerance, &mut simplified);
    simplified.push(points[points.len() - 1]);

    simplified
}

/// Simplifies a polyline within a given tolerance.
///
///
/// # Arguments
///
/// - `tolerance`: A distance measurement used for both radial distance and Douglas–Peucker -- the
/// higher the tolerance, the more points will be removed from the polyline.
/// - `high_quality`: Controls the algorithm(s) to be used in simplification
///   - `true`: this will take the entire array of points and simplify using the Douglas–Peucker
///     algorithm.
///   - `false`: the list of points are first filtered using a simple radial distance algorithm,
///     and then passed to the the Douglas-Peucker algorithm for final simplification.
pub fn simplify<T: ExtendedNumOps>(
    points: &[Point<T>],
    tolerance: f64,
    high_quality: bool,
) -> Vec<Point<T>> {
    if points.len() <= 2 {
        return points.to_vec();
    }

    let tolerance_t = T::from_f64(tolerance).unwrap_or_else(T::one);

    let tolerance_sq = tolerance_t * tolerance_t;
    let intermediate = if high_quality {
        points.to_vec()
    } else {
        simplify_radial_dist(points, tolerance_sq)
    };

    simplify_douglas_peucker(&intermediate, tolerance_sq)
}