|
| 1 | +/* Copyright (c) 2022, Gonzalo Ferrer |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + * |
| 15 | + * |
| 16 | + * arun.cpp |
| 17 | + * |
| 18 | + * Created on: July 12, 2023 |
| 19 | + * Author: Gonzalo Ferrer |
| 20 | + * g.ferrer@skoltech.ru |
| 21 | + * Mobile Robotics Lab, Skoltech |
| 22 | + */ |
| 23 | + |
| 24 | +#include <Eigen/LU> |
| 25 | +#include <Eigen/SVD> |
| 26 | + |
| 27 | +#include <memory> |
| 28 | +#include <iostream> |
| 29 | +#include "mrob/pc_registration.hpp" |
| 30 | +#include <cmath> |
| 31 | + |
| 32 | +using namespace mrob; |
| 33 | +using namespace Eigen; |
| 34 | + |
| 35 | +int PCRegistration::scaled_arun(MatRefConst X, MatRefConst Y, Mat4 &S) |
| 36 | +{ |
| 37 | + assert(X.cols() == 3 && "PCRegistration::Arun: Incorrect sizing, we expect Nx3"); |
| 38 | + assert(X.rows() >= 3 && "PCRegistration::Arun: Incorrect sizing, we expect at least 3 correspondences (not aligned)"); |
| 39 | + assert(Y.rows() == X.rows() && "PCRegistration::Arun: Same number of correspondences"); |
| 40 | + uint_t N = X.rows(); |
| 41 | + /** Algorithm: |
| 42 | + * 1) calculate centroids cx = sum x_i. cy = sum y_i |
| 43 | + * 2) calculate dispersion from centroids qx = x_i - cx |
| 44 | + * 3) calculate matrix H = sum qx_i * qy_i^T |
| 45 | + * 4) svd decomposition: H = U*D*V' |
| 46 | + * 4.5) look for co-linear solutions, that is 2 of the 3 singular values are equal |
| 47 | + * 5) Calculate the rotation solution R = V*U' |
| 48 | + * 5.5) check for correct solution (det = +1) or reflection (det = -1) |
| 49 | + * step 5.5 is actually unnecessary IF applying Umeyama technique |
| 50 | + * 6) NEW calcualte scale as |
| 51 | + * s = |
| 52 | + * 7) NEW calculate translation as: t = cy - s R * cx |
| 53 | + */ |
| 54 | + // We have already asserted in base_T that they are 3xN matrices. (and the same length). |
| 55 | + |
| 56 | + //std::cout << "X: \n" << X << "\nY:\n" << Y << std::endl; |
| 57 | + // 1) calculate centroids cx = E{x_i}. cy = E{y_i} |
| 58 | + //More efficient than creating a matrix of ones when on Release mode (not is Debug mode) |
| 59 | + Mat13 cxm = X.colwise().sum(); |
| 60 | + cxm /= (double)N; |
| 61 | + Mat13 cym = Y.colwise().sum(); |
| 62 | + cym /= (double)N; |
| 63 | + |
| 64 | + // 2) calculate dispersion from centroids qx = x_i - cx |
| 65 | + MatX qx = X.rowwise() - cxm; |
| 66 | + MatX qy = Y.rowwise() - cym; |
| 67 | + |
| 68 | + |
| 69 | + // 3) calculate matrix H = sum qx_i * qy_i^T (noting that we are obtaingin row vectors) |
| 70 | + Mat3 H = qx.transpose() * qy; |
| 71 | + |
| 72 | + // 4) svd decomposition: H = U*D*V' |
| 73 | + JacobiSVD<Matrix3d> SVD(H, ComputeFullU | ComputeFullV);//Full matrices indicate Square matrices |
| 74 | + |
| 75 | + //test: prints results so far |
| 76 | + /*std::cout << "Checking matrix SVD: \n" << SVD.singularValues() << |
| 77 | + ",\n U = " << SVD.matrixU() << |
| 78 | + ",\n V = " << SVD.matrixV() << std::endl;*/ |
| 79 | + |
| 80 | + |
| 81 | + // 4.5) look for co-linear solutions, that is 2 of the 3 singular values are equal |
| 82 | + double l_prev = SVD.singularValues()(0), l; |
| 83 | + for(int i =1; i < 3; ++i) |
| 84 | + { |
| 85 | + l = SVD.singularValues()(i); |
| 86 | + if (fabs(l - l_prev) < 1e-6) |
| 87 | + |
| 88 | + return 0; //they are co-linear, there exist infinite transformations |
| 89 | + else |
| 90 | + l_prev = l;//this works because we assume that they singular values are ordered. |
| 91 | + } |
| 92 | + |
| 93 | + // 5) Calculate the rotation solution R = V*U' |
| 94 | + Mat3 R = SVD.matrixV() * SVD.matrixU().transpose(); |
| 95 | + |
| 96 | + // 5.5) check for correct solution (det = +1) or reflection (det = -1) |
| 97 | + // that is, solve the problem for co-planar set of points and centroid, when is l1 > l2 > l3 = 0 |
| 98 | + // Since H = D1*u1*v1' + D2*u2*v2' + D3*u3*v3', and D3 = 0, we can swap signs in V |
| 99 | + // such as Vp = [v1,v2,-v3] and the solution is still minimal, but we want a valid rotation R \in SO(3) |
| 100 | + if (R.determinant() < 0.0 ) |
| 101 | + { |
| 102 | + Mat3 Vn; |
| 103 | + Vn << SVD.matrixV().topLeftCorner<3,2>(), -SVD.matrixV().topRightCorner<3,1>(); |
| 104 | + R << Vn * SVD.matrixU().transpose(); |
| 105 | + //std::cout << "R value = " << R << std::endl; |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + // 6) calculate scale |
| 110 | + matData_t scale; |
| 111 | + matData_t qx2_sum = qx.col(0).squaredNorm() + qx.col(1).squaredNorm() + qx.col(2).squaredNorm(); |
| 112 | + matData_t qy2_sum = qy.col(0).squaredNorm() + qy.col(1).squaredNorm() + qy.col(2).squaredNorm(); |
| 113 | + scale = std::sqrt(qy2_sum / qx2_sum); |
| 114 | + |
| 115 | + // 7) calculate translation as: t = cy - scale *R * cx |
| 116 | + Mat31 t = cym.transpose() - scale*R*cxm.transpose(); |
| 117 | + //std::cout << "t = " << t << std::endl; |
| 118 | + |
| 119 | + // 7) return result |
| 120 | + S << scale * R, t, |
| 121 | + 0,0,0,1; |
| 122 | + |
| 123 | + return 1; |
| 124 | +} |
0 commit comments