b3ClipFaces.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #ifndef B3_CLIP_FACES_H
  2. #define B3_CLIP_FACES_H
  3. #include "Bullet3Common/shared/b3Int4.h"
  4. #include "Bullet3Collision/NarrowPhaseCollision/shared/b3RigidBodyData.h"
  5. #include "Bullet3Collision/NarrowPhaseCollision/shared/b3Collidable.h"
  6. #include "Bullet3Collision/BroadPhaseCollision/shared/b3Aabb.h"
  7. #include "Bullet3Collision/NarrowPhaseCollision/shared/b3BvhSubtreeInfoData.h"
  8. #include "Bullet3Collision/NarrowPhaseCollision/shared/b3QuantizedBvhNodeData.h"
  9. #include "Bullet3Collision/NarrowPhaseCollision/shared/b3ConvexPolyhedronData.h"
  10. inline b3Float4 b3Lerp3(b3Float4ConstArg a, b3Float4ConstArg b, float t)
  11. {
  12. return b3MakeFloat4(a.x + (b.x - a.x) * t,
  13. a.y + (b.y - a.y) * t,
  14. a.z + (b.z - a.z) * t,
  15. 0.f);
  16. }
  17. // Clips a face to the back of a plane, return the number of vertices out, stored in ppVtxOut
  18. int clipFaceGlobal(__global const b3Float4* pVtxIn, int numVertsIn, b3Float4ConstArg planeNormalWS, float planeEqWS, __global b3Float4* ppVtxOut)
  19. {
  20. int ve;
  21. float ds, de;
  22. int numVertsOut = 0;
  23. //double-check next test
  24. // if (numVertsIn < 2)
  25. // return 0;
  26. b3Float4 firstVertex = pVtxIn[numVertsIn - 1];
  27. b3Float4 endVertex = pVtxIn[0];
  28. ds = b3Dot(planeNormalWS, firstVertex) + planeEqWS;
  29. for (ve = 0; ve < numVertsIn; ve++)
  30. {
  31. endVertex = pVtxIn[ve];
  32. de = b3Dot(planeNormalWS, endVertex) + planeEqWS;
  33. if (ds < 0)
  34. {
  35. if (de < 0)
  36. {
  37. // Start < 0, end < 0, so output endVertex
  38. ppVtxOut[numVertsOut++] = endVertex;
  39. }
  40. else
  41. {
  42. // Start < 0, end >= 0, so output intersection
  43. ppVtxOut[numVertsOut++] = b3Lerp3(firstVertex, endVertex, (ds * 1.f / (ds - de)));
  44. }
  45. }
  46. else
  47. {
  48. if (de < 0)
  49. {
  50. // Start >= 0, end < 0 so output intersection and end
  51. ppVtxOut[numVertsOut++] = b3Lerp3(firstVertex, endVertex, (ds * 1.f / (ds - de)));
  52. ppVtxOut[numVertsOut++] = endVertex;
  53. }
  54. }
  55. firstVertex = endVertex;
  56. ds = de;
  57. }
  58. return numVertsOut;
  59. }
  60. __kernel void clipFacesAndFindContactsKernel(__global const b3Float4* separatingNormals,
  61. __global const int* hasSeparatingAxis,
  62. __global b3Int4* clippingFacesOut,
  63. __global b3Float4* worldVertsA1,
  64. __global b3Float4* worldNormalsA1,
  65. __global b3Float4* worldVertsB1,
  66. __global b3Float4* worldVertsB2,
  67. int vertexFaceCapacity,
  68. int pairIndex)
  69. {
  70. // int i = get_global_id(0);
  71. //int pairIndex = i;
  72. int i = pairIndex;
  73. float minDist = -1e30f;
  74. float maxDist = 0.02f;
  75. // if (i<numPairs)
  76. {
  77. if (hasSeparatingAxis[i])
  78. {
  79. // int bodyIndexA = pairs[i].x;
  80. // int bodyIndexB = pairs[i].y;
  81. int numLocalContactsOut = 0;
  82. int capacityWorldVertsB2 = vertexFaceCapacity;
  83. __global b3Float4* pVtxIn = &worldVertsB1[pairIndex * capacityWorldVertsB2];
  84. __global b3Float4* pVtxOut = &worldVertsB2[pairIndex * capacityWorldVertsB2];
  85. {
  86. __global b3Int4* clippingFaces = clippingFacesOut;
  87. int closestFaceA = clippingFaces[pairIndex].x;
  88. // int closestFaceB = clippingFaces[pairIndex].y;
  89. int numVertsInA = clippingFaces[pairIndex].z;
  90. int numVertsInB = clippingFaces[pairIndex].w;
  91. int numVertsOut = 0;
  92. if (closestFaceA >= 0)
  93. {
  94. // clip polygon to back of planes of all faces of hull A that are adjacent to witness face
  95. for (int e0 = 0; e0 < numVertsInA; e0++)
  96. {
  97. const b3Float4 aw = worldVertsA1[pairIndex * capacityWorldVertsB2 + e0];
  98. const b3Float4 bw = worldVertsA1[pairIndex * capacityWorldVertsB2 + ((e0 + 1) % numVertsInA)];
  99. const b3Float4 WorldEdge0 = aw - bw;
  100. b3Float4 worldPlaneAnormal1 = worldNormalsA1[pairIndex];
  101. b3Float4 planeNormalWS1 = -b3Cross(WorldEdge0, worldPlaneAnormal1);
  102. b3Float4 worldA1 = aw;
  103. float planeEqWS1 = -b3Dot(worldA1, planeNormalWS1);
  104. b3Float4 planeNormalWS = planeNormalWS1;
  105. float planeEqWS = planeEqWS1;
  106. numVertsOut = clipFaceGlobal(pVtxIn, numVertsInB, planeNormalWS, planeEqWS, pVtxOut);
  107. __global b3Float4* tmp = pVtxOut;
  108. pVtxOut = pVtxIn;
  109. pVtxIn = tmp;
  110. numVertsInB = numVertsOut;
  111. numVertsOut = 0;
  112. }
  113. b3Float4 planeNormalWS = worldNormalsA1[pairIndex];
  114. float planeEqWS = -b3Dot(planeNormalWS, worldVertsA1[pairIndex * capacityWorldVertsB2]);
  115. for (int i = 0; i < numVertsInB; i++)
  116. {
  117. float depth = b3Dot(planeNormalWS, pVtxIn[i]) + planeEqWS;
  118. if (depth <= minDist)
  119. {
  120. depth = minDist;
  121. }
  122. /*
  123. static float maxDepth = 0.f;
  124. if (depth < maxDepth)
  125. {
  126. maxDepth = depth;
  127. if (maxDepth < -10)
  128. {
  129. printf("error at framecount %d?\n",myframecount);
  130. }
  131. printf("maxDepth = %f\n", maxDepth);
  132. }
  133. */
  134. if (depth <= maxDist)
  135. {
  136. b3Float4 pointInWorld = pVtxIn[i];
  137. pVtxOut[numLocalContactsOut++] = b3MakeFloat4(pointInWorld.x, pointInWorld.y, pointInWorld.z, depth);
  138. }
  139. }
  140. }
  141. clippingFaces[pairIndex].w = numLocalContactsOut;
  142. }
  143. for (int i = 0; i < numLocalContactsOut; i++)
  144. pVtxIn[i] = pVtxOut[i];
  145. } // if (hasSeparatingAxis[i])
  146. } // if (i<numPairs)
  147. }
  148. #endif //B3_CLIP_FACES_H