817 lines
40 KiB
C++
817 lines
40 KiB
C++
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
|
// ShapeModelGenDialog.cpp ─ 离线轮廓库生成工具 (V5)
|
|||
|
|
// 支持左右相机分别加载标定参数,分别生成轮廓库
|
|||
|
|
// ════════════════════════════════════════════════════════════════════════════
|
|||
|
|
#include "ShapeModelGenDialog.h"
|
|||
|
|
#include "AppSettings.h"
|
|||
|
|
#include "UserVisibleLog.h"
|
|||
|
|
|
|||
|
|
#define NOMINMAX
|
|||
|
|
#include <VisionGuideApi.h>
|
|||
|
|
using namespace VisionGuideApi;
|
|||
|
|
|
|||
|
|
#include <QVBoxLayout>
|
|||
|
|
#include <QHBoxLayout>
|
|||
|
|
#include <QFormLayout>
|
|||
|
|
#include <QSplitter>
|
|||
|
|
#include <QGroupBox>
|
|||
|
|
#include <QLabel>
|
|||
|
|
#include <QLineEdit>
|
|||
|
|
#include <QPushButton>
|
|||
|
|
#include <QProgressBar>
|
|||
|
|
#include <QTextEdit>
|
|||
|
|
#include <QDoubleSpinBox>
|
|||
|
|
#include <QSpinBox>
|
|||
|
|
#include <QCheckBox>
|
|||
|
|
#include <QComboBox>
|
|||
|
|
#include <QFileDialog>
|
|||
|
|
#include <QDir>
|
|||
|
|
#include <QFile>
|
|||
|
|
#include <QFileInfo>
|
|||
|
|
#include <QDateTime>
|
|||
|
|
#include <QApplication>
|
|||
|
|
#include <QMessageBox>
|
|||
|
|
#include <QFont>
|
|||
|
|
#include <QPainter>
|
|||
|
|
#include <cmath>
|
|||
|
|
|
|||
|
|
#ifndef M_PI
|
|||
|
|
#define M_PI 3.14159265358979323846
|
|||
|
|
#endif
|
|||
|
|
static inline double deg2rad(double d) { return d * M_PI / 180.0; }
|
|||
|
|
|
|||
|
|
// ── AppSettings 键 ──────────────────────────────────────────────────
|
|||
|
|
static const char* SK_LongMin = "smgen/longMinDeg";
|
|||
|
|
static const char* SK_LongMax = "smgen/longMaxDeg";
|
|||
|
|
static const char* SK_LatMin = "smgen/latMinDeg";
|
|||
|
|
static const char* SK_LatMax = "smgen/latMaxDeg";
|
|||
|
|
static const char* SK_RollMin = "smgen/rollMinDeg";
|
|||
|
|
static const char* SK_RollMax = "smgen/rollMaxDeg";
|
|||
|
|
static const char* SK_DistMin = "smgen/distMin";
|
|||
|
|
static const char* SK_DistMax = "smgen/distMax";
|
|||
|
|
static const char* SK_MinContrast = "smgen/minContrast";
|
|||
|
|
static const char* SK_CustomLvl = "smgen/customLevels";
|
|||
|
|
static const char* SK_NumLvl = "smgen/numLevels";
|
|||
|
|
static const char* SK_CustomLow = "smgen/customLowest";
|
|||
|
|
static const char* SK_LowLvl = "smgen/lowestLevel";
|
|||
|
|
static const char* SK_PvLong = "smgen/pvLong";
|
|||
|
|
static const char* SK_PvLat = "smgen/pvLat";
|
|||
|
|
static const char* SK_PvRoll = "smgen/pvRoll";
|
|||
|
|
static const char* SK_PvDist = "smgen/pvDist";
|
|||
|
|
|
|||
|
|
// ════════════════════════════════════════════════════════════════════════════
|
|||
|
|
struct ShapeModelGenDialog::Impl
|
|||
|
|
{
|
|||
|
|
HTuple objectModel3D;
|
|||
|
|
bool cadLoaded = false;
|
|||
|
|
|
|||
|
|
// 左右各一个模板
|
|||
|
|
HTuple shapeModel3D[2]; // [0]=左, [1]=右
|
|||
|
|
bool modelCreated[2] = { false, false };
|
|||
|
|
|
|||
|
|
HTuple camParam[2]; // [0]=左, [1]=右
|
|||
|
|
bool camParamLoaded[2] = { false, false };
|
|||
|
|
|
|||
|
|
QString cadPath;
|
|||
|
|
QString sm3Path[2];
|
|||
|
|
int numVertices = 0, numFaces = 0;
|
|||
|
|
|
|||
|
|
static HTuple defaultCamParam() {
|
|||
|
|
HTuple p;
|
|||
|
|
p[0] = "area_scan_division"; p[1] = 0.035; p[2] = 0.0;
|
|||
|
|
p[3] = 2.5e-6; p[4] = 2.5e-6; p[5] = 2560.0;
|
|||
|
|
p[6] = 2560.0; p[7] = 5120; p[8] = 5120;
|
|||
|
|
return p;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void clearModel(int side) {
|
|||
|
|
if (modelCreated[side]) {
|
|||
|
|
try { ClearShapeModel3d(shapeModel3D[side]); } catch (...) {}
|
|||
|
|
modelCreated[side] = false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
void clearCAD() {
|
|||
|
|
if (cadLoaded) {
|
|||
|
|
try { ClearObjectModel3d(objectModel3D); } catch (...) {}
|
|||
|
|
cadLoaded = false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
~Impl() {
|
|||
|
|
clearModel(0); clearModel(1); clearCAD();
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// ════════════════════════════════════════════════════════════════════════════
|
|||
|
|
ShapeModelGenDialog::ShapeModelGenDialog(QWidget* parent)
|
|||
|
|
: QDialog(parent), m_d(std::make_unique<Impl>())
|
|||
|
|
{
|
|||
|
|
setWindowTitle(QString::fromUtf8("生成离线轮廓库 (3D Shape Model) - 双相机"));
|
|||
|
|
setMinimumSize(1300, 900);
|
|||
|
|
resize(1400, 950);
|
|||
|
|
buildUi();
|
|||
|
|
loadParams();
|
|||
|
|
setupConnections();
|
|||
|
|
|
|||
|
|
// 尝试加载左相机参数
|
|||
|
|
QString camPathL = m_editCamParamLeft->text();
|
|||
|
|
if (!camPathL.isEmpty() && QFile::exists(camPathL)) {
|
|||
|
|
try {
|
|||
|
|
ReadCamPar(HTuple(camPathL.toLocal8Bit().constData()), &m_d->camParam[0]);
|
|||
|
|
m_d->camParamLoaded[0] = true;
|
|||
|
|
m_labelCamLeftStatus->setText(QString::fromUtf8("✓ 已加载"));
|
|||
|
|
m_labelCamLeftStatus->setStyleSheet("color:#00cc66;font-size:11px;");
|
|||
|
|
} catch (...) {}
|
|||
|
|
}
|
|||
|
|
// 尝试加载右相机参数
|
|||
|
|
QString camPathR = m_editCamParamRight->text();
|
|||
|
|
if (!camPathR.isEmpty() && QFile::exists(camPathR)) {
|
|||
|
|
try {
|
|||
|
|
ReadCamPar(HTuple(camPathR.toLocal8Bit().constData()), &m_d->camParam[1]);
|
|||
|
|
m_d->camParamLoaded[1] = true;
|
|||
|
|
m_labelCamRightStatus->setText(QString::fromUtf8("✓ 已加载"));
|
|||
|
|
m_labelCamRightStatus->setStyleSheet("color:#00cc66;font-size:11px;");
|
|||
|
|
} catch (...) {}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
log(QString::fromUtf8("轮廓库生成工具已打开。"));
|
|||
|
|
log(QString::fromUtf8("流程: 导入CAD → 加载左右相机参数 → 生成 → 预览/导出"));
|
|||
|
|
log(QString::fromUtf8("注意: 左右相机参数不同,将分别生成两个轮廓库"));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
ShapeModelGenDialog::~ShapeModelGenDialog() { saveParams(); }
|
|||
|
|
|
|||
|
|
// ════════════════════════════════════════════════════════════════════════════
|
|||
|
|
void ShapeModelGenDialog::saveParams()
|
|||
|
|
{
|
|||
|
|
auto& s = AppSettings::instance();
|
|||
|
|
s.set(SK_LongMin, QString::number(m_spinLongMin->value()));
|
|||
|
|
s.set(SK_LongMax, QString::number(m_spinLongMax->value()));
|
|||
|
|
s.set(SK_LatMin, QString::number(m_spinLatMin->value()));
|
|||
|
|
s.set(SK_LatMax, QString::number(m_spinLatMax->value()));
|
|||
|
|
s.set(SK_RollMin, QString::number(m_spinRollMin->value()));
|
|||
|
|
s.set(SK_RollMax, QString::number(m_spinRollMax->value()));
|
|||
|
|
s.set(SK_DistMin, QString::number(m_spinDistMin->value()));
|
|||
|
|
s.set(SK_DistMax, QString::number(m_spinDistMax->value()));
|
|||
|
|
s.set(SK_MinContrast, QString::number(m_spinMinContrast->value()));
|
|||
|
|
s.set(SK_CustomLvl, m_chkCustomLevels->isChecked() ? "1" : "0");
|
|||
|
|
s.set(SK_NumLvl, QString::number(m_spinNumLevels->value()));
|
|||
|
|
s.set(SK_CustomLow, m_chkCustomLowestLevel->isChecked() ? "1" : "0");
|
|||
|
|
s.set(SK_LowLvl, QString::number(m_spinLowestLevel->value()));
|
|||
|
|
s.set(SK_PvLong, QString::number(m_spinPreviewLong->value()));
|
|||
|
|
s.set(SK_PvLat, QString::number(m_spinPreviewLat->value()));
|
|||
|
|
s.set(SK_PvRoll, QString::number(m_spinPreviewRoll->value()));
|
|||
|
|
s.set(SK_PvDist, QString::number(m_spinPreviewDist->value()));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void ShapeModelGenDialog::loadParams()
|
|||
|
|
{
|
|||
|
|
auto& s = AppSettings::instance();
|
|||
|
|
m_spinLongMin->setValue( s.getRaw(SK_LongMin, "-180").toDouble());
|
|||
|
|
m_spinLongMax->setValue( s.getRaw(SK_LongMax, "180").toDouble());
|
|||
|
|
m_spinLatMin->setValue( s.getRaw(SK_LatMin, "-25").toDouble());
|
|||
|
|
m_spinLatMax->setValue( s.getRaw(SK_LatMax, "25").toDouble());
|
|||
|
|
m_spinRollMin->setValue( s.getRaw(SK_RollMin, "-180").toDouble());
|
|||
|
|
m_spinRollMax->setValue( s.getRaw(SK_RollMax, "180").toDouble());
|
|||
|
|
m_spinDistMin->setValue( s.getRaw(SK_DistMin, "0.28").toDouble());
|
|||
|
|
m_spinDistMax->setValue( s.getRaw(SK_DistMax, "0.36").toDouble());
|
|||
|
|
m_spinMinContrast->setValue(s.getRaw(SK_MinContrast, "2").toInt());
|
|||
|
|
m_chkCustomLevels->setChecked( s.getRaw(SK_CustomLvl, "0") == "1");
|
|||
|
|
m_spinNumLevels->setValue( s.getRaw(SK_NumLvl, "4").toInt());
|
|||
|
|
m_chkCustomLowestLevel->setChecked( s.getRaw(SK_CustomLow, "0") == "1");
|
|||
|
|
m_spinLowestLevel->setValue( s.getRaw(SK_LowLvl, "1").toInt());
|
|||
|
|
m_spinNumLevels->setEnabled(m_chkCustomLevels->isChecked());
|
|||
|
|
m_spinLowestLevel->setEnabled(m_chkCustomLowestLevel->isChecked());
|
|||
|
|
m_spinPreviewLong->setValue( s.getRaw(SK_PvLong, "0").toDouble());
|
|||
|
|
m_spinPreviewLat->setValue( s.getRaw(SK_PvLat, "0").toDouble());
|
|||
|
|
m_spinPreviewRoll->setValue( s.getRaw(SK_PvRoll, "0").toDouble());
|
|||
|
|
m_spinPreviewDist->setValue( s.getRaw(SK_PvDist, "0.314").toDouble());
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ════════════════════════════════════════════════════════════════════════════
|
|||
|
|
void ShapeModelGenDialog::buildUi()
|
|||
|
|
{
|
|||
|
|
auto* mainHLay = new QHBoxLayout(this);
|
|||
|
|
mainHLay->setContentsMargins(8,8,8,8);
|
|||
|
|
mainHLay->setSpacing(8);
|
|||
|
|
|
|||
|
|
auto* leftW = new QWidget;
|
|||
|
|
auto* leftV = new QVBoxLayout(leftW);
|
|||
|
|
|
|||
|
|
// --- CAD ---
|
|||
|
|
auto* cadG = new QGroupBox(QString::fromUtf8("CAD 模型"));
|
|||
|
|
auto* cadF = new QFormLayout(cadG);
|
|||
|
|
m_editCADPath = new QLineEdit(AppSettings::instance().get(AppSettings::K_CAD, ""));
|
|||
|
|
m_editCADPath->setPlaceholderText(QString::fromUtf8("STL / OBJ / PLY 文件"));
|
|||
|
|
m_btnBrowseCAD = new QPushButton(QString::fromUtf8("浏览...")); m_btnBrowseCAD->setFixedWidth(70);
|
|||
|
|
m_btnLoadCAD = new QPushButton(QString::fromUtf8("加载")); m_btnLoadCAD->setFixedWidth(50);
|
|||
|
|
auto* cadR = new QHBoxLayout;
|
|||
|
|
cadR->addWidget(m_editCADPath); cadR->addWidget(m_btnBrowseCAD); cadR->addWidget(m_btnLoadCAD);
|
|||
|
|
cadF->addRow(QString::fromUtf8("CAD 文件:"), cadR);
|
|||
|
|
m_labelCADInfo = new QLabel(QString::fromUtf8("未加载"));
|
|||
|
|
m_labelCADInfo->setStyleSheet("color:gray; font-size:11px;");
|
|||
|
|
cadF->addRow("", m_labelCADInfo);
|
|||
|
|
leftV->addWidget(cadG);
|
|||
|
|
|
|||
|
|
// --- 相机参数 (左右分开) ---
|
|||
|
|
auto* camG = new QGroupBox(QString::fromUtf8("相机参数 (左右相机分别加载)"));
|
|||
|
|
auto* camV = new QVBoxLayout(camG);
|
|||
|
|
|
|||
|
|
// 左相机
|
|||
|
|
auto* camLeftRow = new QHBoxLayout;
|
|||
|
|
camLeftRow->addWidget(new QLabel(QString::fromUtf8("左相机 .cal:")));
|
|||
|
|
m_editCamParamLeft = new QLineEdit(AppSettings::instance().get(AppSettings::K_LeftCal, ""));
|
|||
|
|
m_editCamParamLeft->setPlaceholderText(QString::fromUtf8("左相机标定文件"));
|
|||
|
|
m_btnBrowseCamLeft = new QPushButton(QString::fromUtf8("加载...")); m_btnBrowseCamLeft->setFixedWidth(70);
|
|||
|
|
camLeftRow->addWidget(m_editCamParamLeft);
|
|||
|
|
camLeftRow->addWidget(m_btnBrowseCamLeft);
|
|||
|
|
camV->addLayout(camLeftRow);
|
|||
|
|
m_labelCamLeftStatus = new QLabel(QString::fromUtf8("未加载"));
|
|||
|
|
m_labelCamLeftStatus->setStyleSheet("color:gray;font-size:11px;");
|
|||
|
|
camV->addWidget(m_labelCamLeftStatus);
|
|||
|
|
|
|||
|
|
// 右相机
|
|||
|
|
auto* camRightRow = new QHBoxLayout;
|
|||
|
|
camRightRow->addWidget(new QLabel(QString::fromUtf8("右相机 .cal:")));
|
|||
|
|
m_editCamParamRight = new QLineEdit(AppSettings::instance().get(AppSettings::K_RightCal, ""));
|
|||
|
|
m_editCamParamRight->setPlaceholderText(QString::fromUtf8("右相机标定文件"));
|
|||
|
|
m_btnBrowseCamRight = new QPushButton(QString::fromUtf8("加载...")); m_btnBrowseCamRight->setFixedWidth(70);
|
|||
|
|
camRightRow->addWidget(m_editCamParamRight);
|
|||
|
|
camRightRow->addWidget(m_btnBrowseCamRight);
|
|||
|
|
camV->addLayout(camRightRow);
|
|||
|
|
m_labelCamRightStatus = new QLabel(QString::fromUtf8("未加载"));
|
|||
|
|
m_labelCamRightStatus->setStyleSheet("color:gray;font-size:11px;");
|
|||
|
|
camV->addWidget(m_labelCamRightStatus);
|
|||
|
|
|
|||
|
|
leftV->addWidget(camG);
|
|||
|
|
|
|||
|
|
// --- 生成参数 (角度°) ---
|
|||
|
|
auto* pG = new QGroupBox(QString::fromUtf8("模板生成参数"));
|
|||
|
|
auto* pF = new QFormLayout(pG);
|
|||
|
|
|
|||
|
|
auto mkD = [](double mn, double mx, double v, int dec, double st) {
|
|||
|
|
auto* sp = new QDoubleSpinBox;
|
|||
|
|
sp->setRange(mn, mx); sp->setValue(v); sp->setDecimals(dec); sp->setSingleStep(st);
|
|||
|
|
return sp;
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
m_spinLongMin = mkD(-180, 180, -180, 1, 5);
|
|||
|
|
m_spinLongMax = mkD(-180, 180, 180, 1, 5);
|
|||
|
|
auto* lr = new QHBoxLayout;
|
|||
|
|
lr->addWidget(new QLabel("Min:")); lr->addWidget(m_spinLongMin);
|
|||
|
|
lr->addWidget(new QLabel("Max:")); lr->addWidget(m_spinLongMax);
|
|||
|
|
pF->addRow(QString::fromUtf8("经度范围 (°):"), lr);
|
|||
|
|
|
|||
|
|
m_spinLatMin = mkD(-90, 90, -25, 1, 5);
|
|||
|
|
m_spinLatMax = mkD(-90, 90, 25, 1, 5);
|
|||
|
|
auto* latr = new QHBoxLayout;
|
|||
|
|
latr->addWidget(new QLabel("Min:")); latr->addWidget(m_spinLatMin);
|
|||
|
|
latr->addWidget(new QLabel("Max:")); latr->addWidget(m_spinLatMax);
|
|||
|
|
pF->addRow(QString::fromUtf8("纬度范围 (°):"), latr);
|
|||
|
|
|
|||
|
|
m_spinRollMin = mkD(-180, 180, -180, 1, 5);
|
|||
|
|
m_spinRollMax = mkD(-180, 180, 180, 1, 5);
|
|||
|
|
auto* rr = new QHBoxLayout;
|
|||
|
|
rr->addWidget(new QLabel("Min:")); rr->addWidget(m_spinRollMin);
|
|||
|
|
rr->addWidget(new QLabel("Max:")); rr->addWidget(m_spinRollMax);
|
|||
|
|
pF->addRow(QString::fromUtf8("滚转角 (°):"), rr);
|
|||
|
|
|
|||
|
|
// 与原 HDevelop 模板生成程序保持一致:STL 的默认坐标系需要先绕
|
|||
|
|
// X 轴旋转 -90°,否则圆柱零件会以端面朝向作为参考姿态生成模板。
|
|||
|
|
auto* refPoseLabel = new QLabel(QString::fromUtf8("参考旋转: X=-90°, Y=0°, Z=0°(与原程序一致)"));
|
|||
|
|
refPoseLabel->setStyleSheet("color:#d6a84f;font-size:11px;");
|
|||
|
|
pF->addRow("", refPoseLabel);
|
|||
|
|
|
|||
|
|
m_spinDistMin = mkD(0.01, 10.0, 0.28, 3, 0.01);
|
|||
|
|
m_spinDistMax = mkD(0.01, 10.0, 0.36, 3, 0.01);
|
|||
|
|
auto* dr = new QHBoxLayout;
|
|||
|
|
dr->addWidget(new QLabel("Min:")); dr->addWidget(m_spinDistMin);
|
|||
|
|
dr->addWidget(new QLabel("Max:")); dr->addWidget(m_spinDistMax);
|
|||
|
|
pF->addRow(QString::fromUtf8("距离范围 (m):"), dr);
|
|||
|
|
|
|||
|
|
m_spinMinContrast = new QSpinBox; m_spinMinContrast->setRange(1,255); m_spinMinContrast->setValue(2);
|
|||
|
|
pF->addRow("MinContrast:", m_spinMinContrast);
|
|||
|
|
|
|||
|
|
m_chkCustomLevels = new QCheckBox(QString::fromUtf8("手动指定"));
|
|||
|
|
m_spinNumLevels = new QSpinBox; m_spinNumLevels->setRange(1,10); m_spinNumLevels->setValue(4); m_spinNumLevels->setEnabled(false);
|
|||
|
|
auto* lvlR = new QHBoxLayout; lvlR->addWidget(m_chkCustomLevels); lvlR->addWidget(m_spinNumLevels);
|
|||
|
|
pF->addRow(QString::fromUtf8("金字塔层级:"), lvlR);
|
|||
|
|
|
|||
|
|
m_chkCustomLowestLevel = new QCheckBox(QString::fromUtf8("手动指定"));
|
|||
|
|
m_spinLowestLevel = new QSpinBox; m_spinLowestLevel->setRange(1,10); m_spinLowestLevel->setValue(1); m_spinLowestLevel->setEnabled(false);
|
|||
|
|
auto* lowR = new QHBoxLayout; lowR->addWidget(m_chkCustomLowestLevel); lowR->addWidget(m_spinLowestLevel);
|
|||
|
|
pF->addRow(QString::fromUtf8("最低金字塔层:"), lowR);
|
|||
|
|
|
|||
|
|
leftV->addWidget(pG);
|
|||
|
|
|
|||
|
|
// --- 操作 ---
|
|||
|
|
auto* ctrlG = new QGroupBox(QString::fromUtf8("操作"));
|
|||
|
|
auto* ctrlV = new QVBoxLayout(ctrlG);
|
|||
|
|
m_btnGenerate = new QPushButton(QString::fromUtf8(">> 生成左右3D形状模板"));
|
|||
|
|
m_btnGenerate->setMinimumHeight(36);
|
|||
|
|
m_btnGenerate->setStyleSheet("QPushButton{background:#1e6f9f;color:white;font-weight:bold;font-size:13px;border-radius:4px;}"
|
|||
|
|
"QPushButton:hover{background:#2589c4;}QPushButton:disabled{background:#555;color:#999;}");
|
|||
|
|
m_btnGenerate->setEnabled(false);
|
|||
|
|
m_btnExport = new QPushButton(QString::fromUtf8("导出左右 .sm3 文件...")); m_btnExport->setEnabled(false);
|
|||
|
|
m_btnApply = new QPushButton(QString::fromUtf8("应用到主界面")); m_btnApply->setEnabled(false);
|
|||
|
|
m_btnApply->setStyleSheet("QPushButton{background:#2e8b57;color:white;font-weight:bold;border-radius:4px;}"
|
|||
|
|
"QPushButton:hover{background:#38a56a;}QPushButton:disabled{background:#555;color:#999;}");
|
|||
|
|
|
|||
|
|
auto* importRow = new QHBoxLayout;
|
|||
|
|
m_btnImportSM3Left = new QPushButton(QString::fromUtf8("导入左相机 .sm3"));
|
|||
|
|
m_btnImportSM3Right = new QPushButton(QString::fromUtf8("导入右相机 .sm3"));
|
|||
|
|
importRow->addWidget(m_btnImportSM3Left);
|
|||
|
|
importRow->addWidget(m_btnImportSM3Right);
|
|||
|
|
|
|||
|
|
m_progress = new QProgressBar; m_progress->setValue(0);
|
|||
|
|
ctrlV->addWidget(m_btnGenerate);
|
|||
|
|
ctrlV->addWidget(m_btnExport);
|
|||
|
|
ctrlV->addWidget(m_btnApply);
|
|||
|
|
ctrlV->addLayout(importRow);
|
|||
|
|
ctrlV->addWidget(m_progress);
|
|||
|
|
leftV->addWidget(ctrlG);
|
|||
|
|
leftV->addStretch();
|
|||
|
|
|
|||
|
|
// ── 右侧 ─────────────────────────────────────────────────────
|
|||
|
|
auto* rightW = new QWidget;
|
|||
|
|
auto* rightV = new QVBoxLayout(rightW);
|
|||
|
|
|
|||
|
|
auto* pvG = new QGroupBox(QString::fromUtf8("轮廓预览 (自由视角)"));
|
|||
|
|
auto* pvV = new QVBoxLayout(pvG);
|
|||
|
|
|
|||
|
|
m_labelPreview = new QLabel(QString::fromUtf8("生成或导入模板后,调整参数预览轮廓"));
|
|||
|
|
m_labelPreview->setMinimumSize(520, 520);
|
|||
|
|
m_labelPreview->setAlignment(Qt::AlignCenter);
|
|||
|
|
m_labelPreview->setStyleSheet("background:#1a1a2e; color:#666; font-size:13px;");
|
|||
|
|
m_labelPreview->setFrameShape(QFrame::Box);
|
|||
|
|
m_labelPreview->setScaledContents(false);
|
|||
|
|
m_labelPreview->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
|||
|
|
pvV->addWidget(m_labelPreview);
|
|||
|
|
|
|||
|
|
m_spinPreviewLong = mkD(-180, 180, 0, 1, 5);
|
|||
|
|
m_spinPreviewLat = mkD(-90, 90, 0, 1, 5);
|
|||
|
|
m_spinPreviewRoll = mkD(-180, 180, 0, 1, 5);
|
|||
|
|
m_spinPreviewDist = mkD(0.01, 10.0, 0.314, 3, 0.01);
|
|||
|
|
|
|||
|
|
auto* pr1 = new QHBoxLayout;
|
|||
|
|
pr1->addWidget(new QLabel(QString::fromUtf8("经度(°):"))); pr1->addWidget(m_spinPreviewLong);
|
|||
|
|
pr1->addWidget(new QLabel(QString::fromUtf8("纬度(°):"))); pr1->addWidget(m_spinPreviewLat);
|
|||
|
|
pvV->addLayout(pr1);
|
|||
|
|
|
|||
|
|
auto* pr2 = new QHBoxLayout;
|
|||
|
|
pr2->addWidget(new QLabel(QString::fromUtf8("滚转(°):"))); pr2->addWidget(m_spinPreviewRoll);
|
|||
|
|
pr2->addWidget(new QLabel(QString::fromUtf8("距离(m):"))); pr2->addWidget(m_spinPreviewDist);
|
|||
|
|
pvV->addLayout(pr2);
|
|||
|
|
|
|||
|
|
// 预览选择左/右
|
|||
|
|
m_comboPreviewSide = new QComboBox;
|
|||
|
|
m_comboPreviewSide->addItem(QString::fromUtf8("左相机模板"));
|
|||
|
|
m_comboPreviewSide->addItem(QString::fromUtf8("右相机模板"));
|
|||
|
|
|
|||
|
|
m_btnRefreshPreview = new QPushButton(QString::fromUtf8("刷新预览")); m_btnRefreshPreview->setFixedWidth(90); m_btnRefreshPreview->setEnabled(false);
|
|||
|
|
m_labelViewInfo = new QLabel(""); m_labelViewInfo->setStyleSheet("color:gray; font-size:11px;");
|
|||
|
|
auto* pr3 = new QHBoxLayout;
|
|||
|
|
pr3->addWidget(new QLabel(QString::fromUtf8("预览:")));
|
|||
|
|
pr3->addWidget(m_comboPreviewSide);
|
|||
|
|
pr3->addWidget(m_btnRefreshPreview); pr3->addWidget(m_labelViewInfo); pr3->addStretch();
|
|||
|
|
pvV->addLayout(pr3);
|
|||
|
|
|
|||
|
|
rightV->addWidget(pvG, 3);
|
|||
|
|
|
|||
|
|
auto* logG = new QGroupBox(QString::fromUtf8("日志"));
|
|||
|
|
auto* logV = new QVBoxLayout(logG);
|
|||
|
|
m_textLog = new QTextEdit; m_textLog->setReadOnly(true);
|
|||
|
|
m_textLog->setFont(QFont("Consolas", 10));
|
|||
|
|
m_textLog->setStyleSheet("background:#0d0d1a; color:#00cc88;");
|
|||
|
|
m_textLog->setMinimumHeight(150);
|
|||
|
|
logV->addWidget(m_textLog);
|
|||
|
|
rightV->addWidget(logG, 1);
|
|||
|
|
|
|||
|
|
auto* splitter = new QSplitter(Qt::Horizontal);
|
|||
|
|
splitter->addWidget(leftW); splitter->addWidget(rightW);
|
|||
|
|
splitter->setStretchFactor(0, 2); splitter->setStretchFactor(1, 3);
|
|||
|
|
mainHLay->addWidget(splitter);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void ShapeModelGenDialog::setupConnections()
|
|||
|
|
{
|
|||
|
|
connect(m_btnBrowseCAD, &QPushButton::clicked, this, &ShapeModelGenDialog::onBrowseCAD);
|
|||
|
|
connect(m_btnLoadCAD, &QPushButton::clicked, this, &ShapeModelGenDialog::onLoadCAD);
|
|||
|
|
connect(m_btnBrowseCamLeft, &QPushButton::clicked, this, &ShapeModelGenDialog::onBrowseCamParamLeft);
|
|||
|
|
connect(m_btnBrowseCamRight, &QPushButton::clicked, this, &ShapeModelGenDialog::onBrowseCamParamRight);
|
|||
|
|
connect(m_btnGenerate, &QPushButton::clicked, this, &ShapeModelGenDialog::onGenerate);
|
|||
|
|
connect(m_btnExport, &QPushButton::clicked, this, &ShapeModelGenDialog::onExportSM3);
|
|||
|
|
connect(m_btnApply, &QPushButton::clicked, this, &ShapeModelGenDialog::onApplyToMain);
|
|||
|
|
connect(m_btnImportSM3Left, &QPushButton::clicked, this, &ShapeModelGenDialog::onImportSM3Left);
|
|||
|
|
connect(m_btnImportSM3Right, &QPushButton::clicked, this, &ShapeModelGenDialog::onImportSM3Right);
|
|||
|
|
connect(m_btnRefreshPreview, &QPushButton::clicked, this, &ShapeModelGenDialog::onRefreshPreview);
|
|||
|
|
connect(m_chkCustomLevels, &QCheckBox::toggled, m_spinNumLevels, &QSpinBox::setEnabled);
|
|||
|
|
connect(m_chkCustomLowestLevel, &QCheckBox::toggled, m_spinLowestLevel, &QSpinBox::setEnabled);
|
|||
|
|
|
|||
|
|
auto refresh = [this]() { updatePreview(); };
|
|||
|
|
connect(m_spinPreviewLong, QOverload<double>::of(&QDoubleSpinBox::valueChanged), refresh);
|
|||
|
|
connect(m_spinPreviewLat, QOverload<double>::of(&QDoubleSpinBox::valueChanged), refresh);
|
|||
|
|
connect(m_spinPreviewRoll, QOverload<double>::of(&QDoubleSpinBox::valueChanged), refresh);
|
|||
|
|
connect(m_spinPreviewDist, QOverload<double>::of(&QDoubleSpinBox::valueChanged), refresh);
|
|||
|
|
connect(m_comboPreviewSide, QOverload<int>::of(&QComboBox::currentIndexChanged), refresh);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void ShapeModelGenDialog::log(const QString& msg)
|
|||
|
|
{
|
|||
|
|
QString ts = QDateTime::currentDateTime().toString("hh:mm:ss");
|
|||
|
|
m_textLog->append(QString("[%1] %2").arg(ts, sanitizeUserVisibleLog(msg)));
|
|||
|
|
QApplication::processEvents();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void ShapeModelGenDialog::setProgress(int pct, const QString& msg)
|
|||
|
|
{
|
|||
|
|
m_progress->setValue(pct);
|
|||
|
|
if (!msg.isEmpty()) log(msg);
|
|||
|
|
QApplication::processEvents();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ════════════════════════════════════════════════════════════════════════════
|
|||
|
|
// CAD 导入 + 自动中心化
|
|||
|
|
// ════════════════════════════════════════════════════════════════════════════
|
|||
|
|
void ShapeModelGenDialog::onBrowseCAD()
|
|||
|
|
{
|
|||
|
|
QString p = AppSettings::instance().openFile(
|
|||
|
|
this, AppSettings::K_CAD, QString::fromUtf8("导入 CAD 模型"),
|
|||
|
|
QString::fromUtf8("3D Models (*.stl *.STL *.obj *.ply);;All (*)"));
|
|||
|
|
if (!p.isEmpty()) { m_editCADPath->setText(p); onLoadCAD(); }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void ShapeModelGenDialog::onLoadCAD()
|
|||
|
|
{
|
|||
|
|
QString path = m_editCADPath->text();
|
|||
|
|
if (path.isEmpty() || !QFile::exists(path)) { log(QString::fromUtf8("[错误] CAD 文件不存在")); return; }
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
m_d->clearCAD();
|
|||
|
|
HTuple status;
|
|||
|
|
ReadObjectModel3d(HTuple(path.toLocal8Bit().constData()), HTuple("mm"), HTuple(), HTuple(), &m_d->objectModel3D, &status);
|
|||
|
|
|
|||
|
|
// 自动中心化
|
|||
|
|
HTuple bbox;
|
|||
|
|
GetObjectModel3dParams(m_d->objectModel3D, "bounding_box1", &bbox);
|
|||
|
|
double cx = (bbox[0].D()+bbox[3].D())/2, cy = (bbox[1].D()+bbox[4].D())/2, cz = (bbox[2].D()+bbox[5].D())/2;
|
|||
|
|
double sx = bbox[3].D()-bbox[0].D(), sy = bbox[4].D()-bbox[1].D(), sz = bbox[5].D()-bbox[2].D();
|
|||
|
|
if (std::abs(cx)>1e-9 || std::abs(cy)>1e-9 || std::abs(cz)>1e-9) {
|
|||
|
|
HTuple hm; HomMat3dIdentity(&hm); HomMat3dTranslate(hm, -cx, -cy, -cz, &hm);
|
|||
|
|
HTuple cm; AffineTransObjectModel3d(m_d->objectModel3D, hm, &cm);
|
|||
|
|
ClearObjectModel3d(m_d->objectModel3D); m_d->objectModel3D = cm;
|
|||
|
|
log(QString::fromUtf8(" 模型已自动中心化"));
|
|||
|
|
}
|
|||
|
|
PrepareObjectModel3d(m_d->objectModel3D, "shape_based_matching_3d", "true", HTuple(), HTuple());
|
|||
|
|
|
|||
|
|
m_d->cadLoaded = true; m_d->cadPath = path;
|
|||
|
|
try { HTuple i; GetObjectModel3dParams(m_d->objectModel3D,"num_points",&i); m_d->numVertices=i[0].I(); } catch(...){ m_d->numVertices=0; }
|
|||
|
|
try { HTuple i; GetObjectModel3dParams(m_d->objectModel3D,"num_triangles",&i); m_d->numFaces=i[0].I(); } catch(...){ m_d->numFaces=0; }
|
|||
|
|
|
|||
|
|
m_labelCADInfo->setText(QString::fromUtf8("✓ %1 (顶点:%2 面:%3 尺寸:%4×%5×%6mm)")
|
|||
|
|
.arg(QFileInfo(path).fileName()).arg(m_d->numVertices).arg(m_d->numFaces)
|
|||
|
|
.arg(sx*1000,0,'f',1).arg(sy*1000,0,'f',1).arg(sz*1000,0,'f',1));
|
|||
|
|
m_labelCADInfo->setStyleSheet("color:#00cc66; font-size:11px;");
|
|||
|
|
AppSettings::instance().set(AppSettings::K_CAD, path);
|
|||
|
|
m_btnGenerate->setEnabled(true);
|
|||
|
|
log(QString::fromUtf8("CAD 加载成功: %1").arg(QFileInfo(path).fileName()));
|
|||
|
|
} catch (HException& e) {
|
|||
|
|
m_d->cadLoaded = false; m_btnGenerate->setEnabled(false);
|
|||
|
|
m_labelCADInfo->setText(QString::fromUtf8("加载失败")); m_labelCADInfo->setStyleSheet("color:red;font-size:11px;");
|
|||
|
|
log(QString::fromUtf8("[VisionGuide] ") + sanitizeUserVisibleLog(QString::fromLocal8Bit(e.ErrorMessage().TextA())));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void ShapeModelGenDialog::onBrowseCamParamLeft()
|
|||
|
|
{
|
|||
|
|
QString p = AppSettings::instance().openFile(this, AppSettings::K_LeftCal,
|
|||
|
|
QString::fromUtf8("加载左相机内参"), QString::fromUtf8("VisionGuide Cal (*.cal);;All (*)"));
|
|||
|
|
if (p.isEmpty()) return;
|
|||
|
|
try {
|
|||
|
|
ReadCamPar(HTuple(p.toLocal8Bit().constData()), &m_d->camParam[0]);
|
|||
|
|
m_d->camParamLoaded[0] = true;
|
|||
|
|
m_editCamParamLeft->setText(p);
|
|||
|
|
m_labelCamLeftStatus->setText(QString::fromUtf8("✓ 已加载: ") + QFileInfo(p).fileName());
|
|||
|
|
m_labelCamLeftStatus->setStyleSheet("color:#00cc66;font-size:11px;");
|
|||
|
|
log(QString::fromUtf8("✓ 左相机参数: ") + QFileInfo(p).fileName());
|
|||
|
|
if (m_d->modelCreated[0]) updatePreview();
|
|||
|
|
} catch (HException& e) {
|
|||
|
|
m_labelCamLeftStatus->setText(QString::fromUtf8("加载失败"));
|
|||
|
|
m_labelCamLeftStatus->setStyleSheet("color:red;font-size:11px;");
|
|||
|
|
log(QString::fromUtf8("[错误] ") + sanitizeUserVisibleLog(QString::fromLocal8Bit(e.ErrorMessage().TextA())));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void ShapeModelGenDialog::onBrowseCamParamRight()
|
|||
|
|
{
|
|||
|
|
QString p = AppSettings::instance().openFile(this, AppSettings::K_RightCal,
|
|||
|
|
QString::fromUtf8("加载右相机内参"), QString::fromUtf8("VisionGuide Cal (*.cal);;All (*)"));
|
|||
|
|
if (p.isEmpty()) return;
|
|||
|
|
try {
|
|||
|
|
ReadCamPar(HTuple(p.toLocal8Bit().constData()), &m_d->camParam[1]);
|
|||
|
|
m_d->camParamLoaded[1] = true;
|
|||
|
|
m_editCamParamRight->setText(p);
|
|||
|
|
m_labelCamRightStatus->setText(QString::fromUtf8("✓ 已加载: ") + QFileInfo(p).fileName());
|
|||
|
|
m_labelCamRightStatus->setStyleSheet("color:#00cc66;font-size:11px;");
|
|||
|
|
log(QString::fromUtf8("✓ 右相机参数: ") + QFileInfo(p).fileName());
|
|||
|
|
if (m_d->modelCreated[1]) updatePreview();
|
|||
|
|
} catch (HException& e) {
|
|||
|
|
m_labelCamRightStatus->setText(QString::fromUtf8("加载失败"));
|
|||
|
|
m_labelCamRightStatus->setStyleSheet("color:red;font-size:11px;");
|
|||
|
|
log(QString::fromUtf8("[错误] ") + sanitizeUserVisibleLog(QString::fromLocal8Bit(e.ErrorMessage().TextA())));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ════════════════════════════════════════════════════════════════════════════
|
|||
|
|
// 生成 — 分别为左右相机生成轮廓库
|
|||
|
|
// ════════════════════════════════════════════════════════════════════════════
|
|||
|
|
void ShapeModelGenDialog::onGenerate()
|
|||
|
|
{
|
|||
|
|
if (!m_d->cadLoaded) { log(QString::fromUtf8("请先加载 CAD 模型")); return; }
|
|||
|
|
if (!m_d->camParamLoaded[0] && !m_d->camParamLoaded[1]) {
|
|||
|
|
log(QString::fromUtf8("[错误] 请至少加载一个相机的标定参数"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
saveParams();
|
|||
|
|
|
|||
|
|
log(QString::fromUtf8("═══ 开始生成3D形状模板 (左右分别生成) ═══"));
|
|||
|
|
setProgress(5);
|
|||
|
|
|
|||
|
|
double longMinR = deg2rad(m_spinLongMin->value());
|
|||
|
|
double longMaxR = deg2rad(m_spinLongMax->value());
|
|||
|
|
double latMinR = deg2rad(m_spinLatMin->value());
|
|||
|
|
double latMaxR = deg2rad(m_spinLatMax->value());
|
|||
|
|
double rollMinR = deg2rad(m_spinRollMin->value());
|
|||
|
|
double rollMaxR = deg2rad(m_spinRollMax->value());
|
|||
|
|
double distMin = m_spinDistMin->value();
|
|||
|
|
double distMax = m_spinDistMax->value();
|
|||
|
|
int minC = m_spinMinContrast->value();
|
|||
|
|
|
|||
|
|
log(QString::fromUtf8(" 经度: [%1°, %2°] 纬度: [%3°, %4°]")
|
|||
|
|
.arg(m_spinLongMin->value(),0,'f',1).arg(m_spinLongMax->value(),0,'f',1)
|
|||
|
|
.arg(m_spinLatMin->value(),0,'f',1).arg(m_spinLatMax->value(),0,'f',1));
|
|||
|
|
log(QString::fromUtf8(" 滚转: [%1°, %2°] 距离: [%3, %4]m MinContrast=%5")
|
|||
|
|
.arg(m_spinRollMin->value(),0,'f',1).arg(m_spinRollMax->value(),0,'f',1)
|
|||
|
|
.arg(distMin,0,'f',3).arg(distMax,0,'f',3).arg(minC));
|
|||
|
|
|
|||
|
|
HTuple gpn, gpv;
|
|||
|
|
if (m_chkCustomLevels->isChecked()) {
|
|||
|
|
gpn.Append("num_levels"); gpv.Append(m_spinNumLevels->value());
|
|||
|
|
}
|
|||
|
|
if (m_chkCustomLowestLevel->isChecked()) {
|
|||
|
|
gpn.Append("lowest_model_level"); gpv.Append(m_spinLowestLevel->value());
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const QString sideNames[2] = { QString::fromUtf8("左"), QString::fromUtf8("右") };
|
|||
|
|
int successCount = 0;
|
|||
|
|
|
|||
|
|
for (int side = 0; side < 2; ++side) {
|
|||
|
|
if (!m_d->camParamLoaded[side]) {
|
|||
|
|
log(QString::fromUtf8(" 跳过%1相机 (未加载标定参数)").arg(sideNames[side]));
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
int baseProgress = (side == 0) ? 10 : 55;
|
|||
|
|
setProgress(baseProgress, QString::fromUtf8("正在生成%1相机模板...").arg(sideNames[side]));
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
m_d->clearModel(side);
|
|||
|
|
|
|||
|
|
// 这三个参考旋转不是搜索范围。原 HDevelop 程序使用
|
|||
|
|
// RefRotX=-90°,用于把 STL 坐标系转到零件实际母线方向。
|
|||
|
|
CreateShapeModel3d(m_d->objectModel3D, m_d->camParam[side],
|
|||
|
|
deg2rad(-90.0), 0.0, 0.0, HTuple("gba"),
|
|||
|
|
longMinR, longMaxR, latMinR, latMaxR,
|
|||
|
|
rollMinR, rollMaxR, distMin, distMax,
|
|||
|
|
minC, gpn, gpv, &m_d->shapeModel3D[side]);
|
|||
|
|
|
|||
|
|
m_d->modelCreated[side] = true;
|
|||
|
|
successCount++;
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
HTuple n;
|
|||
|
|
GetShapeModel3dParams(m_d->shapeModel3D[side], "num_levels", &n);
|
|||
|
|
log(QString::fromUtf8(" %1相机模板: 金字塔层数=%2").arg(sideNames[side]).arg(n[0].I()));
|
|||
|
|
} catch (...) {}
|
|||
|
|
|
|||
|
|
log(QString::fromUtf8("✓ %1相机模板生成成功!").arg(sideNames[side]));
|
|||
|
|
setProgress(baseProgress + 40);
|
|||
|
|
|
|||
|
|
} catch (HException& e) {
|
|||
|
|
log(QString::fromUtf8("[VisionGuide] %1相机模板生成失败: %2")
|
|||
|
|
.arg(sideNames[side])
|
|||
|
|
.arg(sanitizeUserVisibleLog(QString::fromLocal8Bit(e.ErrorMessage().TextA()))));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (successCount > 0) {
|
|||
|
|
m_btnRefreshPreview->setEnabled(true);
|
|||
|
|
m_btnExport->setEnabled(true);
|
|||
|
|
m_btnApply->setEnabled(true);
|
|||
|
|
updatePreview();
|
|||
|
|
setProgress(100, QString::fromUtf8("生成完成 (成功%1个模板)").arg(successCount));
|
|||
|
|
} else {
|
|||
|
|
setProgress(0, QString::fromUtf8("生成失败"));
|
|||
|
|
QMessageBox::critical(this, QString::fromUtf8("生成失败"),
|
|||
|
|
QString::fromUtf8("左右相机模板均生成失败"));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ════════════════════════════════════════════════════════════════════════════
|
|||
|
|
// 预览 — ProjectShapeModel3d + 自由视角(度)
|
|||
|
|
// ════════════════════════════════════════════════════════════════════════════
|
|||
|
|
void ShapeModelGenDialog::onRefreshPreview() { updatePreview(); }
|
|||
|
|
|
|||
|
|
void ShapeModelGenDialog::updatePreview()
|
|||
|
|
{
|
|||
|
|
int side = m_comboPreviewSide->currentIndex(); // 0=左, 1=右
|
|||
|
|
if (!m_d->modelCreated[side]) {
|
|||
|
|
m_labelPreview->setText(QString::fromUtf8("该侧模板未生成"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
if (!m_d->camParamLoaded[side]) {
|
|||
|
|
m_labelPreview->setText(QString::fromUtf8("该侧相机参数未加载"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
double longD = m_spinPreviewLong->value();
|
|||
|
|
double latD = m_spinPreviewLat->value();
|
|||
|
|
double rollD = m_spinPreviewRoll->value();
|
|||
|
|
double dist = m_spinPreviewDist->value();
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
HTuple hm;
|
|||
|
|
HomMat3dIdentity(&hm);
|
|||
|
|
// 预览必须使用与 CreateShapeModel3d 相同的参考旋转,否则预览
|
|||
|
|
// 与实际生成/匹配的模型姿态会相差 90°。
|
|||
|
|
HomMat3dRotate(hm, deg2rad(-90.0), "x", 0,0,0, &hm);
|
|||
|
|
HomMat3dRotate(hm, deg2rad(longD), "y", 0,0,0, &hm);
|
|||
|
|
HomMat3dRotate(hm, deg2rad(latD), "x", 0,0,0, &hm);
|
|||
|
|
HomMat3dRotate(hm, deg2rad(rollD), "z", 0,0,0, &hm);
|
|||
|
|
HomMat3dTranslate(hm, 0, 0, dist, &hm);
|
|||
|
|
HTuple pose;
|
|||
|
|
HomMat3dToPose(hm, &pose);
|
|||
|
|
|
|||
|
|
HObject contours;
|
|||
|
|
ProjectShapeModel3d(&contours, m_d->shapeModel3D[side],
|
|||
|
|
m_d->camParam[side], pose,
|
|||
|
|
"true", HTuple(30.0*M_PI/180.0));
|
|||
|
|
|
|||
|
|
HTuple cc; CountObj(contours, &cc);
|
|||
|
|
if (cc[0].I() == 0) {
|
|||
|
|
m_labelPreview->setText(QString::fromUtf8("该视角下无可见轮廓"));
|
|||
|
|
m_labelViewInfo->setText(QString::fromUtf8("轮廓数=0")); return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
HTuple rMn, cMn, rMx, cMx;
|
|||
|
|
SmallestRectangle1Xld(contours, &rMn, &cMn, &rMx, &cMx);
|
|||
|
|
double rMin=rMn[0].D(), rMax=rMx[0].D(), cMin=cMn[0].D(), cMax=cMx[0].D();
|
|||
|
|
for (int i=0;i<rMn.Length();++i) {
|
|||
|
|
if (rMn[i].D()<rMin) rMin=rMn[i].D(); if (cMn[i].D()<cMin) cMin=cMn[i].D();
|
|||
|
|
if (rMx[i].D()>rMax) rMax=rMx[i].D(); if (cMx[i].D()>cMax) cMax=cMx[i].D();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
int imgW=512, imgH=512;
|
|||
|
|
QImage img(imgW, imgH, QImage::Format_RGB888); img.fill(QColor(26,26,46));
|
|||
|
|
QPainter painter(&img); painter.setRenderHint(QPainter::Antialiasing);
|
|||
|
|
double cW=qMax(cMax-cMin,1.0), cH=qMax(rMax-rMin,1.0);
|
|||
|
|
double sc=qMin((imgW-40)/cW,(imgH-40)/cH);
|
|||
|
|
double oX=(imgW-cW*sc)/2.0-cMin*sc, oY=(imgH-cH*sc)/2.0-rMin*sc;
|
|||
|
|
|
|||
|
|
QColor lineColor = (side == 0) ? QColor(0,200,100) : QColor(100,150,255);
|
|||
|
|
painter.setPen(QPen(lineColor, 1.5));
|
|||
|
|
for (int c=1;c<=cc[0].I();++c) {
|
|||
|
|
HObject sc2; SelectObj(contours,&sc2,c);
|
|||
|
|
HTuple rows,cols; GetContourXld(sc2,&rows,&cols);
|
|||
|
|
if (rows.Length()<2) continue;
|
|||
|
|
QVector<QPointF> pts;
|
|||
|
|
for (int p=0;p<rows.Length();++p) pts.append(QPointF(cols[p].D()*sc+oX, rows[p].D()*sc+oY));
|
|||
|
|
for (int p=0;p<pts.size()-1;++p) painter.drawLine(pts[p],pts[p+1]);
|
|||
|
|
}
|
|||
|
|
painter.setPen(Qt::white); painter.setFont(QFont("Consolas",10));
|
|||
|
|
QString sideLabel = (side == 0) ? QString::fromUtf8("[左相机]") : QString::fromUtf8("[右相机]");
|
|||
|
|
painter.drawText(10, imgH-10, QString::fromUtf8("%1 经度=%2° 纬度=%3° 滚转=%4° 距离=%5m")
|
|||
|
|
.arg(sideLabel).arg(longD,0,'f',1).arg(latD,0,'f',1).arg(rollD,0,'f',1).arg(dist,0,'f',3));
|
|||
|
|
painter.drawText(10, 20, QString::fromUtf8("轮廓数: %1").arg(cc[0].I()));
|
|||
|
|
painter.end();
|
|||
|
|
m_labelPreview->setPixmap(QPixmap::fromImage(img).scaled(m_labelPreview->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
|
|||
|
|
m_labelViewInfo->setText(QString::fromUtf8("轮廓数=%1").arg(cc[0].I()));
|
|||
|
|
} catch (HException& e) {
|
|||
|
|
log(QString::fromUtf8(" 预览异常: ") + sanitizeUserVisibleLog(QString::fromLocal8Bit(e.ErrorMessage().TextA())));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ════════════════════════════════════════════════════════════════════════════
|
|||
|
|
void ShapeModelGenDialog::onImportSM3Left()
|
|||
|
|
{
|
|||
|
|
QString p = AppSettings::instance().openFile(this, AppSettings::K_ShapeModelLeft,
|
|||
|
|
QString::fromUtf8("导入左相机3D形状模板"), QString::fromUtf8("VisionGuide SM3 (*.sm3);;All (*)"));
|
|||
|
|
if (p.isEmpty()) return;
|
|||
|
|
try {
|
|||
|
|
m_d->clearModel(0);
|
|||
|
|
ReadShapeModel3d(HTuple(p.toLocal8Bit().constData()), &m_d->shapeModel3D[0]);
|
|||
|
|
m_d->modelCreated[0] = true; m_d->sm3Path[0] = p; m_lastSM3PathLeft = p;
|
|||
|
|
m_btnRefreshPreview->setEnabled(true); m_btnExport->setEnabled(true); m_btnApply->setEnabled(true);
|
|||
|
|
m_comboPreviewSide->setCurrentIndex(0);
|
|||
|
|
updatePreview();
|
|||
|
|
log(QString::fromUtf8("✓ 左相机模板已导入: %1").arg(QFileInfo(p).fileName()));
|
|||
|
|
} catch (HException& e) {
|
|||
|
|
log(QString::fromUtf8("[VisionGuide] 导入失败: ") + sanitizeUserVisibleLog(QString::fromLocal8Bit(e.ErrorMessage().TextA())));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void ShapeModelGenDialog::onImportSM3Right()
|
|||
|
|
{
|
|||
|
|
QString p = AppSettings::instance().openFile(this, AppSettings::K_ShapeModelRight,
|
|||
|
|
QString::fromUtf8("导入右相机3D形状模板"), QString::fromUtf8("VisionGuide SM3 (*.sm3);;All (*)"));
|
|||
|
|
if (p.isEmpty()) return;
|
|||
|
|
try {
|
|||
|
|
m_d->clearModel(1);
|
|||
|
|
ReadShapeModel3d(HTuple(p.toLocal8Bit().constData()), &m_d->shapeModel3D[1]);
|
|||
|
|
m_d->modelCreated[1] = true; m_d->sm3Path[1] = p; m_lastSM3PathRight = p;
|
|||
|
|
m_btnRefreshPreview->setEnabled(true); m_btnExport->setEnabled(true); m_btnApply->setEnabled(true);
|
|||
|
|
m_comboPreviewSide->setCurrentIndex(1);
|
|||
|
|
updatePreview();
|
|||
|
|
log(QString::fromUtf8("✓ 右相机模板已导入: %1").arg(QFileInfo(p).fileName()));
|
|||
|
|
} catch (HException& e) {
|
|||
|
|
log(QString::fromUtf8("[VisionGuide] 导入失败: ") + sanitizeUserVisibleLog(QString::fromLocal8Bit(e.ErrorMessage().TextA())));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void ShapeModelGenDialog::onExportSM3()
|
|||
|
|
{
|
|||
|
|
const QString sideNames[2] = { QString::fromUtf8("左"), QString::fromUtf8("右") };
|
|||
|
|
const QString defaultNames[2] = { "left_part_shape_model_3d.sm3", "right_part_shape_model_3d.sm3" };
|
|||
|
|
|
|||
|
|
for (int side = 0; side < 2; ++side) {
|
|||
|
|
if (!m_d->modelCreated[side]) {
|
|||
|
|
log(QString::fromUtf8(" 跳过%1相机模板导出 (未生成)").arg(sideNames[side]));
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
QString p = QFileDialog::getSaveFileName(this,
|
|||
|
|
QString::fromUtf8("导出%1相机3D形状模板").arg(sideNames[side]),
|
|||
|
|
QCoreApplication::applicationDirPath() + "/" + defaultNames[side],
|
|||
|
|
"VisionGuide SM3 (*.sm3)");
|
|||
|
|
if (p.isEmpty()) continue;
|
|||
|
|
try {
|
|||
|
|
WriteShapeModel3d(m_d->shapeModel3D[side], HTuple(p.toLocal8Bit().constData()));
|
|||
|
|
if (side == 0) { m_lastSM3PathLeft = p; m_d->sm3Path[0] = p; }
|
|||
|
|
else { m_lastSM3PathRight = p; m_d->sm3Path[1] = p; }
|
|||
|
|
QString settingsKey = (side == 0) ? AppSettings::K_ShapeModelLeft : AppSettings::K_ShapeModelRight;
|
|||
|
|
AppSettings::instance().set(settingsKey, p);
|
|||
|
|
log(QString::fromUtf8("✓ %1相机模板已导出: %2").arg(sideNames[side], p));
|
|||
|
|
} catch (HException& e) {
|
|||
|
|
log(QString::fromUtf8("[VisionGuide] %1相机模板导出失败: %2")
|
|||
|
|
.arg(sideNames[side])
|
|||
|
|
.arg(sanitizeUserVisibleLog(QString::fromLocal8Bit(e.ErrorMessage().TextA()))));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void ShapeModelGenDialog::onApplyToMain()
|
|||
|
|
{
|
|||
|
|
if (!m_d->modelCreated[0] && !m_d->modelCreated[1]) {
|
|||
|
|
log(QString::fromUtf8("请先生成或导入模板"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const QString defaultNames[2] = { "left_part_shape_model_3d_generated.sm3",
|
|||
|
|
"right_part_shape_model_3d_generated.sm3" };
|
|||
|
|
QString* lastPaths[2] = { &m_lastSM3PathLeft, &m_lastSM3PathRight };
|
|||
|
|
|
|||
|
|
for (int side = 0; side < 2; ++side) {
|
|||
|
|
if (!m_d->modelCreated[side]) continue;
|
|||
|
|
if (lastPaths[side]->isEmpty() || !QFile::exists(*lastPaths[side])) {
|
|||
|
|
QString tmp = QCoreApplication::applicationDirPath() + "/" + defaultNames[side];
|
|||
|
|
try {
|
|||
|
|
WriteShapeModel3d(m_d->shapeModel3D[side], HTuple(tmp.toLocal8Bit().constData()));
|
|||
|
|
*lastPaths[side] = tmp;
|
|||
|
|
m_d->sm3Path[side] = tmp;
|
|||
|
|
} catch (HException& e) {
|
|||
|
|
log(QString::fromUtf8("[VisionGuide] 写入失败: ") + sanitizeUserVisibleLog(QString::fromLocal8Bit(e.ErrorMessage().TextA())));
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
m_appliedPathLeft = m_lastSM3PathLeft;
|
|||
|
|
m_appliedPathRight = m_lastSM3PathRight;
|
|||
|
|
|
|||
|
|
auto& s = AppSettings::instance();
|
|||
|
|
if (!m_appliedPathLeft.isEmpty())
|
|||
|
|
s.set(AppSettings::K_ShapeModelLeft, m_appliedPathLeft);
|
|||
|
|
if (!m_appliedPathRight.isEmpty())
|
|||
|
|
s.set(AppSettings::K_ShapeModelRight, m_appliedPathRight);
|
|||
|
|
|
|||
|
|
saveParams();
|
|||
|
|
|
|||
|
|
emit modelApplied(m_appliedPathLeft, m_appliedPathRight);
|
|||
|
|
|
|||
|
|
log(QString::fromUtf8("✓ 已应用:"));
|
|||
|
|
if (!m_appliedPathLeft.isEmpty())
|
|||
|
|
log(QString::fromUtf8(" 左相机: ") + m_appliedPathLeft);
|
|||
|
|
if (!m_appliedPathRight.isEmpty())
|
|||
|
|
log(QString::fromUtf8(" 右相机: ") + m_appliedPathRight);
|
|||
|
|
|
|||
|
|
accept();
|
|||
|
|
}
|