summaryrefslogtreecommitdiffstats
blob: fea1afe160cb6b5a69ec04a3794e4687c1370015 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ANDROID_QUAT_H
#define ANDROID_QUAT_H

#include <math.h>

#include "vec.h"
#include "mat.h"

// -----------------------------------------------------------------------
namespace android {
// -----------------------------------------------------------------------

template <typename TYPE>
mat<TYPE, 3, 3> quatToMatrix(const vec<TYPE, 4>& q) {
    mat<TYPE, 3, 3> R;
    TYPE q0(q.w);
    TYPE q1(q.x);
    TYPE q2(q.y);
    TYPE q3(q.z);
    TYPE sq_q1 = 2 * q1 * q1;
    TYPE sq_q2 = 2 * q2 * q2;
    TYPE sq_q3 = 2 * q3 * q3;
    TYPE q1_q2 = 2 * q1 * q2;
    TYPE q3_q0 = 2 * q3 * q0;
    TYPE q1_q3 = 2 * q1 * q3;
    TYPE q2_q0 = 2 * q2 * q0;
    TYPE q2_q3 = 2 * q2 * q3;
    TYPE q1_q0 = 2 * q1 * q0;
    R[0][0] = 1 - sq_q2 - sq_q3;
    R[0][1] = q1_q2 - q3_q0;
    R[0][2] = q1_q3 + q2_q0;
    R[1][0] = q1_q2 + q3_q0;
    R[1][1] = 1 - sq_q1 - sq_q3;
    R[1][2] = q2_q3 - q1_q0;
    R[2][0] = q1_q3 - q2_q0;
    R[2][1] = q2_q3 + q1_q0;
    R[2][2] = 1 - sq_q1 - sq_q2;
    return R;
}

template <typename TYPE>
vec<TYPE, 4> matrixToQuat(const mat<TYPE, 3, 3>& R) {
    // matrix to quaternion

    struct {
        inline TYPE operator()(TYPE v) {
            return v < 0 ? 0 : v;
        }
    } clamp;

    vec<TYPE, 4> q;
    const float Hx = R[0].x;
    const float My = R[1].y;
    const float Az = R[2].z;
    q.x = sqrtf( clamp( Hx - My - Az + 1) * 0.25f );
    q.y = sqrtf( clamp(-Hx + My - Az + 1) * 0.25f );
    q.z = sqrtf( clamp(-Hx - My + Az + 1) * 0.25f );
    q.w = sqrtf( clamp( Hx + My + Az + 1) * 0.25f );
    q.x = copysignf(q.x, R[2].y - R[1].z);
    q.y = copysignf(q.y, R[0].z - R[2].x);
    q.z = copysignf(q.z, R[1].x - R[0].y);
    // guaranteed to be unit-quaternion
    return q;
}

template <typename TYPE>
vec<TYPE, 4> normalize_quat(const vec<TYPE, 4>& q) {
    vec<TYPE, 4> r(q);
    if (r.w < 0) {
        r = -r;
    }
    return normalize(r);
}

// -----------------------------------------------------------------------

typedef vec4_t quat_t;

// -----------------------------------------------------------------------
}; // namespace android

#endif /* ANDROID_QUAT_H */