lines.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 { ValueCell } from '../../../mol-util'
  7. import { Mat4 } from '../../../mol-math/linear-algebra'
  8. import { transformPositionArray/* , transformDirectionArray, getNormalMatrix */ } from '../../util';
  9. import { GeometryUtils } from '../geometry';
  10. import { createColors } from '../color-data';
  11. import { createMarkers } from '../marker-data';
  12. import { createSizes } from '../size-data';
  13. import { TransformData } from '../transform-data';
  14. import { LocationIterator } from '../../util/location-iterator';
  15. import { LinesValues } from '../../../mol-gl/renderable/lines';
  16. import { Mesh } from '../mesh/mesh';
  17. import { LinesBuilder } from './lines-builder';
  18. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  19. import { calculateBoundingSphere } from '../../../mol-gl/renderable/util';
  20. import { Sphere3D } from '../../../mol-math/geometry';
  21. import { Theme } from '../../../mol-theme/theme';
  22. import { Color } from '../../../mol-util/color';
  23. import { BaseGeometry } from '../base';
  24. import { createEmptyOverpaint } from '../overpaint-data';
  25. import { createEmptyTransparency } from '../transparency-data';
  26. /** Wide line */
  27. export interface Lines {
  28. readonly kind: 'lines',
  29. /** Number of lines */
  30. lineCount: number,
  31. /** Mapping buffer as array of xy values wrapped in a value cell */
  32. readonly mappingBuffer: ValueCell<Float32Array>,
  33. /** Index buffer as array of vertex index triplets wrapped in a value cell */
  34. readonly indexBuffer: ValueCell<Uint32Array>,
  35. /** Group buffer as array of group ids for each vertex wrapped in a value cell */
  36. readonly groupBuffer: ValueCell<Float32Array>,
  37. /** Line start buffer as array of xyz values wrapped in a value cell */
  38. readonly startBuffer: ValueCell<Float32Array>,
  39. /** Line end buffer as array of xyz values wrapped in a value cell */
  40. readonly endBuffer: ValueCell<Float32Array>,
  41. }
  42. export namespace Lines {
  43. export function createEmpty(lines?: Lines): Lines {
  44. const mb = lines ? lines.mappingBuffer.ref.value : new Float32Array(0)
  45. const ib = lines ? lines.indexBuffer.ref.value : new Uint32Array(0)
  46. const gb = lines ? lines.groupBuffer.ref.value : new Float32Array(0)
  47. const sb = lines ? lines.startBuffer.ref.value : new Float32Array(0)
  48. const eb = lines ? lines.endBuffer.ref.value : new Float32Array(0)
  49. return {
  50. kind: 'lines',
  51. lineCount: 0,
  52. mappingBuffer: lines ? ValueCell.update(lines.mappingBuffer, mb) : ValueCell.create(mb),
  53. indexBuffer: lines ? ValueCell.update(lines.indexBuffer, ib) : ValueCell.create(ib),
  54. groupBuffer: lines ? ValueCell.update(lines.groupBuffer, gb) : ValueCell.create(gb),
  55. startBuffer: lines ? ValueCell.update(lines.startBuffer, sb) : ValueCell.create(sb),
  56. endBuffer: lines ? ValueCell.update(lines.endBuffer, eb) : ValueCell.create(eb),
  57. }
  58. }
  59. export function fromMesh(mesh: Mesh, lines?: Lines) {
  60. const vb = mesh.vertexBuffer.ref.value
  61. const ib = mesh.indexBuffer.ref.value
  62. const gb = mesh.groupBuffer.ref.value
  63. const builder = LinesBuilder.create(mesh.triangleCount * 3, mesh.triangleCount / 10, lines)
  64. // TODO avoid duplicate lines
  65. for (let i = 0, il = mesh.triangleCount * 3; i < il; i += 3) {
  66. const i0 = ib[i], i1 = ib[i + 1], i2 = ib[i + 2];
  67. const x0 = vb[i0 * 3], y0 = vb[i0 * 3 + 1], z0 = vb[i0 * 3 + 2];
  68. const x1 = vb[i1 * 3], y1 = vb[i1 * 3 + 1], z1 = vb[i1 * 3 + 2];
  69. const x2 = vb[i2 * 3], y2 = vb[i2 * 3 + 1], z2 = vb[i2 * 3 + 2];
  70. builder.add(x0, y0, z0, x1, y1, z1, gb[i0])
  71. builder.add(x0, y0, z0, x2, y2, z2, gb[i0])
  72. builder.add(x1, y1, z1, x2, y2, z2, gb[i1])
  73. }
  74. return builder.getLines();
  75. }
  76. export function transformImmediate(line: Lines, t: Mat4) {
  77. transformRangeImmediate(line, t, 0, line.lineCount)
  78. }
  79. export function transformRangeImmediate(lines: Lines, t: Mat4, offset: number, count: number) {
  80. const start = lines.startBuffer.ref.value
  81. transformPositionArray(t, start, offset, count * 4)
  82. ValueCell.update(lines.startBuffer, start);
  83. const end = lines.endBuffer.ref.value
  84. transformPositionArray(t, end, offset, count * 4)
  85. ValueCell.update(lines.endBuffer, end);
  86. }
  87. //
  88. export const Params = {
  89. ...BaseGeometry.Params,
  90. sizeFactor: PD.Numeric(1, { min: 0, max: 10, step: 0.1 }),
  91. lineSizeAttenuation: PD.Boolean(false),
  92. }
  93. export type Params = typeof Params
  94. export const Utils: GeometryUtils<Lines, Params> = {
  95. Params,
  96. createEmpty,
  97. createValues,
  98. createValuesSimple,
  99. updateValues,
  100. updateBoundingSphere,
  101. createRenderableState: BaseGeometry.createRenderableState,
  102. updateRenderableState: BaseGeometry.updateRenderableState
  103. }
  104. function createValues(lines: Lines, transform: TransformData, locationIt: LocationIterator, theme: Theme, props: PD.Values<Params>): LinesValues {
  105. const { instanceCount, groupCount } = locationIt
  106. const color = createColors(locationIt, theme.color)
  107. const size = createSizes(locationIt, theme.size)
  108. const marker = createMarkers(instanceCount * groupCount)
  109. const overpaint = createEmptyOverpaint()
  110. const transparency = createEmptyTransparency()
  111. const counts = { drawCount: lines.lineCount * 2 * 3, groupCount, instanceCount }
  112. const { boundingSphere, invariantBoundingSphere } = getBoundingSphere(lines.startBuffer.ref.value, lines.endBuffer.ref.value, lines.lineCount,
  113. transform.aTransform.ref.value, transform.instanceCount.ref.value)
  114. return {
  115. aMapping: lines.mappingBuffer,
  116. aGroup: lines.groupBuffer,
  117. aStart: lines.startBuffer,
  118. aEnd: lines.endBuffer,
  119. elements: lines.indexBuffer,
  120. boundingSphere: ValueCell.create(boundingSphere),
  121. invariantBoundingSphere: ValueCell.create(invariantBoundingSphere),
  122. ...color,
  123. ...size,
  124. ...marker,
  125. ...overpaint,
  126. ...transparency,
  127. ...transform,
  128. ...BaseGeometry.createValues(props, counts),
  129. uSizeFactor: ValueCell.create(props.sizeFactor),
  130. dLineSizeAttenuation: ValueCell.create(props.lineSizeAttenuation),
  131. dDoubleSided: ValueCell.create(true),
  132. dFlipSided: ValueCell.create(false),
  133. }
  134. }
  135. function createValuesSimple(lines: Lines, props: Partial<PD.Values<Params>>, colorValue: Color, sizeValue: number, transform?: TransformData) {
  136. const s = BaseGeometry.createSimple(colorValue, sizeValue, transform)
  137. const p = { ...PD.getDefaultValues(Params), ...props }
  138. return createValues(lines, s.transform, s.locationIterator, s.theme, p)
  139. }
  140. function updateValues(values: LinesValues, props: PD.Values<Params>) {
  141. BaseGeometry.updateValues(values, props)
  142. ValueCell.updateIfChanged(values.uSizeFactor, props.sizeFactor)
  143. ValueCell.updateIfChanged(values.dLineSizeAttenuation, props.lineSizeAttenuation)
  144. }
  145. function updateBoundingSphere(values: LinesValues, lines: Lines) {
  146. const { boundingSphere, invariantBoundingSphere } = getBoundingSphere(
  147. values.aStart.ref.value, values.aEnd.ref.value, lines.lineCount,
  148. values.aTransform.ref.value, values.instanceCount.ref.value
  149. )
  150. if (!Sphere3D.equals(boundingSphere, values.boundingSphere.ref.value)) {
  151. ValueCell.update(values.boundingSphere, boundingSphere)
  152. }
  153. if (!Sphere3D.equals(invariantBoundingSphere, values.invariantBoundingSphere.ref.value)) {
  154. ValueCell.update(values.invariantBoundingSphere, invariantBoundingSphere)
  155. }
  156. }
  157. }
  158. function getBoundingSphere(lineStart: Float32Array, lineEnd: Float32Array, lineCount: number, transform: Float32Array, transformCount: number) {
  159. const start = calculateBoundingSphere(lineStart, lineCount * 4, transform, transformCount, 0, 4)
  160. const end = calculateBoundingSphere(lineEnd, lineCount * 4, transform, transformCount, 0, 4)
  161. return {
  162. boundingSphere: Sphere3D.expandBySphere(start.boundingSphere, end.boundingSphere),
  163. invariantBoundingSphere: Sphere3D.expandBySphere(start.invariantBoundingSphere, end.invariantBoundingSphere)
  164. }
  165. }