-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLayoutNavigationPanelWidget.cpp
More file actions
329 lines (284 loc) · 12.2 KB
/
Copy pathLayoutNavigationPanelWidget.cpp
File metadata and controls
329 lines (284 loc) · 12.2 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
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
#include "application/LayoutNavigationPanelWidget.h"
#include "domain/GeometryQueries.h"
#include <algorithm>
#include <cmath>
#include <limits>
#include <utility>
#include <vector>
#include <QColor>
#include <QVBoxLayout>
#include "application/NavigationTreeWidget.h"
#include "application/ToolIconResources.h"
namespace safecrowd::application {
namespace {
constexpr double kGeometryEpsilon = 1e-4;
const QColor kExitAccentColor("#2d8f5b");
const QColor kDoorAccentColor("#ff8c00");
using safecrowd::domain::distanceToPolygonBoundary;
using safecrowd::domain::pointInPolygon;
QString floorActionId(const std::string& floorId) {
return QString("floor:%1").arg(QString::fromStdString(floorId));
}
bool matchesFloor(const std::string& elementFloorId, const std::string& floorId) {
return floorId.empty() || elementFloorId.empty() || elementFloorId == floorId;
}
QIcon treeIcon(const QString& resourcePath, const QColor& color) {
return makeSvgToolIcon(resourcePath, color, QSize(18, 18));
}
QIcon floorIcon() {
return treeIcon(QStringLiteral(":/tool-icons/layout-authoring/floor.svg"), QColor("#4f5d6b"));
}
QIcon zoneIcon(const safecrowd::domain::Zone2D& zone) {
if (zone.kind == safecrowd::domain::ZoneKind::Exit) {
return treeIcon(QStringLiteral(":/tool-icons/layout-authoring/draw-exit.svg"), kExitAccentColor);
}
if (zone.kind == safecrowd::domain::ZoneKind::Stair || zone.isStair || zone.isRamp) {
return treeIcon(QStringLiteral(":/tool-icons/layout-authoring/draw-stair-ramp.svg"), QColor("#6a5d9f"));
}
return treeIcon(QStringLiteral(":/tool-icons/layout-authoring/draw-room.svg"), QColor("#2f5d8a"));
}
QIcon barrierIcon(const safecrowd::domain::Barrier2D& barrier) {
return barrier.geometry.closed
? treeIcon(QStringLiteral(":/tool-icons/layout-authoring/draw-obstruction.svg"), QColor("#6c4f38"))
: treeIcon(QStringLiteral(":/tool-icons/layout-authoring/draw-wall.svg"), QColor("#4f5d6b"));
}
QIcon connectionIcon(const safecrowd::domain::Connection2D& connection) {
if (connection.kind == safecrowd::domain::ConnectionKind::Stair
|| connection.kind == safecrowd::domain::ConnectionKind::Ramp
|| connection.isStair
|| connection.isRamp) {
return treeIcon(QStringLiteral(":/tool-icons/layout-authoring/draw-stair-ramp.svg"), QColor("#6a5d9f"));
}
if (connection.kind == safecrowd::domain::ConnectionKind::Exit) {
return treeIcon(QStringLiteral(":/tool-icons/layout-authoring/draw-exit.svg"), kExitAccentColor);
}
return treeIcon(QStringLiteral(":/tool-icons/layout-authoring/draw-door.svg"), kDoorAccentColor);
}
QString zoneLabel(const safecrowd::domain::Zone2D& zone) {
const auto id = QString::fromStdString(zone.id);
const auto label = QString::fromStdString(zone.label);
return label.isEmpty() ? id : QString("%1 - %2").arg(label, id);
}
NavigationTreeNode makeZoneNode(const safecrowd::domain::Zone2D& zone) {
return {
.label = zoneLabel(zone),
.id = QString::fromStdString(zone.id),
.detail = QString("Zone: %1").arg(QString::fromStdString(zone.id)),
.icon = zoneIcon(zone),
.expanded = false,
};
}
QString floorLabel(const safecrowd::domain::Floor2D& floor) {
const auto id = QString::fromStdString(floor.id);
const auto label = QString::fromStdString(floor.label);
return label.isEmpty() || label == id ? id : QString("%1 - %2").arg(label, id);
}
QString connectionLabel(const safecrowd::domain::Connection2D& connection) {
const auto id = QString::fromStdString(connection.id);
return id.isEmpty()
? QString("%1 -> %2").arg(QString::fromStdString(connection.fromZoneId), QString::fromStdString(connection.toZoneId))
: id;
}
QString barrierLabel(const safecrowd::domain::Barrier2D& barrier) {
return QString::fromStdString(barrier.id);
}
QString barrierKindLabel(const safecrowd::domain::Barrier2D& barrier) {
return barrier.geometry.closed ? QString("Obstruction") : QString("Wall");
}
NavigationTreeNode makeSection(const QString& label, std::vector<NavigationTreeNode> children, const QString& id = {}) {
return {
.label = label,
.id = id,
.icon = floorIcon(),
.children = std::move(children),
.expanded = false,
.selectable = !id.isEmpty(),
};
}
double overlapLength(double firstStart, double firstEnd, double secondStart, double secondEnd) {
const auto firstMin = std::min(firstStart, firstEnd);
const auto firstMax = std::max(firstStart, firstEnd);
const auto secondMin = std::min(secondStart, secondEnd);
const auto secondMax = std::max(secondStart, secondEnd);
return std::min(firstMax, secondMax) - std::max(firstMin, secondMin);
}
bool segmentsShareSpan(
const safecrowd::domain::Point2D& firstStart,
const safecrowd::domain::Point2D& firstEnd,
const safecrowd::domain::Point2D& secondStart,
const safecrowd::domain::Point2D& secondEnd) {
const auto firstDx = firstEnd.x - firstStart.x;
const auto firstDy = firstEnd.y - firstStart.y;
const auto firstLength = std::hypot(firstDx, firstDy);
const auto secondDx = secondEnd.x - secondStart.x;
const auto secondDy = secondEnd.y - secondStart.y;
const auto secondLength = std::hypot(secondDx, secondDy);
if (firstLength <= kGeometryEpsilon || secondLength <= kGeometryEpsilon) {
return false;
}
const auto directionCross = (firstDx * secondDy) - (firstDy * secondDx);
if (std::abs(directionCross) > kGeometryEpsilon * std::max(firstLength, secondLength)) {
return false;
}
const auto startCross = (firstDx * (secondStart.y - firstStart.y)) - (firstDy * (secondStart.x - firstStart.x));
const auto endCross = (firstDx * (secondEnd.y - firstStart.y)) - (firstDy * (secondEnd.x - firstStart.x));
if (std::abs(startCross) > kGeometryEpsilon * firstLength
|| std::abs(endCross) > kGeometryEpsilon * firstLength) {
return false;
}
const bool useX = std::abs(firstDx) >= std::abs(firstDy);
const auto overlap = useX
? overlapLength(firstStart.x, firstEnd.x, secondStart.x, secondEnd.x)
: overlapLength(firstStart.y, firstEnd.y, secondStart.y, secondEnd.y);
return overlap > kGeometryEpsilon;
}
bool segmentSharesRingSpan(
const safecrowd::domain::Point2D& segmentStart,
const safecrowd::domain::Point2D& segmentEnd,
const std::vector<safecrowd::domain::Point2D>& ring) {
if (ring.size() < 2) {
return false;
}
for (std::size_t index = 0; index < ring.size(); ++index) {
if (segmentsShareSpan(segmentStart, segmentEnd, ring[index], ring[(index + 1) % ring.size()])) {
return true;
}
}
return false;
}
bool openWallBelongsToZoneBoundary(const safecrowd::domain::Barrier2D& barrier, const safecrowd::domain::Zone2D& zone) {
const auto& start = barrier.geometry.vertices[0];
const auto& end = barrier.geometry.vertices[1];
if (segmentSharesRingSpan(start, end, zone.area.outline)) {
return true;
}
return std::any_of(zone.area.holes.begin(), zone.area.holes.end(), [&](const auto& hole) {
return segmentSharesRingSpan(start, end, hole);
});
}
bool barrierContainedInZone(const safecrowd::domain::Barrier2D& barrier, const safecrowd::domain::Zone2D& zone) {
for (const auto& point : barrier.geometry.vertices) {
if (pointInPolygon(zone.area, point) || distanceToPolygonBoundary(zone.area, point) <= 0.08) {
return true;
}
}
return false;
}
bool barrierBelongsToZone(const safecrowd::domain::Barrier2D& barrier, const safecrowd::domain::Zone2D& zone) {
if (!barrier.geometry.closed && barrier.geometry.vertices.size() == 2) {
return openWallBelongsToZoneBoundary(barrier, zone);
}
return barrierContainedInZone(barrier, zone);
}
bool isRoomLikeZone(const safecrowd::domain::Zone2D& zone) {
return zone.kind == safecrowd::domain::ZoneKind::Room
|| zone.kind == safecrowd::domain::ZoneKind::Intersection
|| zone.kind == safecrowd::domain::ZoneKind::Unknown
|| zone.kind == safecrowd::domain::ZoneKind::Exit
|| zone.kind == safecrowd::domain::ZoneKind::Stair;
}
std::vector<NavigationTreeNode> roomChildren(
const safecrowd::domain::FacilityLayout2D& layout,
const safecrowd::domain::Zone2D& room) {
std::vector<NavigationTreeNode> children;
for (const auto& barrier : layout.barriers) {
if (!matchesFloor(barrier.floorId, room.floorId) || !barrierBelongsToZone(barrier, room)) {
continue;
}
const auto kind = barrierKindLabel(barrier);
children.push_back({
.label = QString("%1 - %2").arg(kind, barrierLabel(barrier)),
.id = QString::fromStdString(barrier.id),
.detail = QString("%1: %2").arg(kind, QString::fromStdString(barrier.id)),
.icon = barrierIcon(barrier),
});
}
for (const auto& connection : layout.connections) {
if (!matchesFloor(connection.floorId, room.floorId)
|| (connection.fromZoneId != room.id && connection.toZoneId != room.id)) {
continue;
}
children.push_back({
.label = QString("Door - %1").arg(connectionLabel(connection)),
.id = QString::fromStdString(connection.id),
.detail = QString("Door: %1").arg(QString::fromStdString(connection.id)),
.icon = connectionIcon(connection),
});
}
return children;
}
std::vector<safecrowd::domain::Floor2D> layoutFloors(const safecrowd::domain::FacilityLayout2D& layout) {
if (!layout.floors.empty()) {
return layout.floors;
}
std::vector<safecrowd::domain::Floor2D> floors;
const auto fallback = layout.levelId.empty() ? std::string{"L1"} : layout.levelId;
floors.push_back({.id = fallback, .label = fallback});
return floors;
}
std::vector<NavigationTreeNode> buildLayoutTree(const safecrowd::domain::FacilityLayout2D* facilityLayout) {
if (facilityLayout == nullptr) {
return {};
}
std::vector<NavigationTreeNode> nodes;
for (const auto& floor : layoutFloors(*facilityLayout)) {
std::vector<NavigationTreeNode> rooms;
for (const auto& zone : facilityLayout->zones) {
if (!matchesFloor(zone.floorId, floor.id) || !isRoomLikeZone(zone)) {
continue;
}
auto roomNode = makeZoneNode(zone);
roomNode.children = roomChildren(*facilityLayout, zone);
rooms.push_back(std::move(roomNode));
}
nodes.push_back(makeSection(floorLabel(floor), std::move(rooms), floorActionId(floor.id)));
}
if (nodes.empty()) {
std::vector<NavigationTreeNode> rooms;
for (const auto& zone : facilityLayout->zones) {
if (isRoomLikeZone(zone)) {
auto roomNode = makeZoneNode(zone);
roomNode.children = roomChildren(*facilityLayout, zone);
rooms.push_back(std::move(roomNode));
}
}
if (!rooms.empty()) {
nodes.push_back({
.label = "Layout",
.id = "layout",
.icon = floorIcon(),
.children = std::move(rooms),
.expanded = false,
.selectable = false,
});
}
}
return nodes;
}
} // namespace
LayoutNavigationPanelWidget::LayoutNavigationPanelWidget(
const safecrowd::domain::FacilityLayout2D* facilityLayout,
std::function<void(const QString&)> selectElementHandler,
QWidget* parent,
QWidget* headerWidget,
NavigationTreeState navigationState,
std::function<void(const QSet<QString>&)> expandedStateChangedHandler,
std::function<void(const QString&)> deleteElementHandler)
: QWidget(parent) {
auto* layout = new QVBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
layout->addWidget(new NavigationTreeWidget(
"Layout",
buildLayoutTree(facilityLayout),
"No recognized layout elements",
std::move(selectElementHandler),
this,
headerWidget,
std::move(navigationState),
std::move(expandedStateChangedHandler),
std::move(deleteElementHandler),
{},
QString("Settings...")));
}
} // namespace safecrowd::application