Coverage Control Library
Loading...
Searching...
No Matches
voronoi.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_VORONOI_H_
30#define CPPSRC_CORE_INCLUDE_COVERAGECONTROL_VORONOI_H_
31
32#include <cmath>
33#include <memory>
34#include <vector>
35
37
38namespace CoverageControl {
39
56
57 private:
58 double mass_ = 0;
59 Point2 centroid_;
60 double sum_idf_site_dist_sqr_ = 0;
61 double sum_idf_goal_dist_sqr_ = 0;
62 double sum_idf_site_dist_ = 0;
63 double sum_idf_goal_dist_ = 0;
64
65 public:
66 Point2 centroid() const { return centroid_; }
67 double mass() const { return mass_; }
68 double sum_idf_site_dist() const { return sum_idf_site_dist_; }
69 double sum_idf_site_dist_sqr() const { return sum_idf_site_dist_sqr_; }
70 double sum_idf_goal_dist() const { return sum_idf_goal_dist_; }
71 double sum_idf_goal_dist_sqr() const { return sum_idf_goal_dist_sqr_; }
72
73 std::vector<double> GetFeatureVector() const {
74 return std::vector<double>{
75 centroid_.x(), centroid_.y(), mass_,
76 sum_idf_site_dist_sqr_, sum_idf_goal_dist_sqr_, sum_idf_site_dist_,
77 sum_idf_goal_dist_};
78 }
79
80 void SetZero() {
81 mass_ = 0;
82 centroid_ = Point2{0, 0};
83 origin_shift = Point2{0, 0};
84 sum_idf_site_dist_ = 0;
85 sum_idf_site_dist_sqr_ = 0;
86 sum_idf_goal_dist_ = 0;
87 sum_idf_goal_dist_sqr_ = 0;
88 }
89 void MassCentroidFunctional(double const &map_val, Point2 pt) {
90 pt = pt + origin_shift;
91 mass_ += map_val;
92 centroid_ += pt * map_val;
93 sum_idf_site_dist_ += (pt - site).norm() * map_val;
94 sum_idf_site_dist_sqr_ += (pt - site).squaredNorm() * map_val;
95 }
96 void GoalObjFunctional(double const &map_val, Point2 pt) {
97 pt = pt + origin_shift;
98 sum_idf_goal_dist_sqr_ += (pt - centroid_).squaredNorm() * map_val;
99 sum_idf_goal_dist_ += (pt - centroid_).norm() * map_val;
100 }
101
103};
104
116class Voronoi {
117 private:
118 PointVector sites_;
119 std::shared_ptr<const MapType> map_ = nullptr;
120 Point2 map_size_;
121 double resolution_ = 0;
122 Point2 origin_shift_;
123
124 // compute_single_ is used to determine if the voronoi mass and centroid needs
125 // to be computed for only a single site, given by robot_id_. This is useful
126 // for distributed voronoi computation The result is stored in voronoi_cell_
127 // (as opposed to voronoi_cells_ when compute_single_ = false)
128 bool compute_single_ = false;
129 int robot_id_ = 0;
130 VoronoiCell voronoi_cell_;
131
132 int num_sites_;
133 std::vector<VoronoiCell> voronoi_cells_;
134 void ComputeMassCentroid(VoronoiCell &);
135 void ComputeMassCentroid2(VoronoiCell &);
136 void MassCentroidFunctional(VoronoiCell &vcell, double const &map_val,
137 Point2 const &pt);
138 void CellNavigator(VoronoiCell const &, std::function<void(double, Point2)>);
139
140 /* std::vector <Edge> voronoi_edges_; */
141 public:
143 Voronoi(PointVector const &sites, MapType const &map, Point2 map_size,
144 double const &resolution, bool const compute_single = false,
145 int const robot_id = 0)
146 : sites_{sites},
147 map_size_{map_size},
148 resolution_{resolution},
149 compute_single_{compute_single},
150 robot_id_{robot_id} {
151 /* std::cout << "map size" << std::endl; */
152 /* std::cout << map_size_.x() << " " << map_size_.y() << std::endl; */
153 map_ = std::make_shared<const MapType>(map);
154 num_sites_ = sites_.size();
155 /* double shortest_dist = std::numeric_limits<double>::max(); */
156 /* for (int i = 0; i < num_sites_; ++i) { */
157 /* auto const &site = sites[i]; */
158 /* for (int j = i + 1; j < num_sites_; ++j) { */
159 /* auto const &site2 = sites[j]; */
160 /* double dist = (site - site2).norm(); */
161 /* if (dist < shortest_dist) { */
162 /* shortest_dist = dist; */
163 /* } */
164 /* } */
165 /* } */
166 /* std::cout << "Shortest dist between sites: " << shortest_dist <<
167 * std::endl; */
168 if (compute_single_ == false) {
169 voronoi_cells_.resize(num_sites_);
170 } else {
171 origin_shift_ = -sites[0];
172 }
173
175 }
176
177 // Update the cites and recompute the voronoi
178 void UpdateSites(PointVector const &sites) {
179 sites_ = sites;
180 num_sites_ = sites_.size();
182 }
183
184 void ComputeVoronoiCells();
185 auto GetVoronoiCells() const { return voronoi_cells_; }
186 auto GetVoronoiCell() { return voronoi_cell_; }
187
189 double obj = 0;
190 for (auto const &cell : voronoi_cells_) {
191 obj = obj + cell.sum_idf_site_dist_sqr();
192 }
193 return obj;
194 }
196 double obj = 0;
197 for (auto const &cell : voronoi_cells_) {
198 obj = obj + cell.sum_idf_goal_dist_sqr();
199 }
200 return obj;
201 }
202 /* auto GetVoronoiEdges() {return voronoi_edges_;} */
203};
204
205} /* namespace CoverageControl */
206#endif // CPPSRC_CORE_INCLUDE_COVERAGECONTROL_VORONOI_H_
Class for computing Voronoi cells.
Definition voronoi.h:116
Voronoi(PointVector const &sites, MapType const &map, Point2 map_size, double const &resolution, bool const compute_single=false, int const robot_id=0)
Definition voronoi.h:143
auto GetVoronoiCells() const
Definition voronoi.h:185
double GetSumIDFSiteDistSqr()
Definition voronoi.h:188
void UpdateSites(PointVector const &sites)
Definition voronoi.h:178
double GetSumIDFGoalDistSqr()
Definition voronoi.h:195
Eigen::Matrix< float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > MapType
Definition typedefs.h:48
std::vector< Point2 > PointVector
Definition typedefs.h:51
Eigen::Vector2d Point2
Definition typedefs.h:44
Namespace for the CoverageControl library.
Struct for Voronoi cell.
Definition voronoi.h:52
double sum_idf_goal_dist_sqr() const
Definition voronoi.h:71
double sum_idf_goal_dist() const
Definition voronoi.h:70
void MassCentroidFunctional(double const &map_val, Point2 pt)
Definition voronoi.h:89
std::vector< double > GetFeatureVector() const
Definition voronoi.h:73
double sum_idf_site_dist() const
Definition voronoi.h:68
void GoalObjFunctional(double const &map_val, Point2 pt)
Definition voronoi.h:96
double sum_idf_site_dist_sqr() const
Definition voronoi.h:69
Point2 centroid() const
Definition voronoi.h:66
Contains typedefs for the library.