StandardVoronoi.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // -----------------------------------------------------------------------
  2. // <copyright file="StandardVoronoi.cs">
  3. // Triangle.NET code by Christian Woltering, http://triangle.codeplex.com/
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. namespace UnityEngine.U2D.Animation.TriangleNet
  7. .Voronoi
  8. {
  9. using System.Collections.Generic;
  10. using Animation.TriangleNet.Geometry;
  11. using Animation.TriangleNet.Tools;
  12. using Animation.TriangleNet.Topology.DCEL;
  13. internal class StandardVoronoi : VoronoiBase
  14. {
  15. public StandardVoronoi(Mesh mesh)
  16. : this(mesh, mesh.bounds, new DefaultVoronoiFactory(), RobustPredicates.Default)
  17. {
  18. }
  19. public StandardVoronoi(Mesh mesh, Rectangle box)
  20. : this(mesh, box, new DefaultVoronoiFactory(), RobustPredicates.Default)
  21. {
  22. }
  23. public StandardVoronoi(Mesh mesh, Rectangle box, IVoronoiFactory factory, IPredicates predicates)
  24. : base(mesh, factory, predicates, true)
  25. {
  26. // We assume the box to be at least as large as the mesh.
  27. box.Expand(mesh.bounds);
  28. // We explicitly told the base constructor to call the Generate method, so
  29. // at this point the basic Voronoi diagram is already created.
  30. PostProcess(box);
  31. }
  32. /// <summary>
  33. /// Compute edge intersections with bounding box.
  34. /// </summary>
  35. private void PostProcess(Rectangle box)
  36. {
  37. foreach (var edge in rays)
  38. {
  39. // The vertices of the infinite edge.
  40. var v1 = (Point)edge.origin;
  41. var v2 = (Point)edge.twin.origin;
  42. if (box.Contains(v1) || box.Contains(v2))
  43. {
  44. // Move infinite vertex v2 onto the box boundary.
  45. IntersectionHelper.BoxRayIntersection(box, v1, v2, ref v2);
  46. }
  47. else
  48. {
  49. // There is actually no easy way to handle the second case. The two edges
  50. // leaving v1, pointing towards the mesh, don't have to intersect the box
  51. // (the could join with edges of other cells outside the box).
  52. // A general intersection algorithm (DCEL <-> Rectangle) is needed, which
  53. // computes intersections with all edges and discards objects outside the
  54. // box.
  55. }
  56. }
  57. }
  58. }
  59. }