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