OpenSceneGraph Forum Forum Index OpenSceneGraph Forum
Official forum which mirrors the existent OSG mailing lists. Messages posted here are forwarded to the mailing list and vice versa.
 
   FAQFAQ    SearchSearch    MemberlistMemberlist    RulesRules    UsergroupsUsergroups    RegisterRegister 
 Mail2Forum SettingsMail2Forum Settings  ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
   AlbumAlbum  OpenSceneGraph IRC ChatOpenSceneGraph IRC Chat   SmartFeedSmartFeed 

Same vertices selected multiple times


 
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General
View previous topic :: View next topic  
Author Message
arennuit
User


Joined: 06 Dec 2013
Posts: 70

PostPosted: Thu Jan 11, 2018 3:13 pm    Post subject:
Same vertices selected multiple times
Reply with quote

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.
Back to top
View user's profile Send private message
mp3butcher (Julien Valentin)
Appreciator


Joined: 17 Feb 2010
Posts: 488
Location: France

PostPosted: Fri Jan 12, 2018 8:46 pm    Post subject:
Re: Same vertices selected multiple times
Reply with quote

Hi Antoine

No magic here...
Either you share vertices among triangles else you manage yourself duplicates during selection....

Cheers

arennuit wrote:
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.
Back to top
View user's profile Send private message Visit poster's website
arennuit
User


Joined: 06 Dec 2013
Posts: 70

PostPosted: Mon Jan 15, 2018 3:47 pm    Post subject:
Reply with quote

Hello and thanks for your answer,

What you mean is that I should share the vertices when building the osg::Geometry, right?

This is possible indeed, but then OSG would still select faces in the osgUtil::PolytopeIntersector, and I would still have to convert the faces into vertices, right? So if I understand well, what you are saying is that I should then convert the faces back into vertex iindices using a reference to the primivites set triangles_osg (and not via 3 * primitives[i] + 0/1/2 as currently done). And finally I understand that this would return redundant vertex indices (which are much easier to make unique than vertex coordinates).

Did I get it?

Kind regards,

Antoine.
Back to top
View user's profile Send private message
mp3butcher (Julien Valentin)
Appreciator


Joined: 17 Feb 2010
Posts: 488
Location: France

PostPosted: Mon Jan 15, 2018 4:20 pm    Post subject:
Reply with quote

Hi Antoine
You've totally get it Smile
I would add that TemplatePrimitiveIndexFunctor is your friend for this use case

And an other way would be to use GPU for selection (draw tri as points and output selected vertid using transform feedback) but it would require more work and wouldn't take advantage of existing osg feature...

Cheers

arennuit wrote:
Hello and thanks for your answer,

What you mean is that I should share the vertices when building the osg::Geometry, right?

This is possible indeed, but then OSG would still select faces in the osgUtil::PolytopeIntersector, and I would still have to convert the faces into vertices, right? So if I understand well, what you are saying is that I should then convert the faces back into vertex iindices using a reference to the primivites set triangles_osg (and not via 3 * primitives[i] + 0/1/2 as currently done). And finally I understand that this would return redundant vertex indices (which are much easier to make unique than vertex coordinates).

Did I get it?

Kind regards,

Antoine.
Back to top
View user's profile Send private message Visit poster's website
arennuit
User


Joined: 06 Dec 2013
Posts: 70

PostPosted: Tue Jan 16, 2018 3:34 pm    Post subject:
Reply with quote

Hello Julien,

Thanks for your detailed answer, it is very appreciated!

Cheers,

Antoine.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum

Similar Topics
Topic Author Forum Replies Posted
No new posts Render multiple PRE_RENDER cameras to... ivar General 3 Tue Nov 13, 2018 3:41 pm View latest post
No new posts Rendering multiple screens njr3 General 1 Wed Oct 31, 2018 8:46 pm View latest post
No new posts CompositeViewer with multiple view, a... Angel General 3 Tue Aug 28, 2018 1:15 pm View latest post
No new posts Load OSG trimesh to ODE: Read Vertice... TKoller General 2 Fri Jul 20, 2018 3:06 pm View latest post
No new posts Model(s) load ~10 times longer in 3.6... davisjamesf General 16 Wed Jul 04, 2018 7:18 pm View latest post


Board Security Anti Bot Question MOD - phpBB MOD against Spam Bots
Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP