Skip to main content

cumulus3d_cli/
densewrap.rs

1/*
2 * densewrap.rs
3 *
4 * Copyright (c) 2026 ParkSangWoo
5 *
6 * Author(s):
7 *
8 *      ParkSangWoo <dev.parksangwoo@gmail.com>
9 *
10 *
11 * This file is part of cumulus3d.
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License");
14 * you may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 *      http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *
25 * SPDX-License-Identifier: Apache-2.0
26 */
27
28//! Shared densification path: exclude images outside the zone -> undistortion -> scene conversion -> densify.
29//! Used by both the stream and the `densify` subcommand.
30
31use 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
41/// PatchMatch backend selection. Currently only `cuda` exists. Without a device, an error string.
42pub 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
51/// Image undistortion backend selection (cpu|cuda). `None` = the CPU path.
52pub 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
62/// Interpretation of `--mvs-profile`.
63pub fn parse_profile(s: &str) -> Result<MvsProfile, String> {
64    MvsProfile::parse(s).ok_or_else(|| format!("알 수 없는 --mvs-profile {s} (fast|quality)"))
65}
66
67/// SIFT backend selection (cpu|cuda).
68pub 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
78/// Descriptor matching backend selection (cpu|cuda).
79pub 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/// Densification settings.
90#[derive(Clone)]
91pub struct DenseConfig {
92    /// Undistortion options.
93    pub undistort: UndistortOptions,
94    /// Dense scene conversion options.
95    pub scene: SceneOptions,
96    /// Densification (PatchMatch, filter, fusion) options.
97    pub densify: DensifyOptions,
98    /// Score fusion settings (if present, score fusion is used instead of `densify.fusion.mode`).
99    pub score: Option<cumulus3d_dense::fusion_score::ScoreFusionOptions>,
100    /// Backend (if it could not be created, that error; reported in the densification stage).
101    pub backend: Result<Arc<dyn PatchMatchBackend>, String>,
102    /// `--serialize-dense`: one backend call at a time.
103    pub lock: Option<Arc<Mutex<()>>>,
104    /// Depth map cache (`--depth-cache`).
105    pub depth_cache: Option<Arc<DepthMapCache>>,
106    /// Undistortion result cache.
107    pub undistort_cache: Arc<UndistortCache>,
108    /// Image undistortion backend (None = CPU).
109    pub undistort_backend: Option<Arc<dyn UndistortBackend>>,
110}
111
112impl DenseConfig {
113    /// Undistortion max_image_size 960, profile defaults.
114    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
129/// Densification result summary.
130pub struct DenseRun {
131    /// Number of images used for densification.
132    pub frames: usize,
133    /// Densification input scene.
134    pub scene: DenseScene,
135    /// Densification result (point cloud etc.).
136    pub output: DenseOutput,
137    /// Undistortion time.
138    pub undistort_time: Duration,
139    /// Densification time.
140    pub densify_time: Duration,
141    /// Time spent waiting for the backend serialization lock.
142    pub lock_wait: Duration,
143}
144
145/// Densify after removing registered images for which `keep` is false (image_deleter). The input image before undistortion is
146/// `image_root/name`.
147pub 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}