util.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 { Sphere3D } from 'mol-math/geometry'
  7. import { Mat4, Vec3 } from 'mol-math/linear-algebra'
  8. import { ValueCell } from 'mol-util';
  9. export function calculateTextureInfo (n: number, itemSize: number) {
  10. const sqN = Math.sqrt(n)
  11. let width = Math.ceil(sqN)
  12. width = width + (itemSize - (width % itemSize)) % itemSize
  13. const height = width > 0 ? Math.ceil(n / width) : 0
  14. return { width, height, length: width * height * itemSize }
  15. }
  16. export interface TextureImage {
  17. array: Uint8Array
  18. width: number
  19. height: number
  20. }
  21. export function createTextureImage (n: number, itemSize: number): TextureImage {
  22. const { length, width, height } = calculateTextureInfo(n, itemSize)
  23. return { array: new Uint8Array(length), width, height }
  24. }
  25. export interface PositionValues {
  26. aPosition: ValueCell<Float32Array>
  27. drawCount: ValueCell<number>,
  28. aTransform: ValueCell<Float32Array>,
  29. instanceCount: ValueCell<number>,
  30. }
  31. function getPositionDataFromValues(values: PositionValues) {
  32. return {
  33. position: values.aPosition.ref.value,
  34. positionCount: values.drawCount.ref.value / 3 / 3,
  35. transform: values.aTransform.ref.value,
  36. transformCount: values.instanceCount.ref.value
  37. }
  38. }
  39. export function calculateBoundingSphereFromValues(values: PositionValues) {
  40. const { position, positionCount, transform, transformCount } = getPositionDataFromValues(values)
  41. return calculateBoundingSphere(position, positionCount, transform, transformCount)
  42. }
  43. export function calculateBoundingSphere(position: Float32Array, positionCount: number, transform: Float32Array, transformCount: number): Sphere3D {
  44. const m = Mat4.zero()
  45. let cx = 0, cy = 0, cz = 0;
  46. let radiusSq = 0;
  47. for (let i = 0, _i = positionCount * 3; i < _i; i += 3) {
  48. cx += position[i];
  49. cy += position[i + 1];
  50. cz += position[i + 2];
  51. }
  52. if (positionCount > 0) {
  53. cx /= positionCount;
  54. cy /= positionCount;
  55. cz /= positionCount;
  56. }
  57. for (let i = 0, _i = positionCount * 3; i < _i; i += 3) {
  58. const dx = position[i] - cx
  59. const dy = position[i + 1] - cy
  60. const dz = position[i + 2] - cz;
  61. const d = dx * dx + dy * dy + dz * dz;
  62. if (d > radiusSq) radiusSq = d;
  63. }
  64. const c = Vec3.create(cx, cy, cz)
  65. const ct = Vec3.zero()
  66. const center = Vec3.zero()
  67. const centers = new Float32Array(3 * transformCount)
  68. for (let i = 0, _i = transformCount; i < _i; ++i) {
  69. Mat4.fromArray(m, transform, i * 16)
  70. Vec3.transformMat4(ct, c, m)
  71. Vec3.add(center, center, ct)
  72. Vec3.toArray(ct, centers, i * 3)
  73. }
  74. Vec3.scale(center, center, 1 / transformCount)
  75. let r = Math.sqrt(radiusSq)
  76. let radius = r
  77. for (let i = 0, _i = transformCount; i < _i; ++i) {
  78. Vec3.fromArray(ct, centers, i * 3)
  79. radius = Math.max(radius, Vec3.distance(center, ct) + r)
  80. }
  81. return { center, radius };
  82. }