You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

189 line
6.3 KiB

  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. {
  71. // int i = get_global_id(0);
  72. //int pairIndex = i;
  73. int i = pairIndex;
  74. float minDist = -1e30f;
  75. float maxDist = 0.02f;
  76. // if (i<numPairs)
  77. {
  78. if (hasSeparatingAxis[i])
  79. {
  80. // int bodyIndexA = pairs[i].x;
  81. // int bodyIndexB = pairs[i].y;
  82. int numLocalContactsOut = 0;
  83. int capacityWorldVertsB2 = vertexFaceCapacity;
  84. __global b3Float4* pVtxIn = &worldVertsB1[pairIndex*capacityWorldVertsB2];
  85. __global b3Float4* pVtxOut = &worldVertsB2[pairIndex*capacityWorldVertsB2];
  86. {
  87. __global b3Int4* clippingFaces = clippingFacesOut;
  88. int closestFaceA = clippingFaces[pairIndex].x;
  89. int closestFaceB = clippingFaces[pairIndex].y;
  90. int numVertsInA = clippingFaces[pairIndex].z;
  91. int numVertsInB = clippingFaces[pairIndex].w;
  92. int numVertsOut = 0;
  93. if (closestFaceA>=0)
  94. {
  95. // clip polygon to back of planes of all faces of hull A that are adjacent to witness face
  96. for(int e0=0;e0<numVertsInA;e0++)
  97. {
  98. const b3Float4 aw = worldVertsA1[pairIndex*capacityWorldVertsB2+e0];
  99. const b3Float4 bw = worldVertsA1[pairIndex*capacityWorldVertsB2+((e0+1)%numVertsInA)];
  100. const b3Float4 WorldEdge0 = aw - bw;
  101. b3Float4 worldPlaneAnormal1 = worldNormalsA1[pairIndex];
  102. b3Float4 planeNormalWS1 = -b3Cross(WorldEdge0,worldPlaneAnormal1);
  103. b3Float4 worldA1 = aw;
  104. float planeEqWS1 = -b3Dot(worldA1,planeNormalWS1);
  105. b3Float4 planeNormalWS = planeNormalWS1;
  106. float planeEqWS=planeEqWS1;
  107. numVertsOut = clipFaceGlobal(pVtxIn, numVertsInB, planeNormalWS,planeEqWS, pVtxOut);
  108. __global b3Float4* tmp = pVtxOut;
  109. pVtxOut = pVtxIn;
  110. pVtxIn = tmp;
  111. numVertsInB = numVertsOut;
  112. numVertsOut = 0;
  113. }
  114. b3Float4 planeNormalWS = worldNormalsA1[pairIndex];
  115. float planeEqWS=-b3Dot(planeNormalWS,worldVertsA1[pairIndex*capacityWorldVertsB2]);
  116. for (int i=0;i<numVertsInB;i++)
  117. {
  118. float depth = b3Dot(planeNormalWS,pVtxIn[i])+planeEqWS;
  119. if (depth <=minDist)
  120. {
  121. depth = minDist;
  122. }
  123. /*
  124. static float maxDepth = 0.f;
  125. if (depth < maxDepth)
  126. {
  127. maxDepth = depth;
  128. if (maxDepth < -10)
  129. {
  130. printf("error at framecount %d?\n",myframecount);
  131. }
  132. printf("maxDepth = %f\n", maxDepth);
  133. }
  134. */
  135. if (depth <=maxDist)
  136. {
  137. b3Float4 pointInWorld = pVtxIn[i];
  138. pVtxOut[numLocalContactsOut++] = b3MakeFloat4(pointInWorld.x,pointInWorld.y,pointInWorld.z,depth);
  139. }
  140. }
  141. }
  142. clippingFaces[pairIndex].w =numLocalContactsOut;
  143. }
  144. for (int i=0;i<numLocalContactsOut;i++)
  145. pVtxIn[i] = pVtxOut[i];
  146. }// if (hasSeparatingAxis[i])
  147. }// if (i<numPairs)
  148. }
  149. #endif //B3_CLIP_FACES_H