tsd_value_converter.h
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 #ifndef CARTOGRAPHER_MAPPING_TSD_VALUE_CONVERTER_H_
00018 #define CARTOGRAPHER_MAPPING_TSD_VALUE_CONVERTER_H_
00019 
00020 #include <cmath>
00021 #include <vector>
00022 
00023 #include "cartographer/common/math.h"
00024 #include "cartographer/common/port.h"
00025 #include "cartographer/mapping/value_conversion_tables.h"
00026 #include "glog/logging.h"
00027 
00028 namespace cartographer {
00029 namespace mapping {
00030 
00031 // Provides conversions between float and uint16 representations for
00032 // truncated signed distance values and weights.
00033 class TSDValueConverter {
00034  public:
00035   TSDValueConverter(float max_tsd, float max_weight,
00036                     ValueConversionTables* conversion_tables);
00037 
00038   // Converts a tsd to a uint16 in the [1, 32767] range.
00039   inline uint16 TSDToValue(const float tsd) const {
00040     const int value =
00041         common::RoundToInt((ClampTSD(tsd) - min_tsd_) * tsd_resolution_) + 1;
00042     DCHECK_GE(value, 1);
00043     DCHECK_LE(value, 32767);
00044     return value;
00045   }
00046 
00047   // Converts a weight to a uint16 in the [1, 32767] range.
00048   inline uint16 WeightToValue(const float weight) const {
00049     const int value = common::RoundToInt((ClampWeight(weight) - min_weight_) *
00050                                          weight_resolution_) +
00051                       1;
00052     DCHECK_GE(value, 1);
00053     DCHECK_LE(value, 32767);
00054     return value;
00055   }
00056 
00057   // Converts a uint16 (which may or may not have the update marker set) to a
00058   // value in the range [min_tsd_, max_tsd_].
00059   inline float ValueToTSD(const uint16 value) const {
00060     return (*value_to_tsd_)[value];
00061   }
00062 
00063   // Converts a uint16 (which may or may not have the update marker set) to a
00064   // value in the range [min_weight_, max_weight_].
00065   inline float ValueToWeight(const uint16 value) const {
00066     return (*value_to_weight_)[value];
00067   }
00068 
00069   static uint16 getUnknownTSDValue() { return unknown_tsd_value_; }
00070   static uint16 getUnknownWeightValue() { return unknown_weight_value_; }
00071   static uint16 getUpdateMarker() { return update_marker_; }
00072   float getMaxTSD() const { return max_tsd_; }
00073   float getMinTSD() const { return min_tsd_; }
00074   float getMaxWeight() const { return max_weight_; }
00075   float getMinWeight() const { return min_weight_; }
00076 
00077  private:
00078   // Clamps TSD to be in the range [min_tsd_, max_tsd_].
00079   inline float ClampTSD(const float tsd) const {
00080     return common::Clamp(tsd, min_tsd_, max_tsd_);
00081   }
00082   // Clamps weight to be in the range [min_tsd_, max_tsd_].
00083   inline float ClampWeight(const float weight) const {
00084     return common::Clamp(weight, min_weight_, max_weight_);
00085   }
00086 
00087   float max_tsd_;
00088   float min_tsd_;
00089   float max_weight_;
00090   float tsd_resolution_;
00091   float weight_resolution_;
00092   static constexpr float min_weight_ = 0.f;
00093   static constexpr uint16 unknown_tsd_value_ = 0;
00094   static constexpr uint16 unknown_weight_value_ = 0;
00095   static constexpr uint16 update_marker_ = 1u << 15;
00096 
00097   const std::vector<float>* value_to_tsd_;
00098   const std::vector<float>* value_to_weight_;
00099 };
00100 
00101 }  // namespace mapping
00102 }  // namespace cartographer
00103 
00104 #endif  // CARTOGRAPHER_MAPPING_TSD_VALUE_CONVERTER_H_


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