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
//! # Physics Geometry Utilities
//!
//! This module provides utilities for converting and processing geometric shapes extracted from Tiled maps
//! into formats suitable for physics simulation. It bridges the gap between Tiled's geometry representation
//! and the requirements of physics backends (Rapier, Avian, etc.).
use *;
use VecDeque;
use crate;
/// Converts a [`geo::MultiPolygon<f32>`] into a vector of triangles and their centroids.
///
/// Each triangle is represented as an array of three [`Vec2`] points, and its centroid as a [`Vec2`].
/// This is useful for physics backends that require triangulated shapes.
///
/// # Arguments
/// * `multi_polygon` - The input geometry to triangulate.
///
/// # Returns
/// A vector of tuples: ([triangle_vertices; 3], centroid).
/// Converts a [`geo::MultiPolygon<f32>`] into a vector of [`geo::LineString<f32>`].
///
/// This function extracts all exterior and interior rings from the input geometry and returns them as line strings.
/// Useful for physics backends that operate on polylines or linestrips.
///
/// # Arguments
/// * `multi_polygon` - The input geometry to extract line strings from.
///
/// # Returns
/// A vector of [`geo::LineString<f32>`] representing all rings in the geometry.
/// Reduces a collection of items into a single item using a binary reduction function.
///
/// This function implements a **tree reduction algorithm**: it repeatedly pairs consecutive items
/// and applies a reduction function to combine them, until only one item remains. This approach
/// is efficient for aggregating geometric shapes, combining colliders, or reducing complex data.
///
/// # Arguments
/// * `list` - The collection of items to reduce. If empty, returns `None`.
/// * `reduction` - A closure that combines two items into one. Called repeatedly in a tree pattern.
///
/// # Returns
/// * `Some(T)` - The final reduced item if the list is non-empty.
/// * `None` - If the input list is empty.
///
/// # Examples
///
/// **Combine multiple polygons using boolean union**
/// ```rust,no_run
/// use ::geo::BooleanOps;
/// use bevy_ecs_tiled::prelude::*;
///
/// let polygon1 = geo::MultiPolygon::<f32>::empty();
/// let polygon2 = geo::MultiPolygon::<f32>::empty();
/// let polygon3 = geo::MultiPolygon::<f32>::empty();
///
/// let polygons = vec![polygon1, polygon2, polygon3];
/// let combined = simplify_geometry(polygons, |a, b| {
/// // Combine two polygons using boolean union
/// a.union(&b)
/// });
/// ```