OpenCPN Partial API docs
Loading...
Searching...
No Matches
routeman_gui.cpp
1
2/***************************************************************************
3 *
4 * Project: OpenCPN
5 * Purpose: implement routeman_gui.h: Routeman drawing stuff
6 * Author: David Register, Alec Leamas
7 *
8 ***************************************************************************
9 * Copyright (C) 2022 by David Register, Alec Leamas *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
20 * *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
25 ******************A********************************************************/
26
27// For compilers that support precompilation, includes "wx.h".
28#include <wx/wxprec.h>
29
30#ifndef WX_PRECOMP
31#include <wx/wx.h>
32#endif // precompiled headers
33
34#include <wx/utils.h>
35#include <wx/gdicmn.h>
36
37#include "model/georef.h"
38#include "model/nav_object_database.h"
39#include "model/own_ship.h"
40#include "model/route.h"
41#include "model/route_point.h"
42#include "model/select.h"
43#include "model/track.h"
44
45#include "chcanv.h"
46#include "concanv.h"
47#include "model/ais_decoder.h"
48#include "navutil.h"
49#include "ocpn_app.h"
50#include "ocpn_frame.h"
51#include "routemanagerdialog.h"
52#include "routeman_gui.h"
53#include "TrackPropDlg.h"
54#include "vector2D.h"
55#include "model/navobj_db.h"
56
57extern bool g_bShowShipToActive;
58extern bool g_bAdvanceRouteWaypointOnArrivalOnly;
59
60extern MyFrame *gFrame;
61
62extern APConsole *console;
63
64extern std::vector<Track *> g_TrackList;
65extern ActiveTrack *g_pActiveTrack;
66extern TrackPropDlg *pTrackPropDialog;
67extern RouteManagerDialog *pRouteManagerDialog;
68extern MyConfig *pConfig;
69extern bool g_bhide_route_console;
70
71static bool ConfirmDeleteAisMob() {
72 int r = OCPNMessageBox(NULL,
73 _("You are trying to delete an active AIS MOB "
74 "route, are you REALLY sure?"),
75 _("OpenCPN Warning"), wxYES_NO);
76
77 return r == wxID_YES;
78}
79
80RoutemanDlgCtx RoutemanGui::GetDlgCtx() {
82 ctx.confirm_delete_ais_mob = []() { return ConfirmDeleteAisMob(); };
83 ctx.get_global_colour = [](wxString c) { return GetGlobalColor(c); };
84 ctx.show_with_fresh_fonts = [] {
85 if (console && !g_bhide_route_console) console->ShowWithFreshFonts();
86 };
87 ctx.clear_console_background = []() {
88 console->GetCDI()->ClearBackground();
89 console->Show(false);
90 };
91 ctx.route_mgr_dlg_update_list_ctrl = []() {
92 if (pRouteManagerDialog && pRouteManagerDialog->IsShown())
93 pRouteManagerDialog->UpdateRouteListCtrl();
94 };
95 return ctx;
96}
97
98bool RoutemanGui::UpdateProgress() {
99 bool bret_val = false;
100
101 if (m_routeman.pActiveRoute) {
102 // Update bearing, range, and crosstrack error
103
104 // Bearing is calculated as Mercator Sailing, i.e. a cartographic
105 // "bearing"
106 double north, east;
107 toSM(m_routeman.pActivePoint->m_lat, m_routeman.pActivePoint->m_lon, gLat,
108 gLon, &east, &north);
109 double a = atan(north / east);
110 if (fabs(m_routeman.pActivePoint->m_lon - gLon) < 180.) {
111 if (m_routeman.pActivePoint->m_lon > gLon)
112 m_routeman.CurrentBrgToActivePoint = 90. - (a * 180 / PI);
113 else
114 m_routeman.CurrentBrgToActivePoint = 270. - (a * 180 / PI);
115 } else {
116 if (m_routeman.pActivePoint->m_lon > gLon)
117 m_routeman.CurrentBrgToActivePoint = 270. - (a * 180 / PI);
118 else
119 m_routeman.CurrentBrgToActivePoint = 90. - (a * 180 / PI);
120 }
121
122 // Calculate range using Great Circle Formula
123
124 double d5 = DistGreatCircle(gLat, gLon, m_routeman.pActivePoint->m_lat,
125 m_routeman.pActivePoint->m_lon);
126 m_routeman.CurrentRngToActivePoint = d5;
127
128 // Get the XTE vector, normal to current segment
129 vector2D va, vb, vn;
130
131 double brg1, dist1, brg2, dist2;
132 DistanceBearingMercator(
133 m_routeman.pActivePoint->m_lat, m_routeman.pActivePoint->m_lon,
134 m_routeman.pActiveRouteSegmentBeginPoint->m_lat,
135 m_routeman.pActiveRouteSegmentBeginPoint->m_lon, &brg1, &dist1);
136 vb.x = dist1 * sin(brg1 * PI / 180.);
137 vb.y = dist1 * cos(brg1 * PI / 180.);
138
139 DistanceBearingMercator(m_routeman.pActivePoint->m_lat,
140 m_routeman.pActivePoint->m_lon, gLat, gLon, &brg2,
141 &dist2);
142 va.x = dist2 * sin(brg2 * PI / 180.);
143 va.y = dist2 * cos(brg2 * PI / 180.);
144
145 double sdelta = vGetLengthOfNormal(&va, &vb, &vn); // NM
146 m_routeman.CurrentXTEToActivePoint = sdelta;
147
148 // Calculate the distance to the arrival line, which is perpendicular to
149 // the current route segment Taking advantage of the calculated normal
150 // from current position to route segment vn
151 vector2D vToArriveNormal;
152 vSubtractVectors(&va, &vn, &vToArriveNormal);
153
154 m_routeman.CurrentRangeToActiveNormalCrossing =
155 vVectorMagnitude(&vToArriveNormal);
156
157 // Compute current segment course
158 // Using simple Mercater projection
159 double x1, y1, x2, y2;
160 toSM(m_routeman.pActiveRouteSegmentBeginPoint->m_lat,
161 m_routeman.pActiveRouteSegmentBeginPoint->m_lon,
162 m_routeman.pActiveRouteSegmentBeginPoint->m_lat,
163 m_routeman.pActiveRouteSegmentBeginPoint->m_lon, &x1, &y1);
164
165 toSM(m_routeman.pActivePoint->m_lat, m_routeman.pActivePoint->m_lon,
166 m_routeman.pActiveRouteSegmentBeginPoint->m_lat,
167 m_routeman.pActiveRouteSegmentBeginPoint->m_lon, &x2, &y2);
168
169 double e1 = atan2((x2 - x1), (y2 - y1));
170 m_routeman.CurrentSegmentCourse = e1 * 180 / PI;
171 if (m_routeman.CurrentSegmentCourse < 0)
172 m_routeman.CurrentSegmentCourse += 360;
173
174 // Compute XTE direction
175 double h = atan(vn.y / vn.x);
176 if (vn.x > 0)
177 m_routeman.CourseToRouteSegment = 90. - (h * 180 / PI);
178 else
179 m_routeman.CourseToRouteSegment = 270. - (h * 180 / PI);
180
181 h = m_routeman.CurrentBrgToActivePoint - m_routeman.CourseToRouteSegment;
182 if (h < 0) h = h + 360;
183
184 if (h > 180)
185 m_routeman.XTEDir = 1;
186 else
187 m_routeman.XTEDir = -1;
188
189 // Allow DirectShipToActivePoint line (distance XTE in mm is > 3 (arbitrary)
190 // or when active point is the first
191 if (g_bShowShipToActive) {
192 if (m_routeman.pActiveRoute->GetIndexOf(m_routeman.pActivePoint) == 1)
193 g_bAllowShipToActive = true;
194 else {
195 // compute XTE in pixels
196 double tlat, tlon;
197 wxPoint r, r1;
198 ll_gc_ll(gLat, gLon, m_routeman.CourseToRouteSegment,
199 (m_routeman.CurrentXTEToActivePoint / 1.852), &tlat, &tlon);
200 gFrame->GetFocusCanvas()->GetCanvasPointPix(gLat, gLon, &r1);
201 gFrame->GetFocusCanvas()->GetCanvasPointPix(tlat, tlon, &r);
202 double xtepix =
203 sqrt(pow((double)(r1.x - r.x), 2) + pow((double)(r1.y - r.y), 2));
204 // xte in mm
205 double xtemm = xtepix / gFrame->GetFocusCanvas()->GetPixPerMM();
206 // allow display (or not)
207 g_bAllowShipToActive = (xtemm > 3.0) ? true : false;
208 }
209 }
210
211 // Determine Arrival
212
213 bool bDidArrival = false;
214
215 // Duplicate points can result in NaN for normal crossing range.
216 if (isnan(m_routeman.CurrentRangeToActiveNormalCrossing)) {
217 m_routeman.CurrentRangeToActiveNormalCrossing =
218 m_routeman.CurrentRngToActivePoint;
219 }
220
221 // Special signal: if ArrivalRadius < 0, NEVER arrive...
222 // Used for MOB auto-created routes.
223 if (m_routeman.pActivePoint->GetWaypointArrivalRadius() > 0) {
224 if (m_routeman.CurrentRangeToActiveNormalCrossing <=
225 m_routeman.pActivePoint->GetWaypointArrivalRadius()) {
226 m_routeman.m_bArrival = true;
227 m_routeman.UpdateAutopilot();
228
229 bDidArrival = true;
230 DoAdvance();
231
232 } else {
233 // Test to see if we are moving away from the arrival point, and
234 // have been moving away for 2 seconds.
235 // If so, we should declare "Arrival"
236 if ((m_routeman.CurrentRangeToActiveNormalCrossing -
237 m_routeman.m_arrival_min) >
238 m_routeman.pActivePoint->GetWaypointArrivalRadius()) {
239 if (++m_routeman.m_arrival_test > 2 &&
240 !g_bAdvanceRouteWaypointOnArrivalOnly) {
241 m_routeman.m_bArrival = true;
242 m_routeman.UpdateAutopilot();
243
244 bDidArrival = true;
245 DoAdvance();
246 }
247 } else
248 m_routeman.m_arrival_test = 0;
249 }
250 }
251 if (!bDidArrival)
252 m_routeman.m_arrival_min =
253 wxMin(m_routeman.m_arrival_min,
254 m_routeman.CurrentRangeToActiveNormalCrossing);
255 // Only once on arrival
256 if (!bDidArrival) m_routeman.UpdateAutopilot();
257 bret_val = true; // a route is active
258 }
259 m_routeman.m_bDataValid = true;
260 return bret_val;
261}
262
263void RoutemanGui::DeleteTrack(Track *pTrack) {
264 if (pTrack) {
265 if (pTrack->m_bIsInLayer) return;
266
267 ::wxBeginBusyCursor();
268
269 wxGenericProgressDialog *pprog = nullptr;
270
271 int count = pTrack->GetnPoints();
272 if (count > 10000) {
273 pprog = new wxGenericProgressDialog(
274 _("OpenCPN Track Delete"), _T("0/0"), count, NULL,
275 wxPD_APP_MODAL | wxPD_SMOOTH | wxPD_ELAPSED_TIME |
276 wxPD_ESTIMATED_TIME | wxPD_REMAINING_TIME);
277 pprog->SetSize(400, wxDefaultCoord);
278 pprog->Centre();
279 }
280 if (TrackPropDlg::getInstanceFlag() && pTrackPropDialog &&
281 (pTrackPropDialog->IsShown()) &&
282 (pTrack == pTrackPropDialog->GetTrack())) {
283 pTrackPropDialog->Hide();
284 }
285
286 if ((pTrack == g_pActiveTrack) && pTrack->IsRunning()) {
287 pTrack = gFrame->TrackOff();
288 }
289 // Remove the track from associated lists
290 pSelect->DeleteAllSelectableTrackSegments(pTrack);
291 auto it = std::find(g_TrackList.begin(), g_TrackList.end(), pTrack);
292 if (it != g_TrackList.end()) {
293 g_TrackList.erase(it);
294 }
295 delete pTrack;
296
297 ::wxEndBusyCursor();
298
299 delete pprog;
300 }
301}
302
303void RoutemanGui::DeleteAllTracks() {
304 gFrame->TrackOff();
305
306 ::wxBeginBusyCursor();
307
308 // Iterate on the RouteList, we delete from g_TrackList in DeleteTrack,
309 // bigger refactoring is viable, but for now, we simply make a copy
310 // that goes out of scope soon.
311 std::vector<Track *> to_del = g_TrackList;
312 for (Track *ptrack : to_del) {
313 if (ptrack->m_bIsInLayer) continue;
314
315 g_pAIS->DeletePersistentTrack(ptrack);
316 NavObj_dB::GetInstance().DeleteTrack(ptrack);
317 DeleteTrack(ptrack);
318 }
319 ::wxEndBusyCursor();
320}
321
322void RoutemanGui::DoAdvance(void) {
323 if (!m_routeman.ActivateNextPoint(m_routeman.pActiveRoute,
324 false)) // at the end?
325 {
326 Route *pthis_route = m_routeman.pActiveRoute;
327 m_routeman.DeactivateRoute(true); // this is an arrival
328
329 if (pthis_route->m_bDeleteOnArrival && !pthis_route->m_bIsBeingEdited) {
330 m_routeman.DeleteRoute(pthis_route);
331 }
332
333 if (pRouteManagerDialog) pRouteManagerDialog->UpdateRouteListCtrl();
334 }
335}
Class AisDecoder and helpers.
Represents an active track that is currently being recorded.
Definition track.h:221
Main application frame.
Definition ocpn_frame.h:136
Represents a navigational route in the navigation system.
Definition route.h:98
bool m_bDeleteOnArrival
Flag indicating whether the route should be deleted once navigation reaches the end.
Definition route.h:267
bool m_bIsBeingEdited
Flag indicating that the route is currently being edited by the user.
Definition route.h:223
bool ActivateNextPoint(Route *pr, bool skipped)
Activates the next waypoint in a route when the current waypoint is reached.
Definition routeman.cpp:419
bool DeleteRoute(Route *pRoute)
Definition routeman.cpp:834
Class TrackPropDlg.
Represents a track, which is a series of connected track points.
Definition track.h:111
Class NavObj_dB.
Routeman callbacks.
Definition routeman.h:96