55// You can obtain one at https://mozilla.org/MPL/2.0/.
66
77#include < aliceVision/track/TracksMerger.hpp>
8+ #include < aliceVision/system/Logger.hpp>
89
910namespace aliceVision
1011{
@@ -13,24 +14,32 @@ namespace track
1314
1415bool TracksMerger::addTrackMap (const track::TracksMap & inputTracks)
1516{
17+ // Loop over the tracks to add
1618 for (const auto & [idTrack, track]: inputTracks)
1719 {
1820 IndexT foundTrack = UndefinedIndexT;
19- size_t newSize = track.featPerView .size ();
20-
21+
22+ // Find if one of the feature is already existing in a track
23+ // In this case we would like to merge both tracks instead of adding a track
2124 for (const auto & [idView, feat]: track.featPerView )
2225 {
2326 TuplePoint tp = std::make_tuple (track.descType , idView, feat.featureId );
24- auto it = _existingTracks.find (tp);
25- if (it != _existingTracks.end ())
27+ if (_existingTracks.find (tp) == _existingTracks.end ())
28+ {
29+ continue ;
30+ }
31+
32+ if (foundTrack != UndefinedIndexT)
2633 {
27- foundTrack = it-> second ;
28- break ;
34+ ALICEVISION_LOG_INFO ( " Multiple tracks possible. Keeping the first one. " ) ;
35+ continue ;
2936 }
37+
38+ foundTrack = _existingTracks[tp];
3039 }
31-
40+
3241 if (foundTrack == UndefinedIndexT)
33- {
42+ {
3443 // Simply add track
3544 foundTrack = _lastIndex;
3645 _lastIndex++;
@@ -40,25 +49,20 @@ bool TracksMerger::addTrackMap(const track::TracksMap & inputTracks)
4049 auto & outputTrack = _tracks[foundTrack];
4150 outputTrack.descType = track.descType ;
4251
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
52+ // Find if one of the feature is already existing in a track
53+ // In this case we would like to merge both tracks instead of adding a track
4754 for (const auto & [idView, feat]: track.featPerView )
4855 {
49- 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 ())
56+ if (track.featPerView .find (idView) != track.featPerView .end ())
5457 {
55- if (newSize < oldSize)
56- {
57- continue ;
58- }
59- }
60-
58+ // Keep the accumulated one
59+ continue ;
60+ }
61+
6162 outputTrack.featPerView [idView] = feat;
63+
64+ TuplePoint tp = std::make_tuple (track.descType , idView, feat.featureId );
65+ _existingTracks[tp] = foundTrack;
6266 }
6367 }
6468
0 commit comments