polymer-gap-cylinder.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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, VisualUpdateState } from '..';
  8. import { RuntimeContext } from 'mol-task'
  9. import { Mesh } from '../../../geometry/mesh/mesh';
  10. import { MeshBuilder } from '../../../geometry/mesh/mesh-builder';
  11. import { PolymerGapIterator, PolymerGapLocationIterator, markPolymerGapElement, getPolymerGapElementLoci } from './util/polymer';
  12. import { Vec3 } from 'mol-math/linear-algebra';
  13. import { UnitsMeshVisual, UnitsMeshParams } from '../units-visual';
  14. import { SizeTheme, SizeThemeOptions, SizeThemeName } from 'mol-view/theme/size';
  15. import { CylinderProps } from '../../../primitive/cylinder';
  16. import { addSphere } from '../../../geometry/mesh/builder/sphere';
  17. import { addFixedCountDashedCylinder } from '../../../geometry/mesh/builder/cylinder';
  18. import { SelectParam, NumberParam, paramDefaultValues } from 'mol-view/parameter';
  19. import { LinkCylinderParams } from './util/link';
  20. const segmentCount = 10
  21. export const PolymerGapCylinderParams = {
  22. sizeTheme: SelectParam<SizeThemeName>('Size Theme', '', 'physical', SizeThemeOptions),
  23. sizeValue: NumberParam('Size Value', '', 1, 0, 10, 0.1),
  24. sizeFactor: NumberParam('Size Factor', '', 0.3, 0, 10, 0.1),
  25. radialSegments: NumberParam('Radial Segments', '', 16, 3, 56, 1),
  26. }
  27. export const DefaultPolymerGapCylinderProps = paramDefaultValues(PolymerGapCylinderParams)
  28. export type PolymerGapCylinderProps = typeof DefaultPolymerGapCylinderProps
  29. async function createPolymerGapCylinderMesh(ctx: RuntimeContext, unit: Unit, structure: Structure, props: PolymerGapCylinderProps, mesh?: Mesh) {
  30. const polymerGapCount = unit.gapElements.length
  31. if (!polymerGapCount) return Mesh.createEmpty(mesh)
  32. const sizeTheme = SizeTheme({ name: props.sizeTheme, value: props.sizeValue, factor: props.sizeValue })
  33. const { radialSegments } = props
  34. const vertexCountEstimate = segmentCount * radialSegments * 2 * polymerGapCount * 2
  35. const builder = MeshBuilder.create(vertexCountEstimate, vertexCountEstimate / 10, mesh)
  36. const pos = unit.conformation.invariantPosition
  37. const pA = Vec3.zero()
  38. const pB = Vec3.zero()
  39. const cylinderProps: CylinderProps = {
  40. radiusTop: 1, radiusBottom: 1, topCap: true, bottomCap: true, radialSegments
  41. }
  42. let i = 0
  43. const polymerGapIt = PolymerGapIterator(unit)
  44. while (polymerGapIt.hasNext) {
  45. const { centerA, centerB } = polymerGapIt.move()
  46. if (centerA.element === centerB.element) {
  47. builder.setGroup(i)
  48. pos(centerA.element, pA)
  49. addSphere(builder, pA, 0.6, 0)
  50. } else {
  51. pos(centerA.element, pA)
  52. pos(centerB.element, pB)
  53. cylinderProps.radiusTop = cylinderProps.radiusBottom = sizeTheme.size(centerA)
  54. builder.setGroup(i)
  55. addFixedCountDashedCylinder(builder, pA, pB, 0.5, segmentCount, cylinderProps)
  56. cylinderProps.radiusTop = cylinderProps.radiusBottom = sizeTheme.size(centerB)
  57. builder.setGroup(i + 1)
  58. addFixedCountDashedCylinder(builder, pB, pA, 0.5, segmentCount, cylinderProps)
  59. }
  60. if (i % 10000 === 0 && ctx.shouldUpdate) {
  61. await ctx.update({ message: 'Gap mesh', current: i, max: polymerGapCount });
  62. }
  63. i += 2
  64. }
  65. return builder.getMesh()
  66. }
  67. export const InterUnitLinkParams = {
  68. ...UnitsMeshParams,
  69. ...LinkCylinderParams,
  70. sizeTheme: SelectParam<SizeThemeName>('Size Theme', '', 'physical', SizeThemeOptions),
  71. sizeValue: NumberParam('Size Value', '', 1, 0, 20, 0.1),
  72. sizeFactor: NumberParam('Size Factor', '', 0.3, 0, 10, 0.1),
  73. }
  74. export const DefaultIntraUnitLinkProps = paramDefaultValues(InterUnitLinkParams)
  75. export type IntraUnitLinkProps = typeof DefaultIntraUnitLinkProps
  76. export const PolymerGapParams = {
  77. ...UnitsMeshParams,
  78. ...PolymerGapCylinderParams
  79. }
  80. export const DefaultPolymerGapProps = paramDefaultValues(PolymerGapParams)
  81. export type PolymerGapProps = typeof DefaultPolymerGapProps
  82. export function PolymerGapVisual(): UnitsVisual<PolymerGapProps> {
  83. return UnitsMeshVisual<PolymerGapProps>({
  84. defaultProps: DefaultPolymerGapProps,
  85. createGeometry: createPolymerGapCylinderMesh,
  86. createLocationIterator: PolymerGapLocationIterator.fromGroup,
  87. getLoci: getPolymerGapElementLoci,
  88. mark: markPolymerGapElement,
  89. setUpdateState: (state: VisualUpdateState, newProps: PolymerGapProps, currentProps: PolymerGapProps) => {
  90. state.createGeometry = newProps.radialSegments !== currentProps.radialSegments
  91. }
  92. })
  93. }