vertex-map.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 { ChunkedArray } from 'mol-data/util';
  7. import { Mesh } from './mesh';
  8. /** Mapping between vertices and ids */
  9. interface VertexMap {
  10. idCount: number,
  11. offsetCount: number,
  12. ids: Helpers.NumberArray | undefined
  13. offsets: Uint32Array,
  14. }
  15. function createOffsets(idCount: number, ids: Helpers.NumberArray | undefined) {
  16. if (!ids) return new Uint32Array(0)
  17. const offsets = ChunkedArray.create(Uint32Array, 1, 1024, 2048);
  18. let prevId = ids[0]
  19. ChunkedArray.add(offsets, 0)
  20. for (let i = 1; i < idCount; ++i) {
  21. if (prevId !== ids[i]) {
  22. prevId = ids[i]
  23. ChunkedArray.add(offsets, i)
  24. }
  25. }
  26. ChunkedArray.add(offsets, idCount)
  27. return ChunkedArray.compact(offsets, false) as Uint32Array
  28. }
  29. namespace VertexMap {
  30. export function create(idCount: number, offsetCount: number, ids: Helpers.NumberArray | undefined, offsets: Uint32Array): VertexMap {
  31. return {
  32. idCount,
  33. offsetCount,
  34. ids,
  35. offsets
  36. }
  37. }
  38. export function fromMesh(mesh: Mesh) {
  39. const ids = mesh.idBuffer.ref.value
  40. const offsets = createOffsets(mesh.vertexCount, ids)
  41. return create(mesh.vertexCount, offsets.length, ids, offsets)
  42. }
  43. export function rangeFromId (id: number, vertexMap: VertexMap) {
  44. return [0, 0]
  45. }
  46. }
  47. export default VertexMap