Skip to content

Commit

Permalink
some refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
f1r3starter committed Feb 5, 2020
1 parent c01a967 commit bfc6901
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
}
},
"scripts": {
"psalm": "vendor/bin/psalm --config=psalm.xml",
"phpstan": "vendor/bin/phpstan analyse src --level 7",
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-text --coverage-clover=build/coverage.xml"
}
Expand Down
60 changes: 35 additions & 25 deletions src/Structure/KDTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,10 @@ private function insertNode(?NodeInterface $node, PointInterface $point, int $cu
* @param int $dimension
* @param int $cuttingDimension
*
* @return PointInterface|null
* @return PointInterface
*/
private function findMin(?NodeInterface $node, int $dimension, int $cuttingDimension): ?PointInterface
private function findMin(NodeInterface $node, int $dimension, int $cuttingDimension): PointInterface
{
if (null === $node) {
return null;
}

$nextDimension = ($cuttingDimension + 1) % $this->dimensions;
if ($cuttingDimension === $dimension) {
if (null === $node->getLeft()) {
Expand All @@ -169,21 +165,13 @@ private function findMin(?NodeInterface $node, int $dimension, int $cuttingDimen
);
}

$leftMin = $this->findMin($node->getLeft(), $dimension, $nextDimension);
$rightMin = $this->findMin($node->getRight(), $dimension, $nextDimension);
$current = $node->getPoint()->getDAxis($dimension);
$minimums = [
$this->getPointAxis($leftMin, $dimension) => $leftMin,
$this->getPointAxis($rightMin, $dimension) => $rightMin,
$current => $node->getPoint(),
];
$minKey = min(
array_keys(
array_filter($minimums)
)
return $this->chooseMin(
$dimension,
$nextDimension,
$node,
$node->getLeft(),
$node->getRight()
);

return $minimums[$minKey];
}

/**
Expand Down Expand Up @@ -241,11 +229,6 @@ private function rebuildFoundNode(NodeInterface $node, NodeInterface $nextNode,
{
$nextDimension = ($cuttingDimension + 1) % $this->dimensions;
$point = $this->findMin($nextNode, $cuttingDimension, $nextDimension);

if (null === $point) {
return;
}

$node->setPoint($point);
$node->setRight($this->deletePoint($point, $nextNode, $nextDimension));
}
Expand Down Expand Up @@ -300,4 +283,31 @@ private function getAllPoints(?NodeInterface $node, PointsListInterface $pointsL

return $pointsList;
}

/**
* @param int $dimension
* @param int $nextDimension
* @param NodeInterface $currentNode
* @param NodeInterface|null ...$nodes
*
* @return PointInterface
*/
private function chooseMin(int $dimension, int $nextDimension, NodeInterface $currentNode, ?NodeInterface ... $nodes): PointInterface
{
$nodes = array_filter($nodes);

$minAxis = $currentNode->getPoint()->getDAxis($dimension);
$minPoint = $currentNode->getPoint();

foreach ($nodes as $node) {
$currentPoint = $this->findMin($node, $dimension, $nextDimension);
$currentAxis = $this->getPointAxis($currentPoint, $dimension);
if ($currentAxis < $minAxis) {
$minPoint = $currentPoint;
$minAxis = $currentAxis;
}
}

return $minPoint;
}
}

0 comments on commit bfc6901

Please sign in to comment.