Skip to content

WIP hand matrix #207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/bone.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
};
Expand Down
28 changes: 27 additions & 1 deletion lib/hand.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down
34 changes: 34 additions & 0 deletions test/hand.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down