arm_quaternion2rotation_f32.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. @par
  54. Format of rotation matrix
  55. The quaternion a + ib + jc + kd is converted into rotation matrix:
  56. <pre>
  57. a^2 + b^2 - c^2 - d^2 2bc - 2ad 2bd + 2ac
  58. 2bc + 2ad a^2 - b^2 + c^2 - d^2 2cd - 2ab
  59. 2bd - 2ac 2cd + 2ab a^2 - b^2 - c^2 + d^2
  60. </pre>
  61. Rotation matrix is saved in row order : R00 R01 R02 R10 R11 R12 R20 R21 R22
  62. */
  63. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  64. #include "arm_helium_utils.h"
  65. void arm_quaternion2rotation_f32(const float32_t *pInputQuaternions,
  66. float32_t *pOutputRotations,
  67. uint32_t nbQuaternions)
  68. {
  69. f32x4_t vec0,vec1, vec2 ,vec3;
  70. float32_t q2q3, tmp1, tmp2 ;
  71. for(uint32_t nb=0; nb < nbQuaternions; nb++)
  72. {
  73. // q0 q1 q2 q3
  74. vec0 = vld1q(pInputQuaternions);
  75. // q0^2 q1^2 q2^2 q3^2
  76. vec1 = vmulq(vec0,vec0);
  77. // q0^2 q1q0 q2q0 q3q0
  78. vec2 = vmulq_n_f32(vec0, vgetq_lane(vec0,0));
  79. // 2 (q0^2 q1q0 q2q0 q3q0)
  80. vec2 = vmulq_n_f32(vec2, 2.0f);
  81. // 2 q2q3
  82. q2q3 = vgetq_lane(vec0,2) * vgetq_lane(vec0,3);
  83. q2q3 = q2q3 * 2.0f;
  84. // 2 (q0q1 q1^2 q2q1 q3q1)
  85. vec3 = vmulq_n_f32(vec0, vgetq_lane(vec0,1));
  86. vec3 = vmulq_n_f32(vec3, 2.0f);
  87. vec0 = vsetq_lane(vgetq_lane(vec1,0) + vgetq_lane(vec1,1),vec0,0);
  88. vec0 = vsetq_lane(vgetq_lane(vec0,0) - vgetq_lane(vec1,2),vec0,0);
  89. vec0 = vsetq_lane(vgetq_lane(vec0,0) - vgetq_lane(vec1,3),vec0,0);
  90. vec0 = vsetq_lane(vgetq_lane(vec3,2) - vgetq_lane(vec2,3),vec0,1);
  91. vec0 = vsetq_lane(vgetq_lane(vec3,3) + vgetq_lane(vec2,2),vec0,2);
  92. vec0 = vsetq_lane(vgetq_lane(vec3,2) + vgetq_lane(vec2,3),vec0,3);
  93. vst1q(pOutputRotations, vec0);
  94. pOutputRotations += 4;
  95. tmp1 = vgetq_lane(vec1,0) - vgetq_lane(vec1,1);
  96. tmp2 = vgetq_lane(vec1,2) - vgetq_lane(vec1,3);
  97. vec0 = vsetq_lane(tmp1 + tmp2,vec0,0);
  98. vec0 = vsetq_lane(q2q3 - vgetq_lane(vec2,1) ,vec0,1);
  99. vec0 = vsetq_lane(vgetq_lane(vec3,3) - vgetq_lane(vec2,2),vec0,2);
  100. vec0 = vsetq_lane(q2q3 + vgetq_lane(vec2,1) ,vec0,3);
  101. vst1q(pOutputRotations, vec0);
  102. pOutputRotations += 4;
  103. *pOutputRotations = tmp1 - tmp2;
  104. pOutputRotations ++;
  105. pInputQuaternions += 4;
  106. }
  107. }
  108. #else
  109. void arm_quaternion2rotation_f32(const float32_t *pInputQuaternions,
  110. float32_t *pOutputRotations,
  111. uint32_t nbQuaternions)
  112. {
  113. uint32_t nb;
  114. for(nb=0; nb < nbQuaternions; nb++)
  115. {
  116. float32_t q00 = ARM_SQ(pInputQuaternions[0 + nb * 4]);
  117. float32_t q11 = ARM_SQ(pInputQuaternions[1 + nb * 4]);
  118. float32_t q22 = ARM_SQ(pInputQuaternions[2 + nb * 4]);
  119. float32_t q33 = ARM_SQ(pInputQuaternions[3 + nb * 4]);
  120. float32_t q01 = pInputQuaternions[0 + nb * 4]*pInputQuaternions[1 + nb * 4];
  121. float32_t q02 = pInputQuaternions[0 + nb * 4]*pInputQuaternions[2 + nb * 4];
  122. float32_t q03 = pInputQuaternions[0 + nb * 4]*pInputQuaternions[3 + nb * 4];
  123. float32_t q12 = pInputQuaternions[1 + nb * 4]*pInputQuaternions[2 + nb * 4];
  124. float32_t q13 = pInputQuaternions[1 + nb * 4]*pInputQuaternions[3 + nb * 4];
  125. float32_t q23 = pInputQuaternions[2 + nb * 4]*pInputQuaternions[3 + nb * 4];
  126. float32_t xx = q00 + q11 - q22 - q33;
  127. float32_t yy = q00 - q11 + q22 - q33;
  128. float32_t zz = q00 - q11 - q22 + q33;
  129. float32_t xy = 2*(q12 - q03);
  130. float32_t xz = 2*(q13 + q02);
  131. float32_t yx = 2*(q12 + q03);
  132. float32_t yz = 2*(q23 - q01);
  133. float32_t zx = 2*(q13 - q02);
  134. float32_t zy = 2*(q23 + q01);
  135. pOutputRotations[0 + nb * 9] = xx; pOutputRotations[1 + nb * 9] = xy; pOutputRotations[2 + nb * 9] = xz;
  136. pOutputRotations[3 + nb * 9] = yx; pOutputRotations[4 + nb * 9] = yy; pOutputRotations[5 + nb * 9] = yz;
  137. pOutputRotations[6 + nb * 9] = zx; pOutputRotations[7 + nb * 9] = zy; pOutputRotations[8 + nb * 9] = zz;
  138. }
  139. }
  140. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  141. /**
  142. @} end of QuatRot group
  143. */