ball.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*************************************************************************
  2. * *
  3. * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. *
  4. * All rights reserved. Email: [email protected] Web: www.q12.org *
  5. * *
  6. * This library is free software; you can redistribute it and/or *
  7. * modify it under the terms of EITHER: *
  8. * (1) The GNU Lesser General Public License as published by the Free *
  9. * Software Foundation; either version 2.1 of the License, or (at *
  10. * your option) any later version. The text of the GNU Lesser *
  11. * General Public License is included with this library in the *
  12. * file LICENSE.TXT. *
  13. * (2) The BSD-style license that is included with this library in *
  14. * the file LICENSE-BSD.TXT. *
  15. * *
  16. * This library is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
  19. * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
  20. * *
  21. *************************************************************************/
  22. //234567890123456789012345678901234567890123456789012345678901234567890123456789
  23. // 1 2 3 4 5 6 7
  24. ////////////////////////////////////////////////////////////////////////////////
  25. // This file create unit test for some of the functions found in:
  26. // ode/src/joinst/ball.cpp
  27. //
  28. //
  29. ////////////////////////////////////////////////////////////////////////////////
  30. #include <UnitTest++.h>
  31. #include <ode/ode.h>
  32. #include "../../ode/src/config.h"
  33. #include "../../ode/src/joints/ball.h"
  34. using namespace std;
  35. SUITE (TestdxJointBall)
  36. {
  37. // The 2 bodies are positionned at (-1, -2, -3), and (11, 22, 33)
  38. // The bodis have rotation of 27deg around some axis.
  39. // The joint is a Ball Joint
  40. // Axis is along the X axis
  41. // Anchor at (0, 0, 0)
  42. struct dxJointBall_Fixture_B1_and_B2_At_Zero_Axis_Along_X {
  43. dxJointBall_Fixture_B1_and_B2_At_Zero_Axis_Along_X()
  44. {
  45. wId = dWorldCreate();
  46. for (int j=0; j<2; ++j) {
  47. bId[j][0] = dBodyCreate (wId);
  48. dBodySetPosition (bId[j][0], -1, -2, -3);
  49. bId[j][1] = dBodyCreate (wId);
  50. dBodySetPosition (bId[j][1], 11, 22, 33);
  51. dMatrix3 R;
  52. dVector3 axis; // Random axis
  53. axis[0] = REAL(0.53);
  54. axis[1] = -REAL(0.71);
  55. axis[2] = REAL(0.43);
  56. dNormalize3(axis);
  57. dRFromAxisAndAngle (R, axis[0], axis[1], axis[2],
  58. REAL(0.47123)); // 27deg
  59. dBodySetRotation (bId[j][0], R);
  60. axis[0] = REAL(1.2);
  61. axis[1] = REAL(0.87);
  62. axis[2] = -REAL(0.33);
  63. dNormalize3(axis);
  64. dRFromAxisAndAngle (R, axis[0], axis[1], axis[2],
  65. REAL(0.47123)); // 27deg
  66. dBodySetRotation (bId[j][1], R);
  67. jId[j] = dJointCreateBall (wId, 0);
  68. dJointAttach (jId[j], bId[j][0], bId[j][1]);
  69. }
  70. }
  71. ~dxJointBall_Fixture_B1_and_B2_At_Zero_Axis_Along_X()
  72. {
  73. dWorldDestroy (wId);
  74. }
  75. dWorldID wId;
  76. dBodyID bId[2][2];
  77. dJointID jId[2];
  78. };
  79. // Rotate 2nd body 90deg around X then back to original position
  80. //
  81. // ^ ^ ^
  82. // | | => | <---
  83. // | | |
  84. // B1 B2 B1 B2
  85. //
  86. // Start with a Delta of 90deg
  87. // ^ ^ ^
  88. // | <--- => | |
  89. // | | |
  90. // B1 B2 B1 B2
  91. TEST_FIXTURE (dxJointBall_Fixture_B1_and_B2_At_Zero_Axis_Along_X,
  92. test_dJointSetBallAxisOffset_B2_90deg) {
  93. dVector3 anchor;
  94. dJointGetBallAnchor(jId[1], anchor);
  95. dJointSetBallAnchor(jId[1], anchor[0], anchor[1], anchor[2]);
  96. for (int b=0; b<2; ++b) {
  97. // Compare body b of the first joint with its equivalent on the
  98. // second joint
  99. const dReal *qA = dBodyGetQuaternion(bId[0][b]);
  100. const dReal *qB = dBodyGetQuaternion(bId[1][b]);
  101. CHECK_CLOSE (qA[0], qB[0], 1e-4);
  102. CHECK_CLOSE (qA[1], qB[1], 1e-4);
  103. CHECK_CLOSE (qA[2], qB[2], 1e-4);
  104. CHECK_CLOSE (qA[3], qB[3], 1e-4);
  105. }
  106. dWorldStep (wId,0.5);
  107. dWorldStep (wId,0.5);
  108. dWorldStep (wId,0.5);
  109. dWorldStep (wId,0.5);
  110. for (int b=0; b<2; ++b) {
  111. // Compare body b of the first joint with its equivalent on the
  112. // second joint
  113. const dReal *qA = dBodyGetQuaternion(bId[0][b]);
  114. const dReal *qB = dBodyGetQuaternion(bId[1][b]);
  115. CHECK_CLOSE (qA[0], qB[0], 1e-4);
  116. CHECK_CLOSE (qA[1], qB[1], 1e-4);
  117. CHECK_CLOSE (qA[2], qB[2], 1e-4);
  118. CHECK_CLOSE (qA[3], qB[3], 1e-4);
  119. const dReal *posA = dBodyGetPosition(bId[0][b]);
  120. const dReal *posB = dBodyGetPosition(bId[1][b]);
  121. CHECK_CLOSE (posA[0], posB[0], 1e-4);
  122. CHECK_CLOSE (posA[1], posB[1], 1e-4);
  123. CHECK_CLOSE (posA[2], posB[2], 1e-4);
  124. CHECK_CLOSE (posA[3], posB[3], 1e-4);
  125. }
  126. }
  127. } // End of SUITE TestdxJointBall