24 #ifndef PXR_USD_USD_MASS_PROPERTIES_H 25 #define PXR_USD_USD_MASS_PROPERTIES_H 28 #include "pxr/usd/usdPhysics/api.h" 37 PXR_NAMESPACE_OPEN_SCOPE
39 GfQuatf UsdPhysicsIndexedRotation(uint32_t axis,
float s,
float c)
41 float v[3] = { 0, 0, 0 };
43 return GfQuatf(c, v[0], v[1], v[2]);
46 uint32_t UsdPhysicsGetNextIndex3(uint32_t i)
48 return (i + 1 + (i >> 1)) & 3;
54 const uint32_t MAX_ITERS = 24;
59 for (uint32_t i = 0; i < MAX_ITERS; i++)
64 float d0 = fabs(d[1][2]), d1 = fabs(d[0][2]), d2 = fabs(d[0][1]);
65 uint32_t a = uint32_t(d0 > d1 && d0 > d2 ? 0 : d1 > d2 ? 1 : 2);
69 uint32_t a1 = UsdPhysicsGetNextIndex3(a), a2 = UsdPhysicsGetNextIndex3(a1);
70 if (d[a1][a2] == 0.0f || fabs(d[a1][a1] - d[a2][a2]) > 2e6 * fabs(2.0 * d[a1][a2]))
73 float w = (d[a1][a1] - d[a2][a2]) / (2.0f * d[a1][a2]);
78 r = UsdPhysicsIndexedRotation(a, 1 / (4 * w), 1.0f);
81 float t = 1 / (absw + sqrt(w * w + 1));
82 float h = 1 / sqrt(t * t + 1);
84 r = UsdPhysicsIndexedRotation(a, sqrt((1 - h) / 2) * ((w >= 0.0f) ? 1.0f : -1.0f), sqrt((1 + h) / 2));
87 q = (q * r).GetNormalized();
110 _inertiaTensor[0][0] = 1.0;
111 _inertiaTensor[1][1] = 1.0;
112 _inertiaTensor[2][2] = 1.0;
117 : _inertiaTensor(inertiaT), _centerOfMass(com), _mass(m)
144 GfVec3f diagT = UsdPhysicsDiagonalize(inertia, massFrame);
183 float combinedMass = 0.0f;
187 for (uint32_t i = 0; i < count; i++)
189 combinedMass += props[i]._mass;
191 combinedCoM += comTm * props[i]._mass;
194 if (combinedMass > 0.f)
195 combinedCoM /= combinedMass;
197 for (uint32_t i = 0; i < count; i++)
202 props[i]._mass, combinedCoM - comTm);
212 return _inertiaTensor;
219 _inertiaTensor = inTensor;
226 return _centerOfMass;
249 PXR_NAMESPACE_CLOSE_SCOPE
void SetMass(float inMass)
Set mass.
const GfMatrix3f & GetInertiaTensor() const
Get inertia tensor.
Stores a 4x4 matrix of float elements.
Mass properties computation class.
Basic type for a vector of 3 float components.
GF_API GfMatrix3f GetTranspose() const
Returns the transpose of the matrix.
GfVec3f GetColumn(int i) const
Gets a column of the matrix as a Vec3.
static USDPHYSICS_API GfMatrix3f RotateInertia(const GfMatrix3f &inertia, const GfQuatf &q)
Rotate an inertia tensor around the center of mass inertia The inertia tensor to rotate.
GfVec3d Transform(const GfVec3d &vec) const
Transforms the row vector vec by the matrix, returning the result.
Basic type: a quaternion, a complex number with a real coefficient and three imaginary coefficients,...
Stores a 3x3 matrix of float elements.
USDPHYSICS_API UsdPhysicsMassProperties operator *(const float scale) const
Scale mass properties.
void SetColumn(int i, const GfVec3f &v)
Sets a column of the matrix from a Vec3.
static USDPHYSICS_API GfVec3f GetMassSpaceInertia(const GfMatrix3f &inertia, GfQuatf &massFrame)
Get the entries of the diagonalized inertia tensor and the corresponding reference rotation.
float GetMass() const
Get mass.
USDPHYSICS_API UsdPhysicsMassProperties(const float m, const GfMatrix3f &inertiaT, const GfVec3f &com)
Construct from individual elements.
USDPHYSICS_API void Translate(const GfVec3f &t)
Translate the center of mass by a given vector and adjust the inertia tensor accordingly.
const GfVec3f & GetCenterOfMass() const
Get center of mass.
void SetInertiaTensor(const GfMatrix3f &inTensor)
Set inertia tensor inTensor New inertia tensor.
static USDPHYSICS_API UsdPhysicsMassProperties Sum(const UsdPhysicsMassProperties *props, const GfMatrix4f *transforms, const uint32_t count)
Sum up individual mass properties.
USDPHYSICS_API UsdPhysicsMassProperties()
Construct a MassProperties.
static USDPHYSICS_API GfMatrix3f TranslateInertia(const GfMatrix3f &inertia, const float mass, const GfVec3f &t)
Translate an inertia tensor using the parallel axis theorem inertia The inertia tensor to translate.