OpenCPN Partial API docs
Loading...
Searching...
No Matches
routeman.cpp
1/***************************************************************************
2 *
3 * Project: OpenCPN
4 * Purpose: Route Manager
5 * Author: David Register
6 *
7 ***************************************************************************
8 * Copyright (C) 2010 by David S. Register *
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
19 * *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program; if not, write to the *
22 * Free Software Foundation, Inc., *
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
24 **************************************************************************/
25#include <cmath>
26#include <memory>
27#include <vector>
28
29#include <math.h>
30#include <stdlib.h>
31#include <time.h>
32
33#include <wx/wxprec.h>
34
35#include <wx/image.h>
36#include <wx/jsonval.h>
37#include <wx/listimpl.cpp>
38#include <wx/tokenzr.h>
39
40#include "model/ais_decoder.h"
41#include "model/autopilot_output.h"
42#include "model/base_platform.h"
43#include "model/comm_n0183_output.h"
44#include "model/comm_vars.h"
45#include "model/config_vars.h"
46#include "model/cutil.h"
47#include "model/georef.h"
48#include "model/nav_object_database.h"
49#include "model/navutil_base.h"
50#include "model/nmea_ctx_factory.h"
51#include "model/own_ship.h"
52#include "model/route.h"
53#include "model/routeman.h"
54#include "model/track.h"
55
56#include "observable_globvar.h"
59#include "model/navobj_db.h"
60
61#ifdef __ANDROID__
62#include "androidUTIL.h"
63#endif
64
65bool g_bPluginHandleAutopilotRoute;
66
67Routeman *g_pRouteMan;
68Route *pAISMOBRoute;
69
70RoutePoint *pAnchorWatchPoint1;
71RoutePoint *pAnchorWatchPoint2;
72
73RouteList *pRouteList;
74
75float g_ChartScaleFactorExp;
76
77// List definitions for Waypoint Manager Icons
78WX_DECLARE_LIST(wxBitmap, markicon_bitmap_list_type);
79WX_DECLARE_LIST(wxString, markicon_key_list_type);
80WX_DECLARE_LIST(wxString, markicon_description_list_type);
81
82// List implementation for Waypoint Manager Icons
83#include <wx/listimpl.cpp>
84WX_DEFINE_LIST(markicon_bitmap_list_type);
85WX_DEFINE_LIST(markicon_key_list_type);
86WX_DEFINE_LIST(markicon_description_list_type);
87
88// Helper conditional file name dir slash
89void appendOSDirSlash(wxString *pString);
90
91static void ActivatePersistedRoute(Routeman *routeman) {
92 if (g_active_route == "") {
93 wxLogWarning("\"Persist route\" but no persisted route configured");
94 return;
95 }
96 Route *route = routeman->FindRouteByGUID(g_active_route);
97 if (!route) {
98 wxLogWarning("Persisted route GUID not available");
99 return;
100 }
101 routeman->ActivateRoute(route); // FIXME (leamas) better start point
102}
103
104//--------------------------------------------------------------------------------
105// Routeman "Route Manager"
106//--------------------------------------------------------------------------------
107
108Routeman::Routeman(struct RoutePropDlgCtx ctx,
109 struct RoutemanDlgCtx route_dlg_ctx)
110 : pActiveRoute(0),
111 pActivePoint(0),
112 pRouteActivatePoint(0),
113 m_NMEA0183(NmeaCtxFactory()),
114 m_prop_dlg_ctx(ctx),
115 m_route_dlg_ctx(route_dlg_ctx) {
116 GlobalVar<wxString> active_route(&g_active_route);
117 auto route_action = [&](wxCommandEvent) {
118 if (g_persist_active_route) ActivatePersistedRoute(this);
119 };
120 active_route_listener.Init(active_route, route_action);
121}
122
123Routeman::~Routeman() {
124 if (pRouteActivatePoint) delete pRouteActivatePoint;
125}
126
127bool Routeman::IsRouteValid(Route *pRoute) {
128 wxRouteListNode *node = pRouteList->GetFirst();
129 while (node) {
130 if (pRoute == node->GetData()) return true;
131 node = node->GetNext();
132 }
133 return false;
134}
135
136// Make a 2-D search to find the route containing a given waypoint
137Route *Routeman::FindRouteContainingWaypoint(RoutePoint *pWP) {
138 wxRouteListNode *node = pRouteList->GetFirst();
139 while (node) {
140 Route *proute = node->GetData();
141
142 wxRoutePointListNode *pnode = (proute->pRoutePointList)->GetFirst();
143 while (pnode) {
144 RoutePoint *prp = pnode->GetData();
145 if (prp == pWP) return proute;
146 pnode = pnode->GetNext();
147 }
148
149 node = node->GetNext();
150 }
151
152 return NULL; // not found
153}
154
155// Make a 2-D search to find the route containing a given waypoint, by GUID
156Route *Routeman::FindRouteContainingWaypoint(const std::string &guid) {
157 wxRouteListNode *node = pRouteList->GetFirst();
158 while (node) {
159 Route *proute = node->GetData();
160
161 wxRoutePointListNode *pnode = (proute->pRoutePointList)->GetFirst();
162 while (pnode) {
163 RoutePoint *prp = pnode->GetData();
164 if (prp->m_GUID == guid) return proute;
165 pnode = pnode->GetNext();
166 }
167
168 node = node->GetNext();
169 }
170
171 return NULL; // not found
172}
173
174// Make a 2-D search to find the visual route containing a given waypoint
175Route *Routeman::FindVisibleRouteContainingWaypoint(RoutePoint *pWP) {
176 wxRouteListNode *node = pRouteList->GetFirst();
177 while (node) {
178 Route *proute = node->GetData();
179 if (proute->IsVisible()) {
180 wxRoutePointListNode *pnode = (proute->pRoutePointList)->GetFirst();
181 while (pnode) {
182 RoutePoint *prp = pnode->GetData();
183 if (prp == pWP) return proute;
184 pnode = pnode->GetNext();
185 }
186 }
187
188 node = node->GetNext();
189 }
190
191 return NULL; // not found
192}
193
195 wxArrayPtrVoid *pArray = new wxArrayPtrVoid;
196
197 wxRouteListNode *route_node = pRouteList->GetFirst();
198 while (route_node) {
199 Route *proute = route_node->GetData();
200
201 wxRoutePointListNode *waypoint_node = (proute->pRoutePointList)->GetFirst();
202 while (waypoint_node) {
203 RoutePoint *prp = waypoint_node->GetData();
204 if (prp == pWP) { // success
205 pArray->Add((void *)proute);
206 break; // only add a route to the array once, even if there are
207 // duplicate points in the route...See FS#1743
208 }
209
210 waypoint_node = waypoint_node->GetNext(); // next waypoint
211 }
212
213 route_node = route_node->GetNext(); // next route
214 }
215
216 if (pArray->GetCount())
217 return pArray;
218
219 else {
220 delete pArray;
221 return NULL;
222 }
223}
224
225void Routeman::RemovePointFromRoute(RoutePoint *point, Route *route,
226 int route_state) {
227 // Rebuild the route selectables
228 pSelect->DeleteAllSelectableRoutePoints(route);
229 pSelect->DeleteAllSelectableRouteSegments(route);
230
231 route->RemovePoint(point);
232
233 // Check for 1 point routes. If we are creating a route, this is an undo, so
234 // keep the 1 point.
235 if (route->GetnPoints() <= 1 && route_state == 0) {
236 g_pRouteMan->DeleteRoute(route);
237 route = NULL;
238 }
239 // Add this point back into the selectables
240 pSelect->AddSelectableRoutePoint(point->m_lat, point->m_lon, point);
241
242 // if (pRoutePropDialog && (pRoutePropDialog->IsShown())) {
243 // pRoutePropDialog->SetRouteAndUpdate(route, true);
244 // }
245 m_prop_dlg_ctx.set_route_and_update(route);
246}
247
248RoutePoint *Routeman::FindBestActivatePoint(Route *pR, double lat, double lon,
249 double cog, double sog) {
250 if (!pR) return NULL;
251
252 // Walk thru all the points to find the "best"
253 RoutePoint *best_point = NULL;
254 double min_time_found = 1e6;
255
256 wxRoutePointListNode *node = (pR->pRoutePointList)->GetFirst();
257 while (node) {
258 RoutePoint *pn = node->GetData();
259
260 double brg, dist;
261 DistanceBearingMercator(pn->m_lat, pn->m_lon, lat, lon, &brg, &dist);
262
263 double angle = brg - cog;
264 double soa = cos(angle * PI / 180.);
265
266 double time_to_wp = dist / soa;
267
268 if (time_to_wp > 0) {
269 if (time_to_wp < min_time_found) {
270 min_time_found = time_to_wp;
271 best_point = pn;
272 }
273 }
274 node = node->GetNext();
275 }
276 return best_point;
277}
278
279bool Routeman::ActivateRoute(Route *pRouteToActivate, RoutePoint *pStartPoint) {
280 g_bAllowShipToActive = false;
281 wxJSONValue v;
282 v[_T("Route_activated")] = pRouteToActivate->m_RouteNameString;
283 v[_T("GUID")] = pRouteToActivate->m_GUID;
284 json_msg.Notify(std::make_shared<wxJSONValue>(v), "OCPN_RTE_ACTIVATED");
285 if (g_bPluginHandleAutopilotRoute) return true;
286
287 // Capture and maintain a list of data connections configured as "output"
288 // This is performed on "Activate()" to allow dynamic re-config of drivers
289 m_have_n0183_out = false;
290 m_have_n2000_out = false;
291
292 m_output_drivers.clear();
293 for (const auto &handle : GetActiveDrivers()) {
294 const auto &attributes = GetAttributes(handle);
295 if (attributes.find("protocol") == attributes.end()) continue;
296 if (attributes.at("protocol") == "nmea0183") {
297 if (attributes.find("ioDirection") != attributes.end()) {
298 if ((attributes.at("ioDirection") == "IN/OUT") ||
299 (attributes.at("ioDirection") == "OUT")) {
300 m_output_drivers.push_back(handle);
301 m_have_n0183_out = true;
302 }
303 }
304 continue;
305 }
306 // N2K is always configured for output
307 if (attributes.at("protocol") == "nmea2000") {
308 m_output_drivers.push_back(handle);
309 m_have_n2000_out = true;
310 continue;
311 }
312 }
313
314 pActiveRoute = pRouteToActivate;
315 g_active_route = pActiveRoute->GetGUID();
316
317 if (pStartPoint) {
318 pActivePoint = pStartPoint;
319 } else {
320 wxRoutePointListNode *node = (pActiveRoute->pRoutePointList)->GetFirst();
321 pActivePoint = node->GetData(); // start at beginning
322 }
323
324 ActivateRoutePoint(pRouteToActivate, pActivePoint);
325
326 m_bArrival = false;
327 m_arrival_min = 1e6;
328 m_arrival_test = 0;
329
330 pRouteToActivate->m_bRtIsActive = true;
331
332 m_bDataValid = false;
333
334 m_route_dlg_ctx.show_with_fresh_fonts();
335 return true;
336}
337
339 g_bAllowShipToActive = false;
340 wxJSONValue v;
341 v[_T("GUID")] = pRP_target->m_GUID;
342 v[_T("WP_activated")] = pRP_target->GetName();
343
344 json_msg.Notify(std::make_shared<wxJSONValue>(v), "OCPN_WPT_ACTIVATED");
345
346 if (g_bPluginHandleAutopilotRoute) return true;
347
348 pActiveRoute = pA;
349
350 pActivePoint = pRP_target;
351 pActiveRoute->m_pRouteActivePoint = pRP_target;
352
353 wxRoutePointListNode *node = (pActiveRoute->pRoutePointList)->GetFirst();
354 while (node) {
355 RoutePoint *pn = node->GetData();
356 pn->m_bBlink = false; // turn off all blinking points
357 pn->m_bIsActive = false;
358
359 node = node->GetNext();
360 }
361
362 node = (pActiveRoute->pRoutePointList)->GetFirst();
363 RoutePoint *prp_first = node->GetData();
364
365 // If activating first point in route, create a "virtual" waypoint at present
366 // position
367 if (pRP_target == prp_first) {
368 if (pRouteActivatePoint) delete pRouteActivatePoint;
369
370 pRouteActivatePoint =
371 new RoutePoint(gLat, gLon, wxString(_T("")), wxString(_T("Begin")),
372 wxEmptyString, false); // Current location
373 pRouteActivatePoint->m_bShowName = false;
374
375 pActiveRouteSegmentBeginPoint = pRouteActivatePoint;
376 }
377
378 else {
379 prp_first->m_bBlink = false;
380 node = node->GetNext();
381 RoutePoint *np_prev = prp_first;
382 while (node) {
383 RoutePoint *pnext = node->GetData();
384 if (pnext == pRP_target) {
385 pActiveRouteSegmentBeginPoint = np_prev;
386 break;
387 }
388
389 np_prev = pnext;
390 node = node->GetNext();
391 }
392 }
393
394 pRP_target->m_bBlink = true; // blink the active point
395 pRP_target->m_bIsActive = true; // and active
396
397 g_blink_rect = pRP_target->CurrentRect_in_DC; // set up global blinker
398
399 m_bArrival = false;
400 m_arrival_min = 1e6;
401 m_arrival_test = 0;
402
403 // Update the RouteProperties Dialog, if currently shown
409 m_prop_dlg_ctx.set_enroute_point(pA, pActivePoint);
410 return true;
411}
412
413bool Routeman::ActivateNextPoint(Route *pr, bool skipped) {
414 g_bAllowShipToActive = false;
415 wxJSONValue v;
416 bool result = false;
417 if (pActivePoint) {
418 pActivePoint->m_bBlink = false;
419 pActivePoint->m_bIsActive = false;
420
421 v[_T("isSkipped")] = skipped;
422 v[_T("GUID")] = pActivePoint->m_GUID;
423 v[_T("GUID_WP_arrived")] = pActivePoint->m_GUID;
424 v[_T("WP_arrived")] = pActivePoint->GetName();
425 }
426 int n_index_active = pActiveRoute->GetIndexOf(pActivePoint);
427 int step = 1;
428 while (n_index_active == pActiveRoute->GetIndexOf(pActivePoint)) {
429 if ((n_index_active + step) <= pActiveRoute->GetnPoints()) {
430 pActiveRouteSegmentBeginPoint = pActivePoint;
431 pActiveRoute->m_pRouteActivePoint =
432 pActiveRoute->GetPoint(n_index_active + step);
433 pActivePoint = pActiveRoute->GetPoint(n_index_active + step);
434 step++;
435 result = true;
436 } else {
437 n_index_active = -1; // stop the while loop
438 result = false;
439 }
440 }
441 if (result) {
442 v[_T("Next_WP")] = pActivePoint->GetName();
443 v[_T("GUID_Next_WP")] = pActivePoint->m_GUID;
444
445 pActivePoint->m_bBlink = true;
446 pActivePoint->m_bIsActive = true;
447 g_blink_rect = pActivePoint->CurrentRect_in_DC; // set up global blinker
448 m_bArrival = false;
449 m_arrival_min = 1e6;
450 m_arrival_test = 0;
451
452 // Update the RouteProperties Dialog, if currently shown
458 m_prop_dlg_ctx.set_enroute_point(pr, pActivePoint);
459
460 json_msg.Notify(std::make_shared<wxJSONValue>(v), "OCPN_WPT_ARRIVED");
461 }
462 return result;
463}
464
465bool Routeman::DeactivateRoute(bool b_arrival) {
466 if (pActivePoint) {
467 pActivePoint->m_bBlink = false;
468 pActivePoint->m_bIsActive = false;
469 }
470
471 if (pActiveRoute) {
472 pActiveRoute->m_bRtIsActive = false;
473 pActiveRoute->m_pRouteActivePoint = NULL;
474 g_active_route.Clear();
475
476 wxJSONValue v;
477 if (!b_arrival) {
478 v[_T("Route_deactivated")] = pActiveRoute->m_RouteNameString;
479 v[_T("GUID")] = pActiveRoute->m_GUID;
480 json_msg.Notify(std::make_shared<wxJSONValue>(v), "OCPN_RTE_DEACTIVATED");
481 } else {
482 v[_T("GUID")] = pActiveRoute->m_GUID;
483 v[_T("Route_ended")] = pActiveRoute->m_RouteNameString;
484 json_msg.Notify(std::make_shared<wxJSONValue>(v), "OCPN_RTE_ENDED");
485 }
486 }
487
488 pActiveRoute = NULL;
489
490 if (pRouteActivatePoint) delete pRouteActivatePoint;
491 pRouteActivatePoint = NULL;
492
493 pActivePoint = NULL;
494
495 m_route_dlg_ctx.clear_console_background();
496 m_bDataValid = false;
497
498 return true;
499}
500
501bool Routeman::UpdateAutopilot() {
502 if (!pActiveRoute) return false;
503
504 if (!bGPSValid) return false;
505 bool rv = false;
506
507 // Set max WP name length
508 int maxName = 6;
509 if ((g_maxWPNameLength >= 3) && (g_maxWPNameLength <= 32))
510 maxName = g_maxWPNameLength;
511#if 0
512
513
514 auto& registry = CommDriverRegistry::GetInstance();
515 const std::vector<DriverPtr>& drivers = registry.GetDrivers();
516
517 // Look for configured ports
518 bool have_n0183 = false;
519 bool have_n2000 = false;
520
521// AbstractCommDriver* found = nullptr;
522 for (auto key : m_output_drivers) {
523 for (auto &d : drivers) {
524 if (d->Key() == key) {
525 std::unordered_map<std::string, std::string> attributes =
526 GetAttributes(key);
527 auto protocol_it = attributes.find("protocol");
528 if (protocol_it != attributes.end()) {
529 std::string protocol = protocol_it->second;
530
531 if (protocol == "nmea0183") {
532 have_n0183 = true;
533 } else if (protocol == "nmea2000") {
534 have_n2000 = true;
535 }
536 }
537 }
538 }
539 }
540#endif
541
542 if (m_have_n0183_out) rv |= UpdateAutopilotN0183(*this);
543
544 if (m_have_n2000_out) rv |= UpdateAutopilotN2K(*this);
545
546 // Send active leg info directly to plugins
547
548 ActiveLegDat leg_info;
549 leg_info.Btw = CurrentBrgToActivePoint;
550 leg_info.Dtw = CurrentRngToActivePoint;
551 leg_info.Xte = CurrentXTEToActivePoint;
552 if (XTEDir < 0) {
553 leg_info.Xte = -leg_info.Xte; // Left side of the track -> negative XTE
554 }
555 leg_info.wp_name = pActivePoint->GetName().Truncate(maxName);
556 leg_info.arrival = m_bArrival;
557
558 json_leg_info.Notify(std::make_shared<ActiveLegDat>(leg_info), "");
559
560#if 0
561
562
563 // Send all known Autopilot messages upstream
564
565 // Set max WP name length
566 int maxName = 6;
567 if ((g_maxWPNameLength >= 3) && (g_maxWPNameLength <= 32))
568 maxName = g_maxWPNameLength;
569
570 // Avoid a possible not initiated SOG/COG. APs can be confused if in NAV mode
571 // wo valid GPS
572 double r_Sog(0.0), r_Cog(0.0);
573 if (!std::isnan(gSog)) r_Sog = gSog;
574 if (!std::isnan(gCog)) r_Cog = gCog;
575
576 // Send active leg info directly to plugins
577
578 ActiveLegDat leg_info;
579 leg_info.Btw = CurrentBrgToActivePoint;
580 leg_info.Dtw = CurrentRngToActivePoint;
581 leg_info.Xte = CurrentXTEToActivePoint;
582 if (XTEDir < 0) {
583 leg_info.Xte = -leg_info.Xte; // Left side of the track -> negative XTE
584 }
585 leg_info.wp_name = pActivePoint->GetName().Truncate(maxName);
586 leg_info.arrival = m_bArrival;
587
588 json_leg_info.Notify(std::make_shared<ActiveLegDat>(leg_info), "");
589
590 // RMB
591 {
592 m_NMEA0183.TalkerID = "EC";
593 SENTENCE snt;
594 m_NMEA0183.Rmb.IsDataValid = bGPSValid ? NTrue : NFalse;
595 m_NMEA0183.Rmb.CrossTrackError = CurrentXTEToActivePoint;
596 m_NMEA0183.Rmb.DirectionToSteer = XTEDir < 0 ? Left : Right;
597 m_NMEA0183.Rmb.RangeToDestinationNauticalMiles = CurrentRngToActivePoint;
598 m_NMEA0183.Rmb.BearingToDestinationDegreesTrue = CurrentBrgToActivePoint;
599
600 if (pActivePoint->m_lat < 0.)
601 m_NMEA0183.Rmb.DestinationPosition.Latitude.Set(-pActivePoint->m_lat,
602 "S");
603 else
604 m_NMEA0183.Rmb.DestinationPosition.Latitude.Set(pActivePoint->m_lat, "N");
605
606 if (pActivePoint->m_lon < 0.)
607 m_NMEA0183.Rmb.DestinationPosition.Longitude.Set(-pActivePoint->m_lon,
608 "W");
609 else
610 m_NMEA0183.Rmb.DestinationPosition.Longitude.Set(pActivePoint->m_lon,
611 "E");
612
613 m_NMEA0183.Rmb.DestinationClosingVelocityKnots =
614 r_Sog * cos((r_Cog - CurrentBrgToActivePoint) * PI / 180.0);
615 m_NMEA0183.Rmb.IsArrivalCircleEntered = m_bArrival ? NTrue : NFalse;
616 m_NMEA0183.Rmb.FAAModeIndicator = bGPSValid ? "A" : "N";
617 // RMB is close to NMEA0183 length limit
618 // Restrict WP names further if necessary
619 int wp_len = maxName;
620 do {
621 m_NMEA0183.Rmb.To = pActivePoint->GetName().Truncate(wp_len);
622 m_NMEA0183.Rmb.From =
623 pActiveRouteSegmentBeginPoint->GetName().Truncate(wp_len);
624 m_NMEA0183.Rmb.Write(snt);
625 wp_len -= 1;
626 } while (snt.Sentence.size() > 82 && wp_len > 0);
627
628 BroadcastNMEA0183Message(snt.Sentence, *m_nmea_log, on_message_sent);
629 }
630
631 // RMC
632 {
633 m_NMEA0183.TalkerID = _T("EC");
634
635 SENTENCE snt;
636 m_NMEA0183.Rmc.IsDataValid = NTrue;
637 if (!bGPSValid) m_NMEA0183.Rmc.IsDataValid = NFalse;
638
639 if (gLat < 0.)
640 m_NMEA0183.Rmc.Position.Latitude.Set(-gLat, _T("S"));
641 else
642 m_NMEA0183.Rmc.Position.Latitude.Set(gLat, _T("N"));
643
644 if (gLon < 0.)
645 m_NMEA0183.Rmc.Position.Longitude.Set(-gLon, _T("W"));
646 else
647 m_NMEA0183.Rmc.Position.Longitude.Set(gLon, _T("E"));
648
649 m_NMEA0183.Rmc.SpeedOverGroundKnots = r_Sog;
650 m_NMEA0183.Rmc.TrackMadeGoodDegreesTrue = r_Cog;
651
652 if (!std::isnan(gVar)) {
653 if (gVar < 0.) {
654 m_NMEA0183.Rmc.MagneticVariation = -gVar;
655 m_NMEA0183.Rmc.MagneticVariationDirection = West;
656 } else {
657 m_NMEA0183.Rmc.MagneticVariation = gVar;
658 m_NMEA0183.Rmc.MagneticVariationDirection = East;
659 }
660 } else
661 m_NMEA0183.Rmc.MagneticVariation =
662 361.; // A signal to NMEA converter, gVAR is unknown
663
664 // Send GPS time to autopilot if available else send local system time
665 if (!gRmcTime.IsEmpty() && !gRmcDate.IsEmpty()) {
666 m_NMEA0183.Rmc.UTCTime = gRmcTime;
667 m_NMEA0183.Rmc.Date = gRmcDate;
668 } else {
669 wxDateTime now = wxDateTime::Now();
670 wxDateTime utc = now.ToUTC();
671 wxString time = utc.Format(_T("%H%M%S"));
672 m_NMEA0183.Rmc.UTCTime = time;
673 wxString date = utc.Format(_T("%d%m%y"));
674 m_NMEA0183.Rmc.Date = date;
675 }
676
677 m_NMEA0183.Rmc.FAAModeIndicator = "A";
678 if (!bGPSValid) m_NMEA0183.Rmc.FAAModeIndicator = "N";
679
680 m_NMEA0183.Rmc.Write(snt);
681
682 BroadcastNMEA0183Message(snt.Sentence, *m_nmea_log, on_message_sent);
683 }
684
685 // APB
686 {
687 m_NMEA0183.TalkerID = _T("EC");
688
689 SENTENCE snt;
690
691 m_NMEA0183.Apb.IsLoranBlinkOK =
692 NTrue; // considered as "generic invalid fix" flag
693 if (!bGPSValid) m_NMEA0183.Apb.IsLoranBlinkOK = NFalse;
694
695 m_NMEA0183.Apb.IsLoranCCycleLockOK = NTrue;
696 if (!bGPSValid) m_NMEA0183.Apb.IsLoranCCycleLockOK = NFalse;
697
698 m_NMEA0183.Apb.CrossTrackErrorMagnitude = CurrentXTEToActivePoint;
699
700 if (XTEDir < 0)
701 m_NMEA0183.Apb.DirectionToSteer = Left;
702 else
703 m_NMEA0183.Apb.DirectionToSteer = Right;
704
705 m_NMEA0183.Apb.CrossTrackUnits = _T("N");
706
707 if (m_bArrival)
708 m_NMEA0183.Apb.IsArrivalCircleEntered = NTrue;
709 else
710 m_NMEA0183.Apb.IsArrivalCircleEntered = NFalse;
711
712 // We never pass the perpendicular, since we declare arrival before
713 // reaching this point
714 m_NMEA0183.Apb.IsPerpendicular = NFalse;
715
716 m_NMEA0183.Apb.To = pActivePoint->GetName().Truncate(maxName);
717
718 double brg1, dist1;
719 DistanceBearingMercator(pActivePoint->m_lat, pActivePoint->m_lon,
720 pActiveRouteSegmentBeginPoint->m_lat,
721 pActiveRouteSegmentBeginPoint->m_lon, &brg1,
722 &dist1);
723
724 if (g_bMagneticAPB && !std::isnan(gVar)) {
725 double brg1m =
726 ((brg1 - gVar) >= 0.) ? (brg1 - gVar) : (brg1 - gVar + 360.);
727 double bapm = ((CurrentBrgToActivePoint - gVar) >= 0.)
728 ? (CurrentBrgToActivePoint - gVar)
729 : (CurrentBrgToActivePoint - gVar + 360.);
730
731 m_NMEA0183.Apb.BearingOriginToDestination = brg1m;
732 m_NMEA0183.Apb.BearingOriginToDestinationUnits = _T("M");
733
734 m_NMEA0183.Apb.BearingPresentPositionToDestination = bapm;
735 m_NMEA0183.Apb.BearingPresentPositionToDestinationUnits = _T("M");
736
737 m_NMEA0183.Apb.HeadingToSteer = bapm;
738 m_NMEA0183.Apb.HeadingToSteerUnits = _T("M");
739 } else {
740 m_NMEA0183.Apb.BearingOriginToDestination = brg1;
741 m_NMEA0183.Apb.BearingOriginToDestinationUnits = _T("T");
742
743 m_NMEA0183.Apb.BearingPresentPositionToDestination =
744 CurrentBrgToActivePoint;
745 m_NMEA0183.Apb.BearingPresentPositionToDestinationUnits = _T("T");
746
747 m_NMEA0183.Apb.HeadingToSteer = CurrentBrgToActivePoint;
748 m_NMEA0183.Apb.HeadingToSteerUnits = _T("T");
749 }
750
751 m_NMEA0183.Apb.Write(snt);
752 BroadcastNMEA0183Message(snt.Sentence, *m_nmea_log, on_message_sent);
753 }
754
755 // XTE
756 {
757 m_NMEA0183.TalkerID = _T("EC");
758
759 SENTENCE snt;
760
761 m_NMEA0183.Xte.IsLoranBlinkOK =
762 NTrue; // considered as "generic invalid fix" flag
763 if (!bGPSValid) m_NMEA0183.Xte.IsLoranBlinkOK = NFalse;
764
765 m_NMEA0183.Xte.IsLoranCCycleLockOK = NTrue;
766 if (!bGPSValid) m_NMEA0183.Xte.IsLoranCCycleLockOK = NFalse;
767
768 m_NMEA0183.Xte.CrossTrackErrorDistance = CurrentXTEToActivePoint;
769
770 if (XTEDir < 0)
771 m_NMEA0183.Xte.DirectionToSteer = Left;
772 else
773 m_NMEA0183.Xte.DirectionToSteer = Right;
774
775 m_NMEA0183.Xte.CrossTrackUnits = _T("N");
776
777 m_NMEA0183.Xte.Write(snt);
778 BroadcastNMEA0183Message(snt.Sentence, *m_nmea_log, on_message_sent);
779 }
780#endif
781
782 return true;
783}
784
785bool Routeman::DoesRouteContainSharedPoints(Route *pRoute) {
786 if (pRoute) {
787 // walk the route, looking at each point to see if it is used by another
788 // route or is isolated
789 wxRoutePointListNode *pnode = (pRoute->pRoutePointList)->GetFirst();
790 while (pnode) {
791 RoutePoint *prp = pnode->GetData();
792
793 // check all other routes to see if this point appears in any other route
794 wxArrayPtrVoid *pRA = GetRouteArrayContaining(prp);
795
796 if (pRA) {
797 for (unsigned int ir = 0; ir < pRA->GetCount(); ir++) {
798 Route *pr = (Route *)pRA->Item(ir);
799 if (pr == pRoute)
800 continue; // self
801 else
802 return true;
803 }
804 delete pRA;
805 }
806
807 if (pnode) pnode = pnode->GetNext();
808 }
809
810 // Now walk the route again, looking for isolated type shared waypoints
811 pnode = (pRoute->pRoutePointList)->GetFirst();
812 while (pnode) {
813 RoutePoint *prp = pnode->GetData();
814 if (prp->IsShared()) return true;
815
816 if (pnode) pnode = pnode->GetNext();
817 }
818 }
819
820 return false;
821}
822
823bool Routeman::DeleteTrack(Track *pTrack) {
824 if (pTrack && !pTrack->m_bIsInLayer) {
825 ::wxBeginBusyCursor();
826 /*
827 wxGenericProgressDialog *pprog = nullptr;
828
829 int count = pTrack->GetnPoints();
830 if (count > 10000) {
831 pprog = new wxGenericProgressDialog(
832 _("OpenCPN Track Delete"), _T("0/0"), count, NULL,
833 wxPD_APP_MODAL | wxPD_SMOOTH | wxPD_ELAPSED_TIME |
834 wxPD_ESTIMATED_TIME | wxPD_REMAINING_TIME);
835 pprog->SetSize(400, wxDefaultCoord);
836 pprog->Centre();
837 }
838 */
839
840 // Remove the track from associated lists
841 pSelect->DeleteAllSelectableTrackSegments(pTrack);
842 auto it = std::find(g_TrackList.begin(), g_TrackList.end(), pTrack);
843 if (it != g_TrackList.end()) {
844 g_TrackList.erase(it);
845 }
846 delete pTrack;
847
848 ::wxEndBusyCursor();
849
850 // delete pprog;
851 return true;
852 }
853 return false;
854}
855
857 if (pRoute) {
858 if (pRoute == pAISMOBRoute) {
859 if (!m_route_dlg_ctx.confirm_delete_ais_mob()) {
860 return false;
861 }
862 pAISMOBRoute = 0;
863 }
864 ::wxBeginBusyCursor();
865
866 if (GetpActiveRoute() == pRoute) DeactivateRoute();
867
868 if (pRoute->m_bIsInLayer) {
869 ::wxEndBusyCursor();
870 return false;
871 }
876 m_prop_dlg_ctx.hide(pRoute);
877
878 // if (nav_obj_changes) nav_obj_changes->DeleteConfigRoute(pRoute);
879
880 // Remove the route from associated lists
881 pSelect->DeleteAllSelectableRouteSegments(pRoute);
882 pRouteList->DeleteObject(pRoute);
883
884 m_route_dlg_ctx.route_mgr_dlg_update_list_ctrl();
885
886 // walk the route, tentatively deleting/marking points used only by this
887 // route
888 wxRoutePointListNode *pnode = (pRoute->pRoutePointList)->GetFirst();
889 while (pnode) {
890 RoutePoint *prp = pnode->GetData();
891
892 // check all other routes to see if this point appears in any other route
893 Route *pcontainer_route = FindRouteContainingWaypoint(prp);
894
895 if (pcontainer_route == NULL && prp->m_bIsInRoute) {
896 prp->m_bIsInRoute =
897 false; // Take this point out of this (and only) route
898 if (!prp->IsShared()) {
899 pSelect->DeleteSelectablePoint(prp, SELTYPE_ROUTEPOINT);
900
901 // Remove all instances of this point from the list.
902 wxRoutePointListNode *pdnode = pnode;
903 while (pdnode) {
904 pRoute->pRoutePointList->DeleteNode(pdnode);
905 pdnode = pRoute->pRoutePointList->Find(prp);
906 }
907
908 pnode = NULL;
909 NavObj_dB::GetInstance().DeleteRoutePoint(prp);
910 delete prp;
911 } else {
912 prp->m_bIsolatedMark = true; // This has become an isolated mark
913 prp->SetShared(false); // and is no longer part of a route
914 NavObj_dB::GetInstance().UpdateRoutePoint(prp);
915 }
916 }
917 if (pnode)
918 pnode = pnode->GetNext();
919 else
920 pnode = pRoute->pRoutePointList->GetFirst(); // restart the list
921 }
922
923 NavObj_dB::GetInstance().DeleteRoute(pRoute);
924 delete pRoute;
925
926 ::wxEndBusyCursor();
927 }
928 return true;
929}
930
931void Routeman::DeleteAllRoutes() {
932 ::wxBeginBusyCursor();
933
934 // Iterate on the RouteList
935 wxRouteListNode *node = pRouteList->GetFirst();
936 while (node) {
937 Route *proute = node->GetData();
938 if (proute == pAISMOBRoute) {
939 if (!m_route_dlg_ctx.confirm_delete_ais_mob()) {
940 return;
941 }
942 pAISMOBRoute = 0;
943 ::wxBeginBusyCursor();
944 }
945
946 node = node->GetNext();
947 if (proute->m_bIsInLayer) continue;
948
949 DeleteRoute(proute);
950 }
951
952 ::wxEndBusyCursor();
953}
954
955void Routeman::SetColorScheme(ColorScheme cs, double displayDPmm) {
956 // Re-Create the pens and colors
957
958 int scaled_line_width = g_route_line_width;
959 int track_scaled_line_width = g_track_line_width;
960 if (g_btouch) {
961 // 0.2 mm nominal, but not less than 1 pixel
962 double nominal_line_width_pix = wxMax(1.5, floor(displayDPmm / 5.0));
963
964 double sline_width = wxMax(nominal_line_width_pix, g_route_line_width);
965 sline_width *= g_ChartScaleFactorExp;
966 scaled_line_width = wxMax(sline_width, 2);
967
968 double tsline_width = wxMax(nominal_line_width_pix, g_track_line_width);
969 tsline_width *= g_ChartScaleFactorExp;
970 track_scaled_line_width = wxMax(tsline_width, 2);
971 }
972
973 m_pActiveRoutePointPen = wxThePenList->FindOrCreatePen(
974 wxColour(0, 0, 255), scaled_line_width, wxPENSTYLE_SOLID);
975 m_pRoutePointPen = wxThePenList->FindOrCreatePen(
976 wxColour(0, 0, 255), scaled_line_width, wxPENSTYLE_SOLID);
977
978 // Or in something like S-52 compliance
979
980 m_pRoutePen =
981 wxThePenList->FindOrCreatePen(m_route_dlg_ctx.get_global_colour("UINFB"),
982 scaled_line_width, wxPENSTYLE_SOLID);
983 m_pSelectedRoutePen =
984 wxThePenList->FindOrCreatePen(m_route_dlg_ctx.get_global_colour("UINFO"),
985 scaled_line_width, wxPENSTYLE_SOLID);
986 m_pActiveRoutePen =
987 wxThePenList->FindOrCreatePen(m_route_dlg_ctx.get_global_colour("UARTE"),
988 scaled_line_width, wxPENSTYLE_SOLID);
989 m_pTrackPen =
990 wxThePenList->FindOrCreatePen(m_route_dlg_ctx.get_global_colour("CHMGD"),
991 track_scaled_line_width, wxPENSTYLE_SOLID);
992 m_pRouteBrush = wxTheBrushList->FindOrCreateBrush(
993 m_route_dlg_ctx.get_global_colour("UINFB"), wxBRUSHSTYLE_SOLID);
994 m_pSelectedRouteBrush = wxTheBrushList->FindOrCreateBrush(
995 m_route_dlg_ctx.get_global_colour("UINFO"), wxBRUSHSTYLE_SOLID);
996 m_pActiveRouteBrush = wxTheBrushList->FindOrCreateBrush(
997 m_route_dlg_ctx.get_global_colour("PLRTE"), wxBRUSHSTYLE_SOLID);
998}
999
1000wxString Routeman::GetRouteReverseMessage(void) {
1001 return wxString(
1002 _("Waypoints can be renamed to reflect the new order, the names will be "
1003 "'001', '002' etc.\n\nDo you want to rename the waypoints?"));
1004}
1005
1006wxString Routeman::GetRouteResequenceMessage(void) {
1007 return wxString(
1008 _("Waypoints will be renamed to reflect the natural order, the names "
1009 "will be '001', '002' etc.\n\nDo you want to rename the waypoints?"));
1010}
1011
1012Route *Routeman::FindRouteByGUID(const wxString &guid) {
1013 wxRouteListNode *node1 = pRouteList->GetFirst();
1014 while (node1) {
1015 Route *pRoute = node1->GetData();
1016
1017 if (pRoute->m_GUID == guid) return pRoute;
1018 node1 = node1->GetNext();
1019 }
1020
1021 return NULL;
1022}
1023
1024Track *Routeman::FindTrackByGUID(const wxString &guid) {
1025 for (Track *pTrack : g_TrackList) {
1026 if (pTrack->m_GUID == guid) return pTrack;
1027 }
1028
1029 return NULL;
1030}
1031
1032void Routeman::ZeroCurrentXTEToActivePoint() {
1033 // When zeroing XTE create a "virtual" waypoint at present position
1034 if (pRouteActivatePoint) delete pRouteActivatePoint;
1035 pRouteActivatePoint =
1036 new RoutePoint(gLat, gLon, wxString(_T("")), wxString(_T("")),
1037 wxEmptyString, false); // Current location
1038 pRouteActivatePoint->m_bShowName = false;
1039
1040 pActiveRouteSegmentBeginPoint = pRouteActivatePoint;
1041 m_arrival_min = 1e6;
1042}
1043
1044//--------------------------------------------------------------------------------
1045// WayPointman Implementation
1046//--------------------------------------------------------------------------------
1047
1048WayPointman::WayPointman(GlobalColourFunc color_func)
1049 : m_get_global_colour(color_func) {
1050 m_pWayPointList = new RoutePointList;
1051
1052 pmarkicon_image_list = NULL;
1053
1054 // ocpnStyle::Style *style = g_StyleManager->GetCurrentStyle();
1055 m_pIconArray = new ArrayOfMarkIcon;
1056 m_pLegacyIconArray = NULL;
1057 m_pExtendedIconArray = NULL;
1058
1059 m_cs = (ColorScheme)-1;
1060
1061 m_nGUID = 0;
1062 m_iconListScale = -999.0;
1063 m_iconListHeight = -1;
1064}
1065
1066WayPointman::~WayPointman() {
1067 // Two step here, since the RoutePoint dtor also touches the
1068 // RoutePoint list.
1069 // Copy the master RoutePoint list to a temporary list,
1070 // then clear and delete objects from the temp list
1071
1072 RoutePointList temp_list;
1073
1074 wxRoutePointListNode *node = m_pWayPointList->GetFirst();
1075 while (node) {
1076 RoutePoint *pr = node->GetData();
1077
1078 temp_list.Append(pr);
1079 node = node->GetNext();
1080 }
1081
1082 int a = temp_list.GetCount();
1083
1084 temp_list.DeleteContents(true);
1085 temp_list.Clear();
1086
1087 m_pWayPointList->Clear();
1088 delete m_pWayPointList;
1089
1090 for (unsigned int i = 0; i < m_pIconArray->GetCount(); i++) {
1091 MarkIcon *pmi = (MarkIcon *)m_pIconArray->Item(i);
1092 delete pmi->piconBitmap;
1093 delete pmi;
1094 }
1095
1096 m_pIconArray->Clear();
1097 delete m_pIconArray;
1098
1099 if (pmarkicon_image_list) pmarkicon_image_list->RemoveAll();
1100 delete pmarkicon_image_list;
1101 m_pLegacyIconArray->Clear();
1102 delete m_pLegacyIconArray;
1103 m_pExtendedIconArray->Clear();
1104 delete m_pExtendedIconArray;
1105}
1106
1108 if (!prp) return false;
1109
1110 wxRoutePointListNode *prpnode = m_pWayPointList->Append(prp);
1111 prp->SetManagerListNode(prpnode);
1112
1113 return true;
1114}
1115
1117 if (!prp) return false;
1118
1119 wxRoutePointListNode *prpnode =
1120 (wxRoutePointListNode *)prp->GetManagerListNode();
1121
1122 if (prpnode)
1123 delete prpnode;
1124 else
1125 m_pWayPointList->DeleteObject(prp);
1126
1127 prp->SetManagerListNode(NULL);
1128
1129 return true;
1130}
1131
1132wxImageList *WayPointman::Getpmarkicon_image_list(int nominal_height) {
1133 // Cached version available?
1134 if (pmarkicon_image_list && (nominal_height == m_iconListHeight)) {
1135 return pmarkicon_image_list;
1136 }
1137
1138 // Build an image list large enough
1139 if (NULL != pmarkicon_image_list) {
1140 pmarkicon_image_list->RemoveAll();
1141 delete pmarkicon_image_list;
1142 }
1143 pmarkicon_image_list = new wxImageList(nominal_height, nominal_height);
1144
1145 m_iconListHeight = nominal_height;
1146 m_bitmapSizeForList = nominal_height;
1147
1148 return pmarkicon_image_list;
1149}
1150
1151wxBitmap *WayPointman::CreateDimBitmap(wxBitmap *pBitmap, double factor) {
1152 wxImage img = pBitmap->ConvertToImage();
1153 int sx = img.GetWidth();
1154 int sy = img.GetHeight();
1155
1156 wxImage new_img(img);
1157
1158 for (int i = 0; i < sx; i++) {
1159 for (int j = 0; j < sy; j++) {
1160 if (!img.IsTransparent(i, j)) {
1161 new_img.SetRGB(i, j, (unsigned char)(img.GetRed(i, j) * factor),
1162 (unsigned char)(img.GetGreen(i, j) * factor),
1163 (unsigned char)(img.GetBlue(i, j) * factor));
1164 }
1165 }
1166 }
1167
1168 wxBitmap *pret = new wxBitmap(new_img);
1169
1170 return pret;
1171}
1172
1173wxImage WayPointman::CreateDimImage(wxImage &image, double factor) {
1174 int sx = image.GetWidth();
1175 int sy = image.GetHeight();
1176
1177 wxImage new_img(image);
1178
1179 for (int i = 0; i < sx; i++) {
1180 for (int j = 0; j < sy; j++) {
1181 if (!image.IsTransparent(i, j)) {
1182 new_img.SetRGB(i, j, (unsigned char)(image.GetRed(i, j) * factor),
1183 (unsigned char)(image.GetGreen(i, j) * factor),
1184 (unsigned char)(image.GetBlue(i, j) * factor));
1185 }
1186 }
1187 }
1188
1189 return wxImage(new_img);
1190}
1191
1192bool WayPointman::DoesIconExist(const wxString &icon_key) const {
1193 MarkIcon *pmi;
1194 unsigned int i;
1195
1196 for (i = 0; i < m_pIconArray->GetCount(); i++) {
1197 pmi = (MarkIcon *)m_pIconArray->Item(i);
1198 if (pmi->icon_name.IsSameAs(icon_key)) return true;
1199 }
1200
1201 return false;
1202}
1203
1204wxBitmap *WayPointman::GetIconBitmap(const wxString &icon_key) const {
1205 wxBitmap *pret = NULL;
1206 MarkIcon *pmi = NULL;
1207 unsigned int i;
1208
1209 for (i = 0; i < m_pIconArray->GetCount(); i++) {
1210 pmi = (MarkIcon *)m_pIconArray->Item(i);
1211 if (pmi->icon_name.IsSameAs(icon_key)) break;
1212 }
1213
1214 if (i == m_pIconArray->GetCount()) // key not found
1215 {
1216 // find and return bitmap for "circle"
1217 for (i = 0; i < m_pIconArray->GetCount(); i++) {
1218 pmi = (MarkIcon *)m_pIconArray->Item(i);
1219 // if( pmi->icon_name.IsSameAs( _T("circle") ) )
1220 // break;
1221 }
1222 }
1223
1224 if (i == m_pIconArray->GetCount()) // "circle" not found
1225 pmi = (MarkIcon *)m_pIconArray->Item(0); // use item 0
1226
1227 if (pmi) {
1228 if (pmi->piconBitmap)
1229 pret = pmi->piconBitmap;
1230 else {
1231 if (pmi->iconImage.IsOk()) {
1232 pmi->piconBitmap = new wxBitmap(pmi->iconImage);
1233 pret = pmi->piconBitmap;
1234 }
1235 }
1236 }
1237 return pret;
1238}
1239
1240bool WayPointman::GetIconPrescaled(const wxString &icon_key) const {
1241 MarkIcon *pmi = NULL;
1242 unsigned int i;
1243
1244 for (i = 0; i < m_pIconArray->GetCount(); i++) {
1245 pmi = (MarkIcon *)m_pIconArray->Item(i);
1246 if (pmi->icon_name.IsSameAs(icon_key)) break;
1247 }
1248
1249 if (i == m_pIconArray->GetCount()) // key not found
1250 {
1251 // find and return bitmap for "circle"
1252 for (i = 0; i < m_pIconArray->GetCount(); i++) {
1253 pmi = (MarkIcon *)m_pIconArray->Item(i);
1254 // if( pmi->icon_name.IsSameAs( _T("circle") ) )
1255 // break;
1256 }
1257 }
1258
1259 if (i == m_pIconArray->GetCount()) // "circle" not found
1260 pmi = (MarkIcon *)m_pIconArray->Item(0); // use item 0
1261
1262 if (pmi)
1263 return pmi->preScaled;
1264 else
1265 return false;
1266}
1267
1268wxBitmap WayPointman::GetIconBitmapForList(int index, int height) const {
1269 wxBitmap pret;
1270 MarkIcon *pmi;
1271
1272 if (index >= 0) {
1273 pmi = (MarkIcon *)m_pIconArray->Item(index);
1274 // Scale the icon to "list size" if necessary
1275 if (pmi->iconImage.GetHeight() != height) {
1276 int w = height;
1277 int h = height;
1278 int w0 = pmi->iconImage.GetWidth();
1279 int h0 = pmi->iconImage.GetHeight();
1280
1281 wxImage icon_resized = pmi->iconImage; // make a copy
1282 if (h0 <= h && w0 <= w) {
1283 icon_resized = pmi->iconImage.Resize(
1284 wxSize(w, h), wxPoint(w / 2 - w0 / 2, h / 2 - h0 / 2));
1285 } else {
1286 // rescale in one or two directions to avoid cropping, then resize to
1287 // fit to cell
1288 int h1 = h;
1289 int w1 = w;
1290 if (h0 > h)
1291 w1 = wxRound((double)w0 * ((double)h / (double)h0));
1292
1293 else if (w0 > w)
1294 h1 = wxRound((double)h0 * ((double)w / (double)w0));
1295
1296 icon_resized = pmi->iconImage.Rescale(w1, h1);
1297 icon_resized = pmi->iconImage.Resize(
1298 wxSize(w, h), wxPoint(w / 2 - w1 / 2, h / 2 - h1 / 2));
1299 }
1300
1301 pret = wxBitmap(icon_resized);
1302
1303 } else
1304 pret = wxBitmap(pmi->iconImage);
1305 }
1306
1307 return pret;
1308}
1309
1310wxString *WayPointman::GetIconDescription(int index) const {
1311 wxString *pret = NULL;
1312
1313 if (index >= 0) {
1314 MarkIcon *pmi = (MarkIcon *)m_pIconArray->Item(index);
1315 pret = &pmi->icon_description;
1316 }
1317 return pret;
1318}
1319
1320wxString WayPointman::GetIconDescription(wxString icon_key) const {
1321 MarkIcon *pmi;
1322 unsigned int i;
1323
1324 for (i = 0; i < m_pIconArray->GetCount(); i++) {
1325 pmi = (MarkIcon *)m_pIconArray->Item(i);
1326 if (pmi->icon_name.IsSameAs(icon_key))
1327 return wxString(pmi->icon_description);
1328 }
1329
1330 return wxEmptyString;
1331}
1332
1333wxString *WayPointman::GetIconKey(int index) const {
1334 wxString *pret = NULL;
1335
1336 if ((index >= 0) && ((unsigned int)index < m_pIconArray->GetCount())) {
1337 MarkIcon *pmi = (MarkIcon *)m_pIconArray->Item(index);
1338 pret = &pmi->icon_name;
1339 }
1340 return pret;
1341}
1342
1343int WayPointman::GetIconIndex(const wxBitmap *pbm) const {
1344 unsigned int ret = 0;
1345 MarkIcon *pmi;
1346
1347 wxASSERT(m_pIconArray->GetCount() >= 1);
1348 for (unsigned int i = 0; i < m_pIconArray->GetCount(); i++) {
1349 pmi = (MarkIcon *)m_pIconArray->Item(i);
1350 if (pmi->piconBitmap == pbm) {
1351 ret = i;
1352 break;
1353 }
1354 }
1355
1356 return ret;
1357}
1358
1359int WayPointman::GetIconImageListIndex(const wxBitmap *pbm) const {
1360 MarkIcon *pmi = (MarkIcon *)m_pIconArray->Item(GetIconIndex(pbm));
1361
1362 // Build a "list - sized" image
1363 if (pmarkicon_image_list && !pmi->m_blistImageOK) {
1364 int h0 = pmi->iconImage.GetHeight();
1365 int w0 = pmi->iconImage.GetWidth();
1366 int h = m_bitmapSizeForList;
1367 int w = m_bitmapSizeForList;
1368
1369 wxImage icon_larger = pmi->iconImage; // make a copy
1370 if (h0 <= h && w0 <= w) {
1371 icon_larger = pmi->iconImage.Resize(
1372 wxSize(w, h), wxPoint(w / 2 - w0 / 2, h / 2 - h0 / 2));
1373 } else {
1374 // We want to maintain the aspect ratio of the original image, but need
1375 // the canvas to fit the fixed cell size rescale in one or two directions
1376 // to avoid cropping, then resize to fit to cell (Adds border/croops as
1377 // necessary)
1378 int h1 = h;
1379 int w1 = w;
1380 if (h0 > h)
1381 w1 = wxRound((double)w0 * ((double)h / (double)h0));
1382
1383 else if (w0 > w)
1384 h1 = wxRound((double)h0 * ((double)w / (double)w0));
1385
1386 icon_larger = pmi->iconImage.Rescale(w1, h1).Resize(
1387 wxSize(w, h), wxPoint(w / 2 - w1 / 2, h / 2 - h1 / 2));
1388 }
1389
1390 int index = pmarkicon_image_list->Add(wxBitmap(icon_larger));
1391
1392 // Create and replace "x-ed out" and "fixed visibility" icon,
1393 // Being careful to preserve (some) transparency
1394
1395 icon_larger.ConvertAlphaToMask(128);
1396
1397 unsigned char r, g, b;
1398 icon_larger.GetOrFindMaskColour(&r, &g, &b);
1399 wxColour unused_color(r, g, b);
1400
1401 // X-out
1402 wxBitmap xIcon(icon_larger);
1403
1404 wxBitmap xbmp(w, h, -1);
1405 wxMemoryDC mdc(xbmp);
1406 mdc.SetBackground(wxBrush(unused_color));
1407 mdc.Clear();
1408 mdc.DrawBitmap(xIcon, 0, 0);
1409 int xm = xbmp.GetWidth() / 2;
1410 int ym = xbmp.GetHeight() / 2;
1411 int dp = xm / 2;
1412 int width = wxMax(xm / 10, 2);
1413 wxPen red(m_get_global_colour("URED"), width);
1414 mdc.SetPen(red);
1415 mdc.DrawLine(xm - dp, ym - dp, xm + dp, ym + dp);
1416 mdc.DrawLine(xm - dp, ym + dp, xm + dp, ym - dp);
1417 mdc.SelectObject(wxNullBitmap);
1418
1419 wxMask *pmask = new wxMask(xbmp, unused_color);
1420 xbmp.SetMask(pmask);
1421
1422 pmarkicon_image_list->Add(xbmp);
1423
1424 // fixed Viz
1425 wxBitmap fIcon(icon_larger);
1426
1427 wxBitmap fbmp(w, h, -1);
1428 wxMemoryDC fmdc(fbmp);
1429 fmdc.SetBackground(wxBrush(unused_color));
1430 fmdc.Clear();
1431 fmdc.DrawBitmap(xIcon, 0, 0);
1432 xm = fbmp.GetWidth() / 2;
1433 ym = fbmp.GetHeight() / 2;
1434 dp = xm / 2;
1435 width = wxMax(xm / 10, 2);
1436 wxPen fred(m_get_global_colour("UGREN"), width);
1437 fmdc.SetPen(fred);
1438 fmdc.DrawLine(xm - dp, ym + dp, xm + dp, ym + dp);
1439 fmdc.SelectObject(wxNullBitmap);
1440
1441 wxMask *pfmask = new wxMask(fbmp, unused_color);
1442 fbmp.SetMask(pfmask);
1443
1444 pmarkicon_image_list->Add(fbmp);
1445
1446 pmi->m_blistImageOK = true;
1447 pmi->listIndex = index;
1448 }
1449
1450 return pmi->listIndex;
1451}
1452
1453int WayPointman::GetXIconImageListIndex(const wxBitmap *pbm) const {
1454 return GetIconImageListIndex(pbm) + 1;
1455}
1456
1457int WayPointman::GetFIconImageListIndex(const wxBitmap *pbm) const {
1458 return GetIconImageListIndex(pbm) + 2;
1459}
1460
1461// Create the unique identifier
1462wxString WayPointman::CreateGUID(RoutePoint *pRP) {
1463 return GpxDocument::GetUUID();
1464}
1465
1466RoutePoint *WayPointman::FindRoutePointByGUID(const wxString &guid) {
1467 wxRoutePointListNode *prpnode = m_pWayPointList->GetFirst();
1468 while (prpnode) {
1469 RoutePoint *prp = prpnode->GetData();
1470
1471 if (prp->m_GUID == guid) return (prp);
1472
1473 prpnode = prpnode->GetNext(); // RoutePoint
1474 }
1475
1476 return NULL;
1477}
1478
1479RoutePoint *WayPointman::GetNearbyWaypoint(double lat, double lon,
1480 double radius_meters) {
1481 // Iterate on the RoutePoint list, checking distance
1482
1483 wxRoutePointListNode *node = m_pWayPointList->GetFirst();
1484 while (node) {
1485 RoutePoint *pr = node->GetData();
1486
1487 double a = lat - pr->m_lat;
1488 double b = lon - pr->m_lon;
1489 double l = sqrt((a * a) + (b * b));
1490
1491 if ((l * 60. * 1852.) < radius_meters) return pr;
1492
1493 node = node->GetNext();
1494 }
1495 return NULL;
1496}
1497
1498RoutePoint *WayPointman::GetOtherNearbyWaypoint(double lat, double lon,
1499 double radius_meters,
1500 const wxString &guid) {
1501 // Iterate on the RoutePoint list, checking distance
1502
1503 wxRoutePointListNode *node = m_pWayPointList->GetFirst();
1504 while (node) {
1505 RoutePoint *pr = node->GetData();
1506
1507 double a = lat - pr->m_lat;
1508 double b = lon - pr->m_lon;
1509 double l = sqrt((a * a) + (b * b));
1510
1511 if ((l * 60. * 1852.) < radius_meters)
1512 if (pr->m_GUID != guid) return pr;
1513
1514 node = node->GetNext();
1515 }
1516 return NULL;
1517}
1518
1519bool WayPointman::IsReallyVisible(RoutePoint *pWP) {
1520 if (pWP->m_bIsolatedMark)
1521 return pWP->IsVisible(); // isolated point
1522 else {
1523 wxRouteListNode *node = pRouteList->GetFirst();
1524 while (node) {
1525 Route *proute = node->GetData();
1526 if (proute && proute->pRoutePointList) {
1527 if (proute->pRoutePointList->IndexOf(pWP) != wxNOT_FOUND) {
1528 if (proute->IsVisible()) return true;
1529 }
1530 }
1531 node = node->GetNext();
1532 }
1533 }
1534 if (pWP->IsShared()) // is not visible as part of route, but still exists as
1535 // a waypoint
1536 return pWP->IsVisible(); // so treat as isolated point
1537
1538 return false;
1539}
1540
1541void WayPointman::ClearRoutePointFonts(void) {
1542 // Iterate on the RoutePoint list, clearing Font pointers
1543 // This is typically done globally after a font switch
1544
1545 wxRoutePointListNode *node = m_pWayPointList->GetFirst();
1546 while (node) {
1547 RoutePoint *pr = node->GetData();
1548
1549 pr->m_pMarkFont = NULL;
1550 node = node->GetNext();
1551 }
1552}
1553
1554bool WayPointman::SharedWptsExist() {
1555 wxRoutePointListNode *node = m_pWayPointList->GetFirst();
1556 while (node) {
1557 RoutePoint *prp = node->GetData();
1558 if (prp->IsShared() && (prp->m_bIsInRoute || prp == pAnchorWatchPoint1 ||
1559 prp == pAnchorWatchPoint2))
1560 return true;
1561 node = node->GetNext();
1562 }
1563 return false;
1564}
1565
1566void WayPointman::DeleteAllWaypoints(bool b_delete_used) {
1567 // Iterate on the RoutePoint list, deleting all
1568 wxRoutePointListNode *node = m_pWayPointList->GetFirst();
1569 while (node) {
1570 RoutePoint *prp = node->GetData();
1571 // if argument is false, then only delete non-route waypoints
1572 if (!prp->m_bIsInLayer && (prp->GetIconName() != _T("mob")) &&
1573 ((b_delete_used && prp->IsShared()) ||
1574 ((!prp->m_bIsInRoute) && !(prp == pAnchorWatchPoint1) &&
1575 !(prp == pAnchorWatchPoint2)))) {
1576 DestroyWaypoint(prp);
1577 delete prp;
1578 node = m_pWayPointList->GetFirst();
1579 } else
1580 node = node->GetNext();
1581 }
1582 return;
1583}
1584
1585RoutePoint *WayPointman::FindWaypointByGuid(const std::string &guid) {
1586 wxRoutePointListNode *node = m_pWayPointList->GetFirst();
1587 while (node) {
1588 RoutePoint *rp = node->GetData();
1589 if (guid == rp->m_GUID) return rp;
1590 node = node->GetNext();
1591 }
1592 return 0;
1593}
1594void WayPointman::DestroyWaypoint(RoutePoint *pRp, bool b_update_changeset) {
1595 if (pRp) {
1596 // Get a list of all routes containing this point
1597 // and remove the point from them all
1598 wxArrayPtrVoid *proute_array = g_pRouteMan->GetRouteArrayContaining(pRp);
1599 if (proute_array) {
1600 for (unsigned int ir = 0; ir < proute_array->GetCount(); ir++) {
1601 Route *pr = (Route *)proute_array->Item(ir);
1602
1603 /* FS#348
1604 if ( g_pRouteMan->GetpActiveRoute() == pr ) // Deactivate
1605 any route containing this point g_pRouteMan->DeactivateRoute();
1606 */
1607 pr->RemovePoint(pRp);
1608 }
1609
1610 // Scrub the routes, looking for one-point routes
1611 for (unsigned int ir = 0; ir < proute_array->GetCount(); ir++) {
1612 Route *pr = (Route *)proute_array->Item(ir);
1613 if (pr->GetnPoints() < 2) {
1614 g_pRouteMan->DeleteRoute(pr);
1615 }
1616 }
1617
1618 delete proute_array;
1619 }
1620
1621 // Now it is safe to delete the point
1622 NavObj_dB::GetInstance().DeleteRoutePoint(pRp);
1623
1624 pSelect->DeleteSelectableRoutePoint(pRp);
1625
1626 // The RoutePoint might be currently in use as an anchor watch point
1627 if (pRp == pAnchorWatchPoint1) pAnchorWatchPoint1 = NULL;
1628 if (pRp == pAnchorWatchPoint2) pAnchorWatchPoint2 = NULL;
1629
1630 RemoveRoutePoint(pRp);
1631 }
1632}
const void Notify()
Notify all listeners, no data supplied.
Wrapper for global variable, supports notification events when value changes.
static wxString GetUUID(void)
Return a unique RFC4122 version 4 compliant GUID string.
Represents a waypoint or mark within the navigation system.
Definition route_point.h:70
wxRect CurrentRect_in_DC
Current rectangle occupied by the waypoint in the display.
wxString m_GUID
Globally Unique Identifier for the waypoint.
bool m_bIsolatedMark
Flag indicating if the waypoint is a standalone mark.
bool m_bIsActive
Flag indicating if this waypoint is active for navigation.
bool m_bIsInRoute
Flag indicating if this waypoint is part of a route.
wxFont * m_pMarkFont
Font used for rendering the waypoint name.
bool m_bShowName
Flag indicating if the waypoint name should be shown.
bool m_bBlink
Flag indicating if the waypoint should blink when displayed.
bool m_bIsInLayer
Flag indicating if the waypoint belongs to a layer.
Represents a navigational route in the navigation system.
Definition route.h:98
RoutePointList * pRoutePointList
Ordered list of waypoints (RoutePoints) that make up this route.
Definition route.h:335
bool m_bRtIsActive
Flag indicating whether this route is currently active for navigation.
Definition route.h:207
RoutePoint * m_pRouteActivePoint
Pointer to the currently active waypoint within this route.
Definition route.h:213
wxString m_RouteNameString
User-assigned name for the route.
Definition route.h:246
wxString m_GUID
Globally unique identifier for this route.
Definition route.h:272
bool m_bIsInLayer
Flag indicating whether this route belongs to a layer.
Definition route.h:277
bool ActivateRoutePoint(Route *pA, RoutePoint *pRP)
Activates a specific waypoint within a route for navigation.
Definition routeman.cpp:338
wxArrayPtrVoid * GetRouteArrayContaining(RoutePoint *pWP)
Find all routes that contain the given waypoint.
Definition routeman.cpp:194
bool ActivateNextPoint(Route *pr, bool skipped)
Activates the next waypoint in a route when the current waypoint is reached.
Definition routeman.cpp:413
bool DeleteRoute(Route *pRoute)
Definition routeman.cpp:856
bool ActivateRoute(Route *pRouteToActivate, RoutePoint *pStartPoint=NULL)
Activates a route for navigation.
Definition routeman.cpp:279
EventVar json_msg
Notified with message targeting all plugins.
Definition routeman.h:260
EventVar json_leg_info
Notified with a shared_ptr<ActiveLegDat>, leg info to all plugins.
Definition routeman.h:263
EventVar on_message_sent
Notified when a message available as GetString() is sent to garmin.
Definition routeman.h:266
Represents a track, which is a series of connected track points.
Definition track.h:111
int GetXIconImageListIndex(const wxBitmap *pbm) const
index of "X-ed out" icon in the image list
int GetFIconImageListIndex(const wxBitmap *pbm) const
index of "fixed viz" icon in the image list
bool AddRoutePoint(RoutePoint *prp)
Add a point to list which owns it.
bool RemoveRoutePoint(RoutePoint *prp)
Remove a routepoint from list if present, deallocate it all cases.
The JSON value class implementation.
Definition jsonval.h:84
NMEA0183 serial driver.
Driver registration container, a singleton.
Class NavObj_dB.
std::vector< DriverHandle > GetActiveDrivers()
Comm port plugin TX support methods
const std::unordered_map< std::string, std::string > GetAttributes(DriverHandle handle)
Query a specific driver for attributes.
Callbacks for RoutePropDlg.
Definition routeman.h:84
Routeman callbacks.
Definition routeman.h:96