tiles_tools 0.2.0

High-performance tile-based game development toolkit with comprehensive coordinate systems (hexagonal, square, triangular, isometric), pathfinding, ECS integration, and grid management.
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
//! Spatial partitioning and optimization structures for tile-based games.
//!
//! This module provides efficient spatial data structures for managing large numbers
//! of entities in tile-based games. It includes quadtree implementation for fast
//! collision detection, spatial queries, and entity management.
//!
//! # Spatial Partitioning
//!
//! Spatial partitioning divides game space into hierarchical regions to enable:
//! - **Fast Collision Detection**: O(log n) instead of O(n²) for entity pairs
//! - **Efficient Spatial Queries**: Quick area-based entity searches
//! - **Level-of-Detail**: Different processing levels based on proximity
//! - **Culling**: Skip processing for entities outside view regions
//!
//! # Quadtree Implementation
//!
//! The quadtree recursively subdivides 2D space into four quadrants:
//! - **Automatic Subdivision**: Splits when node capacity exceeded
//! - **Dynamic Updates**: Handles moving entities efficiently
//! - **Query Optimization**: Fast rectangular and circular queries
//! - **Memory Efficient**: Only allocates nodes as needed
//!
//! # Examples
//!
//! ```rust
//! use tiles_tools::spatial::{ Quadtree, SpatialBounds, SpatialEntity };
//! use tiles_tools::coordinates::square::{ Coordinate as SquareCoord, FourConnected };
//!
//! // Create a quadtree for a 100x100 game world
//! let bounds = SpatialBounds::new(0, 0, 100, 100);
//! let mut quadtree = Quadtree::new(bounds, 10); // Max 10 entities per node
//!
//! // Add entities to the quadtree
//! let entity_id = 1;
//! let position = SquareCoord::<FourConnected>::new(25, 25);
//! quadtree.insert(SpatialEntity::new(entity_id, position, 1)); // radius 1
//!
//! // Query entities in a region
//! let query_bounds = SpatialBounds::new(20, 20, 30, 30);
//! let nearby_entities = quadtree.query_region(&query_bounds);
//! println!("Found {} entities in region", nearby_entities.len());
//! ```

use crate::coordinates::Distance;

/// Represents a rectangular spatial boundary for quadtree operations.
#[ derive( Debug, Clone, Copy, PartialEq ) ]
pub struct SpatialBounds
{
    /// Left boundary (minimum x)
    pub left: i32,
    /// Top boundary (minimum y)  
    pub top: i32,
    /// Right boundary (maximum x)
    pub right: i32,
    /// Bottom boundary (maximum y)
    pub bottom: i32,
}

impl SpatialBounds
{
    /// Creates a new spatial boundary.
    pub fn new(left: i32, top: i32, right: i32, bottom: i32) -> Self {
        Self { left, top, right, bottom }
    }

    /// Creates a boundary from center point and dimensions.
    pub fn from_center_size(center_x: i32, center_y: i32, width: i32, height: i32) -> Self {
        let half_width = width / 2;
        let half_height = height / 2;
        Self {
            left: center_x - half_width,
            top: center_y - half_height,
            right: center_x + half_width,
            bottom: center_y + half_height,
        }
    }

    /// Returns the width of this boundary.
    pub fn width(&self) -> i32 {
        self.right - self.left
    }

    /// Returns the height of this boundary.
    pub fn height(&self) -> i32 {
        self.bottom - self.top
    }

    /// Returns the area of this boundary.
    pub fn area(&self) -> i32 {
        self.width() * self.height()
    }

    /// Checks if this boundary contains a point.
    pub fn contains_point(&self, x: i32, y: i32) -> bool {
        x >= self.left && x <= self.right && y >= self.top && y <= self.bottom
    }

    /// Checks if this boundary intersects with another boundary.
    pub fn intersects(&self, other: &SpatialBounds) -> bool {
        !(self.right < other.left || 
          self.left > other.right || 
          self.bottom < other.top || 
          self.top > other.bottom)
    }

    /// Checks if this boundary completely contains another boundary.
    pub fn contains(&self, other: &SpatialBounds) -> bool {
        self.left <= other.left && 
        self.right >= other.right && 
        self.top <= other.top && 
        self.bottom >= other.bottom
    }

    /// Returns the center point of this boundary.
    pub fn center(&self) -> (i32, i32) {
        ((self.left + self.right) / 2, (self.top + self.bottom) / 2)
    }
}

/// Represents an entity with spatial properties for quadtree storage.
#[derive(Debug, Clone, PartialEq)]
pub struct SpatialEntity<C> {
    /// Unique identifier for this entity
    pub id: u32,
    /// Position in coordinate space
    pub position: C,
    /// Collision/interaction radius
    pub radius: i32,
}

impl<C> SpatialEntity<C> {
    /// Creates a new spatial entity.
    pub fn new(id: u32, position: C, radius: i32) -> Self {
        Self { id, position, radius }
    }

    /// Gets the spatial bounds of this entity.
    pub fn bounds(&self) -> SpatialBounds
    where
        C: SpatialCoordinate,
    {
        let (x, y) = self.position.to_spatial_coords();
        SpatialBounds::from_center_size(x, y, self.radius * 2, self.radius * 2)
    }

    /// Checks if this entity intersects with a boundary.
    pub fn intersects_bounds(&self, bounds: &SpatialBounds) -> bool
    where
        C: SpatialCoordinate,
    {
        self.bounds().intersects(bounds)
    }

    /// Checks if this entity intersects with another entity.
    pub fn intersects_entity(&self, other: &SpatialEntity<C>) -> bool
    where
        C: Distance,
    {
        let distance = self.position.distance(&other.position);
        distance <= (self.radius + other.radius) as u32
    }
}

/// Trait for coordinate types that can be used in spatial partitioning.
pub trait SpatialCoordinate {
    /// Converts this coordinate to spatial (x, y) integers.
    fn to_spatial_coords(&self) -> (i32, i32);
    
    /// Creates a coordinate from spatial (x, y) integers.
    fn from_spatial_coords(x: i32, y: i32) -> Self;
}

/// Quadtree node for hierarchical spatial partitioning.
#[derive(Debug)]
enum QuadtreeNode<C> {
    /// Leaf node containing entities
    Leaf {
        entities: Vec<SpatialEntity<C>>,
    },
    /// Internal node with four child quadrants
    Internal {
        northeast: Box<QuadtreeNode<C>>,
        northwest: Box<QuadtreeNode<C>>,
        southeast: Box<QuadtreeNode<C>>,
        southwest: Box<QuadtreeNode<C>>,
    },
}

impl<C> QuadtreeNode<C> {
    /// Creates a new empty leaf node.
    fn new_leaf() -> Self {
        QuadtreeNode::Leaf {
            entities: Vec::new(),
        }
    }
}

/// Quadtree for efficient spatial partitioning and queries.
#[derive(Debug)]
pub struct Quadtree<C> {
    /// Root node of the quadtree
    root: QuadtreeNode<C>,
    /// Spatial boundary of the entire quadtree
    bounds: SpatialBounds,
    /// Maximum entities per leaf node before subdivision
    max_entities: usize,
    /// Current depth of the quadtree
    max_depth: usize,
}

impl<C> Quadtree<C>
where
    C: SpatialCoordinate + Clone,
{
    /// Creates a new quadtree with the specified bounds and capacity.
    pub fn new(bounds: SpatialBounds, max_entities: usize) -> Self {
        Self {
            root: QuadtreeNode::new_leaf(),
            bounds,
            max_entities,
            max_depth: 0,
        }
    }

    /// Inserts an entity into the quadtree.
    pub fn insert(&mut self, entity: SpatialEntity<C>) {
        let bounds = self.bounds;
        let max_entities = self.max_entities;
        Self::insert_recursive_static(&mut self.root, entity, &bounds, 0, max_entities, &mut self.max_depth);
    }

    /// Removes all entities with the specified ID from the quadtree.
    pub fn remove(&mut self, entity_id: u32) -> Vec<SpatialEntity<C>> {
        let mut removed = Vec::new();
        Self::remove_recursive_static(&mut self.root, entity_id, &mut removed);
        removed
    }

    /// Queries all entities that intersect with the specified boundary.
    pub fn query_region(&self, query_bounds: &SpatialBounds) -> Vec<SpatialEntity<C>> {
        let mut results = Vec::new();
        self.query_recursive(&self.root, query_bounds, &self.bounds, &mut results);
        results
    }

    /// Queries all entities within a circular area.
    pub fn query_circle(&self, center_x: i32, center_y: i32, radius: i32) -> Vec<SpatialEntity<C>>
    where
        C: Distance,
    {
        // First get candidates from rectangular query
        let query_bounds = SpatialBounds::from_center_size(center_x, center_y, radius * 2, radius * 2);
        let candidates = self.query_region(&query_bounds);

        // Filter by actual circular distance
        let center_coord = C::from_spatial_coords(center_x, center_y);
        candidates.into_iter()
            .filter(|entity| {
                let distance = entity.position.distance(&center_coord);
                distance <= (radius as u32)
            })
            .collect()
    }

    /// Gets all entities stored in the quadtree.
    pub fn all_entities(&self) -> Vec<SpatialEntity<C>> {
        let mut entities = Vec::new();
        self.collect_all_entities(&self.root, &mut entities);
        entities
    }

    /// Clears all entities from the quadtree.
    pub fn clear(&mut self) {
        self.root = QuadtreeNode::new_leaf();
        self.max_depth = 0;
    }

    /// Returns statistics about the quadtree structure.
    pub fn stats(&self) -> QuadtreeStats {
        let mut stats = QuadtreeStats::default();
        self.calculate_stats(&self.root, 0, &mut stats);
        stats
    }

    // Private implementation methods

    fn insert_recursive_static(
        node: &mut QuadtreeNode<C>, 
        entity: SpatialEntity<C>, 
        bounds: &SpatialBounds,
        depth: usize,
        max_entities: usize,
        current_max_depth: &mut usize,
    ) {
        *current_max_depth = (*current_max_depth).max(depth);

        match node {
            QuadtreeNode::Leaf { entities } => {
                entities.push(entity);
                
                // Check if we need to subdivide
                if entities.len() > max_entities && depth < 16 { // Max depth limit
                    Self::subdivide_node_static(node, bounds, depth, max_entities, current_max_depth);
                }
            }
            QuadtreeNode::Internal { northeast, northwest, southeast, southwest } => {
                let (center_x, center_y) = bounds.center();
                let (entity_x, entity_y) = entity.position.to_spatial_coords();

                // Determine which quadrant(s) the entity belongs to
                let in_north = entity_y <= center_y;
                let in_east = entity_x >= center_x;

                match (in_north, in_east) {
                    (true, true) => {
                        Self::insert_recursive_static(northeast, entity, 
                            &SpatialBounds::new(center_x, bounds.top, bounds.right, center_y), 
                            depth + 1, max_entities, current_max_depth);
                    }
                    (true, false) => {
                        Self::insert_recursive_static(northwest, entity,
                            &SpatialBounds::new(bounds.left, bounds.top, center_x, center_y), 
                            depth + 1, max_entities, current_max_depth);
                    }
                    (false, true) => {
                        Self::insert_recursive_static(southeast, entity,
                            &SpatialBounds::new(center_x, center_y, bounds.right, bounds.bottom), 
                            depth + 1, max_entities, current_max_depth);
                    }
                    (false, false) => {
                        Self::insert_recursive_static(southwest, entity,
                            &SpatialBounds::new(bounds.left, center_y, center_x, bounds.bottom), 
                            depth + 1, max_entities, current_max_depth);
                    }
                };
            }
        }
    }

    fn subdivide_node_static(
        node: &mut QuadtreeNode<C>, 
        bounds: &SpatialBounds, 
        depth: usize,
        max_entities: usize,
        current_max_depth: &mut usize,
    ) {
        if let QuadtreeNode::Leaf { entities } = node {
            let entities_to_redistribute = std::mem::take(entities);
            
            // Create four child nodes
            *node = QuadtreeNode::Internal {
                northeast: Box::new(QuadtreeNode::new_leaf()),
                northwest: Box::new(QuadtreeNode::new_leaf()),
                southeast: Box::new(QuadtreeNode::new_leaf()),
                southwest: Box::new(QuadtreeNode::new_leaf()),
            };

            // Redistribute entities to child nodes
            for entity in entities_to_redistribute {
                Self::insert_recursive_static(node, entity, bounds, depth, max_entities, current_max_depth);
            }
        }
    }


    fn remove_recursive_static(
        node: &mut QuadtreeNode<C>,
        entity_id: u32,
        removed: &mut Vec<SpatialEntity<C>>
    ) {
        match node {
            QuadtreeNode::Leaf { entities } => {
                let _original_len = entities.len();
                entities.retain(|e| {
                    if e.id == entity_id {
                        removed.push(e.clone());
                        false
                    } else {
                        true
                    }
                });
            }
            QuadtreeNode::Internal { northeast, northwest, southeast, southwest } => {
                Self::remove_recursive_static(northeast, entity_id, removed);
                Self::remove_recursive_static(northwest, entity_id, removed);
                Self::remove_recursive_static(southeast, entity_id, removed);
                Self::remove_recursive_static(southwest, entity_id, removed);
            }
        }
    }

    fn query_recursive(
        &self,
        node: &QuadtreeNode<C>,
        query_bounds: &SpatialBounds,
        node_bounds: &SpatialBounds,
        results: &mut Vec<SpatialEntity<C>>
    ) {
        if !query_bounds.intersects(node_bounds) {
            return;
        }

        match node {
            QuadtreeNode::Leaf { entities } => {
                for entity in entities {
                    if entity.intersects_bounds(query_bounds) {
                        results.push(entity.clone());
                    }
                }
            }
            QuadtreeNode::Internal { northeast, northwest, southeast, southwest } => {
                let (center_x, center_y) = node_bounds.center();
                
                self.query_recursive(
                    northeast, query_bounds,
                    &SpatialBounds::new(center_x, node_bounds.top, node_bounds.right, center_y),
                    results
                );
                self.query_recursive(
                    northwest, query_bounds,
                    &SpatialBounds::new(node_bounds.left, node_bounds.top, center_x, center_y),
                    results
                );
                self.query_recursive(
                    southeast, query_bounds,
                    &SpatialBounds::new(center_x, center_y, node_bounds.right, node_bounds.bottom),
                    results
                );
                self.query_recursive(
                    southwest, query_bounds,
                    &SpatialBounds::new(node_bounds.left, center_y, center_x, node_bounds.bottom),
                    results
                );
            }
        }
    }

    fn collect_all_entities(&self, node: &QuadtreeNode<C>, entities: &mut Vec<SpatialEntity<C>>) {
        match node {
            QuadtreeNode::Leaf { entities: node_entities } => {
                entities.extend_from_slice(node_entities);
            }
            QuadtreeNode::Internal { northeast, northwest, southeast, southwest } => {
                self.collect_all_entities(northeast, entities);
                self.collect_all_entities(northwest, entities);
                self.collect_all_entities(southeast, entities);
                self.collect_all_entities(southwest, entities);
            }
        }
    }

    fn calculate_stats(&self, node: &QuadtreeNode<C>, depth: usize, stats: &mut QuadtreeStats) {
        stats.total_nodes += 1;
        stats.max_depth = stats.max_depth.max(depth);

        match node {
            QuadtreeNode::Leaf { entities } => {
                stats.leaf_nodes += 1;
                stats.total_entities += entities.len();
                stats.max_entities_per_node = stats.max_entities_per_node.max(entities.len());
                if entities.is_empty() {
                    stats.empty_nodes += 1;
                }
            }
            QuadtreeNode::Internal { northeast, northwest, southeast, southwest } => {
                stats.internal_nodes += 1;
                self.calculate_stats(northeast, depth + 1, stats);
                self.calculate_stats(northwest, depth + 1, stats);
                self.calculate_stats(southeast, depth + 1, stats);
                self.calculate_stats(southwest, depth + 1, stats);
            }
        }
    }
}

/// Statistics about quadtree structure and performance.
#[derive(Debug, Default, Clone)]
pub struct QuadtreeStats {
    /// Total number of nodes in the quadtree
    pub total_nodes: usize,
    /// Number of leaf nodes
    pub leaf_nodes: usize,
    /// Number of internal nodes
    pub internal_nodes: usize,
    /// Number of empty leaf nodes
    pub empty_nodes: usize,
    /// Maximum depth of the tree
    pub max_depth: usize,
    /// Total number of entities stored
    pub total_entities: usize,
    /// Maximum entities in any single node
    pub max_entities_per_node: usize,
}

impl QuadtreeStats {
    /// Calculates the average entities per leaf node.
    pub fn average_entities_per_leaf(&self) -> f32 {
        if self.leaf_nodes > 0 {
            self.total_entities as f32 / self.leaf_nodes as f32
        } else {
            0.0
        }
    }

    /// Calculates the fill ratio (non-empty nodes / total nodes).
    pub fn fill_ratio(&self) -> f32 {
        if self.total_nodes > 0 {
            (self.total_nodes - self.empty_nodes) as f32 / self.total_nodes as f32
        } else {
            0.0
        }
    }
}

// Implement SpatialCoordinate for common coordinate types
impl SpatialCoordinate for (i32, i32) {
    fn to_spatial_coords(&self) -> (i32, i32) {
        (self.0, self.1)
    }

    fn from_spatial_coords(x: i32, y: i32) -> Self {
        (x, y)
    }
}

impl<T> SpatialCoordinate for crate::coordinates::square::Coordinate<T> {
    fn to_spatial_coords(&self) -> (i32, i32) {
        (self.x, self.y)
    }

    fn from_spatial_coords(x: i32, y: i32) -> Self {
        Self::new(x, y)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::coordinates::square::{Coordinate as SquareCoord, FourConnected};

    #[test]
    fn test_spatial_bounds_creation() {
        let bounds = SpatialBounds::new(0, 0, 100, 100);
        assert_eq!(bounds.width(), 100);
        assert_eq!(bounds.height(), 100);
        assert_eq!(bounds.area(), 10000);
        assert_eq!(bounds.center(), (50, 50));
    }

    #[test]
    fn test_spatial_bounds_contains() {
        let bounds = SpatialBounds::new(10, 10, 50, 50);
        assert!(bounds.contains_point(25, 25));
        assert!(!bounds.contains_point(5, 5));
        assert!(!bounds.contains_point(60, 60));
    }

    #[test]
    fn test_spatial_bounds_intersects() {
        let bounds1 = SpatialBounds::new(0, 0, 50, 50);
        let bounds2 = SpatialBounds::new(25, 25, 75, 75);
        let bounds3 = SpatialBounds::new(100, 100, 150, 150);

        assert!(bounds1.intersects(&bounds2));
        assert!(!bounds1.intersects(&bounds3));
    }

    #[test]
    fn test_spatial_entity_creation() {
        let pos = SquareCoord::<FourConnected>::new(10, 20);
        let entity = SpatialEntity::new(1, pos, 5);
        
        assert_eq!(entity.id, 1);
        assert_eq!(entity.radius, 5);
        
        let bounds = entity.bounds();
        assert_eq!(bounds.center(), (10, 20));
    }

    #[test]
    fn test_quadtree_basic_operations() {
        let bounds = SpatialBounds::new(0, 0, 100, 100);
        let mut quadtree = Quadtree::new(bounds, 4);

        // Insert entities
        let entity1 = SpatialEntity::new(1, SquareCoord::<FourConnected>::new(25, 25), 1);
        let entity2 = SpatialEntity::new(2, SquareCoord::<FourConnected>::new(75, 75), 1);
        
        quadtree.insert(entity1);
        quadtree.insert(entity2);

        // Query all entities
        let all_entities = quadtree.all_entities();
        assert_eq!(all_entities.len(), 2);

        // Query specific region
        let query_bounds = SpatialBounds::new(0, 0, 50, 50);
        let region_entities = quadtree.query_region(&query_bounds);
        assert_eq!(region_entities.len(), 1);
        assert_eq!(region_entities[0].id, 1);
    }

    #[test]
    fn test_quadtree_subdivision() {
        let bounds = SpatialBounds::new(0, 0, 100, 100);
        let mut quadtree = Quadtree::new(bounds, 2); // Low capacity to force subdivision

        // Insert enough entities to trigger subdivision
        for i in 0..10 {
            let entity = SpatialEntity::new(i, SquareCoord::<FourConnected>::new((i * 10) as i32, (i * 10) as i32), 1);
            quadtree.insert(entity);
        }

        let stats = quadtree.stats();
        assert!(stats.max_depth > 0); // Should have subdivided
        assert_eq!(stats.total_entities, 10);
    }

    #[test]
    fn test_quadtree_circular_query() {
        let bounds = SpatialBounds::new(0, 0, 100, 100);
        let mut quadtree = Quadtree::new(bounds, 10);

        // Insert entities in a pattern
        quadtree.insert(SpatialEntity::new(1, SquareCoord::<FourConnected>::new(50, 50), 1)); // Center
        quadtree.insert(SpatialEntity::new(2, SquareCoord::<FourConnected>::new(52, 50), 1)); // Close
        quadtree.insert(SpatialEntity::new(3, SquareCoord::<FourConnected>::new(80, 80), 1)); // Far

        // Query circle around center
        let nearby = quadtree.query_circle(50, 50, 5);
        assert_eq!(nearby.len(), 2); // Should find entities 1 and 2, not 3
    }

    #[test]
    fn test_quadtree_remove() {
        let bounds = SpatialBounds::new(0, 0, 100, 100);
        let mut quadtree = Quadtree::new(bounds, 10);

        let entity1 = SpatialEntity::new(1, SquareCoord::<FourConnected>::new(25, 25), 1);
        let entity2 = SpatialEntity::new(2, SquareCoord::<FourConnected>::new(75, 75), 1);
        
        quadtree.insert(entity1);
        quadtree.insert(entity2);

        // Remove entity
        let removed = quadtree.remove(1);
        assert_eq!(removed.len(), 1);
        assert_eq!(removed[0].id, 1);

        // Verify removal
        let remaining = quadtree.all_entities();
        assert_eq!(remaining.len(), 1);
        assert_eq!(remaining[0].id, 2);
    }

    #[test]
    fn test_quadtree_stats() {
        let bounds = SpatialBounds::new(0, 0, 100, 100);
        let mut quadtree = Quadtree::new(bounds, 5);

        // Insert entities to create interesting stats
        for i in 0..20 {
            let entity = SpatialEntity::new(i, SquareCoord::<FourConnected>::new((i * 5) as i32, (i * 5) as i32), 1);
            quadtree.insert(entity);
        }

        let stats = quadtree.stats();
        assert_eq!(stats.total_entities, 20);
        assert!(stats.average_entities_per_leaf() > 0.0);
        assert!(stats.fill_ratio() > 0.0);
    }
}