react-cheminfo 0.135.0 → 0.136.0

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 (287) hide show
  1. package/lib/crystal/core/cellAtoms.d.ts +43 -0
  2. package/lib/crystal/core/cellAtoms.d.ts.map +1 -0
  3. package/lib/crystal/core/cellAtoms.js +70 -0
  4. package/lib/crystal/core/cellAtoms.js.map +1 -0
  5. package/lib/crystal/core/index.d.ts +2 -0
  6. package/lib/crystal/core/index.d.ts.map +1 -1
  7. package/lib/crystal/core/index.js +1 -0
  8. package/lib/crystal/core/index.js.map +1 -1
  9. package/lib/crystal/core/molecules.d.ts.map +1 -1
  10. package/lib/crystal/core/molecules.js +6 -40
  11. package/lib/crystal/core/molecules.js.map +1 -1
  12. package/lib/crystal3d/core/cellClip.d.ts +46 -0
  13. package/lib/crystal3d/core/cellClip.d.ts.map +1 -0
  14. package/lib/crystal3d/core/cellClip.js +198 -0
  15. package/lib/crystal3d/core/cellClip.js.map +1 -0
  16. package/lib/crystal3d/core/cellGeometry.d.ts +63 -0
  17. package/lib/crystal3d/core/cellGeometry.d.ts.map +1 -0
  18. package/lib/crystal3d/core/cellGeometry.js +153 -0
  19. package/lib/crystal3d/core/cellGeometry.js.map +1 -0
  20. package/lib/crystal3d/core/cellStack.d.ts +51 -0
  21. package/lib/crystal3d/core/cellStack.d.ts.map +1 -0
  22. package/lib/crystal3d/core/cellStack.js +79 -0
  23. package/lib/crystal3d/core/cellStack.js.map +1 -0
  24. package/lib/crystal3d/core/crystalView.d.ts +71 -0
  25. package/lib/crystal3d/core/crystalView.d.ts.map +1 -0
  26. package/lib/crystal3d/core/crystalView.js +48 -0
  27. package/lib/crystal3d/core/crystalView.js.map +1 -0
  28. package/lib/crystal3d/core/drawingSet.d.ts +31 -0
  29. package/lib/crystal3d/core/drawingSet.d.ts.map +1 -0
  30. package/lib/crystal3d/core/drawingSet.js +63 -0
  31. package/lib/crystal3d/core/drawingSet.js.map +1 -0
  32. package/lib/crystal3d/core/elementDrawing.d.ts +50 -0
  33. package/lib/crystal3d/core/elementDrawing.d.ts.map +1 -0
  34. package/lib/crystal3d/core/elementDrawing.js +100 -0
  35. package/lib/crystal3d/core/elementDrawing.js.map +1 -0
  36. package/lib/crystal3d/core/elementExtent.d.ts +42 -0
  37. package/lib/crystal3d/core/elementExtent.d.ts.map +1 -0
  38. package/lib/crystal3d/core/elementExtent.js +56 -0
  39. package/lib/crystal3d/core/elementExtent.js.map +1 -0
  40. package/lib/crystal3d/core/elementLabels.d.ts +31 -0
  41. package/lib/crystal3d/core/elementLabels.d.ts.map +1 -0
  42. package/lib/crystal3d/core/elementLabels.js +99 -0
  43. package/lib/crystal3d/core/elementLabels.js.map +1 -0
  44. package/lib/crystal3d/core/elementShapes.d.ts +43 -0
  45. package/lib/crystal3d/core/elementShapes.d.ts.map +1 -0
  46. package/lib/crystal3d/core/elementShapes.js +115 -0
  47. package/lib/crystal3d/core/elementShapes.js.map +1 -0
  48. package/lib/crystal3d/core/frame.d.ts +36 -0
  49. package/lib/crystal3d/core/frame.d.ts.map +1 -0
  50. package/lib/crystal3d/core/frame.js +35 -0
  51. package/lib/crystal3d/core/frame.js.map +1 -0
  52. package/lib/crystal3d/core/framing.d.ts +41 -0
  53. package/lib/crystal3d/core/framing.d.ts.map +1 -0
  54. package/lib/crystal3d/core/framing.js +79 -0
  55. package/lib/crystal3d/core/framing.js.map +1 -0
  56. package/lib/crystal3d/core/index.d.ts +42 -0
  57. package/lib/crystal3d/core/index.d.ts.map +1 -0
  58. package/lib/crystal3d/core/index.js +29 -0
  59. package/lib/crystal3d/core/index.js.map +1 -0
  60. package/lib/crystal3d/core/labelText.d.ts +19 -0
  61. package/lib/crystal3d/core/labelText.d.ts.map +1 -0
  62. package/lib/crystal3d/core/labelText.js +42 -0
  63. package/lib/crystal3d/core/labelText.js.map +1 -0
  64. package/lib/crystal3d/core/latticeBasis.d.ts +49 -0
  65. package/lib/crystal3d/core/latticeBasis.d.ts.map +1 -0
  66. package/lib/crystal3d/core/latticeBasis.js +89 -0
  67. package/lib/crystal3d/core/latticeBasis.js.map +1 -0
  68. package/lib/crystal3d/core/mat3.d.ts +68 -0
  69. package/lib/crystal3d/core/mat3.d.ts.map +1 -0
  70. package/lib/crystal3d/core/mat3.js +146 -0
  71. package/lib/crystal3d/core/mat3.js.map +1 -0
  72. package/lib/crystal3d/core/operationMatrix.d.ts +44 -0
  73. package/lib/crystal3d/core/operationMatrix.d.ts.map +1 -0
  74. package/lib/crystal3d/core/operationMatrix.js +172 -0
  75. package/lib/crystal3d/core/operationMatrix.js.map +1 -0
  76. package/lib/crystal3d/core/orientation.d.ts +62 -0
  77. package/lib/crystal3d/core/orientation.d.ts.map +1 -0
  78. package/lib/crystal3d/core/orientation.js +166 -0
  79. package/lib/crystal3d/core/orientation.js.map +1 -0
  80. package/lib/crystal3d/core/palette.d.ts +24 -0
  81. package/lib/crystal3d/core/palette.d.ts.map +1 -0
  82. package/lib/crystal3d/core/palette.js +34 -0
  83. package/lib/crystal3d/core/palette.js.map +1 -0
  84. package/lib/crystal3d/core/primitives.d.ts +133 -0
  85. package/lib/crystal3d/core/primitives.d.ts.map +1 -0
  86. package/lib/crystal3d/core/primitives.js +80 -0
  87. package/lib/crystal3d/core/primitives.js.map +1 -0
  88. package/lib/crystal3d/core/structureText.d.ts +20 -0
  89. package/lib/crystal3d/core/structureText.d.ts.map +1 -0
  90. package/lib/crystal3d/core/structureText.js +37 -0
  91. package/lib/crystal3d/core/structureText.js.map +1 -0
  92. package/lib/crystal3d/core/supercell.d.ts +20 -0
  93. package/lib/crystal3d/core/supercell.d.ts.map +1 -0
  94. package/lib/crystal3d/core/supercell.js +35 -0
  95. package/lib/crystal3d/core/supercell.js.map +1 -0
  96. package/lib/crystal3d/core/types.d.ts +166 -0
  97. package/lib/crystal3d/core/types.d.ts.map +1 -0
  98. package/lib/crystal3d/core/types.js +10 -0
  99. package/lib/crystal3d/core/types.js.map +1 -0
  100. package/lib/crystal3d/core/vec3.d.ts +75 -0
  101. package/lib/crystal3d/core/vec3.d.ts.map +1 -0
  102. package/lib/crystal3d/core/vec3.js +119 -0
  103. package/lib/crystal3d/core/vec3.js.map +1 -0
  104. package/lib/crystal3d/ui/CrystalCanvas3D.d.ts +52 -0
  105. package/lib/crystal3d/ui/CrystalCanvas3D.d.ts.map +1 -0
  106. package/lib/crystal3d/ui/CrystalCanvas3D.js +242 -0
  107. package/lib/crystal3d/ui/CrystalCanvas3D.js.map +1 -0
  108. package/lib/crystal3d/ui/CrystalStructureView.d.ts +17 -0
  109. package/lib/crystal3d/ui/CrystalStructureView.d.ts.map +1 -0
  110. package/lib/crystal3d/ui/CrystalStructureView.js +76 -0
  111. package/lib/crystal3d/ui/CrystalStructureView.js.map +1 -0
  112. package/lib/crystal3d/ui/CrystalStructureViewer.d.ts +22 -0
  113. package/lib/crystal3d/ui/CrystalStructureViewer.d.ts.map +1 -0
  114. package/lib/crystal3d/ui/CrystalStructureViewer.js +29 -0
  115. package/lib/crystal3d/ui/CrystalStructureViewer.js.map +1 -0
  116. package/lib/crystal3d/ui/CrystalViewControls.d.ts +25 -0
  117. package/lib/crystal3d/ui/CrystalViewControls.d.ts.map +1 -0
  118. package/lib/crystal3d/ui/CrystalViewControls.js +22 -0
  119. package/lib/crystal3d/ui/CrystalViewControls.js.map +1 -0
  120. package/lib/crystal3d/ui/CrystalViewer3D.d.ts +72 -0
  121. package/lib/crystal3d/ui/CrystalViewer3D.d.ts.map +1 -0
  122. package/lib/crystal3d/ui/CrystalViewer3D.js +70 -0
  123. package/lib/crystal3d/ui/CrystalViewer3D.js.map +1 -0
  124. package/lib/crystal3d/ui/crystalScene.d.ts +91 -0
  125. package/lib/crystal3d/ui/crystalScene.d.ts.map +1 -0
  126. package/lib/crystal3d/ui/crystalScene.js +10 -0
  127. package/lib/crystal3d/ui/crystalScene.js.map +1 -0
  128. package/lib/crystal3d/ui/crystalStructureViewerProps.d.ts +52 -0
  129. package/lib/crystal3d/ui/crystalStructureViewerProps.d.ts.map +1 -0
  130. package/lib/crystal3d/ui/crystalStructureViewerProps.js +2 -0
  131. package/lib/crystal3d/ui/crystalStructureViewerProps.js.map +1 -0
  132. package/lib/crystal3d/ui/index.d.ts +16 -0
  133. package/lib/crystal3d/ui/index.d.ts.map +1 -0
  134. package/lib/crystal3d/ui/index.js +12 -0
  135. package/lib/crystal3d/ui/index.js.map +1 -0
  136. package/lib/crystal3d/ui/useCrystalCaption.d.ts +14 -0
  137. package/lib/crystal3d/ui/useCrystalCaption.d.ts.map +1 -0
  138. package/lib/crystal3d/ui/useCrystalCaption.js +32 -0
  139. package/lib/crystal3d/ui/useCrystalCaption.js.map +1 -0
  140. package/lib/crystal3d/viewer/animate.d.ts +49 -0
  141. package/lib/crystal3d/viewer/animate.d.ts.map +1 -0
  142. package/lib/crystal3d/viewer/animate.js +83 -0
  143. package/lib/crystal3d/viewer/animate.js.map +1 -0
  144. package/lib/crystal3d/viewer/camera.d.ts +44 -0
  145. package/lib/crystal3d/viewer/camera.d.ts.map +1 -0
  146. package/lib/crystal3d/viewer/camera.js +103 -0
  147. package/lib/crystal3d/viewer/camera.js.map +1 -0
  148. package/lib/crystal3d/viewer/index.d.ts +13 -0
  149. package/lib/crystal3d/viewer/index.d.ts.map +1 -0
  150. package/lib/crystal3d/viewer/index.js +8 -0
  151. package/lib/crystal3d/viewer/index.js.map +1 -0
  152. package/lib/crystal3d/viewer/layerRegistry.d.ts +24 -0
  153. package/lib/crystal3d/viewer/layerRegistry.d.ts.map +1 -0
  154. package/lib/crystal3d/viewer/layerRegistry.js +39 -0
  155. package/lib/crystal3d/viewer/layerRegistry.js.map +1 -0
  156. package/lib/crystal3d/viewer/meshPrimitives.d.ts +13 -0
  157. package/lib/crystal3d/viewer/meshPrimitives.d.ts.map +1 -0
  158. package/lib/crystal3d/viewer/meshPrimitives.js +76 -0
  159. package/lib/crystal3d/viewer/meshPrimitives.js.map +1 -0
  160. package/lib/crystal3d/viewer/plugin.d.ts +133 -0
  161. package/lib/crystal3d/viewer/plugin.d.ts.map +1 -0
  162. package/lib/crystal3d/viewer/plugin.js +181 -0
  163. package/lib/crystal3d/viewer/plugin.js.map +1 -0
  164. package/lib/crystal3d/viewer/renderCell.d.ts +43 -0
  165. package/lib/crystal3d/viewer/renderCell.d.ts.map +1 -0
  166. package/lib/crystal3d/viewer/renderCell.js +53 -0
  167. package/lib/crystal3d/viewer/renderCell.js.map +1 -0
  168. package/lib/crystal3d/viewer/renderElements.d.ts +54 -0
  169. package/lib/crystal3d/viewer/renderElements.d.ts.map +1 -0
  170. package/lib/crystal3d/viewer/renderElements.js +77 -0
  171. package/lib/crystal3d/viewer/renderElements.js.map +1 -0
  172. package/lib/crystal3d/viewer/renderStructure.d.ts +55 -0
  173. package/lib/crystal3d/viewer/renderStructure.d.ts.map +1 -0
  174. package/lib/crystal3d/viewer/renderStructure.js +103 -0
  175. package/lib/crystal3d/viewer/renderStructure.js.map +1 -0
  176. package/lib/crystal3d/viewer/sceneParts.d.ts +32 -0
  177. package/lib/crystal3d/viewer/sceneParts.d.ts.map +1 -0
  178. package/lib/crystal3d/viewer/sceneParts.js +77 -0
  179. package/lib/crystal3d/viewer/sceneParts.js.map +1 -0
  180. package/lib/crystal3d/viewer/shapeLayer.d.ts +41 -0
  181. package/lib/crystal3d/viewer/shapeLayer.d.ts.map +1 -0
  182. package/lib/crystal3d/viewer/shapeLayer.js +57 -0
  183. package/lib/crystal3d/viewer/shapeLayer.js.map +1 -0
  184. package/lib/crystal3d/viewer/textLayer.d.ts +21 -0
  185. package/lib/crystal3d/viewer/textLayer.d.ts.map +1 -0
  186. package/lib/crystal3d/viewer/textLayer.js +59 -0
  187. package/lib/crystal3d/viewer/textLayer.js.map +1 -0
  188. package/lib/crystal3d/viewer/viewer.d.ts +110 -0
  189. package/lib/crystal3d/viewer/viewer.d.ts.map +1 -0
  190. package/lib/crystal3d/viewer/viewer.js +157 -0
  191. package/lib/crystal3d/viewer/viewer.js.map +1 -0
  192. package/lib/crystal3d.d.ts +12 -0
  193. package/lib/crystal3d.d.ts.map +1 -0
  194. package/lib/crystal3d.js +12 -0
  195. package/lib/crystal3d.js.map +1 -0
  196. package/lib/i18n/core/chromeCatalog.d.ts +1 -1
  197. package/lib/i18n/core/chromeCatalog.d.ts.map +1 -1
  198. package/lib/locales/de.json +19 -1
  199. package/lib/locales/en.json +19 -1
  200. package/lib/locales/es.json +19 -1
  201. package/lib/locales/fr.json +19 -1
  202. package/lib/locales/it.json +19 -1
  203. package/lib/molecule3d/ui/MoleculeCanvas3D.d.ts.map +1 -1
  204. package/lib/molecule3d/ui/MoleculeCanvas3D.js +16 -27
  205. package/lib/molecule3d/ui/MoleculeCanvas3D.js.map +1 -1
  206. package/lib/molstar/bonds/bondRule.d.ts +16 -0
  207. package/lib/molstar/bonds/bondRule.d.ts.map +1 -0
  208. package/lib/molstar/bonds/bondRule.js +23 -0
  209. package/lib/molstar/bonds/bondRule.js.map +1 -0
  210. package/lib/molstar/bonds/index.d.ts +9 -0
  211. package/lib/molstar/bonds/index.d.ts.map +1 -0
  212. package/lib/molstar/bonds/index.js +9 -0
  213. package/lib/molstar/bonds/index.js.map +1 -0
  214. package/lib/molstar/ui/MolstarToolbar.d.ts +47 -0
  215. package/lib/molstar/ui/MolstarToolbar.d.ts.map +1 -0
  216. package/lib/molstar/ui/MolstarToolbar.js +28 -0
  217. package/lib/molstar/ui/MolstarToolbar.js.map +1 -0
  218. package/lib/molstar/ui/index.d.ts +10 -0
  219. package/lib/molstar/ui/index.d.ts.map +1 -0
  220. package/lib/molstar/ui/index.js +8 -0
  221. package/lib/molstar/ui/index.js.map +1 -0
  222. package/lib/molstar/ui/useMolstarTools.d.ts +63 -0
  223. package/lib/molstar/ui/useMolstarTools.d.ts.map +1 -0
  224. package/lib/molstar/ui/useMolstarTools.js +36 -0
  225. package/lib/molstar/ui/useMolstarTools.js.map +1 -0
  226. package/package.json +2 -1
  227. package/src/crystal/core/cellAtoms.ts +106 -0
  228. package/src/crystal/core/index.ts +2 -0
  229. package/src/crystal/core/molecules.ts +7 -44
  230. package/src/crystal3d/core/cellClip.ts +229 -0
  231. package/src/crystal3d/core/cellGeometry.ts +193 -0
  232. package/src/crystal3d/core/cellStack.ts +87 -0
  233. package/src/crystal3d/core/crystalView.ts +114 -0
  234. package/src/crystal3d/core/drawingSet.ts +71 -0
  235. package/src/crystal3d/core/elementDrawing.ts +147 -0
  236. package/src/crystal3d/core/elementExtent.ts +81 -0
  237. package/src/crystal3d/core/elementLabels.ts +130 -0
  238. package/src/crystal3d/core/elementShapes.ts +141 -0
  239. package/src/crystal3d/core/frame.ts +56 -0
  240. package/src/crystal3d/core/framing.ts +101 -0
  241. package/src/crystal3d/core/index.ts +132 -0
  242. package/src/crystal3d/core/labelText.ts +44 -0
  243. package/src/crystal3d/core/latticeBasis.ts +110 -0
  244. package/src/crystal3d/core/mat3.ts +163 -0
  245. package/src/crystal3d/core/operationMatrix.ts +255 -0
  246. package/src/crystal3d/core/orientation.ts +229 -0
  247. package/src/crystal3d/core/palette.ts +43 -0
  248. package/src/crystal3d/core/primitives.ts +183 -0
  249. package/src/crystal3d/core/structureText.ts +43 -0
  250. package/src/crystal3d/core/supercell.ts +40 -0
  251. package/src/crystal3d/core/types.ts +186 -0
  252. package/src/crystal3d/core/vec3.ts +127 -0
  253. package/src/crystal3d/ui/CrystalCanvas3D.tsx +384 -0
  254. package/src/crystal3d/ui/CrystalStructureView.tsx +148 -0
  255. package/src/crystal3d/ui/CrystalStructureViewer.tsx +39 -0
  256. package/src/crystal3d/ui/CrystalViewControls.tsx +100 -0
  257. package/src/crystal3d/ui/CrystalViewer3D.tsx +183 -0
  258. package/src/crystal3d/ui/crystalScene.ts +100 -0
  259. package/src/crystal3d/ui/crystalStructureViewerProps.ts +54 -0
  260. package/src/crystal3d/ui/index.ts +20 -0
  261. package/src/crystal3d/ui/useCrystalCaption.ts +38 -0
  262. package/src/crystal3d/viewer/animate.ts +113 -0
  263. package/src/crystal3d/viewer/camera.ts +139 -0
  264. package/src/crystal3d/viewer/index.ts +13 -0
  265. package/src/crystal3d/viewer/layerRegistry.ts +47 -0
  266. package/src/crystal3d/viewer/meshPrimitives.ts +102 -0
  267. package/src/crystal3d/viewer/plugin.ts +258 -0
  268. package/src/crystal3d/viewer/renderCell.ts +98 -0
  269. package/src/crystal3d/viewer/renderElements.ts +121 -0
  270. package/src/crystal3d/viewer/renderStructure.ts +144 -0
  271. package/src/crystal3d/viewer/sceneParts.ts +119 -0
  272. package/src/crystal3d/viewer/shapeLayer.ts +108 -0
  273. package/src/crystal3d/viewer/textLayer.ts +85 -0
  274. package/src/crystal3d/viewer/viewer.ts +207 -0
  275. package/src/crystal3d.ts +12 -0
  276. package/src/locales/de.json +19 -1
  277. package/src/locales/en.json +19 -1
  278. package/src/locales/es.json +19 -1
  279. package/src/locales/fr.json +19 -1
  280. package/src/locales/it.json +19 -1
  281. package/src/molecule3d/ui/MoleculeCanvas3D.tsx +45 -71
  282. package/src/molstar/bonds/bondRule.ts +34 -0
  283. package/src/molstar/bonds/index.ts +9 -0
  284. package/src/molstar/ui/MolstarToolbar.tsx +103 -0
  285. package/src/molstar/ui/index.ts +14 -0
  286. package/src/molstar/ui/useMolstarTools.ts +98 -0
  287. package/styles/chrome.css +90 -0
@@ -0,0 +1,43 @@
1
+ /**
2
+ * The atoms as a file molstar can read.
3
+ *
4
+ * XYZ, and only XYZ: the page has already applied every operation, so the
5
+ * viewer needs a format that carries elements and Cartesian coordinates and
6
+ * nothing else. Handing molstar a CIF would invite it to expand the symmetry a
7
+ * second time, which is the one thing a crystal view must not let it do.
8
+ */
9
+
10
+ import type { ViewerAtom } from './types.ts';
11
+
12
+ /**
13
+ * Write the atoms as XYZ.
14
+ * @param atoms - The whole scene, Cartesian ångström.
15
+ * @param comment - The second line of the file, which molstar keeps as the
16
+ * model's comment.
17
+ * @returns The file text, ending in a newline.
18
+ * @throws {Error} When there are no atoms: molstar reads a count of zero as the end of
19
+ * the file and returns a model with no atoms rather than an error.
20
+ */
21
+ export function toXyzText(
22
+ atoms: readonly ViewerAtom[],
23
+ comment = 'crystal',
24
+ ): string {
25
+ if (atoms.length === 0) {
26
+ throw new Error('an XYZ file needs at least one atom.');
27
+ }
28
+ const lines: string[] = [String(atoms.length), comment];
29
+ for (const atom of atoms) {
30
+ const [x, y, z] = atom.position;
31
+ lines.push(`${atom.element} ${format(x)} ${format(y)} ${format(z)}`);
32
+ }
33
+ return `${lines.join('\n')}\n`;
34
+ }
35
+
36
+ /**
37
+ * Six decimals is a thousandth of the shortest bond, and reads as a number.
38
+ * @param value - A coordinate, ångström.
39
+ * @returns It with six decimals, never `-0.000000`.
40
+ */
41
+ function format(value: number): string {
42
+ return (value === 0 ? 0 : value).toFixed(6);
43
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * The bridge from an expanded cell to the atom list the scene draws.
3
+ *
4
+ * The site applies its own operations — molstar's space-group expansion is
5
+ * unusable here, since it neither wraps into the cell nor deduplicates a special
6
+ * position, and it falls back to `P 1` without warning on settings this site
7
+ * ships. So the atoms arrive already expanded, and all that is left is to repeat
8
+ * them over the lattice.
9
+ */
10
+
11
+ import { cellAxes, latticeShifts } from './cellGeometry.ts';
12
+ import type { CellRepeat, UnitCell, ViewerAtom } from './types.ts';
13
+ import { addVectors } from './vec3.ts';
14
+
15
+ /**
16
+ * One cell's atoms, repeated over an n×n×n stack.
17
+ * @param atoms - The contents of one cell, Cartesian ångström.
18
+ * @param cell - The six cell parameters.
19
+ * @param repeat - Cells along a, b and c; anything below 1 counts as 1.
20
+ * @returns The atoms of every cell, the original ones first.
21
+ * @throws {RangeError} When the three angles describe no cell.
22
+ */
23
+ export function supercellAtoms(
24
+ atoms: readonly ViewerAtom[],
25
+ cell: UnitCell,
26
+ repeat: CellRepeat = [1, 1, 1],
27
+ ): ViewerAtom[] {
28
+ const shifts = latticeShifts(cellAxes(cell), repeat);
29
+ if (shifts.length === 1) return [...atoms];
30
+ const expanded: ViewerAtom[] = [];
31
+ for (const shift of shifts) {
32
+ for (const atom of atoms) {
33
+ expanded.push({
34
+ element: atom.element,
35
+ position: addVectors(atom.position, shift),
36
+ });
37
+ }
38
+ }
39
+ return expanded;
40
+ }
@@ -0,0 +1,186 @@
1
+ /**
2
+ * What a page hands the crystal viewer, and nothing molstar knows about.
3
+ *
4
+ * Every coordinate here is **Cartesian ångström**. The page has already applied
5
+ * the space group's operations by the time it draws: molstar's own expansion
6
+ * silently falls back to `P 1` on several common settings, so nothing in
7
+ * `viewer/` ever asks it to expand anything.
8
+ */
9
+
10
+ import type { Vec3 } from './vec3.ts';
11
+
12
+ export type { UnitCell } from '../../crystal/core/symmetry/lattice.ts';
13
+
14
+ /** A point or a direction in Cartesian ångström. */
15
+ export type Point3 = Vec3;
16
+
17
+ /** How many cells to draw along a, b and c. */
18
+ export type CellRepeat = readonly [number, number, number];
19
+
20
+ /** One atom of the scene, already in its final place. */
21
+ export interface ViewerAtom {
22
+ /** Element symbol, as `Na`, `Cl`, `C`. */
23
+ readonly element: string;
24
+ /** Where it sits, Cartesian ångström. */
25
+ readonly position: Point3;
26
+ }
27
+
28
+ /** What every drawn symmetry element carries. */
29
+ export interface DrawingBase {
30
+ /** Stable across redraws, so one element can be removed on its own. */
31
+ readonly id: string;
32
+ /** What the pointer reads: `C3`, `2₁ along [001]`, `σv`, `n glide`. */
33
+ readonly label: string;
34
+ /**
35
+ * The short form written on the element in the scene — `m`, `3`, `2₁`, `-1`.
36
+ * Forty rods each carrying their full name is a wall of text nobody reads,
37
+ * and the list beside the view says the rest.
38
+ * @default the label
39
+ */
40
+ readonly badge?: string;
41
+ /**
42
+ * Its colour, `#rrggbb`. The kind's own convention is used when absent.
43
+ * @default the convention in `palette.ts`
44
+ */
45
+ readonly colour?: string;
46
+ }
47
+
48
+ /** A proper rotation axis: a rod through `point` along `direction`. */
49
+ export interface RotationDrawing extends DrawingBase {
50
+ readonly kind: 'rotation';
51
+ /** A point the axis passes through. */
52
+ readonly point: Point3;
53
+ /** The axis direction; need not be normalised. */
54
+ readonly direction: Point3;
55
+ /** Total rod length, ångström, centred on `point`. */
56
+ readonly length: number;
57
+ /** n of the n-fold, which is what gets labelled. */
58
+ readonly order: number;
59
+ }
60
+
61
+ /** A screw axis: the rod, plus an arrow as long as the pitch beside it. */
62
+ export interface ScrewDrawing extends DrawingBase {
63
+ readonly kind: 'screw';
64
+ readonly point: Point3;
65
+ readonly direction: Point3;
66
+ readonly length: number;
67
+ readonly order: number;
68
+ /** The intrinsic translation of one turn, ångström along `direction`. */
69
+ readonly pitch: number;
70
+ }
71
+
72
+ /** A rotoinversion: a dashed rod, with a ball at the inversion point on it. */
73
+ export interface RotoinversionDrawing extends DrawingBase {
74
+ readonly kind: 'rotoinversion';
75
+ /** The inversion point, which is where the ball sits. */
76
+ readonly point: Point3;
77
+ readonly direction: Point3;
78
+ readonly length: number;
79
+ readonly order: number;
80
+ }
81
+
82
+ /** A mirror plane: a translucent face through `point`. */
83
+ export interface MirrorDrawing extends DrawingBase {
84
+ readonly kind: 'mirror';
85
+ readonly point: Point3;
86
+ /** The plane normal; need not be normalised. */
87
+ readonly normal: Point3;
88
+ /** Side of the drawn square, ångström, when there is no outline. */
89
+ readonly size: number;
90
+ /**
91
+ * The corners the plane is drawn between, in order around it. A crystal
92
+ * hands over the polygon where the plane cuts the cell, which says where it
93
+ * sits far better than a square floating in the middle of one does.
94
+ * @default a square of `size`, spanned by the frame of the normal
95
+ */
96
+ readonly outline?: readonly Point3[];
97
+ }
98
+
99
+ /** A glide plane: the face, plus an arrow in it along the glide vector. */
100
+ export interface GlideDrawing extends DrawingBase {
101
+ readonly kind: 'glide';
102
+ readonly point: Point3;
103
+ readonly normal: Point3;
104
+ readonly size: number;
105
+ /** The glide vector itself, ångström, lying in the plane. */
106
+ readonly glide: Point3;
107
+ /**
108
+ * The corners the plane is drawn between, in order around it.
109
+ * @default a square of `size`, spanned by the frame of the normal
110
+ */
111
+ readonly outline?: readonly Point3[];
112
+ }
113
+
114
+ /** An inversion centre: a small ball. */
115
+ export interface InversionDrawing extends DrawingBase {
116
+ readonly kind: 'inversion';
117
+ readonly point: Point3;
118
+ }
119
+
120
+ /** One symmetry element as the viewer draws it. */
121
+ export type SymmetryDrawing =
122
+ | RotationDrawing
123
+ | ScrewDrawing
124
+ | RotoinversionDrawing
125
+ | MirrorDrawing
126
+ | GlideDrawing
127
+ | InversionDrawing;
128
+
129
+ /** Which of the six kinds a drawing is. */
130
+ export type SymmetryDrawingKind = SymmetryDrawing['kind'];
131
+
132
+ /**
133
+ * An operation to apply to the structure, so a student watches it map onto
134
+ * itself.
135
+ *
136
+ * A proper rotation and a screw have a rigid path from the identity and are
137
+ * turned through it. The improper three have none, and say so by passing
138
+ * through the plane or the centre rather than around it.
139
+ */
140
+ export type ViewerOperation =
141
+ | {
142
+ readonly kind: 'rotation';
143
+ readonly axis: Point3;
144
+ readonly origin: Point3;
145
+ /** n of the n-fold. */
146
+ readonly order: number;
147
+ /**
148
+ * k of `Cₙᵏ`.
149
+ * @default 1
150
+ */
151
+ readonly power?: number;
152
+ }
153
+ | {
154
+ readonly kind: 'screw';
155
+ readonly axis: Point3;
156
+ readonly origin: Point3;
157
+ readonly order: number;
158
+ /** @default 1 */
159
+ readonly power?: number;
160
+ /** The translation of one application, Cartesian ångström. */
161
+ readonly translation: Point3;
162
+ }
163
+ | {
164
+ readonly kind: 'improperRotation';
165
+ readonly axis: Point3;
166
+ readonly origin: Point3;
167
+ readonly order: number;
168
+ /** @default 1 */
169
+ readonly power?: number;
170
+ }
171
+ | { readonly kind: 'mirror'; readonly normal: Point3; readonly point: Point3 }
172
+ | {
173
+ readonly kind: 'glide';
174
+ readonly normal: Point3;
175
+ readonly point: Point3;
176
+ readonly translation: Point3;
177
+ }
178
+ | { readonly kind: 'inversion'; readonly centre: Point3 }
179
+ | {
180
+ readonly kind: 'rotoinversion';
181
+ readonly axis: Point3;
182
+ readonly origin: Point3;
183
+ readonly order: number;
184
+ /** @default 1 */
185
+ readonly power?: number;
186
+ };
@@ -0,0 +1,127 @@
1
+ /** A point or a direction in Cartesian space, in ångström. */
2
+ export type Vec3 = readonly [number, number, number];
3
+
4
+ /**
5
+ * a + b.
6
+ * @param a - The first vector.
7
+ * @param b - The vector added to it.
8
+ * @returns The sum.
9
+ */
10
+ export function addVectors(a: Vec3, b: Vec3): Vec3 {
11
+ return [a[0] + b[0], a[1] + b[1], a[2] + b[2]];
12
+ }
13
+
14
+ /**
15
+ * a − b.
16
+ * @param a - The vector subtracted from.
17
+ * @param b - The vector subtracted.
18
+ * @returns The difference.
19
+ */
20
+ export function subtractVectors(a: Vec3, b: Vec3): Vec3 {
21
+ return [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
22
+ }
23
+
24
+ /**
25
+ * a · factor.
26
+ * @param a - The vector.
27
+ * @param factor - The scalar it is multiplied by.
28
+ * @returns The scaled vector.
29
+ */
30
+ export function scaleVector(a: Vec3, factor: number): Vec3 {
31
+ return [a[0] * factor, a[1] * factor, a[2] * factor];
32
+ }
33
+
34
+ /**
35
+ * a · b.
36
+ * @param a - The first vector.
37
+ * @param b - The second vector.
38
+ * @returns The dot product.
39
+ */
40
+ export function dotProduct(a: Vec3, b: Vec3): number {
41
+ return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
42
+ }
43
+
44
+ /**
45
+ * a × b.
46
+ * @param a - The first vector.
47
+ * @param b - The second vector.
48
+ * @returns The cross product, right-handed.
49
+ */
50
+ export function crossProduct(a: Vec3, b: Vec3): Vec3 {
51
+ return [
52
+ a[1] * b[2] - a[2] * b[1],
53
+ a[2] * b[0] - a[0] * b[2],
54
+ a[0] * b[1] - a[1] * b[0],
55
+ ];
56
+ }
57
+
58
+ /**
59
+ * |a|.
60
+ * @param a - The vector.
61
+ * @returns Its Euclidean length.
62
+ */
63
+ export function vectorNorm(a: Vec3): number {
64
+ return Math.hypot(a[0], a[1], a[2]);
65
+ }
66
+
67
+ /**
68
+ * |a − b|.
69
+ * @param a - The first point.
70
+ * @param b - The second point.
71
+ * @returns The distance between them.
72
+ */
73
+ export function vectorDistance(a: Vec3, b: Vec3): number {
74
+ const dx = a[0] - b[0];
75
+ const dy = a[1] - b[1];
76
+ const dz = a[2] - b[2];
77
+ return Math.hypot(dx, dy, dz);
78
+ }
79
+
80
+ /**
81
+ * a / |a|.
82
+ * @param a - The vector.
83
+ * @param minimumNorm - The length under which `a` counts as zero.
84
+ * @returns The unit vector along `a`.
85
+ * @throws {RangeError} When `a` is shorter than `minimumNorm`, which has no direction.
86
+ */
87
+ export function normalizeVector(a: Vec3, minimumNorm = 1e-9): Vec3 {
88
+ const norm = vectorNorm(a);
89
+ if (norm < minimumNorm) {
90
+ throw new RangeError('a vector of zero length has no direction');
91
+ }
92
+ return [a[0] / norm, a[1] / norm, a[2] / norm];
93
+ }
94
+
95
+ /**
96
+ * Whether two unit vectors describe the same **axis**, which is a direction
97
+ * without a sense: `û` and `−û` are one axis.
98
+ * @param a - The first unit vector.
99
+ * @param b - The second unit vector.
100
+ * @param parallel - `|û · v̂|` above which they are the same; `1 - 1e-4` by default.
101
+ * @returns Whether they lie on one axis.
102
+ */
103
+ export function sameAxis(a: Vec3, b: Vec3, parallel = 1 - 1e-4): boolean {
104
+ return Math.abs(dotProduct(a, b)) >= parallel;
105
+ }
106
+
107
+ /**
108
+ * Whether every point lies on one straight line through the first two.
109
+ * @param points - The points, Cartesian ångström.
110
+ * @param tolerance - The distance, in ångström, a point may stray from the line.
111
+ * @returns Whether they are collinear; true for fewer than three points.
112
+ */
113
+ export function collinear(points: readonly Vec3[], tolerance = 1e-6): boolean {
114
+ if (points.length < 3) return true;
115
+ const origin = points[0] as Vec3;
116
+ let direction: Vec3 | null = null;
117
+ for (let i = 1; i < points.length; i++) {
118
+ const offset = subtractVectors(points[i] as Vec3, origin);
119
+ if (vectorNorm(offset) < tolerance) continue;
120
+ if (direction === null) {
121
+ direction = normalizeVector(offset);
122
+ continue;
123
+ }
124
+ if (vectorNorm(crossProduct(direction, offset)) > tolerance) return false;
125
+ }
126
+ return true;
127
+ }