ClipPoly.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Geometry/AABox.h>
  5. namespace JPH {
  6. /// Clip inPolygonToClip against the positive halfspace of plane defined by inPlaneOrigin and inPlaneNormal.
  7. /// inPlaneNormal does not need to be normalized.
  8. template <class VERTEX_ARRAY>
  9. void ClipPolyVsPlane(const VERTEX_ARRAY &inPolygonToClip, Vec3Arg inPlaneOrigin, Vec3Arg inPlaneNormal, VERTEX_ARRAY &outClippedPolygon)
  10. {
  11. JPH_ASSERT(inPolygonToClip.size() >= 2);
  12. JPH_ASSERT(outClippedPolygon.empty());
  13. // Determine state of last point
  14. Vec3 e1 = inPolygonToClip[inPolygonToClip.size() - 1];
  15. float prev_num = (inPlaneOrigin - e1).Dot(inPlaneNormal);
  16. bool prev_inside = prev_num < 0.0f;
  17. // Loop through all vertices
  18. for (typename VERTEX_ARRAY::size_type j = 0; j < inPolygonToClip.size(); ++j)
  19. {
  20. // Check if second point is inside
  21. Vec3Arg e2 = inPolygonToClip[j];
  22. float num = (inPlaneOrigin - e2).Dot(inPlaneNormal);
  23. bool cur_inside = num < 0.0f;
  24. // In -> Out or Out -> In: Add point on clipping plane
  25. if (cur_inside != prev_inside)
  26. {
  27. // Solve: (X - inPlaneOrigin) . inPlaneNormal = 0 and X = e1 + t * (e2 - e1) for X
  28. Vec3 e12 = e2 - e1;
  29. float denom = e12.Dot(inPlaneNormal);
  30. outClippedPolygon.push_back(e1 + (prev_num / denom) * e12);
  31. }
  32. // Point inside, add it
  33. if (cur_inside)
  34. outClippedPolygon.push_back(e2);
  35. // Update previous state
  36. prev_num = num;
  37. prev_inside = cur_inside;
  38. e1 = e2;
  39. }
  40. }
  41. /// Clip polygon versus polygon.
  42. /// Both polygons are assumed to be in counter clockwise order.
  43. /// @param inClippingPolygonNormal is used to create planes of all edges in inClippingPolygon against which inPolygonToClip is clipped, inClippingPolygonNormal does not need to be normalized
  44. /// @param inClippingPolygon is the polygon which inClippedPolygon is clipped against
  45. /// @param inPolygonToClip is the polygon that is clipped
  46. /// @param outClippedPolygon will contain clipped polygon when function returns
  47. template <class VERTEX_ARRAY>
  48. void ClipPolyVsPoly(const VERTEX_ARRAY &inPolygonToClip, const VERTEX_ARRAY &inClippingPolygon, Vec3Arg inClippingPolygonNormal, VERTEX_ARRAY &outClippedPolygon)
  49. {
  50. JPH_ASSERT(inPolygonToClip.size() >= 2);
  51. JPH_ASSERT(inClippingPolygon.size() >= 3);
  52. VERTEX_ARRAY tmp_vertices[2];
  53. int tmp_vertices_idx = 0;
  54. for (typename VERTEX_ARRAY::size_type i = 0; i < inClippingPolygon.size(); ++i)
  55. {
  56. // Get edge to clip against
  57. Vec3 clip_e1 = inClippingPolygon[i];
  58. Vec3 clip_e2 = inClippingPolygon[(i + 1) % inClippingPolygon.size()];
  59. Vec3 clip_normal = inClippingPolygonNormal.Cross(clip_e2 - clip_e1); // Pointing inward to the clipping polygon
  60. // Get source and target polygon
  61. const VERTEX_ARRAY &src_polygon = (i == 0)? inPolygonToClip : tmp_vertices[tmp_vertices_idx];
  62. tmp_vertices_idx ^= 1;
  63. VERTEX_ARRAY &tgt_polygon = (i == inClippingPolygon.size() - 1)? outClippedPolygon : tmp_vertices[tmp_vertices_idx];
  64. tgt_polygon.clear();
  65. // Clip against the edge
  66. ClipPolyVsPlane(src_polygon, clip_e1, clip_normal, tgt_polygon);
  67. // Break out if no polygon left
  68. if (tgt_polygon.size() < 3)
  69. {
  70. outClippedPolygon.clear();
  71. break;
  72. }
  73. }
  74. }
  75. /// Clip inPolygonToClip against an edge, the edge is projected on inPolygonToClip using inClippingEdgeNormal.
  76. /// The positive half space (the side on the edge in the direction of inClippingEdgeNormal) is cut away.
  77. template <class VERTEX_ARRAY>
  78. void ClipPolyVsEdge(const VERTEX_ARRAY &inPolygonToClip, Vec3Arg inEdgeVertex1, Vec3Arg inEdgeVertex2, Vec3Arg inClippingEdgeNormal, VERTEX_ARRAY &outClippedPolygon)
  79. {
  80. JPH_ASSERT(inPolygonToClip.size() >= 3);
  81. JPH_ASSERT(outClippedPolygon.empty());
  82. // Get normal that is perpendicular to the edge and the clipping edge normal
  83. Vec3 edge = inEdgeVertex2 - inEdgeVertex1;
  84. Vec3 edge_normal = inClippingEdgeNormal.Cross(edge);
  85. // Project vertices of edge on inPolygonToClip
  86. Vec3 polygon_normal = (inPolygonToClip[2] - inPolygonToClip[0]).Cross(inPolygonToClip[1] - inPolygonToClip[0]);
  87. float polygon_normal_len_sq = polygon_normal.LengthSq();
  88. Vec3 v1 = inEdgeVertex1 + polygon_normal.Dot(inPolygonToClip[0] - inEdgeVertex1) * polygon_normal / polygon_normal_len_sq;
  89. Vec3 v2 = inEdgeVertex2 + polygon_normal.Dot(inPolygonToClip[0] - inEdgeVertex2) * polygon_normal / polygon_normal_len_sq;
  90. Vec3 v12 = v2 - v1;
  91. float v12_len_sq = v12.LengthSq();
  92. // Determine state of last point
  93. Vec3 e1 = inPolygonToClip[inPolygonToClip.size() - 1];
  94. float prev_num = (inEdgeVertex1 - e1).Dot(edge_normal);
  95. bool prev_inside = prev_num < 0.0f;
  96. // Loop through all vertices
  97. for (typename VERTEX_ARRAY::size_type j = 0; j < inPolygonToClip.size(); ++j)
  98. {
  99. // Check if second point is inside
  100. Vec3 e2 = inPolygonToClip[j];
  101. float num = (inEdgeVertex1 - e2).Dot(edge_normal);
  102. bool cur_inside = num < 0.0f;
  103. // In -> Out or Out -> In: Add point on clipping plane
  104. if (cur_inside != prev_inside)
  105. {
  106. // Solve: (X - inPlaneOrigin) . inPlaneNormal = 0 and X = e1 + t * (e2 - e1) for X
  107. Vec3 e12 = e2 - e1;
  108. float denom = e12.Dot(edge_normal);
  109. Vec3 clipped_point = e1 + (prev_num / denom) * e12;
  110. // Project point on line segment v1, v2 so see if it falls outside if the edge
  111. float projection = (clipped_point - v1).Dot(v12);
  112. if (projection < 0.0f)
  113. outClippedPolygon.push_back(v1);
  114. else if (projection > v12_len_sq)
  115. outClippedPolygon.push_back(v2);
  116. else
  117. outClippedPolygon.push_back(clipped_point);
  118. }
  119. // Update previous state
  120. prev_num = num;
  121. prev_inside = cur_inside;
  122. e1 = e2;
  123. }
  124. }
  125. /// Clip polygon vs axis aligned box, inPolygonToClip is assume to be in counter clockwise order.
  126. /// Output will be stored in outClippedPolygon. Everything inside inAABox will be kept.
  127. template <class VERTEX_ARRAY>
  128. void ClipPolyVsAABox(const VERTEX_ARRAY &inPolygonToClip, const AABox &inAABox, VERTEX_ARRAY &outClippedPolygon)
  129. {
  130. JPH_ASSERT(inPolygonToClip.size() >= 2);
  131. VERTEX_ARRAY tmp_vertices[2];
  132. int tmp_vertices_idx = 0;
  133. for (int coord = 0; coord < 3; ++coord)
  134. for (int side = 0; side < 2; ++side)
  135. {
  136. // Get plane to clip against
  137. Vec3 origin = Vec3::sZero(), normal = Vec3::sZero();
  138. if (side == 0)
  139. {
  140. normal.SetComponent(coord, 1.0f);
  141. origin.SetComponent(coord, inAABox.mMin[coord]);
  142. }
  143. else
  144. {
  145. normal.SetComponent(coord, -1.0f);
  146. origin.SetComponent(coord, inAABox.mMax[coord]);
  147. }
  148. // Get source and target polygon
  149. const VERTEX_ARRAY &src_polygon = tmp_vertices_idx == 0? inPolygonToClip : tmp_vertices[tmp_vertices_idx & 1];
  150. tmp_vertices_idx++;
  151. VERTEX_ARRAY &tgt_polygon = tmp_vertices_idx == 6? outClippedPolygon : tmp_vertices[tmp_vertices_idx & 1];
  152. tgt_polygon.clear();
  153. // Clip against the edge
  154. ClipPolyVsPlane(src_polygon, origin, normal, tgt_polygon);
  155. // Break out if no polygon left
  156. if (tgt_polygon.size() < 3)
  157. {
  158. outClippedPolygon.clear();
  159. return;
  160. }
  161. // Flip normal
  162. normal = -normal;
  163. }
  164. }
  165. } // JPH