-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbreadcrumbs.cpp
More file actions
198 lines (168 loc) · 7.08 KB
/
breadcrumbs.cpp
File metadata and controls
198 lines (168 loc) · 7.08 KB
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
//
// Created by Mark on 5/23/2020.
//
#include <memory>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <utility>
#include "breadcrumbs.h"
using std::vector;
using std::deque;
using std::make_shared;
using std::make_unique;
using std::shared_ptr;
using std::priority_queue;
bool inBounds(const Matrix &matrix, const MatrixPoint &p)
{
return (p.x > -1 && p.y > -1) && p.y < matrix.size() && p.x < matrix[0].size();
}
void getSurroundingPoints(const Matrix &matrix, const MatrixPoint ¤tPoint, vector<MatrixPoint> &surroundingPoints)
{
auto stepSize = 1;
auto writeIndex = 0;
for (long x = currentPoint.x - stepSize; x <= currentPoint.x + stepSize; x += stepSize)
{
for (long y = currentPoint.y - stepSize; y <= currentPoint.y + stepSize; y += stepSize)
{
if(!(x == currentPoint.x && y == currentPoint.y) && inBounds(matrix, {x, y}))
{
surroundingPoints[writeIndex].x = x;
surroundingPoints[writeIndex].y = y;
surroundingPoints[writeIndex].movementCost = 0;
surroundingPoints[writeIndex].totalCost = 0;
++writeIndex;
}
}
}
surroundingPoints.resize(writeIndex);
}
double distance(const MatrixPoint &a, const MatrixPoint &b, double height = 0, double xScale = 1, double yScale = 1, double zScale = 1)
{
auto xScaled = (double)(b.x - a.x) * xScale;
auto yScaled = (double)(b.y - a.y) * yScale;
auto zScaled = height * zScale;
return std::sqrt(xScaled*xScaled + yScaled*yScaled + zScaled*zScaled);
}
double scaledHeight(const Matrix &matrix, const MatrixPoint &a, const MatrixPoint &b, const double &unitsPerPixel)
{
double scaleFactor = 1 / unitsPerPixel;
return std::abs(matrix[b.y][b.x] - matrix[a.y][a.x]) * scaleFactor;
}
double gradeCost(const MatrixPoint ¤tPoint, const MatrixPoint &successor, const Matrix &matrix, const Weights &weights)
{
long x = currentPoint.x;
long y = currentPoint.y;
const long xDiff = successor.x - currentPoint.x;
const long yDiff = successor.y - currentPoint.y;
double worstHeight = 0;
const unsigned int radius = weights.gradeRadius;
for (auto i = 0u; i < radius; ++i)
{
if (inBounds(matrix, {x + xDiff, y + yDiff}) && inBounds(matrix, {x, y}))
{
worstHeight = std::max(worstHeight,
scaledHeight(matrix, {x, y}, {x + xDiff, y + yDiff}, weights.unitsPerPixel));
}
x += xDiff;
y += yDiff;
}
return std::pow(weights.gradeBase, worstHeight / distance(currentPoint, successor));
}
//controlPoints needs to be a deque because the algorithm needs to pop things off the front quickly but also have
//random access. std::queue does not have random access.
//controlPoints must also be passed by value, to allow it to be used multiple times
vector<vector<int>> getShortestPath(const Matrix &elevationMatrix,
const Matrix &costMatrix,
deque<MatrixPoint> controlPoints,
const Weights &weights)
{
auto finalMatrix = vector<vector<int>>(elevationMatrix.size(), vector<int>(elevationMatrix[0].size(), 0));
auto visitedPoint = 10;
MatrixPoint finishingPoint;
while (controlPoints.size() >= 2)
{
MatrixPoint startingPoint = controlPoints[0];
MatrixPoint target = controlPoints[1];
auto differenceGreater = [](auto a, auto b) { return a.totalCost > b.totalCost; };
using PointQueue = priority_queue<MatrixPoint, vector<MatrixPoint>, decltype(differenceGreater)>;
PointQueue pointQueue(differenceGreater);
pointQueue.push(startingPoint);
auto pathMatrix = vector<vector<MatrixPoint>>(elevationMatrix.size(), vector<MatrixPoint>(elevationMatrix[0].size(), MatrixPoint{}));
startingPoint.visited = true;
pathMatrix[startingPoint.y][startingPoint.x]= startingPoint;
vector<MatrixPoint> surroundingPoints(8, MatrixPoint{});
bool stop = false;
while (!stop && !pointQueue.empty())
{
auto currentPoint = pointQueue.top();
pointQueue.pop();
finishingPoint = currentPoint;
surroundingPoints.resize(8);
getSurroundingPoints(elevationMatrix, currentPoint, surroundingPoints);
for (auto &successor : surroundingPoints)
{
if (currentPoint.x == target.x && currentPoint.y == target.y)
{
stop = true;
break;
}
if (!pathMatrix[successor.y][successor.x].visited)
{
successor.visited =true;
double heightToSuccessor = scaledHeight(
elevationMatrix,
currentPoint,
successor,
weights.unitsPerPixel
);
successor.movementCost = (
distance(
currentPoint,
successor,
heightToSuccessor,
weights.movementCostXY,
weights.movementCostXY,
weights.movementCostZ
)
+ gradeCost(
currentPoint,
successor,
elevationMatrix,
weights
)
+ currentPoint.movementCost
+ costMatrix[successor.y][successor.x]
);
const double heightToTarget = scaledHeight(
elevationMatrix,
successor,
target,
weights.unitsPerPixel
);
double distToTarget = distance(
successor,
target,
heightToTarget,
weights.heuristicXY,
weights.heuristicXY,
weights.heuristicZ
);
successor.totalCost = successor.movementCost + distToTarget;
successor.parent = {currentPoint.x, currentPoint.y};
pointQueue.push(successor);
pathMatrix[successor.y][successor.x] = successor;
}
}
}
controlPoints.pop_front();
while(finishingPoint.parent != std::make_pair<long, long>(-1, -1))
{
finalMatrix[finishingPoint.y][finishingPoint.x] = visitedPoint;
finishingPoint = pathMatrix[finishingPoint.parent.second][finishingPoint.parent.first];
}
fill(pathMatrix.begin(), pathMatrix.end(), vector<MatrixPoint>(pathMatrix.size(), MatrixPoint{}));
}
return finalMatrix;
}