1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//! Non-linear optimization for camera calibration with automatic differentiation.
//!
//! This crate provides a backend-agnostic optimization framework for camera calibration
//! problems. The core design separates problem definition from solver implementation using
//! an intermediate representation (IR) that can be compiled to different optimization backends.
//!
//! # Architecture
//!
//! The optimization pipeline has three stages:
//!
//! 1. **Problem Definition** - Build a \[`ir::ProblemIR`\] describing parameters, factors, and constraints
//! 2. **Backend Compilation** - Translate IR into solver-specific problem (e.g., \[`backend::TinySolverBackend`\])
//! 3. **Optimization** - Run solver and extract solution as domain types
//!
//! ```text
//! Problem Builder → ProblemIR → Backend.compile() → Backend.solve() → Domain Result
//! ```
//!
//! ## Key Components
//!
//! - **\[`ir`\]** - Backend-agnostic intermediate representation for optimization problems
//! - **\[`params`\]** - Parameter block definitions (intrinsics, distortion, poses)
//! - **\[`factors`\]** - Residual functions with automatic differentiation support
//! - **\[`backend`\]** - Solver implementations (currently tiny-solver with Levenberg-Marquardt)
//! - **\[`problems`\]** - High-level calibration problem builders (planar intrinsics, etc.)
//!
//! # Examples
//!
//! ## Basic Planar Intrinsics Calibration
//!
//! ```rust,no_run
//! use vision_calibration_core::{BrownConrady5, CorrespondenceView, DistortionFixMask, FxFyCxCySkew, Iso3, PlanarDataset, Pt2, Pt3, View};
//! use vision_calibration_optim::{
//! optimize_planar_intrinsics, BackendSolveOptions, PlanarIntrinsicsParams,
//! PlanarIntrinsicsSolveOptions, RobustLoss,
//! };
//!
//! # fn example() -> anyhow::Result<()> {
//! // 1. Prepare observations (world points + image detections)
//! let view = View::without_meta(CorrespondenceView::new(
//! vec![
//! Pt3::new(0.0, 0.0, 0.0),
//! Pt3::new(1.0, 0.0, 0.0),
//! Pt3::new(1.0, 1.0, 0.0),
//! Pt3::new(0.0, 1.0, 0.0),
//! ],
//! vec![
//! Pt2::new(100.0, 100.0),
//! Pt2::new(200.0, 100.0),
//! Pt2::new(200.0, 200.0),
//! Pt2::new(100.0, 200.0),
//! ],
//! )?);
//! let dataset = PlanarDataset::new(vec![view])?;
//!
//! // 2. Initialize with linear method or prior calibration
//! let init = PlanarIntrinsicsParams::new_from_components(
//! FxFyCxCySkew {
//! fx: 800.0,
//! fy: 800.0,
//! cx: 640.0,
//! cy: 360.0,
//! skew: 0.0,
//! },
//! BrownConrady5 {
//! k1: 0.0,
//! k2: 0.0,
//! k3: 0.0,
//! p1: 0.0,
//! p2: 0.0,
//! iters: 8,
//! },
//! vec![Iso3::identity()],
//! )?;
//!
//! // 3. Configure optimization
//! let opts = PlanarIntrinsicsSolveOptions {
//! robust_loss: RobustLoss::Huber { scale: 2.0 },
//! fix_distortion: DistortionFixMask { k3: true, ..Default::default() }, // Fix k3 to prevent overfitting
//! ..Default::default()
//! };
//!
//! // 4. Run optimization
//! let result = optimize_planar_intrinsics(&dataset, &init, opts, BackendSolveOptions::default())?;
//!
//! println!("Calibrated camera: {:?}", result.params.camera);
//! # Ok(())
//! # }
//! ```
//!
//! ## Custom Problem with IR
//!
//! # Feature Highlights
//!
//! ## Automatic Differentiation
//!
//! All residual functions are generic over [`nalgebra::RealField`], enabling automatic
//! differentiation via dual numbers. The \[`factors::reprojection_model`\] module provides
//! autodiff-compatible implementations of:
//!
//! - Pinhole projection with SE3 poses
//! - Brown-Conrady distortion (k1, k2, k3, p1, p2)
//! - Weighted reprojection residuals
//!
//! ## Flexible Parameter Fixing
//!
//! Use \[`ir::FixedMask`\] to selectively fix optimization variables:
//!
//! ```rust
//! # use vision_calibration_optim::PlanarIntrinsicsSolveOptions;
//! # use vision_calibration_core::{DistortionFixMask, IntrinsicsFixMask};
//! let opts = PlanarIntrinsicsSolveOptions {
//! fix_intrinsics: IntrinsicsFixMask { fx: true, ..Default::default() }, // Fix focal length
//! fix_distortion: DistortionFixMask { p1: true, p2: true, ..Default::default() }, // Fix tangential distortion
//! ..Default::default()
//! };
//! ```
//!
//! ## Robust Loss Functions
//!
//! Handle outliers with M-estimators ([`ir::RobustLoss`]):
//!
//! - `Huber` - L2 near zero, L1 for outliers
//! - `Cauchy` - Gradual outlier suppression
//! - `Arctan` - Bounded influence
//!
//! # Performance Considerations
//!
//! - **Initialization**: Always initialize with linear methods (the `vision-calibration-linear` crate) for faster convergence
//! - **Robust Loss**: Use Huber with `scale ≈ 2.0` for real data with corner detection noise
//! - **Distortion**: Fix `k3` by default unless calibrating wide-angle lenses
//! - **Manifolds**: SE3/SO3 parameters use proper Lie group updates for stability
//!
//! # Numerical Stability
//!
//! The implementation uses several techniques for numerical robustness:
//!
//! - Safe division with epsilon thresholds in projection
//! - Hartley normalization in linear initialization (via vision-calibration-linear)
//! - Manifold-aware parameter updates for rotations
//! - Sparse linear solvers for large problems
pub use Error;
pub use *;
pub use crate;
pub use crate;
pub use crate;
pub use crate;
pub use crateLaserPlane;
pub use crate;
pub use crate;
pub use crate;
pub use crate;
pub use crate;
pub use crate;
pub use ;