DTSweepContext.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. namespace Veldrid.Common.Poly2Tri.Triangulation.Delaunay.Sweep
  32. {
  33. /**
  34. *
  35. * @author Thomas Åhlén, thahlen@gmail.com
  36. *
  37. */
  38. public class DTSweepContext : TriangulationContext {
  39. // Inital triangle factor, seed triangle will extend 30% of
  40. // PointSet width to both left and right.
  41. private readonly float ALPHA = 0.3f;
  42. public AdvancingFront Front;
  43. public TriangulationPoint Head { get; set; }
  44. public TriangulationPoint Tail { get; set; }
  45. public DTSweepBasin Basin = new DTSweepBasin();
  46. public DTSweepEdgeEvent EdgeEvent = new DTSweepEdgeEvent();
  47. private DTSweepPointComparator _comparator = new DTSweepPointComparator();
  48. public DTSweepContext() {
  49. Clear();
  50. }
  51. public override bool IsDebugEnabled { get {
  52. return base.IsDebugEnabled;
  53. } protected set {
  54. if (value && DebugContext == null) DebugContext = new DTSweepDebugContext(this);
  55. base.IsDebugEnabled = value;
  56. }}
  57. public void RemoveFromList( DelaunayTriangle triangle ) {
  58. Triangles.Remove(triangle);
  59. // TODO: remove all neighbor pointers to this triangle
  60. // for( int i=0; i<3; i++ )
  61. // {
  62. // if( triangle.neighbors[i] != null )
  63. // {
  64. // triangle.neighbors[i].clearNeighbor( triangle );
  65. // }
  66. // }
  67. // triangle.clearNeighbors();
  68. }
  69. public void MeshClean( DelaunayTriangle triangle ) {
  70. MeshCleanReq(triangle);
  71. }
  72. private void MeshCleanReq( DelaunayTriangle triangle ) {
  73. if (triangle != null && !triangle.IsInterior) {
  74. triangle.IsInterior = true;
  75. Triangulatable.AddTriangle(triangle);
  76. for (int i = 0; i < 3; i++)
  77. if (!triangle.EdgeIsConstrained[i])
  78. {
  79. MeshCleanReq(triangle.Neighbors[i]);
  80. }
  81. }
  82. }
  83. public override void Clear() {
  84. base.Clear();
  85. Triangles.Clear();
  86. }
  87. public void AddNode( AdvancingFrontNode node ) {
  88. // Console.WriteLine( "add:" + node.key + ":" + System.identityHashCode(node.key));
  89. // m_nodeTree.put( node.getKey(), node );
  90. Front.AddNode(node);
  91. }
  92. public void RemoveNode( AdvancingFrontNode node ) {
  93. // Console.WriteLine( "remove:" + node.key + ":" + System.identityHashCode(node.key));
  94. // m_nodeTree.delete( node.getKey() );
  95. Front.RemoveNode(node);
  96. }
  97. public AdvancingFrontNode LocateNode( TriangulationPoint point ) {
  98. return Front.LocateNode(point);
  99. }
  100. public void CreateAdvancingFront() {
  101. AdvancingFrontNode head, tail, middle;
  102. // Initial triangle
  103. DelaunayTriangle iTriangle = new DelaunayTriangle(Points[0], Tail, Head);
  104. Triangles.Add(iTriangle);
  105. head = new AdvancingFrontNode(iTriangle.Points[1]);
  106. head.Triangle = iTriangle;
  107. middle = new AdvancingFrontNode(iTriangle.Points[0]);
  108. middle.Triangle = iTriangle;
  109. tail = new AdvancingFrontNode(iTriangle.Points[2]);
  110. Front = new AdvancingFront(head, tail);
  111. Front.AddNode(middle);
  112. // TODO: I think it would be more intuitive if head is middles next and not previous
  113. // so swap head and tail
  114. Front.Head.Next = middle;
  115. middle.Next = Front.Tail;
  116. middle.Prev = Front.Head;
  117. Front.Tail.Prev = middle;
  118. }
  119. /// <summary>
  120. /// Try to map a node to all sides of this triangle that don't have
  121. /// a neighbor.
  122. /// </summary>
  123. public void MapTriangleToNodes( DelaunayTriangle t ) {
  124. for (int i = 0; i < 3; i++)
  125. if (t.Neighbors[i] == null)
  126. {
  127. AdvancingFrontNode n = Front.LocatePoint(t.PointCWFrom(t.Points[i]));
  128. if (n != null) n.Triangle = t;
  129. }
  130. }
  131. public override void PrepareTriangulation( Triangulatable t ) {
  132. base.PrepareTriangulation(t);
  133. double xmax, xmin;
  134. double ymax, ymin;
  135. xmax = xmin = Points[0].X;
  136. ymax = ymin = Points[0].Y;
  137. // Calculate bounds. Should be combined with the sorting
  138. foreach (TriangulationPoint p in Points) {
  139. if (p.X > xmax) xmax = p.X;
  140. if (p.X < xmin) xmin = p.X;
  141. if (p.Y > ymax) ymax = p.Y;
  142. if (p.Y < ymin) ymin = p.Y;
  143. }
  144. double deltaX = ALPHA * (xmax - xmin);
  145. double deltaY = ALPHA * (ymax - ymin);
  146. TriangulationPoint p1 = new TriangulationPoint(xmax + deltaX, ymin - deltaY);
  147. TriangulationPoint p2 = new TriangulationPoint(xmin - deltaX, ymin - deltaY);
  148. Head = p1;
  149. Tail = p2;
  150. // long time = System.nanoTime();
  151. // Sort the points along y-axis
  152. Points.Sort(_comparator);
  153. // logger.info( "Triangulation setup [{}ms]", ( System.nanoTime() - time ) / 1e6 );
  154. }
  155. public void FinalizeTriangulation() {
  156. Triangulatable.AddTriangles(Triangles);
  157. Triangles.Clear();
  158. }
  159. public override TriangulationConstraint NewConstraint( TriangulationPoint a, TriangulationPoint b ) {
  160. return new DTSweepConstraint(a, b);
  161. }
  162. public override TriangulationAlgorithm Algorithm { get { return TriangulationAlgorithm.DTSweep; }}
  163. }
  164. }