CloudViewer: added EDL shading option

This commit is contained in:
matlabbe
2020-06-01 21:44:42 -04:00
parent d00717b068
commit bda18bc517
2 changed files with 111 additions and 32 deletions

View File

@@ -74,6 +74,12 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <vtkQuad.h>
#include <opencv/vtkImageMatSource.h>
#if VTK_MAJOR_VERSION >= 7
#include <vtkEDLShading.h>
#include <vtkRenderStepsPass.h>
#include <vtkOpenGLRenderer.h>
#endif
#ifdef RTABMAP_OCTOMAP
#include <rtabmap/core/OctoMap.h>
#endif
@@ -102,6 +108,7 @@ CloudViewer::CloudViewer(QWidget *parent, CloudViewerInteractorStyle * style) :
_aSetNormalsScale(0),
_aSetBackgroundColor(0),
_aSetRenderingRate(0),
_aSetEDLShading(0),
_aSetLighting(0),
_aSetFlatShading(0),
_aSetEdgeVisibility(0),
@@ -131,6 +138,7 @@ CloudViewer::CloudViewer(QWidget *parent, CloudViewerInteractorStyle * style) :
int argc = 0;
UASSERT(style!=0);
style->setCloudViewer(this);
style->SetAutoAdjustCameraClippingRange(true);
_visualizer = new pcl::visualization::PCLVisualizer(
argc,
0,
@@ -141,8 +149,10 @@ CloudViewer::CloudViewer(QWidget *parent, CloudViewerInteractorStyle * style) :
_visualizer->setShowFPS(false);
int viewport;
_visualizer->createViewPort (0,0,1.0, 1.0, viewport); // all 3d objects here
_visualizer->createViewPort (0,0,1.0, 1.0, viewport); // text overlay
// Layer 0: unavailable layer, used as "all" by PCLVisualizer
_visualizer->createViewPort (0,0,1.0, 1.0, viewport); // Layer 1: all clouds here
_visualizer->createViewPort (0,0,1.0, 1.0, viewport); // Layer 2: all 3d objects here
_visualizer->createViewPort (0,0,1.0, 1.0, viewport); // Layer 3: text overlay
_visualizer->getRendererCollection()->InitTraversal ();
vtkRenderer* renderer = NULL;
int i =0;
@@ -151,11 +161,18 @@ CloudViewer::CloudViewer(QWidget *parent, CloudViewerInteractorStyle * style) :
renderer->SetLayer(i);
if(i==1)
{
renderer->PreserveColorBufferOff();
renderer->PreserveDepthBufferOff();
_visualizer->getInteractorStyle()->SetDefaultRenderer(renderer);
}
else if(i==2)
{
renderer->PreserveColorBufferOn();
renderer->PreserveDepthBufferOn();
}
++i;
}
_visualizer->getRenderWindow()->SetNumberOfLayers(3);
_visualizer->getRenderWindow()->SetNumberOfLayers(4);
this->SetRenderWindow(_visualizer->getRenderWindow());
@@ -262,9 +279,17 @@ void CloudViewer::createMenu()
_aSetIntensityMaximum = new QAction("Set maximum absolute intensity...", this);
_aSetBackgroundColor = new QAction("Set background color...", this);
_aSetRenderingRate = new QAction("Set rendering rate...", this);
_aSetEDLShading = new QAction("Eye-Dome Lighting Shading", this);
_aSetEDLShading->setCheckable(true);
_aSetEDLShading->setChecked(false);
_aSetLighting = new QAction("Lighting", this);
_aSetLighting->setCheckable(true);
_aSetLighting->setChecked(false);
#if VTK_MAJOR_VERSION < 7
_aSetLighting->setEnabled(false);
#endif
_aSetFlatShading = new QAction("Flat Shading", this);
_aSetFlatShading->setCheckable(true);
_aSetFlatShading->setChecked(false);
@@ -326,6 +351,7 @@ void CloudViewer::createMenu()
_menu->addMenu(scanMenu);
_menu->addAction(_aSetBackgroundColor);
_menu->addAction(_aSetRenderingRate);
_menu->addAction(_aSetEDLShading);
_menu->addAction(_aSetLighting);
_menu->addAction(_aSetFlatShading);
_menu->addAction(_aSetEdgeVisibility);
@@ -619,7 +645,8 @@ bool CloudViewer::addCloud(
bool rgb,
bool hasNormals,
bool hasIntensity,
const QColor & color)
const QColor & color,
int viewport)
{
int previousColorIndex = -1;
if(_addedClouds.contains(id))
@@ -636,7 +663,7 @@ bool CloudViewer::addCloud(
pcl::PointCloud<pcl::PointNormal>::Ptr cloud_xyz (new pcl::PointCloud<pcl::PointNormal>);
pcl::fromPCLPointCloud2 (*binaryCloud, *cloud_xyz);
std::string idNormals = id + "-normals";
if(_visualizer->addPointCloudNormals<pcl::PointNormal>(cloud_xyz, _normalsStep, _normalsScale, idNormals, 0))
if(_visualizer->addPointCloudNormals<pcl::PointNormal>(cloud_xyz, _normalsStep, _normalsScale, idNormals, viewport))
{
_visualizer->updatePointCloudPose(idNormals, pose.toEigen3f());
_addedClouds.insert(idNormals, pose);
@@ -646,7 +673,7 @@ bool CloudViewer::addCloud(
// add random color channel
pcl::visualization::PointCloudColorHandler<pcl::PCLPointCloud2>::Ptr colorHandler;
colorHandler.reset (new pcl::visualization::PointCloudColorHandlerRandom<pcl::PCLPointCloud2> (binaryCloud));
if(_visualizer->addPointCloud (binaryCloud, colorHandler, origin, orientation, id, 1))
if(_visualizer->addPointCloud (binaryCloud, colorHandler, origin, orientation, id, viewport))
{
QColor c = Qt::gray;
if(color.isValid())
@@ -654,27 +681,27 @@ bool CloudViewer::addCloud(
c = color;
}
colorHandler.reset (new pcl::visualization::PointCloudColorHandlerCustom<pcl::PCLPointCloud2> (binaryCloud, c.red(), c.green(), c.blue()));
_visualizer->addPointCloud (binaryCloud, colorHandler, origin, orientation, id, 1);
_visualizer->addPointCloud (binaryCloud, colorHandler, origin, orientation, id, viewport);
// x,y,z
colorHandler.reset (new pcl::visualization::PointCloudColorHandlerGenericField<pcl::PCLPointCloud2> (binaryCloud, "x"));
_visualizer->addPointCloud (binaryCloud, colorHandler, origin, orientation, id, 1);
_visualizer->addPointCloud (binaryCloud, colorHandler, origin, orientation, id, viewport);
colorHandler.reset (new pcl::visualization::PointCloudColorHandlerGenericField<pcl::PCLPointCloud2> (binaryCloud, "y"));
_visualizer->addPointCloud (binaryCloud, colorHandler, origin, orientation, id, 1);
_visualizer->addPointCloud (binaryCloud, colorHandler, origin, orientation, id, viewport);
colorHandler.reset (new pcl::visualization::PointCloudColorHandlerGenericField<pcl::PCLPointCloud2> (binaryCloud, "z"));
_visualizer->addPointCloud (binaryCloud, colorHandler, origin, orientation, id, 1);
_visualizer->addPointCloud (binaryCloud, colorHandler, origin, orientation, id, viewport);
if(rgb)
{
//rgb
colorHandler.reset(new pcl::visualization::PointCloudColorHandlerRGBField<pcl::PCLPointCloud2>(binaryCloud));
_visualizer->addPointCloud (binaryCloud, colorHandler, origin, orientation, id, 1);
_visualizer->addPointCloud (binaryCloud, colorHandler, origin, orientation, id, viewport);
}
else if(hasIntensity)
{
//intensity
colorHandler.reset(new PointCloudColorHandlerIntensityField(binaryCloud, _intensityAbsMax, _aSetIntensityRedColormap->isChecked()));
_visualizer->addPointCloud (binaryCloud, colorHandler, origin, orientation, id, 1);
_visualizer->addPointCloud (binaryCloud, colorHandler, origin, orientation, id, viewport);
}
else if(previousColorIndex == 5)
{
@@ -685,11 +712,11 @@ bool CloudViewer::addCloud(
{
//normals
colorHandler.reset (new pcl::visualization::PointCloudColorHandlerGenericField<pcl::PCLPointCloud2> (binaryCloud, "normal_x"));
_visualizer->addPointCloud (binaryCloud, colorHandler, origin, orientation, id, 1);
_visualizer->addPointCloud (binaryCloud, colorHandler, origin, orientation, id, viewport);
colorHandler.reset (new pcl::visualization::PointCloudColorHandlerGenericField<pcl::PCLPointCloud2> (binaryCloud, "normal_y"));
_visualizer->addPointCloud (binaryCloud, colorHandler, origin, orientation, id, 1);
_visualizer->addPointCloud (binaryCloud, colorHandler, origin, orientation, id, viewport);
colorHandler.reset (new pcl::visualization::PointCloudColorHandlerGenericField<pcl::PCLPointCloud2> (binaryCloud, "normal_z"));
_visualizer->addPointCloud (binaryCloud, colorHandler, origin, orientation, id, 1);
_visualizer->addPointCloud (binaryCloud, colorHandler, origin, orientation, id, viewport);
}
else if(previousColorIndex > 5)
{
@@ -1028,11 +1055,11 @@ bool CloudViewer::addOctomap(const OctoMap * octomap, unsigned int treeDepth, bo
vtkSmartPointer<vtkGlyph3DMapper> mapper = vtkSmartPointer<vtkGlyph3DMapper>::New();
mapper->SetSourceConnection(cubeSource->GetOutputPort());
#if VTK_MAJOR_VERSION <= 5
#if VTK_MAJOR_VERSION <= 5
mapper->SetInputConnection(polydata->GetProducerPort());
#else
#else
mapper->SetInputData(polydata);
#endif
#endif
mapper->SetScalarRange(0, obstacles->size() - 1);
mapper->SetLookupTable(lut);
mapper->ScalingOff();
@@ -1444,7 +1471,7 @@ void CloudViewer::addOrUpdateCoordinate(
{
_coordinates.insert(id);
#if PCL_VERSION_COMPARE(>=, 1, 7, 2)
_visualizer->addCoordinateSystem(scale, transform.toEigen3f(), id, foreground?2:1);
_visualizer->addCoordinateSystem(scale, transform.toEigen3f(), id, foreground?3:2);
#else
// Well, on older versions, just update the main coordinate
_visualizer->addCoordinateSystem(scale, transform.toEigen3f(), 0);
@@ -1532,11 +1559,11 @@ void CloudViewer::addOrUpdateLine(
if(arrow)
{
_visualizer->addArrow(pt2, pt1, c.redF(), c.greenF(), c.blueF(), false, id, foreground?2:1);
_visualizer->addArrow(pt2, pt1, c.redF(), c.greenF(), c.blueF(), false, id, foreground?3:2);
}
else
{
_visualizer->addLine(pt2, pt1, c.redF(), c.greenF(), c.blueF(), id, foreground?2:1);
_visualizer->addLine(pt2, pt1, c.redF(), c.greenF(), c.blueF(), id, foreground?3:2);
}
_visualizer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_OPACITY, c.alphaF(), id);
}
@@ -1593,7 +1620,7 @@ void CloudViewer::addOrUpdateSphere(
}
pcl::PointXYZ center(pose.x(), pose.y(), pose.z());
_visualizer->addSphere(center, radius, c.redF(), c.greenF(), c.blueF(), id, foreground?2:1);
_visualizer->addSphere(center, radius, c.redF(), c.greenF(), c.blueF(), id, foreground?3:2);
_visualizer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_OPACITY, c.alphaF(), id);
}
}
@@ -1650,7 +1677,7 @@ void CloudViewer::addOrUpdateCube(
{
c = color;
}
_visualizer->addCube(Eigen::Vector3f(pose.x(), pose.y(), pose.z()), pose.getQuaternionf(), width, height, depth, id, foreground?2:1);
_visualizer->addCube(Eigen::Vector3f(pose.x(), pose.y(), pose.z()), pose.getQuaternionf(), width, height, depth, id, foreground?3:2);
if(wireframe)
{
_visualizer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_REPRESENTATION, pcl::visualization::PCL_VISUALIZER_REPRESENTATION_WIREFRAME, id);
@@ -1780,7 +1807,7 @@ void CloudViewer::addOrUpdateQuad(
int i = 0;
while ((renderer = _visualizer->getRendererCollection()->GetNextItem ()) != NULL)
{
if ((foreground?2:1) == i) // add the actor only to the specified viewport
if ((foreground?3:2) == i) // add the actor only to the specified viewport
{
renderer->AddActor (actor);
}
@@ -1899,7 +1926,7 @@ void CloudViewer::addOrUpdateFrustum(
}
pcl::toPCLPointCloud2(frustumPoints, mesh.cloud);
mesh.polygons.push_back(vertices);
_visualizer->addPolylineFromPolygonMesh(mesh, id, 1);
_visualizer->addPolylineFromPolygonMesh(mesh, id, 2);
_visualizer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, c.redF(), c.greenF(), c.blueF(), id);
_visualizer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_OPACITY, c.alphaF(), id);
}
@@ -2013,11 +2040,13 @@ void CloudViewer::addOrUpdateGraph(
}
pcl::toPCLPointCloud2(*graph, mesh.cloud);
mesh.polygons.push_back(vertices);
_visualizer->addPolylineFromPolygonMesh(mesh, id, 1);
_visualizer->addPolylineFromPolygonMesh(mesh, id, 2);
_visualizer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, color.redF(), color.greenF(), color.blueF(), id);
_visualizer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_OPACITY, color.alphaF(), id);
this->addCloud(id+"_nodes", graph, Transform::getIdentity(), color);
pcl::PCLPointCloud2Ptr binaryCloud(new pcl::PCLPointCloud2);
pcl::toPCLPointCloud2(*graph, *binaryCloud);
this->addCloud(id+"_nodes", binaryCloud, Transform::getIdentity(), false, false, false, color, 2);
this->setCloudPointSize(id+"_nodes", 5);
}
}
@@ -2075,7 +2104,7 @@ void CloudViewer::addOrUpdateText(
color.greenF(),
color.blueF(),
id,
foreground?2:1);
foreground?3:2);
}
}
@@ -2443,6 +2472,41 @@ void CloudViewer::setRenderingRate(double rate)
_visualizer->getInteractorStyle()->GetInteractor()->SetDesiredUpdateRate(_renderingRate);
}
void CloudViewer::setEDLShading(bool on)
{
#if VTK_MAJOR_VERSION >= 7
_aSetEDLShading->setChecked(on);
_visualizer->getRendererCollection()->InitTraversal ();
vtkRenderer* renderer = NULL;
renderer = _visualizer->getRendererCollection()->GetNextItem ();
renderer = _visualizer->getRendererCollection()->GetNextItem (); // Get Layer 1
UASSERT(renderer);
vtkOpenGLRenderer* glrenderer = vtkOpenGLRenderer::SafeDownCast(renderer);
UASSERT(glrenderer);
if(on)
{
// EDL shader
vtkSmartPointer<vtkRenderStepsPass> basicPasses = vtkSmartPointer<vtkRenderStepsPass>::New ();
vtkSmartPointer<vtkEDLShading> edl = vtkSmartPointer<vtkEDLShading>::New ();
edl->SetDelegatePass(basicPasses);
glrenderer->SetPass(edl);
}
else if(glrenderer->GetPass())
{
glrenderer->GetPass()->ReleaseGraphicsResources(NULL);
glrenderer->SetPass(NULL);
}
this->update();
#else
if(on)
{
UERROR("RTAB-Map must be built with VTK>=7 to enable EDL shading!");
}
#endif
}
void CloudViewer::setLighting(bool on)
{
_aSetLighting->setChecked(on);
@@ -2563,7 +2627,7 @@ void CloudViewer::updateCameraTargetPosition(const Transform & pose)
}
pcl::toPCLPointCloud2(*_trajectory, mesh.cloud);
mesh.polygons.push_back(vertices);
_visualizer->addPolylineFromPolygonMesh(mesh, "trajectory", 1);
_visualizer->addPolylineFromPolygonMesh(mesh, "trajectory", 2);
}
if(pose != _lastPose || _lastPose.isNull())
@@ -2850,6 +2914,10 @@ bool CloudViewer::isPolygonPicking() const
{
return _aPolygonPicking->isChecked();
}
bool CloudViewer::isEDLShadingOn() const
{
return _aSetEDLShading->isChecked();
}
bool CloudViewer::isLightingOn() const
{
return _aSetLighting->isChecked();
@@ -2940,14 +3008,17 @@ void CloudViewer::addGrid()
{
//over x
name = uFormat("line%d", ++id);
_visualizer->addLine(pcl::PointXYZ(i, min, 0.0f), pcl::PointXYZ(i, max, 0.0f), r, g, b, name, 1);
_visualizer->addLine(
pcl::PointXYZ(i, min, 0.0f),
pcl::PointXYZ(i, max, 0.0f),
r, g, b, name, 2);
_gridLines.push_back(name);
//over y or z
name = uFormat("line%d", ++id);
_visualizer->addLine(
pcl::PointXYZ(min, i, 0),
pcl::PointXYZ(max, i, 0),
r, g, b, name, 1);
r, g, b, name, 2);
_gridLines.push_back(name);
}
}
@@ -3417,6 +3488,10 @@ void CloudViewer::handleAction(QAction * a)
{
this->setCameraOrtho(_aCameraOrtho->isChecked());
}
else if(a == _aSetEDLShading)
{
this->setEDLShading(_aSetEDLShading->isChecked());
}
else if(a == _aSetLighting)
{
this->setLighting(_aSetLighting->isChecked());