Turi Create  4.0
perspective_projection.hpp
1 /* Copyright © 2019 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 #include <Eigen/Core>
7 
8 namespace warp_perspective {
9 
10 Eigen::MatrixXf get_2D_to_3D(int width, int height) {
11  Eigen::MatrixXf A1(4,3);
12  A1 << 1, 0, -1*(float)width/2,
13  0, 1, -1*(float)height/2,
14  0, 0, 1,
15  0, 0, 1;
16  return A1;
17 }
18 
19 Eigen::MatrixXf get_rotation(float theta, float phi, float gamma) {
20  Eigen::Matrix4f Rx, Ry, Rz, R;
21  Rx << 1, 0, 0, 0,
22  0, std::cos(theta), -1*std::sin(theta), 0,
23  0, std::sin(theta), std::cos(theta), 0,
24  0, 0, 0, 1;
25  Ry << std::cos(phi), 0, -1*std::sin(phi), 0,
26  0, 1, 0, 0,
27  std::sin(phi), 0, std::cos(phi), 0,
28  0, 0, 0, 1;
29  Rz << std::cos(gamma), -1*std::sin(gamma), 0, 0,
30  std::sin(gamma), std::cos(gamma), 0, 0,
31  0, 0, 1, 0,
32  0, 0, 0, 1;
33  R = (Rx * Ry) * Rz;
34  return R;
35 }
36 
37 Eigen::MatrixXf get_translation(int dx, int dy, int dz) {
38  Eigen::Matrix4f T;
39  T << 1, 0, 0, dx,
40  0, 1, 0, dy,
41  0, 0, 1, dz,
42  0, 0, 0, 1;
43  return T;
44 }
45 
46 Eigen::MatrixXf get_3D_to_2D(float focal, int width, int height) {
47  Eigen::MatrixXf A2(3,4);
48  A2 << focal, 0, (float)width/2, 0,
49  0, focal, (float)height/2, 0,
50  0, 0, 1, 0;
51  return A2;
52 }
53 
54 Eigen::Matrix<float, 3, 3> get_transformation_matrix(
55  int width, int height,
56  float theta, float phi, float gamma,
57  int dx, int dy, int dz,
58  float focal) {
59  Eigen::MatrixXf A1 = get_2D_to_3D(width, height);
60  Eigen::Matrix4f R = get_rotation(theta, phi, gamma);
61  Eigen::Matrix4f T = get_translation(dx, dy, dz);
62  Eigen::MatrixXf A2 = get_3D_to_2D(focal, width, height);
63  return (A2 * (T * (R * A1)));
64 }
65 
66 }
double gamma(const double alpha=double(1))
Definition: random.hpp:438