Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to delete a 3d point/QEntity from a 3Dscene?



  • Hi all,

    I've some code for creating a 3D scene, displaying an STL model/object and adding to this some 3D points.
    What I'm looking for is if I want to delete one or more of these points how could I do it.

    Below is my code:

    scenemodifier.h (header file)

    #ifndef SCENEMODIFIER_H
    #define SCENEMODIFIER_H
    
    #include <QtCore/QObject>
    #include <Qt3DCore/qentity.h>
    #include <Qt3DCore/qtransform.h>
    #include <QTime>
    
    class SceneModifier : public QObject
    {
        Q_OBJECT
    
    public:
        explicit SceneModifier(Qt3DCore::QEntity *rootEntity);
        ~SceneModifier();
    
        void LoadSTL();
        void displayPoints(float x, float y,float z);
        void create_light();
        void create_light2();
    
    public slots:
    
    private:
        Qt3DCore::QEntity *m_rootEntity;
        Qt3DCore::QEntity *m_partEntity;
        Qt3DCore::QEntity *m_sphereEntity;
    
    };
    
    #endif // SCENEMODIFIER_H
    

    scenemodifier.cpp (source file of 3D stuff)

    SceneModifier::SceneModifier(Qt3DCore::QEntity *rootEntity)
    : m_rootEntity(rootEntity)
    {
    }
    
    SceneModifier::~SceneModifier()
    {
    }
    
    void SceneModifier::LoadSTL()
    {
    QUrl data =QUrl::fromLocalFile("C:/Users/Dimitrios/MyQtapps/stylus_tip/block1.stl");
    
    Qt3DRender::QMesh *partMesh = new Qt3DRender::QMesh;
    partMesh->setSource(data);
    
     // Part Transform
     Qt3DCore::QTransform *partTransform = new Qt3DCore::QTransform();
    
    
     Qt3DExtras::QPhongMaterial *partMaterial = new Qt3DExtras::QPhongMaterial();
     partMaterial->setDiffuse(QColor(0, 200, 255));
    
        //part (STL model)
        m_partEntity = new Qt3DCore::QEntity(m_rootEntity);
        m_partEntity->addComponent(partMesh);
        m_partEntity->addComponent(partMaterial);
        m_partEntity->addComponent(partTransform);
    }
    
    void SceneModifier::displayPoints(float x, float y, float z)
    {
    // Sphere shape data
    Qt3DExtras::QSphereMesh *sphereMesh = new Qt3DExtras::QSphereMesh();
    sphereMesh->setRadius(0.75f);;
    
    // Sphere mesh transform
    Qt3DCore::QTransform *sphereTransform = new Qt3DCore::QTransform();
    sphereTransform->setTranslation(QVector3D(x, y, z));
    sphereTransform->setScale(1.3f);
    
    Qt3DExtras::QPhongMaterial *sphereMaterial = new Qt3DExtras::QPhongMaterial();
    sphereMaterial->setDiffuse(QColor(250, 250, 0));
    
    // Sphere
    m_sphereEntity = new Qt3DCore::QEntity(m_rootEntity);
    m_sphereEntity->addComponent(sphereMesh);
    m_sphereEntity->addComponent(sphereMaterial);
    m_sphereEntity->addComponent(sphereTransform);
    }
    
    void SceneModifier::create_light()
    {
    Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity(m_rootEntity);
    Qt3DRender::QPointLight *light = new Qt3DRender::QPointLight(lightEntity);
    light->setColor("white");
    light->setIntensity(1);
    lightEntity->addComponent(light);
    
    Qt3DCore::QTransform *lightTransform = new Qt3DCore::QTransform(lightEntity);
    lightTransform->setTranslation(QVector3D(50, -50, -150.0f));
    lightEntity->addComponent(lightTransform);
    }
    
    void SceneModifier::create_light2()
    {
     Qt3DCore::QEntity *lightEntity2 = new Qt3DCore::QEntity(m_rootEntity);
     Qt3DRender::QPointLight *light2 = new Qt3DRender::QPointLight(lightEntity2);
        light2->setColor("white");
        light2->setIntensity(1);
        lightEntity2->addComponent(light2);
    
    Qt3DCore::QTransform *lightTransform2 = new Qt3DCore::QTransform(lightEntity2);
        lightTransform2->setTranslation(QVector3D(-50, 50, 150.0f));
        lightEntity2->addComponent(lightTransform2);
    }
    

    mainwindow.h (mainwindow header file)

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include "scenemodifier.h"
    
    #include <QMainWindow>
    #include <QVector3D>
    #include <QtCore/QObject>
    #include <QPushButton>
    
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    public slots:
    
    
    private slots:
     void setup3D();
     void tick();
     void addmultipoints();
    
    signals:
    
    private:
        Ui::MainWindow *ui;
        SceneModifier *modifier;
            QTimer *tmr;
            int pttmr;
            QPushButton *addButton;
    
    };
    
    #endif // MAINWINDOW_H
    

    mainwindow.cpp

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    setup3D();
    
    tmr = new QTimer(this);
    tmr->setInterval(1000);
    
    modifier->LoadSTL();
    modifier->create_light();
    modifier->create_light2();
    modifier->displayPoints(10, 10, 30);
    
    connect(tmr,SIGNAL(timeout()),this,SLOT(tick()));
    connect(addButton,SIGNAL(released()),this,SLOT(addmultipoints()));
    
    pttmr=1;
    tmr->start();
    
    }
    
    MainWindow::~MainWindow()
    {
    tmr->stop();
    delete ui;
    }
    
    void MainWindow::addmultipoints()
    {
    float x=80.0;
    float y=80.0;
    float z=20.0;
    
    modifier->displayPoints(x,y,z);
    }
    
    void MainWindow::tick()
    {
    pttmr=pttmr+1;
    modifier->displayPoints(70,70,pttmr);
    qDebug() << pttmr;
    }
    
    void MainWindow::setup3D()
    {
    Qt3DExtras::Qt3DWindow *view= new Qt3DExtras::Qt3DWindow();
    
    Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity;
    modifier = new SceneModifier(rootEntity);
    
    addButton = new QPushButton("button");
    QWidget *container=QWidget::createWindowContainer(view);
    
    QHBoxLayout *hLayout = new QHBoxLayout(ui->centralWidget);
    hLayout->addWidget(container,1);
    hLayout->addWidget(addButton);
    
    Qt3DRender::QCamera *camera = view->camera();
    camera->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);
    camera->setPosition(QVector3D(0, 0, 320.0f));
    camera->setViewCenter(QVector3D(0, 0, 0));
    
    Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(rootEntity);
    camController->setCamera(camera);
    
    view->setRootEntity(rootEntity);
    view->show();
    }
    

    If you have any ideas for deleting the points or improving existing code, please let me know.
    Thank you in advance for your time and help.



  • Just in the case anyone is interested in this, I have sort it out.
    In short, below is my solution.

    Every point created is a Qt3DCore::QEntity *m_sphereEntity with a node id;
    These are stored in a QVectorQt3DCore::QEntity* entityVector;

    Also the 3d coordinates of each point are stored along with the id in a custom QAbstractTableModel and displayed in a tableView, where the user can select what to delete.

    With this selection the node ID is known, which by scanning the whole entityVector, the respective point is found and gets deleted.

     // int v is the sphereEntity node id selected
     void SceneModifier::destroyPoint(int v)
        {
                for (int i=0; i<entityVector.size(); i++)
                      {
                         qDebug() << entityVector.at(i)->id() << "and v= " << v;
                                 if (entityVector.at(i)->id().id()==v)
                                 {
                                     qDebug() << "FOUND " << i;
                                        entityVector.at(i)->~QEntity();
                                        entityVector.removeAt(i);
                                }
                        }
    // check vector's size that point is deleted
    qDebug() << "Size of entityVector inside destroyPoint after deletion: " << entityVector.size();
        
        }
    

    I hope anyone interested in this topic will find my solution useful.



  • Old posts on internet are always useful, thank you.


Log in to reply