Refactor number of cells magic number (#1522)

master
damienrg 2019-03-07 11:56:01 +01:00 committed by Alexander Belyaev
parent aed6ee7b1d
commit a60cca4a43
1 changed files with 7 additions and 5 deletions

View File

@ -23,14 +23,16 @@ namespace mapping {
namespace { namespace {
constexpr int kNumberOfValues = 32768;
// 0 is unknown, [1, 32767] maps to [lower_bound, upper_bound]. // 0 is unknown, [1, 32767] maps to [lower_bound, upper_bound].
float SlowValueToBoundedFloat(const uint16 value, const uint16 unknown_value, float SlowValueToBoundedFloat(const uint16 value, const uint16 unknown_value,
const float unknown_result, const float unknown_result,
const float lower_bound, const float lower_bound,
const float upper_bound) { const float upper_bound) {
CHECK_LE(value, 32767); CHECK_LT(value, kNumberOfValues);
if (value == unknown_value) return unknown_result; if (value == unknown_value) return unknown_result;
const float kScale = (upper_bound - lower_bound) / 32766.f; const float kScale = (upper_bound - lower_bound) / (kNumberOfValues - 2.f);
return value * kScale + (lower_bound - kScale); return value * kScale + (lower_bound - kScale);
} }
@ -41,7 +43,7 @@ std::unique_ptr<std::vector<float>> PrecomputeValueToBoundedFloat(
// Repeat two times, so that both values with and without the update marker // Repeat two times, so that both values with and without the update marker
// can be converted to a probability. // can be converted to a probability.
for (int repeat = 0; repeat != 2; ++repeat) { for (int repeat = 0; repeat != 2; ++repeat) {
for (int value = 0; value != 32768; ++value) { for (int value = 0; value != kNumberOfValues; ++value) {
result->push_back(SlowValueToBoundedFloat( result->push_back(SlowValueToBoundedFloat(
value, unknown_value, unknown_result, lower_bound, upper_bound)); value, unknown_value, unknown_result, lower_bound, upper_bound));
} }
@ -73,7 +75,7 @@ std::vector<uint16> ComputeLookupTableToApplyOdds(const float odds) {
std::vector<uint16> result; std::vector<uint16> result;
result.push_back(ProbabilityToValue(ProbabilityFromOdds(odds)) + result.push_back(ProbabilityToValue(ProbabilityFromOdds(odds)) +
kUpdateMarker); kUpdateMarker);
for (int cell = 1; cell != 32768; ++cell) { for (int cell = 1; cell != kNumberOfValues; ++cell) {
result.push_back(ProbabilityToValue(ProbabilityFromOdds( result.push_back(ProbabilityToValue(ProbabilityFromOdds(
odds * Odds((*kValueToProbability)[cell]))) + odds * Odds((*kValueToProbability)[cell]))) +
kUpdateMarker); kUpdateMarker);
@ -87,7 +89,7 @@ std::vector<uint16> ComputeLookupTableToApplyCorrespondenceCostOdds(
result.push_back(CorrespondenceCostToValue(ProbabilityToCorrespondenceCost( result.push_back(CorrespondenceCostToValue(ProbabilityToCorrespondenceCost(
ProbabilityFromOdds(odds))) + ProbabilityFromOdds(odds))) +
kUpdateMarker); kUpdateMarker);
for (int cell = 1; cell != 32768; ++cell) { for (int cell = 1; cell != kNumberOfValues; ++cell) {
result.push_back( result.push_back(
CorrespondenceCostToValue( CorrespondenceCostToValue(
ProbabilityToCorrespondenceCost(ProbabilityFromOdds( ProbabilityToCorrespondenceCost(ProbabilityFromOdds(