sepax2d 0.3.6

A safe crate for finding and resolving collisions of 2D convex shapes using the Separating Axis Theorem.
Documentation
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#[cfg(feature = "serde")]
use serde::{Serialize, Deserialize};

/// A polygon with a position and finitely many vertices given in either clockwise or
/// counterclockwise orientation.
/// 
/// The polygon is represented as a star shape: to find the absolute position of the 
/// n-th vertex, add the `position` to the n-th element of `vertices`.
/// 
/// The position is NOT used as a vertex by default: if you want your position to
/// be a vertex, then define a vertex with offset `(0.0, 0.0)`.
/// 
/// The order of the vertices container determines the order in which the vertices are connected.
/// Both clockwise and counterclockwise orientations are valid.
/// 
/// # Examples
/// 
/// ```
/// use sepax2d::prelude::*;
/// 
/// let mut triangle = Polygon::new((3.0, 2.0));
/// triangle.add((-1.0, -1.0));
/// triangle.add((0.0, 1.0));
/// triangle.add((1.0, -1.0)); 
/// //Triangle with vertices (2, 1), (3, 3), and (4, 1)
/// 
/// assert!(triangle.is_convex());
/// 
/// let vertices = vec![(0.0, 0.0), (2.0, 0.0), (2.0, 1.0), (0.0, 1.0)];
/// let rectangle = Polygon::from_vertices((0.0, 0.0), vertices); 
/// //Rectangle with vertices (0,0), (2,0), (2,1), and (0, 1)
/// 
/// assert!(rectangle.is_convex());
/// 
/// let concave_vertices = vec![(0.0, 0.0), (2.0, 0.0), (0.0, 1.0), (2.0, 1.0)];
/// let concave_shape = Polygon::from_vertices((0.0, 0.0), concave_vertices); 
/// //Non-convex hour-glass shape with the same vertices
/// 
/// assert!(!concave_shape.is_convex());
/// ```
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Polygon
{

    pub position: (f32, f32),
    pub vertices: Vec::<(f32,f32)>

}

impl Polygon
{

    /// Define a new polygon with no vertices at the given position. 
    /// 
    /// # Examples
    /// 
    /// ```
    /// use sepax2d::prelude::*;
    /// 
    /// let point = Polygon::new((17.0, 5.0));
    /// ```
    pub fn new(position: (f32, f32)) -> Polygon
    {

        return Polygon { position, vertices: Vec::new() };

    }

    /// Returns `true` when the polygon is convex, i.e. when none of its points lie on opposite sides of one
    /// of its perimeter segements, and `false` when it is concave.
    /// 
    /// Returns true automatically when the shape has two or fewer vertices, as points and line segments are
    /// trivially convex.
    /// 
    /// This method performs a floating point comparison with Rust's built in epsilon constant, so it may
    /// return the incorrect answer for polygons which are almost complex or almost concave.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use sepax2d::prelude::*;
    /// 
    /// let vertices = vec![(-1.0, 1.0), (1.0, 1.0), (1.0, -1.0), (-1.0, -1.0)];
    /// let square = Polygon::from_vertices((0.0, 0.0), vertices);
    /// 
    /// assert!(square.is_convex());
    /// 
    /// let concave_vertices = vec![(0.0, 0.0), (4.0, 0.0), (4.0, 2.0), (2.0, 1.0), (0.0, 2.0)];
    /// let concave_shape = Polygon::from_vertices((0.0, 0.0), concave_vertices);
    /// 
    /// assert!(!concave_shape.is_convex());
    /// ```
    pub fn is_convex(&self) -> bool
    {

        if self.vertices.len() > 2
        {

            if let Some((first_x, first_y)) = self.vertices.first()
            {

                //We do not need to use position, as convexity is independent of position
                let mut previous = (*first_x, *first_y);

                //For each side, project the polygon onto its perpendicular axis
                for (i, (x, y)) in self.vertices.iter().cycle().skip(1).take(self.vertices.len()).enumerate()
                {

                    let side = (*x - previous.0, *y - previous.1);
                    let axis = (-side.1, side.0);

                    let (min, max) = self.side_projection(axis, i + 1);

                    if min < -f32::EPSILON && max > f32::EPSILON
                    {

                        //There are points on both sides of a polygon edge, which must mean it is not convex
                        return false;

                    }

                    previous = (*x, *y);

                }

            }

        }

        //If the polygon doesn't have enough vertices, then it is trivially convex
        return true;

    }

    fn side_projection(&self, axis: (f32, f32), start: usize) -> (f32, f32)
    {

        let mut min = f32::MAX;
        let mut max = f32::MIN;

        //Project all vertices not touching the given side onto the given axis
        for (x, y) in self.vertices.iter().cycle().skip(start + 1).take(self.vertices.len() - 2)
        {

            //Dot product the current position vector to the axis of projection
            let projection = (*x * axis.0) + (*y * axis.1);

            min = f32::min(min, projection);
            max = f32::max(max, projection);

        }

        return (min, max);

    }

    /// Adds a vertex to the given shape, setting it as the last vertex.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use sepax2d::prelude::*;
    /// 
    /// let vertices = vec![(0.0, 0.0), (2.0, 0.0), (2.0, 2.0)];
    /// let mut polygon = Polygon::from_vertices((0.0, 0.0), vertices); //Triangle with vertices (0, 0), (2, 0), and (2, 2)
    /// 
    /// polygon.add((0.0, 2.0)) //Square with vertices (0, 0), (2, 0), (2, 2), and (0, 2)
    /// ```
    pub fn add(&mut self, vertex: (f32, f32))
    {

        self.vertices.push(vertex);

    }

    /// Creates a polygon from the given vertices.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use sepax2d::prelude::*;
    /// 
    /// let vertices = vec![(0.0, 0.0), (2.0, 0.0), (2.0, 1.0), (0.0, 1.0)];
    /// let rectangle = Polygon::from_vertices((0.0, 0.0), vertices); 
    /// //Rectangle with vertices (0,0), (2,0), (2,1), and (0, 1)
    /// ```
    pub fn from_vertices(position: (f32, f32), vertices: Vec<(f32, f32)>) -> Polygon
    {

        return Polygon { position, vertices };

    }

    /// Attempts to create a convex polygon from the given vertices. It returns the poloygon if
    /// it is convex, and `None` if it is concave. 
    /// 
    /// # Examples
    /// 
    /// ```
    /// use sepax2d::prelude::*;
    /// 
    /// let vertices = vec![(-1.0, 1.0), (1.0, 1.0), (1.0, -1.0), (-1.0, -1.0)];
    /// let square = Polygon::convex_from_vertices((0.0, 0.0), vertices);
    /// 
    /// assert!(square.is_some());
    /// 
    /// let concave_vertices = vec![(0.0, 0.0), (4.0, 0.0), (4.0, 2.0), (2.0, 1.0), (0.0, 2.0)];
    /// let concave_shape = Polygon::convex_from_vertices((0.0, 0.0), concave_vertices);
    /// 
    /// assert!(concave_shape.is_none());
    /// ```
    pub fn convex_from_vertices(position: (f32, f32), vertices: Vec<(f32, f32)>) -> Option<Polygon>
    {

        let polygon = Polygon { position, vertices };

        if polygon.is_convex()
        {

            return Some(polygon);

        }

        return None;

    }

}

impl crate::Shape for Polygon
{

    fn position(&self) -> (f32, f32)
    {

        return self.position;

    }

    fn set_position(&mut self, position: (f32, f32))
    {

        self.position = position;

    }

    fn num_axes(&self) -> usize
    {

        //We only perform collision check for polygons, not lines or points
        if self.vertices.len() <= 1
        {

            return 0;

        }

        if self.vertices.len() == 2
        {

            return 1;

        }

        return self.vertices.len();

    }


    fn get_axis(&self, index: usize, _target: (f32, f32)) -> (f32, f32)
    {

        if self.vertices.len() <= 1 || index >= self.vertices.len()
        {

            return (0.0, 0.0);

        }

        let vertex = self.vertices[index];
        let next = self.vertices[(index + 1) % self.vertices.len()];

        let side = (next.0 - vertex.0, next.1 - vertex.1);
        let axis = (-side.1, side.0);

        return axis;

    }

    fn project(&self, axis: (f32, f32), _normalize: bool) -> (f32, f32)
    {

        return crate::project(self.position, axis, &self.vertices);

    }

    fn needs_closest(&self, _index: usize) -> bool
    {

        return false;

    }

    fn get_closest(&self, target: (f32, f32)) -> (f32, f32)
    {

        return crate::closest(self.position, target, &self.vertices);

    }

    fn point(&self, index: usize) -> (f32, f32)
    {

        if index >= self.vertices.len()
        {

            return self.position;

        }

        let vertex = self.vertices[index];
        return (self.position.0 + vertex.0, self.position.1 + vertex.1);

    }

}

#[cfg(test)]
mod polygon_tests
{

    use super::*;
    use crate::{float_equal, Shape};

    #[test]
    fn test_add_is_convex()
    {

        let mut square = Polygon::new((0.0, 0.0));
        square.add((0.0, 0.0));
        square.add((1.0, 0.0));
        square.add((1.0, 1.0));
        square.add((0.0, 1.0));

        assert!(square.is_convex());

        //Ensure that it works for polygons with more vertices than necessary
        let mut triangle = Polygon::new((-32.0, 160.0));
        triangle.add((-5.0, 0.0));
        triangle.add((-2.0, -2.0));
        triangle.add((5.0, 0.0));
        triangle.add((0.0, 0.0));

        assert!(triangle.is_convex());

    }

    #[test]
    fn test_add_not_convex()
    {

        let mut four = Polygon::new((0.0, 0.0));
        four.add((0.0, 0.0));
        four.add((1.0, 0.0));
        four.add((0.0, 1.0));
        four.add((1.0, 1.0));

        assert!(!four.is_convex());

        let mut five = Polygon::new((-32.0, 160.0));
        five.add((-5.0, 0.0));
        five.add((-2.0, -2.0));
        five.add((5.0, 0.0));
        five.add((5.0, 10.0));
        five.add((0.0, 0.0));

        assert!(!five.is_convex());

    }

    #[test]
    fn test_from_vertices_is_convex()
    {

        let vertices = vec![(8.0, 0.0), (12.0, 4.0), (6.0, 8.0), (0.0, 4.0), (0.0, 0.0)];
        let pentagon = Polygon::from_vertices((3.0, 2.0), vertices);

        assert!(pentagon.is_convex());

    }

    #[test]
    fn test_from_vertices_not_convex()
    {

        let vertices = vec![(-4.0, 0.0), (-3.0, 2.0), (0.0, 1.0), (3.0, 2.0), (4.0, 0.0), (0.0, 0.0)];
        let concave = Polygon::from_vertices((32.0, 4.56), vertices);

        assert!(!concave.is_convex());

    }

    #[test]
    fn test_convex_from_vertices_is_convex()
    {

        let vertices = vec![(1.0, 1.0), (1.0, 2.0), (0.0, 3.0), (-1.0, 2.0), (-1.0, 1.0), (0.0, 0.0)];
        let hexagon = Polygon::convex_from_vertices((0.0, 0.0), vertices);

        assert!(hexagon.is_some());

    }

    #[test]
    fn test_convex_from_vertices_not_convex()
    {

        let vertices = vec![(1.0, 1.0), (-1.0, 1.0), (0.0, 3.0), (-1.0, 2.0), (2.0, 1.0), (0.0, 0.0)];
        let concave = Polygon::convex_from_vertices((0.0, 0.0), vertices);

        assert!(concave.is_none());

    }

    #[test]
    fn test_num_axes()
    {

        let empty = Polygon::new((1.0, 2.0));
        let point = Polygon::from_vertices((0.0, 3.0), vec![(2.0, 1.0)]);
        let line = Polygon::from_vertices((0.0, 0.0), vec![(0.0, 1.0), (0.0, 0.0)]);

        let vertices = vec![(1.0, 1.0), (1.0, 2.0), (0.0, 3.0), (-1.0, 2.0), (-1.0, 1.0), (0.0, 0.0)];
        let hexagon = Polygon::from_vertices((0.0, 0.0), vertices);

        assert_eq!(empty.num_axes(), 0);
        assert_eq!(point.num_axes(), 0);
        assert_eq!(line.num_axes(), 1);
        assert_eq!(hexagon.num_axes(), 6);


    }

    #[test]
    fn test_get_axis()
    {

        let vertices = vec![(1.0, 1.0), (1.0, 2.0), (0.0, 3.0), (-1.0, 2.0), (-1.0, 1.0), (0.0, 0.0)];
        let hexagon = Polygon::from_vertices((0.0, 0.0), vertices);

        let axis1 = hexagon.get_axis(1, (0.0, 0.0));
        let axis3 = hexagon.get_axis(3, (110.0, 11.0));
        let axis4 = hexagon.get_axis(5, (-1.0, 1.0 / 16.0));

        assert!(float_equal(axis1.0, -1.0));
        assert!(float_equal(axis1.1, -1.0));
        assert!(float_equal(axis3.0, 1.0));
        assert!(float_equal(axis3.1, 0.0));
        assert!(float_equal(axis4.0, -1.0));
        assert!(float_equal(axis4.1, 1.0));

    }

    #[test]
    fn test_project()
    {

        let vertices = vec![(1.0, 1.0), (1.0, 2.0), (0.0, 3.0), (-1.0, 2.0), (-1.0, 1.0), (0.0, 0.0)];
        let hexagon = Polygon::from_vertices((0.0, 0.0), vertices);

        let axis = (1.0, 2.0);
        let projection = hexagon.project(axis, false);

        assert!(float_equal(projection.0, 0.0));
        assert!(float_equal(projection.1, 6.0));

    }

    #[test]
    fn test_needs_closest()
    {

        let square = Polygon::from_vertices((1.0, 2.0), vec![(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)]);

        assert!(!square.needs_closest(2));

    }

}