Program Listing for File track-manager.h

Return to documentation for file (aslam_cv2/aslam_cv_tracker/include/aslam/tracker/track-manager.h)

#ifndef ASLAM_TRACK_MANAGER_H_
#define ASLAM_TRACK_MANAGER_H_

#include <mutex>
#include <unordered_set>

#include <aslam/matcher/match.h>
#include <glog/logging.h>

namespace aslam {
  struct MatchWithScore;
  class VisualNFrame;

  template<typename IdType>
  class ThreadSafeIdProvider {
   public:
    ThreadSafeIdProvider(IdType initial_id) : initial_id_(initial_id) {
      reset();
    }

    IdType getNewId() {
      std::lock_guard<std::mutex> lock(mutex_);
      return id_++;
    }

    void reset() {
      std::lock_guard<std::mutex> lock(mutex_);
      id_ = initial_id_;
    }

   private:
    std::mutex mutex_;
    IdType id_;
    IdType initial_id_;
  };

  class TrackManager {
   public:
    TrackManager() {}
    virtual ~TrackManager() {};

    virtual void applyMatchesToFrames(
        const FrameToFrameMatchesWithScore& matches_A_B,
        VisualFrame* apple_frame, VisualFrame* banana_frame) = 0;

    static Eigen::VectorXi* createAndGetTrackIdChannel(VisualFrame* frame);

    static void resetIdProvider() {
      track_id_provider_.reset();
    }

   protected:
    static ThreadSafeIdProvider<size_t> track_id_provider_;
  };


  class SimpleTrackManager : public TrackManager {
   public:
    SimpleTrackManager() = default;
    virtual ~SimpleTrackManager() {};

    virtual void applyMatchesToFrames(
        const FrameToFrameMatchesWithScore& matches,
        VisualFrame* apple_frame, VisualFrame* banana_frame);
  };

  class UniformTrackManager : public TrackManager {
   public:
    UniformTrackManager(size_t num_buckets_root,
                        size_t max_number_of_weak_new_tracks,
                        size_t num_strong_new_tracks_to_force_push,
                        double match_score_very_strong_new_tracks_threshold) :
      number_of_tracking_buckets_root_(num_buckets_root),
      bucket_capacity_(std::floor(
          static_cast<double>(max_number_of_weak_new_tracks) /
          static_cast<double>(num_buckets_root * num_buckets_root))),
      number_of_very_strong_new_tracks_to_force_push_(
          num_strong_new_tracks_to_force_push),
      match_score_very_strong_new_tracks_threshold_(
          match_score_very_strong_new_tracks_threshold) {}

    virtual ~UniformTrackManager() {};

    virtual void applyMatchesToFrames(
        const FrameToFrameMatchesWithScore& matches_A_B,
        VisualFrame* apple_frame, VisualFrame* banana_frame);
   private:
    size_t number_of_tracking_buckets_root_;
    size_t bucket_capacity_;
    size_t number_of_very_strong_new_tracks_to_force_push_;
    double match_score_very_strong_new_tracks_threshold_;
  };

  inline void addToSetsAndCheckExclusiveness(
      int index_apple, int index_banana, std::unordered_set<int>* consumed_apples,
      std::unordered_set<int>* consumed_bananas) {
    CHECK_NOTNULL(consumed_apples);
    CHECK_NOTNULL(consumed_bananas);
    std::pair<std::unordered_set<int>::iterator, bool> ret_apple =
        consumed_apples->insert(index_apple);
    CHECK(ret_apple.second) << "The given matches don't seem to be exclusive."
        " Trying to assign apple " << index_apple << " more than once!";
    std::pair<std::unordered_set<int>::iterator, bool> ret_banana =
        consumed_bananas->insert(index_banana);
    CHECK(ret_banana.second) << "The given matches don't seem to be "
        "exclusive. Trying to assign banana " << index_banana << " more than "
            "once!";

  }

}  // namespace aslam

#endif  // ASLAM_TRACK_MANAGER_H_