refactor(synthetic): use shared_ptr for parent_

This commit is contained in:
TinyOh 2019-01-22 11:23:54 +08:00
parent aa83d3bf85
commit 69168303d6
20 changed files with 97 additions and 36 deletions

View File

@ -52,7 +52,7 @@ std::string Processor::Name() {
} }
void Processor::AddChild(const std::shared_ptr<Processor> &child) { void Processor::AddChild(const std::shared_ptr<Processor> &child) {
child->parent_ = this; child->parent_ = shared_from_this();
childs_.push_back(child); childs_.push_back(child);
} }
@ -81,7 +81,8 @@ void Processor::Activate(bool parents) {
return; return;
if (parents) { if (parents) {
// Activate all parents // Activate all parents
Processor *parent = parent_; auto parent = parent_;
// Processor *parent = parent_;
while (parent != nullptr) { while (parent != nullptr) {
parent->Activate(); parent->Activate();
parent = parent->parent_; parent = parent->parent_;
@ -97,7 +98,8 @@ void Processor::Deactivate(bool childs) {
return; return;
if (childs) { if (childs) {
// Deactivate all childs // Deactivate all childs
iterate_processors(GetChilds(), [](std::shared_ptr<Processor> proc) { iterate_processors_PtoC_after(GetChilds(),
[](std::shared_ptr<Processor> proc) {
proc->Deactivate(); proc->Deactivate();
}); });
} }
@ -152,6 +154,10 @@ std::uint64_t Processor::GetDroppedCount() {
return dropped_count_; return dropped_count_;
} }
std::shared_ptr<Processor> Processor::GetParent() {
return parent_;
}
void Processor::Run() { void Processor::Run() {
VLOG(2) << Name() << " thread start"; VLOG(2) << Name() << " thread start";

View File

@ -23,18 +23,22 @@
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <thread> #include <thread>
#include <vector>
// #include "mynteye/api/synthetic.h"
#include "mynteye/mynteye.h" #include "mynteye/mynteye.h"
#include "mynteye/api/object.h" #include "mynteye/api/object.h"
MYNTEYE_BEGIN_NAMESPACE MYNTEYE_BEGIN_NAMESPACE
class Processor /*: public std::enable_shared_from_this<Processor>*/ { class Processor :
public std::enable_shared_from_this<Processor> {
public: public:
using PreProcessCallback = std::function<void(Object *const)>; using PreProcessCallback = std::function<void(Object *const)>;
using PostProcessCallback = std::function<void(Object *const)>; using PostProcessCallback = std::function<void(Object *const)>;
using ProcessCallback = std::function<bool( using ProcessCallback = std::function<bool(
Object *const in, Object *const out, Processor *const parent)>; Object *const in, Object *const out,
std::shared_ptr<Processor> const parent)>;
explicit Processor(std::int32_t proc_period = 0); explicit Processor(std::int32_t proc_period = 0);
virtual ~Processor(); virtual ~Processor();
@ -45,6 +49,7 @@ class Processor /*: public std::enable_shared_from_this<Processor>*/ {
void RemoveChild(const std::shared_ptr<Processor> &child); void RemoveChild(const std::shared_ptr<Processor> &child);
std::shared_ptr<Processor> GetParent();
std::list<std::shared_ptr<Processor>> GetChilds(); std::list<std::shared_ptr<Processor>> GetChilds();
void SetPreProcessCallback(PreProcessCallback callback); void SetPreProcessCallback(PreProcessCallback callback);
@ -71,7 +76,8 @@ class Processor /*: public std::enable_shared_from_this<Processor>*/ {
protected: protected:
virtual Object *OnCreateOutput() = 0; virtual Object *OnCreateOutput() = 0;
virtual bool OnProcess( virtual bool OnProcess(
Object *const in, Object *const out, Processor *const parent) = 0; Object *const in, Object *const out,
std::shared_ptr<Processor> const parent) = 0;
private: private:
/** Run in standalone thread. */ /** Run in standalone thread. */
@ -101,20 +107,44 @@ class Processor /*: public std::enable_shared_from_this<Processor>*/ {
PostProcessCallback post_callback_; PostProcessCallback post_callback_;
ProcessCallback callback_; ProcessCallback callback_;
Processor *parent_; // Processor *parent_;
std::shared_ptr<Processor> parent_;
std::list<std::shared_ptr<Processor>> childs_; std::list<std::shared_ptr<Processor>> childs_;
std::thread thread_; std::thread thread_;
}; };
template <typename T> template <typename T>
void iterate_processors( void iterate_processors_PtoC_after(
const T &processors, std::function<void(std::shared_ptr<Processor>)> fn) { const T &processors, std::function<void(std::shared_ptr<Processor>)> fn) {
for (auto &&proc : processors) { for (auto &&proc : processors) {
fn(proc); fn(proc);
iterate_processors(proc->GetChilds(), fn); iterate_processors_PtoC_after(proc->GetChilds(), fn);
} }
} }
template <typename T>
void iterate_processors_PtoC_before(
const T &processors, std::function<void(std::shared_ptr<Processor>)> fn) {
for (auto &&proc : processors) {
iterate_processors_PtoC_before(proc->GetChilds(), fn);
fn(proc);
}
}
template <typename T>
void iterate_processors_CtoP_before(
T processor, std::function<void(std::shared_ptr<Processor>)> fn) {
if (processor->GetParent() != nullptr)
iterate_processors_CtoP_before(processor->GetParent(), fn);
fn(processor);
}
template <typename T>
void iterate_processors_CtoP_after(
T processor, std::function<void(std::shared_ptr<Processor>)> fn) {
fn(processor);
if (processor->GetParent() != nullptr)
iterate_processors_CtoP_after(processor->GetParent(), fn);
}
MYNTEYE_END_NAMESPACE MYNTEYE_END_NAMESPACE

View File

@ -45,7 +45,8 @@ Object *DepthProcessor::OnCreateOutput() {
} }
bool DepthProcessor::OnProcess( bool DepthProcessor::OnProcess(
Object *const in, Object *const out, Processor *const parent) { Object *const in, Object *const out,
std::shared_ptr<Processor> const parent) {
MYNTEYE_UNUSED(parent) MYNTEYE_UNUSED(parent)
const ObjMat *input = Object::Cast<ObjMat>(in); const ObjMat *input = Object::Cast<ObjMat>(in);
ObjMat *output = Object::Cast<ObjMat>(out); ObjMat *output = Object::Cast<ObjMat>(out);

View File

@ -36,7 +36,8 @@ class DepthProcessor : public Processor {
protected: protected:
Object *OnCreateOutput() override; Object *OnCreateOutput() override;
bool OnProcess( bool OnProcess(
Object *const in, Object *const out, Processor *const parent) override; Object *const in, Object *const out,
std::shared_ptr<Processor> const parent) override;
private: private:
std::shared_ptr<struct camera_calib_info_pair> calib_infos_; std::shared_ptr<struct camera_calib_info_pair> calib_infos_;
}; };

View File

@ -39,7 +39,8 @@ Object *DepthProcessorOCV::OnCreateOutput() {
} }
bool DepthProcessorOCV::OnProcess( bool DepthProcessorOCV::OnProcess(
Object *const in, Object *const out, Processor *const parent) { Object *const in, Object *const out,
std::shared_ptr<Processor> const parent) {
MYNTEYE_UNUSED(parent) MYNTEYE_UNUSED(parent)
const ObjMat *input = Object::Cast<ObjMat>(in); const ObjMat *input = Object::Cast<ObjMat>(in);
ObjMat *output = Object::Cast<ObjMat>(out); ObjMat *output = Object::Cast<ObjMat>(out);

View File

@ -33,7 +33,8 @@ class DepthProcessorOCV : public Processor {
protected: protected:
Object *OnCreateOutput() override; Object *OnCreateOutput() override;
bool OnProcess( bool OnProcess(
Object *const in, Object *const out, Processor *const parent) override; Object *const in, Object *const out,
std::shared_ptr<Processor> const parent) override;
}; };
MYNTEYE_END_NAMESPACE MYNTEYE_END_NAMESPACE

View File

@ -43,7 +43,8 @@ Object *DisparityNormalizedProcessor::OnCreateOutput() {
} }
bool DisparityNormalizedProcessor::OnProcess( bool DisparityNormalizedProcessor::OnProcess(
Object *const in, Object *const out, Processor *const parent) { Object *const in, Object *const out,
std::shared_ptr<Processor> const parent) {
MYNTEYE_UNUSED(parent) MYNTEYE_UNUSED(parent)
const ObjMat *input = Object::Cast<ObjMat>(in); const ObjMat *input = Object::Cast<ObjMat>(in);
ObjMat *output = Object::Cast<ObjMat>(out); ObjMat *output = Object::Cast<ObjMat>(out);

View File

@ -33,7 +33,8 @@ class DisparityNormalizedProcessor : public Processor {
protected: protected:
Object *OnCreateOutput() override; Object *OnCreateOutput() override;
bool OnProcess( bool OnProcess(
Object *const in, Object *const out, Processor *const parent) override; Object *const in, Object *const out,
std::shared_ptr<Processor> const parent) override;
}; };
MYNTEYE_END_NAMESPACE MYNTEYE_END_NAMESPACE

View File

@ -66,7 +66,6 @@ DisparityProcessor::DisparityProcessor(DisparityProcessorType type,
#endif #endif
#ifdef WITH_BM_SOBEL_FILTER #ifdef WITH_BM_SOBEL_FILTER
} else if (type_ == DisparityProcessorType::BM) { } else if (type_ == DisparityProcessorType::BM) {
int bmWinSize = 3;
#ifdef WITH_OPENCV2 #ifdef WITH_OPENCV2
LOG(ERROR) << "not supported in opencv 2.x"; LOG(ERROR) << "not supported in opencv 2.x";
// int bmWinSize = 3; // int bmWinSize = 3;
@ -147,7 +146,8 @@ Object *DisparityProcessor::OnCreateOutput() {
} }
bool DisparityProcessor::OnProcess( bool DisparityProcessor::OnProcess(
Object *const in, Object *const out, Processor *const parent) { Object *const in, Object *const out,
std::shared_ptr<Processor> const parent) {
MYNTEYE_UNUSED(parent) MYNTEYE_UNUSED(parent)
const ObjMat2 *input = Object::Cast<ObjMat2>(in); const ObjMat2 *input = Object::Cast<ObjMat2>(in);
ObjMat *output = Object::Cast<ObjMat>(out); ObjMat *output = Object::Cast<ObjMat>(out);

View File

@ -49,7 +49,8 @@ class DisparityProcessor : public Processor {
protected: protected:
Object *OnCreateOutput() override; Object *OnCreateOutput() override;
bool OnProcess( bool OnProcess(
Object *const in, Object *const out, Processor *const parent) override; Object *const in, Object *const out,
std::shared_ptr<Processor> const parent) override;
private: private:
cv::Ptr<cv::StereoSGBM> sgbm_matcher; cv::Ptr<cv::StereoSGBM> sgbm_matcher;

View File

@ -73,7 +73,8 @@ Object *PointsProcessor::OnCreateOutput() {
} }
bool PointsProcessor::OnProcess( bool PointsProcessor::OnProcess(
Object *const in, Object *const out, Processor *const parent) { Object *const in, Object *const out,
std::shared_ptr<Processor> const parent) {
MYNTEYE_UNUSED(parent) MYNTEYE_UNUSED(parent)
float fx = calib_infos_->left.K[0]; float fx = calib_infos_->left.K[0];

View File

@ -38,7 +38,8 @@ class PointsProcessor : public Processor {
protected: protected:
Object *OnCreateOutput() override; Object *OnCreateOutput() override;
bool OnProcess( bool OnProcess(
Object *const in, Object *const out, Processor *const parent) override; Object *const in, Object *const out,
std::shared_ptr<Processor> const parent) override;
private: private:
std::shared_ptr<struct camera_calib_info_pair> calib_infos_; std::shared_ptr<struct camera_calib_info_pair> calib_infos_;

View File

@ -41,7 +41,8 @@ Object *PointsProcessorOCV::OnCreateOutput() {
} }
bool PointsProcessorOCV::OnProcess( bool PointsProcessorOCV::OnProcess(
Object *const in, Object *const out, Processor *const parent) { Object *const in, Object *const out,
std::shared_ptr<Processor> const parent) {
MYNTEYE_UNUSED(parent) MYNTEYE_UNUSED(parent)
const ObjMat *input = Object::Cast<ObjMat>(in); const ObjMat *input = Object::Cast<ObjMat>(in);
ObjMat *output = Object::Cast<ObjMat>(out); ObjMat *output = Object::Cast<ObjMat>(out);

View File

@ -35,7 +35,8 @@ class PointsProcessorOCV : public Processor {
protected: protected:
Object *OnCreateOutput() override; Object *OnCreateOutput() override;
bool OnProcess( bool OnProcess(
Object *const in, Object *const out, Processor *const parent) override; Object *const in, Object *const out,
std::shared_ptr<Processor> const parent) override;
private: private:
cv::Mat Q_; cv::Mat Q_;

View File

@ -417,7 +417,8 @@ Object *RectifyProcessor::OnCreateOutput() {
} }
bool RectifyProcessor::OnProcess( bool RectifyProcessor::OnProcess(
Object *const in, Object *const out, Processor *const parent) { Object *const in, Object *const out,
std::shared_ptr<Processor> const parent) {
MYNTEYE_UNUSED(parent) MYNTEYE_UNUSED(parent)
const ObjMat2 *input = Object::Cast<ObjMat2>(in); const ObjMat2 *input = Object::Cast<ObjMat2>(in);
ObjMat2 *output = Object::Cast<ObjMat2>(out); ObjMat2 *output = Object::Cast<ObjMat2>(out);

View File

@ -77,7 +77,8 @@ class RectifyProcessor : public Processor {
protected: protected:
Object *OnCreateOutput() override; Object *OnCreateOutput() override;
bool OnProcess( bool OnProcess(
Object *const in, Object *const out, Processor *const parent) override; Object *const in, Object *const out,
std::shared_ptr<Processor> const parent) override;
private: private:
void InitParams(IntrinsicsEquidistant in_left, void InitParams(IntrinsicsEquidistant in_left,

View File

@ -61,7 +61,8 @@ Object *RectifyProcessorOCV::OnCreateOutput() {
} }
bool RectifyProcessorOCV::OnProcess( bool RectifyProcessorOCV::OnProcess(
Object *const in, Object *const out, Processor *const parent) { Object *const in, Object *const out,
std::shared_ptr<Processor> const parent) {
MYNTEYE_UNUSED(parent) MYNTEYE_UNUSED(parent)
const ObjMat2 *input = Object::Cast<ObjMat2>(in); const ObjMat2 *input = Object::Cast<ObjMat2>(in);
ObjMat2 *output = Object::Cast<ObjMat2>(out); ObjMat2 *output = Object::Cast<ObjMat2>(out);

View File

@ -51,7 +51,8 @@ class RectifyProcessorOCV : public Processor {
protected: protected:
Object *OnCreateOutput() override; Object *OnCreateOutput() override;
bool OnProcess( bool OnProcess(
Object *const in, Object *const out, Processor *const parent) override; Object *const in, Object *const out,
std::shared_ptr<Processor> const parent) override;
private: private:
void InitParams(IntrinsicsPinhole in_left, void InitParams(IntrinsicsPinhole in_left,

View File

@ -809,7 +809,8 @@ void Synthetic::ProcessNativeStream(
} }
bool Synthetic::OnRectifyProcess( bool Synthetic::OnRectifyProcess(
Object *const in, Object *const out, Processor *const parent) { Object *const in, Object *const out,
std::shared_ptr<Processor> const parent) {
MYNTEYE_UNUSED(parent) MYNTEYE_UNUSED(parent)
if (plugin_ && plugin_->OnRectifyProcess(in, out)) { if (plugin_ && plugin_->OnRectifyProcess(in, out)) {
return true; return true;
@ -819,7 +820,8 @@ bool Synthetic::OnRectifyProcess(
} }
bool Synthetic::OnDisparityProcess( bool Synthetic::OnDisparityProcess(
Object *const in, Object *const out, Processor *const parent) { Object *const in, Object *const out,
std::shared_ptr<Processor> const parent) {
MYNTEYE_UNUSED(parent) MYNTEYE_UNUSED(parent)
if (plugin_ && plugin_->OnDisparityProcess(in, out)) { if (plugin_ && plugin_->OnDisparityProcess(in, out)) {
return true; return true;
@ -828,7 +830,8 @@ bool Synthetic::OnDisparityProcess(
} }
bool Synthetic::OnDisparityNormalizedProcess( bool Synthetic::OnDisparityNormalizedProcess(
Object *const in, Object *const out, Processor *const parent) { Object *const in, Object *const out,
std::shared_ptr<Processor> const parent) {
MYNTEYE_UNUSED(parent) MYNTEYE_UNUSED(parent)
if (plugin_ && plugin_->OnDisparityNormalizedProcess(in, out)) { if (plugin_ && plugin_->OnDisparityNormalizedProcess(in, out)) {
return true; return true;
@ -837,7 +840,8 @@ bool Synthetic::OnDisparityNormalizedProcess(
} }
bool Synthetic::OnPointsProcess( bool Synthetic::OnPointsProcess(
Object *const in, Object *const out, Processor *const parent) { Object *const in, Object *const out,
std::shared_ptr<Processor> const parent) {
MYNTEYE_UNUSED(parent) MYNTEYE_UNUSED(parent)
if (plugin_ && plugin_->OnPointsProcess(in, out)) { if (plugin_ && plugin_->OnPointsProcess(in, out)) {
return true; return true;
@ -846,7 +850,8 @@ bool Synthetic::OnPointsProcess(
} }
bool Synthetic::OnDepthProcess( bool Synthetic::OnDepthProcess(
Object *const in, Object *const out, Processor *const parent) { Object *const in, Object *const out,
std::shared_ptr<Processor> const parent) {
MYNTEYE_UNUSED(parent) MYNTEYE_UNUSED(parent)
if (plugin_ && plugin_->OnDepthProcess(in, out)) { if (plugin_ && plugin_->OnDepthProcess(in, out)) {
return true; return true;

View File

@ -88,15 +88,20 @@ class Synthetic {
void ProcessNativeStream(const Stream &stream, const api::StreamData &data); void ProcessNativeStream(const Stream &stream, const api::StreamData &data);
bool OnRectifyProcess( bool OnRectifyProcess(
Object *const in, Object *const out, Processor *const parent); Object *const in, Object *const out,
std::shared_ptr<Processor> const parent);
bool OnDisparityProcess( bool OnDisparityProcess(
Object *const in, Object *const out, Processor *const parent); Object *const in, Object *const out,
std::shared_ptr<Processor> const parent);
bool OnDisparityNormalizedProcess( bool OnDisparityNormalizedProcess(
Object *const in, Object *const out, Processor *const parent); Object *const in, Object *const out,
std::shared_ptr<Processor> const parent);
bool OnPointsProcess( bool OnPointsProcess(
Object *const in, Object *const out, Processor *const parent); Object *const in, Object *const out,
std::shared_ptr<Processor> const parent);
bool OnDepthProcess( bool OnDepthProcess(
Object *const in, Object *const out, Processor *const parent); Object *const in, Object *const out,
std::shared_ptr<Processor> const parent);
void OnRectifyPostProcess(Object *const out); void OnRectifyPostProcess(Object *const out);
void OnDisparityPostProcess(Object *const out); void OnDisparityPostProcess(Object *const out);