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