arm_quaternion2rotation_f32.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_quaternion2rotation_f32.c
  4. * Description: Floating-point quaternion 2 rotation conversion
  5. *
  6. * $Date: 23 April 2021
  7. * $Revision: V1.9.0
  8. *
  9. * Target Processor: Cortex-M and Cortex-A cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2021 ARM Limited or its affiliates. All rights reserved.
  13. *
  14. * SPDX-License-Identifier: Apache-2.0
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the License); you may
  17. * not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  24. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. */
  28. #include "dsp/quaternion_math_functions.h"
  29. #include <math.h>
  30. /**
  31. @ingroup groupQuaternionMath
  32. */
  33. /**
  34. @defgroup QuatConv Quaternion conversions
  35. Conversions between quaternion and rotation representations.
  36. */
  37. /**
  38. @ingroup QuatConv
  39. */
  40. /**
  41. @defgroup QuatRot Quaternion to Rotation
  42. Conversions from quaternion to rotation.
  43. */
  44. /**
  45. @addtogroup QuatRot
  46. @{
  47. */
  48. /**
  49. @brief Conversion of quaternion to equivalent rotation matrix.
  50. @param[in] pInputQuaternions points to an array of normalized quaternions
  51. @param[out] pOutputRotations points to an array of 3x3 rotations (in row order)
  52. @param[in] nbQuaternions number of quaternions in the array
  53. @return none.
  54. @par
  55. Format of rotation matrix
  56. The quaternion a + ib + jc + kd is converted into rotation matrix:
  57. <pre>
  58. a^2 + b^2 - c^2 - d^2 2bc - 2ad 2bd + 2ac
  59. 2bc + 2ad a^2 - b^2 + c^2 - d^2 2cd - 2ab
  60. 2bd - 2ac 2cd + 2ab a^2 - b^2 - c^2 + d^2
  61. </pre>
  62. Rotation matrix is saved in row order : R00 R01 R02 R10 R11 R12 R20 R21 R22
  63. */
  64. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  65. #include "arm_helium_utils.h"
  66. void arm_quaternion2rotation_f32(const float32_t *pInputQuaternions,
  67. float32_t *pOutputRotations,
  68. uint32_t nbQuaternions)
  69. {
  70. f32x4_t vec0,vec1, vec2 ,vec3;
  71. float32_t q2q3, tmp1, tmp2 ;
  72. for(uint32_t nb=0; nb < nbQuaternions; nb++)
  73. {
  74. // q0 q1 q2 q3
  75. vec0 = vld1q(pInputQuaternions);
  76. // q0^2 q1^2 q2^2 q3^2
  77. vec1 = vmulq(vec0,vec0);
  78. // q0^2 q1q0 q2q0 q3q0
  79. vec2 = vmulq_n_f32(vec0, vgetq_lane(vec0,0));
  80. // 2 (q0^2 q1q0 q2q0 q3q0)
  81. vec2 = vmulq_n_f32(vec2, 2.0f);
  82. // 2 q2q3
  83. q2q3 = vgetq_lane(vec0,2) * vgetq_lane(vec0,3);
  84. q2q3 = q2q3 * 2.0f;
  85. // 2 (q0q1 q1^2 q2q1 q3q1)
  86. vec3 = vmulq_n_f32(vec0, vgetq_lane(vec0,1));
  87. vec3 = vmulq_n_f32(vec3, 2.0f);
  88. vec0 = vsetq_lane(vgetq_lane(vec1,0) + vgetq_lane(vec1,1),vec0,0);
  89. vec0 = vsetq_lane(vgetq_lane(vec0,0) - vgetq_lane(vec1,2),vec0,0);
  90. vec0 = vsetq_lane(vgetq_lane(vec0,0) - vgetq_lane(vec1,3),vec0,0);
  91. vec0 = vsetq_lane(vgetq_lane(vec3,2) - vgetq_lane(vec2,3),vec0,1);
  92. vec0 = vsetq_lane(vgetq_lane(vec3,3) + vgetq_lane(vec2,2),vec0,2);
  93. vec0 = vsetq_lane(vgetq_lane(vec3,2) + vgetq_lane(vec2,3),vec0,3);
  94. vst1q(pOutputRotations, vec0);
  95. pOutputRotations += 4;
  96. tmp1 = vgetq_lane(vec1,0) - vgetq_lane(vec1,1);
  97. tmp2 = vgetq_lane(vec1,2) - vgetq_lane(vec1,3);
  98. vec0 = vsetq_lane(tmp1 + tmp2,vec0,0);
  99. vec0 = vsetq_lane(q2q3 - vgetq_lane(vec2,1) ,vec0,1);
  100. vec0 = vsetq_lane(vgetq_lane(vec3,3) - vgetq_lane(vec2,2),vec0,2);
  101. vec0 = vsetq_lane(q2q3 + vgetq_lane(vec2,1) ,vec0,3);
  102. vst1q(pOutputRotations, vec0);
  103. pOutputRotations += 4;
  104. *pOutputRotations = tmp1 - tmp2;
  105. pOutputRotations ++;
  106. pInputQuaternions += 4;
  107. }
  108. }
  109. #else
  110. void arm_quaternion2rotation_f32(const float32_t *pInputQuaternions,
  111. float32_t *pOutputRotations,
  112. uint32_t nbQuaternions)
  113. {
  114. uint32_t nb;
  115. for(nb=0; nb < nbQuaternions; nb++)
  116. {
  117. float32_t q00 = SQ(pInputQuaternions[0 + nb * 4]);
  118. float32_t q11 = SQ(pInputQuaternions[1 + nb * 4]);
  119. float32_t q22 = SQ(pInputQuaternions[2 + nb * 4]);
  120. float32_t q33 = SQ(pInputQuaternions[3 + nb * 4]);
  121. float32_t q01 = pInputQuaternions[0 + nb * 4]*pInputQuaternions[1 + nb * 4];
  122. float32_t q02 = pInputQuaternions[0 + nb * 4]*pInputQuaternions[2 + nb * 4];
  123. float32_t q03 = pInputQuaternions[0 + nb * 4]*pInputQuaternions[3 + nb * 4];
  124. float32_t q12 = pInputQuaternions[1 + nb * 4]*pInputQuaternions[2 + nb * 4];
  125. float32_t q13 = pInputQuaternions[1 + nb * 4]*pInputQuaternions[3 + nb * 4];
  126. float32_t q23 = pInputQuaternions[2 + nb * 4]*pInputQuaternions[3 + nb * 4];
  127. float32_t xx = q00 + q11 - q22 - q33;
  128. float32_t yy = q00 - q11 + q22 - q33;
  129. float32_t zz = q00 - q11 - q22 + q33;
  130. float32_t xy = 2*(q12 - q03);
  131. float32_t xz = 2*(q13 + q02);
  132. float32_t yx = 2*(q12 + q03);
  133. float32_t yz = 2*(q23 - q01);
  134. float32_t zx = 2*(q13 - q02);
  135. float32_t zy = 2*(q23 + q01);
  136. pOutputRotations[0 + nb * 9] = xx; pOutputRotations[1 + nb * 9] = xy; pOutputRotations[2 + nb * 9] = xz;
  137. pOutputRotations[3 + nb * 9] = yx; pOutputRotations[4 + nb * 9] = yy; pOutputRotations[5 + nb * 9] = yz;
  138. pOutputRotations[6 + nb * 9] = zx; pOutputRotations[7 + nb * 9] = zy; pOutputRotations[8 + nb * 9] = zz;
  139. }
  140. }
  141. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  142. /**
  143. @} end of QuatRot group
  144. */