polymer-gap-cylinder.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * Copyright (c) 2018-2019 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 { addFixedCountDashedCylinder } from '../../../mol-geo/geometry/mesh/builder/cylinder';
  16. import { UnitsMeshParams, UnitsVisual, UnitsMeshVisual } from '../units-visual';
  17. import { VisualUpdateState } from '../../util';
  18. import { BaseGeometry } from '../../../mol-geo/geometry/base';
  19. // import { TriangularPyramid } from '../../../mol-geo/primitive/pyramid';
  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: 2, max: 56, step: 2 }, BaseGeometry.CustomQualityParamInfo),
  24. }
  25. export const DefaultPolymerGapCylinderProps = PD.getDefaultValues(PolymerGapCylinderParams)
  26. export type PolymerGapCylinderProps = typeof DefaultPolymerGapCylinderProps
  27. // const triangularPyramid = TriangularPyramid()
  28. // const t = Mat4.identity()
  29. // const pd = Vec3.zero()
  30. function createPolymerGapCylinderMesh(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: PolymerGapCylinderProps, mesh?: Mesh) {
  31. const polymerGapCount = unit.gapElements.length
  32. if (!polymerGapCount) return Mesh.createEmpty(mesh)
  33. const { sizeFactor, radialSegments } = props
  34. const vertexCountEstimate = segmentCount * radialSegments * 2 * polymerGapCount * 2
  35. const builderState = MeshBuilder.createState(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(structure, unit)
  44. while (polymerGapIt.hasNext) {
  45. const { centerA, centerB } = polymerGapIt.move()
  46. if (centerA.element === centerB.element) {
  47. // TODO
  48. // builderState.currentGroup = i
  49. // pos(centerA.element, pA)
  50. // Vec3.add(pd, pA, Vec3.create(1, 0, 0))
  51. // Mat4.targetTo(t, pA, pd, Vec3.create(0, 1, 0))
  52. // Mat4.setTranslation(t, pA)
  53. // Mat4.scale(t, t, Vec3.create(0.7, 0.7, 2.5))
  54. // MeshBuilder.addPrimitive(builderState, t, triangularPyramid)
  55. } else {
  56. pos(centerA.element, pA)
  57. pos(centerB.element, pB)
  58. cylinderProps.radiusTop = cylinderProps.radiusBottom = theme.size.size(centerA) * sizeFactor
  59. builderState.currentGroup = i
  60. addFixedCountDashedCylinder(builderState, pA, pB, 0.5, segmentCount, cylinderProps)
  61. cylinderProps.radiusTop = cylinderProps.radiusBottom = theme.size.size(centerB) * sizeFactor
  62. builderState.currentGroup = i + 1
  63. addFixedCountDashedCylinder(builderState, pB, pA, 0.5, segmentCount, cylinderProps)
  64. }
  65. i += 2
  66. }
  67. return MeshBuilder.getMesh(builderState)
  68. }
  69. export const PolymerGapParams = {
  70. ...UnitsMeshParams,
  71. ...PolymerGapCylinderParams
  72. }
  73. export type PolymerGapParams = typeof PolymerGapParams
  74. export function PolymerGapVisual(materialId: number): UnitsVisual<PolymerGapParams> {
  75. return UnitsMeshVisual<PolymerGapParams>({
  76. defaultProps: PD.getDefaultValues(PolymerGapParams),
  77. createGeometry: createPolymerGapCylinderMesh,
  78. createLocationIterator: PolymerGapLocationIterator.fromGroup,
  79. getLoci: getPolymerGapElementLoci,
  80. eachLocation: eachPolymerGapElement,
  81. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<PolymerGapParams>, currentProps: PD.Values<PolymerGapParams>) => {
  82. state.createGeometry = (
  83. newProps.sizeFactor !== currentProps.sizeFactor ||
  84. newProps.radialSegments !== currentProps.radialSegments
  85. )
  86. }
  87. }, materialId)
  88. }