Coverage Control Library
Loading...
Searching...
No Matches
world_idf.h
Go to the documentation of this file.
1/*
2 * This file is part of the CoverageControl library
3 *
4 * Author: Saurav Agarwal
5 * Contact: sauravag@seas.upenn.edu, agr.saurav1@gmail.com
6 * Repository: https://github.com/KumarRobotics/CoverageControl
7 *
8 * Copyright (c) 2024, Saurav Agarwal
9 *
10 * The CoverageControl library is free software: you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or (at your
13 * option) any later version.
14 *
15 * The CoverageControl library is distributed in the hope that it will be
16 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
18 * Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along with
21 * CoverageControl library. If not, see <https://www.gnu.org/licenses/>.
22 */
23
29#ifndef CPPSRC_CORE_INCLUDE_COVERAGECONTROL_WORLD_IDF_H_
30#define CPPSRC_CORE_INCLUDE_COVERAGECONTROL_WORLD_IDF_H_
31
32#define EIGEN_NO_CUDA // Don't use eigen's cuda facility
33#include <Eigen/Dense> // Eigen is used for maps
34 //
35#include <fstream>
36#include <iostream>
37#include <string>
38#include <vector>
39
40#include "CoverageControl/Config.h"
46
47namespace CoverageControl {
48
61class WorldIDF {
62 private:
63 std::vector<BivariateNormalDistribution> normal_distributions_;
64 std::vector<PolygonFeature> polygon_features_;
65 MapType world_map_;
66 Parameters params_;
67 float normalization_factor_ = 0;
68 bool is_cuda_available_ = false;
69
73 template <class PointType = Point2, typename NumType = double>
74 NumType ComputeImportanceBND(PointType const &bottom_left,
75 PointType const &top_right) const {
76 PointType bottom_right(top_right.x(), bottom_left.y());
77 PointType top_left(bottom_left.x(), top_right.y());
78 NumType importance = 0;
79 PointType mid_point = (bottom_left + top_right) / 2.f;
80 float trun_f = static_cast<float>(params_.pTruncationBND);
81 NumType trun_sqr = trun_f * trun_f;
82 for (auto const &normal_distribution : normal_distributions_) {
83 PointType mid_transformed = normal_distribution.TransformPoint(mid_point);
84 if (mid_transformed[0] * mid_transformed[0] +
85 mid_transformed[1] * mid_transformed[1] >
86 trun_sqr) {
87 continue;
88 }
89 importance += normal_distribution.IntegrateQuarterPlane(bottom_left);
90 importance -= normal_distribution.IntegrateQuarterPlane(bottom_right);
91 importance -= normal_distribution.IntegrateQuarterPlane(top_left);
92 importance += normal_distribution.IntegrateQuarterPlane(top_right);
93 }
94 return importance;
95 }
96
97#ifdef COVERAGECONTROL_WITH_CUDA
98 void GenerateMapCuda(float const resolution, float const truncation,
99 int const map_size);
100#endif
101
102 public:
103 explicit WorldIDF(size_t sz) {
104 world_map_ = MapType(sz, sz);
105 if (CudaUtils::InitializeCUDA() == true) {
106 is_cuda_available_ = true;
107 } else {
108 is_cuda_available_ = false;
109 }
110 }
111
112 explicit WorldIDF(Parameters const &params) : WorldIDF(params.pWorldMapSize) {
113 params_ = params;
114 }
115
116 WorldIDF(Parameters const &params, MapType const &world_map)
117 : WorldIDF(params.pWorldMapSize) {
118 params_ = params;
119 if (world_map.rows() != params.pWorldMapSize ||
120 world_map.cols() != params.pWorldMapSize) {
121 std::cout << "Error: World map size does not match the specified size"
122 << std::endl;
123 throw std::invalid_argument(
124 "World map size does not match the specified size");
125 exit(1);
126 }
127 world_map_ = world_map;
128 }
129
130 WorldIDF(Parameters const &params, std::string const &file_name)
131 : WorldIDF(params.pWorldMapSize) {
132 params_ = params;
133 // Load Bivariate Normal Distribution from file
134 params_ = params;
135 std::ifstream file(file_name);
136 if (!file.is_open()) {
137 std::cout << "Error: Could not open file " << file_name << std::endl;
138 exit(1);
139 }
140 std::string type;
141 while (file >> type) {
142 if (type == "BND") {
143 double x, y, sigma_x, sigma_y, rho, scale;
144 file >> x >> y >> sigma_x >> sigma_y >> rho >> scale;
146 Point2(x, y), Point2(sigma_x, sigma_y), rho, scale));
147 } else if (type == "CircularBND") {
148 double x, y, sigma, scale;
149 file >> x >> y >> sigma >> scale;
151 BivariateNormalDistribution(Point2(x, y), sigma, scale));
152 } else if (type == "Uniform") {
153 int num_vertices;
154 file >> num_vertices;
155 double importance;
156 file >>
157 importance; // importance moved here. Be careful with semantic file
158 std::vector<Point2> vertices;
159 for (int i = 0; i < num_vertices; ++i) {
160 double x, y;
161 file >> x >> y;
162 vertices.push_back(Point2(x, y));
163 }
164 AddUniformDistributionPolygon(PolygonFeature(vertices, importance));
165 } else {
166 std::cout << "Error: Unknown feature type " << type << std::endl;
167 exit(1);
168 }
169 }
170 GenerateMap();
171 }
172
174 WorldIDF(WorldIDF const &other) {
175 world_map_ = other.world_map_;
176 params_ = other.params_;
177 normalization_factor_ = other.normalization_factor_;
178 is_cuda_available_ = other.is_cuda_available_;
179 normal_distributions_ = other.normal_distributions_;
180 polygon_features_ = other.polygon_features_;
181 }
182
183 WorldIDF &operator=(WorldIDF const &other) {
184 if (this != &other) {
185 world_map_ = other.world_map_;
186 params_ = other.params_;
187 normalization_factor_ = other.normalization_factor_;
188 is_cuda_available_ = other.is_cuda_available_;
189 normal_distributions_ = other.normal_distributions_;
190 polygon_features_ = other.polygon_features_;
191 }
192 return *this;
193 }
194
195 void LoadMap(std::string const &file_name) {
196 std::ifstream file(file_name);
197 if (!file.is_open()) {
198 std::cout << "Error: Could not open file " << file_name << std::endl;
199 exit(1);
200 }
201 for (int i = 0; i < params_.pWorldMapSize; ++i) {
202 for (int j = 0; j < params_.pWorldMapSize; ++j) {
203 file >> world_map_(i, j);
204 }
205 }
206 }
207
210 polygon_features_.push_back(poly_feature);
211 }
212
215 normal_distributions_.push_back(distribution);
216 }
217
220 std::vector<BivariateNormalDistribution> const &dists) {
221 normal_distributions_.reserve(normal_distributions_.size() + dists.size());
222 for (auto const &dist : dists) {
223 normal_distributions_.push_back(dist);
224 }
225 }
226
227 void GenerateMap() {
228 /* std::cout << "Generating map" << std::endl; */
229#ifdef COVERAGECONTROL_WITH_CUDA
230 /* GenerateMapCuda(); */
231 if (is_cuda_available_) {
232 GenerateMapCuda();
233 } else {
235 }
236#else
238#endif
239 }
241 int WriteWorldMap(std::string const &file_name) const {
242 return MapUtils::WriteMap(world_map_, file_name);
243 }
244
245 void GetSubWorldMap(Point2 const &pos, int const sensor_size,
246 MapType &submap) const {
247 MapUtils::GetSubMap(params_.pResolution, pos, params_.pWorldMapSize,
248 world_map_, sensor_size, submap);
249 }
250
251 void PrintMapSize() const {
252 std::cout << "World map size: " << world_map_.rows() << " "
253 << world_map_.cols() << std::endl;
254 }
255
256 auto GetNormalizationFactor() const { return normalization_factor_; }
257
258 const MapType &GetWorldMap() const { return world_map_; }
259
260 MapType &GetWorldMapMutable() { return world_map_; }
261
262 int WriteDistributions(std::string const &file_name) const;
263
264 auto GetNumFeatures() const {
265 return normal_distributions_.size() + polygon_features_.size();
266 }
267
268 void GenerateMapCPU();
269
270#ifdef COVERAGECONTROL_WITH_CUDA
271 void GenerateMapCuda() {
272 /* std::cout << "Generating map using CUDA" << std::endl; */
273 GenerateMapCuda(static_cast<float>(params_.pResolution),
274 static_cast<float>(params_.pTruncationBND),
275 static_cast<int>(params_.pWorldMapSize));
276 }
277#endif
278};
279
280} /* namespace CoverageControl */
281#endif // CPPSRC_CORE_INCLUDE_COVERAGECONTROL_WORLD_IDF_H_
Class for Bivariate Normal Distribution.
static bool InitializeCUDA()
Initializes a CUDA device use_cuda_ must be set to true before calling this function.
Definition cuda_utils.h:91
Class to store parameters.
Definition parameters.h:48
Class for Importance Density Function (IDF) for the world.
Definition world_idf.h:61
void GetSubWorldMap(Point2 const &pos, int const sensor_size, MapType &submap) const
Definition world_idf.h:245
MapType & GetWorldMapMutable()
Definition world_idf.h:260
int WriteDistributions(std::string const &file_name) const
void AddNormalDistribution(std::vector< BivariateNormalDistribution > const &dists)
Definition world_idf.h:219
auto GetNumFeatures() const
Definition world_idf.h:264
WorldIDF & operator=(WorldIDF const &other)
Definition world_idf.h:183
auto GetNormalizationFactor() const
Definition world_idf.h:256
const MapType & GetWorldMap() const
Definition world_idf.h:258
WorldIDF(Parameters const &params, MapType const &world_map)
Definition world_idf.h:116
void PrintMapSize() const
Definition world_idf.h:251
void LoadMap(std::string const &file_name)
Definition world_idf.h:195
void AddNormalDistribution(BivariateNormalDistribution const &distribution)
Definition world_idf.h:214
int WriteWorldMap(std::string const &file_name) const
Definition world_idf.h:241
void AddUniformDistributionPolygon(PolygonFeature const &poly_feature)
Definition world_idf.h:209
WorldIDF(WorldIDF const &other)
Definition world_idf.h:174
WorldIDF(Parameters const &params, std::string const &file_name)
Definition world_idf.h:130
WorldIDF(Parameters const &params)
Definition world_idf.h:112
Utility functions for CUDA.
Eigen::Matrix< float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > MapType
Definition typedefs.h:48
Eigen::Vector2d Point2
Definition typedefs.h:44
Utility functions for transforming maps.
void GetSubMap(double const resolution, Point2 const &pos, int const map_size, T const &map, int const submap_size, T &submap)
Definition map_utils.h:89
int WriteMap(MapType const &map, std::string const &file_name)
Write the world map to a file.
Definition map_utils.h:109
Namespace for the CoverageControl library.
Contains parameters.
A struct to store a polygon feature and a uniform importance value.
Definition typedefs.h:56
Contains typedefs for the library.