79 #include <wx/colour.h>
80 #include <wx/datetime.h>
82 #include <wx/jsonval.h>
84 #include <wx/progdlg.h>
85 #include <wx/string.h>
88 #include "model/track.h"
90 #include "model/config_vars.h"
91 #include "model/georef.h"
92 #include "model/json_event.h"
93 #include "model/nav_object_database.h"
94 #include "model/navutil_base.h"
95 #include "model/own_ship.h"
96 #include "model/routeman.h"
97 #include "model/select.h"
99 std::vector<Track*> g_TrackList;
101 #if defined(__UNIX__) && \
106 void Reset() { clock_gettime(CLOCK_REALTIME, &tp); }
110 clock_gettime(CLOCK_REALTIME, &tp_end);
111 return (tp_end.tv_sec - tp.tv_sec) * 1.e3 +
112 (tp_end.tv_nsec - tp.tv_nsec) / 1.e6;
120 TrackPoint::TrackPoint(
double lat,
double lon, wxString ts)
121 : m_lat(lat), m_lon(lon), m_GPXTrkSegNo(1) {
125 TrackPoint::TrackPoint(
double lat,
double lon, wxDateTime dt)
126 : m_lat(lat), m_lon(lon), m_GPXTrkSegNo(1) {
132 : m_lat(orig->m_lat),
135 SetCreateTime(orig->GetCreateTime());
138 TrackPoint::~TrackPoint() { }
140 wxDateTime TrackPoint::GetCreateTime() {
141 wxDateTime CreateTimeX;
142 ParseGPXDateTime(CreateTimeX, wxString(m_stimestring.c_str()));
146 void TrackPoint::SetCreateTime(wxDateTime dt) {
149 ts = dt.FormatISODate()
151 .Append(dt.FormatISOTime())
157 void TrackPoint::SetCreateTime(wxString ts) {
159 m_stimestring = ts.mb_str();
168 double _distance2(vector2D &a, vector2D &b) {
169 return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
171 double _distance(vector2D &a, vector2D &b) {
return sqrt(_distance2(a, b)); }
178 m_width = WIDTH_UNDEFINED;
179 m_style = wxPENSTYLE_INVALID;
181 m_GUID = pWayPointMan->CreateGUID(NULL);
182 m_bIsInLayer =
false;
185 m_HyperlinkList =
new HyperlinkList;
186 m_HighlightedTrackPoint = -1;
189 Track::~Track(
void) {
190 for (
size_t i = 0; i < TrackPoints.size(); i++)
delete TrackPoints[i];
192 delete m_HyperlinkList;
195 #define TIMER_TRACK1 778
198 EVT_TIMER(TIMER_TRACK1, ActiveTrack::OnTimerTrack)
202 m_TimerTrack.SetOwner(
this, TIMER_TRACK1);
206 SetPrecision(g_nTrackPrecision);
208 m_prev_time = wxInvalidDateTime;
209 m_lastStoredTP = NULL;
211 wxDateTime now = wxDateTime::Now();
213 trackPointState = firstPoint;
214 m_lastStoredTP = NULL;
218 m_CurrentTrackSeg = 0;
222 ActiveTrack::~ActiveTrack() { Stop(); }
224 void ActiveTrack::SetPrecision(
int prec) {
226 switch (m_nPrecision) {
228 m_allowedMaxAngle = 10;
229 m_allowedMaxXTE = 0.008;
231 m_minTrackpoint_delta = .004;
235 m_allowedMaxAngle = 10;
236 m_allowedMaxXTE = 0.004;
238 m_minTrackpoint_delta = .002;
242 m_allowedMaxAngle = 10;
243 m_allowedMaxXTE = 0.0015;
245 m_minTrackpoint_delta = .001;
251 void ActiveTrack::Start(
void) {
254 m_TimerTrack.Start(1000, wxTIMER_CONTINUOUS);
259 void ActiveTrack::Stop(
bool do_add_point) {
266 delta = DistGreatCircle(gLat, gLon, m_lastStoredTP->m_lat,
267 m_lastStoredTP->m_lon);
269 if (delta > m_minTrackpoint_delta) AddPointNow(
true);
278 Track *ActiveTrack::DoExtendDaily() {
279 Track *pExtendTrack = NULL;
283 if (!pLastPoint->GetCreateTime().IsValid())
286 for (
Track* ptrack : g_TrackList) {
287 if (!ptrack->m_bIsInLayer && ptrack->m_GUID != m_GUID) {
288 TrackPoint *track_node = ptrack->GetLastPoint();
289 if (!track_node->GetCreateTime().IsValid())
291 if (track_node->GetCreateTime() <= pLastPoint->GetCreateTime()) {
293 track_node->GetCreateTime() > pExtendPoint->GetCreateTime()) {
294 pExtendPoint = track_node;
295 pExtendTrack = ptrack;
300 if (pExtendTrack && pExtendTrack->GetPoint(0)
302 .FromTimezone(wxDateTime::GMT0)
303 .IsSameDate(pLastPoint->GetCreateTime().FromTimezone(
304 wxDateTime::GMT0))) {
306 if (pLastPoint->GetCreateTime() == pExtendPoint->GetCreateTime()) begin = 2;
307 pSelect->DeleteAllSelectableTrackSegments(pExtendTrack);
308 wxString suffix = _T(
"");
309 if (GetName().IsNull()) {
310 suffix = pExtendTrack->GetName();
311 if (suffix.IsNull()) suffix = wxDateTime::Today().FormatISODate();
313 pExtendTrack->Clone(
this, begin, GetnPoints(), suffix);
314 pSelect->AddAllSelectableTrackSegments(pExtendTrack);
315 pSelect->DeleteAllSelectableTrackSegments(
this);
319 if (GetName().IsNull()) SetName(wxDateTime::Today().FormatISODate());
324 void Track::Clone(
Track *psourcetrack,
int start_nPoint,
int end_nPoint,
325 const wxString &suffix) {
326 if (psourcetrack->m_bIsInLayer)
return;
328 m_TrackNameString = psourcetrack->m_TrackNameString + suffix;
329 m_TrackStartString = psourcetrack->m_TrackStartString;
330 m_TrackEndString = psourcetrack->m_TrackEndString;
332 bool b_splitting = GetnPoints() == 0;
336 startTrkSegNo = psourcetrack->GetPoint(start_nPoint)->m_GPXTrkSegNo;
338 startTrkSegNo = GetLastPoint()->m_GPXTrkSegNo;
341 for (i = start_nPoint; i <= end_nPoint; i++) {
342 TrackPoint *psourcepoint = psourcetrack->GetPoint(i);
346 AddPoint(ptargetpoint);
351 void ActiveTrack::AdjustCurrentTrackPoint(
TrackPoint *prototype) {
353 *m_lastStoredTP = *prototype;
354 m_prev_time = prototype->GetCreateTime().FromUTC();
358 void ActiveTrack::OnTimerTrack(wxTimerEvent &event) {
363 m_prev_dist = DistGreatCircle(gLat, gLon, m_lastStoredTP->m_lat,
364 m_lastStoredTP->m_lon);
368 bool b_addpoint =
false;
370 if ((m_TrackTimerSec > 0.) && ((
double)m_track_run >= m_TrackTimerSec) &&
371 (m_prev_dist > m_minTrackpoint_delta)) {
379 if ((trackPointState == firstPoint) && !g_bTrackDaily) {
380 wxDateTime now = wxDateTime::Now();
381 if (TrackPoints.empty()) TrackPoints.front()->SetCreateTime(now.ToUTC());
384 m_TimerTrack.Start(1000, wxTIMER_CONTINUOUS);
387 void ActiveTrack::AddPointNow(
bool do_add_point) {
388 wxDateTime now = wxDateTime::Now();
390 if (m_prev_dist < 0.0005)
391 if (!do_add_point)
return;
393 if (m_prev_time.IsValid())
394 if (m_prev_time == now)
395 if (!do_add_point)
return;
397 vector2D gpsPoint(gLon, gLat);
404 if (g_trackFilterMax){
405 if (trackPointState == potentialPoint)
407 double distToLastGpsPoint = DistLoxodrome(m_lastStoredTP->m_lat, m_lastStoredTP->m_lon, gLat, gLon);
408 if (distToLastGpsPoint > g_trackFilterMax)
return;
416 switch (trackPointState) {
418 TrackPoint *pTrackPoint = AddNewPoint(gpsPoint, now.ToUTC());
419 m_lastStoredTP = pTrackPoint;
420 trackPointState = secondPoint;
421 do_add_point =
false;
425 vector2D pPoint(gLon, gLat);
426 skipPoints.push_back(pPoint);
427 skipTimes.push_back(now.ToUTC());
428 trackPointState = potentialPoint;
431 case potentialPoint: {
432 if (gpsPoint == skipPoints[skipPoints.size() - 1])
break;
434 unsigned int xteMaxIndex = 0;
439 for (
unsigned int i = 0; i < skipPoints.size(); i++) {
440 double xte = GetXTE(m_lastStoredTP->m_lat, m_lastStoredTP->m_lon, gLat,
441 gLon, skipPoints[i].lat, skipPoints[i].lon);
447 if (xteMax > m_allowedMaxXTE) {
449 AddNewPoint(skipPoints[xteMaxIndex], skipTimes[xteMaxIndex]);
450 pSelect->AddSelectableTrackSegment(
451 m_lastStoredTP->m_lat, m_lastStoredTP->m_lon, pTrackPoint->m_lat,
452 pTrackPoint->m_lon, m_lastStoredTP, pTrackPoint,
this);
454 m_prevFixedTP = m_fixedTP;
455 m_fixedTP = m_removeTP;
456 m_removeTP = m_lastStoredTP;
457 m_lastStoredTP = pTrackPoint;
458 for (
unsigned int i = 0; i <= xteMaxIndex; i++) {
459 skipPoints.pop_front();
460 skipTimes.pop_front();
467 if (GetnPoints() > 2) {
469 DistGreatCircle(m_fixedTP->m_lat, m_fixedTP->m_lon,
470 m_lastStoredTP->m_lat, m_lastStoredTP->m_lon);
471 double xte = GetXTE(m_fixedTP, m_lastStoredTP, m_removeTP);
472 if (xte < m_allowedMaxXTE / wxMax(1.0, 2.0 - dist * 2.0)) {
473 TrackPoints.pop_back();
474 TrackPoints.pop_back();
475 TrackPoints.push_back(m_lastStoredTP);
476 pSelect->DeletePointSelectableTrackSegments(m_removeTP);
477 pSelect->AddSelectableTrackSegment(
478 m_fixedTP->m_lat, m_fixedTP->m_lon, m_lastStoredTP->m_lat,
479 m_lastStoredTP->m_lon, m_fixedTP, m_lastStoredTP,
this);
481 m_removeTP = m_fixedTP;
482 m_fixedTP = m_prevFixedTP;
487 skipPoints.push_back(gpsPoint);
488 skipTimes.push_back(now.ToUTC());
495 TrackPoint *pTrackPoint = AddNewPoint(gpsPoint, now.ToUTC());
496 pSelect->AddSelectableTrackSegment(
497 m_lastStoredTP->m_lat, m_lastStoredTP->m_lon, pTrackPoint->m_lat,
498 pTrackPoint->m_lon, m_lastStoredTP, pTrackPoint,
this);
505 void Track::ClearHighlights() { m_HighlightedTrackPoint = -1; }
508 TrackPoint *Track::GetPoint(
int nWhichPoint) {
509 if (nWhichPoint < (
int)TrackPoints.size())
510 return TrackPoints[nWhichPoint];
516 if (TrackPoints.empty())
return NULL;
518 return TrackPoints.back();
521 static double heading_diff(
double x) {
522 if (x > 180)
return 360 - x;
523 if (x < -180)
return -360 + x;
531 double Track::ComputeScale(
int left,
int right) {
532 const double z = WGS84_semimajor_axis_meters * mercator_k0;
533 const double mult = DEGREE * z;
538 double lata = TrackPoints[left]->m_lat, lona = TrackPoints[left]->m_lon;
539 double latb = TrackPoints[right]->m_lat, lonb = TrackPoints[right]->m_lon;
541 double bx = heading_diff(lonb - lona), by = latb - lata;
543 double lengthSquared = bx * bx + by * by;
547 if (lengthSquared > 3)
return INFINITY;
549 if (lengthSquared == 0.0) {
550 for (
int i = left + 1; i < right; i++) {
551 double lat = TrackPoints[i]->m_lat, lon = TrackPoints[i]->m_lon;
553 double vx = heading_diff(lon - lona);
554 double vy = lat - lata;
555 double dist = vx * vx + vy * vy;
557 if (dist > max_dist) max_dist = dist;
560 double invLengthSquared = 1 / lengthSquared;
561 for (
int i = left + 1; i < right; i++) {
562 double lat = TrackPoints[i]->m_lat, lon = TrackPoints[i]->m_lon;
564 double vx = heading_diff(lon - lona);
565 double vy = lat - lata;
566 double t = (vx * bx + vy * by) * invLengthSquared;
570 dist = vx * vx + vy * vy;
572 double wx = heading_diff(lona - lon);
573 double wy = lata - lat;
574 dist = wx * wx + wy * wy;
576 double projx = vx - t * bx;
577 double projy = vy - t * by;
578 dist = projx * projx + projy * projy;
581 if (dist > max_dist) max_dist = dist;
585 return max_dist * mult * mult;
592 TrackPoints.push_back(pNewPoint);
597 void Track::Finalize() {
598 if (SubTracks.size())
603 int n = TrackPoints.size() - 1;
606 std::vector<SubTrack> new_level;
609 for (
int i = 0; i < n; i++) {
610 new_level[i].m_box.SetFromSegment(
611 TrackPoints[i]->m_lat, TrackPoints[i]->m_lon,
612 TrackPoints[i + 1]->m_lat, TrackPoints[i + 1]->m_lon);
613 new_level[i].m_scale = 0;
616 for (
int i = 0; i < n; i++) {
618 new_level[i].m_box = SubTracks[level - 1][p].m_box;
619 if (p + 1 < (
int)SubTracks[level - 1].size())
620 new_level[i].m_box.Expand(SubTracks[level - 1][p + 1].m_box);
622 int left = i << level;
623 int right = wxMin(left + (1 << level), TrackPoints.size() - 1);
624 new_level[i].m_scale = ComputeScale(left, right);
627 SubTracks.push_back(new_level);
629 if (n > 1 && n & 1) n++;
638 void Track::InsertSubTracks(LLBBox &box,
int level,
int pos) {
639 if (level == (
int)SubTracks.size()) {
640 std::vector<SubTrack> new_level;
641 if (level > 0) box.Expand(SubTracks[level - 1][0].m_box);
643 new_level[pos].m_box = box;
644 SubTracks.push_back(new_level);
645 }
else if (pos < (
int)SubTracks[level].size())
646 SubTracks[level][pos].m_box.Expand(box);
648 SubTracks[level].push_back(
SubTrack());
649 SubTracks[level][pos].m_box = box;
653 SubTracks[level][pos].m_scale = 0;
655 int left = pos << level;
656 int right = wxMin(left + (1 << level), TrackPoints.size() - 1);
657 SubTracks[level][pos].m_scale = ComputeScale(left, right);
660 if (pos > 0) InsertSubTracks(box, level + 1, pos >> 1);
669 void Track::AddPointFinalized(
TrackPoint *pNewPoint) {
670 TrackPoints.push_back(pNewPoint);
672 int pos = TrackPoints.size() - 1;
676 box.SetFromSegment(TrackPoints[pos - 1]->m_lat, TrackPoints[pos - 1]->m_lon,
677 TrackPoints[pos]->m_lat, TrackPoints[pos]->m_lon);
678 InsertSubTracks(box, 0, pos - 1);
682 TrackPoint *Track::AddNewPoint(vector2D point, wxDateTime time) {
685 AddPointFinalized(tPoint);
687 NavObjectChanges::getInstance()->AddNewTrackPoint(
692 v[
"lat"] = tPoint->m_lat;
693 v[
"lon"] = tPoint->m_lon;
694 v[
"Track_ID"] = m_GUID;
695 std::string msg_id(
"OCPN_TRK_POINT_ADDED");
696 JsonEvent::getInstance().Notify(msg_id, std::make_shared<wxJSONValue>(v));
701 void Track::DouglasPeuckerReducer(std::vector<TrackPoint *> &list,
702 std::vector<bool> &keeplist,
int from,
int to,
704 keeplist[from] =
true;
707 int maxdistIndex = -1;
710 for (
int i = from + 1; i < to; i++) {
711 double dist = 1852.0 * GetXTE(list[from], list[to], list[i]);
713 if (dist > maxdist) {
719 if (maxdist > delta) {
720 DouglasPeuckerReducer(list, keeplist, from, maxdistIndex, delta);
721 DouglasPeuckerReducer(list, keeplist, maxdistIndex, to, delta);
725 double Track::Length() {
728 for (
size_t i = 0; i < TrackPoints.size(); i++) {
731 const double offsetLat = 1e-6;
732 const double deltaLat = l->m_lat - t->m_lat;
733 if (fabs(deltaLat) > offsetLat)
734 total += DistGreatCircle(l->m_lat, l->m_lon, t->m_lat, t->m_lon);
736 total += DistGreatCircle(l->m_lat + copysign(offsetLat, deltaLat),
737 l->m_lon, t->m_lat, t->m_lon);
745 int Track::Simplify(
double maxDelta) {
748 std::vector<TrackPoint *> pointlist;
749 std::vector<bool> keeplist;
751 ::wxBeginBusyCursor();
753 for (
size_t i = 0; i < TrackPoints.size(); i++) {
756 pointlist.push_back(trackpoint);
757 keeplist.push_back(
false);
760 DouglasPeuckerReducer(pointlist, keeplist, 0, pointlist.size() - 1, maxDelta);
762 pSelect->DeleteAllSelectableTrackSegments(
this);
766 for (
size_t i = 0; i < pointlist.size(); i++) {
768 TrackPoints.push_back(pointlist[i]);
776 pSelect->AddAllSelectableTrackSegments(
this);
783 Route *Track::RouteFromTrack(wxGenericProgressDialog *pprog) {
792 wxString icon = _T(
"xmblue");
793 if (g_TrackDeltaDistance >= 0.1) icon = _T(
"diamond");
797 int nPoints = TrackPoints.size();
798 bool isProminent =
true;
799 double delta_dist = 0.;
800 double delta_hdg, xte;
801 double leg_speed = 0.1;
803 leg_speed = g_PlanSpeed;
807 pWP_dst =
new RoutePoint(pWP_src->m_lat, pWP_src->m_lon, icon, _T (
"" ),
809 route->AddPoint(pWP_dst);
811 pWP_dst->m_bShowName =
false;
813 pSelect->AddSelectableRoutePoint(pWP_dst->m_lat, pWP_dst->m_lon, pWP_dst);
817 for (
size_t i = 1; i < TrackPoints.size();) {
820 pWP_dst->m_lat = pWP_prev->m_lat;
821 pWP_dst->m_lon = pWP_prev->m_lon;
828 DistanceBearingMercator(prp->m_lat, prp->m_lon, pWP_prev->m_lat,
829 pWP_prev->m_lon, &delta_hdg, &delta_dist);
831 if ((delta_dist > (leg_speed * 6.0)) && !prp_OK) {
832 int delta_inserts = floor(delta_dist / (leg_speed * 4.0));
833 delta_dist = delta_dist / (delta_inserts + 1);
837 while (delta_inserts--) {
838 ll_gc_ll(pWP_prev->m_lat, pWP_prev->m_lon, delta_hdg, delta_dist, &tlat,
840 pWP_dst =
new RoutePoint(tlat, tlon, icon, _T (
"" ), wxEmptyString);
841 route->AddPoint(pWP_dst);
842 pWP_dst->m_bShowName =
false;
843 pSelect->AddSelectableRoutePoint(pWP_dst->m_lat, pWP_dst->m_lon,
846 pSelect->AddSelectableRouteSegment(pWP_prev->m_lat, pWP_prev->m_lon,
847 pWP_dst->m_lat, pWP_dst->m_lon,
848 pWP_prev, pWP_dst, route);
861 if (delta_dist >= (leg_speed * 4.0)) isProminent =
true;
862 if (!prp_OK) prp_OK = prp;
864 while (prpnodeX < TrackPoints.size()) {
867 xte = GetXTE(pWP_src, prpX, prp);
868 if (isProminent || (xte > g_TrackDeltaDistance)) {
869 pWP_dst =
new RoutePoint(prp_OK->m_lat, prp_OK->m_lon, icon, _T (
"" ),
872 route->AddPoint(pWP_dst);
873 pWP_dst->m_bShowName =
false;
875 pSelect->AddSelectableRoutePoint(pWP_dst->m_lat, pWP_dst->m_lon,
878 pSelect->AddSelectableRouteSegment(pWP_prev->m_lat, pWP_prev->m_lon,
879 pWP_dst->m_lat, pWP_dst->m_lon,
880 pWP_prev, pWP_dst, route);
884 prpnodeX = TrackPoints.size();
888 if (prpnodeX != TrackPoints.size()) prpnodeX--;
889 if (back_ic-- <= 0) {
890 prpnodeX = TrackPoints.size();
898 DistanceBearingMercator(prp->m_lat, prp->m_lon, pWP_prev->m_lat,
899 pWP_prev->m_lon, NULL, &delta_dist);
901 if (!((delta_dist > (g_TrackDeltaDistance)) && !prp_OK)) {
905 int iProg = (i * 100) / nPoints;
906 if (pprog && (iProg > dProg)) {
908 pprog->Update(dProg);
913 if (delta_dist >= g_TrackDeltaDistance) {
915 new RoutePoint(TrackPoints.back()->m_lat, TrackPoints.back()->m_lon,
916 icon, _T (
"" ), wxEmptyString);
917 route->AddPoint(pWP_dst);
919 pWP_dst->m_bShowName =
false;
921 pSelect->AddSelectableRoutePoint(pWP_dst->m_lat, pWP_dst->m_lon, pWP_dst);
923 pSelect->AddSelectableRouteSegment(pWP_prev->m_lat, pWP_prev->m_lon,
924 pWP_dst->m_lat, pWP_dst->m_lon, pWP_prev,
927 route->m_RouteNameString = m_TrackNameString;
928 route->m_RouteStartString = m_TrackStartString;
929 route->m_RouteEndString = m_TrackEndString;
930 route->m_bDeleteOnArrival =
false;
935 double Track::GetXTE(
double fm1Lat,
double fm1Lon,
double fm2Lat,
double fm2Lon,
936 double toLat,
double toLon) {
942 double brg1, dist1, brg2, dist2;
943 DistanceBearingMercator(toLat, toLon, fm1Lat, fm1Lon, &brg1, &dist1);
944 w.x = dist1 * sin(brg1 * PI / 180.);
945 w.y = dist1 * cos(brg1 * PI / 180.);
947 DistanceBearingMercator(toLat, toLon, fm2Lat, fm2Lon, &brg2, &dist2);
948 v.x = dist2 * sin(brg2 * PI / 180.);
949 v.y = dist2 * cos(brg2 * PI / 180.);
954 const double lengthSquared = _distance2(v, w);
955 if (lengthSquared == 0.0) {
957 return _distance(p, v);
967 double t = vDotProduct(&a, &b) / lengthSquared;
970 return _distance(p, v);
972 return _distance(p, w);
973 vector2D projection = v + t * (w - v);
974 return _distance(p, projection);
978 if (!fm1 || !fm2 || !to)
return 0.0;
979 if (fm1 == to)
return 0.0;
980 if (fm2 == to)
return 0.0;
981 return GetXTE(fm1->m_lat, fm1->m_lon, fm2->m_lat, fm2->m_lon, to->m_lat,