|
| 1 | +use crate::BA_NCAMPARAMS; |
| 2 | +use crate::compute_zach_weight_error; |
| 3 | +use std::autodiff::autodiff_reverse; |
| 4 | +use std::convert::TryInto; |
| 5 | + |
| 6 | +fn sqsum(x: &[f64]) -> f64 { |
| 7 | + x.iter().map(|&v| v * v).sum() |
| 8 | +} |
| 9 | + |
| 10 | +#[inline] |
| 11 | +fn cross(a: &[f64; 3], b: &[f64; 3]) -> [f64; 3] { |
| 12 | + [ |
| 13 | + a[1] * b[2] - a[2] * b[1], |
| 14 | + a[2] * b[0] - a[0] * b[2], |
| 15 | + a[0] * b[1] - a[1] * b[0], |
| 16 | + ] |
| 17 | +} |
| 18 | + |
| 19 | +fn radial_distort(rad_params: &[f64], proj: &mut [f64]) { |
| 20 | + let rsq = sqsum(proj); |
| 21 | + let l = 1. + rad_params[0] * rsq + rad_params[1] * rsq * rsq; |
| 22 | + proj[0] = proj[0] * l; |
| 23 | + proj[1] = proj[1] * l; |
| 24 | +} |
| 25 | + |
| 26 | +fn rodrigues_rotate_point(rot: &[f64; 3], pt: &[f64; 3], rotated_pt: &mut [f64; 3]) { |
| 27 | + let sqtheta = sqsum(rot); |
| 28 | + if sqtheta != 0. { |
| 29 | + let theta = sqtheta.sqrt(); |
| 30 | + let costheta = theta.cos(); |
| 31 | + let sintheta = theta.sin(); |
| 32 | + let theta_inverse = 1. / theta; |
| 33 | + let mut w = [0.; 3]; |
| 34 | + for i in 0..3 { |
| 35 | + w[i] = rot[i] * theta_inverse; |
| 36 | + } |
| 37 | + let w_cross_pt = cross(&w, &pt); |
| 38 | + let tmp = (w[0] * pt[0] + w[1] * pt[1] + w[2] * pt[2]) * (1. - costheta); |
| 39 | + for i in 0..3 { |
| 40 | + rotated_pt[i] = pt[i] * costheta + w_cross_pt[i] * sintheta + w[i] * tmp; |
| 41 | + } |
| 42 | + } else { |
| 43 | + let rot_cross_pt = cross(&rot, &pt); |
| 44 | + for i in 0..3 { |
| 45 | + rotated_pt[i] = pt[i] + rot_cross_pt[i]; |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +fn project(cam: &[f64; 11], X: &[f64; 3], proj: &mut [f64; 2]) { |
| 51 | + let C = &cam[3..6]; |
| 52 | + let mut Xo = [0.; 3]; |
| 53 | + let mut Xcam = [0.; 3]; |
| 54 | + |
| 55 | + Xo[0] = X[0] - C[0]; |
| 56 | + Xo[1] = X[1] - C[1]; |
| 57 | + Xo[2] = X[2] - C[2]; |
| 58 | + |
| 59 | + rodrigues_rotate_point(cam.first_chunk::<3>().unwrap(), &Xo, &mut Xcam); |
| 60 | + |
| 61 | + proj[0] = Xcam[0] / Xcam[2]; |
| 62 | + proj[1] = Xcam[1] / Xcam[2]; |
| 63 | + |
| 64 | + radial_distort(&cam[9..], proj); |
| 65 | + |
| 66 | + proj[0] = proj[0] * cam[6] + cam[7]; |
| 67 | + proj[1] = proj[1] * cam[6] + cam[8]; |
| 68 | +} |
| 69 | + |
| 70 | +#[no_mangle] |
| 71 | +pub extern "C" fn rust_dcompute_reproj_error( |
| 72 | + cam: *const [f64; 11], |
| 73 | + dcam: *mut [f64; 11], |
| 74 | + x: *const [f64; 3], |
| 75 | + dx: *mut [f64; 3], |
| 76 | + w: *const [f64; 1], |
| 77 | + wb: *mut [f64; 1], |
| 78 | + feat: *const [f64; 2], |
| 79 | + err: *mut [f64; 2], |
| 80 | + derr: *mut [f64; 2], |
| 81 | +) { |
| 82 | + unsafe {dcompute_reproj_error(cam, dcam, x, dx, w, wb, feat, err, derr)}; |
| 83 | +} |
| 84 | + |
| 85 | +#[autodiff_reverse( |
| 86 | + dcompute_reproj_error, |
| 87 | + Duplicated, |
| 88 | + Duplicated, |
| 89 | + Duplicated, |
| 90 | + Const, |
| 91 | + DuplicatedOnly |
| 92 | +)] |
| 93 | +pub fn compute_reproj_error( |
| 94 | + cam: *const [f64; 11], |
| 95 | + x: *const [f64; 3], |
| 96 | + w: *const [f64; 1], |
| 97 | + feat: *const [f64; 2], |
| 98 | + err: *mut [f64; 2], |
| 99 | +) { |
| 100 | + let cam = unsafe { &*cam }; |
| 101 | + let w = unsafe { *(*w).get_unchecked(0) }; |
| 102 | + let x = unsafe { &*x }; |
| 103 | + let feat = unsafe { &*feat }; |
| 104 | + let err = unsafe { &mut *err }; |
| 105 | + let mut proj = [0.; 2]; |
| 106 | + project(cam, x, &mut proj); |
| 107 | + err[0] = w * (proj[0] - feat[0]); |
| 108 | + err[1] = w * (proj[1] - feat[1]); |
| 109 | +} |
| 110 | + |
| 111 | +// n number of cameras |
| 112 | +// m number of points |
| 113 | +// p number of observations |
| 114 | +// cams: 11*n cameras in format [r1 r2 r3 C1 C2 C3 f u0 v0 k1 k2] |
| 115 | +// r1, r2, r3 are angle - axis rotation parameters(Rodrigues) |
| 116 | +// [C1 C2 C3]' is the camera center |
| 117 | +// f is the focal length in pixels |
| 118 | +// [u0 v0]' is the principal point |
| 119 | +// k1, k2 are radial distortion parameters |
| 120 | +// X: 3*m points |
| 121 | +// obs: 2*p observations (pairs cameraIdx, pointIdx) |
| 122 | +// feats: 2*p features (x,y coordinates corresponding to observations) |
| 123 | +// reproj_err: 2*p errors of observations |
| 124 | +// w_err: p weight "error" terms |
| 125 | +fn rust_ba_objective( |
| 126 | + n: usize, |
| 127 | + m: usize, |
| 128 | + p: usize, |
| 129 | + cams: &[f64], |
| 130 | + x: &[f64], |
| 131 | + w: &[f64], |
| 132 | + obs: &[i32], |
| 133 | + feats: &[f64], |
| 134 | + reproj_err: &mut [f64], |
| 135 | + w_err: &mut [f64], |
| 136 | +) { |
| 137 | + assert_eq!(cams.len(), n * 11); |
| 138 | + assert_eq!(x.len(), m * 3); |
| 139 | + assert_eq!(w.len(), p); |
| 140 | + assert_eq!(obs.len(), p * 2); |
| 141 | + assert_eq!(feats.len(), p * 2); |
| 142 | + assert_eq!(reproj_err.len(), p * 2); |
| 143 | + assert_eq!(w_err.len(), p); |
| 144 | + |
| 145 | + for i in 0..p { |
| 146 | + let cam_idx = obs[i * 2 + 0] as usize; |
| 147 | + let pt_idx = obs[i * 2 + 1] as usize; |
| 148 | + let start = cam_idx * BA_NCAMPARAMS; |
| 149 | + let cam: &[f64; 11] = unsafe { |
| 150 | + cams[start..] |
| 151 | + .get_unchecked(..11) |
| 152 | + .try_into() |
| 153 | + .unwrap_unchecked() |
| 154 | + }; |
| 155 | + let x: &[f64; 3] = unsafe { |
| 156 | + x[pt_idx * 3..] |
| 157 | + .get_unchecked(..3) |
| 158 | + .try_into() |
| 159 | + .unwrap_unchecked() |
| 160 | + }; |
| 161 | + let w: &[f64; 1] = unsafe { w[i..].get_unchecked(..1).try_into().unwrap_unchecked() }; |
| 162 | + let feat: &[f64; 2] = unsafe { |
| 163 | + feats[i * 2..] |
| 164 | + .get_unchecked(..2) |
| 165 | + .try_into() |
| 166 | + .unwrap_unchecked() |
| 167 | + }; |
| 168 | + let reproj_err: &mut [f64; 2] = unsafe { |
| 169 | + reproj_err[i * 2..] |
| 170 | + .get_unchecked_mut(..2) |
| 171 | + .try_into() |
| 172 | + .unwrap_unchecked() |
| 173 | + }; |
| 174 | + compute_reproj_error(cam, x, w, feat, reproj_err); |
| 175 | + } |
| 176 | + |
| 177 | + for i in 0..p { |
| 178 | + let w_err: &mut f64 = unsafe { w_err.get_unchecked_mut(i) }; |
| 179 | + compute_zach_weight_error(w[i..].as_ptr(), w_err as *mut f64); |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +#[no_mangle] |
| 184 | +extern "C" fn rust2_ba_objective( |
| 185 | + n: i32, |
| 186 | + m: i32, |
| 187 | + p: i32, |
| 188 | + cams: *const f64, |
| 189 | + x: *const f64, |
| 190 | + w: *const f64, |
| 191 | + obs: *const i32, |
| 192 | + feats: *const f64, |
| 193 | + reproj_err: *mut f64, |
| 194 | + w_err: *mut f64, |
| 195 | +) { |
| 196 | + let n = n as usize; |
| 197 | + let m = m as usize; |
| 198 | + let p = p as usize; |
| 199 | + let cams = unsafe { std::slice::from_raw_parts(cams, n * 11) }; |
| 200 | + let x = unsafe { std::slice::from_raw_parts(x, m * 3) }; |
| 201 | + let w = unsafe { std::slice::from_raw_parts(w, p) }; |
| 202 | + let obs = unsafe { std::slice::from_raw_parts(obs, p * 2) }; |
| 203 | + let feats = unsafe { std::slice::from_raw_parts(feats, p * 2) }; |
| 204 | + let reproj_err = unsafe { std::slice::from_raw_parts_mut(reproj_err, p * 2) }; |
| 205 | + let w_err = unsafe { std::slice::from_raw_parts_mut(w_err, p) }; |
| 206 | + rust_ba_objective(n, m, p, cams, x, w, obs, feats, reproj_err, w_err); |
| 207 | +} |
0 commit comments