polymer-gap-cylinder.ts 4.3 KB

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