ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
triangulation.cc
Go to the documentation of this file.
1 // Copyright (c) 2018, ETH Zurich and UNC Chapel Hill.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 //
14 // * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of
15 // its contributors may be used to endorse or promote products derived
16 // from this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Johannes L. Schoenberger (jsch-at-demuc-dot-de)
31 
32 #include "base/triangulation.h"
33 
34 #include "base/essential_matrix.h"
35 #include "base/pose.h"
36 
37 namespace colmap {
38 
39 Eigen::Vector3d TriangulatePoint(const Eigen::Matrix3x4d& proj_matrix1,
40  const Eigen::Matrix3x4d& proj_matrix2,
41  const Eigen::Vector2d& point1,
42  const Eigen::Vector2d& point2) {
43  Eigen::Matrix4d A;
44 
45  A.row(0) = point1(0) * proj_matrix1.row(2) - proj_matrix1.row(0);
46  A.row(1) = point1(1) * proj_matrix1.row(2) - proj_matrix1.row(1);
47  A.row(2) = point2(0) * proj_matrix2.row(2) - proj_matrix2.row(0);
48  A.row(3) = point2(1) * proj_matrix2.row(2) - proj_matrix2.row(1);
49 
50  Eigen::JacobiSVD<Eigen::Matrix4d> svd(A, Eigen::ComputeFullV);
51 
52  return svd.matrixV().col(3).hnormalized();
53 }
54 
55 std::vector<Eigen::Vector3d> TriangulatePoints(
56  const Eigen::Matrix3x4d& proj_matrix1,
57  const Eigen::Matrix3x4d& proj_matrix2,
58  const std::vector<Eigen::Vector2d>& points1,
59  const std::vector<Eigen::Vector2d>& points2) {
60  CHECK_EQ(points1.size(), points2.size());
61 
62  std::vector<Eigen::Vector3d> points3D(points1.size());
63 
64  for (size_t i = 0; i < points3D.size(); ++i) {
65  points3D[i] =
66  TriangulatePoint(proj_matrix1, proj_matrix2, points1[i], points2[i]);
67  }
68 
69  return points3D;
70 }
71 
72 Eigen::Vector3d TriangulateMultiViewPoint(
73  const std::vector<Eigen::Matrix3x4d>& proj_matrices,
74  const std::vector<Eigen::Vector2d>& points) {
75  CHECK_EQ(proj_matrices.size(), points.size());
76 
77  Eigen::Matrix4d A = Eigen::Matrix4d::Zero();
78 
79  for (size_t i = 0; i < points.size(); i++) {
80  const Eigen::Vector3d point = points[i].homogeneous().normalized();
81  const Eigen::Matrix3x4d term =
82  proj_matrices[i] - point * point.transpose() * proj_matrices[i];
83  A += term.transpose() * term;
84  }
85 
86  Eigen::SelfAdjointEigenSolver<Eigen::Matrix4d> eigen_solver(A);
87 
88  return eigen_solver.eigenvectors().col(0).hnormalized();
89 }
90 
91 Eigen::Vector3d TriangulateOptimalPoint(const Eigen::Matrix3x4d& proj_matrix1,
92  const Eigen::Matrix3x4d& proj_matrix2,
93  const Eigen::Vector2d& point1,
94  const Eigen::Vector2d& point2) {
95  const Eigen::Matrix3d E =
96  EssentialMatrixFromAbsolutePoses(proj_matrix1, proj_matrix2);
97 
98  Eigen::Vector2d optimal_point1;
99  Eigen::Vector2d optimal_point2;
100  FindOptimalImageObservations(E, point1, point2, &optimal_point1,
101  &optimal_point2);
102 
103  return TriangulatePoint(proj_matrix1, proj_matrix2, optimal_point1,
104  optimal_point2);
105 }
106 
107 std::vector<Eigen::Vector3d> TriangulateOptimalPoints(
108  const Eigen::Matrix3x4d& proj_matrix1,
109  const Eigen::Matrix3x4d& proj_matrix2,
110  const std::vector<Eigen::Vector2d>& points1,
111  const std::vector<Eigen::Vector2d>& points2) {
112  std::vector<Eigen::Vector3d> points3D(points1.size());
113 
114  for (size_t i = 0; i < points3D.size(); ++i) {
115  points3D[i] =
116  TriangulatePoint(proj_matrix1, proj_matrix2, points1[i], points2[i]);
117  }
118 
119  return points3D;
120 }
121 
122 double CalculateTriangulationAngle(const Eigen::Vector3d& proj_center1,
123  const Eigen::Vector3d& proj_center2,
124  const Eigen::Vector3d& point3D) {
125  const double baseline_length_squared =
126  (proj_center1 - proj_center2).squaredNorm();
127 
128  const double ray_length_squared1 = (point3D - proj_center1).squaredNorm();
129  const double ray_length_squared2 = (point3D - proj_center2).squaredNorm();
130 
131  // Using "law of cosines" to compute the enclosing angle between rays.
132  const double denominator =
133  2.0 * std::sqrt(ray_length_squared1 * ray_length_squared2);
134  if (denominator == 0.0) {
135  return 0.0;
136  }
137  const double nominator =
138  ray_length_squared1 + ray_length_squared2 - baseline_length_squared;
139  const double angle = std::abs(std::acos(nominator / denominator));
140 
141  // Triangulation is unstable for acute angles (far away points) and
142  // obtuse angles (close points), so always compute the minimum angle
143  // between the two intersecting rays.
144  return std::min(angle, M_PI - angle);
145 }
146 
147 std::vector<double> CalculateTriangulationAngles(
148  const Eigen::Vector3d& proj_center1, const Eigen::Vector3d& proj_center2,
149  const std::vector<Eigen::Vector3d>& points3D) {
150  // Baseline length between camera centers.
151  const double baseline_length_squared =
152  (proj_center1 - proj_center2).squaredNorm();
153 
154  std::vector<double> angles(points3D.size());
155 
156  for (size_t i = 0; i < points3D.size(); ++i) {
157  // Ray lengths from cameras to point.
158  const double ray_length_squared1 =
159  (points3D[i] - proj_center1).squaredNorm();
160  const double ray_length_squared2 =
161  (points3D[i] - proj_center2).squaredNorm();
162 
163  // Using "law of cosines" to compute the enclosing angle between rays.
164  const double denominator =
165  2.0 * std::sqrt(ray_length_squared1 * ray_length_squared2);
166  if (denominator == 0.0) {
167  angles[i] = 0.0;
168  continue;
169  }
170  const double nominator =
171  ray_length_squared1 + ray_length_squared2 - baseline_length_squared;
172  const double angle = std::abs(std::acos(nominator / denominator));
173 
174  // Triangulation is unstable for acute angles (far away points) and
175  // obtuse angles (close points), so always compute the minimum angle
176  // between the two intersecting rays.
177  angles[i] = std::min(angle, M_PI - angle);
178  }
179 
180  return angles;
181 }
182 
183 } // namespace colmap
constexpr double M_PI
Pi.
Definition: CVConst.h:19
int points
Matrix< double, 3, 4 > Matrix3x4d
Definition: types.h:39
Eigen::Vector3d TriangulateOptimalPoint(const Eigen::Matrix3x4d &proj_matrix1, const Eigen::Matrix3x4d &proj_matrix2, const Eigen::Vector2d &point1, const Eigen::Vector2d &point2)
std::vector< Eigen::Vector3d > TriangulateOptimalPoints(const Eigen::Matrix3x4d &proj_matrix1, const Eigen::Matrix3x4d &proj_matrix2, const std::vector< Eigen::Vector2d > &points1, const std::vector< Eigen::Vector2d > &points2)
Eigen::Matrix3d EssentialMatrixFromAbsolutePoses(const Eigen::Matrix3x4d &proj_matrix1, const Eigen::Matrix3x4d &proj_matrix2)
void FindOptimalImageObservations(const Eigen::Matrix3d &E, const Eigen::Vector2d &point1, const Eigen::Vector2d &point2, Eigen::Vector2d *optimal_point1, Eigen::Vector2d *optimal_point2)
std::vector< double > CalculateTriangulationAngles(const Eigen::Vector3d &proj_center1, const Eigen::Vector3d &proj_center2, const std::vector< Eigen::Vector3d > &points3D)
Eigen::Vector3d TriangulateMultiViewPoint(const std::vector< Eigen::Matrix3x4d > &proj_matrices, const std::vector< Eigen::Vector2d > &points)
Eigen::Vector3d TriangulatePoint(const Eigen::Matrix3x4d &proj_matrix1, const Eigen::Matrix3x4d &proj_matrix2, const Eigen::Vector2d &point1, const Eigen::Vector2d &point2)
std::vector< Eigen::Vector3d > TriangulatePoints(const Eigen::Matrix3x4d &proj_matrix1, const Eigen::Matrix3x4d &proj_matrix2, const std::vector< Eigen::Vector2d > &points1, const std::vector< Eigen::Vector2d > &points2)
double CalculateTriangulationAngle(const Eigen::Vector3d &proj_center1, const Eigen::Vector3d &proj_center2, const Eigen::Vector3d &point3D)