-
Notifications
You must be signed in to change notification settings - Fork 191
/
drawCartoon.js
1260 lines (1104 loc) · 38.8 KB
/
drawCartoon.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
// glcartoon.js
// This contains all the routines for rendering a cartoon given a set
// of atoms with assigned secondary structure
import {CC, getColorFromStyle} from './colors';
import GLDraw from './GLDraw';
import Gradient from './Gradient';
import isNumeric from './util/isNumeric';
import subdivideSpline from './util/subdivideSpline';
import {Geometry} from './WebGL/core';
import {FaceColors, LineBasicMaterial, MeshDoubleLambertMaterial} from './WebGL/materials';
import {Vector3} from './WebGL/math';
import {Line, LineStrip, Mesh} from './WebGL/objects';
import {Triangle, Sphere} from './WebGL/shapes';
// TODO: generate normals directly in drawStrip and drawThinStrip
const defaultNum = 5; // for cross-sectional shape
const defaultDiv = 5; // for length-wise splicing
const coilWidth = 0.5;
const helixSheetWidth = 1.3;
const nucleicAcidWidth = 0.8;
const defaultThickness = 0.4;
// proteins na backbone na terminus nucleobases
const cartoonAtoms = {
C: true,
CA: true,
O: true,
P: true,
OP2: true,
O2P: true,
"O5'": true,
"O3'": true,
"C5'": true,
"C2'": true,
'O5*': true,
'O3*': true,
'C5*': true,
'C2*': true,
N1: true,
N3: true,
};
const purResns = {DA: true, DG: true, A: true, G: true};
const pyrResns = {DT: true, DC: true, U: true, C: true, T: true};
const naResns = {
DA: true,
DG: true,
A: true,
G: true,
DT: true,
DC: true,
U: true,
C: true,
T: true,
};
function drawThinStrip(geo, p1, p2, colors) {
let offset;
let vertoffset;
let color;
let colori;
for (let i = 0, lim = p1.length; i < lim; i++) {
colori = Math.round((i * (colors.length - 1)) / lim);
color = CC.color(colors[colori]);
const geoGroup = geo.updateGeoGroup(2);
const {vertexArray} = geoGroup;
const {colorArray} = geoGroup;
const {faceArray} = geoGroup;
offset = geoGroup.vertices;
vertoffset = offset * 3;
vertexArray[vertoffset] = p1[i].x;
vertexArray[vertoffset + 1] = p1[i].y;
vertexArray[vertoffset + 2] = p1[i].z;
vertexArray[vertoffset + 3] = p2[i].x;
vertexArray[vertoffset + 4] = p2[i].y;
vertexArray[vertoffset + 5] = p2[i].z;
for (let j = 0; j < 6; ++j) {
colorArray[vertoffset + 3 * j] = color.r;
colorArray[vertoffset + 1 + 3 * j] = color.g;
colorArray[vertoffset + 2 + 3 * j] = color.b;
}
if (i > 0) {
const faces = [offset, offset + 1, offset - 1, offset - 2];
const faceoffset = geoGroup.faceidx;
faceArray[faceoffset] = faces[0];
faceArray[faceoffset + 1] = faces[1];
faceArray[faceoffset + 2] = faces[3];
faceArray[faceoffset + 3] = faces[1];
faceArray[faceoffset + 4] = faces[2];
faceArray[faceoffset + 5] = faces[3];
geoGroup.faceidx += 6;
}
geoGroup.vertices += 2;
}
}
function drawShapeStrip(geo, points, colors, div, thickness, opacity, shape) {
// points is a 2D array, dimensionality given by [num = cross-sectional
// resolution][len = length of strip]
let i;
let j;
const num = points.length;
if (num < 2 || points[0].length < 2) return null;
for (i = 0; i < num; i++) {
// spline to generate greater length-wise
// resolution
points[i] = subdivideSpline(points[i], div);
}
const len = points[0].length;
if (!thickness)
// if thickness is 0, we can use a smaller geometry than
// this function generates
return drawThinStrip(geo, points[0], points[num - 1], colors);
let axis;
let csShape;
/** @type {Vector3[]} */
let csBottom = [];
/** @type {Vector3[]} */
let csTop = [];
/** @type {Vector3[]} */
let lastCsBottom;
/** @type {Vector3[]} */
let lastCsTop;
// cache the available cross-sectional shapes
const csEllipse = [];
const csRectangle = [];
const csParabola = [];
for (j = 0; j < num; j++) {
csEllipse.push(0.25 + (1.5 * Math.sqrt((num - 1) * j - j ** 2)) / (num - 1));
csRectangle.push(0.5);
csParabola.push(2 * ((j / num) ** 2 - j / num) + 0.6);
}
/*
* faceRefs array is used to generate faces from vertexArray
* iteratively. As we move through each cross-sectional segment of
* points, we draw lateral faces backwards to the previous
* cross-sectional segment.
*
* To correctly identify the points needed to make each face we use this
* array as a lookup table for the relative indices of each needed point
* in the vertices array.
*
* 4 points are used to create 2 faces.
*/
const faceRefs = [];
for (j = 0; j < num * 2 - 1; j++) {
/*
* [curr vertex in curr cross-section, next vertex in curr
* cross-section, next vertex in prev cross-section, curr vertex in
* prev cross-section]
*/
faceRefs[j] = [j, j + 1, j + 1 - 2 * num, j - 2 * num];
}
// last face is different. easier to conceptualize this by drawing a
// diagram
faceRefs[num * 2 - 1] = [j, j + 1 - 2 * num, j + 1 - 4 * num, j - 2 * num];
let vOffset;
let vaOffset;
let fOffset;
let currentAtom;
let lastAtom;
let color;
let colori;
const geoGroup = geo.updateGeoGroup(2 * num * len); // ensure vertex
// capacity
let vertexArray;
let colorArray;
let faceArray;
let face;
for (i = 0; i < len; i++) {
colori = Math.round((i * (colors.length - 1)) / len);
color = CC.color(colors[colori]);
lastCsBottom = csBottom;
lastCsTop = csTop;
csBottom = [];
csTop = [];
axis = [];
if (points[0][i].atom !== undefined) {
// TODO better edge case
// handling
currentAtom = points[0][i].atom;
if (shape === 'oval') csShape = csEllipse;
else if (shape === 'rectangle') csShape = csRectangle;
else if (shape === 'parabola') csShape = csParabola;
}
if (!csShape) csShape = csRectangle;
// calculate thickness at each width point, from cross-sectional
// shape
let toNext;
let toSide;
for (j = 0; j < num; j++) {
if (i < len - 1) toNext = points[j][i + 1].clone().sub(points[j][i]);
else toNext = points[j][i - 1].clone().sub(points[j][i]).negate();
if (j < num - 1) toSide = points[j + 1][i].clone().sub(points[j][i]);
else toSide = points[j - 1][i].clone().sub(points[j][i]).negate();
axis[j] = toSide
.cross(toNext)
.normalize()
.multiplyScalar(thickness * csShape[j]);
}
// generate vertices by applying cross-sectional shape thickness to
// input points
for (j = 0; j < num; j++) csBottom[j] = points[j][i].clone().add(axis[j].clone().negate());
for (j = 0; j < num; j++) csTop[j] = points[j][i].clone().add(axis[j]);
/*
* Until this point the vertices have been dealt with as
* Vector3() objects, but we need to serialize them into the
* geoGroup.vertexArray, where every three indices represents the
* next vertex. The colorArray is analogous.
*
* In the following for-loops, j iterates through VERTICES so we
* need to index them in vertexArray by 3*j + either 0, 1, or 2 for
* xyz or rgb component.
*/
vertexArray = geoGroup.vertexArray;
colorArray = geoGroup.colorArray;
faceArray = geoGroup.faceArray;
vOffset = geoGroup.vertices;
vaOffset = vOffset * 3; // in case geoGroup already contains
// vertices
// bottom edge of cross-section, vertices [0, num)
for (j = 0; j < num; j++) {
vertexArray[vaOffset + 3 * j + 0] = csBottom[j].x;
vertexArray[vaOffset + 3 * j + 1] = csBottom[j].y;
vertexArray[vaOffset + 3 * j + 2] = csBottom[j].z;
}
// top edge of cross-section, vertices [num, 2*num)
// add these backwards, so that each cross-section's vertices are
// added sequentially to vertexArray
for (j = 0; j < num; j++) {
vertexArray[vaOffset + 3 * j + 0 + 3 * num] = csTop[num - 1 - j].x;
vertexArray[vaOffset + 3 * j + 1 + 3 * num] = csTop[num - 1 - j].y;
vertexArray[vaOffset + 3 * j + 2 + 3 * num] = csTop[num - 1 - j].z;
}
for (j = 0; j < 2 * num; ++j) {
colorArray[vaOffset + 3 * j + 0] = color.r;
colorArray[vaOffset + 3 * j + 1] = color.g;
colorArray[vaOffset + 3 * j + 2] = color.b;
}
if (i > 0) {
for (j = 0; j < num * 2; j++) {
// get VERTEX indices of the 4 points of a rectangular face
// (as opposed to literal vertexArray indices)
face = [
vOffset + faceRefs[j][0],
vOffset + faceRefs[j][1],
vOffset + faceRefs[j][2],
vOffset + faceRefs[j][3],
];
fOffset = geoGroup.faceidx;
// need 2 triangles to draw a face between 4 points
faceArray[fOffset] = face[0];
faceArray[fOffset + 1] = face[1];
faceArray[fOffset + 2] = face[3];
faceArray[fOffset + 3] = face[1];
faceArray[fOffset + 4] = face[2];
faceArray[fOffset + 5] = face[3];
geoGroup.faceidx += 6;
// TODO implement clickable the right way. midpoints of
// strand between consecutive atoms
}
if (currentAtom.clickable || currentAtom.hoverable) {
const faces = [];
faces.push(new Triangle(lastCsBottom[0], csBottom[0], csBottom[num - 1]));
faces.push(new Triangle(lastCsBottom[0], csBottom[num - 1], lastCsBottom[num - 1]));
faces.push(new Triangle(lastCsBottom[num - 1], csBottom[num - 1], csTop[num - 1]));
faces.push(new Triangle(lastCsBottom[num - 1], csTop[num - 1], lastCsTop[num - 1]));
faces.push(new Triangle(csTop[0], lastCsTop[0], lastCsTop[num - 1]));
faces.push(new Triangle(csTop[num - 1], csTop[0], lastCsTop[num - 1]));
faces.push(new Triangle(csBottom[0], lastCsBottom[0], lastCsTop[0]));
faces.push(new Triangle(csTop[0], csBottom[0], lastCsTop[0]));
for (j in faces) {
currentAtom.intersectionShape.triangle.push(faces[j]);
}
}
}
geoGroup.vertices += 2 * num;
lastAtom = currentAtom;
}
// for terminal faces
vertexArray = geoGroup.vertexArray;
colorArray = geoGroup.colorArray;
faceArray = geoGroup.faceArray;
vOffset = geoGroup.vertices;
vaOffset = vOffset * 3;
fOffset = geoGroup.faceidx;
for (
i = 0;
i < num - 1;
i++ // "rear" face
) {
face = [i, i + 1, 2 * num - 2 - i, 2 * num - 1 - i];
fOffset = geoGroup.faceidx;
faceArray[fOffset] = face[0];
faceArray[fOffset + 1] = face[1];
faceArray[fOffset + 2] = face[3];
faceArray[fOffset + 3] = face[1];
faceArray[fOffset + 4] = face[2];
faceArray[fOffset + 5] = face[3];
geoGroup.faceidx += 6;
}
for (
i = 0;
i < num - 1;
i++ // "front" face
) {
face = [vOffset - 1 - i, vOffset - 2 - i, vOffset - 2 * num + i + 1, vOffset - 2 * num + i];
fOffset = geoGroup.faceidx;
faceArray[fOffset] = face[0];
faceArray[fOffset + 1] = face[1];
faceArray[fOffset + 2] = face[3];
faceArray[fOffset + 3] = face[1];
faceArray[fOffset + 4] = face[2];
faceArray[fOffset + 5] = face[3];
geoGroup.faceidx += 6;
}
}
function drawPlainStrip(geo, points, colors, div, thickness, opacity) {
if (points.length < 2) return;
let p1;
let p2;
p1 = points[0];
p2 = points[points.length - 1];
p1 = subdivideSpline(p1, div);
p2 = subdivideSpline(p2, div);
if (!thickness) return drawThinStrip(geo, p1, p2, colors);
// var vs = geo.vertices, fs = geo.faces;
const vs = [];
let axis;
let p1v;
let p2v;
let a1v;
let a2v;
const faces = [
[0, 2, -6, -8],
[-4, -2, 6, 4],
[7, -1, -5, 3],
[-3, 5, 1, -7],
];
let offset;
let vertoffset;
let faceoffset;
let color;
let colori;
let currentAtom;
let lastAtom;
let i;
let lim;
let j;
let face1;
let face2;
let face3;
let geoGroup;
let vertexArray;
let colorArray;
let faceArray;
for (i = 0, lim = p1.length; i < lim; i++) {
colori = Math.round((i * (colors.length - 1)) / lim);
color = CC.color(colors[colori]);
vs.push((p1v = p1[i])); // 0
vs.push(p1v); // 1
vs.push((p2v = p2[i])); // 2
vs.push(p2v); // 3
if (i < lim - 1) {
const toNext = p1[i + 1].clone().sub(p1[i]);
const toSide = p2[i].clone().sub(p1[i]);
axis = toSide.cross(toNext).normalize().multiplyScalar(thickness);
}
vs.push((a1v = p1[i].clone().add(axis))); // 4
vs.push(a1v); // 5
vs.push((a2v = p2[i].clone().add(axis))); // 6
vs.push(a2v); // 7
if (p1v.atom !== undefined) currentAtom = p1v.atom;
geoGroup = geo.updateGeoGroup(8);
vertexArray = geoGroup.vertexArray;
colorArray = geoGroup.colorArray;
faceArray = geoGroup.faceArray;
offset = geoGroup.vertices;
vertoffset = offset * 3;
vertexArray[vertoffset] = p1v.x;
vertexArray[vertoffset + 1] = p1v.y;
vertexArray[vertoffset + 2] = p1v.z;
vertexArray[vertoffset + 3] = p1v.x;
vertexArray[vertoffset + 4] = p1v.y;
vertexArray[vertoffset + 5] = p1v.z;
vertexArray[vertoffset + 6] = p2v.x;
vertexArray[vertoffset + 7] = p2v.y;
vertexArray[vertoffset + 8] = p2v.z;
vertexArray[vertoffset + 9] = p2v.x;
vertexArray[vertoffset + 10] = p2v.y;
vertexArray[vertoffset + 11] = p2v.z;
vertexArray[vertoffset + 12] = a1v.x;
vertexArray[vertoffset + 13] = a1v.y;
vertexArray[vertoffset + 14] = a1v.z;
vertexArray[vertoffset + 15] = a1v.x;
vertexArray[vertoffset + 16] = a1v.y;
vertexArray[vertoffset + 17] = a1v.z;
vertexArray[vertoffset + 18] = a2v.x;
vertexArray[vertoffset + 19] = a2v.y;
vertexArray[vertoffset + 20] = a2v.z;
vertexArray[vertoffset + 21] = a2v.x;
vertexArray[vertoffset + 22] = a2v.y;
vertexArray[vertoffset + 23] = a2v.z;
for (j = 0; j < 8; ++j) {
colorArray[vertoffset + 3 * j] = color.r;
colorArray[vertoffset + 1 + 3 * j] = color.g;
colorArray[vertoffset + 2 + 3 * j] = color.b;
}
if (i > 0) {
// both points have distinct atoms
const diffAtoms =
lastAtom !== undefined &&
currentAtom !== undefined &&
lastAtom.serial !== currentAtom.serial;
for (j = 0; j < 4; j++) {
const face = [
offset + faces[j][0],
offset + faces[j][1],
offset + faces[j][2],
offset + faces[j][3],
];
faceoffset = geoGroup.faceidx;
faceArray[faceoffset] = face[0];
faceArray[faceoffset + 1] = face[1];
faceArray[faceoffset + 2] = face[3];
faceArray[faceoffset + 3] = face[1];
faceArray[faceoffset + 4] = face[2];
faceArray[faceoffset + 5] = face[3];
geoGroup.faceidx += 6;
if (
currentAtom.clickable ||
lastAtom.clickable ||
currentAtom.hoverable ||
lastAtom.hoverable
) {
const p1a = vs[face[3]].clone();
const p1b = vs[face[0]].clone();
const p2a = vs[face[2]].clone();
const p2b = vs[face[1]].clone();
p1a.atom = vs[face[3]].atom || null; // should be
// same
p2a.atom = vs[face[2]].atom || null;
p1b.atom = vs[face[0]].atom || null; // should be
// same
p2b.atom = vs[face[1]].atom || null;
if (diffAtoms) {
const m1 = p1a.clone().add(p1b).multiplyScalar(0.5);
const m2 = p2a.clone().add(p2b).multiplyScalar(0.5);
const m = p1a.clone().add(p2b).multiplyScalar(0.5);
if (j % 2 === 0) {
if (lastAtom.clickable || lastAtom.hoverable) {
face1 = new Triangle(m1, m, p1a);
face2 = new Triangle(m2, p2a, m);
face3 = new Triangle(m, p2a, p1a);
lastAtom.intersectionShape.triangle.push(face1);
lastAtom.intersectionShape.triangle.push(face2);
lastAtom.intersectionShape.triangle.push(face3);
}
if (currentAtom.clickable || currentAtom.hoverable) {
face1 = new Triangle(p1b, p2b, m);
face2 = new Triangle(p2b, m2, m);
face3 = new Triangle(p1b, m, m1);
currentAtom.intersectionShape.triangle.push(face1);
currentAtom.intersectionShape.triangle.push(face2);
currentAtom.intersectionShape.triangle.push(face3);
}
} else {
if (currentAtom.clickable || currentAtom.hoverable) {
face1 = new Triangle(m1, m, p1a);
face2 = new Triangle(m2, p2a, m);
face3 = new Triangle(m, p2a, p1a);
currentAtom.intersectionShape.triangle.push(face1);
currentAtom.intersectionShape.triangle.push(face2);
currentAtom.intersectionShape.triangle.push(face3);
}
if (lastAtom.clickable || lastAtom.hoverable) {
face1 = new Triangle(p1b, p2b, m);
face2 = new Triangle(p2b, m2, m);
face3 = new Triangle(p1b, m, m1);
lastAtom.intersectionShape.triangle.push(face1);
lastAtom.intersectionShape.triangle.push(face2);
lastAtom.intersectionShape.triangle.push(face3);
}
}
}
// face for single atom
else if (currentAtom.clickable || currentAtom.hoverable) {
face1 = new Triangle(p1b, p2b, p1a);
face2 = new Triangle(p2b, p2a, p1a);
currentAtom.intersectionShape.triangle.push(face1);
currentAtom.intersectionShape.triangle.push(face2);
}
}
}
}
geoGroup.vertices += 8;
lastAtom = currentAtom;
}
let vsize = vs.length - 8; // Cap
geoGroup = geo.updateGeoGroup(8);
vertexArray = geoGroup.vertexArray;
colorArray = geoGroup.colorArray;
faceArray = geoGroup.faceArray;
offset = geoGroup.vertices;
vertoffset = offset * 3;
faceoffset = geoGroup.faceidx;
for (i = 0; i < 4; i++) {
vs.push(vs[i * 2]);
vs.push(vs[vsize + i * 2]);
const v1 = vs[i * 2];
const v2 = vs[vsize + i * 2];
vertexArray[vertoffset + 6 * i] = v1.x;
vertexArray[vertoffset + 1 + 6 * i] = v1.y;
vertexArray[vertoffset + 2 + 6 * i] = v1.z;
vertexArray[vertoffset + 3 + 6 * i] = v2.x;
vertexArray[vertoffset + 4 + 6 * i] = v2.y;
vertexArray[vertoffset + 5 + 6 * i] = v2.z;
colorArray[vertoffset + 6 * i] = color.r;
colorArray[vertoffset + 1 + 6 * i] = color.g;
colorArray[vertoffset + 2 + 6 * i] = color.b;
colorArray[vertoffset + 3 + 6 * i] = color.r;
colorArray[vertoffset + 4 + 6 * i] = color.g;
colorArray[vertoffset + 5 + 6 * i] = color.b;
}
vsize += 8;
face1 = [offset, offset + 2, offset + 6, offset + 4];
face2 = [offset + 1, offset + 5, offset + 7, offset + 3];
faceArray[faceoffset] = face1[0];
faceArray[faceoffset + 1] = face1[1];
faceArray[faceoffset + 2] = face1[3];
faceArray[faceoffset + 3] = face1[1];
faceArray[faceoffset + 4] = face1[2];
faceArray[faceoffset + 5] = face1[3];
faceArray[faceoffset + 6] = face2[0];
faceArray[faceoffset + 7] = face2[1];
faceArray[faceoffset + 8] = face2[3];
faceArray[faceoffset + 9] = face2[1];
faceArray[faceoffset + 10] = face2[2];
faceArray[faceoffset + 11] = face2[3];
geoGroup.faceidx += 12;
geoGroup.vertices += 8;
// TODO: Add intersection planes for caps
}
// TODO: Need to update this (will we ever use this?)
/**
* @param {import("./WebGL/core").geometryGroup} group
* @param {Vector3[]} points
* @param {number} width
* @returns
*/
function drawSmoothCurve(group, points, width) {
if (points.length === 0) return;
const geo = new Geometry();
const lineMaterial = new LineBasicMaterial({
linewidth: width,
});
lineMaterial.vertexColors = true;
const line = new Line(geo, lineMaterial);
line.type = LineStrip;
// @ts-ignore
group.add(line);
}
function drawStrip(geo, points, colors, div, thickness, opacity, shape) {
if (!shape || shape === 'default') shape = 'rectangle';
if (shape === 'edged') drawPlainStrip(geo, points, colors, div, thickness, opacity);
else if (shape === 'rectangle' || shape === 'oval' || shape === 'parabola')
drawShapeStrip(geo, points, colors, div, thickness, opacity, shape);
}
// check if given atom is an alpha carbon
function isAlphaCarbon(atom) {
return atom && atom.elem === 'C' && atom.atom === 'CA'; // note that
// calcium is
// also CA
}
// check whether two atoms are members of the same residue or subsequent,
// connected residues (a before b)
function inConnectedResidues(a, b) {
if (a && b && a.chain === b.chain) {
if (
!a.hetflag &&
!b.hetflag &&
a.reschain === b.reschain &&
(a.resi === b.resi || a.resi === b.resi - 1)
)
return true;
if (a.resi < b.resi) {
// some PDBs have gaps in the numbering but the residues are
// still connected
// assume if within 4A they are connected
const dx = a.x - b.x;
const dy = a.y - b.y;
const dz = a.z - b.z;
const dist = dx * dx + dy * dy + dz * dz;
if (a.atom === 'CA' && b.atom === 'CA' && dist < 16.0)
// protein residues not connected
return true; // calpha dist
if ((a.atom === 'P' || b.atom === 'P') && dist < 64.0)
// dna
return true;
}
}
return false;
}
// add geo to the group
function setGeo(group, geo, opacity, outline, setNormals) {
if (geo == null || geo.vertices === 0) return;
if (setNormals) {
geo.initTypedArrays();
geo.setUpNormals();
}
const cartoonMaterial = new MeshDoubleLambertMaterial();
cartoonMaterial.vertexColors = FaceColors;
if (typeof opacity === 'number' && opacity >= 0 && opacity < 1) {
cartoonMaterial.transparent = true;
cartoonMaterial.opacity = opacity;
}
cartoonMaterial.outline = outline;
const cartoonMesh = new Mesh(geo, cartoonMaterial);
group.add(cartoonMesh);
}
function addBackbonePoints(
points,
num,
smoothen,
backbonePt,
orientPt,
prevOrientPt,
backboneAtom,
atoms,
atomi
) {
let widthScalar;
let i;
let delta;
let v;
let addArrowPoints;
if (!backbonePt || !orientPt || !backboneAtom) return null;
// the side vector points along the axis from backbone atom to
// orientation atom (eg. CA to O, in peptides)
const sideVec = orientPt.sub(backbonePt);
sideVec.normalize();
// find next atom like this one
let forwardVec = atoms[atomi];
for (i = atomi + 1; i < atoms.length; i++) {
forwardVec = atoms[i];
if (forwardVec.atom === backboneAtom.atom) break;
}
// the forward vector points along the axis from backbone atom to next
// backbone atom
forwardVec = forwardVec
? new Vector3(forwardVec.x, forwardVec.y, forwardVec.z)
: new Vector3(0, 0, 0);
forwardVec.sub(backbonePt);
// adjustments for proper beta arrow appearance
if (backboneAtom.ss === 'arrow start') {
const adjustment = forwardVec.clone().multiplyScalar(0.3).cross(orientPt); // adjust perpendicularly to strand face
backbonePt.add(adjustment);
const upVec = forwardVec.clone().cross(sideVec).normalize();
sideVec.rotateAboutVector(upVec, 0.43);
}
// determine from cartoon style or secondary structure how wide the
// strand should be here
// ribbon shape should have same width as thickness
if (backboneAtom.style.cartoon.ribbon) {
widthScalar = backboneAtom.style.cartoon.thickness || defaultThickness;
} // depending on secondary structure, multiply the orientation
// vector by some scalar
else if (!backboneAtom.style.cartoon.width) {
if (backboneAtom.ss === 'c') {
if (backboneAtom.atom === 'P') widthScalar = nucleicAcidWidth;
else widthScalar = coilWidth;
} else if (backboneAtom.ss === 'arrow start') {
widthScalar = helixSheetWidth;
addArrowPoints = true;
} else if (backboneAtom.ss === 'arrow end') widthScalar = coilWidth;
else if (
(backboneAtom.ss === 'h' && backboneAtom.style.cartoon.tubes) ||
backboneAtom.ss === 'tube start'
)
widthScalar = coilWidth;
else widthScalar = helixSheetWidth;
} else widthScalar = backboneAtom.style.cartoon.width;
// make sure the strand orientation doesn't twist more than 90 degrees
if (prevOrientPt != null && sideVec.dot(prevOrientPt) < 0) sideVec.negate();
sideVec.multiplyScalar(widthScalar);
for (i = 0; i < num; i++) {
// produces NUM incremental points from backbone atom minus
// orientation vector
// to backbone atom plus orientation vector
delta = -1 + (i * 2) / (num - 1); // -1 to 1 incrementing by num
v = new Vector3(
backbonePt.x + delta * sideVec.x,
backbonePt.y + delta * sideVec.y,
backbonePt.z + delta * sideVec.z
);
v.atom = backboneAtom;
if (smoothen && backboneAtom.ss === 's') v.smoothen = true;
points[i].push(v); // a num-length array of arrays, where each
// inner array contains length-wise points
// along the backbone offset by some constant pertaining to its cell
// in the outer array
}
if (addArrowPoints) {
sideVec.multiplyScalar(2);
for (i = 0; i < num; i++) {
delta = -1 + (i * 2) / (num - 1); // -1 to 1 incrementing by num
v = new Vector3(
backbonePt.x + delta * sideVec.x,
backbonePt.y + delta * sideVec.y,
backbonePt.z + delta * sideVec.z
);
v.atom = backboneAtom;
v.smoothen = false;
v.skip = true;
points[i].push(v);
}
}
// make sure the strand is all the same style
const testStyle = backboneAtom.style.cartoon.style || 'default';
if (points.style) {
if (points.style !== testStyle) {
console.log("Warning: a cartoon chain's strand-style is ambiguous");
points.style = 'default';
}
} else points.style = testStyle;
// revert ss keywords used for arrow rendering back to original value
if (backboneAtom.ss === 'arrow start' || backboneAtom.ss === 'arrow end') backboneAtom.ss = 's';
return addArrowPoints;
}
/**
* @ignore
* @param {import("./WebGL/core").geometryGroup} group
* @param {import("./specs").AtomSpec} atomList
*/
function drawCartoonBase(group, atomList, gradientrange, fill, doNotSmoothen, num, div) {
num = num || defaultNum;
div = div || defaultDiv;
let cartoon;
let prev;
let curr;
let next;
let currColor;
let nextColor;
let thickness;
let i;
let backbonePt;
let orientPt;
let prevOrientPt;
let terminalPt;
let termOrientPt;
let baseStartPt;
let baseEndPt;
let tubeStart;
let tubeEnd;
let drawingTube;
let shapeGeo = new Geometry(true); // for shapes that don't need normals computed
let geo = new Geometry(true);
let colors = [];
/** @type {any[]&{style?:any}} */
let points = [];
let opacity = 1;
let outline = false;
const gradients = {};
for (const g in Gradient.builtinGradients) {
if (Gradient.builtinGradients.hasOwnProperty(g)) {
// COUNTER INTUITIVE - spectrum reverses direction to gradient to match other tools
gradients[g] = new Gradient.builtinGradients[g](gradientrange[1], gradientrange[0]);
}
}
function cartoonColor(next, cartoon) {
// atom and cartoon style object
if (gradientrange && cartoon.color === 'spectrum') {
if (cartoon.colorscheme in gradients) {
return gradients[cartoon.colorscheme].valueToHex(next.resi);
}
return gradients.sinebow.valueToHex(next.resi);
}
return getColorFromStyle(next, cartoon).getHex();
};
for (i = 0; i < num; i++) points[i] = [];
// first determine where beta sheet arrows and alpha helix tubes belong
let inSheet = false;
let inHelix = false; // only considering tube styled helices
const atoms = [];
for (i in atomList) {
next = atomList[i];
if (next.elem === 'C' && next.atom === 'CA') {
const connected = inConnectedResidues(curr, next);
// last two residues in a beta sheet become arrowhead
if (connected && next.ss === 's') {
inSheet = true;
} else if (inSheet) {
if (curr && prev && curr.style.cartoon.arrows && prev.style.cartoon.arrows) {
curr.ss = 'arrow end';
prev.ss = 'arrow start';
}
inSheet = false;
}
// first and last residues in a helix are used to draw tube
if (connected && (curr.ss === 'h' || curr.ss === 'tube start') && curr.style.cartoon.tubes) {
if (!inHelix && curr.ss !== 'tube start' && next.style.cartoon.tubes) {
next.ss = 'tube start';
inHelix = true;
}
} else if (inHelix) {
if (curr.ss === 'tube start') {
curr.ss = 'tube end'; // only one residue
} else if (prev && prev.style.cartoon.tubes) {
prev.ss = 'tube end';
}
inHelix = false;
}
prev = curr;
curr = next;
}
if (next && next.atom in cartoonAtoms) {
atoms.push(next);
}
}
if (inHelix && curr.style.cartoon.tubes) {
curr.ss = 'tube end';
inHelix = false;
}
const flushGeom = function (connect) {
// write out points, update geom,etc
for (let i = 0; !thickness && i < num; i++)
drawSmoothCurve(group, points[i], 1);
if (fill && points[0].length > 0) {
drawStrip(geo, points, colors, div, thickness, opacity, points.style);
}
const saved = [];
let savedc = null;
if (connect) {
// recycle last point to first point of next points array
for (i = 0; i < num; i++) {
saved[i] = points[i][points[i].length - 1];
}
savedc = colors[colors.length - 1];
}
points = [];
for (i = 0; i < num; i++) points[i] = [];
colors = [];
if (connect) {
for (i = 0; i < num; i++) {
// @ts-ignore
points[i].push(saved[i]);
}
colors.push(savedc);
}
setGeo(group, geo, opacity, outline, true);
setGeo(group, shapeGeo, opacity, outline, false);
geo = new Geometry(true);
shapeGeo = new Geometry(true);
};
// then accumulate points
curr = undefined;
let a = 0;
for (a = 0; a < atoms.length; a++) {
next = atoms[a];
const nextresn = next.resn.trim();
const inNucleicAcid = nextresn in naResns;
opacity = 1;
// determine cartoon style
cartoon = next.style.cartoon;
if (curr && curr.style.cartoon) opacity = curr.style.cartoon.opacity;
if (curr && curr.style.cartoon && curr.style.cartoon.outline)
outline = curr.style.cartoon.outline;
// create a new geometry when opacity changes
// this should work fine if opacity is set by chain, but will
// break if it changes within the chain
if (
curr &&
curr.style.cartoon &&