| 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_EXTENSIONS_TRIANGLE_MESH_H |
| 32 | #define PX_PHYSICS_EXTENSIONS_TRIANGLE_MESH_H |
| 33 | /** \addtogroup extensions |
| 34 | @{ |
| 35 | */ |
| 36 | |
| 37 | #include "PxPhysXConfig.h" |
| 38 | #include "common/PxPhysXCommonConfig.h" |
| 39 | |
| 40 | #if !PX_DOXYGEN |
| 41 | namespace physx |
| 42 | { |
| 43 | #endif |
| 44 | |
| 45 | class PxGeometry; |
| 46 | class PxTriangleMeshGeometry; |
| 47 | class PxHeightFieldGeometry; |
| 48 | |
| 49 | /** |
| 50 | \brief Utility class to find mesh triangles touched by a specified geometry object. |
| 51 | |
| 52 | This class is a helper calling PxMeshQuery::findOverlapTriangleMesh or PxMeshQuery::findOverlapHeightField under the hood, |
| 53 | while taking care of necessary memory management issues. |
| 54 | |
| 55 | PxMeshQuery::findOverlapTriangleMesh and PxMeshQuery::findOverlapHeightField are the "raw" functions operating on user-provided fixed-size |
| 56 | buffers. These functions abort with an error code in case of buffer overflow. PxMeshOverlapUtil is a convenient helper function checking |
| 57 | this error code, and resizing buffers appropriately, until the desired call succeeds. |
| 58 | |
| 59 | Returned triangle indices are stored within the class, and can be used with PxMeshQuery::getTriangle() to retrieve the triangle properties. |
| 60 | */ |
| 61 | class PxMeshOverlapUtil |
| 62 | { |
| 63 | public: |
| 64 | PxMeshOverlapUtil(); |
| 65 | ~PxMeshOverlapUtil(); |
| 66 | /** |
| 67 | \brief Find the mesh triangles which touch the specified geometry object. |
| 68 | |
| 69 | \param[in] geom The geometry object to test for mesh triangle overlaps. Supported geometries are #PxSphereGeometry, #PxCapsuleGeometry and #PxBoxGeometry |
| 70 | \param[in] geomPose Pose of the geometry object |
| 71 | \param[in] meshGeom The triangle mesh geometry to check overlap against |
| 72 | \param[in] meshPose Pose of the triangle mesh |
| 73 | \return Number of overlaps found. Triangle indices can then be accessed through the #getResults() function. |
| 74 | |
| 75 | @see PxGeometry PxTransform PxTriangleMeshGeometry PxMeshQuery::findOverlapTriangleMesh |
| 76 | */ |
| 77 | PxU32 findOverlap(const PxGeometry& geom, const PxTransform& geomPose, const PxTriangleMeshGeometry& meshGeom, const PxTransform& meshPose); |
| 78 | |
| 79 | /** |
| 80 | \brief Find the height field triangles which touch the specified geometry object. |
| 81 | |
| 82 | \param[in] geom The geometry object to test for height field overlaps. Supported geometries are #PxSphereGeometry, #PxCapsuleGeometry and #PxBoxGeometry. The sphere and capsule queries are currently conservative estimates. |
| 83 | \param[in] geomPose Pose of the geometry object |
| 84 | \param[in] hfGeom The height field geometry to check overlap against |
| 85 | \param[in] hfPose Pose of the height field |
| 86 | \return Number of overlaps found. Triangle indices can then be accessed through the #getResults() function. |
| 87 | |
| 88 | @see PxGeometry PxTransform PxHeightFieldGeometry PxMeshQuery::findOverlapHeightField |
| 89 | */ |
| 90 | PxU32 findOverlap(const PxGeometry& geom, const PxTransform& geomPose, const PxHeightFieldGeometry& hfGeom, const PxTransform& hfPose); |
| 91 | |
| 92 | /** |
| 93 | \brief Retrieves array of triangle indices after a findOverlap call. |
| 94 | \return Indices of touched triangles |
| 95 | */ |
| 96 | PX_FORCE_INLINE const PxU32* getResults() const { return mResultsMemory; } |
| 97 | |
| 98 | /** |
| 99 | \brief Retrieves number of triangle indices after a findOverlap call. |
| 100 | \return Number of touched triangles |
| 101 | */ |
| 102 | PX_FORCE_INLINE PxU32 getNbResults() const { return mNbResults; } |
| 103 | |
| 104 | private: |
| 105 | PxU32* mResultsMemory; |
| 106 | PxU32 mResults[256]; |
| 107 | PxU32 mNbResults; |
| 108 | PxU32 mMaxNbResults; |
| 109 | }; |
| 110 | |
| 111 | /** |
| 112 | \brief Computes an approximate minimum translational distance (MTD) between a geometry object and a mesh. |
| 113 | |
| 114 | This iterative function computes an approximate vector that can be used to depenetrate a geom object |
| 115 | from a triangle mesh. Returned depenetration vector should be applied to 'geom', to get out of the mesh. |
| 116 | |
| 117 | The function works best when the amount of overlap between the geom object and the mesh is small. If the |
| 118 | geom object's center goes inside the mesh, backface culling usually kicks in, no overlap is detected, |
| 119 | and the function does not compute an MTD vector. |
| 120 | |
| 121 | The function early exits if no overlap is detected after a depenetration attempt. This means that if |
| 122 | maxIter = N, the code will attempt at most N iterations but it might exit earlier if depenetration has |
| 123 | been successful. Usually N = 4 gives good results. |
| 124 | |
| 125 | \param[out] direction Computed MTD unit direction |
| 126 | \param[out] depth Penetration depth. Always positive or zero. |
| 127 | \param[in] geom The geometry object |
| 128 | \param[in] geomPose Pose for the geometry object |
| 129 | \param[in] meshGeom The mesh geometry |
| 130 | \param[in] meshPose Pose for the mesh |
| 131 | \param[in] maxIter Max number of iterations before returning. |
| 132 | \param[out] usedIter Number of depenetrations attempts performed during the call. Will not be returned if the pointer is NULL. |
| 133 | |
| 134 | \return True if the MTD has successfully been computed, i.e. if objects do overlap. |
| 135 | |
| 136 | @see PxGeometry PxTransform PxTriangleMeshGeometry |
| 137 | */ |
| 138 | bool PxComputeTriangleMeshPenetration(PxVec3& direction, |
| 139 | PxReal& depth, |
| 140 | const PxGeometry& geom, |
| 141 | const PxTransform& geomPose, |
| 142 | const PxTriangleMeshGeometry& meshGeom, |
| 143 | const PxTransform& meshPose, |
| 144 | PxU32 maxIter, |
| 145 | PxU32* usedIter = NULL); |
| 146 | |
| 147 | /** |
| 148 | \brief Computes an approximate minimum translational distance (MTD) between a geometry object and a heightfield. |
| 149 | |
| 150 | This iterative function computes an approximate vector that can be used to depenetrate a geom object |
| 151 | from a heightfield. Returned depenetration vector should be applied to 'geom', to get out of the heightfield. |
| 152 | |
| 153 | The function works best when the amount of overlap between the geom object and the mesh is small. If the |
| 154 | geom object's center goes inside the heightfield, backface culling usually kicks in, no overlap is detected, |
| 155 | and the function does not compute an MTD vector. |
| 156 | |
| 157 | The function early exits if no overlap is detected after a depenetration attempt. This means that if |
| 158 | maxIter = N, the code will attempt at most N iterations but it might exit earlier if depenetration has |
| 159 | been successful. Usually N = 4 gives good results. |
| 160 | |
| 161 | \param[out] direction Computed MTD unit direction |
| 162 | \param[out] depth Penetration depth. Always positive or zero. |
| 163 | \param[in] geom The geometry object |
| 164 | \param[in] geomPose Pose for the geometry object |
| 165 | \param[in] heightFieldGeom The heightfield geometry |
| 166 | \param[in] heightFieldPose Pose for the heightfield |
| 167 | \param[in] maxIter Max number of iterations before returning. |
| 168 | \param[out] usedIter Number of depenetrations attempts performed during the call. Will not be returned if the pointer is NULL. |
| 169 | |
| 170 | \return True if the MTD has successfully been computed, i.e. if objects do overlap. |
| 171 | |
| 172 | @see PxGeometry PxTransform PxHeightFieldGeometry |
| 173 | */ |
| 174 | bool PxComputeHeightFieldPenetration(PxVec3& direction, |
| 175 | PxReal& depth, |
| 176 | const PxGeometry& geom, |
| 177 | const PxTransform& geomPose, |
| 178 | const PxHeightFieldGeometry& heightFieldGeom, |
| 179 | const PxTransform& heightFieldPose, |
| 180 | PxU32 maxIter, |
| 181 | PxU32* usedIter = NULL); |
| 182 | |
| 183 | #if !PX_DOXYGEN |
| 184 | } // namespace physx |
| 185 | #endif |
| 186 | |
| 187 | /** @} */ |
| 188 | #endif |
| 189 | |