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 
29 #include "dsp/quaternion_math_functions.h"
30 #include <math.h>
31 
32 /**
33   @ingroup groupQuaternionMath
34  */
35 
36 /**
37   @defgroup QuatConv Quaternion conversions
38 
39   Conversions between quaternion and rotation representations.
40  */
41 
42 /**
43   @ingroup QuatConv
44  */
45 
46 /**
47   @defgroup QuatRot Quaternion to Rotation
48 
49   Conversions from quaternion to rotation.
50  */
51 
52 /**
53   @addtogroup QuatRot
54   @{
55  */
56 
57 /**
58    @brief Conversion of quaternion to equivalent rotation matrix.
59    @param[in]       pInputQuaternions points to an array of normalized quaternions
60    @param[out]      pOutputRotations points to an array of 3x3 rotations (in row order)
61    @param[in]       nbQuaternions number of quaternions in the array
62 
63    @par
64    Format of rotation matrix
65 
66 
67    The quaternion a + ib + jc + kd is converted into rotation matrix:
68    <pre>
69      a^2 + b^2 - c^2 - d^2                 2bc - 2ad                 2bd + 2ac
70                  2bc + 2ad     a^2 - b^2 + c^2 - d^2                 2cd - 2ab
71                  2bd - 2ac                 2cd + 2ab     a^2 - b^2 - c^2 + d^2
72    </pre>
73    Rotation matrix is saved in row order : R00 R01 R02 R10 R11 R12 R20 R21 R22
74  */
75 
76 #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
77 
78 #include "arm_helium_utils.h"
79 
arm_quaternion2rotation_f32(const float32_t * pInputQuaternions,float32_t * pOutputRotations,uint32_t nbQuaternions)80 void arm_quaternion2rotation_f32(const float32_t *pInputQuaternions,
81     float32_t *pOutputRotations,
82     uint32_t nbQuaternions)
83 {
84   f32x4_t vec0,vec1, vec2 ,vec3;
85   float32_t q2q3, tmp1, tmp2 ;
86 
87   for(uint32_t nb=0; nb < nbQuaternions; nb++)
88   {
89 
90     // q0 q1 q2 q3
91     vec0 = vld1q(pInputQuaternions);
92 
93     // q0^2 q1^2 q2^2 q3^2
94     vec1 = vmulq(vec0,vec0);
95 
96     // q0^2 q1q0 q2q0 q3q0
97     vec2 = vmulq_n_f32(vec0, vgetq_lane(vec0,0));
98 
99     // 2 (q0^2 q1q0 q2q0 q3q0)
100     vec2 = vmulq_n_f32(vec2, 2.0f);
101 
102 
103     // 2 q2q3
104     q2q3 = vgetq_lane(vec0,2) * vgetq_lane(vec0,3);
105     q2q3 = q2q3 * 2.0f;
106 
107     // 2 (q0q1 q1^2 q2q1 q3q1)
108     vec3 = vmulq_n_f32(vec0, vgetq_lane(vec0,1));
109     vec3 = vmulq_n_f32(vec3, 2.0f);
110 
111 
112 
113     vec0 = vsetq_lane(vgetq_lane(vec1,0) + vgetq_lane(vec1,1),vec0,0);
114     vec0 = vsetq_lane(vgetq_lane(vec0,0) - vgetq_lane(vec1,2),vec0,0);
115     vec0 = vsetq_lane(vgetq_lane(vec0,0) - vgetq_lane(vec1,3),vec0,0);
116     vec0 = vsetq_lane(vgetq_lane(vec3,2) - vgetq_lane(vec2,3),vec0,1);
117     vec0 = vsetq_lane(vgetq_lane(vec3,3) + vgetq_lane(vec2,2),vec0,2);
118     vec0 = vsetq_lane(vgetq_lane(vec3,2) + vgetq_lane(vec2,3),vec0,3);
119 
120     vst1q(pOutputRotations, vec0);
121     pOutputRotations += 4;
122 
123     tmp1 = vgetq_lane(vec1,0) - vgetq_lane(vec1,1);
124     tmp2 = vgetq_lane(vec1,2) - vgetq_lane(vec1,3);
125 
126 
127     vec0 = vsetq_lane(tmp1 + tmp2,vec0,0);
128     vec0 = vsetq_lane(q2q3 - vgetq_lane(vec2,1) ,vec0,1);
129     vec0 = vsetq_lane(vgetq_lane(vec3,3) - vgetq_lane(vec2,2),vec0,2);
130     vec0 = vsetq_lane(q2q3 + vgetq_lane(vec2,1) ,vec0,3);
131 
132     vst1q(pOutputRotations, vec0);
133     pOutputRotations += 4;
134 
135     *pOutputRotations = tmp1 - tmp2;
136     pOutputRotations ++;
137 
138     pInputQuaternions += 4;
139   }
140 }
141 
142 #else
arm_quaternion2rotation_f32(const float32_t * pInputQuaternions,float32_t * pOutputRotations,uint32_t nbQuaternions)143 void arm_quaternion2rotation_f32(const float32_t *pInputQuaternions,
144     float32_t *pOutputRotations,
145     uint32_t nbQuaternions)
146 {
147    uint32_t nb;
148    for(nb=0; nb < nbQuaternions; nb++)
149    {
150         float32_t q00 = ARM_SQ(pInputQuaternions[0 + nb * 4]);
151         float32_t q11 = ARM_SQ(pInputQuaternions[1 + nb * 4]);
152         float32_t q22 = ARM_SQ(pInputQuaternions[2 + nb * 4]);
153         float32_t q33 = ARM_SQ(pInputQuaternions[3 + nb * 4]);
154         float32_t q01 =  pInputQuaternions[0 + nb * 4]*pInputQuaternions[1 + nb * 4];
155         float32_t q02 =  pInputQuaternions[0 + nb * 4]*pInputQuaternions[2 + nb * 4];
156         float32_t q03 =  pInputQuaternions[0 + nb * 4]*pInputQuaternions[3 + nb * 4];
157         float32_t q12 =  pInputQuaternions[1 + nb * 4]*pInputQuaternions[2 + nb * 4];
158         float32_t q13 =  pInputQuaternions[1 + nb * 4]*pInputQuaternions[3 + nb * 4];
159         float32_t q23 =  pInputQuaternions[2 + nb * 4]*pInputQuaternions[3 + nb * 4];
160 
161         float32_t xx = q00 + q11 - q22 - q33;
162         float32_t yy = q00 - q11 + q22 - q33;
163         float32_t zz = q00 - q11 - q22 + q33;
164         float32_t xy = 2*(q12 - q03);
165         float32_t xz = 2*(q13 + q02);
166         float32_t yx = 2*(q12 + q03);
167         float32_t yz = 2*(q23 - q01);
168         float32_t zx = 2*(q13 - q02);
169         float32_t zy = 2*(q23 + q01);
170 
171         pOutputRotations[0 + nb * 9] = xx; pOutputRotations[1 + nb * 9] = xy; pOutputRotations[2 + nb * 9] = xz;
172         pOutputRotations[3 + nb * 9] = yx; pOutputRotations[4 + nb * 9] = yy; pOutputRotations[5 + nb * 9] = yz;
173         pOutputRotations[6 + nb * 9] = zx; pOutputRotations[7 + nb * 9] = zy; pOutputRotations[8 + nb * 9] = zz;
174    }
175 }
176 #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
177 
178 /**
179   @} end of QuatRot group
180  */
181