geometry.ts 4.6 KB

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