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
use super::{PlineCreation, PlineSource, PlineSourceMut, PlineVertex};
use crate::core::traits::Real;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::ops::{Index, IndexMut};

/// Basic polyline data representation that implements the core polyline traits:
/// [PlineSource], [PlineSourceMut], and [PlineCreation]. See the traits documentation for all
/// the polyline methods/operations available.
#[cfg_attr(
    feature = "serde",
    derive(Serialize, Deserialize),
    serde(rename_all = "camelCase")
)]
#[derive(Debug, Clone)]
pub struct Polyline<T = f64> {
    #[cfg_attr(feature = "serde", serde(rename = "vertexes"))]
    /// Contiguous sequence of vertexes.
    pub vertex_data: Vec<PlineVertex<T>>,
    /// Bool to indicate whether the polyline is closed or open.
    pub is_closed: bool,
}

impl<T> Default for Polyline<T>
where
    T: Real,
{
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl<T> Polyline<T>
where
    T: Real,
{
    /// Create a new empty [Polyline] with `is_closed` set to false.
    #[inline]
    pub fn new() -> Self {
        Polyline {
            vertex_data: Vec::new(),
            is_closed: false,
        }
    }

    /// Create a new empty [Polyline] with `is_closed` set to true.
    #[inline]
    pub fn new_closed() -> Self {
        Polyline {
            vertex_data: Vec::new(),
            is_closed: true,
        }
    }
}

impl<T> Index<usize> for Polyline<T> {
    type Output = PlineVertex<T>;

    #[inline]
    fn index(&self, index: usize) -> &Self::Output {
        &self.vertex_data[index]
    }
}

impl<T> IndexMut<usize> for Polyline<T> {
    #[inline]
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        &mut self.vertex_data[index]
    }
}

impl<T> PlineSource for Polyline<T>
where
    T: Real,
{
    type Num = T;
    type OutputPolyline = Polyline<T>;

    #[inline]
    fn vertex_count(&self) -> usize {
        self.vertex_data.len()
    }

    #[inline]
    fn is_closed(&self) -> bool {
        self.is_closed
    }

    #[inline]
    fn get(&self, index: usize) -> Option<PlineVertex<Self::Num>> {
        self.vertex_data.get(index).copied()
    }

    #[inline]
    fn at(&self, index: usize) -> PlineVertex<Self::Num> {
        self[index]
    }
}

impl<T> PlineSourceMut for Polyline<T>
where
    T: Real,
{
    #[inline]
    fn set_vertex(&mut self, index: usize, vertex: PlineVertex<Self::Num>) {
        self.vertex_data[index] = vertex;
    }

    #[inline]
    fn insert_vertex(&mut self, index: usize, vertex: PlineVertex<Self::Num>) {
        self.vertex_data.insert(index, vertex);
    }

    #[inline]
    fn remove(&mut self, index: usize) -> PlineVertex<Self::Num> {
        self.vertex_data.remove(index)
    }

    #[inline]
    fn add_vertex(&mut self, vertex: PlineVertex<Self::Num>) {
        self.vertex_data.push(vertex)
    }

    #[inline]
    fn reserve(&mut self, additional: usize) {
        self.vertex_data.reserve(additional);
    }

    #[inline]
    fn set_is_closed(&mut self, is_closed: bool) {
        self.is_closed = is_closed;
    }

    #[inline]
    fn clear(&mut self) {
        self.vertex_data.clear()
    }

    #[inline]
    fn extend_vertexes<I>(&mut self, vertexes: I)
    where
        I: IntoIterator<Item = PlineVertex<Self::Num>>,
    {
        self.vertex_data.extend(vertexes);
    }
}

impl<T> PlineCreation for Polyline<T>
where
    T: Real,
{
    #[inline]
    fn with_capacity(capacity: usize, is_closed: bool) -> Self {
        Polyline {
            vertex_data: Vec::with_capacity(capacity),
            is_closed,
        }
    }

    #[inline]
    fn from_iter<I>(iter: I, is_closed: bool) -> Self
    where
        I: Iterator<Item = PlineVertex<Self::Num>>,
    {
        Polyline {
            vertex_data: iter.collect(),
            is_closed,
        }
    }
}