Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix refresh of LineSegmentShapeNode #1381

Merged
merged 3 commits into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Fixed memory leaks from dart::gui::osg::Viewer: [#1349](https://github.com/dartsim/dart/pull/1349)
* Added point rendering mode to PointCloudShape: [#1351](https://github.com/dartsim/dart/pull/1351), [#1355](https://github.com/dartsim/dart/pull/1355)
* Updated ImGui to 1.71: [#1362](https://github.com/dartsim/dart/pull/1362)
* Fixed refresh of LineSegmentShapeNode: [#1381](https://github.com/dartsim/dart/pull/1381)

* dartpy

Expand Down
15 changes: 8 additions & 7 deletions dart/gui/osg/render/LineSegmentShapeNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class LineSegmentShapeDrawable : public ::osg::Geometry

::osg::ref_ptr<::osg::Vec3Array> mVertices;
::osg::ref_ptr<::osg::Vec4Array> mColors;
::osg::ref_ptr<::osg::DrawElementsUInt> mElements;
};

//==============================================================================
Expand Down Expand Up @@ -187,8 +188,10 @@ LineSegmentShapeDrawable::LineSegmentShapeDrawable(
: mLineSegmentShape(shape),
mVisualAspect(visualAspect),
mVertices(new ::osg::Vec3Array),
mColors(new ::osg::Vec4Array)
mColors(new ::osg::Vec4Array),
mElements(new ::osg::DrawElementsUInt(::osg::PrimitiveSet::LINES))
{
addPrimitiveSet(mElements);
refresh(true);
}

Expand All @@ -207,18 +210,16 @@ void LineSegmentShapeDrawable::refresh(bool firstTime)
const common::aligned_vector<Eigen::Vector2i>& connections
= mLineSegmentShape->getConnections();

::osg::ref_ptr<::osg::DrawElementsUInt> elements
= new ::osg::DrawElementsUInt(::osg::PrimitiveSet::LINES);
elements->reserve(2 * connections.size());
mElements->resize(2 * connections.size());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want to use resize(~) alongside push_back(~), because that means we're going to make mElements the size 2 * connections.size() and then later push back an additional 2 * connections.size() number of new elements.

I would recommend using reserve(2 * connections.size() and then clear().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively resize(~) and then [2*i] = and [2*i+1] = would also work, but that feels more risky to me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this out. Let me fix this as suggested.


for (std::size_t i = 0; i < connections.size(); ++i)
{
const Eigen::Vector2i& c = connections[i];
elements->push_back(c[0]);
elements->push_back(c[1]);
mElements->push_back(static_cast<unsigned int>(c[0]));
mElements->push_back(static_cast<unsigned int>(c[1]));
}

addPrimitiveSet(elements);
setPrimitiveSet(0, mElements);
}

if (mLineSegmentShape->checkDataVariance(
Expand Down