line.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * Copyright (c) 2020-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { IntraUnitBondLineVisual, IntraUnitBondLineParams } from '../visual/bond-intra-unit-line';
  7. import { InterUnitBondLineVisual, InterUnitBondLineParams } from '../visual/bond-inter-unit-line';
  8. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  9. import { UnitsRepresentation } from '../units-representation';
  10. import { ComplexRepresentation } from '../complex-representation';
  11. import { StructureRepresentation, StructureRepresentationProvider, StructureRepresentationStateBuilder } from '../representation';
  12. import { Representation, RepresentationParamsGetter, RepresentationContext } from '../../../mol-repr/representation';
  13. import { ThemeRegistryContext } from '../../../mol-theme/theme';
  14. import { Structure } from '../../../mol-model/structure';
  15. import { getUnitKindsParam } from '../params';
  16. import { ElementPointParams, ElementPointVisual } from '../visual/element-point';
  17. import { ElementCrossParams, ElementCrossVisual } from '../visual/element-cross';
  18. import { Points } from '../../../mol-geo/geometry/points/points';
  19. const LineVisuals = {
  20. 'intra-bond': (ctx: RepresentationContext, getParams: RepresentationParamsGetter<Structure, IntraUnitBondLineParams>) => UnitsRepresentation('Intra-unit bond line', ctx, getParams, IntraUnitBondLineVisual),
  21. 'inter-bond': (ctx: RepresentationContext, getParams: RepresentationParamsGetter<Structure, InterUnitBondLineParams>) => ComplexRepresentation('Inter-unit bond line', ctx, getParams, InterUnitBondLineVisual),
  22. 'element-point': (ctx: RepresentationContext, getParams: RepresentationParamsGetter<Structure, ElementPointParams>) => UnitsRepresentation('Points', ctx, getParams, ElementPointVisual),
  23. 'element-cross': (ctx: RepresentationContext, getParams: RepresentationParamsGetter<Structure, ElementCrossParams>) => UnitsRepresentation('Crosses', ctx, getParams, ElementCrossVisual),
  24. };
  25. export const LineParams = {
  26. ...IntraUnitBondLineParams,
  27. ...InterUnitBondLineParams,
  28. ...ElementPointParams,
  29. ...ElementCrossParams,
  30. pointStyle: PD.Select('circle', PD.objectToOptions(Points.StyleTypes)),
  31. multipleBonds: PD.Select('offset', PD.arrayToOptions(['off', 'symmetric', 'offset'] as const)),
  32. includeParent: PD.Boolean(false),
  33. sizeFactor: PD.Numeric(2, { min: 0.01, max: 10, step: 0.01 }),
  34. unitKinds: getUnitKindsParam(['atomic']),
  35. visuals: PD.MultiSelect(['intra-bond', 'inter-bond', 'element-point', 'element-cross'], PD.objectToOptions(LineVisuals))
  36. };
  37. export type LineParams = typeof LineParams
  38. export function getLineParams(ctx: ThemeRegistryContext, structure: Structure) {
  39. return LineParams;
  40. }
  41. export type LineRepresentation = StructureRepresentation<LineParams>
  42. export function LineRepresentation(ctx: RepresentationContext, getParams: RepresentationParamsGetter<Structure, LineParams>): LineRepresentation {
  43. return Representation.createMulti('Line', ctx, getParams, StructureRepresentationStateBuilder, LineVisuals as unknown as Representation.Def<Structure, LineParams>);
  44. }
  45. export const LineRepresentationProvider = StructureRepresentationProvider({
  46. name: 'line',
  47. label: 'Line',
  48. description: 'Displays bonds as lines and atoms as points or croses.',
  49. factory: LineRepresentation,
  50. getParams: getLineParams,
  51. defaultValues: PD.getDefaultValues(LineParams),
  52. defaultColorTheme: { name: 'element-symbol' },
  53. defaultSizeTheme: { name: 'uniform' },
  54. isApplicable: (structure: Structure) => structure.elementCount > 0,
  55. getData: (structure: Structure, props: PD.Values<LineParams>) => {
  56. return props.includeParent ? structure.asParent() : structure;
  57. },
  58. mustRecreate: (oldProps: PD.Values<LineParams>, newProps: PD.Values<LineParams>) => {
  59. return oldProps.includeParent !== newProps.includeParent;
  60. }
  61. });