| 1 |
// |
| 2 |
// quaternion.h |
| 3 |
// CubicVR2 |
| 4 |
// |
| 5 |
// Created by Charles J. Cliffe on 2013-02-22. |
| 6 |
// Copyright (c) 2013 Charles J. Cliffe. All rights reserved. |
| 7 |
// |
| 8 |
|
| 9 |
#ifndef __CubicVR2__quaternion__ |
| 10 |
#define __CubicVR2__quaternion__ |
| 11 |
|
| 12 |
#include <iostream> |
| 13 |
#include "vec4.h" |
| 14 |
#include "mat4.h" |
| 15 |
|
| 16 |
namespace CubicVR { |
| 17 |
|
| 18 |
struct quaternion : vec4 { |
| 19 |
|
| 20 |
static vec4 fromMatrix(mat4 mat) { |
| 21 |
__float t = 1 + mat[0] + mat[5] + mat[10]; |
| 22 |
__float S,X,Y,Z,W; |
| 23 |
|
| 24 |
if ( t > 0.00000001 ) { |
| 25 |
S = sqrtf(t) * 2; |
| 26 |
X = ( mat[9] - mat[6] ) / S; |
| 27 |
Y = ( mat[2] - mat[8] ) / S; |
| 28 |
Z = ( mat[4] - mat[1] ) / S; |
| 29 |
W = 0.25f * S; |
| 30 |
} else { |
| 31 |
if ( mat[0] > mat[5] && mat[0] > mat[10] ) { // Column 0: |
| 32 |
S = sqrtf( 1.0f + mat[0] - mat[5] - mat[10] ) * 2.0f; |
| 33 |
X = 0.25f * S; |
| 34 |
Y = (mat[4] + mat[1] ) / S; |
| 35 |
Z = (mat[2] + mat[8] ) / S; |
| 36 |
W = (mat[9] - mat[6] ) / S; |
| 37 |
} else if ( mat[5] > mat[10] ) { // Column 1: |
| 38 |
S = sqrtf( 1.0f + mat[5] - mat[0] - mat[10] ) * 2.0f; |
| 39 |
X = (mat[4] + mat[1] ) / S; |
| 40 |
Y = 0.25f * S; |
| 41 |
Z = (mat[9] + mat[6] ) / S; |
| 42 |
W = (mat[2] - mat[8] ) / S; |
| 43 |
} else { // Column 2: |
| 44 |
S = sqrtf( 1.0f + mat[10] - mat[0] - mat[5] ) * 2.0f; |
| 45 |
X = (mat[2] + mat[8] ) / S; |
| 46 |
Y = (mat[9] + mat[6] ) / S; |
| 47 |
Z = 0.25f * S; |
| 48 |
W = (mat[4] - mat[1] ) / S; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
return vec4(X,Y,Z,W); |
| 53 |
}; |
| 54 |
|
| 55 |
static vec4 fromEuler(__float bank, __float heading, __float pitch) { // x,y,z |
| 56 |
__float c1 = cosf(((float)M_PI / 180.0f) * heading / 2.0f); |
| 57 |
__float s1 = sinf(((float)M_PI / 180.0f) * heading / 2.0f); |
| 58 |
__float c2 = cosf(((float)M_PI / 180.0f) * pitch / 2.0f); |
| 59 |
__float s2 = sinf(((float)M_PI / 180.0f) * pitch / 2.0f); |
| 60 |
__float c3 = cosf(((float)M_PI / 180.0f) * bank / 2.0f); |
| 61 |
__float s3 = sinf(((float)M_PI / 180.0f) * bank / 2.0f); |
| 62 |
__float c1c2 = c1 * c2; |
| 63 |
__float s1s2 = s1 * s2; |
| 64 |
|
| 65 |
vec4 mOut; |
| 66 |
|
| 67 |
mOut[0] = c1c2 * c3 - s1s2 * s3; |
| 68 |
mOut[1] = c1c2 * s3 + s1s2 * c3; |
| 69 |
mOut[2] = s1 * c2 * c3 + c1 * s2 * s3; |
| 70 |
mOut[3] = c1 * s2 * c3 - s1 * c2 * s3; |
| 71 |
|
| 72 |
return mOut; |
| 73 |
}; |
| 74 |
|
| 75 |
static vec3 toEuler(vec4 q) { |
| 76 |
__float sqx = q[0] * q[0]; |
| 77 |
__float sqy = q[1] * q[1]; |
| 78 |
__float sqz = q[2] * q[2]; |
| 79 |
__float sqw = q[3] * q[3]; |
| 80 |
|
| 81 |
__float x = (180.0f / (float)M_PI) * ((atan2f(2.0f * (q[1] * q[2] + q[0] * q[3]), (-sqx - sqy + sqz + sqw)))); |
| 82 |
__float y = (180.0f / (float)M_PI) * ((asinf(-2.0f * (q[0] * q[2] - q[1] * q[3])))); |
| 83 |
__float z = (180.0f / (float)M_PI) * ((atan2f(2.0f * (q[0] * q[1] + q[2] * q[3]), (sqx - sqy - sqz + sqw)))); |
| 84 |
|
| 85 |
return vec3(x, y, z); |
| 86 |
}; |
| 87 |
|
| 88 |
static vec4 multiply(vec4 q1, vec4 q2) { |
| 89 |
__float x = q1[0] * q2[3] + q1[3] * q2[0] + q1[1] * q2[2] - q1[2] * q2[1]; |
| 90 |
__float y = q1[1] * q2[3] + q1[3] * q2[1] + q1[2] * q2[0] - q1[0] * q2[2]; |
| 91 |
__float z = q1[2] * q2[3] + q1[3] * q2[2] + q1[0] * q2[1] - q1[1] * q2[0]; |
| 92 |
__float w = q1[3] * q2[3] - q1[0] * q2[0] - q1[1] * q2[1] - q1[2] * q2[2]; |
| 93 |
|
| 94 |
return vec4(x,y,z,w); |
| 95 |
}; |
| 96 |
|
| 97 |
}; |
| 98 |
} |
| 99 |
|
| 100 |
#endif /* defined(__CubicVR2__quaternion__) */ |