Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef CARTOGRAPHER_MAPPING_PROBABILITY_VALUES_H_
00018 #define CARTOGRAPHER_MAPPING_PROBABILITY_VALUES_H_
00019
00020 #include <cmath>
00021 #include <vector>
00022
00023 #include "cartographer/common/math.h"
00024 #include "cartographer/common/port.h"
00025 #include "glog/logging.h"
00026
00027 namespace cartographer {
00028 namespace mapping {
00029
00030 namespace {
00031
00032 inline uint16 BoundedFloatToValue(const float float_value,
00033 const float lower_bound,
00034 const float upper_bound) {
00035 const int value =
00036 common::RoundToInt(
00037 (common::Clamp(float_value, lower_bound, upper_bound) - lower_bound) *
00038 (32766.f / (upper_bound - lower_bound))) +
00039 1;
00040
00041 DCHECK_GE(value, 1);
00042 DCHECK_LE(value, 32767);
00043 return value;
00044 }
00045
00046 }
00047
00048 inline float Odds(float probability) {
00049 return probability / (1.f - probability);
00050 }
00051
00052 inline float ProbabilityFromOdds(const float odds) {
00053 return odds / (odds + 1.f);
00054 }
00055
00056 inline float ProbabilityToCorrespondenceCost(const float probability) {
00057 return 1.f - probability;
00058 }
00059
00060 inline float CorrespondenceCostToProbability(const float correspondence_cost) {
00061 return 1.f - correspondence_cost;
00062 }
00063
00064 constexpr float kMinProbability = 0.1f;
00065 constexpr float kMaxProbability = 1.f - kMinProbability;
00066 constexpr float kMinCorrespondenceCost = 1.f - kMaxProbability;
00067 constexpr float kMaxCorrespondenceCost = 1.f - kMinProbability;
00068
00069
00070 inline float ClampProbability(const float probability) {
00071 return common::Clamp(probability, kMinProbability, kMaxProbability);
00072 }
00073
00074
00075 inline float ClampCorrespondenceCost(const float correspondence_cost) {
00076 return common::Clamp(correspondence_cost, kMinCorrespondenceCost,
00077 kMaxCorrespondenceCost);
00078 }
00079
00080 constexpr uint16 kUnknownProbabilityValue = 0;
00081 constexpr uint16 kUnknownCorrespondenceValue = kUnknownProbabilityValue;
00082 constexpr uint16 kUpdateMarker = 1u << 15;
00083
00084
00085 inline uint16 CorrespondenceCostToValue(const float correspondence_cost) {
00086 return BoundedFloatToValue(correspondence_cost, kMinCorrespondenceCost,
00087 kMaxCorrespondenceCost);
00088 }
00089
00090
00091 inline uint16 ProbabilityToValue(const float probability) {
00092 return BoundedFloatToValue(probability, kMinProbability, kMaxProbability);
00093 }
00094
00095 extern const std::vector<float>* const kValueToProbability;
00096 extern const std::vector<float>* const kValueToCorrespondenceCost;
00097
00098
00099
00100 inline float ValueToProbability(const uint16 value) {
00101 return (*kValueToProbability)[value];
00102 }
00103
00104
00105
00106
00107 inline float ValueToCorrespondenceCost(const uint16 value) {
00108 return (*kValueToCorrespondenceCost)[value];
00109 }
00110
00111 inline uint16 ProbabilityValueToCorrespondenceCostValue(
00112 uint16 probability_value) {
00113 if (probability_value == kUnknownProbabilityValue) {
00114 return kUnknownCorrespondenceValue;
00115 }
00116 bool update_carry = false;
00117 if (probability_value > kUpdateMarker) {
00118 probability_value -= kUpdateMarker;
00119 update_carry = true;
00120 }
00121 uint16 result = CorrespondenceCostToValue(
00122 ProbabilityToCorrespondenceCost(ValueToProbability(probability_value)));
00123 if (update_carry) result += kUpdateMarker;
00124 return result;
00125 }
00126
00127 inline uint16 CorrespondenceCostValueToProbabilityValue(
00128 uint16 correspondence_cost_value) {
00129 if (correspondence_cost_value == kUnknownCorrespondenceValue)
00130 return kUnknownProbabilityValue;
00131 bool update_carry = false;
00132 if (correspondence_cost_value > kUpdateMarker) {
00133 correspondence_cost_value -= kUpdateMarker;
00134 update_carry = true;
00135 }
00136 uint16 result = ProbabilityToValue(CorrespondenceCostToProbability(
00137 ValueToCorrespondenceCost(correspondence_cost_value)));
00138 if (update_carry) result += kUpdateMarker;
00139 return result;
00140 }
00141
00142 std::vector<uint16> ComputeLookupTableToApplyOdds(float odds);
00143 std::vector<uint16> ComputeLookupTableToApplyCorrespondenceCostOdds(float odds);
00144
00145 }
00146 }
00147
00148 #endif // CARTOGRAPHER_MAPPING_PROBABILITY_VALUES_H_