Skip to content

Commit

Permalink
Issue: #9 (KNN misses few neighborer nodes)
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeta17 committed Nov 26, 2023
1 parent f9b1a2e commit 6dba0d3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
12 changes: 7 additions & 5 deletions octree.h
Original file line number Diff line number Diff line change
Expand Up @@ -1753,7 +1753,7 @@ namespace OrthoTree

private: // K Nearest Neighbor helpers

static geometry_type getBoxWallDistanceMax(vector_type const& pt, box_type const& box) noexcept
static geometry_type getBoxWallDistance(vector_type const& pt, box_type const& box) noexcept
{
autoc& ptMin = AD::box_min_c(box);
autoc& ptMax = AD::box_max_c(box);
Expand All @@ -1771,7 +1771,7 @@ namespace OrthoTree
return 0.0;
}

return *std::min_element(begin(vDist), end(vDist));
return *std::ranges::min_element(vDist);
}


Expand Down Expand Up @@ -1807,9 +1807,10 @@ namespace OrthoTree
if (base::IsValidKey(kSmallestNode))
{
autoc& nodeSmallest = cont_at(this->m_nodes, kSmallestNode);
autoc wallDist = getBoxWallDistance(pt, nodeSmallest.box);
createEntityDistance(nodeSmallest, pt, vpt, setEntity);
if (!nodeSmallest.IsAnyChildExist())
if (getFarestDistance(setEntity, k) < getBoxWallDistanceMax(pt, nodeSmallest.box))
if (getFarestDistance(setEntity, k) < wallDist)
return convertEntityDistanceToList(setEntity, k);
}

Expand Down Expand Up @@ -1837,15 +1838,16 @@ namespace OrthoTree

if (!setNodeDist.empty())
{
auto rLatestNodeDist = std::begin(setNodeDist)->distance;
auto rLatestNodeDist = setEntity.size() > k ? next(setEntity.begin(), k)->distance : std::numeric_limits<geometry_type>::max();
for (autoc& nodeDist : setNodeDist)
{
autoc n = setEntity.size();
if (k <= n && rLatestNodeDist < nodeDist.distance)
break;

createEntityDistance(nodeDist.node, pt, vpt, setEntity);
rLatestNodeDist = nodeDist.distance;
if (setEntity.size() > k)
rLatestNodeDist = next(setEntity.begin(), k)->distance;
}
}

Expand Down
50 changes: 50 additions & 0 deletions unittests/general.tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1709,6 +1709,56 @@ namespace Tree2DTest
Assert::IsTrue(std::ranges::is_permutation(vector<size_t>{0, 1, 5, 6, 11}, vnn));
}


TEST_METHOD(Issue9)
{
autoc poses = vector<array<double, 2>>
{
{ 78.2619, 77.843 },
{ 90.3005, 90.5172 },
{ 69.8652, 12.2467 },
{ 48.4675, 48.4948 },
{ 36.3226, 68.4619 },
{ 98.8799, 42.7149 },
{ 31.412, 38.6866 },
{ 63.2748, 77.0524 },
{ 62.8844, 17.0536 },
{ 80.8931, 39.8099 },
{ 77.426, 64.9844 },
{ 81.9552, 25.009 },
{ 87.6088, 51.319 },
{ 78.5609, 80.4623 },
{ 51.3967, 39.5269 },
{ 32.2042, 81.8779 },
{ 79.1739, 81.5467 },
{ 95.2619, 40.4742 },
{ 86.437, 92.4406 },
{ 3.95388, 60.2327 },
{ 31.1283, 44.4917 },
{ 35.6778, 79.8545 },
{ 50.9926, 66.1373 },
{ 3.16271, 65.2519 },
{ 56.3665, 45.3819 }
};


autoce search_point = array<double, 2>{ 43.6406, 57.5691 };
using AD = OrthoTree::AdaptorGeneral<2, array<double, 2>, OrthoTree::BoundingBox2D>;
autoc itMin = std::ranges::min_element(poses, [&search_point](autoc& lhs, autoc& rhs) { return AD::distance2(lhs, search_point) < AD::distance2(rhs, search_point); });

std::array<double, 2> inspection_space_min = { 0.0, 0.0 };
std::array<double, 2> inspection_space_max = { 100.0, 100.0 };
OrthoTree::BoundingBox2D inspection_space;
inspection_space.Min = inspection_space_min;
inspection_space.Max = inspection_space_max;
//Standard Tree
auto tree = QuadtreePointC(poses, 9, inspection_space);

auto neighbors = tree.GetNearestNeighbors(search_point, 1);
Assert::AreEqual<size_t>(std::distance(poses.begin(), itMin), neighbors[0]);
}


};

TEST_CLASS(Box_General)
Expand Down

0 comments on commit 6dba0d3

Please sign in to comment.