Program Listing for File DepthMap.h

Return to documentation for file (lib/DepthEstimation/DepthMap.h)

#pragma once
#include "opencv2/core/core.hpp"
#include "util/Configuration.h"
#include "util/EigenCoreInclude.h"
#include "util/IndexThreadReduce.h"
#include "util/MovingAverage.h"
#include "util/SophusUtil.h"
#include "util/Timer.h"
#include "util/settings.h"

#include "DataStructures/Frame.h"
#include "DataStructures/ImageSet.h"
#include "DepthMapDebugImages.h"

namespace lsd_slam {

class DepthMapPixelHypothesis;
class KeyFrameGraph;

class KeyFrame;

class DepthMap {
public:

  typedef std::shared_ptr<DepthMap> SharedPtr;

  // Delete default constructors
  DepthMap(const DepthMap &) = delete;
  DepthMap &operator=(const DepthMap &) = delete;

    DepthMap( const std::shared_ptr<Frame> &parent );

    // //== Propagation constructor for subsequent keyframes
    // DepthMap( const DepthMap::SharedPtr &other, const Frame::SharedPtr &frame );

    ~DepthMap();

    void reset();

    // /**
    //  * does obervation and regularization only.
    //  **/
    // void updateKeyframe(std::deque< Frame::SharedPtr > referenceFrames);
    //
    // /**
    //  * does propagation and whole-filling-regularization (no observation, for that need to call updateKeyframe()!)
    //  **/
    // void createKeyFrame( const Frame::SharedPtr &new_keyframe );

    // void finalizeKeyFrame();

    // void invalidate();
    // inline bool isValid() {return (bool)activeKeyFrame;};

    //int debugPlotDepthMap();
    const DepthMapDebugImages &debugImages() const { return _debugImages; }

    // This is the only debug plot which is triggered externally..
    void plotDepthMap( const char *buf1, const char *buf2 );


    //== Initializers, required only when propagating from a previous keyframe
    void initializeFromFrame();
    void initializeFromGTDepth();
  void initializeFromStereo();
    void initializeRandomly();
    // void initializefromStereo( const std::shared_ptr<ImageSet> &set);

    void propagateFrom( const DepthMap::SharedPtr &new_keyframe, float &rescaleFactor );

    void finalize();

    // void activateExistingKF(const Frame::SharedPtr &kf);

  void activateExistingKF(const Frame::SharedPtr &kf);

    //==
    bool updateDepthFrom( const Frame::SharedPtr &frame );

  struct PerformanceData {
    PerformanceData(void) { ; }

    MsRateAverage update, create, finalize, observe, regularize, propagate,
        fillHoles, setDepth;
  };

  PerformanceData perf() const { return _perf; }
  void logPerformanceData();

    // Convenience accessors
    std::shared_ptr<Frame> &frame() { return _frame; }

    //
    DepthMapPixelHypothesis *hypothesisAt( const int x, const int y )
        { return currentDepthMap + x + y*Conf().slamImageSize.width; }




private:

    IndexThreadReduce threadReducer;

    PerformanceData _perf;
    DepthMapDebugImages _debugImages;

    // ============= parameter copies for convenience ===========================
    // these are just copies of the pointers given to this function, for convenience.
    // these are NOT managed by this object!
    std::shared_ptr<Frame> _frame;

    const float* activeKeyFrameImageData()    { return frame()->image(0); }
    bool activeKeyFrameIsReactivated;

    // Frame::SharedPtr oldest_referenceFrame;
    // Frame::SharedPtr newest_referenceFrame;
    // std::vector< Frame::SharedPtr > referenceFrameByID;
    // int referenceFrameByID_offset;

    // ============= internally used buffers for intermediate calculations etc. =============
    // for internal depth tracking, their memory is managed (created & deleted) by this object.
    DepthMapPixelHypothesis* otherDepthMap;
    DepthMapPixelHypothesis* currentDepthMap;
    int* validityIntegralBuffer;



    // ============ internal functions ==================================================
    // does the line-stereo seeking.
    // takes a lot of parameters, because they all have been pre-computed before.
    inline float doLineStereo(
            const float u, const float v, const float epxn, const float epyn,
            const float min_idepth, const float prior_idepth, float max_idepth,
            const Frame* const referenceFrame, const float* referenceFrameImage,
            float &result_idepth, float &result_var, float &result_eplLength,
            RunningStats* const stats);

    // Reset currentDepthMap by re-projecting is from activeKeyFrame to new_keyframe
    void propagateDepthFrom(const DepthMap::SharedPtr &new_keyframe, float &rescaleFactor );


    // This is a local state variable used to share data between the observeDepth* functions.  Sucks, I know
    Frame::SharedPtr _observeFrame;
    void observeDepth( const Frame::SharedPtr &updateFrame );
    void observeDepthRow(int yMin, int yMax, RunningStats* stats);
    bool observeDepthCreate(const int &x, const int &y, const int &idx, RunningStats* const &stats);
    bool observeDepthUpdate(const int &x, const int &y, const int &idx, const float* keyFrameMaxGradBuf, RunningStats* const &stats);
    bool makeAndCheckEPL(const int x, const int y, const Frame* const ref, float* pepx, float* pepy, RunningStats* const stats);


    void regularizeDepthMap(bool removeOcclusion, int validityTH);
    template<bool removeOcclusions> void regularizeDepthMapRow(int validityTH, int yMin, int yMax, RunningStats* stats);


    void buildRegIntegralBuffer();
    void buildRegIntegralBufferRow1(int yMin, int yMax, RunningStats* stats);
    void regularizeDepthMapFillHoles();
    void regularizeDepthMapFillHolesRow(int yMin, int yMax, RunningStats* stats);


    void resetCounters();

    //float clocksPropagate, clocksPropagateKF, clocksObserve, msObserve, clocksReg1, clocksReg2, msReg1, msReg2, clocksFinalize;
};

} // namespace lsd_slam