refactor(synthetic): add SyntheticProcessorPart class

This commit is contained in:
TinyOh 2019-01-22 11:30:34 +08:00
parent 69168303d6
commit 18ffd0372b
6 changed files with 483 additions and 3 deletions

View File

@ -232,6 +232,7 @@ if(WITH_API)
src/mynteye/api/synthetic.cc src/mynteye/api/synthetic.cc
src/mynteye/api/processor/disparity_processor.cc src/mynteye/api/processor/disparity_processor.cc
src/mynteye/api/processor/disparity_normalized_processor.cc src/mynteye/api/processor/disparity_normalized_processor.cc
src/mynteye/api/processor/root_camera_processor.cc
src/mynteye/api/processor/points_processor_ocv.cc src/mynteye/api/processor/points_processor_ocv.cc
src/mynteye/api/processor/depth_processor_ocv.cc src/mynteye/api/processor/depth_processor_ocv.cc
src/mynteye/api/processor/rectify_processor_ocv.cc src/mynteye/api/processor/rectify_processor_ocv.cc

View File

@ -24,7 +24,7 @@
#include <string> #include <string>
#include <thread> #include <thread>
#include <vector> #include <vector>
// #include "mynteye/api/synthetic.h" #include "mynteye/api/synthetic.h"
#include "mynteye/mynteye.h" #include "mynteye/mynteye.h"
#include "mynteye/api/object.h" #include "mynteye/api/object.h"
@ -32,7 +32,8 @@
MYNTEYE_BEGIN_NAMESPACE MYNTEYE_BEGIN_NAMESPACE
class Processor : class Processor :
public std::enable_shared_from_this<Processor> { public std::enable_shared_from_this<Processor>,
public SyntheticProcessorPart {
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)>;

View File

@ -0,0 +1,53 @@
// Copyright 2018 Slightech Co., Ltd. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "mynteye/api/processor/root_camera_processor.h"
#include <utility>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "mynteye/logger.h"
MYNTEYE_BEGIN_NAMESPACE
const char RootProcessor::NAME[] = "RootProcessor";
RootProcessor::RootProcessor(std::int32_t proc_period)
: Processor(std::move(proc_period)) {
// todo
}
RootProcessor::~RootProcessor() {
VLOG(2) << __func__;
}
std::string RootProcessor::Name() {
return NAME;
}
Object *RootProcessor::OnCreateOutput() {
return new ObjMat2();
}
bool RootProcessor::OnProcess(
Object *const in, Object *const out,
std::shared_ptr<Processor> const parent) {
MYNTEYE_UNUSED(parent)
// const ObjMat2 *input = Object::Cast<ObjMat2>(in);
// ObjMat2 *output = Object::Cast<ObjMat2>(out);
// cv::remap(input->first, output->first, map11, map12, cv::INTER_LINEAR);
// cv::remap(input->second, output->second, map21, map22, cv::INTER_LINEAR);
// output->first_id = input->first_id;
// output->first_data = input->first_data;
// output->second_id = input->second_id;
// output->second_data = input->second_data;
return true;
}
MYNTEYE_END_NAMESPACE

View File

@ -0,0 +1,45 @@
// Copyright 2018 Slightech Co., Ltd. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef MYNTEYE_API_PROCESSOR_ROOT_CAMERA_PROCESSOR_H_
#define MYNTEYE_API_PROCESSOR_ROOT_CAMERA_PROCESSOR_H_
#pragma once
#include <string>
#include <opencv2/core/core.hpp>
#include "mynteye/api/processor.h"
#include "mynteye/logger.h"
MYNTEYE_BEGIN_NAMESPACE
class RootProcessor : public Processor {
public:
static const char NAME[];
explicit RootProcessor(std::int32_t proc_period = 0);
virtual ~RootProcessor();
std::string Name() override;
protected:
Object *OnCreateOutput() override;
bool OnProcess(
Object *const in, Object *const out,
std::shared_ptr<Processor> const parent) override;
};
MYNTEYE_END_NAMESPACE
#endif // MYNTEYE_API_PROCESSOR_ROOT_CAMERA_PROCESSOR_H_

View File

@ -25,6 +25,7 @@
#include "mynteye/api/processor.h" #include "mynteye/api/processor.h"
#include "mynteye/api/processor/disparity_normalized_processor.h" #include "mynteye/api/processor/disparity_normalized_processor.h"
#include "mynteye/api/processor/disparity_processor.h" #include "mynteye/api/processor/disparity_processor.h"
#include "mynteye/api/processor/root_camera_processor.h"
#include "mynteye/api/processor/rectify_processor_ocv.h" #include "mynteye/api/processor/rectify_processor_ocv.h"
#include "mynteye/api/processor/depth_processor_ocv.h" #include "mynteye/api/processor/depth_processor_ocv.h"
#include "mynteye/api/processor/points_processor_ocv.h" #include "mynteye/api/processor/points_processor_ocv.h"
@ -107,8 +108,8 @@ Synthetic::Synthetic(API *api, CalibrationModel calib_model)
VLOG(2) << __func__; VLOG(2) << __func__;
CHECK_NOTNULL(api_); CHECK_NOTNULL(api_);
InitCalibInfo(); InitCalibInfo();
InitStreamSupports();
InitProcessors(); InitProcessors();
InitStreamSupports();
} }
Synthetic::~Synthetic() { Synthetic::~Synthetic() {
@ -142,6 +143,96 @@ void Synthetic::NotifyImageParamsChanged() {
} }
} }
const struct Synthetic::stream_control_t Synthetic::getControlDateWithStream(
const Stream& stream) const {
for (auto &&it : processors_) {
for (auto it_s : it->getTargetStreams()) {
if (it_s.stream == stream) {
return it_s;
}
}
}
LOG(ERROR) << "ERROR: no suited processor for stream "<< stream;
return {};
}
void Synthetic::setControlDateCallbackWithStream(
const struct stream_control_t& ctr_data) {
for (auto &&it : processors_) {
int i = 0;
for (auto it_s : it->getTargetStreams()) {
if (it_s.stream == ctr_data.stream) {
it->target_streams_[i].stream_callback = ctr_data.stream_callback;
return;
}
i++;
}
}
LOG(ERROR) << "ERROR: no suited processor for stream "<< ctr_data.stream;
}
void Synthetic::setControlDateModeWithStream(
const struct stream_control_t& ctr_data) {
for (auto &&it : processors_) {
int i = 0;
for (auto it_s : it->getTargetStreams()) {
if (it_s.stream == ctr_data.stream) {
it->target_streams_[i].mode = ctr_data.mode;
return;
}
i++;
}
}
LOG(ERROR) << "ERROR: no suited processor for stream "<< ctr_data.stream;
}
bool Synthetic::checkControlDateWithStream(const Stream& stream) const {
for (auto &&it : processors_) {
for (auto it_s : it->getTargetStreams()) {
if (it_s.stream == stream) {
return true;
}
}
}
return stream == Stream::LEFT || stream == Stream::RIGHT;
}
// bool Synthetic::Supports(const Stream &stream) const {
// return checkControlDateWithStream(stream);
// }
Synthetic::status_mode_t Synthetic::GetStreamStatusMode(
const Stream &stream) const {
if (checkControlDateWithStream(stream)) {
auto ctrData = getControlDateWithStream(stream);
return ctrData.mode;
} else {
return MODE_STATUS_LAST;
}
}
// void Synthetic::EnableStreamData(const Stream &stream) {
// // Activate processors of synthetic stream
// auto processor = getProcessorWithStream(stream);
// iterate_processors_CtoP_before(processor,
// [](std::shared_ptr<Processor> proce){
// auto streams = proce->getTargetStreams();
// int act_tag = 0;
// for (unsigned int i = 0; i < proce->getStreamsSum() ; i++) {
// if (proce->target_streams_[i].mode == MODE_STATUS_DISABLE) {
// act_tag++;
// proce->target_streams_[i].mode = MODE_STATUS_ENABLE;
// }
// }
// if (act_tag > 0) {
// std::cout << proce->Name() << "active" << std::endl;
// proce->Activate();
// }
// // std::cout <<std::endl;
// });
// }
bool Synthetic::Supports(const Stream &stream) const { bool Synthetic::Supports(const Stream &stream) const {
return stream_supports_mode_.find(stream) != stream_supports_mode_.end(); return stream_supports_mode_.find(stream) != stream_supports_mode_.end();
} }
@ -659,7 +750,25 @@ void Synthetic::InitProcessors() {
} else { } else {
depth_processor = std::make_shared<DepthProcessorOCV>(DEPTH_PROC_PERIOD); depth_processor = std::make_shared<DepthProcessorOCV>(DEPTH_PROC_PERIOD);
} }
rectify_processor->addTargetStreams({Stream::LEFT_RECTIFIED, StatusMode::MODE_STATUS_LAST, Mode::MODE_LAST, Mode::MODE_LAST, nullptr});
rectify_processor->addTargetStreams({Stream::RIGHT_RECTIFIED, StatusMode::MODE_STATUS_LAST, Mode::MODE_LAST, Mode::MODE_LAST, nullptr});
disparity_processor->addTargetStreams({Stream::DISPARITY, StatusMode::MODE_STATUS_LAST, Mode::MODE_LAST, Mode::MODE_LAST, nullptr});
disparitynormalized_processor->addTargetStreams({Stream::DISPARITY_NORMALIZED, StatusMode::MODE_STATUS_LAST, Mode::MODE_LAST, Mode::MODE_LAST, nullptr});
points_processor->addTargetStreams({Stream::POINTS, StatusMode::MODE_STATUS_LAST, Mode::MODE_LAST, Mode::MODE_LAST, nullptr});
depth_processor->addTargetStreams({Stream::DEPTH, StatusMode::MODE_STATUS_LAST, Mode::MODE_LAST, Mode::MODE_LAST, nullptr});
auto root_processor =
std::make_shared<RootProcessor>(RECTIFY_PROC_PERIOD);
root_processor->addTargetStreams({Stream::LEFT, StatusMode::MODE_STATUS_LAST, Mode::MODE_LAST, Mode::MODE_LAST, nullptr});
root_processor->addTargetStreams({Stream::RIGHT, StatusMode::MODE_STATUS_LAST, Mode::MODE_LAST, Mode::MODE_LAST, nullptr});
root_processor->AddChild(rectify_processor);
processors_.push_back(root_processor);
processors_.push_back(rectify_processor);
processors_.push_back(disparity_processor);
processors_.push_back(disparitynormalized_processor);
processors_.push_back(points_processor);
processors_.push_back(depth_processor);
using namespace std::placeholders; // NOLINT using namespace std::placeholders; // NOLINT
rectify_processor->SetProcessCallback( rectify_processor->SetProcessCallback(
std::bind(&Synthetic::OnRectifyProcess, this, _1, _2, _3)); std::bind(&Synthetic::OnRectifyProcess, this, _1, _2, _3));

View File

@ -41,6 +41,21 @@ class Synthetic {
MODE_LAST // Unsupported MODE_LAST // Unsupported
} mode_t; } mode_t;
typedef enum StatusMode {
MODE_STATUS_NATIVE, // Native stream
MODE_STATUS_DISABLE, // Synthetic stream
MODE_STATUS_ENABLE, // Synthetic stream
MODE_STATUS_LAST // Unsupported
} status_mode_t;
struct stream_control_t {
Stream stream;
status_mode_t mode;
mode_t support_mode_;
mode_t enabled_mode_;
stream_callback_t stream_callback;
};
explicit Synthetic(API *api, CalibrationModel calib_model); explicit Synthetic(API *api, CalibrationModel calib_model);
~Synthetic(); ~Synthetic();
@ -67,6 +82,12 @@ class Synthetic {
void SetPlugin(std::shared_ptr<Plugin> plugin); void SetPlugin(std::shared_ptr<Plugin> plugin);
bool HasPlugin() const; bool HasPlugin() const;
const struct stream_control_t getControlDateWithStream(const Stream& stream) const;
void setControlDateCallbackWithStream(const struct stream_control_t& ctr_data);
void setControlDateModeWithStream(const struct stream_control_t& ctr_data);
bool checkControlDateWithStream(const Stream& stream) const;
status_mode_t GetStreamStatusMode(const Stream &stream) const;
private: private:
void InitCalibInfo(); void InitCalibInfo();
void InitStreamSupports(); void InitStreamSupports();
@ -126,6 +147,23 @@ class Synthetic {
std::shared_ptr<IntrinsicsBase> intr_right_; std::shared_ptr<IntrinsicsBase> intr_right_;
std::shared_ptr<Extrinsics> extr_; std::shared_ptr<Extrinsics> extr_;
bool calib_default_tag_; bool calib_default_tag_;
std::vector<std::shared_ptr<Processor>> processors_;
};
class SyntheticProcessorPart {
private:
inline std::vector<Synthetic::stream_control_t> getTargetStreams() {
return target_streams_;
}
inline Stream addTargetStreams(const Synthetic::stream_control_t& strm) {
target_streams_.push_back(strm);
}
std::vector<Synthetic::stream_control_t> target_streams_;
inline unsigned int getStreamsSum() {return target_streams_.size();}
friend Synthetic;
}; };
template <class T, class P> template <class T, class P>
@ -186,3 +224,236 @@ bool Synthetic::DeactivateProcessor(bool childs) {
MYNTEYE_END_NAMESPACE MYNTEYE_END_NAMESPACE
#endif // MYNTEYE_API_SYNTHETIC_H_ #endif // MYNTEYE_API_SYNTHETIC_H_
/*
// Copyright 2018 Slightech Co., Ltd. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef MYNTEYE_API_SYNTHETIC_H_
#define MYNTEYE_API_SYNTHETIC_H_
#pragma once
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "mynteye/api/api.h"
#include "mynteye/api/config.h"
// #include "mynteye/api/processor.h"
MYNTEYE_BEGIN_NAMESPACE
class API;
class Plugin;
class Processor;
struct Object;
class Synthetic {
public:
using stream_callback_t = API::stream_callback_t;
typedef enum Mode {
MODE_NATIVE, // Native stream
MODE_SYNTHETIC, // Synthetic stream
MODE_LAST // Unsupported
} mode_t;
typedef enum StatusMode {
MODE_STATUS_NATIVE, // Native stream
MODE_STATUS_DISABLE, // Synthetic stream
MODE_STATUS_ENABLE, // Synthetic stream
MODE_STATUS_LAST // Unsupported
} status_mode_t;
struct stream_control_t {
Stream stream;
status_mode_t mode;
stream_callback_t stream_callback;
};
explicit Synthetic(API *api, CalibrationModel calib_model);
~Synthetic();
void NotifyImageParamsChanged();
bool Supports(const Stream &stream) const;
void EnableStreamData(const Stream &stream);
void DisableStreamData(const Stream &stream);
bool IsStreamDataEnabled(const Stream &stream) const;
bool IsStreamDataNative(const Stream &stream) const;
void SetStreamCallback(const Stream &stream, stream_callback_t callback);
bool HasStreamCallback(const Stream &stream) const;
void StartVideoStreaming();
void StopVideoStreaming();
void WaitForStreams();
api::StreamData GetStreamData(const Stream &stream);
std::vector<api::StreamData> GetStreamDatas(const Stream &stream);
void SetPlugin(std::shared_ptr<Plugin> plugin);
bool HasPlugin() const;
private:
void InitCalibInfo();
void InitStreamSupports();
status_mode_t GetStreamStatusMode(const Stream &stream) const;
std::shared_ptr<Processor> getProcessorWithStream(
const Stream& stream);
// mode_t GetStreamEnabledMode(const Stream &stream) const;
bool IsStreamEnabledNative(const Stream &stream) const;
bool IsStreamEnabledSynthetic(const Stream &stream) const;
// void EnableStreamData(const Stream &stream, std::uint32_t depth);
// void DisableStreamData(const Stream &stream, std::uint32_t depth);
void InitProcessors();
template <class T>
bool ActivateProcessor();
template <class T>
bool DeactivateProcessor();
void ProcessNativeStream(const Stream &stream, const api::StreamData &data);
bool OnRectifyProcess(
Object *const in, Object *const out,
std::shared_ptr<Processor> const parent);
bool OnDisparityProcess(
Object *const in, Object *const out,
std::shared_ptr<Processor> const parent);
bool OnDisparityNormalizedProcess(
Object *const in, Object *const out,
std::shared_ptr<Processor> const parent);
bool OnPointsProcess(
Object *const in, Object *const out,
std::shared_ptr<Processor> const parent);
bool OnDepthProcess(
Object *const in, Object *const out,
std::shared_ptr<Processor> const parent);
void OnRectifyPostProcess(Object *const out);
void OnDisparityPostProcess(Object *const out);
void OnDisparityNormalizedPostProcess(Object *const out);
void OnPointsPostProcess(Object *const out);
void OnDepthPostProcess(Object *const out);
const struct stream_control_t
getControlDateWithStream(const Stream& stream) const;
void setControlDateModeWithStream(const struct stream_control_t& ctr_data);
void setControlDateCallbackWithStream(
const struct stream_control_t& ctr_data);
bool checkControlDateWithStream(const Stream& stream) const;
std::vector<std::shared_ptr<Processor>> processors_;
API *api_;
std::map<Stream, mode_t> stream_supports_mode_;
std::map<Stream, mode_t> stream_enabled_mode_;
std::map<Stream, stream_callback_t> stream_callbacks_;
std::shared_ptr<Processor> processor_;
std::shared_ptr<Plugin> plugin_;
CalibrationModel calib_model_;
std::shared_ptr<IntrinsicsBase> intr_left_;
std::shared_ptr<IntrinsicsBase> intr_right_;
std::shared_ptr<Extrinsics> extr_;
bool calib_default_tag_;
};
class SyntheticProcessorPart {
private:
inline std::vector<Synthetic::stream_control_t> getTargetStreams() {
return target_streams_;
}
inline Stream addTargetStreams(const Synthetic::stream_control_t& strm) {
target_streams_.push_back(strm);
}
std::vector<Synthetic::stream_control_t> target_streams_;
inline unsigned int getStreamsSum() {return target_streams_.size();}
friend Synthetic;
};
template <class T, class P>
std::shared_ptr<T> find_processor(const P &processor) {
return find_processor<T>(processor, T::NAME);
}
template <class T, class P>
std::shared_ptr<T> find_processor(const P &processor, const std::string &name) {
if (processor->Name() == name) {
return std::dynamic_pointer_cast<T>(processor);
}
auto &&childs = processor->GetChilds();
return find_processor<T>(std::begin(childs), std::end(childs), name);
}
template <class T, class InputIt>
std::shared_ptr<T> find_processor(
InputIt first, InputIt last, const std::string &name) {
if (first == last)
return nullptr;
for (auto it = first; it != last; ++it) {
if ((*it)->Name() == name) {
return std::dynamic_pointer_cast<T>(*it);
}
}
for (auto it = first; it != last; ++it) {
auto &&childs = (*it)->GetChilds();
if (childs.empty())
continue;
auto &&result =
find_processor<T>(std::begin(childs), std::end(childs), name);
if (result == nullptr)
continue;
return result;
}
return nullptr;
}
template <class T>
bool Synthetic::ActivateProcessor() {
auto &&processor = find_processor<T>(processor_);
if (processor == nullptr)
return false;
processor->Activate();
return true;
}
template <class T>
bool Synthetic::DeactivateProcessor() {
auto &&processor = find_processor<T>(processor_);
if (processor == nullptr)
return false;
processor->Deactivate();
return true;
}
MYNTEYE_END_NAMESPACE
#endif // MYNTEYE_API_SYNTHETIC_H_
*/