collision_point_depth.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include <UnitTest++.h>
  2. #include <ode/ode.h>
  3. #include "common.h"
  4. TEST(test_collision_sphere_point_depth)
  5. {
  6. // Test case: sphere at the origin.
  7. {
  8. const dReal radius = 1;
  9. dGeomID sphere = dCreateSphere(0, radius);
  10. dGeomSetPosition(sphere, 0,0,0);
  11. // depth at center should equal radius
  12. CHECK_EQUAL(radius, dGeomSpherePointDepth(sphere, 0,0,0));
  13. // half-radius depth
  14. CHECK_EQUAL(0.5 * radius, dGeomSpherePointDepth(sphere, 0.5, 0, 0));
  15. CHECK_EQUAL(0.5 * radius, dGeomSpherePointDepth(sphere, 0, 0.5, 0));
  16. CHECK_EQUAL(0.5 * radius, dGeomSpherePointDepth(sphere, 0, 0, 0.5));
  17. CHECK_EQUAL(0.5 * radius, dGeomSpherePointDepth(sphere, -0.5, 0, 0));
  18. CHECK_EQUAL(0.5 * radius, dGeomSpherePointDepth(sphere, 0, -0.5, 0));
  19. CHECK_EQUAL(0.5 * radius, dGeomSpherePointDepth(sphere, 0, 0, -0.5));
  20. CHECK_CLOSE(0.5 * radius, dGeomSpherePointDepth(sphere, 0.3, 0.4, 0), 1e-12);
  21. CHECK_CLOSE(0.5 * radius, dGeomSpherePointDepth(sphere, 0, 0.3, 0.4), 1e-12);
  22. CHECK_CLOSE(0.5 * radius, dGeomSpherePointDepth(sphere, 0.4, 0, 0.3), 1e-12);
  23. CHECK_CLOSE(0.5 * radius, dGeomSpherePointDepth(sphere, -0.3, 0.4, 0), 1e-12);
  24. CHECK_CLOSE(0.5 * radius, dGeomSpherePointDepth(sphere, 0, -0.3, 0.4), 1e-12);
  25. CHECK_CLOSE(0.5 * radius, dGeomSpherePointDepth(sphere, 0.4, 0, -0.3), 1e-12);
  26. CHECK_CLOSE(0.5 * radius, dGeomSpherePointDepth(sphere, 0.3, -0.4, 0), 1e-12);
  27. CHECK_CLOSE(0.5 * radius, dGeomSpherePointDepth(sphere, 0, 0.3, -0.4), 1e-12);
  28. CHECK_CLOSE(0.5 * radius, dGeomSpherePointDepth(sphere, -0.4, 0, 0.3), 1e-12);
  29. CHECK_CLOSE(0.5 * radius, dGeomSpherePointDepth(sphere, -0.3, -0.4, 0), 1e-12);
  30. CHECK_CLOSE(0.5 * radius, dGeomSpherePointDepth(sphere, 0, -0.3, -0.4), 1e-12);
  31. CHECK_CLOSE(0.5 * radius, dGeomSpherePointDepth(sphere, -0.4, 0, -0.3), 1e-12);
  32. // 0.1 radius depth
  33. CHECK_CLOSE(0.1 * radius, dGeomSpherePointDepth(sphere, 0.9, 0, 0), 1e-12);
  34. CHECK_CLOSE(0.1 * radius, dGeomSpherePointDepth(sphere, 0, 0.9, 0), 1e-12);
  35. CHECK_CLOSE(0.1 * radius, dGeomSpherePointDepth(sphere, 0, 0, 0.9), 1e-12);
  36. CHECK_CLOSE(0.1 * radius, dGeomSpherePointDepth(sphere, -0.9, 0, 0), 1e-12);
  37. CHECK_CLOSE(0.1 * radius, dGeomSpherePointDepth(sphere, 0, -0.9, 0), 1e-12);
  38. CHECK_CLOSE(0.1 * radius, dGeomSpherePointDepth(sphere, 0, 0, -0.9), 1e-12);
  39. // on surface (zero depth)
  40. CHECK_EQUAL(0, dGeomSpherePointDepth(sphere, 1.0, 0, 0));
  41. CHECK_EQUAL(0, dGeomSpherePointDepth(sphere, 0, 1.0, 0));
  42. CHECK_EQUAL(0, dGeomSpherePointDepth(sphere, 0, 0, 1.0));
  43. CHECK_EQUAL(0, dGeomSpherePointDepth(sphere, -1.0, 0, 0));
  44. CHECK_EQUAL(0, dGeomSpherePointDepth(sphere, 0, -1.0, 0));
  45. CHECK_EQUAL(0, dGeomSpherePointDepth(sphere, 0, 0, -1.0));
  46. CHECK_CLOSE(0, dGeomSpherePointDepth(sphere, 0.6, 0.8, 0), 1e-12);
  47. CHECK_CLOSE(0, dGeomSpherePointDepth(sphere, 0, 0.6, 0.8), 1e-12);
  48. CHECK_CLOSE(0, dGeomSpherePointDepth(sphere, 0.8, 0, 0.6), 1e-12);
  49. CHECK_CLOSE(0, dGeomSpherePointDepth(sphere, -0.6, 0.8, 0), 1e-12);
  50. CHECK_CLOSE(0, dGeomSpherePointDepth(sphere, 0, -0.6, 0.8), 1e-12);
  51. CHECK_CLOSE(0, dGeomSpherePointDepth(sphere, 0.8, 0, -0.6), 1e-12);
  52. CHECK_CLOSE(0, dGeomSpherePointDepth(sphere, 0.6, -0.8, 0), 1e-12);
  53. CHECK_CLOSE(0, dGeomSpherePointDepth(sphere, 0, 0.6, -0.8), 1e-12);
  54. CHECK_CLOSE(0, dGeomSpherePointDepth(sphere, -0.8, 0, 0.6), 1e-12);
  55. CHECK_CLOSE(0, dGeomSpherePointDepth(sphere, -0.6, -0.8, 0), 1e-12);
  56. CHECK_CLOSE(0, dGeomSpherePointDepth(sphere, 0, -0.6, -0.8), 1e-12);
  57. CHECK_CLOSE(0, dGeomSpherePointDepth(sphere, -0.8, 0, -0.6), 1e-12);
  58. // 0.1 radius from surface (negative depth)
  59. CHECK_CLOSE(-0.1 * radius, dGeomSpherePointDepth(sphere, 1.1, 0, 0), 1e-12);
  60. CHECK_CLOSE(-0.1 * radius, dGeomSpherePointDepth(sphere, 0, 1.1, 0), 1e-12);
  61. CHECK_CLOSE(-0.1 * radius, dGeomSpherePointDepth(sphere, 0, 0, 1.1), 1e-12);
  62. CHECK_CLOSE(-0.1 * radius, dGeomSpherePointDepth(sphere, -1.1, 0, 0), 1e-12);
  63. CHECK_CLOSE(-0.1 * radius, dGeomSpherePointDepth(sphere, 0, -1.1, 0), 1e-12);
  64. CHECK_CLOSE(-0.1 * radius, dGeomSpherePointDepth(sphere, 0, 0, -1.1), 1e-12);
  65. // half-radius from surface (negative depth)
  66. CHECK_EQUAL(-0.5 * radius, dGeomSpherePointDepth(sphere, 1.5, 0, 0));
  67. CHECK_EQUAL(-0.5 * radius, dGeomSpherePointDepth(sphere, 0, 1.5, 0));
  68. CHECK_EQUAL(-0.5 * radius, dGeomSpherePointDepth(sphere, 0, 0, 1.5));
  69. CHECK_EQUAL(-0.5 * radius, dGeomSpherePointDepth(sphere, -1.5, 0, 0));
  70. CHECK_EQUAL(-0.5 * radius, dGeomSpherePointDepth(sphere, 0, -1.5, 0));
  71. CHECK_EQUAL(-0.5 * radius, dGeomSpherePointDepth(sphere, 0, 0, -1.5));
  72. CHECK_CLOSE(-0.5, dGeomSpherePointDepth(sphere, 0.9, 1.2, 0), 1e-12);
  73. CHECK_CLOSE(-0.5, dGeomSpherePointDepth(sphere, 0, 0.9, 1.2), 1e-12);
  74. CHECK_CLOSE(-0.5, dGeomSpherePointDepth(sphere, 1.2, 0, 0.9), 1e-12);
  75. CHECK_CLOSE(-0.5, dGeomSpherePointDepth(sphere, -0.9, 1.2, 0), 1e-12);
  76. CHECK_CLOSE(-0.5, dGeomSpherePointDepth(sphere, 0, -0.9, 1.2), 1e-12);
  77. CHECK_CLOSE(-0.5, dGeomSpherePointDepth(sphere, 1.2, 0, -0.9), 1e-12);
  78. CHECK_CLOSE(-0.5, dGeomSpherePointDepth(sphere, 0.9, -1.2, 0), 1e-12);
  79. CHECK_CLOSE(-0.5, dGeomSpherePointDepth(sphere, 0, 0.9, -1.2), 1e-12);
  80. CHECK_CLOSE(-0.5, dGeomSpherePointDepth(sphere, -1.2, 0, 0.9), 1e-12);
  81. CHECK_CLOSE(-0.5, dGeomSpherePointDepth(sphere, -0.9, -1.2, 0), 1e-12);
  82. CHECK_CLOSE(-0.5, dGeomSpherePointDepth(sphere, 0, -0.9, -1.2), 1e-12);
  83. CHECK_CLOSE(-0.5, dGeomSpherePointDepth(sphere, -1.2, 0, -0.9), 1e-12);
  84. }
  85. }
  86. TEST(test_collision_box_point_depth)
  87. {
  88. // Test case: cube at the origin.
  89. {
  90. const dReal length = 1;
  91. dGeomID cube = dCreateBox(0, 2*length, 2*length, 2*length);
  92. dGeomSetPosition(cube, 0,0,0);
  93. // depth at center should equal half length
  94. CHECK_EQUAL(length, dGeomBoxPointDepth(cube, 0,0,0));
  95. // half-length depth
  96. CHECK_EQUAL(0.5 * length, dGeomBoxPointDepth(cube, 0.5, 0, 0));
  97. CHECK_EQUAL(0.5 * length, dGeomBoxPointDepth(cube, 0, 0.5, 0));
  98. CHECK_EQUAL(0.5 * length, dGeomBoxPointDepth(cube, 0, 0, 0.5));
  99. CHECK_EQUAL(0.5 * length, dGeomBoxPointDepth(cube, -0.5, 0, 0));
  100. CHECK_EQUAL(0.5 * length, dGeomBoxPointDepth(cube, 0, -0.5, 0));
  101. CHECK_EQUAL(0.5 * length, dGeomBoxPointDepth(cube, 0, 0, -0.5));
  102. // closest point 0.6 * length
  103. CHECK_CLOSE(0.6 * length, dGeomBoxPointDepth(cube, 0.3, 0.4, 0), 1e-12);
  104. CHECK_CLOSE(0.6 * length, dGeomBoxPointDepth(cube, 0, 0.3, 0.4), 1e-12);
  105. CHECK_CLOSE(0.6 * length, dGeomBoxPointDepth(cube, 0.4, 0, 0.3), 1e-12);
  106. CHECK_CLOSE(0.6 * length, dGeomBoxPointDepth(cube, -0.3, 0.4, 0), 1e-12);
  107. CHECK_CLOSE(0.6 * length, dGeomBoxPointDepth(cube, 0, -0.3, 0.4), 1e-12);
  108. CHECK_CLOSE(0.6 * length, dGeomBoxPointDepth(cube, 0.4, 0, -0.3), 1e-12);
  109. CHECK_CLOSE(0.6 * length, dGeomBoxPointDepth(cube, 0.3, -0.4, 0), 1e-12);
  110. CHECK_CLOSE(0.6 * length, dGeomBoxPointDepth(cube, 0, 0.3, -0.4), 1e-12);
  111. CHECK_CLOSE(0.6 * length, dGeomBoxPointDepth(cube, -0.4, 0, 0.3), 1e-12);
  112. CHECK_CLOSE(0.6 * length, dGeomBoxPointDepth(cube, -0.3, -0.4, 0), 1e-12);
  113. CHECK_CLOSE(0.6 * length, dGeomBoxPointDepth(cube, 0, -0.3, -0.4), 1e-12);
  114. CHECK_CLOSE(0.6 * length, dGeomBoxPointDepth(cube, -0.4, 0, -0.3), 1e-12);
  115. // 0.1 length depth
  116. CHECK_CLOSE(0.1 * length, dGeomBoxPointDepth(cube, 0.9, 0, 0), 1e-12);
  117. CHECK_CLOSE(0.1 * length, dGeomBoxPointDepth(cube, 0, 0.9, 0), 1e-12);
  118. CHECK_CLOSE(0.1 * length, dGeomBoxPointDepth(cube, 0, 0, 0.9), 1e-12);
  119. CHECK_CLOSE(0.1 * length, dGeomBoxPointDepth(cube, -0.9, 0, 0), 1e-12);
  120. CHECK_CLOSE(0.1 * length, dGeomBoxPointDepth(cube, 0, -0.9, 0), 1e-12);
  121. CHECK_CLOSE(0.1 * length, dGeomBoxPointDepth(cube, 0, 0, -0.9), 1e-12);
  122. // on surface (zero depth)
  123. CHECK_EQUAL(0, dGeomBoxPointDepth(cube, 1.0, 0, 0));
  124. CHECK_EQUAL(0, dGeomBoxPointDepth(cube, 0, 1.0, 0));
  125. CHECK_EQUAL(0, dGeomBoxPointDepth(cube, 0, 0, 1.0));
  126. CHECK_EQUAL(0, dGeomBoxPointDepth(cube, -1.0, 0, 0));
  127. CHECK_EQUAL(0, dGeomBoxPointDepth(cube, 0, -1.0, 0));
  128. CHECK_EQUAL(0, dGeomBoxPointDepth(cube, 0, 0, -1.0));
  129. CHECK_EQUAL(0, dGeomBoxPointDepth(cube, 0.3, 1.0, 0));
  130. CHECK_EQUAL(0, dGeomBoxPointDepth(cube, 0, 0.3, 1.0));
  131. CHECK_EQUAL(0, dGeomBoxPointDepth(cube, 1.0, 0, 0.3));
  132. CHECK_EQUAL(0, dGeomBoxPointDepth(cube, -0.3, 1.0, 0));
  133. CHECK_EQUAL(0, dGeomBoxPointDepth(cube, 0, -0.3, 1.0));
  134. CHECK_EQUAL(0, dGeomBoxPointDepth(cube, 1.0, 0, -0.3));
  135. CHECK_EQUAL(0, dGeomBoxPointDepth(cube, 0.3, -1.0, 0));
  136. CHECK_EQUAL(0, dGeomBoxPointDepth(cube, 0, 0.3, -1.0));
  137. CHECK_EQUAL(0, dGeomBoxPointDepth(cube, -1.0, 0, 0.3));
  138. CHECK_EQUAL(0, dGeomBoxPointDepth(cube, -0.3, -1.0, 0));
  139. CHECK_EQUAL(0, dGeomBoxPointDepth(cube, 0, -0.3, -1.0));
  140. CHECK_EQUAL(0, dGeomBoxPointDepth(cube, -1.0, 0, -0.3));
  141. // 0.1 length from surface (negative depth)
  142. CHECK_CLOSE(-0.1 * length, dGeomBoxPointDepth(cube, 1.1, 0, 0), 1e-12);
  143. CHECK_CLOSE(-0.1 * length, dGeomBoxPointDepth(cube, 0, 1.1, 0), 1e-12);
  144. CHECK_CLOSE(-0.1 * length, dGeomBoxPointDepth(cube, 0, 0, 1.1), 1e-12);
  145. CHECK_CLOSE(-0.1 * length, dGeomBoxPointDepth(cube, -1.1, 0, 0), 1e-12);
  146. CHECK_CLOSE(-0.1 * length, dGeomBoxPointDepth(cube, 0, -1.1, 0), 1e-12);
  147. CHECK_CLOSE(-0.1 * length, dGeomBoxPointDepth(cube, 0, 0, -1.1), 1e-12);
  148. // half-length from surface face (negative depth)
  149. CHECK_EQUAL(-0.5 * length, dGeomBoxPointDepth(cube, 1.5, 0, 0));
  150. CHECK_EQUAL(-0.5 * length, dGeomBoxPointDepth(cube, 0, 1.5, 0));
  151. CHECK_EQUAL(-0.5 * length, dGeomBoxPointDepth(cube, 0, 0, 1.5));
  152. CHECK_EQUAL(-0.5 * length, dGeomBoxPointDepth(cube, -1.5, 0, 0));
  153. CHECK_EQUAL(-0.5 * length, dGeomBoxPointDepth(cube, 0, -1.5, 0));
  154. CHECK_EQUAL(-0.5 * length, dGeomBoxPointDepth(cube, 0, 0, -1.5));
  155. CHECK_EQUAL(-0.5 * length, dGeomBoxPointDepth(cube, 0.3, 1.5, 0));
  156. CHECK_EQUAL(-0.5 * length, dGeomBoxPointDepth(cube, 0, 0.3, 1.5));
  157. CHECK_EQUAL(-0.5 * length, dGeomBoxPointDepth(cube, 1.5, 0, 0.3));
  158. CHECK_EQUAL(-0.5 * length, dGeomBoxPointDepth(cube, -0.3, 1.5, 0));
  159. CHECK_EQUAL(-0.5 * length, dGeomBoxPointDepth(cube, 0, -0.3, 1.5));
  160. CHECK_EQUAL(-0.5 * length, dGeomBoxPointDepth(cube, 1.5, 0, -0.3));
  161. CHECK_EQUAL(-0.5 * length, dGeomBoxPointDepth(cube, 0.3, -1.5, 0));
  162. CHECK_EQUAL(-0.5 * length, dGeomBoxPointDepth(cube, 0, 0.3, -1.5));
  163. CHECK_EQUAL(-0.5 * length, dGeomBoxPointDepth(cube, -1.5, 0, 0.3));
  164. CHECK_EQUAL(-0.5 * length, dGeomBoxPointDepth(cube, -0.3, -1.5, 0));
  165. CHECK_EQUAL(-0.5 * length, dGeomBoxPointDepth(cube, 0, -0.3, -1.5));
  166. CHECK_EQUAL(-0.5 * length, dGeomBoxPointDepth(cube, -1.5, 0, -0.3));
  167. // half-length from surface edge (negative depth)
  168. CHECK_CLOSE(-0.5 * length, dGeomBoxPointDepth(cube, 1.3, 1.4, 0), 1e-12);
  169. CHECK_CLOSE(-0.5 * length, dGeomBoxPointDepth(cube, 0, 1.3, 1.4), 1e-12);
  170. CHECK_CLOSE(-0.5 * length, dGeomBoxPointDepth(cube, 1.4, 0, 1.3), 1e-12);
  171. CHECK_CLOSE(-0.5 * length, dGeomBoxPointDepth(cube, -1.3, 1.4, 0), 1e-12);
  172. CHECK_CLOSE(-0.5 * length, dGeomBoxPointDepth(cube, 0, -1.3, 1.4), 1e-12);
  173. CHECK_CLOSE(-0.5 * length, dGeomBoxPointDepth(cube, 1.4, 0, -1.3), 1e-12);
  174. CHECK_CLOSE(-0.5 * length, dGeomBoxPointDepth(cube, 1.3, -1.4, 0), 1e-12);
  175. CHECK_CLOSE(-0.5 * length, dGeomBoxPointDepth(cube, 0, 1.3, -1.4), 1e-12);
  176. CHECK_CLOSE(-0.5 * length, dGeomBoxPointDepth(cube, -1.4, 0, 1.3), 1e-12);
  177. CHECK_CLOSE(-0.5 * length, dGeomBoxPointDepth(cube, -1.3, -1.4, 0), 1e-12);
  178. CHECK_CLOSE(-0.5 * length, dGeomBoxPointDepth(cube, 0, -1.3, -1.4), 1e-12);
  179. CHECK_CLOSE(-0.5 * length, dGeomBoxPointDepth(cube, -1.4, 0, -1.3), 1e-12);
  180. // 0.6 length from corner (negative depth)
  181. CHECK_CLOSE(-0.6 * length, dGeomBoxPointDepth(cube, 1.2, 1.4, 1.4), 1e-12);
  182. CHECK_CLOSE(-0.6 * length, dGeomBoxPointDepth(cube, 1.4, 1.2, 1.4), 1e-12);
  183. CHECK_CLOSE(-0.6 * length, dGeomBoxPointDepth(cube, 1.4, 1.4, 1.2), 1e-12);
  184. CHECK_CLOSE(-0.6 * length, dGeomBoxPointDepth(cube, -1.2, 1.4, 1.4), 1e-12);
  185. CHECK_CLOSE(-0.6 * length, dGeomBoxPointDepth(cube, 1.4, -1.2, 1.4), 1e-12);
  186. CHECK_CLOSE(-0.6 * length, dGeomBoxPointDepth(cube, 1.4, 1.4, -1.2), 1e-12);
  187. CHECK_CLOSE(-0.6 * length, dGeomBoxPointDepth(cube, 1.2, -1.4, 1.4), 1e-12);
  188. CHECK_CLOSE(-0.6 * length, dGeomBoxPointDepth(cube, 1.4, 1.2, -1.4), 1e-12);
  189. CHECK_CLOSE(-0.6 * length, dGeomBoxPointDepth(cube, -1.4, 1.4, 1.2), 1e-12);
  190. CHECK_CLOSE(-0.6 * length, dGeomBoxPointDepth(cube, -1.2, -1.4, 1.4), 1e-12);
  191. CHECK_CLOSE(-0.6 * length, dGeomBoxPointDepth(cube, 1.4, -1.2, -1.4), 1e-12);
  192. CHECK_CLOSE(-0.6 * length, dGeomBoxPointDepth(cube, -1.4, 1.4, -1.2), 1e-12);
  193. }
  194. }