[Interest] Accessing vertex data of primitive shapes (cuboid, sphere, …)

Federico Ferri federico.ferri.it at gmail.com
Tue Mar 10 15:14:31 CET 2020


Nevermind, I was keeping the previous result of Qt3DRender::QBuffer::data()
which was invalid.

Calling buf->data() again after setData/setSyncData gave thew correct
result.


On 9 March 2020 at 13:35:40, Federico Ferri (federico.ferri.it at gmail.com)
wrote:

I added your fix (just after the line const QByteArray &data =
buf->data();) but the problem persists (Cuboid mesh):

geometryBoundingBox: Qt3DExtras::QCuboidGeometry(0x7ff299c87ba0)

geometryBoundingBox: setData

geometryBoundingBox: scanning vertex 0 0 0

geometryBoundingBox: scanning vertex 0 0 0

geometryBoundingBox: scanning vertex nan 2.52234e-44 0

geometryBoundingBox: scanning vertex nan 4.2039e-45 0

geometryBoundingBox: scanning vertex nan 5.60519e-45 0

geometryBoundingBox: scanning vertex nan 8.40779e-45 0

geometryBoundingBox: scanning vertex nan 8.40779e-45 0

geometryBoundingBox: scanning vertex nan 1.4013e-44 0

geometryBoundingBox: scanning vertex nan 1.4013e-44 0

geometryBoundingBox: scanning vertex nan 1.68156e-44 0

geometryBoundingBox: scanning vertex nan 1.68156e-44 0

geometryBoundingBox: scanning vertex nan 1.12104e-44 0

geometryBoundingBox: scanning vertex nan 1.12104e-44 0

geometryBoundingBox: scanning vertex 4.49633e+27 2.12622e-07 8.50487e-07

geometryBoundingBox: scanning vertex 2.72478e+26 1.65959e-07 4.22971e+21

geometryBoundingBox: scanning vertex 1.2992e-08 1.37658e+11 1.01525e+12

geometryBoundingBox: scanning vertex 1.2992e-08 5.50632e+11 1.29257e+19

geometryBoundingBox: scanning vertex 0 0 nan

geometryBoundingBox: scanning vertex 1.17589e-37 1.4013e-45 1.17589e-37

geometryBoundingBox: scanning vertex 0 0 nan

geometryBoundingBox: scanning vertex nan 1.4013e-45 0

geometryBoundingBox: scanning vertex 3.36312e-44 0 9.27555e-39

geometryBoundingBox: scanning vertex 1.45735e-43 0 nan

geometryBoundingBox: scanning vertex nan 1.12104e-44 0



QVector<qreal> geometryBoundingBox(Qt3DRender::QGeometry *geom)

{

    qDebug() << "geometryBoundingBox:" << geom;

    QVector<qreal> ret;

    auto attrs = geom->attributes();

    for(const auto &attr : attrs)

    {

        if(attr->name() != "vertexPosition") continue;

        int n = attr->count();

        auto buf = attr->buffer();

        if(!buf) continue;

        const QByteArray &data = buf->data();

        if(data.isEmpty())

        {

            qDebug() << "geometryBoundingBox: setData";

            buf->setData(buf->dataGenerator()->operator()());

        }

        buf->setSyncData(true);

        buf->setAccessType(Qt3DRender::QBuffer::AccessType::ReadWrite);

        float vmin[3], vmax[3];

        for(int i = 0; i < n; i++)

        {

            int vertexOffset = i * attr->byteStride();

            int offset = vertexOffset + attr->byteOffset();

            const char *rawData = &(data.constData()[offset]);

            auto value = reinterpret_cast<const float*>(rawData);

            qDebug() << "geometryBoundingBox: scanning vertex" <<
value[0] << value[1] << value[2];

            for(int j = 0; j < 3; j++)

            {

                vmin[j] = i ? qMin(vmin[j], value[j]) : value[j];

                vmax[j] = i ? qMax(vmax[j], value[j]) : value[j];

            }

        }

        ret.resize(6);

        ret[0] = vmin[0]; ret[1] = vmin[1]; ret[2] = vmin[2];

        ret[3] = vmax[0]; ret[4] = vmax[1]; ret[5] = vmax[2];

        return ret;

    }

    return ret;

}





On 9 March 2020 at 11:46:12, Megidd Git (megiddgit at gmail.com) wrote:

You might want to do this:

       Qt3DRender::QGeometryRenderer *mesh = ...

       Qt3DRender::QGeometry *meshGeometry = mesh->geometry();

    for (Qt3DRender::QAttribute *attribute : meshGeometry->attributes()) {

        Qt3DRender::QBuffer *buf = attribute->buffer();

        if (buf) {

            if (buf->data().isEmpty())

                buf->setData(buf->dataGenerator()->operator()());

            buf->setSyncData(true);

            buf->setAccessType(Qt3DRender::QBuffer::AccessType::ReadWrite);

        }

    }


On Mon, Mar 9, 2020 at 12:54 PM Federico Ferri <federico.ferri.it at gmail.com>
wrote:

> I’m trying to access vertex data of QGeometry objects for computing the
> bounding box of the object.
> This works fine for QMesh, but for QCuboidMesh, QSphereMesh, etc… vertex
> data seems invalid.
> Is there some method to call to make vertex data actual?
>
> This is my code:
>
> QVector<qreal> entityBoundingBox(Qt3DCore::QEntity *entity)
> {
>     for(const auto &comp : entity->componentsOfType<Qt3DRender::QGeometryRenderer>())
>     {
>         auto geom = comp->geometry();
>         if(geom)
>             return geometryBoundingBox(geom);
>     }
>     return {};
> }
>
> QVector<qreal> geometryBoundingBox(Qt3DRender::QGeometry *geom)
> {
>     QVector<qreal> ret;
>     auto attrs = geom->attributes();
>     for(const auto &attr : attrs)
>     {
>         if(attr->name() != "vertexPosition") continue;
>         int n = attr->count();
>         auto buf = attr->buffer();
>         const QByteArray &data = buf->data();
>         float vmin[3], vmax[3];
>         for(int i = 0; i < n; i++)
>         {
>             int vertexOffset = i * attr->byteStride();
>             int offset = vertexOffset + attr->byteOffset();
>             const char *rawData = &(data.constData()[offset]);
>             auto value = reinterpret_cast<const float*>(rawData);
>             qDebug() << "geometryBoundingBox: scanning vertex" << value[0] << value[1] << value[2];
>             for(int j = 0; j < 3; j++)
>             {
>                 vmin[j] = i ? qMin(vmin[j], value[j]) : value[j];
>                 vmax[j] = i ? qMax(vmax[j], value[j]) : value[j];
>             }
>         }
>         ret.resize(6);
>         ret[0] = vmin[0]; ret[1] = vmin[1]; ret[2] = vmin[2];
>         ret[3] = vmax[0]; ret[4] = vmax[1]; ret[5] = vmax[2];
>         return ret;
>     }
>     return ret;
> }
>
>
> when working on the cuboid or sphere meshes I see vertex data such as:
>
> geometryBoundingBox: scanning vertex 1.2992e-08 5.50632e+11 1.29257e+19
>
> geometryBoundingBox: scanning vertex 0 0 nan
>
> geometryBoundingBox: scanning vertex 9.40053e-33 1.4013e-45 9.40053e-33
>
> geometryBoundingBox: scanning vertex 0 0 nan
>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> https://lists.qt-project.org/listinfo/interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20200310/99ab3eb7/attachment.html>


More information about the Interest mailing list