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
// SPDX-License-Identifier: MIT
/*
 * Copyright (c) [2023 - Present] Emily Matheys <emilymatt96@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

use nalgebra::{Point2, RealField, Vector2};
use num_traits::{AsPrimitive, Bounded};

use crate::{utils::calculate_polygon_extents, Vec};

#[inline]
#[cfg_attr(
    feature = "tracing",
    tracing::instrument("Does Ray Intersect Polygon", skip_all, level = "trace")
)]
fn does_ray_intersect_polygon_segment<T>(
    point: &Vector2<T>,
    vertex1: Point2<T>,
    vertex2: Point2<T>,
) -> bool
where
    T: Copy + RealField,
    f32: AsPrimitive<T>,
{
    if point.y > vertex1.y.min(vertex2.y)
        && point.y <= vertex1.y.max(vertex2.y)
        && point.x <= vertex1.x.max(vertex2.x)
    {
        let origin_x = (vertex1.y != vertex2.y)
            .then(|| {
                (point.y - vertex1.y) * (vertex2.x - vertex1.x) / (vertex2.y - vertex1.y)
                    + vertex1.x
            })
            .unwrap_or(point.x);

        if vertex1.x == vertex2.x || point.x <= origin_x {
            return true;
        }
    }

    false
}

/// Check if the provided point is within the provided polygon.
///
/// # Arguments
/// * `point`: A reference to a [`Point2`].
/// * `polygon`: A slice of [`Point2`]s representing the vertices.
///
/// # Generics:
/// * `T`: Either an [`prim@f32`] or [`prim@f64`]
///
/// # Returns
/// A boolean value, specifying if the point is within the polygon.
#[cfg_attr(
    feature = "tracing",
    tracing::instrument("Is Point In Polygon", skip_all, level = "debug")
)]
pub fn is_single_point_in_polygon<T>(point: &Point2<T>, polygon: &[Point2<T>]) -> bool
where
    T: Copy + RealField,
    f32: AsPrimitive<T>,
{
    let polygon_len = polygon.len();
    (0..polygon_len)
        .filter_map(|current_vertex_idx| {
            let current_vertex = polygon[current_vertex_idx];
            let next_vertex = polygon[(current_vertex_idx + 1) % polygon_len];

            does_ray_intersect_polygon_segment(&point.coords, current_vertex, next_vertex)
                .then_some(1)
        })
        .sum::<usize>()
        % 2
        == 1 // If the number of intersections is odd - we didn't exit the polygon, and are therefor in it.
}

/// This function will run the [`is_single_point_in_polygon`] for each on of the points given, and the provided polygon,
/// But pre-calculates the polygon extents to reduce workloads for larger datasets, please profile this for you specific use-case.
///
/// # Arguments
/// * `points`: A slice of [`Point2`].
/// * `polygon`: A slice of [`Point2`]s, representing the vertices.
///
/// # Generics:
/// * `T`: Either an [`prim@f32`] or [`prim@f64`]
///
/// # Returns
/// A [`Vec`] of booleans, with the same size as `points`, containing the result for each point.
#[cfg_attr(
    feature = "tracing",
    tracing::instrument("Are Points In Polygon", skip_all, level = "info")
)]
pub fn are_multiple_points_in_polygon<T>(points: &[Point2<T>], polygon: &[Point2<T>]) -> Vec<bool>
where
    T: Bounded + Copy + RealField,
    f32: AsPrimitive<T>,
{
    let polygon_extents = calculate_polygon_extents(polygon);

    points
        .iter()
        .map(|current_point| {
            // Verify that each coordinate is within the bounds of the polygon, will save a lot of computational load for large polygons
            polygon_extents
                .iter()
                .zip(current_point.coords.iter())
                .fold(
                    true,
                    |is_in_extents, (extent_for_dimension, vertex_coord)| {
                        is_in_extents && extent_for_dimension.contains(vertex_coord)
                    },
                )
                && is_single_point_in_polygon(current_point, polygon)
        })
        .collect()
}

#[cfg(feature = "pregenerated")]
macro_rules! impl_p_i_p_algorithm {
    ($prec:expr, doc $doc:tt) => {
        ::paste::paste! {
            #[doc = "A " $doc "-precision implementation of a point-in-polygon algorithm."]
            pub mod [<$doc _precision>] {
                use nalgebra::{Point2};
                use crate::Vec;

                #[doc = "Check if the provided point is within the provided polygon."]
                #[doc = ""]
                #[doc = "# Arguments"]
                #[doc = "* `point`: A reference to a [`Point2`]."]
                #[doc = "* `polygon`: A slice of [`Point2`]s representing the vertices."]
                #[doc = "# Returns"]
                #[doc = "A boolean value, specifying if the point is within the polygon."]
                pub fn is_single_point_in_polygon(point: &Point2<$prec>, polygon: &[Point2<$prec>]) -> bool {
                    super::is_single_point_in_polygon(point, polygon)
                }

                #[doc = "This function will run the [`is_single_point_in_polygon`] for each on of the points given, and the provided polygon,"]
                #[doc = "But pre-calculates the polygon extents to reduce workloads for larger datasets, please profile this for you specific use-case."]
                #[doc = ""]
                #[doc = "# Arguments"]
                #[doc = "* `points`: A slice of [`Point2`]."]
                #[doc = "* `polygon`: A slice of [`Point2`]s, representing the vertices."]
                #[doc = ""]
                #[doc = "# Returns"]
                #[doc = "A [`Vec`](crate::Vec) of booleans, with the same size as `points`, containing the result for each point."]
                pub fn are_multiple_points_in_polygon(
                    points: &[Point2<$prec>],
                    polygon: &[Point2<$prec>],
                ) -> Vec<bool> {
                    super::are_multiple_points_in_polygon(points, polygon)
                }
            }
        }
    };
}

#[cfg(feature = "pregenerated")]
impl_p_i_p_algorithm!(f32, doc single);
#[cfg(feature = "pregenerated")]
impl_p_i_p_algorithm!(f64, doc double);

#[cfg(test)]
mod tests {
    use nalgebra::{Point2, Vector2};

    use crate::Vec;

    use super::*;

    fn get_polygon_for_tests() -> Vec<Point2<f32>> {
        Vec::from([
            Point2::from([0.0, 0.0]),
            Point2::from([1.0, 1.2]),
            Point2::from([1.4, 1.2]),
            Point2::from([1.4, 2.0]),
            Point2::from([0.5, 1.8]),
            Point2::from([-2.0, 0.2]),
            Point2::from([-1.2, -0.4]),
            Point2::from([-0.3, -0.4]),
        ])
    }

    #[test]
    fn test_does_ray_intersect() {
        let point_a = Vector2::new(0.5, -1.5);
        let vertex_a1 = Point2::new(5.0, 0.0);
        let vertex_a2 = Point2::new(1.0, -4.0);
        assert!(does_ray_intersect_polygon_segment(
            &point_a, vertex_a1, vertex_a2
        ));

        let point_b = Vector2::new(-0.5, -1.5);
        let vertex_b1 = Point2::new(0.0, 0.0);
        let vertex_b2 = Point2::new(1.0, 5.0);
        assert!(!does_ray_intersect_polygon_segment(
            &point_b, vertex_b1, vertex_b2
        ));
    }

    #[test]
    fn test_is_single_points_in_polygon() {
        let polygon = get_polygon_for_tests();

        let point = Point2::from([0.5, 1.5]);

        assert!(single_precision::is_single_point_in_polygon(
            &point, &polygon
        ));
    }

    #[test]
    fn test_multiple_points_in_polygon_clockwise() {
        let polygon = get_polygon_for_tests();

        // One point inside the polygon and one outside.
        let points = &[
            Point2::from([0.5, 1.5]), // Inside
            Point2::from([1.5, 1.5]), // Outside
        ];

        let result = single_precision::are_multiple_points_in_polygon(points, &polygon);

        // Expecting [true, false] since the first point is inside and the second is outside.
        assert_eq!(result, Vec::from([true, false]));
    }

    #[test]
    fn test_multiple_points_in_polygon_counter_clockwise() {
        let polygon = get_polygon_for_tests();

        // One point inside the polygon and one outside.
        let points = &[
            Point2::from([0.5, 1.5]), // Inside
            Point2::from([1.5, 1.5]), // Outside
        ];

        let result = single_precision::are_multiple_points_in_polygon(points, &polygon);

        // Expecting [true, false] since the first point is inside and the second is outside.
        assert_eq!(result, Vec::from([true, false]));
    }
}