hinge2.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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/hinge2.cpp
  27. //
  28. //
  29. ////////////////////////////////////////////////////////////////////////////////
  30. #include <UnitTest++.h>
  31. #include <ode/ode.h>
  32. #include "../../ode/src/joints/hinge2.h"
  33. using namespace std;
  34. SUITE (TestdxJointHinge2)
  35. {
  36. // The 2 bodies are positionned at (-1, -2, -3), and (11, 22, 33)
  37. // The bodis have rotation of 27deg around some axis.
  38. // The joint is a Hinge2 Joint
  39. // Axis is along the X axis
  40. // Anchor at (0, 0, 0)
  41. struct dxJointHinge2_Fixture_B1_and_B2_At_Zero_Axis_Along_X {
  42. dxJointHinge2_Fixture_B1_and_B2_At_Zero_Axis_Along_X()
  43. {
  44. wId = dWorldCreate();
  45. for (int j=0; j<2; ++j) {
  46. bId[j][0] = dBodyCreate (wId);
  47. dBodySetPosition (bId[j][0], -1, -2, -3);
  48. bId[j][1] = dBodyCreate (wId);
  49. dBodySetPosition (bId[j][1], 11, 22, 33);
  50. dMatrix3 R;
  51. dVector3 axis; // Random axis
  52. axis[0] = REAL(0.53);
  53. axis[1] = -REAL(0.71);
  54. axis[2] = REAL(0.43);
  55. dNormalize3(axis);
  56. dRFromAxisAndAngle (R, axis[0], axis[1], axis[2],
  57. REAL(0.47123)); // 27deg
  58. dBodySetRotation (bId[j][0], R);
  59. axis[0] = REAL(1.2);
  60. axis[1] = REAL(0.87);
  61. axis[2] = -REAL(0.33);
  62. dNormalize3(axis);
  63. dRFromAxisAndAngle (R, axis[0], axis[1], axis[2],
  64. REAL(0.47123)); // 27deg
  65. dBodySetRotation (bId[j][1], R);
  66. jId[j] = dJointCreateHinge2 (wId, 0);
  67. dJointAttach (jId[j], bId[j][0], bId[j][1]);
  68. }
  69. }
  70. ~dxJointHinge2_Fixture_B1_and_B2_At_Zero_Axis_Along_X()
  71. {
  72. dWorldDestroy (wId);
  73. }
  74. dWorldID wId;
  75. dBodyID bId[2][2];
  76. dJointID jId[2];
  77. };
  78. // Rotate 2nd body 90deg around X then back to original position
  79. //
  80. // ^ ^ ^
  81. // | | => | <---
  82. // | | |
  83. // B1 B2 B1 B2
  84. //
  85. // Start with a Delta of 90deg
  86. // ^ ^ ^
  87. // | <--- => | |
  88. // | | |
  89. // B1 B2 B1 B2
  90. TEST_FIXTURE (dxJointHinge2_Fixture_B1_and_B2_At_Zero_Axis_Along_X,
  91. test_dJointSetHinge2AxisOffset_B2_90deg) {
  92. dVector3 anchor;
  93. dJointGetHinge2Anchor(jId[1], anchor);
  94. dJointSetHinge2Anchor(jId[1], anchor[0], anchor[1], anchor[2]);
  95. dVector3 axis;
  96. dJointGetHinge2Axis1(jId[1], axis);
  97. dJointSetHinge2Axis1(jId[1], axis[0], axis[1], axis[2]);
  98. dJointGetHinge2Axis2(jId[1], axis);
  99. dJointSetHinge2Axis2(jId[1], axis[0], axis[1], axis[2]);
  100. for (int b=0; b<2; ++b) {
  101. // Compare body b of the first joint with its equivalent on the
  102. // second joint
  103. const dReal *qA = dBodyGetQuaternion(bId[0][b]);
  104. const dReal *qB = dBodyGetQuaternion(bId[1][b]);
  105. CHECK_CLOSE (qA[0], qB[0], 1e-4);
  106. CHECK_CLOSE (qA[1], qB[1], 1e-4);
  107. CHECK_CLOSE (qA[2], qB[2], 1e-4);
  108. CHECK_CLOSE (qA[3], qB[3], 1e-4);
  109. }
  110. dWorldStep (wId,0.5);
  111. dWorldStep (wId,0.5);
  112. dWorldStep (wId,0.5);
  113. dWorldStep (wId,0.5);
  114. for (int b=0; b<2; ++b) {
  115. // Compare body b of the first joint with its equivalent on the
  116. // second joint
  117. const dReal *qA = dBodyGetQuaternion(bId[0][b]);
  118. const dReal *qB = dBodyGetQuaternion(bId[1][b]);
  119. CHECK_CLOSE (qA[0], qB[0], 1e-4);
  120. CHECK_CLOSE (qA[1], qB[1], 1e-4);
  121. CHECK_CLOSE (qA[2], qB[2], 1e-4);
  122. CHECK_CLOSE (qA[3], qB[3], 1e-4);
  123. const dReal *posA = dBodyGetPosition(bId[0][b]);
  124. const dReal *posB = dBodyGetPosition(bId[1][b]);
  125. CHECK_CLOSE (posA[0], posB[0], 1e-4);
  126. CHECK_CLOSE (posA[1], posB[1], 1e-4);
  127. CHECK_CLOSE (posA[2], posB[2], 1e-4);
  128. CHECK_CLOSE (posA[3], posB[3], 1e-4);
  129. }
  130. }
  131. } // End of SUITE TestdxJointHinge2