AdvancingFront.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. /// Removed BST code, but not all artifacts of it
  33. /// Future possibilities
  34. /// Eliminate Add/RemoveNode ?
  35. /// Comments comments and more comments!
  36. using System.Text;
  37. using System;
  38. namespace Veldrid.Common.Poly2Tri.Triangulation.Delaunay.Sweep
  39. {
  40. /**
  41. * @author Thomas Åhlen (thahlen@gmail.com)
  42. */
  43. public class AdvancingFront {
  44. public AdvancingFrontNode Head;
  45. public AdvancingFrontNode Tail;
  46. protected AdvancingFrontNode Search;
  47. public AdvancingFront( AdvancingFrontNode head, AdvancingFrontNode tail ) {
  48. this.Head = head;
  49. this.Tail = tail;
  50. this.Search = head;
  51. AddNode(head);
  52. AddNode(tail);
  53. }
  54. public void AddNode( AdvancingFrontNode node ) { }
  55. public void RemoveNode( AdvancingFrontNode node ) { }
  56. public override string ToString() {
  57. StringBuilder sb = new StringBuilder();
  58. AdvancingFrontNode node = Head;
  59. while (node != Tail) {
  60. sb.Append(node.Point.X).Append("->");
  61. node = node.Next;
  62. }
  63. sb.Append(Tail.Point.X);
  64. return sb.ToString();
  65. }
  66. /// <summary>
  67. /// MM: This seems to be used by LocateNode to guess a position in the implicit linked list of AdvancingFrontNodes near x
  68. /// Removed an overload that depended on this being exact
  69. /// </summary>
  70. private AdvancingFrontNode FindSearchNode( double x ) {
  71. return Search;
  72. }
  73. /// <summary>
  74. /// We use a balancing tree to locate a node smaller or equal to given key value (in theory)
  75. /// </summary>
  76. public AdvancingFrontNode LocateNode( TriangulationPoint point ) {
  77. return LocateNode(point.X);
  78. }
  79. private AdvancingFrontNode LocateNode( double x ) {
  80. AdvancingFrontNode node = FindSearchNode(x);
  81. if (x < node.Value) {
  82. while ((node = node.Prev) != null)
  83. if (x >= node.Value) {
  84. Search = node;
  85. return node;
  86. }
  87. } else {
  88. while ((node = node.Next) != null)
  89. if (x < node.Value) {
  90. Search = node.Prev;
  91. return node.Prev;
  92. }
  93. }
  94. return null;
  95. }
  96. /// <summary>
  97. /// This implementation will use simple node traversal algorithm to find a point on the front
  98. /// </summary>
  99. public AdvancingFrontNode LocatePoint( TriangulationPoint point ) {
  100. double px = point.X;
  101. AdvancingFrontNode node = FindSearchNode(px);
  102. double nx = node.Point.X;
  103. if (px == nx) {
  104. if (point != node.Point) {
  105. // We might have two nodes with same x value for a short time
  106. if (point == node.Prev.Point) {
  107. node = node.Prev;
  108. } else if (point == node.Next.Point) {
  109. node = node.Next;
  110. } else {
  111. throw new Exception("Failed to find Node for given afront point");
  112. }
  113. }
  114. } else if (px < nx) {
  115. while ((node = node.Prev) != null) if (point == node.Point) break;
  116. } else {
  117. while ((node = node.Next) != null) if (point == node.Point) break;
  118. }
  119. Search = node;
  120. return node;
  121. }
  122. }
  123. }