TriangulationUtil.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. using FarseerPhysics.Common.Decomposition.CDT;
  32. namespace Poly2Tri.Triangulation
  33. {
  34. /**
  35. * @author Thomas Åhlén, [email protected]
  36. */
  37. public class TriangulationUtil
  38. {
  39. public static double EPSILON = 1e-12;
  40. /// <summary>
  41. /// Requirements:
  42. /// 1. a,b and c form a triangle.
  43. /// 2. a and d is know to be on opposite side of bc
  44. /// <code>
  45. /// a
  46. /// +
  47. /// / \
  48. /// / \
  49. /// b/ \c
  50. /// +-------+
  51. /// / B \
  52. /// / \
  53. /// </code>
  54. /// Facts:
  55. /// d has to be in area B to have a chance to be inside the circle formed by a,b and c
  56. /// d is outside B if orient2d(a,b,d) or orient2d(c,a,d) is CW
  57. /// This preknowledge gives us a way to optimize the incircle test
  58. /// </summary>
  59. /// <param name="pa">triangle point, opposite d</param>
  60. /// <param name="pb">triangle point</param>
  61. /// <param name="pc">triangle point</param>
  62. /// <param name="pd">point opposite a</param>
  63. /// <returns>true if d is inside circle, false if on circle edge</returns>
  64. public static bool SmartIncircle(TriangulationPoint pa, TriangulationPoint pb, TriangulationPoint pc,
  65. TriangulationPoint pd)
  66. {
  67. double pdx = pd.X;
  68. double pdy = pd.Y;
  69. double adx = pa.X - pdx;
  70. double ady = pa.Y - pdy;
  71. double bdx = pb.X - pdx;
  72. double bdy = pb.Y - pdy;
  73. double adxbdy = adx*bdy;
  74. double bdxady = bdx*ady;
  75. double oabd = adxbdy - bdxady;
  76. // oabd = orient2d(pa,pb,pd);
  77. if (oabd <= 0) return false;
  78. double cdx = pc.X - pdx;
  79. double cdy = pc.Y - pdy;
  80. double cdxady = cdx*ady;
  81. double adxcdy = adx*cdy;
  82. double ocad = cdxady - adxcdy;
  83. // ocad = orient2d(pc,pa,pd);
  84. if (ocad <= 0) return false;
  85. double bdxcdy = bdx*cdy;
  86. double cdxbdy = cdx*bdy;
  87. double alift = adx*adx + ady*ady;
  88. double blift = bdx*bdx + bdy*bdy;
  89. double clift = cdx*cdx + cdy*cdy;
  90. double det = alift*(bdxcdy - cdxbdy) + blift*ocad + clift*oabd;
  91. return det > 0;
  92. }
  93. public static bool InScanArea(TriangulationPoint pa, TriangulationPoint pb, TriangulationPoint pc,
  94. TriangulationPoint pd)
  95. {
  96. double pdx = pd.X;
  97. double pdy = pd.Y;
  98. double adx = pa.X - pdx;
  99. double ady = pa.Y - pdy;
  100. double bdx = pb.X - pdx;
  101. double bdy = pb.Y - pdy;
  102. double adxbdy = adx*bdy;
  103. double bdxady = bdx*ady;
  104. double oabd = adxbdy - bdxady;
  105. // oabd = orient2d(pa,pb,pd);
  106. if (oabd <= 0)
  107. {
  108. return false;
  109. }
  110. double cdx = pc.X - pdx;
  111. double cdy = pc.Y - pdy;
  112. double cdxady = cdx*ady;
  113. double adxcdy = adx*cdy;
  114. double ocad = cdxady - adxcdy;
  115. // ocad = orient2d(pc,pa,pd);
  116. if (ocad <= 0)
  117. {
  118. return false;
  119. }
  120. return true;
  121. }
  122. /// Forumla to calculate signed area
  123. /// Positive if CCW
  124. /// Negative if CW
  125. /// 0 if collinear
  126. /// A[P1,P2,P3] = (x1*y2 - y1*x2) + (x2*y3 - y2*x3) + (x3*y1 - y3*x1)
  127. /// = (x1-x3)*(y2-y3) - (y1-y3)*(x2-x3)
  128. public static Orientation Orient2d(TriangulationPoint pa, TriangulationPoint pb, TriangulationPoint pc)
  129. {
  130. double detleft = (pa.X - pc.X)*(pb.Y - pc.Y);
  131. double detright = (pa.Y - pc.Y)*(pb.X - pc.X);
  132. double val = detleft - detright;
  133. if (val > -EPSILON && val < EPSILON)
  134. {
  135. return Orientation.Collinear;
  136. }
  137. else if (val > 0)
  138. {
  139. return Orientation.CCW;
  140. }
  141. return Orientation.CW;
  142. }
  143. }
  144. }