| 1 |
import {describe, expect, test} from "vitest"; |
| 2 |
|
| 3 |
import {discoverSparseModels, parseReconstruction, reconstructionTransferables} from "../viewer_src/parser"; |
| 4 |
import {findPoint3DIndex, point2DAt, point3DAt} from "../viewer_src/types"; |
| 5 |
import {BinaryWriter, cameraFile, imageFile, modernRigFiles, pointFile} from "./binary_fixture"; |
| 6 |
|
| 7 |
function modelFiles(cameraId = 1): Map<string, File> { |
| 8 |
return new Map([ |
| 9 |
["cameras.bin", cameraFile(cameraId)], |
| 10 |
["images.bin", imageFile(2, cameraId)], |
| 11 |
["points3D.bin", pointFile()], |
| 12 |
]); |
| 13 |
} |
| 14 |
|
| 15 |
describe("binary reconstruction parser", () => { |
| 16 |
test("parses a legacy model without losing uint64 point ids", async () => { |
| 17 |
const pointId = 9007199254740993n; |
| 18 |
const files = modelFiles(); |
| 19 |
files.set("images.bin", imageFile(2, 1, pointId)); |
| 20 |
files.set("points3D.bin", pointFile(2, pointId)); |
| 21 |
const reconstruction = await parseReconstruction(files); |
| 22 |
expect(reconstruction.modernRigFormat).toBe(false); |
| 23 |
expect(reconstruction.cameras.get(1)?.params).toEqual([500, 320, 240]); |
| 24 |
expect(reconstruction.images.get(2)?.camFromWorld.translation).toEqual([4, 5, 6]); |
| 25 |
expect(point2DAt(reconstruction.images.get(2)!, 0)?.point3DId).toBe(pointId); |
| 26 |
const pointIndex = findPoint3DIndex(reconstruction.points3D, pointId); |
| 27 |
expect(point3DAt(reconstruction.points3D, pointIndex).track).toEqual([{imageId: 2, point2DIdx: 0}]); |
| 28 |
expect(reconstruction.points3D.xyz).toBeInstanceOf(Float64Array); |
| 29 |
expect(reconstruction.points3D.colors).toBeInstanceOf(Uint8Array); |
| 30 |
expect(reconstruction.points3D.trackImageIds).toBeInstanceOf(Uint32Array); |
| 31 |
const transfer = reconstructionTransferables(reconstruction); |
| 32 |
expect(transfer).toHaveLength(9); |
| 33 |
const transferred = structuredClone(reconstruction, {transfer}); |
| 34 |
expect(reconstruction.points3D.ids.byteLength).toBe(0); |
| 35 |
expect(transferred.points3D.ids[0]).toBe(pointId); |
| 36 |
}); |
| 37 |
|
| 38 |
test("sorts point ids for compact binary-search lookup", async () => { |
| 39 |
const points = new BinaryWriter().u64(2); |
| 40 |
for (const [id, x] of [[9, 90], [3, 30]] as const) { |
| 41 |
points.u64(id).f64(x).f64(0).f64(0).u8(1).u8(2).u8(3).f64(0.5).u64(0); |
| 42 |
} |
| 43 |
const files = modelFiles(); |
| 44 |
files.set("points3D.bin", points.file("points3D.bin")); |
| 45 |
const reconstruction = await parseReconstruction(files); |
| 46 |
expect([...reconstruction.points3D.ids]).toEqual([3n, 9n]); |
| 47 |
expect(point3DAt(reconstruction.points3D, findPoint3DIndex(reconstruction.points3D, 3n)).xyz).toEqual([30, 0, 0]); |
| 48 |
expect(point3DAt(reconstruction.points3D, findPoint3DIndex(reconstruction.points3D, 9n)).xyz).toEqual([90, 0, 0]); |
| 49 |
}); |
| 50 |
|
| 51 |
test("composes modern sensor and rig poses", async () => { |
| 52 |
const files = modelFiles(2); |
| 53 |
const [rigs, frames] = modernRigFiles(); |
| 54 |
files.set("rigs.bin", rigs); |
| 55 |
files.set("frames.bin", frames); |
| 56 |
const reconstruction = await parseReconstruction(files); |
| 57 |
expect(reconstruction.modernRigFormat).toBe(true); |
| 58 |
expect(reconstruction.images.get(2)?.camFromWorld.translation).toEqual([1, 2, 0]); |
| 59 |
expect(reconstruction.images.get(2)?.frameId).toBe(9); |
| 60 |
expect(reconstruction.images.get(2)?.rigId).toBe(7); |
| 61 |
}); |
| 62 |
|
| 63 |
test("rejects incomplete and truncated models", async () => { |
| 64 |
const files = modelFiles(); |
| 65 |
files.set("rigs.bin", new File([new Uint8Array(8)], "rigs.bin")); |
| 66 |
await expect(parseReconstruction(files)).rejects.toThrow("both rigs.bin and frames.bin"); |
| 67 |
files.delete("rigs.bin"); |
| 68 |
files.set("cameras.bin", new File([new Uint8Array([1])], "cameras.bin")); |
| 69 |
await expect(parseReconstruction(files)).rejects.toThrow("truncated"); |
| 70 |
}); |
| 71 |
}); |
| 72 |
|
| 73 |
test("discovers nested models and ignores unrelated files", () => { |
| 74 |
const files = [cameraFile(), imageFile(), pointFile()]; |
| 75 |
const entries = files.map((file) => ({path: `workspace/sparse/0/${file.name}`, file})); |
| 76 |
entries.push({path: "workspace/images/frame.jpg", file: new File([], "frame.jpg")}); |
| 77 |
const candidates = discoverSparseModels(entries); |
| 78 |
expect(candidates).toHaveLength(1); |
| 79 |
expect(candidates[0]?.path).toBe("workspace/sparse/0"); |
| 80 |
}); |