-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuaternion.java
More file actions
184 lines (157 loc) · 4.1 KB
/
Quaternion.java
File metadata and controls
184 lines (157 loc) · 4.1 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
public class Quaternion{
private double[] qt = {0.0, 0.0, 0.0, 0.0};
public Quaternion(){
this.qt[0] = 0;
this.qt[1] = 0;
this.qt[2] = 0;
this.qt[3] = 0;
}
public Quaternion(Vector r, double a){
Vector axis;
if(r.dimension() == 3){
axis = r.unit();
} else {
axis = r.resetDimensionality(3).unit();
}
this.qt[0] = Math.cos(a/2);
double normer = Math.sin(a/2);
this.qt[1] = axis.element(0)*normer;
this.qt[2] = axis.element(1)*normer;
this.qt[3] = axis.element(2)*normer;
}
public Quaternion(double w, double x, double y, double z){
this.qt[0] = w;
this.qt[1] = x;
this.qt[2] = y;
this.qt[3] = z;
}
public Quaternion(double[] arr){
for(int i = 0; i < 4; i++){
if(arr.length > i){
this.qt[i] = arr[i];
} else {
this.qt[i] = 0.0;
}
}
}
public Quaternion copy(){
return new Quaternion(this.qt[0],this.qt[1],this.qt[2],this.qt[3]);
}
public double element(int index){
if(index >= 0 && index < 4){
return qt[index];
} else {
return 0.0;
}
}
public double[] toArray(){
return this.qt;
}
public Quaternion plus(double r){
double[] sum = new double[4];
for(int i=0; i<4; i++){
sum[i] = this.qt[i] + r;
}
return new Quaternion(sum);
}
public Quaternion minus(double r){
double[] diff = new double[4];
for(int i=0; i<4; i++){
diff[i] = this.qt[i] - r;
}
return new Quaternion(diff);
}
public Quaternion times(double r){
double[] prod = new double[4];
for(int i=0; i<4; i++){
prod[i] = this.qt[i] * r;
}
return new Quaternion(prod);
}
public Quaternion dividedBy(double r){
double[] div = new double[4];
for(int i=0; i<4; i++){
div[i] = this.qt[i] / r;
}
return new Quaternion(div);
}
public Quaternion plus(Quaternion q2){
double[] sum = new double[4];
for(int i=0; i<4; i++){
sum[i] = this.qt[i] + q2.element(i);
}
return new Quaternion(sum);
}
public Quaternion minus(Quaternion q2){
double[] diff = new double[4];
for(int i=0; i<4; i++){
diff[i] = this.qt[i] - q2.element(i);
}
return new Quaternion(diff);
}
public Quaternion times(Quaternion qq){
double[] prod = new double[4];
double[] q2 = qq.toArray();
prod[0] = qt[0]*q2[0] - qt[1]*q2[1] - qt[2]*q2[2] - qt[3]*q2[3];
prod[1] = qt[0]*q2[1] + qt[1]*q2[0] + qt[2]*q2[3] - qt[3]*q2[2];
prod[2] = qt[0]*q2[2] - qt[1]*q2[3] + qt[2]*q2[0] + qt[3]*q2[1];
prod[3] = qt[0]*q2[3] + qt[1]*q2[2] - qt[2]*q2[1] + qt[3]*q2[0];
return new Quaternion(prod);
}
public Quaternion conjugate(){
double[] conj = new double[4];
conj[0] = this.qt[0];
conj[1] = -this.qt[1];
conj[2] = -this.qt[2];
conj[3] = -this.qt[3];
return new Quaternion(conj);
}
public double norm(){
return Math.sqrt(qt[0]*qt[0] + qt[1]*qt[1] + qt[2]*qt[2] + qt[3]*qt[3]);
}
public Quaternion inverse(){
return this.conjugate().dividedBy(this.norm());
}
public Quaternion unit(){
return this.dividedBy(this.norm());
}
public boolean isUnit(){
return (Math.abs(this.unit().norm()-1.0) < 0.00000001 );
}
public double rotationAngle(){
return 2*Math.acos(this.element(0));
}
public Vector rotationAxis(){
double normer = Math.sin(this.rotationAngle());
if(normer != 0.0){
double[] axis = new double[3];
axis[0] = this.qt[1]/normer;
axis[1] = this.qt[2]/normer;
axis[2] = this.qt[3]/normer;
return new Vector(axis);
} else {
double[] axis = new double[3];
axis[0] = 0;
axis[1] = 0;
axis[2] = 1;
return new Vector(axis);
}
}
public double[][] rotationMatrix(){
double[][] mat = new double[3][3];
mat[0][0] = 1-2*(qt[2]*qt[2]+qt[3]*qt[3]);
mat[0][1] = 2*(qt[1]*qt[2]-qt[0]*qt[3]);
mat[0][2] = 2*(qt[1]*qt[3]+qt[0]*qt[2]);
mat[1][0] = 2*(qt[1]*qt[2]+qt[0]*qt[3]);
mat[1][1] = 1-2*(qt[1]*qt[1]+qt[3]*qt[3]);
mat[1][2] = 2*(qt[2]*qt[3]-qt[0]*qt[1]);
mat[2][0] = 2*(qt[1]*qt[3]-qt[0]*qt[2]);
mat[2][1] = 2*(qt[2]*qt[3]+qt[0]*qt[1]);
mat[2][2] = 1-2*(qt[2]*qt[2]+qt[1]*qt[1]);
return mat;
}
public Vector vectorize(){
double[] elems = {qt[1],qt[2],qt[3]};
return new Vector(elems);
}
}