@ccp-nc/crystvis-js 0.4.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/.eslintrc.json +16 -0
  2. package/.github/workflows/test-mocha.yml +30 -0
  3. package/.vscode/settings.json +4 -0
  4. package/LICENSE +21 -0
  5. package/README.html +1127 -0
  6. package/README.md +76 -0
  7. package/demo/demo.css +30 -0
  8. package/demo/index.html +76 -0
  9. package/demo/main.js +143 -0
  10. package/docs/.nojekyll +0 -0
  11. package/docs-tutorials/Events.md +57 -0
  12. package/docs-tutorials/Queries.md +50 -0
  13. package/fonts/Rubik/OFL.txt +93 -0
  14. package/fonts/Rubik/README.txt +77 -0
  15. package/fonts/Rubik/Rubik-Italic-VariableFont_wght.ttf +0 -0
  16. package/fonts/Rubik/Rubik-VariableFont_wght.ttf +0 -0
  17. package/fonts/Rubik/static/Rubik-Black.ttf +0 -0
  18. package/fonts/Rubik/static/Rubik-BlackItalic.ttf +0 -0
  19. package/fonts/Rubik/static/Rubik-Bold.ttf +0 -0
  20. package/fonts/Rubik/static/Rubik-BoldItalic.ttf +0 -0
  21. package/fonts/Rubik/static/Rubik-ExtraBold.ttf +0 -0
  22. package/fonts/Rubik/static/Rubik-ExtraBoldItalic.ttf +0 -0
  23. package/fonts/Rubik/static/Rubik-Italic.ttf +0 -0
  24. package/fonts/Rubik/static/Rubik-Light.ttf +0 -0
  25. package/fonts/Rubik/static/Rubik-LightItalic.ttf +0 -0
  26. package/fonts/Rubik/static/Rubik-Medium.ttf +0 -0
  27. package/fonts/Rubik/static/Rubik-MediumItalic.ttf +0 -0
  28. package/fonts/Rubik/static/Rubik-Regular.ttf +0 -0
  29. package/fonts/Rubik/static/Rubik-SemiBold.ttf +0 -0
  30. package/fonts/Rubik/static/Rubik-SemiBoldItalic.ttf +0 -0
  31. package/index.html +25 -0
  32. package/index.js +11 -0
  33. package/jsconf.json +14 -0
  34. package/lib/assets/fonts/Rubik-Medium.fnt +297 -0
  35. package/lib/assets/fonts/Rubik-Medium.png +0 -0
  36. package/lib/assets/fonts/bmpfonts.in.js +16 -0
  37. package/lib/assets/fonts/bmpfonts.js +9 -0
  38. package/lib/assets/fonts/font.js +82 -0
  39. package/lib/assets/fonts/index.js +14 -0
  40. package/lib/assets/fonts/threebmfont.js +28 -0
  41. package/lib/data.js +125 -0
  42. package/lib/formats/cell.js +114 -0
  43. package/lib/formats/cif.js +22 -0
  44. package/lib/formats/magres.js +337 -0
  45. package/lib/formats/xyz.js +124 -0
  46. package/lib/loader.js +87 -0
  47. package/lib/model.js +2076 -0
  48. package/lib/modelview.js +382 -0
  49. package/lib/nmrdata.js +2898 -0
  50. package/lib/orbit.js +1233 -0
  51. package/lib/primitives/atoms.js +261 -0
  52. package/lib/primitives/cell.js +160 -0
  53. package/lib/primitives/dither.js +156 -0
  54. package/lib/primitives/ellipsoid.js +183 -0
  55. package/lib/primitives/geometries.js +20 -0
  56. package/lib/primitives/index.js +48 -0
  57. package/lib/primitives/isosurface.js +171 -0
  58. package/lib/primitives/shapes.js +100 -0
  59. package/lib/primitives/sprites.js +172 -0
  60. package/lib/query.js +158 -0
  61. package/lib/render.js +440 -0
  62. package/lib/selbox.js +361 -0
  63. package/lib/shaders/aura.frag +26 -0
  64. package/lib/shaders/aura.vert +37 -0
  65. package/lib/shaders/dither.frag +42 -0
  66. package/lib/shaders/dither.vert +8 -0
  67. package/lib/shaders/index.in.js +17 -0
  68. package/lib/shaders/index.js +25 -0
  69. package/lib/shaders/msdf300.frag +25 -0
  70. package/lib/shaders/msdf300.vert +45 -0
  71. package/lib/tensor.js +227 -0
  72. package/lib/utils.js +168 -0
  73. package/lib/visualizer.js +480 -0
  74. package/package.json +106 -0
  75. package/scripts/build-bundle.js +17 -0
  76. package/scripts/build-fonts.js +43 -0
  77. package/scripts/build-resources.js +46 -0
  78. package/scripts/plugins-shim.js +10 -0
  79. package/test/chemdata.js +69 -0
  80. package/test/data/CHA.cif +74 -0
  81. package/test/data/H2O.xyz +8 -0
  82. package/test/data/H2_bound.xyz +4 -0
  83. package/test/data/ethanol.cell +25 -0
  84. package/test/data/ethanol.magres +238 -0
  85. package/test/data/example_single.cif +789 -0
  86. package/test/data/frac.cell +8 -0
  87. package/test/data/org.cif +427 -0
  88. package/test/data/pyridine.xyz +13 -0
  89. package/test/data/si8.xyz +10 -0
  90. package/test/loader.js +107 -0
  91. package/test/model.js +368 -0
  92. package/test/query.js +135 -0
  93. package/test/tensor.js +133 -0
  94. package/test/test-html/examples.js +1485 -0
  95. package/test/test-html/index.html +33 -0
  96. package/test/test-html/index.js +279 -0
  97. package/tools/compile_colors.py +120 -0
  98. package/tools/compile_periodic.py +96 -0
  99. package/tools/ptable.json +497 -0
  100. package/tools/test +5844 -0
@@ -0,0 +1,382 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * @fileoverview Class holding "model views", subsets of atoms in a Model used
5
+ * for selection or to perform operations in block
6
+ * @module
7
+ */
8
+
9
+ import _ from 'lodash';
10
+
11
+ /** A 'view' representing a subset of atom images of a model, used for selection and further manipulations */
12
+ class ModelView {
13
+
14
+ /**
15
+ * @param {Model} model Model to use for the view
16
+ * @param {int[]} indices Indices of the atom images to include in the view
17
+ */
18
+ constructor(model, indices) {
19
+
20
+ this._model = model;
21
+ this._indices = indices;
22
+ this._images = _.map(indices, function(i) {
23
+ return model._atom_images[i];
24
+ });
25
+
26
+ this._bonds = {};
27
+ for (var i = 0; i < this._images.length; ++i) {
28
+ var img = this._images[i];
29
+ var bonds = img.bonds;
30
+ for (var j = 0; j < bonds.length; ++j) {
31
+ var b = bonds[j];
32
+ this._bonds[b.key] = b;
33
+ }
34
+ }
35
+
36
+ this._bonds = Object.values(this._bonds);
37
+ }
38
+
39
+ /**
40
+ * Model used by this view
41
+ * @readonly
42
+ * @type {Model}
43
+ */
44
+ get model() {
45
+ return this._model;
46
+ }
47
+
48
+ /**
49
+ * Indices of the atom images in this view
50
+ * @readonly
51
+ * @type {int[]}
52
+ */
53
+ get indices() {
54
+ return Array.from(this._indices);
55
+ }
56
+
57
+ /**
58
+ * Atom images in this view
59
+ * @readonly
60
+ * @type {AtomImage[]}
61
+ */
62
+ get atoms() {
63
+ return Array.from(this._images);
64
+ }
65
+
66
+ /**
67
+ * Number of atom images in this view
68
+ * @readonly
69
+ * @type {int}
70
+ */
71
+ get length() {
72
+ return this._indices.length;
73
+ }
74
+
75
+ // Operations on the selected atoms
76
+ // Visibility
77
+
78
+ /**
79
+ * Make all atoms in the view visible. Can be chained
80
+ * @return {ModelView}
81
+ */
82
+ show() {
83
+ this._model._setAtomsProperty(this._images, 'visible', true);
84
+ return this;
85
+ }
86
+
87
+ /**
88
+ * Make all atoms in the view invisible. Can be chained
89
+ * @return {ModelView}
90
+ */
91
+ hide() {
92
+ this._model._setAtomsProperty(this._images, 'visible', false);
93
+ return this;
94
+ }
95
+
96
+ /**
97
+ * Run a function on each AtomImage, returning an Array of the results.
98
+ *
99
+ * @param {Function} func Function to run, should take AtomImage and
100
+ * index as arguments
101
+ *
102
+ * @return {Array} Return values
103
+ */
104
+ map(func) {
105
+ var returns = [];
106
+ for (var i = 0; i < this.length; ++i) {
107
+ returns.push(func(this._images[i], i));
108
+ }
109
+ return returns;
110
+ }
111
+
112
+ /**
113
+ * Perform a further search within the atoms included in this ModelView.
114
+ *
115
+ * @param {Array} query Query for the search, formatted as for
116
+ * the Model.find function.
117
+ *
118
+ * @return {ModelView} Result of the query
119
+ */
120
+ find(query) {
121
+ var found = this._model._qparse.parse(query);
122
+ return new ModelView(this._model,
123
+ _.intersectionWith(this._indices, found));
124
+ }
125
+
126
+ // Logical operations with another ModelView
127
+ /**
128
+ * Intersection with another ModelView
129
+ * @param {ModelView} mview Other view
130
+ * @return {ModelView} Result
131
+ */
132
+ and(mview) {
133
+ if (this._model != mview._model)
134
+ throw 'The two ModelViews do not refer to the same Model';
135
+ return new ModelView(this._model,
136
+ _.intersectionWith(this._indices, mview._indices));
137
+ }
138
+
139
+ /**
140
+ * Union with another ModelView
141
+ * @param {ModelView} mview Other view
142
+ * @return {ModelView} Result
143
+ */
144
+ or(mview) {
145
+ if (this._model != mview._model)
146
+ throw 'The two ModelViews do not refer to the same Model';
147
+ return new ModelView(this._model,
148
+ _.unionWith(this._indices, mview._indices));
149
+ }
150
+
151
+ /**
152
+ * Exclusive OR with another ModelView
153
+ * @param {ModelView} mview Other view
154
+ * @return {ModelView} Result
155
+ */
156
+ xor(mview) {
157
+ if (this._model != mview._model)
158
+ throw 'The two ModelViews do not refer to the same Model';
159
+ return new ModelView(this._model,
160
+ _.xorWith(this._indices, mview._indices));
161
+ }
162
+
163
+ /**
164
+ * Complement to this ModelView
165
+ * @return {ModelView} Result
166
+ */
167
+ not() {
168
+ var indices = _.xorWith(this._indices, _.range(this._model._atom_images.length));
169
+ return new ModelView(this._model, indices);
170
+ }
171
+
172
+ /**
173
+ * Internal function used to turn a single value, array of values, or
174
+ * function into an array of values
175
+ * @private
176
+ */
177
+ _standardValueArray(value) {
178
+
179
+ if (_.isFunction(value)) {
180
+ value = this.map(value);
181
+ } else if (!(value instanceof Array)) {
182
+ value = Array(this.length).fill(value);
183
+ }
184
+
185
+ return Array.from(value);
186
+ }
187
+
188
+ /**
189
+ * Set some property of the atoms within the ModelView.
190
+ *
191
+ * @param {String} name Name of the property to set
192
+ * @param {int|Array|function} value Value to set for the atoms. It can
193
+ * be either:
194
+ *
195
+ * 1. a single value for all of them
196
+ * 2. an Array of values as long as
197
+ * the ModelView
198
+ * 3. a function that accepts an
199
+ * AtomImage and an index and returns
200
+ * a value
201
+ *
202
+ * If left empty, the property is
203
+ * restored to its default value.
204
+ */
205
+ setProperty(name, value=null) {
206
+
207
+ if (name[0] == '_') {
208
+ // Assignment of hidden properties not supported!
209
+ throw 'Can not assign a value to hidden properties';
210
+ }
211
+
212
+ value = this._standardValueArray(value);
213
+
214
+ for (var i = 0; i < this.length; ++i) {
215
+ var v = value[i];
216
+ var aimg = this._images[i];
217
+ aimg[name] = v;
218
+ }
219
+
220
+ return this;
221
+ }
222
+
223
+ /**
224
+ * Add labels to the atom images in this ModelView
225
+ *
226
+ * @param {String | String[] | Function} text Text of the labels,
227
+ * as single value, array,
228
+ * or function returning a
229
+ * string for each atom image.
230
+ * @param {String | String[] | Function} name Name of the label
231
+ * @param {Object | Object[] | Function} args Arguments for creating the label
232
+ */
233
+ addLabels(text, name = 'label', args = {}) {
234
+
235
+ // Defaults
236
+ if (!text) {
237
+ text = function(a) {
238
+ return a.element;
239
+ }
240
+ }
241
+
242
+ text = this._standardValueArray(text);
243
+ name = this._standardValueArray(name);
244
+ args = this._standardValueArray(args);
245
+
246
+ for (var i = 0; i < this.length; ++i) {
247
+ var aimg = this._images[i];
248
+ aimg.addLabel(String(text[i]), name[i], args[i]);
249
+ }
250
+
251
+ return this;
252
+ }
253
+
254
+ /**
255
+ * Remove labels from the atom images in this ModelView
256
+ *
257
+ * @param {String | String[] | Function} name Name of the labels to remove
258
+ */
259
+ removeLabels(name = 'label') {
260
+
261
+ name = this._standardValueArray(name);
262
+
263
+ for (var i = 0; i < this.length; ++i) {
264
+ var aimg = this._images[i];
265
+ aimg.removeLabel(name[i]);
266
+ }
267
+
268
+ return this;
269
+ }
270
+
271
+ /**
272
+ * Get or set labels' properties for the atom images in this ModelView
273
+ *
274
+ * @param {String | String[] | Function} name Name of the labels
275
+ * @param {String | String[] | Function} property Property to get or set
276
+ * @param {Any | Any[] | Function} value If not provided, get. If provided,
277
+ * set this value
278
+ */
279
+ labelProperties(name = 'label', property = 'color', value = null) {
280
+
281
+ name = this._standardValueArray(name);
282
+ property = this._standardValueArray(property);
283
+
284
+ var ans = null;
285
+ if (value !== null) {
286
+ value = this._standardValueArray(value);
287
+ ans = [];
288
+ }
289
+
290
+ for (var i = 0; i < this.length; ++i) {
291
+ var aimg = this._images[i];
292
+ if (value !== null) {
293
+ aimg.labelProperty(name[i], property[i], value[i]);
294
+ } else {
295
+ ans.push(aimg.labelProperty(name[i], property[i]));
296
+ }
297
+ }
298
+
299
+ if (value === null)
300
+ return ans;
301
+ else
302
+ return this;
303
+ }
304
+
305
+ /**
306
+ * Add ellipsoids to the atom images in this ModelView
307
+ *
308
+ * @param {Object | Object[] | Function} data Data to use for the ellipsoid
309
+ * (see AtomImage.addEllipsoid for details)
310
+ * @param {String | String[] | Function} name Name of the ellipsoids
311
+ * @param {Object | Object[] | Function} args Arguments for creating the ellipsoids
312
+ */
313
+ addEllipsoids(data, name = 'ellipsoid', args = {}) {
314
+
315
+ data = this._standardValueArray(data);
316
+ name = this._standardValueArray(name);
317
+ args = this._standardValueArray(args);
318
+
319
+ for (var i = 0; i < this.length; ++i) {
320
+ var aimg = this._images[i];
321
+ aimg.addEllipsoid(data[i], name[i], args[i]);
322
+ }
323
+
324
+ return this;
325
+ }
326
+
327
+ /**
328
+ * Remove ellipsoids from the atom images in this ModelView
329
+ *
330
+ * @param {String | String[] | Function} name Name of the ellipsoids to remove
331
+ */
332
+ removeEllipsoids(name = 'ellipsoid') {
333
+
334
+ name = this._standardValueArray(name);
335
+
336
+ for (var i = 0; i < this.length; ++i) {
337
+ var aimg = this._images[i];
338
+ aimg.removeEllipsoid(name[i]);
339
+ }
340
+
341
+ return this;
342
+ }
343
+
344
+ /**
345
+ * Get or set ellipsoids' properties for the atom images in this ModelView
346
+ *
347
+ * @param {String | String[] | Function} name Name of the ellipsoids
348
+ * @param {String | String[] | Function} property Property to get or set
349
+ * @param {Any | Any[] | Function} value If not provided, get. If provided,
350
+ * set this value
351
+ */
352
+ ellipsoidProperties(name = 'ellipsoid', property = 'color', value = null) {
353
+
354
+ name = this._standardValueArray(name);
355
+ property = this._standardValueArray(property);
356
+
357
+ var ans = null;
358
+ if (value !== null) {
359
+ value = this._standardValueArray(value);
360
+ ans = [];
361
+ }
362
+
363
+ for (var i = 0; i < this.length; ++i) {
364
+ var aimg = this._images[i];
365
+ if (value !== null) {
366
+ aimg.ellipsoidProperty(name[i], property[i], value[i]);
367
+ } else {
368
+ ans.push(aimg.ellipsoidProperty(name[i], property[i]));
369
+ }
370
+ }
371
+
372
+ if (value === null)
373
+ return ans;
374
+ else
375
+ return this;
376
+ }
377
+
378
+ }
379
+
380
+ export {
381
+ ModelView
382
+ }