diff options
Diffstat (limited to 'js/helper-classes/RDGE/src/core/script/math/quat.js')
-rw-r--r-- | js/helper-classes/RDGE/src/core/script/math/quat.js | 225 |
1 files changed, 225 insertions, 0 deletions
diff --git a/js/helper-classes/RDGE/src/core/script/math/quat.js b/js/helper-classes/RDGE/src/core/script/math/quat.js new file mode 100644 index 00000000..5c2c8bb6 --- /dev/null +++ b/js/helper-classes/RDGE/src/core/script/math/quat.js | |||
@@ -0,0 +1,225 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | |||
7 | |||
8 | /** | ||
9 | * quat = {} | ||
10 | * This library contains utility functions for operating on quaternions. | ||
11 | * -- | ||
12 | * TODO: | ||
13 | * -need to add more helper functions for generating quaternions from | ||
14 | * other representations (i.e. - eulers, angle-axis). | ||
15 | */ | ||
16 | quat = {} | ||
17 | |||
18 | /** | ||
19 | * vec4.string | ||
20 | */ | ||
21 | quat.string = function(q) { | ||
22 | return "{ " + q[0] + ", " + q[1] + ", " + q[2] + ", " + q[3] + " }"; | ||
23 | } | ||
24 | |||
25 | /** | ||
26 | * quat.verify | ||
27 | */ | ||
28 | quat.verify = function(q) { | ||
29 | if (q == undefined || q.length == undefined || q.length < 4) { | ||
30 | return false; | ||
31 | } | ||
32 | |||
33 | if (typeof (q[0]) != "number" || typeof (q[1]) != "number" || typeof (q[2]) != "number" || typeof (q[3]) != "number") { | ||
34 | return false; | ||
35 | } | ||
36 | |||
37 | return true; | ||
38 | } | ||
39 | |||
40 | /** | ||
41 | * quat.identity | ||
42 | */ | ||
43 | quat.identity = function() { | ||
44 | return [0.0, 0.0, 0.0, 1.0]; | ||
45 | } | ||
46 | |||
47 | /** | ||
48 | * quat.add | ||
49 | */ | ||
50 | quat.add = function(a, b) { | ||
51 | return [ a[0] + b[0], | ||
52 | a[1] + b[1], | ||
53 | a[2] + b[2], | ||
54 | a[3] + b[3]]; | ||
55 | } | ||
56 | |||
57 | /** | ||
58 | * quat.sub | ||
59 | */ | ||
60 | quat.sub = function(a, b) { | ||
61 | return [ a[0] - b[0], | ||
62 | a[1] - b[1], | ||
63 | a[2] - b[2], | ||
64 | a[3] - b[3]]; | ||
65 | } | ||
66 | |||
67 | /** | ||
68 | * quat.mul | ||
69 | */ | ||
70 | quat.mul = function( a, b ) { | ||
71 | return [ a[3]*b[3] - a[0]*b[0] - a[1]*b[1] - a[2]*b[2], | ||
72 | a[3]*b[0] + a[0]*b[3] + a[1]*b[2] - a[2]*b[1], | ||
73 | a[3]*b[1] - a[0]*b[2] + a[1]*b[3] + a[2]*b[0], | ||
74 | a[3]*b[2] + a[0]*b[1] - a[1]*b[0] + a[2]*b[3] ]; | ||
75 | } | ||
76 | |||
77 | /** | ||
78 | * quat.addMul | ||
79 | */ | ||
80 | quat.addMul = function(a, b, s) { | ||
81 | if (s.length != undefined && s.length >= 4) { | ||
82 | return [a[0] + b[0] * s[0], a[1] + b[1] * s[1], a[2] + b[2] * s[2], a[3] + b[3] * s[3]]; | ||
83 | } else { | ||
84 | return [a[0] + b[0] * s, a[1] + b[1] * s, a[2] + b[2] * s, a[3] + b[3] * s]; | ||
85 | } | ||
86 | } | ||
87 | |||
88 | |||
89 | /** | ||
90 | * quat.scale | ||
91 | */ | ||
92 | quat.scale = function(q, s) { | ||
93 | if (s.length != undefined && s.length >= 4) { | ||
94 | return [q[0] * s[0], q[1] * q[1], q[2] * s[2], q[3] * s[3]]; | ||
95 | } else { | ||
96 | return [q[0] * s, q[1] * s, q[2] * s, q[3] * s]; | ||
97 | } | ||
98 | } | ||
99 | |||
100 | /** | ||
101 | * quat.lengthSq | ||
102 | */ | ||
103 | quat.lengthSq = function(q) { | ||
104 | return q[0] * q[0] + | ||
105 | q[1] * q[1] + | ||
106 | q[2] * q[2] + | ||
107 | q[3] * q[3]; | ||
108 | } | ||
109 | |||
110 | /** | ||
111 | * quat.length | ||
112 | */ | ||
113 | quat.length = function(q) { | ||
114 | return Math.sqrt( q[0] * q[0] + | ||
115 | q[1] * q[1] + | ||
116 | q[2] * q[2] + | ||
117 | q[3] * q[3]); | ||
118 | } | ||
119 | |||
120 | /** | ||
121 | * quat.normalize | ||
122 | */ | ||
123 | quat.normalize = function(q) { | ||
124 | var l = Math.sqrt(q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3]); | ||
125 | if (Math.abs(1.0 - l) > 0.0001) { | ||
126 | var ool = 1.0 / l; | ||
127 | return [q[0] * ool, q[1] * ool, q[2] * ool, q[3] * ool]; | ||
128 | } | ||
129 | return q; | ||
130 | } | ||
131 | |||
132 | /** | ||
133 | * quat.inverse | ||
134 | */ | ||
135 | quat.inverse = function(q) { | ||
136 | var n = vec4.lengthSq( q ); | ||
137 | if( n > 0.00001 ) { | ||
138 | n = 1.0 / n; | ||
139 | return [ q[0] * -n, q[1] * -n, q[2] * -n, q[3] ]; | ||
140 | } else { | ||
141 | // error condition | ||
142 | } | ||
143 | return q; | ||
144 | } | ||
145 | |||
146 | /** | ||
147 | * quat.dot | ||
148 | */ | ||
149 | quat.dot = function(a, b) { | ||
150 | return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]; | ||
151 | } | ||
152 | |||
153 | /** | ||
154 | * quat.applyRotation | ||
155 | */ | ||
156 | quat.applyRotation = function(q, v) { | ||
157 | return mat4.transformPoint(quat.toMatrix(q), v); | ||
158 | } | ||
159 | |||
160 | /** | ||
161 | * quat.lerp | ||
162 | */ | ||
163 | quat.lerp = function(q0, q1, t) { | ||
164 | return quat.normalize( [ q0[0] + (q1[0] - q0[0]) * t, q0[1] + (q1[1] - q0[1]) * t, q0[2] + (q1[2] - q0[2]) * t, q0[3] + (q1[3] - q0[3]) * t ] ); | ||
165 | } | ||
166 | |||
167 | /** | ||
168 | * quat.slerp | ||
169 | */ | ||
170 | quat.slerp = function(q0, q1, t) { | ||
171 | var c = quat.dot(q0, q1); // cosine of the angle | ||
172 | |||
173 | // just lerp if the quats are "close" enough | ||
174 | if (c >= 0.9) { | ||
175 | return quat.lerp(q0, q1, t); | ||
176 | } | ||
177 | |||
178 | var s = Math.sqrt(Math.abs(1.0 - c * c)); // sine of the angle | ||
179 | if (s < 0.001) | ||
180 | return q0; // too close | ||
181 | |||
182 | var sign = c < 0.0 ? -1.0 : 1.0; | ||
183 | var angle = Math.asin(s); | ||
184 | |||
185 | var invs = 1.0 / s; // sine^-1 | ||
186 | var coef0 = Math.sin((1.0 - t) * angle) * invs; // interp. coefficients | ||
187 | var coef1 = Math.sin(t * angle) * invs * sign; | ||
188 | |||
189 | quat.scale(q0, coef0); | ||
190 | quat.scale(q1, coef1); | ||
191 | |||
192 | return quat.normalize( quat.add(q0, q1) ); | ||
193 | } | ||
194 | |||
195 | /** | ||
196 | * quat.toMatrix | ||
197 | */ | ||
198 | quat.toMatrix = function(q) { | ||
199 | var tx = 2.0 * q[0]; | ||
200 | var ty = 2.0 * q[1]; | ||
201 | var tz = 2.0 * q[2]; | ||
202 | var twx = tx * q[3]; | ||
203 | var twy = ty * q[3]; | ||
204 | var twz = tz * q[3]; | ||
205 | var txx = tx * q[0]; | ||
206 | var txy = ty * q[0]; | ||
207 | var txz = tz * q[0]; | ||
208 | var tyy = ty * q[1]; | ||
209 | var tyz = tz * q[1]; | ||
210 | var tzz = tz * q[2]; | ||
211 | |||
212 | return [ 1.0 - (tyy + tzz), | ||
213 | txy + twz, | ||
214 | txz - twy, | ||
215 | 0, | ||
216 | txy - twz, | ||
217 | 1.0 - (txx + tzz), | ||
218 | tyz + twx, | ||
219 | 0, | ||
220 | txz + twy, | ||
221 | tyz - twx, | ||
222 | 1.0 - (txx + tyy), | ||
223 | 0, 0, 0, 0, 1]; | ||
224 | } | ||
225 | |||