histogram.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2016 The Cartographer Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #include <algorithm>
20 #include <numeric>
21 #include <string>
22 
24 #include "glog/logging.h"
25 
26 namespace cartographer {
27 namespace common {
28 
29 void Histogram::Add(const float value) { values_.push_back(value); }
30 
31 string Histogram::ToString(const int buckets) const {
32  CHECK_GE(buckets, 1);
33  if (values_.empty()) {
34  return "Count: 0";
35  }
36  const float min = *std::min_element(values_.begin(), values_.end());
37  const float max = *std::max_element(values_.begin(), values_.end());
38  const float mean =
39  std::accumulate(values_.begin(), values_.end(), 0.f) / values_.size();
40  string result = "Count: " + std::to_string(values_.size()) +
41  " Min: " + std::to_string(min) +
42  " Max: " + std::to_string(max) +
43  " Mean: " + std::to_string(mean);
44  if (min == max) {
45  return result;
46  }
47  CHECK_LT(min, max);
48  float lower_bound = min;
49  int total_count = 0;
50  for (int i = 0; i != buckets; ++i) {
51  const float upper_bound =
52  (i + 1 == buckets)
53  ? max
54  : (max * (i + 1) / buckets + min * (buckets - i - 1) / buckets);
55  int count = 0;
56  for (const float value : values_) {
57  if (lower_bound <= value &&
58  (i + 1 == buckets ? value <= upper_bound : value < upper_bound)) {
59  ++count;
60  }
61  }
62  total_count += count;
63  result += "\n[" + std::to_string(lower_bound) + ", " +
64  std::to_string(upper_bound) + ((i + 1 == buckets) ? "]" : ")");
65  constexpr int kMaxBarChars = 20;
66  const int bar =
67  (count * kMaxBarChars + values_.size() / 2) / values_.size();
68  result += "\t";
69  for (int i = 0; i != kMaxBarChars; ++i) {
70  result += (i < (kMaxBarChars - bar)) ? " " : "#";
71  }
72  result += "\tCount: " + std::to_string(count) + " (" +
73  std::to_string(count * 1e2f / values_.size()) + "%)";
74  result += "\tTotal: " + std::to_string(total_count) + " (" +
75  std::to_string(total_count * 1e2f / values_.size()) + "%)";
76  lower_bound = upper_bound;
77  }
78  return result;
79 }
80 
81 } // namespace common
82 } // namespace cartographer
int count
Definition: 3d/submaps.cc:42
std::vector< float > values_
Definition: histogram.h:35
float value
string ToString(int buckets) const
Definition: histogram.cc:31


cartographer
Author(s):
autogenerated on Mon Jun 10 2019 12:51:38