Blender V4.3
projection_test.cc
Go to the documentation of this file.
1// Copyright (c) 2007, 2008 libmv authors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to
5// deal in the Software without restriction, including without limitation the
6// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7// sell copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19// IN THE SOFTWARE.
20
21#include <iostream>
22
25#include "testing/testing.h"
26
27namespace {
28using namespace libmv;
29
31 Mat3 K, Kp;
32 // clang-format off
33 K << 10, 1, 30,
34 0, 20, 40,
35 0, 0, 1;
36 // clang-format on
37
38 Mat3 R, Rp;
39 // clang-format off
40 R << 1, 0, 0,
41 0, 1, 0,
42 0, 0, 1;
43 // clang-format on
44
45 Vec3 t, tp;
46 t << 1, 2, 3;
47
48 Mat34 P;
49 P_From_KRt(K, R, t, &P);
50 KRt_From_P(P, &Kp, &Rp, &tp);
51
52 EXPECT_MATRIX_NEAR(K, Kp, 1e-8);
53 EXPECT_MATRIX_NEAR(R, Rp, 1e-8);
54 EXPECT_MATRIX_NEAR(t, tp, 1e-8);
55
56 // TODO(keir): Change the code to ensure det(R) == 1, which is not currently
57 // the case. Also add a test for that here.
58}
59
60Vec4 GetRandomPoint() {
61 Vec4 X;
62 X.setRandom();
63 X(3) = 1;
64 return X;
65}
66
68 Mat34 P;
69 // clang-format off
70 P << 1, 0, 0, 0,
71 0, 1, 0, 0,
72 0, 0, 1, 0;
73 // clang-format on
74
75 Vec4 X_front = GetRandomPoint();
76 Vec4 X_back = GetRandomPoint();
77 X_front(2) = 10; /* Any point in the positive Z direction
78 * where Z > 1 is in front of the camera. */
79 X_back(2) = -10; /* Any point in the negative Z direction
80 * is behind the camera. */
81
82 bool res_front = isInFrontOfCamera(P, X_front);
83 bool res_back = isInFrontOfCamera(P, X_back);
84
85 EXPECT_TRUE(res_front);
86 EXPECT_FALSE(res_back);
87}
88
89TEST(AutoCalibration, ProjectionShiftPrincipalPoint) {
90 Mat34 P1, P2;
91 // clang-format off
92 P1 << 1, 0, 0, 0,
93 0, 1, 0, 0,
94 0, 0, 1, 0;
95 P2 << 1, 0, 3, 0,
96 0, 1, 4, 0,
97 0, 0, 1, 0;
98 // clang-format on
99 Mat34 P1_computed, P2_computed;
100 ProjectionShiftPrincipalPoint(P1, Vec2(0, 0), Vec2(3, 4), &P2_computed);
101 ProjectionShiftPrincipalPoint(P2, Vec2(3, 4), Vec2(0, 0), &P1_computed);
102
103 EXPECT_MATRIX_EQ(P1, P1_computed);
104 EXPECT_MATRIX_EQ(P2, P2_computed);
105}
106
107TEST(AutoCalibration, ProjectionChangeAspectRatio) {
108 Mat34 P1, P2;
109 // clang-format off
110 P1 << 1, 0, 3, 0,
111 0, 1, 4, 0,
112 0, 0, 1, 0;
113 P2 << 1, 0, 3, 0,
114 0, 2, 4, 0,
115 0, 0, 1, 0;
116 // clang-format on
117 Mat34 P1_computed, P2_computed;
118 ProjectionChangeAspectRatio(P1, Vec2(3, 4), 1, 2, &P2_computed);
119 ProjectionChangeAspectRatio(P2, Vec2(3, 4), 2, 1, &P1_computed);
120
121 EXPECT_MATRIX_EQ(P1, P1_computed);
122 EXPECT_MATRIX_EQ(P2, P2_computed);
123}
124
125} // namespace
#define K(key)
#define X
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
float[3][3] Mat3
Definition gpu_matrix.cc:28
#define Projection
Definition gpu_matrix.cc:53
#define R
Eigen::Vector4d Vec4
Definition numeric.h:107
Eigen::Vector2d Vec2
Definition numeric.h:105
TEST(PolynomialCameraIntrinsics2, ApplyOnFocalCenter)
void ProjectionShiftPrincipalPoint(const Mat34 &P, const Vec2 &principal_point, const Vec2 &principal_point_new, Mat34 *P_new)
void KRt_From_P(const Mat34 &P, Mat3 *Kp, Mat3 *Rp, Vec3 *tp)
Definition projection.cc:32
Eigen::Matrix< double, 3, 4 > Mat34
Definition numeric.h:73
Eigen::Vector3d Vec3
Definition numeric.h:106
void ProjectionChangeAspectRatio(const Mat34 &P, const Vec2 &principal_point, double aspect_ratio, double aspect_ratio_new, Mat34 *P_new)
void P_From_KRt(const Mat3 &K, const Mat3 &R, const Vec3 &t, Mat34 *P)
Definition projection.cc:26
bool isInFrontOfCamera(const Mat34 &P, const Vec4 &X)