/
glshape.js
1546 lines (1293 loc) · 55.6 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
/**
* A GLShape is a collection of user specified shapes.
*
* @constructor $3Dmol.GLShape
* @extends {ShapeSpec}
* @param {number} sid - Unique identifier
* @param {ShapeSpec} stylespec - shape style specification
*/
$3Dmol.GLShape = (function() {
// Marching cube, to match with protein surface generation
var ISDONE = 2;
var finalizeGeo = function(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
var geoGroup = geo.updateGeoGroup(0);
if (geoGroup.vertices > 0) {
geoGroup.truncateArrayBuffers(true, true);
}
};
/**
*
* @param {$3Dmol.Geometry}
* geo
* @param {$3Dmol.Color |
* colorlike} color
*/
var updateColor = function(geo, color) {
color = color || $3Dmol.CC.color(color);
geo.colorsNeedUpdate = true;
var r, g, b;
if (color.constructor !== Array) {
r = color.r;
g = color.g;
b = color.b;
}
for (var gg in geo.geometryGroups) {
var geoGroup = geo.geometryGroups[gg];
var colorArr = geoGroup.colorArray;
for (var i = 0, il = geoGroup.vertices; i < il; ++i) {
if (color.constructor === Array) {
var 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 {$3Dmol.GLShape}
* shape
* @param {geometryGroup}
* geoGroup
* @param {ArrowSpec}
* spec
*/
var drawArrow = function(shape, geo, spec) {
var from = spec.start, end = spec.end, radius = spec.radius,
radiusRatio = spec.radiusRatio, mid = spec.mid, midoffset = spec.midpos;
if (!(from && end))
return;
var geoGroup = geo.updateGeoGroup(51);
// vertices
var dir = end.clone().sub(from);
if (midoffset) { //absolute offset, convert to relative
let length = dir.length();
if (midoffset > 0) mid = midoffset / length;
else mid = (length + midoffset) / length;
}
dir.multiplyScalar(mid);
var to = from.clone().add(dir);
var negDir = dir.clone().negate();
shape.intersectionShape.cylinder.push(new $3Dmol.Cylinder(from.clone(),
to.clone(), radius));
shape.intersectionShape.sphere.push(new $3Dmol.Sphere(from.clone(),
radius));
// get orthonormal vector
var 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();
var start = geoGroup.vertices;
var vertexArray = geoGroup.vertexArray;
var faceArray = geoGroup.faceArray;
var normalArray = geoGroup.normalArray;
var lineArray = geoGroup.lineArray;
var offset, i, n;
// add vertices, opposing vertices paired together
for (i = 0, n = nvecs.length; i < n; ++i) {
offset = 3 * (start + 3 * i);
var bottom = nvecs[i].clone().multiplyScalar(radius).add(from);
var top = nvecs[i].clone().multiplyScalar(radius).add(to);
var conebase = nvecs[i].clone()
.multiplyScalar(radius * radiusRatio).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) {
var prev_x = vertexArray[offset - 3];
var prev_y = vertexArray[offset - 2];
var prev_z = vertexArray[offset - 1];
var c = new $3Dmol.Vector3(prev_x, prev_y, prev_z);
var b = end.clone(), b2 = to.clone();
var a = new $3Dmol.Vector3(conebase.x, conebase.y, conebase.z);
shape.intersectionShape.triangle.push(new $3Dmol.Triangle(a, b,
c));
shape.intersectionShape.triangle.push(new $3Dmol.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
var face, norm, faceoffset, lineoffset;
var t1, t2, t2b, t3, t3b, t4, t1offset, t2offset, t2boffset, t3offset, t3boffset, t4offset;
var n1, n2, n3, n4;
var fromi = geoGroup.vertices - 3, toi = geoGroup.vertices - 2, endi = geoGroup.vertices - 1;
var fromoffset = fromi * 3, tooffset = toi * 3, endoffset = endi * 3;
for (i = 0, n = nvecs.length - 1; i < n; ++i) {
var 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];
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
face = [start + 45, start + 46, start + 1, start, start + 47,
start + 2];
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 {$3Dmol.Sphere}
* sphere
* @param {Object}
* components, centroid of all objects in shape
* @param {Array}
* points, flat array of all points in shape
* @param {int} numPoints, number of valid poitns in points
*/
var updateBoundingFromPoints = function(sphere, components, points, numPoints) {
sphere.center.set(0, 0, 0);
var i, il;
if (components.length > 0) {
for (i = 0, il = components.length; i < il; ++i) {
var centroid = components[i].centroid;
sphere.center.add(centroid);
}
sphere.center.divideScalar(components.length);
}
var maxRadiusSq = sphere.radius * sphere.radius;
if (points.length / 3 < numPoints)
numPoints = points.length / 3;
for (i = 0, il = numPoints; i < il; i++) {
var x = points[i * 3], y = points[i * 3 + 1], z = points[i * 3 + 2];
var radiusSq = sphere.center.distanceToSquared({
x: x,
y: y,
z: z
});
maxRadiusSq = Math.max(maxRadiusSq, radiusSq);
}
sphere.radius = Math.sqrt(maxRadiusSq);
};
//helper function for adding an appropriately sized mesh
var addCustomGeo = function(shape, geo, mesh, color, clickable) {
var geoGroup = geo.addGeoGroup();
var vertexArr = mesh.vertexArr, normalArr = mesh.normalArr,
faceArr = mesh.faceArr;
geoGroup.vertices = vertexArr.length;
geoGroup.faceidx = faceArr.length;
var offset, v, a, b, c, i, il, r, g;
var vertexArray = geoGroup.vertexArray;
var colorArray = geoGroup.colorArray;
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];
var vA = new $3Dmol.Vector3(), vB = new $3Dmol.Vector3(), vC = new $3Dmol.Vector3();
shape.intersectionShape.triangle.push(new $3Dmol.Triangle(vA
.copy(vertexArr[a]), vB.copy(vertexArr[b]), vC
.copy(vertexArr[c])));
}
}
if (clickable) {
var center = new $3Dmol.Vector3(0, 0, 0);
var cnt = 0;
for (let g = 0; g < geo.geometryGroups.length; g++) {
center.add(geo.geometryGroups[g].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 {
var normalArray = geoGroup.normalArray = new Float32Array(geoGroup.vertices * 3);
var 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;
};
// handles custom shape generation from user supplied arrays
// May need to generate normal and/or line indices
/**
* @param {$3Dmol.GLShape}
* shape
* @param {geometry}
* geo
* @param {CustomSpec}
* customSpec
*/
var drawCustom = function(shape, geo, customSpec) {
var mesh = customSpec;
var vertexArr = mesh.vertexArr;
var faceArr = mesh.faceArr;
if (vertexArr.length === 0 || faceArr.length === 0) {
console
.warn("Error adding custom shape component: No vertices and/or face indices supplied!");
}
var color = customSpec.color;
if (typeof (color) == 'undefined') {
color = shape.color;
}
color = $3Dmol.CC.color(color);
//var firstgeo = geo.geometryGroups.length;
var splits = $3Dmol.splitMesh(mesh);
for (var i = 0, n = splits.length; i < n; i++) {
addCustomGeo(shape, geo, splits[i], splits[i].colorArr ? splits[i].colorArr : color, customSpec.clickable);
}
};
/**
*
* @param {$3Dmol.GLShape}
* shape
* @param {ShapeSpec}
* stylespec
* @returns {undefined}
*/
var updateFromStyle = function(shape, stylespec) {
if (typeof (stylespec.color) != 'undefined') {
shape.color = stylespec.color || new $3Dmol.Color();
if (!(stylespec.color instanceof $3Dmol.Color))
shape.color = $3Dmol.CC.color(stylespec.color);
} else {
shape.color = $3Dmol.CC.color(0);
}
shape.wireframe = stylespec.wireframe ? true : false;
//opacity is the preferred nomenclature, support alpha for backwards compat
shape.opacity = stylespec.alpha ? $3Dmol.Math.clamp(stylespec.alpha, 0.0,
1.0) : 1.0;
if (typeof (stylespec.opacity) != 'undefined') {
shape.opacity = $3Dmol.Math.clamp(stylespec.opacity, 0.0, 1.0);
}
shape.side = (stylespec.side !== undefined) ? stylespec.side
: $3Dmol.DoubleSide;
shape.linewidth = typeof (stylespec.linewidth) == 'undefined' ? 1 : stylespec.linewidth;
// Click handling
shape.clickable = stylespec.clickable ? true : false;
shape.callback = $3Dmol.makeFunction(stylespec.callback);
shape.hoverable = stylespec.hoverable ? true : false;
shape.hover_callback = $3Dmol.makeFunction(stylespec.hover_callback);
shape.unhover_callback = $3Dmol.makeFunction(stylespec.unhover_callback);
shape.hidden = stylespec.hidden;
shape.frame = stylespec.frame;
};
/**
* Custom renderable shape
*
* @constructor $3Dmol.GLShape
*
* @param {Object}
* stylespec
* @returns {$3Dmol.GLShape}
*/
function GLShape(stylespec) {
stylespec = stylespec || {};
$3Dmol.ShapeIDCount++;
this.boundingSphere = new $3Dmol.Sphere();
/** @type {IntersectionShapes} */
this.intersectionShape = {
sphere: [],
cylinder: [],
line: [],
triangle: []
};
updateFromStyle(this, stylespec);
// Keep track of shape components and their centroids
var components = [];
var shapeObj = null;
var renderedShapeObj = null;
var geo = new $3Dmol.Geometry(true);
var linegeo = new $3Dmol.Geometry(true);
/** Update shape with new style specification
* @function $3Dmol.GLShape#updateStyle
* @param {ShapeSpec} newspec
* @return {$3Dmol.GLShape}
*/
this.updateStyle = function(newspec) {
for (var prop in newspec) {
stylespec[prop] = newspec[prop];
}
updateFromStyle(this, stylespec);
if (newspec.voldata && newspec.volscheme) {
$3Dmol.adjustVolumeStyle(newspec);
//convert volumetric data into colors
const scheme = newspec.volscheme;
const voldata = newspec.voldata;
const cc = $3Dmol.CC;
const range = scheme.range() || [-1, 1];
geo.setColors(function(x, y, z) {
let val = voldata.getVal(x, y, z);
let col = cc.color(scheme.valueToHex(val, range));
return col;
});
delete this.color;
}
};
/**
* Creates a custom shape from supplied vertex and face arrays
* @function $3Dmol.GLShape#addCustom
* @param {CustomShapeSpec} customSpec
* @return {$3Dmol.GLShape}
*/
this.addCustom = function(customSpec) {
customSpec.vertexArr = customSpec.vertexArr || [];
customSpec.faceArr = customSpec.faceArr || [];
customSpec.normalArr = customSpec.normalArr || [];
// will split mesh as needed
drawCustom(this, geo, customSpec);
};
/**
* Creates a sphere shape
* @function $3Dmol.GLShape#addSphere
* @param {SphereSpec} sphereSpec
* @return {$3Dmol.GLShape}
@example
viewer.addSphere({center:{x:0,y:0,z:0},radius:10.0,color:'red'});
viewer.render();
*/
this.addSphere = function(sphereSpec) {
sphereSpec.center = sphereSpec.center || {
x: 0,
y: 0,
z: 0
};
sphereSpec.radius = sphereSpec.radius ? $3Dmol.Math.clamp(
sphereSpec.radius, 0, Infinity) : 1.5;
sphereSpec.color = $3Dmol.CC.color(sphereSpec.color);
this.intersectionShape.sphere.push(new $3Dmol.Sphere(
sphereSpec.center, sphereSpec.radius));
$3Dmol.GLDraw.drawSphere(geo, sphereSpec.center,
sphereSpec.radius, sphereSpec.color);
components.push({
centroid: new $3Dmol.Vector3(sphereSpec.center.x,
sphereSpec.center.y, sphereSpec.center.z)
});
var geoGroup = geo.updateGeoGroup(0);
updateBoundingFromPoints(this.boundingSphere, components,
geoGroup.vertexArray, geoGroup.vertices);
};
/**
* Creates a box
* @function $3Dmol.GLShape#addBox
* @param {BoxSpec} boxSpec
* @return {$3Dmol.GLShape}
@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();
*/
this.addBox = function(boxSpec) {
var dim = boxSpec.dimensions || { w: 1, h: 1, d: 1 };
//dimensions may be scalar or vector quantities
var w = dim.w;
if (typeof (dim.w) == "number") {
w = { x: dim.w, y: 0, z: 0 };
}
var h = dim.h;
if (typeof (dim.h) == "number") {
h = { x: 0, y: dim.h, z: 0 };
}
var d = dim.d;
if (typeof (dim.d) == "number") {
d = { x: 0, y: 0, z: dim.d };
}
//can position using corner OR center
var c = boxSpec.corner;
if (c == undefined) {
if (boxSpec.center !== undefined) {
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
var uv =
[{ 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
var verts = [];
var 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);
var 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;
var spec = $3Dmol.extend({}, boxSpec);
spec.vertexArr = verts;
spec.faceArr = faces;
spec.normalArr = [];
drawCustom(this, geo, spec);
var centroid = new $3Dmol.Vector3();
components.push({
centroid: centroid.addVectors(uv[0], uv[7]).multiplyScalar(0.5)
});
var geoGroup = geo.updateGeoGroup(0);
updateBoundingFromPoints(this.boundingSphere, components, geoGroup.vertexArray, geoGroup.vertices);
};
/**
* Creates a cylinder shape
* @function $3Dmol.GLShape#addCylinder
* @param {CylinderSpec} cylinderSpec
* @return {$3Dmol.GLShape}
@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();
*/
this.addCylinder = function(cylinderSpec) {
cylinderSpec.start = cylinderSpec.start || {};
cylinderSpec.end = cylinderSpec.end || {};
var start = new $3Dmol.Vector3(cylinderSpec.start.x || 0,
cylinderSpec.start.y || 0, cylinderSpec.start.z || 0);
var end = new $3Dmol.Vector3(cylinderSpec.end.x,
cylinderSpec.end.y || 0, cylinderSpec.end.z || 0);
if (typeof (end.x) == 'undefined') end.x = 3; //show something even if undefined
var radius = cylinderSpec.radius || 0.1;
var color = $3Dmol.CC.color(cylinderSpec.color);
this.intersectionShape.cylinder.push(new $3Dmol.Cylinder(start, end, radius));
$3Dmol.GLDraw.drawCylinder(geo, start, end, radius, color, cylinderSpec.fromCap, cylinderSpec.toCap);
var centroid = new $3Dmol.Vector3();
components.push({
centroid: centroid.addVectors(start, end).multiplyScalar(0.5)
});
var geoGroup = geo.updateGeoGroup(0);
updateBoundingFromPoints(this.boundingSphere, components,
geoGroup.vertexArray, geoGroup.vertices);
};
/**
* Creates a dashed cylinder shape
* @function $3Dmol.GLShape#addDashedCylinder
* @param {CylinderSpec} cylinderSpec
* @return {$3Dmol.GLShape}
*/
this.addDashedCylinder = function(cylinderSpec) {
cylinderSpec.start = cylinderSpec.start || {};
cylinderSpec.end = cylinderSpec.end || {};
cylinderSpec.dashLength = cylinderSpec.dashLength || 0.25;
cylinderSpec.gapLength = cylinderSpec.gapLength || 0.25;
var start = new $3Dmol.Vector3(cylinderSpec.start.x || 0,
cylinderSpec.start.y || 0, cylinderSpec.start.z || 0);
var end = new $3Dmol.Vector3(cylinderSpec.end.x,
cylinderSpec.end.y || 0, cylinderSpec.end.z || 0);
if (typeof (end.x) == 'undefined') end.x = 3; //show something even if undefined
var radius = cylinderSpec.radius || 0.1;
var color = $3Dmol.CC.color(cylinderSpec.color);
var cylinderLength = Math.sqrt(Math.pow((start.x - end.x), 2) + Math.pow((start.y - end.y), 2) + Math.pow((start.z - end.z), 2));
var count = cylinderLength / (cylinderSpec.gapLength + cylinderSpec.dashLength);
var new_start = new $3Dmol.Vector3(cylinderSpec.start.x || 0,
cylinderSpec.start.y || 0, cylinderSpec.start.z || 0);
var new_end = new $3Dmol.Vector3(cylinderSpec.end.x,
cylinderSpec.end.y || 0, cylinderSpec.end.z || 0);
var gapVector = new $3Dmol.Vector3((end.x - start.x) / (cylinderLength / cylinderSpec.gapLength), (end.y - start.y) / (cylinderLength / cylinderSpec.gapLength), (end.z - start.z) / (cylinderLength / cylinderSpec.gapLength));
var dashVector = new $3Dmol.Vector3((end.x - start.x) / (cylinderLength / cylinderSpec.dashLength), (end.y - start.y) / (cylinderLength / cylinderSpec.dashLength), (end.z - start.z) / (cylinderLength / cylinderSpec.dashLength));
for (var place = 0; place < count; place++) {
new_end = new $3Dmol.Vector3(new_start.x + dashVector.x, new_start.y + dashVector.y, new_start.z + dashVector.z);
this.intersectionShape.cylinder.push(new $3Dmol.Cylinder(new_start, new_end, radius));
$3Dmol.GLDraw.drawCylinder(geo, new_start, new_end, radius, color, cylinderSpec.fromCap, cylinderSpec.toCap);
new_start = new $3Dmol.Vector3(new_end.x + gapVector.x, new_end.y + gapVector.y, new_end.z + gapVector.z);
}
var centroid = new $3Dmol.Vector3();
components.push({
centroid: centroid.addVectors(start, end).multiplyScalar(0.5)
});
var geoGroup = geo.updateGeoGroup(0);
updateBoundingFromPoints(this.boundingSphere, components,
geoGroup.vertexArray, geoGroup.vertices);
};
/**
* Creates a curved shape
* @function $3Dmol.GLShape#addCurve
* @param {CurveSpec} curveSpec
* @return {$3Dmol.GLShape}
*/
this.addCurve = function(curveSpec) {
curveSpec.points = curveSpec.points || [];
curveSpec.smooth = curveSpec.smooth || 10;
if (typeof (curveSpec.fromCap) == "undefined") curveSpec.fromCap = 2;
if (typeof (curveSpec.toCap) == "undefined") curveSpec.toCap = 2;
//subdivide into smoothed spline points
var points = $3Dmol.subdivide_spline(curveSpec.points, curveSpec.smooth);
if (points.length < 3) {
console.log("Too few points in addCurve");
return;
}
var radius = curveSpec.radius || 0.1;
var color = $3Dmol.CC.color(curveSpec.color);
//TODO TODO - this is very inefficient, should create our
//own water tight model with proper normals...