diff options
author | Samuli Piippo <samuli.piippo@digia.com> | 2014-09-02 10:23:39 +0300 |
---|---|---|
committer | Samuli Piippo <samuli.piippo@digia.com> | 2014-09-02 10:24:26 +0300 |
commit | 406db8fabcf8de5ab31880ca579edb7004cc4f4e (patch) | |
tree | 6b349020fef1d2f8f601a509d7c259fa40bea0f9 /basicsuite/webengine/content/rubiks/js/quaternion.js | |
parent | 2aab20bb2c32aadba2bef16828e3dd0f6c29a30b (diff) | |
parent | ad9ea42d493bc9b125c019373fa5fe84cfcc225d (diff) |
Merge remote-tracking branch 'origin/stable' into dev
* origin/stable: (63 commits)
Doc: Bump version to 3.1.1
Make FPS checkbox persistent
Handle accelerometer readings in rotated items
webengine: Add slight delay before loading the start page
webengine: disable the webgl demo
Update screenshot of the Virtual Keyboard demo
[Doc] Use symbolic links for demo preview images
Changed the audio track on the Qt_EnterpriseEmbedded_1080p.mp4 video.
Fix demo descriptions.
launchersettings: make ip field span two columns
Remove deleted demos also from doc
Update all VirtualKeyboard import to version 1.1
Doc: Bump version to 3.1.0
Doc: Content/language improvement for About QtEE demo.
Disable GraphicalEffects demo on beagleboneblack
about: fit text properly to the box
Fix Meet Qt Enterprise Embedded video url on startup
Fix a typo in the new About presentation
About Boot to Qt-demo update
Update Enterprise gallery demo description
...
Change-Id: I928ab65867993c83ae645834430fd15825349fde
Diffstat (limited to 'basicsuite/webengine/content/rubiks/js/quaternion.js')
-rw-r--r-- | basicsuite/webengine/content/rubiks/js/quaternion.js | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/basicsuite/webengine/content/rubiks/js/quaternion.js b/basicsuite/webengine/content/rubiks/js/quaternion.js new file mode 100644 index 0000000..a44cfea --- /dev/null +++ b/basicsuite/webengine/content/rubiks/js/quaternion.js @@ -0,0 +1,78 @@ +var Quaternion = OZ.Class(); + +Quaternion.fromRotation = function(axis, angle) { + var DEG2RAD = Math.PI/180; + var a = angle * DEG2RAD; + + var sin = Math.sin(a/2); + var cos = Math.cos(a/2); + + return new this( + axis[0]*sin, axis[1]*sin, axis[2]*sin, + cos + ); +} + +Quaternion.fromUnit = function() { + return new this(0, 0, 0, 1); +} + +Quaternion.prototype.init = function(x, y, z, w) { + this.x = x; + this.y = y; + this.z = z; + this.w = w; +} + +Quaternion.prototype.normalize = function() { + var norm = Math.sqrt(this.x*this.x + this.y*this.y + this.z*this.z + this.w*this.w); + return new this.constructor(this.x/norm, this.y/norm, this.z/norm, this.w/norm); +} + +Quaternion.prototype.conjugate = function() { + return new this.constructor(-this.x, -this.y, -this.z, this.w); +} + +Quaternion.prototype.toString = function() { + return [this.x, this.y, this.z, this.w].toString(", "); +} + +Quaternion.prototype.multiply = function(q) { + var p = this; + + var x = p.w*q.x + p.x*q.w + p.y*q.z - p.z*q.y; + var y = p.w*q.y + p.y*q.w + p.z*q.x - p.x*q.z; + var z = p.w*q.z + p.z*q.w + p.x*q.y - p.y*q.x; + var w = p.w*q.w - p.x*q.x - p.y*q.y - p.z*q.z; + + return new this.constructor(x, y, z, w); +} + +Quaternion.prototype.toAxis = function() { + return [this.x, this.y, this.z]; +} + +Quaternion.prototype.toAngle = function() { + var RAD2DEG = 180/Math.PI; + return RAD2DEG * 2 * Math.acos(this.w); +} + +Quaternion.prototype.toRotation = function() { + var axis = this.toAxis(); + var angle = this.toAngle(); + return "rotate3d(" + axis[0].toFixed(10) + "," + axis[1].toFixed(10) + "," + axis[2].toFixed(10) + "," + angle.toFixed(10) + "deg)"; +} + +Quaternion.prototype.toRotations = function() { + var RAD2DEG = 180/Math.PI; + + var x = RAD2DEG * Math.atan2(2*(this.w*this.x + this.y*this.z), 1 - 2*(this.x*this.x + this.y*this.y)); + var y = RAD2DEG * Math.asin(2*(this.w*this.y - this.x*this.z)); + var z = RAD2DEG * Math.atan2(2*(this.w*this.z + this.x*this.y), 1 - 2*(this.y*this.y + this.z*this.z)); + + if (x < 0) { x += 360; } + if (y < 0) { y += 360; } + if (z < 0) { z += 360; } + + return "rotateX(" + x.toFixed(10) + "deg) rotateY(" + y.toFixed(10) + "deg) rotate(" + z.toFixed(10) + "deg)"; +} |