image_framework_ymj/image_framework/plugins/MsgBase.cpp
2024-12-06 16:25:16 +08:00

160 lines
4.3 KiB
C++
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "MsgBase.h"
#include "LibapiThread.h"
#include "Libapi2D3D.h"
// for test
MsgBase* MsgBase::msg_ptr1 = nullptr;
FuncCallback MsgBase::pCallback = nullptr;
bool MsgBase::bAllRoisConnersCheckIsEnd = false;
bool MsgBase::bCloudProjectIsOk = false;
int MsgBase::iCameraToLidarMsg = CLIENT_MSG_NONE; // 2D第一阶段处理结束0无回复 1继续执行 2重新检测 3停止检测
int MsgBase::iClinetMsg = CLIENT_MSG_NONE; // 客户端确认0无回复 1继续执行 2重新检测 3停止检测
static std::mutex s_mutex;
void MsgBase::SetMsgPtr1(MsgBase* msg)
{
MsgBase::msg_ptr1 = msg;
};
MsgBase* MsgBase::GetMsgPtr1()
{
return MsgBase::msg_ptr1;
};
void MsgBase::SetCallBack(FuncCallback f)
{
MsgBase::pCallback = f;
}
void MsgBase::Callback(void* data)
{
if (MsgBase::pCallback != nullptr)
MsgBase::pCallback(data);
else assert(0);
}
void MsgBase::Callback(int code, std::string errorInfo)
{
CallbackInfo cbInfo;
cbInfo.code = code;
memset(cbInfo.errorInfo, 0, sizeof(cbInfo.errorInfo));
memcpy(cbInfo.errorInfo, errorInfo.c_str(), errorInfo.size());
cbInfo.bGetData = false;
if (MsgBase::pCallback != nullptr)
MsgBase::pCallback(&cbInfo);
//else assert(0);
}
void MsgBase::CallbackWithData(int code, std::string errorInfo)
{
CallbackInfo cbInfo;
cbInfo.code = code;
memset(cbInfo.errorInfo, 0, sizeof(cbInfo.errorInfo));
memcpy(cbInfo.errorInfo, errorInfo.c_str(), errorInfo.size());
cbInfo.bGetData = true;
CMeasureInfo* measureInfo = CMeasureInfo::Get();
std::vector<std::shared_ptr<CSubRoi>> rois = measureInfo->GetSubRois();
cbInfo.roi_size = rois.size();
std::cout << "=====================" << rois.size() << std::endl;
for (int i = 0; i < rois.size(); ++i)
{
std::shared_ptr<CSubRoi> roi = rois[i];
SubRoiData* cb_roi = &cbInfo.subRois[i];
for (int n = 0; n < (roi.get()->H).total(); ++n)
cb_roi->H[n] = (roi.get()->H).at<float>(0, n);
for (int n = 0; n < roi.get()->mRoiPoints.size(); ++n)
{
cv::Point2f p2f = roi.get()->mRoiPoints[n];
cb_roi->mRoiPoints[n * 2] = p2f.x;
cb_roi->mRoiPoints[n * 2 + 1] = p2f.y;
}
for (int n = 0; n < roi.get()->mInnerConners.size(); ++n)
{
cv::Point2f p2f = roi.get()->mInnerConners[n];
cb_roi->mInnerConners[n * 2] = p2f.x;
cb_roi->mInnerConners[n * 2 + 1] = p2f.y;
}
for (int n = 0; n < roi.get()->mInnerConners3D.size(); ++n)
{
cv::Point3f p3f = roi.get()->mInnerConners3D[n];
cb_roi->mInnerConners3D[n * 3] = p3f.x;
cb_roi->mInnerConners3D[n * 3 + 1] = p3f.y;
cb_roi->mInnerConners3D[n * 3 + 2] = p3f.z;
}
//for (int n = 0; n < roi.get()->mInnerConners3D.size(); ++n)
//{
//}
}
if (MsgBase::pCallback != nullptr)
MsgBase::pCallback(&cbInfo);
}
void MsgBase::WaitBool(bool& b)
{
while (true)
{
LibapiThread::delay_second(0.05);
if (b)
{
b = false;
break;
}
}
}
void MsgBase::SetWaitBool(bool& b)
{
b = true;
}
int MsgBase::WaitInt(int& val)
{
int rst = 0;
while (true)
{
LibapiThread::delay_second(0.05);
std::unique_lock<std::mutex> lock(s_mutex);
if (val != 0)
{
rst = val;
val = 0;
break;
}
}
return rst;
}
void MsgBase::SetWaitInt(int& it, int val)
{
std::unique_lock<std::mutex> lock(s_mutex);
it = val;
}
// 待优化
std::string MsgBase::GetCodeInfo(int code)
{
if (code == CAL_OK) return "success";
else if (code == TO_CLIENT_NOTIFY_CAMERA_CAP_START) return "开始采集数据";
else if (code == TO_CLIENT_NOTIFY_CAMERA_CAP_END) return "采集数据已完成";
else if (code == TO_CLIENT_NOTIFY_DETECTION_START) return "开始识别预埋件";
else if (code == TO_CLIENT_NOTIFY_DETECTION_END) return "识别预埋件已完成";
else if (code == TO_CLIENT_NOTIFY_CONNER2D_START) return "开始检测角点";
else if (code == TO_CLIENT_NOTIFY_CONNER2D_END) return "检测角点已完成";
else if (code == TO_CLIENT_NOTIFY_CONNER3D_START) return "开始计算角点3D数据";
else if (code == TO_CLIENT_NOTIFY_CONNER3D_END) return "角点3D数据计算已完成";
else if (code == TO_CLIENT_NOTIFY_MEASURE_START) return "开始计算预埋件数据";
else if (code == TO_CLIENT_NOTIFY_MEASURE_END) return "计算预埋件数据已完成";
else return "未定义事件";
}