DelaunayTriangle.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* Poly2Tri
  2. * Copyright (c) 2009-2010, Poly2Tri Contributors
  3. * http://code.google.com/p/poly2tri/
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted provided that the following conditions are met:
  9. *
  10. * * Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * * Neither the name of Poly2Tri nor the names of its contributors may be
  16. * used to endorse or promote products derived from this software without specific
  17. * prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /// Changes from the Java version
  32. /// attributification
  33. /// Future possibilities
  34. /// Flattening out the number of indirections
  35. /// Replacing arrays of 3 with fixed-length arrays?
  36. /// Replacing bool[3] with a bit array of some sort?
  37. /// Bundling everything into an AoS mess?
  38. /// Hardcode them all as ABC ?
  39. using System;
  40. using System.Diagnostics;
  41. using System.Collections.Generic;
  42. using Veldrid.Common.Poly2Tri.Utility;
  43. using Veldrid.Common.Poly2Tri.Triangulation.Delaunay.Sweep;
  44. namespace Veldrid.Common.Poly2Tri.Triangulation.Delaunay
  45. {
  46. public class DelaunayTriangle {
  47. public FixedArray3<TriangulationPoint> Points;
  48. public FixedArray3<DelaunayTriangle > Neighbors;
  49. public FixedBitArray3 EdgeIsConstrained, EdgeIsDelaunay;
  50. public bool IsInterior { get; set; }
  51. public DelaunayTriangle(TriangulationPoint p1, TriangulationPoint p2, TriangulationPoint p3) {
  52. Points[0] = p1;
  53. Points[1] = p2;
  54. Points[2] = p3;
  55. }
  56. public int IndexOf(TriangulationPoint p) {
  57. int i = Points.IndexOf(p);
  58. if (i==-1) throw new Exception("Calling index with a point that doesn't exist in triangle");
  59. return i;
  60. }
  61. public int IndexCWFrom (TriangulationPoint p) { return (IndexOf(p)+2)%3; }
  62. public int IndexCCWFrom(TriangulationPoint p) { return (IndexOf(p)+1)%3; }
  63. public bool Contains(TriangulationPoint p) { return Points.Contains(p); }
  64. /// <summary>
  65. /// Update neighbor pointers
  66. /// </summary>
  67. /// <param name="p1">Point 1 of the shared edge</param>
  68. /// <param name="p2">Point 2 of the shared edge</param>
  69. /// <param name="t">This triangle's new neighbor</param>
  70. private void MarkNeighbor( TriangulationPoint p1, TriangulationPoint p2, DelaunayTriangle t ) {
  71. int i = EdgeIndex(p1,p2);
  72. if ( i==-1 ) throw new Exception( "Error marking neighbors -- t doesn't contain edge p1-p2!" );
  73. Neighbors[i] = t;
  74. }
  75. /// <summary>
  76. /// Exhaustive search to update neighbor pointers
  77. /// </summary>
  78. public void MarkNeighbor( DelaunayTriangle t ) {
  79. // Points of this triangle also belonging to t
  80. bool a = t.Contains(Points[0]);
  81. bool b = t.Contains(Points[1]);
  82. bool c = t.Contains(Points[2]);
  83. if (b&&c) { Neighbors[0]=t; t.MarkNeighbor(Points[1],Points[2],this); }
  84. else if (a&&c) { Neighbors[1]=t; t.MarkNeighbor(Points[0],Points[2],this); }
  85. else if (a&&b) { Neighbors[2]=t; t.MarkNeighbor(Points[0],Points[1],this); }
  86. else throw new Exception( "Failed to mark neighbor, doesn't share an edge!");
  87. }
  88. /// <param name="t">Opposite triangle</param>
  89. /// <param name="p">The point in t that isn't shared between the triangles</param>
  90. public TriangulationPoint OppositePoint(DelaunayTriangle t, TriangulationPoint p) {
  91. Debug.Assert(t != this, "self-pointer error");
  92. return PointCWFrom(t.PointCWFrom(p));
  93. }
  94. public DelaunayTriangle NeighborCWFrom (TriangulationPoint point) { return Neighbors[(Points.IndexOf(point)+1)%3]; }
  95. public DelaunayTriangle NeighborCCWFrom (TriangulationPoint point) { return Neighbors[(Points.IndexOf(point)+2)%3]; }
  96. public DelaunayTriangle NeighborAcrossFrom(TriangulationPoint point) { return Neighbors[ Points.IndexOf(point) ]; }
  97. public TriangulationPoint PointCCWFrom(TriangulationPoint point) { return Points[(IndexOf(point)+1)%3]; }
  98. public TriangulationPoint PointCWFrom (TriangulationPoint point) { return Points[(IndexOf(point)+2)%3]; }
  99. private void RotateCW() {
  100. var t = Points[2];
  101. Points[2] = Points[1];
  102. Points[1] = Points[0];
  103. Points[0] = t;
  104. }
  105. /// <summary>
  106. /// Legalize triangle by rotating clockwise around oPoint
  107. /// </summary>
  108. /// <param name="oPoint">The origin point to rotate around</param>
  109. /// <param name="nPoint">???</param>
  110. public void Legalize(TriangulationPoint oPoint, TriangulationPoint nPoint) {
  111. RotateCW();
  112. Points[IndexCCWFrom(oPoint)] = nPoint;
  113. }
  114. public override string ToString() { return Points[0] + "," + Points[1] + "," + Points[2]; }
  115. /// <summary>
  116. /// Finalize edge marking
  117. /// </summary>
  118. public void MarkNeighborEdges() {
  119. for (int i = 0; i < 3; i++) if ( EdgeIsConstrained[i] && Neighbors[i] != null ) {
  120. Neighbors[i].MarkConstrainedEdge(Points[(i+1)%3], Points[(i+2)%3]);
  121. }
  122. }
  123. public void MarkEdge(DelaunayTriangle triangle) {
  124. for (int i = 0; i < 3; i++) if ( EdgeIsConstrained[i] ) {
  125. triangle.MarkConstrainedEdge(Points[(i+1)%3], Points[(i+2)%3]);
  126. }
  127. }
  128. public void MarkEdge(List<DelaunayTriangle> tList) {
  129. foreach ( DelaunayTriangle t in tList )
  130. for ( int i = 0; i < 3; i++ )
  131. if ( t.EdgeIsConstrained[i] )
  132. {
  133. MarkConstrainedEdge( t.Points[(i+1)%3], t.Points[(i+2)%3] );
  134. }
  135. }
  136. public void MarkConstrainedEdge(int index) {
  137. EdgeIsConstrained[index] = true;
  138. }
  139. public void MarkConstrainedEdge(DTSweepConstraint edge) {
  140. MarkConstrainedEdge(edge.P, edge.Q);
  141. }
  142. /// <summary>
  143. /// Mark edge as constrained
  144. /// </summary>
  145. public void MarkConstrainedEdge(TriangulationPoint p, TriangulationPoint q) {
  146. int i = EdgeIndex(p,q);
  147. if ( i != -1 ) EdgeIsConstrained[i] = true;
  148. }
  149. public double Area() {
  150. double b = Points[0].X - Points[1].X;
  151. double h = Points[2].Y - Points[1].Y;
  152. return Math.Abs((b * h * 0.5f));
  153. }
  154. public TriangulationPoint Centroid() {
  155. double cx = (Points[0].X + Points[1].X + Points[2].X) / 3f;
  156. double cy = (Points[0].Y + Points[1].Y + Points[2].Y) / 3f;
  157. return new TriangulationPoint(cx, cy);
  158. }
  159. /// <summary>
  160. /// Get the index of the neighbor that shares this edge (or -1 if it isn't shared)
  161. /// </summary>
  162. /// <returns>index of the shared edge or -1 if edge isn't shared</returns>
  163. public int EdgeIndex(TriangulationPoint p1, TriangulationPoint p2) {
  164. int i1 = Points.IndexOf(p1);
  165. int i2 = Points.IndexOf(p2);
  166. // Points of this triangle in the edge p1-p2
  167. bool a = (i1==0 || i2==0);
  168. bool b = (i1==1 || i2==1);
  169. bool c = (i1==2 || i2==2);
  170. if (b&&c) return 0;
  171. if (a&&c) return 1;
  172. if (a&&b) return 2;
  173. return -1;
  174. }
  175. public bool GetConstrainedEdgeCCW ( TriangulationPoint p ) { return EdgeIsConstrained[(IndexOf(p)+2)%3]; }
  176. public bool GetConstrainedEdgeCW ( TriangulationPoint p ) { return EdgeIsConstrained[(IndexOf(p)+1)%3]; }
  177. public bool GetConstrainedEdgeAcross( TriangulationPoint p ) { return EdgeIsConstrained[ IndexOf(p) ]; }
  178. public void SetConstrainedEdgeCCW ( TriangulationPoint p, bool ce ) { EdgeIsConstrained[(IndexOf(p)+2)%3] = ce; }
  179. public void SetConstrainedEdgeCW ( TriangulationPoint p, bool ce ) { EdgeIsConstrained[(IndexOf(p)+1)%3] = ce; }
  180. public void SetConstrainedEdgeAcross( TriangulationPoint p, bool ce ) { EdgeIsConstrained[ IndexOf(p) ] = ce; }
  181. public bool GetDelaunayEdgeCCW ( TriangulationPoint p ) { return EdgeIsDelaunay[(IndexOf(p)+2)%3]; }
  182. public bool GetDelaunayEdgeCW ( TriangulationPoint p ) { return EdgeIsDelaunay[(IndexOf(p)+1)%3]; }
  183. public bool GetDelaunayEdgeAcross( TriangulationPoint p ) { return EdgeIsDelaunay[ IndexOf(p) ]; }
  184. public void SetDelaunayEdgeCCW ( TriangulationPoint p, bool ce ) { EdgeIsDelaunay[(IndexOf(p)+2)%3] = ce; }
  185. public void SetDelaunayEdgeCW ( TriangulationPoint p, bool ce ) { EdgeIsDelaunay[(IndexOf(p)+1)%3] = ce; }
  186. public void SetDelaunayEdgeAcross( TriangulationPoint p, bool ce ) { EdgeIsDelaunay[ IndexOf(p) ] = ce; }
  187. }
  188. }