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
56extern bool g_bShowShipToActive;
57extern bool g_bAdvanceRouteWaypointOnArrivalOnly;
58
59extern MyFrame *gFrame;
60
61extern ConsoleCanvas *console;
62
63extern std::vector<Track *> g_TrackList;
64extern ActiveTrack *g_pActiveTrack;
65extern TrackPropDlg *pTrackPropDialog;
66extern RouteManagerDialog *pRouteManagerDialog;
67extern MyConfig *pConfig;
68
69static bool ConfirmDeleteAisMob() {
70 int r = OCPNMessageBox(NULL,
71 _("You are trying to delete an active AIS MOB "
72 "route, are you REALLY sure?"),
73 _("OpenCPN Warning"), wxYES_NO);
74
75 return r == wxID_YES;
76}
77
78RoutemanDlgCtx RoutemanGui::GetDlgCtx() {
80 ctx.confirm_delete_ais_mob = []() { return ConfirmDeleteAisMob(); };
81 ctx.get_global_colour = [](wxString c) { return GetGlobalColor(c); };
82 ctx.show_with_fresh_fonts = [] {
83 if (console) console->ShowWithFreshFonts();
84 };
85 ctx.clear_console_background = []() {
86 console->pCDI->ClearBackground();
87 console->Show(false);
88 };
89 ctx.route_mgr_dlg_update_list_ctrl = []() {
90 if (pRouteManagerDialog && pRouteManagerDialog->IsShown())
91 pRouteManagerDialog->UpdateRouteListCtrl();
92 };
93 return ctx;
94}
95
96bool RoutemanGui::UpdateProgress() {
97 bool bret_val = false;
98
99 if (m_routeman.pActiveRoute) {
100 // Update bearing, range, and crosstrack error
101
102 // Bearing is calculated as Mercator Sailing, i.e. a cartographic
103 // "bearing"
104 double north, east;
105 toSM(m_routeman.pActivePoint->m_lat, m_routeman.pActivePoint->m_lon, gLat,
106 gLon, &east, &north);
107 double a = atan(north / east);
108 if (fabs(m_routeman.pActivePoint->m_lon - gLon) < 180.) {
109 if (m_routeman.pActivePoint->m_lon > gLon)
110 m_routeman.CurrentBrgToActivePoint = 90. - (a * 180 / PI);
111 else
112 m_routeman.CurrentBrgToActivePoint = 270. - (a * 180 / PI);
113 } else {
114 if (m_routeman.pActivePoint->m_lon > gLon)
115 m_routeman.CurrentBrgToActivePoint = 270. - (a * 180 / PI);
116 else
117 m_routeman.CurrentBrgToActivePoint = 90. - (a * 180 / PI);
118 }
119
120 // Calculate range using Great Circle Formula
121
122 double d5 = DistGreatCircle(gLat, gLon, m_routeman.pActivePoint->m_lat,
123 m_routeman.pActivePoint->m_lon);
124 m_routeman.CurrentRngToActivePoint = d5;
125
126 // Get the XTE vector, normal to current segment
127 vector2D va, vb, vn;
128
129 double brg1, dist1, brg2, dist2;
130 DistanceBearingMercator(
131 m_routeman.pActivePoint->m_lat, m_routeman.pActivePoint->m_lon,
132 m_routeman.pActiveRouteSegmentBeginPoint->m_lat,
133 m_routeman.pActiveRouteSegmentBeginPoint->m_lon, &brg1, &dist1);
134 vb.x = dist1 * sin(brg1 * PI / 180.);
135 vb.y = dist1 * cos(brg1 * PI / 180.);
136
137 DistanceBearingMercator(m_routeman.pActivePoint->m_lat,
138 m_routeman.pActivePoint->m_lon, gLat, gLon, &brg2,
139 &dist2);
140 va.x = dist2 * sin(brg2 * PI / 180.);
141 va.y = dist2 * cos(brg2 * PI / 180.);
142
143 double sdelta = vGetLengthOfNormal(&va, &vb, &vn); // NM
144 m_routeman.CurrentXTEToActivePoint = sdelta;
145
146 // Calculate the distance to the arrival line, which is perpendicular to
147 // the current route segment Taking advantage of the calculated normal
148 // from current position to route segment vn
149 vector2D vToArriveNormal;
150 vSubtractVectors(&va, &vn, &vToArriveNormal);
151
152 m_routeman.CurrentRangeToActiveNormalCrossing =
153 vVectorMagnitude(&vToArriveNormal);
154
155 // Compute current segment course
156 // Using simple Mercater projection
157 double x1, y1, x2, y2;
158 toSM(m_routeman.pActiveRouteSegmentBeginPoint->m_lat,
159 m_routeman.pActiveRouteSegmentBeginPoint->m_lon,
160 m_routeman.pActiveRouteSegmentBeginPoint->m_lat,
161 m_routeman.pActiveRouteSegmentBeginPoint->m_lon, &x1, &y1);
162
163 toSM(m_routeman.pActivePoint->m_lat, m_routeman.pActivePoint->m_lon,
164 m_routeman.pActiveRouteSegmentBeginPoint->m_lat,
165 m_routeman.pActiveRouteSegmentBeginPoint->m_lon, &x2, &y2);
166
167 double e1 = atan2((x2 - x1), (y2 - y1));
168 m_routeman.CurrentSegmentCourse = e1 * 180 / PI;
169 if (m_routeman.CurrentSegmentCourse < 0)
170 m_routeman.CurrentSegmentCourse += 360;
171
172 // Compute XTE direction
173 double h = atan(vn.y / vn.x);
174 if (vn.x > 0)
175 m_routeman.CourseToRouteSegment = 90. - (h * 180 / PI);
176 else
177 m_routeman.CourseToRouteSegment = 270. - (h * 180 / PI);
178
179 h = m_routeman.CurrentBrgToActivePoint - m_routeman.CourseToRouteSegment;
180 if (h < 0) h = h + 360;
181
182 if (h > 180)
183 m_routeman.XTEDir = 1;
184 else
185 m_routeman.XTEDir = -1;
186
187 // Allow DirectShipToActivePoint line (distance XTE in mm is > 3 (arbitrary)
188 // or when active point is the first
189 if (g_bShowShipToActive) {
190 if (m_routeman.pActiveRoute->GetIndexOf(m_routeman.pActivePoint) == 1)
191 g_bAllowShipToActive = true;
192 else {
193 // compute XTE in pixels
194 double tlat, tlon;
195 wxPoint r, r1;
196 ll_gc_ll(gLat, gLon, m_routeman.CourseToRouteSegment,
197 (m_routeman.CurrentXTEToActivePoint / 1.852), &tlat, &tlon);
198 gFrame->GetFocusCanvas()->GetCanvasPointPix(gLat, gLon, &r1);
199 gFrame->GetFocusCanvas()->GetCanvasPointPix(tlat, tlon, &r);
200 double xtepix =
201 sqrt(pow((double)(r1.x - r.x), 2) + pow((double)(r1.y - r.y), 2));
202 // xte in mm
203 double xtemm = xtepix / gFrame->GetFocusCanvas()->GetPixPerMM();
204 // allow display (or not)
205 g_bAllowShipToActive = (xtemm > 3.0) ? true : false;
206 }
207 }
208
209 // Determine Arrival
210
211 bool bDidArrival = false;
212
213 // Special signal: if ArrivalRadius < 0, NEVER arrive...
214 // Used for MOB auto-created routes.
215 if (m_routeman.pActivePoint->GetWaypointArrivalRadius() > 0) {
216 if (m_routeman.CurrentRangeToActiveNormalCrossing <=
217 m_routeman.pActivePoint->GetWaypointArrivalRadius()) {
218 m_routeman.m_bArrival = true;
219 m_routeman.UpdateAutopilot();
220
221 bDidArrival = true;
222 DoAdvance();
223
224 } else {
225 // Test to see if we are moving away from the arrival point, and
226 // have been moving away for 2 seconds.
227 // If so, we should declare "Arrival"
228 if ((m_routeman.CurrentRangeToActiveNormalCrossing -
229 m_routeman.m_arrival_min) >
230 m_routeman.pActivePoint->GetWaypointArrivalRadius()) {
231 if (++m_routeman.m_arrival_test > 2 &&
232 !g_bAdvanceRouteWaypointOnArrivalOnly) {
233 m_routeman.m_bArrival = true;
234 m_routeman.UpdateAutopilot();
235
236 bDidArrival = true;
237 DoAdvance();
238 }
239 } else
240 m_routeman.m_arrival_test = 0;
241 }
242 }
243 if (!bDidArrival)
244 m_routeman.m_arrival_min =
245 wxMin(m_routeman.m_arrival_min,
246 m_routeman.CurrentRangeToActiveNormalCrossing);
247 // Only once on arrival
248 if (!bDidArrival) m_routeman.UpdateAutopilot();
249 bret_val = true; // a route is active
250 }
251 m_routeman.m_bDataValid = true;
252 return bret_val;
253}
254
255void RoutemanGui::DeleteTrack(Track *pTrack) {
256 if (pTrack) {
257 if (pTrack->m_bIsInLayer) return;
258
259 ::wxBeginBusyCursor();
260
261 wxGenericProgressDialog *pprog = nullptr;
262
263 int count = pTrack->GetnPoints();
264 if (count > 10000) {
265 pprog = new wxGenericProgressDialog(
266 _("OpenCPN Track Delete"), _T("0/0"), count, NULL,
267 wxPD_APP_MODAL | wxPD_SMOOTH | wxPD_ELAPSED_TIME |
268 wxPD_ESTIMATED_TIME | wxPD_REMAINING_TIME);
269 pprog->SetSize(400, wxDefaultCoord);
270 pprog->Centre();
271 }
272 if (TrackPropDlg::getInstanceFlag() && pTrackPropDialog &&
273 (pTrackPropDialog->IsShown()) &&
274 (pTrack == pTrackPropDialog->GetTrack())) {
275 pTrackPropDialog->Hide();
276 }
277
278 if ((pTrack == g_pActiveTrack) && pTrack->IsRunning()) {
279 pTrack = gFrame->TrackOff();
280 }
281 // Remove the track from associated lists
282 pSelect->DeleteAllSelectableTrackSegments(pTrack);
283 auto it = std::find(g_TrackList.begin(), g_TrackList.end(), pTrack);
284 if (it != g_TrackList.end()) {
285 g_TrackList.erase(it);
286 }
287 delete pTrack;
288
289 ::wxEndBusyCursor();
290
291 delete pprog;
292 }
293}
294
295void RoutemanGui::DeleteAllTracks() {
296 gFrame->TrackOff();
297
298 ::wxBeginBusyCursor();
299
300 // Iterate on the RouteList, we delete from g_TrackList in DeleteTrack,
301 // bigger refactoring is viable, but for now, we simply make a copy
302 // that goes out of scope soon.
303 std::vector<Track *> to_del = g_TrackList;
304 for (Track *ptrack : to_del) {
305 if (ptrack->m_bIsInLayer) continue;
306
307 g_pAIS->DeletePersistentTrack(ptrack);
308 NavObjectChanges::getInstance()->m_bSkipChangeSetUpdate = true;
309 NavObjectChanges::getInstance()->DeleteConfigTrack(ptrack);
310 DeleteTrack(ptrack);
311 NavObjectChanges::getInstance()->m_bSkipChangeSetUpdate = false;
312 }
313
314 if (pConfig && pConfig->IsChangesFileDirty()) {
315 pConfig->UpdateNavObj(true);
316 }
317
318 ::wxEndBusyCursor();
319}
320
321void RoutemanGui::DoAdvance(void) {
322 if (!m_routeman.ActivateNextPoint(m_routeman.pActiveRoute,
323 false)) // at the end?
324 {
325 Route *pthis_route = m_routeman.pActiveRoute;
326 m_routeman.DeactivateRoute(true); // this is an arrival
327
328 if (pthis_route->m_bDeleteOnArrival && !pthis_route->m_bIsBeingEdited) {
329 NavObjectChanges::getInstance()->DeleteConfigRoute(pthis_route);
330 m_routeman.DeleteRoute(pthis_route, NavObjectChanges::getInstance());
331 }
332
333 if (pRouteManagerDialog) pRouteManagerDialog->UpdateRouteListCtrl();
334 }
335}
Represents an active track that is currently being recorded.
Definition track.h:203
Primary navigation console display for route and vessel tracking.
Definition concanv.h:127
void ShowWithFreshFonts(void)
Recomputes and applies new fonts to console elements.
Definition concanv.cpp:440
Main application frame.
Definition ocpn_frame.h:136
Definition route.h:75
bool ActivateNextPoint(Route *pr, bool skipped)
Definition routeman.cpp:365
bool DeleteRoute(Route *pRoute, NavObjectChanges *nav_obj_changes)
Definition routeman.cpp:747
Class TrackPropDlg.
Represents a track, which is a series of connected track points.
Definition track.h:78
Routeman callbacks.
Definition routeman.h:95