From c1398cf2910f216f70fe2b62b7518c1ed344a10f Mon Sep 17 00:00:00 2001 From: Constantin Shalnev Date: Fri, 11 Dec 2015 14:07:27 +0300 Subject: [PATCH] Added GpsTrackFilter --- map/gps_track.cpp | 27 +++++++++++++++------------ map/gps_track.hpp | 19 ++++++++++--------- map/gps_track_filter.cpp | 25 +++++++++++++++++++++++++ map/gps_track_filter.hpp | 12 ++++++++++++ map/map.pro | 2 ++ map/map_tests/gps_track_test.cpp | 7 ++++--- 6 files changed, 68 insertions(+), 24 deletions(-) create mode 100644 map/gps_track_filter.cpp create mode 100644 map/gps_track_filter.hpp diff --git a/map/gps_track.cpp b/map/gps_track.cpp index a81ba2b97b..34460f933e 100644 --- a/map/gps_track.cpp +++ b/map/gps_track.cpp @@ -63,7 +63,7 @@ GpsTrack::~GpsTrack() } } -void GpsTrack::AddPoint(TItem const & point) +void GpsTrack::AddPoint(location::GpsInfo const & point) { { lock_guard lg(m_dataGuard); @@ -72,7 +72,7 @@ void GpsTrack::AddPoint(TItem const & point) ScheduleTask(); } -void GpsTrack::AddPoints(vector const & points) +void GpsTrack::AddPoints(vector const & points) { { lock_guard lg(m_dataGuard); @@ -174,10 +174,10 @@ void GpsTrack::InitCollection(hours duration) try { - m_storage->ForEach([this](TItem const & info)->bool + m_storage->ForEach([this](location::GpsTrackInfo const & point)->bool { pair evictedIds; - m_collection->Add(info, evictedIds); + m_collection->Add(point, evictedIds); return true; }); } @@ -191,18 +191,21 @@ void GpsTrack::InitCollection(hours duration) void GpsTrack::ProcessPoints() { - vector points; + vector originPoints; hours duration; bool needClear; // Steal data for processing { lock_guard lg(m_dataGuard); - points.swap(m_points); + originPoints.swap(m_points); duration = m_duration; needClear = m_needClear; m_needClear = false; } + vector points; + m_filter.Process(originPoints, points); + // Create collection only if callback appears if (!m_collection && HasCallback()) InitCollection(duration); @@ -225,7 +228,7 @@ bool GpsTrack::HasCallback() return m_callback != nullptr; } -void GpsTrack::UpdateStorage(bool needClear, vector const & points) +void GpsTrack::UpdateStorage(bool needClear, vector const & points) { InitStorageIfNeed(); if (!m_storage) @@ -245,7 +248,7 @@ void GpsTrack::UpdateStorage(bool needClear, vector const & points) } } -void GpsTrack::UpdateCollection(hours duration, bool needClear, vector const & points, +void GpsTrack::UpdateCollection(hours duration, bool needClear, vector const & points, pair & addedIds, pair & evictedIds) { // Apply Clear, SetDuration and Add points @@ -282,9 +285,9 @@ void GpsTrack::NotifyCallback(pair const & addedIds, pair> toAdd; + vector> toAdd; toAdd.reserve(m_collection->GetSize()); - m_collection->ForEach([&toAdd](TItem const & point, size_t id)->bool + m_collection->ForEach([&toAdd](location::GpsTrackInfo const & point, size_t id)->bool { toAdd.emplace_back(id, point); return true; @@ -297,13 +300,13 @@ void GpsTrack::NotifyCallback(pair const & addedIds, pair> toAdd; + vector> toAdd; if (addedIds.first != kInvalidId) { size_t const addedCount = addedIds.second - addedIds.first + 1; ASSERT_GREATER_OR_EQUAL(m_collection->GetSize(), addedCount, ()); toAdd.reserve(addedCount); - m_collection->ForEach([&toAdd](TItem const & point, size_t id)->bool + m_collection->ForEach([&toAdd](location::GpsTrackInfo const & point, size_t id)->bool { toAdd.emplace_back(id, point); return true; diff --git a/map/gps_track.hpp b/map/gps_track.hpp index cadf8573c2..ae746dc1d0 100644 --- a/map/gps_track.hpp +++ b/map/gps_track.hpp @@ -1,6 +1,7 @@ #pragma once #include "map/gps_track_collection.hpp" +#include "map/gps_track_filter.hpp" #include "map/gps_track_storage.hpp" #include "std/condition_variable.hpp" @@ -13,18 +14,17 @@ class GpsTrack final public: static size_t const kInvalidId; // = numeric_limits::max(); - using TItem = location::GpsTrackInfo; - /// @param filePath - path to the file on disk to persist track + /// @param maxItemCount - number of points to store on disk + /// @param duration - initial value of track duration GpsTrack(string const & filePath, size_t maxItemCount, hours duration); - ~GpsTrack(); /// Adds point or collection of points to gps tracking /// @note Callback is called with 'toAdd' and 'toRemove' points, if some points were added or removed. /// @note Only points with good timestamp will be added, other will be skipped. - void AddPoint(TItem const & point); - void AddPoints(vector const & points); + void AddPoint(location::GpsInfo const & point); + void AddPoints(vector const & points); /// Clears any previous tracking info /// @note Callback is called with 'toRemove' points, if some points were removed. @@ -42,7 +42,7 @@ public: /// @param toAdd - collection of points and ids to add. /// @param toRemove - range of point indices to remove, or pair(kInvalidId,kInvalidId) if nothing to remove /// @note Calling of a GpsTrack.SetCallback function from the callback causes deadlock. - using TGpsTrackDiffCallback = std::function> && toAdd, + using TGpsTrackDiffCallback = std::function> && toAdd, pair const & toRemove)>; /// Sets callback on change of gps track. @@ -60,8 +60,8 @@ private: bool HasCallback(); void InitStorageIfNeed(); void InitCollection(hours duration); - void UpdateStorage(bool needClear, vector const & points); - void UpdateCollection(hours duration, bool needClear, vector const & points, + void UpdateStorage(bool needClear, vector const & points); + void UpdateCollection(hours duration, bool needClear, vector const & points, pair & addedIds, pair & evictedIds); void NotifyCallback(pair const & addedIds, pair const & evictedIds); @@ -69,7 +69,7 @@ private: string const m_filePath; mutable mutex m_dataGuard; // protects data for stealing - vector m_points; // accumulated points to adding + vector m_points; // accumulated points for adding hours m_duration; bool m_needClear; // need clear file @@ -82,6 +82,7 @@ private: unique_ptr m_storage; // used in the worker thread unique_ptr m_collection; // used in the worker thread + GpsTrackFilter m_filter; mutex m_threadGuard; thread m_thread; diff --git a/map/gps_track_filter.cpp b/map/gps_track_filter.cpp new file mode 100644 index 0000000000..0834f864a2 --- /dev/null +++ b/map/gps_track_filter.cpp @@ -0,0 +1,25 @@ +#include "map/gps_track_filter.hpp" + +namespace +{ + +double constexpr kMinHorizontalAccuracyMeters = 30; + +} // namespace + +void GpsTrackFilter::Process(vector const & inPoints, + vector & outPoints) +{ + // Very simple initial implementation of filter. + // Further, it is going to be improved. + + outPoints.reserve(inPoints.size()); + + for (auto const & inPt : inPoints) + { + if (inPt.m_horizontalAccuracy > kMinHorizontalAccuracyMeters) + continue; + + outPoints.emplace_back(inPt); + } +} diff --git a/map/gps_track_filter.hpp b/map/gps_track_filter.hpp new file mode 100644 index 0000000000..c65b5cfcea --- /dev/null +++ b/map/gps_track_filter.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include "platform/location.hpp" + +#include "std/vector.hpp" + +class GpsTrackFilter +{ +public: + void Process(vector const & inPoints, + vector & outPoints); +}; diff --git a/map/map.pro b/map/map.pro index c6f8c44116..21d5a35229 100644 --- a/map/map.pro +++ b/map/map.pro @@ -23,6 +23,7 @@ HEADERS += \ geourl_process.hpp \ gps_track.hpp \ gps_track_collection.hpp \ + gps_track_filter.hpp \ gps_track_storage.hpp \ mwm_url.hpp \ storage_bridge.hpp \ @@ -44,6 +45,7 @@ SOURCES += \ ge0_parser.cpp \ geourl_process.cpp \ gps_track.cpp \ + gps_track_filter.cpp \ gps_track_collection.cpp \ gps_track_storage.cpp \ mwm_url.cpp \ diff --git a/map/map_tests/gps_track_test.cpp b/map/map_tests/gps_track_test.cpp index 2513558e05..7576b45375 100644 --- a/map/map_tests/gps_track_test.cpp +++ b/map/map_tests/gps_track_test.cpp @@ -20,13 +20,14 @@ namespace { -inline location::GpsTrackInfo Make(double timestamp, ms::LatLon const & ll, double speed) +inline location::GpsInfo Make(double timestamp, ms::LatLon const & ll, double speed) { - location::GpsTrackInfo info; + location::GpsInfo info; info.m_timestamp = timestamp; info.m_speed = speed; info.m_latitude = ll.lat; info.m_longitude = ll.lon; + info.m_horizontalAccuracy = 15; return info; } @@ -92,7 +93,7 @@ UNIT_TEST(GpsTrack_Simple) size_t const maxItemCount = 100000; size_t const writeItemCount = 50000; - vector points; + vector points; points.reserve(writeItemCount); for (size_t i = 0; i < writeItemCount; ++i) points.emplace_back(Make(timestamp + i, ms::LatLon(-90 + i, -180 + i), 10 + i));