probability_values.h
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 
17 #ifndef CARTOGRAPHER_MAPPING_PROBABILITY_VALUES_H_
18 #define CARTOGRAPHER_MAPPING_PROBABILITY_VALUES_H_
19 
20 #include <cmath>
21 #include <vector>
22 
25 #include "glog/logging.h"
26 
27 namespace cartographer {
28 namespace mapping {
29 
30 namespace {
31 
32 inline uint16 BoundedFloatToValue(const float float_value,
33  const float lower_bound,
34  const float upper_bound) {
35  const int value =
37  (common::Clamp(float_value, lower_bound, upper_bound) - lower_bound) *
38  (32766.f / (upper_bound - lower_bound))) +
39  1;
40  // DCHECK for performance.
41  DCHECK_GE(value, 1);
42  DCHECK_LE(value, 32767);
43  return value;
44 }
45 
46 } // namespace
47 
48 inline float Odds(float probability) {
49  return probability / (1.f - probability);
50 }
51 
52 inline float ProbabilityFromOdds(const float odds) {
53  return odds / (odds + 1.f);
54 }
55 
56 inline float ProbabilityToCorrespondenceCost(const float probability) {
57  return 1.f - probability;
58 }
59 
60 inline float CorrespondenceCostToProbability(const float correspondence_cost) {
61  return 1.f - correspondence_cost;
62 }
63 
64 constexpr float kMinProbability = 0.1f;
65 constexpr float kMaxProbability = 1.f - kMinProbability;
66 constexpr float kMinCorrespondenceCost = 1.f - kMaxProbability;
67 constexpr float kMaxCorrespondenceCost = 1.f - kMinProbability;
68 
69 // Clamps probability to be in the range [kMinProbability, kMaxProbability].
70 inline float ClampProbability(const float probability) {
71  return common::Clamp(probability, kMinProbability, kMaxProbability);
72 }
73 // Clamps correspondece cost to be in the range [kMinCorrespondenceCost,
74 // kMaxCorrespondenceCost].
75 inline float ClampCorrespondenceCost(const float correspondence_cost) {
76  return common::Clamp(correspondence_cost, kMinCorrespondenceCost,
77  kMaxCorrespondenceCost);
78 }
79 
82 constexpr uint16 kUpdateMarker = 1u << 15;
83 
84 // Converts a correspondence_cost to a uint16 in the [1, 32767] range.
85 inline uint16 CorrespondenceCostToValue(const float correspondence_cost) {
86  return BoundedFloatToValue(correspondence_cost, kMinCorrespondenceCost,
87  kMaxCorrespondenceCost);
88 }
89 
90 // Converts a probability to a uint16 in the [1, 32767] range.
91 inline uint16 ProbabilityToValue(const float probability) {
92  return BoundedFloatToValue(probability, kMinProbability, kMaxProbability);
93 }
94 
95 extern const std::vector<float>* const kValueToProbability;
96 extern const std::vector<float>* const kValueToCorrespondenceCost;
97 
98 // Converts a uint16 (which may or may not have the update marker set) to a
99 // probability in the range [kMinProbability, kMaxProbability].
100 inline float ValueToProbability(const uint16 value) {
101  return (*kValueToProbability)[value];
102 }
103 
104 // Converts a uint16 (which may or may not have the update marker set) to a
105 // correspondence cost in the range [kMinCorrespondenceCost,
106 // kMaxCorrespondenceCost].
107 inline float ValueToCorrespondenceCost(const uint16 value) {
108  return (*kValueToCorrespondenceCost)[value];
109 }
110 
112  uint16 probability_value) {
113  if (probability_value == kUnknownProbabilityValue) {
115  }
116  bool update_carry = false;
117  if (probability_value > kUpdateMarker) {
118  probability_value -= kUpdateMarker;
119  update_carry = true;
120  }
123  if (update_carry) result += kUpdateMarker;
124  return result;
125 }
126 
128  uint16 correspondence_cost_value) {
129  if (correspondence_cost_value == kUnknownCorrespondenceValue)
131  bool update_carry = false;
132  if (correspondence_cost_value > kUpdateMarker) {
133  correspondence_cost_value -= kUpdateMarker;
134  update_carry = true;
135  }
137  ValueToCorrespondenceCost(correspondence_cost_value)));
138  if (update_carry) result += kUpdateMarker;
139  return result;
140 }
141 
142 std::vector<uint16> ComputeLookupTableToApplyOdds(float odds);
143 std::vector<uint16> ComputeLookupTableToApplyCorrespondenceCostOdds(float odds);
144 
145 } // namespace mapping
146 } // namespace cartographer
147 
148 #endif // CARTOGRAPHER_MAPPING_PROBABILITY_VALUES_H_
const std::vector< float > *const kValueToCorrespondenceCost
uint16 ProbabilityValueToCorrespondenceCostValue(uint16 probability_value)
float ClampProbability(const float probability)
std::vector< uint16 > ComputeLookupTableToApplyOdds(const float odds)
std::vector< uint16 > ComputeLookupTableToApplyCorrespondenceCostOdds(float odds)
float ProbabilityFromOdds(const float odds)
float ValueToCorrespondenceCost(const uint16 value)
int RoundToInt(const float x)
Definition: port.h:41
constexpr float kMinProbability
constexpr float kMaxCorrespondenceCost
float ValueToProbability(const uint16 value)
constexpr float kMaxProbability
float CorrespondenceCostToProbability(const float correspondence_cost)
float ClampCorrespondenceCost(const float correspondence_cost)
uint16 CorrespondenceCostValueToProbabilityValue(uint16 correspondence_cost_value)
uint16_t uint16
Definition: port.h:35
float Odds(float probability)
const std::vector< float > *const kValueToProbability
constexpr uint16 kUnknownProbabilityValue
float ProbabilityToCorrespondenceCost(const float probability)
uint16 ProbabilityToValue(const float probability)
T Clamp(const T value, const T min, const T max)
Definition: math.h:32
uint16 CorrespondenceCostToValue(const float correspondence_cost)
constexpr uint16 kUpdateMarker
constexpr uint16 kUnknownCorrespondenceValue
constexpr float kMinCorrespondenceCost


cartographer
Author(s): The Cartographer Authors
autogenerated on Mon Feb 28 2022 22:00:58