map_server_comparison.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2020, Locus Robotics
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 #include <gtest/gtest.h>
35 #include <map_server/image_loader.h>
37 #include <ros/package.h>
38 #include <string>
39 #include <vector>
40 
41 std::vector<std::string> filenames = {"spectrum.png", "spectrum.pgm"};
42 
43 bool compareToClassic(const std::string& filename, bool should_negate, double occ_th, double free_th, MapMode mode)
44 {
45  const double resolution = 0.1;
46  double origin[3] = {0.0, 0.0, 0.0};
47  nav_msgs::GetMap::Response map_resp;
48 
49  std::string path = ros::package::getPath("nav_grid_server") + "/test/data/" + filename;
50 
51  map_server::loadMapFromFile(&map_resp, path.c_str(), resolution, should_negate, occ_th, free_th, origin, mode);
52 
53  std::string mode_str;
54  if (mode == RAW)
55  {
56  mode_str = "raw";
57  }
58  else if (mode == TRINARY)
59  {
60  mode_str = "trinary";
61  }
62  else
63  {
64  mode_str = "scale";
65  }
66 
68  nav_grid_server::classicLoadMapFromFile(path, resolution, should_negate, occ_th, free_th, mode_str);
69 
70  if (the_map.size() != map_resp.map.data.size())
71  {
72  return false;
73  }
74  unsigned int correct = 0;
75  for (unsigned int i=0; i < the_map.size(); i++)
76  {
77  unsigned char original = map_resp.map.data[i];
78  unsigned char updated = the_map[i];
79  if (original == updated)
80  correct += 1;
81  }
82  if (correct != the_map.size())
83  {
84  printf("%.2f correct\n", correct * 100.0 / the_map.size());
85  for (unsigned int i=0; i < the_map.size(); i++)
86  {
87  unsigned char original = map_resp.map.data[i];
88  unsigned char updated = the_map[i];
89  if (original == updated)
90  correct += 1;
91  printf("%d) %d %d %c\n", i, updated, original, updated == original ? ' ' : 'x');
92  }
93  }
94 
95  return correct == the_map.size();
96 }
97 
98 TEST(MapServerComparison, raw)
99 {
100  for (auto& filename : filenames)
101  {
102  EXPECT_TRUE(compareToClassic(filename, false, 0.9, 0.1, RAW)) << filename;
103  EXPECT_TRUE(compareToClassic(filename, true, 0.9, 0.1, RAW)) << filename;
104  }
105 }
106 
107 TEST(MapServerComparison, trinary)
108 {
109  for (auto& filename : filenames)
110  {
111  EXPECT_TRUE(compareToClassic(filename, true, 0.65, 0.196, TRINARY)) << filename;
112  EXPECT_TRUE(compareToClassic(filename, true, 0.85, 0.15, TRINARY)) << filename;
113  EXPECT_TRUE(compareToClassic(filename, false, 0.65, 0.196, TRINARY)) << filename;
114  EXPECT_TRUE(compareToClassic(filename, false, 0.85, 0.15, TRINARY)) << filename;
115  }
116 }
117 
118 TEST(MapServerComparison, scale)
119 {
120  for (auto& filename : filenames)
121  {
122  EXPECT_TRUE(compareToClassic(filename, true, 0.65, 0.196, SCALE)) << filename;
123  EXPECT_TRUE(compareToClassic(filename, true, 0.85, 0.15, SCALE)) << filename;
124  EXPECT_TRUE(compareToClassic(filename, false, 0.65, 0.196, SCALE)) << filename;
125  EXPECT_TRUE(compareToClassic(filename, false, 0.85, 0.15, SCALE)) << filename;
126  }
127 }
128 
129 int main(int argc, char** argv)
130 {
131  testing::InitGoogleTest(&argc, argv);
132  return RUN_ALL_TESTS();
133 }
nav_grid::VectorNavGrid< unsigned char > classicLoadMapFromFile(const std::string &filepath, const double resolution, const bool negate_param, const double occ_th, const double free_th, const std::string &mode)
Load an image from a file, mimicking map_server&#39;s loading style Resulting values are [0...
filename
bool compareToClassic(const std::string &filename, bool should_negate, double occ_th, double free_th, MapMode mode)
TEST(MapServerComparison, raw)
std::vector< std::string > filenames
ROSLIB_DECL std::string getPath(const std::string &package_name)
int main(int argc, char **argv)
unsigned int size() const


nav_grid_server
Author(s):
autogenerated on Sun Jan 10 2021 04:08:55