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

Federico Ferri federico.ferri.it at gmail.com
Mon Mar 9 10:22:43 CET 2020


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20200309/54d4a8f9/attachment.html>


More information about the Interest mailing list