8 #ifndef ALICEVISION_LINFINITY_COMPUTER_VISION_TRANSLATIONANDSTRUCTUREFrom_xi_RI_H_
9 #define ALICEVISION_LINFINITY_COMPUTER_VISION_TRANSLATIONANDSTRUCTUREFrom_xi_RI_H_
11 #include "aliceVision/numeric/numeric.hpp"
12 #include "aliceVision/linearProgramming/ISolver.hpp"
18 #pragma warning(once : 4267) // warning C4267: 'argument' : conversion from 'size_t' to 'const int', possible loss of data
33 namespace lInfinityCV {
35 using namespace linearProgramming;
49 inline void EncodeTiXi(
const Mat& M,
50 const std::vector<Mat3>& Ri,
54 std::vector<LPConstraints::eLP_SIGN>& vec_sign,
55 std::vector<double>& vec_costs,
56 std::vector<std::pair<double, double>>& vec_bounds)
59 const size_t Ncam = (size_t)M.row(3).maxCoeff() + 1;
60 const size_t N3D = (size_t)M.row(2).maxCoeff() + 1;
61 const size_t Nobs = M.cols();
63 assert(Ncam == Ri.size());
65 A.resize(5 * Nobs, 3 * (N3D + Ncam));
67 C.resize(5 * Nobs, 1);
69 vec_sign.resize(5 * Nobs + 3);
71 const size_t transStart = 0;
72 const size_t pointStart = transStart + 3 * Ncam;
73 #define TVAR(i, el) (0 + 3 * (i) + (el))
74 #define XVAR(j, el) (pointStart + 3 * (j) + (el))
77 vec_bounds = std::vector<std::pair<double, double>>(3 * (N3D + Ncam));
78 fill(vec_bounds.begin(), vec_bounds.end(), std::make_pair((
double)-1e+30, (
double)1e+30));
80 vec_bounds[0] = vec_bounds[1] = vec_bounds[2] = std::make_pair(0, 0);
84 for (
size_t k = 0; k < Nobs; ++k)
86 const size_t indexPt3D = M(2, k);
87 const size_t indexCam = M(3, k);
88 const Mat3& R = Ri[indexCam];
90 A.coeffRef(rowPos, XVAR(indexPt3D, 0)) = R(2, 0);
91 A.coeffRef(rowPos, XVAR(indexPt3D, 1)) = R(2, 1);
92 A.coeffRef(rowPos, XVAR(indexPt3D, 2)) = R(2, 2);
93 A.coeffRef(rowPos, TVAR(indexCam, 2)) = 1.0;
95 vec_sign[rowPos] = LPConstraints::LP_GREATER_OR_EQUAL;
98 const Vec2 pt = M.block<2, 1>(0, k);
99 const double u = pt(0);
100 const double v = pt(1);
107 A.coeffRef(rowPos, XVAR(indexPt3D, 0)) = R(0, 0) + (sigma - u) * R(2, 0);
108 A.coeffRef(rowPos, XVAR(indexPt3D, 1)) = R(0, 1) + (sigma - u) * R(2, 1);
109 A.coeffRef(rowPos, XVAR(indexPt3D, 2)) = R(0, 2) + (sigma - u) * R(2, 2);
110 A.coeffRef(rowPos, TVAR(indexCam, 0)) = 1.0;
111 A.coeffRef(rowPos, TVAR(indexCam, 2)) = sigma - u;
113 vec_sign[rowPos] = LPConstraints::LP_GREATER_OR_EQUAL;
116 A.coeffRef(rowPos, XVAR(indexPt3D, 0)) = R(0, 0) - (sigma + u) * R(2, 0);
117 A.coeffRef(rowPos, XVAR(indexPt3D, 1)) = R(0, 1) - (sigma + u) * R(2, 1);
118 A.coeffRef(rowPos, XVAR(indexPt3D, 2)) = R(0, 2) - (sigma + u) * R(2, 2);
119 A.coeffRef(rowPos, TVAR(indexCam, 0)) = 1.0;
120 A.coeffRef(rowPos, TVAR(indexCam, 2)) = -(sigma + u);
122 vec_sign[rowPos] = LPConstraints::LP_LESS_OR_EQUAL;
129 A.coeffRef(rowPos, XVAR(indexPt3D, 0)) = R(1, 0) + (sigma - v) * R(2, 0);
130 A.coeffRef(rowPos, XVAR(indexPt3D, 1)) = R(1, 1) + (sigma - v) * R(2, 1);
131 A.coeffRef(rowPos, XVAR(indexPt3D, 2)) = R(1, 2) + (sigma - v) * R(2, 2);
132 A.coeffRef(rowPos, TVAR(indexCam, 1)) = 1.0;
133 A.coeffRef(rowPos, TVAR(indexCam, 2)) = sigma - v;
135 vec_sign[rowPos] = LPConstraints::LP_GREATER_OR_EQUAL;
138 A.coeffRef(rowPos, XVAR(indexPt3D, 0)) = R(1, 0) - (sigma + v) * R(2, 0);
139 A.coeffRef(rowPos, XVAR(indexPt3D, 1)) = R(1, 1) - (sigma + v) * R(2, 1);
140 A.coeffRef(rowPos, XVAR(indexPt3D, 2)) = R(1, 2) - (sigma + v) * R(2, 2);
141 A.coeffRef(rowPos, TVAR(indexCam, 1)) = 1.0;
142 A.coeffRef(rowPos, TVAR(indexCam, 2)) = -(sigma + v);
144 vec_sign[rowPos] = LPConstraints::LP_LESS_OR_EQUAL;
173 constraint._constraintMat,
174 constraint._Cst_objective,
175 constraint._vec_sign,
176 constraint._vec_cost,
177 constraint._vec_bounds);
181 const size_t N3D = (size_t)_M.row(2).maxCoeff() + 1;
182 const size_t Ncam = (size_t)_M.row(3).maxCoeff() + 1;
184 constraint._nbParams = (Ncam + N3D) * 3;
189 std::vector<Mat3> _vec_Ri;
196 #endif // ALICEVISION_LINFINITY_COMPUTER_VISION_TRANSLATIONANDSTRUCTUREFrom_xi_RI_H_