polymer-direction-wedge.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 '../index';
  8. import { PolymerTraceIterator, createCurveSegmentState, interpolateCurveSegment, PolymerLocationIterator, getPolymerElementLoci, markPolymerElement } from './util/polymer';
  9. import { Vec3, Mat4 } from 'mol-math/linear-algebra';
  10. import { SecondaryStructureType, isNucleic } from 'mol-model/structure/model/types';
  11. import { UnitsMeshVisual, UnitsMeshParams } from '../units-visual';
  12. import { SizeThemeName, SizeThemeOptions } from 'mol-theme/size';
  13. import { ParamDefinition as PD } from 'mol-util/param-definition';
  14. import { Wedge } from 'mol-geo/primitive/wedge';
  15. import { Mesh } from 'mol-geo/geometry/mesh/mesh';
  16. import { MeshBuilder } from 'mol-geo/geometry/mesh/mesh-builder';
  17. import { VisualContext } from 'mol-repr';
  18. import { Theme } from 'mol-geo/geometry/geometry';
  19. const t = Mat4.identity()
  20. const sVec = Vec3.zero()
  21. const n0 = Vec3.zero()
  22. const n1 = Vec3.zero()
  23. const upVec = Vec3.zero()
  24. const depthFactor = 4
  25. const widthFactor = 4
  26. const heightFactor = 6
  27. const wedge = Wedge()
  28. export const PolymerDirectionWedgeParams = {
  29. sizeTheme: PD.Select<SizeThemeName>('Size Theme', '', 'uniform', SizeThemeOptions),
  30. sizeValue: PD.Numeric('Size Value', '', 1, 0, 20, 0.1),
  31. }
  32. export const DefaultPolymerDirectionWedgeProps = PD.getDefaultValues(PolymerDirectionWedgeParams)
  33. export type PolymerDirectionWedgeProps = typeof DefaultPolymerDirectionWedgeProps
  34. async function createPolymerDirectionWedgeMesh(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: PolymerDirectionWedgeProps, mesh?: Mesh) {
  35. const polymerElementCount = unit.polymerElements.length
  36. if (!polymerElementCount) return Mesh.createEmpty(mesh)
  37. const vertexCount = polymerElementCount * 24
  38. const builder = MeshBuilder.create(vertexCount, vertexCount / 10, mesh)
  39. const linearSegments = 1
  40. const state = createCurveSegmentState(linearSegments)
  41. const { normalVectors, binormalVectors } = state
  42. let i = 0
  43. const polymerTraceIt = PolymerTraceIterator(unit)
  44. while (polymerTraceIt.hasNext) {
  45. const v = polymerTraceIt.move()
  46. builder.setGroup(i)
  47. const isNucleicType = isNucleic(v.moleculeType)
  48. const isSheet = SecondaryStructureType.is(v.secStrucType, SecondaryStructureType.Flag.Beta)
  49. const tension = (isNucleicType || isSheet) ? 0.5 : 0.9
  50. const shift = isNucleicType ? 0.3 : 0.5
  51. interpolateCurveSegment(state, v, tension, shift)
  52. if ((isSheet && !v.secStrucChange) || !isSheet) {
  53. const size = theme.size.size(v.center)
  54. const depth = depthFactor * size
  55. const width = widthFactor * size
  56. const height = heightFactor * size
  57. const vectors = isNucleicType ? binormalVectors : normalVectors
  58. Vec3.fromArray(n0, vectors, 0)
  59. Vec3.fromArray(n1, vectors, 3)
  60. Vec3.normalize(upVec, Vec3.add(upVec, n0, n1))
  61. Mat4.targetTo(t, v.p3, v.p1, upVec)
  62. Mat4.mul(t, t, Mat4.rotY90Z180)
  63. Mat4.scale(t, t, Vec3.set(sVec, height, width, depth))
  64. Mat4.setTranslation(t, v.p2)
  65. builder.add(t, wedge)
  66. }
  67. if (i % 10000 === 0 && ctx.runtime.shouldUpdate) {
  68. await ctx.runtime.update({ message: 'Polymer direction mesh', current: i, max: polymerElementCount });
  69. }
  70. ++i
  71. }
  72. return builder.getMesh()
  73. }
  74. export const PolymerDirectionParams = {
  75. ...UnitsMeshParams,
  76. ...PolymerDirectionWedgeParams
  77. }
  78. export const DefaultPolymerDirectionProps = PD.getDefaultValues(PolymerDirectionParams)
  79. export type PolymerDirectionProps = typeof DefaultPolymerDirectionProps
  80. export function PolymerDirectionVisual(): UnitsVisual<PolymerDirectionProps> {
  81. return UnitsMeshVisual<PolymerDirectionProps>({
  82. defaultProps: DefaultPolymerDirectionProps,
  83. createGeometry: createPolymerDirectionWedgeMesh,
  84. createLocationIterator: PolymerLocationIterator.fromGroup,
  85. getLoci: getPolymerElementLoci,
  86. mark: markPolymerElement,
  87. setUpdateState: () => {}
  88. })
  89. }