triangulate.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*************************************************************************/
  2. /* triangulate.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "triangulate.h"
  30. real_t Triangulate::get_area(const Vector<Vector2> &contour)
  31. {
  32. int n = contour.size();
  33. const Vector2 *c=&contour[0];
  34. real_t A=0.0;
  35. for(int p=n-1,q=0; q<n; p=q++)
  36. {
  37. A+= c[p].cross(c[q]);
  38. }
  39. return A*0.5;
  40. }
  41. /*
  42. is_inside_triangle decides if a point P is Inside of the triangle
  43. defined by A, B, C.
  44. */
  45. bool Triangulate::is_inside_triangle(real_t Ax, real_t Ay,
  46. real_t Bx, real_t By,
  47. real_t Cx, real_t Cy,
  48. real_t Px, real_t Py)
  49. {
  50. real_t ax, ay, bx, by, cx, cy, apx, apy, bpx, bpy, cpx, cpy;
  51. real_t cCROSSap, bCROSScp, aCROSSbp;
  52. ax = Cx - Bx; ay = Cy - By;
  53. bx = Ax - Cx; by = Ay - Cy;
  54. cx = Bx - Ax; cy = By - Ay;
  55. apx= Px - Ax; apy= Py - Ay;
  56. bpx= Px - Bx; bpy= Py - By;
  57. cpx= Px - Cx; cpy= Py - Cy;
  58. aCROSSbp = ax*bpy - ay*bpx;
  59. cCROSSap = cx*apy - cy*apx;
  60. bCROSScp = bx*cpy - by*cpx;
  61. return ((aCROSSbp >= 0.0) && (bCROSScp >= 0.0) && (cCROSSap >= 0.0));
  62. };
  63. bool Triangulate::snip(const Vector<Vector2> &p_contour,int u,int v,int w,int n,const Vector<int>& V)
  64. {
  65. int p;
  66. real_t Ax, Ay, Bx, By, Cx, Cy, Px, Py;
  67. const Vector2 *contour=&p_contour[0];
  68. Ax = contour[V[u]].x;
  69. Ay = contour[V[u]].y;
  70. Bx = contour[V[v]].x;
  71. By = contour[V[v]].y;
  72. Cx = contour[V[w]].x;
  73. Cy = contour[V[w]].y;
  74. if ( CMP_EPSILON > (((Bx-Ax)*(Cy-Ay)) - ((By-Ay)*(Cx-Ax))) ) return false;
  75. for (p=0;p<n;p++)
  76. {
  77. if( (p == u) || (p == v) || (p == w) ) continue;
  78. Px = contour[V[p]].x;
  79. Py = contour[V[p]].y;
  80. if (is_inside_triangle(Ax,Ay,Bx,By,Cx,Cy,Px,Py)) return false;
  81. }
  82. return true;
  83. }
  84. bool Triangulate::triangulate(const Vector<Vector2> &contour,Vector<int> &result)
  85. {
  86. /* allocate and initialize list of Vertices in polygon */
  87. int n = contour.size();
  88. if ( n < 3 ) return false;
  89. Vector<int> V;
  90. V.resize(n);
  91. /* we want a counter-clockwise polygon in V */
  92. if ( 0.0 < get_area(contour) )
  93. for (int v=0; v<n; v++) V[v] = v;
  94. else
  95. for(int v=0; v<n; v++) V[v] = (n-1)-v;
  96. int nv = n;
  97. /* remove nv-2 Vertices, creating 1 triangle every time */
  98. int count = 2*nv; /* error detection */
  99. for(int v=nv-1; nv>2; )
  100. {
  101. /* if we loop, it is probably a non-simple polygon */
  102. if (0 >= (count--))
  103. {
  104. //** Triangulate: ERROR - probable bad polygon!
  105. return false;
  106. }
  107. /* three consecutive vertices in current polygon, <u,v,w> */
  108. int u = v ; if (nv <= u) u = 0; /* previous */
  109. v = u+1; if (nv <= v) v = 0; /* new v */
  110. int w = v+1; if (nv <= w) w = 0; /* next */
  111. if ( snip(contour,u,v,w,nv,V) )
  112. {
  113. int a,b,c,s,t;
  114. /* true names of the vertices */
  115. a = V[u]; b = V[v]; c = V[w];
  116. /* output Triangle */
  117. result.push_back( a );
  118. result.push_back( b );
  119. result.push_back( c );
  120. /* remove v from remaining polygon */
  121. for(s=v,t=v+1;t<nv;s++,t++)
  122. V[s] = V[t];
  123. nv--;
  124. /* resest error detection counter */
  125. count = 2*nv;
  126. }
  127. }
  128. return true;
  129. }