13template <
class VERTEX_ARRAY>
20 Vec3 e1 = inPolygonToClip[inPolygonToClip.size() - 1];
21 float prev_num = (inPlaneOrigin - e1).Dot(inPlaneNormal);
22 bool prev_inside = prev_num < 0.0f;
25 for (
typename VERTEX_ARRAY::size_type j = 0; j < inPolygonToClip.size(); ++j)
28 Vec3Arg e2 = inPolygonToClip[j];
29 float num = (inPlaneOrigin - e2).Dot(inPlaneNormal);
30 bool cur_inside = num < 0.0f;
33 if (cur_inside != prev_inside)
37 float denom = e12.
Dot(inPlaneNormal);
39 outClippedPolygon.push_back(e1 + (prev_num / denom) * e12);
41 cur_inside = prev_inside;
46 outClippedPolygon.push_back(e2);
50 prev_inside = cur_inside;
61template <
class VERTEX_ARRAY>
62void ClipPolyVsPoly(
const VERTEX_ARRAY &inPolygonToClip,
const VERTEX_ARRAY &inClippingPolygon,
Vec3Arg inClippingPolygonNormal, VERTEX_ARRAY &outClippedPolygon)
67 VERTEX_ARRAY tmp_vertices[2];
68 int tmp_vertices_idx = 0;
70 for (
typename VERTEX_ARRAY::size_type i = 0; i < inClippingPolygon.size(); ++i)
73 Vec3 clip_e1 = inClippingPolygon[i];
74 Vec3 clip_e2 = inClippingPolygon[(i + 1) % inClippingPolygon.size()];
75 Vec3 clip_normal = inClippingPolygonNormal.
Cross(clip_e2 - clip_e1);
78 const VERTEX_ARRAY &src_polygon = (i == 0)? inPolygonToClip : tmp_vertices[tmp_vertices_idx];
79 tmp_vertices_idx ^= 1;
80 VERTEX_ARRAY &tgt_polygon = (i == inClippingPolygon.size() - 1)? outClippedPolygon : tmp_vertices[tmp_vertices_idx];
87 if (tgt_polygon.size() < 3)
89 outClippedPolygon.clear();
97template <
class VERTEX_ARRAY>
104 Vec3 edge = inEdgeVertex2 - inEdgeVertex1;
105 Vec3 edge_normal = inClippingEdgeNormal.
Cross(edge);
108 Vec3 polygon_normal = (inPolygonToClip[2] - inPolygonToClip[0]).Cross(inPolygonToClip[1] - inPolygonToClip[0]);
109 float polygon_normal_len_sq = polygon_normal.
LengthSq();
110 Vec3 v1 = inEdgeVertex1 + polygon_normal.
Dot(inPolygonToClip[0] - inEdgeVertex1) * polygon_normal / polygon_normal_len_sq;
111 Vec3 v2 = inEdgeVertex2 + polygon_normal.
Dot(inPolygonToClip[0] - inEdgeVertex2) * polygon_normal / polygon_normal_len_sq;
116 Vec3 e1 = inPolygonToClip[inPolygonToClip.size() - 1];
117 float prev_num = (inEdgeVertex1 - e1).Dot(edge_normal);
118 bool prev_inside = prev_num < 0.0f;
121 for (
typename VERTEX_ARRAY::size_type j = 0; j < inPolygonToClip.size(); ++j)
124 Vec3 e2 = inPolygonToClip[j];
125 float num = (inEdgeVertex1 - e2).Dot(edge_normal);
126 bool cur_inside = num < 0.0f;
129 if (cur_inside != prev_inside)
133 float denom = e12.
Dot(edge_normal);
134 Vec3 clipped_point = e1 + (prev_num / denom) * e12;
137 float projection = (clipped_point - v1).Dot(v12);
138 if (projection < 0.0f)
139 outClippedPolygon.push_back(v1);
140 else if (projection > v12_len_sq)
141 outClippedPolygon.push_back(v2);
143 outClippedPolygon.push_back(clipped_point);
148 prev_inside = cur_inside;
155template <
class VERTEX_ARRAY>
156void ClipPolyVsAABox(
const VERTEX_ARRAY &inPolygonToClip,
const AABox &inAABox, VERTEX_ARRAY &outClippedPolygon)
160 VERTEX_ARRAY tmp_vertices[2];
161 int tmp_vertices_idx = 0;
163 for (
int coord = 0; coord < 3; ++coord)
164 for (
int side = 0; side < 2; ++side)
170 normal.SetComponent(coord, 1.0f);
175 normal.SetComponent(coord, -1.0f);
180 const VERTEX_ARRAY &src_polygon = tmp_vertices_idx == 0? inPolygonToClip : tmp_vertices[tmp_vertices_idx & 1];
182 VERTEX_ARRAY &tgt_polygon = tmp_vertices_idx == 6? outClippedPolygon : tmp_vertices[tmp_vertices_idx & 1];
189 if (tgt_polygon.size() < 3)
191 outClippedPolygon.clear();
void ClipPolyVsPoly(const VERTEX_ARRAY &inPolygonToClip, const VERTEX_ARRAY &inClippingPolygon, Vec3Arg inClippingPolygonNormal, VERTEX_ARRAY &outClippedPolygon)
Definition: ClipPoly.h:62
JPH_NAMESPACE_BEGIN void ClipPolyVsPlane(const VERTEX_ARRAY &inPolygonToClip, Vec3Arg inPlaneOrigin, Vec3Arg inPlaneNormal, VERTEX_ARRAY &outClippedPolygon)
Definition: ClipPoly.h:14
void ClipPolyVsEdge(const VERTEX_ARRAY &inPolygonToClip, Vec3Arg inEdgeVertex1, Vec3Arg inEdgeVertex2, Vec3Arg inClippingEdgeNormal, VERTEX_ARRAY &outClippedPolygon)
Definition: ClipPoly.h:98
void ClipPolyVsAABox(const VERTEX_ARRAY &inPolygonToClip, const AABox &inAABox, VERTEX_ARRAY &outClippedPolygon)
Definition: ClipPoly.h:156
#define JPH_NAMESPACE_END
Definition: Core.h:367
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
Axis aligned box.
Definition: AABox.h:16
Vec3 mMin
Bounding box min and max.
Definition: AABox.h:300
Vec3 mMax
Definition: AABox.h:301
JPH_INLINE float Dot(Vec3Arg inV2) const
Dot product.
Definition: Vec3.inl:637
JPH_INLINE void SetComponent(uint inCoordinate, float inValue)
Set float component by index.
Definition: Vec3.h:140
JPH_INLINE Vec3 Cross(Vec3Arg inV2) const
Cross product.
Definition: Vec3.inl:582
JPH_INLINE float LengthSq() const
Squared length of vector.
Definition: Vec3.inl:653
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition: Vec3.inl:107