/usr/include/CGAL/remove_outliers.h is in libcgal-dev 4.11-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | // Copyright (c) 2007-09 INRIA Sophia-Antipolis (France).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org).
// You can redistribute it and/or modify it under the terms of the GNU
// General Public License as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// Licensees holding a valid commercial license may use this file in
// accordance with the commercial license agreement provided with the software.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// $URL$
// $Id$
//
// Author(s) : Laurent Saboret and Nader Salman and Pierre Alliez
#ifndef CGAL_REMOVE_OUTLIERS_H
#define CGAL_REMOVE_OUTLIERS_H
#include <CGAL/license/Point_set_processing_3.h>
#include <CGAL/Search_traits_3.h>
#include <CGAL/Orthogonal_k_neighbor_search.h>
#include <CGAL/property_map.h>
#include <CGAL/point_set_processing_assertions.h>
#include <iterator>
#include <algorithm>
#include <map>
namespace CGAL {
// ----------------------------------------------------------------------------
// Private section
// ----------------------------------------------------------------------------
/// \cond SKIP_IN_MANUAL
namespace internal {
/// Utility function for remove_outliers():
/// Computes average squared distance to the K nearest neighbors.
///
/// \pre `k >= 2`
///
/// @tparam Kernel Geometric traits class.
/// @tparam Tree KD-tree.
///
/// @return computed distance.
template < typename Kernel,
typename Tree >
typename Kernel::FT
compute_avg_knn_sq_distance_3(
const typename Kernel::Point_3& query, ///< 3D point to project
Tree& tree, ///< KD-tree
unsigned int k) ///< number of neighbors
{
// geometric types
typedef typename Kernel::FT FT;
typedef typename Kernel::Point_3 Point;
// types for K nearest neighbors search
typedef typename CGAL::Search_traits_3<Kernel> Tree_traits;
typedef typename CGAL::Orthogonal_k_neighbor_search<Tree_traits> Neighbor_search;
typedef typename Neighbor_search::iterator Search_iterator;
// Gather set of (k+1) neighboring points.
// Perform k+1 queries (if in point set, the query point is
// output first). Search may be aborted if k is greater
// than number of input points.
std::vector<Point> points; points.reserve(k+1);
Neighbor_search search(tree,query,k+1);
Search_iterator search_iterator = search.begin();
unsigned int i;
for(i=0;i<(k+1);i++)
{
if(search_iterator == search.end())
break; // premature ending
points.push_back(search_iterator->first);
search_iterator++;
}
CGAL_point_set_processing_precondition(points.size() >= 1);
// compute average squared distance
typename Kernel::Compute_squared_distance_3 sqd;
FT sq_distance = (FT)0.0;
for(typename std::vector<Point>::iterator neighbor = points.begin(); neighbor != points.end(); neighbor++)
sq_distance += sqd(*neighbor, query);
sq_distance /= FT(points.size());
return sq_distance;
}
} /* namespace internal */
/// \endcond
// ----------------------------------------------------------------------------
// Public section
// ----------------------------------------------------------------------------
/// \ingroup PkgPointSetProcessingAlgorithms
/// Removes outliers:
/// - computes average squared distance to the K nearest neighbors,
/// - and sorts the points in increasing order of average distance.
///
/// This method modifies the order of input points so as to pack all remaining points first,
/// and returns an iterator over the first point to remove (see erase-remove idiom).
/// For this reason it should not be called on sorted containers.
///
/// \pre `k >= 2`
///
/// @tparam InputIterator iterator over input points.
/// @tparam PointPMap is a model of `ReadablePropertyMap` with value type `Point_3<Kernel>`.
/// It can be omitted ifthe value type of `InputIterator` is convertible to `Point_3<Kernel>`.
/// @tparam Kernel Geometric traits class.
/// It can be omitted and deduced automatically from the value type of `PointPMap`.
///
/// @return iterator over the first point to remove.
///
/// @note There are two thresholds that can be used:
/// `threshold_percent` and `threshold_distance`. This function
/// returns the smallest number of outliers such that at least one of
/// these threshold is fullfilled. This means that if
/// `threshold_percent=100`, only `threshold_distance` is taken into
/// account; if `threshold_distance=0` only `threshold_percent` is
/// taken into account.
// This variant requires all parameters.
template <typename InputIterator,
typename PointPMap,
typename Kernel
>
InputIterator
remove_outliers(
InputIterator first, ///< iterator over the first input point.
InputIterator beyond, ///< past-the-end iterator over the input points.
PointPMap point_pmap, ///< property map: value_type of InputIterator -> Point_3
unsigned int k, ///< number of neighbors.
double threshold_percent, ///< maximum percentage of points to remove.
double threshold_distance, ///< minimum distance for a point to be
///< considered as outlier (distance here is the square root of the average
///< squared distance to K nearest
///< neighbors)
const Kernel& /*kernel*/) ///< geometric traits.
{
// geometric types
typedef typename Kernel::FT FT;
// basic geometric types
typedef typename Kernel::Point_3 Point;
// actual type of input points
typedef typename std::iterator_traits<InputIterator>::value_type Enriched_point;
// types for K nearest neighbors search structure
typedef typename CGAL::Search_traits_3<Kernel> Tree_traits;
typedef typename CGAL::Orthogonal_k_neighbor_search<Tree_traits> Neighbor_search;
typedef typename Neighbor_search::Tree Tree;
// precondition: at least one element in the container.
// to fix: should have at least three distinct points
// but this is costly to check
CGAL_point_set_processing_precondition(first != beyond);
// precondition: at least 2 nearest neighbors
CGAL_point_set_processing_precondition(k >= 2);
CGAL_point_set_processing_precondition(threshold_percent >= 0 && threshold_percent <= 100);
InputIterator it;
// Instanciate a KD-tree search.
// Note: We have to convert each input iterator to Point_3.
std::vector<Point> kd_tree_points;
for(it = first; it != beyond; it++)
kd_tree_points.push_back( get(point_pmap, *it) );
Tree tree(kd_tree_points.begin(), kd_tree_points.end());
// iterate over input points and add them to multimap sorted by distance to k
std::multimap<FT,Enriched_point> sorted_points;
for(it = first; it != beyond; it++)
{
FT sq_distance = internal::compute_avg_knn_sq_distance_3<Kernel>(
get(point_pmap,*it),
tree, k);
sorted_points.insert( std::make_pair(sq_distance, *it) );
}
// Replaces [first, beyond) range by the multimap content.
// Returns the iterator after the (100-threshold_percent) % best points.
InputIterator first_point_to_remove = first;
InputIterator dst = first;
int first_index_to_remove = int(double(sorted_points.size()) * ((100.0-threshold_percent)/100.0));
typename std::multimap<FT,Enriched_point>::iterator src;
int index;
for (src = sorted_points.begin(), index = 0;
src != sorted_points.end();
++src, ++index)
{
*dst++ = src->second;
if (index <= first_index_to_remove ||
src->first < threshold_distance * threshold_distance)
first_point_to_remove = dst;
}
return first_point_to_remove;
}
/// @cond SKIP_IN_MANUAL
// This variant deduces the kernel from the iterator type.
template <typename InputIterator,
typename PointPMap
>
InputIterator
remove_outliers(
InputIterator first, ///< iterator over the first input point
InputIterator beyond, ///< past-the-end iterator
PointPMap point_pmap, ///< property map: value_type of InputIterator -> Point_3
unsigned int k, ///< number of neighbors.
double threshold_percent, ///< percentage of points to remove
double threshold_distance = 0.0) ///< minimum average squared distance to K nearest neighbors
///< for a point to be removed.
{
typedef typename boost::property_traits<PointPMap>::value_type Point;
typedef typename Kernel_traits<Point>::Kernel Kernel;
return remove_outliers(
first,beyond,
point_pmap,
k, threshold_percent, threshold_distance,
Kernel());
}
/// @endcond
/// @cond SKIP_IN_MANUAL
// This variant creates a default point property map = Identity_property_map.
template <typename InputIterator
>
InputIterator
remove_outliers(
InputIterator first, ///< iterator over the first input point
InputIterator beyond, ///< past-the-end iterator
unsigned int k, ///< number of neighbors.
double threshold_percent, ///< percentage of points to remove
double threshold_distance = 0.0) ///< minimum average squared distance to K nearest neighbors
///< for a point to be removed.
{
return remove_outliers(
first,beyond,
make_identity_property_map(
typename std::iterator_traits<InputIterator>::value_type()),
k, threshold_percent, threshold_distance);
}
/// @endcond
} //namespace CGAL
#endif // CGAL_REMOVE_OUTLIERS_H
|