lines.ts 8.2 KB

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