-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulateNavigation.cpp
More file actions
544 lines (488 loc) · 24.1 KB
/
SimulateNavigation.cpp
File metadata and controls
544 lines (488 loc) · 24.1 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
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
// SPDX-FileCopyrightText: 2021-2026 Magic Lane International B.V. <info@magiclane.com>
// SPDX-License-Identifier: Apache-2.0
//
// Contact Magic Lane at <info@magiclane.com> for SDK licensing options.
#include "Environment.h"
#include "BitmapImpl.h"
#include <API/GEM_MapView.h>
#include <API/GEM_RoutingService.h>
#include <API/GEM_NavigationListener.h>
#include <API/GEM_NavigationService.h>
#include <imgui.h>
namespace
{
////////////////////////////////////////////////////////////////////////
class MyNavigationListener : public gem::INavigationListener
{
private:
const ImVec2 INSTRUCTION_ICON_SIZE{70, 70};
void onNavigationStarted() override
{
GEM_LOGI( "Simulation started" );
}
void onNavigationInstructionUpdated( const gem::NavigationInstruction &navinstruct ) override
{
GEM_LOGE("NAV INSTRUCTION ( E N T R Y P O I N T ) %%%%%%%%%%%%%%%%%");
gem::String instructionText = navinstruct.getNextTurnInstruction();
instructionText.fallbackToLegacyUnicode();
if (navinstruct && !navinstruct.isDefault())
{
GEM_LOGE("NAV INSTRUCTION ( %s ) %%%%%%%%%%%%%%%%%", instructionText.toStdString().c_str());
}
else if (navinstruct && navinstruct.isDefault())
{
GEM_LOGE("NAV INSTRUCTION ( D E F A U L T ) %%%%%%%%%%%%%%%%%");
}
else if (!navinstruct)
{
GEM_LOGE("NAV INSTRUCTION ( N U L L ) %%%%%%%%%%%%%%%%%");
}
else
{
GEM_LOGE("NAV INSTRUCTION ( O T H E R ) %%%%%%%%%%%%%%%%%");
}
}
void onWaypointReached( const gem::Landmark& lmk ) override
{
GEM_LOGI( "Intermediary destination reached" );
}
void onDestinationReached( const gem::Landmark& lmk ) override
{
GEM_LOGI( "Final destination reached" );
}
void onNavigationError( int error ) override
{
GEM_LOGI( "Nav error: %d", error );
}
void onRouteUpdated( const gem::Route& route ) override
{
GEM_LOGI( "Route updated" );
}
void onNavigationSound( gem::ISound const& sound ) override
{
GEM_LOGI( "Nav sound play request" );
}
bool canPlayNavigationSound() override
{
return true;
}
void onBetterRouteDetected( const gem::Route& route, int travelTime, int delay, int timeGain ) override
{
}
};
#define NEXT_TURN_ICON_SIZE_PIXELS 80
auto getUiRender()
{
return std::bind([](gem::StrongPointer<gem::MapView> mapView)
{
ImGuiIO& io = ImGui::GetIO();
static bool iskm = true;
const ImGuiViewport* main_viewport = ImGui::GetMainViewport();
ImGui::SetNextWindowPos(ImVec2(main_viewport->WorkPos.x + 0, main_viewport->WorkPos.y + 120), ImGuiCond_FirstUseEver);
ImGui::Begin("panel", nullptr, ImGuiWindowFlags_NoMove
| ImGuiWindowFlags_NoDecoration
| ImGuiWindowFlags_AlwaysAutoResize
| ImGuiWindowFlags_NoSavedSettings);
auto navListener = gem::StrongPointerFactory<MyNavigationListener>();
ImGui::PushFont(io.Fonts->Fonts[0]);
if (!gem::NavigationService().isNavigationActive()
&& !gem::NavigationService().isSimulationActive())
{
ImGui::Spacing();
if (ImGui::Button("Simulate navigation on a route"))
{
// At least 2 waypoints define the route, the first is the departure position, and the last is the destination.
// There can be zero or more intermediate waypoints through which the route passes in the order they are listed.
// The coordinates are {latitude,longitude} in degrees; the landmark name is optional and can be an empty string.
gem::LandmarkList waypoints({ { "San Francisco", { 37.77903, -122.41991 } }, { "San Jose", { 37.33619, -121.89058 } } });
// Compute route using these preferences: car / fastest / without alternatives in result
gem::RouteList routes;
ProgressListener routeListener;
gem::RoutingService().calculateRoute(routes, waypoints, gem::RoutePreferences().setTransportMode(gem::RTM_Car).setRouteType(gem::RT_Fastest).setAlternativesSchema(gem::AS_Never), &routeListener);
// Wait until route calculation finished & check success
if (WAIT_UNTIL(std::bind(&ProgressListener::IsFinished, &routeListener), 30000) && routeListener.GetError() == gem::KNoError && !routes.empty())
{
// Add the first resulting route (at index 0) to map view
mapView->preferences().routes().add(routes[0]);
// Start simulated navigation along the route
gem::NavigationService().startSimulation(routes[0], navListener, gem::ProgressListener());
// Start follow GPS positions ( generated by the simulation ) - camera follows the position along the route
mapView->startFollowingPosition();
}
}
}
if (!mapView->isFollowingPosition())
{
ImGui::Spacing();
if (ImGui::Button("Follow position"))
{
mapView->startFollowingPosition();
}
}
else
{
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.f, 0.f, 1.f, 1.f));
ImGui::PushFont(io.Fonts->Fonts[1]);
ImGui::Text("Following position!");
ImGui::PopFont();
ImGui::PopStyleColor(1);
}
if (iskm)
{
ImGui::Spacing();
if (ImGui::Button("Switch to miles"))
{
iskm = false;
}
}
else
{
ImGui::Spacing();
if (ImGui::Button("Switch to kilometers"))
{
iskm = true;
}
}
if (gem::NavigationService().isNavigationActive()
|| gem::NavigationService().isSimulationActive())
{
ImGui::Spacing();
if (ImGui::Button("Stop navigation"))
gem::NavigationService().cancelNavigation(navListener);
}
ImGui::PopFont();
ImGui::End();
//////////////////////////////////////////////////////////
// TOP STATUS PANEL
//////////////////////////////////////////////////////////
if (gem::NavigationService().isNavigationActive()
|| gem::NavigationService().isSimulationActive())
{
ImGui::SetNextWindowBgAlpha(1.0f);
ImGui::SetNextWindowPos(ImVec2(main_viewport->WorkPos.x + 0, main_viewport->WorkPos.y + 0), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(main_viewport->WorkSize.x, 108));
ImGui::GetStyle().WindowRounding = 0.0f;
ImGui::Begin("panel2", nullptr, ImGuiWindowFlags_NoMove
| ImGuiWindowFlags_NoDecoration
| ImGuiWindowFlags_AlwaysAutoResize
//| ImGuiWindowFlags_NoBackground
| ImGuiWindowFlags_NoSavedSettings);
const ImU32 colorblack = ImGui::GetColorU32(ImVec4(0, 0, 0, 1));
ImVec2 wPos = ImGui::GetWindowPos();
ImVec2 wSize = ImGui::GetWindowSize();
ImGui::GetWindowDrawList()->AddRectFilled(ImVec2(wPos.x + 0, wPos.y + 0), ImVec2(wSize.x, wSize.y), colorblack);
auto navinstruct = gem::NavigationService().getNavigationInstruction();
gem::String instructionText = navinstruct.getNextTurnInstruction();
instructionText.fallbackToLegacyUnicode();
if (ImGui::BeginTable("top_instruction_panel", 2))
{
ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, NEXT_TURN_ICON_SIZE_PIXELS + 12);
ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, wSize.x - 12 - NEXT_TURN_ICON_SIZE_PIXELS);
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
//////////////////////////////////////////////////////////
// TURN ICON
//////////////////////////////////////////////////////////
{
const ImVec2 nextTurnIconSize{ NEXT_TURN_ICON_SIZE_PIXELS, NEXT_TURN_ICON_SIZE_PIXELS };
gem::Rgba color(255, 0, 255, 255);
gem::AbstractGeometryImageRenderSettings settings(gem::Rgba::white(), gem::Rgba::black(), color);
auto bitmap = gem::StrongPointerFactory<BitmapImpl>(70, 70);
navinstruct.getNextTurnDetails().getAbstractGeometryImage().render(*bitmap, settings);
unsigned int textureId = BitmapImpl::LoadTextureIntoGPU(bitmap->size().width, bitmap->size().height, bitmap->begin());
ImGui::Image(textureId, nextTurnIconSize);
}
ImGui::TableSetColumnIndex(1);
//////////////////////////////////////////////////////////
// NEXT STREET NAME
//////////////////////////////////////////////////////////
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.f, 1.f, 1.f, 1.f));
ImGui::PushFont(io.Fonts->Fonts[2]);
ImGui::Text("%s", navinstruct.getNextStreetName().toStdString().c_str());
ImGui::PopFont();
ImGui::PopStyleColor(1);
//////////////////////////////////////////////////////////
// NEXT TURN INSTRUCTION TEXT
//////////////////////////////////////////////////////////
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.f, 1.f, 1.f, 1.f));
ImGui::PushFont(io.Fonts->Fonts[1]);
ImGui::Text("%s", instructionText.toStdString().c_str());
auto remainingDistanceToNextTurn = navinstruct.getTimeDistanceToNextTurn().getTotalDistance();
auto remainingTimeToNextTurn = navinstruct.getTimeDistanceToNextTurn().getTotalTime();
ImGui::SameLine();
ImGui::Text(" ");
ImGui::SameLine();
//////////////////////////////////////////////////////////
// NEXT TURN INSTRUCTION DISTANCE
//////////////////////////////////////////////////////////
if (iskm)
{
if (remainingDistanceToNextTurn > 1000)
{
ImGui::Text("%.1f ", (float)(remainingDistanceToNextTurn * .001));
ImGui::SameLine();
ImGui::Text("km ");
}
else
{
ImGui::Text("%d ", (int)(remainingDistanceToNextTurn));
ImGui::SameLine();
ImGui::Text("m ");
}
ImGui::SameLine();
}
else
{
if ((remainingDistanceToNextTurn / 1609.344 * 5280) > 5280)
{
ImGui::Text("%.1f ", (float)((remainingDistanceToNextTurn / 1609.344 * 5280)/5280.0));
ImGui::SameLine();
ImGui::Text("mi ");
}
else
{
ImGui::Text("%d ", (int)(remainingDistanceToNextTurn / 1609.344 * 5280));
ImGui::SameLine();
ImGui::Text("ft ");
}
ImGui::SameLine();
}
ImGui::PopFont();
ImGui::PopStyleColor(1);
ImGui::EndTable();
} // end table
ImGuiStyle& style = ImGui::GetStyle();
ImVec4* colors = style.Colors;
ImGui::End();
//////////////////////////////////////////////////////////
// BOTTOM STATUS PANEL
//////////////////////////////////////////////////////////
{
ImGui::SetNextWindowBgAlpha(1.0f);
ImGui::SetNextWindowPos(ImVec2(main_viewport->Pos.x + 20, main_viewport->Pos.y + main_viewport->WorkSize.y - 72));
ImGui::SetNextWindowSize(ImVec2(main_viewport->WorkSize.x - 40, 64));
ImGui::GetStyle().WindowRounding = 12.0f;
ImGui::Begin("panel4", nullptr, ImGuiWindowFlags_NoMove
| ImGuiWindowFlags_NoDecoration
//| ImGuiWindowFlags_AlwaysAutoResize
//| ImGuiWindowFlags_NoBackground
| ImGuiWindowFlags_NoSavedSettings);
ImVec4 mycolor0{ 0,0,0,255 };
ImVec4 mycolor1{ 255,0,0,255 };
ImVec4 mycolor2{ 0,255,0,255 };
ImVec4 mycolor3{ 0,0,255,255 };
ImVec4 mycolor7{ 255,255,255,255 };
auto remainingTravelTime = navinstruct.getRemainingTravelTimeDistance().getTotalTime();
auto remainingTravelDistance = navinstruct.getRemainingTravelTimeDistance().getTotalDistance();
time_t currentTimeSec = (time_t)(gem::Time::getLocalTime().asInt() / 1000);
time_t eta = (time_t)(currentTimeSec + remainingTravelTime);
struct tm tmcurrent, etatm;
struct tm* tmptr = gmtime((const time_t*)&(currentTimeSec));
memcpy(&tmcurrent, tmptr, sizeof(struct tm));
tmptr = gmtime((const time_t*)&(eta));
memcpy(&etatm, tmptr, sizeof(struct tm));
/*
//////////////////////////////////////////////////////////
// CURRENT TIME
//////////////////////////////////////////////////////////
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.f, 0.f, 1.f, 1.f));
//ImGui::SetWindowFontScale(2);
//auto rpos = ImGui::GetCursorScreenPos();
ImGui::PushFont(io.Fonts->Fonts[1]);
ImGui::Text("%02d:%02d ", tmcurrent.tm_hour, tmcurrent.tm_min);
ImGui::PopFont();
ImGui::SameLine();
ImGui::PopStyleColor(1);
*/
//////////////////////////////////////////////////////////
// REMAINING TRAVEL HOURS, MIN, SEC: PRINT ONLY HOURS:MIN IF >= 1H OR MIN:SEC IF < 1H
//////////////////////////////////////////////////////////
//ImGui::Text("Time %d:%02d:%02d", (int)(remainingTravelTime / 3600), (int)((remainingTravelTime % 3600) / 60), (int)(remainingTravelTime % 60));
int remainingTravelHours = (int)(remainingTravelTime / 3600);
int remainingTravelMin = (int)((remainingTravelTime % 3600) / 60);
int remainingTravelSec = (int)(remainingTravelTime % 60);
//////////////////////////////////////////////////////////
// COMPUTE BOTTOM STAT STR LENGTH TO ENABLE CENTERING STR HORIZONTALLY AND VERTICALLY!
//////////////////////////////////////////////////////////
ImGui::PushFont(io.Fonts->Fonts[0]);
float font_size1 = ImGui::GetFontSize();
auto textWidth1 = ImGui::CalcTextSize("ETAminkm").x;
ImGui::PopFont();
ImGui::PushFont(io.Fonts->Fonts[1]);
float font_size2 = ImGui::GetFontSize();
auto textWidth2 = ImGui::CalcTextSize("00:00 00").x;
auto textHeight2 = ImGui::CalcTextSize("00").y;
ImGui::PopFont();
if (remainingTravelHours > 0)
{
int digitsInHours = (int)(log10(remainingTravelHours)) + 1;
ImGui::PushFont(io.Fonts->Fonts[1]);
textWidth2 += digitsInHours * ImGui::CalcTextSize("0").x;
ImGui::PopFont();
ImGui::PushFont(io.Fonts->Fonts[0]);
textWidth1 += ImGui::CalcTextSize("h ").x;
ImGui::PopFont();
}
else
{
ImGui::PushFont(io.Fonts->Fonts[1]);
textWidth2 += ImGui::CalcTextSize(" 00").x;
ImGui::PopFont();
}
int digitsInMeters = (int)(log10(remainingTravelDistance)) + 1;
if (remainingTravelDistance > 1000)
{
digitsInMeters -= 1;//printed %.1f
}
ImGui::PushFont(io.Fonts->Fonts[1]);
textWidth2 += digitsInMeters * ImGui::CalcTextSize("0").x;
ImGui::PopFont();
ImGui::PushFont(io.Fonts->Fonts[0]);
textWidth1 += ImGui::CalcTextSize("km").x;
ImGui::PopFont();
ImGui::SetCursorScreenPos(ImVec2((ImGui::GetWindowSize().x - textWidth2 - textWidth1) * 0.4f + ImGui::GetWindowPos().x,
(ImGui::GetWindowSize().y - textHeight2) * 0.5f + ImGui::GetWindowPos().y));
//////////////////////////////////////////////////////////
// ETA
//////////////////////////////////////////////////////////
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.f, 0.f, 0.f, 1.f));
//auto rpos = ImGui::GetCursorScreenPos();
//ImGui::SetCursorScreenPos(ImVec2(rpos.x, rpos.y + 12));
ImGui::PushFont(io.Fonts->Fonts[0]);
ImGui::Text("ETA");
ImGui::PopFont();
ImGui::SameLine();
ImGui::PushFont(io.Fonts->Fonts[1]);
ImGui::Text("%02d:%02d ", etatm.tm_hour, etatm.tm_min);
ImGui::PopFont();
ImGui::SameLine();
ImGui::PopStyleColor(1);
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.f, .5f, 0.f, 1.f));
if (remainingTravelHours > 0)
{
ImGui::PushFont(io.Fonts->Fonts[1]);
ImGui::Text("%d", remainingTravelHours);
ImGui::PopFont();
ImGui::SameLine();
ImGui::PushFont(io.Fonts->Fonts[0]);
ImGui::Text("h ");
ImGui::PopFont();
ImGui::SameLine();
}
{
ImGui::PushFont(io.Fonts->Fonts[1]);
ImGui::Text("%02d", remainingTravelMin);
ImGui::PopFont();
ImGui::SameLine();
ImGui::PushFont(io.Fonts->Fonts[0]);
ImGui::Text("min");
ImGui::PopFont();
ImGui::SameLine();
}
if (remainingTravelHours == 0)
{
ImGui::PushFont(io.Fonts->Fonts[1]);
ImGui::Text(" %02d", remainingTravelSec);
ImGui::PopFont();
ImGui::SameLine();
}
ImGui::PopStyleColor(1);
ImGui::PushFont(io.Fonts->Fonts[1]);
ImGui::Text(" ");
ImGui::PopFont();
ImGui::SameLine();
//////////////////////////////////////////////////////////
// REMAINING TRAVEL DISTANCE KM
//////////////////////////////////////////////////////////
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.f, 0.f, 1.f, 1.f));
if (iskm)
{
if (remainingTravelDistance > 1000)
{
ImGui::PushFont(io.Fonts->Fonts[1]);
ImGui::Text("%.1f", (float)(remainingTravelDistance * .001));
ImGui::PopFont();
ImGui::SameLine();
ImGui::PushFont(io.Fonts->Fonts[0]);
ImGui::Text("km");
ImGui::PopFont();
}
else
{
ImGui::PushFont(io.Fonts->Fonts[1]);
ImGui::Text("%.1f", (float)(remainingTravelDistance));
ImGui::PopFont();
ImGui::SameLine();
ImGui::PushFont(io.Fonts->Fonts[0]);
ImGui::Text("m");
ImGui::PopFont();
}
}
else
{
if (remainingTravelDistance > 1000)
{
ImGui::PushFont(io.Fonts->Fonts[1]);
ImGui::Text("%.1f", (float)(remainingTravelDistance / 1609.344));
ImGui::PopFont();
ImGui::SameLine();
ImGui::PushFont(io.Fonts->Fonts[0]);
ImGui::Text("mi");
ImGui::PopFont();
}
else
{
ImGui::PushFont(io.Fonts->Fonts[1]);
ImGui::Text("%.1f", (float)(remainingTravelDistance / 1609.344 * 5280));
ImGui::PopFont();
ImGui::SameLine();
ImGui::PushFont(io.Fonts->Fonts[0]);
ImGui::Text("ft");
ImGui::PopFont();
}
}
ImGui::PopStyleColor(1);
colors[ImGuiCol_WindowBg] = mycolor7;
ImGui::End();
}
}
}
, std::placeholders::_1);
}
} //namespace
int main( int argc, char** argv )
{
// Get new project API token from:
// https://developer.magiclane.com/api/projects
std::string projectApiToken = "";
#if defined(API_TOKEN)
projectApiToken = std::string( API_TOKEN );
#else
auto value = std::getenv( "GEM_TOKEN" );
if( value != nullptr )
projectApiToken = value;
#endif
// Sdk objects can be created & used below this line
Environment::SdkSession session(projectApiToken, { argc > 1 ? argv[1] : "" }); // SDK API debug logging path
if (GEM_GET_API_ERROR() != gem::KNoError) // check for errors after session creation
return GEM_GET_API_ERROR();
// Create a map view
CTouchEventListener pTouchEventListener;
gem::StrongPointer<gem::MapView> mapView = gem::MapView::produce(session.produceOpenGLContext(Environment::WindowFrameworks::ImGUI, "SimulateNavigation", &pTouchEventListener, getUiRender()));
if ( !mapView )
{
GEM_LOGE( "Error creating gem::MapView: %d", GEM_GET_API_ERROR() );
}
WAIT_UNTIL_WINDOW_CLOSE();
return 0;
}
#if ( defined(_WIN32) || defined(_WIN64) ) && !defined(__MINGW32__) && !defined(__MINGW64__)
int WINAPI WinMain( HINSTANCE hInstance, // Instance
HINSTANCE hPrevInstance, // Previous Instance
LPSTR lpCmdLine, // Command Line Parameters
int nCmdShow )
{
main( 0, nullptr );
return 0;
}
#endif