-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1475 lines (1256 loc) · 43.2 KB
/
index.html
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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Particle Simulation</title>
<script src="js/three.js-r108/three.min.js"></script>
<script src="js/OrbitControls.js"></script>
<script src="js/stats.js-r17/stats.min.js"></script>
<script src="js/dat.gui-0.7.6/dat.gui.min.js"></script>
<script src="js/d3-5.11.0/d3.min.js" charset="utf-8"></script>
<script src="js/c3-0.7.8/c3.js"></script>
<link href="js/c3-0.7.8/c3.min.css" rel="stylesheet">
<style>
* {
margin: 0;
}
html, body {
height: 100%;
}
.container {
position: relative;
height: 100%;
margin: 0px auto;
overflow: hidden;
}
#animation {
position: absolute;
height: 100%;
width: 100%;
}
#animation canvas {
display: block; /* The default display, inline, would add small margin below the canvas */
height: 100%;
width: 100%;
}
.chart-container {
position: absolute;
width: 35%;
height: 50%;
left: 0;
bottom: 25%;
}
.dg
.dg input,
.dg .title,
.dg .close-button,
.dg .property-name,
.dg .save-row select,
.dg .save-row .button {
font-size: 15px !important;
}
.dg .c input[type=text] {
padding-top: 0px;
padding-bottom: 2px;
height: 17px;
}
.dg .cr.number input[type=text] {
color: white;
}
.dg .save-row {
line-height: 30px !important;
}
.dg .save-row select {
width: 152px !important;
}
.dg li.save-row .button {
color: black;
background: white;
}
.dg li.save-row .button.gears {
color: black;
background: white;
background-size: 13px;
background-repeat: no-repeat;
background-position: center;
background-image:
url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 16 16'><path d='M14.59 9.535c-0.839-1.454-0.335-3.317 1.127-4.164l-1.572-2.723c-0.449 0.263-0.972 0.414-1.529 0.414-1.68 0-3.042-1.371-3.042-3.062h-3.145c0.004 0.522-0.126 1.051-0.406 1.535-0.839 1.454-2.706 1.948-4.17 1.106l-1.572 2.723c0.453 0.257 0.845 0.634 1.123 1.117 0.838 1.452 0.336 3.311-1.12 4.16l1.572 2.723c0.448-0.261 0.967-0.41 1.522-0.41 1.675 0 3.033 1.362 3.042 3.046h3.145c-0.001-0.517 0.129-1.040 0.406-1.519 0.838-1.452 2.7-1.947 4.163-1.11l1.572-2.723c-0.45-0.257-0.839-0.633-1.116-1.113zM8 11.24c-1.789 0-3.24-1.45-3.24-3.24s1.45-3.24 3.24-3.24c1.789 0 3.24 1.45 3.24 3.24s-1.45 3.24-3.24 3.24z'></path></svg>");
}
.dg li.save-row .button.gears:hover {
background-color: #bab19e;
}
.info-wrapper {
position: fixed;
display: none;
z-index: 1;
opacity: 0;
background-color: rgba(0, 0, 0, 0.8);
top: 0px;
left: 0px;
right: 0px;
bottom: 0px;
transition: opacity .2s linear;
}
#info {
position: fixed;
display: none;
z-index: 2;
opacity: 0;
left: 50%;
top: 50%;
transform: translate(-50%, -50%) scale(1.1);
width: 460px;
padding: 15px;
color: #eee;
background-color: #222;
font: 13px 'Lucida Grande', sans-serif;
white-space: pre-line;
transition: transform .2s ease-out 0s, opacity .2s linear 0s;
}
#info a {
color: white;
display: inline-block;
position: relative;
text-decoration: none;
}
#info a:before {
background-color: white;
content: '';
height: 2px;
position: absolute;
bottom: -2px;
transition: width 0.3s ease-in-out;
width: 100%;
}
#info a:hover:before {
width: 0;
}
#info button {
margin: 0 auto;
display: block;
}
</style>
<!--
Chart styles inspired from here: https://github.com/c3js/c3/issues/210#issuecomment-353671994
Please keep one rule per style element, since they will be updated dynamically in adjustColors()
-->
<style id="chart-axes-color">
.chart-container .c3 .c3-axis-x path,
.chart-container .c3 .c3-axis-x line,
.chart-container .c3 .c3-axis-y path,
.chart-container .c3 .c3-axis-y line {
}
</style>
<style id="chart-text-elements-color">
.chart-container .c3 .c3-title,
.chart-container .c3 .c3-axis-x g,
.chart-container .c3 .c3-axis-y g,
.chart-container .c3 .c3-axis-y-label,
.chart-container .c3 .c3-axis-x-label {
}
</style>
<style id="chart-legend-body-text-color">
.chart-container .c3 .c3-legend-item-body text {
}
</style>
<style id="chart-legend-target-text-color">
.chart-container .c3 .c3-legend-item-target text {
}
</style>
<style id="c3-title-text-size">
.chart-container .c3 .c3-title {
font-size: 150%;
}
</style>
<style id="c3-axis-label-text-size">
.chart-container .c3 .c3-axis .c3-axis-x-label,
.chart-container .c3 .c3-axis .c3-axis-y-label {
font-size: 150%;
}
</style>
<style id="c3-axis-tick-text-size">
.chart-container .c3 .c3-axis text {
font-size: 175%;
}
</style>
<style id="c3-data-target-line-stroke">
.chart-container .c3 .c3-target-target {
stroke-dasharray: 5;
}
</style>
<style id="c3-legend-target-tile-stroke">
.chart-container .c3 .c3-legend-item-target .c3-legend-item-tile {
stroke-dasharray: 5;
}
</style>
<script>
"use strict";
let presets = {
"preset": "Sim-1",
"remembered": {
"Sim-1": {
"0": {},
"1": {},
"2": {},
"3": {
"width": 0.6,
"height": 1.6,
"depth": 0.4
},
"4": {
"radius": 0.08,
"cageEnabled": false
},
"5": {
"radius": 0.04
},
"6": {},
"7": {
"changeColor": false,
"particlesInvisibleInBox": false,
"radius": 0.004,
"count": 5000
},
"8": {},
"9": {},
"10": {
"initialDelay": 250,
"rate": 50
},
"11": {
"initialDelay": 500,
"rate": 25
}
},
"Sim-2": {
"0": {},
"1": {},
"2": {},
"3": {
"width": 0.6,
"height": 1.6,
"depth": 0.4
},
"4": {
"radius": 0.08,
"cageEnabled": false
},
"5": {
"radius": 0.04
},
"6": {},
"7": {
"changeColor": false,
"particlesInvisibleInBox": false,
"radius": 0.004,
"count": 5000
},
"8": {},
"9": {},
"10": {
"initialDelay": 250,
"rate": 25
},
"11": {
"initialDelay": 750,
"rate": 10
}
},
"Sim-3": {
"0": {},
"1": {},
"2": {},
"3": {
"width": 0.6,
"height": 1.6,
"depth": 0.4
},
"4": {
"radius": 0.08,
"cageEnabled": false
},
"5": {
"radius": 0.04
},
"6": {},
"7": {
"changeColor": true,
"particlesInvisibleInBox": false,
"radius": 0.004,
"count": 5000
},
"8": {},
"9": {},
"10": {
"initialDelay": 250,
"rate": 50
},
"11": {
"initialDelay": 500,
"rate": 20
}
},
"Sim-4": {
"0": {},
"1": {},
"2": {},
"3": {
"width": 0.6,
"height": 1.6,
"depth": 0.4
},
"4": {
"radius": 0.08,
"cageEnabled": false
},
"5": {
"radius": 0.04
},
"6": {},
"7": {
"changeColor": true,
"particlesInvisibleInBox": false,
"radius": 0.003,
"count": 5000
},
"8": {},
"9": {},
"10": {
"initialDelay": 250,
"rate": 25
},
"11": {
"initialDelay": 750,
"rate": 10
}
},
"Sim-5": {
"0": {},
"1": {},
"2": {},
"3": {
"width": 0.6,
"height": 1.6,
"depth": 0.4
},
"4": {
"radius": 0.08,
"cageEnabled": true
},
"5": {
"radius": 0.04
},
"6": {},
"7": {
"changeColor": false,
"particlesInvisibleInBox": false,
"radius": 0.004,
"count": 5000
},
"8": {},
"9": {},
"10": {
"initialDelay": 250,
"rate": 25
},
"11": {
"initialDelay": 750,
"rate": 10
}
},
"Sim-6": {
"0": {},
"1": {},
"2": {},
"3": {
"width": 0.6,
"height": 1.6,
"depth": 0.4
},
"4": {
"radius": 0.08,
"cageEnabled": true
},
"5": {
"radius": 0.04
},
"6": {},
"7": {
"changeColor": true,
"particlesInvisibleInBox": false,
"radius": 0.004,
"count": 5000
},
"8": {},
"9": {},
"10": {
"initialDelay": 250,
"rate": 25
},
"11": {
"initialDelay": 750,
"rate": 10
}
}
}
};
const originalPresets = Object.keys(presets.remembered);
const SCENE_SCALE = 1;
let Config = function() {
this.scene = {
background: {
color: getGuiColorString('black'),
},
box: {
color: '#c7c9d2',
edgesColor: '#f7ebeb',
opacity: 0.3,
width: 0.6,
height: 1.6,
depth: 0.4,
},
targetCage: {
color: getGuiColorString('gray'),
opacity: 0.2,
radius: 0.08,
viscosity: 0.9,
rotationSpeed: 0.005,
particleCaptureProbability: 0.5,
gravity: 0.05,
cageEnabled: false,
},
target: {
x: 0,
y: 0,
z: 0,
color: getGuiColorString('red'),
opacity: 0.07,
radius: 0.04,
viscosity: 0.155,
shininess: 50,
emissiveColor: getGuiColorString('springgreen'),
emissiveIntensityMax: 2,
},
tubes: {
color: '#e8cdcd',
edgesColor: '#edcaca',
height: 0.2,
radius: 0.015,
verticalDisplacement: 0.75,
},
particles: {
color: '#00a4ff',
changeColor: false,
particlesInvisibleInBox: false,
colorInTarget: getGuiColorString('red'),
radius: 0.004,
opacityInBox: 0.15,
count: 5000,
batchSize: 100,
batchDelay: 5,
velocityMin: SCENE_SCALE / 1000,
velocityMax: SCENE_SCALE / 100,
},
};
this.chart = {
bodyPlotColor: getGuiColorString('white'),
targetPlotColor: getGuiColorString('yellow'),
axesColor: getGuiColorString('white'),
textElementsColor: getGuiColorString('white'),
showPoints: true,
sampleInterval: 30,
transitionDuration: 130,
exclusivePlots: true,
};
this.gui = {
width: 380,
};
this.excretion = {
initialDelay: 250,
rate: 50,
delay: 20,
};
this.targetExcretion = {
initialDelay: 500,
rate: 25,
delay: 40,
};
};
// Handles which need to be accessed when rendering frames
let animationFrame, scene, camera, renderer, controls, stats, gui, chart, config;
// Scene objects
let box, boxEdges, targetCage, target, inputTube, inputTubeEdges, outputTube, outputTubeEdges, particles = [], particleBatches = [], visibleParticles = [], particlesInBox = 0, particlesInTarget = 0, currentParticleBatch = 0;
// GUI controls that need to be refreshed when adjusting sizes
let guiTargetCageRadius, guiTargetRadius, guiTargetX, guiTargetY, guiTargetZ;
// Keep track of elapsed frames
let elapsed = 0;
// Hack: Attach pauseAnimation to the `window` global, so we
// can pass the `window` object to gui.add()
window.pauseAnimation = false;
function getGuiColorString(color) {
return '#' + new THREE.Color(color).getHexString();
};
// Return a random value in interval [-max, -min) \cup [min, max)
function getRandomValueWithGap(min, max, positiveOnly = false) {
let rand = Math.random();
let value = rand * (max - min) + min;
// 4294967296 = 2^32
if (positiveOnly || ((rand * 4294967296) & 1) > 0) {
return value;
}
return -value;
};
function getRandomVelocity() {
return new THREE.Vector3(
getRandomValueWithGap(config.scene.particles.velocityMin, config.scene.particles.velocityMax, true),
getRandomValueWithGap(config.scene.particles.velocityMin, config.scene.particles.velocityMax),
getRandomValueWithGap(config.scene.particles.velocityMin, config.scene.particles.velocityMax),
);
}
// Check to see if the position of the particle doesn't go past the box wall
// and also check to see if it's velocity is less than or greater than 0 so
// it doesn't spike past and get stuck by the edge of the wall.
function handleBoxWallColisions(p) {
if(p.position.x >= box.geometry.parameters.width / 2 - p.geometry.parameters.radius && p.velocity.x > 0 || p.position.x <= -box.geometry.parameters.width / 2 + p.geometry.parameters.radius && p.velocity.x < 0)
p.velocity.setX(-p.velocity.x);
if(p.position.y >= box.geometry.parameters.height / 2 - p.geometry.parameters.radius && p.velocity.y > 0 || p.position.y <= -box.geometry.parameters.height / 2 + p.geometry.parameters.radius && p.velocity.y < 0)
p.velocity.setY(-p.velocity.y);
if(p.position.z >= box.geometry.parameters.depth / 2 - p.geometry.parameters.radius && p.velocity.z > 0 || p.position.z <= -box.geometry.parameters.depth / 2 + p.geometry.parameters.radius && p.velocity.z < 0)
p.velocity.setZ(-p.velocity.z);
}
// Pass collisionProbability=1 to not skip any collision
function handleSphereWallColisions(sphere, p, collisionProbability) {
if (collisionProbability < 1 && Math.random() < (1 - collisionProbability)) {
return;
}
if (sphere.geometry.boundingSphere.distanceToPoint(p.position) > 0) {
let normal = new THREE.Vector3();
normal.subVectors(p.position, sphere.geometry.boundingSphere.center).normalize();
p.velocity = p.velocity.reflect(normal);
}
}
function excreteParticle(p) {
p.isInTarget = false;
p.isExcreted = true;
p.visible = true;
p.material.opacity = 1;
p.position.x = box.geometry.parameters.width / 2 + config.scene.tubes.height;
p.position.y = -config.scene.box.height / 2 * config.scene.tubes.verticalDisplacement;
p.position.z = 0;
p.velocity = getRandomVelocity();
}
function updateObjects() {
if (window.pauseAnimation) {
return;
}
if (config.scene.targetCage.cageEnabled) {
targetCage.rotation.y += config.scene.targetCage.rotationSpeed;
}
// Release a new batch of particles
if (elapsed % config.scene.particles.batchDelay === 0 && currentParticleBatch < particleBatches.length) {
let batch = particleBatches[currentParticleBatch];
for (let i = 0; i < batch.length; i++) {
let p = batch[i];
p.visible = !config.scene.particles.particlesInvisibleInBox;
p.position.add(p.velocity); // Start moving the particles as soon as they become visible
visibleParticles.push(p);
}
particlesInBox += batch.length;
currentParticleBatch++;
}
// Set the number of particles that need to be excreted during this iteration.
// We might not have that many left, so we handle this case gracefully in the loop below.
let particlesToExcreteFromBox = 0;
let particlesToExcreteFromTarget = 0;
if (elapsed > config.excretion.initialDelay && elapsed % config.excretion.delay === 0) {
particlesToExcreteFromBox = config.excretion.rate;
}
if (elapsed > config.targetExcretion.initialDelay && elapsed % config.targetExcretion.delay === 0) {
particlesToExcreteFromTarget = config.targetExcretion.rate;
}
// Animate particles
for (let i = 0; i < visibleParticles.length; i++) {
let p = visibleParticles[i];
if (p.isInTarget || target.geometry.boundingSphere.containsPoint(p.position)) {
if (!p.isInTarget) {
p.isInTarget = true;
p.visible = true;
p.material.opacity = 1;
if (config.scene.particles.changeColor) {
p.material.color.set(config.scene.particles.colorInTarget);
}
particlesInTarget++;
particlesInBox--;
}
p.position.addScaledVector(p.velocity, 1 - config.scene.target.viscosity);
handleSphereWallColisions(target, p, 1);
if (particlesToExcreteFromTarget > 0) {
excreteParticle(p);
particlesInTarget--;
particlesToExcreteFromTarget--;
}
} else {
// Check if we leaked any particles from the target and update them accordingly.
// This situation doesn't occur in the current model.
if (p.isInTarget) {
p.isInTarget = false;
p.material.color.set(config.scene.particles.color);
particlesInTarget--;
particlesInBox++;
}
if (config.scene.targetCage.cageEnabled && (targetCage.geometry.boundingSphere.containsPoint(p.position))) {
p.visible = true;
// Get the vector from the target center to the particle
let positionDirection = target.position.clone().sub(p.position);
let depthInCage = config.scene.targetCage.radius - p.position.length();
// Simulate gravity
let l = p.velocity.length();
positionDirection.multiplyScalar(depthInCage * config.scene.targetCage.gravity);
p.velocity.add(positionDirection);
p.velocity.normalize().multiplyScalar(l);
// Simulate viscosity
p.position.addScaledVector(p.velocity, (1 - config.scene.targetCage.viscosity));
handleSphereWallColisions(targetCage, p, config.scene.targetCage.particleCaptureProbability);
} else {
// Inside or outside the box
// Remove if too far away
if (p.position.length() > config.scene.box.width * 2) {
p.visible = false;
} else {
p.position.add(p.velocity);
if (config.scene.targetCage.cageEnabled && !p.isExcreted && !targetCage.geometry.boundingSphere.containsPoint(p.position)) {
p.visible = !config.scene.particles.particlesInvisibleInBox;
p.material.opacity = config.scene.particles.opacityInBox;
}
}
}
if (!p.isExcreted) {
handleBoxWallColisions(p);
if (particlesToExcreteFromBox > 0) {
excreteParticle(p);
particlesInBox--;
particlesToExcreteFromBox--;
}
}
}
}
// Handle target glow
target.material.emissiveIntensity = config.scene.target.emissiveIntensityMax / config.scene.particles.count * particlesInTarget;
// Add datapoints to chart
if ((particlesInBox > 0 || particlesInTarget > 0) && elapsed % config.chart.sampleInterval == 0) {
let bodyValue = config.chart.exclusivePlots ? particlesInBox : (particlesInBox + particlesInTarget);
chart.flow({
columns: [
['body', bodyValue / config.scene.particles.count * 100],
['target', particlesInTarget / config.scene.particles.count * 100],
],
length: 0,
});
}
// Advance to the next frame
elapsed += 1;
}
function renderLoop() {
animationFrame = window.requestAnimationFrame(renderLoop);
updateObjects();
renderer.render(scene, camera);
controls.update();
stats.update();
}
function adjustColors() {
scene.background = new THREE.Color(config.scene.background.color);
box.material.color.set(config.scene.box.color);
boxEdges.material.color.set(config.scene.box.edgesColor);
targetCage.material.color.set(config.scene.targetCage.color);
target.material.color.set(config.scene.target.color);
target.material.emissive.set(config.scene.target.emissiveColor);
inputTube.material.color.set(config.scene.tubes.color);
inputTubeEdges.material.color.set(config.scene.tubes.edgesColor);
outputTube.material.color.set(config.scene.tubes.color);
outputTubeEdges.material.color.set(config.scene.tubes.edgesColor);
for (let i = 0; i < particles.length; i++) {
let p = particles[i];
if (p.isInTarget && config.scene.particles.changeColor) {
p.material.color.set(config.scene.particles.colorInTarget);
} else if (config.scene.particles.changeColor) {
p.material.color.set(config.scene.particles.color);
} else {
// Use colorInTarget if changeColor is unchecked
p.material.color.set(config.scene.particles.colorInTarget);
}
}
document.getElementById("chart-axes-color").sheet.cssRules[0].style.stroke = config.chart.axesColor;
document.getElementById("chart-text-elements-color").sheet.cssRules[0].style.fill = config.chart.textElementsColor;
document.getElementById("chart-legend-body-text-color").sheet.cssRules[0].style.fill = config.chart.bodyPlotColor;
document.getElementById("chart-legend-target-text-color").sheet.cssRules[0].style.fill = config.chart.targetPlotColor;
}
function toggleParticlesInBoxVisibility() {
for (let i = 0; i < visibleParticles.length; i++) {
let p = particles[i];
if (!p.isInTarget && !p.isExcreted) {
p.visible = !config.scene.particles.particlesInvisibleInBox;
}
}
}
function adjustOpacities() {
adjustColors();
box.material.opacity = config.scene.box.opacity;
targetCage.material.opacity = config.scene.targetCage.opacity;
target.material.opacity = config.scene.target.opacity;
for (let i = 0; i < particles.length; i++) {
let p = particles[i];
if (!p.isInTarget && !p.isExcreted) {
p.material.transparent = config.scene.particles.changeColor
p.material.opacity = config.scene.particles.opacityInBox;
}
}
}
function adjustTargetShininess() {
target.material.shininess = config.scene.target.shininess;
}
function adjustTargetPosition() {
// Moving the target implies moving the cage as well
targetCage.position.setX(config.scene.target.x);
targetCage.position.setY(config.scene.target.y);
targetCage.position.setZ(config.scene.target.z);
targetCage.geometry.boundingSphere.center = targetCage.position;
target.position.setX(config.scene.target.x);
target.position.setY(config.scene.target.y);
target.position.setZ(config.scene.target.z);
target.geometry.boundingSphere.center = target.position;
}
function getTargetPositionRange(range) {
let output = range / 2 - config.scene.targetCage.radius;
if (range < 0) {
return config.scene.targetCage.radius;
}
return output
}
function adjustGUIRanges() {
if (!guiTargetCageRadius || !guiTargetRadius) {
return;
}
guiTargetCageRadius.min(config.scene.target.radius).updateDisplay();
guiTargetRadius.max(config.scene.targetCage.radius).updateDisplay();
if (!guiTargetX || !targetYRange || !targetZRange) {
return;
}
let targetXRange = getTargetPositionRange(config.scene.box.width);
guiTargetX.min(-targetXRange).max(targetXRange).updateDisplay();
let targetYRange = getTargetPositionRange(config.scene.box.height);
guiTargetY.min(-targetYRange).max(targetYRange).updateDisplay();
let targetZRange = getTargetPositionRange(config.scene.box.depth);
guiTargetZ.min(-targetZRange).max(targetZRange).updateDisplay();
}
function toggleChartPoints() {
chart.internal.config.point_show = config.chart.showPoints;
}
function resetAnimation() {
// Stop the animation
window.cancelAnimationFrame(animationFrame);
removeSceneObjects();
// Reset globals. This is super-clumsy and needs refactoring!
particles = [];
particleBatches = [];
visibleParticles = [];
particlesInBox = 0;
particlesInTarget = 0;
currentParticleBatch = 0;
elapsed = 0;
initSceneObjects();
chart = chart.destroy();
initChart();
adjustColors();
adjustGUIRanges();
// Restart the animation
renderLoop();
}
function rememberGUISettings() {
// Invoking gui.remember() at least once will show the save menu.
// Note that changing presets will fire the event handlers for all the configuration options
// which reloads all the UI items multiple times quickly via resetAnimation. This is not ideal.
gui.remember(config);
// Descend recursively through all the subobjects of the config and inform the GUI of their existence.
let descender = (obj) => {
Object.keys(obj).forEach(key => {
if (typeof obj[key] === "object") {
gui.remember(obj[key]);
descender(obj[key]);
}
});
};
descender(config);
}
function toggleCage() {
targetCage.visible = !targetCage.visible;
}
function toggleChart() {
// For some reason `chart.toggle('', {withLegend: true})` doesn't work :(
let c = document.getElementById('chart');
if (!c.style.visibility || c.style.visibility == 'visible') {
chart.hide('', {withLegend: true});
c.style.visibility = 'hidden';
} else {
chart.show('', {withLegend: true});
c.style.visibility = 'visible';
}
}
function toggleStats() {
if (!stats.dom.style.visibility || stats.dom.style.visibility == 'visible') {
stats.dom.style.visibility = "hidden";
} else {
stats.dom.style.visibility = "visible";
}
}
function saveData() {
// Set the header of the CSV file
let payload = '"% FOV","% ROI","Time interval"';
let bodyValues = chart.data('body')[0].values;
let targetValues = chart.data('target')[0].values;
// Write the values wrapped in quotes, just in case some computer locale
// uses commas as the decimal separator.
for (let i = 0; i < bodyValues.length; i++) {
payload += '\n"' + bodyValues[i].value.toString() + '","' + targetValues[i].value.toString() + '","' + (i+1).toString() + '"';
}
// Create a dummy anchor element
let downloadAnchor = window.document.createElement('a');
downloadAnchor.href = window.URL.createObjectURL(new Blob([payload], { type: 'text/csv' }));
downloadAnchor.download = 'data.csv';
// Append anchor to body
document.body.appendChild(downloadAnchor);
// Trigger download
downloadAnchor.click();
// Remove anchor from body
document.body.removeChild(downloadAnchor);
}
function toggleInfo() {
let infoBox = document.getElementById('info');
if (!infoBox.style.display || infoBox.style.display === 'none') {
infoBox.style.display = 'block';
infoBox.parentNode.style.display = 'block';
window.setTimeout(function(){
infoBox.style.opacity = 1;
infoBox.style.transform = 'translate(-50%, -50%) scale(1)';
infoBox.parentNode.style.opacity = 1;
}, 25); // Fire the animation with a slight delay because otherwise the browser might skip it
} else {
infoBox.style.opacity = 0;
infoBox.style.transform = 'translate(-50%, -50%) scale(1.1)';
infoBox.parentNode.style.opacity = 0;
window.setTimeout(function(){
infoBox.style.display = 'none';
infoBox.parentNode.style.display = 'none';
}, 200);
}
}
function initGUI() {
// Inspired from https://stackoverflow.com/questions/27362914/how-to-add-tooltips-to-dat-gui
// dat.GUI copies the prototype of superclass Controller to all other controllers so it
// is not enough to add it only to the super class as the reference is not maintained.
for (let controllerName in dat.controllers) {
if (dat.controllers.hasOwnProperty(controllerName)) {
if (!dat.controllers[controllerName].prototype.hasOwnProperty('title')) {
dat.controllers[controllerName].prototype.title = function(v) {
// __li is the root dom element of each controller
if (v) {
this.__li.setAttribute('title', v);
} else {
this.__li.removeAttribute('title')