| 1 | // | 
| 2 | // Redistribution and use in source and binary forms, with or without | 
| 3 | // modification, are permitted provided that the following conditions | 
| 4 | // are met: | 
| 5 | //  * Redistributions of source code must retain the above copyright | 
| 6 | //    notice, this list of conditions and the following disclaimer. | 
| 7 | //  * Redistributions in binary form must reproduce the above copyright | 
| 8 | //    notice, this list of conditions and the following disclaimer in the | 
| 9 | //    documentation and/or other materials provided with the distribution. | 
| 10 | //  * Neither the name of NVIDIA CORPORATION nor the names of its | 
| 11 | //    contributors may be used to endorse or promote products derived | 
| 12 | //    from this software without specific prior written permission. | 
| 13 | // | 
| 14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY | 
| 15 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
| 16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 
| 17 | // PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR | 
| 18 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 
| 19 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 
| 20 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 
| 21 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 
| 22 | // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
| 23 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
| 24 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
| 25 | // | 
| 26 | // Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved. | 
| 27 | // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved. | 
| 28 | // Copyright (c) 2001-2004 NovodeX AG. All rights reserved.   | 
| 29 |  | 
| 30 |  | 
| 31 | #ifndef PX_PHYSICS_NX_MESHSCALE | 
| 32 | #define PX_PHYSICS_NX_MESHSCALE | 
| 33 | /** \addtogroup geomutils | 
| 34 | @{ | 
| 35 | */ | 
| 36 |  | 
| 37 | #include "common/PxPhysXCommonConfig.h" | 
| 38 | #include "foundation/PxMat33.h" | 
| 39 | #include "foundation/PxAssert.h" | 
| 40 |  | 
| 41 | /** \brief Minimum allowed absolute magnitude for each of mesh scale's components (x,y,z). | 
| 42 | 	\note Only positive scale values are allowed for convex meshes. */ | 
| 43 | #define PX_MESH_SCALE_MIN 1e-6f | 
| 44 |  | 
| 45 | /** \brief Maximum allowed absolute magnitude for each of mesh scale's components (x,y,z). | 
| 46 | 	\note Only positive scale values are allowed for convex meshes. */ | 
| 47 | #define PX_MESH_SCALE_MAX 1e6f | 
| 48 |  | 
| 49 | #if !PX_DOXYGEN | 
| 50 | namespace physx | 
| 51 | { | 
| 52 | #endif | 
| 53 |  | 
| 54 | /** | 
| 55 | \brief A class expressing a nonuniform scaling transformation. | 
| 56 |  | 
| 57 | The scaling is along arbitrary axes that are specified by PxMeshScale::rotation. | 
| 58 |  | 
| 59 | \note	Negative scale values are supported for PxTriangleMeshGeometry | 
| 60 | 		with absolute values for each component within [PX_MIN_ABS_MESH_SCALE, PX_MAX_ABS_MESH_SCALE] range. | 
| 61 | 		Negative scale causes a reflection around the specified axis, in addition PhysX will flip the normals | 
| 62 | 		for mesh triangles when scale.x*scale.y*scale.z < 0. | 
| 63 | \note	Only positive scale values are supported for PxConvexMeshGeometry | 
| 64 | 		with values for each component within [PX_MIN_ABS_MESH_SCALE, PX_MAX_ABS_MESH_SCALE] range). | 
| 65 |  | 
| 66 | @see PxConvexMeshGeometry PxTriangleMeshGeometry | 
| 67 | */ | 
| 68 | class PxMeshScale | 
| 69 | { | 
| 70 | //= ATTENTION! ===================================================================================== | 
| 71 | // Changing the data layout of this class breaks the binary serialization format.  See comments for  | 
| 72 | // PX_BINARY_SERIAL_VERSION.  If a modification is required, please adjust the getBinaryMetaData  | 
| 73 | // function.  If the modification is made on a custom branch, please change PX_BINARY_SERIAL_VERSION | 
| 74 | // accordingly. | 
| 75 | //================================================================================================== | 
| 76 | public: | 
| 77 | 	/** | 
| 78 | 	\brief Constructor initializes to identity scale. | 
| 79 | 	*/ | 
| 80 | 	PX_CUDA_CALLABLE PX_FORCE_INLINE PxMeshScale(): scale(1.0f), rotation(PxIdentity)  | 
| 81 | 	{ | 
| 82 | 	} | 
| 83 |  | 
| 84 | 	/** | 
| 85 | 	\brief Constructor from scalar. | 
| 86 | 	*/ | 
| 87 | 	explicit PX_CUDA_CALLABLE PX_FORCE_INLINE PxMeshScale(PxReal r): scale(r), rotation(PxIdentity)  | 
| 88 | 	{ | 
| 89 | 	} | 
| 90 |  | 
| 91 | 	/** | 
| 92 | 	\brief Constructor to initialize to arbitrary scale and identity scale rotation. | 
| 93 | 	*/ | 
| 94 | 	PX_CUDA_CALLABLE PX_FORCE_INLINE PxMeshScale(const PxVec3& s) | 
| 95 | 	{ | 
| 96 | 		scale = s; | 
| 97 | 		rotation = PxQuat(PxIdentity); | 
| 98 | 	} | 
| 99 |  | 
| 100 | 	/** | 
| 101 | 	\brief Constructor to initialize to arbitrary scaling. | 
| 102 | 	*/ | 
| 103 | 	PX_CUDA_CALLABLE PX_FORCE_INLINE PxMeshScale(const PxVec3& s, const PxQuat& r) | 
| 104 | 	{ | 
| 105 | 		PX_ASSERT(r.isUnit()); | 
| 106 | 		scale = s; | 
| 107 | 		rotation = r; | 
| 108 | 	} | 
| 109 |  | 
| 110 |  | 
| 111 | 	/** | 
| 112 | 	\brief Returns true if the scaling is an identity transformation. | 
| 113 | 	*/ | 
| 114 | 	PX_CUDA_CALLABLE PX_FORCE_INLINE bool isIdentity()	const | 
| 115 | 	{ | 
| 116 | 		return (scale.x == 1.0f && scale.y == 1.0f && scale.z == 1.0f); | 
| 117 | 	} | 
| 118 |  | 
| 119 | 	/** | 
| 120 | 	\brief Returns the inverse of this scaling transformation. | 
| 121 | 	*/ | 
| 122 | 	PX_CUDA_CALLABLE PX_FORCE_INLINE  PxMeshScale getInverse() const  | 
| 123 | 	{ | 
| 124 | 		return PxMeshScale(PxVec3(1.0f/scale.x, 1.0f/scale.y, 1.0f/scale.z), rotation); | 
| 125 | 	} | 
| 126 |  | 
| 127 | 	/** | 
| 128 | 	\brief Converts this transformation to a 3x3 matrix representation. | 
| 129 | 	*/ | 
| 130 | 	PX_CUDA_CALLABLE PX_FORCE_INLINE PxMat33 toMat33() const  | 
| 131 | 	{ | 
| 132 | 		PxMat33 rot(rotation); | 
| 133 | 		PxMat33 trans = rot.getTranspose(); | 
| 134 | 		trans.column0 *= scale[0]; | 
| 135 | 		trans.column1 *= scale[1]; | 
| 136 | 		trans.column2 *= scale[2]; | 
| 137 | 		return trans * rot; | 
| 138 | 	} | 
| 139 |  | 
| 140 | 	/** | 
| 141 | 	\brief Returns true if combination of negative scale components will cause the triangle normal to flip. The SDK will flip the normals internally. | 
| 142 | 	*/ | 
| 143 | 	PX_CUDA_CALLABLE PX_FORCE_INLINE bool hasNegativeDeterminant() const | 
| 144 | 	{ | 
| 145 | 		return (scale.x * scale.y * scale.z < 0.0f); | 
| 146 | 	} | 
| 147 |  | 
| 148 | 	PxVec3		transform(const PxVec3& v) const | 
| 149 | 	{ | 
| 150 | 		return rotation.rotateInv(v: scale.multiply(a: rotation.rotate(v))); | 
| 151 | 	} | 
| 152 |  | 
| 153 | 	bool		isValidForTriangleMesh() const | 
| 154 | 	{ | 
| 155 | 		PxVec3 absXYZ = scale.abs(); | 
| 156 | 		return (absXYZ.maxElement() <= PX_MESH_SCALE_MAX) && (absXYZ.minElement() >= PX_MESH_SCALE_MIN); | 
| 157 | 	} | 
| 158 |  | 
| 159 | 	bool		isValidForConvexMesh() const | 
| 160 | 	{ | 
| 161 | 		return (scale.maxElement() <= PX_MESH_SCALE_MAX) && (scale.minElement() >= PX_MESH_SCALE_MIN); | 
| 162 | 	} | 
| 163 |  | 
| 164 | 	PxVec3		scale;		//!< A nonuniform scaling | 
| 165 | 	PxQuat		rotation;	//!< The orientation of the scaling axes | 
| 166 |  | 
| 167 |  | 
| 168 | }; | 
| 169 |  | 
| 170 | #if !PX_DOXYGEN | 
| 171 | } // namespace physx | 
| 172 | #endif | 
| 173 |  | 
| 174 | /** @} */ | 
| 175 | #endif | 
| 176 |  |