physical.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { StructureElement, Unit, Bond, ElementIndex } from '../../mol-model/structure';
  7. import { Location } from '../../mol-model/location';
  8. import { SizeTheme } from '../size';
  9. import { VdwRadius } from '../../mol-model/structure/model/properties/atomic';
  10. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  11. import { ThemeDataContext } from '../../mol-theme/theme';
  12. const DefaultSize = 1;
  13. const Description = 'Assigns a physical size, i.e. vdW radius for atoms or given radius for coarse spheres.';
  14. export const PhysicalSizeThemeParams = {
  15. scale: PD.Numeric(1, { min: 0.1, max: 5, step: 0.1 })
  16. };
  17. export type PhysicalSizeThemeParams = typeof PhysicalSizeThemeParams
  18. export function getPhysicalSizeThemeParams(ctx: ThemeDataContext) {
  19. return PhysicalSizeThemeParams; // TODO return copy
  20. }
  21. export function getPhysicalRadius(unit: Unit, element: ElementIndex): number {
  22. if (Unit.isAtomic(unit)) {
  23. return VdwRadius(unit.model.atomicHierarchy.atoms.type_symbol.value(element));
  24. } else if (Unit.isSpheres(unit)) {
  25. return unit.model.coarseConformation.spheres.radius[element];
  26. } else {
  27. return 0;
  28. }
  29. }
  30. /**
  31. * Create attribute data with the physical size of an element,
  32. * i.e. vdw for atoms and radius for coarse spheres
  33. */
  34. export function PhysicalSizeTheme(ctx: ThemeDataContext, props: PD.Values<PhysicalSizeThemeParams>): SizeTheme<PhysicalSizeThemeParams> {
  35. const scale = props.scale === void 0 ? 1 : props.scale;
  36. function size(location: Location): number {
  37. let size: number;
  38. if (StructureElement.Location.is(location)) {
  39. size = scale * getPhysicalRadius(location.unit, location.element);
  40. } else if (Bond.isLocation(location)) {
  41. size = scale * Math.min(
  42. getPhysicalRadius(location.aUnit, location.aUnit.elements[location.aIndex]),
  43. getPhysicalRadius(location.bUnit, location.bUnit.elements[location.bIndex])
  44. );
  45. } else {
  46. size = scale * DefaultSize;
  47. }
  48. return size;
  49. }
  50. return {
  51. factory: PhysicalSizeTheme,
  52. granularity: 'group',
  53. size,
  54. props,
  55. description: Description
  56. };
  57. }
  58. export const PhysicalSizeThemeProvider: SizeTheme.Provider<PhysicalSizeThemeParams, 'physical'> = {
  59. name: 'physical',
  60. label: 'Physical',
  61. category: '',
  62. factory: PhysicalSizeTheme,
  63. getParams: getPhysicalSizeThemeParams,
  64. defaultValues: PD.getDefaultValues(PhysicalSizeThemeParams),
  65. isApplicable: (ctx: ThemeDataContext) => !!ctx.structure
  66. };