// loaders.gl // SPDX-License-Identifier: MIT // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors import type {COLMAPCamera, COLMAPImage, COLMAPPoint3D, COLMAPSparseModel} from './types'; // Indexed by upstream CameraModelId. Unknown widths cannot be skipped in this binary format. const CAMERA_MODELS: readonly (readonly [string, number])[] = [ ['SIMPLE_PINHOLE', 3], ['PINHOLE', 4], ['SIMPLE_RADIAL', 4], ['RADIAL', 5], ['OPENCV', 8], ['OPENCV_FISHEYE', 8], ['FULL_OPENCV', 12], ['FOV', 5], ['SIMPLE_RADIAL_FISHEYE', 4], ['RADIAL_FISHEYE', 5], ['THIN_PRISM_FISHEYE', 12], ['RAD_TAN_THIN_PRISM_FISHEYE', 16], ['SIMPLE_DIVISION', 4], ['DIVISION', 5], ['SIMPLE_FISHEYE', 3], ['FISHEYE', 4], ['EUCM', 6], ['EQUIRECTANGULAR', 2], ['SKEWED_PINHOLE', 5] ]; /** Cursor for unaligned, packed little-endian records. Check counts before allocating typed arrays. */ class BinaryReader { private readonly view: DataView; private readonly bytes: Uint8Array; private offset = 0; constructor(buffer: ArrayBuffer) { this.view = new DataView(buffer); this.bytes = new Uint8Array(buffer); } /** Read an unsigned color channel. */ uint8() { return this.view.getUint8(this.offset++); } /** Read a 32-bit ID or index. */ uint32() { const offset = this.offset; this.offset += 4; return this.view.getUint32(offset, true); } /** Read an exact 64-bit ID or count. */ uint64() { const offset = this.offset; this.offset += 8; return this.view.getBigUint64(offset, true); } /** Read a double without alignment assumptions. */ float64() { const offset = this.offset; this.offset += 8; return this.view.getFloat64(offset, true); } /** Convert a size only when JavaScript can represent it exactly. */ size() { const value = Number(this.uint64()); if (!Number.isSafeInteger(value)) throw new Error('COLMAP integer exceeds JavaScript safe range'); return value; } /** Bound a record count by the bytes remaining before allocating. */ count(minimumBytes: number) { const count = this.size(); if (count > (this.view.byteLength - this.offset) / minimumBytes) throw new Error('Truncated COLMAP records'); return count; } /** Read a zero-terminated UTF-8 image path. */ name() { const end = this.bytes.indexOf(0, this.offset); if (end < 0) throw new Error('Unterminated COLMAP image name'); const start = this.offset; this.offset = end + 1; return new TextDecoder().decode(this.bytes.subarray(start, end)); } /** Read an XYZ vector. */ vector(): [number, number, number] { return [this.float64(), this.float64(), this.float64()]; } /** Require exact consumption of the component file. */ checkEnd() { if (this.offset !== this.view.byteLength) throw new Error('Unexpected trailing COLMAP data'); } } /** Index a component file by its original record IDs. */ function parseRecords( buffer: ArrayBuffer, minimumBytes: number, read: (reader: BinaryReader) => RecordType ) { const reader = new BinaryReader(buffer); const result = new Map(); const count = reader.count(minimumBytes); for (let index = 0; index < count; index++) { const record = read(reader); if (result.has(record.id)) throw new Error('Duplicate COLMAP record ID'); result.set(record.id, record); } reader.checkEnd(); return result; } /** Read calibration with model-dependent parameter count. */ function readCamera(reader: BinaryReader): COLMAPCamera { const id = reader.uint32(); const modelId = reader.uint32(); const model = CAMERA_MODELS[modelId]; if (!model) throw new Error(`Unknown COLMAP camera model ${modelId}`); const width = reader.size(); const height = reader.size(); return { id, modelId, model: model[0], width, height, parameters: Array.from({length: model[1]}, () => reader.float64()) }; } /** Read a registered image and its indexed observations. */ function readImage(reader: BinaryReader): COLMAPImage { const id = reader.uint32(); const quaternion: COLMAPImage['quaternion'] = [reader.float64(), ...reader.vector()]; const translation = reader.vector(); const cameraId = reader.uint32(); const name = reader.name(); const count = reader.count(24); const imagePoints = new Float64Array(count * 2); const point3DIds = new BigUint64Array(count); for (let index = 0; index < count; index++) { imagePoints[index * 2] = reader.float64(); imagePoints[index * 2 + 1] = reader.float64(); point3DIds[index] = reader.uint64(); } return {id, cameraId, name, quaternion, translation, imagePoints, point3DIds}; } /** Read a triangulated world point and its observation track. */ function readPoint(reader: BinaryReader): COLMAPPoint3D { const id = reader.uint64(); const position = reader.vector(); const color: COLMAPPoint3D['color'] = [reader.uint8(), reader.uint8(), reader.uint8()]; const error = reader.float64(); const track = new Uint32Array(reader.count(8) * 2); for (let index = 0; index < track.length; index++) track[index] = reader.uint32(); return {id, position, color, error, track}; } /** Decode a model from its three component files, e.g. from a browser directory picker. */ export function parseCOLMAPSparseModel(files: { cameras: ArrayBuffer; images: ArrayBuffer; points3D: ArrayBuffer; }): COLMAPSparseModel { return { cameras: parseRecords(files.cameras, 24, readCamera), images: parseRecords(files.images, 73, readImage), points3D: parseRecords(files.points3D, 51, readPoint) }; }