| 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 | #include "geometry/PxMeshQuery.h" |
| 32 | #include "geometry/PxGeometryQuery.h" |
| 33 | #include "geometry/PxTriangleMeshGeometry.h" |
| 34 | #include "geometry/PxHeightFieldGeometry.h" |
| 35 | #include "geometry/PxHeightField.h" |
| 36 | #include "geometry/PxTriangleMesh.h" |
| 37 | #include "extensions/PxTriangleMeshExt.h" |
| 38 | |
| 39 | #include "PsAllocator.h" |
| 40 | |
| 41 | using namespace physx; |
| 42 | |
| 43 | PxMeshOverlapUtil::PxMeshOverlapUtil() : mResultsMemory(mResults), mNbResults(0), mMaxNbResults(256) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | PxMeshOverlapUtil::~PxMeshOverlapUtil() |
| 48 | { |
| 49 | if(mResultsMemory != mResults) |
| 50 | PX_FREE(mResultsMemory); |
| 51 | } |
| 52 | |
| 53 | PxU32 PxMeshOverlapUtil::findOverlap(const PxGeometry& geom, const PxTransform& geomPose, const PxTriangleMeshGeometry& meshGeom, const PxTransform& meshPose) |
| 54 | { |
| 55 | bool overflow; |
| 56 | PxU32 nbTouchedTris = PxMeshQuery::findOverlapTriangleMesh(geom, geomPose, meshGeom, meshPose, results: mResultsMemory, maxResults: mMaxNbResults, startIndex: 0, overflow); |
| 57 | |
| 58 | if(overflow) |
| 59 | { |
| 60 | const PxU32 maxNbTris = meshGeom.triangleMesh->getNbTriangles(); |
| 61 | if(!maxNbTris) |
| 62 | { |
| 63 | mNbResults = 0; |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | if(mMaxNbResults<maxNbTris) |
| 68 | { |
| 69 | if(mResultsMemory != mResults) |
| 70 | PX_FREE(mResultsMemory); |
| 71 | |
| 72 | mResultsMemory = reinterpret_cast<PxU32*>(PX_ALLOC(sizeof(PxU32)*maxNbTris, "PxMeshOverlapUtil::findOverlap" )); |
| 73 | mMaxNbResults = maxNbTris; |
| 74 | } |
| 75 | nbTouchedTris = PxMeshQuery::findOverlapTriangleMesh(geom, geomPose, meshGeom, meshPose, results: mResultsMemory, maxResults: mMaxNbResults, startIndex: 0, overflow); |
| 76 | PX_ASSERT(nbTouchedTris); |
| 77 | PX_ASSERT(!overflow); |
| 78 | } |
| 79 | mNbResults = nbTouchedTris; |
| 80 | return nbTouchedTris; |
| 81 | } |
| 82 | |
| 83 | PxU32 PxMeshOverlapUtil::findOverlap(const PxGeometry& geom, const PxTransform& geomPose, const PxHeightFieldGeometry& hfGeom, const PxTransform& hfPose) |
| 84 | { |
| 85 | bool overflow = true; |
| 86 | PxU32 nbTouchedTris = PxMeshQuery::findOverlapHeightField(geom, geomPose, hfGeom, hfPose, results: mResultsMemory, maxResults: mMaxNbResults, startIndex: 0, overflow); |
| 87 | |
| 88 | if(overflow) |
| 89 | { |
| 90 | const PxU32 maxNbTris = hfGeom.heightField->getNbRows()*hfGeom.heightField->getNbColumns()*2; |
| 91 | if(!maxNbTris) |
| 92 | { |
| 93 | mNbResults = 0; |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | if(mMaxNbResults<maxNbTris) |
| 98 | { |
| 99 | if(mResultsMemory != mResults) |
| 100 | PX_FREE(mResultsMemory); |
| 101 | |
| 102 | mResultsMemory = reinterpret_cast<PxU32*>(PX_ALLOC(sizeof(PxU32)*maxNbTris, "PxMeshOverlapUtil::findOverlap" )); |
| 103 | mMaxNbResults = maxNbTris; |
| 104 | } |
| 105 | nbTouchedTris = PxMeshQuery::findOverlapHeightField(geom, geomPose, hfGeom, hfPose, results: mResultsMemory, maxResults: mMaxNbResults, startIndex: 0, overflow); |
| 106 | PX_ASSERT(nbTouchedTris); |
| 107 | PX_ASSERT(!overflow); |
| 108 | } |
| 109 | mNbResults = nbTouchedTris; |
| 110 | return nbTouchedTris; |
| 111 | |
| 112 | } |
| 113 | namespace |
| 114 | { |
| 115 | template<typename MeshGeometry> |
| 116 | bool computeMeshPenetrationT(PxVec3& direction, |
| 117 | PxReal& depth, |
| 118 | const PxGeometry& geom, |
| 119 | const PxTransform& geomPose, |
| 120 | const MeshGeometry& meshGeom, |
| 121 | const PxTransform& meshPose, |
| 122 | PxU32 maxIter, |
| 123 | PxU32* nbIterOut) |
| 124 | { |
| 125 | PxU32 nbIter = 0; |
| 126 | PxTransform pose = geomPose; |
| 127 | for (; nbIter < maxIter; nbIter++) |
| 128 | { |
| 129 | PxVec3 currentDir; |
| 130 | PxF32 currentDepth; |
| 131 | |
| 132 | if (!PxGeometryQuery::computePenetration(direction&: currentDir, depth&: currentDepth, geom0: geom, pose0: pose, geom1: meshGeom, pose1: meshPose)) |
| 133 | break; |
| 134 | |
| 135 | pose.p += currentDir * currentDepth; |
| 136 | } |
| 137 | |
| 138 | if(nbIterOut) |
| 139 | *nbIterOut = nbIter; |
| 140 | |
| 141 | PxVec3 diff = pose.p - geomPose.p; |
| 142 | depth = diff.magnitude(); |
| 143 | |
| 144 | if (depth>0) |
| 145 | direction = diff / depth; |
| 146 | |
| 147 | return nbIter!=0; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | bool physx::PxComputeTriangleMeshPenetration(PxVec3& direction, |
| 152 | PxReal& depth, |
| 153 | const PxGeometry& geom, |
| 154 | const PxTransform& geomPose, |
| 155 | const PxTriangleMeshGeometry& meshGeom, |
| 156 | const PxTransform& meshPose, |
| 157 | PxU32 maxIter, |
| 158 | PxU32* nbIter) |
| 159 | { |
| 160 | return computeMeshPenetrationT(direction, depth, geom, geomPose, meshGeom, meshPose, maxIter, nbIterOut: nbIter); |
| 161 | } |
| 162 | |
| 163 | bool physx::PxComputeHeightFieldPenetration(PxVec3& direction, |
| 164 | PxReal& depth, |
| 165 | const PxGeometry& geom, |
| 166 | const PxTransform& geomPose, |
| 167 | const PxHeightFieldGeometry& hfGeom, |
| 168 | const PxTransform& meshPose, |
| 169 | PxU32 maxIter, |
| 170 | PxU32* nbIter) |
| 171 | { |
| 172 | return computeMeshPenetrationT(direction, depth, geom, geomPose, meshGeom: hfGeom, meshPose, maxIter, nbIterOut: nbIter); |
| 173 | } |
| 174 | |
| 175 | |