Coverage Control Library
Loading...
Searching...
No Matches
plotter.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
29#include "CoverageControl/extern/gnuplot/gnuplot-iostream.h"
31
32namespace CoverageControl {
33
34[[nodiscard]] bool Plotter::GnuplotCommands(Gnuplot &gp) {
35 std::filesystem::path map_filename{
36 std::filesystem::weakly_canonical(dir + "/" + plot_name)};
37 std::filesystem::path dir_path{map_filename.parent_path()};
38 if (!std::filesystem::exists(dir_path)) {
39 std::cerr << "Directory does not exist: " << dir_path << std::endl;
40 return 1;
41 }
42 gp << "set o '" << map_filename.string() << "'\n";
43 gp << "set terminal pngcairo enhanced font 'Times," << font_sz << "' size "
44 << image_sz << "," << image_sz << "\n";
45 gp << "set palette defined (-5 'black', -1 '" << color_unknown
46 << "', 0 'white', 1 '" << color_idf << "')\n";
47 gp << "set cbrange [-5:1]\n";
48 gp << "set size ratio -1\n";
49 gp << "set xrange [0:" << range_max << "]\n";
50 gp << "set yrange [0:" << range_max << "]\n";
51 gp << "set border linewidth 1.5\n";
52 if (unset_colorbox) gp << "unset colorbox\n";
53 return 0;
54}
55
56void Plotter::StreamMap(Gnuplot &gp, MapType const &map) {
57 for (int i = 0; i < map.rows(); ++i) {
58 for (int j = 0; j < map.cols(); ++j) {
59 gp << map(i, j) << " ";
60 }
61 gp << "\n";
62 }
63 gp << "e" << std::endl;
64}
65
66void Plotter::PlotMap(Gnuplot &gp, bool begin) {
67 if (begin == true)
68 gp << "plot ";
69 else
70 gp << ", ";
71 gp << "'-' matrix using ($2*" << resolution << "):($1*" << resolution
72 << "):3 with image notitle ";
73}
74
75void Plotter::PlotLine(Gnuplot &gp, int marker_size, std::string color,
76 bool begin) {
77 if (begin == true)
78 gp << "plot ";
79 else
80 gp << ", ";
81 gp << "'-' with line lw " << marker_size << " lc rgb '" << color
82 << "' notitle";
83}
84
85void Plotter::PlotPoints(Gnuplot &gp, int point_type, int marker_size,
86 std::string color, bool begin) {
87 if (begin == true)
88 gp << "plot ";
89 else
90 gp << ", ";
91 gp << "'-' with points pt " << point_type << " ps " << marker_size
92 << " lc rgb '" << color << "' notitle";
93}
94
95void Plotter::PlotMap(MapType const &map) {
96 Gnuplot gp;
97 if (GnuplotCommands(gp)) {
98 std::cerr << "Error in GnuplotCommands" << std::endl;
99 return;
100 }
101 PlotMap(gp);
102 gp << "\n";
103 StreamMap(gp, map);
104}
105
106void Plotter::PlotMap(MapType const &map, PointVector const &positions) {
107 Gnuplot gp;
108 if (GnuplotCommands(gp)) {
109 std::cerr << "Error in GnuplotCommands" << std::endl;
110 return;
111 }
112 PlotMap(gp);
113 PlotPoints(gp, 7, marker_sz, color_robot);
114 gp << "\n";
115
116 StreamMap(gp, map);
117
118 for (auto const &pos : positions) {
119 gp << pos[0] << " " << pos[1] << std::endl;
120 }
121 gp << "e" << std::endl;
122}
123
124void Plotter::PlotMap(MapType const &map, PointVector const &positions,
125 std::vector<std::list<Point2>> const &trajectories,
126 std::vector<int> const &robot_status) {
127 Gnuplot gp;
128 if (GnuplotCommands(gp)) {
129 std::cerr << "Error in GnuplotCommands" << std::endl;
130 return;
131 }
132 PlotMap(gp);
133
134 for (size_t i = 0; i < positions.size(); ++i) {
135 if (robot_status[i] == 0) {
136 PlotLine(gp, marker_sz, color_robot, false);
137 } else {
138 PlotLine(gp, marker_sz, color_robot_alt, false);
139 }
140 }
141 for (size_t i = 0; i < positions.size(); ++i) {
142 if (robot_status[i] == 0) {
143 PlotPoints(gp, 7, marker_sz, color_robot, false);
144 } else {
145 PlotPoints(gp, 7, marker_sz, color_robot_alt, false);
146 }
147 }
148 gp << "\n";
149
150 StreamMap(gp, map);
151 for (auto const &trajectory : trajectories) {
152 for (auto const &pos : trajectory) {
153 gp << pos[0] << " " << pos[1] << std::endl;
154 }
155 gp << "e" << std::endl;
156 }
157
158 for (auto const &pos : positions) {
159 gp << pos[0] << " " << pos[1] << std::endl;
160 gp << "e" << std::endl;
161 }
162}
163
164void Plotter::PlotMap(MapType const &map, PointVector const &positions,
165 std::vector<std::list<Point2>> const &trajectories,
166 std::vector<int> const &robot_status,
167 double const &communication_range) {
168 Gnuplot gp;
169 if (GnuplotCommands(gp)) {
170 std::cerr << "Error in GnuplotCommands" << std::endl;
171 return;
172 }
173 PlotMap(gp);
174
175 for (size_t i = 0; i < positions.size(); ++i) {
176 if (robot_status[i] == 0) {
177 PlotLine(gp, marker_sz, color_robot, false);
178 } else {
179 PlotLine(gp, marker_sz, color_robot_alt, false);
180 }
181 }
182 PlotLine(gp, half_marker_sz, color_communication_links, false);
183 for (size_t i = 0; i < positions.size(); ++i) {
184 if (robot_status[i] == 0) {
185 PlotPoints(gp, 7, marker_sz, color_robot, false);
186 } else {
187 PlotPoints(gp, 7, marker_sz, color_robot_alt, false);
188 }
189 }
190 gp << "\n";
191
192 StreamMap(gp, map);
193 for (auto const &trajectory : trajectories) {
194 for (auto const &pos : trajectory) {
195 gp << pos[0] << " " << pos[1] << std::endl;
196 }
197 gp << "e" << std::endl;
198 }
199
200 for (size_t i = 0; i < positions.size(); ++i) {
201 for (size_t j = i + 1; j < positions.size(); ++j) {
202 if ((positions[i] - positions[j]).norm() < communication_range) {
203 gp << positions[i][0] << " " << positions[i][1] << "\n";
204 gp << positions[j][0] << " " << positions[j][1] << "\n";
205 gp << "\n";
206 }
207 }
208 }
209 gp << "e" << std::endl;
210 for (auto const &pos : positions) {
211 gp << pos[0] << " " << pos[1] << std::endl;
212 gp << "e" << std::endl;
213 }
214}
215
216void Plotter::PlotMap(MapType const &map, PointVector const &positions,
217 std::vector<std::list<Point2>> const &voronoi,
218 std::vector<std::list<Point2>> const &trajectories) {
219 Gnuplot gp;
220 if (GnuplotCommands(gp)) {
221 std::cerr << "Error in GnuplotCommands" << std::endl;
222 return;
223 }
224 PlotMap(gp);
225
226 PlotLine(gp, marker_sz, color_robot, false);
227 PlotLine(gp, half_marker_sz, color_voronoi, false); // voronoi
228 PlotPoints(gp, 7, marker_sz, color_robot, false); // robots
229 gp << "\n";
230
231 StreamMap(gp, map);
232
233 for (auto const &trajectory : trajectories) {
234 for (auto const &pos : trajectory) {
235 gp << pos[0] << " " << pos[1] << "\n";
236 }
237 gp << "\n";
238 }
239 gp << "e" << std::endl;
240
241 for (auto const &vcell : voronoi) {
242 for (auto const &pos : vcell) {
243 gp << pos[0] << " " << pos[1] << "\n";
244 }
245 gp << "\n";
246 }
247 gp << "e" << std::endl;
248
249 for (auto const &pos : positions) {
250 gp << pos[0] << " " << pos[1] << "\n";
251 }
252 gp << "e" << std::endl;
253}
254
255void Plotter::PlotMap(MapType const &map, PointVector const &positions,
256 Voronoi const &voronoi,
257 std::vector<std::list<Point2>> const &trajectories) {
258 Gnuplot gp;
259 if (GnuplotCommands(gp)) {
260 std::cerr << "Error in GnuplotCommands" << std::endl;
261 return;
262 }
263 PlotMap(gp);
264
265 PlotLine(gp, marker_sz, color_robot, false);
266 PlotLine(gp, half_marker_sz, color_voronoi, false); // voronoi
267 PlotPoints(gp, 7, marker_sz, color_robot, false); // robots
268 gp << "\n";
269
270 StreamMap(gp, map);
271
272 for (auto const &trajectory : trajectories) {
273 for (auto const &pos : trajectory) {
274 gp << pos[0] << " " << pos[1] << "\n";
275 }
276 gp << "\n";
277 }
278 gp << "e" << std::endl;
279
280 auto voronoi_cells = voronoi.GetVoronoiCells();
281 for (auto const &vcell : voronoi_cells) {
282 for (auto const &pos : vcell.cell) {
283 gp << pos[0] << " " << pos[1] << "\n";
284 }
285 auto const &pos = vcell.cell.front();
286 gp << pos[0] << " " << pos[1] << "\n";
287 gp << "\n";
288 }
289 gp << "e" << std::endl;
290
291 for (auto const &pos : positions) {
292 gp << pos[0] << " " << pos[1] << "\n";
293 }
294 gp << "e" << std::endl;
295}
296
297void Plotter::PlotMap(MapType const &map, PointVector const &positions,
298 PointVector const &goals, Voronoi const &voronoi) {
299 Gnuplot gp;
300 if (GnuplotCommands(gp)) {
301 std::cerr << "Error in GnuplotCommands" << std::endl;
302 return;
303 }
304 PlotMap(gp);
305
306 PlotLine(gp, half_marker_sz, color_voronoi, false); // voronoi
307 PlotLine(gp, half_marker_sz, color_robot, false); // goals path
308 PlotPoints(gp, 28, marker_sz, color_robot, false); // goals
309 PlotPoints(gp, 7, marker_sz, color_robot, false); // robots
310 gp << "\n";
311
312 StreamMap(gp, map);
313
314 auto voronoi_cells = voronoi.GetVoronoiCells();
315 for (auto const &vcell : voronoi_cells) {
316 for (auto const &pos : vcell.cell) {
317 gp << pos[0] << " " << pos[1] << std::endl;
318 }
319 auto const &pos = vcell.cell.front();
320 gp << pos[0] << " " << pos[1] << std::endl;
321 gp << "\n";
322 }
323 gp << "e" << std::endl;
324
325 for (size_t i = 0; i < positions.size(); ++i) {
326 auto const &pos = positions[i];
327 auto const &goal = goals[i];
328 gp << pos[0] << " " << pos[1] << std::endl;
329 gp << goal[0] << " " << goal[1] << std::endl;
330 gp << "\n";
331 }
332 gp << "e" << std::endl;
333
334 for (auto const &pos : goals) {
335 gp << pos[0] << " " << pos[1] << std::endl;
336 }
337 gp << "e" << std::endl;
338
339 for (auto const &pos : positions) {
340 gp << pos[0] << " " << pos[1] << std::endl;
341 }
342 gp << "e" << std::endl;
343}
344
345void Plotter::PlotMap(MapType const &map, PointVector const &positions,
346 std::vector<std::list<Point2>> const &trajectories,
347 PointVector const &frontiers) {
348 Gnuplot gp;
349 if (GnuplotCommands(gp)) {
350 std::cerr << "Error in GnuplotCommands" << std::endl;
351 return;
352 }
353
354 PlotMap(gp);
355 PlotLine(gp, marker_sz, color_robot);
356 PlotPoints(gp, 7, marker_sz, color_robot);
357 PlotPoints(gp, 1, half_marker_sz, color_robot);
358
359 gp << "\n";
360
361 StreamMap(gp, map);
362 for (auto const &trajectory : trajectories) {
363 for (auto const &pos : trajectory) {
364 gp << pos[0] << " " << pos[1] << std::endl;
365 }
366 gp << "\n";
367 }
368 gp << "e" << std::endl;
369
370 for (auto const &pos : positions) {
371 gp << pos[0] << " " << pos[1] << std::endl;
372 }
373 gp << "e" << std::endl;
374
375 for (auto const &pos : frontiers) {
376 gp << pos[0] << " " << pos[1] << std::endl;
377 }
378 gp << "e" << std::endl;
379}
380
381} // namespace CoverageControl
Eigen::Matrix< float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > MapType
Definition typedefs.h:48
Namespace for the CoverageControl library.
Class to plot the map.