441 lines
14 KiB
C++
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.

#ifndef IVRCONFIG_H
#define IVRCONFIG_H
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>
#include <cstring>
/**
* @brief 项目类型枚举
*/
enum class ProjectType
{
Workpiece = 0, // 激光焊接
DirectBag = 1, // 带方向的编织袋
};
/**
* @brief 项目类型字符串转换函数
*/
inline std::string ProjectTypeToString(ProjectType type)
{
switch (type) {
case ProjectType::Workpiece:
return "Workpiece";
case ProjectType::DirectBag:
return "DirectBag";
default:
return "Unknown";
}
}
/**
* @brief 字符串转项目类型函数
*/
inline ProjectType StringToProjectType(const std::string& str)
{
if (str == "Workpiece" || str == "0") {
return ProjectType::Workpiece;
} else if (str == "DirectBag" || str == "1") {
return ProjectType::DirectBag;
} else {
return ProjectType::Workpiece; // 默认返回激光焊接
}
}
struct DeviceInfo
{
std::string name;
std::string ip;
// 显式赋值构造函数,确保 std::string 正确复制
DeviceInfo& operator=(const DeviceInfo& other) {
if (this != &other) {
name = other.name;
ip = other.ip;
}
return *this;
}
// 显式复制构造函数
DeviceInfo(const DeviceInfo& other)
: name(other.name)
, ip(other.ip) {
}
// 默认构造函数
DeviceInfo() = default;
};
/**
* @brief 串口配置信息
*/
struct SerialConfig
{
#ifdef _WIN32
std::string portName = "COM6"; // 串口名称
#else
std::string portName = "/dev/ttyS3"; // 串口名称
#endif
int baudRate = 115200; // 波特率
int dataBits = 8; // 数据位
int stopBits = 1; // 停止位
int parity = 0; // 校验位 (0-无校验, 1-奇校验, 2-偶校验)
int flowControl = 0; // 流控制 (0-无, 1-硬件, 2-软件)
bool enabled = true; // 是否启用串口通信
// 显式赋值构造函数,确保 std::string 正确复制
SerialConfig& operator=(const SerialConfig& other) {
if (this != &other) {
portName = other.portName;
baudRate = other.baudRate;
dataBits = other.dataBits;
stopBits = other.stopBits;
parity = other.parity;
flowControl = other.flowControl;
enabled = other.enabled;
}
return *this;
}
// 显式复制构造函数
SerialConfig(const SerialConfig& other)
: portName(other.portName)
, baudRate(other.baudRate)
, dataBits(other.dataBits)
, stopBits(other.stopBits)
, parity(other.parity)
, flowControl(other.flowControl)
, enabled(other.enabled) {
}
// 默认构造函数
SerialConfig() = default;
};
/**
* @brief 离群点滤波参数
*/
struct VrOutlierFilterParam
{
double continuityTh = 20.0; // 连续性阈值
double outlierTh = 5.0; // 离群点判断阈值
};
/**
* @brief 拐角参数
*/
struct VrCornerParam
{
double cornerTh = 60; // 拐角阈值
double scale = 50; // 计算方向角的窗口比例因子
double minEndingGap = 20; // Y方向最小结束间隔
double minEndingGap_z = 20; // Z方向最小结束间隔
double jumpCornerTh_1 = 10; // 跳跃拐角阈值1
double jumpCornerTh_2 = 60; // 跳跃拐角阈值2
};
/**
* @brief 树生长参数
*/
struct VrTreeGrowParam
{
int maxLineSkipNum = 10; // 生长时允许跳过的最大线条数
double yDeviation_max = 10.0; // 生长时允许的最大Y偏差
double maxSkipDistance = 10.0; // 最大跳跃距离
double zDeviation_max = 10; // 生长时允许的最大Z偏差
double minLTypeTreeLen = 100.0; // L型树的最小长度
double minVTypeTreeLen = 100.0; // V型树的最小长度
};
/**
* @brief 激光焊接参数
*/
struct VrWorkpieceParam
{
double lineLen = 180.0; // 工件角点提取:直线段长度阈值
};
/**
* @brief 单个相机的平面校准参数
*/
struct VrCameraPlaneCalibParam
{
int cameraIndex = 1; // 相机索引1-based
std::string cameraName = ""; // 相机名称
double planeCalib[9] = {1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0}; // 旋转矩阵,将数据调平(默认单位矩阵)
double planeHeight = -1.0; // 参考平面的高度,用于去除地面数据
double invRMatrix[9] = {1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0}; // 逆旋转矩阵,回到原坐标系(默认单位矩阵)
bool isCalibrated = false; // 是否已经校准
// 显式赋值构造函数,确保 std::string 正确复制
VrCameraPlaneCalibParam& operator=(const VrCameraPlaneCalibParam& other) {
if (this != &other) {
cameraIndex = other.cameraIndex;
cameraName = other.cameraName;
memcpy(planeCalib, other.planeCalib, sizeof(planeCalib));
planeHeight = other.planeHeight;
memcpy(invRMatrix, other.invRMatrix, sizeof(invRMatrix));
isCalibrated = other.isCalibrated;
}
return *this;
}
// 显式复制构造函数
VrCameraPlaneCalibParam(const VrCameraPlaneCalibParam& other)
: cameraIndex(other.cameraIndex)
, cameraName(other.cameraName)
, planeHeight(other.planeHeight)
, isCalibrated(other.isCalibrated) {
memcpy(planeCalib, other.planeCalib, sizeof(planeCalib));
memcpy(invRMatrix, other.invRMatrix, sizeof(invRMatrix));
}
// 默认构造函数
VrCameraPlaneCalibParam() = default;
};
/**
* @brief 平面校准参数(支持多相机)
*/
struct VrPlaneCalibParam
{
std::vector<VrCameraPlaneCalibParam> cameraCalibParams; // 各个相机的校准参数
// 获取指定相机的校准参数C++11兼容方式返回bool通过引用参数传出结果
// 返回true表示找到该相机的参数false表示未找到
bool GetCameraCalibParam(int cameraIndex, VrCameraPlaneCalibParam& outParam) const {
for (const auto& param : cameraCalibParams) {
if (param.cameraIndex == cameraIndex) {
outParam = param; // 返回一个副本
return true;
}
}
return false;
}
// 设置或更新指定相机的校准参数
void SetCameraCalibParam(const VrCameraPlaneCalibParam& param) {
for (auto& existingParam : cameraCalibParams) {
if (existingParam.cameraIndex == param.cameraIndex) {
existingParam = param;
return;
}
}
// 如果不存在,则添加新的
cameraCalibParams.push_back(param);
}
// 移除指定相机的校准参数
void RemoveCameraCalibParam(int cameraIndex) {
cameraCalibParams.erase(
std::remove_if(cameraCalibParams.begin(), cameraCalibParams.end(),
[cameraIndex](const VrCameraPlaneCalibParam& param) {
return param.cameraIndex == cameraIndex;
}),
cameraCalibParams.end());
}
// 显式赋值构造函数
VrPlaneCalibParam& operator=(const VrPlaneCalibParam& other) {
if (this != &other) {
cameraCalibParams = other.cameraCalibParams;
}
return *this;
}
// 显式复制构造函数
VrPlaneCalibParam(const VrPlaneCalibParam& other)
: cameraCalibParams(other.cameraCalibParams) {
}
// 默认构造函数
VrPlaneCalibParam() = default;
};
/**
* @brief 调试参数
*/
struct VrDebugParam
{
bool enableDebug = true; // 是否开启调试模式
bool savePointCloud = false; // 是否保存点云数据
bool saveDebugImage = false; // 是否保存调试图像
bool printDetailLog = true; // 是否打印详细日志
std::string debugOutputPath = ""; // 调试输出路径
// 显式赋值构造函数,确保 std::string 正确复制
VrDebugParam& operator=(const VrDebugParam& other) {
if (this != &other) {
enableDebug = other.enableDebug;
savePointCloud = other.savePointCloud;
saveDebugImage = other.saveDebugImage;
printDetailLog = other.printDetailLog;
debugOutputPath = other.debugOutputPath; // std::string 深拷贝
}
return *this;
}
// 显式复制构造函数
VrDebugParam(const VrDebugParam& other)
: enableDebug(other.enableDebug)
, savePointCloud(other.savePointCloud)
, saveDebugImage(other.saveDebugImage)
, printDetailLog(other.printDetailLog)
, debugOutputPath(other.debugOutputPath) {
}
// 默认构造函数
VrDebugParam() = default;
};
/**
* @brief 算法参数配置结构
*/
struct VrAlgorithmParams
{
VrOutlierFilterParam filterParam; // 离群点滤波参数
VrCornerParam cornerParam; // 拐角参数
VrTreeGrowParam growParam; // 树生长参数
VrPlaneCalibParam planeCalibParam; // 平面校准参数
VrWorkpieceParam workpieceParam; // 工件参数
// 显式赋值构造函数,确保正确的深拷贝
VrAlgorithmParams& operator=(const VrAlgorithmParams& other) {
if (this != &other) {
filterParam = other.filterParam;
cornerParam = other.cornerParam;
growParam = other.growParam;
planeCalibParam = other.planeCalibParam;
workpieceParam = other.workpieceParam;
}
return *this;
}
// 显式复制构造函数
VrAlgorithmParams(const VrAlgorithmParams& other)
: filterParam(other.filterParam)
, cornerParam(other.cornerParam)
, growParam(other.growParam)
, planeCalibParam(other.planeCalibParam)
, workpieceParam(other.workpieceParam) {
}
// 默认构造函数
VrAlgorithmParams() = default;
};
/**
* @brief 配置加载结果
*/
struct ConfigResult
{
std::vector<DeviceInfo> cameraList;
std::vector<DeviceInfo> deviceList;
VrAlgorithmParams algorithmParams; // 算法参数
VrDebugParam debugParam; // 调试参数
SerialConfig serialConfig; // 串口配置
ProjectType projectType; // 项目类型
// 显式赋值构造函数,确保正确的深拷贝
ConfigResult& operator=(const ConfigResult& other) {
if (this != &other) {
cameraList = other.cameraList;
deviceList = other.deviceList;
algorithmParams = other.algorithmParams;
debugParam = other.debugParam;
serialConfig = other.serialConfig;
projectType = other.projectType;
}
return *this;
}
// 显式复制构造函数
ConfigResult(const ConfigResult& other)
: cameraList(other.cameraList)
, deviceList(other.deviceList)
, algorithmParams(other.algorithmParams)
, debugParam(other.debugParam)
, serialConfig(other.serialConfig)
, projectType(other.projectType) {
}
// 默认构造函数
ConfigResult() = default;
};
/**
* @brief 配置加载错误代码
*/
enum LoadConfigErrorCode
{
LOAD_CONFIG_SUCCESS = 0, // 加载成功
LOAD_CONFIG_FILE_NOT_FOUND = -1, // 配置文件不存在
LOAD_CONFIG_PARSE_ERROR = -2, // 配置文件解析错误
LOAD_CONFIG_INVALID_FORMAT = -3, // 配置文件格式无效
LOAD_CONFIG_UNKNOWN_ERROR = -99 // 未知错误
};
/**
* @brief 配置改变通知接口
*/
class IVrConfigChangeNotify
{
public:
virtual ~IVrConfigChangeNotify() {}
/**
* @brief 配置数据改变通知
* @param configResult 新的配置数据
*/
virtual void OnConfigChanged(const ConfigResult& configResult) = 0;
};
/**
* @brief VrConfig接口类
*/
class IVrConfig
{
public:
/**
* @brief 虚析构函数
*/
virtual ~IVrConfig() {}
/**
* @brief 创建实例
* @return 实例
*/
static bool CreateInstance(IVrConfig** ppVrConfig);
/**
* @brief 加载配置文件
* @param filePath 配置文件路径
* @param configResult 输出参数,加载的配置结果
* @return 错误代码 (LoadConfigErrorCode): 0=成功, 负值表示错误
*/
virtual int LoadConfig(const std::string& filePath, ConfigResult& configResult) = 0;
/**
* @brief 保存配置文件
* @param filePath 配置文件路径
* @param configResult 配置结果
* @return 是否保存成功
*/
virtual bool SaveConfig(const std::string& filePath, ConfigResult& configResult) = 0;
/**
* @brief 设置配置改变通知回调
* @param notify 通知接口指针
*/
virtual void SetConfigChangeNotify(IVrConfigChangeNotify* notify) = 0;
};
#endif // IVRCONFIG_H