2026-09-18 16:22:20 +08:00
|
|
|
|
#include "urdfviewerwidget.h"
|
|
|
|
|
|
#include "uiicons.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include <Qt3DCore/QComponent>
|
|
|
|
|
|
#include <Qt3DCore/QEntity>
|
|
|
|
|
|
#include <Qt3DCore/QTransform>
|
2026-09-23 16:04:01 +08:00
|
|
|
|
#include <Qt3DExtras/QConeMesh>
|
2026-09-18 16:22:20 +08:00
|
|
|
|
#include <Qt3DExtras/QCuboidMesh>
|
|
|
|
|
|
#include <Qt3DExtras/QCylinderMesh>
|
|
|
|
|
|
#include <Qt3DExtras/QForwardRenderer>
|
|
|
|
|
|
#include <Qt3DExtras/QPerVertexColorMaterial>
|
|
|
|
|
|
#include <Qt3DExtras/QPhongMaterial>
|
|
|
|
|
|
#include <Qt3DExtras/QSphereMesh>
|
2026-09-23 16:04:01 +08:00
|
|
|
|
#include <Qt3DExtras/QText2DEntity>
|
2026-09-18 16:22:20 +08:00
|
|
|
|
#include <Qt3DExtras/Qt3DWindow>
|
|
|
|
|
|
#include <Qt3DRender/QAttribute>
|
|
|
|
|
|
#include <Qt3DRender/QBuffer>
|
|
|
|
|
|
#include <Qt3DRender/QCamera>
|
|
|
|
|
|
#include <Qt3DRender/QCameraLens>
|
2026-09-23 16:04:01 +08:00
|
|
|
|
#include <Qt3DRender/QCameraSelector>
|
|
|
|
|
|
#include <Qt3DRender/QClearBuffers>
|
2026-09-18 16:22:20 +08:00
|
|
|
|
#include <Qt3DRender/QGeometry>
|
|
|
|
|
|
#include <Qt3DRender/QGeometryRenderer>
|
2026-09-23 16:04:01 +08:00
|
|
|
|
#include <Qt3DRender/QLayer>
|
|
|
|
|
|
#include <Qt3DRender/QLayerFilter>
|
2026-09-18 16:22:20 +08:00
|
|
|
|
#include <Qt3DRender/QPickingSettings>
|
|
|
|
|
|
#include <Qt3DRender/QPointLight>
|
|
|
|
|
|
#include <Qt3DRender/QRayCasterHit>
|
|
|
|
|
|
#include <Qt3DRender/QRenderSettings>
|
2026-09-23 16:04:01 +08:00
|
|
|
|
#include <Qt3DRender/QRenderSurfaceSelector>
|
2026-09-18 16:22:20 +08:00
|
|
|
|
#include <Qt3DRender/QSceneLoader>
|
|
|
|
|
|
#include <Qt3DRender/QScreenRayCaster>
|
2026-09-23 16:04:01 +08:00
|
|
|
|
#include <Qt3DRender/QViewport>
|
2026-09-18 16:22:20 +08:00
|
|
|
|
|
|
|
|
|
|
#include <urdf_model/joint.h>
|
|
|
|
|
|
#include <urdf_model/link.h>
|
|
|
|
|
|
#include <urdf_model/model.h>
|
|
|
|
|
|
#include <urdf_parser/urdf_parser.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include <QColor>
|
2026-09-23 11:11:49 +08:00
|
|
|
|
#include <QComboBox>
|
2026-09-18 16:22:20 +08:00
|
|
|
|
#include <QCoreApplication>
|
|
|
|
|
|
#include <QCursor>
|
|
|
|
|
|
#include <QDir>
|
2026-09-23 11:11:49 +08:00
|
|
|
|
#include <QDirIterator>
|
2026-09-18 16:22:20 +08:00
|
|
|
|
#include <QEvent>
|
|
|
|
|
|
#include <QEventLoop>
|
|
|
|
|
|
#include <QFile>
|
|
|
|
|
|
#include <QFileDialog>
|
|
|
|
|
|
#include <QFileInfo>
|
2026-09-23 16:04:01 +08:00
|
|
|
|
#include <QFont>
|
2026-09-18 16:22:20 +08:00
|
|
|
|
#include <QFrame>
|
|
|
|
|
|
#include <QHBoxLayout>
|
|
|
|
|
|
#include <QLabel>
|
|
|
|
|
|
#include <QLineEdit>
|
2026-09-23 16:04:01 +08:00
|
|
|
|
#include <QMatrix3x3>
|
2026-09-18 16:22:20 +08:00
|
|
|
|
#include <QMouseEvent>
|
|
|
|
|
|
#include <QProgressBar>
|
|
|
|
|
|
#include <QPushButton>
|
|
|
|
|
|
#include <QQuaternion>
|
2026-09-23 11:11:49 +08:00
|
|
|
|
#include <QSignalBlocker>
|
2026-09-18 16:22:20 +08:00
|
|
|
|
#include <QShowEvent>
|
|
|
|
|
|
#include <QStyle>
|
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
|
#include <QUrl>
|
|
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
|
#include <QWheelEvent>
|
2026-09-23 11:11:49 +08:00
|
|
|
|
#include <QXmlStreamReader>
|
2026-09-18 16:22:20 +08:00
|
|
|
|
#include <QtMath>
|
|
|
|
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
QVector3D toVector(const urdf::Vector3 &value)
|
|
|
|
|
|
{
|
|
|
|
|
|
return QVector3D(static_cast<float>(value.x),
|
|
|
|
|
|
static_cast<float>(value.y),
|
|
|
|
|
|
static_cast<float>(value.z));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QQuaternion toQuaternion(const urdf::Rotation &value)
|
|
|
|
|
|
{
|
|
|
|
|
|
return QQuaternion(static_cast<float>(value.w),
|
|
|
|
|
|
static_cast<float>(value.x),
|
|
|
|
|
|
static_cast<float>(value.y),
|
|
|
|
|
|
static_cast<float>(value.z));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void applyPose(Qt3DCore::QTransform *transform, const urdf::Pose &pose)
|
|
|
|
|
|
{
|
|
|
|
|
|
transform->setTranslation(toVector(pose.position));
|
|
|
|
|
|
transform->setRotation(toQuaternion(pose.rotation));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-23 11:11:49 +08:00
|
|
|
|
bool needsAssimpZUpCompensation(const QString &source)
|
|
|
|
|
|
{
|
|
|
|
|
|
const QString suffix = QFileInfo(source).suffix().toLower();
|
|
|
|
|
|
if (suffix == QStringLiteral("3ds"))
|
|
|
|
|
|
return true;
|
|
|
|
|
|
if (suffix != QStringLiteral("dae"))
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
QFile file(source);
|
|
|
|
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
QXmlStreamReader xml(&file);
|
|
|
|
|
|
while (!xml.atEnd()) {
|
|
|
|
|
|
xml.readNext();
|
|
|
|
|
|
if (!xml.isStartElement()
|
|
|
|
|
|
|| xml.name().compare(QStringLiteral("up_axis"), Qt::CaseInsensitive) != 0) {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
return xml.readElementText().trimmed().compare(
|
|
|
|
|
|
QStringLiteral("Z_UP"), Qt::CaseInsensitive) == 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-18 16:22:20 +08:00
|
|
|
|
QColor visualColor(const urdf::VisualSharedPtr &visual)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!visual || !visual->material)
|
|
|
|
|
|
return QColor(QStringLiteral("#aeb8c4"));
|
|
|
|
|
|
const urdf::Color &color = visual->material->color;
|
|
|
|
|
|
return QColor::fromRgbF(qBound(0.0f, color.r, 1.0f),
|
|
|
|
|
|
qBound(0.0f, color.g, 1.0f),
|
|
|
|
|
|
qBound(0.0f, color.b, 1.0f),
|
|
|
|
|
|
qBound(0.0f, color.a, 1.0f));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Qt3DExtras::QPhongMaterial *makeMaterial(const urdf::VisualSharedPtr &visual,
|
|
|
|
|
|
Qt3DCore::QEntity *parent)
|
|
|
|
|
|
{
|
|
|
|
|
|
Qt3DExtras::QPhongMaterial *material = new Qt3DExtras::QPhongMaterial(parent);
|
|
|
|
|
|
const QColor color = visualColor(visual);
|
|
|
|
|
|
material->setDiffuse(color);
|
|
|
|
|
|
material->setAmbient(color.darker(170));
|
|
|
|
|
|
material->setSpecular(QColor(QStringLiteral("#dfe5eb")));
|
|
|
|
|
|
material->setShininess(24.0f);
|
|
|
|
|
|
return material;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Qt3DCore::QEntity *createReferenceGrid(Qt3DCore::QEntity *parent)
|
|
|
|
|
|
{
|
|
|
|
|
|
constexpr int subdivisions = 20;
|
|
|
|
|
|
constexpr float spacing = 0.1f;
|
|
|
|
|
|
constexpr float extent = subdivisions * spacing;
|
|
|
|
|
|
constexpr int floatsPerVertex = 7;
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<float> vertices;
|
|
|
|
|
|
vertices.reserve(static_cast<size_t>((subdivisions * 4 + 6) * 2 * floatsPerVertex));
|
|
|
|
|
|
|
|
|
|
|
|
auto appendVertex = [&vertices](float x, float y, float z, const QColor &color) {
|
|
|
|
|
|
vertices.push_back(x);
|
|
|
|
|
|
vertices.push_back(y);
|
|
|
|
|
|
vertices.push_back(z);
|
|
|
|
|
|
vertices.push_back(static_cast<float>(color.redF()));
|
|
|
|
|
|
vertices.push_back(static_cast<float>(color.greenF()));
|
|
|
|
|
|
vertices.push_back(static_cast<float>(color.blueF()));
|
|
|
|
|
|
vertices.push_back(static_cast<float>(color.alphaF()));
|
|
|
|
|
|
};
|
|
|
|
|
|
auto appendLine = [&appendVertex](const QVector3D &start, const QVector3D &end,
|
|
|
|
|
|
const QColor &color) {
|
|
|
|
|
|
appendVertex(start.x(), start.y(), start.z(), color);
|
|
|
|
|
|
appendVertex(end.x(), end.y(), end.z(), color);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const QColor minorColor(QStringLiteral("#aab4be"));
|
|
|
|
|
|
const QColor majorColor(QStringLiteral("#7f8d99"));
|
|
|
|
|
|
for (int i = -subdivisions; i <= subdivisions; ++i) {
|
|
|
|
|
|
if (i == 0)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
const float value = static_cast<float>(i) * spacing;
|
|
|
|
|
|
const QColor color = (i % 5 == 0) ? majorColor : minorColor;
|
|
|
|
|
|
appendLine(QVector3D(value, -extent, 0.0f), QVector3D(value, extent, 0.0f), color);
|
|
|
|
|
|
appendLine(QVector3D(-extent, value, 0.0f), QVector3D(extent, value, 0.0f), color);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
appendLine(QVector3D(-extent, 0.0f, 0.002f), QVector3D(extent, 0.0f, 0.002f),
|
|
|
|
|
|
QColor(QStringLiteral("#dc4c4c")));
|
|
|
|
|
|
appendLine(QVector3D(0.0f, -extent, 0.002f), QVector3D(0.0f, extent, 0.002f),
|
|
|
|
|
|
QColor(QStringLiteral("#3c9b62")));
|
|
|
|
|
|
appendLine(QVector3D(0.0f, 0.0f, 0.0f), QVector3D(0.0f, 0.0f, 1.0f),
|
|
|
|
|
|
QColor(QStringLiteral("#3478c7")));
|
|
|
|
|
|
|
|
|
|
|
|
Qt3DCore::QEntity *entity = new Qt3DCore::QEntity(parent);
|
|
|
|
|
|
entity->setObjectName(QStringLiteral("referenceGrid"));
|
|
|
|
|
|
Qt3DRender::QGeometry *geometry = new Qt3DRender::QGeometry(entity);
|
|
|
|
|
|
Qt3DRender::QBuffer *buffer = new Qt3DRender::QBuffer(geometry);
|
|
|
|
|
|
buffer->setData(QByteArray(reinterpret_cast<const char *>(vertices.data()),
|
|
|
|
|
|
static_cast<int>(vertices.size() * sizeof(float))));
|
|
|
|
|
|
|
|
|
|
|
|
const int vertexCount = static_cast<int>(vertices.size() / floatsPerVertex);
|
|
|
|
|
|
Qt3DRender::QAttribute *position = new Qt3DRender::QAttribute(geometry);
|
|
|
|
|
|
position->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
|
|
|
|
|
|
position->setVertexBaseType(Qt3DRender::QAttribute::Float);
|
|
|
|
|
|
position->setVertexSize(3);
|
|
|
|
|
|
position->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
|
|
|
|
|
|
position->setBuffer(buffer);
|
|
|
|
|
|
position->setByteStride(floatsPerVertex * static_cast<int>(sizeof(float)));
|
|
|
|
|
|
position->setByteOffset(0);
|
|
|
|
|
|
position->setCount(vertexCount);
|
|
|
|
|
|
geometry->addAttribute(position);
|
|
|
|
|
|
|
|
|
|
|
|
Qt3DRender::QAttribute *color = new Qt3DRender::QAttribute(geometry);
|
|
|
|
|
|
color->setName(Qt3DRender::QAttribute::defaultColorAttributeName());
|
|
|
|
|
|
color->setVertexBaseType(Qt3DRender::QAttribute::Float);
|
|
|
|
|
|
color->setVertexSize(4);
|
|
|
|
|
|
color->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
|
|
|
|
|
|
color->setBuffer(buffer);
|
|
|
|
|
|
color->setByteStride(floatsPerVertex * static_cast<int>(sizeof(float)));
|
|
|
|
|
|
color->setByteOffset(3 * static_cast<int>(sizeof(float)));
|
|
|
|
|
|
color->setCount(vertexCount);
|
|
|
|
|
|
geometry->addAttribute(color);
|
|
|
|
|
|
|
|
|
|
|
|
Qt3DRender::QGeometryRenderer *renderer = new Qt3DRender::QGeometryRenderer(entity);
|
|
|
|
|
|
renderer->setGeometry(geometry);
|
|
|
|
|
|
renderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Lines);
|
|
|
|
|
|
renderer->setVertexCount(vertexCount);
|
|
|
|
|
|
entity->addComponent(renderer);
|
|
|
|
|
|
entity->addComponent(new Qt3DExtras::QPerVertexColorMaterial(entity));
|
|
|
|
|
|
return entity;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const QStringList &motionDemoJointNames()
|
|
|
|
|
|
{
|
|
|
|
|
|
static const QStringList names = {
|
|
|
|
|
|
QStringLiteral("L_SHOULDER_P"), QStringLiteral("L_SHOULDER_R"),
|
|
|
|
|
|
QStringLiteral("L_SHOULDER_Y"), QStringLiteral("L_ELBOW_R"),
|
|
|
|
|
|
QStringLiteral("L_WRIST_P"), QStringLiteral("L_WRIST_Y"),
|
|
|
|
|
|
QStringLiteral("L_WRIST_R"), QStringLiteral("R_SHOULDER_P"),
|
|
|
|
|
|
QStringLiteral("R_SHOULDER_R"), QStringLiteral("R_SHOULDER_Y"),
|
|
|
|
|
|
QStringLiteral("R_ELBOW_R"), QStringLiteral("R_WRIST_P"),
|
|
|
|
|
|
QStringLiteral("R_WRIST_Y"), QStringLiteral("R_WRIST_R")
|
|
|
|
|
|
};
|
|
|
|
|
|
return names;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const QVector<QVector<double>> &motionDemoPoses()
|
|
|
|
|
|
{
|
|
|
|
|
|
static const QVector<QVector<double>> poses = {
|
|
|
|
|
|
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
|
|
|
|
|
|
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
|
|
|
|
|
|
{0.0, -1.15, -0.15, -0.20, 0.20, 0.0, -0.10,
|
|
|
|
|
|
0.0, 1.15, 0.15, 0.20, -0.20, 0.0, 0.10},
|
|
|
|
|
|
{-0.70, -0.30, -0.50, -0.80, 0.90, 0.0, -0.20,
|
|
|
|
|
|
0.70, 0.30, 0.50, 0.80, -0.90, 0.0, 0.20},
|
|
|
|
|
|
{0.30, -0.65, -1.00, -1.55, 1.40, 0.30, -0.50,
|
|
|
|
|
|
-0.30, 0.65, 1.00, 1.55, -1.40, -0.30, 0.50},
|
|
|
|
|
|
{0.0, -0.35, -0.35, -0.70, 0.70, 0.0, -0.20,
|
|
|
|
|
|
-0.45, 1.20, -0.60, 1.10, -0.80, 0.50, 0.80}
|
|
|
|
|
|
};
|
|
|
|
|
|
return poses;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const QStringList &motionDemoPoseNames()
|
|
|
|
|
|
{
|
|
|
|
|
|
static const QStringList names = {
|
|
|
|
|
|
QStringLiteral("归零"), QStringLiteral("展臂"), QStringLiteral("前伸"),
|
|
|
|
|
|
QStringLiteral("屈臂"), QStringLiteral("单臂动作")
|
|
|
|
|
|
};
|
|
|
|
|
|
return names;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UrdfViewerWidget::UrdfViewerWidget(QWidget *parent)
|
|
|
|
|
|
: QWidget(parent)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_layout = new QVBoxLayout(this);
|
|
|
|
|
|
m_layout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
|
|
m_layout->setSpacing(0);
|
|
|
|
|
|
|
|
|
|
|
|
QFrame *toolbar = new QFrame;
|
|
|
|
|
|
toolbar->setObjectName(QStringLiteral("urdfToolbar"));
|
|
|
|
|
|
QHBoxLayout *toolbarLayout = new QHBoxLayout(toolbar);
|
|
|
|
|
|
toolbarLayout->setContentsMargins(10, 6, 10, 6);
|
|
|
|
|
|
toolbarLayout->setSpacing(8);
|
|
|
|
|
|
|
2026-09-23 11:11:49 +08:00
|
|
|
|
toolbarLayout->addWidget(new QLabel(QStringLiteral("模型"), toolbar));
|
|
|
|
|
|
m_modelCombo = new QComboBox(toolbar);
|
|
|
|
|
|
m_modelCombo->setObjectName(QStringLiteral("urdfModelCombo"));
|
|
|
|
|
|
m_modelCombo->setMinimumWidth(240);
|
|
|
|
|
|
m_modelCombo->setMaxVisibleItems(24);
|
|
|
|
|
|
m_modelCombo->setToolTip(QStringLiteral("选择随程序提供的机械臂 URDF 模型"));
|
|
|
|
|
|
connect(m_modelCombo, QOverload<int>::of(&QComboBox::activated),
|
|
|
|
|
|
this, &UrdfViewerWidget::chooseBundledUrdf);
|
|
|
|
|
|
toolbarLayout->addWidget(m_modelCombo, 1);
|
|
|
|
|
|
|
2026-09-18 16:22:20 +08:00
|
|
|
|
m_pathEdit = new QLineEdit;
|
|
|
|
|
|
m_pathEdit->setReadOnly(true);
|
|
|
|
|
|
m_pathEdit->setPlaceholderText(QStringLiteral("未加载 URDF 模型"));
|
2026-09-23 11:11:49 +08:00
|
|
|
|
m_pathEdit->setMinimumWidth(140);
|
|
|
|
|
|
toolbarLayout->addWidget(m_pathEdit);
|
2026-09-18 16:22:20 +08:00
|
|
|
|
|
|
|
|
|
|
QPushButton *openButton = new QPushButton(
|
|
|
|
|
|
UiIcons::icon(QStringLiteral(":/icons/folder-open.svg")), QString());
|
|
|
|
|
|
openButton->setObjectName(QStringLiteral("iconButton"));
|
|
|
|
|
|
openButton->setFixedSize(32, 32);
|
|
|
|
|
|
openButton->setToolTip(QStringLiteral("选择 URDF 模型文件"));
|
|
|
|
|
|
connect(openButton, &QPushButton::clicked, this, &UrdfViewerWidget::chooseUrdf);
|
|
|
|
|
|
toolbarLayout->addWidget(openButton);
|
|
|
|
|
|
|
|
|
|
|
|
m_reloadButton = new QPushButton(
|
|
|
|
|
|
UiIcons::icon(QStringLiteral(":/icons/rotate-ccw.svg")), QString());
|
|
|
|
|
|
m_reloadButton->setObjectName(QStringLiteral("iconButton"));
|
|
|
|
|
|
m_reloadButton->setToolTip(QStringLiteral("重新加载当前 URDF"));
|
|
|
|
|
|
m_reloadButton->setFixedSize(32, 32);
|
|
|
|
|
|
m_reloadButton->setEnabled(false);
|
|
|
|
|
|
connect(m_reloadButton, &QPushButton::clicked, this, &UrdfViewerWidget::reloadUrdf);
|
|
|
|
|
|
toolbarLayout->addWidget(m_reloadButton);
|
|
|
|
|
|
|
|
|
|
|
|
QPushButton *resetButton = new QPushButton(
|
|
|
|
|
|
UiIcons::icon(QStringLiteral(":/icons/maximize.svg")), QString());
|
|
|
|
|
|
resetButton->setObjectName(QStringLiteral("iconButton"));
|
|
|
|
|
|
resetButton->setToolTip(QStringLiteral("复位模型视角"));
|
|
|
|
|
|
resetButton->setFixedSize(32, 32);
|
|
|
|
|
|
connect(resetButton, &QPushButton::clicked, this, &UrdfViewerWidget::resetView);
|
|
|
|
|
|
toolbarLayout->addWidget(resetButton);
|
|
|
|
|
|
|
|
|
|
|
|
m_demoButton = new QPushButton(
|
|
|
|
|
|
UiIcons::icon(QStringLiteral(":/icons/play.svg")), QString());
|
|
|
|
|
|
m_demoButton->setObjectName(QStringLiteral("iconButton"));
|
|
|
|
|
|
m_demoButton->setToolTip(QStringLiteral("播放本地关节动作"));
|
|
|
|
|
|
m_demoButton->setCheckable(true);
|
|
|
|
|
|
m_demoButton->setFixedSize(32, 32);
|
|
|
|
|
|
connect(m_demoButton, &QPushButton::toggled,
|
|
|
|
|
|
this, &UrdfViewerWidget::setMotionDemoRunning);
|
|
|
|
|
|
toolbarLayout->addWidget(m_demoButton);
|
|
|
|
|
|
m_layout->addWidget(toolbar);
|
|
|
|
|
|
|
|
|
|
|
|
m_viewportPlaceholder = new QWidget(this);
|
|
|
|
|
|
m_viewportPlaceholder->setObjectName(QStringLiteral("urdfLoadingView"));
|
|
|
|
|
|
m_viewportPlaceholder->setMinimumHeight(320);
|
|
|
|
|
|
QVBoxLayout *loadingLayout = new QVBoxLayout(m_viewportPlaceholder);
|
|
|
|
|
|
loadingLayout->setContentsMargins(48, 48, 48, 48);
|
|
|
|
|
|
loadingLayout->setSpacing(10);
|
|
|
|
|
|
loadingLayout->addStretch(1);
|
|
|
|
|
|
m_loadingTitle = new QLabel(QStringLiteral("3D 模型尚未初始化"), m_viewportPlaceholder);
|
|
|
|
|
|
m_loadingTitle->setObjectName(QStringLiteral("urdfLoadingTitle"));
|
|
|
|
|
|
m_loadingTitle->setAlignment(Qt::AlignCenter);
|
|
|
|
|
|
loadingLayout->addWidget(m_loadingTitle);
|
|
|
|
|
|
m_loadingDetail = new QLabel(QStringLiteral("首次进入机械臂服务时加载"),
|
|
|
|
|
|
m_viewportPlaceholder);
|
|
|
|
|
|
m_loadingDetail->setObjectName(QStringLiteral("urdfLoadingDetail"));
|
|
|
|
|
|
m_loadingDetail->setAlignment(Qt::AlignCenter);
|
|
|
|
|
|
loadingLayout->addWidget(m_loadingDetail);
|
|
|
|
|
|
m_loadingProgress = new QProgressBar(m_viewportPlaceholder);
|
|
|
|
|
|
m_loadingProgress->setObjectName(QStringLiteral("urdfLoadingProgress"));
|
|
|
|
|
|
m_loadingProgress->setRange(0, 0);
|
|
|
|
|
|
m_loadingProgress->setTextVisible(false);
|
|
|
|
|
|
m_loadingProgress->setMaximumWidth(260);
|
|
|
|
|
|
m_loadingProgress->setVisible(false);
|
|
|
|
|
|
loadingLayout->addWidget(m_loadingProgress, 0, Qt::AlignHCenter);
|
|
|
|
|
|
loadingLayout->addStretch(1);
|
|
|
|
|
|
m_layout->addWidget(m_viewportPlaceholder, 1);
|
|
|
|
|
|
|
|
|
|
|
|
QFrame *statusBar = new QFrame;
|
|
|
|
|
|
statusBar->setObjectName(QStringLiteral("urdfStatusBar"));
|
|
|
|
|
|
QHBoxLayout *statusLayout = new QHBoxLayout(statusBar);
|
|
|
|
|
|
statusLayout->setContentsMargins(12, 8, 12, 8);
|
|
|
|
|
|
m_statusLabel = new QLabel(QStringLiteral("等待打开机械臂服务"));
|
|
|
|
|
|
m_statusLabel->setObjectName(QStringLiteral("mutedLabel"));
|
|
|
|
|
|
statusLayout->addWidget(m_statusLabel);
|
|
|
|
|
|
statusLayout->addStretch(1);
|
|
|
|
|
|
statusLayout->addWidget(new QLabel(QStringLiteral("URDF / Qt3D")));
|
|
|
|
|
|
m_layout->addWidget(statusBar);
|
|
|
|
|
|
|
|
|
|
|
|
m_demoTimer = new QTimer(this);
|
|
|
|
|
|
m_demoTimer->setInterval(40);
|
|
|
|
|
|
connect(m_demoTimer, &QTimer::timeout, this, &UrdfViewerWidget::advanceMotionDemo);
|
2026-09-23 11:11:49 +08:00
|
|
|
|
|
|
|
|
|
|
refreshBundledModels();
|
2026-09-18 16:22:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UrdfViewerWidget::ensureSceneInitialized()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_sceneInitialized)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
m_sceneInitialized = true;
|
|
|
|
|
|
m_view = new Qt3DExtras::Qt3DWindow;
|
|
|
|
|
|
m_view->defaultFrameGraph()->setClearColor(QColor(QStringLiteral("#c7ced6")));
|
|
|
|
|
|
m_view->renderSettings()->setRenderPolicy(Qt3DRender::QRenderSettings::OnDemand);
|
|
|
|
|
|
Qt3DRender::QPickingSettings *pickingSettings =
|
|
|
|
|
|
m_view->renderSettings()->pickingSettings();
|
|
|
|
|
|
pickingSettings->setPickMethod(Qt3DRender::QPickingSettings::TrianglePicking);
|
|
|
|
|
|
pickingSettings->setPickResultMode(Qt3DRender::QPickingSettings::AllPicks);
|
|
|
|
|
|
pickingSettings->setFaceOrientationPickingMode(
|
|
|
|
|
|
Qt3DRender::QPickingSettings::FrontAndBackFace);
|
|
|
|
|
|
m_viewContainer = QWidget::createWindowContainer(m_view, this);
|
|
|
|
|
|
m_viewContainer->setObjectName(QStringLiteral("urdfViewport"));
|
|
|
|
|
|
m_viewContainer->setMinimumHeight(320);
|
|
|
|
|
|
m_viewContainer->setFocusPolicy(Qt::StrongFocus);
|
|
|
|
|
|
m_viewContainer->installEventFilter(this);
|
|
|
|
|
|
m_view->installEventFilter(this);
|
|
|
|
|
|
m_layout->insertWidget(1, m_viewContainer, 1);
|
|
|
|
|
|
|
|
|
|
|
|
m_rootEntity = new Qt3DCore::QEntity;
|
|
|
|
|
|
createReferenceGrid(m_rootEntity);
|
|
|
|
|
|
m_teachingRayCaster = new Qt3DRender::QScreenRayCaster(m_rootEntity);
|
|
|
|
|
|
m_teachingRayCaster->setRunMode(Qt3DRender::QAbstractRayCaster::SingleShot);
|
|
|
|
|
|
m_rootEntity->addComponent(m_teachingRayCaster);
|
|
|
|
|
|
connect(m_teachingRayCaster, &Qt3DRender::QAbstractRayCaster::hitsChanged,
|
|
|
|
|
|
this, [this](const Qt3DRender::QAbstractRayCaster::Hits &hits) {
|
|
|
|
|
|
if (!m_teachingEnabled)
|
|
|
|
|
|
return;
|
|
|
|
|
|
for (const Qt3DRender::QRayCasterHit &hit : hits) {
|
|
|
|
|
|
QObject *node = hit.entity();
|
|
|
|
|
|
while (node) {
|
|
|
|
|
|
const QString jointName =
|
|
|
|
|
|
node->property("cmvrTeachingJoint").toString();
|
|
|
|
|
|
if (m_joints.contains(jointName)) {
|
|
|
|
|
|
selectTeachingJoint(jointName);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
node = node->parent();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
m_camera = m_view->camera();
|
|
|
|
|
|
m_camera->lens()->setPerspectiveProjection(42.0f, 16.0f / 9.0f, 0.01f, 1000.0f);
|
|
|
|
|
|
|
2026-09-23 16:04:01 +08:00
|
|
|
|
createOrientationIndicator();
|
|
|
|
|
|
connect(m_camera, &Qt3DRender::QCamera::positionChanged,
|
|
|
|
|
|
this, [this](const QVector3D &) { updateOrientationIndicator(); });
|
|
|
|
|
|
connect(m_camera, &Qt3DRender::QCamera::viewCenterChanged,
|
|
|
|
|
|
this, [this](const QVector3D &) { updateOrientationIndicator(); });
|
|
|
|
|
|
connect(m_camera, &Qt3DRender::QCamera::upVectorChanged,
|
|
|
|
|
|
this, [this](const QVector3D &) { updateOrientationIndicator(); });
|
|
|
|
|
|
|
2026-09-18 16:22:20 +08:00
|
|
|
|
Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity(m_rootEntity);
|
|
|
|
|
|
Qt3DRender::QPointLight *light = new Qt3DRender::QPointLight(lightEntity);
|
|
|
|
|
|
light->setColor(QColor(QStringLiteral("#ffffff")));
|
|
|
|
|
|
light->setIntensity(1.35f);
|
|
|
|
|
|
Qt3DCore::QTransform *lightTransform = new Qt3DCore::QTransform(lightEntity);
|
|
|
|
|
|
lightTransform->setTranslation(QVector3D(4.0f, -4.0f, 6.0f));
|
|
|
|
|
|
lightEntity->addComponent(light);
|
|
|
|
|
|
lightEntity->addComponent(lightTransform);
|
|
|
|
|
|
|
|
|
|
|
|
m_view->setRootEntity(m_rootEntity);
|
|
|
|
|
|
if (m_viewportPlaceholder)
|
|
|
|
|
|
m_viewportPlaceholder->hide();
|
|
|
|
|
|
m_viewContainer->show();
|
|
|
|
|
|
m_viewContainer->raise();
|
|
|
|
|
|
m_view->requestUpdate();
|
|
|
|
|
|
resetView();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-23 16:04:01 +08:00
|
|
|
|
void UrdfViewerWidget::createOrientationIndicator()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_view || !m_rootEntity || m_orientationCamera)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
constexpr float shaftLength = 0.78f;
|
|
|
|
|
|
constexpr float headLength = 0.24f;
|
|
|
|
|
|
constexpr float labelDistance = 1.12f;
|
|
|
|
|
|
|
|
|
|
|
|
Qt3DCore::QEntity *root = new Qt3DCore::QEntity(m_rootEntity);
|
|
|
|
|
|
root->setObjectName(QStringLiteral("orientationIndicator"));
|
|
|
|
|
|
|
|
|
|
|
|
m_orientationLayer = new Qt3DRender::QLayer(root);
|
|
|
|
|
|
m_orientationLayer->setRecursive(true);
|
|
|
|
|
|
root->addComponent(m_orientationLayer);
|
|
|
|
|
|
|
|
|
|
|
|
const auto createMaterial = [root](const QColor &color) {
|
|
|
|
|
|
Qt3DExtras::QPhongMaterial *material =
|
|
|
|
|
|
new Qt3DExtras::QPhongMaterial(root);
|
|
|
|
|
|
material->setAmbient(color);
|
|
|
|
|
|
material->setDiffuse(color);
|
|
|
|
|
|
material->setSpecular(QColor(QStringLiteral("#ffffff")));
|
|
|
|
|
|
material->setShininess(36.0f);
|
|
|
|
|
|
return material;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const auto addAxis = [this, root, &createMaterial](
|
|
|
|
|
|
const QVector3D &direction,
|
|
|
|
|
|
const QColor &color,
|
|
|
|
|
|
const QString &name) {
|
|
|
|
|
|
Qt3DCore::QEntity *shaft = new Qt3DCore::QEntity(root);
|
|
|
|
|
|
Qt3DExtras::QCylinderMesh *shaftMesh =
|
|
|
|
|
|
new Qt3DExtras::QCylinderMesh(shaft);
|
|
|
|
|
|
shaftMesh->setRadius(0.035f);
|
|
|
|
|
|
shaftMesh->setLength(shaftLength);
|
|
|
|
|
|
shaftMesh->setRings(2);
|
|
|
|
|
|
shaftMesh->setSlices(24);
|
|
|
|
|
|
Qt3DCore::QTransform *shaftTransform =
|
|
|
|
|
|
new Qt3DCore::QTransform(shaft);
|
|
|
|
|
|
shaftTransform->setTranslation(direction * (shaftLength * 0.5f));
|
|
|
|
|
|
shaftTransform->setRotation(QQuaternion::rotationTo(
|
|
|
|
|
|
QVector3D(0.0f, 1.0f, 0.0f), direction));
|
|
|
|
|
|
shaft->addComponent(shaftMesh);
|
|
|
|
|
|
shaft->addComponent(createMaterial(color));
|
|
|
|
|
|
shaft->addComponent(shaftTransform);
|
|
|
|
|
|
|
|
|
|
|
|
Qt3DCore::QEntity *head = new Qt3DCore::QEntity(root);
|
|
|
|
|
|
Qt3DExtras::QConeMesh *headMesh = new Qt3DExtras::QConeMesh(head);
|
|
|
|
|
|
headMesh->setBottomRadius(0.105f);
|
|
|
|
|
|
headMesh->setTopRadius(0.0f);
|
|
|
|
|
|
headMesh->setLength(headLength);
|
|
|
|
|
|
headMesh->setRings(2);
|
|
|
|
|
|
headMesh->setSlices(24);
|
|
|
|
|
|
Qt3DCore::QTransform *headTransform = new Qt3DCore::QTransform(head);
|
|
|
|
|
|
headTransform->setTranslation(
|
|
|
|
|
|
direction * (shaftLength + headLength * 0.5f));
|
|
|
|
|
|
headTransform->setRotation(QQuaternion::rotationTo(
|
|
|
|
|
|
QVector3D(0.0f, 1.0f, 0.0f), direction));
|
|
|
|
|
|
head->addComponent(headMesh);
|
|
|
|
|
|
head->addComponent(createMaterial(color));
|
|
|
|
|
|
head->addComponent(headTransform);
|
|
|
|
|
|
|
|
|
|
|
|
Qt3DExtras::QText2DEntity *label =
|
|
|
|
|
|
new Qt3DExtras::QText2DEntity(root);
|
|
|
|
|
|
QFont font;
|
|
|
|
|
|
font.setPointSize(22);
|
|
|
|
|
|
font.setBold(true);
|
|
|
|
|
|
label->setFont(font);
|
|
|
|
|
|
label->setText(name);
|
|
|
|
|
|
label->setColor(color.darker(115));
|
|
|
|
|
|
label->setWidth(42.0f);
|
|
|
|
|
|
label->setHeight(34.0f);
|
|
|
|
|
|
Qt3DCore::QTransform *labelTransform =
|
|
|
|
|
|
new Qt3DCore::QTransform(label);
|
|
|
|
|
|
labelTransform->setTranslation(direction * labelDistance);
|
|
|
|
|
|
labelTransform->setScale(0.0075f);
|
|
|
|
|
|
label->addComponent(labelTransform);
|
|
|
|
|
|
m_orientationLabelTransforms.append(labelTransform);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
addAxis(QVector3D(1.0f, 0.0f, 0.0f),
|
|
|
|
|
|
QColor(QStringLiteral("#e24848")), QStringLiteral("X"));
|
|
|
|
|
|
addAxis(QVector3D(0.0f, 1.0f, 0.0f),
|
|
|
|
|
|
QColor(QStringLiteral("#25a05a")), QStringLiteral("Y"));
|
|
|
|
|
|
addAxis(QVector3D(0.0f, 0.0f, 1.0f),
|
|
|
|
|
|
QColor(QStringLiteral("#2575d8")), QStringLiteral("Z"));
|
|
|
|
|
|
|
|
|
|
|
|
Qt3DCore::QEntity *origin = new Qt3DCore::QEntity(root);
|
|
|
|
|
|
Qt3DExtras::QSphereMesh *originMesh =
|
|
|
|
|
|
new Qt3DExtras::QSphereMesh(origin);
|
|
|
|
|
|
originMesh->setRadius(0.085f);
|
|
|
|
|
|
originMesh->setRings(16);
|
|
|
|
|
|
originMesh->setSlices(24);
|
|
|
|
|
|
origin->addComponent(originMesh);
|
|
|
|
|
|
origin->addComponent(createMaterial(QColor(QStringLiteral("#60758a"))));
|
|
|
|
|
|
|
|
|
|
|
|
m_orientationCamera = new Qt3DRender::QCamera(root);
|
|
|
|
|
|
m_orientationCamera->lens()->setOrthographicProjection(
|
|
|
|
|
|
-1.42f, 1.42f, -1.42f, 1.42f, 0.1f, 20.0f);
|
|
|
|
|
|
m_orientationCamera->setViewCenter(QVector3D(0.0f, 0.0f, 0.0f));
|
|
|
|
|
|
|
|
|
|
|
|
Qt3DRender::QRenderSurfaceSelector *surfaceSelector =
|
|
|
|
|
|
new Qt3DRender::QRenderSurfaceSelector;
|
|
|
|
|
|
surfaceSelector->setSurface(m_view);
|
|
|
|
|
|
|
|
|
|
|
|
Qt3DRender::QViewport *mainViewport =
|
|
|
|
|
|
new Qt3DRender::QViewport(surfaceSelector);
|
|
|
|
|
|
mainViewport->setNormalizedRect(QRectF(0.0, 0.0, 1.0, 1.0));
|
|
|
|
|
|
Qt3DRender::QClearBuffers *mainClear =
|
|
|
|
|
|
new Qt3DRender::QClearBuffers(mainViewport);
|
|
|
|
|
|
mainClear->setBuffers(Qt3DRender::QClearBuffers::ColorDepthBuffer);
|
|
|
|
|
|
mainClear->setClearColor(QColor(QStringLiteral("#c7ced6")));
|
|
|
|
|
|
Qt3DRender::QCameraSelector *mainCameraSelector =
|
|
|
|
|
|
new Qt3DRender::QCameraSelector(mainClear);
|
|
|
|
|
|
mainCameraSelector->setCamera(m_camera);
|
|
|
|
|
|
Qt3DRender::QLayerFilter *mainLayerFilter =
|
|
|
|
|
|
new Qt3DRender::QLayerFilter(mainCameraSelector);
|
|
|
|
|
|
mainLayerFilter->setFilterMode(
|
|
|
|
|
|
Qt3DRender::QLayerFilter::DiscardAnyMatchingLayers);
|
|
|
|
|
|
mainLayerFilter->addLayer(m_orientationLayer);
|
|
|
|
|
|
|
|
|
|
|
|
m_orientationViewport = new Qt3DRender::QViewport(surfaceSelector);
|
|
|
|
|
|
Qt3DRender::QClearBuffers *orientationDepthClear =
|
|
|
|
|
|
new Qt3DRender::QClearBuffers(m_orientationViewport);
|
|
|
|
|
|
orientationDepthClear->setBuffers(Qt3DRender::QClearBuffers::DepthBuffer);
|
|
|
|
|
|
Qt3DRender::QCameraSelector *orientationCameraSelector =
|
|
|
|
|
|
new Qt3DRender::QCameraSelector(orientationDepthClear);
|
|
|
|
|
|
orientationCameraSelector->setCamera(m_orientationCamera);
|
|
|
|
|
|
Qt3DRender::QLayerFilter *orientationLayerFilter =
|
|
|
|
|
|
new Qt3DRender::QLayerFilter(orientationCameraSelector);
|
|
|
|
|
|
orientationLayerFilter->setFilterMode(
|
|
|
|
|
|
Qt3DRender::QLayerFilter::AcceptAnyMatchingLayers);
|
|
|
|
|
|
orientationLayerFilter->addLayer(m_orientationLayer);
|
|
|
|
|
|
|
|
|
|
|
|
m_view->setActiveFrameGraph(surfaceSelector);
|
|
|
|
|
|
positionOrientationIndicator();
|
|
|
|
|
|
updateOrientationIndicator();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UrdfViewerWidget::updateOrientationIndicator()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_camera || !m_orientationCamera || !m_view)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
QVector3D forward = m_camera->viewCenter() - m_camera->position();
|
|
|
|
|
|
if (forward.lengthSquared() < 1.0e-8f)
|
|
|
|
|
|
return;
|
|
|
|
|
|
forward.normalize();
|
|
|
|
|
|
|
|
|
|
|
|
QVector3D up = m_camera->upVector();
|
|
|
|
|
|
if (up.lengthSquared() < 1.0e-8f)
|
|
|
|
|
|
up = QVector3D(0.0f, 0.0f, 1.0f);
|
|
|
|
|
|
else
|
|
|
|
|
|
up.normalize();
|
|
|
|
|
|
|
|
|
|
|
|
m_orientationCamera->setPosition(-forward * 4.0f);
|
|
|
|
|
|
m_orientationCamera->setViewCenter(QVector3D(0.0f, 0.0f, 0.0f));
|
|
|
|
|
|
m_orientationCamera->setUpVector(up);
|
|
|
|
|
|
|
|
|
|
|
|
const QVector3D cameraPosition = m_orientationCamera->position();
|
|
|
|
|
|
for (Qt3DCore::QTransform *transform : m_orientationLabelTransforms) {
|
|
|
|
|
|
if (!transform)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
QVector3D normal = cameraPosition - transform->translation();
|
|
|
|
|
|
if (normal.lengthSquared() < 1.0e-8f)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
normal.normalize();
|
|
|
|
|
|
|
|
|
|
|
|
QVector3D right = QVector3D::crossProduct(up, normal);
|
|
|
|
|
|
if (right.lengthSquared() < 1.0e-8f)
|
|
|
|
|
|
right = QVector3D::crossProduct(QVector3D(1.0f, 0.0f, 0.0f), normal);
|
|
|
|
|
|
right.normalize();
|
|
|
|
|
|
const QVector3D billboardUp =
|
|
|
|
|
|
QVector3D::crossProduct(normal, right).normalized();
|
|
|
|
|
|
|
|
|
|
|
|
QMatrix3x3 rotation;
|
|
|
|
|
|
rotation(0, 0) = right.x();
|
|
|
|
|
|
rotation(1, 0) = right.y();
|
|
|
|
|
|
rotation(2, 0) = right.z();
|
|
|
|
|
|
rotation(0, 1) = billboardUp.x();
|
|
|
|
|
|
rotation(1, 1) = billboardUp.y();
|
|
|
|
|
|
rotation(2, 1) = billboardUp.z();
|
|
|
|
|
|
rotation(0, 2) = normal.x();
|
|
|
|
|
|
rotation(1, 2) = normal.y();
|
|
|
|
|
|
rotation(2, 2) = normal.z();
|
|
|
|
|
|
transform->setRotation(QQuaternion::fromRotationMatrix(rotation));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
m_view->requestUpdate();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UrdfViewerWidget::positionOrientationIndicator()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_view || !m_orientationViewport)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
constexpr int indicatorSize = 168;
|
|
|
|
|
|
constexpr int margin = 14;
|
|
|
|
|
|
const int viewportWidth = m_view->width();
|
|
|
|
|
|
const int viewportHeight = m_view->height();
|
|
|
|
|
|
if (viewportWidth <= 0 || viewportHeight <= 0)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
const qreal width = qMin(1.0, static_cast<qreal>(indicatorSize)
|
|
|
|
|
|
/ static_cast<qreal>(viewportWidth));
|
|
|
|
|
|
const qreal height = qMin(1.0, static_cast<qreal>(indicatorSize)
|
|
|
|
|
|
/ static_cast<qreal>(viewportHeight));
|
|
|
|
|
|
const qreal left = qMax(0.0, 1.0 - width
|
|
|
|
|
|
- static_cast<qreal>(margin) / viewportWidth);
|
|
|
|
|
|
const qreal top = qMax(0.0, static_cast<qreal>(margin) / viewportHeight);
|
|
|
|
|
|
m_orientationViewport->setNormalizedRect(
|
|
|
|
|
|
QRectF(left, top, width, height));
|
|
|
|
|
|
m_view->requestUpdate();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-18 16:22:20 +08:00
|
|
|
|
void UrdfViewerWidget::setLoading(bool loading, const QString &message)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_loadingTitle)
|
|
|
|
|
|
m_loadingTitle->setText(loading ? QStringLiteral("正在加载机械臂模型")
|
|
|
|
|
|
: QStringLiteral("3D 模型加载失败"));
|
|
|
|
|
|
if (m_loadingDetail && !message.isEmpty())
|
|
|
|
|
|
m_loadingDetail->setText(message);
|
|
|
|
|
|
if (m_loadingProgress)
|
|
|
|
|
|
m_loadingProgress->setVisible(loading);
|
|
|
|
|
|
if (m_viewportPlaceholder)
|
|
|
|
|
|
m_viewportPlaceholder->show();
|
|
|
|
|
|
if (m_viewContainer)
|
|
|
|
|
|
m_viewContainer->hide();
|
|
|
|
|
|
if (loading)
|
|
|
|
|
|
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UrdfViewerWidget::~UrdfViewerWidget()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Qt3DWindow's aspect engine owns and synchronously tears down the scene.
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UrdfViewerWidget::showEvent(QShowEvent *event)
|
|
|
|
|
|
{
|
|
|
|
|
|
QWidget::showEvent(event);
|
|
|
|
|
|
if (!m_view)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
m_view->requestUpdate();
|
|
|
|
|
|
if (m_modelRoot)
|
|
|
|
|
|
scheduleViewAll();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool UrdfViewerWidget::eventFilter(QObject *watched, QEvent *event)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (watched != m_view && watched->objectName() != QStringLiteral("urdfViewport"))
|
|
|
|
|
|
return QWidget::eventFilter(watched, event);
|
|
|
|
|
|
|
|
|
|
|
|
switch (event->type()) {
|
2026-09-23 16:04:01 +08:00
|
|
|
|
case QEvent::Resize:
|
|
|
|
|
|
case QEvent::Show:
|
|
|
|
|
|
QTimer::singleShot(0, this, [this]() {
|
|
|
|
|
|
positionOrientationIndicator();
|
|
|
|
|
|
});
|
|
|
|
|
|
break;
|
2026-09-18 16:22:20 +08:00
|
|
|
|
case QEvent::MouseButtonPress: {
|
|
|
|
|
|
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
|
|
|
|
|
|
if (mouseEvent->button() != Qt::LeftButton && mouseEvent->button() != Qt::RightButton)
|
|
|
|
|
|
break;
|
|
|
|
|
|
if (m_teachingEnabled && mouseEvent->button() == Qt::LeftButton) {
|
|
|
|
|
|
m_lastMousePosition = mouseEvent->localPos().toPoint();
|
|
|
|
|
|
m_jointDragActive = !m_selectedTeachingJoint.isEmpty();
|
|
|
|
|
|
if (m_teachingRayCaster)
|
|
|
|
|
|
m_teachingRayCaster->trigger(m_lastMousePosition);
|
|
|
|
|
|
m_view->setMouseGrabEnabled(true);
|
|
|
|
|
|
m_view->setCursor(QCursor(Qt::SizeHorCursor));
|
|
|
|
|
|
mouseEvent->accept();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (m_dragButton != Qt::NoButton) {
|
|
|
|
|
|
mouseEvent->accept();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
m_dragButton = mouseEvent->button();
|
|
|
|
|
|
m_lastMousePosition = mouseEvent->localPos().toPoint();
|
|
|
|
|
|
m_view->setMouseGrabEnabled(true);
|
|
|
|
|
|
m_view->setCursor(QCursor(m_dragButton == Qt::LeftButton
|
|
|
|
|
|
? Qt::ClosedHandCursor : Qt::SizeAllCursor));
|
|
|
|
|
|
mouseEvent->accept();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
case QEvent::MouseMove: {
|
|
|
|
|
|
if (m_teachingEnabled && m_jointDragActive) {
|
|
|
|
|
|
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
|
|
|
|
|
|
const QPoint position = mouseEvent->localPos().toPoint();
|
|
|
|
|
|
const QPoint delta = position - m_lastMousePosition;
|
|
|
|
|
|
m_lastMousePosition = position;
|
|
|
|
|
|
const auto bindingIt = m_joints.constFind(m_selectedTeachingJoint);
|
|
|
|
|
|
if (bindingIt != m_joints.constEnd() && !delta.isNull()) {
|
|
|
|
|
|
const double scale = bindingIt->type == urdf::Joint::PRISMATIC
|
|
|
|
|
|
? 0.0015 : 0.01;
|
|
|
|
|
|
setTeachingJointPosition(
|
|
|
|
|
|
m_selectedTeachingJoint,
|
|
|
|
|
|
m_jointPositions.value(m_selectedTeachingJoint)
|
|
|
|
|
|
+ (delta.x() - delta.y()) * scale);
|
|
|
|
|
|
}
|
|
|
|
|
|
mouseEvent->accept();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (m_dragButton == Qt::NoButton)
|
|
|
|
|
|
break;
|
|
|
|
|
|
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
|
|
|
|
|
|
const QPoint position = mouseEvent->localPos().toPoint();
|
|
|
|
|
|
const QPoint delta = position - m_lastMousePosition;
|
|
|
|
|
|
m_lastMousePosition = position;
|
|
|
|
|
|
if (m_dragButton == Qt::LeftButton)
|
|
|
|
|
|
orbitCamera(delta);
|
|
|
|
|
|
else
|
|
|
|
|
|
panCamera(delta);
|
|
|
|
|
|
mouseEvent->accept();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
case QEvent::MouseButtonRelease: {
|
|
|
|
|
|
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
|
|
|
|
|
|
if (m_teachingEnabled && mouseEvent->button() == Qt::LeftButton) {
|
|
|
|
|
|
m_jointDragActive = false;
|
|
|
|
|
|
m_view->setMouseGrabEnabled(false);
|
|
|
|
|
|
m_view->unsetCursor();
|
|
|
|
|
|
mouseEvent->accept();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (mouseEvent->button() != m_dragButton)
|
|
|
|
|
|
break;
|
|
|
|
|
|
m_dragButton = Qt::NoButton;
|
|
|
|
|
|
m_view->setMouseGrabEnabled(false);
|
|
|
|
|
|
m_view->unsetCursor();
|
|
|
|
|
|
mouseEvent->accept();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
case QEvent::MouseButtonDblClick: {
|
|
|
|
|
|
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
|
|
|
|
|
|
if (mouseEvent->button() == Qt::LeftButton) {
|
|
|
|
|
|
resetView();
|
|
|
|
|
|
mouseEvent->accept();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
case QEvent::Wheel: {
|
|
|
|
|
|
QWheelEvent *wheelEvent = static_cast<QWheelEvent *>(event);
|
|
|
|
|
|
zoomCamera(wheelEvent->angleDelta().y());
|
|
|
|
|
|
wheelEvent->accept();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
case QEvent::UngrabMouse:
|
|
|
|
|
|
case QEvent::WindowDeactivate:
|
|
|
|
|
|
m_dragButton = Qt::NoButton;
|
|
|
|
|
|
m_jointDragActive = false;
|
|
|
|
|
|
m_view->unsetCursor();
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
return QWidget::eventFilter(watched, event);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UrdfViewerWidget::orbitCamera(const QPoint &delta)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_camera || delta.isNull())
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
const QVector3D worldUp(0.0f, 0.0f, 1.0f);
|
|
|
|
|
|
const QVector3D center = m_camera->viewCenter();
|
|
|
|
|
|
QVector3D offset = m_camera->position() - center;
|
|
|
|
|
|
if (offset.lengthSquared() < 1.0e-8f)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
const QQuaternion yaw = QQuaternion::fromAxisAndAngle(
|
|
|
|
|
|
worldUp, -static_cast<float>(delta.x()) * 0.32f);
|
|
|
|
|
|
offset = yaw.rotatedVector(offset);
|
|
|
|
|
|
|
|
|
|
|
|
const QVector3D forward = -offset.normalized();
|
|
|
|
|
|
const QVector3D right = QVector3D::crossProduct(forward, worldUp).normalized();
|
|
|
|
|
|
if (!right.isNull()) {
|
|
|
|
|
|
const QQuaternion pitch = QQuaternion::fromAxisAndAngle(
|
|
|
|
|
|
right, -static_cast<float>(delta.y()) * 0.28f);
|
|
|
|
|
|
const QVector3D candidate = pitch.rotatedVector(offset);
|
|
|
|
|
|
const float poleAlignment = qAbs(QVector3D::dotProduct(-candidate.normalized(), worldUp));
|
|
|
|
|
|
if (poleAlignment < 0.985f)
|
|
|
|
|
|
offset = candidate;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
m_camera->setPosition(center + offset);
|
|
|
|
|
|
m_camera->setUpVector(worldUp);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UrdfViewerWidget::panCamera(const QPoint &delta)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_camera || delta.isNull())
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
const QVector3D position = m_camera->position();
|
|
|
|
|
|
const QVector3D center = m_camera->viewCenter();
|
|
|
|
|
|
const QVector3D forward = (center - position).normalized();
|
|
|
|
|
|
const QVector3D worldUp(0.0f, 0.0f, 1.0f);
|
|
|
|
|
|
const QVector3D right = QVector3D::crossProduct(forward, worldUp).normalized();
|
|
|
|
|
|
if (right.isNull())
|
|
|
|
|
|
return;
|
|
|
|
|
|
const QVector3D screenUp = QVector3D::crossProduct(right, forward).normalized();
|
|
|
|
|
|
const float viewportHeight = static_cast<float>(qMax(1, m_view->height()));
|
|
|
|
|
|
const float worldPerPixel = 2.0f * (center - position).length()
|
|
|
|
|
|
* qTan(qDegreesToRadians(m_camera->fieldOfView() * 0.5f)) / viewportHeight;
|
|
|
|
|
|
const QVector3D translation = (-right * static_cast<float>(delta.x())
|
|
|
|
|
|
+ screenUp * static_cast<float>(delta.y())) * worldPerPixel;
|
|
|
|
|
|
m_camera->setPosition(position + translation);
|
|
|
|
|
|
m_camera->setViewCenter(center + translation);
|
|
|
|
|
|
m_camera->setUpVector(worldUp);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UrdfViewerWidget::zoomCamera(int angleDelta)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_camera || angleDelta == 0)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
const QVector3D center = m_camera->viewCenter();
|
|
|
|
|
|
const QVector3D offset = m_camera->position() - center;
|
|
|
|
|
|
const float distance = offset.length();
|
|
|
|
|
|
if (distance < 1.0e-6f)
|
|
|
|
|
|
return;
|
|
|
|
|
|
const float steps = static_cast<float>(angleDelta) / 120.0f;
|
|
|
|
|
|
const float zoomFactor = static_cast<float>(qPow(0.88, static_cast<double>(steps)));
|
|
|
|
|
|
const float targetDistance = qBound(0.05f, distance * zoomFactor, 500.0f);
|
|
|
|
|
|
m_camera->setPosition(center + offset.normalized() * targetDistance);
|
|
|
|
|
|
m_camera->setUpVector(QVector3D(0.0f, 0.0f, 1.0f));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString UrdfViewerWidget::currentFile() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_currentFile;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UrdfViewerWidget::prepareLoad(const QString &filePath)
|
|
|
|
|
|
{
|
|
|
|
|
|
const QString fileName = QFileInfo(filePath).fileName();
|
|
|
|
|
|
setLoading(true, fileName.isEmpty()
|
|
|
|
|
|
? QStringLiteral("正在准备模型资源…")
|
|
|
|
|
|
: QStringLiteral("正在准备 %1…").arg(fileName));
|
|
|
|
|
|
m_statusLabel->setText(QStringLiteral("正在初始化 3D 模型…"));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool UrdfViewerWidget::loadUrdf(const QString &filePath)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_demoButton && m_demoButton->isChecked())
|
|
|
|
|
|
m_demoButton->setChecked(false);
|
|
|
|
|
|
const QFileInfo fileInfo(filePath);
|
|
|
|
|
|
if (!fileInfo.exists() || !fileInfo.isFile()) {
|
|
|
|
|
|
setLoading(false, QStringLiteral("找不到指定的 URDF 文件"));
|
|
|
|
|
|
m_statusLabel->setText(QStringLiteral("模型文件不存在"));
|
|
|
|
|
|
emit logMessage(QStringLiteral("ERROR"),
|
|
|
|
|
|
QStringLiteral("URDF 文件不存在:%1").arg(filePath));
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
setLoading(true, QStringLiteral("正在初始化 Qt3D 场景…"));
|
|
|
|
|
|
ensureSceneInitialized();
|
|
|
|
|
|
m_statusLabel->setText(QStringLiteral("正在解析 %1…").arg(fileInfo.fileName()));
|
|
|
|
|
|
|
|
|
|
|
|
QFile file(fileInfo.absoluteFilePath());
|
|
|
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
|
|
|
|
|
setLoading(false, QStringLiteral("无法读取模型文件"));
|
|
|
|
|
|
m_statusLabel->setText(QStringLiteral("无法读取模型文件"));
|
|
|
|
|
|
emit logMessage(QStringLiteral("ERROR"),
|
|
|
|
|
|
QStringLiteral("无法读取 URDF:%1").arg(file.errorString()));
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const QByteArray xml = file.readAll();
|
|
|
|
|
|
urdf::ModelInterfaceSharedPtr model = urdf::parseURDF(xml.toStdString());
|
|
|
|
|
|
if (!model || !model->getRoot()) {
|
|
|
|
|
|
setLoading(false, QStringLiteral("URDF 文件解析失败"));
|
|
|
|
|
|
m_statusLabel->setText(QStringLiteral("URDF 解析失败"));
|
|
|
|
|
|
emit logMessage(QStringLiteral("ERROR"),
|
|
|
|
|
|
QStringLiteral("URDF 解析失败:%1").arg(fileInfo.absoluteFilePath()));
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
clearModel();
|
|
|
|
|
|
m_currentFile = fileInfo.absoluteFilePath();
|
|
|
|
|
|
m_urdfDirectory = fileInfo.absolutePath();
|
2026-09-23 11:11:49 +08:00
|
|
|
|
syncModelSelection(m_currentFile);
|
2026-09-18 16:22:20 +08:00
|
|
|
|
m_pathEdit->setText(fileInfo.fileName());
|
|
|
|
|
|
m_pathEdit->setToolTip(QDir::toNativeSeparators(m_currentFile));
|
|
|
|
|
|
m_reloadButton->setEnabled(true);
|
|
|
|
|
|
m_modelRoot = new Qt3DCore::QEntity(m_rootEntity);
|
|
|
|
|
|
m_modelRoot->setObjectName(QStringLiteral("urdfModelRoot"));
|
|
|
|
|
|
|
|
|
|
|
|
std::function<void(const urdf::LinkConstSharedPtr &, Qt3DCore::QEntity *,
|
|
|
|
|
|
const QString &)> buildLink;
|
|
|
|
|
|
buildLink = [this, &buildLink](const urdf::LinkConstSharedPtr &link,
|
|
|
|
|
|
Qt3DCore::QEntity *parent,
|
|
|
|
|
|
const QString &teachingJointName) {
|
|
|
|
|
|
if (!link)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
Qt3DCore::QEntity *linkEntity = new Qt3DCore::QEntity(parent);
|
|
|
|
|
|
linkEntity->setObjectName(QString::fromStdString(link->name));
|
|
|
|
|
|
|
|
|
|
|
|
for (const urdf::VisualSharedPtr &visual : link->visual_array) {
|
|
|
|
|
|
if (!visual || !visual->geometry)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
Qt3DCore::QEntity *visualEntity = new Qt3DCore::QEntity(linkEntity);
|
|
|
|
|
|
Qt3DCore::QTransform *visualTransform = new Qt3DCore::QTransform(visualEntity);
|
|
|
|
|
|
applyPose(visualTransform, visual->origin);
|
|
|
|
|
|
visualEntity->addComponent(visualTransform);
|
|
|
|
|
|
|
|
|
|
|
|
Qt3DCore::QEntity *geometryEntity = new Qt3DCore::QEntity(visualEntity);
|
|
|
|
|
|
Qt3DCore::QTransform *geometryTransform = new Qt3DCore::QTransform(geometryEntity);
|
|
|
|
|
|
geometryEntity->addComponent(geometryTransform);
|
|
|
|
|
|
bool pickable = false;
|
|
|
|
|
|
|
|
|
|
|
|
switch (visual->geometry->type) {
|
|
|
|
|
|
case urdf::Geometry::BOX: {
|
|
|
|
|
|
const std::shared_ptr<urdf::Box> box =
|
|
|
|
|
|
std::dynamic_pointer_cast<urdf::Box>(visual->geometry);
|
|
|
|
|
|
if (!box)
|
|
|
|
|
|
break;
|
|
|
|
|
|
Qt3DExtras::QCuboidMesh *mesh = new Qt3DExtras::QCuboidMesh(geometryEntity);
|
|
|
|
|
|
mesh->setXExtent(static_cast<float>(box->dim.x));
|
|
|
|
|
|
mesh->setYExtent(static_cast<float>(box->dim.y));
|
|
|
|
|
|
mesh->setZExtent(static_cast<float>(box->dim.z));
|
|
|
|
|
|
geometryEntity->addComponent(mesh);
|
|
|
|
|
|
geometryEntity->addComponent(makeMaterial(visual, geometryEntity));
|
|
|
|
|
|
pickable = true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
case urdf::Geometry::SPHERE: {
|
|
|
|
|
|
const std::shared_ptr<urdf::Sphere> sphere =
|
|
|
|
|
|
std::dynamic_pointer_cast<urdf::Sphere>(visual->geometry);
|
|
|
|
|
|
if (!sphere)
|
|
|
|
|
|
break;
|
|
|
|
|
|
Qt3DExtras::QSphereMesh *mesh = new Qt3DExtras::QSphereMesh(geometryEntity);
|
|
|
|
|
|
mesh->setRadius(static_cast<float>(sphere->radius));
|
|
|
|
|
|
mesh->setRings(24);
|
|
|
|
|
|
mesh->setSlices(32);
|
|
|
|
|
|
geometryEntity->addComponent(mesh);
|
|
|
|
|
|
geometryEntity->addComponent(makeMaterial(visual, geometryEntity));
|
|
|
|
|
|
pickable = true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
case urdf::Geometry::CYLINDER: {
|
|
|
|
|
|
const std::shared_ptr<urdf::Cylinder> cylinder =
|
|
|
|
|
|
std::dynamic_pointer_cast<urdf::Cylinder>(visual->geometry);
|
|
|
|
|
|
if (!cylinder)
|
|
|
|
|
|
break;
|
|
|
|
|
|
Qt3DExtras::QCylinderMesh *mesh = new Qt3DExtras::QCylinderMesh(geometryEntity);
|
|
|
|
|
|
mesh->setRadius(static_cast<float>(cylinder->radius));
|
|
|
|
|
|
mesh->setLength(static_cast<float>(cylinder->length));
|
|
|
|
|
|
mesh->setRings(2);
|
|
|
|
|
|
mesh->setSlices(32);
|
|
|
|
|
|
geometryTransform->setRotation(QQuaternion::fromAxisAndAngle(1.0f, 0.0f, 0.0f, 90.0f));
|
|
|
|
|
|
geometryEntity->addComponent(mesh);
|
|
|
|
|
|
geometryEntity->addComponent(makeMaterial(visual, geometryEntity));
|
|
|
|
|
|
pickable = true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
case urdf::Geometry::MESH: {
|
|
|
|
|
|
const std::shared_ptr<urdf::Mesh> mesh =
|
|
|
|
|
|
std::dynamic_pointer_cast<urdf::Mesh>(visual->geometry);
|
|
|
|
|
|
if (!mesh)
|
|
|
|
|
|
break;
|
|
|
|
|
|
geometryTransform->setScale3D(toVector(mesh->scale));
|
|
|
|
|
|
const QString source = resolveMeshPath(QString::fromStdString(mesh->filename));
|
|
|
|
|
|
if (source.isEmpty()) {
|
|
|
|
|
|
emit logMessage(QStringLiteral("WARN"),
|
|
|
|
|
|
QStringLiteral("找不到 URDF 网格:%1")
|
|
|
|
|
|
.arg(QString::fromStdString(mesh->filename)));
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2026-09-23 11:11:49 +08:00
|
|
|
|
// Assimp normalizes COLLADA and 3DS scenes from Z-up to Y-up by
|
|
|
|
|
|
// adding a -90 degree X rotation to the imported scene root.
|
|
|
|
|
|
// URDF and this viewer both use Z-up, so cancel that conversion
|
|
|
|
|
|
// on the mesh only; the URDF joint hierarchy must remain intact.
|
|
|
|
|
|
if (needsAssimpZUpCompensation(source)) {
|
|
|
|
|
|
geometryTransform->setRotation(
|
|
|
|
|
|
QQuaternion::fromAxisAndAngle(1.0f, 0.0f, 0.0f, 90.0f));
|
|
|
|
|
|
}
|
2026-09-18 16:22:20 +08:00
|
|
|
|
Qt3DRender::QSceneLoader *loader = new Qt3DRender::QSceneLoader(geometryEntity);
|
|
|
|
|
|
connect(loader, &Qt3DRender::QSceneLoader::statusChanged, this,
|
|
|
|
|
|
[this, loader, source, geometryEntity, teachingJointName]
|
|
|
|
|
|
(Qt3DRender::QSceneLoader::Status status) {
|
|
|
|
|
|
if (status == Qt3DRender::QSceneLoader::Error) {
|
2026-09-23 11:11:49 +08:00
|
|
|
|
m_statusLabel->setText(
|
|
|
|
|
|
QStringLiteral("网格加载失败 · 请检查 Qt3D Assimp 插件"));
|
2026-09-18 16:22:20 +08:00
|
|
|
|
emit logMessage(QStringLiteral("ERROR"),
|
|
|
|
|
|
QStringLiteral("Qt3D 无法加载网格:%1").arg(source));
|
|
|
|
|
|
} else if (status == Qt3DRender::QSceneLoader::Ready
|
|
|
|
|
|
&& !teachingJointName.isEmpty()) {
|
|
|
|
|
|
const QStringList entityNames = loader->entityNames();
|
|
|
|
|
|
for (const QString &entityName : entityNames) {
|
|
|
|
|
|
Qt3DCore::QEntity *loadedEntity = loader->entity(entityName);
|
|
|
|
|
|
if (!loadedEntity)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
if (loader->component(
|
|
|
|
|
|
entityName,
|
|
|
|
|
|
Qt3DRender::QSceneLoader::GeometryRendererComponent)) {
|
|
|
|
|
|
attachTeachingPicker(loadedEntity, teachingJointName);
|
|
|
|
|
|
}
|
|
|
|
|
|
attachTeachingPickersRecursively(loadedEntity,
|
|
|
|
|
|
teachingJointName);
|
|
|
|
|
|
}
|
|
|
|
|
|
attachTeachingPickersRecursively(geometryEntity,
|
|
|
|
|
|
teachingJointName);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (m_view)
|
|
|
|
|
|
m_view->requestUpdate();
|
|
|
|
|
|
});
|
|
|
|
|
|
loader->setSource(QUrl::fromLocalFile(source));
|
|
|
|
|
|
geometryEntity->addComponent(loader);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (pickable && !teachingJointName.isEmpty())
|
|
|
|
|
|
attachTeachingPicker(geometryEntity, teachingJointName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (const urdf::JointSharedPtr &joint : link->child_joints) {
|
|
|
|
|
|
if (!joint)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
Qt3DCore::QEntity *originEntity = new Qt3DCore::QEntity(linkEntity);
|
|
|
|
|
|
Qt3DCore::QTransform *originTransform = new Qt3DCore::QTransform(originEntity);
|
|
|
|
|
|
applyPose(originTransform, joint->parent_to_joint_origin_transform);
|
|
|
|
|
|
originEntity->addComponent(originTransform);
|
|
|
|
|
|
|
|
|
|
|
|
Qt3DCore::QEntity *motionEntity = new Qt3DCore::QEntity(originEntity);
|
|
|
|
|
|
Qt3DCore::QTransform *motionTransform = new Qt3DCore::QTransform(motionEntity);
|
|
|
|
|
|
motionEntity->addComponent(motionTransform);
|
|
|
|
|
|
|
|
|
|
|
|
const bool movable = joint->type == urdf::Joint::REVOLUTE
|
|
|
|
|
|
|| joint->type == urdf::Joint::CONTINUOUS
|
|
|
|
|
|
|| joint->type == urdf::Joint::PRISMATIC;
|
|
|
|
|
|
QString childTeachingJoint = teachingJointName;
|
|
|
|
|
|
if (movable) {
|
|
|
|
|
|
JointBinding binding;
|
|
|
|
|
|
binding.type = joint->type;
|
|
|
|
|
|
binding.axis = toVector(joint->axis).normalized();
|
|
|
|
|
|
binding.transform = motionTransform;
|
|
|
|
|
|
if (joint->type == urdf::Joint::CONTINUOUS) {
|
|
|
|
|
|
binding.lower = qDegreesToRadians(-360.0);
|
|
|
|
|
|
binding.upper = qDegreesToRadians(360.0);
|
|
|
|
|
|
} else if (joint->limits) {
|
|
|
|
|
|
binding.lower = joint->limits->lower;
|
|
|
|
|
|
binding.upper = joint->limits->upper;
|
|
|
|
|
|
} else if (joint->type == urdf::Joint::PRISMATIC) {
|
|
|
|
|
|
binding.lower = -1.0;
|
|
|
|
|
|
binding.upper = 1.0;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
binding.lower = qDegreesToRadians(-180.0);
|
|
|
|
|
|
binding.upper = qDegreesToRadians(180.0);
|
|
|
|
|
|
}
|
|
|
|
|
|
childTeachingJoint = QString::fromStdString(joint->name);
|
|
|
|
|
|
m_joints.insert(childTeachingJoint, binding);
|
|
|
|
|
|
m_jointOrder.append(childTeachingJoint);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
urdf::LinkConstSharedPtr childLink;
|
|
|
|
|
|
for (const urdf::LinkSharedPtr &candidate : link->child_links) {
|
|
|
|
|
|
if (candidate && candidate->name == joint->child_link_name) {
|
|
|
|
|
|
childLink = candidate;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
buildLink(childLink, motionEntity, childTeachingJoint);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
buildLink(model->getRoot(), m_modelRoot, QString());
|
|
|
|
|
|
m_jointPositions.clear();
|
|
|
|
|
|
for (const QString &jointName : m_jointOrder)
|
|
|
|
|
|
m_jointPositions.insert(jointName, m_latestTelemetryPositions.value(jointName, 0.0));
|
|
|
|
|
|
applyJointPositions(m_jointPositions);
|
|
|
|
|
|
const int linkCount = static_cast<int>(model->links_.size());
|
|
|
|
|
|
const int jointCount = static_cast<int>(model->joints_.size());
|
|
|
|
|
|
m_modelSummary = QStringLiteral("%1 个 Link · %2 个 Joint")
|
|
|
|
|
|
.arg(linkCount).arg(jointCount);
|
|
|
|
|
|
m_statusLabel->setText(m_modelSummary);
|
|
|
|
|
|
emit logMessage(QStringLiteral("OK"),
|
|
|
|
|
|
QStringLiteral("URDF 已加载:%1(%2 Link,%3 Joint)")
|
|
|
|
|
|
.arg(fileInfo.fileName()).arg(linkCount).arg(jointCount));
|
|
|
|
|
|
emit modelLoaded(m_currentFile, linkCount, jointCount);
|
|
|
|
|
|
setLoading(false);
|
|
|
|
|
|
if (m_viewportPlaceholder)
|
|
|
|
|
|
m_viewportPlaceholder->hide();
|
|
|
|
|
|
if (m_viewContainer) {
|
|
|
|
|
|
m_viewContainer->show();
|
|
|
|
|
|
m_viewContainer->raise();
|
|
|
|
|
|
}
|
|
|
|
|
|
if (m_view)
|
|
|
|
|
|
m_view->requestUpdate();
|
|
|
|
|
|
if (m_teachingEnabled) {
|
|
|
|
|
|
emit teachingPositionsChanged(m_jointPositions);
|
|
|
|
|
|
if (!m_jointOrder.isEmpty())
|
|
|
|
|
|
selectTeachingJoint(m_jointOrder.first());
|
|
|
|
|
|
}
|
|
|
|
|
|
scheduleViewAll();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UrdfViewerWidget::setJointPositions(const QHash<QString, double> &positions)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_latestTelemetryPositions = positions;
|
|
|
|
|
|
if (m_teachingEnabled)
|
|
|
|
|
|
return;
|
|
|
|
|
|
if (m_demoButton && m_demoButton->isChecked())
|
|
|
|
|
|
m_demoButton->setChecked(false);
|
|
|
|
|
|
for (auto it = positions.constBegin(); it != positions.constEnd(); ++it) {
|
|
|
|
|
|
if (m_joints.contains(it.key()))
|
|
|
|
|
|
m_jointPositions.insert(it.key(), it.value());
|
|
|
|
|
|
}
|
|
|
|
|
|
applyJointPositions(positions);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QStringList UrdfViewerWidget::jointNames() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_jointOrder;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-23 16:04:01 +08:00
|
|
|
|
QHash<QString, QPair<double, double>> UrdfViewerWidget::jointLimits() const
|
|
|
|
|
|
{
|
|
|
|
|
|
QHash<QString, QPair<double, double>> limits;
|
|
|
|
|
|
limits.reserve(m_joints.size());
|
|
|
|
|
|
for (auto it = m_joints.constBegin(); it != m_joints.constEnd(); ++it)
|
|
|
|
|
|
limits.insert(it.key(), qMakePair(it.value().lower, it.value().upper));
|
|
|
|
|
|
return limits;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-18 16:22:20 +08:00
|
|
|
|
QHash<QString, double> UrdfViewerWidget::teachingPositions() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_jointPositions;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString UrdfViewerWidget::selectedTeachingJoint() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_selectedTeachingJoint;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UrdfViewerWidget::setTeachingEnabled(bool enabled)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (enabled && m_joints.isEmpty()) {
|
|
|
|
|
|
emit teachingModeChanged(false);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (m_teachingEnabled == enabled)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
if (m_demoButton && m_demoButton->isChecked())
|
|
|
|
|
|
m_demoButton->setChecked(false);
|
|
|
|
|
|
m_teachingEnabled = enabled;
|
|
|
|
|
|
m_jointDragActive = false;
|
|
|
|
|
|
if (m_demoButton)
|
|
|
|
|
|
m_demoButton->setEnabled(!enabled);
|
|
|
|
|
|
|
|
|
|
|
|
if (enabled) {
|
|
|
|
|
|
for (const QString &jointName : m_jointOrder) {
|
|
|
|
|
|
const double value = m_latestTelemetryPositions.contains(jointName)
|
|
|
|
|
|
? m_latestTelemetryPositions.value(jointName)
|
|
|
|
|
|
: m_jointPositions.value(jointName, 0.0);
|
|
|
|
|
|
m_jointPositions.insert(jointName, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
applyJointPositions(m_jointPositions);
|
|
|
|
|
|
emit teachingPositionsChanged(m_jointPositions);
|
|
|
|
|
|
selectTeachingJoint(m_selectedTeachingJoint.isEmpty()
|
|
|
|
|
|
? m_jointOrder.first() : m_selectedTeachingJoint);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
for (auto it = m_latestTelemetryPositions.constBegin();
|
|
|
|
|
|
it != m_latestTelemetryPositions.constEnd(); ++it) {
|
|
|
|
|
|
if (m_joints.contains(it.key()))
|
|
|
|
|
|
m_jointPositions.insert(it.key(), it.value());
|
|
|
|
|
|
}
|
|
|
|
|
|
applyJointPositions(m_jointPositions);
|
|
|
|
|
|
m_statusLabel->setText(m_modelSummary);
|
|
|
|
|
|
}
|
|
|
|
|
|
emit teachingModeChanged(enabled);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UrdfViewerWidget::selectTeachingJoint(const QString &jointName)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_teachingEnabled || !m_joints.contains(jointName))
|
|
|
|
|
|
return;
|
|
|
|
|
|
m_selectedTeachingJoint = jointName;
|
|
|
|
|
|
emitTeachingSelection();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UrdfViewerWidget::setTeachingJointPosition(const QString &jointName, double position)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_teachingEnabled)
|
|
|
|
|
|
return;
|
|
|
|
|
|
const auto bindingIt = m_joints.constFind(jointName);
|
|
|
|
|
|
if (bindingIt == m_joints.constEnd())
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
const JointBinding &binding = bindingIt.value();
|
|
|
|
|
|
const double boundedPosition = qBound(binding.lower, position, binding.upper);
|
|
|
|
|
|
m_jointPositions.insert(jointName, boundedPosition);
|
|
|
|
|
|
QHash<QString, double> changed;
|
|
|
|
|
|
changed.insert(jointName, boundedPosition);
|
|
|
|
|
|
applyJointPositions(changed);
|
|
|
|
|
|
m_selectedTeachingJoint = jointName;
|
|
|
|
|
|
emit teachingPositionsChanged(m_jointPositions);
|
|
|
|
|
|
emitTeachingSelection();
|
|
|
|
|
|
m_statusLabel->setText(QStringLiteral("示教 · %1 · %2 rad")
|
|
|
|
|
|
.arg(jointName).arg(boundedPosition, 0, 'f', 6));
|
|
|
|
|
|
if (m_view)
|
|
|
|
|
|
m_view->requestUpdate();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UrdfViewerWidget::restoreTeachingFromTelemetry()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_teachingEnabled)
|
|
|
|
|
|
return;
|
|
|
|
|
|
for (const QString &jointName : m_jointOrder) {
|
|
|
|
|
|
if (m_latestTelemetryPositions.contains(jointName))
|
|
|
|
|
|
m_jointPositions.insert(jointName,
|
|
|
|
|
|
m_latestTelemetryPositions.value(jointName));
|
|
|
|
|
|
}
|
|
|
|
|
|
applyJointPositions(m_jointPositions);
|
|
|
|
|
|
emit teachingPositionsChanged(m_jointPositions);
|
|
|
|
|
|
emitTeachingSelection();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UrdfViewerWidget::attachTeachingPicker(Qt3DCore::QEntity *entity,
|
|
|
|
|
|
const QString &jointName)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!entity || jointName.isEmpty())
|
|
|
|
|
|
return;
|
|
|
|
|
|
entity->setProperty("cmvrTeachingJoint", jointName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UrdfViewerWidget::attachTeachingPickersRecursively(Qt3DCore::QEntity *root,
|
|
|
|
|
|
const QString &jointName)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!root || jointName.isEmpty())
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
const QList<Qt3DCore::QEntity *> entities =
|
|
|
|
|
|
root->findChildren<Qt3DCore::QEntity *>();
|
|
|
|
|
|
for (Qt3DCore::QEntity *entity : entities) {
|
|
|
|
|
|
bool hasGeometry = false;
|
|
|
|
|
|
for (Qt3DCore::QComponent *component : entity->components()) {
|
|
|
|
|
|
if (qobject_cast<Qt3DRender::QGeometryRenderer *>(component)) {
|
|
|
|
|
|
hasGeometry = true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (hasGeometry)
|
|
|
|
|
|
attachTeachingPicker(entity, jointName);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UrdfViewerWidget::emitTeachingSelection()
|
|
|
|
|
|
{
|
|
|
|
|
|
const auto bindingIt = m_joints.constFind(m_selectedTeachingJoint);
|
|
|
|
|
|
if (bindingIt == m_joints.constEnd())
|
|
|
|
|
|
return;
|
|
|
|
|
|
const JointBinding &binding = bindingIt.value();
|
|
|
|
|
|
emit teachingJointSelected(m_selectedTeachingJoint,
|
|
|
|
|
|
m_jointPositions.value(m_selectedTeachingJoint, 0.0),
|
|
|
|
|
|
binding.lower, binding.upper);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UrdfViewerWidget::applyJointPositions(const QHash<QString, double> &positions)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (auto it = positions.constBegin(); it != positions.constEnd(); ++it) {
|
|
|
|
|
|
const auto bindingIt = m_joints.constFind(it.key());
|
|
|
|
|
|
if (bindingIt == m_joints.constEnd() || !bindingIt->transform)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
const JointBinding &binding = bindingIt.value();
|
|
|
|
|
|
if (binding.type == urdf::Joint::PRISMATIC) {
|
|
|
|
|
|
binding.transform->setTranslation(binding.axis * static_cast<float>(it.value()));
|
|
|
|
|
|
} else {
|
|
|
|
|
|
binding.transform->setRotation(QQuaternion::fromAxisAndAngle(
|
|
|
|
|
|
binding.axis, static_cast<float>(qRadiansToDegrees(it.value()))));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UrdfViewerWidget::setMotionDemoRunning(bool running)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_demoButton || !m_demoTimer)
|
|
|
|
|
|
return;
|
|
|
|
|
|
if (running && (!m_modelRoot || m_joints.isEmpty())) {
|
|
|
|
|
|
m_demoButton->setChecked(false);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
m_demoButton->setIcon(UiIcons::icon(
|
|
|
|
|
|
running ? QStringLiteral(":/icons/square.svg")
|
|
|
|
|
|
: QStringLiteral(":/icons/play.svg")));
|
|
|
|
|
|
m_demoButton->setToolTip(running ? QStringLiteral("停止本地关节动作")
|
|
|
|
|
|
: QStringLiteral("播放本地关节动作"));
|
|
|
|
|
|
if (running) {
|
|
|
|
|
|
m_demoElapsedMs = 0;
|
|
|
|
|
|
advanceMotionDemo();
|
|
|
|
|
|
m_demoTimer->start();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
m_demoTimer->stop();
|
|
|
|
|
|
if (m_statusLabel)
|
|
|
|
|
|
m_statusLabel->setText(m_modelSummary);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UrdfViewerWidget::advanceMotionDemo()
|
|
|
|
|
|
{
|
|
|
|
|
|
constexpr int segmentDurationMs = 2200;
|
|
|
|
|
|
const QVector<QVector<double>> &poses = motionDemoPoses();
|
|
|
|
|
|
if (poses.isEmpty())
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
const int segment = (m_demoElapsedMs / segmentDurationMs) % poses.size();
|
|
|
|
|
|
const int nextSegment = (segment + 1) % poses.size();
|
|
|
|
|
|
const double linear = static_cast<double>(m_demoElapsedMs % segmentDurationMs)
|
|
|
|
|
|
/ static_cast<double>(segmentDurationMs);
|
|
|
|
|
|
const double blend = linear * linear * (3.0 - 2.0 * linear);
|
|
|
|
|
|
const QStringList &jointNames = motionDemoJointNames();
|
|
|
|
|
|
QHash<QString, double> positions;
|
|
|
|
|
|
for (int i = 0; i < jointNames.size(); ++i) {
|
|
|
|
|
|
const double start = poses.at(segment).at(i);
|
|
|
|
|
|
const double end = poses.at(nextSegment).at(i);
|
|
|
|
|
|
positions.insert(jointNames.at(i), start + (end - start) * blend);
|
|
|
|
|
|
}
|
|
|
|
|
|
applyJointPositions(positions);
|
|
|
|
|
|
m_statusLabel->setText(QStringLiteral("本地演示 · %1 → %2")
|
|
|
|
|
|
.arg(motionDemoPoseNames().at(segment),
|
|
|
|
|
|
motionDemoPoseNames().at(nextSegment)));
|
|
|
|
|
|
m_demoElapsedMs += m_demoTimer->interval();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UrdfViewerWidget::chooseUrdf()
|
|
|
|
|
|
{
|
|
|
|
|
|
const QString initialDirectory = m_currentFile.isEmpty()
|
|
|
|
|
|
? QDir::homePath() : QFileInfo(m_currentFile).absolutePath();
|
|
|
|
|
|
const QString path = QFileDialog::getOpenFileName(
|
|
|
|
|
|
this, QStringLiteral("加载 URDF 模型"), initialDirectory,
|
|
|
|
|
|
QStringLiteral("URDF 模型 (*.urdf);;XML 文件 (*.xml);;所有文件 (*.*)"));
|
|
|
|
|
|
if (!path.isEmpty())
|
|
|
|
|
|
loadUrdf(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-23 11:11:49 +08:00
|
|
|
|
void UrdfViewerWidget::chooseBundledUrdf(int index)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_modelCombo || index < 0)
|
|
|
|
|
|
return;
|
|
|
|
|
|
const QString path = m_modelCombo->itemData(index).toString();
|
|
|
|
|
|
if (!path.isEmpty() && path != m_currentFile)
|
|
|
|
|
|
loadUrdf(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool UrdfViewerWidget::loadModelForIdentity(const QString &manufacturer,
|
|
|
|
|
|
const QString &model,
|
|
|
|
|
|
const QString &subtype,
|
|
|
|
|
|
const QString &descriptionId)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_modelCombo)
|
|
|
|
|
|
return false;
|
|
|
|
|
|
const auto normalized = [](QString value) {
|
|
|
|
|
|
value = value.toLower();
|
|
|
|
|
|
QString result;
|
|
|
|
|
|
result.reserve(value.size());
|
|
|
|
|
|
for (const QChar character : value) {
|
|
|
|
|
|
if (character.isLetterOrNumber())
|
|
|
|
|
|
result.append(character);
|
|
|
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
|
|
|
};
|
|
|
|
|
|
QStringList candidates;
|
|
|
|
|
|
if (!model.trimmed().isEmpty() && !subtype.trimmed().isEmpty())
|
|
|
|
|
|
candidates.append(normalized(model + subtype));
|
|
|
|
|
|
if (!descriptionId.trimmed().isEmpty())
|
|
|
|
|
|
candidates.append(normalized(descriptionId));
|
|
|
|
|
|
if (!model.trimmed().isEmpty())
|
|
|
|
|
|
candidates.append(normalized(model));
|
|
|
|
|
|
candidates.removeDuplicates();
|
|
|
|
|
|
|
|
|
|
|
|
int bestIndex = -1;
|
|
|
|
|
|
int bestScore = -1;
|
|
|
|
|
|
const QString vendor = normalized(manufacturer);
|
|
|
|
|
|
for (int index = 1; index < m_modelCombo->count(); ++index) {
|
|
|
|
|
|
const QString path = QDir::fromNativeSeparators(
|
|
|
|
|
|
m_modelCombo->itemData(index).toString());
|
|
|
|
|
|
const QString base = normalized(QFileInfo(path).completeBaseName());
|
|
|
|
|
|
int score = -1;
|
|
|
|
|
|
for (int rank = 0; rank < candidates.size(); ++rank) {
|
|
|
|
|
|
if (base == candidates.at(rank)) {
|
|
|
|
|
|
score = 100 - rank * 10;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (score < 0)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
const QString normalizedPath = normalized(path);
|
|
|
|
|
|
if (vendor.contains(QStringLiteral("aubo"))
|
|
|
|
|
|
&& normalizedPath.contains(QStringLiteral("aubodescription"))) {
|
|
|
|
|
|
score += 5;
|
|
|
|
|
|
} else if ((vendor.contains(QStringLiteral("huayan"))
|
|
|
|
|
|
|| vendor.contains(QStringLiteral("elfin")))
|
|
|
|
|
|
&& normalizedPath.contains(QStringLiteral("elfinmodel"))) {
|
|
|
|
|
|
score += 5;
|
|
|
|
|
|
if (path.contains(QStringLiteral("/485/")))
|
|
|
|
|
|
score += 2;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (score > bestScore) {
|
|
|
|
|
|
bestScore = score;
|
|
|
|
|
|
bestIndex = index;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (bestIndex < 0) {
|
|
|
|
|
|
emit logMessage(QStringLiteral("WARN"),
|
|
|
|
|
|
QStringLiteral("未找到与 %1 %2 %3 匹配的内置 URDF")
|
|
|
|
|
|
.arg(manufacturer, model, subtype));
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
const QString path = m_modelCombo->itemData(bestIndex).toString();
|
|
|
|
|
|
m_modelCombo->setCurrentIndex(bestIndex);
|
|
|
|
|
|
return path == m_currentFile || loadUrdf(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UrdfViewerWidget::refreshBundledModels()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_modelCombo)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
const QDir modelsRoot(QDir(QCoreApplication::applicationDirPath())
|
|
|
|
|
|
.filePath(QStringLiteral("models")));
|
|
|
|
|
|
QStringList files;
|
|
|
|
|
|
if (modelsRoot.exists()) {
|
|
|
|
|
|
QDirIterator iterator(modelsRoot.absolutePath(),
|
|
|
|
|
|
QStringList() << QStringLiteral("*.urdf"),
|
|
|
|
|
|
QDir::Files, QDirIterator::Subdirectories);
|
|
|
|
|
|
while (iterator.hasNext())
|
|
|
|
|
|
files.append(QFileInfo(iterator.next()).absoluteFilePath());
|
|
|
|
|
|
}
|
|
|
|
|
|
files.sort(Qt::CaseInsensitive);
|
|
|
|
|
|
|
|
|
|
|
|
const QSignalBlocker blocker(m_modelCombo);
|
|
|
|
|
|
m_modelCombo->clear();
|
|
|
|
|
|
m_modelCombo->addItem(files.isEmpty()
|
|
|
|
|
|
? QStringLiteral("未发现内置 URDF")
|
|
|
|
|
|
: QStringLiteral("请选择机械臂模型…"),
|
|
|
|
|
|
QString());
|
|
|
|
|
|
for (const QString &path : files) {
|
|
|
|
|
|
QString relative = QDir::fromNativeSeparators(modelsRoot.relativeFilePath(path));
|
|
|
|
|
|
const QStringList parts = relative.split(QLatin1Char('/'), QString::SkipEmptyParts);
|
|
|
|
|
|
QString label = QFileInfo(path).completeBaseName();
|
|
|
|
|
|
if (parts.size() >= 3 && parts.first() == QStringLiteral("aubo_description")) {
|
|
|
|
|
|
label = QStringLiteral("AUBO · %1").arg(label);
|
|
|
|
|
|
} else if (parts.size() >= 4
|
|
|
|
|
|
&& parts.first() == QStringLiteral("elfin_model")) {
|
|
|
|
|
|
label = QStringLiteral("Elfin · %1 · %2").arg(parts.at(2), label);
|
|
|
|
|
|
} else if (parts.size() >= 2
|
|
|
|
|
|
&& parts.first() == QStringLiteral("xiaoyan_description")) {
|
|
|
|
|
|
label = QStringLiteral("小研 · %1").arg(label);
|
|
|
|
|
|
} else if (relative == QStringLiteral("demo_arm.urdf")) {
|
|
|
|
|
|
label = QStringLiteral("演示 · %1").arg(label);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
label = relative;
|
|
|
|
|
|
}
|
|
|
|
|
|
m_modelCombo->addItem(label, path);
|
|
|
|
|
|
m_modelCombo->setItemData(m_modelCombo->count() - 1,
|
|
|
|
|
|
QDir::toNativeSeparators(path), Qt::ToolTipRole);
|
|
|
|
|
|
}
|
|
|
|
|
|
syncModelSelection(m_currentFile);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UrdfViewerWidget::syncModelSelection(const QString &filePath)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_modelCombo)
|
|
|
|
|
|
return;
|
|
|
|
|
|
const QString absolutePath = QFileInfo(filePath).absoluteFilePath();
|
|
|
|
|
|
int index = m_modelCombo->findData(absolutePath);
|
|
|
|
|
|
if (index < 0 && !filePath.isEmpty()) {
|
|
|
|
|
|
m_modelCombo->addItem(
|
|
|
|
|
|
QStringLiteral("自定义 · %1").arg(QFileInfo(filePath).completeBaseName()),
|
|
|
|
|
|
absolutePath);
|
|
|
|
|
|
index = m_modelCombo->count() - 1;
|
|
|
|
|
|
m_modelCombo->setItemData(index, QDir::toNativeSeparators(absolutePath),
|
|
|
|
|
|
Qt::ToolTipRole);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (index >= 0) {
|
|
|
|
|
|
const QSignalBlocker blocker(m_modelCombo);
|
|
|
|
|
|
m_modelCombo->setCurrentIndex(index);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-18 16:22:20 +08:00
|
|
|
|
void UrdfViewerWidget::reloadUrdf()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_currentFile.isEmpty())
|
|
|
|
|
|
loadUrdf(m_currentFile);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UrdfViewerWidget::resetView()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_camera)
|
|
|
|
|
|
return;
|
|
|
|
|
|
m_camera->setPosition(QVector3D(2.8f, -3.2f, 2.2f));
|
|
|
|
|
|
m_camera->setViewCenter(QVector3D(0.0f, 0.0f, 0.7f));
|
|
|
|
|
|
m_camera->setUpVector(QVector3D(0.0f, 0.0f, 1.0f));
|
|
|
|
|
|
if (m_modelRoot)
|
|
|
|
|
|
scheduleViewAll();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UrdfViewerWidget::clearModel()
|
|
|
|
|
|
{
|
|
|
|
|
|
m_joints.clear();
|
|
|
|
|
|
m_jointOrder.clear();
|
|
|
|
|
|
m_jointPositions.clear();
|
|
|
|
|
|
m_selectedTeachingJoint.clear();
|
|
|
|
|
|
m_jointDragActive = false;
|
|
|
|
|
|
if (m_modelRoot) {
|
|
|
|
|
|
delete m_modelRoot;
|
|
|
|
|
|
m_modelRoot = nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString UrdfViewerWidget::resolveMeshPath(const QString &uri) const
|
|
|
|
|
|
{
|
|
|
|
|
|
QString path = QDir::fromNativeSeparators(uri.trimmed());
|
|
|
|
|
|
if (path.isEmpty())
|
|
|
|
|
|
return QString();
|
|
|
|
|
|
|
|
|
|
|
|
const QFileInfo direct(path);
|
|
|
|
|
|
if (direct.isAbsolute() && direct.exists())
|
|
|
|
|
|
return direct.absoluteFilePath();
|
|
|
|
|
|
|
|
|
|
|
|
if (path.startsWith(QStringLiteral("file://"), Qt::CaseInsensitive)) {
|
|
|
|
|
|
const QString local = QUrl(path).toLocalFile();
|
|
|
|
|
|
return QFileInfo::exists(local) ? QFileInfo(local).absoluteFilePath() : QString();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!path.startsWith(QStringLiteral("package://"), Qt::CaseInsensitive)) {
|
|
|
|
|
|
const QString local = QDir(m_urdfDirectory).absoluteFilePath(path);
|
|
|
|
|
|
return QFileInfo::exists(local) ? QFileInfo(local).absoluteFilePath() : QString();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
path.remove(0, QStringLiteral("package://").size());
|
|
|
|
|
|
const int separator = path.indexOf(QLatin1Char('/'));
|
|
|
|
|
|
if (separator <= 0)
|
|
|
|
|
|
return QString();
|
|
|
|
|
|
const QString packageName = path.left(separator);
|
|
|
|
|
|
const QString relativePath = path.mid(separator + 1);
|
|
|
|
|
|
|
|
|
|
|
|
QStringList candidates;
|
|
|
|
|
|
QDir current(m_urdfDirectory);
|
|
|
|
|
|
for (;;) {
|
|
|
|
|
|
if (current.dirName().compare(packageName, Qt::CaseInsensitive) == 0)
|
|
|
|
|
|
candidates.append(current.absoluteFilePath(relativePath));
|
|
|
|
|
|
candidates.append(current.absoluteFilePath(packageName + QLatin1Char('/') + relativePath));
|
|
|
|
|
|
if (!current.cdUp())
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const QStringList rosRoots = QString::fromLocal8Bit(qgetenv("ROS_PACKAGE_PATH"))
|
|
|
|
|
|
.split(QDir::listSeparator(), QString::SkipEmptyParts);
|
|
|
|
|
|
for (const QString &root : rosRoots) {
|
|
|
|
|
|
candidates.append(QDir(root).absoluteFilePath(packageName + QLatin1Char('/') + relativePath));
|
|
|
|
|
|
if (QFileInfo(root).fileName().compare(packageName, Qt::CaseInsensitive) == 0)
|
|
|
|
|
|
candidates.append(QDir(root).absoluteFilePath(relativePath));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const QStringList amentRoots = QString::fromLocal8Bit(qgetenv("AMENT_PREFIX_PATH"))
|
|
|
|
|
|
.split(QDir::listSeparator(), QString::SkipEmptyParts);
|
|
|
|
|
|
for (const QString &root : amentRoots)
|
|
|
|
|
|
candidates.append(QDir(root).absoluteFilePath(
|
|
|
|
|
|
QStringLiteral("share/%1/%2").arg(packageName, relativePath)));
|
|
|
|
|
|
|
|
|
|
|
|
for (const QString &candidate : candidates) {
|
|
|
|
|
|
if (QFileInfo::exists(candidate))
|
|
|
|
|
|
return QFileInfo(candidate).absoluteFilePath();
|
|
|
|
|
|
}
|
|
|
|
|
|
return QString();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UrdfViewerWidget::scheduleViewAll()
|
|
|
|
|
|
{
|
|
|
|
|
|
QTimer::singleShot(120, this, [this]() {
|
|
|
|
|
|
if (m_camera && m_modelRoot) {
|
|
|
|
|
|
m_camera->viewEntity(m_modelRoot);
|
|
|
|
|
|
if (m_view)
|
|
|
|
|
|
m_view->requestUpdate();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
QTimer::singleShot(700, this, [this]() {
|
|
|
|
|
|
if (m_camera && m_modelRoot) {
|
|
|
|
|
|
m_camera->viewEntity(m_modelRoot);
|
|
|
|
|
|
if (m_view)
|
|
|
|
|
|
m_view->requestUpdate();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|