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
use nalgebra::Affine2;
use crate::core::multires;
use crate::misc::type_aliases::{Float, Iso3, Mat3, Point2, Point3, Vec3};
#[derive(PartialEq, Debug, Clone)]
pub struct Camera {
pub intrinsics: Intrinsics,
pub extrinsics: Extrinsics,
}
impl Camera {
pub fn new(intrinsics: Intrinsics, extrinsics: Extrinsics) -> Self {
Self {
intrinsics,
extrinsics,
}
}
pub fn project(&self, point: Point3) -> Vec3 {
self.intrinsics
.project(extrinsics::project(&self.extrinsics, point))
}
pub fn back_project(&self, point: Point2, depth: Float) -> Point3 {
extrinsics::back_project(&self.extrinsics, self.intrinsics.back_project(point, depth))
}
pub fn multi_res(self, n: usize) -> Vec<Self> {
multires::limited_sequence(n, self, |cam| Some(cam.half_res()))
}
pub fn half_res(&self) -> Self {
Self::new(self.intrinsics.half_res(), self.extrinsics)
}
}
pub type Extrinsics = Iso3;
pub mod extrinsics {
use super::*;
pub fn project(pose: &Extrinsics, point: Point3) -> Point3 {
pose.rotation.inverse() * (pose.translation.inverse() * point)
}
pub fn back_project(pose: &Extrinsics, point: Point3) -> Point3 {
pose * point
}
}
#[derive(PartialEq, Debug, Clone)]
pub struct Intrinsics {
pub principal_point: (Float, Float),
pub focal: (Float, Float),
pub skew: Float,
}
impl Intrinsics {
#[rustfmt::skip]
pub fn matrix(&self) -> Affine2<Float> {
Affine2::from_matrix_unchecked(Mat3::new(
self.focal.0, self.skew, self.principal_point.0,
0.0, self.focal.1, self.principal_point.1,
0.0, 0.0, 1.0,
))
}
pub fn multi_res(self, n: usize) -> Vec<Self> {
multires::limited_sequence(n, self, |intrinsics| Some(intrinsics.half_res()))
}
pub fn half_res(&self) -> Self {
let (cx, cy) = self.principal_point;
let (fx, fy) = self.focal;
Self {
principal_point: ((cx + 0.5) / 2.0 - 0.5, (cy + 0.5) / 2.0 - 0.5),
focal: (0.5 * fx, 0.5 * fy),
skew: self.skew,
}
}
pub fn project(&self, point: Point3) -> Vec3 {
Vec3::new(
self.focal.0 * point[0] + self.skew * point[1] + self.principal_point.0 * point[2],
self.focal.1 * point[1] + self.principal_point.1 * point[2],
point[2],
)
}
pub fn back_project(&self, point: Point2, depth: Float) -> Point3 {
let z = depth;
let y = (point[1] - self.principal_point.1) * z / self.focal.1;
let x = ((point[0] - self.principal_point.0) * z - self.skew * y) / self.focal.0;
Point3::new(x, y, z)
}
}