ATTPCROOT  0.3.0-alpha
A ROOT-based framework for analyzing data from active target detectors
msd.cxx
Go to the documentation of this file.
1 #include "msd.h"
2 
3 #include <boost/smart_ptr/shared_ptr.hpp> // for shared_ptr
4 #include <pcl/kdtree/kdtree_flann.h>
5 
6 #include <algorithm>
7 #include <cstddef> // for size_t
8 #include <numeric>
9 #include <string> // for string
10 #include <vector>
11 
12 namespace msd {
13 /*
14 @brief Compute mean squared distance.
15 
16 This function computes the mean squared distance from a point to its k
17 neighbours. This is done for all points in the given point cloud.
18 
19 @param k number of neighbours
20 @param cloud the point cloud
21 @return vector of mean squared distances of every point in the cloud
22 */
23 std::vector<float> compute_mean_square_distance(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, int k)
24 {
25  // compute mean square distances for every point to its k nearest neighbours
26  pcl::KdTreeFLANN<pcl::PointXYZ> kdtree;
27  std::vector<float> msd;
28 
29  k++; // k must be one higher because the first point found by the kdtree is
30  // the point itself
31  kdtree.setInputCloud(cloud);
32 
33  for (size_t i = 0; i < cloud->points.size(); ++i) {
34  // compute
35  float sum;
36  std::vector<int> nnIndices;
37  std::vector<float> knnSquaredDistances;
38  nnIndices.reserve(k);
39  knnSquaredDistances.reserve(k);
40  kdtree.nearestKSearch(cloud->at(i), k, nnIndices, knnSquaredDistances);
41 
42  knnSquaredDistances.erase(knnSquaredDistances.begin()); // The first value
43  // must be deleted
44  // because it is
45  // the distance
46  // with the point
47  // itself
48 
49  sum = std::accumulate(knnSquaredDistances.begin(), knnSquaredDistances.end(), 0.0);
50  msd.push_back(sum / knnSquaredDistances.size());
51  }
52  return msd;
53 }
54 
55 /*
56 @brief Compute first quartile mean squared distance.
57 
58 This function computes the first quartile of the mean squared distances from
59 every point to its nearest neighbour. This is done for all points in the given
60 oint cloud.
61 
62 @param cloud the point cloud
63 @return first quartile
64 */
65 float first_quartile(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud)
66 {
67  std::vector<float> msd = compute_mean_square_distance(cloud, 1), lower_half;
68  float const q1 = msd.size() / 4;
69  std::nth_element(msd.begin(), msd.begin() + q1, msd.end());
70  return msd[q1];
71 }
72 } // namespace msd
msd::compute_mean_square_distance
std::vector< float > compute_mean_square_distance(pcl::PointCloud< pcl::PointXYZ >::Ptr cloud, int k)
Definition: msd.cxx:23
msd.h
msd
Definition: msd.cxx:12
msd::first_quartile
float first_quartile(pcl::PointCloud< pcl::PointXYZ >::Ptr cloud)
Definition: msd.cxx:65