geometry.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. import { Cylinders } from './cylinders/cylinders';
  25. export type GeometryKind = 'mesh' | 'points' | 'spheres' | 'cylinders' | 'text' | 'lines' | 'direct-volume' | 'image' | 'texture-mesh'
  26. export type Geometry<T extends GeometryKind = GeometryKind> =
  27. T extends 'mesh' ? Mesh :
  28. T extends 'points' ? Points :
  29. T extends 'spheres' ? Spheres :
  30. T extends 'cylinders' ? Cylinders :
  31. T extends 'text' ? Text :
  32. T extends 'lines' ? Lines :
  33. T extends 'direct-volume' ? DirectVolume :
  34. T extends 'image' ? Image :
  35. T extends 'texture-mesh' ? TextureMesh : never
  36. type GeometryParams<T extends GeometryKind> =
  37. T extends 'mesh' ? Mesh.Params :
  38. T extends 'points' ? Points.Params :
  39. T extends 'spheres' ? Spheres.Params :
  40. T extends 'cylinders' ? Cylinders.Params :
  41. T extends 'text' ? Text.Params :
  42. T extends 'lines' ? Lines.Params :
  43. T extends 'direct-volume' ? DirectVolume.Params :
  44. T extends 'image' ? Image.Params :
  45. T extends 'texture-mesh' ? TextureMesh.Params : never
  46. export interface GeometryUtils<G extends Geometry, P extends PD.Params = GeometryParams<G['kind']>, V = RenderObjectValues<G['kind']>> {
  47. Params: P
  48. createEmpty(geometry?: G): G
  49. createValues(geometry: G, transform: TransformData, locationIt: LocationIterator, theme: Theme, props: PD.Values<P>): V
  50. createValuesSimple(geometry: G, props: Partial<PD.Values<P>>, colorValue: Color, sizeValue: number, transform?: TransformData): V
  51. updateValues(values: V, props: PD.Values<P>): void
  52. updateBoundingSphere(values: V, geometry: G): void
  53. createRenderableState(props: Partial<PD.Values<P>>): RenderableState
  54. updateRenderableState(state: RenderableState, props: PD.Values<P>): void
  55. createPositionIterator(geometry: G, transform: TransformData): LocationIterator
  56. }
  57. export namespace Geometry {
  58. export type Params<G extends Geometry> = GeometryParams<G['kind']>
  59. export function getDrawCount(geometry: Geometry): number {
  60. switch (geometry.kind) {
  61. case 'mesh': return geometry.triangleCount * 3;
  62. case 'points': return geometry.pointCount;
  63. case 'spheres': return geometry.sphereCount * 2 * 3;
  64. case 'cylinders': return geometry.cylinderCount * 4 * 3;
  65. case 'text': return geometry.charCount * 2 * 3;
  66. case 'lines': return geometry.lineCount * 2 * 3;
  67. case 'direct-volume': return 12 * 3;
  68. case 'image': return 2 * 3;
  69. case 'texture-mesh': return geometry.vertexCount;
  70. }
  71. }
  72. export function getVertexCount(geometry: Geometry): number {
  73. switch (geometry.kind) {
  74. case 'mesh': return geometry.vertexCount;
  75. case 'points': return geometry.pointCount;
  76. case 'spheres': return geometry.sphereCount * 4;
  77. case 'cylinders': return geometry.cylinderCount * 6;
  78. case 'text': return geometry.charCount * 4;
  79. case 'lines': return geometry.lineCount * 4;
  80. case 'direct-volume':
  81. const [x, y, z] = geometry.gridDimension.ref.value;
  82. return x * y * z;
  83. case 'image': return 4;
  84. case 'texture-mesh': return geometry.vertexCount / 3;
  85. }
  86. }
  87. export function getGroupCount(geometry: Geometry): number {
  88. switch (geometry.kind) {
  89. case 'mesh':
  90. case 'points':
  91. case 'spheres':
  92. case 'cylinders':
  93. case 'text':
  94. case 'lines':
  95. return getDrawCount(geometry) === 0 ? 0 : (arrayMax(geometry.groupBuffer.ref.value) + 1);
  96. case 'direct-volume':
  97. return 1;
  98. case 'image':
  99. return arrayMax(geometry.groupTexture.ref.value.array) + 1;
  100. case 'texture-mesh':
  101. return geometry.groupCount;
  102. }
  103. }
  104. export function getUtils<G extends Geometry>(geometry: G): GeometryUtils<G> {
  105. // TODO avoid casting
  106. switch (geometry.kind) {
  107. case 'mesh': return Mesh.Utils as any;
  108. case 'points': return Points.Utils as any;
  109. case 'spheres': return Spheres.Utils as any;
  110. case 'cylinders': return Cylinders.Utils as any;
  111. case 'text': return Text.Utils as any;
  112. case 'lines': return Lines.Utils as any;
  113. case 'direct-volume': return DirectVolume.Utils as any;
  114. case 'image': return Image.Utils as any;
  115. case 'texture-mesh': return TextureMesh.Utils as any;
  116. }
  117. }
  118. export function getGranularity<T extends ColorType | SizeType>(locationIt: LocationIterator, granularity: T) {
  119. return granularity === 'instance' && locationIt.nonInstanceable ? 'group' : granularity;
  120. }
  121. }