element-cross.ts 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. traceOnly: PD.Boolean(false),
  28. crosses: PD.Select('lone', PD.arrayToOptions(['lone', 'all'] as const)),
  29. crossSize: PD.Numeric(0.35, { min: 0, max: 2, step: 0.01 }),
  30. };
  31. export type ElementCrossParams = typeof ElementCrossParams
  32. export function createElementCross(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: PD.Values<ElementCrossParams>, lines: Lines) {
  33. const { child } = structure;
  34. if (child && !child.unitMap.get(unit.id)) return Lines.createEmpty(lines);
  35. const elements = unit.elements;
  36. const n = elements.length;
  37. const builder = LinesBuilder.create(n, n / 10, lines);
  38. const p = Vec3();
  39. const s = Vec3();
  40. const e = Vec3();
  41. const pos = unit.conformation.invariantPosition;
  42. const ignore = makeElementIgnoreTest(structure, unit, props);
  43. const r = props.crossSize / 2;
  44. const lone = props.crosses === 'lone';
  45. for (let i = 0 as StructureElement.UnitIndex; i < n; ++i) {
  46. if (ignore && ignore(elements[i])) continue;
  47. if (lone && Unit.isAtomic(unit) && bondCount(structure, unit, i) !== 0) continue;
  48. pos(elements[i], p);
  49. v3scaleAndAdd(s, p, v3unitX, r);
  50. v3scaleAndAdd(e, p, v3unitX, -r);
  51. builder.add(s[0], s[1], s[2], e[0], e[1], e[2], i);
  52. v3scaleAndAdd(s, p, v3unitY, r);
  53. v3scaleAndAdd(e, p, v3unitY, -r);
  54. builder.add(s[0], s[1], s[2], e[0], e[1], e[2], i);
  55. v3scaleAndAdd(s, p, v3unitZ, r);
  56. v3scaleAndAdd(e, p, v3unitZ, -r);
  57. builder.add(s[0], s[1], s[2], e[0], e[1], e[2], i);
  58. }
  59. const l = builder.getLines();
  60. const sphere = Sphere3D.expand(Sphere3D(), unit.boundary.sphere, 1 * props.sizeFactor);
  61. l.setBoundingSphere(sphere);
  62. return l;
  63. }
  64. export function ElementCrossVisual(materialId: number): UnitsVisual<ElementCrossParams> {
  65. return UnitsLinesVisual<ElementCrossParams>({
  66. defaultProps: PD.getDefaultValues(ElementCrossParams),
  67. createGeometry: createElementCross,
  68. createLocationIterator: ElementIterator.fromGroup,
  69. getLoci: getElementLoci,
  70. eachLocation: eachElement,
  71. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<ElementCrossParams>, currentProps: PD.Values<ElementCrossParams>) => {
  72. state.createGeometry = (
  73. newProps.ignoreHydrogens !== currentProps.ignoreHydrogens ||
  74. newProps.traceOnly !== currentProps.traceOnly ||
  75. newProps.crosses !== currentProps.crosses ||
  76. newProps.crossSize !== currentProps.crossSize
  77. );
  78. }
  79. }, materialId);
  80. }