geometry.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 { Mesh } from './mesh/mesh';
  7. import { Points } from './points/points';
  8. import { Text } from './text/text';
  9. import { RenderableState } from 'mol-gl/renderable';
  10. import { LocationIterator } from '../util/location-iterator';
  11. import { ColorType } from './color-data';
  12. import { SizeType } from './size-data';
  13. import { Lines } from './lines/lines';
  14. import { ParamDefinition as PD } from 'mol-util/param-definition'
  15. import { DirectVolume } from './direct-volume/direct-volume';
  16. import { Color } from 'mol-util/color';
  17. import { Spheres } from './spheres/spheres';
  18. import { arrayMax } from 'mol-util/array';
  19. import { TransformData } from './transform-data';
  20. import { Theme } from 'mol-theme/theme';
  21. import { RenderObjectValuesType } from 'mol-gl/render-object';
  22. import { ValueOf } from 'mol-util/type-helpers';
  23. export type GeometryKindType = {
  24. 'mesh': Mesh,
  25. 'points': Points,
  26. 'spheres': Spheres,
  27. 'text': Text,
  28. 'lines': Lines,
  29. 'direct-volume': DirectVolume,
  30. }
  31. export type GeometryKindParams = {
  32. 'mesh': Mesh.Params,
  33. 'points': Points.Params,
  34. 'spheres': Spheres.Params,
  35. 'text': Text.Params,
  36. 'lines': Lines.Params,
  37. 'direct-volume': DirectVolume.Params,
  38. }
  39. export type GeometryKind = keyof GeometryKindType
  40. export type Geometry = ValueOf<GeometryKindType>
  41. export interface GeometryUtils<G extends Geometry, P extends PD.Params = GeometryKindParams[G['kind']], V = RenderObjectValuesType[G['kind']]> {
  42. Params: P
  43. createEmpty(geometry?: G): G
  44. createValues(geometry: G, transform: TransformData, locationIt: LocationIterator, theme: Theme, props: PD.Values<P>): V
  45. createValuesSimple(geometry: G, props: Partial<PD.Values<P>>, colorValue: Color, sizeValue: number, transform?: TransformData): V
  46. updateValues(values: V, props: PD.Values<P>): void
  47. updateBoundingSphere(values: V, geometry: G): void
  48. createRenderableState(props: Partial<PD.Values<P>>): RenderableState
  49. updateRenderableState(state: RenderableState, props: PD.Values<P>): void
  50. }
  51. export namespace Geometry {
  52. export type Params<G extends Geometry> = GeometryKindParams[G['kind']]
  53. export function getDrawCount(geometry: Geometry): number {
  54. switch (geometry.kind) {
  55. case 'mesh': return geometry.triangleCount * 3
  56. case 'points': return geometry.pointCount
  57. case 'spheres': return geometry.sphereCount * 2 * 3
  58. case 'text': return geometry.charCount * 2 * 3
  59. case 'lines': return geometry.lineCount * 2 * 3
  60. case 'direct-volume': return 12 * 3
  61. }
  62. }
  63. export function getGroupCount(geometry: Geometry): number {
  64. switch (geometry.kind) {
  65. case 'mesh':
  66. case 'points':
  67. case 'spheres':
  68. case 'text':
  69. case 'lines':
  70. return getDrawCount(geometry) === 0 ? 0 : (arrayMax(geometry.groupBuffer.ref.value) + 1)
  71. case 'direct-volume':
  72. return 1
  73. }
  74. }
  75. export function getUtils<G extends Geometry>(geometry: G): GeometryUtils<G> {
  76. // TODO avoid casting
  77. switch (geometry.kind) {
  78. case 'mesh': return Mesh.Utils as any
  79. case 'points': return Points.Utils as any
  80. case 'spheres': return Spheres.Utils as any
  81. case 'text': return Text.Utils as any
  82. case 'lines': return Lines.Utils as any
  83. case 'direct-volume': return DirectVolume.Utils as any
  84. }
  85. throw new Error('unknown geometry kind')
  86. }
  87. export function getGranularity(locationIt: LocationIterator, granularity: ColorType | SizeType) {
  88. // Always use 'group' granularity for 'complex' location iterators,
  89. // i.e. for which an instance may include multiple units
  90. return granularity === 'instance' && locationIt.isComplex ? 'group' : granularity
  91. }
  92. }