MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
camera.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015, Simon Fuhrmann
3 * TU Darmstadt - Graphics, Capture and Massively Parallel Computing
4 * All rights reserved.
5 *
6 * This software may be modified and distributed under the terms
7 * of the BSD 3-Clause license. See the LICENSE.txt file for details.
8 */
9
10#include <iostream>
11#include <iomanip>
12#include <sstream>
13#include <algorithm>
14
15#include "math/matrix_tools.h"
16#include "mve/camera.h"
17
19
20CameraInfo::CameraInfo (void)
21{
22 this->flen = 0.0f;
23 this->paspect = 1.0f;
24 std::fill(this->ppoint, this->ppoint + 2, 0.5f);
25 std::fill(this->dist, this->dist + 2, 0.0f);
26
27 std::fill(this->trans, this->trans + 3, 0.0f);
28 math::matrix_set_identity(this->rot, 3);
29}
30
31/* ---------------------------------------------------------------- */
32
33void
34CameraInfo::fill_camera_pos (float* pos) const
35{
36 pos[0] = -rot[0] * trans[0] - rot[3] * trans[1] - rot[6] * trans[2];
37 pos[1] = -rot[1] * trans[0] - rot[4] * trans[1] - rot[7] * trans[2];
38 pos[2] = -rot[2] * trans[0] - rot[5] * trans[1] - rot[8] * trans[2];
39}
40
41/* ---------------------------------------------------------------- */
42
43void
44CameraInfo::fill_camera_translation (float* trans) const
45{
46 std::copy(this->trans, this->trans + 3, trans);
47}
48
49/* ---------------------------------------------------------------- */
50
51void
52CameraInfo::fill_viewing_direction (float* viewdir) const
53{
54 for (int i = 0; i < 3; ++i)
55 viewdir[i] = this->rot[6 + i];
56}
57
58/* ---------------------------------------------------------------- */
59
60void
61CameraInfo::fill_world_to_cam (float* mat) const
62{
63 mat[0] = rot[0]; mat[1] = rot[1]; mat[2] = rot[2]; mat[3] = trans[0];
64 mat[4] = rot[3]; mat[5] = rot[4]; mat[6] = rot[5]; mat[7] = trans[1];
65 mat[8] = rot[6]; mat[9] = rot[7]; mat[10] = rot[8]; mat[11] = trans[2];
66 mat[12] = 0.0f; mat[13] = 0.0f; mat[14] = 0.0f; mat[15] = 1.0f;
67}
68
69/* ---------------------------------------------------------------- */
70
71void
72CameraInfo::fill_gl_viewtrans (float* mat) const
73{
74 mat[0] = rot[0]; mat[1] = rot[1]; mat[2] = rot[2]; mat[3] = trans[0];
75 mat[4] = -rot[3]; mat[5] = -rot[4]; mat[6] = -rot[5]; mat[7] = -trans[1];
76 mat[8] = -rot[6]; mat[9] = -rot[7]; mat[10] = -rot[8]; mat[11] = -trans[2];
77 mat[12] = 0.0f; mat[13] = 0.0f; mat[14] = 0.0f; mat[15] = 1.0f;
78}
79
80/* ---------------------------------------------------------------- */
81
82void
83CameraInfo::fill_cam_to_world (float* mat) const
84{
85 mat[0] = rot[0]; mat[1] = rot[3]; mat[2] = rot[6];
86 mat[4] = rot[1]; mat[5] = rot[4]; mat[6] = rot[7];
87 mat[8] = rot[2]; mat[9] = rot[5]; mat[10] = rot[8];
88 mat[3] = -(rot[0] * trans[0] + rot[3] * trans[1] + rot[6] * trans[2]);
89 mat[7] = -(rot[1] * trans[0] + rot[4] * trans[1] + rot[7] * trans[2]);
90 mat[11] = -(rot[2] * trans[0] + rot[5] * trans[1] + rot[8] * trans[2]);
91 mat[12] = 0.0f; mat[13] = 0.0f; mat[14] = 0.0f; mat[15] = 1.0f;
92}
93
94/* ---------------------------------------------------------------- */
95
96void
97CameraInfo::fill_world_to_cam_rot (float* mat) const
98{
99 std::copy(this->rot, this->rot + 9, mat);
100}
101
102/* ---------------------------------------------------------------- */
103
104void
105CameraInfo::fill_cam_to_world_rot (float* mat) const
106{
107 mat[0] = rot[0]; mat[1] = rot[3]; mat[2] = rot[6];
108 mat[3] = rot[1]; mat[4] = rot[4]; mat[5] = rot[7];
109 mat[6] = rot[2]; mat[7] = rot[5]; mat[8] = rot[8];
110}
111
112/* ---------------------------------------------------------------- */
113
114void
115CameraInfo::set_transformation (float const* mat)
116{
117 rot[0] = mat[0]; rot[1] = mat[1]; rot[2] = mat[2]; trans[0] = mat[3];
118 rot[3] = mat[4]; rot[4] = mat[5]; rot[5] = mat[6]; trans[1] = mat[7];
119 rot[6] = mat[8]; rot[7] = mat[9]; rot[8] = mat[10]; trans[2] = mat[11];
120}
121
122/* ---------------------------------------------------------------- */
123
124void
125CameraInfo::fill_calibration (float* mat, float width, float height) const
126{
127 float dim_aspect = width / height;
128 float image_aspect = dim_aspect * this->paspect;
129 float ax, ay;
130 if (image_aspect < 1.0f) /* Portrait. */
131 {
132 ax = this->flen * height / this->paspect;
133 ay = this->flen * height;
134 }
135 else /* Landscape. */
136 {
137 ax = this->flen * width;
138 ay = this->flen * width * this->paspect;
139 }
140
141 mat[0] = ax; mat[1] = 0.0f; mat[2] = width * this->ppoint[0];
142 mat[3] = 0.0f; mat[4] = ay; mat[5] = height * this->ppoint[1];
143 mat[6] = 0.0f; mat[7] = 0.0f; mat[8] = 1.0f;
144}
145
146/* ---------------------------------------------------------------- */
147
148void
149CameraInfo::fill_gl_projection (float* mat, float width, float height,
150 float znear, float zfar) const
151{
152 float dim_aspect = width / height;
153 float image_aspect = dim_aspect * this->paspect;
154 float ax, ay;
155 if (image_aspect < 1.0f) /* Portrait. */
156 {
157 ax = this->flen / image_aspect;
158 ay = this->flen;
159 }
160 else /* Landscape */
161 {
162 ax = this->flen;
163 ay = this->flen * image_aspect;
164 }
165
166 std::fill(mat, mat + 16, 0.0f);
167
168 mat[4 * 0 + 0] = 2.0f * ax;
169 mat[4 * 0 + 2] = 2.0f * (this->ppoint[0] - 0.5f);
170 mat[4 * 1 + 1] = 2.0f * ay;
171 mat[4 * 1 + 2] = 2.0f * (this->ppoint[1] - 0.5f);
172 mat[4 * 2 + 2] = -(zfar + znear) / (zfar - znear);
173 mat[4 * 2 + 3] = -2.0f * zfar * znear / (zfar - znear);
174 mat[4 * 3 + 2] = -1.0f;
175}
176
177/* ---------------------------------------------------------------- */
178
179void
180CameraInfo::fill_inverse_calibration (float* mat,
181 float width, float height) const
182{
183 float dim_aspect = width / height;
184 float image_aspect = dim_aspect * this->paspect;
185 float ax, ay;
186 if (image_aspect < 1.0f) /* Portrait. */
187 {
188 ax = this->flen * height / this->paspect;
189 ay = this->flen * height;
190 }
191 else /* Landscape. */
192 {
193 ax = this->flen * width;
194 ay = this->flen * width * this->paspect;
195 }
196
197 mat[0] = 1.0f / ax; mat[1] = 0.0f; mat[2] = -width * this->ppoint[0] / ax;
198 mat[3] = 0.0f; mat[4] = 1.0f / ay; mat[5] = -height * this->ppoint[1] / ay;
199 mat[6] = 0.0f; mat[7] = 0.0f; mat[8] = 1.0f;
200}
201
202/* ---------------------------------------------------------------- */
203
204void
205CameraInfo::fill_reprojection (CameraInfo const& destination,
206 float src_width, float src_height, float dst_width, float dst_height,
207 float* mat, float* vec) const
208{
209 math::Matrix3f dst_K, dst_R, src_Ri, src_Ki;
210 math::Vec3f dst_t, src_t;
211 destination.fill_calibration(dst_K.begin(), dst_width, dst_height);
212 destination.fill_world_to_cam_rot(dst_R.begin());
213 destination.fill_camera_translation(dst_t.begin());
214 this->fill_cam_to_world_rot(src_Ri.begin());
215 this->fill_inverse_calibration(src_Ki.begin(), src_width, src_height);
216 this->fill_camera_translation(src_t.begin());
217
218 math::Matrix3f ret_mat = dst_K * dst_R * src_Ri * src_Ki;
219 math::Vec3f ret_vec = dst_K * (dst_t - dst_R * src_Ri * src_t);
220 std::copy(ret_mat.begin(), ret_mat.end(), mat);
221 std::copy(ret_vec.begin(), ret_vec.end(), vec);
222}
223
224/* ---------------------------------------------------------------- */
225
226std::string
227CameraInfo::get_rotation_string (void) const
228{
229 std::stringstream ss;
230 ss << std::setprecision(10);
231 for (int i = 0; i < 9; ++i)
232 ss << this->rot[i] << (i < 8 ? " " : "");
233 return ss.str();
234}
235
236std::string
237CameraInfo::get_translation_string (void) const
238{
239 std::stringstream ss;
240 ss << std::setprecision(10);
241 for (int i = 0; i < 3; ++i)
242 ss << this->trans[i] << (i < 2 ? " " : "");
243 return ss.str();
244}
245
246void
247CameraInfo::set_translation_from_string (std::string const& trans_string)
248{
249 std::stringstream ss(trans_string);
250 for (int i = 0; i < 3; ++i)
251 ss >> this->trans[i];
252}
253
254void
255CameraInfo::set_rotation_from_string (std::string const& rot_string)
256{
257 std::stringstream ss(rot_string);
258 for (int i = 0; i < 9; ++i)
259 ss >> this->rot[i];
260}
261
262/* ---------------------------------------------------------------- */
263
264void
265CameraInfo::debug_print (void) const
266{
267 std::cout << "Extrinsic camera parameters:" << std::endl
268 << " Trans: " << math::Vec3f(this->trans) << std::endl
269 << " Rot: " << math::Vec3f(&this->rot[0]) << std::endl
270 << " " << math::Vec3f(&this->rot[3]) << std::endl
271 << " " << math::Vec3f(&this->rot[6]) << std::endl
272 << "Intrinsic camera parameters:" << std::endl
273 << " Focal Length: " << this->flen << std::endl
274 << " Principal Point: " << math::Vec2f(this->ppoint) << std::endl
275 << " Pixel aspect: " << this->paspect << std::endl
276 << " Radial distortion: " << math::Vec2f(this->dist) << std::endl
277 << std::endl;
278}
279
Matrix class for arbitrary dimensions and types.
Definition matrix.h:54
T * end(void)
Definition matrix.h:520
T * begin(void)
Definition matrix.h:506
Vector class for arbitrary dimensions and types.
Definition vector.h:87
T * end(void)
Definition vector.h:597
T * begin(void)
Definition vector.h:583
#define MVE_NAMESPACE_BEGIN
Definition defines.h:13
#define MVE_NAMESPACE_END
Definition defines.h:14
Vector< float, 2 > Vec2f
Definition vector.h:30
Matrix< T, N, N > & matrix_set_identity(Matrix< T, N, N > *mat)
Sets the given square matrix to the identity matrix.
Vector< float, 3 > Vec3f
Definition vector.h:31
Per-view camera information with various helper functions.
Definition camera.h:24
void fill_camera_translation(float *trans) const
Stores the camera translation 3-vector into array pointed to by pos.
Definition camera.cc:44
void fill_calibration(float *mat, float width, float height) const
Stores the 3x3 calibration (or projection) matrix (K-matrix in Hartley, Zisserman).
Definition camera.cc:125
void fill_world_to_cam_rot(float *mat) const
Stores the world to camera 3x3 rotation matrix in mat.
Definition camera.cc:97