1 // Copyright (c) 2009 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 // Compute a 3D position of a point from several images of it. In particular,
22 // compute the projective point X in R^4 such that x = PX.
23 //
24 // Algorithm is the standard DLT; for derivation see appendix of Keir's thesis.
25 
26 #ifndef LIBMV_MULTIVIEW_NVIEWTRIANGULATION_H
27 #define LIBMV_MULTIVIEW_NVIEWTRIANGULATION_H
28 
29 #include "libmv/base/vector.h"
30 #include "libmv/logging/logging.h"
31 #include "libmv/numeric/numeric.h"
32 
33 namespace libmv {
34 
35 // x's are 2D coordinates (x,y,1) in each image; Ps are projective cameras. The
36 // output, X, is a homogeneous four vectors.
37 template<typename T>
NViewTriangulate(const Matrix<T,2,Dynamic> & x,const vector<Matrix<T,3,4>> & Ps,Matrix<T,4,1> * X)38 void NViewTriangulate(const Matrix<T, 2, Dynamic> &x,
39                       const vector<Matrix<T, 3, 4> > &Ps,
40                       Matrix<T, 4, 1> *X) {
41   int nviews = x.cols();
42   assert(nviews == Ps.size());
43 
44   Matrix<T, Dynamic, Dynamic> design(3*nviews, 4 + nviews);
45   design.setConstant(0.0);
46   for (int i = 0; i < nviews; i++) {
47     design.template block<3, 4>(3*i, 0) = -Ps[i];
48     design(3*i + 0, 4 + i) = x(0, i);
49     design(3*i + 1, 4 + i) = x(1, i);
50     design(3*i + 2, 4 + i) = 1.0;
51   }
52   Matrix<T, Dynamic, 1>  X_and_alphas;
53   Nullspace(&design, &X_and_alphas);
54   X->resize(4);
55   *X = X_and_alphas.head(4);
56 }
57 
58 // x's are 2D coordinates (x,y,1) in each image; Ps are projective cameras. The
59 // output, X, is a homogeneous four vectors.
60 // This method uses the algebraic distance approximation.
61 // Note that this method works better when the 2D points are normalized
62 // with an isotopic normalization.
63 template<typename T>
NViewTriangulateAlgebraic(const Matrix<T,2,Dynamic> & x,const vector<Matrix<T,3,4>> & Ps,Matrix<T,4,1> * X)64 void NViewTriangulateAlgebraic(const Matrix<T, 2, Dynamic> &x,
65                                const vector<Matrix<T, 3, 4> > &Ps,
66                                Matrix<T, 4, 1> *X) {
67   int nviews = x.cols();
68   assert(nviews == Ps.size());
69 
70   Matrix<T, Dynamic, 4> design(2*nviews, 4);
71   for (int i = 0; i < nviews; i++) {
72     design.template block<2, 4>(2*i, 0) = SkewMatMinimal(x.col(i)) * Ps[i];
73   }
74   X->resize(4);
75   Nullspace(&design, X);
76 }
77 
78 }  // namespace libmv
79 
80 #endif  // LIBMV_MULTIVIEW_RESECTION_H
81