-
Notifications
You must be signed in to change notification settings - Fork 9
/
rotations.html
142 lines (130 loc) · 3.38 KB
/
rotations.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<!DOCTYPE html>
<meta charset="utf-8">
<title>Rotations</title>
<style>
body {
width: 980px;
margin: 20px auto;
font-size: 13px;
font-family: sans-serif;
}
svg {
width:980px;
height: 500px;
position: absolute;
}
.third {
float: left;
width: 33.3%;
height: 280px;
}
</style>
<script src="js/lib/d3.v3.js"></script>
<script src="js/lib/underscore.js"></script>
<script src="js/lib/leap.js"></script>
<h1>Rotation Matrices</h1>
<div class="third">
Frame
<canvas id="frame"></canvas>
</div>
<div class="third">
Hand <span id="hand0-id"></span>
<canvas id="hand0"></canvas>
</div>
<div class="third">
Hand <span id="hand1-id"></span>
<canvas id="hand1"></canvas>
</div>
<div class="third">
Hand <span id="hand2-id"></span>
<canvas id="hand2"></canvas>
</div>
<div class="third">
Hand <span id="hand3-id"></span>
<canvas id="hand3"></canvas>
</div>
<p>
Caveats: The components are created using palmNormal rather than the rotation matrix. The Y isn't oriented in the right direction, and seems quite finicky. Sometimes the rotation matrix drops out, even when the palmNormal keeps working.
</p>
<p>
<a href="http://en.wikipedia.org/wiki/Rotation_matrix">Rotation Matrix</a>
</p>
<p>
<a href="http://en.wikipedia.org/wiki/Rotation_formalisms_in_three_dimensions#Conversion_formulae_between_formalisms">Conversions between 3d rotation formalisms</a>
</p>
<script>
// connect leap motion
var controller = new Leap.Controller();
controller.connect();
controller.onFrame(function() {
var obj = controller.frame();
// clear hand canvases
for (k in canvases) {
ctxs[k].clearRect(0,0,canvases[k].width,canvases[k].height);
};
frameRotate("frame", obj.rotation);
if (obj.hands.length > 0) {
var ids = _(obj.hands).pluck("id");
ids.forEach(function(id,i) {
d3.select("#hand" + i + "-id").text(id);
rotate("hand" + i, obj.hands[i]);
});
}
});
var canvases = {};
var ctxs = {};
canvases.frame = document.getElementById("frame");
[0,1,2,3].forEach(function(i) {
canvases["hand" + i] = document.getElementById("hand" + i);
});
for (k in canvases) {
canvases[k].height = 240;
canvases[k].width = 300;
ctxs[k] = canvases[k].getContext("2d");
ctxs[k].font = "13pt sans-serif";
};
var format = d3.format(".3f");
function frameRotate(id, rot) {
var ctx = ctxs[id];
var canvas = canvases[id];
// rotation matrix
rot.forEach(function(row,j) {
row.forEach(function(d,i) {
ctx.fillStyle = color(d);
ctx.font = Math.round(15*Math.sqrt(Math.abs(d))) + "pt sans-serif";
ctx.fillText(format(d), 100*i, 30+40*j);
});
});
};
function rotate(id, hand) {
var rot = hand.rotation;
var norm = hand.palmNormal;
var ctx = ctxs[id];
var canvas = canvases[id];
// rotation matrix
rot.forEach(function(row,j) {
row.forEach(function(d,i) {
ctx.fillStyle = color(d);
ctx.font = Math.round(15*Math.sqrt(Math.abs(d))) + "pt sans-serif";
ctx.fillText(format(d), 100*i, 30+40*j);
});
});
// normal vector
["X","Y","Z"].forEach(function(coord, i) {
ctx.fillStyle = "#000";
ctx.font = "10pt sans-serif";
ctx.fillText(coord, 34+100*i, 160);
ctx.beginPath();
ctx.moveTo(40+100*i, 180);
ctx.lineTo(40+100*i+30*Math.sin(Math.PI*norm[i]),180+30*Math.cos(Math.PI*norm[i]));
ctx.stroke();
});
};
function color(d) {
if (d > 0) {
return "black";
} else {
return "brown";
}
};
</script>