PointToPoint.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 PointToPointErrorMinimizer<T>::PointToPointErrorMinimizer() :
00044 PointMatcher<T>::ErrorMinimizer("PointToPointErrorMinimizer",
00045                                                                                                                                 ParametersDoc(),
00046                                                                                                                                 Parameters()) {}
00047 
00048 template<typename T>
00049 PointToPointErrorMinimizer<T>::PointToPointErrorMinimizer(const std::string& className, const ParametersDoc paramsDoc, const Parameters& params):
00050         ErrorMinimizer(className, paramsDoc, params)
00051 {
00052 }
00053 
00054 template<typename T>
00055 typename PointMatcher<T>::TransformationParameters PointToPointErrorMinimizer<T>::compute(const ErrorElements& mPts_const)
00056 {
00057         ErrorElements mPts = mPts_const;
00058         return compute_in_place(mPts);
00059 }
00060 
00061 template<typename T>
00062 typename PointMatcher<T>::TransformationParameters PointToPointErrorMinimizer<T>::compute_in_place(ErrorElements& mPts) {
00063         const int dimCount(mPts.reading.features.rows());
00064         //const int ptsCount(mPts.reading.features.cols()); //Both point clouds have now the same number of (matched) point
00065         
00066         
00067         const Vector w = mPts.weights.row(0);
00068         const T w_sum_inv = T(1.)/w.sum();
00069         const Vector meanReading =
00070                 (mPts.reading.features.topRows(dimCount-1).array().rowwise() * w.array().transpose()).rowwise().sum() * w_sum_inv;
00071         const Vector meanReference =
00072                 (mPts.reference.features.topRows(dimCount-1).array().rowwise() * w.array().transpose()).rowwise().sum() * w_sum_inv;
00073         
00074         
00075         // Remove the mean from the point clouds
00076         mPts.reading.features.topRows(dimCount-1).colwise() -= meanReading;
00077         mPts.reference.features.topRows(dimCount-1).colwise() -= meanReference;
00078         
00079         // Singular Value Decomposition
00080         const Matrix m(mPts.reference.features.topRows(dimCount-1) * w.asDiagonal()
00081                        * mPts.reading.features.topRows(dimCount-1).transpose());
00082         const JacobiSVD<Matrix> svd(m, ComputeThinU | ComputeThinV);
00083         Matrix rotMatrix(svd.matrixU() * svd.matrixV().transpose());
00084         // It is possible to get a reflection instead of a rotation. In this case, we
00085         // take the second best solution, guaranteed to be a rotation. For more details,
00086         // read the tech report: "Least-Squares Rigid Motion Using SVD", Olga Sorkine
00087         // http://igl.ethz.ch/projects/ARAP/svd_rot.pdf
00088         if (rotMatrix.determinant() < 0.)
00089         {
00090                 Matrix tmpV = svd.matrixV().transpose();
00091                 tmpV.row(dimCount-2) *= -1.;
00092                 rotMatrix = svd.matrixU() * tmpV;
00093         }
00094         const Vector trVector(meanReference - rotMatrix * meanReading);
00095         
00096         Matrix result(Matrix::Identity(dimCount, dimCount));
00097         result.topLeftCorner(dimCount-1, dimCount-1) = rotMatrix;
00098         result.topRightCorner(dimCount-1, 1) = trVector;
00099         
00100         return result;
00101 }
00102 
00103 template<typename T>
00104 T PointToPointErrorMinimizer<T>::getResidualError(
00105         const DataPoints& filteredReading,
00106         const DataPoints& filteredReference,
00107         const OutlierWeights& outlierWeights,
00108         const Matches& matches) const
00109 {
00110         assert(matches.ids.rows() > 0);
00111         
00112         // Fetch paired points
00113         typename ErrorMinimizer::ErrorElements mPts(filteredReading, filteredReference, outlierWeights, matches);
00114         
00115         return PointToPointErrorMinimizer::computeResidualError(mPts);
00116 }
00117 
00118 template<typename T>
00119 T PointToPointErrorMinimizer<T>::getOverlap() const
00120 {
00121         //NOTE: computing overlap of 2 point clouds can be complicated due to
00122         // the sparse nature of the representation. Here is only an estimate
00123         // of the true overlap.
00124         const int nbPoints = this->lastErrorElements.reading.features.cols();
00125         const int dim = this->lastErrorElements.reading.features.rows();
00126         if(nbPoints == 0)
00127         {
00128                 throw std::runtime_error("Error, last error element empty. Error minimizer needs to be called at least once before using this method.");
00129         }
00130         
00131         if (!this->lastErrorElements.reading.descriptorExists("simpleSensorNoise"))
00132         {
00133                 LOG_INFO_STREAM("PointToPointErrorMinimizer - warning, no sensor noise found. Using best estimate given outlier rejection instead.");
00134                 return this->getWeightedPointUsedRatio();
00135         }
00136         
00137         const BOOST_AUTO(noises, this->lastErrorElements.reading.getDescriptorViewByName("simpleSensorNoise"));
00138         
00139         const Vector dists = (this->lastErrorElements.reading.features.topRows(dim-1) - this->lastErrorElements.reference.features.topRows(dim-1)).colwise().norm();
00140         const T mean = dists.sum()/nbPoints;
00141         
00142         int count = 0;
00143         for(int i=0; i < nbPoints; i++)
00144         {
00145                 if(dists(i) < (mean + noises(0,i)))
00146                 {
00147                         count++;
00148                 }
00149         }
00150         
00151         return (T)count/(T)nbPoints;
00152 }
00153 
00154 template<typename T>
00155 T PointToPointErrorMinimizer<T>::computeResidualError(const ErrorElements& mPts)
00156 {
00157         //typedef typename PointMatcher<T>::Matrix Matrix;
00158         
00159         const Matrix deltas = mPts.reading.features - mPts.reference.features;
00160         
00161         // return sum of the norm of each delta
00162         Matrix deltaNorms = deltas.colwise().norm();
00163         return deltaNorms.sum();
00164 }
00165 
00166 template struct PointToPointErrorMinimizer<float>;
00167 template struct PointToPointErrorMinimizer<double>;


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