467 lines
16 KiB
C++
467 lines
16 KiB
C++
#include "dialogalgoarg.h"
|
||
#include "ui_dialogalgoarg.h"
|
||
#include "HolePitPositionPresenter.h"
|
||
#include "HandEyeCalibWidget.h"
|
||
#include "NetworkConfigWidget.h"
|
||
#include "PathManager.h"
|
||
#include "StyledMessageBox.h"
|
||
#include "VrLog.h"
|
||
#include <cstring>
|
||
#include <cmath>
|
||
|
||
DialogAlgoarg::DialogAlgoarg(ConfigManager* configManager, HolePitPositionPresenter* presenter, QWidget *parent)
|
||
: QDialog(parent)
|
||
, ui(new Ui::DialogAlgoarg)
|
||
, m_pConfigManager(configManager)
|
||
, m_presenter(presenter)
|
||
, m_handEyeCalibWidget(nullptr)
|
||
, m_networkConfigWidget(nullptr)
|
||
{
|
||
try {
|
||
ui->setupUi(this);
|
||
|
||
// 获取配置文件路径
|
||
m_configFilePath = PathManager::GetInstance().GetConfigFilePath();
|
||
|
||
if (m_configFilePath.isEmpty()) {
|
||
StyledMessageBox::critical(this, "错误", "无法获取配置文件路径!");
|
||
return;
|
||
}
|
||
|
||
// 初始化姿态输出顺序下拉框
|
||
InitPoseOutputOrderComboBox();
|
||
|
||
// 初始化手眼标定 tab(使用共享 HandEyeCalibWidget)
|
||
InitHandEyeCalibTab();
|
||
|
||
// 初始化网络配置 tab(使用共享 NetworkConfigWidget)
|
||
InitNetworkConfigTab();
|
||
|
||
// 从配置文件加载数据到界面
|
||
LoadConfigToUI();
|
||
|
||
} catch (const std::exception& e) {
|
||
StyledMessageBox::critical(this, "初始化错误", QString("对话框初始化失败: %1").arg(e.what()));
|
||
} catch (...) {
|
||
StyledMessageBox::critical(this, "初始化错误", "对话框初始化失败!(未知错误)");
|
||
}
|
||
}
|
||
|
||
DialogAlgoarg::~DialogAlgoarg()
|
||
{
|
||
delete ui;
|
||
}
|
||
|
||
// ========== 手眼标定相关实现 ==========
|
||
|
||
void DialogAlgoarg::InitHandEyeCalibTab()
|
||
{
|
||
if (!m_presenter) return;
|
||
|
||
m_handEyeCalibWidget = new HandEyeCalibWidget(this);
|
||
m_handEyeCalibWidget->setMatrixEditable(true);
|
||
ui->verticalLayout_handEyeCalibHost->addWidget(m_handEyeCalibWidget);
|
||
|
||
m_handEyeCalibWidget->setDefaultFilePath(
|
||
PathManager::GetInstance().GetAppConfigDirectory());
|
||
|
||
// 获取相机列表
|
||
const auto& cameraList = m_presenter->GetCameraList();
|
||
|
||
QVector<HandEyeCalibCameraInfo> calibCameraList;
|
||
if (cameraList.empty()) {
|
||
HandEyeCalibCameraInfo defaultCam;
|
||
defaultCam.cameraIndex = 1;
|
||
defaultCam.displayName = QString::fromUtf8("相机 1");
|
||
calibCameraList.append(defaultCam);
|
||
} else {
|
||
for (size_t i = 0; i < cameraList.size(); ++i) {
|
||
HandEyeCalibCameraInfo info;
|
||
info.cameraIndex = static_cast<int>(i + 1);
|
||
info.displayName = QString::fromStdString(cameraList[i].first);
|
||
calibCameraList.append(info);
|
||
}
|
||
}
|
||
m_handEyeCalibWidget->setCameraList(calibCameraList);
|
||
|
||
// 加载各相机的标定矩阵
|
||
for (const auto& camInfo : calibCameraList) {
|
||
CalibMatrix calibMatrix = m_presenter->GetClibMatrix(camInfo.cameraIndex - 1);
|
||
bool hasCalibData = false;
|
||
for (int i = 0; i < 16; ++i) {
|
||
double identity = (i / 4 == i % 4) ? 1.0 : 0.0;
|
||
if (qAbs(calibMatrix.clibMatrix[i] - identity) > 1e-9) {
|
||
hasCalibData = true;
|
||
break;
|
||
}
|
||
}
|
||
m_handEyeCalibWidget->setCalibData(camInfo.cameraIndex, calibMatrix.clibMatrix, hasCalibData);
|
||
}
|
||
|
||
connect(m_handEyeCalibWidget, &HandEyeCalibWidget::calibMatrixLoaded,
|
||
this, &DialogAlgoarg::onCalibMatrixLoaded);
|
||
connect(m_handEyeCalibWidget, &HandEyeCalibWidget::saveCalibRequested,
|
||
this, &DialogAlgoarg::onSaveCalibRequested);
|
||
}
|
||
|
||
void DialogAlgoarg::onCalibMatrixLoaded(int cameraIndex, const double* matrix)
|
||
{
|
||
Q_UNUSED(cameraIndex);
|
||
Q_UNUSED(matrix);
|
||
StyledMessageBox::information(this, "提示", "已从文件导入标定矩阵");
|
||
}
|
||
|
||
void DialogAlgoarg::onSaveCalibRequested(int cameraIndex, const double* matrix)
|
||
{
|
||
Q_UNUSED(cameraIndex);
|
||
Q_UNUSED(matrix);
|
||
|
||
if (SaveConfigFromUI()) {
|
||
StyledMessageBox::information(this, "成功", "手眼标定参数已保存!");
|
||
} else {
|
||
StyledMessageBox::warning(this, "失败", "保存手眼标定参数失败!");
|
||
}
|
||
}
|
||
|
||
// ========== 网络配置相关实现 ==========
|
||
|
||
void DialogAlgoarg::InitNetworkConfigTab()
|
||
{
|
||
if (!m_pConfigManager) return;
|
||
|
||
// 创建网络配置控件:不显示PLC配置,显示TCP端口配置
|
||
m_networkConfigWidget = new NetworkConfigWidget(false, true, this);
|
||
ui->verticalLayout_networkConfigHost->addWidget(m_networkConfigWidget);
|
||
}
|
||
|
||
void DialogAlgoarg::LoadNetworkConfigToUI()
|
||
{
|
||
if (!m_pConfigManager || !m_networkConfigWidget) return;
|
||
|
||
ConfigResult configResult = m_pConfigManager->GetConfigResult();
|
||
const VrPlcRobotServerConfig& plcConfig = configResult.plcRobotServerConfig;
|
||
|
||
NetworkConfigData netConfig;
|
||
netConfig.tcpServerPort = plcConfig.tcpServerPort;
|
||
netConfig.dirVectorInvert = plcConfig.dirVectorInvert;
|
||
|
||
// eulerOrder: 从第一个手眼标定矩阵的旋转顺序获取
|
||
if (!configResult.handEyeCalibMatrixList.empty()) {
|
||
netConfig.eulerOrder = configResult.handEyeCalibMatrixList[0].eulerOrder;
|
||
}
|
||
|
||
m_networkConfigWidget->setConfig(netConfig);
|
||
|
||
// 加载姿态输出顺序
|
||
int poseOrderIndex = ui->comboBox_poseOutputOrder->findData(plcConfig.poseOutputOrder);
|
||
if (poseOrderIndex >= 0) {
|
||
ui->comboBox_poseOutputOrder->setCurrentIndex(poseOrderIndex);
|
||
}
|
||
}
|
||
|
||
// ========== 配置加载与保存 ==========
|
||
|
||
void DialogAlgoarg::LoadConfigToUI()
|
||
{
|
||
if (!m_pConfigManager) {
|
||
StyledMessageBox::critical(this, "错误", "配置对象未初始化!");
|
||
return;
|
||
}
|
||
|
||
try {
|
||
ConfigResult configData = m_pConfigManager->GetConfigResult();
|
||
const VrAlgorithmParams& algoParams = configData.algorithmParams;
|
||
|
||
// 加载各个参数组
|
||
LoadLineSegParamToUI(algoParams.lineSegParam);
|
||
LoadCornerParamToUI(algoParams.cornerParam);
|
||
LoadOutlierFilterParamToUI(algoParams.outlierFilterParam);
|
||
LoadTreeGrowParamToUI(algoParams.treeGrowParam);
|
||
|
||
// 加载网络配置
|
||
LoadNetworkConfigToUI();
|
||
|
||
} catch (const std::exception& e) {
|
||
LOG_ERROR("Exception in LoadConfigToUI: %s\n", e.what());
|
||
StyledMessageBox::warning(this, "警告",
|
||
QString("加载配置时发生异常: %1\n将使用默认参数显示").arg(e.what()));
|
||
|
||
ConfigResult configData;
|
||
const VrAlgorithmParams& algoParams = configData.algorithmParams;
|
||
LoadLineSegParamToUI(algoParams.lineSegParam);
|
||
LoadCornerParamToUI(algoParams.cornerParam);
|
||
LoadOutlierFilterParamToUI(algoParams.outlierFilterParam);
|
||
LoadTreeGrowParamToUI(algoParams.treeGrowParam);
|
||
} catch (...) {
|
||
LOG_ERROR("Unknown exception in LoadConfigToUI\n");
|
||
StyledMessageBox::warning(this, "警告", "加载配置文件失败(未知错误),将使用默认参数显示");
|
||
|
||
ConfigResult configData;
|
||
const VrAlgorithmParams& algoParams = configData.algorithmParams;
|
||
LoadLineSegParamToUI(algoParams.lineSegParam);
|
||
LoadCornerParamToUI(algoParams.cornerParam);
|
||
LoadOutlierFilterParamToUI(algoParams.outlierFilterParam);
|
||
LoadTreeGrowParamToUI(algoParams.treeGrowParam);
|
||
}
|
||
}
|
||
|
||
bool DialogAlgoarg::SaveConfigFromUI()
|
||
{
|
||
if (!m_pConfigManager) {
|
||
return false;
|
||
}
|
||
|
||
try {
|
||
SystemConfig systemConfig = m_pConfigManager->GetConfig();
|
||
VrAlgorithmParams& algoParams = systemConfig.configResult.algorithmParams;
|
||
|
||
// 保存线段分割参数
|
||
if (!SaveLineSegParamFromUI(algoParams.lineSegParam)) {
|
||
StyledMessageBox::warning(this, "错误", "线段分割参数输入有误,请检查!");
|
||
return false;
|
||
}
|
||
|
||
// 保存角点参数
|
||
if (!SaveCornerParamFromUI(algoParams.cornerParam)) {
|
||
StyledMessageBox::warning(this, "错误", "角点参数输入有误,请检查!");
|
||
return false;
|
||
}
|
||
|
||
// 保存噪声过滤参数
|
||
if (!SaveOutlierFilterParamFromUI(algoParams.outlierFilterParam)) {
|
||
StyledMessageBox::warning(this, "错误", "噪声过滤参数输入有误,请检查!");
|
||
return false;
|
||
}
|
||
|
||
// 保存树生长参数
|
||
if (!SaveTreeGrowParamFromUI(algoParams.treeGrowParam)) {
|
||
StyledMessageBox::warning(this, "错误", "树生长参数输入有误,请检查!");
|
||
return false;
|
||
}
|
||
|
||
// 保存手眼标定矩阵(使用共享控件)
|
||
if (m_handEyeCalibWidget && m_presenter) {
|
||
const auto& oldMatrixList = systemConfig.configResult.handEyeCalibMatrixList;
|
||
std::vector<VrHandEyeCalibMatrix> newMatrixList;
|
||
|
||
const auto& cameraList = m_presenter->GetCameraList();
|
||
int cameraCount = std::max(1, static_cast<int>(cameraList.size()));
|
||
|
||
for (int i = 0; i < cameraCount; ++i) {
|
||
int camIdx = i + 1;
|
||
|
||
// 先从已有配置中获取该相机的矩阵作为基础
|
||
VrHandEyeCalibMatrix calibMatrix;
|
||
calibMatrix.cameraIndex = camIdx;
|
||
for (const auto& old : oldMatrixList) {
|
||
if (old.cameraIndex == camIdx) {
|
||
calibMatrix = old;
|
||
break;
|
||
}
|
||
}
|
||
|
||
// 如果控件中有更新的数据则覆盖
|
||
bool isCalibrated = false;
|
||
double matrix[16];
|
||
if (m_handEyeCalibWidget->getCalibData(camIdx, matrix, isCalibrated) && isCalibrated) {
|
||
memcpy(calibMatrix.matrix, matrix, sizeof(double) * 16);
|
||
}
|
||
|
||
newMatrixList.push_back(calibMatrix);
|
||
}
|
||
systemConfig.configResult.handEyeCalibMatrixList = newMatrixList;
|
||
}
|
||
|
||
// 保存网络配置(使用共享控件)
|
||
if (m_networkConfigWidget) {
|
||
NetworkConfigData netConfig = m_networkConfigWidget->getConfig();
|
||
systemConfig.configResult.plcRobotServerConfig.tcpServerPort = netConfig.tcpServerPort;
|
||
systemConfig.configResult.plcRobotServerConfig.dirVectorInvert = netConfig.dirVectorInvert;
|
||
|
||
// 保存 eulerOrder 到所有手眼标定矩阵
|
||
for (auto& calibMatrix : systemConfig.configResult.handEyeCalibMatrixList) {
|
||
calibMatrix.eulerOrder = netConfig.eulerOrder;
|
||
}
|
||
}
|
||
|
||
// 保存姿态输出顺序
|
||
systemConfig.configResult.plcRobotServerConfig.poseOutputOrder =
|
||
ui->comboBox_poseOutputOrder->currentData().toInt();
|
||
|
||
// 更新并保存配置到文件
|
||
if (!m_pConfigManager->UpdateFullConfig(systemConfig)) {
|
||
return false;
|
||
}
|
||
|
||
if (!m_pConfigManager->SaveConfigToFile(m_configFilePath.toStdString())) {
|
||
return false;
|
||
}
|
||
|
||
// 通知 Presenter 重新加载配置
|
||
if (m_presenter) {
|
||
m_presenter->OnConfigChanged(systemConfig.configResult);
|
||
}
|
||
|
||
return true;
|
||
|
||
} catch (const std::exception& e) {
|
||
LOG_ERROR("Exception in SaveConfigFromUI: %s\n", e.what());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
// ========== 参数加载到 UI ==========
|
||
|
||
void DialogAlgoarg::LoadLineSegParamToUI(const VrLineSegParam& param)
|
||
{
|
||
if (!ui) return;
|
||
|
||
ui->lineEdit_distScale->setText(QString::number(param.distScale));
|
||
ui->lineEdit_segGapTh_y->setText(QString::number(param.segGapTh_y));
|
||
ui->lineEdit_segGapTh_z->setText(QString::number(param.segGapTh_z));
|
||
}
|
||
|
||
void DialogAlgoarg::LoadCornerParamToUI(const VrCornerParam& param)
|
||
{
|
||
if (!ui) return;
|
||
|
||
ui->lineEdit_cornerTh->setText(QString::number(param.cornerTh));
|
||
ui->lineEdit_scale->setText(QString::number(param.scale));
|
||
ui->lineEdit_minEndingGap->setText(QString::number(param.minEndingGap));
|
||
ui->lineEdit_minEndingGap_z->setText(QString::number(param.minEndingGap_z));
|
||
ui->lineEdit_jumpCornerTh_1->setText(QString::number(param.jumpCornerTh_1));
|
||
ui->lineEdit_jumpCornerTh_2->setText(QString::number(param.jumpCornerTh_2));
|
||
}
|
||
|
||
void DialogAlgoarg::LoadOutlierFilterParamToUI(const VrOutlierFilterParam& param)
|
||
{
|
||
if (!ui) return;
|
||
|
||
ui->lineEdit_continuityTh->setText(QString::number(param.continuityTh));
|
||
ui->lineEdit_outlierTh->setText(QString::number(param.outlierTh));
|
||
}
|
||
|
||
void DialogAlgoarg::LoadTreeGrowParamToUI(const VrTreeGrowParam& param)
|
||
{
|
||
if (!ui) return;
|
||
|
||
ui->lineEdit_maxLineSkipNum->setText(QString::number(param.maxLineSkipNum));
|
||
ui->lineEdit_yDeviation_max->setText(QString::number(param.yDeviation_max));
|
||
ui->lineEdit_maxSkipDistance->setText(QString::number(param.maxSkipDistance));
|
||
ui->lineEdit_zDeviation_max->setText(QString::number(param.zDeviation_max));
|
||
ui->lineEdit_minLTypeTreeLen->setText(QString::number(param.minLTypeTreeLen));
|
||
ui->lineEdit_minVTypeTreeLen->setText(QString::number(param.minVTypeTreeLen));
|
||
}
|
||
|
||
// ========== 从 UI 保存参数 ==========
|
||
|
||
bool DialogAlgoarg::SaveLineSegParamFromUI(VrLineSegParam& param)
|
||
{
|
||
bool ok = true;
|
||
|
||
param.distScale = ui->lineEdit_distScale->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
param.segGapTh_y = ui->lineEdit_segGapTh_y->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
param.segGapTh_z = ui->lineEdit_segGapTh_z->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
return true;
|
||
}
|
||
|
||
bool DialogAlgoarg::SaveCornerParamFromUI(VrCornerParam& param)
|
||
{
|
||
bool ok = true;
|
||
|
||
param.cornerTh = ui->lineEdit_cornerTh->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
param.scale = ui->lineEdit_scale->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
param.minEndingGap = ui->lineEdit_minEndingGap->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
param.minEndingGap_z = ui->lineEdit_minEndingGap_z->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
param.jumpCornerTh_1 = ui->lineEdit_jumpCornerTh_1->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
param.jumpCornerTh_2 = ui->lineEdit_jumpCornerTh_2->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
return true;
|
||
}
|
||
|
||
bool DialogAlgoarg::SaveOutlierFilterParamFromUI(VrOutlierFilterParam& param)
|
||
{
|
||
bool ok = true;
|
||
|
||
param.continuityTh = ui->lineEdit_continuityTh->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
param.outlierTh = ui->lineEdit_outlierTh->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
return true;
|
||
}
|
||
|
||
bool DialogAlgoarg::SaveTreeGrowParamFromUI(VrTreeGrowParam& param)
|
||
{
|
||
bool ok = true;
|
||
|
||
param.maxLineSkipNum = ui->lineEdit_maxLineSkipNum->text().toInt(&ok);
|
||
if (!ok) return false;
|
||
|
||
param.yDeviation_max = ui->lineEdit_yDeviation_max->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
param.maxSkipDistance = ui->lineEdit_maxSkipDistance->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
param.zDeviation_max = ui->lineEdit_zDeviation_max->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
param.minLTypeTreeLen = ui->lineEdit_minLTypeTreeLen->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
param.minVTypeTreeLen = ui->lineEdit_minVTypeTreeLen->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
return true;
|
||
}
|
||
|
||
// ========== 按钮事件 ==========
|
||
|
||
void DialogAlgoarg::on_btn_camer_ok_clicked()
|
||
{
|
||
if (SaveConfigFromUI()) {
|
||
StyledMessageBox::information(this, "成功", "配置保存成功!");
|
||
accept();
|
||
} else {
|
||
StyledMessageBox::warning(this, "失败", "配置保存失败,请检查文件权限或参数输入!");
|
||
}
|
||
}
|
||
|
||
void DialogAlgoarg::on_btn_camer_cancel_clicked()
|
||
{
|
||
reject();
|
||
}
|
||
|
||
// ========== 下拉框初始化 ==========
|
||
|
||
void DialogAlgoarg::InitPoseOutputOrderComboBox()
|
||
{
|
||
ui->comboBox_poseOutputOrder->addItem("RPY (Roll, Pitch, Yaw)", POSE_ORDER_RPY);
|
||
ui->comboBox_poseOutputOrder->addItem("RYP (Roll, Yaw, Pitch)", POSE_ORDER_RYP);
|
||
ui->comboBox_poseOutputOrder->addItem("PRY (Pitch, Roll, Yaw)", POSE_ORDER_PRY);
|
||
ui->comboBox_poseOutputOrder->addItem("PYR (Pitch, Yaw, Roll)", POSE_ORDER_PYR);
|
||
ui->comboBox_poseOutputOrder->addItem("YRP (Yaw, Roll, Pitch)", POSE_ORDER_YRP);
|
||
ui->comboBox_poseOutputOrder->addItem("YPR (Yaw, Pitch, Roll)", POSE_ORDER_YPR);
|
||
|
||
ui->comboBox_poseOutputOrder->setCurrentIndex(0);
|
||
}
|