colmap/sfm/mod.rs
1//! Structure from Motion (SfM) 模块
2//!
3//! 这个模块实现了完整的 Structure from Motion 重建管道,用于从多视角图像中恢复 3D 场景结构和相机姿态。
4//! 采用增量式重建策略,逐步添加图像并优化重建结果。
5//!
6//! # 主要功能
7//!
8//! - **增量式重建**:逐步添加图像进行 3D 重建
9//! - **三角测量**:从多个视角恢复 3D 点坐标
10//! - **束调整优化**:全局优化相机参数和 3D 点位置
11//! - **姿态估计**:估计相机的位置和方向
12//! - **轨迹管理**:管理特征点在多个图像中的对应关系
13//!
14//! # 使用示例
15//!
16//! ## 基本 SfM 重建
17//!
18//! ```rust,ignore
19//! use colmap::sfm::*;
20//! use colmap::core::*;
21//!
22//! // 创建 SfM 配置
23//! let config = SfmConfig {
24//! min_track_length: 2,
25//! max_reprojection_error: 4.0,
26//! ..Default::default()
27//! };
28//!
29//! // 创建增量式重建器
30//! let mut reconstructor = IncrementalSfm::new(config);
31//!
32//! // 添加图像和特征匹配
33//! for (image_id, image) in images.iter() {
34//! reconstructor.add_image(*image_id, image.clone())?;
35//! }
36//!
37//! // 执行重建
38//! let reconstruction = reconstructor.reconstruct()?;
39//! println!("重建了 {} 个 3D 点", reconstruction.points.len());
40//! # Ok::<(), ColmapError>(())
41//! ```
42//!
43//! ## 三角测量示例
44//!
45//! ```rust,ignore
46//! use colmap::sfm::*;
47//! use colmap::core::*;
48//!
49//! // 配置三角测量参数
50//! let config = TriangulationConfig {
51//! min_angle: 1.5, // 最小视角(度)
52//! max_reprojection_error: 4.0,
53//! min_depth: 0.1,
54//! max_depth: 1000.0,
55//! };
56//!
57//! // 执行三角测量
58//! let triangulator = Triangulator::new(config);
59//! let point_3d = triangulator.triangulate(&observations, &cameras)?;
60//!
61//! println!("三角测量结果: {:?}", point_3d);
62//! # Ok::<(), ColmapError>(())
63//! ```
64//!
65//! ## 完整重建流程
66//!
67//! ```rust,ignore
68//! use colmap::sfm::*;
69//! use colmap::feature::*;
70//! use colmap::core::*;
71//!
72//! // 1. 特征提取和匹配
73//! let pipeline = FeaturePipeline::new(PipelineConfig::default());
74//! let images = vec![]; // 示例图像列表
75//! let matches = pipeline.extract_and_match(&images)?;
76//!
77//! // 2. SfM 重建
78//! let sfm_config = SfmConfig::default();
79//! let mut reconstructor = IncrementalReconstructor::new(sfm_config);
80//!
81//! // 3. 添加数据并重建
82//! reconstructor.set_matches(matches);
83//! let reconstruction = reconstructor.reconstruct()?;
84//!
85//! // 4. 输出结果
86//! println!("重建统计:");
87//! println!(" - 注册图像: {}", reconstruction.registered_images());
88//! println!(" - 3D 点数量: {}", reconstruction.points.len());
89//! println!(" - 平均重投影误差: {:.2}", reconstruction.mean_reprojection_error());
90//! # Ok::<(), ColmapError>(())
91//! ```
92
93pub mod incremental;
94pub mod triangulation;
95// TODO: 实现这些模块
96// pub mod bundle_adjustment;
97// pub mod pose_estimation;
98// pub mod reconstruction;
99
100// 重新导出主要类型
101pub use incremental::*;
102pub use triangulation::*;
103// pub use bundle_adjustment::*;
104// pub use pose_estimation::*;
105// pub use reconstruction::*;
106
107use serde::{Deserialize, Serialize};
108
109/// SfM 重建配置
110#[derive(Debug, Clone, Serialize, Deserialize)]
111pub struct SfmConfig {
112 /// 最小轨迹长度
113 pub min_track_length: usize,
114 /// 最大重投影误差
115 pub max_reprojection_error: f64,
116 /// 束调整配置
117 pub bundle_adjustment: BundleAdjustmentConfig,
118 /// 三角测量配置
119 pub triangulation: TriangulationConfig,
120}
121
122impl Default for SfmConfig {
123 fn default() -> Self {
124 Self {
125 min_track_length: 2,
126 max_reprojection_error: 4.0,
127 bundle_adjustment: BundleAdjustmentConfig::default(),
128 triangulation: TriangulationConfig::default(),
129 }
130 }
131}
132
133/// 束调整配置
134#[derive(Debug, Clone, Serialize, Deserialize)]
135pub struct BundleAdjustmentConfig {
136 /// 最大迭代次数
137 pub max_iterations: usize,
138 /// 收敛阈值
139 pub convergence_threshold: f64,
140 /// 优化相机内参
141 pub optimize_intrinsics: bool,
142 /// 优化畸变参数
143 pub optimize_distortion: bool,
144}
145
146impl Default for BundleAdjustmentConfig {
147 fn default() -> Self {
148 Self {
149 max_iterations: 100,
150 convergence_threshold: 1e-6,
151 optimize_intrinsics: false,
152 optimize_distortion: false,
153 }
154 }
155}
156
157/// 三角测量配置
158#[derive(Debug, Clone, Serialize, Deserialize)]
159pub struct TriangulationConfig {
160 /// 最小角度(弧度)
161 pub min_angle: f64,
162 /// 最大重投影误差
163 pub max_reprojection_error: f64,
164 /// 最小深度
165 pub min_depth: f64,
166 /// 最大深度
167 pub max_depth: f64,
168}
169
170impl Default for TriangulationConfig {
171 fn default() -> Self {
172 Self {
173 min_angle: 1.5_f64.to_radians(), // 1.5 度
174 max_reprojection_error: 4.0,
175 min_depth: 0.1,
176 max_depth: 1000.0,
177 }
178 }
179}