VoronoiRegion.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // -----------------------------------------------------------------------
  2. // <copyright file="VoronoiRegion.cs" company="">
  3. // Triangle.NET code by Christian Woltering, http://triangle.codeplex.com/
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. namespace UnityEngine.U2D.Animation.TriangleNet
  7. .Voronoi.Legacy
  8. {
  9. using System;
  10. using System.Collections.Generic;
  11. using Animation.TriangleNet.Topology;
  12. using Animation.TriangleNet.Geometry;
  13. /// <summary>
  14. /// Represents a region in the Voronoi diagram.
  15. /// </summary>
  16. internal class VoronoiRegion
  17. {
  18. int id;
  19. Point generator;
  20. List<Point> vertices;
  21. bool bounded;
  22. // A map (vertex id) -> (neighbor across adjacent edge)
  23. Dictionary<int, VoronoiRegion> neighbors;
  24. /// <summary>
  25. /// Gets the Voronoi region id (which is the same as the generators vertex id).
  26. /// </summary>
  27. public int ID
  28. {
  29. get { return id; }
  30. }
  31. /// <summary>
  32. /// Gets the Voronoi regions generator.
  33. /// </summary>
  34. public Point Generator
  35. {
  36. get { return generator; }
  37. }
  38. /// <summary>
  39. /// Gets the Voronoi vertices on the regions boundary.
  40. /// </summary>
  41. public ICollection<Point> Vertices
  42. {
  43. get { return vertices; }
  44. }
  45. /// <summary>
  46. /// Gets or sets whether the Voronoi region is bounded.
  47. /// </summary>
  48. public bool Bounded
  49. {
  50. get { return bounded; }
  51. set { bounded = value; }
  52. }
  53. public VoronoiRegion(Vertex generator)
  54. {
  55. this.id = generator.id;
  56. this.generator = generator;
  57. this.vertices = new List<Point>();
  58. this.bounded = true;
  59. this.neighbors = new Dictionary<int, VoronoiRegion>();
  60. }
  61. public void Add(Point point)
  62. {
  63. this.vertices.Add(point);
  64. }
  65. public void Add(List<Point> points)
  66. {
  67. this.vertices.AddRange(points);
  68. }
  69. /// <summary>
  70. /// Returns the neighbouring Voronoi region, that lies across the edge starting at
  71. /// given vertex.
  72. /// </summary>
  73. /// <param name="p">Vertex defining an edge of the region.</param>
  74. /// <returns>Neighbouring Voronoi region</returns>
  75. /// <remarks>
  76. /// The edge starting at p is well defined (vertices are ordered counterclockwise).
  77. /// </remarks>
  78. public VoronoiRegion GetNeighbor(Point p)
  79. {
  80. VoronoiRegion neighbor;
  81. if (neighbors.TryGetValue(p.id, out neighbor))
  82. {
  83. return neighbor;
  84. }
  85. return null;
  86. }
  87. internal void AddNeighbor(int id, VoronoiRegion neighbor)
  88. {
  89. this.neighbors.Add(id, neighbor);
  90. }
  91. public override string ToString()
  92. {
  93. return String.Format("R-ID {0}", id);
  94. }
  95. }
  96. }