ClipPoly.h 7.0 KB

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