value_conversion_tables.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright 2018 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/value_conversion_tables.h"
00018 
00019 #include "absl/memory/memory.h"
00020 #include "glog/logging.h"
00021 
00022 namespace cartographer {
00023 namespace mapping {
00024 namespace {
00025 
00026 constexpr uint16 kUpdateMarker = 1u << 15;
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_LE(value, 32767);
00034   if (value == unknown_value) return unknown_result;
00035   const float kScale = (upper_bound - lower_bound) / 32766.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   size_t num_values = std::numeric_limits<uint16>::max() + 1;
00044   result->reserve(num_values);
00045   for (size_t value = 0; value != num_values; ++value) {
00046     result->push_back(SlowValueToBoundedFloat(
00047         static_cast<uint16>(value) & ~kUpdateMarker, unknown_value,
00048         unknown_result, lower_bound, upper_bound));
00049   }
00050   return result;
00051 }
00052 }  // namespace
00053 
00054 const std::vector<float>* ValueConversionTables::GetConversionTable(
00055     float unknown_result, float lower_bound, float upper_bound) {
00056   std::tuple<float, float, float> bounds =
00057       std::make_tuple(unknown_result, lower_bound, upper_bound);
00058   auto lookup_table_iterator = bounds_to_lookup_table_.find(bounds);
00059   if (lookup_table_iterator == bounds_to_lookup_table_.end()) {
00060     auto insertion_result = bounds_to_lookup_table_.emplace(
00061         bounds, PrecomputeValueToBoundedFloat(0, unknown_result, lower_bound,
00062                                               upper_bound));
00063     return insertion_result.first->second.get();
00064   } else {
00065     return lookup_table_iterator->second.get();
00066   }
00067 }
00068 
00069 }  // namespace mapping
00070 }  // namespace cartographer


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