aboutsummaryrefslogtreecommitdiff
path: root/libs/map/track.cpp
blob: 8fc71380d715cb1a044d8e47df236e6b35fbe06c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include "map/track.hpp"

#include "map/bookmark_helpers.hpp"
#include "map/user_mark_id_storage.hpp"

#include "geometry/mercator.hpp"
#include "geometry/parametrized_segment.hpp"

#include <algorithm>

Track::Track(kml::TrackData && data)
  : Base(data.m_id == kml::kInvalidTrackId ? UserMarkIdStorage::Instance().GetNextTrackId() : data.m_id)
  , m_data(std::move(data))
{
  m_data.m_id = GetId();
  CHECK(m_data.m_geometry.IsValid(), ());
}

void Track::CacheDataForInteraction() const
{
  m_interactionData = InteractionData();
  m_interactionData->m_lengths = GetLengthsImpl();
  m_interactionData->m_limitRect = GetLimitRectImpl();
}

std::vector<Track::Lengths> Track::GetLengthsImpl() const
{
  double distance = 0;
  std::vector<Lengths> lengths;
  for (auto const & line : m_data.m_geometry.m_lines)
  {
    ASSERT(!line.empty(), ());
    Lengths lineLengths;
    lineLengths.emplace_back(distance);
    for (size_t j = 1; j < line.size(); ++j)
    {
      auto const & pt1 = line[j - 1].GetPoint();
      auto const & pt2 = line[j].GetPoint();
      distance += mercator::DistanceOnEarth(pt1, pt2);
      lineLengths.emplace_back(distance);
    }
    lengths.emplace_back(std::move(lineLengths));
  }
  return lengths;
}

m2::RectD Track::GetLimitRectImpl() const
{
  m2::RectD limitRect;
  for (auto const & line : m_data.m_geometry.m_lines)
    for (auto const & pt : line)
      limitRect.Add(pt.GetPoint());
  return limitRect;
}

bool Track::HasAltitudes() const
{
  bool hasNonDefaultAltitude = false;

  for (auto const & line : m_data.m_geometry.m_lines)
  {
    for (auto const & pt : line)
    {
      if (pt.GetAltitude() == geometry::kInvalidAltitude)
        return false;
      if (!hasNonDefaultAltitude && pt.GetAltitude() != geometry::kDefaultAltitudeMeters)
        hasNonDefaultAltitude = true;
    }
  }

  return hasNonDefaultAltitude;
}

std::string Track::GetName() const
{
  return GetPreferredBookmarkStr(m_data.m_name);
}

void Track::SetName(std::string const & name)
{
  m_isDirty = true;
  kml::SetDefaultStr(m_data.m_name, name);
}

std::string Track::GetDescription() const
{
  return GetPreferredBookmarkStr(m_data.m_description);
}

void Track::SetDescription(std::string const & description)
{
  m_isDirty = true;
  kml::SetDefaultStr(m_data.m_description, description);
}

void Track::SetData(kml::TrackData const & data)
{
  m_isDirty = true;
  m_data = data;

  m_elevationInfo.reset();
  m_interactionData.reset();
}

m2::RectD Track::GetLimitRect() const
{
  if (m_interactionData)
    return m_interactionData->m_limitRect;
  return GetLimitRectImpl();
}

double Track::GetLengthMeters() const
{
  if (!m_interactionData)
    CacheDataForInteraction();
  auto const & lengths = m_interactionData->m_lengths;
  return lengths.empty() ? 0 : lengths.back().back();
}

double Track::GetLengthMetersImpl(size_t lineIndex, size_t ptIndex) const
{
  if (!m_interactionData)
    CacheDataForInteraction();
  auto const & lineLengths = m_interactionData->m_lengths[lineIndex];
  return lineLengths[ptIndex];
}

void Track::UpdateSelectionInfo(m2::PointD const & tapPoint, TrackSelectionInfo & info) const
{
  // Caller sets info.m_squareDist to the max allowed squared distance (mercator). Any segment
  // whose closest point is strictly closer replaces the current best.
  if (m_interactionData && m_interactionData->m_limitRect.SquaredDistance(tapPoint) >= info.m_squareDist)
    return;

  for (size_t lineIndex = 0; lineIndex < m_data.m_geometry.m_lines.size(); ++lineIndex)
  {
    auto const & line = m_data.m_geometry.m_lines[lineIndex];
    for (size_t ptIndex = 0; ptIndex + 1 < line.size(); ++ptIndex)
    {
      m2::ParametrizedSegment<m2::PointD> seg(line[ptIndex].GetPoint(), line[ptIndex + 1].GetPoint());
      auto const closestPoint = seg.ClosestPointTo(tapPoint);
      auto const squaredDist = closestPoint.SquaredLength(tapPoint);
      if (squaredDist >= info.m_squareDist)
        continue;

      info.m_title = GetName();
      info.m_color = GetColor(0);
      info.m_squareDist = squaredDist;
      info.m_trackId = m_data.m_id;
      info.m_trackPoint = closestPoint;

      auto const segDistInMeters = mercator::DistanceOnEarth(line[ptIndex].GetPoint(), closestPoint);
      info.m_distFromBegM = segDistInMeters + GetLengthMetersImpl(lineIndex, ptIndex);
    }
  }
}

df::DepthLayer Track::GetDepthLayer() const
{
  return df::DepthLayer::UserLineLayer;
}

size_t Track::GetLayerCount() const
{
  return m_data.m_layers.size();
}

dp::Color Track::GetColor(size_t layerIndex) const
{
  CHECK_LESS(layerIndex, m_data.m_layers.size(), ());
  return dp::Color(m_data.m_layers[layerIndex].m_color.m_rgba);
}

void Track::SetColor(dp::Color color)
{
  m_isDirty = true;
  m_data.m_layers[0].m_color.m_rgba = color.GetRGBA();
}

float Track::GetWidth(size_t layerIndex) const
{
  CHECK_LESS(layerIndex, m_data.m_layers.size(), ());
  return static_cast<float>(m_data.m_layers[layerIndex].m_lineWidth);
}

bool Track::IsVisible() const
{
  return m_data.m_visible;
}

void Track::SetVisibility(bool visible)
{
  // Visibility doesn't affect geometry, so (unlike SetData) the elevation/interaction caches
  // stay valid and are intentionally preserved across a hide/show toggle.
  m_isDirty = true;
  m_data.m_visible = visible;
}

float Track::GetDepth(size_t layerIndex) const
{
  return layerIndex * 10;
}

void Track::ForEachGeometry(GeometryFnT && fn) const
{
  for (auto const & line : m_data.m_geometry.m_lines)
  {
    std::vector<m2::PointD> points;
    points.reserve(line.size());
    for (auto const & pt : line)
      points.push_back(pt.GetPoint());

    fn(std::move(points));
  }
}

void Track::Attach(kml::MarkGroupId groupId)
{
  ASSERT_EQUAL(m_groupID, kml::kInvalidMarkGroupId, ());
  m_isDirty = true;
  m_groupID = groupId;
}

void Track::Detach()
{
  m_isDirty = true;
  m_groupID = kml::kInvalidMarkGroupId;
}

m2::PointD Track::GetPoint(double distanceInMeters) const
{
  if (!m_interactionData)
    CacheDataForInteraction();

  auto const & lengths = m_interactionData->m_lengths;
  ASSERT(!lengths.empty(), ());

  for (size_t lineIndex = 0; lineIndex < lengths.size(); ++lineIndex)
  {
    auto const & lineLengths = lengths[lineIndex];
    ASSERT(!lineLengths.empty(), ());

    if (distanceInMeters > lineLengths.back())
      continue;

    if (distanceInMeters <= lineLengths.front())
      return m_data.m_geometry.m_lines[lineIndex].front().GetPoint();

    auto const it = std::upper_bound(lineLengths.begin(), lineLengths.end(), distanceInMeters);
    if (it == lineLengths.end())
      return m_data.m_geometry.m_lines[lineIndex].back().GetPoint();

    ASSERT(it != lineLengths.begin(), ());
    size_t const ptIdx = std::distance(lineLengths.begin(), it) - 1;

    auto const & line = m_data.m_geometry.m_lines[lineIndex];
    auto const segLen = lineLengths[ptIdx + 1] - lineLengths[ptIdx];
    if (segLen < 1e-9)
      return line[ptIdx].GetPoint();

    double const f = (distanceInMeters - lineLengths[ptIdx]) / segLen;
    auto const & p1 = line[ptIdx].GetPoint();
    auto const & p2 = line[ptIdx + 1].GetPoint();
    return p1 + (p2 - p1) * f;
  }

  // Distance beyond last line — return last point.
  return m_data.m_geometry.m_lines.back().back().GetPoint();
}

kml::MultiGeometry::LineT Track::GetGeometry() const
{
  kml::MultiGeometry::LineT geometry;
  for (auto const & line : m_data.m_geometry.m_lines)
    for (size_t i = 0; i < line.size(); ++i)
      geometry.emplace_back(line[i]);
  return geometry;
}

TrackStatistics Track::GetStatistics() const
{
  TrackStatistics ts;
  ts.m_length = GetLengthMeters();
  ts.CalculateDuration(m_data.m_geometry);

  if (auto const * ei = GetElevationInfo())
  {
    // Relation tracks from MWM have cleaner altitude data than raw GPS tracks.
    auto const threshold =
        (m_data.m_id == kml::kTempRelationTrackId) ? ElevationInfo::kDefThresholdMWM : ElevationInfo::kDefThresholdGPS;
    auto const altInfo = ei->CalculateAltitudesInfo(threshold);
    ts.m_ascent = altInfo.GetTotalAscent();
    ts.m_descent = altInfo.GetTotalDescent();
    ts.m_minElevation = altInfo.m_minAltitude;
    ts.m_maxElevation = altInfo.m_maxAltitude;
  }
  return ts;
}

ElevationInfo const * Track::GetElevationInfo() const
{
  if (!HasAltitudes())
    return nullptr;
  if (!m_elevationInfo)
  {
    m_elevationInfo = ElevationInfo(GetData().m_geometry.m_lines);

    // Relation tracks from MWM have cleaner altitude data than raw GPS tracks.
    if (m_data.m_id != kml::kTempRelationTrackId)
      m_elevationInfo->SmoothSlopeOutliers();
    m_elevationInfo->Simplify();
  }
  return &(*m_elevationInfo);
}

double Track::GetDurationInSeconds() const
{
  TrackStatistics ts;
  ts.CalculateDuration(m_data.m_geometry);
  return ts.m_duration;
}