matterviz 0.3.0 → 0.3.2

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 (286) hide show
  1. package/dist/FilePicker.svelte +37 -20
  2. package/dist/Icon.svelte +2 -2
  3. package/dist/MillerIndexInput.svelte +60 -0
  4. package/dist/MillerIndexInput.svelte.d.ts +7 -0
  5. package/dist/app.css +38 -2
  6. package/dist/brillouin/BrillouinZone.svelte +20 -62
  7. package/dist/brillouin/BrillouinZone.svelte.d.ts +1 -1
  8. package/dist/brillouin/BrillouinZoneExportPane.svelte +12 -20
  9. package/dist/brillouin/BrillouinZoneScene.svelte +2 -2
  10. package/dist/brillouin/BrillouinZoneScene.svelte.d.ts +1 -1
  11. package/dist/chempot-diagram/ChemPotDiagram.svelte +192 -0
  12. package/dist/chempot-diagram/ChemPotDiagram.svelte.d.ts +13 -0
  13. package/dist/chempot-diagram/ChemPotDiagram2D.svelte +677 -0
  14. package/dist/chempot-diagram/ChemPotDiagram2D.svelte.d.ts +16 -0
  15. package/dist/chempot-diagram/ChemPotDiagram3D.svelte +2688 -0
  16. package/dist/chempot-diagram/ChemPotDiagram3D.svelte.d.ts +16 -0
  17. package/dist/chempot-diagram/ChemPotScene3D.svelte +8 -0
  18. package/dist/chempot-diagram/ChemPotScene3D.svelte.d.ts +7 -0
  19. package/dist/chempot-diagram/color.d.ts +10 -0
  20. package/dist/chempot-diagram/color.js +33 -0
  21. package/dist/chempot-diagram/compute.d.ts +38 -0
  22. package/dist/chempot-diagram/compute.js +650 -0
  23. package/dist/chempot-diagram/index.d.ts +5 -0
  24. package/dist/chempot-diagram/index.js +5 -0
  25. package/dist/chempot-diagram/pointer.d.ts +16 -0
  26. package/dist/chempot-diagram/pointer.js +40 -0
  27. package/dist/chempot-diagram/temperature.d.ts +15 -0
  28. package/dist/chempot-diagram/temperature.js +37 -0
  29. package/dist/chempot-diagram/types.d.ts +83 -0
  30. package/dist/chempot-diagram/types.js +27 -0
  31. package/dist/colors/index.d.ts +3 -1
  32. package/dist/colors/index.js +4 -0
  33. package/dist/composition/BarChart.svelte +13 -22
  34. package/dist/composition/BubbleChart.svelte +5 -3
  35. package/dist/composition/FormulaFilter.svelte +770 -90
  36. package/dist/composition/FormulaFilter.svelte.d.ts +37 -1
  37. package/dist/composition/PieChart.svelte +43 -18
  38. package/dist/composition/PieChart.svelte.d.ts +1 -1
  39. package/dist/constants.d.ts +1 -0
  40. package/dist/constants.js +2 -0
  41. package/dist/convex-hull/ConvexHull.svelte +14 -1
  42. package/dist/convex-hull/ConvexHull.svelte.d.ts +1 -1
  43. package/dist/convex-hull/ConvexHull2D.svelte +14 -45
  44. package/dist/convex-hull/ConvexHull2D.svelte.d.ts +1 -1
  45. package/dist/convex-hull/ConvexHull3D.svelte +396 -134
  46. package/dist/convex-hull/ConvexHull3D.svelte.d.ts +1 -1
  47. package/dist/convex-hull/ConvexHull4D.svelte +93 -42
  48. package/dist/convex-hull/ConvexHull4D.svelte.d.ts +1 -1
  49. package/dist/convex-hull/ConvexHullControls.svelte +94 -31
  50. package/dist/convex-hull/ConvexHullControls.svelte.d.ts +4 -2
  51. package/dist/convex-hull/ConvexHullStats.svelte +697 -128
  52. package/dist/convex-hull/ConvexHullStats.svelte.d.ts +6 -1
  53. package/dist/convex-hull/ConvexHullTooltip.svelte +1 -0
  54. package/dist/convex-hull/GasPressureControls.svelte +72 -38
  55. package/dist/convex-hull/GasPressureControls.svelte.d.ts +2 -1
  56. package/dist/convex-hull/TemperatureSlider.svelte +46 -19
  57. package/dist/convex-hull/TemperatureSlider.svelte.d.ts +2 -1
  58. package/dist/convex-hull/demo-temperature.d.ts +6 -0
  59. package/dist/convex-hull/demo-temperature.js +36 -0
  60. package/dist/convex-hull/gas-thermodynamics.js +16 -5
  61. package/dist/convex-hull/helpers.d.ts +7 -1
  62. package/dist/convex-hull/helpers.js +45 -15
  63. package/dist/convex-hull/index.d.ts +15 -1
  64. package/dist/convex-hull/index.js +1 -0
  65. package/dist/convex-hull/thermodynamics.d.ts +8 -21
  66. package/dist/convex-hull/thermodynamics.js +106 -17
  67. package/dist/convex-hull/types.d.ts +7 -0
  68. package/dist/convex-hull/types.js +11 -0
  69. package/dist/coordination/CoordinationBarPlot.svelte +29 -46
  70. package/dist/element/BohrAtom.svelte +1 -1
  71. package/dist/element/data.js +2 -14
  72. package/dist/element/data.json.gz +0 -0
  73. package/dist/element/index.d.ts +1 -1
  74. package/dist/element/index.js +1 -0
  75. package/dist/element/types.d.ts +1 -0
  76. package/dist/fermi-surface/FermiSurface.svelte +21 -65
  77. package/dist/fermi-surface/FermiSurface.svelte.d.ts +1 -1
  78. package/dist/fermi-surface/FermiSurfaceControls.svelte.d.ts +1 -1
  79. package/dist/fermi-surface/FermiSurfaceScene.svelte +1 -1
  80. package/dist/fermi-surface/FermiSurfaceScene.svelte.d.ts +1 -1
  81. package/dist/fermi-surface/compute.js +1 -21
  82. package/dist/fermi-surface/marching-cubes.d.ts +2 -13
  83. package/dist/fermi-surface/marching-cubes.js +2 -519
  84. package/dist/fermi-surface/parse.js +17 -23
  85. package/dist/heatmap-matrix/HeatmapMatrix.svelte +1273 -0
  86. package/dist/heatmap-matrix/HeatmapMatrix.svelte.d.ts +110 -0
  87. package/dist/heatmap-matrix/HeatmapMatrixControls.svelte +171 -0
  88. package/dist/heatmap-matrix/HeatmapMatrixControls.svelte.d.ts +31 -0
  89. package/dist/heatmap-matrix/index.d.ts +53 -0
  90. package/dist/heatmap-matrix/index.js +100 -0
  91. package/dist/heatmap-matrix/shared.d.ts +2 -0
  92. package/dist/heatmap-matrix/shared.js +4 -0
  93. package/dist/icons.d.ts +119 -0
  94. package/dist/icons.js +119 -0
  95. package/dist/index.d.ts +6 -1
  96. package/dist/index.js +6 -1
  97. package/dist/io/export.js +15 -3
  98. package/dist/io/file-drop.d.ts +7 -0
  99. package/dist/io/file-drop.js +43 -0
  100. package/dist/io/index.d.ts +2 -2
  101. package/dist/io/index.js +2 -112
  102. package/dist/io/types.d.ts +1 -0
  103. package/dist/io/url-drop.d.ts +2 -0
  104. package/dist/io/url-drop.js +118 -0
  105. package/dist/isosurface/Isosurface.svelte +231 -0
  106. package/dist/isosurface/Isosurface.svelte.d.ts +8 -0
  107. package/dist/isosurface/IsosurfaceControls.svelte +273 -0
  108. package/dist/isosurface/IsosurfaceControls.svelte.d.ts +9 -0
  109. package/dist/isosurface/index.d.ts +5 -0
  110. package/dist/isosurface/index.js +6 -0
  111. package/dist/isosurface/parse.d.ts +6 -0
  112. package/dist/isosurface/parse.js +548 -0
  113. package/dist/isosurface/slice.d.ts +11 -0
  114. package/dist/isosurface/slice.js +145 -0
  115. package/dist/isosurface/types.d.ts +55 -0
  116. package/dist/isosurface/types.js +178 -0
  117. package/dist/labels.d.ts +2 -1
  118. package/dist/labels.js +1 -0
  119. package/dist/layout/InfoTag.svelte +62 -62
  120. package/dist/layout/SubpageGrid.svelte +74 -0
  121. package/dist/layout/SubpageGrid.svelte.d.ts +14 -0
  122. package/dist/layout/index.d.ts +1 -0
  123. package/dist/layout/index.js +1 -0
  124. package/dist/layout/json-tree/JsonNode.svelte +226 -53
  125. package/dist/layout/json-tree/JsonTree.svelte +425 -51
  126. package/dist/layout/json-tree/JsonTree.svelte.d.ts +1 -1
  127. package/dist/layout/json-tree/JsonValue.svelte +218 -97
  128. package/dist/layout/json-tree/types.d.ts +27 -2
  129. package/dist/layout/json-tree/utils.d.ts +14 -1
  130. package/dist/layout/json-tree/utils.js +254 -0
  131. package/dist/marching-cubes.d.ts +14 -0
  132. package/dist/marching-cubes.js +519 -0
  133. package/dist/math.d.ts +8 -0
  134. package/dist/math.js +374 -7
  135. package/dist/overlays/ContextMenu.svelte +3 -2
  136. package/dist/overlays/DraggablePane.svelte +163 -58
  137. package/dist/overlays/DraggablePane.svelte.d.ts +2 -0
  138. package/dist/phase-diagram/IsobaricBinaryPhaseDiagram.svelte +232 -77
  139. package/dist/phase-diagram/IsobaricBinaryPhaseDiagram.svelte.d.ts +6 -2
  140. package/dist/phase-diagram/PhaseDiagramControls.svelte +32 -11
  141. package/dist/phase-diagram/PhaseDiagramControls.svelte.d.ts +3 -2
  142. package/dist/phase-diagram/PhaseDiagramEditorPane.svelte +103 -0
  143. package/dist/phase-diagram/PhaseDiagramEditorPane.svelte.d.ts +15 -0
  144. package/dist/phase-diagram/PhaseDiagramExportPane.svelte +102 -95
  145. package/dist/phase-diagram/PhaseDiagramExportPane.svelte.d.ts +7 -0
  146. package/dist/phase-diagram/PhaseDiagramTooltip.svelte +100 -26
  147. package/dist/phase-diagram/PhaseDiagramTooltip.svelte.d.ts +6 -3
  148. package/dist/phase-diagram/index.d.ts +2 -0
  149. package/dist/phase-diagram/index.js +2 -0
  150. package/dist/phase-diagram/svg-to-diagram.d.ts +2 -0
  151. package/dist/phase-diagram/svg-to-diagram.js +865 -0
  152. package/dist/phase-diagram/types.d.ts +10 -0
  153. package/dist/phase-diagram/utils.d.ts +7 -4
  154. package/dist/phase-diagram/utils.js +149 -59
  155. package/dist/plot/AxisLabel.svelte +26 -0
  156. package/dist/plot/AxisLabel.svelte.d.ts +16 -0
  157. package/dist/plot/BarPlot.svelte +473 -228
  158. package/dist/plot/BarPlot.svelte.d.ts +3 -3
  159. package/dist/plot/BarPlotControls.svelte +3 -2
  160. package/dist/plot/BarPlotControls.svelte.d.ts +1 -1
  161. package/dist/plot/ColorBar.svelte +54 -54
  162. package/dist/plot/ColorBar.svelte.d.ts +1 -1
  163. package/dist/plot/ElementScatter.svelte +4 -3
  164. package/dist/plot/FillArea.svelte +4 -1
  165. package/dist/plot/Histogram.svelte +320 -230
  166. package/dist/plot/Histogram.svelte.d.ts +2 -2
  167. package/dist/plot/HistogramControls.svelte +29 -10
  168. package/dist/plot/HistogramControls.svelte.d.ts +6 -2
  169. package/dist/plot/InteractiveAxisLabel.svelte.d.ts +2 -2
  170. package/dist/plot/PlotControls.svelte +109 -27
  171. package/dist/plot/PlotControls.svelte.d.ts +1 -1
  172. package/dist/plot/PlotLegend.svelte +1 -1
  173. package/dist/plot/PortalSelect.svelte +2 -1
  174. package/dist/plot/ReferenceLine.svelte +2 -1
  175. package/dist/plot/ReferenceLine.svelte.d.ts +1 -0
  176. package/dist/plot/ReferencePlane.svelte +1 -3
  177. package/dist/plot/ScatterPlot.svelte +343 -209
  178. package/dist/plot/ScatterPlot.svelte.d.ts +3 -3
  179. package/dist/plot/ScatterPlot3D.svelte.d.ts +2 -2
  180. package/dist/plot/ScatterPlot3DControls.svelte +203 -250
  181. package/dist/plot/ScatterPlot3DScene.svelte +4 -7
  182. package/dist/plot/ScatterPlot3DScene.svelte.d.ts +2 -2
  183. package/dist/plot/ScatterPlotControls.svelte +95 -55
  184. package/dist/plot/ScatterPlotControls.svelte.d.ts +1 -1
  185. package/dist/plot/ZeroLines.svelte +44 -0
  186. package/dist/plot/ZeroLines.svelte.d.ts +32 -0
  187. package/dist/plot/ZoomRect.svelte +21 -0
  188. package/dist/plot/ZoomRect.svelte.d.ts +8 -0
  189. package/dist/plot/axis-utils.d.ts +1 -1
  190. package/dist/plot/data-cleaning.js +1 -5
  191. package/dist/plot/index.d.ts +6 -2
  192. package/dist/plot/index.js +6 -2
  193. package/dist/plot/interactions.d.ts +8 -10
  194. package/dist/plot/interactions.js +10 -19
  195. package/dist/plot/layout.d.ts +7 -1
  196. package/dist/plot/layout.js +12 -4
  197. package/dist/plot/reference-line.d.ts +4 -21
  198. package/dist/plot/reference-line.js +7 -81
  199. package/dist/plot/types.d.ts +42 -17
  200. package/dist/plot/types.js +10 -0
  201. package/dist/plot/utils/label-placement.js +14 -11
  202. package/dist/plot/utils.d.ts +1 -0
  203. package/dist/plot/utils.js +14 -0
  204. package/dist/rdf/RdfPlot.svelte +55 -66
  205. package/dist/rdf/RdfPlot.svelte.d.ts +1 -1
  206. package/dist/rdf/index.d.ts +1 -1
  207. package/dist/rdf/index.js +1 -1
  208. package/dist/settings.d.ts +5 -0
  209. package/dist/settings.js +37 -3
  210. package/dist/spectral/Bands.svelte +515 -143
  211. package/dist/spectral/Bands.svelte.d.ts +22 -2
  212. package/dist/spectral/helpers.d.ts +23 -1
  213. package/dist/spectral/helpers.js +65 -9
  214. package/dist/spectral/types.d.ts +2 -0
  215. package/dist/structure/AtomLegend.svelte +31 -10
  216. package/dist/structure/AtomLegend.svelte.d.ts +1 -1
  217. package/dist/structure/CellSelect.svelte +92 -22
  218. package/dist/structure/Lattice.svelte +2 -0
  219. package/dist/structure/Structure.svelte +716 -173
  220. package/dist/structure/Structure.svelte.d.ts +7 -2
  221. package/dist/structure/StructureControls.svelte +26 -14
  222. package/dist/structure/StructureControls.svelte.d.ts +5 -1
  223. package/dist/structure/StructureInfoPane.svelte +7 -1
  224. package/dist/structure/StructureScene.svelte +386 -95
  225. package/dist/structure/StructureScene.svelte.d.ts +15 -4
  226. package/dist/structure/atom-properties.d.ts +6 -2
  227. package/dist/structure/atom-properties.js +38 -25
  228. package/dist/structure/export.js +10 -7
  229. package/dist/structure/ferrox-wasm-types.d.ts +3 -2
  230. package/dist/structure/ferrox-wasm-types.js +0 -3
  231. package/dist/structure/ferrox-wasm.d.ts +3 -2
  232. package/dist/structure/ferrox-wasm.js +1 -2
  233. package/dist/structure/index.d.ts +7 -0
  234. package/dist/structure/index.js +22 -0
  235. package/dist/structure/parse.js +19 -16
  236. package/dist/structure/partial-occupancy.d.ts +25 -0
  237. package/dist/structure/partial-occupancy.js +102 -0
  238. package/dist/structure/validation.js +6 -3
  239. package/dist/symmetry/SymmetryStats.svelte +18 -4
  240. package/dist/symmetry/WyckoffTable.svelte +18 -10
  241. package/dist/symmetry/index.d.ts +7 -4
  242. package/dist/symmetry/index.js +83 -18
  243. package/dist/table/HeatmapTable.svelte +468 -69
  244. package/dist/table/HeatmapTable.svelte.d.ts +13 -1
  245. package/dist/table/ToggleMenu.svelte +291 -44
  246. package/dist/table/ToggleMenu.svelte.d.ts +4 -1
  247. package/dist/table/index.d.ts +3 -0
  248. package/dist/tooltip/index.d.ts +1 -1
  249. package/dist/tooltip/index.js +1 -0
  250. package/dist/trajectory/Trajectory.svelte +147 -145
  251. package/dist/trajectory/TrajectoryExportPane.svelte +13 -9
  252. package/dist/trajectory/TrajectoryExportPane.svelte.d.ts +1 -1
  253. package/dist/trajectory/constants.d.ts +6 -0
  254. package/dist/trajectory/constants.js +7 -0
  255. package/dist/trajectory/extract.js +3 -5
  256. package/dist/trajectory/format-detect.d.ts +9 -0
  257. package/dist/trajectory/format-detect.js +76 -0
  258. package/dist/trajectory/frame-reader.d.ts +17 -0
  259. package/dist/trajectory/frame-reader.js +339 -0
  260. package/dist/trajectory/helpers.d.ts +15 -0
  261. package/dist/trajectory/helpers.js +187 -0
  262. package/dist/trajectory/index.d.ts +1 -0
  263. package/dist/trajectory/index.js +11 -4
  264. package/dist/trajectory/parse/ase.d.ts +2 -0
  265. package/dist/trajectory/parse/ase.js +76 -0
  266. package/dist/trajectory/parse/hdf5.d.ts +2 -0
  267. package/dist/trajectory/parse/hdf5.js +121 -0
  268. package/dist/trajectory/parse/index.d.ts +12 -0
  269. package/dist/trajectory/parse/index.js +304 -0
  270. package/dist/trajectory/parse/lammps.d.ts +5 -0
  271. package/dist/trajectory/parse/lammps.js +169 -0
  272. package/dist/trajectory/parse/vasp.d.ts +2 -0
  273. package/dist/trajectory/parse/vasp.js +65 -0
  274. package/dist/trajectory/parse/xyz.d.ts +2 -0
  275. package/dist/trajectory/parse/xyz.js +109 -0
  276. package/dist/trajectory/types.d.ts +11 -0
  277. package/dist/trajectory/types.js +1 -0
  278. package/dist/utils.d.ts +2 -0
  279. package/dist/utils.js +4 -0
  280. package/dist/xrd/XrdPlot.svelte +6 -4
  281. package/dist/xrd/calc-xrd.js +0 -1
  282. package/package.json +33 -23
  283. package/readme.md +4 -4
  284. package/dist/trajectory/parse.d.ts +0 -42
  285. package/dist/trajectory/parse.js +0 -1267
  286. /package/dist/element/{data.json.d.ts → data.json.gz.d.ts} +0 -0
@@ -1,519 +1,2 @@
1
- // Edge table: for each cube configuration (256 cases), which edges are intersected
2
- // Each bit indicates whether that edge has an intersection
3
- // deno-fmt-ignore
4
- const EDGE_TABLE = [0x0, 0x109, 0x203, 0x30a, 0x406, 0x50f, 0x605, 0x70c, 0x80c, 0x905, 0xa0f, 0xb06, 0xc0a, 0xd03, 0xe09, 0xf00, 0x190, 0x99, 0x393, 0x29a, 0x596, 0x49f, 0x795, 0x69c, 0x99c, 0x895, 0xb9f, 0xa96, 0xd9a, 0xc93, 0xf99, 0xe90, 0x230, 0x339, 0x33, 0x13a, 0x636, 0x73f, 0x435, 0x53c, 0xa3c, 0xb35, 0x83f, 0x936, 0xe3a, 0xf33, 0xc39, 0xd30, 0x3a0, 0x2a9, 0x1a3, 0xaa, 0x7a6, 0x6af, 0x5a5, 0x4ac, 0xbac, 0xaa5, 0x9af, 0x8a6, 0xfaa, 0xea3, 0xda9, 0xca0, 0x460, 0x569, 0x663, 0x76a, 0x66, 0x16f, 0x265, 0x36c, 0xc6c, 0xd65, 0xe6f, 0xf66, 0x86a, 0x963, 0xa69, 0xb60, 0x5f0, 0x4f9, 0x7f3, 0x6fa, 0x1f6, 0xff, 0x3f5, 0x2fc, 0xdfc, 0xcf5, 0xfff, 0xef6, 0x9fa, 0x8f3, 0xbf9, 0xaf0, 0x650, 0x759, 0x453, 0x55a, 0x256, 0x35f, 0x55, 0x15c, 0xe5c, 0xf55, 0xc5f, 0xd56, 0xa5a, 0xb53, 0x859, 0x950, 0x7c0, 0x6c9, 0x5c3, 0x4ca, 0x3c6, 0x2cf, 0x1c5, 0xcc, 0xfcc, 0xec5, 0xdcf, 0xcc6, 0xbca, 0xac3, 0x9c9, 0x8c0, 0x8c0, 0x9c9, 0xac3, 0xbca, 0xcc6, 0xdcf, 0xec5, 0xfcc, 0xcc, 0x1c5, 0x2cf, 0x3c6, 0x4ca, 0x5c3, 0x6c9, 0x7c0, 0x950, 0x859, 0xb53, 0xa5a, 0xd56, 0xc5f, 0xf55, 0xe5c, 0x15c, 0x55, 0x35f, 0x256, 0x55a, 0x453, 0x759, 0x650, 0xaf0, 0xbf9, 0x8f3, 0x9fa, 0xef6, 0xfff, 0xcf5, 0xdfc, 0x2fc, 0x3f5, 0xff, 0x1f6, 0x6fa, 0x7f3, 0x4f9, 0x5f0, 0xb60, 0xa69, 0x963, 0x86a, 0xf66, 0xe6f, 0xd65, 0xc6c, 0x36c, 0x265, 0x16f, 0x66, 0x76a, 0x663, 0x569, 0x460, 0xca0, 0xda9, 0xea3, 0xfaa, 0x8a6, 0x9af, 0xaa5, 0xbac, 0x4ac, 0x5a5, 0x6af, 0x7a6, 0xaa, 0x1a3, 0x2a9, 0x3a0, 0xd30, 0xc39, 0xf33, 0xe3a, 0x936, 0x83f, 0xb35, 0xa3c, 0x53c, 0x435, 0x73f, 0x636, 0x13a, 0x33, 0x339, 0x230, 0xe90, 0xf99, 0xc93, 0xd9a, 0xa96, 0xb9f, 0x895, 0x99c, 0x69c, 0x795, 0x49f, 0x596, 0x29a, 0x393, 0x99, 0x190, 0xf00, 0xe09, 0xd03, 0xc0a, 0xb06, 0xa0f, 0x905, 0x80c, 0x70c, 0x605, 0x50f, 0x406, 0x30a, 0x203, 0x109, 0x0];
5
- // Triangle table: for each cube configuration, list of edge triplets forming triangles
6
- // -1 marks the end of the triangle list for that configuration
7
- const TRI_TABLE = [
8
- [],
9
- [0, 8, 3],
10
- [0, 1, 9],
11
- [1, 8, 3, 9, 8, 1],
12
- [1, 2, 10],
13
- [0, 8, 3, 1, 2, 10],
14
- [9, 2, 10, 0, 2, 9],
15
- [2, 8, 3, 2, 10, 8, 10, 9, 8],
16
- [3, 11, 2],
17
- [0, 11, 2, 8, 11, 0],
18
- [1, 9, 0, 2, 3, 11],
19
- [1, 11, 2, 1, 9, 11, 9, 8, 11],
20
- [3, 10, 1, 11, 10, 3],
21
- [0, 10, 1, 0, 8, 10, 8, 11, 10],
22
- [3, 9, 0, 3, 11, 9, 11, 10, 9],
23
- [9, 8, 10, 10, 8, 11],
24
- [4, 7, 8],
25
- [4, 3, 0, 7, 3, 4],
26
- [0, 1, 9, 8, 4, 7],
27
- [4, 1, 9, 4, 7, 1, 7, 3, 1],
28
- [1, 2, 10, 8, 4, 7],
29
- [3, 4, 7, 3, 0, 4, 1, 2, 10],
30
- [9, 2, 10, 9, 0, 2, 8, 4, 7],
31
- [2, 10, 9, 2, 9, 7, 2, 7, 3, 7, 9, 4],
32
- [8, 4, 7, 3, 11, 2],
33
- [11, 4, 7, 11, 2, 4, 2, 0, 4],
34
- [9, 0, 1, 8, 4, 7, 2, 3, 11],
35
- [4, 7, 11, 9, 4, 11, 9, 11, 2, 9, 2, 1],
36
- [3, 10, 1, 3, 11, 10, 7, 8, 4],
37
- [1, 11, 10, 1, 4, 11, 1, 0, 4, 7, 11, 4],
38
- [4, 7, 8, 9, 0, 11, 9, 11, 10, 11, 0, 3],
39
- [4, 7, 11, 4, 11, 9, 9, 11, 10],
40
- [9, 5, 4],
41
- [9, 5, 4, 0, 8, 3],
42
- [0, 5, 4, 1, 5, 0],
43
- [8, 5, 4, 8, 3, 5, 3, 1, 5],
44
- [1, 2, 10, 9, 5, 4],
45
- [3, 0, 8, 1, 2, 10, 4, 9, 5],
46
- [5, 2, 10, 5, 4, 2, 4, 0, 2],
47
- [2, 10, 5, 3, 2, 5, 3, 5, 4, 3, 4, 8],
48
- [9, 5, 4, 2, 3, 11],
49
- [0, 11, 2, 0, 8, 11, 4, 9, 5],
50
- [0, 5, 4, 0, 1, 5, 2, 3, 11],
51
- [2, 1, 5, 2, 5, 8, 2, 8, 11, 4, 8, 5],
52
- [10, 3, 11, 10, 1, 3, 9, 5, 4],
53
- [4, 9, 5, 0, 8, 1, 8, 10, 1, 8, 11, 10],
54
- [5, 4, 0, 5, 0, 11, 5, 11, 10, 11, 0, 3],
55
- [5, 4, 8, 5, 8, 10, 10, 8, 11],
56
- [9, 7, 8, 5, 7, 9],
57
- [9, 3, 0, 9, 5, 3, 5, 7, 3],
58
- [0, 7, 8, 0, 1, 7, 1, 5, 7],
59
- [1, 5, 3, 3, 5, 7],
60
- [9, 7, 8, 9, 5, 7, 10, 1, 2],
61
- [10, 1, 2, 9, 5, 0, 5, 3, 0, 5, 7, 3],
62
- [8, 0, 2, 8, 2, 5, 8, 5, 7, 10, 5, 2],
63
- [2, 10, 5, 2, 5, 3, 3, 5, 7],
64
- [7, 9, 5, 7, 8, 9, 3, 11, 2],
65
- [9, 5, 7, 9, 7, 2, 9, 2, 0, 2, 7, 11],
66
- [2, 3, 11, 0, 1, 8, 1, 7, 8, 1, 5, 7],
67
- [11, 2, 1, 11, 1, 7, 7, 1, 5],
68
- [9, 5, 8, 8, 5, 7, 10, 1, 3, 10, 3, 11],
69
- [5, 7, 0, 5, 0, 9, 7, 11, 0, 1, 0, 10, 11, 10, 0],
70
- [11, 10, 0, 11, 0, 3, 10, 5, 0, 8, 0, 7, 5, 7, 0],
71
- [11, 10, 5, 7, 11, 5],
72
- [10, 6, 5],
73
- [0, 8, 3, 5, 10, 6],
74
- [9, 0, 1, 5, 10, 6],
75
- [1, 8, 3, 1, 9, 8, 5, 10, 6],
76
- [1, 6, 5, 2, 6, 1],
77
- [1, 6, 5, 1, 2, 6, 3, 0, 8],
78
- [9, 6, 5, 9, 0, 6, 0, 2, 6],
79
- [5, 9, 8, 5, 8, 2, 5, 2, 6, 3, 2, 8],
80
- [2, 3, 11, 10, 6, 5],
81
- [11, 0, 8, 11, 2, 0, 10, 6, 5],
82
- [0, 1, 9, 2, 3, 11, 5, 10, 6],
83
- [5, 10, 6, 1, 9, 2, 9, 11, 2, 9, 8, 11],
84
- [6, 3, 11, 6, 5, 3, 5, 1, 3],
85
- [0, 8, 11, 0, 11, 5, 0, 5, 1, 5, 11, 6],
86
- [3, 11, 6, 0, 3, 6, 0, 6, 5, 0, 5, 9],
87
- [6, 5, 9, 6, 9, 11, 11, 9, 8],
88
- [5, 10, 6, 4, 7, 8],
89
- [4, 3, 0, 4, 7, 3, 6, 5, 10],
90
- [1, 9, 0, 5, 10, 6, 8, 4, 7],
91
- [10, 6, 5, 1, 9, 7, 1, 7, 3, 7, 9, 4],
92
- [6, 1, 2, 6, 5, 1, 4, 7, 8],
93
- [1, 2, 5, 5, 2, 6, 3, 0, 4, 3, 4, 7],
94
- [8, 4, 7, 9, 0, 5, 0, 6, 5, 0, 2, 6],
95
- [7, 3, 9, 7, 9, 4, 3, 2, 9, 5, 9, 6, 2, 6, 9],
96
- [3, 11, 2, 7, 8, 4, 10, 6, 5],
97
- [5, 10, 6, 4, 7, 2, 4, 2, 0, 2, 7, 11],
98
- [0, 1, 9, 4, 7, 8, 2, 3, 11, 5, 10, 6],
99
- [9, 2, 1, 9, 11, 2, 9, 4, 11, 7, 11, 4, 5, 10, 6],
100
- [8, 4, 7, 3, 11, 5, 3, 5, 1, 5, 11, 6],
101
- [5, 1, 11, 5, 11, 6, 1, 0, 11, 7, 11, 4, 0, 4, 11],
102
- [0, 5, 9, 0, 6, 5, 0, 3, 6, 11, 6, 3, 8, 4, 7],
103
- [6, 5, 9, 6, 9, 11, 4, 7, 9, 7, 11, 9],
104
- [10, 4, 9, 6, 4, 10],
105
- [4, 10, 6, 4, 9, 10, 0, 8, 3],
106
- [10, 0, 1, 10, 6, 0, 6, 4, 0],
107
- [8, 3, 1, 8, 1, 6, 8, 6, 4, 6, 1, 10],
108
- [1, 4, 9, 1, 2, 4, 2, 6, 4],
109
- [3, 0, 8, 1, 2, 9, 2, 4, 9, 2, 6, 4],
110
- [0, 2, 4, 4, 2, 6],
111
- [8, 3, 2, 8, 2, 4, 4, 2, 6],
112
- [10, 4, 9, 10, 6, 4, 11, 2, 3],
113
- [0, 8, 2, 2, 8, 11, 4, 9, 10, 4, 10, 6],
114
- [3, 11, 2, 0, 1, 6, 0, 6, 4, 6, 1, 10],
115
- [6, 4, 1, 6, 1, 10, 4, 8, 1, 2, 1, 11, 8, 11, 1],
116
- [9, 6, 4, 9, 3, 6, 9, 1, 3, 11, 6, 3],
117
- [8, 11, 1, 8, 1, 0, 11, 6, 1, 9, 1, 4, 6, 4, 1],
118
- [3, 11, 6, 3, 6, 0, 0, 6, 4],
119
- [6, 4, 8, 11, 6, 8],
120
- [7, 10, 6, 7, 8, 10, 8, 9, 10],
121
- [0, 7, 3, 0, 10, 7, 0, 9, 10, 6, 7, 10],
122
- [10, 6, 7, 1, 10, 7, 1, 7, 8, 1, 8, 0],
123
- [10, 6, 7, 10, 7, 1, 1, 7, 3],
124
- [1, 2, 6, 1, 6, 8, 1, 8, 9, 8, 6, 7],
125
- [2, 6, 9, 2, 9, 1, 6, 7, 9, 0, 9, 3, 7, 3, 9],
126
- [7, 8, 0, 7, 0, 6, 6, 0, 2],
127
- [7, 3, 2, 6, 7, 2],
128
- [2, 3, 11, 10, 6, 8, 10, 8, 9, 8, 6, 7],
129
- [2, 0, 7, 2, 7, 11, 0, 9, 7, 6, 7, 10, 9, 10, 7],
130
- [1, 8, 0, 1, 7, 8, 1, 10, 7, 6, 7, 10, 2, 3, 11],
131
- [11, 2, 1, 11, 1, 7, 10, 6, 1, 6, 7, 1],
132
- [8, 9, 6, 8, 6, 7, 9, 1, 6, 11, 6, 3, 1, 3, 6],
133
- [0, 9, 1, 11, 6, 7],
134
- [7, 8, 0, 7, 0, 6, 3, 11, 0, 11, 6, 0],
135
- [7, 11, 6],
136
- [7, 6, 11],
137
- [3, 0, 8, 11, 7, 6],
138
- [0, 1, 9, 11, 7, 6],
139
- [8, 1, 9, 8, 3, 1, 11, 7, 6],
140
- [10, 1, 2, 6, 11, 7],
141
- [1, 2, 10, 3, 0, 8, 6, 11, 7],
142
- [2, 9, 0, 2, 10, 9, 6, 11, 7],
143
- [6, 11, 7, 2, 10, 3, 10, 8, 3, 10, 9, 8],
144
- [7, 2, 3, 6, 2, 7],
145
- [7, 0, 8, 7, 6, 0, 6, 2, 0],
146
- [2, 7, 6, 2, 3, 7, 0, 1, 9],
147
- [1, 6, 2, 1, 8, 6, 1, 9, 8, 8, 7, 6],
148
- [10, 7, 6, 10, 1, 7, 1, 3, 7],
149
- [10, 7, 6, 1, 7, 10, 1, 8, 7, 1, 0, 8],
150
- [0, 3, 7, 0, 7, 10, 0, 10, 9, 6, 10, 7],
151
- [7, 6, 10, 7, 10, 8, 8, 10, 9],
152
- [6, 8, 4, 11, 8, 6],
153
- [3, 6, 11, 3, 0, 6, 0, 4, 6],
154
- [8, 6, 11, 8, 4, 6, 9, 0, 1],
155
- [9, 4, 6, 9, 6, 3, 9, 3, 1, 11, 3, 6],
156
- [6, 8, 4, 6, 11, 8, 2, 10, 1],
157
- [1, 2, 10, 3, 0, 11, 0, 6, 11, 0, 4, 6],
158
- [4, 11, 8, 4, 6, 11, 0, 2, 9, 2, 10, 9],
159
- [10, 9, 3, 10, 3, 2, 9, 4, 3, 11, 3, 6, 4, 6, 3],
160
- [8, 2, 3, 8, 4, 2, 4, 6, 2],
161
- [0, 4, 2, 4, 6, 2],
162
- [1, 9, 0, 2, 3, 4, 2, 4, 6, 4, 3, 8],
163
- [1, 9, 4, 1, 4, 2, 2, 4, 6],
164
- [8, 1, 3, 8, 6, 1, 8, 4, 6, 6, 10, 1],
165
- [10, 1, 0, 10, 0, 6, 6, 0, 4],
166
- [4, 6, 3, 4, 3, 8, 6, 10, 3, 0, 3, 9, 10, 9, 3],
167
- [10, 9, 4, 6, 10, 4],
168
- [4, 9, 5, 7, 6, 11],
169
- [0, 8, 3, 4, 9, 5, 11, 7, 6],
170
- [5, 0, 1, 5, 4, 0, 7, 6, 11],
171
- [11, 7, 6, 8, 3, 4, 3, 5, 4, 3, 1, 5],
172
- [9, 5, 4, 10, 1, 2, 7, 6, 11],
173
- [6, 11, 7, 1, 2, 10, 0, 8, 3, 4, 9, 5],
174
- [7, 6, 11, 5, 4, 10, 4, 2, 10, 4, 0, 2],
175
- [3, 4, 8, 3, 5, 4, 3, 2, 5, 10, 5, 2, 11, 7, 6],
176
- [7, 2, 3, 7, 6, 2, 5, 4, 9],
177
- [9, 5, 4, 0, 8, 6, 0, 6, 2, 6, 8, 7],
178
- [3, 6, 2, 3, 7, 6, 1, 5, 0, 5, 4, 0],
179
- [6, 2, 8, 6, 8, 7, 2, 1, 8, 4, 8, 5, 1, 5, 8],
180
- [9, 5, 4, 10, 1, 6, 1, 7, 6, 1, 3, 7],
181
- [1, 6, 10, 1, 7, 6, 1, 0, 7, 8, 7, 0, 9, 5, 4],
182
- [4, 0, 10, 4, 10, 5, 0, 3, 10, 6, 10, 7, 3, 7, 10],
183
- [7, 6, 10, 7, 10, 8, 5, 4, 10, 4, 8, 10],
184
- [6, 9, 5, 6, 11, 9, 11, 8, 9],
185
- [3, 6, 11, 0, 6, 3, 0, 5, 6, 0, 9, 5],
186
- [0, 11, 8, 0, 5, 11, 0, 1, 5, 5, 6, 11],
187
- [6, 11, 3, 6, 3, 5, 5, 3, 1],
188
- [1, 2, 10, 9, 5, 11, 9, 11, 8, 11, 5, 6],
189
- [0, 11, 3, 0, 6, 11, 0, 9, 6, 5, 6, 9, 1, 2, 10],
190
- [11, 8, 5, 11, 5, 6, 8, 0, 5, 10, 5, 2, 0, 2, 5],
191
- [6, 11, 3, 6, 3, 5, 2, 10, 3, 10, 5, 3],
192
- [5, 8, 9, 5, 2, 8, 5, 6, 2, 3, 8, 2],
193
- [9, 5, 6, 9, 6, 0, 0, 6, 2],
194
- [1, 5, 8, 1, 8, 0, 5, 6, 8, 3, 8, 2, 6, 2, 8],
195
- [1, 5, 6, 2, 1, 6],
196
- [1, 3, 6, 1, 6, 10, 3, 8, 6, 5, 6, 9, 8, 9, 6],
197
- [10, 1, 0, 10, 0, 6, 9, 5, 0, 5, 6, 0],
198
- [0, 3, 8, 5, 6, 10],
199
- [10, 5, 6],
200
- [11, 5, 10, 7, 5, 11],
201
- [11, 5, 10, 11, 7, 5, 8, 3, 0],
202
- [5, 11, 7, 5, 10, 11, 1, 9, 0],
203
- [10, 7, 5, 10, 11, 7, 9, 8, 1, 8, 3, 1],
204
- [11, 1, 2, 11, 7, 1, 7, 5, 1],
205
- [0, 8, 3, 1, 2, 7, 1, 7, 5, 7, 2, 11],
206
- [9, 7, 5, 9, 2, 7, 9, 0, 2, 2, 11, 7],
207
- [7, 5, 2, 7, 2, 11, 5, 9, 2, 3, 2, 8, 9, 8, 2],
208
- [2, 5, 10, 2, 3, 5, 3, 7, 5],
209
- [8, 2, 0, 8, 5, 2, 8, 7, 5, 10, 2, 5],
210
- [9, 0, 1, 5, 10, 3, 5, 3, 7, 3, 10, 2],
211
- [9, 8, 2, 9, 2, 1, 8, 7, 2, 10, 2, 5, 7, 5, 2],
212
- [1, 3, 5, 3, 7, 5],
213
- [0, 8, 7, 0, 7, 1, 1, 7, 5],
214
- [9, 0, 3, 9, 3, 5, 5, 3, 7],
215
- [9, 8, 7, 5, 9, 7],
216
- [5, 8, 4, 5, 10, 8, 10, 11, 8],
217
- [5, 0, 4, 5, 11, 0, 5, 10, 11, 11, 3, 0],
218
- [0, 1, 9, 8, 4, 10, 8, 10, 11, 10, 4, 5],
219
- [10, 11, 4, 10, 4, 5, 11, 3, 4, 9, 4, 1, 3, 1, 4],
220
- [2, 5, 1, 2, 8, 5, 2, 11, 8, 4, 5, 8],
221
- [0, 4, 11, 0, 11, 3, 4, 5, 11, 2, 11, 1, 5, 1, 11],
222
- [0, 2, 5, 0, 5, 9, 2, 11, 5, 4, 5, 8, 11, 8, 5],
223
- [9, 4, 5, 2, 11, 3],
224
- [2, 5, 10, 3, 5, 2, 3, 4, 5, 3, 8, 4],
225
- [5, 10, 2, 5, 2, 4, 4, 2, 0],
226
- [3, 10, 2, 3, 5, 10, 3, 8, 5, 4, 5, 8, 0, 1, 9],
227
- [5, 10, 2, 5, 2, 4, 1, 9, 2, 9, 4, 2],
228
- [8, 4, 5, 8, 5, 3, 3, 5, 1],
229
- [0, 4, 5, 1, 0, 5],
230
- [8, 4, 5, 8, 5, 3, 9, 0, 5, 0, 3, 5],
231
- [9, 4, 5],
232
- [4, 11, 7, 4, 9, 11, 9, 10, 11],
233
- [0, 8, 3, 4, 9, 7, 9, 11, 7, 9, 10, 11],
234
- [1, 10, 11, 1, 11, 4, 1, 4, 0, 7, 4, 11],
235
- [3, 1, 4, 3, 4, 8, 1, 10, 4, 7, 4, 11, 10, 11, 4],
236
- [4, 11, 7, 9, 11, 4, 9, 2, 11, 9, 1, 2],
237
- [9, 7, 4, 9, 11, 7, 9, 1, 11, 2, 11, 1, 0, 8, 3],
238
- [11, 7, 4, 11, 4, 2, 2, 4, 0],
239
- [11, 7, 4, 11, 4, 2, 8, 3, 4, 3, 2, 4],
240
- [2, 9, 10, 2, 7, 9, 2, 3, 7, 7, 4, 9],
241
- [9, 10, 7, 9, 7, 4, 10, 2, 7, 8, 7, 0, 2, 0, 7],
242
- [3, 7, 10, 3, 10, 2, 7, 4, 10, 1, 10, 0, 4, 0, 10],
243
- [1, 10, 2, 8, 7, 4],
244
- [4, 9, 1, 4, 1, 7, 7, 1, 3],
245
- [4, 9, 1, 4, 1, 7, 0, 8, 1, 8, 7, 1],
246
- [4, 0, 3, 7, 4, 3],
247
- [4, 8, 7],
248
- [9, 10, 8, 10, 11, 8],
249
- [3, 0, 9, 3, 9, 11, 11, 9, 10],
250
- [0, 1, 10, 0, 10, 8, 8, 10, 11],
251
- [3, 1, 10, 11, 3, 10],
252
- [1, 2, 11, 1, 11, 9, 9, 11, 8],
253
- [3, 0, 9, 3, 9, 11, 1, 2, 9, 2, 11, 9],
254
- [0, 2, 11, 8, 0, 11],
255
- [3, 2, 11],
256
- [2, 3, 8, 2, 8, 10, 10, 8, 9],
257
- [9, 10, 2, 0, 9, 2],
258
- [2, 3, 8, 2, 8, 10, 0, 1, 8, 1, 10, 8],
259
- [1, 10, 2],
260
- [1, 3, 8, 9, 1, 8],
261
- [0, 9, 1],
262
- [0, 3, 8],
263
- [],
264
- ];
265
- // Vertex positions for the 8 corners of a unit cube - flat arrays for speed
266
- // Format: [x0,y0,z0, x1,y1,z1, ...] - access as CUBE_VERTS_X[i], etc.
267
- const CUBE_VERTS_X = new Int8Array([0, 1, 1, 0, 0, 1, 1, 0]);
268
- const CUBE_VERTS_Y = new Int8Array([0, 0, 1, 1, 0, 0, 1, 1]);
269
- const CUBE_VERTS_Z = new Int8Array([0, 0, 0, 0, 1, 1, 1, 1]);
270
- // Edge definitions: pairs of vertex indices for each of 12 edges
271
- // Flattened for direct access: edge i has vertices EDGE_V1[i] and EDGE_V2[i]
272
- const EDGE_V1 = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3]);
273
- const EDGE_V2 = new Uint8Array([1, 2, 3, 0, 5, 6, 7, 4, 4, 5, 6, 7]);
274
- // Compute gradient (normal) at a grid point using central differences
275
- function compute_gradient(grid, ix, iy, iz, nx, ny, nz, periodic) {
276
- // Wrap for periodic, clamp for non-periodic boundaries
277
- const wrap = (val, n) => ((val % n) + n) % n;
278
- const clamp = (val, max) => Math.max(0, Math.min(val, max));
279
- const [ix_w, iy_w, iz_w] = periodic
280
- ? [wrap(ix, nx), wrap(iy, ny), wrap(iz, nz)]
281
- : [clamp(ix, nx - 1), clamp(iy, ny - 1), clamp(iz, nz - 1)];
282
- const [ix_m, ix_p] = periodic
283
- ? [wrap(ix - 1, nx), wrap(ix + 1, nx)]
284
- : [Math.max(0, ix - 1), Math.min(nx - 1, ix + 1)];
285
- const [iy_m, iy_p] = periodic
286
- ? [wrap(iy - 1, ny), wrap(iy + 1, ny)]
287
- : [Math.max(0, iy - 1), Math.min(ny - 1, iy + 1)];
288
- const [iz_m, iz_p] = periodic
289
- ? [wrap(iz - 1, nz), wrap(iz + 1, nz)]
290
- : [Math.max(0, iz - 1), Math.min(nz - 1, iz + 1)];
291
- const dx = (grid[ix_p][iy_w][iz_w] - grid[ix_m][iy_w][iz_w]) * 0.5;
292
- const dy = (grid[ix_w][iy_p][iz_w] - grid[ix_w][iy_m][iz_w]) * 0.5;
293
- const dz = (grid[ix_w][iy_w][iz_p] - grid[ix_w][iy_w][iz_m]) * 0.5;
294
- const len_sq = dx * dx + dy * dy + dz * dz;
295
- if (len_sq < 1e-20)
296
- return [0, 0, 1];
297
- const inv_len = 1 / Math.sqrt(len_sq);
298
- return [-dx * inv_len, -dy * inv_len, -dz * inv_len];
299
- }
300
- // Main marching cubes algorithm (optimized version)
301
- export function marching_cubes(grid, iso_value, k_lattice, options = {}) {
302
- const { periodic = true, interpolate = true, centered = true } = options;
303
- // When centered=true, shift fractional coordinates by -0.5 so the grid is
304
- // centered at the origin (Γ point). This is needed for proper BZ visualization.
305
- const center_offset = centered ? 0.5 : 0;
306
- const nx = grid.length;
307
- const ny = grid[0]?.length || 0;
308
- const nz = grid[0]?.[0]?.length || 0;
309
- if (nx < 2 || ny < 2 || nz < 2) {
310
- return { vertices: [], faces: [], normals: [] };
311
- }
312
- const vertices = [];
313
- const faces = [];
314
- const normals = [];
315
- // Precompute grid dimension products for flattening and cache keys
316
- const ny_nz = ny * nz;
317
- const max_flat = nx * ny_nz; // for computing cache keys
318
- // Use numeric cache key - safe for grids up to ~300³ (2^53 / 2 / max_flat)
319
- // For much larger grids (>30M cells), consider switching to Map<string, number>
320
- // with keys like `${flat1},${flat2}` or Map<bigint, number> to avoid
321
- // Number.MAX_SAFE_INTEGER limits. The current approach is faster for typical grids.
322
- if (max_flat > 30_000_000) {
323
- console.warn(`Grid size ${nx}×${ny}×${nz} may cause cache key overflow`);
324
- }
325
- const vertex_cache = new Map();
326
- // Safe modulo wrapping helper (handles negative values correctly)
327
- const wrap = (val, size) => ((val % size) + size) % size;
328
- // Precompute k_lattice values for faster coordinate transform
329
- const [kx0, kx1, kx2] = k_lattice[0];
330
- const [ky0, ky1, ky2] = k_lattice[1];
331
- const [kz0, kz1, kz2] = k_lattice[2];
332
- // Precompute inverse grid sizes
333
- // Use (n-1) because n grid points span [0,1] with spacing 1/(n-1)
334
- // Grid index 0 maps to 0, grid index (n-1) maps to 1
335
- // This matches scikit-image's marching cubes coordinate convention
336
- const inv_nx = 1 / (nx - 1);
337
- const inv_ny = 1 / (ny - 1);
338
- const inv_nz = 1 / (nz - 1);
339
- // Get or create vertex on an edge (fully optimized with flat array lookups)
340
- const get_vertex_on_edge = (ix, iy, iz, edge_idx, cube_values) => {
341
- // Use flat arrays instead of destructuring
342
- const v1_idx = EDGE_V1[edge_idx];
343
- const v2_idx = EDGE_V2[edge_idx];
344
- const ox1 = CUBE_VERTS_X[v1_idx];
345
- const oy1 = CUBE_VERTS_Y[v1_idx];
346
- const oz1 = CUBE_VERTS_Z[v1_idx];
347
- const ox2 = CUBE_VERTS_X[v2_idx];
348
- const oy2 = CUBE_VERTS_Y[v2_idx];
349
- const oz2 = CUBE_VERTS_Z[v2_idx];
350
- // Compute wrapped grid positions using safe modulo for periodic boundaries
351
- const g1x = periodic ? wrap(ix + ox1, nx) : ix + ox1;
352
- const g1y = periodic ? wrap(iy + oy1, ny) : iy + oy1;
353
- const g1z = periodic ? wrap(iz + oz1, nz) : iz + oz1;
354
- const g2x = periodic ? wrap(ix + ox2, nx) : ix + ox2;
355
- const g2y = periodic ? wrap(iy + oy2, ny) : iy + oy2;
356
- const g2z = periodic ? wrap(iz + oz2, nz) : iz + oz2;
357
- // Create numeric cache key (sorted for consistency)
358
- // Safe for grids up to ~300³ before exceeding Number.MAX_SAFE_INTEGER
359
- const flat1 = g1x * ny_nz + g1y * nz + g1z;
360
- const flat2 = g2x * ny_nz + g2y * nz + g2z;
361
- const cache_key = flat1 < flat2 ? flat1 * max_flat + flat2 : flat2 * max_flat + flat1;
362
- const cached = vertex_cache.get(cache_key);
363
- if (cached !== undefined)
364
- return cached;
365
- // Compute vertex position
366
- const v1 = cube_values[v1_idx];
367
- const v2 = cube_values[v2_idx];
368
- let fx, fy, fz;
369
- if (interpolate) {
370
- const f1x = (ix + ox1) * inv_nx - center_offset;
371
- const f1y = (iy + oy1) * inv_ny - center_offset;
372
- const f1z = (iz + oz1) * inv_nz - center_offset;
373
- const f2x = (ix + ox2) * inv_nx - center_offset;
374
- const f2y = (iy + oy2) * inv_ny - center_offset;
375
- const f2z = (iz + oz2) * inv_nz - center_offset;
376
- const dv = v2 - v1;
377
- if (Math.abs(dv) < 1e-10) {
378
- fx = f1x;
379
- fy = f1y;
380
- fz = f1z;
381
- }
382
- else {
383
- const t = (iso_value - v1) / dv;
384
- fx = f1x + t * (f2x - f1x);
385
- fy = f1y + t * (f2y - f1y);
386
- fz = f1z + t * (f2z - f1z);
387
- }
388
- }
389
- else {
390
- fx = (ix + (ox1 + ox2) * 0.5) * inv_nx - center_offset;
391
- fy = (iy + (oy1 + oy2) * 0.5) * inv_ny - center_offset;
392
- fz = (iz + (oz1 + oz2) * 0.5) * inv_nz - center_offset;
393
- }
394
- // Transform to Cartesian (inlined)
395
- const vert_idx = vertices.length;
396
- vertices.push([
397
- fx * kx0 + fy * ky0 + fz * kz0,
398
- fx * kx1 + fy * ky1 + fz * kz1,
399
- fx * kx2 + fy * ky2 + fz * kz2,
400
- ]);
401
- // Compute normal (simplified - skip gradient interpolation for speed)
402
- const norm = compute_gradient(grid, ix + ox1, iy + oy1, iz + oz1, nx, ny, nz, periodic);
403
- normals.push(norm);
404
- vertex_cache.set(cache_key, vert_idx);
405
- return vert_idx;
406
- };
407
- // Iterate over all cubes in the grid
408
- const max_x = periodic ? nx : nx - 1;
409
- const max_y = periodic ? ny : ny - 1;
410
- const max_z = periodic ? nz : nz - 1;
411
- // Preallocate cube_values array (reuse across iterations)
412
- const cube_values = new Array(8);
413
- for (let ix = 0; ix < max_x; ix++) {
414
- const ix_row = grid[ix];
415
- const ix1_row = grid[(ix + 1) % nx];
416
- for (let iy = 0; iy < max_y; iy++) {
417
- const iy_col = ix_row[iy];
418
- const iy1_col = ix_row[(iy + 1) % ny];
419
- const ix1_iy_col = ix1_row[iy];
420
- const ix1_iy1_col = ix1_row[(iy + 1) % ny];
421
- for (let iz = 0; iz < max_z; iz++) {
422
- const iz1 = (iz + 1) % nz;
423
- // Get corner values (inlined for speed)
424
- cube_values[0] = iy_col[iz];
425
- cube_values[1] = ix1_iy_col[iz];
426
- cube_values[2] = ix1_iy1_col[iz];
427
- cube_values[3] = iy1_col[iz];
428
- cube_values[4] = iy_col[iz1];
429
- cube_values[5] = ix1_iy_col[iz1];
430
- cube_values[6] = ix1_iy1_col[iz1];
431
- cube_values[7] = iy1_col[iz1];
432
- // Compute cube index (unrolled for speed)
433
- let cube_index = 0;
434
- if (cube_values[0] < iso_value)
435
- cube_index |= 1;
436
- if (cube_values[1] < iso_value)
437
- cube_index |= 2;
438
- if (cube_values[2] < iso_value)
439
- cube_index |= 4;
440
- if (cube_values[3] < iso_value)
441
- cube_index |= 8;
442
- if (cube_values[4] < iso_value)
443
- cube_index |= 16;
444
- if (cube_values[5] < iso_value)
445
- cube_index |= 32;
446
- if (cube_values[6] < iso_value)
447
- cube_index |= 64;
448
- if (cube_values[7] < iso_value)
449
- cube_index |= 128;
450
- // Skip if cube is entirely inside or outside
451
- if (EDGE_TABLE[cube_index] === 0)
452
- continue;
453
- // Get triangles for this cube configuration
454
- const tri_list = TRI_TABLE[cube_index];
455
- const tri_len = tri_list.length;
456
- // Create triangles
457
- for (let tri_idx = 0; tri_idx < tri_len; tri_idx += 3) {
458
- const v0 = get_vertex_on_edge(ix, iy, iz, tri_list[tri_idx], cube_values);
459
- const v1 = get_vertex_on_edge(ix, iy, iz, tri_list[tri_idx + 1], cube_values);
460
- const v2 = get_vertex_on_edge(ix, iy, iz, tri_list[tri_idx + 2], cube_values);
461
- // Skip degenerate triangles
462
- if (v0 !== v1 && v1 !== v2 && v0 !== v2) {
463
- faces.push([v0, v1, v2]);
464
- }
465
- }
466
- }
467
- }
468
- }
469
- return { vertices, faces, normals };
470
- }
471
- // Compute per-vertex normals from faces using area-weighted averaging
472
- // Uses fan triangulation for N-gon faces (quads, etc.)
473
- export function compute_vertex_normals(vertices, faces) {
474
- const normals = vertices.map(() => [0, 0, 0]);
475
- for (const face of faces) {
476
- // Validate face has at least 3 indices and all are within bounds
477
- if (face.length < 3)
478
- continue;
479
- if (face.some((idx) => idx < 0 || idx >= vertices.length))
480
- continue;
481
- // Fan triangulation: for N vertices, process N-2 triangles (0,1,2), (0,2,3), ...
482
- const v0 = vertices[face[0]];
483
- for (let fan_idx = 1; fan_idx < face.length - 1; fan_idx++) {
484
- const idx1 = face[fan_idx];
485
- const idx2 = face[fan_idx + 1];
486
- const v1 = vertices[idx1];
487
- const v2 = vertices[idx2];
488
- // Edge vectors
489
- const e1 = [v1[0] - v0[0], v1[1] - v0[1], v1[2] - v0[2]];
490
- const e2 = [v2[0] - v0[0], v2[1] - v0[1], v2[2] - v0[2]];
491
- // Cross product (face normal * 2 * area)
492
- const normal = [
493
- e1[1] * e2[2] - e1[2] * e2[1],
494
- e1[2] * e2[0] - e1[0] * e2[2],
495
- e1[0] * e2[1] - e1[1] * e2[0],
496
- ];
497
- // Add to the 3 vertices of this triangle
498
- normals[face[0]][0] += normal[0];
499
- normals[face[0]][1] += normal[1];
500
- normals[face[0]][2] += normal[2];
501
- normals[idx1][0] += normal[0];
502
- normals[idx1][1] += normal[1];
503
- normals[idx1][2] += normal[2];
504
- normals[idx2][0] += normal[0];
505
- normals[idx2][1] += normal[1];
506
- normals[idx2][2] += normal[2];
507
- }
508
- }
509
- // Normalize all normals
510
- for (const normal of normals) {
511
- const len = Math.sqrt(normal[0] ** 2 + normal[1] ** 2 + normal[2] ** 2);
512
- if (len > 0) {
513
- normal[0] /= len;
514
- normal[1] /= len;
515
- normal[2] /= len;
516
- }
517
- }
518
- return normals;
519
- }
1
+ // Re-export from shared location for backward compatibility
2
+ export { compute_vertex_normals, marching_cubes } from '../marching-cubes';
@@ -1,24 +1,15 @@
1
1
  import * as constants from './constants';
2
2
  import { compute_vertex_normals } from './marching-cubes';
3
+ function parse_number_tokens(line) {
4
+ return line.split(/\s+/).filter(Boolean);
5
+ }
3
6
  // Parse whitespace-separated floats from a line (optimized with unary +)
4
7
  function parse_floats(line) {
5
- const parts = line.split(/\s+/);
6
- const result = [];
7
- for (let idx = 0; idx < parts.length; idx++) {
8
- if (parts[idx])
9
- result.push(+parts[idx]);
10
- }
11
- return result;
8
+ return parse_number_tokens(line).map((part) => +part);
12
9
  }
13
10
  // Parse whitespace-separated integers from a line
14
11
  function parse_ints(line) {
15
- const parts = line.split(/\s+/);
16
- const result = [];
17
- for (let idx = 0; idx < parts.length; idx++) {
18
- if (parts[idx])
19
- result.push(parseInt(parts[idx], 10));
20
- }
21
- return result;
12
+ return parse_number_tokens(line).map((part) => parseInt(part, 10));
22
13
  }
23
14
  // Parse BXSF (Band-XSF) format used by XCrySDen, Quantum ESPRESSO, etc.
24
15
  // Format specification: http://www.xcrysden.org/doc/XSF.html
@@ -231,18 +222,21 @@ function parse_frmsf(content) {
231
222
  function is_valid_isosurface(obj) {
232
223
  if (!obj || typeof obj !== `object`)
233
224
  return false;
234
- const iso = obj;
235
- // Required array fields
236
- if (!Array.isArray(iso.vertices) || iso.vertices.length === 0)
225
+ const isosurface_obj = obj;
226
+ const vertices = isosurface_obj.vertices;
227
+ const faces = isosurface_obj.faces;
228
+ const normals = isosurface_obj.normals;
229
+ const band_index = isosurface_obj.band_index;
230
+ const spin = isosurface_obj.spin;
231
+ if (!Array.isArray(vertices) || vertices.length === 0)
237
232
  return false;
238
- if (!Array.isArray(iso.faces))
233
+ if (!Array.isArray(faces))
239
234
  return false;
240
- if (!Array.isArray(iso.normals))
235
+ if (!Array.isArray(normals))
241
236
  return false;
242
- // Required scalar fields
243
- if (typeof iso.band_index !== `number`)
237
+ if (typeof band_index !== `number` || !Number.isFinite(band_index))
244
238
  return false;
245
- if (iso.spin !== null && iso.spin !== `up` && iso.spin !== `down`)
239
+ if (spin !== null && spin !== `up` && spin !== `down`)
246
240
  return false;
247
241
  return true;
248
242
  }
@@ -389,7 +383,7 @@ function parse_ifermi_surface(data) {
389
383
  e1[2] * e2[0] - e1[0] * e2[2],
390
384
  e1[0] * e2[1] - e1[1] * e2[0],
391
385
  ];
392
- area += 0.5 * Math.sqrt(cross[0] ** 2 + cross[1] ** 2 + cross[2] ** 2);
386
+ area += 0.5 * Math.hypot(cross[0], cross[1], cross[2]);
393
387
  }
394
388
  }
395
389
  isosurfaces.push({