ListSpatialIndex.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace Dbscan
  4. {
  5. /// <summary>
  6. /// An implementation of the <see cref="T:Dbscan.ISpatialIndex`1" /> using a simple <see cref="T:System.Collections.Generic.List`1" />
  7. /// to hold the elements and a linear search of all items to <see cref="M:Dbscan.ListSpatialIndex`1.Search(Dbscan.IPointData@,System.Double)" />
  8. /// for nearby items.
  9. /// </summary>
  10. /// <typeparam name="T">The type of items in the index.</typeparam>
  11. public class ListSpatialIndex<T> : ISpatialIndex<T> where T : IPointData
  12. {
  13. private readonly IReadOnlyList<T> _list;
  14. private readonly DistanceFunction _distanceFunction;
  15. /// <summary>
  16. /// Initializes a <see cref="T:Dbscan.ListSpatialIndex`1" /> with a collection of data, using the
  17. /// Euclidean distance between two points as the distance function to search for points
  18. /// in a given neighborhood.
  19. /// </summary>
  20. /// <param name="data">The collection of data to put into the index</param>
  21. public ListSpatialIndex(IEnumerable<T> data)
  22. : this(data, (DistanceFunction)DistanceFunctions.EuclideanDistance)
  23. {
  24. }
  25. /// <summary>
  26. /// Initializes a <see cref="T:Dbscan.ListSpatialIndex`1" /> with a collection of data, using the
  27. /// specified distanct function to search for points in a given neighborhood.
  28. /// </summary>
  29. /// <param name="data">The collection of data to put into the index</param>
  30. /// <param name="distanceFunction">The function used to determine if a point is within a specified distance of a given point.</param>
  31. public ListSpatialIndex(IEnumerable<T> data, DistanceFunction distanceFunction)
  32. {
  33. _list = data.ToList();
  34. _distanceFunction = distanceFunction;
  35. }
  36. /// <summary>
  37. /// Get all of the elements within the current <see cref="T:Dbscan.ListSpatialIndex`1" />.
  38. /// </summary>
  39. /// <returns>
  40. /// A list of every element contained in the <see cref="T:Dbscan.ListSpatialIndex`1" />.
  41. /// </returns>
  42. public IReadOnlyList<T> Search()
  43. {
  44. return _list;
  45. }
  46. /// <summary>
  47. /// Get all of the elements from this <see cref="T:Dbscan.ListSpatialIndex`1" />
  48. /// within a circle centered at the point <see cref="P:Dbscan.IPointData.Point" />
  49. /// with a radius of <paramref name="epsilon" />.
  50. /// </summary>
  51. /// <param name="p">The center of the search circle.</param>
  52. /// <param name="epsilon">The radius of the search circle.</param>
  53. /// <returns>
  54. /// A list of the points that are within the search area.
  55. /// </returns>
  56. public IReadOnlyList<T> Search(in IPointData p, double epsilon)
  57. {
  58. List<T> list = new List<T>();
  59. foreach (T item in _list)
  60. {
  61. DistanceFunction distanceFunction = _distanceFunction;
  62. IPointData p2 = item;
  63. if (distanceFunction(in p, in p2) < epsilon)
  64. {
  65. list.Add(item);
  66. }
  67. }
  68. return list;
  69. }
  70. IReadOnlyList<T> ISpatialIndex<T>.Search(in IPointData p, double epsilon)
  71. {
  72. return Search(in p, epsilon);
  73. }
  74. }
  75. }