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
// Copyright (c) 2018-2020 Thomas Kramer.
// SPDX-FileCopyrightText: 2018-2022 Thomas Kramer
//
// SPDX-License-Identifier: AGPL-3.0-or-later

//! `Path` is essentially a chain of line segments but with a possibly non-zero width.
//! It can be thought of the shape resulting by a stroke of a thick pen along the line segments.

use crate::vector::Vector;
use crate::point::Point;
use crate::point_string::PointString;

use crate::CoordinateType;

use crate::traits::{Scale, Translate, TryBoundingBox, MapPointwise};
pub use crate::traits::{BoundingBox, RotateOrtho};
pub use crate::types::Angle;

pub use crate::types::{Side, ContainsResult};

use num_traits::{Float, NumCast, Num};
use crate::simple_polygon::SimplePolygon;
use std::iter::FromIterator;
use crate::edge::*;
use crate::rect::Rect;
use crate::transform::SimpleTransform;
use std::ops::{Add, Mul};

/// Encoding for the type of the beginning and end of the path.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum PathEndType<T> {
    /// Beginning and end of path are not extended.
    Flat,
    /// Define the extension length at the beginning and at the end of the path.
    Extended(T, T),
    /// Path ends are round (approximately semi-circles).
    Round,
}

/// `Path` is essentially a chain of line segments but with a possibly a non-zero width.
/// It can be thought of the shape resulting by a stroke of a thick pen along the line segments.
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Path<T> {
    /// The vertices of the path which define the sequence of line segments.
    pub points: PointString<T>,
    /// Width of the path.
    pub width: T,
    /// Type of the path endings.
    pub path_type: PathEndType<T>,
}

impl<T> Path<T> {
    /// Get number of vertices defining the path.
    pub fn len(&self) -> usize {
        self.points.len()
    }
}

impl<T: Copy> Path<T> {
    /// Create new path by taking vertices from a type that implements `Into<PointString<T>>`.
    pub fn new<I>(i: I, width: T) -> Self
        where I: Into<PointString<T>> {
        Path {
            points: i.into(),
            width,
            path_type: PathEndType::Flat,
        }
    }

    /// Create a path with extended beginning and end.
    pub fn new_extended<I>(i: I, width: T, ext_begin: T, ext_end: T) -> Self
        where I: Into<PointString<T>> {
        Path {
            points: i.into(),
            width,
            path_type: PathEndType::Extended(ext_begin, ext_end),
        }
    }

    /// Create a path with rounded beginning and end.
    pub fn new_rounded<I>(i: I, width: T) -> Self
        where I: Into<PointString<T>> {
        Path {
            points: i.into(),
            width,
            path_type: PathEndType::Round,
        }
    }
}

impl<T: Copy + Add<Output=T>> Path<T> {
    /// Translate the path by an offset vector.
    pub fn translate(&self, v: Vector<T>) -> Self {
        Path {
            points: self.points.translate(v),
            width: self.width,
            path_type: self.path_type,
        }
    }
}

impl<T: Copy + Mul<Output=T>> Path<T> {
    /// Scale the path. Scaling center is the origin `(0, 0)`.
    pub fn scale(&self, factor: T) -> Self {
        Path {
            points: self.points.scale(factor),
            width: self.width * factor,
            path_type: self.path_type,
        }
    }
}

impl<T: CoordinateType> Path<T> {
    /// Rotate the path by a multiple of 90 degrees around the origin `(0, 0)`.
    pub fn rotate_ortho(&self, angle: Angle) -> Self {
        Path {
            points: self.points.rotate_ortho(angle),
            width: self.width,
            path_type: self.path_type,
        }
    }

    /// Get the transformed version of this path by applying `tf`.
    pub fn transform(&self, tf: &SimpleTransform<T>) -> Self {
        Self {
            points: self.points.transform(|p| tf.transform_point(p)),
            width: tf.transform_distance(self.width),
            path_type: self.path_type,
        }
    }
}

impl<T: CoordinateType + NumCast> Path<T> {
    /// Compute approximate area occupied by the path.
    /// Simply computes length*width.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use iron_shapes::prelude::*;
    /// let path = Path::new(&[(0, 0), (0, 2)], 1);
    /// assert_eq!(path.area_approx::<f64>(), 2f64);
    /// ```
    pub fn area_approx<F: Float>(&self) -> F {
        let base_len: F = self.points.path_length();
        let w = F::from(self.width).unwrap();
        let l = match self.path_type {
            PathEndType::Extended(l1, l2) => base_len + F::from(l1 + l2).unwrap(),
            _ => base_len,
        };

        let base_area = l * w;

        // Add area of circle if path ends are round.
        match self.path_type {
            PathEndType::Round => base_area + F::from(std::f64::consts::PI).unwrap() * w * w,
            _ => base_area
        }
    }


    /// Convert the path into a polygon.
    /// The polygon can be self-intersecting.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use iron_shapes::prelude::*;
    /// let path = Path::new(&[(0, 0), (10, 0), (10, 20)], 4);
    /// let polygon = path.to_polygon_approx();
    /// assert_eq!(polygon, SimplePolygon::from(&[(0., 2.), (0., -2.), (12., -2.), (12., 20.), (8., 20.), (8., 2.)]));
    /// ```
    pub fn to_polygon_approx(&self) -> SimplePolygon<f64> {
        let mut points_forward: Vec<Point<f64>> = Vec::new();
        let mut points_backward: Vec<Point<f64>> = Vec::new();

        let edges: Vec<Edge<f64>> = self.points.edges()
            .filter(|e| e.start != e.end) // Skip zero-length edges.
            .map(|e| e.cast_to_float()).collect();


        // Construct rectangular start and end caps.
        let create_flat_cap = |center_edge: Edge<f64>, width: f64, extension: f64| -> Vec<Point<f64>> {
            let d = center_edge.vector().normalized();
            let n = d.rotate_ortho(Angle::R90);
            let p = center_edge.end;
            let w_half = width / 2.;
            if extension == 0. {
                let p1 = p - n * w_half;
                let p2 = p + n * w_half;
                vec![p1, p2]
            } else {
                let p1 = p - n * w_half;
                let p4 = p + n * w_half;
                let p2 = p1 + d * extension;
                let p3 = p4 + d * extension;
                vec![p1, p2, p3, p4]
            }
        };

        // Calculate start/end extensions.
        let (start_ext, end_ext) = match self.path_type {
            PathEndType::Extended(start_ext, end_ext) => {
                let start_ext = NumCast::from(start_ext).unwrap();
                let end_ext = NumCast::from(end_ext).unwrap();
                (start_ext, end_ext)
            }
            PathEndType::Flat => (0., 0.),
            PathEndType::Round => unimplemented!("Not implemented for round path ends.")
        };

        // Path width.
        let width = NumCast::from(self.width).unwrap();
        let half_width = width * 0.5;

        // Create caps.
        let start_cap = edges.first()
            .map(|e| create_flat_cap(e.reversed(), width, start_ext))
            .unwrap_or_else(|| Vec::new());
        let end_cap = edges.last()
            .map(|e| create_flat_cap(*e, width, end_ext))
            .unwrap_or_else(|| Vec::new());

        // Pre-compute normals (scaled by half the width).
        let normals: Vec<Vector<f64>> = edges.iter()
            .map(|e| e.vector().normal() * half_width)
            .collect();

        let edge_pairs = edges.iter().zip(edges.iter().skip(1));
        let normal_pairs = normals.iter().zip(normals.iter().skip(1));

        for ((&e1, &e2), (&n1, &n2)) in edge_pairs.zip(normal_pairs) {
            let border1f = e1.translate(-n1);
            let border1b = e1.translate(n1);
            let border2f = e2.translate(-n2);
            let border2b = e2.translate(n2);

            // Forward.
            let border_intersection_f = border1f.line_intersection_approx(&border2f, 1e-15);

            match border_intersection_f {
                LineIntersection::Collinear => {}
                LineIntersection::None => {}
                LineIntersection::Point(p, _) => {
                    points_forward.push(p)
                }
            }

            // Backward.
            let border_intersection_b = border1b.line_intersection_approx(&border2b, 1e-15);

            match border_intersection_b {
                LineIntersection::Collinear => {}
                LineIntersection::None => {}
                LineIntersection::Point(p, _) => {
                    points_backward.push(p)
                }
            }
        }

        // Concatenate forward and backward points including start and end cap.
        SimplePolygon::from_iter(
            start_cap.iter()
                .chain(points_forward.iter())
                .chain(end_cap.iter())
                .chain(points_backward.iter().rev())
        )
    }
}


impl<T: CoordinateType + NumCast, Dst: CoordinateType + NumCast> TryCastCoord<T, Dst> for Path<T> {
    type Output = Path<Dst>;

    fn try_cast(&self) -> Option<Self::Output> {
        let new_width = Dst::from(self.width);
        let new_points = self.points.try_cast();
        let new_path_type = match self.path_type {
            PathEndType::Extended(begin_ext, end_ext) => {
                let new_begin_ext = Dst::from(begin_ext);
                let new_end_ext = Dst::from(end_ext);
                match (new_begin_ext, new_end_ext) {
                    (Some(b), Some(e)) => Some(PathEndType::Extended(b, e)),
                    _ => None
                }
            }
            PathEndType::Flat => Some(PathEndType::Flat),
            PathEndType::Round => Some(PathEndType::Round),
        };

        match (new_width, new_points, new_path_type) {
            (Some(width), Some(points), Some(path_type)) =>
                Some(Path {
                    points,
                    width,
                    path_type,
                }),
            _ => {
                // Failed to cast some values.
                None
            }
        }
    }
}

impl<T: Copy + PartialOrd + Num> TryBoundingBox<T> for Path<T> {
    // /// Compute the bounding box of this path.
    // fn bounding_box(&self) -> Rect<T> {
    //     // Compute the bounding box by first converting the path into a polygon
    //     // and then computing the bounding box of the polygon.
    //     // Since integer Paths do not support conversion to a polygon the path needs
    //     // to be converted to a float coordinate type.
    //     // TODO: Make this more efficient and preferably without type conversions.
    //     let float_path: Path<FloatType> = self.cast();
    //     let bbox = float_path.to_polygon_approx().bounding_box();
    //     bbox.cast()
    // }

    /// Compute the bounding box of this path.
    /// The returned bounding box is not necessarily the smallest bounding box.
    ///
    /// TODO: Find a better approximation.
    fn try_bounding_box(&self) -> Option<Rect<T>> {
        // Find the bounding box of a zero-width path.
        let bbox = self.points.try_bounding_box();
        let _1 = T::one();
        let _2 = _1 + _1;
        bbox.map(|bbox| {
            // Enlarge it by width/2 in all directions to make sure the path is fully contained
            // in the bounding box.
            let (p1, p2) = (bbox.lower_left(), bbox.upper_right());
            let w_half = (self.width + _1) / _2;
            let width = Vector::new(w_half, w_half);
            Rect::new(p1 - width, p2 + width)
        })
    }
}

// impl<T> Translate<T> for Path<T>
//     where T: CoordinateType {
//     fn translate(&self, v: Vector<T>) -> Self {
//         Path {
//             points: self.points.translate(v),
//             width: self.width,
//             path_type: self.path_type,
//         }
//     }
// }
//
// impl<T> Scale<T> for Path<T>
//     where T: CoordinateType {
//     fn scale(&self, factor: T) -> Self {
//         Path {
//             points: self.points.scale(factor),
//             width: self.width * factor,
//             path_type: self.path_type,
//         }
//     }
// }