geometry.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * Copyright (c) 2018-2020 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 { RenderObjectValues } from '../../mol-gl/render-object';
  22. import { TextureMesh } from './texture-mesh/texture-mesh';
  23. import { Image } from './image/image';
  24. export type GeometryKind = 'mesh' | 'points' | 'spheres' | 'text' | 'lines' | 'direct-volume' | 'image' | 'texture-mesh'
  25. export type Geometry<T extends GeometryKind = GeometryKind> =
  26. T extends 'mesh' ? Mesh :
  27. T extends 'points' ? Points :
  28. T extends 'spheres' ? Spheres :
  29. T extends 'text' ? Text :
  30. T extends 'lines' ? Lines :
  31. T extends 'direct-volume' ? DirectVolume :
  32. T extends 'image' ? Image :
  33. T extends 'texture-mesh' ? TextureMesh : never
  34. type GeometryParams<T extends GeometryKind> =
  35. T extends 'mesh' ? Mesh.Params :
  36. T extends 'points' ? Points.Params :
  37. T extends 'spheres' ? Spheres.Params :
  38. T extends 'text' ? Text.Params :
  39. T extends 'lines' ? Lines.Params :
  40. T extends 'direct-volume' ? DirectVolume.Params :
  41. T extends 'image' ? Image.Params :
  42. T extends 'texture-mesh' ? TextureMesh.Params : never
  43. export interface GeometryUtils<G extends Geometry, P extends PD.Params = GeometryParams<G['kind']>, V = RenderObjectValues<G['kind']>> {
  44. Params: P
  45. createEmpty(geometry?: G): G
  46. createValues(geometry: G, transform: TransformData, locationIt: LocationIterator, theme: Theme, props: PD.Values<P>): V
  47. createValuesSimple(geometry: G, props: Partial<PD.Values<P>>, colorValue: Color, sizeValue: number, transform?: TransformData): V
  48. updateValues(values: V, props: PD.Values<P>): void
  49. updateBoundingSphere(values: V, geometry: G): void
  50. createRenderableState(props: Partial<PD.Values<P>>): RenderableState
  51. updateRenderableState(state: RenderableState, props: PD.Values<P>): void
  52. }
  53. export namespace Geometry {
  54. export type Params<G extends Geometry> = GeometryParams<G['kind']>
  55. export function getDrawCount(geometry: Geometry): number {
  56. switch (geometry.kind) {
  57. case 'mesh': return geometry.triangleCount * 3;
  58. case 'points': return geometry.pointCount;
  59. case 'spheres': return geometry.sphereCount * 2 * 3;
  60. case 'text': return geometry.charCount * 2 * 3;
  61. case 'lines': return geometry.lineCount * 2 * 3;
  62. case 'direct-volume': return 12 * 3;
  63. case 'image': return 2 * 3;
  64. case 'texture-mesh': return geometry.vertexCount;
  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 'image':
  78. return arrayMax(geometry.groupTexture.ref.value.array) + 1;
  79. case 'texture-mesh':
  80. return geometry.groupCount;
  81. }
  82. }
  83. export function getUtils<G extends Geometry>(geometry: G): GeometryUtils<G> {
  84. // TODO avoid casting
  85. switch (geometry.kind) {
  86. case 'mesh': return Mesh.Utils as any;
  87. case 'points': return Points.Utils as any;
  88. case 'spheres': return Spheres.Utils as any;
  89. case 'text': return Text.Utils as any;
  90. case 'lines': return Lines.Utils as any;
  91. case 'direct-volume': return DirectVolume.Utils as any;
  92. case 'image': return Image.Utils as any;
  93. case 'texture-mesh': return TextureMesh.Utils as any;
  94. }
  95. }
  96. export function getGranularity(locationIt: LocationIterator, granularity: ColorType | SizeType) {
  97. // Always use 'group' granularity for 'complex' location iterators,
  98. // i.e. for which an instance may include multiple units
  99. return granularity === 'instance' && locationIt.isComplex ? 'group' : granularity;
  100. }
  101. }