Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
ScaleHelpers.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
8
10
12namespace ScaleHelpers
13{
15 static constexpr float cMinScale = 1.0e-6f;
16
18 static constexpr float cScaleToleranceSq = 1.0e-8f;
19
21 inline bool IsNotScaled(Vec3Arg inScale) { return inScale.IsClose(Vec3::sReplicate(1.0f), cScaleToleranceSq); }
22
24 inline bool IsUniformScale(Vec3Arg inScale) { return inScale.Swizzle<SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_X>().IsClose(inScale, cScaleToleranceSq); }
25
27 inline bool IsUniformScaleXZ(Vec3Arg inScale) { return inScale.Swizzle<SWIZZLE_Z, SWIZZLE_Y, SWIZZLE_X>().IsClose(inScale, ScaleHelpers::cScaleToleranceSq); }
28
30 inline float ScaleConvexRadius(float inConvexRadius, Vec3Arg inScale) { return min(inConvexRadius * inScale.Abs().ReduceMin(), cDefaultConvexRadius); }
31
33 inline bool IsInsideOut(Vec3Arg inScale) { return (CountBits(Vec3::sLess(inScale, Vec3::sZero()).GetTrues() & 0x7) & 1) != 0; }
34
36 inline bool IsZeroScale(Vec3Arg inScale) { return Vec3::sLess(inScale.Abs(), Vec3::sReplicate(cMinScale)).TestAnyXYZTrue(); }
37
39 inline Vec3 MakeNonZeroScale(Vec3Arg inScale) { return inScale.GetSign() * Vec3::sMax(inScale.Abs(), Vec3::sReplicate(cMinScale)); }
40
42 inline Vec3 MakeUniformScale(Vec3Arg inScale) { return Vec3::sReplicate((inScale.GetX() + inScale.GetY() + inScale.GetZ()) / 3.0f); }
43
45 inline Vec3 MakeUniformScaleXZ(Vec3Arg inScale) { return 0.5f * (inScale + inScale.Swizzle<SWIZZLE_Z, SWIZZLE_Y, SWIZZLE_X>()); }
46
51 inline bool CanScaleBeRotated(QuatArg inRotation, Vec3Arg inScale)
52 {
53 // inScale is a scale in local space of the shape, so the transform for the shape (ignoring translation) is: T = Mat44::sScale(inScale) * mRotation.
54 // when we pass the scale to the child it needs to be local to the child, so we want T = mRotation * Mat44::sScale(ChildScale).
55 // Solving for ChildScale: ChildScale = mRotation^-1 * Mat44::sScale(inScale) * mRotation = mRotation^T * Mat44::sScale(inScale) * mRotation
56 // If any of the off diagonal elements are non-zero, it means the scale / rotation is not compatible.
57 Mat44 r = Mat44::sRotation(inRotation);
58 Mat44 child_scale = r.Multiply3x3LeftTransposed(r.PostScaled(inScale));
59
60 // Get the columns, but zero the diagonal
61 Vec4 zero = Vec4::sZero();
62 Vec4 c0 = Vec4::sSelect(child_scale.GetColumn4(0), zero, UVec4(0xffffffff, 0, 0, 0)).Abs();
63 Vec4 c1 = Vec4::sSelect(child_scale.GetColumn4(1), zero, UVec4(0, 0xffffffff, 0, 0)).Abs();
64 Vec4 c2 = Vec4::sSelect(child_scale.GetColumn4(2), zero, UVec4(0, 0, 0xffffffff, 0)).Abs();
65
66 // Check if all elements are less than epsilon
67 Vec4 epsilon = Vec4::sReplicate(1.0e-6f);
68 return UVec4::sAnd(UVec4::sAnd(Vec4::sLess(c0, epsilon), Vec4::sLess(c1, epsilon)), Vec4::sLess(c2, epsilon)).TestAllTrue();
69 }
70
75 inline Vec3 RotateScale(QuatArg inRotation, Vec3Arg inScale)
76 {
77 // Get the diagonal of mRotation^T * Mat44::sScale(inScale) * mRotation (see comment at CanScaleBeRotated)
78 Mat44 r = Mat44::sRotation(inRotation);
80 }
81}
82
#define JPH_NAMESPACE_END
Definition Core.h:414
#define JPH_NAMESPACE_BEGIN
Definition Core.h:408
uint CountBits(uint32 inValue)
Count the number of 1 bits in a value.
Definition Math.h:164
constexpr float cDefaultConvexRadius
How much padding to add around objects.
Definition PhysicsSettings.h:16
@ SWIZZLE_Z
Use the Z component.
Definition Swizzle.h:14
@ SWIZZLE_X
Use the X component.
Definition Swizzle.h:12
@ SWIZZLE_Y
Use the Y component.
Definition Swizzle.h:13
Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
Definition Mat44.h:13
JPH_INLINE Mat44 Multiply3x3LeftTransposed(Mat44Arg inM) const
Multiply transpose of 3x3 matrix by 3x3 matrix ( )
Definition Mat44.inl:383
JPH_INLINE Mat44 PostScaled(Vec3Arg inScale) const
Scale a matrix: result = Mat44::sScale(inScale) * this.
Definition Mat44.inl:913
JPH_INLINE Vec3 GetDiagonal3() const
Definition Mat44.h:154
JPH_INLINE Vec4 GetColumn4(uint inCol) const
Definition Mat44.h:160
static JPH_INLINE Mat44 sRotation(Vec3Arg inAxis, float inAngle)
Rotate around arbitrary axis.
Definition Mat44.inl:139
Definition Quat.h:33
Definition UVec4.h:12
JPH_INLINE bool TestAllTrue() const
Test if all components are true (true is when highest bit of component is set)
Definition UVec4.inl:408
JPH_INLINE bool TestAnyXYZTrue() const
Test if any of X, Y or Z components are true (true is when highest bit of component is set)
Definition UVec4.inl:403
static JPH_INLINE UVec4 sAnd(UVec4Arg inV1, UVec4Arg inV2)
Logical and (component wise)
Definition UVec4.inl:202
Definition Vec3.h:17
JPH_INLINE bool IsClose(Vec3Arg inV2, float inMaxDistSq=1.0e-12f) const
Test if two vectors are close.
Definition Vec3.inl:342
static JPH_INLINE Vec3 sMax(Vec3Arg inV1, Vec3Arg inV2)
Return the maximum of each of the components.
Definition Vec3.inl:155
JPH_INLINE float GetX() const
Get individual components.
Definition Vec3.h:124
JPH_INLINE float ReduceMin() const
Get the minimum of X, Y and Z.
Definition Vec3.inl:808
JPH_INLINE Vec3 Abs() const
Return the absolute value of each of the components.
Definition Vec3.inl:572
JPH_INLINE float GetY() const
Definition Vec3.h:125
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition Vec3.inl:103
static JPH_INLINE UVec4 sLess(Vec3Arg inV1, Vec3Arg inV2)
Less than (component wise)
Definition Vec3.inl:188
static JPH_INLINE Vec3 sReplicate(float inV)
Replicate inV across all components.
Definition Vec3.inl:114
JPH_INLINE float GetZ() const
Definition Vec3.h:126
JPH_INLINE Vec3 GetSign() const
Get vector that contains the sign of each element (returns 1.0f if positive, -1.0f if negative)
Definition Vec3.inl:836
JPH_INLINE Vec3 Swizzle() const
Swizzle the elements in inV.
Definition Vec4.h:14
JPH_INLINE Vec4 Abs() const
Return the absolute value of each of the components.
Definition Vec4.inl:602
static JPH_INLINE UVec4 sLess(Vec4Arg inV1, Vec4Arg inV2)
Less than (component wise)
Definition Vec4.inl:180
static JPH_INLINE Vec4 sZero()
Vector with all zeros.
Definition Vec4.inl:63
static JPH_INLINE Vec4 sSelect(Vec4Arg inNotSet, Vec4Arg inSet, UVec4Arg inControl)
Component wise select, returns inNotSet when highest bit of inControl = 0 and inSet when highest bit ...
Definition Vec4.inl:254
static JPH_INLINE Vec4 sReplicate(float inV)
Replicate inV across all components.
Definition Vec4.inl:74
Helper functions to get properties of a scaling vector.
Definition ScaleHelpers.h:13
bool CanScaleBeRotated(QuatArg inRotation, Vec3Arg inScale)
Definition ScaleHelpers.h:51
Vec3 MakeUniformScaleXZ(Vec3Arg inScale)
Average the scale in XZ, used to make the scale uniform when a shape doesn't support non-uniform scal...
Definition ScaleHelpers.h:45
Vec3 MakeUniformScale(Vec3Arg inScale)
Get the average scale if inScale, used to make the scale uniform when a shape doesn't support non-uni...
Definition ScaleHelpers.h:42
Vec3 RotateScale(QuatArg inRotation, Vec3Arg inScale)
Definition ScaleHelpers.h:75
float ScaleConvexRadius(float inConvexRadius, Vec3Arg inScale)
Scale the convex radius of an object.
Definition ScaleHelpers.h:30
bool IsZeroScale(Vec3Arg inScale)
Test if any of the components of the scale have a value below cMinScale.
Definition ScaleHelpers.h:36
Vec3 MakeNonZeroScale(Vec3Arg inScale)
Ensure that the scale for each component is at least cMinScale.
Definition ScaleHelpers.h:39
bool IsUniformScale(Vec3Arg inScale)
Test if a scale is uniform.
Definition ScaleHelpers.h:24
bool IsInsideOut(Vec3Arg inScale)
Test if a scale flips an object inside out (which requires flipping all normals and polygon windings)
Definition ScaleHelpers.h:33
bool IsUniformScaleXZ(Vec3Arg inScale)
Test if a scale is uniform in XZ.
Definition ScaleHelpers.h:27
bool IsNotScaled(Vec3Arg inScale)
Test if a scale is identity.
Definition ScaleHelpers.h:21