[osg-users] Same vertices selected multiple times

Antoine Rennuit antoinerennuit at hotmail.com
Thu Jan 11 07:13:39 PST 2018


Dear OSG forum,

I have a vertices selection problem (these are the vertices of a mesh). When I select vertices, each vertex appears multiple times in my selection (e.g. if I select 30000 vertices, there are actually 90000 entries in the selected vertices array - each vertex being duplicated).

The reason of this behavior stems from the way I have coded the selection: I actually select faces/triangles, then look for the corresponding vertices and add these to my selected vertices array, via this code:


Code:

// Find the primitives inside the polygon.
osg::ref_ptr<osgUtil::PolytopeIntersector> intersector = new osgUtil::PolytopeIntersector( osgUtil::Intersector::WINDOW, polytope );
osgUtil::IntersectionVisitor intersectionVisitor( intersector.get() );
viewer->getCamera()->accept( intersectionVisitor );

if ( !intersector->containsIntersections() )
    return;

// Loop through each primitive.
primitives.reserve( intersector->getIntersections().size() );

for (auto it = intersector->getIntersections().begin(); it != intersector->getIntersections().end(); ++it)
{
    osgUtil::PolytopeIntersector::Intersection intersection = *it;

    primitives.push_back( intersection.primitiveIndex );
}

std::vector<unsigned int> subObjects;
for (uint i = 0; i< primitives.size(); ++i)
{
    subObjects.push_back(3 * primitives[i]);
    subObjects.push_back(3 * primitives[i] + 1);
    subObjects.push_back(3 * primitives[i] + 2);
}




subObjects being my vertices array.

Do you have any idea of how I could do to only select vertices once rather than multiple times?

THIS IS NOT DIRECTLY LINKED TO THE QUESTION BUT IT COULD HAVE AN IMPACT ON YOUR ANSWER: for your information, currently the mesh is drawn in such a way that vertices are NOT shared among triangles (that is if 2 triangles share the same vertex, the same vertex appears twice in the vertices array) as shown below:


Code:

osg::ref_ptr<osg::Geode> SetupMesh(const Eigen::Vector3Array& vertices, IntegerArray const& triangles, const Eigen::Vector3Array& normals, const osg::Vec4f& color, Node* node)
{
    osg::ref_ptr<osg::Geode> geode = new osg::Geode();
    assert(geode);
    osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry();
    assert(geometry);
    osg::ref_ptr<osg::Vec3Array> vertices_osg = new osg::Vec3Array();
    assert(vertices_osg);
    osg::ref_ptr<osg::Vec3Array> normals_osg = new osg::Vec3Array();
    assert(normals_osg);
    osg::ref_ptr<osg::DrawElementsUInt> triangles_osg = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
    assert(triangles_osg);
    osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array();
    assert(colors);

    // Attach a reference to the node inside the geometry.
    // NOTE: this is used for picking.
    osg::ref_ptr<NodeReference> userData = new NodeReference(node);
    geometry->setUserData(userData);

    // Use VBOs (rather than deprecated display lists).
    geometry->setUseVertexBufferObjects(true);

    // Setup.
    geode->addDrawable(geometry);
    geometry->setVertexArray(vertices_osg);
    geometry->setNormalArray(normals_osg);
    geometry->addPrimitiveSet(triangles_osg);
    geometry->setColorArray(colors);

    // Vertices.
    vertices_osg->reserve(vertices.size());
    for (int i = 0; i < vertices.size(); ++i)
        vertices_osg->push_back(EigenToOsgVector3(vertices[i]));

    // Normals.
    normals_osg->setBinding(osg::Array::BIND_PER_VERTEX);

    normals_osg->reserve(normals.size());
    for (int i = 0; i < normals.size(); ++i)
        normals_osg->push_back(EigenToOsgVector3(normals[i]));

    // Triangles (primitive sets).
    triangles_osg->reserve(triangles.size());
    for (int i = 0; i < triangles.size(); ++i)
        triangles_osg->push_back(triangles[i]);

    // Colors.
    colors->setBinding(osg::Array::BIND_PER_VERTEX);

    colors->reserve(vertices.size());
    for (int i = 0; i < vertices.size(); ++i)
        colors->push_back(color);

    return geode;
}




Kind regards,

Antoine.

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=72734#72734







More information about the osg-users mailing list