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 #ifndef COLMAP_SRC_BASE_TRIANGULATION_H_
33 #define COLMAP_SRC_BASE_TRIANGULATION_H_
34 
35 #include <vector>
36 
37 #include <Eigen/Core>
38 
39 #include "base/camera.h"
40 #include "util/alignment.h"
41 #include "util/math.h"
42 #include "util/types.h"
43 
44 namespace colmap {
45 
46 // Triangulate 3D point from corresponding image point observations.
47 //
48 // Implementation of the direct linear transform triangulation method in
49 //   R. Hartley and A. Zisserman, Multiple View Geometry in Computer Vision,
50 //   Cambridge Univ. Press, 2003.
51 //
52 // @param proj_matrix1   Projection matrix of the first image as 3x4 matrix.
53 // @param proj_matrix2   Projection matrix of the second image as 3x4 matrix.
54 // @param point1         Corresponding 2D point in first image.
55 // @param point2         Corresponding 2D point in second image.
56 //
57 // @return               Triangulated 3D point.
58 Eigen::Vector3d TriangulatePoint(const Eigen::Matrix3x4d& proj_matrix1,
59                                  const Eigen::Matrix3x4d& proj_matrix2,
60                                  const Eigen::Vector2d& point1,
61                                  const Eigen::Vector2d& point2);
62 
63 // Triangulate multiple 3D points from multiple image correspondences.
64 std::vector<Eigen::Vector3d> TriangulatePoints(
65     const Eigen::Matrix3x4d& proj_matrix1,
66     const Eigen::Matrix3x4d& proj_matrix2,
67     const std::vector<Eigen::Vector2d>& points1,
68     const std::vector<Eigen::Vector2d>& points2);
69 
70 // Triangulate point from multiple views minimizing the L2 error.
71 //
72 // @param proj_matrices       Projection matrices of multi-view observations.
73 // @param points              Image observations of multi-view observations.
74 //
75 // @return                    Estimated 3D point.
76 Eigen::Vector3d TriangulateMultiViewPoint(
77     const std::vector<Eigen::Matrix3x4d>& proj_matrices,
78     const std::vector<Eigen::Vector2d>& points);
79 
80 // Triangulate optimal 3D point from corresponding image point observations by
81 // finding the optimal image observations.
82 //
83 // Note that camera poses should be very good in order for this method to yield
84 // good results. Otherwise just use `TriangulatePoint`.
85 //
86 // Implementation of the method described in
87 //   P. Lindstrom, "Triangulation Made Easy," IEEE Computer Vision and Pattern
88 //   Recognition 2010, pp. 1554-1561, June 2010.
89 //
90 // @param proj_matrix1   Projection matrix of the first image as 3x4 matrix.
91 // @param proj_matrix2   Projection matrix of the second image as 3x4 matrix.
92 // @param point1         Corresponding 2D point in first image.
93 // @param point2         Corresponding 2D point in second image.
94 //
95 // @return               Triangulated optimal 3D point.
96 Eigen::Vector3d TriangulateOptimalPoint(const Eigen::Matrix3x4d& proj_matrix1,
97                                         const Eigen::Matrix3x4d& proj_matrix2,
98                                         const Eigen::Vector2d& point1,
99                                         const Eigen::Vector2d& point2);
100 
101 // Triangulate multiple optimal 3D points from multiple image correspondences.
102 std::vector<Eigen::Vector3d> TriangulateOptimalPoints(
103     const Eigen::Matrix3x4d& proj_matrix1,
104     const Eigen::Matrix3x4d& proj_matrix2,
105     const std::vector<Eigen::Vector2d>& points1,
106     const std::vector<Eigen::Vector2d>& points2);
107 
108 // Calculate angle in radians between the two rays of a triangulated point.
109 double CalculateTriangulationAngle(const Eigen::Vector3d& proj_center1,
110                                    const Eigen::Vector3d& proj_center2,
111                                    const Eigen::Vector3d& point3D);
112 std::vector<double> CalculateTriangulationAngles(
113     const Eigen::Vector3d& proj_center1, const Eigen::Vector3d& proj_center2,
114     const std::vector<Eigen::Vector3d>& points3D);
115 
116 }  // namespace colmap
117 
118 #endif  // COLMAP_SRC_BASE_TRIANGULATION_H_
119