VoxelPolygonClipper.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. namespace Pathfinding.Voxels {
  2. /// <summary>Utility for clipping polygons</summary>
  3. internal struct VoxelPolygonClipper {
  4. public float[] x;
  5. public float[] y;
  6. public float[] z;
  7. public int n;
  8. public VoxelPolygonClipper (int capacity) {
  9. x = new float[capacity];
  10. y = new float[capacity];
  11. z = new float[capacity];
  12. n = 0;
  13. }
  14. public UnityEngine.Vector3 this[int i] {
  15. set {
  16. x[i] = value.x;
  17. y[i] = value.y;
  18. z[i] = value.z;
  19. }
  20. }
  21. /// <summary>
  22. /// Clips a polygon against an axis aligned half plane.
  23. /// The polygons stored in this object are clipped against the half plane at x = -offset.
  24. /// </summary>
  25. /// <param name="result">Ouput vertices</param>
  26. /// <param name="multi">Scale factor for the input vertices. Should be +1 or -1. If -1 the negative half plane is kept.</param>
  27. /// <param name="offset">Offset to move the input vertices with before cutting</param>
  28. public void ClipPolygonAlongX (ref VoxelPolygonClipper result, float multi, float offset) {
  29. // Number of resulting vertices
  30. int m = 0;
  31. bool prev, curr;
  32. float dj = multi*x[(n-1)]+offset;
  33. for (int i = 0, j = n-1; i < n; j = i, i++) {
  34. float di = multi*x[i]+offset;
  35. prev = dj >= 0;
  36. curr = di >= 0;
  37. if (prev != curr) {
  38. float s = dj / (dj - di);
  39. result.x[m] = x[j] + (x[i]-x[j])*s;
  40. result.y[m] = y[j] + (y[i]-y[j])*s;
  41. result.z[m] = z[j] + (z[i]-z[j])*s;
  42. m++;
  43. }
  44. if (curr) {
  45. result.x[m] = x[i];
  46. result.y[m] = y[i];
  47. result.z[m] = z[i];
  48. m++;
  49. }
  50. dj = di;
  51. }
  52. result.n = m;
  53. }
  54. /// <summary>
  55. /// Clips a polygon against an axis aligned half plane.
  56. /// The polygons stored in this object are clipped against the half plane at z = -offset.
  57. /// </summary>
  58. /// <param name="result">Ouput vertices. Only the Y and Z coordinates are calculated. The X coordinates are undefined.</param>
  59. /// <param name="multi">Scale factor for the input vertices. Should be +1 or -1. If -1 the negative half plane is kept.</param>
  60. /// <param name="offset">Offset to move the input vertices with before cutting</param>
  61. public void ClipPolygonAlongZWithYZ (ref VoxelPolygonClipper result, float multi, float offset) {
  62. // Number of resulting vertices
  63. int m = 0;
  64. bool prev, curr;
  65. float dj = multi*z[(n-1)]+offset;
  66. for (int i = 0, j = n-1; i < n; j = i, i++) {
  67. float di = multi*z[i]+offset;
  68. prev = dj >= 0;
  69. curr = di >= 0;
  70. if (prev != curr) {
  71. float s = dj / (dj - di);
  72. result.y[m] = y[j] + (y[i]-y[j])*s;
  73. result.z[m] = z[j] + (z[i]-z[j])*s;
  74. m++;
  75. }
  76. if (curr) {
  77. result.y[m] = y[i];
  78. result.z[m] = z[i];
  79. m++;
  80. }
  81. dj = di;
  82. }
  83. result.n = m;
  84. }
  85. /// <summary>
  86. /// Clips a polygon against an axis aligned half plane.
  87. /// The polygons stored in this object are clipped against the half plane at z = -offset.
  88. /// </summary>
  89. /// <param name="result">Ouput vertices. Only the Y coordinates are calculated. The X and Z coordinates are undefined.</param>
  90. /// <param name="multi">Scale factor for the input vertices. Should be +1 or -1. If -1 the negative half plane is kept.</param>
  91. /// <param name="offset">Offset to move the input vertices with before cutting</param>
  92. public void ClipPolygonAlongZWithY (ref VoxelPolygonClipper result, float multi, float offset) {
  93. // Number of resulting vertices
  94. int m = 0;
  95. bool prev, curr;
  96. float dj = multi*z[(n-1)]+offset;
  97. for (int i = 0, j = n-1; i < n; j = i, i++) {
  98. float di = multi*z[i]+offset;
  99. prev = dj >= 0;
  100. curr = di >= 0;
  101. if (prev != curr) {
  102. float s = dj / (dj - di);
  103. result.y[m] = y[j] + (y[i]-y[j])*s;
  104. m++;
  105. }
  106. if (curr) {
  107. result.y[m] = y[i];
  108. m++;
  109. }
  110. dj = di;
  111. }
  112. result.n = m;
  113. }
  114. }
  115. /// <summary>Utility for clipping polygons</summary>
  116. internal struct Int3PolygonClipper {
  117. /// <summary>Cache this buffer to avoid unnecessary allocations</summary>
  118. float[] clipPolygonCache;
  119. /// <summary>Cache this buffer to avoid unnecessary allocations</summary>
  120. int[] clipPolygonIntCache;
  121. /// <summary>Initialize buffers if they are null</summary>
  122. public void Init () {
  123. if (clipPolygonCache == null) {
  124. clipPolygonCache = new float[7*3];
  125. clipPolygonIntCache = new int[7*3];
  126. }
  127. }
  128. /// <summary>
  129. /// Clips a polygon against an axis aligned half plane.
  130. ///
  131. /// Returns: Number of output vertices
  132. ///
  133. /// The vertices will be scaled and then offset, after that they will be cut using either the
  134. /// x axis, y axis or the z axis as the cutting line. The resulting vertices will be added to the
  135. /// vOut array in their original space (i.e before scaling and offsetting).
  136. /// </summary>
  137. /// <param name="vIn">Input vertices</param>
  138. /// <param name="n">Number of input vertices (may be less than the length of the vIn array)</param>
  139. /// <param name="vOut">Output vertices, needs to be large enough</param>
  140. /// <param name="multi">Scale factor for the input vertices</param>
  141. /// <param name="offset">Offset to move the input vertices with before cutting</param>
  142. /// <param name="axis">Axis to cut along, either x=0, y=1, z=2</param>
  143. public int ClipPolygon (Int3[] vIn, int n, Int3[] vOut, int multi, int offset, int axis) {
  144. Init();
  145. int[] d = clipPolygonIntCache;
  146. for (int i = 0; i < n; i++) {
  147. d[i] = multi*vIn[i][axis]+offset;
  148. }
  149. // Number of resulting vertices
  150. int m = 0;
  151. for (int i = 0, j = n-1; i < n; j = i, i++) {
  152. bool prev = d[j] >= 0;
  153. bool curr = d[i] >= 0;
  154. if (prev != curr) {
  155. double s = (double)d[j] / (d[j] - d[i]);
  156. vOut[m] = vIn[j] + (vIn[i]-vIn[j])*s;
  157. m++;
  158. }
  159. if (curr) {
  160. vOut[m] = vIn[i];
  161. m++;
  162. }
  163. }
  164. return m;
  165. }
  166. }
  167. }