element-cross.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * Copyright (c) 2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  7. import { UnitsVisual, UnitsLinesParams, UnitsLinesVisual } from '../units-visual';
  8. import { VisualContext } from '../../visual';
  9. import { Unit, Structure, StructureElement } from '../../../mol-model/structure';
  10. import { Theme } from '../../../mol-theme/theme';
  11. import { Vec3 } from '../../../mol-math/linear-algebra';
  12. import { ElementIterator, getElementLoci, eachElement, makeElementIgnoreTest } from './util/element';
  13. import { VisualUpdateState } from '../../util';
  14. import { Sphere3D } from '../../../mol-math/geometry';
  15. import { Lines } from '../../../mol-geo/geometry/lines/lines';
  16. import { LinesBuilder } from '../../../mol-geo/geometry/lines/lines-builder';
  17. import { bondCount } from '../../../mol-model-props/computed/chemistry/util';
  18. // avoiding namespace lookup improved performance in Chrome (Aug 2020)
  19. const v3scaleAndAdd = Vec3.scaleAndAdd;
  20. const v3unitX = Vec3.unitX;
  21. const v3unitY = Vec3.unitY;
  22. const v3unitZ = Vec3.unitZ;
  23. export const ElementCrossParams = {
  24. ...UnitsLinesParams,
  25. lineSizeAttenuation: PD.Boolean(false),
  26. ignoreHydrogens: PD.Boolean(false),
  27. ignoreHydrogensVariant: PD.Select('all', PD.arrayToOptions(['all', 'non-polar'] as const)),
  28. traceOnly: PD.Boolean(false),
  29. crosses: PD.Select('lone', PD.arrayToOptions(['lone', 'all'] as const)),
  30. crossSize: PD.Numeric(0.35, { min: 0, max: 2, step: 0.01 }),
  31. };
  32. export type ElementCrossParams = typeof ElementCrossParams
  33. export function createElementCross(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: PD.Values<ElementCrossParams>, lines: Lines) {
  34. const { child } = structure;
  35. if (child && !child.unitMap.get(unit.id)) return Lines.createEmpty(lines);
  36. const elements = unit.elements;
  37. const n = elements.length;
  38. const builder = LinesBuilder.create(n, n / 10, lines);
  39. const p = Vec3();
  40. const s = Vec3();
  41. const e = Vec3();
  42. const pos = unit.conformation.invariantPosition;
  43. const ignore = makeElementIgnoreTest(structure, unit, props);
  44. const r = props.crossSize / 2;
  45. const lone = props.crosses === 'lone';
  46. for (let i = 0 as StructureElement.UnitIndex; i < n; ++i) {
  47. if (ignore && ignore(elements[i])) continue;
  48. if (lone && Unit.isAtomic(unit) && bondCount(structure, unit, i) !== 0) continue;
  49. pos(elements[i], p);
  50. v3scaleAndAdd(s, p, v3unitX, r);
  51. v3scaleAndAdd(e, p, v3unitX, -r);
  52. builder.add(s[0], s[1], s[2], e[0], e[1], e[2], i);
  53. v3scaleAndAdd(s, p, v3unitY, r);
  54. v3scaleAndAdd(e, p, v3unitY, -r);
  55. builder.add(s[0], s[1], s[2], e[0], e[1], e[2], i);
  56. v3scaleAndAdd(s, p, v3unitZ, r);
  57. v3scaleAndAdd(e, p, v3unitZ, -r);
  58. builder.add(s[0], s[1], s[2], e[0], e[1], e[2], i);
  59. }
  60. const l = builder.getLines();
  61. const sphere = Sphere3D.expand(Sphere3D(), unit.boundary.sphere, 1 * props.sizeFactor);
  62. l.setBoundingSphere(sphere);
  63. return l;
  64. }
  65. export function ElementCrossVisual(materialId: number): UnitsVisual<ElementCrossParams> {
  66. return UnitsLinesVisual<ElementCrossParams>({
  67. defaultProps: PD.getDefaultValues(ElementCrossParams),
  68. createGeometry: createElementCross,
  69. createLocationIterator: ElementIterator.fromGroup,
  70. getLoci: getElementLoci,
  71. eachLocation: eachElement,
  72. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<ElementCrossParams>, currentProps: PD.Values<ElementCrossParams>) => {
  73. state.createGeometry = (
  74. newProps.ignoreHydrogens !== currentProps.ignoreHydrogens ||
  75. newProps.ignoreHydrogensVariant !== currentProps.ignoreHydrogensVariant ||
  76. newProps.traceOnly !== currentProps.traceOnly ||
  77. newProps.crosses !== currentProps.crosses ||
  78. newProps.crossSize !== currentProps.crossSize
  79. );
  80. }
  81. }, materialId);
  82. }