b3BvhTraversal.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. )
  37. return;
  38. b3BvhInfo bvhInfo = bvhInfos[collidables[collidableIndexA].m_numChildShapes];
  39. b3Float4 bvhAabbMin = bvhInfo.m_aabbMin;
  40. b3Float4 bvhAabbMax = bvhInfo.m_aabbMax;
  41. b3Float4 bvhQuantization = bvhInfo.m_quantization;
  42. int numSubtreeHeaders = bvhInfo.m_numSubTrees;
  43. __global const b3BvhSubtreeInfoData* subtreeHeaders = &subtreeHeadersRoot[bvhInfo.m_subTreeOffset];
  44. __global const b3QuantizedBvhNodeData* quantizedNodes = &quantizedNodesRoot[bvhInfo.m_nodeOffset];
  45. unsigned short int quantizedQueryAabbMin[3];
  46. unsigned short int quantizedQueryAabbMax[3];
  47. b3QuantizeWithClamp(quantizedQueryAabbMin,aabbs[bodyIndexB].m_minVec,false,bvhAabbMin, bvhAabbMax,bvhQuantization);
  48. b3QuantizeWithClamp(quantizedQueryAabbMax,aabbs[bodyIndexB].m_maxVec,true ,bvhAabbMin, bvhAabbMax,bvhQuantization);
  49. for (int i=0;i<numSubtreeHeaders;i++)
  50. {
  51. b3BvhSubtreeInfoData subtree = subtreeHeaders[i];
  52. int overlap = b3TestQuantizedAabbAgainstQuantizedAabbSlow(quantizedQueryAabbMin,quantizedQueryAabbMax,subtree.m_quantizedAabbMin,subtree.m_quantizedAabbMax);
  53. if (overlap != 0)
  54. {
  55. int startNodeIndex = subtree.m_rootNodeIndex;
  56. int endNodeIndex = subtree.m_rootNodeIndex+subtree.m_subtreeSize;
  57. int curIndex = startNodeIndex;
  58. int escapeIndex;
  59. int isLeafNode;
  60. int aabbOverlap;
  61. while (curIndex < endNodeIndex)
  62. {
  63. b3QuantizedBvhNodeData rootNode = quantizedNodes[curIndex];
  64. aabbOverlap = b3TestQuantizedAabbAgainstQuantizedAabbSlow(quantizedQueryAabbMin,quantizedQueryAabbMax,rootNode.m_quantizedAabbMin,rootNode.m_quantizedAabbMax);
  65. isLeafNode = b3IsLeaf(&rootNode);
  66. if (aabbOverlap)
  67. {
  68. if (isLeafNode)
  69. {
  70. int triangleIndex = b3GetTriangleIndex(&rootNode);
  71. if (shapeTypeB==SHAPE_COMPOUND_OF_CONVEX_HULLS)
  72. {
  73. int numChildrenB = collidables[collidableIndexB].m_numChildShapes;
  74. int pairIdx = b3AtomicAdd (numConcavePairsOut,numChildrenB);
  75. for (int b=0;b<numChildrenB;b++)
  76. {
  77. if ((pairIdx+b)<maxNumConcavePairsCapacity)
  78. {
  79. int childShapeIndexB = collidables[collidableIndexB].m_shapeIndex+b;
  80. b3Int4 newPair = b3MakeInt4(bodyIndexA,bodyIndexB,triangleIndex,childShapeIndexB);
  81. concavePairsOut[pairIdx+b] = newPair;
  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. } else
  96. {
  97. if (isLeafNode)
  98. {
  99. curIndex++;
  100. } else
  101. {
  102. escapeIndex = b3GetEscapeIndex(&rootNode);
  103. curIndex += escapeIndex;
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }