Triangle.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // -----------------------------------------------------------------------
  2. // <copyright file="Triangle.cs" company="">
  3. // Original Triangle code by Jonathan Richard Shewchuk, http://www.cs.cmu.edu/~quake/triangle.html
  4. // Triangle.NET code by Christian Woltering, http://triangle.codeplex.com/
  5. // </copyright>
  6. // -----------------------------------------------------------------------
  7. namespace UnityEngine.U2D.Animation.TriangleNet
  8. .Topology
  9. {
  10. using System;
  11. using Animation.TriangleNet.Geometry;
  12. /// <summary>
  13. /// The triangle data structure.
  14. /// </summary>
  15. internal class Triangle : ITriangle
  16. {
  17. // Hash for dictionary. Will be set by mesh instance.
  18. internal int hash;
  19. // The ID is only used for mesh output.
  20. internal int id;
  21. internal Otri[] neighbors;
  22. internal Vertex[] vertices;
  23. internal Osub[] subsegs;
  24. internal int label;
  25. internal double area;
  26. internal bool infected;
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="Triangle" /> class.
  29. /// </summary>
  30. public Triangle()
  31. {
  32. // Three NULL vertices.
  33. vertices = new Vertex[3];
  34. // Initialize the three adjoining subsegments to be the omnipresent subsegment.
  35. subsegs = new Osub[3];
  36. // Initialize the three adjoining triangles to be "outer space".
  37. neighbors = new Otri[3];
  38. // area = -1.0;
  39. }
  40. #region Public properties
  41. /// <summary>
  42. /// Gets or sets the triangle id.
  43. /// </summary>
  44. public int ID
  45. {
  46. get { return this.id; }
  47. set { this.id = value; }
  48. }
  49. /// <summary>
  50. /// Region ID the triangle belongs to.
  51. /// </summary>
  52. public int Label
  53. {
  54. get { return this.label; }
  55. set { this.label = value; }
  56. }
  57. /// <summary>
  58. /// Gets the triangle area constraint.
  59. /// </summary>
  60. public double Area
  61. {
  62. get { return this.area; }
  63. set { this.area = value; }
  64. }
  65. /// <summary>
  66. /// Gets the specified corners vertex.
  67. /// </summary>
  68. public Vertex GetVertex(int index)
  69. {
  70. return this.vertices[index]; // TODO: Check range?
  71. }
  72. public int GetVertexID(int index)
  73. {
  74. return this.vertices[index].id;
  75. }
  76. /// <summary>
  77. /// Gets a triangles' neighbor.
  78. /// </summary>
  79. /// <param name="index">The neighbor index (0, 1 or 2).</param>
  80. /// <returns>The neigbbor opposite of vertex with given index.</returns>
  81. public ITriangle GetNeighbor(int index)
  82. {
  83. return neighbors[index].tri.hash == Mesh.DUMMY ? null : neighbors[index].tri;
  84. }
  85. /// <inheritdoc />
  86. public int GetNeighborID(int index)
  87. {
  88. return neighbors[index].tri.hash == Mesh.DUMMY ? -1 : neighbors[index].tri.id;
  89. }
  90. /// <summary>
  91. /// Gets a triangles segment.
  92. /// </summary>
  93. /// <param name="index">The vertex index (0, 1 or 2).</param>
  94. /// <returns>The segment opposite of vertex with given index.</returns>
  95. public ISegment GetSegment(int index)
  96. {
  97. return subsegs[index].seg.hash == Mesh.DUMMY ? null : subsegs[index].seg;
  98. }
  99. #endregion
  100. public override int GetHashCode()
  101. {
  102. return this.hash;
  103. }
  104. public override string ToString()
  105. {
  106. return String.Format("TID {0}", hash);
  107. }
  108. }
  109. }