Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions examples/threejs-bones.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
</body>

<script>
// note: before implementing based off of this, you can instead grab the boneHand plugin
// here: js.leapmotion.com

var colors = [0xff0000, 0x00ff00, 0x0000ff];
var baseBoneRotation = (new THREE.Quaternion).setFromEuler(
new THREE.Euler(Math.PI / 2, 0, 0)
Expand Down
28 changes: 20 additions & 8 deletions lib/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,21 @@ Frame.prototype.rotationAngle = function(sinceFrame, axis) {
if (!this.valid || !sinceFrame.valid) return 0.0;

var rot = this.rotationMatrix(sinceFrame);
var cs = (rot[0] + rot[4] + rot[8] - 1.0)*0.5;
var angle = Math.acos(cs);
angle = isNaN(angle) ? 0.0 : angle;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A reasonably clear derivation can be found here: http://en.wikipedia.org/wiki/Axis–angle_representation#Exponential_map_from_so.283.29_to_SO.283.29

Also see the log map section.

var sin = Leap.vec3.len(this.rotationAxis(sinceFrame));

var cos = (rot[0] + rot[4] + rot[8] - 1.0) * 0.5;

var angle;
if (-1e-7 < sin && sin < 1e-7) { // small values of sin
angle = 0;
} else {
angle = Math.atan2(sin,cos);
angle = isNaN(angle) ? 0.0 : angle;
}

if (axis !== undefined) {
var rotAxis = this.rotationAxis(sinceFrame);
angle *= vec3.dot(rotAxis, vec3.normalize(vec3.create(), axis));
angle *= vec3.dot(sin, vec3.normalize(vec3.create(), axis));
}

return angle;
Expand All @@ -351,10 +359,14 @@ Frame.prototype.rotationAngle = function(sinceFrame, axis) {
*/
Frame.prototype.rotationAxis = function(sinceFrame) {
if (!this.valid || !sinceFrame.valid) return vec3.create();

var rotation = Leap.mat3.create();
Leap.mat3.multiply(rotation, this._rotation, sinceFrame._rotation);

return vec3.normalize(vec3.create(), [
this._rotation[7] - sinceFrame._rotation[5],
this._rotation[2] - sinceFrame._rotation[6],
this._rotation[3] - sinceFrame._rotation[1]
rotation[7] - rotation[5],
rotation[2] - rotation[6],
rotation[3] - rotation[1]
]);
}

Expand Down
33 changes: 23 additions & 10 deletions lib/hand.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,15 +246,24 @@ Hand.prototype.rotationAngle = function(sinceFrame, axis) {
var sinceHand = sinceFrame.hand(this.id);
if(!sinceHand.valid) return 0.0;
var rot = this.rotationMatrix(sinceFrame);
var cs = (rot[0] + rot[4] + rot[8] - 1.0)*0.5
var angle = Math.acos(cs);
angle = isNaN(angle) ? 0.0 : angle;

var sin = Leap.vec3.len(this.rotationAxis(sinceFrame));

var cos = (rot[0] + rot[4] + rot[8] - 1.0) * 0.5;

var angle;
if (-1e-7 < sin && sin < 1e-7) { // small values of sin
angle = 0;
} else {
angle = Math.atan2(sin,cos);
angle = isNaN(angle) ? 0.0 : angle;
}

if (axis !== undefined) {
var rotAxis = this.rotationAxis(sinceFrame);
angle *= vec3.dot(rotAxis, vec3.normalize(vec3.create(), axis));
angle *= vec3.dot(sin, vec3.normalize(vec3.create(), axis));
}
return angle;
}
};

/**
* The axis of rotation derived from the change in orientation of this hand, and
Expand All @@ -275,12 +284,16 @@ Hand.prototype.rotationAxis = function(sinceFrame) {
if (!this.valid || !sinceFrame.valid) return vec3.create();
var sinceHand = sinceFrame.hand(this.id);
if (!sinceHand.valid) return vec3.create();

var rotation = Leap.mat3.create();
Leap.mat3.multiply(rotation, this._rotation, sinceHand._rotation);

return vec3.normalize(vec3.create(), [
this._rotation[7] - sinceHand._rotation[5],
this._rotation[2] - sinceHand._rotation[6],
this._rotation[3] - sinceHand._rotation[1]
rotation[7] - rotation[5],
rotation[2] - rotation[6],
rotation[3] - rotation[1]
]);
}
};

/**
* The transform matrix expressing the rotation derived from the change in
Expand Down