utils.h
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--2018,
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 #pragma once
00036 
00037 #include "PointMatcher.h"
00038 
00039 #include <vector>
00040 #include <algorithm>
00041 #include <cmath>
00042 
00043 namespace PointMatcherSupport
00044 {
00045 
00046 template<class T>
00047 inline constexpr T pow(const T base, const std::size_t exponent)
00048 {
00049     return exponent == 0 ? 1 : base * pow(base, exponent - 1);
00050 }
00051 
00052 template<typename T>
00053 struct IdxCompare
00054 {
00055         typedef typename PointMatcher<T>::Vector Vector;
00056         const Vector& target;
00057 
00058         IdxCompare(const typename PointMatcher<T>::Vector& target): target(target) {}
00059 
00060         bool operator()(size_t a, size_t b) const { return target(a, 0) < target(b, 0); }
00061 };
00062 
00063 
00064 template<typename T>
00065 std::vector<size_t> 
00066 sortIndexes(const typename PointMatcher<T>::Vector& v)
00067 {
00068         // initialize original index locations
00069         const size_t idxSize = v.size();
00070         std::vector<size_t> idx(idxSize);
00071         for(size_t i=0; i < idxSize; ++i) idx[i]=i;
00072 
00073         // sort indexes based on comparing values in v
00074         std::sort(idx.begin(), idx.end(), IdxCompare<T>(v));
00075 
00076         return idx;
00077 }
00078 
00079 template<typename T>
00080 typename PointMatcher<T>::Vector
00081 sortEigenValues(const typename PointMatcher<T>::Vector& eigenVa)
00082 {
00083         // sort the eigenvalues in ascending order
00084         typename PointMatcher<T>::Vector eigenVaSort = eigenVa;
00085         std::sort(eigenVaSort.data(), eigenVaSort.data() + eigenVaSort.size());
00086         return eigenVaSort;
00087 }
00088 
00089 template<typename T>
00090 typename PointMatcher<T>::Vector 
00091 serializeEigVec(const typename PointMatcher<T>::Matrix& eigenVe)
00092 {
00093         // serialize row major
00094         const int eigenVeDim = eigenVe.cols();
00095         typename PointMatcher<T>::Vector output(eigenVeDim*eigenVeDim);
00096         for(int k=0; k < eigenVeDim; ++k)
00097         {
00098                 output.segment(k*eigenVeDim, eigenVeDim) = 
00099                         eigenVe.row(k).transpose();
00100         }
00101 
00102         return output;
00103 }
00104 
00105 template<typename T>
00106 T computeDensity(const typename PointMatcher<T>::Matrix& NN)
00107 {
00108         //volume in meter
00109         const T volume = (4./3.)*M_PI*std::pow(NN.colwise().norm().maxCoeff(), 3);
00110 
00111         //volume in decimeter
00112         //T volume = (4./3.)*M_PI*std::pow(NN.colwise().norm().maxCoeff()*10.0, 3);
00113         //const T minVolume = 4.18e-9; // minimum of volume of one millimeter radius
00114         //const T minVolume = 0.42; // minimum of volume of one centimeter radius (in dm^3)
00115 
00116         //if(volume < minVolume)
00117         //      volume = minVolume;
00118 
00119         return T(NN.cols())/(volume);
00120 }
00121 template<typename T>
00122 typename PointMatcher<T>::Vector
00123 computeNormal(const typename PointMatcher<T>::Vector& eigenVa, const typename PointMatcher<T>::Matrix& eigenVe)
00124 {
00125         // Keep the smallest eigenvector as surface normal
00126         const int nbEigenCol = eigenVe.cols();
00127         int smallestId(0);
00128         T smallestValue(std::numeric_limits<T>::max());
00129         for(int j = 0; j < nbEigenCol ; ++j)
00130         {
00131                 if (eigenVa(j) < smallestValue)
00132                 {
00133                         smallestId = j;
00134                         smallestValue = eigenVa(j);
00135                 }
00136         }
00137 
00138   return eigenVe.col(smallestId);
00139 }
00140 
00141 template<typename T>
00142 size_t argMax(const typename PointMatcher<T>::Vector& v)
00143 {
00144         //FIXME: Change that to use the new API. the new Eigen API (3.2.8) allows this with the call maxCoeff. See the section Visitors in https://eigen.tuxfamily.org/dox/group__TutorialReductionsVisitorsBroadcasting.html
00145         const int size(v.size());
00146         T maxVal(0);
00147         size_t maxIdx(0);
00148         for (int i = 0; i < size; ++i)
00149         {
00150                 if (v[i] > maxVal)
00151                 {
00152                         maxVal = v[i];
00153                         maxIdx = i;
00154                 }
00155         }
00156         return maxIdx;
00157 }
00158 
00159 };


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