Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Collision/Shape/ScaleHelpers.h
9913 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/Physics/PhysicsSettings.h>78JPH_NAMESPACE_BEGIN910/// Helper functions to get properties of a scaling vector11namespace ScaleHelpers12{13/// Minimum valid scale value. This is used to prevent division by zero when scaling a shape with a zero scale.14static constexpr float cMinScale = 1.0e-6f;1516/// The tolerance used to check if components of the scale vector are the same17static constexpr float cScaleToleranceSq = 1.0e-8f;1819/// Test if a scale is identity20inline bool IsNotScaled(Vec3Arg inScale) { return inScale.IsClose(Vec3::sOne(), cScaleToleranceSq); }2122/// Test if a scale is uniform23inline bool IsUniformScale(Vec3Arg inScale) { return inScale.Swizzle<SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_X>().IsClose(inScale, cScaleToleranceSq); }2425/// Test if a scale is uniform in XZ26inline bool IsUniformScaleXZ(Vec3Arg inScale) { return inScale.Swizzle<SWIZZLE_Z, SWIZZLE_Y, SWIZZLE_X>().IsClose(inScale, ScaleHelpers::cScaleToleranceSq); }2728/// Scale the convex radius of an object29inline float ScaleConvexRadius(float inConvexRadius, Vec3Arg inScale) { return min(inConvexRadius * inScale.Abs().ReduceMin(), cDefaultConvexRadius); }3031/// Test if a scale flips an object inside out (which requires flipping all normals and polygon windings)32inline bool IsInsideOut(Vec3Arg inScale) { return (CountBits(Vec3::sLess(inScale, Vec3::sZero()).GetTrues() & 0x7) & 1) != 0; }3334/// Test if any of the components of the scale have a value below cMinScale35inline bool IsZeroScale(Vec3Arg inScale) { return Vec3::sLess(inScale.Abs(), Vec3::sReplicate(cMinScale)).TestAnyXYZTrue(); }3637/// Ensure that the scale for each component is at least cMinScale38inline Vec3 MakeNonZeroScale(Vec3Arg inScale) { return inScale.GetSign() * Vec3::sMax(inScale.Abs(), Vec3::sReplicate(cMinScale)); }3940/// Get the average scale if inScale, used to make the scale uniform when a shape doesn't support non-uniform scale41inline Vec3 MakeUniformScale(Vec3Arg inScale) { return Vec3::sReplicate((inScale.GetX() + inScale.GetY() + inScale.GetZ()) / 3.0f); }4243/// Average the scale in XZ, used to make the scale uniform when a shape doesn't support non-uniform scale in the XZ plane44inline Vec3 MakeUniformScaleXZ(Vec3Arg inScale) { return 0.5f * (inScale + inScale.Swizzle<SWIZZLE_Z, SWIZZLE_Y, SWIZZLE_X>()); }4546/// Checks in scale can be rotated to child shape47/// @param inRotation Rotation of child shape48/// @param inScale Scale in local space of parent shape49/// @return True if the scale is valid (no shearing introduced)50inline bool CanScaleBeRotated(QuatArg inRotation, Vec3Arg inScale)51{52// inScale is a scale in local space of the shape, so the transform for the shape (ignoring translation) is: T = Mat44::sScale(inScale) * mRotation.53// when we pass the scale to the child it needs to be local to the child, so we want T = mRotation * Mat44::sScale(ChildScale).54// Solving for ChildScale: ChildScale = mRotation^-1 * Mat44::sScale(inScale) * mRotation = mRotation^T * Mat44::sScale(inScale) * mRotation55// If any of the off diagonal elements are non-zero, it means the scale / rotation is not compatible.56Mat44 r = Mat44::sRotation(inRotation);57Mat44 child_scale = r.Multiply3x3LeftTransposed(r.PostScaled(inScale));5859// Get the columns, but zero the diagonal60Vec4 zero = Vec4::sZero();61Vec4 c0 = Vec4::sSelect(child_scale.GetColumn4(0), zero, UVec4(0xffffffff, 0, 0, 0)).Abs();62Vec4 c1 = Vec4::sSelect(child_scale.GetColumn4(1), zero, UVec4(0, 0xffffffff, 0, 0)).Abs();63Vec4 c2 = Vec4::sSelect(child_scale.GetColumn4(2), zero, UVec4(0, 0, 0xffffffff, 0)).Abs();6465// Check if all elements are less than epsilon66Vec4 epsilon = Vec4::sReplicate(1.0e-6f);67return UVec4::sAnd(UVec4::sAnd(Vec4::sLess(c0, epsilon), Vec4::sLess(c1, epsilon)), Vec4::sLess(c2, epsilon)).TestAllTrue();68}6970/// Adjust scale for rotated child shape71/// @param inRotation Rotation of child shape72/// @param inScale Scale in local space of parent shape73/// @return Rotated scale74inline Vec3 RotateScale(QuatArg inRotation, Vec3Arg inScale)75{76// Get the diagonal of mRotation^T * Mat44::sScale(inScale) * mRotation (see comment at CanScaleBeRotated)77Mat44 r = Mat44::sRotation(inRotation);78return r.Multiply3x3LeftTransposed(r.PostScaled(inScale)).GetDiagonal3();79}80}8182JPH_NAMESPACE_END838485