use std::sync::Arc;
use pixelmap::Photo;
use crate::ba;
use crate::calib::{FocalSource, Intrinsics};
use crate::depth::{self, DepthMap, DepthStats};
use crate::error::Error;
use crate::event::{emit, report, Event, Flow, Level, Stage};
use crate::fusion::{self, Fused};
use crate::input::{self, MIN_VIEWS};
use crate::lookup::PairLookup;
use crate::mesh::Mesh;
use crate::options::Options;
use crate::pairs::{self, PairGraph, MIN_PAIR_COVERAGE};
use crate::pose::Pose;
use crate::rng::Rng;
use crate::sfm::{self, DropReason, SparseModel};
use crate::texture::{self, Texture};
use crate::tracks::{self, Track};
use crate::twoview::{self, Degeneracy, RelativePose, Verdict};
use crate::types::{PairId, ViewId};
#[doc(hidden)]
#[derive(Clone, Debug)]
pub struct Params {
pub seed: u64,
pub twoview: twoview::Params,
pub tracks: tracks::Params,
pub sfm: sfm::Params,
pub ba: ba::Params,
pub depth: depth::Params,
pub fusion: fusion::Params,
pub texture: texture::Params,
}
impl Default for Params {
fn default() -> Self {
Params::from_options(&Options::default())
}
}
impl Params {
pub(crate) fn from_options(options: &Options) -> Self {
Params {
seed: options.resolved_seed(),
twoview: twoview::Params::default(),
tracks: tracks::Params::default(),
sfm: sfm::Params::default(),
ba: ba::Params {
refine_focal: options.refine_focal,
..ba::Params::default()
},
depth: depth::Params::default(),
fusion: fusion::Params::default(),
texture: texture::Params {
max_atlas_size: options.max_texture_size,
..texture::Params::default()
},
}
}
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct PairReport {
pub pair: PairId,
pub coverage: f32,
pub pose: Option<Pose>,
pub inlier_ratio: Option<f64>,
pub median_angle_deg: Option<f64>,
pub rejection: Option<Degeneracy>,
}
impl PairReport {
fn new(pair: PairId, coverage: f32, estimate: &Result<RelativePose, Degeneracy>) -> PairReport {
let (pose, inlier_ratio, median_angle_deg, rejection) = match estimate {
Ok(e) => match &e.verdict {
Verdict::Usable => (
Some(e.pose),
Some(e.inlier_ratio),
Some(e.median_angle_deg),
None,
),
Verdict::Degenerate(reason) => (
None,
Some(e.inlier_ratio),
Some(e.median_angle_deg),
Some(reason.clone()),
),
},
Err(reason) => (None, None, None, Some(reason.clone())),
};
PairReport {
pair,
coverage,
pose,
inlier_ratio,
median_angle_deg,
rejection,
}
}
}
#[derive(Clone, Debug)]
pub struct Model {
pub mesh: Mesh,
pub texture: Texture,
pub intrinsics: Intrinsics,
pub cameras: Vec<Option<Pose>>,
pub views: usize,
pub connected: Vec<ViewId>,
pub pairs: Vec<PairReport>,
diagnostics: Option<Box<Diagnostics>>,
}
impl Model {
pub fn diagnostics(&self) -> Option<&Diagnostics> {
self.diagnostics.as_deref()
}
pub fn take_diagnostics(&mut self) -> Option<Diagnostics> {
self.diagnostics.take().map(|d| *d)
}
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct Diagnostics {
pub size: (usize, usize),
pub tracks: Vec<Track>,
pub registered: SparseModel,
pub adjusted: SparseModel,
pub adjustment: ba::Report,
pub depth: Vec<DepthMap>,
pub depth_stats: Vec<DepthStats>,
pub voxel_size: f64,
pub blocks: usize,
pub largest_component: f64,
pub dropped_triangles: usize,
}
pub fn run(
photos: &[Arc<Photo>],
options: &Options,
on_event: &mut dyn FnMut(Event) -> Flow,
) -> Result<Model, Error> {
let first = photos.first().ok_or(Error::TooFewPhotos {
found: 0,
minimum: MIN_VIEWS,
})?;
let intrinsics = options.focal.intrinsics(first.width(), first.height());
let (width, height) = input::validate(photos, &intrinsics)?;
emit(
on_event,
Event::Started {
views: photos.len(),
pairs: PairId::count(photos.len()),
},
)?;
report(
on_event,
Stage::Input,
1.0,
format!("{} photos at {width}×{height}", photos.len()),
)?;
let graph = pairs::compute(photos, options.quality, options.resolved_seed(), on_event)?;
reconstruct_with_params(
&graph,
photos,
&intrinsics,
&Params::from_options(options),
on_event,
)
}
pub fn reconstruct<L: PairLookup>(
graph: &PairGraph<L>,
photos: &[Arc<Photo>],
options: &Options,
on_event: &mut dyn FnMut(Event) -> Flow,
) -> Result<Model, Error> {
let first = photos.first().ok_or(Error::TooFewPhotos {
found: 0,
minimum: MIN_VIEWS,
})?;
let intrinsics = options.focal.intrinsics(first.width(), first.height());
reconstruct_with_params(
graph,
photos,
&intrinsics,
&Params::from_options(options),
on_event,
)
}
#[doc(hidden)]
pub fn reconstruct_with_params<L: PairLookup>(
graph: &PairGraph<L>,
photos: &[Arc<Photo>],
intrinsics: &Intrinsics,
params: &Params,
on_event: &mut dyn FnMut(Event) -> Flow,
) -> Result<Model, Error> {
if graph.views() != photos.len() {
return Err(Error::GraphMismatch {
views: graph.views(),
photos: photos.len(),
});
}
let size = input::validate(photos, intrinsics)?;
let precision = graph.precision_px() as f64;
let connected = pairs::require_connected(graph, MIN_PAIR_COVERAGE)?;
for view in (0..graph.views() as u32).map(ViewId) {
if !connected.contains(&view) {
emit(
on_event,
Event::ViewDropped {
stage: Stage::Pairs,
view,
reason: DropReason::NotLinked {
min_coverage: MIN_PAIR_COVERAGE,
},
},
)?;
}
}
let root = Rng::new(params.seed);
let total = PairId::count(graph.views());
let mut pair_reports = Vec::with_capacity(total);
let mut estimates = Vec::with_capacity(total);
for (index, (pair, lookup)) in graph.pairs().enumerate() {
let estimate = twoview::estimate(
pair,
lookup,
intrinsics,
size,
¶ms.twoview,
&mut root.derive(index as u64),
);
let outcome = PairReport::new(pair, lookup.coverage(), &estimate);
match &outcome.rejection {
Some(reason) => emit(
on_event,
Event::PairRejected {
pair,
index,
of: total,
reason: reason.clone(),
},
)?,
None => {
let estimate = estimate
.as_ref()
.expect("a pair with nothing against it has an estimate");
report(
on_event,
Stage::TwoView,
(index + 1) as f32 / total as f32,
format!(
"{pair}: usable; {:.0}% mapped, {:.0}% of matches agree, {:.1}° between rays",
estimate.coverage * 100.0,
estimate.inlier_ratio * 100.0,
estimate.median_angle_deg
),
)?;
}
}
pair_reports.push(outcome);
estimates.push(estimate);
}
let relative: Vec<RelativePose> = estimates
.into_iter()
.filter_map(Result::ok)
.filter(|r| connected.contains(&r.pair.a()) && connected.contains(&r.pair.b()))
.collect();
let tracks = tracks::build(graph, size, ¶ms.tracks);
let multi = tracks::require_enough(&tracks)?;
report(
on_event,
Stage::Tracks,
1.0,
format!(
"{} tracks, {multi} of them seen in three or more views",
tracks.len()
),
)?;
let refine_focal = params.ba.refine_focal || intrinsics.source == FocalSource::Estimated;
let registered = sfm::reconstruct_with_progress(
&relative,
&tracks,
intrinsics,
graph.views(),
precision,
&sfm::Params {
adjust: ba::Params {
refine_focal,
..params.sfm.adjust.clone()
},
..params.sfm.clone()
},
&mut root.derive(u64::MAX),
on_event,
)
.map_err(|error| explain_no_usable_pair(error, &pair_reports))?;
for warning in ®istered.warnings {
match warning {
sfm::Warning::Unregistered { .. } => {}
other => emit(
on_event,
Event::Log {
stage: Stage::Registration,
level: Level::Warning,
message: other.to_string(),
},
)?,
}
}
report(
on_event,
Stage::Registration,
1.0,
format!(
"{} of {} views placed, starting from {}; {} points",
registered.registered().len(),
graph.views(),
registered.seed,
registered.points.len()
),
)?;
let ba::Adjusted {
model: adjusted,
intrinsics: refined,
report: adjustment,
} = ba::adjust_with_progress(
®istered,
&tracks,
®istered.intrinsics,
precision,
&ba::Params {
refine_focal,
..params.ba.clone()
},
on_event,
)?;
let mut summary = format!(
"median reprojection error {:.2} px → {:.2} px",
adjustment.initial_median_px, adjustment.final_median_px
);
if refine_focal {
summary += &format!(
"; focal length {:.1} px → {:.1} px",
intrinsics.fx, refined.fx
);
}
report(on_event, Stage::BundleAdjustment, 1.0, summary)?;
let (maps, depth_stats) = depth::estimate_with_progress(
graph,
&adjusted.cameras,
&refined,
size,
¶ms.depth,
on_event,
)?;
let valid = depth::require_coverage(&maps)?;
report(
on_event,
Stage::Depth,
1.0,
format!(
"{:.0}% of samples have a depth, one sample every {} px",
valid * 100.0,
maps.first().map_or(0, |m| m.stride)
),
)?;
let Fused {
mesh,
voxel_size,
blocks,
largest_component,
dropped_triangles,
} = fusion::fuse_with_progress(&maps, &adjusted.cameras, &refined, ¶ms.fusion, on_event)?;
report(
on_event,
Stage::Fusion,
1.0,
format!(
"{} vertices, {} triangles, voxel size {voxel_size:.4}",
mesh.positions.len(),
mesh.triangles.len()
),
)?;
let texture = texture::build_with_progress(
&mesh,
&adjusted.cameras,
&refined,
photos,
&maps,
¶ms.texture,
on_event,
)?;
report(
on_event,
Stage::Texture,
1.0,
format!(
"{} charts in a {}×{} atlas",
texture.charts,
texture.atlas.width(),
texture.atlas.height()
),
)?;
Ok(Model {
mesh,
texture,
intrinsics: refined,
cameras: adjusted.cameras.clone(),
views: graph.views(),
connected,
pairs: pair_reports,
diagnostics: Some(Box::new(Diagnostics {
size,
tracks,
registered,
adjusted,
adjustment,
depth: maps,
depth_stats,
voxel_size,
blocks,
largest_component,
dropped_triangles,
})),
})
}
fn explain_no_usable_pair(error: Error, reports: &[PairReport]) -> Error {
match error {
Error::NoUsablePair { .. } => Error::NoUsablePair {
reasons: reports
.iter()
.filter_map(|r| Some((r.pair, r.rejection.clone()?)))
.collect(),
},
other => other,
}
}