aboutsummaryrefslogtreecommitdiff
path: root/src/math/polygon/mod.rs
blob: 45308577123750f4281e763527ec28a507e4e0ef (plain) (blame)
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
//! Contains functions and structures to help with operations on polygons.

pub mod polygon_graph;
pub mod triangulate;

pub use polygon_graph::*;
pub use triangulate::*;

use super::Vec2;
use nalgebra::{ClosedDiv, ClosedMul, ClosedSub, RealField, Scalar};
use num_traits::Zero;
use std::ops::Neg;

#[derive(Debug)]
// TODO: Support polygons with holes
pub struct Polygon<T: Scalar + Copy> {
    pub corners: Vec<Vec2<T>>,
}

impl<T: Scalar + Copy> Polygon<T> {
    pub fn new(corners: Vec<Vec2<T>>) -> Self {
        Self { corners }
    }

    /// Check whether a point is inside a polygon or not. If a point lies on an edge, it also
    /// counts as inside the polygon.
    pub fn contains_point(&self, p: Vec2<T>) -> bool
    where
        T: Zero + ClosedSub + ClosedMul + ClosedDiv + Neg<Output = T> + PartialOrd,
    {
        let n = self.corners.len();

        let a = self.corners[n - 1];
        let mut b = self.corners[n - 2];
        let mut ax;
        let mut ay = a.y - p.y;
        let mut bx = b.x - p.x;
        let mut by = b.y - p.y;

        let mut lup = by > ay;
        for i in 0..n {
            // ax = bx;
            ay = by;
            b = self.corners[i];
            bx = b.x - p.x;
            by = b.y - p.y;

            if ay == by {
                continue;
            }
            lup = by > ay;
        }

        let mut depth = 0;
        for i in 0..n {
            ax = bx;
            ay = by;
            let b = &self.corners[i];
            bx = b.x - p.x;
            by = b.y - p.y;

            if ay < T::zero() && by < T::zero() {
                // both "up" or both "down"
                continue;
            }
            if ay > T::zero() && by > T::zero() {
                // both "up" or both "down"
                continue;
            }
            if ax < T::zero() && bx < T::zero() {
                // both points on the left
                continue;
            }

            if ay == by && (if ax < bx { ax } else { bx }) <= T::zero() {
                return true;
            }
            if ay == by {
                continue;
            }

            let lx = ax + (((bx - ax) * -ay) / (by - ay));
            if lx == T::zero() {
                // point on edge
                return true;
            }
            if lx > T::zero() {
                depth += 1;
            }
            if ay == T::zero() && lup && by > ay {
                // hit vertex, both up
                depth -= 1;
            }
            if ay == T::zero() && !lup && by < ay {
                // hit vertex, both down
                depth -= 1;
            }

            lup = by > ay;
        }

        (depth & 1) == 1
    }

    /// Join this polygon with another, ensuring the area of the two stays the same, but the
    /// overlap is not doubled, but instead joined into one.
    /// Returns the Polygons themselves, if there is no overlap
    pub fn unite(self, other: Polygon<T>) -> Vec<Polygon<T>>
    where
        T: RealField,
    {
        let mut graph = PolygonGraph::from_polygon(&self);
        graph.add_all(&other);

        // TODO: Make bounding box support multiple polygons
        vec![graph.bounding_polygon()]
    }
}

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

    #[test]
    fn polygon_contains() {
        let polygon = Polygon::new(vec![
            Vec2::new(0., 0.),
            Vec2::new(-1., 1.),
            Vec2::new(0., 2.),
            Vec2::new(1., 3.),
            Vec2::new(3., 1.5),
            Vec2::new(2., 0.),
            Vec2::new(1., 1.),
        ]);

        assert!(!polygon.contains_point(Vec2::new(1., -2.)));
        assert!(!polygon.contains_point(Vec2::new(-1., 0.5)));
        assert!(polygon.contains_point(Vec2::new(0., 0.5)));
        assert!(polygon.contains_point(Vec2::new(0.5, 1.)));
        assert!(polygon.contains_point(Vec2::new(0.5, 1.5)));
        assert!(!polygon.contains_point(Vec2::new(-2., 1.9)));
        assert!(!polygon.contains_point(Vec2::new(0., 3.)));
        assert!(polygon.contains_point(Vec2::new(1., 3.)));
    }

    #[test]
    fn polygon_union() {
        let first = Polygon::new(vec![
            Vec2::new(-2., 1.),
            Vec2::new(-0.5, 2.5),
            Vec2::new(2., 2.),
            Vec2::new(0.5, 1.5),
            Vec2::new(1., 0.),
            Vec2::new(-0.5, 1.),
        ]);

        let second = Polygon::new(vec![
            Vec2::new(0., 0.),
            Vec2::new(-2., 2.),
            Vec2::new(3., 2.),
            Vec2::new(1.5, 0.),
        ]);

        let union = first.unite(second);
        assert_eq!(union.len(), 1);
        let union = &union[0];

        println!("Union of the two polygons: {:?}", union);

        assert_eq!(union.corners.len(), 11);
        assert!(union
            .corners
            .iter()
            .find(|&p| p.x == 0. && p.y == 0.)
            .is_some());
    }
}