-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathDemo_Simulation.h
More file actions
308 lines (249 loc) · 10.5 KB
/
Copy pathDemo_Simulation.h
File metadata and controls
308 lines (249 loc) · 10.5 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
/**
* rocky c++
* Copyright 2023 Pelican Mapping
* MIT License
*/
#pragma once
#include <rocky/vsg/ecs/MotionSystem.h>
#include <rocky/vsg/imgui/ImGuiImage.h>
#include <random>
#include "helpers.h"
using namespace ROCKY_NAMESPACE;
using namespace std::chrono_literals;
namespace
{
// Custom component that represents a simulated object
struct SimulatedPlatform
{
std::string name;
unsigned sequence = 0;
};
// Simple simulation system runinng in its own thread.
// It uses a MotionSystem to process Motion components and update
// their corresponding Transform components.
//
// Be careful. A background thread manipulating the Registry must be careful
// not to starve the rendering thread by write-locking the Registry for too long.
class Simulator
{
public:
Application& app;
MotionSystem motion;
float sim_hertz = 10.0f; // updates per second
Simulator(Application& in_app) :
app(in_app),
motion(in_app.registry) { }
void run()
{
app.workers.start("rocky::simulation", app.io(), [this]()
{
run_at_frequency f(sim_hertz);
motion.update(app.vsgcontext);
app.vsgcontext->requestFrame();
return true;
});
}
};
// Create a number of entities with random positions and motions
std::vector<entt::entity> createEntities(entt::registry& registry, unsigned count,
ImGuiImage& image, bool& showPosition, VSGContext context)
{
std::vector<entt::entity> entities;
auto renderEntity = [&image, &showPosition, count, context](WidgetInstance& i)
{
auto& platform = i.registry.get<SimulatedPlatform>(i.entity);
auto& xform = i.registry.get<Transform>(i.entity);
auto pointECEF = xform.position.transform(SRS::ECEF);
float red = 0.0f, green = 0.0f, blue = 0.0f;
if (platform.sequence < (count / 3)) blue = 0.5f;
else if (platform.sequence < (2 * count / 3)) green = 0.5f;
else red = 0.5f;
ImGui::SetCurrentContext(i.context);
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(2, 1));
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0.01f, 0.01f, 0.01f, 0.9f));
ImGui::PushStyleColor(ImGuiCol_Border, ImVec4(0, 1, 0, 1.0f));
ImGui::SetNextWindowPos(
ImVec2{ i.position.x - image.size().x/2, i.position.y - image.size().y / 2 },
ImGuiCond_Always,
ImVec2{ 0.0f, 0.0f }); // pivot point in the upper left
ImGui::Begin(i.uid.c_str(), nullptr, i.windowFlags);
{
// calculate the bearing for our icon:
auto& motion = i.registry.get<MotionGreatCircle>(i.entity);
auto heading = pointECEF.srs.ellipsoid().heading(pointECEF, motion.normalAxis);
if (ImGui::BeginTable("asset", 2))
{
ImGui::TableNextColumn();
image.render(image.size(), heading);
ImGui::TableNextColumn();
ImGui::TextUnformatted(platform.name.c_str());
if (showPosition)
{
auto pointWGS84 = pointECEF.transform(SRS::WGS84);
ImGui::Separator();
ImGui::Text("Pos: %.2f, %.2f", pointWGS84.y, pointWGS84.x);
ImGui::Separator();
ImGui::Text("Alt: %.0f m", pointWGS84.z);
ImGui::Separator();
ImGui::Text("Hdg: %.1f", heading);
}
ImGui::EndTable();
}
}
// update decluttering region:
auto size = ImGui::GetWindowSize();
auto& dc = i.registry.get<Declutter>(i.entity);
dc.rect = Rect(0, 0, size.x, size.y);
// respond to intersections:
i.checkFocus();
ImGui::End();
ImGui::PopStyleColor(2);
ImGui::PopStyleVar(1);
};
auto ll_to_ecef = SRS::WGS84.to(SRS::ECEF);
// Make the drop-line style and geometry. We can share this across all simulated platforms.
auto dropEntity = registry.create();
auto& dropStyle = registry.emplace<LineStyle>(dropEntity);
dropStyle.width = 2.0f;
dropStyle.color = Color{ 0.8f, 0.4f, 0.4f, 1.0f };
auto& dropGeom = registry.emplace<LineGeometry>(dropEntity);
dropGeom.points = { {0.0, 0.0, 0.0}, {0.0, 0.0, -1e6} };
entities.reserve(count);
std::mt19937 mt;
std::uniform_real_distribution<float> rand_unit(0.0, 1.0);
for (unsigned i = 0; i < count; ++i)
{
float t = (float)i / (float)(count);
// Create a host entity & platform:
auto entity = registry.create();
auto& platform = registry.emplace<SimulatedPlatform>(entity);
platform.name = std::string("Sim " + std::to_string((std::int64_t)entity));
platform.sequence = i;
double lat = -80.0 + rand_unit(mt) * 160.0;
double lon = -180 + rand_unit(mt) * 360.0;
double alt = 1000.0 + t * 100000.0;
GeoPoint pos_ecef = GeoPoint(SRS::WGS84, lon, lat, alt).transform(SRS::ECEF);
// Add a transform component:
auto& transform = registry.emplace<Transform>(entity);
transform.position = pos_ecef;
// We need this to support the drop-line. There is a small performance hit.
transform.topocentric = true;
// Add a motion component to represent movement:
double initial_bearing = -180.0 + rand_unit(mt) * 360.0;
auto& motion = registry.emplace<MotionGreatCircle>(entity);
motion.velocity = { -7500 + rand_unit(mt) * 15000, 0.0, 0.0 };
motion.normalAxis = pos_ecef.srs.ellipsoid().rotationAxis(pos_ecef, initial_bearing);
// Add a widget to render the HUD:
auto& widget = registry.emplace<Widget>(entity);
widget.render = renderEntity;
// Add the dropline for this entity:
registry.emplace<Line>(entity, dropGeom, dropStyle);
// Decluttering control. The presence of this component will allow the entity
// to participate in decluttering when it's enabled.
auto& declutter = registry.emplace<Declutter>(entity);
declutter.priority = (float)platform.sequence;
entities.emplace_back(entity);
}
return entities;
}
}
auto Demo_Simulation = [](Application& app)
{
// Make an entity for us to tether to and set it in motion
static NodeLayer::Ptr layer;
static vsg::ref_ptr<EntityNode> entityNode;
static Status status;
static Simulator sim(app);
static ImGuiImage widgetImage;
static unsigned num_platforms = 1500;
static bool showPosition = false;
static bool tethering = false;
if (status.failed())
{
ImGui::TextColored(ImVec4(1, 0, 0, 1), "%s", "Image load failed!");
ImGui::TextColored(ImVec4(1, 0, 0, 1), "%s", status.error().message.c_str());
return;
}
const float s = 20.0;
// Load an image to use in our widget.
if (!widgetImage)
{
// add an icon:
auto io = app.vsgcontext->io;
auto image = io.services().readImageFromURI(ICON_AIRPORT, io);
if (image.ok())
{
image.value()->flipVerticalInPlace();
widgetImage = ImGuiImage(image.value(), app.vsgcontext);
}
}
// Create the layer, the entity node, and start up the background sim thread.
if (!layer)
{
entityNode = EntityNode::create(app.registry);
layer = NodeLayer::create(entityNode);
layer->name = "Simulation Entities";
if (layer->open(app.io()))
app.mapNode->map->add(layer);
sim.run();
app.vsgcontext->requestFrame();
}
// Create some entities!
if (entityNode->entities.empty())
{
app.registry.write([&](entt::registry& registry)
{
entityNode->entities = createEntities(registry, num_platforms,
widgetImage, showPosition, app.vsgcontext);
});
}
ImGui::TextUnformatted("TIP: toggle visibility in the Map panel!");
ImGui::TextUnformatted("TIP: prevent overlap in the Decluttering panel!");
ImGui::Separator();
if (ImGuiLTable::Begin("simulation"))
{
ImGuiLTable::SliderInt("Entities", (int*)&num_platforms, 1, 5000);
if (ImGuiLTable::Button("Refresh"))
{
if (tethering)
{
auto& view = app.display.window(0).view(0);
if (auto manip = MapManipulator::get(view.vsgView))
manip->home();
}
app.registry.write([&](entt::registry& r)
{
r.destroy(entityNode->entities.begin(), entityNode->entities.end());
});
entityNode->entities.clear();
}
ImGuiLTable::SliderFloat("Update rate", &sim.sim_hertz, 1.0f, 120.0f, "%.0f hz");
ImGuiLTable::Checkbox("Show position", &showPosition);
if (ImGuiLTable::Checkbox("Tethering", &tethering))
{
auto& view = app.display.window(0).view(0);
if (auto manip = MapManipulator::get(view.vsgView))
{
if (tethering)
{
// To tether, create a Viewpoint object with a "pointFunction" that
// returns the current location of the tracked object.
Viewpoint vp;
vp.range = 1e6;
vp.pitch = -45;
vp.heading = 45;
vp.pointFunction = [&]()
{
return app.registry.read()->get<Transform>(*entityNode->entities.begin()).position;
};
manip->setViewpoint(vp, 2.0s);
}
else
{
manip->home();
}
}
}
ImGuiLTable::End();
}
};