diff --git a/lib/bone.js b/lib/bone.js index 1fc8ef81..351dc45f 100644 --- a/lib/bone.js +++ b/lib/bone.js @@ -62,7 +62,8 @@ var Bone = module.exports = function(finger, data) { /** * * These fully-specify the orientation of the bone. - * See examples/threejs-bones.html for more info + * See examples/threejs-bones.html or https://developer.leapmotion.com/gallery/bone-basis-arrows for more info + * * Three vec3s: * x (red): The rotation axis of the finger, pointing outwards. (In general, away from the thumb ) * y (green): The "up" vector, orienting the top of the finger @@ -74,6 +75,7 @@ var Bone = module.exports = function(finger, data) { * the first two appear in the same position, but only the second (proximal) rotates. * * Normalized. + * */ this.basis = data.basis; }; diff --git a/lib/hand.js b/lib/hand.js index 5fc78cba..bf2e8b9e 100644 --- a/lib/hand.js +++ b/lib/hand.js @@ -193,7 +193,8 @@ var Hand = module.exports = function(data) { this.grabStrength = data.grabStrength; this.pinchStrength = data.pinchStrength; this.confidence = data.confidence; -} + this._matrix = null; +}; /** * The finger with the specified ID attached to this hand. @@ -422,6 +423,31 @@ Hand.prototype.roll = function() { return Math.atan2(this.palmNormal[0], -this.palmNormal[1]); } +// Returns the 4x4 transformation Matrix for this hand, which contains rotation, position, and scale data. +// Good for compatability with THREE.js Matrix4#fromArray() +Hand.prototype.matrix = function(){ + + if (this._matrix) return this._matrix; + + var cross = vec3.create(); + vec3.cross(cross, this.palmNormal, this.direction); + + var up = vec3.clone(this.palmNormal); + vec3.scale(up, up, -1); + + var b = this.basis, + t = this._matrix = mat4.create(); + + + // open transform mat4 from rotation mat3 + t[0] = cross[0], t[1] = cross[1], t[2] = cross[2]; + t[4] = up[0], t[5] = up[1], t[6] = up[2]; + t[8] = this.direction[0], t[9] = this.direction[1], t[10] = this.direction[2]; + + + +} + /** * An invalid Hand object. * diff --git a/test/hand.js b/test/hand.js index 232e9709..f258cc83 100644 --- a/test/hand.js +++ b/test/hand.js @@ -51,6 +51,40 @@ describe('Hand', function(){ assert.closeTo(0.72273, result, 0.0001) }) }) + + it('should have a transform matrix', function() { + + var matrix = hand.matrix(); + + //scale + assert(matrix[3] == 1); + assert(matrix[7] == 1); + assert(matrix[11] == 1); + assert(matrix[15] == 1); + + //position + assert(matrix[12] = hand.palmPosition[0]); + assert(matrix[13] = hand.palmPosition[1]); + assert(matrix[14] = hand.palmPosition[2]); + + // rotation + assert(matrix[4] = -hand.palmNormal[0]); + assert(matrix[5] = -hand.palmNormal[1]); + assert(matrix[6] = -hand.palmNormal[2]); + + assert(matrix[8] = hand.direction[0]); + assert(matrix[9] = hand.direction[1]); + assert(matrix[10] = hand.direction[2]); + + var cross = vec3.create(); + vec3.cross(cross, this.palmNormal, this.direction); + + assert(matrix[0] = hand.direction[0]); + assert(matrix[1] = hand.direction[1]); + assert(matrix[2] = hand.direction[2]); + + + }) }); describe('#translation()', function(){