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

Improve voxel grid and point cloud rendering performance #1294

Merged
merged 3 commits into from
Apr 28, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

#### Changes

* XXX
* GUI

* XXX: [#XXXX](https://github.com/dartsim/dart/pull/XXXX)
* Improved voxel grid and point cloud rendering performance: [#1294](https://github.com/dartsim/dart/pull/1294)

#### Compilers Tested

Expand Down
8 changes: 7 additions & 1 deletion dart/dynamics/PointCloudShape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Eigen::Vector3d toVector3d(const octomap::point3d& point)
PointCloudShape::PointCloudShape(double visualSize)
: Shape(), mVisualSize(visualSize)
{
mVariance = DYNAMIC_ELEMENTS;
// Do nothing
}

//==============================================================================
Expand Down Expand Up @@ -154,6 +154,12 @@ double PointCloudShape::getVisualSize() const
return mVisualSize;
}

//==============================================================================
void PointCloudShape::notifyColorUpdated(const Eigen::Vector4d& /*color*/)
{
incrementVersion();
}

//==============================================================================
void PointCloudShape::updateVolume() const
{
Expand Down
3 changes: 3 additions & 0 deletions dart/dynamics/PointCloudShape.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ class PointCloudShape : public Shape
/// Returns size of visual object that represents each point.
double getVisualSize() const;

// Documentation inherited.
void notifyColorUpdated(const Eigen::Vector4d& color) override;

protected:
// Documentation inherited.
void updateVolume() const override;
Expand Down
8 changes: 6 additions & 2 deletions dart/dynamics/VoxelGridShape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ VoxelGridShape::VoxelGridShape(fcl_shared_ptr<octomap::OcTree> octree) : Shape()
}

setOctree(std::move(octree));

mVariance = DYNAMIC_ELEMENTS;
}

//==============================================================================
Expand Down Expand Up @@ -212,6 +210,12 @@ Eigen::Matrix3d VoxelGridShape::computeInertia(double /*mass*/) const
return Eigen::Matrix3d::Identity();
}

//==============================================================================
void VoxelGridShape::notifyColorUpdated(const Eigen::Vector4d& /*color*/)
{
incrementVersion();
}

//==============================================================================
void VoxelGridShape::updateBoundingBox() const
{
Expand Down
3 changes: 3 additions & 0 deletions dart/dynamics/VoxelGridShape.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ class VoxelGridShape : public Shape
// Documentation inherited.
Eigen::Matrix3d computeInertia(double mass) const override;

// Documentation inherited.
void notifyColorUpdated(const Eigen::Vector4d& color) override;

protected:
// Documentation inherited.
void updateBoundingBox() const override;
Expand Down
58 changes: 27 additions & 31 deletions dart/gui/osg/render/PointCloudShapeNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class PointCloudShapeDrawable : public ::osg::ShapeDrawable
::osg::CompositeShape* osgShape,
const std::vector<Eigen::Vector3d>& points,
double size);
std::size_t mPointCloudVersion;
std::vector<::osg::ref_ptr<::osg::Box>> mBoxes;
};

Expand All @@ -99,24 +98,32 @@ class PointCloudShapeGeode : public ShapeNode, public ::osg::Geode
PointCloudShapeNode::PointCloudShapeNode(
std::shared_ptr<dart::dynamics::PointCloudShape> shape,
ShapeFrameNode* parent)
: ShapeNode(shape, parent, this), mPointCloudShape(shape), mGeode(nullptr)
: ShapeNode(shape, parent, this),
mPointCloudShape(shape),
mGeode(nullptr),
mPointCloudVersion(dynamics::INVALID_INDEX)
{
mNode = this;
extractData(true);
setNodeMask(mVisualAspect->isHidden() ? 0x0 : ~0x0);
setNodeMask(mVisualAspect->isHidden() ? 0x0u : ~0x0u);
}

//==============================================================================
void PointCloudShapeNode::refresh()
{
mUtilized = true;

setNodeMask(mVisualAspect->isHidden() ? 0x0 : ~0x0);
setNodeMask(mVisualAspect->isHidden() ? 0x0u : ~0x0u);

if (mShape->getDataVariance() == dart::dynamics::Shape::STATIC)
if (mShape->getDataVariance() == dart::dynamics::Shape::STATIC
&& mPointCloudVersion == mPointCloudShape->getVersion())
{
return;
}

extractData(false);

mPointCloudVersion = mPointCloudShape->getVersion();
}

//==============================================================================
Expand Down Expand Up @@ -184,52 +191,41 @@ PointCloudShapeDrawable::PointCloudShapeDrawable(
mVisualAspect(visualAspect),
mVertices(new ::osg::Vec3Array),
mColors(new ::osg::Vec4Array),
mOsgShape(new ::osg::CompositeShape()),
mPointCloudVersion(dynamics::INVALID_INDEX)
mOsgShape(new ::osg::CompositeShape())
{
refresh(true);
}

//==============================================================================
void PointCloudShapeDrawable::refresh(bool firstTime)
void PointCloudShapeDrawable::refresh(bool /*firstTime*/)
{
if (mPointCloudShape->getDataVariance() == dart::dynamics::Shape::STATIC)
setDataVariance(::osg::Object::STATIC);
else
setDataVariance(::osg::Object::DYNAMIC);

if (mPointCloudShape->checkDataVariance(dynamics::Shape::DYNAMIC_ELEMENTS)
|| firstTime)
// This function is called whenever the point cloud version is increased, and
// the point cloud could be updated in the version up. So we always update the
// point cloud.
{
if (mPointCloudVersion != mPointCloudShape->getVersion())
{
const auto size = mPointCloudShape->getVisualSize();
const auto& points = mPointCloudShape->getPoints();
const auto size = mPointCloudShape->getVisualSize();
const auto& points = mPointCloudShape->getPoints();

auto osgShape = new ::osg::CompositeShape();
addBoxes(osgShape, points, size);
auto osgShape = new ::osg::CompositeShape();
addBoxes(osgShape, points, size);

setShape(osgShape);
dirtyDisplayList();
}
setShape(osgShape);
dirtyDisplayList();
}

if (mPointCloudShape->checkDataVariance(dart::dynamics::Shape::DYNAMIC_COLOR)
|| firstTime)
// This function is called whenever the point cloud version is increased, and
// the color could be updated in the version up. So we always update the
// color.
{
setColor(eigToOsgVec4(mVisualAspect->getRGBA()));
}
}

//==============================================================================
::osg::Vec3 toVec3(const Eigen::Vector3d& point)
{
return ::osg::Vec3(
static_cast<float>(point.x()),
static_cast<float>(point.y()),
static_cast<float>(point.z()));
}

//==============================================================================
void PointCloudShapeDrawable::addBoxes(
::osg::CompositeShape* osgShape,
Expand All @@ -254,7 +250,7 @@ void PointCloudShapeDrawable::addBoxes(
for (auto i = mBoxes.size(); i < points.size(); ++i)
{
auto osgSphere
= new ::osg::Box(toVec3(points[i]), static_cast<float>(size));
= new ::osg::Box(eigToOsgVec3(points[i]), static_cast<float>(size));
mBoxes.emplace_back(osgSphere);
osgShape->addChild(mBoxes.back());
}
Expand Down
1 change: 1 addition & 0 deletions dart/gui/osg/render/PointCloudShapeNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class PointCloudShapeNode : public ShapeNode, public ::osg::Group

std::shared_ptr<dart::dynamics::PointCloudShape> mPointCloudShape;
PointCloudShapeGeode* mGeode;
std::size_t mPointCloudVersion;
};

} // namespace render
Expand Down
24 changes: 17 additions & 7 deletions dart/gui/osg/render/VoxelGridShapeNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ class VoxelGridShapeGeode : public ShapeNode, public ::osg::Geode
//==============================================================================
VoxelGridShapeNode::VoxelGridShapeNode(
std::shared_ptr<dynamics::VoxelGridShape> shape, ShapeFrameNode* parent)
: ShapeNode(shape, parent, this), mVoxelGridShape(shape), mGeode(nullptr)
: ShapeNode(shape, parent, this),
mVoxelGridShape(shape),
mGeode(nullptr),
mVoxelGridVersion(dynamics::INVALID_INDEX)
{
extractData(true);
setNodeMask(mVisualAspect->isHidden() ? 0x0u : ~0x0u);
Expand All @@ -111,10 +114,15 @@ void VoxelGridShapeNode::refresh()

setNodeMask(mVisualAspect->isHidden() ? 0x0u : ~0x0u);

if (mShape->getDataVariance() == dynamics::Shape::STATIC)
if (mShape->getDataVariance() == dart::dynamics::Shape::STATIC
&& mVoxelGridVersion == mVoxelGridShape->getVersion())
{
return;
}

extractData(false);

mVoxelGridVersion = mVoxelGridShape->getVersion();
}

//==============================================================================
Expand Down Expand Up @@ -193,15 +201,16 @@ VoxelGridShapeDrawable::VoxelGridShapeDrawable(
}

//==============================================================================
void VoxelGridShapeDrawable::refresh(bool firstTime)
void VoxelGridShapeDrawable::refresh(bool /*firstTime*/)
{
if (mVoxelGridShape->getDataVariance() == dynamics::Shape::STATIC)
setDataVariance(::osg::Object::STATIC);
else
setDataVariance(::osg::Object::DYNAMIC);

if (mVoxelGridShape->checkDataVariance(dynamics::Shape::DYNAMIC_ELEMENTS)
|| firstTime)
// This function is called whenever the voxel grid version is increased, and
// the voxel grid could be updated in the version up. So we always update the
// voxel grid.
{
if (mVoxelVersion != mVoxelGridShape->getVersion())
{
Expand All @@ -214,8 +223,9 @@ void VoxelGridShapeDrawable::refresh(bool firstTime)
}
}

if (mVoxelGridShape->checkDataVariance(dynamics::Shape::DYNAMIC_COLOR)
|| firstTime)
// This function is called whenever the point voxel grid is increased, and
// the color could be updated in the version up. So we always update the
// color.
{
setColor(eigToOsgVec4(mVisualAspect->getRGBA()));
}
Expand Down
1 change: 1 addition & 0 deletions dart/gui/osg/render/VoxelGridShapeNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class VoxelGridShapeNode : public ShapeNode, public ::osg::MatrixTransform

std::shared_ptr<dynamics::VoxelGridShape> mVoxelGridShape;
VoxelGridShapeGeode* mGeode;
std::size_t mVoxelGridVersion;
};

} // namespace render
Expand Down
39 changes: 32 additions & 7 deletions examples/osgExamples/osgPointCloud/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ using namespace dart::common;
using namespace dart::dynamics;
using namespace dart::math;

const std::string& robotName = "KR5";
static const std::string& robotName = "KR5";

class PointCloudWorld : public gui::osg::WorldNode
{
Expand Down Expand Up @@ -72,13 +72,16 @@ class PointCloudWorld : public gui::osg::WorldNode
if (!mRobot)
return;

if (!mUpdate)
return;

// Set robot pose
Eigen::VectorXd pos = mRobot->getPositions();
pos += 0.01 * Eigen::VectorXd::Random(pos.size());
mRobot->setPositions(pos);

// Generate point cloud from robot meshes
auto pointCloud = generatePointCloud(100);
auto pointCloud = generatePointCloud(500);

// Update sensor position
static double time = 0.0;
Expand All @@ -105,11 +108,22 @@ class PointCloudWorld : public gui::osg::WorldNode
{
return mPointCloudVisualAspect;
}

dynamics::VisualAspect* getVoxelGridVisualAspect()
{
return mVoxelGridVisualAspect;
}

void setUpdate(bool update)
{
mUpdate = update;
}

bool getUpdate() const
{
return mUpdate;
}

protected:
octomap::Pointcloud generatePointCloud(std::size_t numPoints)
{
Expand Down Expand Up @@ -173,6 +187,8 @@ class PointCloudWorld : public gui::osg::WorldNode

dynamics::VisualAspect* mPointCloudVisualAspect;
dynamics::VisualAspect* mVoxelGridVisualAspect;

bool mUpdate{true};
};

class PointCloudWidget : public dart::gui::osg::ImGuiWidget
Expand Down Expand Up @@ -247,6 +263,15 @@ class PointCloudWidget : public dart::gui::osg::ImGuiWidget
if (ImGui::RadioButton("Pause", &e, 1) && mViewer->isSimulating())
mViewer->simulate(false);
}

int robotUpdate = mNode->getUpdate() ? 0 : 1;
if (ImGui::RadioButton("Run Robot Updating", &robotUpdate, 0)
&& mNode->getUpdate())
mNode->setUpdate(true);
ImGui::SameLine();
if (ImGui::RadioButton("Stop Robot Updating", &robotUpdate, 1)
&& mNode->getUpdate())
mNode->setUpdate(false);
}

if (ImGui::CollapsingHeader("View", ImGuiTreeNodeFlags_DefaultOpen))
Expand Down Expand Up @@ -329,26 +354,26 @@ class PointCloudWidget : public dart::gui::osg::ImGuiWidget
minorLinesPerMajorLine
= static_cast<int>(mGrid->getNumMinorLinesPerMajorLine());
if (ImGui::InputInt(
"Minor Lines per Major Line", &minorLinesPerMajorLine, 1, 5))
"Minor Lines per Major Line", &minorLinesPerMajorLine, 1, 5))
{
if (minorLinesPerMajorLine < 0)
minorLinesPerMajorLine = 0;
mGrid->setNumMinorLinesPerMajorLine(
static_cast<std::size_t>(minorLinesPerMajorLine));
static_cast<std::size_t>(minorLinesPerMajorLine));
}

static float axisLineWidth;
axisLineWidth = mGrid->getAxisLineWidth();
if (ImGui::InputFloat(
"Axis Line Width", &axisLineWidth, 1.f, 2.f, "%.0f"))
"Axis Line Width", &axisLineWidth, 1.f, 2.f, "%.0f"))
{
mGrid->setAxisLineWidth(axisLineWidth);
}

static float majorLineWidth;
majorLineWidth = mGrid->getMajorLineWidth();
if (ImGui::InputFloat(
"Major Line Width", &majorLineWidth, 1.f, 2.f, "%.0f"))
"Major Line Width", &majorLineWidth, 1.f, 2.f, "%.0f"))
{
mGrid->setMajorLineWidth(majorLineWidth);
}
Expand All @@ -369,7 +394,7 @@ class PointCloudWidget : public dart::gui::osg::ImGuiWidget
static float minorLineWidth;
minorLineWidth = mGrid->getMinorLineWidth();
if (ImGui::InputFloat(
"Minor Line Width", &minorLineWidth, 1.f, 2.f, "%.0f"))
"Minor Line Width", &minorLineWidth, 1.f, 2.f, "%.0f"))
{
mGrid->setMinorLineWidth(minorLineWidth);
}
Expand Down