Skip to content

Commit

Permalink
Infinit recursion fix
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsanger committed Oct 28, 2014
1 parent 7af7ae0 commit 9c76c6c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Quadtree/QuadtreeAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ abstract class QuadtreeAbstract

/** @var Quadtree */
private $se;

/** @var boolean */
private $overlappingInserted = FALSE;

/**
* @param \Quadtree\ICollisionDetector $detector
Expand Down Expand Up @@ -89,6 +92,12 @@ public function collideWithItems(Insertable $item)
*/
private function insertItem(Insertable $item)
{
if ($this->overlappingInserted || $item->getBounds()->contains($this->bounds)) {
$this->items[] = $item;
$this->overlappingInserted = true;
return TRUE;
}

if ($this->nw === NULL && count($this->items) < $this->capacity) {
$this->items[] = $item;
return TRUE;
Expand Down
10 changes: 10 additions & 0 deletions tests/QuadtreeOverlaping/BlackHoleQuadtree.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

class BlackHoleQuadtree extends \Quadtree\QuadtreeAbstract
{
public function __construct(\Quadtree\Geometry\Bounds $bounds, $leafCapacity)
{
$detector = new \DummyCollisionDetector();
parent::__construct($detector, $bounds, $leafCapacity);
}
}
15 changes: 15 additions & 0 deletions tests/QuadtreeOverlaping/DummyCollistionDetector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

class DummyCollisionDetector implements \Quadtree\ICollisionDetector
{
public function collide(array $insertedItems, \Quadtree\Insertable $item)
{
return FALSE;
}

public function intersects(\Quadtree\Geometry\Bounds $bounds, \Quadtree\Insertable $item)
{
return $bounds->intersects($item->getBounds());
}
}

18 changes: 18 additions & 0 deletions tests/QuadtreeOverlaping/Overlapping.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

require __DIR__ . '/../bootstrap.php';
require './DummyCollistionDetector.php';
require './BlackHoleQuadtree.php';

use Tester\Assert;
use Quadtree\Quadtree;
use Quadtree\Geometry\Bounds;

$qt = new BlackHoleQuadtree(new Bounds(100, 100), 2);
Assert::true($qt->insert(new Bounds(100, 100)));
Assert::true($qt->insert(new Bounds(100, 100)));
Assert::true($qt->insert(new Bounds(100, 100)));

Assert::true($qt->insert(new Bounds(10, 10)));
Assert::true($qt->insert(new Bounds(10, 10)));
Assert::true($qt->insert(new Bounds(10, 10)));

0 comments on commit 9c76c6c

Please sign in to comment.