double sampleEuclideanTransform(std::vector<Vector3d> const& left, std::vector<Vector3d> const& right, int const minSample[3], double inlierThresholdL, double inlierThresholdR, Matrix3x3d& R, Vector3d& T, std::vector<int> &inliers) { inliers.clear(); int const N = left.size(); if (N < 3) throwV3DErrorHere("computeRobustEuclideanTransform(): at least 3 point correspondences required."); vector<Vector3d> ptsLeftTrans(N), ptsRightTrans(N); vector<Vector3d> left_pts(3), right_pts(3); int const j0 = minSample[0]; int const j1 = minSample[1]; int const j2 = minSample[2]; left_pts[0] = left[j0]; left_pts[1] = left[j1]; left_pts[2] = left[j2]; right_pts[0] = right[j0]; right_pts[1] = right[j1]; right_pts[2] = right[j2]; Matrix3x3d R0, R0t; Vector3d T0; getEuclideanTransformation(left_pts, right_pts, R0, T0); R0t = R0.transposed(); for (int i = 0; i < N; ++i) ptsLeftTrans[i] = (R0 * left[i] + T0); for (int i = 0; i < N; ++i) ptsRightTrans[i] = R0t * (right[i] - T0); double score = 0; for (int i = 0; i < N; ++i) { double const distL = distance_L2(left[i], ptsRightTrans[i]); double const distR = distance_L2(right[i], ptsLeftTrans[i]); score += std::min(distL, inlierThresholdL); score += std::min(distR, inlierThresholdR); if (distL < inlierThresholdL && distR < inlierThresholdR) inliers.push_back(i); } // end for (i) R = R0; T = T0; return score; } // end sampleEuclideanTransform()
void checkGLErrors(char const * location, ostream& os) { GLuint errnum; char const * errstr; bool hasError = false; while ((errnum = glGetError())) { errstr = reinterpret_cast<const char *>(gluErrorString(errnum)); if (errstr) os << errstr; else os << "Error " << errnum; os << " at " << location << endl; #ifdef WIN32 break; #endif } if (hasError) throwV3DErrorHere(""); }
void computeConsistentRotations(int const nViews, std::vector<Matrix3x3d> const& relativeRotations, std::vector<std::pair<int, int> > const& viewPairs, std::vector<Matrix3x3d>& rotations, int method) { #if !defined(V3DLIB_ENABLE_ARPACK) if (method == V3D_CONSISTENT_ROTATION_METHOD_SPARSE_EIG) method = V3D_CONSISTENT_ROTATION_METHOD_EIG_ATA; #endif int const nRelPoses = relativeRotations.size(); rotations.resize(nViews); switch (method) { case V3D_CONSISTENT_ROTATION_METHOD_SVD: { Matrix<double> A(3*nRelPoses, 3*nViews, 0.0); Matrix3x3d I; makeIdentityMatrix(I); scaleMatrixIP(-1.0, I); for (int i = 0; i < nRelPoses; ++i) { int const view1 = viewPairs[i].first; int const view2 = viewPairs[i].second; Matrix3x3d const& Rrel = relativeRotations[i]; copyMatrixSlice(Rrel, 0, 0, 3, 3, A, 3*i, 3*view1); copyMatrixSlice(I, 0, 0, 3, 3, A, 3*i, 3*view2); } // end for (i) SVD<double> svd(A); int const startColumn = A.num_cols()-3; // last columns of right sing. vec for SVD Matrix<double> const& V = svd.getV(); for (int i = 0; i < nViews; ++i) { copyMatrixSlice(V, 3*i, startColumn, 3, 3, rotations[i], 0, 0); enforceRotationMatrix(rotations[i]); } break; } case V3D_CONSISTENT_ROTATION_METHOD_SVD_ATA: case V3D_CONSISTENT_ROTATION_METHOD_EIG_ATA: case V3D_CONSISTENT_ROTATION_METHOD_SPARSE_EIG: { vector<pair<int, int> > nzA; vector<double> valsA; nzA.reserve(12*nRelPoses); valsA.reserve(12*nRelPoses); for (int i = 0; i < nRelPoses; ++i) { int const view1 = viewPairs[i].first; int const view2 = viewPairs[i].second; Matrix3x3d const& Rrel = relativeRotations[i]; nzA.push_back(make_pair(3*i+0, 3*view1+0)); valsA.push_back(Rrel[0][0]); nzA.push_back(make_pair(3*i+0, 3*view1+1)); valsA.push_back(Rrel[0][1]); nzA.push_back(make_pair(3*i+0, 3*view1+2)); valsA.push_back(Rrel[0][2]); nzA.push_back(make_pair(3*i+1, 3*view1+0)); valsA.push_back(Rrel[1][0]); nzA.push_back(make_pair(3*i+1, 3*view1+1)); valsA.push_back(Rrel[1][1]); nzA.push_back(make_pair(3*i+1, 3*view1+2)); valsA.push_back(Rrel[1][2]); nzA.push_back(make_pair(3*i+2, 3*view1+0)); valsA.push_back(Rrel[2][0]); nzA.push_back(make_pair(3*i+2, 3*view1+1)); valsA.push_back(Rrel[2][1]); nzA.push_back(make_pair(3*i+2, 3*view1+2)); valsA.push_back(Rrel[2][2]); nzA.push_back(make_pair(3*i+0, 3*view2+0)); valsA.push_back(-1.0); nzA.push_back(make_pair(3*i+1, 3*view2+1)); valsA.push_back(-1.0); nzA.push_back(make_pair(3*i+2, 3*view2+2)); valsA.push_back(-1.0); } // end for (i) CCS_Matrix<double> A(3*nRelPoses, 3*nViews, nzA, valsA); if (method == V3D_CONSISTENT_ROTATION_METHOD_SPARSE_EIG) { #if defined(V3DLIB_ENABLE_ARPACK) Vector<double> sigma; Matrix<double> V; SparseSymmetricEigConfig cfg; cfg.maxArnoldiIterations = 100000; computeSparseSVD(A, V3D_ARPACK_SMALLEST_MAGNITUDE_EIGENVALUES, 3, sigma, V, cfg); //computeSparseSVD(A, V3D_ARPACK_SMALLEST_EIGENVALUES, 3, sigma, V, cfg); for (int i = 0; i < nViews; ++i) { copyMatrixSlice(V, 3*i, 0, 3, 1, rotations[i], 0, 2); copyMatrixSlice(V, 3*i, 1, 3, 1, rotations[i], 0, 1); copyMatrixSlice(V, 3*i, 2, 3, 1, rotations[i], 0, 0); } #endif } else { Matrix<double> AtA(3*nViews, 3*nViews); multiply_At_A_SparseDense(A, AtA); if (method == V3D_CONSISTENT_ROTATION_METHOD_SVD_ATA) { SVD<double> svd(AtA); int const startColumn = A.num_cols()-3; // last columns of right sing. vec for SVD Matrix<double> const& V = svd.getV(); for (int i = 0; i < nViews; ++i) copyMatrixSlice(V, 3*i, startColumn, 3, 3, rotations[i], 0, 0); } else { Eigenvalue<double> svd(AtA); int const startColumn = 0; // first columns of eigenvector matrix Matrix<double> const& V = svd.getV(); for (int i = 0; i < nViews; ++i) copyMatrixSlice(V, 3*i, startColumn, 3, 3, rotations[i], 0, 0); } // end if } // end if break; } default: throwV3DErrorHere("Unknown method argument for computeConsistentRotations()."); } // end switch for (int i = 0; i < nViews; ++i) enforceRotationMatrix(rotations[i]); // Remove gauge freedom by setting R[0] = I. Matrix3x3d const R0t = rotations[0].transposed(); for (int i = 0; i < nViews; ++i) rotations[i] = rotations[i] * R0t; // Note: it seems, that either all Rs have det(R)=1 or all have det(R)=-1. // Since we remove the gauge freedem by multiplying all rotations with R_0^t, // we always end up with det(R)=1 and any code to enforce a positive determinant // is not necessary. } // end computeConsistentRotations()