/src/test/test_vec.c

https://github.com/golems/amino · C · 161 lines · 80 code · 26 blank · 55 comment · 2 complexity · f3e88bd468e0d3b43c93579855f02db5 MD5 · raw file

  1. /* -*- mode: C; c-basic-offset: 4 -*- */
  2. /* ex: set shiftwidth=4 tabstop=4 expandtab: */
  3. /*
  4. * Copyright (c) 2010-2014, Georgia Tech Research Corporation
  5. * All rights reserved.
  6. *
  7. * Author(s): Neil T. Dantam <ntd@gatech.edu>
  8. * Georgia Tech Humanoid Robotics Lab
  9. * Under Direction of Prof. Mike Stilman <mstilman@cc.gatech.edu>
  10. *
  11. *
  12. * This file is provided under the following "BSD-style" License:
  13. *
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * * Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * * Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  28. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  29. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  32. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  33. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  34. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  35. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  36. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  37. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  38. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  39. * POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. */
  42. //#define AA_ALLOC_STACK_MAX
  43. #include "amino.h"
  44. #include "amino/test.h"
  45. #include "amino/vec.h"
  46. #include <assert.h>
  47. #include <stdio.h>
  48. #include <unistd.h>
  49. #include <inttypes.h>
  50. #include <sys/resource.h>
  51. double test_qu[8][4];
  52. static void quat(void)
  53. {
  54. double vr[8], mr[8];
  55. aa_vec_4d q0 = aa_vec_4d_ld(test_qu[0]);
  56. aa_vec_4d q1 = aa_vec_4d_ld(test_qu[1]);
  57. aa_vec_4d q2 = aa_vec_4d_ld(test_qu[2]);
  58. aa_vec_4d q3 = aa_vec_4d_ld(test_qu[3]);
  59. // qmul
  60. aa_vec_4d_st(vr, aa_vec_qmul(q0,q1) );
  61. aa_tf_qmul( test_qu[0], test_qu[1], mr );
  62. aveq( "qmul", 4, vr, mr, 0 );
  63. // qconj
  64. aa_vec_4d_st(vr, aa_vec_qconj(q0) );
  65. aa_tf_qconj( test_qu[0], mr );
  66. aveq( "qconj", 4, vr, mr, 0 );
  67. // rotation
  68. aa_vec_4d_st(vr, aa_vec_qrot( q0, q1 ) );
  69. aa_tf_qrot( test_qu[0], test_qu[1], mr );
  70. aveq( "qrot", 3, vr, mr, 1e-9 );
  71. // cross
  72. aa_vec_4d_st(vr, aa_vec_cross( q0, q1 ) );
  73. aa_tf_cross( test_qu[0], test_qu[1], mr );
  74. aveq( "cross", 3, vr, mr, 1e-9 );
  75. /* QUATERNION-VECTOR */
  76. // tf
  77. aa_vec_4d_st(vr, aa_vec_qv_tf( q0, q1, q2 ) );
  78. aa_tf_tf_qv( test_qu[0], test_qu[1], test_qu[2], mr );
  79. aveq( "qv-tf", 3, vr, mr, 1e-9 );
  80. // mul
  81. {
  82. aa_vec_4d tmp1, tmp2;
  83. AA_VEC_QV_MUL( q0, q1, q2, q3, tmp1, tmp2 );
  84. aa_vec_4d_st( vr, tmp1 );
  85. aa_vec_4d_st( vr+4, tmp2 );
  86. aa_tf_qv_chain( test_qu[0], test_qu[1],
  87. test_qu[2], test_qu[3],
  88. mr, mr+4 );
  89. aveq( "qv-mul", 7, vr, mr, 1e-9 );
  90. }
  91. }
  92. static void mat(void)
  93. {
  94. double T[12];
  95. aa_tf_quat2rotmat(test_qu[0], T);
  96. AA_MEM_CPY( T+9, test_qu[1], 3 );
  97. //aa_vec_4d q0 = aa_vec_4d_ld(test_qu[0]);
  98. aa_vec_4d q1 = aa_vec_4d_ld(test_qu[1]);
  99. aa_vec_4d q2 = aa_vec_4d_ld(test_qu[2]);
  100. double vr1[3], mr1[3];
  101. aa_vec_4d vR0, vR1, vR2;
  102. AA_VEC_ROTMAT_LD( vR0, vR1, vR2, T );
  103. aa_vec_4d Tc0, Tc1, Tc2, Tc3;
  104. AA_VEC_TFMAT_LD( Tc0, Tc1, Tc2, Tc3, T );
  105. // store
  106. {
  107. double T1[12];
  108. aa_vec_rotmat_st(T1, vR0, vR1, vR2 );
  109. aveq( "rotmat-st", 9, T, T1, 0 );
  110. aa_vec_tfmat_st(T1, Tc0, Tc1, Tc2, Tc3 );
  111. aveq( "tfmat-st", 12, T, T1, 0 );
  112. }
  113. // rotate
  114. aa_tf_9( T, test_qu[1], mr1 );
  115. aa_vec_3d_st( vr1, aa_vec_rotmat_tf( vR0, vR1, vR2, q1 ) );
  116. aveq( "rotmat-tf", 3, vr1, mr1, 1e-9 );
  117. // transform
  118. aa_tf_12( T, test_qu[2], mr1 );
  119. aa_vec_3d_st( vr1, aa_vec_tfmat_tf( Tc0, Tc1, Tc2, Tc3, q2 ) );
  120. aveq( "tfmat-tf", 3, vr1, mr1, 1e-9 );
  121. }
  122. int main( int argc, char **argv ) {
  123. (void) argc; (void) argv;
  124. // init
  125. srand((unsigned int)time(NULL)); // might break in 2038
  126. aa_test_ulimit();
  127. for( size_t i = 0; i < 1000; i++ ) {
  128. // random data
  129. for( size_t j = 0; j < sizeof(test_qu)/sizeof(test_qu[0]); j++ ) {
  130. aa_tf_qurand( test_qu[j] );
  131. }
  132. quat();
  133. mat();
  134. }
  135. }