@@ -13,24 +13,48 @@ namespace track
1313
1414bool TracksMerger::addTrackMap(const track::TracksMap & inputTracks)
1515{
16+ // Loop over the tracks to add
1617 for (const auto & [idTrack, track]: inputTracks)
1718 {
18- IndexT foundTrack = UndefinedIndexT;
19- size_t newSize = track.featPerView.size();
20-
19+ std::map<IndexT, size_t> candidates;
20+
21+ // Find if one of the features already exists in a track
22+ // In this case we would like to merge both tracks instead of adding a track
2123 for (const auto & [idView, feat]: track.featPerView)
2224 {
2325 TuplePoint tp = std::make_tuple(track.descType, idView, feat.featureId);
24- auto it = _existingTracks.find(tp);
25- if (it != _existingTracks.end())
26+ if (_existingTracks.find(tp) == _existingTracks.end())
27+ {
28+ // This feature is not present in the existing tracks
29+ continue;
30+ }
31+
32+ // Compute stats on best target to use
33+ IndexT target = _existingTracks[tp];
34+ if (candidates.find(target) == candidates.end())
35+ {
36+ candidates[target] = _tracks[target].featPerView.size();
37+ }
38+ else
39+ {
40+ candidates[target]++;
41+ }
42+ }
43+
44+ // Select the largest track solution
45+ size_t bestSize = 0;
46+ IndexT foundTrack = UndefinedIndexT;
47+ for (const auto [trackId, size] : candidates)
48+ {
49+ if (size > bestSize)
2650 {
27- foundTrack = it->second ;
28- break ;
51+ foundTrack = trackId ;
52+ bestSize = size ;
2953 }
3054 }
31-
55+
3256 if (foundTrack == UndefinedIndexT)
33- {
57+ {
3458 //Simply add track
3559 foundTrack = _lastIndex;
3660 _lastIndex++;
@@ -40,25 +64,18 @@ bool TracksMerger::addTrackMap(const track::TracksMap & inputTracks)
4064 auto & outputTrack = _tracks[foundTrack];
4165 outputTrack.descType = track.descType;
4266
43- //Previous Size is either 0 if new track, or the size of the matching track
44- size_t oldSize = outputTrack.featPerView.size();
45-
46- //Append all features from existing track
67+ // Merge both tracks
4768 for (const auto & [idView, feat]: track.featPerView)
4869 {
4970 TuplePoint tp = std::make_tuple(track.descType, idView, feat.featureId);
50- _existingTracks[tp] = foundTrack;
51-
52- // Replace only if the new tracks is longer than the old one.
53- if (outputTrack.featPerView.find(idView) != outputTrack.featPerView.end())
71+ // Ignore any track item that disagrees with the retargeting
72+ if (_existingTracks.find(tp) != _existingTracks.end())
5473 {
55- if (newSize < oldSize)
56- {
57- continue;
58- }
59- }
60-
74+ continue;
75+ }
76+
6177 outputTrack.featPerView[idView] = feat;
78+ _existingTracks[tp] = foundTrack;
6279 }
6380 }
6481
0 commit comments