PointToPointSimilarity.cpp
Go to the documentation of this file.
00001 // kate: replace-tabs off; indent-width 4; indent-mode normal
00002 // vim: ts=4:sw=4:noexpandtab
00003 /*
00004 
00005 Copyright (c) 2010--2012,
00006 François Pomerleau and Stephane Magnenat, ASL, ETHZ, Switzerland
00007 You can contact the authors at <f dot pomerleau at gmail dot com> and
00008 <stephane at magnenat dot net>
00009 
00010 All rights reserved.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014     * Redistributions of source code must retain the above copyright
00015       notice, this list of conditions and the following disclaimer.
00016     * Redistributions in binary form must reproduce the above copyright
00017       notice, this list of conditions and the following disclaimer in the
00018       documentation and/or other materials provided with the distribution.
00019     * Neither the name of the <organization> nor the
00020       names of its contributors may be used to endorse or promote products
00021       derived from this software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00024 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00025 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00026 DISCLAIMED. IN NO EVENT SHALL ETH-ASL BE LIABLE FOR ANY
00027 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00028 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00030 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00031 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00032 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 
00036 #include "ErrorMinimizersImpl.h"
00037 #include "PointMatcherPrivate.h"
00038 #include "Eigen/SVD"
00039 
00040 using namespace Eigen;
00041 
00042 template<typename T>
00043 typename PointMatcher<T>::TransformationParameters PointToPointSimilarityErrorMinimizer<T>::compute(const ErrorElements& mPts_const)
00044 {
00045         
00046         // Copy error element to use as storage later
00047         // TODO: check that, might worth it to only copy useful parts
00048         ErrorElements mPts = mPts_const;
00049         
00050         // now minimize on kept points
00051         const int dimCount(mPts.reading.features.rows());
00052         //const int ptsCount(mPts.reading.features.cols()); //Both point clouds have now the same number of (matched) point
00053         
00054         
00055         // Compute the (weighted) mean of each point cloud
00056         //TODO: change the member weights to be a Vector
00057         const Vector w = mPts.weights.row(0);
00058         const T w_sum_inv = T(1.)/w.sum();
00059         const Vector meanReading =
00060                 (mPts.reading.features.topRows(dimCount-1).array().rowwise() * w.array().transpose()).rowwise().sum() * w_sum_inv;
00061         const Vector meanReference =
00062                 (mPts.reference.features.topRows(dimCount-1).array().rowwise() * w.array().transpose()).rowwise().sum() * w_sum_inv;
00063         
00064         
00065         // Remove the mean from the point clouds
00066         mPts.reading.features.topRows(dimCount-1).colwise() -= meanReading;
00067         mPts.reference.features.topRows(dimCount-1).colwise() -= meanReference;
00068         
00069         const T sigma = mPts.reading.features.topRows(dimCount-1).colwise().squaredNorm().cwiseProduct(w.transpose()).sum();
00070         
00071         // Singular Value Decomposition
00072         const Matrix m(mPts.reference.features.topRows(dimCount-1) * w.asDiagonal()
00073                        * mPts.reading.features.topRows(dimCount-1).transpose());
00074         const JacobiSVD<Matrix> svd(m, ComputeThinU | ComputeThinV);
00075         Matrix rotMatrix(svd.matrixU() * svd.matrixV().transpose());
00076         typedef typename JacobiSVD<Matrix>::SingularValuesType SingularValuesType;
00077         SingularValuesType singularValues = svd.singularValues();
00078         // It is possible to get a reflection instead of a rotation. In this case, we
00079         // take the second best solution, guaranteed to be a rotation. For more details,
00080         // read the tech report: "Least-Squares Rigid Motion Using SVD", Olga Sorkine
00081         // http://igl.ethz.ch/projects/ARAP/svd_rot.pdf
00082         if (rotMatrix.determinant() < 0.)
00083         {
00084                 Matrix tmpV = svd.matrixV().transpose();
00085                 tmpV.row(dimCount-2) *= -1.;
00086                 rotMatrix = svd.matrixU() * tmpV;
00087                 singularValues(dimCount-2) *= -1.;
00088         }
00089         T scale = singularValues.sum() / sigma;
00090         if (sigma < 0.0001) scale = T(1);
00091         const Vector trVector(meanReference - scale * rotMatrix * meanReading);
00092         
00093         Matrix result(Matrix::Identity(dimCount, dimCount));
00094         result.topLeftCorner(dimCount-1, dimCount-1) = scale * rotMatrix;
00095         result.topRightCorner(dimCount-1, 1) = trVector;
00096         
00097         return result;
00098 }
00099 
00100 template<typename T>
00101 T PointToPointSimilarityErrorMinimizer<T>::getResidualError(
00102         const DataPoints& filteredReading,
00103         const DataPoints& filteredReference,
00104         const OutlierWeights& outlierWeights,
00105         const Matches& matches) const
00106 {
00107         assert(matches.ids.rows() > 0);
00108         
00109         // Fetch paired points
00110         typename ErrorMinimizer::ErrorElements mPts(filteredReading, filteredReference, outlierWeights, matches);
00111         
00112         return PointToPointErrorMinimizer<T>::computeResidualError(mPts);
00113 }
00114 
00115 template<typename T>
00116 T PointToPointSimilarityErrorMinimizer<T>::getOverlap() const
00117 {
00118         //NOTE: computing overlap of 2 point clouds can be complicated due to
00119         // the sparse nature of the representation. Here is only an estimate
00120         // of the true overlap.
00121         const int nbPoints = this->lastErrorElements.reading.features.cols();
00122         const int dim = this->lastErrorElements.reading.features.rows();
00123         if(nbPoints == 0)
00124         {
00125                 throw std::runtime_error("Error, last error element empty. Error minimizer needs to be called at least once before using this method.");
00126         }
00127         
00128         if (!this->lastErrorElements.reading.descriptorExists("simpleSensorNoise"))
00129         {
00130                 LOG_INFO_STREAM("PointToPointSimilarityErrorMinimizer - warning, no sensor noise found. Using best estimate given outlier rejection instead.");
00131                 return this->getWeightedPointUsedRatio();
00132         }
00133         
00134         const BOOST_AUTO(noises, this->lastErrorElements.reading.getDescriptorViewByName("simpleSensorNoise"));
00135         
00136         const Vector dists = (this->lastErrorElements.reading.features.topRows(dim-1) - this->lastErrorElements.reference.features.topRows(dim-1)).colwise().norm();
00137         const T mean = dists.sum()/nbPoints;
00138         
00139         int count = 0;
00140         for(int i=0; i < nbPoints; i++)
00141         {
00142                 if(dists(i) < (mean + noises(0,i)))
00143                 {
00144                         count++;
00145                 }
00146         }
00147         
00148         return (T)count/(T)nbPoints;
00149 }
00150 
00151 template struct PointToPointSimilarityErrorMinimizer<float>;
00152 template struct PointToPointSimilarityErrorMinimizer<double>;


libpointmatcher
Author(s):
autogenerated on Thu Jun 20 2019 19:51:32