Program Listing for File visual-npipeline.h

Return to documentation for file (aslam_cv2/aslam_cv_pipeline/include/aslam/pipeline/visual-npipeline.h)

#ifndef VISUAL_NPIPELINE_H_
#define VISUAL_NPIPELINE_H_

#include <condition_variable>
#include <map>
#include <memory>
#include <mutex>
#include <vector>

#include <opencv2/core/core.hpp>

#include <aslam/cameras/ncamera.h>
#include <aslam/common/macros.h>
#include <aslam/common/thread-pool.h>
#include <aslam/frames/visual-frame.h>
#include <aslam/frames/visual-nframe.h>
#include <aslam/pipeline/visual-pipeline.h>

namespace aslam {

class VisualNPipeline final {
 public:
  ASLAM_POINTER_TYPEDEFS(VisualNPipeline);
  ASLAM_DISALLOW_EVIL_CONSTRUCTORS(VisualNPipeline);

  VisualNPipeline(size_t num_threads,
                  const std::vector<VisualPipeline::Ptr>& pipelines,
                  const NCamera::Ptr& input_camera_system,
                  const NCamera::Ptr& output_camera_system,
                  int64_t timestamp_tolerance_ns);

  ~VisualNPipeline();

  void shutdown();

  void processImage(size_t camera_index, const cv::Mat& image, int64_t timestamp);

  bool processImageBlockingIfFull(size_t camera_index, const cv::Mat& image, int64_t timestamp,
                                  size_t max_output_queue_size);

  bool processImageNonBlockingDroppingOldestNFrameIfFull(
      size_t camera_index, const cv::Mat &image, int64_t timestamp,
      size_t max_output_queue_size);

  size_t getNumFramesComplete() const;

  size_t getNumFramesProcessing() const;

  std::shared_ptr<VisualNFrame> getNext();

  bool getNextBlocking(std::shared_ptr<VisualNFrame>* nframe);

  std::shared_ptr<VisualNFrame> getLatestAndClear();

  bool getLatestAndClearBlocking(std::shared_ptr<VisualNFrame>* nframe);

  std::shared_ptr<const NCamera> getInputNCameras() const;

  std::shared_ptr<const NCamera> getOutputNCameras() const;

  void waitForAllWorkToComplete() const;

  static VisualNPipeline::Ptr createTestVisualNPipeline(size_t num_cameras,
      size_t num_threads, int64_t timestamp_tolerance_ns);

 private:
  void work(size_t camera_index, const cv::Mat& image, int64_t timestamp_nanoseconds);

  std::shared_ptr<VisualNFrame> getNextImpl();

  void processImageImpl(size_t camera_index, const cv::Mat& image,
                        int64_t timestamp);

  std::vector<std::shared_ptr<VisualPipeline>> pipelines_;

  mutable std::mutex mutex_;
  std::condition_variable condition_not_full_;
  std::condition_variable condition_not_empty_;
  std::atomic<bool> shutdown_;

  typedef std::map<int64_t, std::shared_ptr<VisualNFrame>> TimestampVisualNFrameMap;
  TimestampVisualNFrameMap processing_;
  TimestampVisualNFrameMap completed_;

  std::shared_ptr<aslam::ThreadPool> thread_pool_;

  std::shared_ptr<NCamera> input_camera_system_;
  std::shared_ptr<NCamera> output_camera_system_;

  int64_t timestamp_tolerance_ns_;
};
}  // namespace aslam
#endif // VISUAL_NPIPELINE_H_