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 float ScaleConvexRadius(float inConvexRadius, Vec3Arg inScale) { return min(inConvexRadius * inScale.Abs().ReduceMin(), cDefaultConvexRadius); }
28
30 inline bool IsInsideOut(Vec3Arg inScale) { return (CountBits(Vec3::sLess(inScale, Vec3::sZero()).GetTrues() & 0x7) & 1) != 0; }
31
33 inline bool IsZeroScale(Vec3Arg inScale) { return Vec3::sLess(inScale.Abs(), Vec3::sReplicate(cMinScale)).TestAnyXYZTrue(); }
34
36 inline Vec3 MakeNonZeroScale(Vec3Arg inScale) { return inScale.GetSign() * Vec3::sMax(inScale.Abs(), Vec3::sReplicate(cMinScale)); }
37
39 inline Vec3 MakeUniformScale(Vec3Arg inScale) { return Vec3::sReplicate((inScale.GetX() + inScale.GetY() + inScale.GetZ()) / 3.0f); }
40
45 inline bool CanScaleBeRotated(QuatArg inRotation, Vec3Arg inScale)
46 {
47 // inScale is a scale in local space of the shape, so the transform for the shape (ignoring translation) is: T = Mat44::sScale(inScale) * mRotation.
48 // when we pass the scale to the child it needs to be local to the child, so we want T = mRotation * Mat44::sScale(ChildScale).
49 // Solving for ChildScale: ChildScale = mRotation^-1 * Mat44::sScale(inScale) * mRotation = mRotation^T * Mat44::sScale(inScale) * mRotation
50 // If any of the off diagonal elements are non-zero, it means the scale / rotation is not compatible.
51 Mat44 r = Mat44::sRotation(inRotation);
52 Mat44 child_scale = r.Multiply3x3LeftTransposed(r.PostScaled(inScale));
53
54 // Get the columns, but zero the diagonal
55 Vec4 zero = Vec4::sZero();
56 Vec4 c0 = Vec4::sSelect(child_scale.GetColumn4(0), zero, UVec4(0xffffffff, 0, 0, 0)).Abs();
57 Vec4 c1 = Vec4::sSelect(child_scale.GetColumn4(1), zero, UVec4(0, 0xffffffff, 0, 0)).Abs();
58 Vec4 c2 = Vec4::sSelect(child_scale.GetColumn4(2), zero, UVec4(0, 0, 0xffffffff, 0)).Abs();
59
60 // Check if all elements are less than epsilon
61 Vec4 epsilon = Vec4::sReplicate(1.0e-6f);
62 return UVec4::sAnd(UVec4::sAnd(Vec4::sLess(c0, epsilon), Vec4::sLess(c1, epsilon)), Vec4::sLess(c2, epsilon)).TestAllTrue();
63 }
64
69 inline Vec3 RotateScale(QuatArg inRotation, Vec3Arg inScale)
70 {
71 // Get the diagonal of mRotation^T * Mat44::sScale(inScale) * mRotation (see comment at CanScaleBeRotated)
72 Mat44 r = Mat44::sRotation(inRotation);
74 }
75}
76
#define JPH_NAMESPACE_END
Definition: Core.h:378
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:372
uint CountBits(uint32 inValue)
Count the number of 1 bits in a value.
Definition: Math.h:161
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:405
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:400
static JPH_INLINE UVec4 sAnd(UVec4Arg inV1, UVec4Arg inV2)
Logical and (component wise)
Definition: UVec4.inl:199
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:159
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:806
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:107
static JPH_INLINE UVec4 sLess(Vec3Arg inV1, Vec3Arg inV2)
Less than (component wise)
Definition: Vec3.inl:192
static JPH_INLINE Vec3 sReplicate(float inV)
Replicate inV across all components.
Definition: Vec3.inl:118
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:834
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:599
static JPH_INLINE UVec4 sLess(Vec4Arg inV1, Vec4Arg inV2)
Less than (component wise)
Definition: Vec4.inl:180
static JPH_INLINE Vec4 sSelect(Vec4Arg inV1, Vec4Arg inV2, UVec4Arg inControl)
Component wise select, returns inV1 when highest bit of inControl = 0 and inV2 when highest bit of in...
Definition: Vec4.inl:254
static JPH_INLINE Vec4 sZero()
Vector with all zeros.
Definition: Vec4.inl:63
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: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:39
Vec3 RotateScale(QuatArg inRotation, Vec3Arg inScale)
Definition: ScaleHelpers.h:69
float ScaleConvexRadius(float inConvexRadius, Vec3Arg inScale)
Scale the convex radius of an object.
Definition: ScaleHelpers.h:27
bool IsZeroScale(Vec3Arg inScale)
Test if any of the components of the scale have a value below cMinScale.
Definition: ScaleHelpers.h:33
Vec3 MakeNonZeroScale(Vec3Arg inScale)
Ensure that the scale for each component is at least cMinScale.
Definition: ScaleHelpers.h:36
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:30
bool IsNotScaled(Vec3Arg inScale)
Test if a scale is identity.
Definition: ScaleHelpers.h:21