colmap 0.1.2

A comprehensive Rust library for COLMAP-style computer vision and 3D reconstruction
Documentation
//! Structure from Motion (SfM) 模块
//!
//! 这个模块实现了完整的 Structure from Motion 重建管道,用于从多视角图像中恢复 3D 场景结构和相机姿态。
//! 采用增量式重建策略,逐步添加图像并优化重建结果。
//!
//! # 主要功能
//!
//! - **增量式重建**:逐步添加图像进行 3D 重建
//! - **三角测量**:从多个视角恢复 3D 点坐标
//! - **束调整优化**:全局优化相机参数和 3D 点位置
//! - **姿态估计**:估计相机的位置和方向
//! - **轨迹管理**:管理特征点在多个图像中的对应关系
//!
//! # 使用示例
//!
//! ## 基本 SfM 重建
//!
//! ```rust,ignore
//! use colmap::sfm::*;
//! use colmap::core::*;
//!
//! // 创建 SfM 配置
//! let config = SfmConfig {
//!     min_track_length: 2,
//!     max_reprojection_error: 4.0,
//!     ..Default::default()
//! };
//!
//! // 创建增量式重建器
//! let mut reconstructor = IncrementalSfm::new(config);
//!
//! // 添加图像和特征匹配
//! for (image_id, image) in images.iter() {
//!     reconstructor.add_image(*image_id, image.clone())?;
//! }
//!
//! // 执行重建
//! let reconstruction = reconstructor.reconstruct()?;
//! println!("重建了 {} 个 3D 点", reconstruction.points.len());
//! # Ok::<(), ColmapError>(())
//! ```
//!
//! ## 三角测量示例
//!
//! ```rust,ignore
//! use colmap::sfm::*;
//! use colmap::core::*;
//!
//! // 配置三角测量参数
//! let config = TriangulationConfig {
//!     min_angle: 1.5, // 最小视角(度)
//!     max_reprojection_error: 4.0,
//!     min_depth: 0.1,
//!     max_depth: 1000.0,
//! };
//!
//! // 执行三角测量
//! let triangulator = Triangulator::new(config);
//! let point_3d = triangulator.triangulate(&observations, &cameras)?;
//!
//! println!("三角测量结果: {:?}", point_3d);
//! # Ok::<(), ColmapError>(())
//! ```
//!
//! ## 完整重建流程
//!
//! ```rust,ignore
//! use colmap::sfm::*;
//! use colmap::feature::*;
//! use colmap::core::*;
//!
//! // 1. 特征提取和匹配
//! let pipeline = FeaturePipeline::new(PipelineConfig::default());
//! let images = vec![]; // 示例图像列表
//! let matches = pipeline.extract_and_match(&images)?;
//!
//! // 2. SfM 重建
//! let sfm_config = SfmConfig::default();
//! let mut reconstructor = IncrementalReconstructor::new(sfm_config);
//!
//! // 3. 添加数据并重建
//! reconstructor.set_matches(matches);
//! let reconstruction = reconstructor.reconstruct()?;
//!
//! // 4. 输出结果
//! println!("重建统计:");
//! println!("  - 注册图像: {}", reconstruction.registered_images());
//! println!("  - 3D 点数量: {}", reconstruction.points.len());
//! println!("  - 平均重投影误差: {:.2}", reconstruction.mean_reprojection_error());
//! # Ok::<(), ColmapError>(())
//! ```

pub mod incremental;
pub mod triangulation;
// TODO: 实现这些模块
// pub mod bundle_adjustment;
// pub mod pose_estimation;
// pub mod reconstruction;

// 重新导出主要类型
pub use incremental::*;
pub use triangulation::*;
// pub use bundle_adjustment::*;
// pub use pose_estimation::*;
// pub use reconstruction::*;

use serde::{Deserialize, Serialize};

/// SfM 重建配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SfmConfig {
    /// 最小轨迹长度
    pub min_track_length: usize,
    /// 最大重投影误差
    pub max_reprojection_error: f64,
    /// 束调整配置
    pub bundle_adjustment: BundleAdjustmentConfig,
    /// 三角测量配置
    pub triangulation: TriangulationConfig,
}

impl Default for SfmConfig {
    fn default() -> Self {
        Self {
            min_track_length: 2,
            max_reprojection_error: 4.0,
            bundle_adjustment: BundleAdjustmentConfig::default(),
            triangulation: TriangulationConfig::default(),
        }
    }
}

/// 束调整配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BundleAdjustmentConfig {
    /// 最大迭代次数
    pub max_iterations: usize,
    /// 收敛阈值
    pub convergence_threshold: f64,
    /// 优化相机内参
    pub optimize_intrinsics: bool,
    /// 优化畸变参数
    pub optimize_distortion: bool,
}

impl Default for BundleAdjustmentConfig {
    fn default() -> Self {
        Self {
            max_iterations: 100,
            convergence_threshold: 1e-6,
            optimize_intrinsics: false,
            optimize_distortion: false,
        }
    }
}

/// 三角测量配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TriangulationConfig {
    /// 最小角度(弧度)
    pub min_angle: f64,
    /// 最大重投影误差
    pub max_reprojection_error: f64,
    /// 最小深度
    pub min_depth: f64,
    /// 最大深度
    pub max_depth: f64,
}

impl Default for TriangulationConfig {
    fn default() -> Self {
        Self {
            min_angle: 1.5_f64.to_radians(), // 1.5 度
            max_reprojection_error: 4.0,
            min_depth: 0.1,
            max_depth: 1000.0,
        }
    }
}