-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecord.js
More file actions
254 lines (194 loc) · 6.1 KB
/
Record.js
File metadata and controls
254 lines (194 loc) · 6.1 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
controllerOptions = {};
var i = 0;
//Force printing of long arrays
nj.config.printThreshold = 1000;
//Establish vars
var currentNumHands = 0;
var previousNumHands = 0;
var numSamples = 100;
var oneFrameOfData = nj.zeros([5,4]);
var framesOfData = nj.zeros([5,4,6,numSamples]); //, numSamples
var currentSample = 0;
function HandleBone(bone, handCount, fingerIndex, interactionBox) {
try {
//Normalize point w/ interaction box
var normalizedPrevJoint = interactionBox.normalizePoint(bone.prevJoint, true);
//console.log("Prev:",normalizedPrevJoint.toString());
var normalizedNextJoint = interactionBox.normalizePoint(bone.nextJoint, true);
//console.log("Next:",normalizedNextJoint.toString());
//Save coordinates to vars
var x1 = normalizedPrevJoint[0];
var y1 = normalizedPrevJoint[1];
var z1 = normalizedPrevJoint[2];
var x2 = normalizedNextJoint[0];
var y2 = normalizedNextJoint[1];
var z2 = normalizedNextJoint[2];
//Set sum of all xyz coords as the data for the current finger @ index
//var coordinateSum = (x1 + y1 + z1 + x2 + y2 + z2);
var row = fingerIndex;
var col = bone.type;
oneFrameOfData.set(row, col, x1);
oneFrameOfData.set(row, col, y1);
oneFrameOfData.set(row, col, z1);
oneFrameOfData.set(row, col, x2);
oneFrameOfData.set(row, col, y2);
oneFrameOfData.set(row, col, z2);
framesOfData.set(row, col, 0, currentSample, x1);
framesOfData.set(row, col, 1, currentSample, y1);
framesOfData.set(row, col, 2, currentSample, z1);
framesOfData.set(row, col, 3, currentSample, x2);
framesOfData.set(row, col, 4, currentSample, y2);
framesOfData.set(row, col, 5, currentSample, z2);
//**MAKE SURE TRANSFORM HAPPENS AFTER DATA IS RECORDED**
//Scale new points to canvas
var canvasX1 = window.innerWidth * x1;
var canvasY1 = window.innerHeight * (1 - y1);
var canvasX2 = window.innerWidth * x2;
var canvasY2 = window.innerHeight * (1 - y2);
//Transform Coords to fit Window
/*var baseCoords = TransformCoordinates(bone.prevJoint[0],bone.prevJoint[1]);
var tipCoords = TransformCoordinates(bone.nextJoint[0],bone.nextJoint[1]);
var x1 = baseCoords[0];
var y1 = baseCoords[1];
var x2 = tipCoords[0];
var y2 = tipCoords[1];*/
//console.log("Row:", row, "Col:", col, "Sum:", coordinateSum);
/*var strokeMax = 200;
var strokeWeightMin = 10;
var zStrokeDensity = 100 * ((bone.prevJoint[2] - rawZmin) * strokeMax) / (rawZmax - rawZmin);*/
//Z min and max
var rawZmax = 200;
var rawZmin = -200
//Stroke thickness
var strokeWeightMax = 50;
var zStrokeWeight = ((bone.prevJoint[2] - rawZmin) * strokeWeightMax) / (rawZmax - rawZmin);
//Bone segment number
var boneType = bone.type;
//GREEN IF 1 HAND
if (handCount === 1) {
if (boneType === 0) {
stroke('rgb(179, 255, 224)');
} else if (boneType === 1) {
stroke('rgb(77, 255, 184)');
} else if (boneType === 2) {
stroke('rgb(0, 230, 138)');
} else if (boneType === 3) {
stroke('rgb(0, 153, 92)');
}
//RED IF 2 HANDS
} else if (handCount === 2) {
if (boneType === 0) {
stroke('rgb(255, 214, 204)');
} else if (boneType === 1) {
stroke('rgb(255, 133, 102)');
} else if (boneType === 2) {
stroke('rgb(255, 71, 26)');
} else if (boneType === 3) {
stroke('rgb(179, 36, 0)');
}
}
strokeWeight(zStrokeWeight);
line(canvasX1,canvasY1,canvasX2,canvasY2);
}
catch(err) {
//console.log("Drawing line failed");
}
}
function HandleHand(hand, handCount, interactionBox) {
//fingers
try {
var fingers = hand.fingers; //fingers in first hand
// each finger TAKEN FROM:
// https://developer-archive.leapmotion.com/documentation/javascript/api/Leap.Hand.html#Hand.fingers[], much easier for loop to work with.
//From distal to metacarpal,
for (var phalangeNum = 4; phalangeNum >= 0; phalangeNum--) {
//From thumb to pinky
for (var fingerNum = 0; fingerNum <= 4; fingerNum++) {
HandleBone(fingers[fingerNum].bones[phalangeNum], handCount, fingerNum, interactionBox);
}
}
} catch(err) {
//console.log("Fingers failed");
}
}
function HandleFrame(frame) {
try {
clear();
}
catch(err) {
//console.log("Clear did not work");
}
//Num hands
var handCount = frame.hands.length;
//data on first hand
var hand1 = frame.hands[0];
var hand2 = frame.hands[1];
//Interaction Box
var interactionBox = frame.interactionBox;
HandleHand(hand1, handCount, interactionBox);
HandleHand(hand2, handCount, interactionBox);
}
//Time
var timeSinceLastChange = new Date();
var timeLimit = 3;
var timeChangeInSeconds = 0;
var timerOn = false;
function Timer() {
//Timer runs until current time is 3 seconds after
if (timeChangeInSeconds < timeLimit) {
currentTime = new Date();
timeChangeInMilliseconds = currentTime - timeSinceLastChange;
timeChangeInSeconds = timeChangeInMilliseconds/1000;
//console.log(timeChangeInSeconds);
return false;
}
//New init time
timeSinceLastChange = new Date();
timeChangeInSeconds = 0;
return true;
//return timeChangeInSeconds > timeLimit;
}
function RecordData() {
/*if (previousNumHands === 2 && currentNumHands === 1) {
// Capture the data when you go from 2 hands to 1 hand
console.log( framesOfData.toString()); //.pick(null,null,null,0)
background(51);
}*/
if (previousNumHands === 1 && currentNumHands === 2) {
//Reset Timer
timerOn = true;
}
//If timing
if (timerOn) {
if (Timer()) {
// Capture the data when you go from 1 hand to 2 hands (Timer)
console.log( framesOfData.toString()); //.pick(null,null,null,0)
background(51);
//Turn it off
timerOn = false;
}
}
text(timeChangeInSeconds, 100, 100);
//Check if 2 hands
//Reset to zero if equal to num samples
if (currentNumHands === 2) {
//Increment Current Sample Count
currentSample++;
if (currentSample === numSamples) {
currentSample = 0;
}
}
//console.log(currentSample);
}
Leap.loop(controllerOptions, function(frame)
{
// # of hands in the current frame
currentNumHands = frame.hands.length;
//Draw hands
HandleFrame(frame);
// Capture the data when you go from 2 hands down to 1 hand
RecordData();
// # of hands in the previous frame
previousNumHands = frame.hands.length;
}
);