-
Notifications
You must be signed in to change notification settings - Fork 191
/
glshape.js
1590 lines (1408 loc) · 47.5 KB
/
glshape.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
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
/* eslint-disable eqeqeq */
import {Triangle, Cylinder, Sphere} from './WebGL/shapes';
import {
DoubleSide,
LineBasicMaterial,
MeshLambertMaterial,
MeshDoubleLambertMaterial,
VertexColors,
} from './WebGL/materials';
import {clamp, Vector3} from './WebGL/math';
import {CC} from './colors';
import {Geometry, Object3D} from './WebGL/core';
import makeFunction from './util/makeFunction';
import extend from './util/extend';
import {Line, LinePieces, Mesh} from './WebGL/objects';
import GLDraw from './GLDraw';
import Color from './WebGL/core/Color';
import vector3LikeToVector3 from './util/vector3LikeToVector3';
import MarchingCube from './MarchingCube';
import subdivideSpline from './util/subdivideSpline';
import adjustVolumeStyle from './util/adjustVolumeStyle';
// Marching cube, to match with protein surface generation
const ISDONE = 2;
function finalizeGeo(geo) {
// to avoid creating a bunch of geometries, we leave geoGroup untruncated
// until render is called, at which point we truncate;
// successive called up updateGeo will return a new geometry
const geoGroup = geo.updateGeoGroup(0);
if (geoGroup.vertices > 0) {
geoGroup.truncateArrayBuffers(true, true);
}
}
/**
* @param {Geometry} geo
* @param {Color | Partial<{r:number,g:number, b:number}> | string | number} colorIn
*/
function updateColor(geo, colorIn) {
const color = colorIn || CC.color(colorIn);
geo.colorsNeedUpdate = true;
let r;
let g;
let b;
if (color.constructor !== Array) {
r = color.r;
g = color.g;
b = color.b;
}
for (const gg in geo.geometryGroups) {
const geoGroup = geo.geometryGroups[gg];
const colorArr = geoGroup.colorArray;
for (let i = 0, il = geoGroup.vertices; i < il; ++i) {
if (color.constructor === Array) {
const c = color[i];
r = c.r;
g = c.g;
b = c.b;
}
colorArr[i * 3] = r;
colorArr[i * 3 + 1] = g;
colorArr[i * 3 + 2] = b;
}
}
}
/**
* @param {GLShape} shape
* @param {import("./WebGL/core").Geometry} geo
* @param {import('./specs').ArrowSpec} spec
*/
function drawArrow(shape, geo, spec) {
const from = vector3LikeToVector3(spec.start);
const end = vector3LikeToVector3(spec.end);
const {radius} = spec;
const {radiusRatio} = spec;
let {mid} = spec;
const midoffset = spec.midpos;
if (!(from && end)) return;
const geoGroup = geo.updateGeoGroup(51);
// vertices
const dir = end.clone().sub(from);
if (midoffset) {
// absolute offset, convert to relative
const length = dir.length();
if (midoffset > 0) mid = midoffset / length;
else mid = (length + midoffset) / length;
}
dir.multiplyScalar(mid);
const to = from.clone().add(dir);
const negDir = dir.clone().negate();
shape.intersectionShape.cylinder.push(new Cylinder(from.clone(), to.clone(), radius));
shape.intersectionShape.sphere.push(new Sphere(from.clone(), radius));
// get orthonormal vector
const nvecs = [];
nvecs[0] = dir.clone();
if (Math.abs(nvecs[0].x) > 0.0001) nvecs[0].y += 1;
else nvecs[0].x += 1;
nvecs[0].cross(dir);
nvecs[0].normalize();
nvecs[0] = nvecs[0];
// another orth vector
nvecs[4] = nvecs[0].clone();
nvecs[4].crossVectors(nvecs[0], dir);
nvecs[4].normalize();
nvecs[8] = nvecs[0].clone().negate();
nvecs[12] = nvecs[4].clone().negate();
// now quarter positions
nvecs[2] = nvecs[0].clone().add(nvecs[4]).normalize();
nvecs[6] = nvecs[4].clone().add(nvecs[8]).normalize();
nvecs[10] = nvecs[8].clone().add(nvecs[12]).normalize();
nvecs[14] = nvecs[12].clone().add(nvecs[0]).normalize();
// eights
nvecs[1] = nvecs[0].clone().add(nvecs[2]).normalize();
nvecs[3] = nvecs[2].clone().add(nvecs[4]).normalize();
nvecs[5] = nvecs[4].clone().add(nvecs[6]).normalize();
nvecs[7] = nvecs[6].clone().add(nvecs[8]).normalize();
nvecs[9] = nvecs[8].clone().add(nvecs[10]).normalize();
nvecs[11] = nvecs[10].clone().add(nvecs[12]).normalize();
nvecs[13] = nvecs[12].clone().add(nvecs[14]).normalize();
nvecs[15] = nvecs[14].clone().add(nvecs[0]).normalize();
const start = geoGroup.vertices;
const {vertexArray} = geoGroup;
const {faceArray} = geoGroup;
const {normalArray} = geoGroup;
const {lineArray} = geoGroup;
let offset;
let i;
let n;
// add vertices, opposing vertices paired together
for (i = 0, n = nvecs.length; i < n; ++i) {
offset = 3 * (start + 3 * i);
const bottom = nvecs[i].clone().multiplyScalar(radius).add(from);
const top = nvecs[i].clone().multiplyScalar(radius).add(to);
const conebase = nvecs[i]
.clone()
.multiplyScalar((radius || 1) * (radiusRatio || 1))
.add(to);
vertexArray[offset] = bottom.x;
vertexArray[offset + 1] = bottom.y;
vertexArray[offset + 2] = bottom.z;
vertexArray[offset + 3] = top.x;
vertexArray[offset + 4] = top.y;
vertexArray[offset + 5] = top.z;
vertexArray[offset + 6] = conebase.x;
vertexArray[offset + 7] = conebase.y;
vertexArray[offset + 8] = conebase.z;
if (i > 0) {
const prevX = vertexArray[offset - 3];
const prevY = vertexArray[offset - 2];
const prevZ = vertexArray[offset - 1];
const c = new Vector3(prevX, prevY, prevZ);
const b = end.clone();
const b2 = to.clone();
const a = new Vector3(conebase.x, conebase.y, conebase.z);
shape.intersectionShape.triangle.push(new Triangle(a, b, c));
shape.intersectionShape.triangle.push(new Triangle(c.clone(), b2, a.clone()));
}
}
geoGroup.vertices += 48;
offset = geoGroup.vertices * 3;
// caps
vertexArray[offset] = from.x;
vertexArray[offset + 1] = from.y;
vertexArray[offset + 2] = from.z;
vertexArray[offset + 3] = to.x;
vertexArray[offset + 4] = to.y;
vertexArray[offset + 5] = to.z;
vertexArray[offset + 6] = end.x;
vertexArray[offset + 7] = end.y;
vertexArray[offset + 8] = end.z;
geoGroup.vertices += 3;
// now faces
let faceoffset;
let lineoffset;
let t1;
let t2;
let t2b;
let t3;
let t3b;
let t4;
let t1offset;
let t2offset;
let t2boffset;
let t3offset;
let t3boffset;
let t4offset;
let n1;
let n2;
let n3;
let n4;
const fromi = geoGroup.vertices - 3;
const toi = geoGroup.vertices - 2;
const endi = geoGroup.vertices - 1;
const fromoffset = fromi * 3;
const tooffset = toi * 3;
const endoffset = endi * 3;
for (i = 0, n = nvecs.length - 1; i < n; ++i) {
const ti = start + 3 * i;
offset = ti * 3;
faceoffset = geoGroup.faceidx;
lineoffset = geoGroup.lineidx;
t1 = ti;
t1offset = t1 * 3;
t2 = ti + 1;
t2offset = t2 * 3;
t2b = ti + 2;
t2boffset = t2b * 3;
t3 = ti + 4;
t3offset = t3 * 3;
t3b = ti + 5;
t3boffset = t3b * 3;
t4 = ti + 3;
t4offset = t4 * 3;
// face = [t1, t2, t4], [t2, t3, t4];
// face = [t1, t2, t3, t4];
// const norm = [nvecs[i], nvecs[i], nvecs[i + 1], nvecs[i + 1]];
n1 = n2 = nvecs[i];
n3 = n4 = nvecs[i + 1];
normalArray[t1offset] = n1.x;
normalArray[t2offset] = n2.x;
normalArray[t4offset] = n4.x;
normalArray[t1offset + 1] = n1.y;
normalArray[t2offset + 1] = n2.y;
normalArray[t4offset + 1] = n4.y;
normalArray[t1offset + 2] = n1.z;
normalArray[t2offset + 2] = n2.z;
normalArray[t4offset + 2] = n4.z;
normalArray[t2offset] = n2.x;
normalArray[t3offset] = n3.x;
normalArray[t4offset] = n4.x;
normalArray[t2offset + 1] = n2.y;
normalArray[t3offset + 1] = n3.y;
normalArray[t4offset + 1] = n4.y;
normalArray[t2offset + 2] = n2.z;
normalArray[t3offset + 2] = n3.z;
normalArray[t4offset + 2] = n4.z;
normalArray[t2boffset] = n2.x;
normalArray[t3boffset] = n3.x;
normalArray[t2boffset + 1] = n2.y;
normalArray[t3boffset + 1] = n3.y;
normalArray[t2boffset + 2] = n2.z;
normalArray[t3boffset + 2] = n3.z;
// sides
faceArray[faceoffset] = t1;
faceArray[faceoffset + 1] = t2;
faceArray[faceoffset + 2] = t4;
faceArray[faceoffset + 3] = t2;
faceArray[faceoffset + 4] = t3;
faceArray[faceoffset + 5] = t4;
// caps
faceArray[faceoffset + 6] = t1;
faceArray[faceoffset + 7] = t4;
faceArray[faceoffset + 8] = fromi;
faceArray[faceoffset + 9] = t2b;
faceArray[faceoffset + 10] = toi;
faceArray[faceoffset + 11] = t3b;
// arrowhead
faceArray[faceoffset + 12] = t2b;
faceArray[faceoffset + 13] = endi;
faceArray[faceoffset + 14] = t3b;
// sides
lineArray[lineoffset] = t1;
lineArray[lineoffset + 1] = t2;
lineArray[lineoffset + 2] = t1;
lineArray[lineoffset + 3] = t4;
// lineArray[lineoffset+4] = t2, lineArray[lineoffset+5] = t3;
lineArray[lineoffset + 4] = t3;
lineArray[lineoffset + 5] = t4;
// caps
lineArray[lineoffset + 6] = t1;
lineArray[lineoffset + 7] = t4;
// lineArray[lineoffset+10] = t1, lineArray[lineoffset+11] = fromi;
// lineArray[lineoffset+12] = t4, lineArray[lineoffset+13] = fromi;
lineArray[lineoffset + 8] = t2b;
lineArray[lineoffset + 9] = t2; // toi
lineArray[lineoffset + 10] = t2b;
lineArray[lineoffset + 11] = t3b;
lineArray[lineoffset + 12] = t3;
lineArray[lineoffset + 13] = t3b; // toi
// arrowhead
lineArray[lineoffset + 14] = t2b;
lineArray[lineoffset + 15] = endi;
lineArray[lineoffset + 16] = t2b;
lineArray[lineoffset + 17] = t3b;
lineArray[lineoffset + 18] = endi;
lineArray[lineoffset + 19] = t3b;
geoGroup.faceidx += 15;
geoGroup.lineidx += 20;
}
// final face
const face = [start + 45, start + 46, start + 1, start, start + 47, start + 2];
// const norm = [nvecs[15], nvecs[15], nvecs[0], nvecs[0]];
faceoffset = geoGroup.faceidx;
lineoffset = geoGroup.lineidx;
t1 = face[0];
t1offset = t1 * 3;
t2 = face[1];
t2offset = t2 * 3;
t2b = face[4];
t2boffset = t2b * 3;
t3 = face[2];
t3offset = t3 * 3;
t3b = face[5];
t3boffset = t3b * 3;
t4 = face[3];
t4offset = t4 * 3;
n1 = n2 = nvecs[15];
n3 = n4 = nvecs[0];
normalArray[t1offset] = n1.x;
normalArray[t2offset] = n2.x;
normalArray[t4offset] = n4.x;
normalArray[t1offset + 1] = n1.y;
normalArray[t2offset + 1] = n2.y;
normalArray[t4offset + 1] = n4.y;
normalArray[t1offset + 2] = n1.z;
normalArray[t2offset + 2] = n2.z;
normalArray[t4offset + 2] = n4.z;
normalArray[t2offset] = n2.x;
normalArray[t3offset] = n3.x;
normalArray[t4offset] = n4.x;
normalArray[t2offset + 1] = n2.y;
normalArray[t3offset + 1] = n3.y;
normalArray[t4offset + 1] = n4.y;
normalArray[t2offset + 2] = n2.z;
normalArray[t3offset + 2] = n3.z;
normalArray[t4offset + 2] = n4.z;
normalArray[t2boffset] = n2.x;
normalArray[t3boffset] = n3.x;
normalArray[t2boffset + 1] = n2.y;
normalArray[t3boffset + 1] = n3.y;
normalArray[t2boffset + 2] = n2.z;
normalArray[t3boffset + 2] = n3.z;
// Cap normals
dir.normalize();
negDir.normalize();
normalArray[fromoffset] = negDir.x;
normalArray[tooffset] = normalArray[endoffset] = dir.x;
normalArray[fromoffset + 1] = negDir.y;
normalArray[tooffset + 1] = normalArray[endoffset + 1] = dir.y;
normalArray[fromoffset + 2] = negDir.z;
normalArray[tooffset + 2] = normalArray[endoffset + 2] = dir.z;
// Final side
faceArray[faceoffset] = t1;
faceArray[faceoffset + 1] = t2;
faceArray[faceoffset + 2] = t4;
faceArray[faceoffset + 3] = t2;
faceArray[faceoffset + 4] = t3;
faceArray[faceoffset + 5] = t4;
// final caps
faceArray[faceoffset + 6] = t1;
faceArray[faceoffset + 7] = t4;
faceArray[faceoffset + 8] = fromi;
faceArray[faceoffset + 9] = t2b;
faceArray[faceoffset + 10] = toi;
faceArray[faceoffset + 11] = t3b;
// final arrowhead
faceArray[faceoffset + 12] = t2b;
faceArray[faceoffset + 13] = endi;
faceArray[faceoffset + 14] = t3b;
// sides
lineArray[lineoffset] = t1;
lineArray[lineoffset + 1] = t2;
lineArray[lineoffset + 2] = t1;
lineArray[lineoffset + 3] = t4;
// lineArray[lineoffset+4] = t2, lineArray[lineoffset+5] = t3;
lineArray[lineoffset + 4] = t3;
lineArray[lineoffset + 5] = t4;
// caps
lineArray[lineoffset + 6] = t1;
lineArray[lineoffset + 7] = t4;
// lineArray[lineoffset+10] = t1, lineArray[lineoffset+11] = fromi;
// lineArray[lineoffset+12] = t4, lineArray[lineoffset+13] = fromi;
lineArray[lineoffset + 8] = t2b;
lineArray[lineoffset + 9] = t2; // toi
lineArray[lineoffset + 10] = t2b;
lineArray[lineoffset + 11] = t3b;
lineArray[lineoffset + 12] = t3;
lineArray[lineoffset + 13] = t3b; // toi
// arrowhead
lineArray[lineoffset + 14] = t2b;
lineArray[lineoffset + 15] = endi;
lineArray[lineoffset + 16] = t2b;
lineArray[lineoffset + 17] = t3b;
lineArray[lineoffset + 18] = endi;
lineArray[lineoffset + 19] = t3b;
geoGroup.faceidx += 15;
geoGroup.lineidx += 20;
}
// Update a bounding sphere's position and radius
// from list of centroids and new points
/**
* @param {import('./specs').SphereStyleSpec} sphere
* @param {Object} _components - centroid of all objects in shape
* @param {Array} points - flat array of all points in shape
* @param {number} numPoints - number of valid poitns in points
*/
function updateBoundingFromPoints(sphere, _components, points, numPoints) {
sphere.center.set(0, 0, 0);
// previously I weighted each component's center equally, but I think
// it is better to use all points
let xmin = Infinity;
let ymin = Infinity;
let zmin = Infinity;
let xmax = -Infinity;
let ymax = -Infinity;
let zmax = -Infinity;
if (sphere.box) {
xmin = sphere.box.min.x;
xmax = sphere.box.max.x;
ymin = sphere.box.min.y;
ymax = sphere.box.max.y;
zmin = sphere.box.min.z;
zmax = sphere.box.max.z;
}
for (let i = 0, il = numPoints; i < il; i++) {
const x = points[i * 3];
const y = points[i * 3 + 1];
const z = points[i * 3 + 2];
if (x < xmin) xmin = x;
if (y < ymin) ymin = y;
if (z < zmin) zmin = z;
if (x > xmax) xmax = x;
if (y > ymax) ymax = y;
if (z > zmax) zmax = z;
}
sphere.center.set((xmax + xmin) / 2, (ymax + ymin) / 2, (zmax + zmin) / 2);
sphere.radius = sphere.center.distanceTo({x: xmax, y: ymax, z: zmax});
sphere.box = {min: {x: xmin, y: ymin, z: zmin}, max: {x: xmax, y: ymax, z: zmax}};
}
// helper function for adding an appropriately sized mesh
function addCustomGeo(shape, geo, mesh, color, clickable) {
const geoGroup = geo.addGeoGroup();
const {vertexArr} = mesh;
const {normalArr} = mesh;
const {faceArr} = mesh;
geoGroup.vertices = vertexArr.length;
geoGroup.faceidx = faceArr.length;
let offset;
let v;
let a;
let b;
let c;
let i;
let il;
let r;
let g;
const {vertexArray} = geoGroup;
const {colorArray} = geoGroup;
if (color.constructor !== Array) {
r = color.r;
g = color.g;
b = color.b;
}
for (i = 0, il = geoGroup.vertices; i < il; ++i) {
offset = i * 3;
v = vertexArr[i];
vertexArray[offset] = v.x;
vertexArray[offset + 1] = v.y;
vertexArray[offset + 2] = v.z;
if (color.constructor === Array) {
c = color[i];
r = c.r;
g = c.g;
b = c.b;
}
colorArray[offset] = r;
colorArray[offset + 1] = g;
colorArray[offset + 2] = b;
}
if (clickable) {
for (i = 0, il = geoGroup.faceidx / 3; i < il; ++i) {
offset = i * 3;
a = faceArr[offset];
b = faceArr[offset + 1];
c = faceArr[offset + 2];
const vA = new Vector3();
const vB = new Vector3();
const vC = new Vector3();
shape.intersectionShape.triangle.push(
new Triangle(vA.copy(vertexArr[a]), vB.copy(vertexArr[b]), vC.copy(vertexArr[c]))
);
}
}
if (clickable) {
const center = new Vector3(0, 0, 0);
let cnt = 0;
for (let gr = 0; gr < geo.geometryGroups.length; gr++) {
center.add(geo.geometryGroups[gr].getCentroid());
cnt++;
}
center.divideScalar(cnt);
updateBoundingFromPoints(
shape.boundingSphere,
{centroid: center},
vertexArray,
geoGroup.vertices
);
}
geoGroup.faceArray = new Uint16Array(faceArr);
geoGroup.truncateArrayBuffers(true, true);
if (normalArr.length < geoGroup.vertices) geoGroup.setNormals();
else {
const normalArray = new Float32Array(geoGroup.vertices * 3);
geoGroup.normalArray = normalArray;
let n;
for (i = 0, il = geoGroup.vertices; i < il; ++i) {
offset = i * 3;
n = normalArr[i];
normalArray[offset] = n.x;
normalArray[offset + 1] = n.y;
normalArray[offset + 2] = n.z;
}
}
geoGroup.setLineIndices();
geoGroup.lineidx = geoGroup.lineArray.length;
}
/**
*
* @param {GLShape} shape
* @param {import('./specs').ShapeSpec} stylespec
*/
function updateFromStyle(shape, stylespec) {
if (typeof stylespec.color != 'undefined') {
shape.color = stylespec.color || new Color();
if (!(stylespec.color instanceof Color)) shape.color = CC.color(stylespec.color);
} else {
shape.color = CC.color(0);
}
shape.wireframe = !!stylespec.wireframe;
// opacity is the preferred nomenclature, support alpha for backwards compat
shape.opacity = stylespec.alpha ? clamp(stylespec.alpha, 0.0, 1.0) : 1.0;
if (typeof stylespec.opacity != 'undefined') {
shape.opacity = clamp(stylespec.opacity, 0.0, 1.0);
}
shape.side = stylespec.side !== undefined ? stylespec.side : DoubleSide;
shape.linewidth = typeof stylespec.linewidth == 'undefined' ? 1 : stylespec.linewidth;
// Click handling
shape.clickable = !!stylespec.clickable;
const nullFn = () => {};
shape.callback = makeFunction(stylespec.callback || nullFn);
shape.hoverable = !!stylespec.hoverable;
shape.hover_callback = makeFunction(stylespec.hover_callback || nullFn);
shape.unhover_callback = makeFunction(stylespec.unhover_callback || nullFn);
shape.hidden = stylespec.hidden;
shape.frame = stylespec.frame;
}
function distanceFrom(c1, c2) {
return Math.sqrt((c1.x - c2.x) ** 2 + (c1.y - c2.y) ** 2 + (c1.z - c2.z) ** 2);
}
function inSelectedRegion(coordinate, selectedRegion, radius) {
for (let i = 0; i < selectedRegion.length; i++) {
if (distanceFrom(selectedRegion[i], coordinate) <= radius) return true;
}
return false;
}
export function splitMesh(mesh) {
const MAXVERT = 64000; // webgl only supports 2^16 elements, leave a little breathing room (require at least 2)
// peel off 64k vertices rsvh into their own mesh
// duplicating vertices and normals as necessary to preserve faces and lines
if (mesh.vertexArr.length < MAXVERT) return [mesh]; // typical case
/** @type {Record<string, any[]>[] & {colorArr?: any[]}} */
const slices = [{vertexArr: [], normalArr: [], faceArr: []}];
if (mesh.colorArr) slices.colorArr = [];
const vertSlice = []; // indexed by original vertex to get current slice
const vertIndex = []; // indexed by original vertex to get index within slice
let currentSlice = 0;
// for each face, make sure all three vertices (or copies) are in the same slice
const faces = mesh.faceArr;
for (let i = 0, nf = faces.length; i < nf; i += 3) {
const slice = slices[currentSlice];
for (let j = 0; j < 3; j++) {
// process each vertex to make sure it is assigned a slice
// all vertices of a face must belong to the same slice
const v = faces[i + j];
if (vertSlice[v] !== currentSlice) {
// true if undefined
vertSlice[v] = currentSlice;
vertIndex[v] = slice.vertexArr.length;
slice.vertexArr.push(mesh.vertexArr[v]);
if (mesh.normalArr && mesh.normalArr[v]) slice.normalArr.push(mesh.normalArr[v]);
if (mesh.colorArr && mesh.colorArr[v]) slice.colorArr.push(mesh.colorArr[v]);
}
slice.faceArr.push(vertIndex[v]);
}
if (slice.vertexArr.length >= MAXVERT) {
// new slice
slices.push({vertexArr: [], normalArr: [], faceArr: []});
if (mesh.colorArr) slices.colorArr = [];
currentSlice++;
}
}
return slices;
}
// handles custom shape generation from user supplied arrays
// May need to generate normal and/or line indices
/**
* @param {GLShape} shape
* @param {import("./WebGL/core").Geometry} geo
* @param {import('./specs').CustomShapeSpec} customSpec
*/
function drawCustom(shape, geo, customSpec) {
const mesh = customSpec;
const {vertexArr} = mesh;
const {faceArr} = mesh;
if (vertexArr.length === 0 || faceArr.length === 0) {
console.warn('Error adding custom shape component: No vertices and/or face indices supplied!');
}
let {color} = customSpec;
if (typeof color == 'undefined') {
color = shape.color;
}
color = CC.color(color);
// var firstgeo = geo.geometryGroups.length;
const splits = splitMesh(mesh);
for (let i = 0, n = splits.length; i < n; i++) {
addCustomGeo(
shape,
geo,
splits[i],
splits[i].colorArr ? splits[i].colorArr : color,
customSpec.clickable
);
}
}
// eslint-disable-next-line import/no-mutable-exports
export let ShapeIDCount = 0;
/**
* Custom renderable shape
* @constructor GLShape
* @param {Object} stylespec
* @returns {GLShape}
*/
export default class GLShape {
boundingSphere = new Sphere();
/** @type {Record<string, any[]>} */
intersectionShape = {
sphere: [],
cylinder: [],
line: [],
triangle: [],
};
// Keep track of shape components and their centroids
components = [];
/** @type {Object3D | null} */
shapeObj = null;
/** @type {Object3D | null} */
renderedShapeObj = null;
geo = new Geometry(true);
linegeo = new Geometry(true);
// globj locals
color;
wireframe;
opacity;
side;
linewidth;
clickable;
callback;
hoverable;
hover_callback;
unhover_callback;
hidden;
frame;
shapePosition;
constructor(stylespec) {
this.stylespec = stylespec || {};
ShapeIDCount++;
updateFromStyle(this, this.stylespec);
}
/** Update shape with new style specification
* @function GLShape#updateStyle
* @param {import('./specs').ShapeSpec} newspec
@example
let sphere = viewer.addSphere({center:{x:0,y:0,z:0},radius:10.0,color:'red'});
sphere.updateStyle({color:'yellow',opacity:0.5});
viewer.render();
*/
updateStyle(newspec) {
for (const prop in newspec) {
this.stylespec[prop] = newspec[prop];
}
updateFromStyle(this, this.stylespec);
if (newspec.voldata && newspec.volscheme) {
adjustVolumeStyle(newspec);
// convert volumetric data into colors
const scheme = newspec.volscheme;
const {voldata} = newspec;
const range = scheme.range() || [-1, 1];
this.geo.setColors((x, y, z) => {
const val = voldata.getVal(x, y, z);
const col = CC.color(scheme.valueToHex(val, range));
return col;
});
delete this.color;
}
}
/**
* Creates a custom shape from supplied vertex and face arrays
* @function GLShape#addCustom
* @param {import('./specs').CustomShapeSpec} customSpec
*/
addCustom(customSpec) {
customSpec.vertexArr = customSpec.vertexArr || [];
customSpec.faceArr = customSpec.faceArr || [];
customSpec.normalArr = customSpec.normalArr || [];
// will split mesh as needed
drawCustom(this, this.geo, customSpec);
}
/**
* Creates a sphere shape
* @function GLShape#addSphere
* @param {import('./specs').SphereStyleSpec} sphereSpec
@example
viewer.addSphere({center:{x:0,y:0,z:0},radius:10.0,color:'red'});
viewer.render();
*/
addSphere(sphereSpec) {
sphereSpec.center = sphereSpec.center || {
x: 0,
y: 0,
z: 0,
};
sphereSpec.radius = sphereSpec.radius ? clamp(sphereSpec.radius, 0, Infinity) : 1.5;
sphereSpec.color = CC.color(sphereSpec.color);
this.intersectionShape.sphere.push(new Sphere(sphereSpec.center, sphereSpec.radius));
GLDraw.drawSphere(this.geo, sphereSpec.center, sphereSpec.radius, sphereSpec.color);
this.components.push({
centroid: new Vector3(sphereSpec.center.x, sphereSpec.center.y, sphereSpec.center.z),
});
const geoGroup = this.geo.updateGeoGroup(0);
updateBoundingFromPoints(
this.boundingSphere,
this.components,
geoGroup.vertexArray,
geoGroup.vertices
);
}
/**
* Creates a box
* @function GLShape#addBox
* @param {import('./specs').BoxSpec} boxSpec
@example
var shape = viewer.addShape({color:'red'});
shape.addBox({corner: {x:1,y:2,z:0}, dimensions: {w: 4, h: 2, d: 6}});
shape.addBox({corner: {x:-5,y:-3,z:0},
dimensions: { w: {x:1,y:1,z:0},
h: {x:-1,y:1,z:0},
d: {x:0,y:0,z:1} }});
viewer.zoomTo();
viewer.rotate(30);
viewer.render();
*/
addBox(boxSpec) {
const dim = boxSpec.dimensions || {w: 1, h: 1, d: 1};
// dimensions may be scalar or vector quantities
let {w} = dim;
if (typeof dim.w == 'number') {
w = {x: dim.w, y: 0, z: 0};
}
let {h} = dim;
if (typeof dim.h == 'number') {
h = {x: 0, y: dim.h, z: 0};
}
let {d} = dim;
if (typeof dim.d == 'number') {
d = {x: 0, y: 0, z: dim.d};
}
// can position using corner OR center
let c = boxSpec.corner;
if (c == undefined) {
if (
boxSpec.center !== undefined &&
typeof w !== 'number' &&
typeof h !== 'number' &&
typeof d !== 'number'
) {
c = {
x: boxSpec.center.x - 0.5 * (w.x + h.x + d.x),
y: boxSpec.center.y - 0.5 * (w.y + h.y + d.y),
z: boxSpec.center.z - 0.5 * (w.z + h.z + d.z),
};
} else {
// default to origin
c = {x: 0, y: 0, z: 0};
}
}
// 8 vertices
const uv =
typeof w !== 'number' && typeof h !== 'number' && typeof d !== 'number'
? [
{x: c.x, y: c.y, z: c.z},
{x: c.x + w.x, y: c.y + w.y, z: c.z + w.z},
{x: c.x + h.x, y: c.y + h.y, z: c.z + h.z},
{x: c.x + w.x + h.x, y: c.y + w.y + h.y, z: c.z + w.z + h.z},
{x: c.x + d.x, y: c.y + d.y, z: c.z + d.z},
{x: c.x + w.x + d.x, y: c.y + w.y + d.y, z: c.z + w.z + d.z},
{x: c.x + h.x + d.x, y: c.y + h.y + d.y, z: c.z + h.z + d.z},
{x: c.x + w.x + h.x + d.x, y: c.y + w.y + h.y + d.y, z: c.z + w.z + h.z + d.z},
]
: [];
// but.. so that we can have sharp issues, we want a unique normal
// for each face - since normals are associated with vertices, need to duplicate
// bottom
// 0 1
// 2 3
// top
// 4 5
// 6 7
const verts = [];
const faces = [];
// bottom
verts.splice(verts.length, 0, uv[0], uv[1], uv[2], uv[3]);
faces.splice(faces.length, 0, 0, 2, 1, 1, 2, 3);
let foff = 4;
// front
verts.splice(verts.length, 0, uv[2], uv[3], uv[6], uv[7]);
faces.splice(faces.length, 0, foff + 0, foff + 2, foff + 1, foff + 1, foff + 2, foff + 3);
foff += 4;
// back
verts.splice(verts.length, 0, uv[4], uv[5], uv[0], uv[1]);
faces.splice(faces.length, 0, foff + 0, foff + 2, foff + 1, foff + 1, foff + 2, foff + 3);
foff += 4;
// top
verts.splice(verts.length, 0, uv[6], uv[7], uv[4], uv[5]);
faces.splice(faces.length, 0, foff + 0, foff + 2, foff + 1, foff + 1, foff + 2, foff + 3);
foff += 4;
// right
verts.splice(verts.length, 0, uv[3], uv[1], uv[7], uv[5]);
faces.splice(faces.length, 0, foff + 0, foff + 2, foff + 1, foff + 1, foff + 2, foff + 3);
foff += 4;
// left
verts.splice(verts.length, 0, uv[2], uv[6], uv[0], uv[4]); // fix: was 2 0 6 4 , was flipped! will this ruin anything?
// and is this the reason for having double sided lambert shading? the box had a flipped face
faces.splice(faces.length, 0, foff + 0, foff + 2, foff + 1, foff + 1, foff + 2, foff + 3);
foff += 4;
const spec = extend({}, boxSpec);
spec.vertexArr = verts;
spec.faceArr = faces;
spec.normalArr = [];
drawCustom(this, this.geo, spec);
const centroid = new Vector3();
this.components.push({
centroid: centroid.addVectors(uv[0], uv[7]).multiplyScalar(0.5),
});
const geoGroup = this.geo.updateGeoGroup(0);
updateBoundingFromPoints(
this.boundingSphere,
this.components,
geoGroup.vertexArray,
geoGroup.vertices
);
}
/**
* Creates a cylinder shape
* @function GLShape#addCylinder
* @param {import('./specs').CylinderSpec} cylinderSpec
@example
viewer.addCylinder({start:{x:0.0,y:0.0,z:0.0},
end:{x:10.0,y:0.0,z:0.0},
radius:1.0,
fromCap:1,
toCap:2,
color:'red',
hoverable:true,
clickable:true,
callback:function(){ this.color.setHex(0x00FFFF00);viewer.render( );},
hover_callback: function(){ viewer.render( );},
unhover_callback: function(){ this.color.setHex(0xFF000000);viewer.render( );}
});
viewer.addCylinder({start:{x:0.0,y:2.0,z:0.0},
end:{x:0.0,y:10.0,z:0.0},
radius:0.5,
fromCap:false,
toCap:true,
color:'teal'});
viewer.addCylinder({start:{x:15.0,y:0.0,z:0.0},
end:{x:20.0,y:0.0,z:0.0},
radius:1.0,
color:'black',
fromCap:false,
toCap:false});
viewer.render();
*/
addCylinder(cylinderSpec) {
cylinderSpec.start = cylinderSpec.start || {x: 0, y: 0, z: 0};
cylinderSpec.end = cylinderSpec.end || {x: 0, y: 0, z: 0};
const start = new Vector3(
cylinderSpec.start.x || 0,
cylinderSpec.start.y || 0,
cylinderSpec.start.z || 0
);