util.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**
  2. * Copyright (c) 2018-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { defaults } from '../mol-util';
  7. import { Structure } from '../mol-model/structure';
  8. import { VisualQuality } from '../mol-geo/geometry/base';
  9. import { Box3D, SpacegroupCell } from '../mol-math/geometry';
  10. import { ModelSymmetry } from '../mol-model-formats/structure/property/symmetry';
  11. import { Volume } from '../mol-model/volume';
  12. export interface VisualUpdateState {
  13. updateTransform: boolean
  14. updateMatrix: boolean
  15. updateColor: boolean
  16. updateSize: boolean
  17. createGeometry: boolean
  18. createNew: boolean
  19. /** holds contextual info, is not reset */
  20. info: { [k: string]: unknown }
  21. }
  22. export namespace VisualUpdateState {
  23. export function create(): VisualUpdateState {
  24. return {
  25. updateTransform: false,
  26. updateMatrix: false,
  27. updateColor: false,
  28. updateSize: false,
  29. createGeometry: false,
  30. createNew: false,
  31. info: {}
  32. };
  33. }
  34. export function reset(state: VisualUpdateState) {
  35. state.updateTransform = false;
  36. state.updateMatrix = false;
  37. state.updateColor = false;
  38. state.updateSize = false;
  39. state.createGeometry = false;
  40. state.createNew = false;
  41. }
  42. }
  43. //
  44. export interface QualityProps {
  45. quality: VisualQuality
  46. detail: number
  47. radialSegments: number
  48. linearSegments: number
  49. resolution: number
  50. probePositions: number
  51. doubleSided: boolean
  52. xrayShaded: boolean
  53. alpha: number
  54. transparentBackfaces: 'off' | 'on' | 'opaque'
  55. }
  56. export const DefaultQualityThresholds = {
  57. lowestElementCount: 1_000_000,
  58. lowerElementCount: 500_000,
  59. lowElementCount: 100_000,
  60. mediumElementCount: 20_000,
  61. highElementCount: 2_000,
  62. coarseGrainedFactor: 10,
  63. elementCountFactor: 1
  64. };
  65. export type QualityThresholds = typeof DefaultQualityThresholds
  66. export function getStructureQuality(structure: Structure, tresholds: Partial<QualityThresholds> = {}): VisualQuality {
  67. const t = { ...DefaultQualityThresholds, ...tresholds };
  68. let score = structure.elementCount * t.elementCountFactor;
  69. if (structure.isCoarseGrained) score *= t.coarseGrainedFactor;
  70. if (score > t.lowestElementCount) {
  71. return 'lowest';
  72. } else if (score > t.lowerElementCount) {
  73. return 'lower';
  74. } else if (score > t.lowElementCount) {
  75. return 'low';
  76. } else if (score > t.mediumElementCount) {
  77. return 'medium';
  78. } else if (score > t.highElementCount) {
  79. return 'high';
  80. } else {
  81. return 'higher';
  82. }
  83. }
  84. /**
  85. * Uses cell volume to avoid costly boundary calculation if
  86. * - single model
  87. * - non-empty 'P 1' spacegroup
  88. */
  89. function getRootVolume(structure: Structure) {
  90. if (structure.root.models.length === 1) {
  91. const sym = ModelSymmetry.Provider.get(structure.root.model);
  92. if (sym && sym.spacegroup.name === 'P 1' && !SpacegroupCell.isZero(sym.spacegroup.cell)) {
  93. return sym.spacegroup.cell.volume;
  94. }
  95. }
  96. return Box3D.volume(structure.root.boundary.box);
  97. }
  98. export function getQualityProps(props: Partial<QualityProps>, data?: any) {
  99. let quality = defaults(props.quality, 'auto' as VisualQuality);
  100. let detail = defaults(props.detail, 1);
  101. let radialSegments = defaults(props.radialSegments, 12);
  102. let linearSegments = defaults(props.linearSegments, 8);
  103. let resolution = defaults(props.resolution, 2);
  104. let probePositions = defaults(props.probePositions, 12);
  105. let doubleSided = defaults(props.doubleSided, true);
  106. let volume = 0;
  107. if (quality === 'auto') {
  108. if (data instanceof Structure) {
  109. quality = getStructureQuality(data.root);
  110. volume = getRootVolume(data);
  111. } else if (Volume.is(data)) {
  112. const [x, y, z] = data.grid.cells.space.dimensions;
  113. volume = x * y * z;
  114. quality = volume < 10_000_000 ? 'medium' : 'low';
  115. }
  116. }
  117. switch (quality) {
  118. case 'highest':
  119. detail = 3;
  120. radialSegments = 36;
  121. linearSegments = 18;
  122. resolution = 0.1;
  123. probePositions = 72;
  124. doubleSided = true;
  125. break;
  126. case 'higher':
  127. detail = 3;
  128. radialSegments = 28;
  129. linearSegments = 14;
  130. resolution = 0.3;
  131. probePositions = 48;
  132. doubleSided = true;
  133. break;
  134. case 'high':
  135. detail = 2;
  136. radialSegments = 20;
  137. linearSegments = 10;
  138. resolution = 0.5;
  139. probePositions = 36;
  140. doubleSided = true;
  141. break;
  142. case 'medium':
  143. detail = 1;
  144. radialSegments = 12;
  145. linearSegments = 8;
  146. resolution = 0.8;
  147. probePositions = 24;
  148. doubleSided = true;
  149. break;
  150. case 'low':
  151. detail = 0;
  152. radialSegments = 8;
  153. linearSegments = 3;
  154. resolution = 1.3;
  155. probePositions = 24;
  156. doubleSided = false;
  157. break;
  158. case 'lower':
  159. detail = 0;
  160. radialSegments = 4;
  161. linearSegments = 2;
  162. resolution = 3;
  163. probePositions = 12;
  164. doubleSided = false;
  165. break;
  166. case 'lowest':
  167. detail = 0;
  168. radialSegments = 2;
  169. linearSegments = 1;
  170. resolution = 8;
  171. probePositions = 12;
  172. doubleSided = false;
  173. break;
  174. case 'custom':
  175. // use defaults or given props as set above
  176. break;
  177. }
  178. // max resolution based on volume (for 'auto' quality)
  179. resolution = Math.max(resolution, volume / 500_000_000);
  180. resolution = Math.min(resolution, 20);
  181. if (props.transparentBackfaces === 'off' && ((props.alpha !== undefined && props.alpha < 1) || !!props.xrayShaded)) {
  182. doubleSided = false;
  183. }
  184. return {
  185. detail,
  186. radialSegments,
  187. linearSegments,
  188. resolution,
  189. probePositions,
  190. doubleSided
  191. };
  192. }