-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotting.cpp
445 lines (375 loc) · 14.8 KB
/
plotting.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#include <vector>
#include <string>
#include <cmath>
#include <chrono>
#include <thread>
#include <utility>
#include <filesystem>
// NOTE: this is very dumb but this #include <assert.h> is needed
// before we #include <Magick++.h>. This is because ImageMagick
// defines some assert related function that conflicts with
// what the protobuf code expects to be available.
// https://github.com/ImageMagick/ImageMagick/issues/4679
// https://stackoverflow.com/questions/43676010/c-boost-assert-fail-was-not-declared-in-this-scope
#include <assert.h> // NOLINT
#include <Magick++.h>
#include "pathing/plotting.hpp"
#include "utilities/datatypes.hpp"
PathingPlot::PathingPlot(std::filesystem::path outputDir) :
outputDir(outputDir), figure(matplot::gcf()) {
// https://alandefreitas.github.io/matplotplusplus/coding-styles/reactive-vs-quiet-figures/
this->figure->quiet_mode(true);
// multiple calls to plot won't erase axes
this->figure->current_axes()->hold(true);
}
PathingPlot::PathingPlot(
std::filesystem::path outputDir,
Polygon flightBoundary,
Polygon aidropBoundary,
std::vector<XYZCoord> waypoints) :
outputDir(outputDir),
figure(matplot::gcf()),
flightBoundary(flightBoundary),
airdropBoundary(aidropBoundary),
waypoints(waypoints) {
// https://alandefreitas.github.io/matplotplusplus/coding-styles/reactive-vs-quiet-figures/
this->figure->quiet_mode(true);
// multiple calls to plot won't erase axes
this->figure->current_axes()->hold(true);
}
void PathingPlot::addFinalPoints(std::vector<XYZCoord> pts) {
for (XYZCoord& coord : pts) {
this->addFinalPoint(coord);
}
}
void PathingPlot::addFinalPolylines(std::vector<Polyline> polylines) {
for (Polyline& polyline : polylines) {
this->addFinalPolyline(polyline);
}
}
void PathingPlot::addFinalPoint(XYZCoord pt) {
// if there are no chunks, create an initial empty one
if (this->plottingChunks.empty()) {
this->plottingChunks.push_back(PlottingChunk());
}
// add a finalItem to the latest chunk
this->plottingChunks.back().finalItem = pt;
// create a new chunk for the next set of intermediates and another final
this->plottingChunks.push_back(PlottingChunk());
}
void PathingPlot::addFinalPolyline(Polyline polyline) {
// if there are no chunks, create an initial empty one
if (this->plottingChunks.empty()) {
this->plottingChunks.push_back(PlottingChunk());
}
// add a finalItem to the latest chunk
this->plottingChunks.back().finalItem = polyline;
// create a new chunk for the next set of intermediates and another final
this->plottingChunks.push_back(PlottingChunk());
}
void PathingPlot::addIntermediatePoints(std::vector<XYZCoord> pts) {
for (XYZCoord& coord : pts) {
this->addIntermediatePoint(coord);
}
}
void PathingPlot::addIntermediatePolylines(std::vector<Polyline> polylines) {
for (Polyline& polyline : polylines) {
this->addIntermediatePolyline(polyline);
}
}
void PathingPlot::addIntermediatePoint(XYZCoord pt) {
// if there are no chunks, create an initial empty one
if (this->plottingChunks.empty()) {
this->plottingChunks.push_back(PlottingChunk());
}
this->plottingChunks.back().intermediateItems.push_back(pt);
}
void PathingPlot::addIntermediatePolyline(Polyline polyline) {
// if there are no chunks, create an initial empty one
if (this->plottingChunks.empty()) {
this->plottingChunks.push_back(PlottingChunk());
}
this->plottingChunks.back().intermediateItems.push_back(polyline);
}
void PathingPlot::output(std::string filename, PathOutputType pathType) {
std::filesystem::create_directories(outputDir);
std::filesystem::path staticFilepath(outputDir / (filename + ".jpg"));
std::filesystem::path animatedFilepath(outputDir / (filename + ".gif"));
auto ax = this->figure->current_axes();
switch (pathType) {
case PathOutputType::NONE:
return;
case PathOutputType::STATIC:
outputStatic(ax, staticFilepath);
return;
case PathOutputType::ANIMATED:
outputAnimated(ax, animatedFilepath);
return;
case PathOutputType::BOTH:
outputStatic(ax, staticFilepath);
outputAnimated(ax, animatedFilepath);
return;
default:
return;
}
}
void PathingPlot::setLineWidth(float lineWidth) {
this->lineWidth = lineWidth;
}
void PathingPlot::setCoordSize(float coordSize) {
this->coordSize = coordSize;
}
void PathingPlot::setAnimationDelay(size_t animationDelayCentiSec) {
this->animationDelayCentiSec = animationDelayCentiSec;
}
void PathingPlot::setFramesPerDistanceUnit(double framesPerDistanceUnit) {
this->framesPerDistanceUnit = framesPerDistanceUnit;
}
void PathingPlot::outputStatic(matplot::axes_handle ax, std::filesystem::path filepath) {
this->clearPlot(ax);
this->plotStaticBackground(ax);
// add all final coords
for (PlottingChunk& chunk : plottingChunks) {
// skip all chunks without final items
if (!chunk.finalItem.has_value()) {
continue;
}
this->plotStaticPlottable(ax, chunk.finalItem.value(), PLANNED_PATH_COLOR);
}
matplot::save(this->figure, filepath.string());
}
void PathingPlot::outputAnimated(matplot::axes_handle ax, std::filesystem::path filepath) {
this->currFrame = 0;
this->clearPlot(ax);
this->plotStaticBackground(ax);
// set limits of axes to allocate enough space for the whole graph
std::pair<double, double> xLimits;
std::pair<double, double> yLimits;
std::tie(xLimits, yLimits) = this->getAnimatedPlotLimits();
ax->xlim({xLimits.first, xLimits.second});
ax->ylim({yLimits.first, yLimits.second});
// create a temporary directory for the frames
std::filesystem::path tmpFrameDir = std::filesystem::temp_directory_path() / "frames";
std::filesystem::create_directories(tmpFrameDir);
// plot each chunk's intermediate and final items
for (PlottingChunk& chunk : this->plottingChunks) {
matplot::axes_type* prevFinalAxRaw = new matplot::axes_type(this->figure);
std::memcpy(prevFinalAxRaw, ax.get(), sizeof(matplot::axes_type));
matplot::axes_handle prevFinalAxShared =
std::make_shared<matplot::axes_type>(*prevFinalAxRaw);
// plot intermediates onto temporary axes
for (Plottable& item : chunk.intermediateItems) {
this->plotAnimatedPlottable(ax, item, TENTATIVE_PATH_COLOR, tmpFrameDir);
}
// plot final item if there is one
if (chunk.finalItem.has_value()) {
// plot final item onto temporary Ax
this->plotAnimatedPlottable(
ax,
chunk.finalItem.value(),
PLANNED_PATH_COLOR,
tmpFrameDir);
// update the current ax to the version before we added any intermediates
*ax = *prevFinalAxShared;
// add an extra frame of the finalized item
this->plotStaticPlottable(ax, chunk.finalItem.value(), PLANNED_PATH_COLOR);
}
}
// use ImageMagick to save the frames to a .gif file
std::vector<Magick::Image> frames;
for (int i = 0; i < this->currFrame; i++) {
Magick::Image image;
// This isn't great but we assume that the temporary frames at these file paths
std::filesystem::path frameFilepath = tmpFrameDir / ("frame" + std::to_string(i) + ".jpg");
image.read(frameFilepath);
image.animationDelay(this->animationDelayCentiSec);
frames.push_back(image);
}
Magick::writeImages(frames.begin(), frames.end(), filepath);
// cleanup temporary gifs and temporary directory
for (int i = 0; i < this->currFrame; i++) {
std::filesystem::path frameFilepath = tmpFrameDir / ("frame" + std::to_string(i) + ".jpg");
std::filesystem::remove(frameFilepath);
}
std::filesystem::remove(tmpFrameDir);
}
void PathingPlot::clearPlot(matplot::axes_handle ax) {
ax->clear();
}
void PathingPlot::plotStaticBackground(matplot::axes_handle ax) {
if (this->flightBoundary.has_value()) {
this->plotStaticPolygon(ax, flightBoundary.value(), FLIGHT_BOUND_COLOR);
}
if (this->airdropBoundary.has_value()) {
this->plotStaticPolygon(ax, airdropBoundary.value(), AIRDROP_BOUND_COLOR);
}
if (this->waypoints.has_value()) {
for (XYZCoord& coord : this->waypoints.value()) {
this->plotStaticCoord(ax, coord, WAYPOINTS_COLOR);
}
}
}
void PathingPlot::plotStaticPolygon(
matplot::axes_handle ax,
const Polygon& polygon,
matplot::color color) {
for (size_t i = 0; i < polygon.size(); i++) {
size_t firstPointIdx = i;
size_t secondPointIdx = i+1;
// if we reached the end, wrap around and
// draw a line from the last point to the first
if (secondPointIdx == polygon.size()) {
secondPointIdx = 0;
}
XYZCoord firstPoint = polygon.at(firstPointIdx);
XYZCoord secondPoint = polygon.at(secondPointIdx);
ax->plot({firstPoint.x, secondPoint.x}, {firstPoint.y, secondPoint.y})->
line_width(this->lineWidth).
color(color);
}
}
void PathingPlot::plotStaticPolyline(
matplot::axes_handle ax,
const Polyline& polyline,
matplot::color color) {
for (size_t i = 0; i < polyline.size() - 1; i++) {
size_t firstPointIdx = i;
size_t secondPointIdx = i+1;
XYZCoord firstPoint = polyline.at(firstPointIdx);
XYZCoord secondPoint = polyline.at(secondPointIdx);
ax->plot({firstPoint.x, secondPoint.x}, {firstPoint.y, secondPoint.y})->
line_width(this->lineWidth).
color(color);
}
}
void PathingPlot::plotStaticCoord(
matplot::axes_handle ax,
const XYZCoord &coord,
matplot::color color) {
ax->scatter({coord.x}, {coord.y})->
marker_face(true).
marker_color(color).
marker_face_color(color).
marker_size(this->coordSize).
color(color);
}
void PathingPlot::plotStaticPlottable(
matplot::axes_handle ax,
const Plottable& plottable,
matplot::color color) {
if (auto *coord = std::get_if<XYZCoord>(&plottable)) {
this->plotStaticCoord(ax, *coord, color);
} else if (auto *polyline = std::get_if<Polyline>(&plottable)) {
this->plotStaticPolyline(ax, *polyline, color);
}
}
void PathingPlot::plotAnimatedPolyline(
matplot::axes_handle ax,
const Polyline& polyline,
matplot::color color,
std::filesystem::path tmpFrameDir) {
for (size_t currPolyline = 0; currPolyline < polyline.size() - 1; currPolyline++) {
size_t firstPointIdx = currPolyline;
size_t secondPointIdx = currPolyline+1;
const XYZCoord& firstPoint = polyline.at(firstPointIdx);
const XYZCoord& secondPoint = polyline.at(secondPointIdx);
// find out how many frames to generate per line segment
double segmentDistance = std::sqrt(std::pow(secondPoint.y - firstPoint.y, 2) +
std::pow(secondPoint.x - firstPoint.x, 2));
int numFrames = std::floor(segmentDistance * this->framesPerDistanceUnit);
double x_step = (1.f / static_cast<double>(numFrames))*(secondPoint.x - firstPoint.x);
double y_step = (1.f / static_cast<double>(numFrames))*(secondPoint.y - firstPoint.y);
double curr_x = firstPoint.x;
double curr_y = firstPoint.y;
double next_x = curr_x + x_step;
double next_y = curr_y + y_step;
for (int i = 0; i < numFrames; i++) {
ax->plot({curr_x, next_x}, {curr_y, next_y})->line_width(4).color(color);
// TODO: don't save to build dir
std::filesystem::path frameFilepath =
tmpFrameDir / ("frame" + std::to_string(this->currFrame) + ".jpg");
++this->currFrame;
// TODO: this is so dumb; can't figure out how to get matplotplus plot
// rendered to a c++ array of pixel vals
// So, we must save it to disk and load it again via imagemagick for GIF
matplot::save(this->figure, frameFilepath.string());
// step forward
curr_x = next_x;
next_x += x_step;
curr_y = next_y;
next_y += y_step;
}
}
}
void PathingPlot::plotAnimatedPlottable(
matplot::axes_handle ax,
const Plottable& plottable,
matplot::color color, std::filesystem::path tmpFrameDir) {
if (auto *coord = std::get_if<XYZCoord>(&plottable)) {
this->plotStaticCoord(ax, *coord, color);
} else if (auto *polyline = std::get_if<Polyline>(&plottable)) {
this->plotAnimatedPolyline(ax, *polyline, color, tmpFrameDir);
}
}
std::pair<std::pair<double, double>, std::pair<double, double>>
PathingPlot::getAnimatedPlotLimits() {
double minX = std::numeric_limits<double>::infinity();
double minY = std::numeric_limits<double>::infinity();
double maxX = -std::numeric_limits<double>::infinity();
double maxY = -std::numeric_limits<double>::infinity();
auto updateLimits = [&minX, &minY, &maxX, &maxY](XYZCoord& coord) mutable {
if (coord.x < minX) {
minX = coord.x;
}
if (coord.x > maxX) {
maxX = coord.x;
}
if (coord.y < minY) {
minY = coord.y;
}
if (coord.y > maxY) {
maxY = coord.y;
}
};
// check background points
if (this->flightBoundary.has_value()) {
for (XYZCoord& coord : this->flightBoundary.value()) {
updateLimits(coord);
}
}
if (this->airdropBoundary.has_value()) {
for (XYZCoord& coord : this->airdropBoundary.value()) {
updateLimits(coord);
}
}
if (this->waypoints.has_value()) {
for (XYZCoord& coord : this->waypoints.value()) {
updateLimits(coord);
}
}
// loop through each chunk
for (PlottingChunk& chunk : this->plottingChunks) {
// loop through intermediates
for (Plottable& item : chunk.intermediateItems) {
if (auto *coord = std::get_if<XYZCoord>(&item)) {
updateLimits(*coord);
} else if (auto *polyline = std::get_if<Polyline>(&item)) {
for (XYZCoord& coord : *polyline) {
updateLimits(coord);
}
}
}
// check final item of each chunk
if (!chunk.finalItem.has_value()) {
continue;
}
if (auto *coord = std::get_if<XYZCoord>(&chunk.finalItem.value())) {
updateLimits(*coord);
} else if (auto *polyline = std::get_if<Polyline>(&chunk.finalItem.value())) {
for (XYZCoord& coord : *polyline) {
updateLimits(coord);
}
}
}
return std::make_pair(std::make_pair(minX, maxX), std::make_pair(minY, maxY));
}