use nalgebra::{Point2, Point3, Quaternion, UnitQuaternion, Vector3};
use std::collections::BTreeMap;
use std::fs;
use std::num::{ParseFloatError, ParseIntError};
use std::path::Path;
use thiserror::Error;
use visloc_core::geometry::Pose;
use visloc_core::types::{
Camera, CameraModel, Frame, Keyframe, Landmark, LandmarkDescriptorStore, Observation,
VisualMap, VisualMapValidationReport,
};
use visloc_localization::{DescriptorProvider, MapProvider};
use visloc_vision::features::FeatureSet;
use visloc_vision::stereo_vo::StereoFeature;
use crate::descriptors::{read_landmark_descriptors_txt, DescriptorStoreError};
#[derive(Debug, Error)]
pub enum ColmapError {
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("integer parse error: {0}")]
ParseInt(#[from] ParseIntError),
#[error("float parse error: {0}")]
ParseFloat(#[from] ParseFloatError),
#[error("invalid COLMAP line in {file}: {line}")]
InvalidLine { file: &'static str, line: String },
#[error("invalid COLMAP binary in {file}: {message}")]
InvalidBinary { file: &'static str, message: String },
#[error("invalid COLMAP export input: {0}")]
InvalidExportInput(String),
}
pub type ReconstructionLandmark = (Point3<f64>, Vec<(usize, usize, Point2<f64>)>);
#[derive(Debug, Clone, PartialEq)]
pub struct ColmapExportSummary {
pub frame_count: usize,
pub landmark_count: usize,
pub observation_count: usize,
}
#[derive(Debug, Error)]
pub enum ColmapMapProviderError {
#[error("COLMAP error: {0}")]
Colmap(#[from] ColmapError),
#[error("descriptor store error: {0}")]
DescriptorStore(#[from] DescriptorStoreError),
#[error("COLMAP map validation failed: {0:?}")]
InvalidMap(VisualMapValidationReport),
}
#[derive(Debug, Clone, PartialEq)]
pub struct ColmapMapProvider {
pub map: VisualMap,
pub descriptor_store: Option<LandmarkDescriptorStore>,
}
impl ColmapMapProvider {
pub fn from_text_model_dir(path: impl AsRef<Path>) -> Result<Self, ColmapMapProviderError> {
Ok(Self {
map: read_colmap_text_model(path)?,
descriptor_store: None,
})
}
pub fn from_binary_model_dir(path: impl AsRef<Path>) -> Result<Self, ColmapMapProviderError> {
Ok(Self {
map: read_colmap_binary_model(path)?,
descriptor_store: None,
})
}
pub fn from_text_model_dir_validated(
path: impl AsRef<Path>,
) -> Result<Self, ColmapMapProviderError> {
let provider = Self::from_text_model_dir(path)?;
provider
.validate_map()
.into_result()
.map_err(ColmapMapProviderError::InvalidMap)?;
Ok(provider)
}
pub fn from_binary_model_dir_validated(
path: impl AsRef<Path>,
) -> Result<Self, ColmapMapProviderError> {
let provider = Self::from_binary_model_dir(path)?;
provider
.validate_map()
.into_result()
.map_err(ColmapMapProviderError::InvalidMap)?;
Ok(provider)
}
pub fn from_text_model_dir_with_descriptors(
model_path: impl AsRef<Path>,
descriptor_path: impl AsRef<Path>,
) -> Result<Self, ColmapMapProviderError> {
Ok(Self {
map: read_colmap_text_model(model_path)?,
descriptor_store: Some(read_landmark_descriptors_txt(descriptor_path)?),
})
}
pub fn from_text_model_dir_with_descriptors_validated(
model_path: impl AsRef<Path>,
descriptor_path: impl AsRef<Path>,
) -> Result<Self, ColmapMapProviderError> {
let provider = Self::from_text_model_dir_with_descriptors(model_path, descriptor_path)?;
provider
.validate_for_localization()
.into_result()
.map_err(ColmapMapProviderError::InvalidMap)?;
Ok(provider)
}
pub fn validate_map(&self) -> VisualMapValidationReport {
self.map.validate()
}
pub fn validate_for_localization(&self) -> VisualMapValidationReport {
self.map
.validate_with_descriptors(self.descriptor_store.as_ref())
}
}
impl MapProvider for ColmapMapProvider {
fn visual_map(&self) -> &VisualMap {
&self.map
}
}
impl DescriptorProvider for ColmapMapProvider {
fn landmark_descriptor_store(&self) -> Option<&LandmarkDescriptorStore> {
self.descriptor_store.as_ref()
}
}
pub fn read_colmap_text_model(path: impl AsRef<Path>) -> Result<VisualMap, ColmapError> {
let path = path.as_ref();
let mut map = VisualMap::new();
let cameras = parse_cameras_txt(&fs::read_to_string(path.join("cameras.txt"))?)?;
map.cameras = cameras
.into_iter()
.map(|camera| (camera.id, camera))
.collect();
let landmarks = parse_points3d_txt(&fs::read_to_string(path.join("points3D.txt"))?)?;
map.landmarks = landmarks
.into_iter()
.map(|landmark| (landmark.id, landmark))
.collect();
let keyframes = parse_images_txt(&fs::read_to_string(path.join("images.txt"))?)?;
map.keyframes = keyframes
.into_iter()
.map(|keyframe| (keyframe.frame.id, keyframe))
.collect();
Ok(map)
}
pub fn read_colmap_binary_model(path: impl AsRef<Path>) -> Result<VisualMap, ColmapError> {
let path = path.as_ref();
let mut map = VisualMap::new();
let cameras = parse_cameras_bin(&fs::read(path.join("cameras.bin"))?)?;
map.cameras = cameras
.into_iter()
.map(|camera| (camera.id, camera))
.collect();
let landmarks = parse_points3d_bin(&fs::read(path.join("points3D.bin"))?)?;
map.landmarks = landmarks
.into_iter()
.map(|landmark| (landmark.id, landmark))
.collect();
let keyframes = parse_images_bin(&fs::read(path.join("images.bin"))?)?;
map.keyframes = keyframes
.into_iter()
.map(|keyframe| (keyframe.frame.id, keyframe))
.collect();
Ok(map)
}
pub fn write_colmap_text_model(map: &VisualMap, path: impl AsRef<Path>) -> Result<(), ColmapError> {
let path = path.as_ref();
for camera in map.cameras.values() {
validated_colmap_camera_record(camera)?;
}
fs::create_dir_all(path)?;
fs::write(path.join("cameras.txt"), format_cameras_txt(map))?;
fs::write(path.join("images.txt"), format_images_txt(map))?;
fs::write(path.join("points3D.txt"), format_points3d_txt(map))?;
Ok(())
}
pub fn write_colmap_binary_model(
map: &VisualMap,
path: impl AsRef<Path>,
) -> Result<(), ColmapError> {
let path = path.as_ref();
let cameras_bytes = format_cameras_bin(map)?;
let images_bytes = format_images_bin(map)?;
let points_bytes = format_points3d_bin(map)?;
fs::create_dir_all(path)?;
fs::write(path.join("cameras.bin"), cameras_bytes)?;
fs::write(path.join("images.bin"), images_bytes)?;
fs::write(path.join("points3D.bin"), points_bytes)?;
Ok(())
}
fn u32_id(value: u64, what: &str) -> Result<u32, ColmapError> {
u32::try_from(value).map_err(|_| {
ColmapError::InvalidExportInput(format!(
"{what} {value} does not fit in u32 (COLMAP binary models use 32-bit ids)"
))
})
}
fn format_cameras_bin(map: &VisualMap) -> Result<Vec<u8>, ColmapError> {
let mut cameras = map.cameras.values().collect::<Vec<_>>();
cameras.sort_by_key(|camera| camera.id);
let mut bytes = Vec::new();
bytes.extend_from_slice(&(cameras.len() as u64).to_le_bytes());
for camera in cameras {
let (model, params) = validated_colmap_camera_record(camera)?;
let model_id = colmap_id_from_camera_model(&model)?;
bytes.extend_from_slice(&u32_id(camera.id, "camera id")?.to_le_bytes());
bytes.extend_from_slice(&model_id.to_le_bytes());
bytes.extend_from_slice(&u64::from(camera.width).to_le_bytes());
bytes.extend_from_slice(&u64::from(camera.height).to_le_bytes());
for param in params {
bytes.extend_from_slice(¶m.to_le_bytes());
}
}
Ok(bytes)
}
fn format_images_bin(map: &VisualMap) -> Result<Vec<u8>, ColmapError> {
let mut keyframes = map.keyframes.values().collect::<Vec<_>>();
keyframes.sort_by_key(|keyframe| keyframe.frame.id);
let mut bytes = Vec::new();
bytes.extend_from_slice(&(keyframes.len() as u64).to_le_bytes());
for keyframe in keyframes {
let frame = &keyframe.frame;
bytes.extend_from_slice(&u32_id(frame.id, "image id")?.to_le_bytes());
let pose = frame.pose.clone().unwrap_or_default();
let q = pose.world_to_camera.rotation.quaternion();
let t = pose.world_to_camera.translation;
for value in [q.w, q.i, q.j, q.k, t.x, t.y, t.z] {
bytes.extend_from_slice(&value.to_le_bytes());
}
bytes.extend_from_slice(&u32_id(frame.camera_id, "camera id")?.to_le_bytes());
bytes.extend_from_slice(format!("image_{}.jpg", frame.id).as_bytes());
bytes.push(0);
let mut landmark_by_keypoint = BTreeMap::new();
for observation in &keyframe.observations {
landmark_by_keypoint
.entry(observation.keypoint_index)
.or_insert(observation.landmark_id);
}
bytes.extend_from_slice(&(frame.keypoints.len() as u64).to_le_bytes());
for (keypoint_index, xy) in frame.keypoints.iter().enumerate() {
bytes.extend_from_slice(&xy.x.to_le_bytes());
bytes.extend_from_slice(&xy.y.to_le_bytes());
let point3d_id = match landmark_by_keypoint.get(&keypoint_index) {
Some(&landmark_id) => i64::try_from(landmark_id).map_err(|_| {
ColmapError::InvalidExportInput(format!(
"landmark id {landmark_id} does not fit in COLMAP's signed POINT3D_ID"
))
})?,
None => -1,
};
bytes.extend_from_slice(&point3d_id.to_le_bytes());
}
}
Ok(bytes)
}
fn format_points3d_bin(map: &VisualMap) -> Result<Vec<u8>, ColmapError> {
let mut landmarks = map.landmarks.values().collect::<Vec<_>>();
landmarks.sort_by_key(|landmark| landmark.id);
let mut bytes = Vec::new();
bytes.extend_from_slice(&(landmarks.len() as u64).to_le_bytes());
for landmark in landmarks {
bytes.extend_from_slice(&landmark.id.to_le_bytes());
for value in [
landmark.position.x,
landmark.position.y,
landmark.position.z,
] {
bytes.extend_from_slice(&value.to_le_bytes());
}
bytes.extend_from_slice(&[255u8, 255u8, 255u8]);
bytes.extend_from_slice(&0.0f64.to_le_bytes());
let mut observations = landmark.observations.iter().collect::<Vec<_>>();
observations.sort_by_key(|observation| (observation.frame_id, observation.keypoint_index));
bytes.extend_from_slice(&(observations.len() as u64).to_le_bytes());
for observation in observations {
bytes.extend_from_slice(&u32_id(observation.frame_id, "track image id")?.to_le_bytes());
let keypoint_index = u32_id(observation.keypoint_index as u64, "track POINT2D_IDX")?;
bytes.extend_from_slice(&keypoint_index.to_le_bytes());
}
}
Ok(bytes)
}
pub fn write_colmap_text_model_for_3dgs<F>(
out_dir: impl AsRef<Path>,
camera: &Camera,
poses: &[Pose],
left_features: &[FeatureSet],
stereo_per_frame: &[Vec<StereoFeature>],
image_name: F,
) -> Result<ColmapExportSummary, ColmapError>
where
F: Fn(usize) -> String,
{
if poses.len() != left_features.len() || poses.len() != stereo_per_frame.len() {
return Err(ColmapError::InvalidExportInput(format!(
"input length mismatch: poses={}, left_features={}, stereo_per_frame={}",
poses.len(),
left_features.len(),
stereo_per_frame.len(),
)));
}
let (colmap_model, colmap_params) = validated_colmap_camera_record(camera)?;
let out_dir = out_dir.as_ref();
fs::create_dir_all(out_dir)?;
let mut cameras_text = String::from("# CAMERA_ID MODEL WIDTH HEIGHT PARAMS[]\n");
cameras_text.push_str(&format_camera_line(camera, &colmap_model, &colmap_params));
fs::write(out_dir.join("cameras.txt"), cameras_text)?;
let mut landmark_count: u64 = 0;
let mut observation_count: usize = 0;
let mut frame_landmarks: Vec<Vec<(usize, u64)>> = Vec::with_capacity(poses.len());
let mut points3d_text =
String::from("# POINT3D_ID X Y Z R G B ERROR TRACK[] as IMAGE_ID POINT2D_IDX\n");
for (frame_idx, (pose, features)) in poses.iter().zip(stereo_per_frame.iter()).enumerate() {
let cam_to_world = pose.camera_to_world();
let mut per_frame: Vec<(usize, u64)> = Vec::with_capacity(features.len());
for feature in features {
landmark_count += 1;
let landmark_id = landmark_count;
let world_point = cam_to_world.transform_point(&feature.point_cam);
points3d_text.push_str(&format!(
"{} {} {} {} 255 255 255 0 {} {}\n",
landmark_id,
format_f64(world_point.x),
format_f64(world_point.y),
format_f64(world_point.z),
frame_idx,
feature.left_index,
));
observation_count += 1;
per_frame.push((feature.left_index, landmark_id));
}
frame_landmarks.push(per_frame);
}
fs::write(out_dir.join("points3D.txt"), points3d_text)?;
let mut images_text = String::from(
"# IMAGE_ID QW QX QY QZ TX TY TZ CAMERA_ID NAME\n# POINTS2D[] as X Y POINT3D_ID\n",
);
for (frame_idx, (pose, features)) in poses.iter().zip(left_features.iter()).enumerate() {
let q = pose.world_to_camera.rotation.quaternion();
let t = pose.world_to_camera.translation;
let name = image_name(frame_idx);
validate_colmap_image_name(&name, frame_idx)?;
images_text.push_str(&format!(
"{} {} {} {} {} {} {} {} {} {}\n",
frame_idx,
format_f64(q.w),
format_f64(q.i),
format_f64(q.j),
format_f64(q.k),
format_f64(t.x),
format_f64(t.y),
format_f64(t.z),
camera.id,
name,
));
let mut by_kp: Vec<(usize, u64)> = frame_landmarks[frame_idx].clone();
by_kp.sort_by_key(|(kp, _)| *kp);
let mut tokens: Vec<String> = Vec::with_capacity(features.len());
for (kp_idx, kp) in features.keypoints.iter().enumerate() {
let landmark_id = by_kp
.iter()
.find(|(observed, _)| *observed == kp_idx)
.map_or_else(
|| "-1".to_owned(),
|(_, landmark_id)| landmark_id.to_string(),
);
tokens.push(format!(
"{} {} {}",
format_f64(kp.x),
format_f64(kp.y),
landmark_id
));
}
images_text.push_str(&tokens.join(" "));
images_text.push('\n');
}
fs::write(out_dir.join("images.txt"), images_text)?;
Ok(ColmapExportSummary {
frame_count: poses.len(),
landmark_count: landmark_count as usize,
observation_count,
})
}
pub fn write_colmap_binary_model_for_3dgs<F>(
out_dir: impl AsRef<Path>,
camera: &Camera,
poses: &[Pose],
left_features: &[FeatureSet],
stereo_per_frame: &[Vec<StereoFeature>],
image_name: F,
) -> Result<ColmapExportSummary, ColmapError>
where
F: Fn(usize) -> String,
{
if poses.len() != left_features.len() || poses.len() != stereo_per_frame.len() {
return Err(ColmapError::InvalidExportInput(format!(
"input length mismatch: poses={}, left_features={}, stereo_per_frame={}",
poses.len(),
left_features.len(),
stereo_per_frame.len(),
)));
}
let out_dir = out_dir.as_ref();
fs::create_dir_all(out_dir)?;
let (colmap_model, colmap_params) = validated_colmap_camera_record(camera)?;
let model_id = colmap_id_from_camera_model(&colmap_model)?;
let camera_id_u32 = u32::try_from(camera.id).map_err(|_| {
ColmapError::InvalidExportInput(format!(
"camera id {} does not fit in u32 (COLMAP binary cameras use u32 ids)",
camera.id
))
})?;
let mut cameras_bytes: Vec<u8> = Vec::new();
cameras_bytes.extend_from_slice(&1u64.to_le_bytes());
cameras_bytes.extend_from_slice(&camera_id_u32.to_le_bytes());
cameras_bytes.extend_from_slice(&model_id.to_le_bytes());
cameras_bytes.extend_from_slice(&(camera.width as u64).to_le_bytes());
cameras_bytes.extend_from_slice(&(camera.height as u64).to_le_bytes());
for param in &colmap_params {
cameras_bytes.extend_from_slice(¶m.to_le_bytes());
}
fs::write(out_dir.join("cameras.bin"), cameras_bytes)?;
let mut landmark_count: u64 = 0;
let mut observation_count: usize = 0;
let mut frame_landmarks: Vec<Vec<(usize, u64)>> = Vec::with_capacity(poses.len());
let mut landmark_records: Vec<(u64, Point3<f64>, u32, u32)> = Vec::new();
for (frame_idx, (pose, features)) in poses.iter().zip(stereo_per_frame.iter()).enumerate() {
let image_id_u32 = u32::try_from(frame_idx).map_err(|_| {
ColmapError::InvalidExportInput(format!(
"frame index {frame_idx} does not fit in u32 (COLMAP binary uses u32 image ids)"
))
})?;
let cam_to_world = pose.camera_to_world();
let mut per_frame: Vec<(usize, u64)> = Vec::with_capacity(features.len());
for feature in features {
landmark_count += 1;
let landmark_id = landmark_count;
let world_point = cam_to_world.transform_point(&feature.point_cam);
let kp_index_u32 = u32::try_from(feature.left_index).map_err(|_| {
ColmapError::InvalidExportInput(format!(
"left keypoint index {} does not fit in u32",
feature.left_index
))
})?;
landmark_records.push((landmark_id, world_point, image_id_u32, kp_index_u32));
observation_count += 1;
per_frame.push((feature.left_index, landmark_id));
}
frame_landmarks.push(per_frame);
}
let mut points_bytes: Vec<u8> = Vec::new();
points_bytes.extend_from_slice(&(landmark_records.len() as u64).to_le_bytes());
for (landmark_id, position, image_id_u32, kp_index_u32) in &landmark_records {
points_bytes.extend_from_slice(&landmark_id.to_le_bytes());
points_bytes.extend_from_slice(&position.x.to_le_bytes());
points_bytes.extend_from_slice(&position.y.to_le_bytes());
points_bytes.extend_from_slice(&position.z.to_le_bytes());
points_bytes.extend_from_slice(&[255u8, 255u8, 255u8]); points_bytes.extend_from_slice(&0.0f64.to_le_bytes()); points_bytes.extend_from_slice(&1u64.to_le_bytes()); points_bytes.extend_from_slice(&image_id_u32.to_le_bytes());
points_bytes.extend_from_slice(&kp_index_u32.to_le_bytes());
}
fs::write(out_dir.join("points3D.bin"), points_bytes)?;
let mut images_bytes: Vec<u8> = Vec::new();
images_bytes.extend_from_slice(&(poses.len() as u64).to_le_bytes());
for (frame_idx, (pose, features)) in poses.iter().zip(left_features.iter()).enumerate() {
let image_id_u32 = u32::try_from(frame_idx).map_err(|_| {
ColmapError::InvalidExportInput(format!(
"frame index {frame_idx} does not fit in u32 (COLMAP binary uses u32 image ids)"
))
})?;
images_bytes.extend_from_slice(&image_id_u32.to_le_bytes());
let q = pose.world_to_camera.rotation.quaternion();
let t = pose.world_to_camera.translation;
for v in [q.w, q.i, q.j, q.k, t.x, t.y, t.z] {
images_bytes.extend_from_slice(&v.to_le_bytes());
}
images_bytes.extend_from_slice(&camera_id_u32.to_le_bytes());
let name = image_name(frame_idx);
validate_colmap_image_name(&name, frame_idx)?;
images_bytes.extend_from_slice(name.as_bytes());
images_bytes.push(0u8);
let by_kp = &frame_landmarks[frame_idx];
images_bytes.extend_from_slice(&(features.keypoints.len() as u64).to_le_bytes());
for (kp_idx, kp) in features.keypoints.iter().enumerate() {
images_bytes.extend_from_slice(&kp.x.to_le_bytes());
images_bytes.extend_from_slice(&kp.y.to_le_bytes());
let landmark_id = by_kp
.iter()
.find(|(observed, _)| *observed == kp_idx)
.map_or(-1, |(_, lid)| *lid as i64);
images_bytes.extend_from_slice(&landmark_id.to_le_bytes());
}
}
fs::write(out_dir.join("images.bin"), images_bytes)?;
Ok(ColmapExportSummary {
frame_count: poses.len(),
landmark_count: landmark_count as usize,
observation_count,
})
}
pub fn write_colmap_reconstruction_for_3dgs<F>(
out_dir: impl AsRef<Path>,
camera: &Camera,
poses: &[Pose],
left_features: &[FeatureSet],
landmarks: &[ReconstructionLandmark],
image_name: F,
) -> Result<ColmapExportSummary, ColmapError>
where
F: Fn(usize) -> String,
{
if poses.len() != left_features.len() {
return Err(ColmapError::InvalidExportInput(format!(
"input length mismatch: poses={}, left_features={}",
poses.len(),
left_features.len(),
)));
}
let (colmap_model, colmap_params) = validated_colmap_camera_record(camera)?;
let out_dir = out_dir.as_ref();
fs::create_dir_all(out_dir)?;
let mut cameras_text = String::from("# CAMERA_ID MODEL WIDTH HEIGHT PARAMS[]\n");
cameras_text.push_str(&format_camera_line(camera, &colmap_model, &colmap_params));
fs::write(out_dir.join("cameras.txt"), cameras_text)?;
let mut frame_kp_to_point: Vec<std::collections::HashMap<usize, u64>> =
vec![std::collections::HashMap::new(); poses.len()];
let mut observation_count: usize = 0;
let mut points3d_text =
String::from("# POINT3D_ID X Y Z R G B ERROR TRACK[] as IMAGE_ID POINT2D_IDX\n");
for (idx, (position, observations)) in landmarks.iter().enumerate() {
let point_id = (idx as u64) + 1;
points3d_text.push_str(&format!(
"{} {} {} {} 255 255 255 0",
point_id,
format_f64(position.x),
format_f64(position.y),
format_f64(position.z),
));
for &(frame, left_idx, _) in observations {
if frame >= poses.len() {
continue;
}
points3d_text.push_str(&format!(" {} {}", frame, left_idx));
frame_kp_to_point[frame].insert(left_idx, point_id);
observation_count += 1;
}
points3d_text.push('\n');
}
fs::write(out_dir.join("points3D.txt"), points3d_text)?;
let mut images_text = String::from(
"# IMAGE_ID QW QX QY QZ TX TY TZ CAMERA_ID NAME\n# POINTS2D[] as X Y POINT3D_ID\n",
);
for (frame_idx, (pose, features)) in poses.iter().zip(left_features.iter()).enumerate() {
let q = pose.world_to_camera.rotation.quaternion();
let t = pose.world_to_camera.translation;
let name = image_name(frame_idx);
validate_colmap_image_name(&name, frame_idx)?;
images_text.push_str(&format!(
"{} {} {} {} {} {} {} {} {} {}\n",
frame_idx,
format_f64(q.w),
format_f64(q.i),
format_f64(q.j),
format_f64(q.k),
format_f64(t.x),
format_f64(t.y),
format_f64(t.z),
camera.id,
name,
));
let kp_to_point = &frame_kp_to_point[frame_idx];
let mut tokens: Vec<String> = Vec::with_capacity(features.keypoints.len());
for (kp_idx, kp) in features.keypoints.iter().enumerate() {
let point_id = kp_to_point
.get(&kp_idx)
.map_or_else(|| "-1".to_owned(), |id| id.to_string());
tokens.push(format!(
"{} {} {}",
format_f64(kp.x),
format_f64(kp.y),
point_id
));
}
images_text.push_str(&tokens.join(" "));
images_text.push('\n');
}
fs::write(out_dir.join("images.txt"), images_text)?;
Ok(ColmapExportSummary {
frame_count: poses.len(),
landmark_count: landmarks.len(),
observation_count,
})
}
pub fn write_colmap_reconstruction_for_3dgs_with_cameras<F>(
out_dir: impl AsRef<Path>,
cameras: &[Camera],
poses: &[Pose],
left_features: &[FeatureSet],
landmarks: &[ReconstructionLandmark],
image_name: F,
) -> Result<ColmapExportSummary, ColmapError>
where
F: Fn(usize) -> String,
{
if cameras.len() != poses.len() || poses.len() != left_features.len() {
return Err(ColmapError::InvalidExportInput(format!(
"input length mismatch: cameras={}, poses={}, left_features={}",
cameras.len(),
poses.len(),
left_features.len(),
)));
}
let mut unique_cameras = BTreeMap::<u64, Camera>::new();
for camera in cameras {
validated_colmap_camera_record(camera)?;
if camera.width == 0 || camera.height == 0 || camera.params.iter().any(|p| !p.is_finite()) {
return Err(ColmapError::InvalidExportInput(format!(
"camera {} has invalid dimensions or non-finite parameters",
camera.id
)));
}
if let Some(previous) = unique_cameras.insert(camera.id, camera.clone()) {
if previous != *camera {
return Err(ColmapError::InvalidExportInput(format!(
"camera id {} is assigned incompatible definitions",
camera.id
)));
}
}
}
let out_dir = out_dir.as_ref();
fs::create_dir_all(out_dir)?;
let mut cameras_text = String::from("# CAMERA_ID MODEL WIDTH HEIGHT PARAMS[]\n");
for camera in unique_cameras.values() {
let (model, params) = colmap_camera_record(camera);
cameras_text.push_str(&format_camera_line(camera, &model, ¶ms));
}
fs::write(out_dir.join("cameras.txt"), cameras_text)?;
let mut frame_kp_to_point: Vec<Vec<u64>> = left_features
.iter()
.map(|features| vec![0; features.keypoints.len()])
.collect();
let mut observation_count = 0usize;
for (landmark_index, (_, observations)) in landmarks.iter().enumerate() {
let point_id = landmark_index as u64 + 1;
for &(frame, keypoint, _) in observations {
if frame >= poses.len() || keypoint >= left_features[frame].keypoints.len() {
return Err(ColmapError::InvalidExportInput(format!(
"landmark {} references invalid observation ({}, {})",
point_id, frame, keypoint
)));
}
let slot = &mut frame_kp_to_point[frame][keypoint];
if *slot != 0 {
return Err(ColmapError::InvalidExportInput(format!(
"keypoint ({}, {}) belongs to multiple exported landmarks",
frame, keypoint
)));
}
*slot = point_id;
observation_count += 1;
}
}
let names: Vec<String> = (0..poses.len()).map(&image_name).collect();
for (frame, name) in names.iter().enumerate() {
validate_colmap_image_name(name, frame)?;
}
let mut points3d_text =
String::from("# POINT3D_ID X Y Z R G B ERROR TRACK[] as IMAGE_ID POINT2D_IDX\n");
points3d_text.push_str(&format_in_chunks(landmarks.len(), |range, out| {
for landmark_index in range {
let (position, observations) = &landmarks[landmark_index];
push_display(out, landmark_index as u64 + 1);
for v in [position.x, position.y, position.z] {
out.push(' ');
push_f64(out, v);
}
out.push_str(" 255 255 255 0");
for &(frame, keypoint, _) in observations {
out.push(' ');
push_display(out, frame);
out.push(' ');
push_display(out, keypoint);
}
out.push('\n');
}
}));
fs::write(out_dir.join("points3D.txt"), points3d_text)?;
let mut images_text = String::from(
"# IMAGE_ID QW QX QY QZ TX TY TZ CAMERA_ID NAME\n# POINTS2D[] as X Y POINT3D_ID\n",
);
images_text.push_str(&format_in_chunks(poses.len(), |range, out| {
for frame in range {
let q = poses[frame].world_to_camera.rotation.quaternion();
let t = poses[frame].world_to_camera.translation;
push_display(out, frame);
for v in [q.w, q.i, q.j, q.k, t.x, t.y, t.z] {
out.push(' ');
push_f64(out, v);
}
out.push(' ');
push_display(out, cameras[frame].id);
out.push(' ');
out.push_str(&names[frame]);
out.push('\n');
let point_ids = &frame_kp_to_point[frame];
for (keypoint, pixel) in left_features[frame].keypoints.iter().enumerate() {
if keypoint > 0 {
out.push(' ');
}
push_f64(out, pixel.x);
out.push(' ');
push_f64(out, pixel.y);
out.push(' ');
match point_ids[keypoint] {
0 => out.push_str("-1"),
id => push_display(out, id),
}
}
out.push('\n');
}
}));
fs::write(out_dir.join("images.txt"), images_text)?;
Ok(ColmapExportSummary {
frame_count: poses.len(),
landmark_count: landmarks.len(),
observation_count,
})
}
pub fn format_cameras_txt(map: &VisualMap) -> String {
let mut output = String::from("# CAMERA_ID MODEL WIDTH HEIGHT PARAMS[]\n");
let mut cameras = map.cameras.values().collect::<Vec<_>>();
cameras.sort_by_key(|camera| camera.id);
for camera in cameras {
let (model, params) = colmap_camera_record(camera);
output.push_str(&format_camera_line(camera, &model, ¶ms));
}
output
}
pub fn format_images_txt(map: &VisualMap) -> String {
let mut output = String::from(
"# IMAGE_ID QW QX QY QZ TX TY TZ CAMERA_ID NAME\n# POINTS2D[] as X Y POINT3D_ID\n",
);
let mut keyframes = map.keyframes.values().collect::<Vec<_>>();
keyframes.sort_by_key(|keyframe| keyframe.frame.id);
for keyframe in keyframes {
let pose = keyframe.frame.pose.clone().unwrap_or_default();
let q = pose.world_to_camera.rotation.quaternion();
let t = pose.world_to_camera.translation;
output.push_str(&format!(
"{} {} {} {} {} {} {} {} {} image_{}.jpg\n",
keyframe.frame.id,
format_f64(q.w),
format_f64(q.i),
format_f64(q.j),
format_f64(q.k),
format_f64(t.x),
format_f64(t.y),
format_f64(t.z),
keyframe.frame.camera_id,
keyframe.frame.id,
));
let mut observation_by_keypoint = keyframe
.observations
.iter()
.map(|observation| (observation.keypoint_index, observation.landmark_id))
.collect::<Vec<_>>();
observation_by_keypoint.sort_by_key(|(keypoint_index, _)| *keypoint_index);
let point_tokens = keyframe
.frame
.keypoints
.iter()
.enumerate()
.map(|(keypoint_index, xy)| {
let landmark_id = observation_by_keypoint
.iter()
.find(|(observed_index, _)| *observed_index == keypoint_index)
.map_or_else(
|| "-1".to_owned(),
|(_, landmark_id)| landmark_id.to_string(),
);
format!("{} {} {}", format_f64(xy.x), format_f64(xy.y), landmark_id)
})
.collect::<Vec<_>>();
output.push_str(&point_tokens.join(" "));
output.push('\n');
}
output
}
pub fn format_points3d_txt(map: &VisualMap) -> String {
let mut output =
String::from("# POINT3D_ID X Y Z R G B ERROR TRACK[] as IMAGE_ID POINT2D_IDX\n");
let mut landmarks = map.landmarks.values().collect::<Vec<_>>();
landmarks.sort_by_key(|landmark| landmark.id);
for landmark in landmarks {
output.push_str(&format!(
"{} {} {} {} 255 255 255 0",
landmark.id,
format_f64(landmark.position.x),
format_f64(landmark.position.y),
format_f64(landmark.position.z),
));
let mut observations = landmark.observations.iter().collect::<Vec<_>>();
observations.sort_by_key(|observation| (observation.frame_id, observation.keypoint_index));
for observation in observations {
output.push_str(&format!(
" {} {}",
observation.frame_id, observation.keypoint_index
));
}
output.push('\n');
}
output
}
pub fn parse_cameras_txt(contents: &str) -> Result<Vec<Camera>, ColmapError> {
let mut cameras = Vec::new();
for line in contents.lines().map(str::trim) {
if line.is_empty() || line.starts_with('#') {
continue;
}
let tokens = line.split_whitespace().collect::<Vec<_>>();
if tokens.len() < 5 {
return Err(ColmapError::InvalidLine {
file: "cameras.txt",
line: line.to_owned(),
});
}
let id = tokens[0].parse()?;
let model = CameraModel::from_colmap_name(tokens[1]);
let width = tokens[2].parse()?;
let height = tokens[3].parse()?;
let params = tokens[4..]
.iter()
.map(|value| value.parse::<f64>())
.collect::<Result<Vec<_>, _>>()?;
cameras.push(Camera {
id,
model,
width,
height,
params,
});
}
Ok(cameras)
}
pub fn parse_points3d_txt(contents: &str) -> Result<Vec<Landmark>, ColmapError> {
let mut landmarks = Vec::new();
for line in contents.lines().map(str::trim) {
if line.is_empty() || line.starts_with('#') {
continue;
}
let tokens = line.split_whitespace().collect::<Vec<_>>();
if tokens.len() < 8 {
return Err(ColmapError::InvalidLine {
file: "points3D.txt",
line: line.to_owned(),
});
}
landmarks.push(Landmark::new(
tokens[0].parse()?,
Point3::new(tokens[1].parse()?, tokens[2].parse()?, tokens[3].parse()?),
));
}
Ok(landmarks)
}
pub fn parse_images_txt(contents: &str) -> Result<Vec<Keyframe>, ColmapError> {
let mut keyframes = Vec::new();
let mut lines = contents
.lines()
.map(str::trim)
.filter(|line| !line.starts_with('#'));
while let Some(header) = lines.next() {
if header.is_empty() {
continue;
}
let tokens = header.split_whitespace().collect::<Vec<_>>();
if tokens.len() < 10 {
return Err(ColmapError::InvalidLine {
file: "images.txt",
line: header.to_owned(),
});
}
let frame_id = tokens[0].parse()?;
let qw = tokens[1].parse()?;
let qx = tokens[2].parse()?;
let qy = tokens[3].parse()?;
let qz = tokens[4].parse()?;
let tx = tokens[5].parse()?;
let ty = tokens[6].parse()?;
let tz = tokens[7].parse()?;
let camera_id = tokens[8].parse()?;
let points_line = lines.next().unwrap_or("");
let mut frame = Frame::new(frame_id, camera_id);
frame.pose = Some(Pose::from_world_to_camera(
UnitQuaternion::from_quaternion(Quaternion::new(qw, qx, qy, qz)),
Vector3::new(tx, ty, tz),
));
let point_tokens = points_line.split_whitespace().collect::<Vec<_>>();
if point_tokens.len() % 3 != 0 {
return Err(ColmapError::InvalidLine {
file: "images.txt",
line: points_line.to_owned(),
});
}
let mut observations = Vec::new();
for (keypoint_index, chunk) in point_tokens.chunks(3).enumerate() {
let xy = Point2::new(chunk[0].parse()?, chunk[1].parse()?);
frame.keypoints.push(xy);
let point_id = chunk[2].parse::<i64>()?;
if point_id >= 0 {
observations.push(Observation {
frame_id,
landmark_id: point_id as u64,
keypoint_index,
xy,
});
}
}
keyframes.push(Keyframe {
frame,
observations,
});
}
Ok(keyframes)
}
pub fn parse_cameras_bin(contents: &[u8]) -> Result<Vec<Camera>, ColmapError> {
const MIN_CAMERA_RECORD_BYTES: usize = 4 + 4 + 8 + 8 + (2 * 8);
let mut reader = BinaryReader::new("cameras.bin", contents);
let camera_count = reader.read_count_with_min_bytes("camera_count", MIN_CAMERA_RECORD_BYTES)?;
let mut cameras = Vec::new();
reader.try_reserve(&mut cameras, camera_count, "camera_count")?;
for _ in 0..camera_count {
let id = reader.read_u32()? as u64;
let model_id = reader.read_i32()?;
let width = u32::try_from(reader.read_u64()?).map_err(|_| ColmapError::InvalidBinary {
file: "cameras.bin",
message: "camera width does not fit in u32".to_owned(),
})?;
let height = u32::try_from(reader.read_u64()?).map_err(|_| ColmapError::InvalidBinary {
file: "cameras.bin",
message: "camera height does not fit in u32".to_owned(),
})?;
let (model, parameter_count) = camera_model_from_colmap_id(model_id)?;
let mut params = Vec::with_capacity(parameter_count);
for _ in 0..parameter_count {
params.push(reader.read_f64()?);
}
cameras.push(Camera {
id,
model,
width,
height,
params,
});
}
reader.finish()?;
Ok(cameras)
}
pub fn parse_images_bin(contents: &[u8]) -> Result<Vec<Keyframe>, ColmapError> {
Ok(parse_images_bin_records(contents)?
.into_iter()
.map(|(keyframe, _name)| keyframe)
.collect())
}
pub fn parse_images_bin_names(contents: &[u8]) -> Result<Vec<(u64, String)>, ColmapError> {
Ok(parse_images_bin_records(contents)?
.into_iter()
.map(|(keyframe, name)| (keyframe.frame.id, name))
.collect())
}
fn parse_images_bin_records(contents: &[u8]) -> Result<Vec<(Keyframe, String)>, ColmapError> {
const MIN_IMAGE_RECORD_BYTES: usize = 4 + (7 * 8) + 4 + 1 + 8;
const IMAGE_POINT2D_BYTES: usize = 3 * 8;
let mut reader = BinaryReader::new("images.bin", contents);
let image_count = reader.read_count_with_min_bytes("image_count", MIN_IMAGE_RECORD_BYTES)?;
let mut keyframes = Vec::new();
reader.try_reserve(&mut keyframes, image_count, "image_count")?;
for _ in 0..image_count {
let frame_id = reader.read_u32()? as u64;
let qw = reader.read_f64()?;
let qx = reader.read_f64()?;
let qy = reader.read_f64()?;
let qz = reader.read_f64()?;
let tx = reader.read_f64()?;
let ty = reader.read_f64()?;
let tz = reader.read_f64()?;
let camera_id = reader.read_u32()? as u64;
let name = reader.read_null_terminated_string()?;
let mut frame = Frame::new(frame_id, camera_id);
frame.pose = Some(Pose::from_world_to_camera(
UnitQuaternion::from_quaternion(Quaternion::new(qw, qx, qy, qz)),
Vector3::new(tx, ty, tz),
));
let point_count = reader.read_count_with_min_bytes("point_count", IMAGE_POINT2D_BYTES)?;
reader.try_reserve(&mut frame.keypoints, point_count, "point_count keypoints")?;
let mut observations = Vec::new();
reader.try_reserve(&mut observations, point_count, "point_count observations")?;
for keypoint_index in 0..point_count {
let xy = Point2::new(reader.read_f64()?, reader.read_f64()?);
frame.keypoints.push(xy);
let point_id = reader.read_i64()?;
if point_id >= 0 {
observations.push(Observation {
frame_id,
landmark_id: point_id as u64,
keypoint_index,
xy,
});
}
}
keyframes.push((
Keyframe {
frame,
observations,
},
name,
));
}
reader.finish()?;
Ok(keyframes)
}
pub fn parse_points3d_bin(contents: &[u8]) -> Result<Vec<Landmark>, ColmapError> {
Ok(parse_points3d_bin_with_colors(contents)?
.into_iter()
.map(|(landmark, _rgb)| landmark)
.collect())
}
pub fn parse_points3d_bin_with_colors(
contents: &[u8],
) -> Result<Vec<(Landmark, [u8; 3])>, ColmapError> {
const MIN_POINT3D_RECORD_BYTES: usize = 8 + (3 * 8) + 3 + 8 + 8;
const POINT3D_TRACK_ENTRY_BYTES: usize = 2 * 4;
let mut reader = BinaryReader::new("points3D.bin", contents);
let point_count =
reader.read_count_with_min_bytes("point3D_count", MIN_POINT3D_RECORD_BYTES)?;
let mut landmarks = Vec::new();
reader.try_reserve(&mut landmarks, point_count, "point3D_count")?;
for _ in 0..point_count {
let id = reader.read_u64()?;
let position = Point3::new(reader.read_f64()?, reader.read_f64()?, reader.read_f64()?);
let rgb = reader.read_array::<3>()?;
let _error = reader.read_f64()?;
let track_length =
reader.read_count_with_min_bytes("track_length", POINT3D_TRACK_ENTRY_BYTES)?;
for _ in 0..track_length {
let _image_id = reader.read_u32()?;
let _point2d_index = reader.read_u32()?;
}
landmarks.push((Landmark::new(id, position), rgb));
}
reader.finish()?;
Ok(landmarks)
}
fn colmap_param_count(model: &CameraModel) -> Option<usize> {
model.colmap_param_count()
}
pub fn colmap_camera_record(camera: &Camera) -> (CameraModel, Vec<f64>) {
let params = &camera.params;
match camera.model {
CameraModel::Pinhole if params.len() > 4 && params[4..].iter().all(|&k| k == 0.0) => {
(CameraModel::Pinhole, params[..4].to_vec())
}
CameraModel::Pinhole | CameraModel::OpenCv if params.len() > 4 && params.len() < 8 => {
let mut opencv = params.clone();
opencv.resize(8, 0.0);
(CameraModel::OpenCv, opencv)
}
_ => (camera.model.clone(), params.clone()),
}
}
fn validated_colmap_camera_record(camera: &Camera) -> Result<(CameraModel, Vec<f64>), ColmapError> {
let (model, params) = colmap_camera_record(camera);
colmap_id_from_camera_model(&model)?;
let expected = colmap_param_count(&model).ok_or_else(|| {
ColmapError::InvalidExportInput(format!(
"camera {} model {} is not a COLMAP camera model",
camera.id,
camera_model_to_colmap_name(&model)
))
})?;
if params.len() != expected {
return Err(ColmapError::InvalidExportInput(format!(
"camera {} model {} needs {expected} params for COLMAP, got {}",
camera.id,
camera_model_to_colmap_name(&model),
params.len()
)));
}
Ok((model, params))
}
fn format_camera_line(camera: &Camera, model: &CameraModel, params: &[f64]) -> String {
let mut line = format!(
"{} {} {} {}",
camera.id,
camera_model_to_colmap_name(model),
camera.width,
camera.height,
);
for param in params {
line.push_str(&format!(" {}", format_f64(*param)));
}
line.push('\n');
line
}
fn camera_model_to_colmap_name(model: &CameraModel) -> &str {
match model {
CameraModel::Pinhole => "PINHOLE",
CameraModel::SimplePinhole => "SIMPLE_PINHOLE",
CameraModel::SimpleRadial => "SIMPLE_RADIAL",
CameraModel::Radial => "RADIAL",
CameraModel::OpenCv => "OPENCV",
CameraModel::FullOpenCv => "FULL_OPENCV",
CameraModel::OpenCvFisheye => "OPENCV_FISHEYE",
CameraModel::SimpleRadialFisheye => "SIMPLE_RADIAL_FISHEYE",
CameraModel::RadialFisheye => "RADIAL_FISHEYE",
CameraModel::ThinPrismFisheye => "THIN_PRISM_FISHEYE",
CameraModel::Fov => "FOV",
CameraModel::Equirectangular => "EQUIRECTANGULAR",
CameraModel::DoubleSphere => "DOUBLE_SPHERE",
CameraModel::Unknown(name) => name.as_str(),
}
}
fn format_f64(value: f64) -> String {
let formatted = format!("{value:.12}");
formatted
.trim_end_matches('0')
.trim_end_matches('.')
.to_owned()
}
fn push_f64(out: &mut String, value: f64) {
use std::fmt::Write;
let start = out.len();
write!(out, "{value:.12}").expect("writing to a String cannot fail");
let trimmed = out[start..]
.trim_end_matches('0')
.trim_end_matches('.')
.len();
out.truncate(start + trimmed);
}
fn push_display(out: &mut String, value: impl std::fmt::Display) {
use std::fmt::Write;
write!(out, "{value}").expect("writing to a String cannot fail");
}
fn format_in_chunks(
count: usize,
format: impl Fn(std::ops::Range<usize>, &mut String) + Sync,
) -> String {
let threads = std::thread::available_parallelism()
.map_or(1, |n| n.get())
.min(count.div_ceil(256).max(1));
if threads <= 1 {
let mut out = String::new();
format(0..count, &mut out);
return out;
}
let format = &format;
let pieces: Vec<String> = std::thread::scope(|scope| {
(0..threads)
.map(|k| {
let range = k * count / threads..(k + 1) * count / threads;
scope.spawn(move || {
let mut out = String::new();
format(range, &mut out);
out
})
})
.collect::<Vec<_>>()
.into_iter()
.map(|handle| handle.join().expect("formatting thread panicked"))
.collect()
});
let mut out = String::with_capacity(pieces.iter().map(String::len).sum());
for piece in pieces {
out.push_str(&piece);
}
out
}
fn validate_colmap_image_name(name: &str, frame_idx: usize) -> Result<(), ColmapError> {
if let Some(bad_idx) = name
.bytes()
.position(|b| matches!(b, 0 | b' ' | b'\t' | b'\n' | b'\r'))
{
return Err(ColmapError::InvalidExportInput(format!(
"image name for frame {frame_idx} contains an invalid character at byte {bad_idx} (NUL or ASCII whitespace would corrupt the COLMAP export); got {name:?}"
)));
}
Ok(())
}
fn colmap_id_from_camera_model(model: &CameraModel) -> Result<i32, ColmapError> {
Ok(match model {
CameraModel::SimplePinhole => 0,
CameraModel::Pinhole => 1,
CameraModel::SimpleRadial => 2,
CameraModel::Radial => 3,
CameraModel::OpenCv => 4,
CameraModel::FullOpenCv => 6,
CameraModel::OpenCvFisheye => 5,
CameraModel::Fov => 7,
CameraModel::SimpleRadialFisheye => 8,
CameraModel::RadialFisheye => 9,
CameraModel::ThinPrismFisheye => 10,
CameraModel::Equirectangular => 17,
CameraModel::DoubleSphere => {
return Err(ColmapError::InvalidExportInput(
"Double Sphere is not a COLMAP camera model".to_owned(),
));
}
CameraModel::Unknown(name) => match name.as_str() {
"OPENCV_FISHEYE" => 5,
"FULL_OPENCV" => 6,
"FOV" => 7,
"SIMPLE_RADIAL_FISHEYE" => 8,
"RADIAL_FISHEYE" => 9,
"THIN_PRISM_FISHEYE" => 10,
other => {
return Err(ColmapError::InvalidExportInput(format!(
"cannot encode CameraModel::Unknown({other:?}) as a COLMAP binary model id"
)));
}
},
})
}
fn camera_model_from_colmap_id(model_id: i32) -> Result<(CameraModel, usize), ColmapError> {
match model_id {
0 => Ok((CameraModel::SimplePinhole, 3)),
1 => Ok((CameraModel::Pinhole, 4)),
2 => Ok((CameraModel::SimpleRadial, 4)),
3 => Ok((CameraModel::Radial, 5)),
4 => Ok((CameraModel::OpenCv, 8)),
5 => Ok((CameraModel::OpenCvFisheye, 8)),
6 => Ok((CameraModel::FullOpenCv, 12)),
7 => Ok((CameraModel::Fov, 5)),
8 => Ok((CameraModel::SimpleRadialFisheye, 4)),
9 => Ok((CameraModel::RadialFisheye, 5)),
10 => Ok((CameraModel::ThinPrismFisheye, 12)),
17 => Ok((CameraModel::Equirectangular, 2)),
other => Err(ColmapError::InvalidBinary {
file: "cameras.bin",
message: format!("unsupported camera model id {other}"),
}),
}
}
#[cfg(test)]
#[allow(clippy::items_after_test_module)]
mod multi_camera_writer_tests {
use super::*;
#[test]
fn fisheye_camera_models_round_trip_through_colmap_ids() {
for (model, id, parameter_count) in [
(CameraModel::OpenCvFisheye, 5, 8),
(CameraModel::FullOpenCv, 6, 12),
(CameraModel::Fov, 7, 5),
(CameraModel::SimpleRadialFisheye, 8, 4),
(CameraModel::RadialFisheye, 9, 5),
(CameraModel::ThinPrismFisheye, 10, 12),
] {
assert_eq!(colmap_id_from_camera_model(&model).unwrap(), id);
let (decoded, count) = camera_model_from_colmap_id(id).unwrap();
assert_eq!(decoded, model);
assert_eq!(count, parameter_count);
assert_eq!(
camera_model_to_colmap_name(&model),
decoded.colmap_name().unwrap()
);
}
assert!(colmap_id_from_camera_model(&CameraModel::DoubleSphere).is_err());
}
#[test]
fn reconstruction_writer_preserves_per_image_camera_ids() {
let root =
std::env::temp_dir().join(format!("visloc_multi_camera_writer_{}", std::process::id()));
let _ = fs::remove_dir_all(&root);
let cameras = vec![
Camera::pinhole(7, 100, 80, 50.0, 50.0, 50.0, 40.0),
Camera::pinhole(9, 200, 160, 100.0, 80.0, 100.0, 80.0),
];
let poses = vec![Pose::default(), Pose::default()];
let features = vec![
FeatureSet::new(vec![Point2::new(50.0, 40.0)], vec![vec![1.0]]).unwrap(),
FeatureSet::new(vec![Point2::new(100.0, 80.0)], vec![vec![2.0]]).unwrap(),
];
let landmarks = vec![
(
Point3::new(0.0, 0.0, 5.0),
vec![(0usize, 0usize, Point2::new(50.0, 40.0))],
),
(
Point3::new(0.0, 0.0, 5.0),
vec![(1usize, 0usize, Point2::new(100.0, 80.0))],
),
];
let summary = write_colmap_reconstruction_for_3dgs_with_cameras(
&root,
&cameras,
&poses,
&features,
&landmarks,
|index| format!("image_{index}.png"),
)
.unwrap();
assert_eq!(summary.frame_count, 2);
assert_eq!(
parse_cameras_txt(&fs::read_to_string(root.join("cameras.txt")).unwrap())
.unwrap()
.iter()
.map(|camera| camera.id)
.collect::<Vec<_>>(),
vec![7, 9]
);
let headers = fs::read_to_string(root.join("images.txt"))
.unwrap()
.lines()
.filter(|line| !line.starts_with('#') && !line.trim().is_empty())
.step_by(2)
.map(|line| {
line.split_whitespace()
.nth(8)
.unwrap()
.parse::<u64>()
.unwrap()
})
.collect::<Vec<_>>();
assert_eq!(headers, vec![7, 9]);
let _ = fs::remove_dir_all(root);
}
}
struct BinaryReader<'a> {
file: &'static str,
contents: &'a [u8],
offset: usize,
}
impl<'a> BinaryReader<'a> {
const fn new(file: &'static str, contents: &'a [u8]) -> Self {
Self {
file,
contents,
offset: 0,
}
}
fn read_u32(&mut self) -> Result<u32, ColmapError> {
Ok(u32::from_le_bytes(self.read_array()?))
}
fn read_i32(&mut self) -> Result<i32, ColmapError> {
Ok(i32::from_le_bytes(self.read_array()?))
}
fn read_u64(&mut self) -> Result<u64, ColmapError> {
Ok(u64::from_le_bytes(self.read_array()?))
}
fn read_i64(&mut self) -> Result<i64, ColmapError> {
Ok(i64::from_le_bytes(self.read_array()?))
}
fn read_f64(&mut self) -> Result<f64, ColmapError> {
Ok(f64::from_le_bytes(self.read_array()?))
}
fn read_count_with_min_bytes(
&mut self,
field: &'static str,
min_record_bytes: usize,
) -> Result<usize, ColmapError> {
let raw = self.read_u64()?;
let count = usize::try_from(raw).map_err(|_| ColmapError::InvalidBinary {
file: self.file,
message: format!("{field}={raw} does not fit in usize"),
})?;
let required_bytes =
count
.checked_mul(min_record_bytes)
.ok_or_else(|| ColmapError::InvalidBinary {
file: self.file,
message: format!("{field}={count} overflows minimum record-byte calculation"),
})?;
let remaining_bytes = self.contents.len().saturating_sub(self.offset);
if required_bytes > remaining_bytes {
return Err(ColmapError::InvalidBinary {
file: self.file,
message: format!("{field}={count} requires at least {required_bytes} remaining byte(s), but only {remaining_bytes} remain"),
});
}
Ok(count)
}
fn try_reserve<T>(
&self,
values: &mut Vec<T>,
additional: usize,
field: &'static str,
) -> Result<(), ColmapError> {
values
.try_reserve(additional)
.map_err(|error| ColmapError::InvalidBinary {
file: self.file,
message: format!("{field}={additional} allocation failed: {error:?}"),
})
}
fn read_null_terminated_string(&mut self) -> Result<String, ColmapError> {
let start = self.offset;
while self.offset < self.contents.len() && self.contents[self.offset] != 0 {
self.offset += 1;
}
if self.offset >= self.contents.len() {
return Err(ColmapError::InvalidBinary {
file: self.file,
message: "unterminated string".to_owned(),
});
}
let name = std::str::from_utf8(&self.contents[start..self.offset])
.map_err(|error| ColmapError::InvalidBinary {
file: self.file,
message: format!("invalid UTF-8 string: {error}"),
})?
.to_owned();
self.offset += 1;
Ok(name)
}
fn finish(&self) -> Result<(), ColmapError> {
if self.offset == self.contents.len() {
Ok(())
} else {
Err(ColmapError::InvalidBinary {
file: self.file,
message: format!(
"{} trailing byte(s) after parsing",
self.contents.len() - self.offset
),
})
}
}
fn read_array<const N: usize>(&mut self) -> Result<[u8; N], ColmapError> {
let bytes = self.read_bytes(N)?;
let mut array = [0; N];
array.copy_from_slice(bytes);
Ok(array)
}
fn read_bytes(&mut self, byte_count: usize) -> Result<&'a [u8], ColmapError> {
let end =
self.offset
.checked_add(byte_count)
.ok_or_else(|| ColmapError::InvalidBinary {
file: self.file,
message: "reader offset overflow".to_owned(),
})?;
if end > self.contents.len() {
return Err(ColmapError::InvalidBinary {
file: self.file,
message: format!("unexpected end of file at byte {}", self.offset),
});
}
let bytes = &self.contents[self.offset..end];
self.offset = end;
Ok(bytes)
}
}