1 Clazz.declarePackage ("javajs.util");
2 Clazz.load (["javajs.util.P4"], "javajs.util.Quat", ["java.lang.Float", "javajs.util.A4", "$.M3", "$.V3"], function () {
3 c$ = Clazz.decorateAsClass (function () {
9 Clazz.instantialize (this, arguments);
10 }, javajs.util, "Quat");
11 Clazz.makeConstructor (c$,
15 c$.newQ = Clazz.defineMethod (c$, "newQ",
17 var q1 = new javajs.util.Quat ();
20 }, "javajs.util.Quat");
21 c$.newVA = Clazz.defineMethod (c$, "newVA",
23 var q = new javajs.util.Quat ();
26 }, "javajs.util.T3,~N");
27 c$.newM = Clazz.defineMethod (c$, "newM",
29 var q = new javajs.util.Quat ();
30 q.setM (javajs.util.M3.newM3 (mat));
33 c$.newAA = Clazz.defineMethod (c$, "newAA",
35 var q = new javajs.util.Quat ();
39 c$.newP4 = Clazz.defineMethod (c$, "newP4",
41 var q = new javajs.util.Quat ();
45 c$.new4 = Clazz.defineMethod (c$, "new4",
46 function (q1, q2, q3, q0) {
47 var q = new javajs.util.Quat ();
60 Clazz.defineMethod (c$, "set",
66 }, "javajs.util.Quat");
67 Clazz.defineMethod (c$, "setP4",
68 ($fz = function (pt) {
69 var factor = (pt == null ? 0 : pt.distance4 (javajs.util.Quat.qZero));
73 }this.q0 = pt.w / factor;
74 this.q1 = pt.x / factor;
75 this.q2 = pt.y / factor;
76 this.q3 = pt.z / factor;
77 }, $fz.isPrivate = true, $fz), "javajs.util.P4");
78 Clazz.defineMethod (c$, "setTA",
79 function (pt, theta) {
80 if (pt.x == 0 && pt.y == 0 && pt.z == 0) {
83 }var fact = (Math.sin (theta / 2 * 0.017453292519943295) / Math.sqrt (pt.x * pt.x + pt.y * pt.y + pt.z * pt.z));
84 this.q0 = (Math.cos (theta / 2 * 0.017453292519943295));
85 this.q1 = (pt.x * fact);
86 this.q2 = (pt.y * fact);
87 this.q3 = (pt.z * fact);
88 }, "javajs.util.T3,~N");
89 Clazz.defineMethod (c$, "setAA",
91 var aa = javajs.util.A4.newAA (a);
92 if (aa.angle == 0) aa.y = 1;
93 this.setM ( new javajs.util.M3 ().setAA (aa));
95 Clazz.defineMethod (c$, "setM",
96 ($fz = function (mat) {
98 var trace = mat.m00 + mat.m11 + mat.m22;
105 w = Math.sqrt (1.0 + trace);
106 x = (mat.m21 - mat.m12) / w;
107 y = (mat.m02 - mat.m20) / w;
108 z = (mat.m10 - mat.m01) / w;
109 } else if ((temp = mat.m00 + mat.m00 - trace) >= 0.5) {
110 x = Math.sqrt (1.0 + temp);
111 w = (mat.m21 - mat.m12) / x;
112 y = (mat.m10 + mat.m01) / x;
113 z = (mat.m20 + mat.m02) / x;
114 } else if ((temp = mat.m11 + mat.m11 - trace) >= 0.5 || mat.m11 > mat.m22) {
115 y = Math.sqrt (1.0 + temp);
116 w = (mat.m02 - mat.m20) / y;
117 x = (mat.m10 + mat.m01) / y;
118 z = (mat.m21 + mat.m12) / y;
120 z = Math.sqrt (1.0 + mat.m22 + mat.m22 - trace);
121 w = (mat.m10 - mat.m01) / z;
122 x = (mat.m20 + mat.m02) / z;
123 y = (mat.m21 + mat.m12) / z;
124 }this.q0 = (w * 0.5);
128 }, $fz.isPrivate = true, $fz), "javajs.util.M3");
129 Clazz.defineMethod (c$, "setRef",
132 this.mul (this.getFixFactor ());
134 }if (this.dot (qref) >= 0) return;
139 }, "javajs.util.Quat");
140 c$.getQuaternionFrame = Clazz.defineMethod (c$, "getQuaternionFrame",
141 function (center, x, xy) {
142 var vA = javajs.util.V3.newV (x);
143 var vB = javajs.util.V3.newV (xy);
144 if (center != null) {
147 }return javajs.util.Quat.getQuaternionFrameV (vA, vB, null, false);
148 }, "javajs.util.P3,javajs.util.T3,javajs.util.T3");
149 c$.getQuaternionFrameV = Clazz.defineMethod (c$, "getQuaternionFrameV",
150 function (vA, vB, vC, yBased) {
152 vC = new javajs.util.V3 ();
154 if (yBased) vA.cross (vB, vC);
155 }var vBprime = new javajs.util.V3 ();
156 vBprime.cross (vC, vA);
158 vBprime.normalize ();
160 var mat = new javajs.util.M3 ();
161 mat.setColumnV (0, vA);
162 mat.setColumnV (1, vBprime);
163 mat.setColumnV (2, vC);
164 var q = javajs.util.Quat.newM (mat);
166 }, "javajs.util.V3,javajs.util.V3,javajs.util.V3,~B");
167 Clazz.defineMethod (c$, "getMatrix",
169 if (this.mat == null) this.setMatrix ();
172 Clazz.defineMethod (c$, "setMatrix",
174 this.mat = new javajs.util.M3 ();
175 this.mat.m00 = this.q0 * this.q0 + this.q1 * this.q1 - this.q2 * this.q2 - this.q3 * this.q3;
176 this.mat.m01 = 2 * this.q1 * this.q2 - 2 * this.q0 * this.q3;
177 this.mat.m02 = 2 * this.q1 * this.q3 + 2 * this.q0 * this.q2;
178 this.mat.m10 = 2 * this.q1 * this.q2 + 2 * this.q0 * this.q3;
179 this.mat.m11 = this.q0 * this.q0 - this.q1 * this.q1 + this.q2 * this.q2 - this.q3 * this.q3;
180 this.mat.m12 = 2 * this.q2 * this.q3 - 2 * this.q0 * this.q1;
181 this.mat.m20 = 2 * this.q1 * this.q3 - 2 * this.q0 * this.q2;
182 this.mat.m21 = 2 * this.q2 * this.q3 + 2 * this.q0 * this.q1;
183 this.mat.m22 = this.q0 * this.q0 - this.q1 * this.q1 - this.q2 * this.q2 + this.q3 * this.q3;
184 }, $fz.isPrivate = true, $fz));
185 Clazz.defineMethod (c$, "add",
187 return javajs.util.Quat.newVA (this.getNormal (), this.getTheta () + x);
189 Clazz.defineMethod (c$, "mul",
191 return (x == 1 ? javajs.util.Quat.new4 (this.q1, this.q2, this.q3, this.q0) : javajs.util.Quat.newVA (this.getNormal (), this.getTheta () * x));
193 Clazz.defineMethod (c$, "mulQ",
195 return javajs.util.Quat.new4 (this.q0 * p.q1 + this.q1 * p.q0 + this.q2 * p.q3 - this.q3 * p.q2, this.q0 * p.q2 + this.q2 * p.q0 + this.q3 * p.q1 - this.q1 * p.q3, this.q0 * p.q3 + this.q3 * p.q0 + this.q1 * p.q2 - this.q2 * p.q1, this.q0 * p.q0 - this.q1 * p.q1 - this.q2 * p.q2 - this.q3 * p.q3);
196 }, "javajs.util.Quat");
197 Clazz.defineMethod (c$, "div",
199 return this.mulQ (p.inv ());
200 }, "javajs.util.Quat");
201 Clazz.defineMethod (c$, "divLeft",
203 return this.inv ().mulQ (p);
204 }, "javajs.util.Quat");
205 Clazz.defineMethod (c$, "dot",
207 return this.q0 * q.q0 + this.q1 * q.q1 + this.q2 * q.q2 + this.q3 * q.q3;
208 }, "javajs.util.Quat");
209 Clazz.defineMethod (c$, "inv",
211 return javajs.util.Quat.new4 (-this.q1, -this.q2, -this.q3, this.q0);
213 Clazz.defineMethod (c$, "negate",
215 return javajs.util.Quat.new4 (-this.q1, -this.q2, -this.q3, -this.q0);
217 Clazz.defineMethod (c$, "getFixFactor",
219 return (this.q0 < 0 || this.q0 == 0 && (this.q1 < 0 || this.q1 == 0 && (this.q2 < 0 || this.q2 == 0 && this.q3 < 0)) ? -1 : 1);
220 }, $fz.isPrivate = true, $fz));
221 Clazz.defineMethod (c$, "getVector",
223 return this.getVectorScaled (i, 1);
225 Clazz.defineMethod (c$, "getVectorScaled",
226 function (i, scale) {
228 scale *= this.getFixFactor ();
229 return javajs.util.V3.new3 (this.q1 * scale, this.q2 * scale, this.q3 * scale);
230 }if (this.mat == null) this.setMatrix ();
231 var v = new javajs.util.V3 ();
232 this.mat.getColumnV (i, v);
233 if (scale != 1) v.scale (scale);
236 Clazz.defineMethod (c$, "getNormal",
238 var v = javajs.util.Quat.getRawNormal (this);
239 v.scale (this.getFixFactor ());
242 c$.getRawNormal = Clazz.defineMethod (c$, "getRawNormal",
243 ($fz = function (q) {
244 var v = javajs.util.V3.new3 (q.q1, q.q2, q.q3);
245 if (v.length () == 0) return javajs.util.V3.new3 (0, 0, 1);
248 }, $fz.isPrivate = true, $fz), "javajs.util.Quat");
249 Clazz.defineMethod (c$, "getTheta",
251 return (Math.acos (Math.abs (this.q0)) * 2 * 180 / 3.141592653589793);
253 Clazz.defineMethod (c$, "getThetaRadians",
255 return (Math.acos (Math.abs (this.q0)) * 2);
257 Clazz.defineMethod (c$, "getNormalDirected",
259 var v = this.getNormal ();
260 if (v.x * v0.x + v.y * v0.y + v.z * v0.z < 0) {
263 }, "javajs.util.V3");
264 Clazz.defineMethod (c$, "get3dProjection",
266 v3d.set (this.q1, this.q2, this.q3);
268 }, "javajs.util.V3");
269 Clazz.defineMethod (c$, "getThetaDirected",
270 function (axisAngle) {
271 var theta = this.getTheta ();
272 var v = this.getNormal ();
273 if (axisAngle.x * this.q1 + axisAngle.y * this.q2 + axisAngle.z * this.q3 < 0) {
276 }axisAngle.set4 (v.x, v.y, v.z, theta);
278 }, "javajs.util.P4");
279 Clazz.defineMethod (c$, "getThetaDirectedV",
281 var theta = this.getTheta ();
282 var v = this.getNormal ();
283 if (vector.x * this.q1 + vector.y * this.q2 + vector.z * this.q3 < 0) {
287 }, "javajs.util.V3");
288 Clazz.defineMethod (c$, "toPoint4f",
290 return javajs.util.P4.new4 (this.q1, this.q2, this.q3, this.q0);
292 Clazz.defineMethod (c$, "toAxisAngle4f",
294 var theta = 2 * Math.acos (Math.abs (this.q0));
295 var sinTheta2 = Math.sin (theta / 2);
296 var v = this.getNormal ();
299 theta = 3.141592653589793 - theta;
300 }return javajs.util.A4.newVA (v, theta);
302 Clazz.defineMethod (c$, "transform2",
303 function (pt, ptNew) {
304 if (this.mat == null) this.setMatrix ();
305 this.mat.rotate2 (pt, ptNew);
307 }, "javajs.util.T3,javajs.util.T3");
308 Clazz.defineMethod (c$, "leftDifference",
310 var q2adjusted = (this.dot (q2) < 0 ? q2.negate () : q2);
311 return this.inv ().mulQ (q2adjusted);
312 }, "javajs.util.Quat");
313 Clazz.defineMethod (c$, "rightDifference",
315 var q2adjusted = (this.dot (q2) < 0 ? q2.negate () : q2);
316 return this.mulQ (q2adjusted.inv ());
317 }, "javajs.util.Quat");
318 Clazz.overrideMethod (c$, "toString",
320 return "{" + this.q1 + " " + this.q2 + " " + this.q3 + " " + this.q0 + "}";
322 c$.div = Clazz.defineMethod (c$, "div",
323 function (data1, data2, nMax, isRelative) {
325 if (data1 == null || data2 == null || (n = Math.min (data1.length, data2.length)) == 0) return null;
326 if (nMax > 0 && n > nMax) n = nMax;
327 var dqs = new Array (n);
328 for (var i = 0; i < n; i++) {
329 if (data1[i] == null || data2[i] == null) return null;
330 dqs[i] = (isRelative ? data1[i].divLeft (data2[i]) : data1[i].div (data2[i]));
334 c$.sphereMean = Clazz.defineMethod (c$, "sphereMean",
335 function (data, retStddev, criterion) {
336 if (data == null || data.length == 0) return new javajs.util.Quat ();
337 if (retStddev == null) retStddev = Clazz.newFloatArray (1, 0);
338 if (data.length == 1) {
340 return javajs.util.Quat.newQ (data[0]);
341 }var diff = 3.4028235E38;
342 var lastStddev = 3.4028235E38;
343 var qMean = javajs.util.Quat.simpleAverage (data);
346 while (diff > criterion && lastStddev != 0 && iter < maxIter) {
347 qMean = javajs.util.Quat.newMean (data, qMean);
348 retStddev[0] = javajs.util.Quat.stdDev (data, qMean);
349 diff = Math.abs (retStddev[0] - lastStddev);
350 lastStddev = retStddev[0];
354 c$.simpleAverage = Clazz.defineMethod (c$, "simpleAverage",
355 ($fz = function (ndata) {
356 var mean = javajs.util.V3.new3 (0, 0, 1);
357 var v = ndata[0].getNormal ();
359 for (var i = ndata.length; --i >= 0; ) mean.add (ndata[i].getNormalDirected (mean));
364 for (var i = ndata.length; --i >= 0; ) f += Math.abs (ndata[i].get3dProjection (v).dot (mean));
366 if (f != 0) mean.scale (f / ndata.length);
367 f = Math.sqrt (1 - mean.lengthSquared ());
368 if (Float.isNaN (f)) f = 0;
369 return javajs.util.Quat.newP4 (javajs.util.P4.new4 (mean.x, mean.y, mean.z, f));
370 }, $fz.isPrivate = true, $fz), "~A");
371 c$.newMean = Clazz.defineMethod (c$, "newMean",
372 ($fz = function (data, mean) {
373 var sum = new javajs.util.V3 ();
377 for (var i = data.length; --i >= 0; ) {
381 v.scale (dq.getTheta ());
384 sum.scale (1 / data.length);
385 var dqMean = javajs.util.Quat.newVA (sum, sum.length ());
386 return dqMean.mulQ (mean);
387 }, $fz.isPrivate = true, $fz), "~A,javajs.util.Quat");
388 c$.stdDev = Clazz.defineMethod (c$, "stdDev",
389 ($fz = function (data, mean) {
392 for (var i = n; --i >= 0; ) {
393 var theta = data[i].div (mean).getTheta ();
394 sum2 += theta * theta;
396 return Math.sqrt (sum2 / n);
397 }, $fz.isPrivate = true, $fz), "~A,javajs.util.Quat");
398 Clazz.defineMethod (c$, "getEulerZYZ",
403 if (this.q1 == 0 && this.q2 == 0) {
404 var theta = this.getTheta ();
405 return Clazz.newFloatArray (-1, [this.q3 < 0 ? -theta : theta, 0, 0]);
406 }rA = Math.atan2 (2 * (this.q2 * this.q3 + this.q0 * this.q1), 2 * (-this.q1 * this.q3 + this.q0 * this.q2));
407 rB = Math.acos (this.q3 * this.q3 - this.q2 * this.q2 - this.q1 * this.q1 + this.q0 * this.q0);
408 rG = Math.atan2 (2 * (this.q2 * this.q3 - this.q0 * this.q1), 2 * (this.q0 * this.q2 + this.q1 * this.q3));
409 return Clazz.newFloatArray (-1, [(rA / 0.017453292519943295), (rB / 0.017453292519943295), (rG / 0.017453292519943295)]);
411 Clazz.defineMethod (c$, "getEulerZXZ",
416 if (this.q1 == 0 && this.q2 == 0) {
417 var theta = this.getTheta ();
418 return Clazz.newFloatArray (-1, [this.q3 < 0 ? -theta : theta, 0, 0]);
419 }rA = Math.atan2 (2 * (this.q1 * this.q3 - this.q0 * this.q2), 2 * (this.q0 * this.q1 + this.q2 * this.q3));
420 rB = Math.acos (this.q3 * this.q3 - this.q2 * this.q2 - this.q1 * this.q1 + this.q0 * this.q0);
421 rG = Math.atan2 (2 * (this.q1 * this.q3 + this.q0 * this.q2), 2 * (-this.q2 * this.q3 + this.q0 * this.q1));
422 return Clazz.newFloatArray (-1, [(rA / 0.017453292519943295), (rB / 0.017453292519943295), (rG / 0.017453292519943295)]);
424 c$.qZero = c$.prototype.qZero = new javajs.util.P4 ();
425 Clazz.defineStatics (c$,
426 "RAD_PER_DEG", 0.017453292519943295);