-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAugmenta-websocket.js
314 lines (253 loc) · 8.62 KB
/
Augmenta-websocket.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
/*
Augmenta protocol :
https://github.com/Theoriz/Augmenta/wiki
This code has been tested on Chataigne 1.7.5+
*/
// The module currently display 5 objects maximum declared in module.json
var maxObjectsDisplayed = 5;
function init()
{
local.parameters.pass_through.setCollapsed(true);
local.values.singleObject.setCollapsed(true);
local.values.fusion.setCollapsed(true);
local.scripts.setCollapsed(true);
local.scripts.getChild("augmenta_websocket").enableLog.set(true); // unknown function set : why ??
for(var i = 0 ; i < maxObjectsDisplayed ; i++)
{
local.values.getChild("object" + i).setCollapsed(true);
}
}
function moduleParameterChanged(param)
{
if(param.is(local.parameters.singleObjectMode)) {
if(local.parameters.singleObjectMode.get() == "none")
{
// Clean and fold single person object
local.values.singleObject.setCollapsed(true);
resetAugmentaObject(local.values.singleObject, args);
} else {
// Unfold single person object panel
local.values.singleObject.setCollapsed(false);
}
} else if(param.is(local.parameters.displayObjectExtraData))
{
if(local.parameters.displayObjectExtraData.get())
{
local.values.singleObject.getChild("Extra").setCollapsed(false);
for (var i = 0; i < maxObjectsDisplayed; i++) {
local.values.getChild("object"+i).getChild("Extra").setCollapsed(false);
}
} else
{
local.values.singleObject.getChild("Extra").setCollapsed(true);
for (var i = 0; i < maxObjectsDisplayed; i++) {
local.values.getChild("object"+i).getChild("Extra").setCollapsed(true);
}
}
}
}
function wsMessageReceived(message)
{
// scene
if (message.charAt(3) == 's') {
// initialize or update scene
setAugmentaScene(local.values.scene,message);
}
// fusion
else if (message.charAt(3) == 'f') {
// initialize or update fusion
setAugmentaFusion(local.values.fusion,message);
}
// object
else if (message.charAt(3) == 'o') {
var msg = JSON.parse(message);
if(message.charAt(15) == 'e') { // enter
var oid = msg.object.enter.oid;
// initialize object
for(var i = 0 ; i < maxObjectsDisplayed ; i++)
{
if(oid == i)
{
local.values.getChild("object" + i).setCollapsed(false);
setAugmentaObject(local.values.getChild("object" + i), message);
}
}
// initialize oldest and newest
// oldest is always oid = 0 if algo is correctly implemented
if(local.parameters.singleObjectMode.get() == "oldest" && oid == 0)
{
setAugmentaObject(local.values.singleObject, message);
} else if(local.parameters.singleObjectMode.get() == "newest" && oid == getNewestId())
{
setAugmentaObject(local.values.singleObject, message);
}
} else if(message.charAt(15) == 'u') { // update
var oid = msg.object.update.oid;
// update object
for(var i = 0 ; i < maxObjectsDisplayed ; i++)
{
if(oid == i)
{
updateAugmentaObject(local.values.getChild("object" + i), message);
// extra
if(local.parameters.displayObjectExtraData.get()) {
setAugmentaExtraObject(local.values.getChild("object" + i).extra, message);
}
}
}
// update oldest and newest
// oldest is always oid = 0 if algo is correctly implemented
if(local.parameters.singleObjectMode.get() == "oldest" && oid == 0)
{
updateAugmentaObject(local.values.singleObject, message);
// extra
if(local.parameters.displayObjectExtraData.get()) {
setAugmentaExtraObject(local.values.singleObject.extra, message);
}
} else if(local.parameters.singleObjectMode.get() == "newest" && oid == getNewestId())
{
updateAugmentaObject(local.values.singleObject, message);
// extra
if(local.parameters.displayObjectExtraData.get()) {
setAugmentaExtraObject(local.values.singleObject.extra, message);
}
}
} else if(message.charAt(15) == 'l') { //leave
var oid = msg.object.leave.oid;
// reset object
for(var i = 0 ; i < maxObjectsDisplayed ; i++)
{
if(oid == i)
{
local.values.getChild("object" + i).setCollapsed(true);
resetAugmentaObject(local.values.getChild("object" + i));
// extra
if(local.parameters.displayObjectExtraData.get()) {
resetAugmentaExtraObject(local.values.getChild("object" + i).extra, message);
}
}
}
// reset oldest and newest
// oldest is always oid = 0 if algo is correctly implemented
if(local.parameters.singleObjectMode.get() == "oldest" && oid == 0)
{
resetAugmentaObject(local.values.singleObject);
// extra
if(local.parameters.displayObjectExtraData.get()) {
setAugmentaExtraObject(local.values.singleObject.extra, message);
}
} else if(local.parameters.singleObjectMode.get() == "newest" && oid == getNewestId())
{
resetAugmentaObject(local.values.singleObject);
// extra
if(local.parameters.displayObjectExtraData.get()) {
setAugmentaExtraObject(local.values.singleObject.extra, message);
}
}
}
}
}
// scene updated or initialized
function setAugmentaScene (scene,message)
{
var msg = JSON.parse(message);
scene.frame.set(msg.scene.frame);
scene.objectCount.set(msg.scene.objectCount);
scene.width.set(msg.scene.scene.width);
scene.height.set(msg.scene.scene.height);
}
// fusion updated or initialized
function setAugmentaFusion(fusion,message)
{
var msg = JSON.parse(message);
fusion.videoOutOffset.set(msg.fusion.textureOffset.x,msg.fusion.textureOffset.y);
fusion.videoOutSize.set(msg.fusion.targetOutSize.x,msg.fusion.targetOutSize.y);
fusion.videoOutWidthInPixels.set(msg.fusion.textureBounds.x);
fusion.videoOutHeightInPixels.set(msg.fusion.textureBounds.y);
}
// called on object entered
function setAugmentaObject (object,message)
{
var msg = JSON.parse(message);
object.hasData.set(true);
object.frame.set(msg.object.enter.frame);
object.id.set(msg.object.enter.id);
object.oid.set(msg.object.enter.oid);
object.age.set(msg.object.enter.age);
object.centroid.set(msg.object.enter.centroid.x, msg.object.enter.centroid.y);
object.velocity.set(msg.object.enter.velocity.x, msg.object.enter.velocity.y);
object.orientation.set(msg.object.enter.orientation);
object.boundingRectCoord.set(msg.object.enter.boundingRect.x, msg.object.enter.boundingRect.y);
object.boundingRectWidth.set(msg.object.enter.boundingRect.width);
object.boundingRectHeight.set(msg.object.enter.boundingRect.height);
object.boundingRectRotation.set(msg.object.enter.orientation); // different from orientation ?
object.height.set(msg.object.enter.height);
}
// called on object updated
function updateAugmentaObject (object,message)
{
var msg = JSON.parse(message);
object.hasData.set(true);
object.frame.set(msg.object.update.frame);
object.id.set(msg.object.update.id);
object.oid.set(msg.object.update.oid);
object.age.set(msg.object.update.age);
object.centroid.set(msg.object.update.centroid.x, msg.object.update.centroid.y);
object.velocity.set(msg.object.update.velocity.x, msg.object.update.velocity.y);
object.orientation.set(msg.object.update.orientation);
object.boundingRectCoord.set(msg.object.update.boundingRect.x, msg.object.update.boundingRect.y);
object.boundingRectWidth.set(msg.object.update.boundingRect.width);
object.boundingRectHeight.set(msg.object.update.boundingRect.height);
object.boundingRectRotation.set(msg.object.update.orientation); // different from orientation ?
object.height.set(msg.object.update.height);
}
// called on object left
function resetAugmentaObject(object)
{
object.hasData.set(false);
object.frame.set(0);
object.id.set(0);
object.oid.set(0);
object.age.set(0);
object.centroid.set(0,0);
object.velocity.set(0,0);
object.orientation.set(0);
object.boundingRectCoord.set(0,0);
object.boundingRectWidth.set(0);
object.boundingRectHeight.set(0);
object.boundingRectRotation.set(0);
object.height.set(0);
}
// Extra
function setAugmentaExtraObject(object, message)
{
var msg = JSON.parse(message);
//script.log(msg.update.extra.distance);
object.frame.set(msg.object.update.extra.frame);
object.id.set(msg.object.update.extra.id);
object.oid.set(msg.object.update.extra.oid);
object.highest.set(msg.update.extra.highest.x,msg.update.extra.highest.y);
object.distance.set(msg.update.extra.distance);
object.reflectivity.set(msg.update.extra.reflectivity);
}
function resetAugmentaExtraObject(object)
{
object.frame.set(0);
object.id.set(0);
object.oid.set(0);
object.highest.set(0,0);
object.distance.set(0);
object.reflectivity.set(0);
}
function getNewestId(args)
{
if(local.values.scene.objectCount.get() > 0)
{
// newest is last id of the scene so newest oid is objectCount-1
return local.values.scene.objectCount.get() - 1;
} else
{
// no object in the scene
return -1;
}
}