main.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2014 Google Inc. http://bulletphysics.org
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. ///Original author: Erwin Coumans, October 2014
  14. ///Initial version of this low-level GJK/EPA/MPR convex-convex collision test
  15. ///You can provide your own support function in combination with the template functions
  16. ///See btComputeGjkEpaSphereSphereCollision below for an example
  17. ///Todo: the test needs proper coverage and using a convex hull point cloud
  18. ///Also the GJK, EPA and MPR should be improved, both quality and performance
  19. #include <gtest/gtest.h>
  20. #include "SphereSphereCollision.h"
  21. #include "BulletCollision/CollisionShapes/btSphereShape.h"
  22. #include "BulletCollision/CollisionShapes/btMultiSphereShape.h"
  23. #include "BulletCollision/NarrowPhaseCollision/btComputeGjkEpaPenetration.h"
  24. #include "BulletCollision/NarrowPhaseCollision/btGjkEpa3.h"
  25. #include "BulletCollision/NarrowPhaseCollision/btMprPenetration.h"
  26. btVector3 MyBulletShapeSupportFunc(const void* shapeAptr, const btVector3& dir, bool includeMargin)
  27. {
  28. btConvexShape* shape = (btConvexShape*) shapeAptr;
  29. if (includeMargin)
  30. {
  31. return shape->localGetSupportingVertex(dir);
  32. }
  33. return shape->localGetSupportingVertexWithoutMargin(dir);
  34. }
  35. btVector3 MyBulletShapeCenterFunc(const void* shapeAptr)
  36. {
  37. return btVector3(0,0,0);
  38. }
  39. enum SphereSphereTestMethod
  40. {
  41. SSTM_ANALYTIC,
  42. SSTM_GJKEPA,
  43. SSTM_GJKEPA_RADIUS_NOT_FULL_MARGIN,
  44. SSTM_GJKMPR
  45. };
  46. struct ConvexWrap
  47. {
  48. btConvexShape* m_convex;
  49. btTransform m_worldTrans;
  50. inline btScalar getMargin() const
  51. {
  52. return m_convex->getMargin();
  53. }
  54. inline btVector3 getObjectCenterInWorld() const
  55. {
  56. return m_worldTrans.getOrigin();
  57. }
  58. inline const btTransform& getWorldTransform() const
  59. {
  60. return m_worldTrans;
  61. }
  62. inline btVector3 getLocalSupportWithMargin(const btVector3& dir) const
  63. {
  64. return m_convex->localGetSupportingVertex(dir);
  65. }
  66. inline btVector3 getLocalSupportWithoutMargin(const btVector3& dir) const
  67. {
  68. return m_convex->localGetSupportingVertexWithoutMargin(dir);
  69. }
  70. };
  71. inline int btComputeGjkEpaSphereSphereCollision(const btSphereSphereCollisionDescription& input, btDistanceInfo* distInfo,SphereSphereTestMethod method)
  72. {
  73. ///for spheres it is best to use a 'point' and set the margin to the radius (which is what btSphereShape does)
  74. btSphereShape singleSphereA(input.m_radiusA);
  75. btSphereShape singleSphereB(input.m_radiusB);
  76. btVector3 org(0,0,0);
  77. btScalar radA =input.m_radiusA;
  78. btScalar radB =input.m_radiusB;
  79. ConvexWrap a,b;
  80. a.m_worldTrans = input.m_sphereTransformA;
  81. b.m_worldTrans = input.m_sphereTransformB;;
  82. btMultiSphereShape multiSphereA(&org,&radA,1);
  83. btMultiSphereShape multiSphereB(&org,&radB,1);
  84. btGjkCollisionDescription colDesc;
  85. switch (method)
  86. {
  87. case SSTM_GJKEPA_RADIUS_NOT_FULL_MARGIN:
  88. {
  89. a.m_convex = &multiSphereA;
  90. b.m_convex = &multiSphereB;
  91. break;
  92. }
  93. default:
  94. {
  95. a.m_convex = &singleSphereA;
  96. b.m_convex = &singleSphereB;
  97. }
  98. };
  99. btVoronoiSimplexSolver simplexSolver;
  100. simplexSolver.reset();
  101. int res=-1;
  102. ///todo(erwincoumans): improve convex-convex quality and performance
  103. ///also compare with https://code.google.com/p/bullet/source/browse/branches/PhysicsEffects/src/base_level/collision/pfx_gjk_solver.cpp
  104. switch (method)
  105. {
  106. case SSTM_GJKEPA_RADIUS_NOT_FULL_MARGIN:
  107. case SSTM_GJKEPA:
  108. {
  109. res = btComputeGjkEpaPenetration(a,b,colDesc,simplexSolver, distInfo);
  110. break;
  111. }
  112. case SSTM_GJKMPR:
  113. {
  114. res = btComputeGjkDistance(a,b,colDesc,distInfo);
  115. if (res==0)
  116. {
  117. // printf("use GJK results in distance %f\n",distInfo->m_distance);
  118. return res;
  119. } else
  120. {
  121. btMprCollisionDescription mprDesc;
  122. res = btComputeMprPenetration(a,b,mprDesc, distInfo);
  123. // if (res==0)
  124. // {
  125. // printf("use MPR results in distance %f\n",distInfo->m_distance);
  126. // }
  127. }
  128. break;
  129. }
  130. default:
  131. {
  132. btAssert(0);
  133. }
  134. }
  135. return res;
  136. }
  137. void testSphereSphereDistance(SphereSphereTestMethod method, btScalar abs_error)
  138. {
  139. {
  140. btSphereSphereCollisionDescription ssd;
  141. ssd.m_sphereTransformA.setIdentity();
  142. ssd.m_sphereTransformB.setIdentity();
  143. ssd.m_radiusA = 0.f;
  144. ssd.m_radiusB = 0.f;
  145. btDistanceInfo distInfo;
  146. int result = btComputeSphereSphereCollision(ssd,&distInfo);
  147. ASSERT_EQ(0,result);
  148. ASSERT_EQ(btScalar(0), distInfo.m_distance);
  149. }
  150. for (int rb=1;rb<5;rb++)
  151. for (int z=-5;z<5;z++)
  152. {
  153. for (int j=1;j<5;j++)
  154. {
  155. for (int i=-5;i<5;i++)
  156. {
  157. if (i!=z)//skip co-centric spheres for now (todo(erwincoumans) fix this)
  158. {
  159. btSphereSphereCollisionDescription ssd;
  160. ssd.m_sphereTransformA.setIdentity();
  161. ssd.m_sphereTransformA.setOrigin(btVector3(0,btScalar(i),0));
  162. ssd.m_sphereTransformB.setIdentity();
  163. ssd.m_sphereTransformB.setOrigin(btVector3(0,btScalar(z),0));
  164. ssd.m_radiusA = btScalar(j);
  165. ssd.m_radiusB = btScalar(rb)*btScalar(0.1);
  166. btDistanceInfo distInfo;
  167. int result=-1;
  168. switch (method)
  169. {
  170. case SSTM_ANALYTIC:
  171. {
  172. result = btComputeSphereSphereCollision(ssd,&distInfo);
  173. break;
  174. }
  175. case SSTM_GJKMPR:
  176. case SSTM_GJKEPA:
  177. case SSTM_GJKEPA_RADIUS_NOT_FULL_MARGIN:
  178. {
  179. result = btComputeGjkEpaSphereSphereCollision(ssd,&distInfo, method);
  180. break;
  181. }
  182. default:
  183. {
  184. ASSERT_EQ(0,1);
  185. btAssert(0);
  186. break;
  187. }
  188. }
  189. // int result = btComputeSphereSphereCollision(ssd,&distInfo);
  190. #if 0
  191. printf("sphereA(pos=[%f,%f,%f],r=%f)-sphereB(pos=[%f,%f,%f],r=%f) Dist=%f,normalOnB[%f,%f,%f],pA=[%f,%f,%f],pB[%f,%f,%f]\n",
  192. ssd.m_sphereTransformA.getOrigin()[0],ssd.m_sphereTransformA.getOrigin()[1],ssd.m_sphereTransformA.getOrigin()[2],ssd.m_radiusA,
  193. ssd.m_sphereTransformB.getOrigin()[0],ssd.m_sphereTransformB.getOrigin()[1],ssd.m_sphereTransformB.getOrigin()[2],ssd.m_radiusB,
  194. distInfo.m_distance,distInfo.m_normalBtoA[0],distInfo.m_normalBtoA[1],distInfo.m_normalBtoA[2],
  195. distInfo.m_pointOnA[0],distInfo.m_pointOnA[1],distInfo.m_pointOnA[2],
  196. distInfo.m_pointOnB[0],distInfo.m_pointOnB[1],distInfo.m_pointOnB[2]);
  197. #endif
  198. ASSERT_EQ(0,result);
  199. ASSERT_NEAR(btFabs(btScalar(i-z))-btScalar(j)-ssd.m_radiusB, distInfo.m_distance, abs_error);
  200. btVector3 computedA = distInfo.m_pointOnB+distInfo.m_distance*distInfo.m_normalBtoA;
  201. ASSERT_NEAR(computedA.x(),distInfo.m_pointOnA.x(),abs_error);
  202. ASSERT_NEAR(computedA.y(),distInfo.m_pointOnA.y(),abs_error);
  203. ASSERT_NEAR(computedA.z(),distInfo.m_pointOnA.z(),abs_error);
  204. }
  205. }
  206. }
  207. }
  208. }
  209. TEST(BulletCollisionTest, GjkMPRSphereSphereDistance) {
  210. testSphereSphereDistance(SSTM_GJKMPR, 0.0001);
  211. }
  212. TEST(BulletCollisionTest, GjkEpaSphereSphereDistance) {
  213. testSphereSphereDistance(SSTM_GJKEPA, 0.00001);
  214. }
  215. TEST(BulletCollisionTest, GjkEpaSphereSphereRadiusNotFullMarginDistance) {
  216. testSphereSphereDistance(SSTM_GJKEPA_RADIUS_NOT_FULL_MARGIN, 0.1);
  217. }
  218. TEST(BulletCollisionTest, AnalyticSphereSphereDistance) {
  219. testSphereSphereDistance(SSTM_ANALYTIC, 0.00001);
  220. }
  221. int main(int argc, char **argv) {
  222. #if _MSC_VER
  223. _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
  224. //void *testWhetherMemoryLeakDetectionWorks = malloc(1);
  225. #endif
  226. ::testing::InitGoogleTest(&argc, argv);
  227. return RUN_ALL_TESTS();
  228. }