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 #include "MaxDensity.h" 00036 00037 // MaxDensityDataPointsFilter 00038 // Constructor 00039 template<typename T> 00040 MaxDensityDataPointsFilter<T>::MaxDensityDataPointsFilter(const Parameters& params): 00041 PointMatcher<T>::DataPointsFilter("MaxDensityDataPointsFilter", 00042 MaxDensityDataPointsFilter::availableParameters(), params), 00043 maxDensity(Parametrizable::get<T>("maxDensity")) 00044 { 00045 } 00046 00047 // Compute 00048 template<typename T> 00049 typename PointMatcher<T>::DataPoints MaxDensityDataPointsFilter<T>::filter( 00050 const DataPoints& input) 00051 { 00052 DataPoints output(input); 00053 inPlaceFilter(output); 00054 return output; 00055 } 00056 00057 // In-place filter 00058 template<typename T> 00059 void MaxDensityDataPointsFilter<T>::inPlaceFilter( 00060 DataPoints& cloud) 00061 { 00062 typedef typename DataPoints::View View; 00063 00064 // Force densities to be computed 00065 if (!cloud.descriptorExists("densities")) 00066 { 00067 throw InvalidField("MaxDensityDataPointsFilter: Error, no densities found in descriptors."); 00068 } 00069 00070 const int nbPointsIn = cloud.features.cols(); 00071 View densities = cloud.getDescriptorViewByName("densities"); 00072 const T lastDensity = densities.maxCoeff(); 00073 const int nbSaturatedPts = (densities.array() == lastDensity).count(); 00074 00075 // fill cloud values 00076 int j = 0; 00077 for (int i = 0; i < nbPointsIn; ++i) 00078 { 00079 const T density(densities(0,i)); 00080 if (density > maxDensity) 00081 { 00082 const float r = (float)std::rand()/(float)RAND_MAX; 00083 float acceptRatio = maxDensity/density; 00084 00085 // Handle saturation value of density 00086 if (density == lastDensity) 00087 { 00088 acceptRatio = acceptRatio * (1-nbSaturatedPts/nbPointsIn); 00089 } 00090 00091 if (r < acceptRatio) 00092 { 00093 cloud.setColFrom(j, cloud, i); 00094 ++j; 00095 } 00096 } 00097 else 00098 { 00099 cloud.setColFrom(j, cloud, i); 00100 ++j; 00101 } 00102 } 00103 00104 cloud.conservativeResize(j); 00105 } 00106 00107 template struct MaxDensityDataPointsFilter<float>; 00108 template struct MaxDensityDataPointsFilter<double>; 00109