test_helpers.cpp
Go to the documentation of this file.
1 /*
2  * common.cpp
3  *
4  * Created on: Nov 8, 2019
5  * Author: Edo Jelavic
6  * Institute: ETH Zurich, Robotic Systems Lab
7  */
8 
9 #include <ros/package.h>
10 
12 
13 #include "test_helpers.hpp"
14 
15 namespace grid_map {
16 namespace grid_map_pcl_test {
17 
18 std::mt19937 rndGenerator;
19 
20 std::string getConfigFilePath() {
21  std::string filename = getTestDataFolderPath() + "/parameters.yaml";
22  return filename;
23 }
24 
25 std::string getTestDataFolderPath() {
26  std::string filename = ros::package::getPath("grid_map_pcl") + "/test/test_data";
27  return filename;
28 }
29 
30 std::vector<Eigen::Vector3d> getNonNanElevationValuesWithCoordinates(const grid_map::GridMap& gridMap) {
31  std::vector<Eigen::Vector3d> nonNanCoordinates;
32  for (grid_map::GridMapIterator iterator(gridMap); !iterator.isPastEnd(); ++iterator) {
33  double value = gridMap.at(layerName, *iterator);
34  if (!std::isnan(value)) {
35  grid_map::Position position;
36  gridMap.getPosition(grid_map::Index(*iterator), position);
37  nonNanCoordinates.push_back(Eigen::Vector3d(position.x(), position.y(), value));
38  }
39  }
40 
41  return nonNanCoordinates;
42 }
43 
44 std::vector<double> getNonNanElevationValues(const grid_map::GridMap& gridMap) {
45  std::vector<double> nonNanElevations;
46  for (grid_map::GridMapIterator iterator(gridMap); !iterator.isPastEnd(); ++iterator) {
47  double value = gridMap.at(layerName, *iterator);
48  if (!std::isnan(value)) {
49  nonNanElevations.push_back(value);
50  }
51  }
52 
53  return nonNanElevations;
54 }
55 
56 Pointcloud::Ptr createNormallyDistributedBlobOfPoints(unsigned int nPoints, double mean, double stdDev, std::mt19937* generator) {
57  std::normal_distribution<double> normalDist(mean, stdDev); // N
58 
59  Pointcloud::Ptr cloud(new Pointcloud());
60  cloud->points.reserve(nPoints);
61  for (unsigned int i = 0; i < nPoints; ++i) {
62  Point point;
63  point.x = normalDist(*generator);
64  point.y = normalDist(*generator);
65  point.z = normalDist(*generator);
66  cloud->push_back(point);
67  }
68 
69  return cloud;
70 }
71 
72 Pointcloud::Ptr createNoisyPlanePointcloud(unsigned int nPoints, double minXY, double maxXY, double meanZ, double stdDevZ,
73  std::mt19937* generator) {
74  const double upperBound = maxXY;
75  const double lowerBound = minXY;
76  std::uniform_real_distribution<double> uniformDist(lowerBound, upperBound);
77  const double mean = meanZ;
78  const double stdDev = stdDevZ;
79  std::normal_distribution<double> normalDist(mean, stdDev); // N
80  // std::cout << distr(generator) << '\n';
81 
82  Pointcloud::Ptr cloud(new Pointcloud());
83  cloud->points.reserve(nPoints);
84  for (unsigned int i = 0; i < nPoints; ++i) {
85  Point point;
86  point.x = uniformDist(*generator);
87  point.y = uniformDist(*generator);
88  point.z = normalDist(*generator);
89  cloud->push_back(point);
90  }
91 
92  return cloud;
93 }
94 
95 Pointcloud::Ptr createPerfectPlane(unsigned int nPoints, double minXY, double maxXY, double desiredHeight, std::mt19937* generator) {
96  const double upperBound = maxXY;
97  const double lowerBound = minXY;
98  std::uniform_real_distribution<double> uniformDist(lowerBound, upperBound);
99 
100  Pointcloud::Ptr cloud(new Pointcloud());
101  cloud->points.reserve(nPoints);
102  for (unsigned int i = 0; i < nPoints; ++i) {
103  Point point;
104  point.x = uniformDist(*generator);
105  point.y = uniformDist(*generator);
106  point.z = desiredHeight;
107  cloud->push_back(point);
108  }
109 
110  return cloud;
111 }
112 
113 Pointcloud::Ptr concatenate(Pointcloud::Ptr cloud1, Pointcloud::Ptr cloud2) {
114  // ghetto concatenate
115  Pointcloud::Ptr concatenatedCloud(new grid_map_pcl_test::Pointcloud());
116  concatenatedCloud->points.reserve(cloud1->points.size() + cloud2->points.size());
117 
118  for (const auto& point : cloud2->points) {
119  concatenatedCloud->push_back(point);
120  }
121 
122  for (const auto& point : cloud1->points) {
123  concatenatedCloud->push_back(point);
124  }
125 
126  return concatenatedCloud;
127 }
128 
129 void runGridMapPclLoaderOnInputCloud(Pointcloud::ConstPtr inputCloud, grid_map::GridMapPclLoader* gridMapPclLoader) {
130  gridMapPclLoader->loadParameters(getConfigFilePath());
131  gridMapPclLoader->setInputCloud(inputCloud);
132  gridMapPclLoader->preProcessInputCloud();
133  gridMapPclLoader->initializeGridMapGeometryFromInputCloud();
134  gridMapPclLoader->addLayerFromInputCloud(layerName);
135 }
136 
137 void runGridMapPclLoaderOnInputCloudAndSavePointCloud(Pointcloud::ConstPtr inputCloud, grid_map::GridMapPclLoader* gridMapPclLoader,
138  const std::string& filename) {
139  runGridMapPclLoaderOnInputCloud(inputCloud, gridMapPclLoader);
140  gridMapPclLoader->savePointCloudAsPcdFile(filename);
141 }
142 
143 Pointcloud::Ptr createStepTerrain(unsigned int nPoints, double minXY, double maxXY, double zHigh, double zLow, double stdDevZ,
144  std::mt19937* generator, double* center) {
145  *center = (maxXY + minXY) / 2.0;
146  std::uniform_real_distribution<double> uniformDist(minXY, maxXY);
147  std::normal_distribution<double> zLowDist(zLow, stdDevZ);
148  std::normal_distribution<double> zHighDist(zHigh, stdDevZ);
149 
150  Pointcloud::Ptr cloud(new Pointcloud());
151  cloud->points.reserve(nPoints);
152  for (unsigned int i = 0; i < nPoints; ++i) {
153  Point point;
154  point.x = uniformDist(*generator);
155  point.y = uniformDist(*generator);
156 
157  if (point.x > *center) {
158  point.z = zHighDist(*generator);
159  } else {
160  point.z = zLowDist(*generator);
161  }
162 
163  cloud->push_back(point);
164  }
165 
166  return cloud;
167 }
171  }
172 }
173 
174 std::string getTestPcdFilePath() {
175  std::string filename = ros::package::getPath("grid_map_pcl") + "/test/test_data/plane_noisy.pcd";
176  return filename;
177 }
178 
179 } /* namespace grid_map_pcl_test */
180 } /* namespace grid_map*/
std::vector< Eigen::Vector3d > getNonNanElevationValuesWithCoordinates(const grid_map::GridMap &gridMap)
void loadParameters(const std::string &filename)
Eigen::Array2i Index
filename
Pointcloud::Ptr createNoisyPlanePointcloud(unsigned int nPoints, double minXY, double maxXY, double meanZ, double stdDevZ, std::mt19937 *generator)
ROSCONSOLE_DECL void notifyLoggerLevelsChanged()
bool getPosition(const Index &index, Position &position) const
void runGridMapPclLoaderOnInputCloud(Pointcloud::ConstPtr inputCloud, grid_map::GridMapPclLoader *gridMapPclLoader)
void runGridMapPclLoaderOnInputCloudAndSavePointCloud(Pointcloud::ConstPtr inputCloud, grid_map::GridMapPclLoader *gridMapPclLoader, const std::string &filename)
std::vector< double > getNonNanElevationValues(const grid_map::GridMap &gridMap)
Eigen::Vector2d Position
void addLayerFromInputCloud(const std::string &layer)
void setInputCloud(Pointcloud::ConstPtr inputCloud)
void setVerbosityLevel(ros::console::levels::Level level)
float & at(const std::string &layer, const Index &index)
ROSLIB_DECL std::string getPath(const std::string &package_name)
static const std::string layerName
void savePointCloudAsPcdFile(const std::string &filename) const
std::string getTestDataFolderPath()
Pointcloud::Ptr createStepTerrain(unsigned int nPoints, double minXY, double maxXY, double zHigh, double zLow, double stdDevZ, std::mt19937 *generator, double *center)
Pointcloud::Ptr concatenate(Pointcloud::Ptr cloud1, Pointcloud::Ptr cloud2)
Pointcloud::Ptr createNormallyDistributedBlobOfPoints(unsigned int nPoints, double mean, double stdDev, std::mt19937 *generator)
Pointcloud::Ptr createPerfectPlane(unsigned int nPoints, double minXY, double maxXY, double desiredHeight, std::mt19937 *generator)
ROSCONSOLE_DECL bool set_logger_level(const std::string &name, levels::Level level)
pcl::PointCloud< Point > Pointcloud
#define ROSCONSOLE_DEFAULT_NAME


grid_map_pcl
Author(s): Dominic Jud , Edo Jelavic
autogenerated on Tue Jun 1 2021 02:13:43