common.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 { LocationIterator } from '../../../../util/location-iterator';
  8. import { Mesh } from '../../../../geometry/mesh/mesh';
  9. import { StructureProps } from '../..';
  10. import { createMeshRenderObject, createPointsRenderObject, createLinesRenderObject, createDirectVolumeRenderObject } from 'mol-gl/render-object';
  11. import { RuntimeContext } from 'mol-task';
  12. import { TransformData, createIdentityTransform, createTransform } from '../../../../geometry/transform-data';
  13. import { Points } from '../../../../geometry/points/points';
  14. import { createRenderableState } from '../../../../geometry/geometry';
  15. import { Mat4 } from 'mol-math/linear-algebra';
  16. import { Lines } from '../../../../geometry/lines/lines';
  17. import { DirectVolume } from '../../../../geometry/direct-volume/direct-volume';
  18. import { SizeProps } from 'mol-geo/geometry/size-data';
  19. import { ColorProps } from 'mol-geo/geometry/color-data';
  20. export function createUnitsTransform({ units }: Unit.SymmetryGroup, transformData?: TransformData) {
  21. const unitCount = units.length
  22. const n = unitCount * 16
  23. const array = transformData && transformData.aTransform.ref.value.length >= n ? transformData.aTransform.ref.value : new Float32Array(n)
  24. for (let i = 0; i < unitCount; i++) {
  25. Mat4.toArray(units[i].conformation.operator.matrix, array, i * 16)
  26. }
  27. return createTransform(array, unitCount, transformData)
  28. }
  29. export const UnitKindInfo = {
  30. 'atomic': {},
  31. 'spheres': {},
  32. 'gaussians': {},
  33. }
  34. export type UnitKind = keyof typeof UnitKindInfo
  35. export const UnitKindNames = Object.keys(UnitKindInfo)
  36. export const UnitKindOptions = UnitKindNames.map(n => [n, n] as [UnitKind, string])
  37. export function includesUnitKind(unitKinds: UnitKind[], unit: Unit) {
  38. for (let i = 0, il = unitKinds.length; i < il; ++i) {
  39. if (Unit.isAtomic(unit) && unitKinds[i] === 'atomic') return true
  40. if (Unit.isSpheres(unit) && unitKinds[i] === 'spheres') return true
  41. if (Unit.isGaussians(unit) && unitKinds[i] === 'gaussians') return true
  42. }
  43. return false
  44. }
  45. export function sizeChanged(oldProps: SizeProps, newProps: SizeProps) {
  46. return (
  47. oldProps.sizeTheme !== newProps.sizeTheme ||
  48. oldProps.sizeValue !== newProps.sizeValue ||
  49. oldProps.sizeFactor !== newProps.sizeFactor
  50. )
  51. }
  52. export function colorChanged(oldProps: ColorProps, newProps: ColorProps) {
  53. return (
  54. oldProps.colorTheme !== newProps.colorTheme ||
  55. oldProps.colorValue !== newProps.colorValue
  56. )
  57. }
  58. // mesh
  59. type StructureMeshProps = Mesh.Props & StructureProps
  60. export async function createComplexMeshRenderObject(ctx: RuntimeContext, structure: Structure, mesh: Mesh, locationIt: LocationIterator, props: StructureMeshProps) {
  61. const transform = createIdentityTransform()
  62. const values = await Mesh.createValues(ctx, mesh, transform, locationIt, props)
  63. const state = createRenderableState(props)
  64. return createMeshRenderObject(values, state)
  65. }
  66. export async function createUnitsMeshRenderObject(ctx: RuntimeContext, group: Unit.SymmetryGroup, mesh: Mesh, locationIt: LocationIterator, props: StructureMeshProps) {
  67. const transform = createUnitsTransform(group)
  68. const values = await Mesh.createValues(ctx, mesh, transform, locationIt, props)
  69. const state = createRenderableState(props)
  70. return createMeshRenderObject(values, state)
  71. }
  72. // points
  73. type StructurePointsProps = Points.Props & StructureProps
  74. export async function createUnitsPointsRenderObject(ctx: RuntimeContext, group: Unit.SymmetryGroup, points: Points, locationIt: LocationIterator, props: StructurePointsProps) {
  75. const transform = createUnitsTransform(group)
  76. const values = await Points.createValues(ctx, points, transform, locationIt, props)
  77. const state = createRenderableState(props)
  78. return createPointsRenderObject(values, state)
  79. }
  80. // lines
  81. type StructureLinesProps = Lines.Props & StructureProps
  82. export async function createUnitsLinesRenderObject(ctx: RuntimeContext, group: Unit.SymmetryGroup, lines: Lines, locationIt: LocationIterator, props: StructureLinesProps) {
  83. const transform = createUnitsTransform(group)
  84. const values = await Lines.createValues(ctx, lines, transform, locationIt, props)
  85. const state = createRenderableState(props)
  86. return createLinesRenderObject(values, state)
  87. }
  88. // direct-volume
  89. type StructureDirectVolumeProps = DirectVolume.Props & StructureProps
  90. export async function createUnitsDirectVolumeRenderObject(ctx: RuntimeContext, group: Unit.SymmetryGroup, directVolume: DirectVolume, locationIt: LocationIterator, props: StructureDirectVolumeProps) {
  91. const transform = createUnitsTransform(group)
  92. const values = await DirectVolume.createValues(ctx, directVolume, transform, locationIt, props)
  93. const state = createRenderableState(props)
  94. return createDirectVolumeRenderObject(values, state)
  95. }