• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ANDROID_QUAT_H
18 #define ANDROID_QUAT_H
19 
20 #include <math.h>
21 
22 #include "vec.h"
23 #include "mat.h"
24 
25 // -----------------------------------------------------------------------
26 namespace android {
27 // -----------------------------------------------------------------------
28 
29 template <typename TYPE>
quatToMatrix(const vec<TYPE,4> & q)30 mat<TYPE, 3, 3> quatToMatrix(const vec<TYPE, 4>& q) {
31     mat<TYPE, 3, 3> R;
32     TYPE q0(q.w);
33     TYPE q1(q.x);
34     TYPE q2(q.y);
35     TYPE q3(q.z);
36     TYPE sq_q1 = 2 * q1 * q1;
37     TYPE sq_q2 = 2 * q2 * q2;
38     TYPE sq_q3 = 2 * q3 * q3;
39     TYPE q1_q2 = 2 * q1 * q2;
40     TYPE q3_q0 = 2 * q3 * q0;
41     TYPE q1_q3 = 2 * q1 * q3;
42     TYPE q2_q0 = 2 * q2 * q0;
43     TYPE q2_q3 = 2 * q2 * q3;
44     TYPE q1_q0 = 2 * q1 * q0;
45     R[0][0] = 1 - sq_q2 - sq_q3;
46     R[0][1] = q1_q2 - q3_q0;
47     R[0][2] = q1_q3 + q2_q0;
48     R[1][0] = q1_q2 + q3_q0;
49     R[1][1] = 1 - sq_q1 - sq_q3;
50     R[1][2] = q2_q3 - q1_q0;
51     R[2][0] = q1_q3 - q2_q0;
52     R[2][1] = q2_q3 + q1_q0;
53     R[2][2] = 1 - sq_q1 - sq_q2;
54     return R;
55 }
56 
57 template <typename TYPE>
matrixToQuat(const mat<TYPE,3,3> & R)58 vec<TYPE, 4> matrixToQuat(const mat<TYPE, 3, 3>& R) {
59     // matrix to quaternion
60 
61     struct {
62         inline TYPE operator()(TYPE v) {
63             return v < 0 ? 0 : v;
64         }
65     } clamp;
66 
67     vec<TYPE, 4> q;
68     const float Hx = R[0].x;
69     const float My = R[1].y;
70     const float Az = R[2].z;
71     q.x = sqrtf( clamp( Hx - My - Az + 1) * 0.25f );
72     q.y = sqrtf( clamp(-Hx + My - Az + 1) * 0.25f );
73     q.z = sqrtf( clamp(-Hx - My + Az + 1) * 0.25f );
74     q.w = sqrtf( clamp( Hx + My + Az + 1) * 0.25f );
75     q.x = copysignf(q.x, R[2].y - R[1].z);
76     q.y = copysignf(q.y, R[0].z - R[2].x);
77     q.z = copysignf(q.z, R[1].x - R[0].y);
78     // guaranteed to be unit-quaternion
79     return q;
80 }
81 
82 template <typename TYPE>
normalize_quat(const vec<TYPE,4> & q)83 vec<TYPE, 4> normalize_quat(const vec<TYPE, 4>& q) {
84     vec<TYPE, 4> r(q);
85     if (r.w < 0) {
86         r = -r;
87     }
88     return normalize(r);
89 }
90 
91 // -----------------------------------------------------------------------
92 
93 typedef vec4_t quat_t;
94 
95 // -----------------------------------------------------------------------
96 }; // namespace android
97 
98 #endif /* ANDROID_QUAT_H */
99