polymer-gap-cylinder.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 { PolymerGapIterator, PolymerGapLocationIterator, markPolymerGapElement, getPolymerGapElementLoci } from './util/polymer';
  10. import { Vec3 } from 'mol-math/linear-algebra';
  11. import { UnitsMeshVisual, UnitsMeshParams } from '../units-visual';
  12. import { ParamDefinition as PD } from 'mol-util/param-definition';
  13. import { LinkCylinderParams } from './util/link';
  14. import { Mesh } from 'mol-geo/geometry/mesh/mesh';
  15. import { MeshBuilder } from 'mol-geo/geometry/mesh/mesh-builder';
  16. import { CylinderProps } from 'mol-geo/primitive/cylinder';
  17. import { addSphere } from 'mol-geo/geometry/mesh/builder/sphere';
  18. import { addFixedCountDashedCylinder } from 'mol-geo/geometry/mesh/builder/cylinder';
  19. import { VisualContext } from 'mol-repr/representation';
  20. import { Theme } from 'mol-theme/theme';
  21. const segmentCount = 10
  22. export const PolymerGapCylinderParams = {
  23. sizeFactor: PD.Numeric(0.2, { min: 0, max: 10, step: 0.01 }),
  24. radialSegments: PD.Numeric(16, { min: 3, max: 56, step: 1 }),
  25. }
  26. export const DefaultPolymerGapCylinderProps = PD.getDefaultValues(PolymerGapCylinderParams)
  27. export type PolymerGapCylinderProps = typeof DefaultPolymerGapCylinderProps
  28. function createPolymerGapCylinderMesh(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: PolymerGapCylinderProps, mesh?: Mesh) {
  29. const polymerGapCount = unit.gapElements.length
  30. if (!polymerGapCount) return Mesh.createEmpty(mesh)
  31. const { sizeFactor, radialSegments } = props
  32. const vertexCountEstimate = segmentCount * radialSegments * 2 * polymerGapCount * 2
  33. const builder = MeshBuilder.create(vertexCountEstimate, vertexCountEstimate / 10, mesh)
  34. const pos = unit.conformation.invariantPosition
  35. const pA = Vec3.zero()
  36. const pB = Vec3.zero()
  37. const cylinderProps: CylinderProps = {
  38. radiusTop: 1, radiusBottom: 1, topCap: true, bottomCap: true, radialSegments
  39. }
  40. let i = 0
  41. const polymerGapIt = PolymerGapIterator(unit)
  42. while (polymerGapIt.hasNext) {
  43. const { centerA, centerB } = polymerGapIt.move()
  44. if (centerA.element === centerB.element) {
  45. builder.setGroup(i)
  46. pos(centerA.element, pA)
  47. addSphere(builder, pA, 0.6, 0)
  48. } else {
  49. pos(centerA.element, pA)
  50. pos(centerB.element, pB)
  51. cylinderProps.radiusTop = cylinderProps.radiusBottom = theme.size.size(centerA) * sizeFactor
  52. builder.setGroup(i)
  53. addFixedCountDashedCylinder(builder, pA, pB, 0.5, segmentCount, cylinderProps)
  54. cylinderProps.radiusTop = cylinderProps.radiusBottom = theme.size.size(centerB) * sizeFactor
  55. builder.setGroup(i + 1)
  56. addFixedCountDashedCylinder(builder, pB, pA, 0.5, segmentCount, cylinderProps)
  57. }
  58. i += 2
  59. }
  60. return builder.getMesh()
  61. }
  62. export const InterUnitLinkParams = {
  63. ...UnitsMeshParams,
  64. ...LinkCylinderParams,
  65. }
  66. export const DefaultIntraUnitLinkProps = PD.getDefaultValues(InterUnitLinkParams)
  67. export type IntraUnitLinkProps = typeof DefaultIntraUnitLinkProps
  68. export const PolymerGapParams = {
  69. ...UnitsMeshParams,
  70. ...PolymerGapCylinderParams
  71. }
  72. export type PolymerGapParams = typeof PolymerGapParams
  73. export function PolymerGapVisual(): UnitsVisual<PolymerGapParams> {
  74. return UnitsMeshVisual<PolymerGapParams>({
  75. defaultProps: PD.getDefaultValues(PolymerGapParams),
  76. createGeometry: createPolymerGapCylinderMesh,
  77. createLocationIterator: PolymerGapLocationIterator.fromGroup,
  78. getLoci: getPolymerGapElementLoci,
  79. mark: markPolymerGapElement,
  80. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<PolymerGapParams>, currentProps: PD.Values<PolymerGapParams>) => {
  81. state.createGeometry = (
  82. newProps.sizeFactor !== currentProps.sizeFactor ||
  83. newProps.radialSegments !== currentProps.radialSegments
  84. )
  85. }
  86. })
  87. }