b3BvhTraversal.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "Bullet3Common/shared/b3Int4.h"
  2. #include "Bullet3Collision/NarrowPhaseCollision/shared/b3RigidBodyData.h"
  3. #include "Bullet3Collision/NarrowPhaseCollision/shared/b3Collidable.h"
  4. #include "Bullet3Collision/BroadPhaseCollision/shared/b3Aabb.h"
  5. #include "Bullet3Collision/NarrowPhaseCollision/shared/b3BvhSubtreeInfoData.h"
  6. #include "Bullet3Collision/NarrowPhaseCollision/shared/b3QuantizedBvhNodeData.h"
  7. // work-in-progress
  8. void b3BvhTraversal(__global const b3Int4* pairs,
  9. __global const b3RigidBodyData* rigidBodies,
  10. __global const b3Collidable* collidables,
  11. __global b3Aabb* aabbs,
  12. __global b3Int4* concavePairsOut,
  13. __global volatile int* numConcavePairsOut,
  14. __global const b3BvhSubtreeInfo* subtreeHeadersRoot,
  15. __global const b3QuantizedBvhNode* quantizedNodesRoot,
  16. __global const b3BvhInfo* bvhInfos,
  17. int numPairs,
  18. int maxNumConcavePairsCapacity,
  19. int id)
  20. {
  21. int bodyIndexA = pairs[id].x;
  22. int bodyIndexB = pairs[id].y;
  23. int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;
  24. int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;
  25. //once the broadphase avoids static-static pairs, we can remove this test
  26. if ((rigidBodies[bodyIndexA].m_invMass == 0) && (rigidBodies[bodyIndexB].m_invMass == 0))
  27. {
  28. return;
  29. }
  30. if (collidables[collidableIndexA].m_shapeType != SHAPE_CONCAVE_TRIMESH)
  31. return;
  32. int shapeTypeB = collidables[collidableIndexB].m_shapeType;
  33. if (shapeTypeB != SHAPE_CONVEX_HULL &&
  34. shapeTypeB != SHAPE_SPHERE &&
  35. shapeTypeB != SHAPE_COMPOUND_OF_CONVEX_HULLS)
  36. return;
  37. b3BvhInfo bvhInfo = bvhInfos[collidables[collidableIndexA].m_numChildShapes];
  38. b3Float4 bvhAabbMin = bvhInfo.m_aabbMin;
  39. b3Float4 bvhAabbMax = bvhInfo.m_aabbMax;
  40. b3Float4 bvhQuantization = bvhInfo.m_quantization;
  41. int numSubtreeHeaders = bvhInfo.m_numSubTrees;
  42. __global const b3BvhSubtreeInfoData* subtreeHeaders = &subtreeHeadersRoot[bvhInfo.m_subTreeOffset];
  43. __global const b3QuantizedBvhNodeData* quantizedNodes = &quantizedNodesRoot[bvhInfo.m_nodeOffset];
  44. unsigned short int quantizedQueryAabbMin[3];
  45. unsigned short int quantizedQueryAabbMax[3];
  46. b3QuantizeWithClamp(quantizedQueryAabbMin, aabbs[bodyIndexB].m_minVec, false, bvhAabbMin, bvhAabbMax, bvhQuantization);
  47. b3QuantizeWithClamp(quantizedQueryAabbMax, aabbs[bodyIndexB].m_maxVec, true, bvhAabbMin, bvhAabbMax, bvhQuantization);
  48. for (int i = 0; i < numSubtreeHeaders; i++)
  49. {
  50. b3BvhSubtreeInfoData subtree = subtreeHeaders[i];
  51. int overlap = b3TestQuantizedAabbAgainstQuantizedAabbSlow(quantizedQueryAabbMin, quantizedQueryAabbMax, subtree.m_quantizedAabbMin, subtree.m_quantizedAabbMax);
  52. if (overlap != 0)
  53. {
  54. int startNodeIndex = subtree.m_rootNodeIndex;
  55. int endNodeIndex = subtree.m_rootNodeIndex + subtree.m_subtreeSize;
  56. int curIndex = startNodeIndex;
  57. int escapeIndex;
  58. int isLeafNode;
  59. int aabbOverlap;
  60. while (curIndex < endNodeIndex)
  61. {
  62. b3QuantizedBvhNodeData rootNode = quantizedNodes[curIndex];
  63. aabbOverlap = b3TestQuantizedAabbAgainstQuantizedAabbSlow(quantizedQueryAabbMin, quantizedQueryAabbMax, rootNode.m_quantizedAabbMin, rootNode.m_quantizedAabbMax);
  64. isLeafNode = b3IsLeaf(&rootNode);
  65. if (aabbOverlap)
  66. {
  67. if (isLeafNode)
  68. {
  69. int triangleIndex = b3GetTriangleIndex(&rootNode);
  70. if (shapeTypeB == SHAPE_COMPOUND_OF_CONVEX_HULLS)
  71. {
  72. int numChildrenB = collidables[collidableIndexB].m_numChildShapes;
  73. int pairIdx = b3AtomicAdd(numConcavePairsOut, numChildrenB);
  74. for (int b = 0; b < numChildrenB; b++)
  75. {
  76. if ((pairIdx + b) < maxNumConcavePairsCapacity)
  77. {
  78. int childShapeIndexB = collidables[collidableIndexB].m_shapeIndex + b;
  79. b3Int4 newPair = b3MakeInt4(bodyIndexA, bodyIndexB, triangleIndex, childShapeIndexB);
  80. concavePairsOut[pairIdx + b] = newPair;
  81. }
  82. }
  83. }
  84. else
  85. {
  86. int pairIdx = b3AtomicInc(numConcavePairsOut);
  87. if (pairIdx < maxNumConcavePairsCapacity)
  88. {
  89. b3Int4 newPair = b3MakeInt4(bodyIndexA, bodyIndexB, triangleIndex, 0);
  90. concavePairsOut[pairIdx] = newPair;
  91. }
  92. }
  93. }
  94. curIndex++;
  95. }
  96. else
  97. {
  98. if (isLeafNode)
  99. {
  100. curIndex++;
  101. }
  102. else
  103. {
  104. escapeIndex = b3GetEscapeIndex(&rootNode);
  105. curIndex += escapeIndex;
  106. }
  107. }
  108. }
  109. }
  110. }
  111. }