/
HeadTransform.java
139 lines (128 loc) · 3.64 KB
/
HeadTransform.java
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.google.vrtoolkit.cardboard;
import android.opengl.Matrix;
import android.util.FloatMath;
public class HeadTransform {
private static final float GIMBAL_LOCK_EPSILON = 0.01F;
private static final float PI = 3.1415927F;
private final float[] mHeadView;
public HeadTransform() {
this.mHeadView = new float[16];
Matrix.setIdentityM(this.mHeadView, 0);
}
float[] getHeadView() {
return this.mHeadView;
}
public void getHeadView(float[] headView, int offset) {
if (offset + 16 > headView.length) {
throw new IllegalArgumentException(
"Not enough space to write the result");
}
System.arraycopy(this.mHeadView, 0, headView, offset, 16);
}
public void getTranslation(float[] translation, int offset) {
if (offset + 3 > translation.length) {
throw new IllegalArgumentException(
"Not enough space to write the result");
}
for (int i = 0; i < 3; i++) {
translation[(i + offset)] = this.mHeadView[(12 + i)];
}
}
public void getForwardVector(float[] forward, int offset) {
if (offset + 3 > forward.length) {
throw new IllegalArgumentException(
"Not enough space to write the result");
}
for (int i = 0; i < 3; i++) {
forward[(i + offset)] = (-this.mHeadView[(8 + i)]);
}
}
public void getUpVector(float[] up, int offset) {
if (offset + 3 > up.length) {
throw new IllegalArgumentException(
"Not enough space to write the result");
}
for (int i = 0; i < 3; i++) {
up[(i + offset)] = this.mHeadView[(4 + i)];
}
}
public void getRightVector(float[] right, int offset) {
if (offset + 3 > right.length) {
throw new IllegalArgumentException(
"Not enough space to write the result");
}
for (int i = 0; i < 3; i++) {
right[(i + offset)] = this.mHeadView[i];
}
}
public void getQuaternion(float[] quaternion, int offset) {
if (offset + 4 > quaternion.length) {
throw new IllegalArgumentException(
"Not enough space to write the result");
}
float[] m = this.mHeadView;
float t = m[0] + m[5] + m[10];
float x;
float y;
float z;
float w;
float s;
if (t >= 0.0F) {
s = FloatMath.sqrt(t + 1.0F);
w = 0.5F * s;
s = 0.5F / s;
x = (m[9] - m[6]) * s;
y = (m[2] - m[8]) * s;
z = (m[4] - m[1]) * s;
} else {
if ((m[0] > m[5]) && (m[0] > m[10])) {
s = FloatMath.sqrt(1.0F + m[0] - m[5] - m[10]);
x = s * 0.5F;
s = 0.5F / s;
y = (m[4] + m[1]) * s;
z = (m[2] + m[8]) * s;
w = (m[9] - m[6]) * s;
} else {
if (m[5] > m[10]) {
s = FloatMath.sqrt(1.0F + m[5] - m[0] - m[10]);
y = s * 0.5F;
s = 0.5F / s;
x = (m[4] + m[1]) * s;
z = (m[9] + m[6]) * s;
w = (m[2] - m[8]) * s;
} else {
s = FloatMath.sqrt(1.0F + m[10] - m[0] - m[5]);
z = s * 0.5F;
s = 0.5F / s;
x = (m[2] + m[8]) * s;
y = (m[9] + m[6]) * s;
w = (m[4] - m[1]) * s;
}
}
}
quaternion[(offset + 0)] = x;
quaternion[(offset + 1)] = y;
quaternion[(offset + 2)] = z;
quaternion[(offset + 3)] = w;
}
public void getEulerAngles(float[] eulerAngles, int offset) {
if (offset + 3 > eulerAngles.length) {
throw new IllegalArgumentException(
"Not enough space to write the result");
}
float pitch = (float) Math.asin(this.mHeadView[6]);
float roll;
float yaw;
if (FloatMath.sqrt(1.0F - this.mHeadView[6] * this.mHeadView[6]) >= 0.01F) {
yaw = (float) Math.atan2(-this.mHeadView[2],
this.mHeadView[10]);
roll = (float) Math.atan2(-this.mHeadView[4], this.mHeadView[5]);
} else {
yaw = 0.0F;
roll = (float) Math.atan2(this.mHeadView[1], this.mHeadView[0]);
}
eulerAngles[(offset + 0)] = (-pitch);
eulerAngles[(offset + 1)] = (-yaw);
eulerAngles[(offset + 2)] = (-roll);
}
}