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 cScaleToleranceSq = 1.0e-8f;
16
18 inline bool IsNotScaled(Vec3Arg inScale) { return inScale.IsClose(Vec3::sReplicate(1.0f), cScaleToleranceSq); }
19
21 inline bool IsUniformScale(Vec3Arg inScale) { return inScale.Swizzle<SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_X>().IsClose(inScale, cScaleToleranceSq); }
22
24 inline float ScaleConvexRadius(float inConvexRadius, Vec3Arg inScale) { return min(inConvexRadius * inScale.Abs().ReduceMin(), cDefaultConvexRadius); }
25
27 inline bool IsInsideOut(Vec3Arg inScale) { return (CountBits(Vec3::sLess(inScale, Vec3::sZero()).GetTrues() & 0x7) & 1) != 0; }
28
30 inline Vec3 MakeUniformScale(Vec3Arg inScale) { return Vec3::sReplicate((inScale.GetX() + inScale.GetY() + inScale.GetZ()) / 3.0f); }
31
36 inline bool CanScaleBeRotated(QuatArg inRotation, Vec3Arg inScale)
37 {
38 // inScale is a scale in local space of the shape, so the transform for the shape (ignoring translation) is: T = Mat44::sScale(inScale) * mRotation.
39 // when we pass the scale to the child it needs to be local to the child, so we want T = mRotation * Mat44::sScale(ChildScale).
40 // Solving for ChildScale: ChildScale = mRotation^-1 * Mat44::sScale(inScale) * mRotation = mRotation^T * Mat44::sScale(inScale) * mRotation
41 // If any of the off diagonal elements are non-zero, it means the scale / rotation is not compatible.
42 Mat44 r = Mat44::sRotation(inRotation);
43 Mat44 child_scale = r.Multiply3x3LeftTransposed(r.PostScaled(inScale));
44
45 // Get the columns, but zero the diagonal
46 Vec4 zero = Vec4::sZero();
47 Vec4 c0 = Vec4::sSelect(child_scale.GetColumn4(0), zero, UVec4(0xffffffff, 0, 0, 0)).Abs();
48 Vec4 c1 = Vec4::sSelect(child_scale.GetColumn4(1), zero, UVec4(0, 0xffffffff, 0, 0)).Abs();
49 Vec4 c2 = Vec4::sSelect(child_scale.GetColumn4(2), zero, UVec4(0, 0, 0xffffffff, 0)).Abs();
50
51 // Check if all elements are less than epsilon
52 Vec4 epsilon = Vec4::sReplicate(1.0e-6f);
53 return UVec4::sAnd(UVec4::sAnd(Vec4::sLess(c0, epsilon), Vec4::sLess(c1, epsilon)), Vec4::sLess(c2, epsilon)).TestAllTrue();
54 }
55
60 inline Vec3 RotateScale(QuatArg inRotation, Vec3Arg inScale)
61 {
62 // Get the diagonal of mRotation^T * Mat44::sScale(inScale) * mRotation (see comment at CanScaleBeRotated)
63 Mat44 r = Mat44::sRotation(inRotation);
65 }
66}
67
#define JPH_NAMESPACE_END
Definition: Core.h:240
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:234
uint CountBits(uint32 inValue)
Count the number of 1 bits in a value.
Definition: Math.h:155
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:374
JPH_INLINE Mat44 PostScaled(Vec3Arg inScale) const
Scale a matrix: result = Mat44::sScale(inScale) * this.
Definition: Mat44.inl:1178
JPH_INLINE Vec3 GetDiagonal3() const
Definition: Mat44.h:150
JPH_INLINE Vec4 GetColumn4(uint inCol) const
Definition: Mat44.h:156
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:400
static JPH_INLINE UVec4 sAnd(UVec4Arg inV1, UVec4Arg inV2)
Logical and (component wise)
Definition: UVec4.inl:194
Definition: Vec3.h:16
JPH_INLINE bool IsClose(Vec3Arg inV2, float inMaxDistSq=1.0e-12f) const
Test if two vectors are close.
Definition: Vec3.inl:342
JPH_INLINE float GetX() const
Get individual components.
Definition: Vec3.h:122
JPH_INLINE float ReduceMin() const
Get the minimum of X, Y and Z.
Definition: Vec3.inl:798
JPH_INLINE Vec3 Abs() const
Return the absolute value of each of the components.
Definition: Vec3.inl:564
JPH_INLINE float GetY() const
Definition: Vec3.h:123
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:124
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:591
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:36
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:30
Vec3 RotateScale(QuatArg inRotation, Vec3Arg inScale)
Definition: ScaleHelpers.h:60
float ScaleConvexRadius(float inConvexRadius, Vec3Arg inScale)
Scale the convex radius of an object.
Definition: ScaleHelpers.h:24
bool IsUniformScale(Vec3Arg inScale)
Test if a scale is uniform.
Definition: ScaleHelpers.h:21
bool IsInsideOut(Vec3Arg inScale)
Test if a scale flips an object inside out (which requires flipping all normals and polygon windings)
Definition: ScaleHelpers.h:27
bool IsNotScaled(Vec3Arg inScale)
Test if a scale is identity.
Definition: ScaleHelpers.h:18