// loaders.gl // SPDX-License-Identifier: MIT // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors import { applySearchParamsToUrl, type LoaderContext, type LoaderOptions } from '@loaders.gl/core/loader-utils'; import {COLMAPLoader} from './colmap-loader-types'; import {parseCOLMAPSparseModel} from './parse-colmap-sparse-model'; const {preload: _preload, ...metadata} = COLMAPLoader; /** Parser-bearing loader using the core fetch context for sibling model files. */ export const COLMAPLoaderWithParser = { ...metadata, async parse(cameras: ArrayBuffer, options?: LoaderOptions, context?: LoaderContext) { // load() retains the requested entry URL even when an object store redirects the response. const entryPattern = /^([^?#]*\/)?cameras\.bin(\?[^#]*)?(?:#.*)?$/; const match = entryPattern.exec(options?.core?.baseUrl || '') || entryPattern.exec(context?.url || ''); if (!context || !match) throw new Error('Load cameras.bin or call parseCOLMAPSparseModel with all three buffers'); const fetchSibling = async (name: string) => { const response = await context.fetch( applySearchParamsToUrl((match[1] || '') + name + (match[2] || ''), options), { signal: options?.signal || context.signal } ); if (!response.ok) throw new Error(`Failed to load COLMAP ${name} (HTTP ${response.status})`); return response.arrayBuffer(); }; const [images, points3D] = await Promise.all([ fetchSibling('images.bin'), fetchSibling('points3D.bin') ]); return parseCOLMAPSparseModel({cameras, images, points3D}); } } as const;