3

我的公司使用 Qt3D 来显示其 CAD 模型。Wee 尝试使用该函数QCamera::viewEntity(Qt3DCore::QEntity *entity)来计算给定实体的边界球,并让实体适应屏幕。

现在,我们偶然发现了一个在空QEntity节点的情况下无法解决的问题。如果它根本不包含任何顶点/点,我将把它称为空节点。在这种情况下,我预计在计算包围体时应该忽略它。相反,它似乎会被处理,因为它有一个中心为 (0.,0.,0.) 且半径为 0 的边界球。

下面的代码说明了这个问题:

主文件

#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QFrame>
#include <Qt3DRender/QRenderSettings>
#include <Qt3DCore/QTransform>
#include <Qt3DRender/QCamera>
#include <Qt3DExtras/QSphereMesh>
#include <Qt3DExtras/QDiffuseSpecularMaterial>
#include <Qt3DExtras/QForwardRenderer>
#include <Qt3DExtras/Qt3DWindow>
#include <Qt3DRender/QCamera>
#include <Qt3DRender/QCameraLens>
#include <QPushButton>

Qt3DCore::QEntity* createSphereMesh()
{
    auto sphereMat = new Qt3DExtras::QDiffuseSpecularMaterial;
    sphereMat->setDiffuse(QColor(Qt::blue));

    auto mesh = new Qt3DExtras::QSphereMesh();
    mesh->setRadius(0.5);

    auto meshEntity = new Qt3DCore::QEntity;
    meshEntity->addComponent(mesh);
    meshEntity->addComponent(sphereMat);

    return meshEntity;
}

int main(int argc, char* argv[])
{
    QApplication a(argc, argv);

    auto view = new Qt3DExtras::Qt3DWindow();
    view->defaultFrameGraph()->setClearColor(QColor(127, 127, 127));
    auto settings = view->renderSettings();
    settings->setActiveFrameGraph(view->activeFrameGraph());

    auto rootEntity = new Qt3DCore::QEntity();
    view->setRootEntity(rootEntity);

    auto sphere1 = createSphereMesh();
    sphere1->setParent(rootEntity);
    auto trafo1 = new Qt3DCore::QTransform;
    trafo1->setTranslation(QVector3D(20, 10, 0));
    sphere1->addComponent(trafo1);
    auto sphere2 = createSphereMesh();
    sphere2->setParent(rootEntity);
    auto trafo2 = new Qt3DCore::QTransform;
    trafo2->setTranslation(QVector3D(20, -10, 0));
    sphere2->addComponent(trafo2);

    QObject::connect(view->camera()->lens(), &Qt3DRender::QCameraLens::viewSphere, [&](const QVector3D& center, float radius) {
        qDebug() << "Bounding Sphere:" << center << radius;
        auto boundingSphereEntity = new Qt3DCore::QEntity;

        auto sphereMat = new Qt3DExtras::QDiffuseSpecularMaterial;
        sphereMat->setAlphaBlendingEnabled(true);
        sphereMat->setDiffuse(QColor(255,255,255,80));

        auto mesh = new Qt3DExtras::QSphereMesh();
        mesh->setRadius(radius);

        boundingSphereEntity->addComponent(sphereMat);
        boundingSphereEntity->addComponent(mesh);

        auto trafoAll = new Qt3DCore::QTransform;
        trafoAll->setTranslation(center);
        boundingSphereEntity->addComponent(trafoAll);
        boundingSphereEntity->addComponent(mesh);
        boundingSphereEntity->addComponent(sphereMat);
        boundingSphereEntity->setParent(rootEntity);
        });
    auto container = QWidget::createWindowContainer(view);


    auto viewAllBtn = new QPushButton("View All");
    QObject::connect(viewAllBtn, &QPushButton::clicked, [&]() {
        view->camera()->viewAll();
        });
    QFrame frame;
    frame.setFixedSize(500, 500);
    frame.setLayout(new QVBoxLayout);
    frame.layout()->addWidget(container);
    frame.layout()->addWidget(viewAllBtn);

    frame.show();

    return a.exec();
}

我有两个半径为 0.5 的蓝色球体,彼此之间的距离为 20。我期待一个中心(20,0,0)和半径(10.5)的边界球。

相反,程序打印:

Bounding Sphere: QVector3D(12.1432, 2.14455, -1.32699e-08) 14.9644

似乎值 2.14455 真的来自无处可去,而且我添加的边界体积球体有些不可预测。

意外的包围体球体

如果我用结果替换翻译QVector3D(20, 10, 0)QVector3D(0,10,0)结果会像预期的那样。

正确的包围体球体

如何从我的包围体计算中排除根实体?

4

0 回答 0