ball-and-stick.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 { getElementSphereVisual, ElementSphereParams } from '../visual/element-sphere';
  7. import { IntraUnitBondVisual, IntraUnitBondParams } from '../visual/bond-intra-unit-cylinder';
  8. import { InterUnitBondVisual, InterUnitBondParams } from '../visual/bond-inter-unit-cylinder';
  9. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  10. import { UnitsRepresentation } from '../units-representation';
  11. import { ComplexRepresentation } from '../complex-representation';
  12. import { StructureRepresentation, StructureRepresentationProvider, StructureRepresentationStateBuilder } from '../representation';
  13. import { Representation, RepresentationParamsGetter, RepresentationContext } from '../../../mol-repr/representation';
  14. import { ThemeRegistryContext } from '../../../mol-theme/theme';
  15. import { Structure } from '../../../mol-model/structure';
  16. import { getUnitKindsParam } from '../params';
  17. const BallAndStickVisuals = {
  18. 'element-sphere': (ctx: RepresentationContext, getParams: RepresentationParamsGetter<Structure, ElementSphereParams>) => UnitsRepresentation('Element sphere mesh', ctx, getParams, getElementSphereVisual(ctx.webgl)),
  19. 'intra-bond': (ctx: RepresentationContext, getParams: RepresentationParamsGetter<Structure, IntraUnitBondParams>) => UnitsRepresentation('Intra-unit bond cylinder', ctx, getParams, IntraUnitBondVisual),
  20. 'inter-bond': (ctx: RepresentationContext, getParams: RepresentationParamsGetter<Structure, InterUnitBondParams>) => ComplexRepresentation('Inter-unit bond cylinder', ctx, getParams, InterUnitBondVisual),
  21. }
  22. export const BallAndStickParams = {
  23. ...ElementSphereParams,
  24. ...IntraUnitBondParams,
  25. ...InterUnitBondParams,
  26. unitKinds: getUnitKindsParam(['atomic']),
  27. sizeFactor: PD.Numeric(0.15, { min: 0.01, max: 10, step: 0.01 }),
  28. sizeAspectRatio: PD.Numeric(2/3, { min: 0.01, max: 3, step: 0.01 }),
  29. visuals: PD.MultiSelect(['element-sphere', 'intra-bond', 'inter-bond'], PD.objectToOptions(BallAndStickVisuals))
  30. }
  31. export type BallAndStickParams = typeof BallAndStickParams
  32. export function getBallAndStickParams(ctx: ThemeRegistryContext, structure: Structure) {
  33. return PD.clone(BallAndStickParams)
  34. }
  35. export type BallAndStickRepresentation = StructureRepresentation<BallAndStickParams>
  36. export function BallAndStickRepresentation(ctx: RepresentationContext, getParams: RepresentationParamsGetter<Structure, BallAndStickParams>): BallAndStickRepresentation {
  37. return Representation.createMulti('Ball & Stick', ctx, getParams, StructureRepresentationStateBuilder, BallAndStickVisuals as unknown as Representation.Def<Structure, BallAndStickParams>)
  38. }
  39. export const BallAndStickRepresentationProvider = StructureRepresentationProvider({
  40. name: 'ball-and-stick',
  41. label: 'Ball & Stick',
  42. description: 'Displays atoms as spheres and bonds as cylinders.',
  43. factory: BallAndStickRepresentation,
  44. getParams: getBallAndStickParams,
  45. defaultValues: PD.getDefaultValues(BallAndStickParams),
  46. defaultColorTheme: { name: 'element-symbol' },
  47. defaultSizeTheme: { name: 'physical' },
  48. isApplicable: (structure: Structure) => structure.elementCount > 0
  49. })