29 #ifndef NVBLASTEXTAUTHORINGVSA_H 30 #define NVBLASTEXTAUTHORINGVSA_H 67 #if VS3D_HIGH_ACCURACY || REAL_DOUBLE 68 const real c2 = c | c;
69 if (c2 != 0) c = c + (1 / c2)*((a | c)*(c^b) + (b | c)*(a^c));
75 inline real
sq(real
x) {
return x*
x; }
78 inline int ext_index(
int c_10,
int c_21,
int c_20) {
return c_10 << c_21 | (c_21&c_20) << 1; }
81 inline int index_of_min(real x0, real x1, real x2) {
return ext_index((
int)(x1 < x0), (
int)(x2 < x1), (
int)(x2 < x0)); }
84 inline bool frac_gt(real a_num, real a_rden2, real b_num, real b_rden2)
86 const bool a_num_neg = a_num < 0;
87 const bool b_num_neg = b_num < 0;
88 return a_num_neg != b_num_neg ? b_num_neg : ((a_num*a_num*a_rden2 > b_num*b_num*b_rden2) != a_num_neg);
92 inline int index_of_max_frac(real x0_num, real x0_rden2, real x1_num, real x1_rden2, real x2_num, real x2_rden2)
94 return ext_index((
int)
frac_gt(x1_num, x1_rden2, x0_num, x0_rden2), (
int)
frac_gt(x2_num, x2_rden2, x1_num, x1_rden2), (
int)
frac_gt(x2_num, x2_rden2, x0_num, x0_rden2));
98 inline bool sgn_sq_gt(real sgn_a, real a2, real sgn_b, real b2) {
return sgn_a*sgn_b < 0 ? (sgn_b < 0) : ((a2 > b2) != (sgn_a < 0)); }
103 return ext_index((
int)
sgn_sq_gt(sgn_x1, sq_x1, sgn_x0, sq_x0), (
int)
sgn_sq_gt(sgn_x2, sq_x2, sgn_x1, sq_x1), (
int)
sgn_sq_gt(sgn_x2, sq_x2, sgn_x0, sq_x0));
109 r = r + (-delta*recip_n2)*
vec3(plane.
x, plane.
y, 0);
110 r = r + (-(r | plane)*recip_n2)*
vec3(plane.
x, plane.
y, 0);
111 if ((r | r) > eps2)
return;
112 r = (-plane.
z*recip_n2)*
vec3(plane.
x, plane.
y, 0);
118 static bool vs3d_update(
Vec4& p,
Vec4 S[4],
int& plane_count,
const Vec4& q, real eps2)
121 const Vec4& h = S[plane_count - 1];
124 if (plane_count == 1)
128 p.
v = p.
v + -(p | h)*h.
v;
129 if ((p | p) <= eps2) p =
vec4(-h.
w*h.
v, 1);
135 const Vec3 y = h.
v^
vec3((real)(min_i == 0), (real)(min_i == 1), (real)(min_i == 2));
139 Vec3 r = { x | q.
v, y | q.
v, q.
w*(y |
y) };
142 if ((r | r) <= eps2) r.
z = 1;
150 for (
int i = 0; i < plane_count - 1; ++i)
152 const Vec3& vi = S[i].
v;
153 const real cos_theta = h.
v | vi;
154 R[N] =
vec3(x | vi, y | vi, S[i].
w - h.
w*cos_theta);
156 const real n2 = R[N].x*R[N].x + R[N].y*R[N].y;
159 const real lin_norm = (
real)1.5 - (real)0.5*n2;
160 R[N] = lin_norm*R[N];
161 recip_n2[N] = 1 / (R[N].x*R[N].x + R[N].y*R[N].y);
165 else if (cos_theta < 0)
return false;
172 if (delta[0] < 0) N = 0;
173 else project2D(r, R[0], delta[0], recip_n2[0], eps2);
176 if (delta[0] < 0 && delta[1] < 0) N = 0;
179 const int max_d_index = (int)
frac_gt(delta[1], recip_n2[1], delta[0], recip_n2[0]);
180 project2D(r, R[max_d_index], delta[max_d_index], recip_n2[max_d_index], eps2);
181 const int min_d_index = max_d_index ^ 1;
182 const real new_delta_min = r | R[min_d_index];
183 if (new_delta_min < 0)
185 index[0] = index[max_d_index];
191 r =
perp(R[0], R[1]);
192 if (r.
z*r.
z*recip_n2[0] * recip_n2[1] < eps2)
194 if (R[0].x*R[1].x + R[0].y*R[1].y < 0)
return false;
202 if (delta[0] < 0 && delta[1] < 0 && delta[2] < 0) N = 0;
205 const Vec3 row_x = { R[0].
x, R[1].x, R[2].x };
206 const Vec3 row_y = { R[0].
y, R[1].y, R[2].y };
207 const Vec3 row_w = { R[0].
z, R[1].z, R[2].z };
208 const Vec3 cof_w =
perp(row_x, row_y);
209 const bool detR_pos = (row_w | cof_w) > 0;
210 const int nrw_sgn0 = cof_w.
x*cof_w.
x*recip_n2[1] * recip_n2[2] < eps2 ? 0 : (((int)((cof_w.
x > 0) == detR_pos) << 1) - 1);
211 const int nrw_sgn1 = cof_w.
y*cof_w.
y*recip_n2[2] * recip_n2[0] < eps2 ? 0 : (((int)((cof_w.
y > 0) == detR_pos) << 1) - 1);
212 const int nrw_sgn2 = cof_w.
z*cof_w.
z*recip_n2[0] * recip_n2[1] < eps2 ? 0 : (((int)((cof_w.
z > 0) == detR_pos) << 1) - 1);
214 if ((nrw_sgn0 | nrw_sgn1 | nrw_sgn2) >= 0)
return false;
216 const int positive_width_count = ((nrw_sgn0 >> 1) & 1) + ((nrw_sgn1 >> 1) & 1) + ((nrw_sgn2 >> 1) & 1);
217 if (positive_width_count == 1)
220 const int pos_width_index = ((nrw_sgn1 >> 1) & 1) | (nrw_sgn2 & 2);
221 R[pos_width_index] = R[2];
222 recip_n2[pos_width_index] = recip_n2[2];
223 delta[pos_width_index] = delta[2];
224 index[pos_width_index] = index[2];
230 const int max_d_index = r.
z != 0
231 ?
index_of_max_frac(delta[0], recip_n2[0], delta[1], recip_n2[1], delta[2], recip_n2[2])
232 :
index_of_max_sgn_sq(delta[0], -
sq(r.
x*R[0].y - r.
y*R[0].x)*recip_n2[0], delta[1], -
sq(r.
x*R[1].y - r.
y*R[1].x)*recip_n2[1], delta[2], -
sq(r.
x*R[2].y - r.
y*R[2].x)*recip_n2[2]);
235 project2D(r, R[max_d_index], delta[max_d_index], recip_n2[max_d_index], eps2);
237 const int index_max = index[max_d_index];
241 const int finite_width_count = (nrw_sgn0 & 1) + (nrw_sgn1 & 1) + (nrw_sgn2 & 1);
242 if (finite_width_count >= 2)
244 const int i_remaining[2] = { (1 << max_d_index) & 3, (3 >> max_d_index) ^ 1 };
245 const int i_select = (int)
frac_gt(delta[i_remaining[1]], recip_n2[i_remaining[1]], delta[i_remaining[0]], recip_n2[i_remaining[0]]);
246 for (
int i = 0; i < 2; ++i)
248 const int j = i_remaining[i_select^i];
251 r =
perp(R[max_d_index], R[j]);
260 index[0] = index_max;
266 p =
vec4(r.
x*x + r.
y*y + (-r.
z*h.
w)*h.
v, r.
z);
269 if (N < 2 || index[1] != 0) {
for (
int i = 0; i < N; ++i) S[i] = S[index[i]]; }
270 else {
const Vec4 temp = S[0]; S[0] = S[index[0]]; S[1] = temp; }
282 const Vec4 objective = q ? (q[3] != 0 ?
vec4((1 / q[3])*
vec3(q[0], q[1], q[2]), 1) : *(
Vec4*)q) :
vec4(
vec3(0, 0, 0), 1);
285 const real eps_f = (
real)1 / (
sizeof(real) == 4 ? (1L << 23) : (1LL << 52));
286 #if VS3D_HIGH_ACCURACY || REAL_DOUBLE 287 const real eps = 8 * eps_f;
289 const real eps = 80 * eps_f;
291 const real eps2 = eps*eps;
294 const int max_iteration_count = 50;
305 for (
int i = 0; result < 0 && i < max_iteration_count; ++i)
307 Vec4& plane = S[plane_count++];
309 #if VS3D_UNNORMALIZED_PLANE_HANDLING != 0 310 const real recip_norm = vs3d_recip_sqrt(plane.
v | plane.
v);
311 plane =
vec4(recip_norm*plane.
v, recip_norm*plane.
w);
314 if (delta <= 0 || delta*delta <= eps2*(p | p)) result = 1;
315 else if (!vs3d_update(p, S, plane_count, objective, eps2)) result = 0;
319 if (q) *(
Vec4*)q = (p.
w != 0) ?
vec4((1 / p.
w)*p.
v, 1) : p;
330 #endif // ifndef NVBLASTEXTAUTHORINGVSA_H Definition: NvBlastExtAuthoringVSA.h:45
Vec4 vec4(const Vec3 &v, real w)
Definition: NvBlastExtAuthoringVSA.h:60
real z
Definition: NvBlastExtAuthoringVSA.h:52
bool frac_gt(real a_num, real a_rden2, real b_num, real b_rden2)
Definition: NvBlastExtAuthoringVSA.h:84
void project2D(Vec3 &r, const Vec3 &plane, real delta, real recip_n2, real eps2)
Definition: NvBlastExtAuthoringVSA.h:107
SIMD_FORCE_INLINE const btScalar & x() const
Return the x value.
Definition: btVector3.h:275
Vec3 operator*(real s, const Vec3 &v)
Definition: NvBlastExtAuthoringVSA.h:55
Vec3 vec3(real x, real y, real z)
Definition: NvBlastExtAuthoringVSA.h:53
real y
Definition: NvBlastExtAuthoringVSA.h:52
virtual real farthest_halfspace(real plane[4], const real point[4])=0
real w
Definition: NvBlastExtAuthoringVSA.h:59
Vec3 v
Definition: NvBlastExtAuthoringVSA.h:59
SIMD_FORCE_INLINE const btScalar & y() const
Return the y value.
Definition: btVector3.h:277
int index_of_max_frac(real x0_num, real x0_rden2, real x1_num, real x1_rden2, real x2_num, real x2_rden2)
Definition: NvBlastExtAuthoringVSA.h:92
Definition: NvBlastExtAuthoringVSA.h:52
real sq(real x)
Definition: NvBlastExtAuthoringVSA.h:75
int ext_index(int c_10, int c_21, int c_20)
Definition: NvBlastExtAuthoringVSA.h:78
int index_of_min(real x0, real x1, real x2)
Definition: NvBlastExtAuthoringVSA.h:81
int index_of_max_sgn_sq(real sgn_x0, real sq_x0, real sgn_x1, real sq_x1, real sgn_x2, real sq_x2)
Definition: NvBlastExtAuthoringVSA.h:101
real x
Definition: NvBlastExtAuthoringVSA.h:52
int vs3d_test(VS3D_Halfspace_Set &halfspace_set, real *q=nullptr)
Definition: NvBlastExtAuthoringVSA.h:279
Definition: NvBlastExtAuthoringVSA.h:59
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition: btVector3.h:279
Vec3 perp(const Vec3 &a, const Vec3 &b)
Definition: NvBlastExtAuthoringVSA.h:64
float real
Definition: NvBlastExtAuthoringVSA.h:43
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition: btVector3.h:281
real operator|(const Vec3 &a, const Vec3 &b)
Definition: NvBlastExtAuthoringVSA.h:56
Vec3 operator+(const Vec3 &a, const Vec3 &b)
Definition: NvBlastExtAuthoringVSA.h:54
Definition: NvBlastArray.h:37
bool sgn_sq_gt(real sgn_a, real a2, real sgn_b, real b2)
Definition: NvBlastExtAuthoringVSA.h:98
Vec3 operator^(const Vec3 &a, const Vec3 &b)
Definition: NvBlastExtAuthoringVSA.h:57