Skip to content

Commit

Permalink
Merge pull request #14 from leapmotion/transform
Browse files Browse the repository at this point in the history
Add transform plugin and example
  • Loading branch information
pehrlich committed Aug 24, 2014
2 parents 3851d6f + 0836280 commit 8732eec
Show file tree
Hide file tree
Showing 12 changed files with 1,210 additions and 3 deletions.
22 changes: 22 additions & 0 deletions Gruntfile.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,27 @@ module.exports = (grunt) ->

grunt.initConfig
pkg: grunt.file.readJSON("package.json")

'string-replace':
main:
files: {
'main/': 'main/**/*.html'
'./': 'bower.json'
}
options: {
replacements: [
# bower.json
{
pattern: /"version": ".*"/,
replacement: '"version": "<%= pkg.version %>"'
},
# examples
{
pattern: /leap-plugins.*\.js/,
replacement: filename + '.js'
}
]
}

coffee:
main:
Expand Down Expand Up @@ -114,6 +135,7 @@ module.exports = (grunt) ->
require('load-grunt-tasks')(grunt);

grunt.registerTask "default", [
"string-replace",
"coffee",
"usebanner:coffeeMessagesMain",
"usebanner:coffeeMessagesExtras",
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ See [hand-entry](http://leapmotion.github.io/leapjs-plugins/docs/index.html#hand
- See [making plugins](http://github.com/leapmotion/leapjs/wiki/plugins) on the leapjs wiki.


## Examples

Examples are available on the [developer gallery](http://developer.leapmotion.com/gallery/tags/javascript) live editor
and in subfolders here of individual plugins.

To run them on localhost, you'll need a web server to resolve asset paths.

```bash
> python -m SimpleHTTPServer
```


Contributing
===============
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "leapjs-plugins",
"main": "index.js",
"version": "0.1.3",
"version": "0.1.6.1",
"homepage": "https://github.com/leapmotion/leapjs-plugins",
"description": "This repository holds a collection of independent plugins which extend the functionality of LeapJS itself.",
"keywords": [
Expand Down
96 changes: 96 additions & 0 deletions main/leap-plugins-0.1.6.1.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ Each event also includes the hand object, which will be invalid for the handLost





/*
* LeapJS Playback - v0.2.1 - 2014-05-14
* http://github.com/leapmotion/leapjs-playback/
Expand Down Expand Up @@ -2083,6 +2085,100 @@ More info on vec3 can be found, here: http://glmatrix.net/docs/2.2.0/symbols/vec

}).call(this);

//CoffeeScript generated from main/transform/leap.transform.coffee
(function() {
var __slice = [].slice;

Leap.plugin('transform', function(scope) {
var noop, transformDirections, transformMat4Implicit0, transformPositions, _matrix;
if (scope == null) {
scope = {};
}
noop = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
_matrix = new THREE.Matrix4;
scope.getMatrix = function(hand) {
var matrix;
if (scope.transform) {
matrix = typeof scope.transform === 'function' ? scope.transform(hand) : scope.transform;
if (window['THREE'] && matrix instanceof THREE.Matrix4) {
return matrix.elements;
} else {
return matrix;
}
} else if (scope.position || scope.quaternion || scope.scale) {
_matrix.set.apply(_matrix, noop);
if (scope.quaternion) {
_matrix.makeRotationFromQuaternion(typeof scope.quaternion === 'function' ? scope.quaternion(hand) : scope.quaternion);
}
if (scope.scale) {
_matrix.scale(typeof scope.scale === 'function' ? scope.scale(hand) : scope.scale);
}
if (scope.position) {
_matrix.setPosition(typeof scope.position === 'function' ? scope.position(hand) : scope.position);
}
return _matrix.elements;
} else {
return noop;
}
};
transformPositions = function() {
var matrix, vec3, vec3s, _i, _len, _results;
matrix = arguments[0], vec3s = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
_results = [];
for (_i = 0, _len = vec3s.length; _i < _len; _i++) {
vec3 = vec3s[_i];
if (vec3) {
_results.push(Leap.vec3.transformMat4(vec3, vec3, matrix));
} else {
_results.push(void 0);
}
}
return _results;
};
transformMat4Implicit0 = function(out, a, m) {
var x, y, z;
x = a[0];
y = a[1];
z = a[2];
out[0] = m[0] * x + m[4] * y + m[8] * z;
out[1] = m[1] * x + m[5] * y + m[9] * z;
out[2] = m[2] * x + m[6] * y + m[10] * z;
return out;
};
transformDirections = function() {
var matrix, vec3, vec3s, _i, _len, _results;
matrix = arguments[0], vec3s = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
_results = [];
for (_i = 0, _len = vec3s.length; _i < _len; _i++) {
vec3 = vec3s[_i];
if (vec3) {
_results.push(transformMat4Implicit0(vec3, vec3, matrix));
} else {
_results.push(void 0);
}
}
return _results;
};
return {
hand: function(hand) {
var finger, matrix, _i, _len, _ref, _results;
matrix = scope.getMatrix(hand);
transformPositions(matrix, hand.palmPosition, hand.stabilizedPalmPosition, hand.sphereCenter);
transformDirections(matrix, hand.direction, hand.palmNormal, hand.palmVelocity);
_ref = hand.fingers;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
finger = _ref[_i];
transformPositions(matrix, finger.carpPosition, finger.mcpPosition, finger.pipPosition, finger.dipPosition, finger.tipPosition);
_results.push(transformDirections(matrix, finger.direction));
}
return _results;
}
};
});

}).call(this);

//CoffeeScript generated from main/version-check/leap.version-check.coffee
(function() {
var versionCheck;
Expand Down
2 changes: 1 addition & 1 deletion main/leap-plugins-0.1.6.1.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion main/screen-position/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>
<head>
<script src="//js.leapmotion.com/leap-0.6.0.js"></script>
<script src="//js.leapmotion.com/leap-plugins-0.1.6.js"></script>
<script src="//js.leapmotion.com/leap-plugins-0.1.6.1.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<style>
Expand Down
168 changes: 168 additions & 0 deletions main/transform/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Leap Transform Plugin</title>

<style>
body{
margin: 0;
font-family: Helvetica;
}
canvas{
pointer-events: none;
}
input{
cursor: pointer;
}
#connect-leap{
bottom: 0;
top: auto !important;
}
</style>


<script src="//cdnjs.cloudflare.com/ajax/libs/three.js/r67/three.min.js"></script>
<script src="//js.leapmotion.com/leap-0.6.2.js"></script>
<script src="../leap-plugins-0.1.6.1.js"></script>
<script src="leap.rigged-hand-0.1.5pre.js"></script>

</head>
<body>

<h1>Transform Plugin</h1>
<p>
Transforms Leap data based off of a rotation matrix or a THREE.js rotation and vectors.
</p>
<p>
Here we use the riggedHand to visualize the transformations, but it is not required. All data in the frame is altered
by the transform plugin.
</p>
<p>
Parameters can be either static objects or methods which transform on every frame.
</p>
<p>
In this way, you can then transform individual hands based upon their id.
</p>

<br/>

<div style="float: left;">
<label>Rotation: <span id="rotationOutput">0</span>π</label><br/>
<input id="rotationInput" type="range" min="-1" max="1" value="0" step="0.001">
</div>

<div style="float: left;">
<label>Z Position: <span id="positionOutput">0</span></label><br/>
<input id="positionInput" type="range" min="-1000" max="300" value="0" step="1">
</div>

<div style="float: left;">
<label>Scale: <span id="scaleOutput">1</span></label><br/>
<input id="scaleInput" type="range" min="0.1" max="1" value="1" step="0.01">
<!-- note - there seems to be a strange issue with THREEjs sections of the hand disappearing for scales larger than 1.
Scale set as an option for the riggedHand will cause the issue. -->
</div>


</body>



<script>
// all units in mm
var initScene = function () {
window.scene = new THREE.Scene();
window.renderer = new THREE.WebGLRenderer({
alpha: true
});

window.renderer.setClearColor(0x000000, 0);
window.renderer.setSize(window.innerWidth, window.innerHeight);

window.renderer.domElement.style.position = 'fixed';
window.renderer.domElement.style.top = 0;
window.renderer.domElement.style.left = 0;
window.renderer.domElement.style.width = '100%';
window.renderer.domElement.style.height = '100%';

document.body.appendChild(window.renderer.domElement);

window.scene.add(new THREE.AmbientLight(0x888888));

var pointLight = new THREE.PointLight(0xFFffff);
pointLight.position = new THREE.Vector3(-20, 10, 100);
pointLight.lookAt(new THREE.Vector3(0, 0, 0));
window.scene.add(pointLight);

window.camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 10000);
window.camera.position.fromArray([0, 160, 400]);
window.camera.lookAt(new THREE.Vector3(0, 0, 0));

window.addEventListener('resize', function () {

camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.render(scene, camera);

}, false);

scene.add(camera);


renderer.render(scene, camera);
};

initScene();




Leap.loop()
// note that transform must be _before_ rigged hand
.use('transform', {
quaternion: new THREE.Quaternion,
position: new THREE.Vector3,
scale: new THREE.Vector3(1,1,1)
})
.use('playback', {recording: 'leap-playback-recording-110fps.json.lz'})
.use('riggedHand', {
dotsMode: false,
parent: scene,
renderFn: function(){
renderer.render(scene, camera);
}

})
.connect();

window.transformPlugin = Leap.loopController.plugins.transform;

document.getElementById('rotationInput').oninput = function(e){
var value = e.target.value;
transformPlugin.quaternion.setFromEuler(
new THREE.Euler(0, Math.PI * parseFloat(value,10) , 0)
);
document.getElementById('rotationOutput').innerHTML = value;
};

document.getElementById('positionInput').oninput = function(e){
var value = e.target.value;
transformPlugin.position.set(
0,0,parseInt(value, 10)
);
document.getElementById('positionOutput').innerHTML = value;
};

document.getElementById('scaleInput').oninput = function(e){
var value = parseFloat(e.target.value,10);
transformPlugin.scale.set(
value, value, value
);
document.getElementById('scaleOutput').innerHTML = value;
};

</script>

</html>
1 change: 1 addition & 0 deletions main/transform/leap-playback-recording-110fps.json.lz

Large diffs are not rendered by default.

0 comments on commit 8732eec

Please sign in to comment.