handle nearest neighbour not found

This commit is contained in:
BobLd
2020-03-09 11:10:33 +00:00
committed by Eliot Jones
parent aa9df30722
commit 8cafda3577
2 changed files with 17 additions and 5 deletions

View File

@@ -63,9 +63,12 @@
{
var paired = kdTree.FindNearestNeighbours(pivot, pivotPoint, distMeasure, out int index, out double dist);
if (filterFinal(pivot, paired) && dist < maxDistanceFunction(pivot, paired))
if (index != -1)
{
indexes[e] = index;
if (filterFinal(pivot, paired) && dist < maxDistanceFunction(pivot, paired))
{
indexes[e] = index;
}
}
}
});

View File

@@ -66,12 +66,21 @@
}
#region NN
/// <summary>
///
/// </summary>
/// <param name="pivot"></param>
/// <param name="pivotPointFunc"></param>
/// <param name="distanceMeasure"></param>
/// <param name="index">The nearest neighbour's index (returns -1 if not found).</param>
/// <param name="distance">The distance between the pivot and the nearest neighbour (returns <see cref="double.NaN"/> if not found).</param>
/// <returns>The nearest neighbour's element.</returns>
public T FindNearestNeighbours(T pivot, Func<T, PdfPoint> pivotPointFunc, Func<PdfPoint, PdfPoint, double> distanceMeasure, out int index, out double distance)
{
var result = FindNearestNeighbours(Root, pivot, pivotPointFunc, distanceMeasure);
index = result.Item1.Index;
distance = result.Item2.Value;
return result.Item1.Element;
index = result.Item1 != null ? result.Item1.Index : -1;
distance = result.Item2.HasValue ? result.Item2.Value : double.NaN;
return result.Item1 != null ? result.Item1.Element : default;
}
private static (KdTreeNode<T>, double?) FindNearestNeighbours(KdTreeNode<T> node, T pivot, Func<T, PdfPoint> pivotPointFunc, Func<PdfPoint, PdfPoint, double> distance)