/
RGBInput.js
778 lines (498 loc) · 16.2 KB
/
RGBInput.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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
/**
* @author syt123450 / https://github.com/syt123450
*/
import { NativeLayer } from "../abstract/NativeLayer";
import { FmCenterGenerator } from "../../utils/FmCenterGenerator";
import { InputMap3d } from "../../elements/InputMap3d";
import { ChannelMap } from "../../elements/ChannelMap";
import { ColorUtils } from "../../utils/ColorUtils";
import { RGBTweenFactory } from "../../animation/RGBChannelTween";
import { ModelInitWidth } from "../../utils/Constant";
import { CloseButtonRatio } from "../../utils/Constant";
/**
* RGBInput, input layer, can be initialized by TensorSpace user.
* Layer for RGB image.
* RGB image has width and height, sometimes, we consider it a 2D input,
* however, in TensorSpace when we separate RGB image into R, G, B channel,
* we can find that the it actual has the third dimension depth = 3.
*
* @param config, user's configuration for RGBInput.
* @constructor
*/
function RGBInput( config ) {
// RGBInput inherits from abstract layer "NativeLayer".
NativeLayer.call( this, config );
/**
* RGBInput has three output dimensions: [ width, height, depth ].
*
* @type { int }
*/
this.width = undefined;
this.height = undefined;
this.depth = 3;
// Load user's RGBInput configuration.
this.loadLayerConfig( config );
/**
* As RGBInput is the first layer model, actualWidth is defined as a const.
* Use actualWidth to calculate actualHeight.
*
* @type { double }
*/
this.actualWidth = ModelInitWidth;
this.actualHeight = this.actualWidth / this.width * this.height;
/**
* Calculate unitLength for latter layers.
*
* @type { double }
*/
this.unitLength = this.actualWidth / this.width;
/**
* Channel maps's centers when layer is totally open.
*
* @type { Array }
*/
this.openFmCenters = FmCenterGenerator.getFmCenters( "line", 3, this.actualWidth, this.actualHeight );
/**
* Channel maps' centers when layer is closed.
*
* @type { Array }
*/
this.closeFmCenters = [];
for ( let i = 0; i < 3; i ++ ) {
this.closeFmCenters.push( {
x: 0,
y: 0,
z: 0
} );
}
// Predefined position for channel map when separate from close position.
this.separateTopPos = {
x: 0,
y: 20,
z: 0
};
this.separateBottomPos = {
x: 0,
y: -20,
z: 0
};
/**
* Channel map's handlers list.
*
* @type { Array }
*/
this.segregationHandlers = [];
/**
* Label to define whether layer need an "output value" from backend model (tfjs, keras, or tf).
* False means that user need to add value for RGBInput when they are preprocessing multi-output for the model.
*
* @type { boolean }
*/
this.autoOutputDetect = false;
this.layerDimension = 3;
this.layerType = "RGBInput";
}
RGBInput.prototype = Object.assign( Object.create( NativeLayer.prototype ), {
/**
* ============
*
* Functions below override base class NativeLayer's abstract method
*
* RGBInput overrides NativeLayer's function:
* init, assemble, updateValue, clear, handleClick, handleHoverIn, handleHoverOut, loadModelConfig,
* calcCloseButtonSize, calcCloseButtonPos, provideRelativeElements, getBoundingWidth
*
* ============
*/
/**
* init() create actual THREE.Object in RGBInput, warp them into a group, and add it to THREE.js's scene.
*
* Model passes two parameters, center and actualDepth, to RGBInput when call init() to initialize RGBInput.
*
* @param { JSON } center, layer's center (x, y, z) relative to model
* @param { double } actualDepth, layer aggregation's depth
*/
init: function( center, actualDepth ) {
this.center = center;
this.actualDepth = actualDepth;
// Init a neuralGroup as the wrapper for all THREE.Object in RGBInput.
this.neuralGroup = new THREE.Group();
this.neuralGroup.position.set( this.center.x, this.center.y, this.center.z );
// Init RGB map element.
this.initAggregationElement();
// Add the wrapper object to the actual THREE.js scene.
this.scene.add( this.neuralGroup );
},
/**
* assemble() configure layer's index in model.
*
* @param { int } layerIndex, this layer's order in model
*/
assemble: function( layerIndex, layerLevel ) {
this.layerIndex = layerIndex;
this.layerLevel = layerLevel;
},
/**
* updateValue() accept layer output value from model, update layer visualization if required.
*
* Model passes layer's output value to layer through updateValue method.
*
* @param { double[] } value, neural output value.
*/
updateValue: function( value ) {
// Store layer output value in "neuralValue" attribute, this attribute can be get by TensorSpace user.
this.neuralValue = value;
if ( this.isOpen ) {
// If layer is open, update Channel maps' visualization.
this.updateSegregationVis();
} else {
// If layer is close, update RGB map's visualization.
this.updateAggregationVis();
}
},
/**
* clear() clear data and visualization in layer.
*/
clear: function() {
if ( this.neuralValue !== undefined ) {
// Use handlers to clear visualization.
if ( this.isOpen ) {
for ( let i = 0; i < this.segregationHandlers.length; i ++ ) {
this.segregationHandlers[ i ].clear();
}
} else {
this.aggregationHandler.clear();
}
// Clear layer data.
this.neuralValue = undefined;
}
},
/**
* handleClick() If clickable element in this layer is clicked, execute this handle function.
*
* @param { THREE.Object } clickedElement, clicked element picked by model's Raycaster.
*/
handleClick: function( clickedElement ) {
if ( clickedElement.elementType === "RGBInputElement" ) {
// If aggregation element is clicked, open layer.
this.openLayer();
} else if ( clickedElement.elementType === "closeButton" ) {
// If close button is clicked, close layer.
this.closeLayer();
}
},
/**
* handleHoverIn() If hoverable element in this layer picked by Raycaster, execute this handle function.
*
* @param { THREE.Object } hoveredElement, hovered element picked by model's Raycaster.
*/
handleHoverIn: function( hoveredElement ) {
// If textSystem is enabled, show hint text, for example, show map size.
if ( this.textSystem ) {
this.showText( hoveredElement );
}
},
/**
* handleHoverOut() called by model if mouse hover out of this layer.
*/
handleHoverOut: function() {
// If textSystem is enabled, hide hint text, for example, hide map size.
if ( this.textSystem ) {
this.hideText();
}
},
/**
* loadModelConfig() load model's configuration into RGBInput object,
* If one specific attribute has been set before, model's configuration will not be loaded into it.
*
* Based on the passed in modelConfig parameter
*
* @param { JSON } modelConfig, default and user's configuration for model
*/
loadModelConfig: function( modelConfig ) {
// Call super class "Layer"'s method to load common model configuration, check out "Layer.js" file for more information.
this.loadBasicModelConfig( modelConfig );
if ( this.color === undefined ) {
this.color = modelConfig.color.RGBInput;
}
},
/**
* calcCloseButtonSize() get close button size.
* Called by initCloseButton function in abstract class "Layer",
*
* @return { number } size, close button size
*/
calcCloseButtonSize: function() {
return 3 * this.actualHeight * CloseButtonRatio;
},
/** y y /**
* calcCloseButtonPos() get close button position.
* Called by initCloseButton function in abstract class "Layer",
*
* @return { JSON } position, close button position, relative to layer.
*/
calcCloseButtonPos: function() {
return {
x: this.openFmCenters[ 0 ].x - this.actualWidth / 2 - 30,
y: 0,
z: 0
};
},
/**
* provideRelativeElements() return relative elements.
*
* Use bridge design patten:
* 1. "getRelativeElements" send request to previous layer for relative elements;
* 2. Previous layer's "provideRelativeElements" receives request, return relative elements.
*
* @param { JSON } request, parameter configured by request layer
* @return { Object } { isOpen: boolean, elementList: elements }
*/
provideRelativeElements: function( request ) {
let relativeElements = [];
if ( request.all !== undefined && request.all ) {
// When "all" attribute in request is true, return all elements displayed in this layer.
if ( this.isOpen ) {
for ( let i = 0; i < this.segregationHandlers.length; i ++ ) {
relativeElements.push( this.segregationHandlers[ i ].getElement() );
}
} else {
relativeElements.push( this.aggregationHandler.getElement() );
}
} else {
if ( request.index !== undefined ) {
if ( this.isOpen ) {
// If index attribute is set in request, and layer is open, return Channel map element which has the same index.
relativeElements.push( this.segregationHandlers[ request.index ].getElement() );
} else {
// If layer is closed, return aggregation element.
relativeElements.push( this.aggregationHandler.getElement() );
}
}
}
return {
isOpen: this.isOpen,
elementList: relativeElements
};
},
/**
* getBoundingWidth(), provide bounding box's width based on layer's status.
*
* @return { number }
*/
getBoundingWidth: function() {
if ( ( this.isOpen && !this.isWaitClose ) || this.isWaitOpen ) {
let maxX = this.openFmCenters[ 0 ].x;
for ( let i = 0; i < this.openFmCenters.length; i ++ ) {
maxX = this.openFmCenters[ i ].x > maxX ? this.openFmCenters[ i ].x : maxX;
}
return maxX - this.calcCloseButtonPos().x + this.calcCloseButtonSize() + this.actualWidth;
} else {
return this.actualWidth;
}
},
/**
* ============
*
* Functions above override base class NativeLayer's abstract method.
*
* ============
*/
/**
* openLayer() open RGBInput, switch layer status from "close" to "open".
*
* This API is exposed to TensorSpace user.
*/
openLayer: function() {
if ( !this.isOpen ) {
// RGBTweenFactory handles actual open animation, checkout "RGBChannelTween.js" for more information.
RGBTweenFactory.separate( this );
}
},
/**
* closeLayer() close Output1d, switch layer status from "open" to "close".
*
* This API is exposed to TensorSpace user.
*/
closeLayer: function() {
if ( this.isOpen ) {
// RGBTweenFactory handles actual close animation, checkout "RGBChannelTween.js" for more information.
RGBTweenFactory.aggregate( this );
}
},
/**
* loadLayerConfig() Load user's configuration into RGBInput.
* The configuration load in this function sometimes has not been loaded in loadBasicLayerConfig.
*
* @param { JSON } layerConfig, user's configuration for RGBInput.
*/
loadLayerConfig: function( layerConfig ) {
// Load input shape from user's configuration.
if ( layerConfig !== undefined ) {
this.inputShape = layerConfig.shape;
this.width = layerConfig.shape[ 0 ];
this.height = layerConfig.shape[ 1 ];
this.outputShape = [ this.width, this.height, this.depth ];
} else {
console.error( "\"shape\" property is require for RGBInput layer." );
}
},
/**
* initAggregationElement() create layer aggregation's THREE.js Object, configure it, and add it to neuralGroup in RGBInput.
*/
initAggregationElement: function() {
// InputMap3d Object is a wrapper for RGB map element, checkout "InputMap3d.js" for more information.
let aggregationHandler = new InputMap3d(
this.width,
this.height,
this.unitLength,
this.actualDepth,
{
x: 0,
y: 0,
z: 0
},
this.color,
this.minOpacity
);
// Set layer index to aggregation, RGB map object can know which layer it has been positioned.
aggregationHandler.setLayerIndex( this.layerIndex );
// Store handler for aggregation for latter use.
this.aggregationHandler = aggregationHandler;
// Get actual THREE.js element and add it to layer wrapper Object.
this.neuralGroup.add( this.aggregationHandler.getElement() );
// Update RGB map's visualization if layer's value has already been set.
if ( this.neuralValue !== undefined ) {
this.updateAggregationVis();
}
},
/**
* disposeAggregationElement() remove aggregation from neuralGroup, clear its handler, and dispose its THREE.js Object in RGBInput.
*/
disposeAggregationElement: function() {
this.neuralGroup.remove( this.aggregationHandler.getElement() );
this.aggregationHandler = undefined;
},
/**
* initSegregationElements() create Channel maps' THREE.js Object, configure them, and add them to neuralGroup in RGBInput.
*/
initSegregationElements: function() {
// Init r, g, b channel elements.
let rChannel = new ChannelMap(
this.width,
this.height,
this.unitLength,
this.actualDepth,
this.closeFmCenters[ 0 ],
this.color,
"R",
this.minOpacity
);
let gChannel = new ChannelMap(
this.width,
this.height,
this.unitLength,
this.actualDepth,
this.closeFmCenters[ 1 ],
this.color,
"G",
this.minOpacity
);
let bChannel = new ChannelMap(
this.width,
this.height,
this.unitLength,
this.actualDepth,
this.closeFmCenters[ 2 ],
this.color,
"B",
this.minOpacity
);
// Set layer index to channel handler, channel map object can know which layer it has been positioned.
rChannel.setLayerIndex( this.layerIndex );
// Set channelIndex index.
rChannel.setFmIndex( 0 );
gChannel.setLayerIndex( this.layerIndex );
gChannel.setFmIndex( 1 );
bChannel.setLayerIndex( this.layerIndex );
bChannel.setFmIndex( 2 );
// Store handler for feature map for latter use.
this.segregationHandlers.push( rChannel );
this.segregationHandlers.push( gChannel );
this.segregationHandlers.push( bChannel );
// Get actual THREE.js element and add it to layer wrapper Object.
this.neuralGroup.add( rChannel.getElement() );
this.neuralGroup.add( gChannel.getElement() );
this.neuralGroup.add( bChannel.getElement() );
// Update all channel maps' visualization if layer's value has already been set.
if ( this.neuralValue !== undefined ) {
this.updateSegregationVis();
}
},
/**
* disposeSegregationElements() remove feature maps from neuralGroup, clear their handlers, and dispose their THREE.js Object in RGBInput.
*/
disposeSegregationElements: function() {
for ( let i = 0; i < this.segregationHandlers.length; i ++ ) {
// Remove channel maps' THREE.js object from neuralGroup.
this.neuralGroup.remove( this.segregationHandlers[ i ].getElement() );
}
// Clear handlers, actual objects will automatically be GC.
this.segregationHandlers = [];
},
/**
* updateAggregationVis() update RGB map's aggregation's visualization.
*/
updateAggregationVis: function() {
// Get colors to render the surface of aggregation.
let colors = ColorUtils.getAdjustValues( this.neuralValue, this.minOpacity );
// aggregationHandler execute update visualization process.
this.aggregationHandler.updateVis( colors );
},
/**
* updateSegregationVis() update channel maps' visualization.
*/
updateSegregationVis: function() {
let colors = ColorUtils.getAdjustValues( this.neuralValue, this.minOpacity );
let rVal = [];
let gVal = [];
let bVal = [];
for ( let i = 0; i < colors.length; i ++ ) {
if ( i % 3 === 0 ) {
rVal.push( colors[ i ] );
} else if ( i % 3 === 1 ) {
gVal.push( colors[ i ] );
} else {
bVal.push( colors[ i ] );
}
}
this.segregationHandlers[ 0 ].updateVis( rVal );
this.segregationHandlers[ 1 ].updateVis( gVal );
this.segregationHandlers[ 2 ].updateVis( bVal );
},
/**
* showText() show hint text relative to given element.
*
* @param { THREE.Object } element
*/
showText: function( element ) {
if ( element.elementType === "channelMap" ) {
let fmIndex = element.fmIndex;
this.segregationHandlers[ fmIndex ].showText();
this.textElementHandler = this.segregationHandlers[ fmIndex ];
} else if ( element.elementType === "RGBInputElement" ) {
this.aggregationHandler.showText();
this.textElementHandler = this.aggregationHandler;
}
},
/**
* hideText() hide hint text.
*/
hideText: function() {
if ( this.textElementHandler !== undefined ) {
this.textElementHandler.hideText();
this.textElementHandler = undefined;
}
}
} );
export { RGBInput };