polymer-backbone-cylinder.ts 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 { Unit, Structure } from 'mol-model/structure';
  7. import { UnitsVisual } from '../representation';
  8. import { VisualUpdateState } from '../../util';
  9. import { PolymerBackboneIterator } from './util/polymer';
  10. import { getElementLoci, markElement, StructureElementIterator } from './util/element';
  11. import { Vec3 } from 'mol-math/linear-algebra';
  12. import { UnitsMeshVisual, UnitsMeshParams } from '../units-visual';
  13. import { OrderedSet } from 'mol-data/int';
  14. import { ParamDefinition as PD } from 'mol-util/param-definition';
  15. import { Mesh } from 'mol-geo/geometry/mesh/mesh';
  16. import { MeshBuilder } from 'mol-geo/geometry/mesh/mesh-builder';
  17. import { CylinderProps } from 'mol-geo/primitive/cylinder';
  18. import { addCylinder } from 'mol-geo/geometry/mesh/builder/cylinder';
  19. import { VisualContext } from 'mol-repr/representation';
  20. import { Theme } from 'mol-theme/theme';
  21. export const PolymerBackboneCylinderParams = {
  22. sizeFactor: PD.Numeric(0.3, { min: 0, max: 10, step: 0.01 }),
  23. radialSegments: PD.Numeric(16, { min: 3, max: 56, step: 1 }),
  24. }
  25. export const DefaultPolymerBackboneCylinderProps = PD.getDefaultValues(PolymerBackboneCylinderParams)
  26. export type PolymerBackboneCylinderProps = typeof DefaultPolymerBackboneCylinderProps
  27. // TODO do group id based on polymer index not element index
  28. function createPolymerBackboneCylinderMesh(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: PolymerBackboneCylinderProps, mesh?: Mesh) {
  29. const polymerElementCount = unit.polymerElements.length
  30. if (!polymerElementCount) return Mesh.createEmpty(mesh)
  31. const { radialSegments, sizeFactor } = props
  32. const vertexCountEstimate = radialSegments * 2 * polymerElementCount * 2
  33. const builder = MeshBuilder.create(vertexCountEstimate, vertexCountEstimate / 10, mesh)
  34. const { elements } = unit
  35. const pos = unit.conformation.invariantPosition
  36. const pA = Vec3.zero()
  37. const pB = Vec3.zero()
  38. const cylinderProps: CylinderProps = { radiusTop: 1, radiusBottom: 1, radialSegments }
  39. const polymerBackboneIt = PolymerBackboneIterator(unit)
  40. while (polymerBackboneIt.hasNext) {
  41. const { centerA, centerB } = polymerBackboneIt.move()
  42. pos(centerA.element, pA)
  43. pos(centerB.element, pB)
  44. cylinderProps.radiusTop = cylinderProps.radiusBottom = theme.size.size(centerA) * sizeFactor
  45. builder.setGroup(OrderedSet.indexOf(elements, centerA.element))
  46. addCylinder(builder, pA, pB, 0.5, cylinderProps)
  47. cylinderProps.radiusTop = cylinderProps.radiusBottom = theme.size.size(centerB) * sizeFactor
  48. builder.setGroup(OrderedSet.indexOf(elements, centerB.element))
  49. addCylinder(builder, pB, pA, 0.5, cylinderProps)
  50. }
  51. return builder.getMesh()
  52. }
  53. export const PolymerBackboneParams = {
  54. ...UnitsMeshParams,
  55. ...PolymerBackboneCylinderParams,
  56. }
  57. export type PolymerBackboneParams = typeof PolymerBackboneParams
  58. export function PolymerBackboneVisual(): UnitsVisual<PolymerBackboneParams> {
  59. return UnitsMeshVisual<PolymerBackboneParams>({
  60. defaultProps: PD.getDefaultValues(PolymerBackboneParams),
  61. createGeometry: createPolymerBackboneCylinderMesh,
  62. // TODO create a specialized location iterator
  63. createLocationIterator: StructureElementIterator.fromGroup,
  64. getLoci: getElementLoci,
  65. mark: markElement,
  66. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<PolymerBackboneParams>, currentProps: PD.Values<PolymerBackboneParams>) => {
  67. state.createGeometry = (
  68. newProps.sizeFactor !== currentProps.sizeFactor ||
  69. newProps.radialSegments !== currentProps.radialSegments
  70. )
  71. }
  72. })
  73. }