This repository has been archived by the owner on Oct 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAugmenta-simulator.js
429 lines (369 loc) · 10 KB
/
Augmenta-simulator.js
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
Augmenta protocol :
https://github.com/Theoriz/Augmenta/wiki
This code has been tested on Chataigne 1.6.0+
*/
// Default values for reset
var sceneDefaultWidth = 640;
var sceneDefaultHeight = 480;
var maxNumPeople = 5;
var numPeopleSaved = -1;
// updateRate
var updateRate;
var lastUpdateTime = 0;
// personData
var personArray = [];
var personStateArray = [];
var pid = 0;
function init()
{
// for now Chataigne allows 50hz max
updateRate = local.parameters.updateRate.get() == 0?0:1.0 / local.parameters.updateRate.get();
personArray = [local.values.person0,
local.values.person1,
local.values.person2,
local.values.person3,
local.values.person4];
// States : none entered update left
for(var i=0;i<maxNumPeople;i++)
{
personStateArray[i] = "none";
}
}
function update()
{
// Update rate computation
var time = util.getTime();
if(time > lastUpdateTime+updateRate)
{
lastUpdateTime = time;
updateScene();
sendScene();
updatePersons();
sendPersons();
}
}
function moduleParameterChanged(param)
{
if(param.name == "updateRate")
{
updateRate = param.get() == 0?0:1.0/param.get();
script.log("new update rate : "+updateRate);
} else if(param.name == "resetAll")
{
resetAll();
//updateUI();
} else if(param.name == "person0")
{
if(local.parameters.person0.get())
{
local.values.scene.numPeople.set(1);
} else {
local.values.scene.numPeople.set(0);
}
updateUI();
} else if(param.name == "person1")
{
if(local.parameters.person1.get())
{
local.values.scene.numPeople.set(2);
} else {
local.values.scene.numPeople.set(1);
}
updateUI();
} else if(param.name == "person2")
{
if(local.parameters.person2.get())
{
local.values.scene.numPeople.set(3);
} else {
local.values.scene.numPeople.set(2);
}
updateUI();
} else if(param.name == "person3")
{
if(local.parameters.person3.get())
{
local.values.scene.numPeople.set(4);
} else {
local.values.scene.numPeople.set(3);
}
updateUI();
} else if(param.name == "person4")
{
if(local.parameters.person4.get())
{
local.values.scene.numPeople.set(5);
} else {
local.values.scene.numPeople.set(4);
}
updateUI();
}
}
// TODO sendData value to take into account
/*function moduleValueChanged(value)
{
if(value.name == "sendData" && value.getParent().name == "person0")
{
script.log("pO sendData " +value.getParent().name);
}
}*/
function resetAll()
{
script.log("Reseting all values to default");
// reset scene to defaults values
setSceneSize(true,0,sceneDefaultWidth,sceneDefaultHeight,0);
setSceneSize(true,0,sceneDefaultWidth,sceneDefaultHeight,0); // dirty mandatory line for good UI
// reset other coordinates
// reset persons to defaults values
for(var i=0;i<maxNumPeople;i++)
{
resetPerson(i);
}
}
function setSceneSize(ResetCurrentTime, numPeople, width, height, depth)
{
if(ResetCurrentTime)
{
local.values.scene.currentTime.set(0);
}
local.values.scene.width.set(width);
local.values.scene.height.set(height);
local.values.scene.depth.set(depth);
}
function setPerson(oid, centroidX, centroidY, velocityX, velocityY, depth, boundingRectX, boundingRectY, boundingRectWidth, boundingRectHeight, highestX, highestY, highestZ)
{
personArray[oid].pid.set(pid+1);
pid++;
// age is computed during update
personArray[oid].centroidX.set(centroidX);
personArray[oid].centroidY.set(centroidY);
personArray[oid].velocityX.set(velocityX);
personArray[oid].velocityY.set(velocityY);
personArray[oid].depth.set(depth);
personArray[oid].boundingRectX.set(boundingRectX);
personArray[oid].boundingRectY.set(boundingRectY);
personArray[oid].boundingRectY.set(boundingRectY);
personArray[oid].boundingRectWidth.set(boundingRectWidth);
personArray[oid].boundingRectHeight.set(boundingRectHeight);
personArray[oid].highestX.set(highestX);
personArray[oid].highestY.set(highestY);
personArray[oid].highestZ.set(highestZ);
}
function setNumPeople(numPeople)
{
// Changing only if necessary
if(local.values.scene.numPeople.get() != numPeople)
{
local.values.scene.numPeople.set(numPeople);
script.log("Setting numPeople to " + local.values.scene.numPeople.get());
updateUI();
}
}
function setPersonX(oid, centroidX)
{
personArray[oid].centroidX.set(centroidX);
}
function setPersonY(oid, centroidY)
{
personArray[oid].centroidY.set(centroidY);
}
function updateScene()
{
local.values.scene.currentTime.set(local.values.scene.currentTime.get()+1); // incrementing time
if(local.values.scene.numPeople.get() != numPeopleSaved)
{
updateUI();
numPeopleSaved = local.values.scene.numPeople.get();
}
// Computing averageMotion and sumPercentcovered
if(local.values.scene.numPeople.get() > 0)
{
var sumVelocityX = 0;
var sumVelocityY = 0;
var sumPercentcovered = 0;
for(var i=0;i<local.values.scene.numPeople.get();i++)
{
sumVelocityX = sumVelocityX + personArray[i].velocityX.get();
sumVelocityY = sumVelocityY + personArray[i].velocityY.get();
sumPercentcovered = sumPercentcovered +
personArray[i].boundingRectWidth.get() * personArray[i].boundingRectHeight.get();
}
local.values.scene.averageMotionX.set(sumVelocityX/local.values.scene.numPeople.get());
local.values.scene.averageMotionY.set(sumVelocityY/local.values.scene.numPeople.get());
local.values.scene.percentCovered.set(sumPercentcovered/local.values.scene.numPeople.get());
}
}
function sendScene()
{
local.send("/au/scene",
local.values.scene.currentTime.get(),
local.values.scene.percentCovered.get(),
local.values.scene.numPeople.get(),
local.values.scene.averageMotionX.get(),
local.values.scene.averageMotionY.get(),
local.values.scene.width.get(),
local.values.scene.height.get(),
local.values.scene.depth.get());
}
function updateUI()
{
if(local.values.scene.numPeople.get() == 0)
{
local.parameters.person0.set(false);
local.parameters.person1.set(false);
local.parameters.person2.set(false);
local.parameters.person3.set(false);
local.parameters.person4.set(false);
for(var i=0;i<maxNumPeople;i++)
{
personArray[i].sendData.set(false);
}
} else if(local.values.scene.numPeople.get() == 1)
{
local.parameters.person0.set(true);
local.parameters.person1.set(false);
local.parameters.person2.set(false);
local.parameters.person3.set(false);
local.parameters.person4.set(false);
for(var i=maxNumPeople-1;i>4;i--)
{
personArray[i].sendData.set(false);
}
} else if(local.values.scene.numPeople.get() == 2)
{
local.parameters.person0.set(true);
local.parameters.person1.set(true);
local.parameters.person2.set(false);
local.parameters.person3.set(false);
local.parameters.person4.set(false);
for(var i=maxNumPeople-1;i>1;i--)
{
personArray[i].sendData.set(false);
}
} else if(local.values.scene.numPeople.get() == 3)
{
local.parameters.person0.set(true);
local.parameters.person1.set(true);
local.parameters.person2.set(true);
local.parameters.person3.set(false);
local.parameters.person4.set(false);
for(var i=maxNumPeople-1;i>2;i--)
{
personArray[i].sendData.set(false);
}
} else if(local.values.scene.numPeople.get() == 4)
{
local.parameters.person0.set(true);
local.parameters.person1.set(true);
local.parameters.person2.set(true);
local.parameters.person3.set(true);
local.parameters.person4.set(false);
for(var i=maxNumPeople-1;i>3;i--)
{
personArray[i].sendData.set(false);
}
} else if(local.values.scene.numPeople.get() == 5)
{
local.parameters.person0.set(true);
local.parameters.person1.set(true);
local.parameters.person2.set(true);
local.parameters.person3.set(true);
local.parameters.person4.set(true);
}
}
function resetPerson(oid)
{
personArray[oid].age.set(0);
personArray[oid].pid.set(0);
personArray[oid].centroidX.set(0);
personArray[oid].centroidY.set(0);
personArray[oid].velocityX.set(0);
personArray[oid].velocityY.set(0);
personArray[oid].depth.set(0);
personArray[oid].boundingRectX.set(0);
personArray[oid].boundingRectY.set(0);
personArray[oid].boundingRectWidth.set(0);
personArray[oid].boundingRectHeight.set(0);
personArray[oid].highestX.set(0);
personArray[oid].highestY.set(0);
personArray[oid].highestZ.set(0);
}
function updatePersons()
{
for(var i=0;i<maxNumPeople;i++)
{
// Processing Alive people
if(i<=(local.values.scene.numPeople.get()-1))
{
if(personStateArray[i] == "none")
{
personStateArray[i] = "personEntered";
personArray[i].pid.set(pid+1);
pid++;
} else if (personStateArray[i] == "personEntered")
{
personStateArray[i] = "personUpdated";
// TODO : compute speed
} else
{
//script.log("Strange state for an alive person : " + personStateArray[oid]);
}
personArray[i].age.set(personArray[i].age.get() + 1);
} else // processing dying people
{
if(personStateArray[i] == "personUpdated")
{
personStateArray[i] = "personWillLeave";
personArray[i].age.set(personArray[i].age.get() + 1);
// TODO : compute speed
} else if(personStateArray[i] == "personWillLeave")
{
personStateArray[i] = "none";
resetPerson(i);
} else
{
//script.log("Strange state for a dying person : " + personStateArray[oid]);
}
}
}
}
function sendPersons()
{
for(var i=0;i<local.values.scene.numPeople.get();i++)
{
sendPerson(i,"update");
}
}
function sendPerson(oid, state)
{
var address = "init";
if(state == "update")
{
address = "/au/personUpdated";
} else if(state == "entered")
{
address = "/au/personEntered";
} else if(state == "left")
{
address = "/au/personWillLeave";
}
local.send(
address,
personArray[oid].pid.get(),
oid,
personArray[oid].age.get(),
personArray[oid].centroidX.get(),
personArray[oid].centroidY.get(),
personArray[oid].velocityX.get(),
personArray[oid].velocityY.get(),
personArray[oid].depth.get(),
personArray[oid].boundingRectX.get(),
personArray[oid].boundingRectY.get(),
personArray[oid].boundingRectWidth.get(),
personArray[oid].boundingRectHeight.get(),
personArray[oid].highestX.get(),
personArray[oid].highestY.get(),
personArray[oid].highestZ.get());
personArray[oid].sendData.set(true);
}