cumulus3d_cli/
densewrap.rs1use cumulus3d_core::{ImageId, Reconstruction};
32use cumulus3d_dense::{
33 undistort_with, DenseOutput, DenseScene, DensifyOptions, DepthMapCache, ImageBuffer, MvsProfile, PatchMatchBackend, SceneOptions,
34 UndistortBackend, UndistortCache, UndistortOptions,
35};
36use std::collections::BTreeMap;
37use std::path::{Path, PathBuf};
38use std::sync::{Arc, Mutex};
39use std::time::{Duration, Instant};
40
41pub fn make_pm_backend(name: &str) -> Result<Arc<dyn PatchMatchBackend>, String> {
43 match name {
44 "cuda" => cumulus3d_cuda::CudaPatchMatch::try_default()
45 .map(|b| Arc::new(b) as Arc<dyn PatchMatchBackend>)
46 .map_err(|e| format!("--pm-backend cuda: {e} (조밀화에는 CUDA 장치가 필요)")),
47 other => Err(format!("알 수 없는 --pm-backend {other} (cuda)")),
48 }
49}
50
51pub fn make_undistort_backend(name: &str) -> Result<Option<Arc<dyn UndistortBackend>>, String> {
53 match name {
54 "cpu" => Ok(None),
55 "cuda" => cumulus3d_cuda::CudaUndistort::try_default()
56 .map(|b| Some(Arc::new(b) as Arc<dyn UndistortBackend>))
57 .map_err(|e| format!("--undistort-backend cuda: {e}")),
58 other => Err(format!("알 수 없는 --undistort-backend {other} (cpu|cuda)")),
59 }
60}
61
62pub fn parse_profile(s: &str) -> Result<MvsProfile, String> {
64 MvsProfile::parse(s).ok_or_else(|| format!("알 수 없는 --mvs-profile {s} (fast|quality)"))
65}
66
67pub fn make_sift_backend(name: &str) -> Result<Arc<dyn cumulus3d_features::SiftEngine>, String> {
69 match name {
70 "cpu" => Ok(Arc::new(cumulus3d_features::CpuSift::default())),
71 "cuda" => cumulus3d_cuda::CudaSift::try_default()
72 .map(|b| Arc::new(b) as Arc<dyn cumulus3d_features::SiftEngine>)
73 .map_err(|e| format!("--sift-backend cuda: {e}")),
74 other => Err(format!("알 수 없는 --sift-backend {other} (cpu|cuda)")),
75 }
76}
77
78pub fn make_match_backend(name: &str) -> Result<Box<dyn cumulus3d_matching::MatcherBackend>, String> {
80 match name {
81 "cpu" => Ok(Box::new(cumulus3d_matching::CpuMatcher::default())),
82 "cuda" => cumulus3d_cuda::CudaMatcher::try_default()
83 .map(|b| Box::new(b) as Box<dyn cumulus3d_matching::MatcherBackend>)
84 .map_err(|e| format!("--match-backend cuda: {e}")),
85 other => Err(format!("알 수 없는 --match-backend {other} (cpu|cuda)")),
86 }
87}
88
89#[derive(Clone)]
91pub struct DenseConfig {
92 pub undistort: UndistortOptions,
94 pub scene: SceneOptions,
96 pub densify: DensifyOptions,
98 pub score: Option<cumulus3d_dense::fusion_score::ScoreFusionOptions>,
100 pub backend: Result<Arc<dyn PatchMatchBackend>, String>,
102 pub lock: Option<Arc<Mutex<()>>>,
104 pub depth_cache: Option<Arc<DepthMapCache>>,
106 pub undistort_cache: Arc<UndistortCache>,
108 pub undistort_backend: Option<Arc<dyn UndistortBackend>>,
110}
111
112impl DenseConfig {
113 pub fn new(backend: Result<Arc<dyn PatchMatchBackend>, String>, profile: MvsProfile) -> Self {
115 Self {
116 undistort: UndistortOptions::pipeline(),
117 scene: SceneOptions::default(),
118 densify: DensifyOptions::with_profile(profile),
119 score: None,
120 backend,
121 lock: None,
122 depth_cache: None,
123 undistort_cache: Arc::new(UndistortCache::new()),
124 undistort_backend: None,
125 }
126 }
127}
128
129pub struct DenseRun {
131 pub frames: usize,
133 pub scene: DenseScene,
135 pub output: DenseOutput,
137 pub undistort_time: Duration,
139 pub densify_time: Duration,
141 pub lock_wait: Duration,
143}
144
145pub fn dense_model(model: &Reconstruction, keep: impl Fn(&str) -> bool, image_root: &Path, cfg: &DenseConfig) -> Result<DenseRun, String> {
148 let backend = cfg.backend.clone()?;
149 let t0 = Instant::now();
150 let mut m = model.clone();
151 let del: Vec<String> =
152 m.registered_images().into_iter().filter_map(|id| m.image(id).map(|im| im.name.clone())).filter(|n| !keep(n)).collect();
153 let del_ref: Vec<&str> = del.iter().map(|s| s.as_str()).collect();
154 m.deregister_images_by_name(&del_ref);
155 let root: PathBuf = image_root.to_path_buf();
156 let und = undistort_with(
157 &m,
158 &cfg.undistort,
159 &cfg.undistort_cache,
160 |im| ImageBuffer::load(root.join(&im.name)).ok().map(Arc::new),
161 cfg.undistort_backend.as_deref(),
162 )
163 .map_err(|e| format!("왜곡 보정 실패: {e}"))?;
164 let images: BTreeMap<ImageId, Arc<ImageBuffer>> = und.images;
165 let frames = images.len();
166 let scene = DenseScene::from_reconstruction(&und.reconstruction, &images, &cfg.scene).map_err(|e| format!("장면 변환 실패: {e}"))?;
167 drop(images);
168 let undistort_time = t0.elapsed();
169 let tw = Instant::now();
170 let guard = cfg.lock.as_ref().map(|l| l.lock().unwrap_or_else(|p| p.into_inner()));
171 let lock_wait = tw.elapsed();
172 let t1 = Instant::now();
173 let output =
174 cumulus3d_dense::densify::densify_with(&scene, &cfg.densify, cfg.score.as_ref(), backend.as_ref(), cfg.depth_cache.as_deref())
175 .map_err(|e| format!("조밀화 실패: {e}"));
176 drop(guard);
177 let output = output?;
178 Ok(DenseRun { frames, scene, output, undistort_time, densify_time: t1.elapsed(), lock_wait })
179}