/
Layer.js
632 lines (447 loc) · 12.5 KB
/
Layer.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
/**
* @author syt123450 / https://github.com/syt123450
* @author zchholmes / https://github.com/zchholmes
*/
import { CloseButton } from "../../elements/CloseButton";
import { OpenTime, SeparateTime } from "../../utils/Constant";
import { LayerTranslateFactory } from "../../animation/LayerTranslateTween";
/**
* Layer, abstract layer, should not be initialized directly.
* Base class for NativeLayer, MergedLayer
*
* @param config, customized configuration for Layer
* @constructor
*/
function Layer( config ) {
/**
* scene object of THREE.js.
*
* @type { THREE.Scene }
*/
this.scene = undefined;
/**
* Order index number of the layer in model.
*
* @type { number }
*/
this.layerIndex = undefined;
/**
* The center (x, y, z) coordinates of the layer, related to the model.
*
* @type { Object } {x: double, y: double, z: double}
*/
this.center = undefined;
/**
* last layer in model relative to this layer.
*
* @type { Layer }
*/
this.lastLayer = undefined;
/**
* Store all neural value as an array.
* "undefined" means no value.
*
* @type { double[] }
*/
this.neuralValue = undefined;
/**
* Important property
* Shape describes input and output dimensions of the layer.
*
* @type { Array }
*/
this.inputShape = [];
this.outputShape = [];
/**
* Wrapper object represented the layer object in scene.
* All Three.js objects within the layer should be added to neuralGroup.
*
* @type { THREE.Object }
*/
this.neuralGroup = undefined;
/**
* Color of the layer for visualization.
*
* @type { HEX }
*/
this.color = undefined;
/**
* Handler for layer aggregation.
*
* @type { Object }
*/
this.aggregationHandler = undefined;
/**
* Handler for close button.
*
* @type { Object }
*/
this.closeButtonHandler = undefined;
/**
* Config to control whether to show close button.
* true -- show close button when layer is open.
* false -- never show close button.
*
* @type { boolean }
*/
this.hasCloseButton = true;
/**
* Config of close button size.
* Close button size is multiplied by the ratio number
*
* @type { number }
*/
this.closeButtonSizeRatio = 1;
/**
* Minimum opacity of the layer.
*
* @type { double } [0, 1]
*/
this.minOpacity = undefined;
/**
* Width and height in Three.js scene.
* actualWidth = unitLength * width
* (1d layer and 2d layer do not have actualHeight).
*
* @type { double }
*/
this.actualWidth = undefined;
this.actualHeight = undefined;
/**
* Depth of the layer object in the scene.
*
* @type { double }
*/
this.actualDepth = undefined;
/**
* Unit length used to render layer object.
*
* If the layer is not the first layer in model, value is from last layer.
* this.unitLength = this.lastLayer.unitLength;
*
* If layer is the first layer in model, checkout input layer for more information.
* this.unitLength = this.actualWidth / this.width;
*
* @type { double }
*/
this.unitLength = undefined;
/**
* Handler for object which is showing text.
*
* @type { Object }
*/
this.textElementHandler = undefined;
/**
* Handler for line group.
*
* @type { Object }
*/
this.lineGroupHandler = undefined;
/**
* Config to control showing text in layer.
*
* @type { boolean }
*/
this.textSystem = undefined;
/**
* Config of whether show relation line or not.
* true -- show relation lines.
* false -- do not show relation lines.
*
* @type { boolean }
*/
this.relationSystem = undefined;
/**
* Layer status on whether the layer is expanded or collapsed.
* true -- expanded;
* false -- collapsed.
*
* @type { boolean }
*/
this.isOpen = undefined;
/**
* Config on the speed of layer expansion and collapsion.
*
* @type { number }
*/
this.animationTimeRatio = 1;
this.openTime = OpenTime;
this.separateTime = SeparateTime;
/**
* Whether the layer is a group or not.
*
* @type { boolean }
*/
this.isGroup = false;
/**
* Label to define whether layer need an "output value" from backend model (tfjs, keras, or tf).
* For example, YoloGrid can automatically detect the output from last layer,
* users do not need to add value for YoloGrid value when they are preprocessing multi-output for the model.
*
* @type { boolean }
*/
this.autoOutputDetect = undefined;
/**
* name of the layer.
*
* @type { String }
*/
this.name = undefined;
/**
* Type of layer, each layer class has a specific layerType.
* For example, "Conv2d", "Pooling2d".
*
* @type { String }
*/
this.layerType = undefined;
// Load layer config.
this.loadBasicLayerConfig( config );
}
Layer.prototype = {
/**
* loadBasicLayerConfig() Load layer config.
* execute while initialization
*
* @param { JSON } config, layer config.
*/
loadBasicLayerConfig: function( config ) {
if ( config !== undefined ) {
if ( config.initStatus !== undefined ) {
if ( config.initStatus === "open" ) {
this.isOpen = true;
} else if ( config.initStatus === "close" ) {
this.isOpen = false;
} else {
console.error( "\"initStatus\" property do not support for " + config.initStatus + ", use \"open\" or \"close\" instead." );
}
}
if ( config.color !== undefined ) {
this.color = config.color;
}
if ( config.name !== undefined ) {
this.name = config.name;
}
if ( config.closeButton !== undefined ) {
if ( config.closeButton.display !== undefined ) {
this.hasCloseButton = config.closeButton.display;
}
if ( config.closeButton.ratio !== undefined ) {
this.closeButtonSizeRatio = config.closeButton.ratio;
}
}
if ( config.animationTimeRatio !== undefined ) {
if ( config.animationTimeRatio > 0 ) {
this.animationTimeRatio = config.animationTimeRatio;
}
this.openTime *= this.animationTimeRatio;
this.separateTime *= this.animationTimeRatio;
}
if ( config.minOpacity !== undefined ) {
this.minOpacity = config.minOpacity;
}
}
},
/**
* loadBasicLayerConfig() Load model config for layers. Model execute before "assemble".
*
* @param { JSON } modelConfig, model config, including default and customized model config.
*/
loadBasicModelConfig: function( modelConfig ) {
if ( this.isOpen === undefined ) {
this.isOpen = modelConfig.layerInitStatus;
}
if ( this.relationSystem === undefined ) {
this.relationSystem = modelConfig.relationSystem;
}
if ( this.textSystem === undefined ) {
this.textSystem = modelConfig.textSystem;
}
if ( this.minOpacity === undefined ) {
this.minOpacity = modelConfig.minOpacity;
}
this.openTime *= modelConfig.animationTimeRatio;
this.separateTime *= modelConfig.animationTimeRatio;
},
/**
* setLastLayer(), hold reference for last layer.
*
* @param { Layer } layer, reference of last layer which positioned before current layer in model.
*/
setLastLayer: function( layer ) {
this.lastLayer = layer;
},
/**
* setEnvironment(), hold ref of THREE.js scene and model
*
* @param { THREE.Object } scene, THREE.js scene.
* @param { Model } model, the model object current layer be added.
*/
setEnvironment: function( scene, model ) {
this.scene = scene;
this.model = model;
},
/**
* initCloseButton() init close button, add to neural group, and store close button handler.
*/
initCloseButton: function() {
if ( this.hasCloseButton ) {
// Get close button metrics.
let closeButtonPos = this.calcCloseButtonPos();
let closeButtonSize = this.closeButtonSizeRatio * this.calcCloseButtonSize();
// Create close button element.
let closeButtonHandler = new CloseButton(
closeButtonSize,
this.unitLength,
closeButtonPos,
this.color,
this.minOpacity
);
// Set layer information to close button.
closeButtonHandler.setLayerIndex( this.layerIndex );
// Store close button handler and add actual Close button element to neuralGroup.
this.closeButtonHandler = closeButtonHandler;
this.neuralGroup.add( this.closeButtonHandler.getElement() );
}
},
/**
* disposeCloseButton() remove close button element, clear handler.
*/
disposeCloseButton: function() {
this.neuralGroup.remove( this.closeButtonHandler.getElement() );
this.closeButtonHandler = undefined;
},
/**
* translateLayer(), translate layer to a target center.
*
* @param targetCenter, target center the layer is moving to
* @param translateTime, animation time
*/
translateLayer: function( targetCenter, translateTime ) {
// LayerTranslateFactory handles actual translate animation, checkout "LayerTranslateTween.js" for more information.
LayerTranslateFactory.translate( this, targetCenter, translateTime );
},
/**
* ============
*
* Functions below are abstract method for Layer.
* SubClasses ( specific layers ) override these abstract methods.
*
* ============
*/
/**
* init() abstract method
* Initialize THREE.Object in Layer, warp them into a group, and add to THREE.js scene.
*
* Model passes two parameters, center and actualDepth.
*
* @param { JSON } center, layer's center (x, y, z) relative to model
* @param { double } actualDepth, layer aggregation's depth
*/
init: function(center, actualDepth ) {
},
/**
* assemble() abstract method
* Configure layer's index in model, calculate the shape and parameters based on previous layer.
*
* Override this function to get information from previous layer
*
* @param { int } layerIndex, this layer's order in model
*/
assemble: function( layerIndex ) {
},
/**
* updateValue() abstract method
* Accept layer output value from model, update layer visualization if required.
*
* Model passes layer's output value to layer through updateValue method.
*
* Override this function to implement layer's own value update strategy.
*
* @param { double[] } value, neural output value.
*/
updateValue: function( value ) {
},
/**
* clear() abstract method
* Clear data and visualization in layer.
*
* Override this function to implement layer's own clear function.
*/
clear: function() {
},
/**
* handleClick() abstract method
* Event callback, if clickable element in this layer is clicked, execute this handle function.
*
* Override this function if layer has any clicked event.
*
* @param { THREE.Object } clickedElement, clicked element picked by model's Raycaster.
*/
handleClick: function( clickedElement ) {
},
/**
* handleHoverIn() abstract method
* Event callback, if hoverable element in this layer picked by Raycaster, execute this handle function.
*
* Override this function if layer has any hover event.
*
* @param { THREE.Object } hoveredElement, hovered element picked by model's Raycaster.
*/
handleHoverIn: function( hoveredElement ) {
},
/**
* handleHoverOut() abstract method
* Event callback, called by model if mouse hover out of this layer.
*
* Override this function if layer has some hover event.
*/
handleHoverOut: function() {
},
/**
* loadModelConfig() abstract method
* Load model's configuration into layer object.
*
* Override this function if there are some specific model configurations for layer.
*
* @param { JSON } modelConfig, default and user's configuration for model
*/
loadModelConfig: function( modelConfig ) {
},
/**
* calcCloseButtonSize() abstract method
* Called by initCloseButton function in abstract class "Layer", get close button size.
*
* Override this function to implement layer's own button size calculation strategy.
*
* @return { number } size, close button size
*/
calcCloseButtonSize: function() {
return 1;
},
/** y y /**
* calcCloseButtonPos() abstract method
* Called by initCloseButton function in abstract class "Layer", get close button position.
*
* Override this function to implement layer's own button position calculation strategy.
*
* @return { Object } close button position, { x: double, y: double, z: double }, relative to layer.
*/
calcCloseButtonPos: function() {
return {
x: 0,
y: 0,
z: 0
};
},
/**
* getBoundingWidth(), abstract layer
*
* Override this function to provide layer's bounding width based on layer's status.
*
* @return { number }
*/
getBoundingWidth: function() {
return 100;
}
};
export { Layer };