You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mapbox-sdk/Unity/MeshGeneration/Data/KdTree/DistanceFunctions.cs

78 lines
2.9 KiB

6 months ago

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace KDTree
{
/// <summary>
/// An interface which enables flexible distance functions.
/// </summary>
public interface DistanceFunctions
{
/// <summary>
/// Compute a distance between two n-dimensional points.
/// </summary>
/// <param name="p1">The first point.</param>
/// <param name="p2">The second point.</param>
/// <returns>The n-dimensional distance.</returns>
double Distance(double[] p1, double[] p2);
/// <summary>
/// Find the shortest distance from a point to an axis aligned rectangle in n-dimensional space.
/// </summary>
/// <param name="point">The point of interest.</param>
/// <param name="min">The minimum coordinate of the rectangle.</param>
/// <param name="max">The maximum coorindate of the rectangle.</param>
/// <returns>The shortest n-dimensional distance between the point and rectangle.</returns>
double DistanceToRectangle(double[] point, double[] min, double[] max);
}
/// <summary>
/// A distance function for our KD-Tree which returns squared euclidean distances.
/// </summary>
public class SquareEuclideanDistanceFunction : DistanceFunctions
{
/// <summary>
/// Find the squared distance between two n-dimensional points.
/// </summary>
/// <param name="p1">The first point.</param>
/// <param name="p2">The second point.</param>
/// <returns>The n-dimensional squared distance.</returns>
public double Distance(double[] p1, double[] p2)
{
double fSum = 0;
for (int i = 0; i < p1.Length; i++)
{
double fDifference = (p1[i] - p2[i]);
fSum += fDifference * fDifference;
}
return fSum;
}
/// <summary>
/// Find the shortest distance from a point to an axis aligned rectangle in n-dimensional space.
/// </summary>
/// <param name="point">The point of interest.</param>
/// <param name="min">The minimum coordinate of the rectangle.</param>
/// <param name="max">The maximum coorindate of the rectangle.</param>
/// <returns>The shortest squared n-dimensional squared distance between the point and rectangle.</returns>
public double DistanceToRectangle(double[] point, double[] min, double[] max)
{
double fSum = 0;
double fDifference = 0;
for (int i = 0; i < point.Length; ++i)
{
fDifference = 0;
if (point[i] > max[i])
fDifference = (point[i] - max[i]);
else if (point[i] < min[i])
fDifference = (point[i] - min[i]);
fSum += fDifference * fDifference;
}
return fSum;
}
}
}