probability_values.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright 2016 The Cartographer Authors
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "cartographer/mapping/probability_values.h"
00018 
00019 #include "absl/memory/memory.h"
00020 
00021 namespace cartographer {
00022 namespace mapping {
00023 
00024 namespace {
00025 
00026 constexpr int kValueCount = 32768;
00027 
00028 // 0 is unknown, [1, 32767] maps to [lower_bound, upper_bound].
00029 float SlowValueToBoundedFloat(const uint16 value, const uint16 unknown_value,
00030                               const float unknown_result,
00031                               const float lower_bound,
00032                               const float upper_bound) {
00033   CHECK_LT(value, kValueCount);
00034   if (value == unknown_value) return unknown_result;
00035   const float kScale = (upper_bound - lower_bound) / (kValueCount - 2.f);
00036   return value * kScale + (lower_bound - kScale);
00037 }
00038 
00039 std::unique_ptr<std::vector<float>> PrecomputeValueToBoundedFloat(
00040     const uint16 unknown_value, const float unknown_result,
00041     const float lower_bound, const float upper_bound) {
00042   auto result = absl::make_unique<std::vector<float>>();
00043   // Repeat two times, so that both values with and without the update marker
00044   // can be converted to a probability.
00045   constexpr int kRepetitionCount = 2;
00046   result->reserve(kRepetitionCount * kValueCount);
00047   for (int repeat = 0; repeat != kRepetitionCount; ++repeat) {
00048     for (int value = 0; value != kValueCount; ++value) {
00049       result->push_back(SlowValueToBoundedFloat(
00050           value, unknown_value, unknown_result, lower_bound, upper_bound));
00051     }
00052   }
00053   return result;
00054 }
00055 
00056 std::unique_ptr<std::vector<float>> PrecomputeValueToProbability() {
00057   return PrecomputeValueToBoundedFloat(kUnknownProbabilityValue,
00058                                        kMinProbability, kMinProbability,
00059                                        kMaxProbability);
00060 }
00061 
00062 std::unique_ptr<std::vector<float>> PrecomputeValueToCorrespondenceCost() {
00063   return PrecomputeValueToBoundedFloat(
00064       kUnknownCorrespondenceValue, kMaxCorrespondenceCost,
00065       kMinCorrespondenceCost, kMaxCorrespondenceCost);
00066 }
00067 
00068 }  // namespace
00069 
00070 const std::vector<float>* const kValueToProbability =
00071     PrecomputeValueToProbability().release();
00072 
00073 const std::vector<float>* const kValueToCorrespondenceCost =
00074     PrecomputeValueToCorrespondenceCost().release();
00075 
00076 std::vector<uint16> ComputeLookupTableToApplyOdds(const float odds) {
00077   std::vector<uint16> result;
00078   result.reserve(kValueCount);
00079   result.push_back(ProbabilityToValue(ProbabilityFromOdds(odds)) +
00080                    kUpdateMarker);
00081   for (int cell = 1; cell != kValueCount; ++cell) {
00082     result.push_back(ProbabilityToValue(ProbabilityFromOdds(
00083                          odds * Odds((*kValueToProbability)[cell]))) +
00084                      kUpdateMarker);
00085   }
00086   return result;
00087 }
00088 
00089 std::vector<uint16> ComputeLookupTableToApplyCorrespondenceCostOdds(
00090     float odds) {
00091   std::vector<uint16> result;
00092   result.reserve(kValueCount);
00093   result.push_back(CorrespondenceCostToValue(ProbabilityToCorrespondenceCost(
00094                        ProbabilityFromOdds(odds))) +
00095                    kUpdateMarker);
00096   for (int cell = 1; cell != kValueCount; ++cell) {
00097     result.push_back(
00098         CorrespondenceCostToValue(
00099             ProbabilityToCorrespondenceCost(ProbabilityFromOdds(
00100                 odds * Odds(CorrespondenceCostToProbability(
00101                            (*kValueToCorrespondenceCost)[cell]))))) +
00102         kUpdateMarker);
00103   }
00104   return result;
00105 }
00106 
00107 }  // namespace mapping
00108 }  // namespace cartographer


cartographer
Author(s): The Cartographer Authors
autogenerated on Thu May 9 2019 02:27:35