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,47 @@
1
+ /**
2
+ * Which drawn layers a canvas is carrying.
3
+ *
4
+ * A layer lives outside the state tree, keyed by name on the plugin, the way
5
+ * `lcao`'s lobes and nodal surfaces do: it is rebuilt whole on every toggle, and
6
+ * the state tree has nothing to add to that. Keeping the register here lets the
7
+ * mesh layers and the text layers share one place to be removed from.
8
+ */
9
+
10
+ import type { PluginContext } from 'molstar/lib/mol-plugin/context.js';
11
+ import type { Representation } from 'molstar/lib/mol-repr/representation.js';
12
+
13
+ /**
14
+ * Remember a layer, so a later call can take it off again.
15
+ * @param plugin - The molstar context.
16
+ * @param key - Which layer.
17
+ * @param representation - What was added to the canvas.
18
+ */
19
+ export function storeLayer(
20
+ plugin: PluginContext,
21
+ key: string,
22
+ representation: Representation.Any,
23
+ ): void {
24
+ let layers = registry.get(plugin);
25
+ if (layers === undefined) {
26
+ layers = new Map<string, Representation.Any>();
27
+ registry.set(plugin, layers);
28
+ }
29
+ layers.set(key, representation);
30
+ }
31
+
32
+ /**
33
+ * Remove one layer, leaving every other in place.
34
+ * @param plugin - The molstar context.
35
+ * @param key - Which layer; an unknown one is ignored.
36
+ */
37
+ export function clearShapeLayer(plugin: PluginContext, key: string): void {
38
+ const layers = registry.get(plugin);
39
+ const previous = layers?.get(key);
40
+ if (layers === undefined || previous === undefined) return;
41
+ layers.delete(key);
42
+ plugin.canvas3d?.remove(previous);
43
+ plugin.canvas3d?.commit();
44
+ }
45
+
46
+ /** Every layer drawn on a plugin, so each can be removed on its own. */
47
+ const registry = new WeakMap<PluginContext, Map<string, Representation.Any>>();
@@ -0,0 +1,102 @@
1
+ /**
2
+ * The one place a shape worked out in `primitives.ts` becomes molstar geometry.
3
+ */
4
+
5
+ import {
6
+ addFixedCountDashedCylinder,
7
+ addSimpleCylinder,
8
+ } from 'molstar/lib/mol-geo/geometry/mesh/builder/cylinder.js';
9
+ import { addPlane } from 'molstar/lib/mol-geo/geometry/mesh/builder/plane.js';
10
+ import { addSphere } from 'molstar/lib/mol-geo/geometry/mesh/builder/sphere.js';
11
+ import { MeshBuilder } from 'molstar/lib/mol-geo/geometry/mesh/mesh-builder.js';
12
+ import { Vec3 } from 'molstar/lib/mol-math/linear-algebra.js';
13
+
14
+ import type { MeshPrimitive } from '../core/primitives.ts';
15
+ import type { Point3 } from '../core/types.ts';
16
+
17
+ /**
18
+ * Add one shape to a mesh under construction.
19
+ * @param state - The builder; its `currentGroup` decides which item the
20
+ * triangles belong to, and so what colour and label they take.
21
+ * @param primitive - The shape.
22
+ */
23
+ export function addMeshPrimitive(
24
+ state: MeshBuilder.State,
25
+ primitive: MeshPrimitive,
26
+ ): void {
27
+ switch (primitive.shape) {
28
+ case 'rod': {
29
+ addSimpleCylinder(state, vec(primitive.start), vec(primitive.end), {
30
+ radiusTop: primitive.radius,
31
+ radiusBottom: primitive.radius,
32
+ radialSegments: ROD_SEGMENTS,
33
+ });
34
+ return;
35
+ }
36
+ case 'dashes': {
37
+ addFixedCountDashedCylinder(
38
+ state,
39
+ vec(primitive.start),
40
+ vec(primitive.end),
41
+ 1,
42
+ primitive.segments,
43
+ true,
44
+ {
45
+ radiusTop: primitive.radius,
46
+ radiusBottom: primitive.radius,
47
+ radialSegments: ROD_SEGMENTS,
48
+ },
49
+ );
50
+ return;
51
+ }
52
+ case 'cone': {
53
+ addSimpleCylinder(state, vec(primitive.base), vec(primitive.tip), {
54
+ radiusTop: 0,
55
+ radiusBottom: primitive.radius,
56
+ radialSegments: CONE_SEGMENTS,
57
+ bottomCap: true,
58
+ });
59
+ return;
60
+ }
61
+ case 'sphere': {
62
+ addSphere(state, vec(primitive.centre), primitive.radius, SPHERE_DETAIL);
63
+ return;
64
+ }
65
+ case 'face': {
66
+ // A fan from the first corner; the corners come round the plane in
67
+ // order, so the face is convex and the fan covers it exactly.
68
+ const { points } = primitive;
69
+ const first = points[0];
70
+ if (first === undefined) return;
71
+ for (let index = 1; index + 1 < points.length; index++) {
72
+ const second = points[index];
73
+ const third = points[index + 1];
74
+ if (second === undefined || third === undefined) continue;
75
+ MeshBuilder.addTriangle(state, vec(first), vec(second), vec(third));
76
+ }
77
+ return;
78
+ }
79
+ case 'plate': {
80
+ // The unit plane is ±0.5, so the scale is the side of the square; the
81
+ // third component is the (zero-thickness) normal direction.
82
+ addPlane(
83
+ state,
84
+ vec(primitive.centre),
85
+ vec(primitive.major),
86
+ vec(primitive.minor),
87
+ Vec3.create(primitive.size, primitive.size, 1),
88
+ 1,
89
+ 1,
90
+ );
91
+ }
92
+ // no default
93
+ }
94
+ }
95
+
96
+ function vec(point: Point3): Vec3 {
97
+ return Vec3.create(point[0], point[1], point[2]);
98
+ }
99
+
100
+ const ROD_SEGMENTS = 12;
101
+ const CONE_SEGMENTS = 16;
102
+ const SPHERE_DETAIL = 2;
@@ -0,0 +1,258 @@
1
+ /**
2
+ * Lifecycle of one molstar canvas: created now, disposed whenever, every call
3
+ * queued behind initialisation.
4
+ *
5
+ * The constructor is **synchronous** on purpose. React 19 runs an effect, its
6
+ * cleanup and the effect again on every mount in development, so an `await`ed
7
+ * constructor hands the cleanup nothing to dispose and leaks a WebGL context per
8
+ * mount — browsers drop the oldest after about sixteen, and the viewer silently
9
+ * goes blank. Returning the handle immediately means `dispose()` can always be
10
+ * called, even before initialisation has finished.
11
+ *
12
+ * molstar's own UI is never mounted: every control is the page's.
13
+ */
14
+
15
+ import type { PluginContext } from 'molstar/lib/mol-plugin/context.js';
16
+
17
+ import type { Molecule3DCamera } from '../../molecule3d/core/camera.ts';
18
+ import type { ImageSize } from '../../molecule3d/core/exportImage.ts';
19
+ import type { RenderedFigure } from '../../molecule3d/core/figureFrame.ts';
20
+ import type {
21
+ Measurement,
22
+ MeasurementKind,
23
+ } from '../../molecule3d/core/measurement.ts';
24
+ import type { SceneCapture } from '../../molecule3d/ui/exportMoleculeImage.ts';
25
+ import {
26
+ MeasurementPicker,
27
+ MolstarPlugin,
28
+ captureFigure,
29
+ captureScene,
30
+ clearMeasurements,
31
+ focusPoint,
32
+ renderMeasurements,
33
+ setSpin,
34
+ subscribeHoverLabel,
35
+ } from '../../molstar/core/index.ts';
36
+ import type { ViewOrientation } from '../core/orientation.ts';
37
+ import type { Point3 } from '../core/types.ts';
38
+
39
+ import {
40
+ DEFAULT_CAMERA_DURATION,
41
+ applyCamera,
42
+ readCamera,
43
+ resetCamera,
44
+ } from './camera.ts';
45
+ import { crystalStructure } from './renderStructure.ts';
46
+
47
+ /** Settings fixed for the life of a viewer. */
48
+ export interface CrystalViewerOptions {
49
+ /**
50
+ * Scene background, as `#rrggbb`.
51
+ * @default '#ffffff'
52
+ */
53
+ background?: string;
54
+ /**
55
+ * Called with each measurement once its atoms are picked; measuring is off
56
+ * without it.
57
+ * @default undefined
58
+ */
59
+ onMeasure?: (measurement: Measurement) => void;
60
+ }
61
+
62
+ /** One molstar canvas, and the queue everything drawn on it goes through. */
63
+ export class CrystalViewerPlugin implements SceneCapture {
64
+ readonly #plugin: MolstarPlugin;
65
+ #picker: MeasurementPicker | undefined;
66
+
67
+ /** Resolves once the canvas exists; every call awaits it internally. */
68
+ readonly ready: Promise<void>;
69
+
70
+ constructor(container: HTMLElement, options: CrystalViewerOptions = {}) {
71
+ this.#plugin = new MolstarPlugin(container, {
72
+ background: options.background,
73
+ // Replacing a scene commits several times, and molstar's own default
74
+ // glides the camera on each, so the structure appears to drift into
75
+ // place. Reframing is right; animating it between two unrelated
76
+ // structures is not.
77
+ cameraResetDurationMilliseconds: 0,
78
+ });
79
+ this.ready = this.#plugin.ready;
80
+ const { onMeasure } = options;
81
+ if (onMeasure !== undefined) {
82
+ // A failed initialisation is reported by the next call the page makes.
83
+ void this.#plugin
84
+ .run((plugin) => {
85
+ this.#picker = new MeasurementPicker(
86
+ plugin,
87
+ onMeasure,
88
+ crystalStructure,
89
+ );
90
+ })
91
+ .catch(() => undefined);
92
+ }
93
+ }
94
+
95
+ /**
96
+ * Whether {@link dispose} has been called.
97
+ * @returns True once the viewer is disposed.
98
+ */
99
+ get disposed(): boolean {
100
+ return this.#plugin.disposed;
101
+ }
102
+
103
+ /**
104
+ * Wait for initialisation, then run `action` on the plugin.
105
+ * @param action - What to do with the molstar context.
106
+ * @returns Nothing once the viewer has been disposed — before the call or
107
+ * while `action` was still running. An initialisation failure, and any
108
+ * error `action` throws while the viewer is alive, still reach the caller.
109
+ */
110
+ run(action: (plugin: PluginContext) => void | Promise<void>): Promise<void> {
111
+ return this.#plugin.run(action).then(() => undefined);
112
+ }
113
+
114
+ /**
115
+ * Frame everything on screen.
116
+ * @param durationMs - Transition length; 0 jumps.
117
+ * @param orientation - Where to look from, from `sceneOrientation`. Left out,
118
+ * the camera keeps the direction it is already pointing.
119
+ * @returns Resolves once the camera has been told where to go.
120
+ */
121
+ resetCamera(
122
+ durationMs = DEFAULT_CAMERA_DURATION,
123
+ orientation?: ViewOrientation,
124
+ ): Promise<void> {
125
+ return this.run((plugin) => {
126
+ resetCamera(plugin, durationMs, orientation);
127
+ });
128
+ }
129
+
130
+ /**
131
+ * Where the camera stands, against what is drawn.
132
+ * @returns The camera, or `null` while nothing is drawn.
133
+ */
134
+ async readCamera(): Promise<Molecule3DCamera | null> {
135
+ return (await this.#plugin.run(readCamera)) ?? null;
136
+ }
137
+
138
+ /**
139
+ * Put the camera back where {@link readCamera} found it.
140
+ * @param camera - Where to stand.
141
+ * @returns Resolves once the camera stands there.
142
+ */
143
+ applyCamera(camera: Molecule3DCamera): Promise<void> {
144
+ return this.run((plugin) => {
145
+ applyCamera(plugin, camera);
146
+ });
147
+ }
148
+
149
+ /**
150
+ * Render the scene off screen at a size of its own, for a picture to save.
151
+ * @param size - Pixel size of the picture.
152
+ * @returns The PNG as a data URI; `undefined` once the viewer is disposed.
153
+ */
154
+ captureImage(size: ImageSize): Promise<string | undefined> {
155
+ return this.#plugin.run((plugin) => captureScene(plugin, size));
156
+ }
157
+
158
+ /**
159
+ * Render the part of the scene holding the crystal, for a figure handed on.
160
+ * @param size - Pixel size of the frame the crystal is cut from.
161
+ * @param longest - Longest side of the figure, in pixels.
162
+ * @returns The figure; `undefined` once the viewer is disposed.
163
+ */
164
+ captureFigure(
165
+ size: ImageSize,
166
+ longest: number,
167
+ ): Promise<RenderedFigure | undefined> {
168
+ return this.#plugin.run((plugin) => captureFigure(plugin, size, longest));
169
+ }
170
+
171
+ /**
172
+ * Zoom onto one point of the scene.
173
+ * @param centre - What to frame, Cartesian ångström.
174
+ * @param radius - Radius of the ball to fit, ångström.
175
+ * @param durationMs - Transition length; 0 jumps.
176
+ * @param orientation - Where to look from; the current direction when absent.
177
+ * @returns Resolves once the camera has been told where to go.
178
+ */
179
+ focus(
180
+ centre: Point3,
181
+ radius: number,
182
+ durationMs = DEFAULT_CAMERA_DURATION,
183
+ orientation?: ViewOrientation,
184
+ ): Promise<void> {
185
+ return this.run((plugin) => {
186
+ focusPoint(plugin, centre, radius, { durationMs, orientation });
187
+ });
188
+ }
189
+
190
+ /**
191
+ * Turn the automatic spin on or off. It moves the camera, never the object:
192
+ * showing that a molecule maps onto itself is a different button.
193
+ * @param spinning - Whether the scene should keep turning.
194
+ * @param speed - molstar's own spin unit.
195
+ * @returns Resolves once the spin is set.
196
+ */
197
+ setSpin(spinning: boolean, speed = 1): Promise<void> {
198
+ return this.run((plugin) => {
199
+ setSpin(plugin, spinning, speed);
200
+ });
201
+ }
202
+
203
+ /**
204
+ * Report what the pointer rests on, by the label its drawing carries. Every
205
+ * axis, plane and centre is drawn with a label already — `C3 along [111]`,
206
+ * `n glide at x = ¼` — and an atom is named `O 1`, never by the row molstar
207
+ * parsed it from.
208
+ * @param listener - Called with the label, or `null` over nothing.
209
+ * @returns A function that stops the reporting; safe to call at any time.
210
+ */
211
+ onHover(listener: (label: string | null) => void): () => void {
212
+ return this.#plugin.subscribe((plugin) =>
213
+ subscribeHoverLabel(plugin, (hover) => {
214
+ listener(hover === null ? null : hover.label);
215
+ }),
216
+ );
217
+ }
218
+
219
+ /**
220
+ * Make clicks on atoms build a measurement, or give them back to the camera.
221
+ * @param kind - The measurement to build, or `null`.
222
+ * @returns Resolves once clicks build that measurement.
223
+ */
224
+ setMeasureKind(kind: MeasurementKind | null): Promise<void> {
225
+ return this.run(() => {
226
+ this.#picker?.setKind(kind);
227
+ });
228
+ }
229
+
230
+ /**
231
+ * Draw exactly these measurements over the atoms on screen.
232
+ * @param measurements - Replaces whatever is drawn; empty clears them.
233
+ * @returns Resolves once they are drawn.
234
+ */
235
+ showMeasurements(measurements: readonly Measurement[]): Promise<void> {
236
+ return this.run(async (plugin) => {
237
+ if (measurements.length === 0) {
238
+ await clearMeasurements(plugin);
239
+ return;
240
+ }
241
+ await renderMeasurements(plugin, measurements, crystalStructure);
242
+ });
243
+ }
244
+
245
+ /** Re-read the container's size. Call from a `ResizeObserver`. */
246
+ handleResize(): void {
247
+ this.#plugin.handleResize();
248
+ }
249
+
250
+ /**
251
+ * Tear the canvas down and release its WebGL context. Idempotent, and safe to
252
+ * call before initialisation has finished.
253
+ */
254
+ dispose(): void {
255
+ this.#picker?.dispose();
256
+ this.#plugin.dispose();
257
+ }
258
+ }
@@ -0,0 +1,98 @@
1
+ /**
2
+ * The unit cell, and the stack of cells around it.
3
+ *
4
+ * molstar's own unit-cell shape reads the space group off the model, and this
5
+ * site gives it none — it has applied its own operations already — so the box is
6
+ * drawn here, which also lets an n×n×n stack be drawn at all.
7
+ */
8
+
9
+ import type { PluginContext } from 'molstar/lib/mol-plugin/context.js';
10
+
11
+ import { cellAxisLabelPoints, cellEdges } from '../core/cellGeometry.ts';
12
+ import { CELL_COLOUR, LABEL_COLOUR } from '../core/palette.ts';
13
+ import type { MeshPrimitive, TextItem } from '../core/primitives.ts';
14
+ import type { CellRepeat, UnitCell } from '../core/types.ts';
15
+
16
+ import { clearShapeLayer } from './layerRegistry.ts';
17
+ import { renderShapeLayer } from './shapeLayer.ts';
18
+ import { renderTextLayer } from './textLayer.ts';
19
+
20
+ /** How the box is drawn. */
21
+ export interface CellStyle {
22
+ /**
23
+ * Radius of an edge, ångström.
24
+ * @default 0.035
25
+ */
26
+ radius?: number;
27
+ /**
28
+ * Whether the a, b and c letters are drawn on the cell at the origin.
29
+ * @default true
30
+ */
31
+ labels?: boolean;
32
+ /**
33
+ * Height of those letters, ångström.
34
+ * @default 0.7
35
+ */
36
+ labelSize?: number;
37
+ }
38
+
39
+ /**
40
+ * Replace the drawn cell.
41
+ * @param plugin - The molstar context.
42
+ * @param cell - The six cell parameters, or `null` to draw none.
43
+ * @param repeat - Cells along a, b and c.
44
+ * @param style - See {@link CellStyle}.
45
+ * @throws {Error} When the canvas is not ready.
46
+ * @throws {RangeError} When the angles describe no cell.
47
+ */
48
+ export async function renderUnitCell(
49
+ plugin: PluginContext,
50
+ cell: UnitCell | null,
51
+ repeat: CellRepeat = [1, 1, 1],
52
+ style: CellStyle = {},
53
+ ): Promise<void> {
54
+ if (cell === null) {
55
+ clearUnitCell(plugin);
56
+ return;
57
+ }
58
+ const radius = style.radius ?? 0.035;
59
+ const primitives: MeshPrimitive[] = [];
60
+ for (const edge of cellEdges(cell, repeat)) {
61
+ primitives.push({ shape: 'rod', start: edge.start, end: edge.end, radius });
62
+ }
63
+ await renderShapeLayer(
64
+ plugin,
65
+ CELL_LAYER,
66
+ 'Unit cell',
67
+ [{ label: 'Unit cell', colour: CELL_COLOUR, primitives }],
68
+ { alpha: 1 },
69
+ );
70
+ await renderTextLayer(
71
+ plugin,
72
+ CELL_LABEL_LAYER,
73
+ 'Cell axes',
74
+ style.labels === false ? [] : axisLabels(cell, style.labelSize ?? 0.7),
75
+ LABEL_COLOUR,
76
+ );
77
+ }
78
+
79
+ /**
80
+ * Remove the cell and its letters.
81
+ * @param plugin - The molstar context.
82
+ */
83
+ export function clearUnitCell(plugin: PluginContext): void {
84
+ clearShapeLayer(plugin, CELL_LAYER);
85
+ clearShapeLayer(plugin, CELL_LABEL_LAYER);
86
+ }
87
+
88
+ function axisLabels(cell: UnitCell, size: number): TextItem[] {
89
+ const points = cellAxisLabelPoints(cell);
90
+ return [
91
+ { text: 'a', position: points[0], size },
92
+ { text: 'b', position: points[1], size },
93
+ { text: 'c', position: points[2], size },
94
+ ];
95
+ }
96
+
97
+ const CELL_LAYER = 'crystal3d-unit-cell';
98
+ const CELL_LABEL_LAYER = 'crystal3d-unit-cell-labels';
@@ -0,0 +1,121 @@
1
+ /**
2
+ * The symmetry elements over the structure — the reason a symmetry page has a viewer
3
+ * of its own at all.
4
+ *
5
+ * The shapes come from `elementDrawing.ts`, which knows no molstar. This module
6
+ * hands them to **three** layers, which is what keeps a cell holding ten of
7
+ * them readable: the planes are drawn nearly transparent, the rods, rims and
8
+ * balls that outline them stay solid, and the names sit in a third layer so
9
+ * they can be switched off without rebuilding anything.
10
+ *
11
+ * Drawing the whole of an element at one opacity is what makes a stack of
12
+ * mirror planes a wash of colour with the structure lost somewhere inside it.
13
+ */
14
+
15
+ import type { PluginContext } from 'molstar/lib/mol-plugin/context.js';
16
+
17
+ import type { ElementGroup } from '../core/elementDrawing.ts';
18
+ import { elementGroups } from '../core/elementDrawing.ts';
19
+ import { LABEL_COLOUR } from '../core/palette.ts';
20
+ import type {
21
+ ElementStyle,
22
+ MeshPrimitive,
23
+ TextItem,
24
+ } from '../core/primitives.ts';
25
+ import type { SymmetryDrawing } from '../core/types.ts';
26
+
27
+ import { clearShapeLayer } from './layerRegistry.ts';
28
+ import type { ShapeGroup } from './shapeLayer.ts';
29
+ import { renderShapeLayer } from './shapeLayer.ts';
30
+ import { renderTextLayer } from './textLayer.ts';
31
+
32
+ /** How the elements are drawn. */
33
+ export interface ElementLayerStyle {
34
+ /** Sizes; the defaults of `primitives.ts` are used for anything left out. */
35
+ style?: ElementStyle;
36
+ /**
37
+ * Whether each element's name floats beside it.
38
+ * @default true
39
+ */
40
+ labels?: boolean;
41
+ /**
42
+ * Opacity of a plane's face. Low on purpose: the structure has to be read
43
+ * through however many planes cut the cell.
44
+ * @default 0.1
45
+ */
46
+ faceAlpha?: number;
47
+ /**
48
+ * Opacity of the rods, rims, arrows and balls.
49
+ * @default 0.9
50
+ */
51
+ lineAlpha?: number;
52
+ }
53
+
54
+ /**
55
+ * Replace the drawn symmetry elements.
56
+ * @param plugin - The molstar context.
57
+ * @param drawings - The elements; an empty list clears them.
58
+ * @param style - See {@link ElementLayerStyle}.
59
+ * @returns The groups that were drawn, so a caller can measure them without
60
+ * building them a second time.
61
+ * @throws {Error} When the canvas is not initialised yet.
62
+ */
63
+ export async function renderSymmetryElements(
64
+ plugin: PluginContext,
65
+ drawings: readonly SymmetryDrawing[],
66
+ style: ElementLayerStyle = {},
67
+ ): Promise<ElementGroup[]> {
68
+ const groups = elementGroups(drawings, {
69
+ style: style.style,
70
+ labels: style.labels ?? true,
71
+ });
72
+ const faces: ShapeGroup[] = [];
73
+ const lines: ShapeGroup[] = [];
74
+ const labels: TextItem[] = [];
75
+ for (const group of groups) {
76
+ const solid: MeshPrimitive[] = [];
77
+ const flat: MeshPrimitive[] = [];
78
+ for (const primitive of group.primitives) {
79
+ const target =
80
+ primitive.shape === 'face' || primitive.shape === 'plate'
81
+ ? flat
82
+ : solid;
83
+ target.push(primitive);
84
+ }
85
+ const { label, colour } = group;
86
+ if (flat.length > 0) faces.push({ label, colour, primitives: flat });
87
+ if (solid.length > 0) lines.push({ label, colour, primitives: solid });
88
+ for (const item of group.labels) labels.push({ ...item, colour });
89
+ }
90
+ await renderShapeLayer(plugin, FACE_LAYER, 'Symmetry planes', faces, {
91
+ alpha: style.faceAlpha ?? 0.1,
92
+ // A face seen edge-on would otherwise darken to nothing.
93
+ ignoreLight: true,
94
+ });
95
+ await renderShapeLayer(plugin, ELEMENT_LAYER, 'Symmetry elements', lines, {
96
+ alpha: style.lineAlpha ?? 0.9,
97
+ ignoreLight: true,
98
+ });
99
+ await renderTextLayer(
100
+ plugin,
101
+ ELEMENT_LABEL_LAYER,
102
+ 'Symmetry element labels',
103
+ labels,
104
+ LABEL_COLOUR,
105
+ );
106
+ return groups;
107
+ }
108
+
109
+ /**
110
+ * Remove the symmetry elements and their labels.
111
+ * @param plugin - The molstar context.
112
+ */
113
+ export function clearSymmetryElements(plugin: PluginContext): void {
114
+ clearShapeLayer(plugin, FACE_LAYER);
115
+ clearShapeLayer(plugin, ELEMENT_LAYER);
116
+ clearShapeLayer(plugin, ELEMENT_LABEL_LAYER);
117
+ }
118
+
119
+ const FACE_LAYER = 'symmetry-element-faces';
120
+ const ELEMENT_LAYER = 'symmetry-elements';
121
+ const ELEMENT_LABEL_LAYER = 'symmetry-element-labels';