MeVisLab Toolbox Reference
mlPCLSupportStatistics.h
Go to the documentation of this file.
1 // Copyright (c) Fraunhofer MEVIS, Germany. All rights reserved.
2 // **InsertLicense** code author="Wolf Spindler"
3 //----------------------------------------------------------------------------------
5 
12 //----------------------------------------------------------------------------------
13 #pragma once
14 
15 #include "MLPCLSupportSystem.h"
16 #include <mlTypeDefs.h>
17 #include <mlPCLTypes.h>
18 #include <mlPCLSupportTools.h>
19 
20 ML_START_NAMESPACE
21 
23 namespace PCLSupportTools {
24 
25 //----------------------------------------------------------------------------------
27 //----------------------------------------------------------------------------------
31  minimum( ML_DOUBLE_MAX),
32  maximum(-ML_DOUBLE_MAX),
33  average(0),
34  sum(0),
35  sumAbsolute(0),
36  median(0){}
37 
39  double minimum;
40 
42  double maximum;
43 
45  double average;
46 
48  double sum;
49 
51  double sumAbsolute;
52 
55  double median;
56 };
57 
58 //----------------------------------------------------------------------------------
67 //----------------------------------------------------------------------------------
68 template <typename POINT_CLOUD_TYPE>
69 inline PointCloudStatistics getStatistics(const POINT_CLOUD_TYPE &pointCloud,
70  const std::string &floatPointMemberName="intensityReplacement",
71  const bool calculateMedian=false)
72 {
74  const size_t numPoints = pointCloud.points.size();
75  if (numPoints > 0){
76  const size_t memberOffset = PCLSupportTools::getByteOffset(pointCloud.points[0], floatPointMemberName);
77  if (memberOffset != ML_SIZE_T_MAX){
78 
79  // Create buffer only for median calculations.
80  std::vector<float> vals;
81  if (calculateMedian){ vals.resize(numPoints); }
82 
83  for (size_t c=0; c < numPoints; ++c){
84  const unsigned char *pointBase = reinterpret_cast<const unsigned char*>(&(pointCloud.points[c].x));
85  const float fltPntVal = *reinterpret_cast<const float *>(pointBase + memberOffset);
86  const double pntVal = fltPntVal;
87  if (stats.minimum > pntVal){ stats.minimum = pntVal; }
88  if (stats.maximum < pntVal){ stats.maximum = pntVal; }
89  stats.sum += pntVal;
90  stats.sumAbsolute += pntVal < 0. ? -pntVal : pntVal;
91  if (calculateMedian){ vals[c] = fltPntVal; }
92  }
93  if (numPoints > 0){
94  stats.average = stats.sum / static_cast<double>(numPoints);
95  if (calculateMedian){
96  // Sort values and take middle.
97  std::sort(vals.begin(), vals.end());
98  stats.median = vals[numPoints/2];
99  }
100  }
101  }
102  }
103  return stats;
104 }
105 
106 //----------------------------------------------------------------------------------
108 //----------------------------------------------------------------------------------
109 template <typename POINT_CLOUD_TYPE>
110 inline float getMedian(const POINT_CLOUD_TYPE &pointCloud,
111  const std::string &floatPointMemberName)
112 {
113  float median = 0;
114  const size_t numPoints = pointCloud.points.size();
115  if (numPoints > 0){
116  const size_t memberOffset = PCLSupportTools::getByteOffset(pointCloud.points[0], floatPointMemberName);
117  if (memberOffset != ML_SIZE_T_MAX){
118  std::vector<float> vals;
119  vals.resize(numPoints);
120  for (size_t c=0; c < numPoints; ++c){
121  const unsigned char *pointBase = reinterpret_cast<const unsigned char*>(&(pointCloud.points[c].x));
122  vals[c] = *reinterpret_cast<const float *>(pointBase + memberOffset);
123  }
124  std::sort(vals.begin(), vals.end());
125  median = vals[numPoints/2];
126  }
127  }
128  return median;
129 }
130 
131 };
132 
133 ML_END_NAMESPACE
134 
Project global and OS specific declarations.
A collection of tool functions used in MLPCLSupport.
Basic types used in the MeVislab binding of the Point Cloud Library(PCL).
#define ML_SIZE_T_MAX
For convenience the maximum value of size_t without "U" which is probably more the expected naming.
Definition: mlTypeDefs.h:640
#define ML_DOUBLE_MAX
Definition: mlTypeDefs.h:226
float getMedian(const POINT_CLOUD_TYPE &pointCloud, const std::string &floatPointMemberName)
Calculate the median of the member floatPointMemberName of all points and returns it as float value.
MLPCLSUPPORT_EXPORT size_t getByteOffset(const pcl::PointXYZ &pnt, const std::string &pointMemberName)
Returns the offset in bytes between the given member described as string and the first member in the ...
PointCloudStatistics getStatistics(const POINT_CLOUD_TYPE &pointCloud, const std::string &floatPointMemberName="intensityReplacement", const bool calculateMedian=false)
Traverses all points in the pointCloud and returns some statistical parameters.
Container for statistical point cloud parameters.
double median
Value of NumPoints/2 entry of all values after sorting or 0 if no values, empty point cloud or not ca...
PointCloudStatistics()
Initializes the parameters to minimum = DBL_MAX, maximum = -DBL_MAX, and average to 0.
double maximum
Maximum of all values or -DBL_MAX if no values or empty point cloud.
double sum
Sum of all values and 0 if no values or empty point cloud.
double minimum
Minimum of all values or DBL_MAX if no values or empty point cloud.
double average
Average of all values and 0 if no values or empty point cloud.
double sumAbsolute
Sum of all absolute values and 0 if no values or empty point cloud.