29#ifndef PX_MATH_UTILS_H
30#define PX_MATH_UTILS_H
36#include "foundation/PxFoundationConfig.h"
37#include "foundation/Px.h"
38#include "foundation/PxVec4.h"
39#include "foundation/PxAssert.h"
40#include "foundation/PxPlane.h"
67PX_FOUNDATION_API PxVec3 PxDiagonalize(
const PxMat33& m, PxQuat& axes);
76PX_FOUNDATION_API PxTransform
PxTransformFromSegment(
const PxVec3& p0,
const PxVec3& p1, PxReal* halfHeight = NULL);
103PX_FOUNDATION_API PxQuat
PxSlerp(
const PxReal t,
const PxQuat& left,
const PxQuat& right);
113PX_FOUNDATION_API
void PxIntegrateTransform(
const PxTransform& curTrans,
const PxVec3& linvel,
const PxVec3& angvel,
114 PxReal timeStep, PxTransform& result);
135 const PxReal s = q.getImaginaryPart().
magnitude();
140 PX_ASSERT(halfAngle >= -PxPi / 2 && halfAngle <= PxPi / 2);
142 return q.getImaginaryPart().
getNormalized() * 2.f * halfAngle;
150 PxU32 m = PxU32(v.y > v.x ? 1 : 0);
151 return v.z > v[m] ? 2 : m;
166 return sin < 0.0f ? -sqrtf(FLT_MAX) : sqrtf(FLT_MAX);
169 return sin / (1.0f + cos);
189 const PxU32 MAX_ITERATIONS = 20;
190 const PxReal convergenceThreshold = 1e-4f;
195 const PxReal tinyEps = 1e-6f;
196 if (radii.y >= radii.z)
199 return PxVec3(0, point.y > 0 ? radii.y : -radii.y, 0);
204 return PxVec3(0, 0, point.z > 0 ? radii.z : -radii.z);
212 PxReal t =
PxMax(eq.y - e2.y, eq.z - e2.z);
214 for (PxU32 i = 0; i < MAX_ITERATIONS; i++)
216 denom =
PxVec3(0, 1 / (t + e2.y), 1 / (t + e2.z));
217 PxVec3 denom2 = eq.multiply(denom);
220 PxReal f = fv.y + fv.z - 1;
226 if (f < convergenceThreshold)
229 PxReal df = fv.
dot(denom) * -2.0f;
248 twist = q.
x != 0.0f ?
PxQuat(q.
x, 0, 0, q.w).getNormalized() :
PxQuat(PxIdentity);
249 swing = q * twist.getConjugate();
260 const PxF32 cos = v0.
dot(v1);
261 const PxF32 sin = (v0.
cross(v1)).magnitude();
274 if (
PxAbs(dir.y) <= 0.9999f)
276 right =
PxVec3(dir.z, 0.0f, -dir.x);
281 up =
PxVec3(dir.y * right.z, dir.z * right.x - dir.x * right.z, -dir.y * right.x);
285 right =
PxVec3(1.0f, 0.0f, 0.0f);
287 up =
PxVec3(0.0f, dir.z, -dir.y);
317 return (i + 1 + (i >> 1)) & 3;
320PX_INLINE PX_CUDA_CALLABLE
void computeBarycentric(
const PxVec3& a,
const PxVec3& b,
const PxVec3& c,
const PxVec3& d,
const PxVec3& p, PxVec4& bary)
322 const PxVec3 ba = b - a;
323 const PxVec3 ca = c - a;
324 const PxVec3 da = d - a;
325 const PxVec3 pa = p - a;
327 const PxReal detBcd = ba.dot(ca.cross(da));
328 const PxReal detPcd = pa.dot(ca.cross(da));
330 bary.y = detPcd / detBcd;
332 const PxReal detBpd = ba.dot(pa.cross(da));
333 bary.z = detBpd / detBcd;
335 const PxReal detBcp = ba.dot(ca.cross(pa));
337 bary.w = detBcp / detBcd;
338 bary.x = 1 - bary.y - bary.z - bary.w;
341PX_INLINE PX_CUDA_CALLABLE
void computeBarycentric(
const PxVec3& a,
const PxVec3& b,
const PxVec3& c,
const PxVec3& p, PxVec4& bary)
343 const PxVec3 v0 = b - a;
344 const PxVec3 v1 = c - a;
345 const PxVec3 v2 = p - a;
347 const float d00 = v0.dot(v0);
348 const float d01 = v0.dot(v1);
349 const float d11 = v1.dot(v1);
350 const float d20 = v2.dot(v0);
351 const float d21 = v2.dot(v1);
353 const float denom = d00 * d11 - d01 * d01;
354 const float v = (d11 * d20 - d01 * d21) / denom;
355 const float w = (d00 * d21 - d01 * d20) / denom;
356 const float u = 1.f - v - w;
358 bary.x = u; bary.y = v; bary.z = w;
366 PX_INLINE PX_CUDA_CALLABLE
static float PxLerp(
float a,
float b,
float t)
368 return a + t * (b - a);
371 PX_INLINE PX_CUDA_CALLABLE
static PxReal PxBiLerp(
376 const PxReal tx,
const PxReal ty)
379 PxLerp(f00, f10, tx),
380 PxLerp(f01, f11, tx),
384 PX_INLINE PX_CUDA_CALLABLE
static PxReal PxTriLerp(
398 PxBiLerp(f000, f100, f010, f110, tx, ty),
399 PxBiLerp(f001, f101, f011, f111, tx, ty),
403 PX_INLINE PX_CUDA_CALLABLE
static PxU32 PxSDFIdx(PxU32 i, PxU32 j, PxU32 k, PxU32 nbX, PxU32 nbY)
405 return i + j * nbX + k * nbX*nbY;
409 const PxVec3& sdfBoxHigher,
const PxReal sdfDx,
const PxReal invSdfDx,
const PxU32 dimX,
const PxU32 dimY,
const PxU32 dimZ, PxReal tolerance)
413 const PxVec3 diff = (localPos - clampedGridPt);
418 PxVec3 f = (clampedGridPt - sdfBoxLower) * invSdfDx;
420 PxU32 i = PxU32(f.x);
421 PxU32 j = PxU32(f.y);
422 PxU32 k = PxU32(f.z);
424 f -=
PxVec3(PxReal(i), PxReal(j), PxReal(k));
429 clampedGridPt.x -= f.x * sdfDx;
435 clampedGridPt.y -= f.y * sdfDx;
441 clampedGridPt.z -= f.z * sdfDx;
445 const PxReal s000 = sdf[Interpolation::PxSDFIdx(i, j, k, dimX, dimY)];
446 const PxReal s100 = sdf[Interpolation::PxSDFIdx(i + 1, j, k, dimX, dimY)];
447 const PxReal s010 = sdf[Interpolation::PxSDFIdx(i, j + 1, k, dimX, dimY)];
448 const PxReal s110 = sdf[Interpolation::PxSDFIdx(i + 1, j + 1, k, dimX, dimY)];
449 const PxReal s001 = sdf[Interpolation::PxSDFIdx(i, j, k + 1, dimX, dimY)];
450 const PxReal s101 = sdf[Interpolation::PxSDFIdx(i + 1, j, k + 1, dimX, dimY)];
451 const PxReal s011 = sdf[Interpolation::PxSDFIdx(i, j + 1, k + 1, dimX, dimY)];
452 const PxReal s111 = sdf[Interpolation::PxSDFIdx(i + 1, j + 1, k + 1, dimX, dimY)];
454 PxReal dist = PxTriLerp(
473 const PxVec3& sdfBoxHigher,
const PxReal sdfDx,
const PxReal invSdfDx,
const PxU32 dimX,
const PxU32 dimY,
const PxU32 dimZ,
PxVec3& gradient, PxReal tolerance = PX_MAX_F32)
476 PxReal dist = Interpolation::PxSDFSampleImpl(sdf, localPos, sdfBoxLower, sdfBoxHigher, sdfDx, invSdfDx, dimX, dimY, dimZ, tolerance);
478 if (dist < tolerance)
482 grad.x = Interpolation::PxSDFSampleImpl(sdf, localPos +
PxVec3(sdfDx, 0.f, 0.f), sdfBoxLower, sdfBoxHigher, sdfDx, invSdfDx, dimX, dimY, dimZ, tolerance) -
483 Interpolation::PxSDFSampleImpl(sdf, localPos -
PxVec3(sdfDx, 0.f, 0.f), sdfBoxLower, sdfBoxHigher, sdfDx, invSdfDx, dimX, dimY, dimZ, tolerance);
484 grad.y = Interpolation::PxSDFSampleImpl(sdf, localPos +
PxVec3(0.f, sdfDx, 0.f), sdfBoxLower, sdfBoxHigher, sdfDx, invSdfDx, dimX, dimY, dimZ, tolerance) -
485 Interpolation::PxSDFSampleImpl(sdf, localPos -
PxVec3(0.f, sdfDx, 0.f), sdfBoxLower, sdfBoxHigher, sdfDx, invSdfDx, dimX, dimY, dimZ, tolerance);
486 grad.z = Interpolation::PxSDFSampleImpl(sdf, localPos +
PxVec3(0.f, 0.f, sdfDx), sdfBoxLower, sdfBoxHigher, sdfDx, invSdfDx, dimX, dimY, dimZ, tolerance) -
487 Interpolation::PxSDFSampleImpl(sdf, localPos -
PxVec3(0.f, 0.f, sdfDx), sdfBoxLower, sdfBoxHigher, sdfDx, invSdfDx, dimX, dimY, dimZ, tolerance);
Representation of a plane.
Definition PxPlane.h:49
PX_CUDA_CALLABLE PX_FORCE_INLINE PxPlane transform(const PxTransform &pose) const
transform plane
Definition PxPlane.h:137
This is a quaternion class. For more information on quaternion mathematics consult a mathematics sour...
Definition PxQuat.h:50
float x
Definition PxQuat.h:395
3 Element vector class.
Definition PxVec3.h:50
PX_CUDA_CALLABLE PX_FORCE_INLINE float dot(const PxVec3 &v) const
returns the scalar product of this and other.
Definition PxVec3.h:276
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 cross(const PxVec3 &v) const
cross product
Definition PxVec3.h:284
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 getNormalized() const
Definition PxVec3.h:291
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 multiply(const PxVec3 &a) const
a[i] * b[i], for all i.
Definition PxVec3.h:336
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 maximum(const PxVec3 &v) const
element-wise maximum
Definition PxVec3.h:360
PX_CUDA_CALLABLE PX_FORCE_INLINE float magnitudeSquared() const
returns the squared magnitude
Definition PxVec3.h:175
PX_CUDA_CALLABLE PX_FORCE_INLINE float normalize()
normalizes the vector in place
Definition PxVec3.h:300
PX_CUDA_CALLABLE PX_FORCE_INLINE float magnitude() const
returns the magnitude
Definition PxVec3.h:183
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 minimum(const PxVec3 &v) const
element-wise minimum
Definition PxVec3.h:344
#define PX_RESTRICT
Definition PxPreprocessor.h:355
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
#define PX_INLINE
Definition PxPreprocessor.h:320
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
PX_CUDA_CALLABLE PX_FORCE_INLINE void PxSeparateSwingTwist(const PxQuat &q, PxQuat &swing, PxQuat &twist)
Compute from an input quaternion q a pair of quaternions (swing, twist) such that q = swing * twist w...
Definition PxMathUtils.h:246
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAtan2(float x, float y)
Arctangent of (x/y) with correct sign. Returns angle between -PI and PI in radians Unit: Radians.
Definition PxMath.h:302
PX_CUDA_CALLABLE PX_FORCE_INLINE PxReal PxTanHalf(PxReal sin, PxReal cos)
Compute tan(theta/2) given sin(theta) and cos(theta) as inputs.
Definition PxMathUtils.h:160
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAbs(float a)
abs returns the absolute value of its argument.
Definition PxMath.h:109
PX_CUDA_CALLABLE PX_FORCE_INLINE PxF32 PxSqr(const PxF32 a)
square of the argument
Definition PxMath.h:170
PX_FOUNDATION_API PxQuat PxShortestRotation(const PxVec3 &from, const PxVec3 &target)
finds the shortest rotation between two vectors.
Definition FdMathUtils.cpp:70
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxRecipSqrt(float a)
reciprocal square root.
Definition PxMath.h:158
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 PxEllipseClamp(const PxVec3 &point, const PxVec3 &radii)
Compute the closest point on an 2d ellipse to a given 2d point.
Definition PxMathUtils.h:178
PX_FOUNDATION_API PxTransform PxTransformFromSegment(const PxVec3 &p0, const PxVec3 &p1, PxReal *halfHeight=NULL)
creates a transform from the endpoints of a segment, suitable for an actor transform for a PxCapsuleG...
Definition FdMathUtils.cpp:59
PX_FOUNDATION_API PxQuat PxSlerp(const PxReal t, const PxQuat &left, const PxQuat &right)
Spherical linear interpolation of two quaternions.
Definition FdMathUtils.cpp:178
PX_CUDA_CALLABLE PX_FORCE_INLINE PxU32 PxLargestAxis(const PxVec3 &v)
return Returns 0 if v.x is largest element of v, 1 if v.y is largest element, 2 if v....
Definition PxMathUtils.h:148
PX_FOUNDATION_API PxTransform PxTransformFromPlaneEquation(const PxPlane &plane)
creates a transform from a plane equation, suitable for an actor transform for a PxPlaneGeometry
Definition FdMathUtils.cpp:40
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxSqrt(float a)
Square root.
Definition PxMath.h:146
PX_INLINE PxPlane PxPlaneEquationFromTransform(const PxTransform &pose)
creates a plane equation from a transform, such as the actor transform for a PxPlaneGeometry
Definition PxMathUtils.h:90
PX_FOUNDATION_API void PxIntegrateTransform(const PxTransform &curTrans, const PxVec3 &linvel, const PxVec3 &angvel, PxReal timeStep, PxTransform &result)
integrate transform.
Definition FdMathUtils.cpp:207
PX_INLINE PxU32 PxGetNextIndex3(PxU32 i)
Compute (i+1)%3.
Definition PxMathUtils.h:315
PX_INLINE void PxComputeBasisVectors(const PxVec3 &dir, PxVec3 &right, PxVec3 &up)
Compute two normalized vectors (right and up) that are perpendicular to an input normalized vector (d...
Definition PxMathUtils.h:271
PX_CUDA_CALLABLE PX_FORCE_INLINE T PxMax(T a, T b)
The return value is the greater of the two specified values.
Definition PxMath.h:72
PX_FOUNDATION_API PxVec3 PxOptimizeBoundingBox(PxMat33 &basis)
computes a oriented bounding box around the scaled basis.
Definition FdMathUtils.cpp:140
PX_CUDA_CALLABLE PX_FORCE_INLINE PxF32 PxComputeAngle(const PxVec3 &v0, const PxVec3 &v1)
Compute the angle between two non-unit vectors.
Definition PxMathUtils.h:258
Definition PxMathUtils.h:365