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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
|
#include "testing/testing.hpp"
#include "map/elevation_info.hpp"
#include "map/track.hpp"
#include "map/track_statistics.hpp"
#include "kml/serdes_gpx.hpp"
#include "geometry/mercator.hpp"
#include "geometry/point_with_altitude.hpp"
#include "platform/platform.hpp"
#include "coding/file_reader.hpp"
#include <vector>
namespace track_tests
{
using namespace geometry;
using namespace location;
double constexpr kEqualPointsEps = 1e-9;
GpsInfo BuildGpsInfo(double latitude, double longitude, double altitude, double timestamp = 0)
{
GpsInfo gpsInfo;
gpsInfo.m_latitude = latitude;
gpsInfo.m_longitude = longitude;
gpsInfo.m_altitude = altitude;
gpsInfo.m_verticalAccuracy = 1.0; // Mark altitude as valid.
gpsInfo.m_timestamp = timestamp;
return gpsInfo;
}
Track MakeTrack(std::initializer_list<std::initializer_list<PointWithAltitude>> const & lines)
{
kml::TrackData data;
data.m_layers.push_back(kml::TrackLayer());
for (auto const & line : lines)
{
data.m_geometry.m_lines.emplace_back(line.begin(), line.end());
data.m_geometry.m_timestamps.emplace_back();
}
return Track(std::move(data));
}
// ===================== ElevationInfo tests =====================
UNIT_TEST(ElevationInfo_EmptyMultiGeometry)
{
ElevationInfo ei;
auto const & lines = ei.GetLines();
TEST_EQUAL(0, lines.size(), ());
}
UNIT_TEST(ElevationInfo_FromMultiGeometry)
{
using PWA = PointWithAltitude;
kml::MultiGeometry geometry;
PWA const point1({0.0, 0.0}, 100);
PWA const point2({1.0, 1.0}, 150);
PWA const point3({2.0, 2.0}, 50);
geometry.AddLine({point1, point2, point3});
ElevationInfo ei(geometry.m_lines);
auto const & lines = ei.GetLines();
TEST_EQUAL(1, lines.size(), ());
auto const & line = lines[0];
TEST_EQUAL(3, line.size(), ());
// Each line starts from distance 0.
TEST_EQUAL(line[0].m_distance, 0, ());
TEST_EQUAL(line[0].m_altitude, 100, ());
double d1 = mercator::DistanceOnEarth(point1, point2);
TEST_EQUAL(line[1].m_distance, d1, ());
TEST_EQUAL(line[1].m_altitude, 150, ());
double d2 = d1 + mercator::DistanceOnEarth(point2, point3);
TEST_EQUAL(line[2].m_distance, d2, ());
TEST_EQUAL(line[2].m_altitude, 50, ());
}
UNIT_TEST(ElevationInfo_MultipleLines_IndependentDistances)
{
using PWA = PointWithAltitude;
kml::MultiGeometry geometry;
geometry.AddLine({PWA({0.0, 0.0}, 100), PWA({1.0, 1.0}, 150)});
geometry.AddLine({PWA({2.0, 2.0}, 50), PWA({3.0, 3.0}, 75)});
ElevationInfo ei(geometry.m_lines);
auto const & lines = ei.GetLines();
TEST_EQUAL(2, lines.size(), ());
TEST_EQUAL(2, lines[0].size(), ());
TEST_EQUAL(2, lines[1].size(), ());
// Each line starts from 0.
TEST_EQUAL(lines[0][0].m_distance, 0, ());
TEST_EQUAL(lines[1][0].m_distance, 0, ());
}
UNIT_TEST(ElevationInfo_BuildWithGpsPoints)
{
GpsTrackElevation gpsElevation;
gpsElevation.AddGpsPoints({
BuildGpsInfo(0.0, 0.0, 0),
BuildGpsInfo(1.0, 1.0, 50),
BuildGpsInfo(2.0, 2.0, 100),
});
gpsElevation.AddGpsPoints({BuildGpsInfo(3.0, 3.0, -50)});
gpsElevation.AddGpsPoints({BuildGpsInfo(4.0, 4.0, 0)});
gpsElevation.AddGpsPoints({});
auto const & lines = gpsElevation.GetLines();
TEST_EQUAL(5, gpsElevation.GetSize(), ());
TEST_EQUAL(1, lines.size(), ());
}
// ===================== Track::GetPoint tests =====================
UNIT_TEST(Track_GetPoint_SingleLine_FirstPoint)
{
auto const p1 = PointWithAltitude({0.0, 0.0}, 100);
auto const p2 = PointWithAltitude({1.0, 0.0}, 200);
Track track = MakeTrack({{p1, p2}});
TEST_ALMOST_EQUAL_ABS(track.GetPoint(0.0), p1.GetPoint(), kEqualPointsEps, ());
}
UNIT_TEST(Track_GetPoint_SingleLine_LastPoint)
{
auto const p1 = PointWithAltitude({0.0, 0.0}, 100);
auto const p2 = PointWithAltitude({1.0, 0.0}, 200);
Track track = MakeTrack({{p1, p2}});
double const totalLen = mercator::DistanceOnEarth(p1.GetPoint(), p2.GetPoint());
TEST_ALMOST_EQUAL_ABS(track.GetPoint(totalLen), p2.GetPoint(), kEqualPointsEps, ());
}
UNIT_TEST(Track_GetPoint_SingleLine_MidSegment)
{
auto const p1 = PointWithAltitude({0.0, 0.0}, 100);
auto const p2 = PointWithAltitude({2.0, 0.0}, 200);
Track track = MakeTrack({{p1, p2}});
double const totalLen = mercator::DistanceOnEarth(p1.GetPoint(), p2.GetPoint());
TEST_ALMOST_EQUAL_ABS(track.GetPoint(totalLen / 2), m2::PointD(1.0, 0.0), 0.01, ());
}
UNIT_TEST(Track_GetPoint_ExactIntermediatePoint)
{
using PWA = PointWithAltitude;
PWA const p1({0.0, 0.0}, 100);
PWA const p2({1.0, 0.0}, 150);
PWA const p3({2.0, 0.0}, 200);
Track track = MakeTrack({{p1, p2, p3}});
double const d = mercator::DistanceOnEarth(p1.GetPoint(), p2.GetPoint());
TEST_ALMOST_EQUAL_ABS(track.GetPoint(d), p2.GetPoint(), kEqualPointsEps, ());
}
UNIT_TEST(Track_GetPoint_MultiLine)
{
using PWA = PointWithAltitude;
PWA const p1({0.0, 0.0}, 100);
PWA const p2({1.0, 0.0}, 150);
PWA const p3({2.0, 0.0}, 50);
PWA const p4({3.0, 0.0}, 75);
Track track = MakeTrack({{p1, p2}, {p3, p4}});
double const len1 = mercator::DistanceOnEarth(p1.GetPoint(), p2.GetPoint());
double const len2 = mercator::DistanceOnEarth(p3.GetPoint(), p4.GetPoint());
TEST_ALMOST_EQUAL_ABS(track.GetPoint(len1), p2.GetPoint(), kEqualPointsEps, ());
TEST_ALMOST_EQUAL_ABS(track.GetPoint(len1 + len2), p4.GetPoint(), kEqualPointsEps, ());
}
UNIT_TEST(Track_GetPoint_BeyondEnd)
{
auto const p1 = PointWithAltitude({0.0, 0.0}, 100);
auto const p2 = PointWithAltitude({1.0, 0.0}, 200);
Track track = MakeTrack({{p1, p2}});
double const totalLen = mercator::DistanceOnEarth(p1.GetPoint(), p2.GetPoint());
TEST_ALMOST_EQUAL_ABS(track.GetPoint(totalLen * 2), p2.GetPoint(), kEqualPointsEps, ());
}
UNIT_TEST(Track_GetPoint_NegativeDistance)
{
auto const p1 = PointWithAltitude({0.0, 0.0}, 100);
auto const p2 = PointWithAltitude({1.0, 0.0}, 200);
Track track = MakeTrack({{p1, p2}});
TEST_ALMOST_EQUAL_ABS(track.GetPoint(-1.0), p1.GetPoint(), kEqualPointsEps, ());
}
// ===================== UpdateSelectionInfo tests =====================
UNIT_TEST(Track_UpdateSelectionInfo_OnSegment)
{
using PWA = PointWithAltitude;
Track track = MakeTrack({{PWA({0.0, 0.0}, 0), PWA({10.0, 0.0}, 0)}});
Track::TrackSelectionInfo info;
info.m_squareDist = 1.0; // allow up to distance 1
track.UpdateSelectionInfo(m2::PointD(5.0, 0.0), info);
TEST_EQUAL(info.m_trackId, track.GetData().m_id, ());
TEST_ALMOST_EQUAL_ABS(info.m_trackPoint, m2::PointD(5.0, 0.0), kEqualPointsEps, ());
TEST_ALMOST_EQUAL_ABS(info.m_squareDist, 0.0, kEqualPointsEps, ());
}
UNIT_TEST(Track_UpdateSelectionInfo_PerpendicularFoot)
{
using PWA = PointWithAltitude;
Track track = MakeTrack({{PWA({0.0, 0.0}, 0), PWA({10.0, 0.0}, 0)}});
// Tap above segment: closest point is the perpendicular foot.
Track::TrackSelectionInfo info;
info.m_squareDist = 1.0; // 0.5^2 = 0.25 < 1, should accept
track.UpdateSelectionInfo(m2::PointD(3.0, 0.5), info);
TEST_EQUAL(info.m_trackId, track.GetData().m_id, ());
TEST_ALMOST_EQUAL_ABS(info.m_trackPoint, m2::PointD(3.0, 0.0), kEqualPointsEps, ());
TEST_ALMOST_EQUAL_ABS(info.m_squareDist, 0.25, kEqualPointsEps, ());
}
// Tap farther than the threshold: info is left untouched.
UNIT_TEST(Track_UpdateSelectionInfo_BeyondThreshold)
{
using PWA = PointWithAltitude;
Track track = MakeTrack({{PWA({0.0, 0.0}, 0), PWA({10.0, 0.0}, 0)}});
Track::TrackSelectionInfo info;
info.m_squareDist = 0.04; // only accept within 0.2
track.UpdateSelectionInfo(m2::PointD(5.0, 1.0), info);
TEST_EQUAL(info.m_trackId, kml::kInvalidTrackId, ());
TEST_ALMOST_EQUAL_ABS(info.m_squareDist, 0.04, kEqualPointsEps, ());
}
// Endpoint fallback: tap beyond the segment, closest point is the endpoint.
UNIT_TEST(Track_UpdateSelectionInfo_BeyondSegmentEnd)
{
using PWA = PointWithAltitude;
Track track = MakeTrack({{PWA({0.0, 0.0}, 0), PWA({10.0, 0.0}, 0)}});
Track::TrackSelectionInfo info;
info.m_squareDist = 100.0;
track.UpdateSelectionInfo(m2::PointD(12.0, 0.0), info);
TEST_EQUAL(info.m_trackId, track.GetData().m_id, ());
TEST_ALMOST_EQUAL_ABS(info.m_trackPoint, m2::PointD(10.0, 0.0), kEqualPointsEps, ());
TEST_ALMOST_EQUAL_ABS(info.m_squareDist, 4.0, kEqualPointsEps, ());
}
// Multi-segment track: best segment wins.
UNIT_TEST(Track_UpdateSelectionInfo_MultiSegmentClosestWins)
{
using PWA = PointWithAltitude;
// L-shape: (0,0) -> (10,0) -> (10,10)
Track track = MakeTrack({{PWA({0.0, 0.0}, 0), PWA({10.0, 0.0}, 0), PWA({10.0, 10.0}, 0)}});
// Tap nearer to the vertical leg.
Track::TrackSelectionInfo info;
info.m_squareDist = 100.0;
track.UpdateSelectionInfo(m2::PointD(9.0, 5.0), info);
TEST_EQUAL(info.m_trackId, track.GetData().m_id, ());
TEST_ALMOST_EQUAL_ABS(info.m_trackPoint, m2::PointD(10.0, 5.0), kEqualPointsEps, ());
TEST_ALMOST_EQUAL_ABS(info.m_squareDist, 1.0, kEqualPointsEps, ());
}
// Multi-line track: best line wins regardless of ordering.
UNIT_TEST(Track_UpdateSelectionInfo_MultiLine)
{
using PWA = PointWithAltitude;
Track track = MakeTrack({{PWA({0.0, 0.0}, 0), PWA({10.0, 0.0}, 0)}, {PWA({0.0, 100.0}, 0), PWA({10.0, 100.0}, 0)}});
Track::TrackSelectionInfo info;
info.m_squareDist = 10000.0;
track.UpdateSelectionInfo(m2::PointD(5.0, 99.0), info);
TEST_EQUAL(info.m_trackId, track.GetData().m_id, ());
TEST_ALMOST_EQUAL_ABS(info.m_trackPoint, m2::PointD(5.0, 100.0), kEqualPointsEps, ());
TEST_ALMOST_EQUAL_ABS(info.m_squareDist, 1.0, kEqualPointsEps, ());
}
// Subsequent call tightens the threshold via info.m_squareDist carry-over.
UNIT_TEST(Track_UpdateSelectionInfo_ThresholdTightensAcrossCalls)
{
using PWA = PointWithAltitude;
Track farTrack = MakeTrack({{PWA({0.0, 5.0}, 0), PWA({10.0, 5.0}, 0)}});
Track nearTrack = MakeTrack({{PWA({0.0, 1.0}, 0), PWA({10.0, 1.0}, 0)}});
Track::TrackSelectionInfo info;
info.m_squareDist = 100.0;
// Tap at (5, 0): far track is at dist 5 (sq=25), near track is at dist 1 (sq=1).
farTrack.UpdateSelectionInfo(m2::PointD(5.0, 0.0), info);
TEST_EQUAL(info.m_trackId, farTrack.GetData().m_id, ());
TEST_ALMOST_EQUAL_ABS(info.m_squareDist, 25.0, kEqualPointsEps, ());
nearTrack.UpdateSelectionInfo(m2::PointD(5.0, 0.0), info);
TEST_EQUAL(info.m_trackId, nearTrack.GetData().m_id, ());
TEST_ALMOST_EQUAL_ABS(info.m_squareDist, 1.0, kEqualPointsEps, ());
}
// Farther track is rejected when a closer one already set a tighter threshold.
UNIT_TEST(Track_UpdateSelectionInfo_FartherTrackRejected)
{
using PWA = PointWithAltitude;
Track nearTrack = MakeTrack({{PWA({0.0, 1.0}, 0), PWA({10.0, 1.0}, 0)}});
Track farTrack = MakeTrack({{PWA({0.0, 5.0}, 0), PWA({10.0, 5.0}, 0)}});
Track::TrackSelectionInfo info;
info.m_squareDist = 100.0;
nearTrack.UpdateSelectionInfo(m2::PointD(5.0, 0.0), info);
auto const nearId = info.m_trackId;
TEST_EQUAL(nearId, nearTrack.GetData().m_id, ());
// Far track is beyond the tightened threshold: must not replace nearTrack.
farTrack.UpdateSelectionInfo(m2::PointD(5.0, 0.0), info);
TEST_EQUAL(info.m_trackId, nearId, ());
TEST_ALMOST_EQUAL_ABS(info.m_squareDist, 1.0, kEqualPointsEps, ());
}
// No explicit threshold: default (max) accepts everything.
UNIT_TEST(Track_UpdateSelectionInfo_DefaultThresholdAcceptsAny)
{
using PWA = PointWithAltitude;
Track track = MakeTrack({{PWA({0.0, 0.0}, 0), PWA({10.0, 0.0}, 0)}});
Track::TrackSelectionInfo info;
// Default info.m_squareDist = max. Tap very far from the track.
track.UpdateSelectionInfo(m2::PointD(1e6, 1e6), info);
TEST_EQUAL(info.m_trackId, track.GetData().m_id, ());
TEST_ALMOST_EQUAL_ABS(info.m_trackPoint, m2::PointD(10.0, 0.0), kEqualPointsEps, ());
}
// ===================== TrackStatistics tests =====================
UNIT_TEST(TrackStatistics_Duration)
{
kml::MultiGeometry geometry;
geometry.AddLine({PointWithAltitude({0.0, 0.0}, 100), PointWithAltitude({1.0, 1.0}, 150)});
geometry.AddTimestamps({0, 5});
geometry.AddLine({PointWithAltitude({2.0, 2.0}, 50), PointWithAltitude({3.0, 3.0}, 75)});
geometry.AddTimestamps({10, 13});
TrackStatistics ts;
ts.CalculateDuration(geometry);
TEST_EQUAL(ts.m_duration, 8, ()); // (5 - 0) + (13 - 10)
}
UNIT_TEST(TrackStatistics_Duration_PartialTimestamps)
{
// A track may carry timestamps on some lines only (e.g. GeoJSON import drops a line's broken
// timestamps). CalculateDuration must skip the timestamp-less line, not assert/crash.
kml::MultiGeometry geometry;
geometry.AddLine({PointWithAltitude({0.0, 0.0}, 100), PointWithAltitude({1.0, 1.0}, 150)});
geometry.AddTimestamps({0, 5});
geometry.AddLine({PointWithAltitude({2.0, 2.0}, 50), PointWithAltitude({3.0, 3.0}, 75)});
geometry.AddTimestamps({}); // Second line has no timestamps.
TrackStatistics ts;
ts.CalculateDuration(geometry);
TEST_EQUAL(ts.m_duration, 5, ()); // Only the first line contributes (5 - 0).
}
UNIT_TEST(TrackStatistics_GpsPoints)
{
GpsInfo const points[] = {BuildGpsInfo(0.0, 0.0, 0, 0), BuildGpsInfo(1.0, 1.0, 50, 1), BuildGpsInfo(2.0, 2.0, 100, 2),
BuildGpsInfo(3.0, 3.0, -50, 5), BuildGpsInfo(4.0, 4.0, 0, 10)};
TrackStatistics ts;
for (auto const & point : points)
ts.AddGpsInfoPoint(point);
TEST_EQUAL(ts.m_minElevation, -50, ());
TEST_EQUAL(ts.m_maxElevation, 100, ());
TEST_EQUAL(ts.m_ascent, 150, ()); // 0->50, 50->100, -50->0
TEST_EQUAL(ts.m_descent, 150, ()); // 100->-50
TEST_EQUAL(ts.m_duration, 10, ());
TEST_GREATER(ts.m_length, 0, ());
}
// ===================== Elevation simplification tests =====================
namespace
{
ElevationInfo::AltitudesInfo CalcAltitudesInfo(std::string const & fileName, Altitude threshold)
{
kml::FileData fileData;
kml::DeserializerGpx(fileData).Deserialize(FileReader(GetPlatform().TestsDataPathForFile(fileName)));
TEST_EQUAL(fileData.m_tracksData.size(), 1, ());
ElevationInfo ei(fileData.m_tracksData[0].m_geometry.m_lines);
ei.Simplify();
return ei.CalculateAltitudesInfo(threshold);
}
} // namespace
// https://github.com/organicmaps/organicmaps/issues/5087#issuecomment-4177262973
UNIT_TEST(Elevation_AscentDescent_Transalpes)
{
auto const altInfo = CalcAltitudesInfo("test_data/gpx/transalpes-2026-preparation.gpx", 15);
// Paper guide: ~1550m ascent, ~850m descent.
double constexpr kEps = 0.04;
TEST_ALMOST_EQUAL_ABS(double(altInfo.GetTotalAscent()), 1550.0, 1550.0 * kEps, ());
TEST_ALMOST_EQUAL_ABS(double(altInfo.GetTotalDescent()), 850.0, 850.0 * kEps, ());
}
// https://github.com/organicmaps/organicmaps/issues/5087#issuecomment-3390365431
UNIT_TEST(Elevation_AscentDescent_AllenMountain)
{
auto const altInfo = CalcAltitudesInfo("test_data/gpx/East River Trail to Allen Mountain.gpx", 4);
// Komoot: 2850ft (868m) ascent, 375ft (114m) descent.
TEST_ALMOST_EQUAL_ABS(double(altInfo.GetTotalAscent()), 868.0, 868.0 * 0.01, ());
TEST_ALMOST_EQUAL_ABS(double(altInfo.GetTotalDescent()), 114.0, 114.0 * 0.1, ());
}
// https://github.com/organicmaps/organicmaps/issues/5454#issuecomment-3140515619
UNIT_TEST(Elevation_AscentDescent_Solden_Merano)
{
auto const altInfo = CalcAltitudesInfo("test_data/gpx/Solden (Tirol) to Merano (South Tirol).gpx", 6);
// Google: 1374m up and 2427m down.
double constexpr kEps = 0.05;
TEST_ALMOST_EQUAL_ABS(double(altInfo.GetTotalAscent()), 1374.0, 1374.0 * kEps, ());
TEST_ALMOST_EQUAL_ABS(double(altInfo.GetTotalDescent()), 2427.0, 2427.0 * kEps, ());
}
// https://github.com/organicmaps/organicmaps/issues/5087#issuecomment-4009485791
UNIT_TEST(Elevation_AscentDescent_Peak4120)
{
auto const altInfo = CalcAltitudesInfo("test_data/gpx/Peak 4120.20260209.gpx", 10);
// User said: 1647ft (502m) up and down.
double constexpr kEps = 0.07;
TEST_ALMOST_EQUAL_ABS(double(altInfo.GetTotalAscent()), 502.0, 502.0 * kEps, ());
TEST_ALMOST_EQUAL_ABS(double(altInfo.GetTotalDescent()), 502.0, 502.0 * kEps, ());
}
UNIT_TEST(Elevation_SmoothSlopeOutliers)
{
kml::FileData fileData;
kml::DeserializerGpx(fileData).Deserialize(
FileReader(GetPlatform().TestsDataPathForFile("test_data/gpx/transalpes-2026-preparation.gpx")));
auto const & lines = fileData.m_tracksData[0].m_geometry.m_lines;
{
ElevationInfo ei(lines);
ei.Simplify();
auto const info = ei.CalculateAltitudesInfo(10);
LOG_SHORT(LINFO, ("maxSlope = NONE", "ascent =", info.GetTotalAscent(), "descent =", info.GetTotalDescent()));
}
for (double maxSlope : {50.0, 70.0, 100.0, 150.0, 200.0})
{
ElevationInfo ei(lines);
ei.SmoothSlopeOutliers(maxSlope);
ei.Simplify();
auto const info = ei.CalculateAltitudesInfo(10);
LOG_SHORT(LINFO, ("maxSlope =", maxSlope, "ascent =", info.GetTotalAscent(), "descent =", info.GetTotalDescent()));
}
}
} // namespace track_tests
|