interpolated_tsdf_2d_test.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/internal/2d/scan_matching/interpolated_tsdf_2d.h"
00018 
00019 #include "Eigen/Core"
00020 #include "gtest/gtest.h"
00021 
00022 namespace cartographer {
00023 namespace mapping {
00024 namespace scan_matching {
00025 namespace {
00026 
00027 class InterpolatedTSDF2DTest : public ::testing::Test {
00028  protected:
00029   InterpolatedTSDF2DTest()
00030       : tsdf_(MapLimits(1., Eigen::Vector2d(5.5, 5.5), CellLimits(10, 10)),
00031               1.0f, 10.0f, &conversion_tables_),
00032         interpolated_tsdf_(tsdf_) {}
00033 
00034   float GetUninterpolatedCorrespondenceCost(float x, float y) const {
00035     return tsdf_.GetCorrespondenceCost(
00036         tsdf_.limits().GetCellIndex(Eigen::Vector2f(x, y)));
00037   }
00038 
00039   float GetUninterpolatedWeight(float x, float y) const {
00040     return tsdf_.GetWeight(tsdf_.limits().GetCellIndex(Eigen::Vector2f(x, y)));
00041   }
00042 
00043   ValueConversionTables conversion_tables_;
00044   TSDF2D tsdf_;
00045   InterpolatedTSDF2D interpolated_tsdf_;
00046 };
00047 
00048 TEST_F(InterpolatedTSDF2DTest, InterpolatesGridPoints) {
00049   std::vector<Eigen::Vector2f> inner_points = {
00050       Eigen::Vector2f(1.f, 1.f), Eigen::Vector2f(2.f, 1.f),
00051       Eigen::Vector2f(1.f, 2.f), Eigen::Vector2f(2.f, 2.f)};
00052   for (const Eigen::Vector2f& point : inner_points) {
00053     tsdf_.SetCell(tsdf_.limits().GetCellIndex(point), 0.1f, 1.0f);
00054   }
00055   // Insert surrounding points.
00056   for (size_t x = 0; x < 4; ++x) {
00057     tsdf_.SetCell(tsdf_.limits().GetCellIndex(
00058                       Eigen::Vector2f(static_cast<float>(x), 0.f)),
00059                   0.1f, 1.0f);
00060     tsdf_.SetCell(tsdf_.limits().GetCellIndex(
00061                       Eigen::Vector2f(static_cast<float>(x), 3.f)),
00062                   0.1f, 1.0f);
00063   }
00064   for (size_t y = 1; y < 3; ++y) {
00065     tsdf_.SetCell(tsdf_.limits().GetCellIndex(
00066                       Eigen::Vector2f(0.f, static_cast<float>(y))),
00067                   0.1f, 1.0f);
00068     tsdf_.SetCell(tsdf_.limits().GetCellIndex(
00069                       Eigen::Vector2f(3.f, static_cast<float>(y))),
00070                   0.1f, 1.0f);
00071   }
00072   for (const Eigen::Vector2f& point : inner_points) {
00073     EXPECT_NEAR(
00074         GetUninterpolatedCorrespondenceCost(point[0], point[1]),
00075         interpolated_tsdf_.GetCorrespondenceCost(static_cast<double>(point[0]),
00076                                                  static_cast<double>(point[1])),
00077         1e-4);
00078     EXPECT_NEAR(GetUninterpolatedWeight(point[0], point[1]),
00079                 interpolated_tsdf_.GetWeight(static_cast<double>(point[0]),
00080                                              static_cast<double>(point[1])),
00081                 1e-4);
00082   }
00083   // Check unknown cell.
00084   EXPECT_NEAR(tsdf_.GetMaxCorrespondenceCost(),
00085               interpolated_tsdf_.GetCorrespondenceCost(3.0, 2.0), 1e-4);
00086   EXPECT_NEAR(GetUninterpolatedWeight(3.f, 2.f),
00087               interpolated_tsdf_.GetWeight(3.0, 2.0), 1e-4);
00088 }
00089 
00090 TEST_F(InterpolatedTSDF2DTest, InterpolatesWithinCell) {
00091   const float tsd_00 = 0.1f;
00092   const float tsd_01 = 0.2f;
00093   const float tsd_10 = 0.3f;
00094   const float tsd_11 = 0.4f;
00095   const float w_00 = 1.f;
00096   const float w_01 = 2.f;
00097   const float w_10 = 3.f;
00098   const float w_11 = 4.f;
00099 
00100   tsdf_.SetCell(tsdf_.limits().GetCellIndex(Eigen::Vector2f(0.f, 0.f)), tsd_00,
00101                 w_00);
00102   tsdf_.SetCell(tsdf_.limits().GetCellIndex(Eigen::Vector2f(0.f, 1.f)), tsd_01,
00103                 w_01);
00104   tsdf_.SetCell(tsdf_.limits().GetCellIndex(Eigen::Vector2f(1.f, 0.f)), tsd_10,
00105                 w_10);
00106   tsdf_.SetCell(tsdf_.limits().GetCellIndex(Eigen::Vector2f(1.f, 1.f)), tsd_11,
00107                 w_11);
00108 
00109   const double kSampleStep = tsdf_.limits().resolution() / 100.;
00110   for (double x = 0. + kSampleStep; x < 1.; x += tsdf_.limits().resolution()) {
00111     for (double y = 0. + kSampleStep; y < 1.;
00112          y += tsdf_.limits().resolution()) {
00113       const float tsd_expected = (x * tsd_10 + (1.f - x) * tsd_00) * (1.f - y) +
00114                                  (x * tsd_11 + (1.f - x) * tsd_01) * y;
00115       EXPECT_NEAR(interpolated_tsdf_.GetCorrespondenceCost(x, y), tsd_expected,
00116                   1e-3);
00117       const float w_expected = (x * w_10 + (1.f - x) * w_00) * (1.f - y) +
00118                                (x * w_11 + (1.f - x) * w_01) * y;
00119       EXPECT_NEAR(interpolated_tsdf_.GetWeight(x, y), w_expected, 1e-3);
00120     }
00121   }
00122 }
00123 
00124 }  // namespace
00125 }  // namespace scan_matching
00126 }  // namespace mapping
00127 }  // namespace cartographer


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