Coverage Control Library
Loading...
Searching...
No Matches
world_idf.cpp
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
33
34namespace CoverageControl {
35
39 /* std::cout << "Generating map using CPU" << std::endl; */
40 std::vector<Polygon_2> cgal_polygons;
41 cgal_polygons.reserve(polygon_features_.size());
42 for (auto const &poly : polygon_features_) {
43 Polygon_2 cgal_poly;
44 for (auto const &pt : poly.poly) {
45 cgal_poly.push_back(CGAL_Point2(pt.x(), pt.y()));
46 }
47 cgal_polygons.push_back(cgal_poly);
48 }
49 float max_importance = 0;
50 float res = static_cast<float>(params_.pResolution);
51 for (int i = 0; i < params_.pWorldMapSize; ++i) { // Row (x index)
52 float x1 = res * i; // Left x-coordinate of pixel
53 float x2 = x1 + res; // Right x-coordinate of pixel
54 for (int j = 0; j < params_.pWorldMapSize; ++j) { // Column (y index)
55 float y1 = res * j; // Lower y-coordinate of pixel
56 float y2 = y1 + res; // Upper y-coordinate of pixel
57 Point2f pt1(x1, y1);
58 Point2f pt2(x2, y2);
59 float importance = ComputeImportanceBND<Point2f, float>(pt1, pt2);
60 /* importance += ComputeImportancePoly(pt1, pt2); */
61 CGAL_Point2 mid((x1 + x2) / 2, (y1 + y2) / 2);
62 float importance_poly = 0;
63 for (size_t k = 0; k < cgal_polygons.size(); ++k) {
64 auto &poly = cgal_polygons[k];
65 if (poly.bounded_side(mid) == CGAL::ON_BOUNDED_SIDE) {
66 importance_poly = std::max(importance_poly, polygon_features_[k].imp);
67 }
68 }
69 importance += importance_poly;
70 /* auto importance = ComputeImportanceBND(pt1, pt2); */
71 /* if (std::abs(importance) < kEps) { */
72 /* importance = 0; */
73 /* } */
74 world_map_(i, j) = importance;
75 if (importance > max_importance) {
76 max_importance = importance;
77 }
78 }
79 }
80
81 if (max_importance < kEps) {
82 normalization_factor_ = static_cast<float>(params_.pNorm);
83 } else {
84 normalization_factor_ = static_cast<float>(params_.pNorm) / max_importance;
85 }
86
87 if (not(normalization_factor_ > 1e-5)) { // Parity with CUDA code
88 return;
89 }
90
91 // Normalize the world map
92#pragma omp parallel for
93 for (int i = 0; i < params_.pWorldMapSize; ++i) {
94 for (int j = 0; j < params_.pWorldMapSize; ++j) {
95 world_map_(i, j) *= normalization_factor_;
96 }
97 }
98}
99
100int WorldIDF::WriteDistributions(std::string const &file_name) const {
101 std::ofstream file(file_name);
102 if (!file.is_open()) {
103 std::cerr << "Could not open file: " << file_name << std::endl;
104 return -1;
105 }
106 file << std::setprecision(kMaxPrecision);
107 for (auto const &dist : normal_distributions_) {
108 Point2 sigma = dist.GetSigma();
109 if (sigma.x() == sigma.y()) {
110 file << "CircularBND" << std::endl;
111 file << dist.GetMean().x() << " " << dist.GetMean().y() << " "
112 << sigma.x() << " " << dist.GetScale() << std::endl;
113 } else {
114 file << "BND" << std::endl;
115 file << dist.GetMean().x() << " " << dist.GetMean().y() << " "
116 << dist.GetSigma().x() << " " << dist.GetSigma().y() << " "
117 << dist.GetRho() << " " << dist.GetScale() << std::endl;
118 }
119 }
120 for (auto const &poly : polygon_features_) {
121 file << "Uniform" << std::endl;
122 file << poly.poly.size() << " " << poly.imp << std::endl;
123 for (auto const &pt : poly.poly) {
124 file << pt.x() << " " << pt.y() << std::endl;
125 }
126 }
127 file.close();
128 return 0;
129}
130} // namespace CoverageControl
Contains the configuration for the CGAL library.
Constants for the CoverageControl library.
double const kEps
Definition constants.h:49
constexpr auto kMaxPrecision
Definition constants.h:57
Eigen::Vector2f Point2f
Definition typedefs.h:45
Namespace for the CoverageControl library.
Provides utilities for polygon manipulation using CGAL.
Contains the class for Importance Density Function (IDF) for the world.