1use crate::error::{Error, Result};
4use crate::geometry::{Mat3, Rigid3};
5
6pub const DESCRIPTOR_DIM: usize = 128;
8
9#[derive(Clone, Copy, Debug, PartialEq, Default)]
14pub struct Keypoint {
15 pub x: f32,
17 pub y: f32,
19 pub a11: f32,
21 pub a12: f32,
23 pub a21: f32,
25 pub a22: f32,
27}
28
29impl Keypoint {
30 pub fn new(x: f32, y: f32) -> Self {
32 Self { x, y, a11: 1.0, a12: 0.0, a21: 0.0, a22: 1.0 }
33 }
34 pub fn from_scale_orientation(x: f32, y: f32, scale: f32, orientation: f32) -> Self {
36 let (s, c) = orientation.sin_cos();
37 Self { x, y, a11: scale * c, a12: -scale * s, a21: scale * s, a22: scale * c }
38 }
39 pub fn from_row(row: &[f32]) -> Result<Self> {
41 match row.len() {
42 2 => Ok(Self::new(row[0], row[1])),
43 4 => Ok(Self::from_scale_orientation(row[0], row[1], row[2], row[3])),
44 6 => Ok(Self { x: row[0], y: row[1], a11: row[2], a12: row[3], a21: row[4], a22: row[5] }),
45 n => Err(Error::Format(format!("키포인트 열 수 {n} 는 지원하지 않음"))),
46 }
47 }
48 pub fn to_row(&self) -> [f32; 6] {
50 [self.x, self.y, self.a11, self.a12, self.a21, self.a22]
51 }
52 pub fn scale_x(&self) -> f32 {
54 (self.a11 * self.a11 + self.a21 * self.a21).sqrt()
55 }
56 pub fn scale_y(&self) -> f32 {
58 (self.a12 * self.a12 + self.a22 * self.a22).sqrt()
59 }
60 pub fn scale(&self) -> f32 {
62 0.5 * (self.scale_x() + self.scale_y())
63 }
64 pub fn orientation(&self) -> f32 {
66 self.a21.atan2(self.a11)
67 }
68 pub fn shear(&self) -> f32 {
70 (-self.a12).atan2(self.a22) - self.orientation()
71 }
72 pub fn rescale(&mut self, sx: f32, sy: f32) {
74 self.x *= sx;
75 self.y *= sy;
76 self.a11 *= sx;
77 self.a21 *= sx;
78 self.a12 *= sy;
79 self.a22 *= sy;
80 }
81}
82
83#[derive(Clone, Debug, PartialEq, Default)]
85pub struct Descriptors {
86 data: Vec<u8>,
87}
88
89impl Descriptors {
90 pub fn new() -> Self {
92 Self::default()
93 }
94 pub fn with_capacity(n: usize) -> Self {
96 Self { data: Vec::with_capacity(n * DESCRIPTOR_DIM) }
97 }
98 pub fn from_vec(data: Vec<u8>) -> Result<Self> {
100 if !data.len().is_multiple_of(DESCRIPTOR_DIM) {
101 return Err(Error::Format(format!("기술자 바이트 수 {} 가 128 의 배수가 아님", data.len())));
102 }
103 Ok(Self { data })
104 }
105 pub fn len(&self) -> usize {
107 self.data.len() / DESCRIPTOR_DIM
108 }
109 pub fn is_empty(&self) -> bool {
111 self.data.is_empty()
112 }
113 pub fn row(&self, i: usize) -> &[u8] {
115 &self.data[i * DESCRIPTOR_DIM..(i + 1) * DESCRIPTOR_DIM]
116 }
117 pub fn row_mut(&mut self, i: usize) -> &mut [u8] {
119 &mut self.data[i * DESCRIPTOR_DIM..(i + 1) * DESCRIPTOR_DIM]
120 }
121 pub fn push(&mut self, d: &[u8; DESCRIPTOR_DIM]) {
123 self.data.extend_from_slice(d);
124 }
125 pub fn as_slice(&self) -> &[u8] {
127 &self.data
128 }
129 pub fn into_vec(self) -> Vec<u8> {
131 self.data
132 }
133 pub fn truncate(&mut self, n: usize) {
135 self.data.truncate(n * DESCRIPTOR_DIM);
136 }
137}
138
139#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
141pub struct FeatureMatch {
142 pub idx1: u32,
144 pub idx2: u32,
146}
147
148impl FeatureMatch {
149 pub fn new(idx1: u32, idx2: u32) -> Self {
151 Self { idx1, idx2 }
152 }
153 pub fn swapped(&self) -> Self {
155 Self { idx1: self.idx2, idx2: self.idx1 }
156 }
157}
158
159#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
161pub enum TwoViewGeometryConfig {
162 #[default]
163 Undefined = 0,
165 Degenerate = 1,
167 Calibrated = 2,
169 Uncalibrated = 3,
171 Planar = 4,
173 Panoramic = 5,
175 PlanarOrRotation = 6,
177 Watermark = 7,
179 Multiple = 8,
181 CalibratedRig = 9,
183}
184
185impl TwoViewGeometryConfig {
186 pub fn from_i32(v: i32) -> Option<Self> {
188 use TwoViewGeometryConfig::*;
189 Some(match v {
190 0 => Undefined,
191 1 => Degenerate,
192 2 => Calibrated,
193 3 => Uncalibrated,
194 4 => Planar,
195 5 => Panoramic,
196 6 => PlanarOrRotation,
197 7 => Watermark,
198 8 => Multiple,
199 9 => CalibratedRig,
200 _ => return None,
201 })
202 }
203 pub fn as_i32(self) -> i32 {
205 self as i32
206 }
207}
208
209#[derive(Clone, Debug, PartialEq, Default)]
211pub struct TwoViewGeometry {
212 pub config: TwoViewGeometryConfig,
214 pub e: Option<Mat3>,
216 pub f: Option<Mat3>,
218 pub h: Option<Mat3>,
220 pub cam1_to_cam2: Option<Rigid3>,
222 pub inlier_matches: Vec<FeatureMatch>,
224 pub tri_angle: Option<f64>,
226}
227
228impl TwoViewGeometry {
229 pub fn inverted(&self) -> Self {
231 Self {
232 config: self.config,
233 e: self.e.map(|m| m.transpose()),
234 f: self.f.map(|m| m.transpose()),
235 h: self.h.and_then(|m| m.try_inverse()),
236 cam1_to_cam2: self.cam1_to_cam2.map(|p| p.inverse()),
237 inlier_matches: self.inlier_matches.iter().map(|m| m.swapped()).collect(),
238 tri_angle: self.tri_angle,
239 }
240 }
241 pub fn invert(&mut self) {
243 *self = self.inverted();
244 }
245}
246
247#[cfg(test)]
248mod tests {
249 use super::*;
250
251 #[test]
252 fn keypoint_shape() {
253 let k = Keypoint::from_scale_orientation(1600.5, 1200.25, 2.0, 0.3);
254 assert!((k.scale() - 2.0).abs() < 1e-6);
255 assert!((k.orientation() - 0.3).abs() < 1e-6);
256 assert!(k.shear().abs() < 1e-6);
257 let mut k2 = k;
258 k2.rescale(4000.0 / 3200.0, 3000.0 / 2400.0);
259 assert!((k2.x - 2000.625).abs() < 1e-5 * 2000.0);
260 assert!((k2.y - 1500.3125).abs() < 1e-5 * 1500.0);
261 assert!((k2.scale() - 2.5).abs() < 1e-5);
262 assert!((k2.orientation() - 0.3).abs() < 1e-5);
263 assert_eq!(Keypoint::from_row(&k.to_row()).unwrap(), k);
264 assert!(Keypoint::from_row(&[1.0; 3]).is_err());
265 }
266
267 #[test]
268 fn tvg_invert() {
269 let h = Mat3::new(1.0, 0.1, 5.0, 0.0, 1.2, -3.0, 0.001, 0.0, 1.0);
270 let g = TwoViewGeometry {
271 config: TwoViewGeometryConfig::Calibrated,
272 h: Some(h),
273 f: Some(h),
274 inlier_matches: vec![FeatureMatch::new(1, 2)],
275 ..Default::default()
276 };
277 let gi = g.inverted();
278 assert!((gi.h.unwrap() * h - Mat3::identity()).norm() < 1e-12);
279 assert_eq!(gi.f.unwrap(), h.transpose());
280 assert_eq!(gi.inlier_matches[0], FeatureMatch::new(2, 1));
281 let gii = gi.inverted();
282 assert!((gii.h.unwrap() - h).norm() < 1e-12);
283 }
284}