-
Notifications
You must be signed in to change notification settings - Fork 1
/
interval.h
250 lines (221 loc) · 8.88 KB
/
interval.h
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
#ifndef PATHFINDER_INTERVAL_H_
#define PATHFINDER_INTERVAL_H_
#include "hash_combine.h"
#include "math_helpers.h"
#include "vector.h"
#include <absl/log/log.h>
#include <absl/strings/str_format.h>
#include <optional>
#include <sstream>
#include <utility>
namespace pathfinder {
using LineSegment = std::pair<Vector, Vector>;
template<typename StateType, typename IndexType>
class Interval {
public:
Interval(const StateType &state) : state(state) {}
void setRoot(const Vector &point) {
rootPoint = point;
}
void setRoot(const Vector &point, std::optional<IndexType> index, AngleDirection direction) {
rootPoint = point;
rootIndex = index;
rootDirection = direction;
}
void setLeft(const Vector &point, std::optional<IndexType> index = std::nullopt) {
leftPoint = point;
leftIndex = index;
}
void setRight(const Vector &point, std::optional<IndexType> index = std::nullopt) {
rightPoint = point;
rightIndex = index;
}
StateType state;
Vector rootPoint, leftPoint, rightPoint;
std::optional<IndexType> rootIndex, leftIndex, rightIndex;
AngleDirection rootDirection{AngleDirection::kNoDirection};
double costToRoot{0.0};
bool isGoal{false};
bool leftIsRoot() const {
return rootPoint == leftPoint;
}
bool rightIsRoot() const {
return rootPoint == rightPoint;
}
std::string toString() const {
return absl::StrFormat("State{%s}, root{%s,%s,%.3f,%.3f}, left{%s,%.3f,%.3f}, right{%s,%.3f,%.3f}, costToRoot{%.5f}",
state.toString(),
pathfinder::toString(rootDirection), (rootIndex ? std::to_string(*rootIndex) : "_"), rootPoint.x(), rootPoint.y(),
(leftIndex ? std::to_string(*leftIndex) : "_"), leftPoint.x(), leftPoint.y(),
(rightIndex ? std::to_string(*rightIndex) : "_"), rightPoint.x(), rightPoint.y(),
costToRoot);
}
// -------------------------------------------------------------------------------------------------------
// Below are additional fields that get populated as we progress through the exploration of the interval.
// -------------------------------------------------------------------------------------------------------
public:
void setIntervalLeftIsConstraintVertex(bool isConstraint) {
intervalLeftIsConstraintVertex_ = isConstraint;
leftDirection_ = (*intervalLeftIsConstraintVertex_ ? AngleDirection::kCounterclockwise : AngleDirection::kNoDirection);
}
void setIntervalRightIsConstraintVertex(bool isConstraint) {
intervalRightIsConstraintVertex_ = isConstraint;
rightDirection_ = (*intervalRightIsConstraintVertex_ ? AngleDirection::kClockwise : AngleDirection::kNoDirection);
}
void setRightIntervals(const std::optional<LineSegment> &rightInterval, const std::optional<LineSegment> &rightIntervalToCurrentEntryEdge) {
rightInterval_ = rightInterval;
rightIntervalToCurrentEntryEdge_ = rightIntervalToCurrentEntryEdge;
}
void setLeftIntervals(const std::optional<LineSegment> &leftInterval, const std::optional<LineSegment> &leftIntervalToCurrentEntryEdge) {
leftInterval_ = leftInterval;
leftIntervalToCurrentEntryEdge_ = leftIntervalToCurrentEntryEdge;
}
bool intervalLeftIsConstraintVertex() const {
if (!intervalLeftIsConstraintVertex_) {
throw std::runtime_error("Getting value of intervalLeftIsConstraintVertex before assignment");
}
return intervalLeftIsConstraintVertex_.value();
}
bool intervalRightIsConstraintVertex() const {
if (!intervalRightIsConstraintVertex_) {
throw std::runtime_error("Getting value of intervalRightIsConstraintVertex before assignment");
}
return intervalRightIsConstraintVertex_.value();
}
AngleDirection leftDirection() const {
if (!leftDirection_) {
throw std::runtime_error("Getting value of leftDirection before assignment");
}
return leftDirection_.value();
}
AngleDirection rightDirection() const {
if (!rightDirection_) {
throw std::runtime_error("Getting value of rightDirection before assignment");
}
return rightDirection_.value();
}
const std::optional<LineSegment>& rightInterval() const {
if (!rightInterval_) {
throw std::runtime_error("Getting value of rightInterval before assignment");
}
return *rightInterval_;
}
const std::optional<LineSegment>& rightIntervalToCurrentEntryEdge() const {
if (!rightIntervalToCurrentEntryEdge_) {
throw std::runtime_error("Getting value of rightIntervalToCurrentEntryEdge before assignment");
}
return *rightIntervalToCurrentEntryEdge_;
}
const std::optional<LineSegment>& leftInterval() const {
if (!leftInterval_) {
throw std::runtime_error("Getting value of leftInterval before assignment");
}
return *leftInterval_;
}
const std::optional<LineSegment>& leftIntervalToCurrentEntryEdge() const {
if (!leftIntervalToCurrentEntryEdge_) {
throw std::runtime_error("Getting value of leftIntervalToCurrentEntryEdge before assignment");
}
return *leftIntervalToCurrentEntryEdge_;
}
private:
std::optional<bool> intervalLeftIsConstraintVertex_;
std::optional<bool> intervalRightIsConstraintVertex_;
std::optional<AngleDirection> leftDirection_;
std::optional<AngleDirection> rightDirection_;
std::optional<std::optional<LineSegment>> rightInterval_;
std::optional<std::optional<LineSegment>> rightIntervalToCurrentEntryEdge_;
std::optional<std::optional<LineSegment>> leftInterval_;
std::optional<std::optional<LineSegment>> leftIntervalToCurrentEntryEdge_;
};
template<typename StateType, typename IndexType>
bool operator==(const pathfinder::Interval<StateType, IndexType> &lhs, const pathfinder::Interval<StateType, IndexType> &rhs) {
if (lhs.isGoal && rhs.isGoal) {
// Both are goal, match
return true;
}
if (lhs.isGoal != rhs.isGoal) {
// One is goal and other is not, no match
return false;
}
return lhs.state == rhs.state &&
lhs.rootPoint == rhs.rootPoint &&
lhs.leftPoint == rhs.leftPoint &&
lhs.rightPoint == rhs.rightPoint &&
lhs.rootIndex == rhs.rootIndex &&
lhs.leftIndex == rhs.leftIndex &&
lhs.rightIndex == rhs.rightIndex &&
lhs.rootDirection == rhs.rootDirection;
}
template<typename IntervalType>
class IntervalCompare {
public:
bool operator()(const IntervalType &lhs, const IntervalType &rhs) const {
auto pointIsLess = [](const auto &lp, const auto &rp) {
if (lp.x() == rp.x()) {
return lp.y() < rp.y();
} else {
return lp.x() < rp.x();
}
};
// Ignore cost-to-root.
if (lhs.isGoal == rhs.isGoal) {
if (lhs.state == rhs.state) {
if (lhs.rootDirection == rhs.rootDirection) {
if (lhs.rootPoint == rhs.rootPoint) {
if (lhs.leftPoint == rhs.leftPoint) {
if (lhs.rightPoint == rhs.rightPoint) {
if (lhs.rootIndex == rhs.rootIndex) {
if (lhs.leftIndex == rhs.leftIndex) {
return lhs.rightIndex < rhs.rightIndex;
} else {
return lhs.leftIndex < rhs.leftIndex;
}
} else {
return lhs.rootIndex < rhs.rootIndex;
}
} else {
return pointIsLess(lhs.rightPoint, rhs.rightPoint);
}
} else {
return pointIsLess(lhs.leftPoint, rhs.leftPoint);
}
} else {
return pointIsLess(lhs.rootPoint, rhs.rootPoint);
}
} else {
return lhs.rootDirection < rhs.rootDirection;
}
} else {
return lhs.state < rhs.state;
}
} else {
return lhs.isGoal < rhs.isGoal;
}
}
};
} // namespace pathfinder
namespace std {
template<typename StateType, typename IndexType>
struct hash<pathfinder::Interval<StateType, IndexType>> {
std::size_t operator()(const pathfinder::Interval<StateType, IndexType> &interval) const {
if (interval.isGoal) {
return std::hash<bool>()(true);
}
std::size_t runningHash{0};
runningHash = pathfinder::hash_combine(runningHash, interval.state);
runningHash = pathfinder::hash_combine(runningHash, interval.rootPoint.x());
runningHash = pathfinder::hash_combine(runningHash, interval.rootPoint.y());
runningHash = pathfinder::hash_combine(runningHash, interval.leftPoint.x());
runningHash = pathfinder::hash_combine(runningHash, interval.leftPoint.y());
runningHash = pathfinder::hash_combine(runningHash, interval.rightPoint.x());
runningHash = pathfinder::hash_combine(runningHash, interval.rightPoint.y());
runningHash = pathfinder::hash_combine(runningHash, interval.rootIndex);
runningHash = pathfinder::hash_combine(runningHash, interval.leftIndex);
runningHash = pathfinder::hash_combine(runningHash, interval.rightIndex);
runningHash = pathfinder::hash_combine(runningHash, interval.rootDirection);
return runningHash;
}
};
} // namespace std
#endif // PATHFINDER_INTERVAL_H_