verify-streaming-tiles.mjs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Verify bounded streaming-v2 tiles: request volumes, Draco payloads, budgets, and coverage.
  2. import assert from 'node:assert/strict'
  3. import { readFileSync, writeFileSync } from 'node:fs'
  4. import { resolve, join } from 'node:path'
  5. import draco3d from 'draco3d'
  6. const argv = process.argv.slice(2)
  7. const reportIndex = argv.indexOf('--report')
  8. const reportPath = reportIndex < 0 ? null : argv.splice(reportIndex, 2)[1]
  9. const dirs = argv.length ? argv : [process.argv[2]]
  10. assert.ok(dirs[0], 'batch directory required')
  11. const decoderModule = await draco3d.createDecoderModule({})
  12. function parseGlb(path) {
  13. const bytes = readFileSync(path)
  14. assert.equal(bytes.toString('ascii', 0, 4), 'glTF', path)
  15. assert.equal(bytes.readUInt32LE(4), 2, path)
  16. assert.equal(bytes.readUInt32LE(8), bytes.length, path)
  17. const jsonLength = bytes.readUInt32LE(12)
  18. const json = JSON.parse(bytes.toString('utf8', 20, 20 + jsonLength))
  19. const binOffset = 20 + jsonLength
  20. assert.equal(bytes.toString('ascii', binOffset + 4, binOffset + 8), 'BIN\0', path)
  21. const binLength = bytes.readUInt32LE(binOffset)
  22. return { bytes, json, bin: bytes.subarray(binOffset + 8, binOffset + 8 + binLength) }
  23. }
  24. function decodePrimitive(path, json, bin, primitive, translation, bounds) {
  25. const extension = primitive.extensions?.KHR_draco_mesh_compression
  26. assert.ok(extension, `${path}: missing Draco extension`)
  27. const view = json.bufferViews[extension.bufferView]
  28. assert.ok(view, `${path}: missing Draco bufferView`)
  29. const encoded = bin.subarray(view.byteOffset || 0, (view.byteOffset || 0) + view.byteLength)
  30. const decoder = new decoderModule.Decoder()
  31. const buffer = new decoderModule.DecoderBuffer()
  32. const mesh = new decoderModule.Mesh()
  33. let status
  34. try {
  35. buffer.Init(new Int8Array(encoded.buffer, encoded.byteOffset, encoded.byteLength), encoded.byteLength)
  36. status = decoder.DecodeBufferToMesh(buffer, mesh)
  37. assert.ok(status.ok(), `${path}: ${status.error_msg()}`)
  38. const points = mesh.num_points()
  39. const faces = mesh.num_faces()
  40. assert.equal(points, json.accessors[primitive.attributes.POSITION].count, `${path}: point count mismatch`)
  41. assert.equal(faces * 3, json.accessors[primitive.indices].count, `${path}: face count mismatch`)
  42. const attribute = decoder.GetAttributeByUniqueId(mesh, extension.attributes.POSITION)
  43. assert.equal(attribute.num_components(), 3)
  44. const positionBytes = points * 3 * 4
  45. const positionPtr = decoderModule._malloc(positionBytes)
  46. try {
  47. decoder.GetAttributeDataArrayForAllPoints(mesh, attribute, decoderModule.DT_FLOAT32, positionBytes, positionPtr)
  48. const positions = new Float32Array(decoderModule.HEAPF32.buffer, positionPtr, points * 3)
  49. for (let index = 0; index < points; index++) {
  50. const enu = [positions[index * 3] + translation[0], -(positions[index * 3 + 2] + translation[2]),
  51. positions[index * 3 + 1] + translation[1]]
  52. for (let axis = 0; axis < 3; axis++) {
  53. assert.ok(Number.isFinite(enu[axis]), `${path}: non-finite position`)
  54. assert.ok(enu[axis] >= bounds.min[axis] - 0.05 && enu[axis] <= bounds.max[axis] + 0.05,
  55. `${path}: decoded world position outside tile bounds`)
  56. }
  57. }
  58. } finally { decoderModule._free(positionPtr) }
  59. const indexBytes = faces * 3 * 4
  60. const indexPtr = decoderModule._malloc(indexBytes)
  61. try {
  62. decoder.GetTrianglesUInt32Array(mesh, indexBytes, indexPtr)
  63. const indices = new Uint32Array(decoderModule.HEAPU32.buffer, indexPtr, faces * 3)
  64. for (const index of indices) assert.ok(index < points, `${path}: out-of-range triangle index`)
  65. } finally { decoderModule._free(indexPtr) }
  66. return { points, triangles: faces }
  67. } finally {
  68. if (status) decoderModule.destroy(status)
  69. decoderModule.destroy(mesh); decoderModule.destroy(buffer); decoderModule.destroy(decoder)
  70. }
  71. }
  72. function collectTiles(tile, out = []) {
  73. if (tile.content?.uri) out.push(tile)
  74. for (const child of tile.children || []) collectTiles(child, out)
  75. return out
  76. }
  77. function boxVolume(box) {
  78. assert.equal(box.length, 12)
  79. assert.deepEqual([box[4], box[5], box[6], box[8], box[9], box[10]], [0, 0, 0, 0, 0, 0])
  80. return Math.max(0.001, box[3] * 2) * Math.max(0.001, box[7] * 2) * Math.max(0.001, box[11] * 2)
  81. }
  82. const all = []
  83. for (const directory of dirs) {
  84. const dir = resolve(directory)
  85. const tree = JSON.parse(readFileSync(join(dir, 'tileset.json'), 'utf8'))
  86. const manifest = JSON.parse(readFileSync(join(dir, 'manifest.json'), 'utf8'))
  87. assert.equal(manifest.provenance?.gisFootprints, true, `${directory}: GIS provenance missing`)
  88. assert.equal(manifest.provenance?.roadsProcedural, false, `${directory}: procedural roads provenance`)
  89. const tiles = collectTiles(tree.root).filter((tile) => tile !== tree.root)
  90. const records = new Map(manifest.files.map((record) => [record.uri, record]))
  91. assert.equal(records.size, manifest.files.length, `${directory}: duplicate file records`)
  92. assert.ok(tiles.length > 0, `${directory}: no content tiles`)
  93. assert.ok(tree.root.content?.uri, `${directory}: no root anchor`)
  94. const root = tree.root
  95. assert.ok(root.children?.length >= 2, `${directory}: too few structural children`)
  96. const ids = manifest.cells.flatMap((cell) => cell.buildingIds)
  97. assert.equal(ids.length, manifest.count, `${directory}: wrong building count`)
  98. assert.equal(new Set(ids).size, manifest.count, `${directory}: duplicate or missing building IDs`)
  99. const source = JSON.parse(readFileSync(join(dir, '..', 'manifest.json'), 'utf8'))
  100. assert.deepEqual([...ids].sort(), source.cells.flatMap((cell) => cell.buildingIds).sort(), `${directory}: source coverage changed`)
  101. const byLevel = { lod1: { triangles: 0, bytes: 0 }, lod0: { triangles: 0, bytes: 0 } }
  102. let files = 0, triangles = 0, vertices = 0, bytes = 0, maxVertices = 0, maxTriangles = 0
  103. for (const tile of tiles) {
  104. assert.ok(tile.viewerRequestVolume?.sphere, `${directory}: content missing viewerRequestVolume`)
  105. assert.ok(tile.content?.uri !== 'root-anchor.glb', `${directory}: anchor in a request tile`)
  106. const record = records.get(tile.content.uri)
  107. assert.ok(record, `${directory}: unrecorded ${tile.content.uri}`)
  108. assert.ok(tile.boundingVolume?.box, `${directory}: missing content box`)
  109. boxVolume(tile.boundingVolume.box)
  110. const box = tile.boundingVolume.box
  111. const center = box.slice(0, 3)
  112. const half = [box[3], box[7], box[11]]
  113. const bounds = { min: center.map((value, axis) => value - half[axis]), max: center.map((value, axis) => value + half[axis]) }
  114. const sphere = tile.viewerRequestVolume.sphere
  115. const distance = record.level === 'lod1' ? manifest.budget.coarseDistance : manifest.budget.detailDistance
  116. assert.equal(sphere.length, 4)
  117. sphere.slice(0, 3).forEach((value, axis) => assert.ok(Math.abs(value - center[axis]) < 0.01, `${directory}: request center drift`))
  118. assert.ok(Math.abs(sphere[3] - Math.hypot(...half) - distance) < 0.01, `${directory}: wrong distance request gate`)
  119. const path = join(dir, tile.content.uri)
  120. const { bytes: raw, json, bin } = parseGlb(path)
  121. const nodes = json.nodes.filter((node) => node.mesh !== undefined)
  122. assert.equal(nodes.length, 1, `${path}: expected one mesh node`)
  123. assert.ok(!nodes[0].matrix && !nodes[0].rotation && !nodes[0].scale, `${path}: unexpected transform`)
  124. const translation = nodes[0].translation || [0, 0, 0]
  125. assert.equal(translation.length, 3)
  126. assert.ok(json.extensionsRequired?.includes('KHR_draco_mesh_compression'), `${path}: Draco is not required`)
  127. let fileTriangles = 0, fileVertices = 0
  128. for (const primitive of json.meshes[nodes[0].mesh].primitives) {
  129. const decoded = decodePrimitive(path, json, bin, primitive, translation, bounds)
  130. fileTriangles += decoded.triangles
  131. fileVertices += decoded.points
  132. }
  133. assert.equal(fileTriangles, record.triangles, `${path}: triangle total differs from manifest`)
  134. assert.equal(raw.length, record.bytes, `${path}: byte total differs from manifest`)
  135. assert.ok(fileVertices <= manifest.budget.maxVertices, `${path}: vertex budget exceeded`)
  136. byLevel[record.level].triangles += fileTriangles
  137. byLevel[record.level].bytes += raw.length
  138. assert.equal(fileVertices, record.vertices, `${path}: vertex total differs from manifest`)
  139. files++; triangles += fileTriangles; vertices += fileVertices; bytes += raw.length
  140. maxVertices = Math.max(maxVertices, fileVertices); maxTriangles = Math.max(maxTriangles, fileTriangles)
  141. records.delete(tile.content.uri)
  142. }
  143. assert.equal(records.size, 0, `${directory}: unreferenced manifest files`)
  144. for (const level of ['lod1', 'lod0']) {
  145. assert.deepEqual(byLevel[level], manifest.totals[level], `${directory}: ${level} totals differ from files`)
  146. assert.equal(byLevel[level].triangles, source.cells.reduce((sum, cell) => sum + cell[level].triangles, 0),
  147. `${directory}: ${level} source triangle count changed`)
  148. }
  149. const result = { directory, buildings: manifest.count, files, triangles, vertices, bytes, maxVertices, maxTriangles,
  150. budget: manifest.budget }
  151. all.push(result)
  152. console.log(JSON.stringify(result))
  153. }
  154. const result = { passed: true, batches: all, totals: {
  155. buildings: all.reduce((sum, item) => sum + item.buildings, 0),
  156. files: all.reduce((sum, item) => sum + item.files, 0),
  157. triangles: all.reduce((sum, item) => sum + item.triangles, 0),
  158. vertices: all.reduce((sum, item) => sum + item.vertices, 0),
  159. bytes: all.reduce((sum, item) => sum + item.bytes, 0),
  160. } }
  161. if (reportPath) writeFileSync(reportPath, JSON.stringify(result, null, 2) + '\n')
  162. console.log(JSON.stringify(result))