31 #include <rtabmap/core/Version.h>
39 #include <pcl/visualization/pcl_visualizer.h>
40 #include <pcl/common/transforms.h>
43 #include <QActionGroup>
44 #include <QtGui/QContextMenuEvent>
45 #include <QInputDialog>
46 #include <QtGui/QWheelEvent>
47 #include <QtGui/QKeyEvent>
48 #include <QColorDialog>
49 #include <QtGui/QVector3D>
50 #include <QMainWindow>
53 #include <vtkCamera.h>
54 #include <vtkRenderWindow.h>
55 #include <vtkCubeSource.h>
56 #include <vtkDataSetMapper.h>
57 #include <vtkDelaunay2D.h>
58 #include <vtkGlyph3D.h>
59 #include <vtkGlyph3DMapper.h>
60 #include <vtkSmartVolumeMapper.h>
61 #include <vtkVolumeProperty.h>
62 #include <vtkColorTransferFunction.h>
63 #include <vtkPiecewiseFunction.h>
64 #include <vtkImageData.h>
65 #include <vtkLookupTable.h>
66 #include <vtkTextureUnitManager.h>
67 #include <vtkJPEGReader.h>
68 #include <vtkBMPReader.h>
69 #include <vtkPNMReader.h>
70 #include <vtkPNGReader.h>
71 #include <vtkTIFFReader.h>
72 #include <vtkElevationFilter.h>
73 #include <vtkOpenGLRenderWindow.h>
74 #include <vtkPointPicker.h>
75 #include <vtkPointData.h>
76 #include <vtkTextActor.h>
77 #include <vtkTexture.h>
78 #include <vtkNamedColors.h>
79 #include <vtkOBBTree.h>
80 #include <vtkObjectFactory.h>
82 #include <vtkWarpScalar.h>
83 #include <vtkUnsignedCharArray.h>
86 #if VTK_MAJOR_VERSION >= 7
87 #include <vtkEDLShading.h>
88 #include <vtkRenderStepsPass.h>
89 #include <vtkOpenGLRenderer.h>
92 #if VTK_MAJOR_VERSION >= 8
93 #include <vtkGenericOpenGLRenderWindow.h>
96 #ifdef RTABMAP_OCTOMAP
101 #ifdef vtkGenericDataArray_h
102 #define InsertNextTupleValue InsertNextTypedTuple
115 _aSetTrajectorySize(0),
116 _aClearTrajectory(0),
119 _aSetFrustumScale(0),
120 _aSetFrustumColor(0),
122 _aSetGridCellCount(0),
123 _aSetGridCellSize(0),
126 _aSetNormalsScale(0),
127 _aSetBackgroundColor(0),
128 _aSetRenderingRate(0),
132 _aSetEdgeVisibility(0),
133 _aSetScalarVisibility(0),
134 _aBackfaceCulling(0),
136 _trajectory(new
pcl::PointCloud<
pcl::PointXYZ>),
137 _maxTrajectorySize(100),
139 _frustumColor(Qt::gray),
144 _buildLocator(
false),
145 _lastCameraOrientation(0,0,0),
146 _lastCameraPose(0,0,0),
147 _defaultBgColor(Qt::black),
148 _currentBgColor(Qt::black),
149 _frontfaceCulling(
false),
152 _intensityAbsMax(100.0
f),
153 _coordinateFrameScale(1.0)
155 this->setMinimumSize(200, 200);
160 style->AutoAdjustCameraClippingRangeOff();
161 #if VTK_MAJOR_VERSION > 8
164 renderWindow1->AddRenderer(renderer1);
165 _visualizer =
new pcl::visualization::PCLVisualizer(
174 _visualizer =
new pcl::visualization::PCLVisualizer(
186 _visualizer->createViewPort (0,0,1.0, 1.0, viewport);
187 _visualizer->createViewPort (0,0,1.0, 1.0, viewport);
188 _visualizer->createViewPort (0,0,1.0, 1.0, viewport);
189 _visualizer->getRendererCollection()->InitTraversal ();
190 vtkRenderer* renderer =
NULL;
192 while ((renderer =
_visualizer->getRendererCollection()->GetNextItem ()) !=
NULL)
194 renderer->SetLayer(
i);
197 #if VTK_MAJOR_VERSION >= 7
198 renderer->PreserveColorBufferOff();
200 renderer->PreserveDepthBufferOff();
201 _visualizer->getInteractorStyle()->SetDefaultRenderer(renderer);
205 #if VTK_MAJOR_VERSION >= 7
206 renderer->PreserveColorBufferOn();
208 renderer->PreserveDepthBufferOn();
212 _visualizer->getRenderWindow()->SetNumberOfLayers(4);
214 #ifdef VTK_GLOBAL_WARNING_DISPLAY_OFF
215 _visualizer->getRenderWindow()->GlobalWarningDisplayOff();
218 #if VTK_MAJOR_VERSION > 8
219 this->setRenderWindow(
_visualizer->getRenderWindow());
221 this->SetRenderWindow(
_visualizer->getRenderWindow());
226 #if VTK_MAJOR_VERSION > 8
228 this->interactor()->SetInteractorStyle (
_visualizer->getInteractorStyle());
231 this->GetInteractor()->SetInteractorStyle (
_visualizer->getInteractorStyle());
235 UDEBUG(
"pick tolerance=%f", pp->GetTolerance());
236 pp->SetTolerance (pp->GetTolerance()/2.0);
237 #if VTK_MAJOR_VERSION > 8
238 this->interactor()->SetPicker (pp);
240 this->GetInteractor()->SetPicker (pp);
257 setMouseTracking(
false);
300 QAction * freeCamera =
new QAction(
"Free",
this);
301 freeCamera->setCheckable(
true);
302 freeCamera->setChecked(
false);
345 #if VTK_MAJOR_VERSION < 7
367 QMenu * cameraMenu =
new QMenu(
"Camera",
this);
370 cameraMenu->addAction(freeCamera);
371 cameraMenu->addSeparator();
375 QActionGroup * group =
new QActionGroup(
this);
378 group->addAction(freeCamera);
380 QMenu * trajectoryMenu =
new QMenu(
"Trajectory",
this);
385 QMenu * frustumMenu =
new QMenu(
"Frustum",
this);
390 QMenu * gridMenu =
new QMenu(
"Grid",
this);
395 QMenu * normalsMenu =
new QMenu(
"Normals",
this);
400 QMenu * scanMenu =
new QMenu(
"Scan color",
this);
406 _menu =
new QMenu(
this);
407 _menu->addMenu(cameraMenu);
408 _menu->addMenu(trajectoryMenu);
411 _menu->addMenu(frustumMenu);
412 _menu->addMenu(gridMenu);
413 _menu->addMenu(normalsMenu);
414 _menu->addMenu(scanMenu);
430 settings.beginGroup(group);
433 float poseX, poseY, poseZ, focalX, focalY, focalZ, upX, upY, upZ;
434 this->
getCameraPosition(poseX, poseY, poseZ, focalX, focalY, focalZ, upX, upY, upZ);
435 QVector3D pose(poseX, poseY, poseZ);
436 QVector3D focal(focalX, focalY, focalZ);
449 pose = QVector3D(newPose.
x(), newPose.
y(), newPose.
z());
450 focal = QVector3D(newFocal.
x(), newFocal.
y(), newFocal.
z());
452 settings.setValue(
"camera_pose", pose);
453 settings.setValue(
"camera_focal", focal);
454 settings.setValue(
"camera_up", QVector3D(upX, upY, upZ));
495 settings.beginGroup(group);
498 float poseX, poseY, poseZ, focalX, focalY, focalZ, upX, upY, upZ;
499 this->
getCameraPosition(poseX, poseY, poseZ, focalX, focalY, focalZ, upX, upY, upZ);
500 QVector3D pose(poseX, poseY, poseZ), focal(focalX, focalY, focalZ), up(upX, upY, upZ);
501 pose = settings.value(
"camera_pose", pose).value<QVector3D>();
502 focal = settings.value(
"camera_focal", focal).value<QVector3D>();
503 up = settings.value(
"camera_up", up).value<QVector3D>();
505 this->
setCameraPosition(pose.x(),pose.y(),pose.z(), focal.x(),focal.y(),focal.z(), up.x(),up.y(),up.z());
509 this->
setGridCellSize(settings.value(
"grid_cell_size",
this->getGridCellSize()).toFloat());
526 this->
setFrustumScale(settings.value(
"frustum_scale",
this->getFrustumScale()).toDouble());
527 this->
setFrustumColor(settings.value(
"frustum_color",
this->getFrustumColor()).value<QColor>());
531 if(settings.value(
"camera_free",
this->isCameraFree()).toBool())
551 #if VTK_MAJOR_VERSION > 8
552 this->renderWindow()->Render();
559 const std::string &
id,
572 #if VTK_MAJOR_VERSION >= 7
576 pcl::visualization::ShapeActorMap::iterator am_it =
_visualizer->getShapeActorMap()->find (
id);
578 if (am_it !=
_visualizer->getShapeActorMap()->end ())
580 actor = vtkActor::SafeDownCast (am_it->second);
585 actor->SetUserMatrix (
matrix);
596 std::string idNormals =
id+
"-normals";
599 _visualizer->updatePointCloudPose(idNormals, posef);
611 typedef pcl::visualization::PointCloudColorHandler<pcl::PCLPointCloud2>::PointCloud
PointCloud;
621 pcl::visualization::PointCloudColorHandler<
pcl::PCLPointCloud2>::PointCloudColorHandler (cloud),
625 field_idx_ = pcl::getFieldIndex (*cloud,
"intensity");
626 if (field_idx_ != -1)
640 #if PCL_VERSION_COMPARE(>, 1, 11, 1)
643 if (!capable_ || !cloud_)
647 if (!capable_ || !cloud_)
652 scalars->SetNumberOfComponents (3);
654 vtkIdType nr_points = cloud_->width * cloud_->height;
656 float * intensities =
new float[nr_points];
658 size_t point_offset = cloud_->fields[field_idx_].offset;
662 int x_idx = pcl::getFieldIndex (*cloud_,
"x");
665 float x_data, y_data, z_data;
666 size_t x_point_offset = cloud_->fields[x_idx].offset;
669 for (vtkIdType cp = 0; cp < nr_points; ++cp,
670 point_offset += cloud_->point_step,
671 x_point_offset += cloud_->point_step)
674 memcpy (&intensity, &cloud_->data[point_offset], sizeof (
float));
676 memcpy (&x_data, &cloud_->data[x_point_offset], sizeof (
float));
677 memcpy (&y_data, &cloud_->data[x_point_offset + sizeof (
float)],
sizeof (
float));
678 memcpy (&z_data, &cloud_->data[x_point_offset + 2 * sizeof (
float)],
sizeof (
float));
683 intensities[
j++] = intensity;
690 for (vtkIdType cp = 0; cp < nr_points; ++cp, point_offset += cloud_->point_step)
693 memcpy (&intensity, &cloud_->data[point_offset], sizeof (
float));
695 intensities[
j++] = intensity;
701 unsigned char* colors =
new unsigned char[
j * 3];
711 for(
size_t k=0; k<
j; ++k)
713 colors[k*3+0] = colors[k*3+1] = colors[k*3+2] =
max>0?(
unsigned char)(
std::min(intensities[k]/
max*255.0
f, 255.0
f)):255;
723 colors[k*3+0] = r*255.0f;
724 colors[k*3+1] =
g*255.0f;
725 colors[k*3+2] =
b*255.0f;
728 reinterpret_cast<vtkUnsignedCharArray*
>(&(*scalars))->SetNumberOfTuples (
j);
729 reinterpret_cast<vtkUnsignedCharArray*
>(&(*scalars))->SetArray (colors,
j*3, 0, vtkUnsignedCharArray::VTK_DATA_ARRAY_DELETE);
732 reinterpret_cast<vtkUnsignedCharArray*
>(&(*scalars))->SetNumberOfTuples (0);
734 delete [] intensities;
735 #if PCL_VERSION_COMPARE(>, 1, 11, 1)
745 getName ()
const {
return (
"PointCloudColorHandlerIntensityField"); }
757 const std::string &
id,
758 const pcl::PCLPointCloud2Ptr & binaryCloud,
763 const QColor & color,
766 int previousColorIndex = -1;
767 if(_addedClouds.contains(
id))
769 previousColorIndex = _visualizer->getColorHandlerIndex(
id);
770 this->removeCloud(
id);
773 Eigen::Vector4f
origin(pose.
x(), pose.
y(), pose.
z(), 0.0f);
776 if(hasNormals && _aShowNormals->isChecked())
778 pcl::PointCloud<pcl::PointNormal>::Ptr cloud_xyz (
new pcl::PointCloud<pcl::PointNormal>);
779 pcl::fromPCLPointCloud2 (*binaryCloud, *cloud_xyz);
780 std::string idNormals =
id +
"-normals";
781 if(_visualizer->addPointCloudNormals<pcl::PointNormal>(cloud_xyz, _normalsStep, _normalsScale, idNormals, viewport))
783 _visualizer->updatePointCloudPose(idNormals, pose.
toEigen3f());
784 _addedClouds.insert(idNormals, pose);
789 pcl::visualization::PointCloudColorHandler<pcl::PCLPointCloud2>::Ptr colorHandler;
790 colorHandler.reset (
new pcl::visualization::PointCloudColorHandlerRandom<pcl::PCLPointCloud2> (binaryCloud));
791 if(_visualizer->addPointCloud (binaryCloud, colorHandler,
origin,
orientation,
id, viewport))
798 colorHandler.reset (
new pcl::visualization::PointCloudColorHandlerCustom<pcl::PCLPointCloud2> (binaryCloud,
c.red(),
c.green(),
c.blue()));
799 _visualizer->addPointCloud (binaryCloud, colorHandler,
origin,
orientation,
id, viewport);
802 colorHandler.reset (
new pcl::visualization::PointCloudColorHandlerGenericField<pcl::PCLPointCloud2> (binaryCloud,
"x"));
803 _visualizer->addPointCloud (binaryCloud, colorHandler,
origin,
orientation,
id, viewport);
804 colorHandler.reset (
new pcl::visualization::PointCloudColorHandlerGenericField<pcl::PCLPointCloud2> (binaryCloud,
"y"));
805 _visualizer->addPointCloud (binaryCloud, colorHandler,
origin,
orientation,
id, viewport);
806 colorHandler.reset (
new pcl::visualization::PointCloudColorHandlerGenericField<pcl::PCLPointCloud2> (binaryCloud,
"z"));
807 _visualizer->addPointCloud (binaryCloud, colorHandler,
origin,
orientation,
id, viewport);
812 colorHandler.reset(
new pcl::visualization::PointCloudColorHandlerRGBField<pcl::PCLPointCloud2>(binaryCloud));
813 _visualizer->addPointCloud (binaryCloud, colorHandler,
origin,
orientation,
id, viewport);
815 else if(hasIntensity)
818 colorHandler.reset(
new PointCloudColorHandlerIntensityField(binaryCloud, _intensityAbsMax, _aSetIntensityRedColormap->isChecked()?1:_aSetIntensityRainbowColormap->isChecked()?2:0));
819 _visualizer->addPointCloud (binaryCloud, colorHandler,
origin,
orientation,
id, viewport);
821 else if(previousColorIndex == 5)
823 previousColorIndex = -1;
829 colorHandler.reset (
new pcl::visualization::PointCloudColorHandlerGenericField<pcl::PCLPointCloud2> (binaryCloud,
"normal_x"));
830 _visualizer->addPointCloud (binaryCloud, colorHandler,
origin,
orientation,
id, viewport);
831 colorHandler.reset (
new pcl::visualization::PointCloudColorHandlerGenericField<pcl::PCLPointCloud2> (binaryCloud,
"normal_y"));
832 _visualizer->addPointCloud (binaryCloud, colorHandler,
origin,
orientation,
id, viewport);
833 colorHandler.reset (
new pcl::visualization::PointCloudColorHandlerGenericField<pcl::PCLPointCloud2> (binaryCloud,
"normal_z"));
834 _visualizer->addPointCloud (binaryCloud, colorHandler,
origin,
orientation,
id, viewport);
836 else if(previousColorIndex > 5)
838 previousColorIndex = -1;
841 if(previousColorIndex>=0)
843 _visualizer->updateColorHandlerIndex(
id, previousColorIndex);
847 _visualizer->updateColorHandlerIndex(
id, 5);
851 _visualizer->updateColorHandlerIndex(
id, hasIntensity?8:7);
853 else if(hasIntensity)
855 _visualizer->updateColorHandlerIndex(
id, 5);
857 else if(color.isValid())
859 _visualizer->updateColorHandlerIndex(
id, 1);
862 _addedClouds.insert(
id, pose);
869 const std::string &
id,
870 const pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr & cloud,
872 const QColor & color)
874 pcl::PCLPointCloud2Ptr binaryCloud(
new pcl::PCLPointCloud2);
875 pcl::toPCLPointCloud2(*cloud, *binaryCloud);
876 return addCloud(
id, binaryCloud, pose,
true,
true,
false, color);
880 const std::string &
id,
881 const pcl::PointCloud<pcl::PointXYZRGB>::Ptr & cloud,
883 const QColor & color)
885 pcl::PCLPointCloud2Ptr binaryCloud(
new pcl::PCLPointCloud2);
886 pcl::toPCLPointCloud2(*cloud, *binaryCloud);
887 return addCloud(
id, binaryCloud, pose,
true,
false,
false, color);
891 const std::string &
id,
892 const pcl::PointCloud<pcl::PointXYZINormal>::Ptr & cloud,
894 const QColor & color)
896 pcl::PCLPointCloud2Ptr binaryCloud(
new pcl::PCLPointCloud2);
897 pcl::toPCLPointCloud2(*cloud, *binaryCloud);
898 return addCloud(
id, binaryCloud, pose,
false,
true,
true, color);
902 const std::string &
id,
903 const pcl::PointCloud<pcl::PointXYZI>::Ptr & cloud,
905 const QColor & color)
907 pcl::PCLPointCloud2Ptr binaryCloud(
new pcl::PCLPointCloud2);
908 pcl::toPCLPointCloud2(*cloud, *binaryCloud);
909 return addCloud(
id, binaryCloud, pose,
false,
false,
true, color);
913 const std::string &
id,
914 const pcl::PointCloud<pcl::PointNormal>::Ptr & cloud,
916 const QColor & color)
918 pcl::PCLPointCloud2Ptr binaryCloud(
new pcl::PCLPointCloud2);
919 pcl::toPCLPointCloud2(*cloud, *binaryCloud);
920 return addCloud(
id, binaryCloud, pose,
false,
true,
false, color);
924 const std::string &
id,
925 const pcl::PointCloud<pcl::PointXYZ>::Ptr & cloud,
927 const QColor & color)
929 pcl::PCLPointCloud2Ptr binaryCloud(
new pcl::PCLPointCloud2);
930 pcl::toPCLPointCloud2(*cloud, *binaryCloud);
931 return addCloud(
id, binaryCloud, pose,
false,
false,
false, color);
935 const std::string &
id,
936 const pcl::PointCloud<pcl::PointXYZ>::Ptr & cloud,
937 const std::vector<pcl::Vertices> & polygons,
940 if(_addedClouds.contains(
id))
942 this->removeCloud(
id);
945 UDEBUG(
"Adding %s with %d points and %d polygons",
id.
c_str(), (
int)cloud->size(), (
int)polygons.size());
946 if(_visualizer->addPolygonMesh<pcl::PointXYZ>(cloud, polygons,
id, 1))
948 #if VTK_MAJOR_VERSION >= 7
949 _visualizer->getCloudActorMap()->find(
id)->second.actor->GetProperty()->SetAmbient(0.1);
951 _visualizer->getCloudActorMap()->find(
id)->second.actor->GetProperty()->SetAmbient(0.5);
953 _visualizer->getCloudActorMap()->find(
id)->second.actor->GetProperty()->SetLighting(_aSetLighting->isChecked());
954 _visualizer->getCloudActorMap()->find(
id)->second.actor->GetProperty()->SetInterpolation(_aSetFlatShading->isChecked()?VTK_FLAT:VTK_PHONG);
955 _visualizer->getCloudActorMap()->find(
id)->second.actor->GetProperty()->SetEdgeVisibility(_aSetEdgeVisibility->isChecked());
956 _visualizer->getCloudActorMap()->find(
id)->second.actor->GetProperty()->SetBackfaceCulling(_aBackfaceCulling->isChecked());
957 _visualizer->getCloudActorMap()->find(
id)->second.actor->GetProperty()->SetFrontfaceCulling(_frontfaceCulling);
958 _visualizer->updatePointCloudPose(
id, pose.
toEigen3f());
962 tree->SetDataSet(_visualizer->getCloudActorMap()->find(
id)->second.actor->GetMapper()->GetInput());
963 tree->BuildLocator();
964 _locators.insert(std::make_pair(
id,
tree));
966 _addedClouds.insert(
id, pose);
973 const std::string &
id,
974 const pcl::PointCloud<pcl::PointXYZRGB>::Ptr & cloud,
975 const std::vector<pcl::Vertices> & polygons,
978 if(_addedClouds.contains(
id))
980 this->removeCloud(
id);
983 UDEBUG(
"Adding %s with %d points and %d polygons",
id.
c_str(), (
int)cloud->size(), (
int)polygons.size());
984 if(_visualizer->addPolygonMesh<pcl::PointXYZRGB>(cloud, polygons,
id, 1))
986 #if VTK_MAJOR_VERSION >= 7
987 _visualizer->getCloudActorMap()->find(
id)->second.actor->GetProperty()->SetAmbient(0.1);
989 _visualizer->getCloudActorMap()->find(
id)->second.actor->GetProperty()->SetAmbient(0.5);
991 _visualizer->getCloudActorMap()->find(
id)->second.actor->GetProperty()->SetLighting(_aSetLighting->isChecked());
992 _visualizer->getCloudActorMap()->find(
id)->second.actor->GetProperty()->SetInterpolation(_aSetFlatShading->isChecked()?VTK_FLAT:VTK_PHONG);
993 _visualizer->getCloudActorMap()->find(
id)->second.actor->GetProperty()->SetEdgeVisibility(_aSetEdgeVisibility->isChecked());
994 _visualizer->getCloudActorMap()->find(
id)->second.actor->GetProperty()->SetBackfaceCulling(_aBackfaceCulling->isChecked());
995 _visualizer->getCloudActorMap()->find(
id)->second.actor->GetProperty()->SetFrontfaceCulling(_frontfaceCulling);
996 _visualizer->updatePointCloudPose(
id, pose.
toEigen3f());
1000 tree->SetDataSet(_visualizer->getCloudActorMap()->find(
id)->second.actor->GetMapper()->GetInput());
1001 tree->BuildLocator();
1002 _locators.insert(std::make_pair(
id,
tree));
1004 _addedClouds.insert(
id, pose);
1011 const std::string &
id,
1012 const pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr & cloud,
1013 const std::vector<pcl::Vertices> & polygons,
1016 if(_addedClouds.contains(
id))
1018 this->removeCloud(
id);
1021 UDEBUG(
"Adding %s with %d points and %d polygons",
id.
c_str(), (
int)cloud->size(), (
int)polygons.size());
1022 if(_visualizer->addPolygonMesh<pcl::PointXYZRGBNormal>(cloud, polygons,
id, 1))
1024 #if VTK_MAJOR_VERSION >= 7
1025 _visualizer->getCloudActorMap()->find(
id)->second.actor->GetProperty()->SetAmbient(0.1);
1027 _visualizer->getCloudActorMap()->find(
id)->second.actor->GetProperty()->SetAmbient(0.5);
1029 _visualizer->getCloudActorMap()->find(
id)->second.actor->GetProperty()->SetLighting(_aSetLighting->isChecked());
1030 _visualizer->getCloudActorMap()->find(
id)->second.actor->GetProperty()->SetInterpolation(_aSetFlatShading->isChecked()?VTK_FLAT:VTK_PHONG);
1031 _visualizer->getCloudActorMap()->find(
id)->second.actor->GetProperty()->SetEdgeVisibility(_aSetEdgeVisibility->isChecked());
1032 _visualizer->getCloudActorMap()->find(
id)->second.actor->GetProperty()->SetBackfaceCulling(_aBackfaceCulling->isChecked());
1033 _visualizer->getCloudActorMap()->find(
id)->second.actor->GetProperty()->SetFrontfaceCulling(_frontfaceCulling);
1034 _visualizer->updatePointCloudPose(
id, pose.
toEigen3f());
1038 tree->SetDataSet(_visualizer->getCloudActorMap()->find(
id)->second.actor->GetMapper()->GetInput());
1039 tree->BuildLocator();
1040 _locators.insert(std::make_pair(
id,
tree));
1042 _addedClouds.insert(
id, pose);
1049 const std::string &
id,
1050 const pcl::PolygonMesh::Ptr & mesh,
1053 if(_addedClouds.contains(
id))
1055 this->removeCloud(
id);
1058 UDEBUG(
"Adding %s with %d polygons",
id.
c_str(), (
int)mesh->polygons.size());
1059 if(_visualizer->addPolygonMesh(*mesh,
id, 1))
1061 #if VTK_MAJOR_VERSION >= 7
1062 _visualizer->getCloudActorMap()->find(
id)->second.actor->GetProperty()->SetAmbient(0.1);
1064 _visualizer->getCloudActorMap()->find(
id)->second.actor->GetProperty()->SetAmbient(0.5);
1066 _visualizer->getCloudActorMap()->find(
id)->second.actor->GetProperty()->SetLighting(_aSetLighting->isChecked());
1067 _visualizer->getCloudActorMap()->find(
id)->second.actor->GetProperty()->SetEdgeVisibility(_aSetEdgeVisibility->isChecked());
1068 _visualizer->getCloudActorMap()->find(
id)->second.actor->GetProperty()->SetBackfaceCulling(_aBackfaceCulling->isChecked());
1069 _visualizer->getCloudActorMap()->find(
id)->second.actor->GetProperty()->SetFrontfaceCulling(_frontfaceCulling);
1070 _visualizer->updatePointCloudPose(
id, pose.
toEigen3f());
1074 tree->SetDataSet(_visualizer->getCloudActorMap()->find(
id)->second.actor->GetMapper()->GetInput());
1075 tree->BuildLocator();
1076 _locators.insert(std::make_pair(
id,
tree));
1078 _addedClouds.insert(
id, pose);
1086 const std::string &
id,
1087 const pcl::TextureMesh::Ptr & textureMesh,
1088 const cv::Mat & texture,
1091 if(_addedClouds.contains(
id))
1093 this->removeCloud(
id);
1097 if(this->addTextureMesh(*textureMesh, texture,
id, 1))
1099 #if VTK_MAJOR_VERSION >= 7
1100 vtkActor* actor = vtkActor::SafeDownCast (_visualizer->getShapeActorMap()->find(
id)->second);
1102 vtkActor* actor = vtkActor::SafeDownCast (_visualizer->getCloudActorMap()->find(
id)->second.actor);
1105 if(!textureMesh->cloud.is_dense)
1107 actor->GetTexture()->SetInterpolate(1);
1108 actor->GetTexture()->SetBlendingMode(vtkTexture::VTK_TEXTURE_BLENDING_MODE_REPLACE);
1113 tree->SetDataSet(actor->GetMapper()->GetInput());
1114 tree->BuildLocator();
1115 _locators.insert(std::make_pair(
id,
tree));
1118 this->updateCloudPose(
id, pose);
1127 #ifdef RTABMAP_OCTOMAP
1130 if(treeDepth == 0 || treeDepth > octomap->
octree()->getTreeDepth())
1134 UWARN(
"Tree depth requested (%d) is deeper than the "
1135 "actual maximum tree depth of %d. Using maximum depth.",
1136 (
int)treeDepth, (
int)octomap->
octree()->getTreeDepth());
1138 treeDepth = octomap->
octree()->getTreeDepth();
1143 if(!volumeRepresentation)
1145 pcl::IndicesPtr obstacles(
new std::vector<int>);
1146 pcl::IndicesPtr ground(
new std::vector<int>);
1148 pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud = octomap->
createCloud(
1149 treeDepth, obstacles.get(), 0, ground.get(),
false);
1150 obstacles->insert(obstacles->end(), ground->begin(), ground->end());
1151 if(obstacles->size())
1157 colors->SetName(
"colors");
1158 colors->SetNumberOfValues(obstacles->size());
1161 lut->SetNumberOfTableValues(obstacles->size());
1166 points->SetNumberOfPoints(obstacles->size());
1167 double s = octomap->
octree()->getNodeSize(treeDepth) / 2.0;
1168 for (
unsigned int i = 0;
i < obstacles->size();
i++)
1170 points->InsertPoint(
i,
1171 cloud->at(obstacles->at(
i)).x,
1172 cloud->at(obstacles->at(
i)).y,
1173 cloud->at(obstacles->at(
i)).z);
1174 colors->InsertValue(
i,
i);
1176 lut->SetTableValue(
i,
1177 double(cloud->at(obstacles->at(
i)).r) / 255.0,
1178 double(cloud->at(obstacles->at(
i)).g) / 255.0,
1179 double(cloud->at(obstacles->at(
i)).b) / 255.0);
1184 polydata->SetPoints(points);
1185 polydata->GetPointData()->SetScalars(colors);
1189 cubeSource->SetBounds(-
s,
s, -
s,
s, -
s,
s);
1192 mapper->SetSourceConnection(cubeSource->GetOutputPort());
1193 #if VTK_MAJOR_VERSION <= 5
1194 mapper->SetInputConnection(polydata->GetProducerPort());
1196 mapper->SetInputData(polydata);
1198 mapper->SetScalarRange(0, obstacles->size() - 1);
1199 mapper->SetLookupTable(lut);
1200 mapper->ScalingOff();
1204 octomapActor->SetMapper(mapper);
1206 octomapActor->GetProperty()->SetRepresentationToSurface();
1207 octomapActor->GetProperty()->SetEdgeVisibility(_aSetEdgeVisibility->isChecked());
1208 octomapActor->GetProperty()->SetLighting(_aSetLighting->isChecked());
1210 _visualizer->getRendererCollection()->InitTraversal ();
1211 vtkRenderer* renderer =
NULL;
1212 renderer = _visualizer->getRendererCollection()->GetNextItem ();
1213 renderer = _visualizer->getRendererCollection()->GetNextItem ();
1215 renderer->AddActor(octomapActor);
1217 _octomapActor = octomapActor.GetPointer();
1223 if(octomap->
octree()->size())
1229 double sizeX, sizeY, sizeZ;
1230 double minX, minY, minZ;
1231 double maxX, maxY, maxZ;
1237 double cellSize = octomap->
octree()->getNodeSize(treeDepth);
1241 imageData->SetExtent(0,
int(sizeX/cellSize+0.5), 0,
int(sizeY/cellSize+0.5), 0,
int(sizeZ/cellSize+0.5));
1242 #if VTK_MAJOR_VERSION <= 5
1243 imageData->SetNumberOfScalarComponents(4);
1244 imageData->SetScalarTypeToUnsignedChar();
1246 imageData->AllocateScalars(VTK_UNSIGNED_CHAR,4);
1250 imageData->GetDimensions(dims);
1252 memset(imageData->GetScalarPointer(), 0, imageData->GetScalarSize()*imageData->GetNumberOfScalarComponents()*dims[0]*dims[1]*dims[2]);
1254 for (RtabmapColorOcTree::iterator it = octomap->
octree()->begin(treeDepth); it != octomap->
octree()->end(); ++it)
1256 if(octomap->
octree()->isNodeOccupied(*it))
1258 octomap::point3d pt = octomap->
octree()->keyToCoord(it.getKey());
1259 int x = (pt.x()-minX) / cellSize;
1260 int y = (pt.y()-minY) / cellSize;
1261 int z = (pt.z()-minZ) / cellSize;
1262 if(
x>=0 &&
x<dims[0] &&
y>=0 &&
y<dims[1] &&
z>=0 &&
z<dims[2])
1264 unsigned char* pixel =
static_cast<unsigned char*
>(imageData->GetScalarPointer(
x,
y,
z));
1265 if(octomap->
octree()->getTreeDepth() == it.getDepth() && it->isColorSet())
1267 pixel[0] = it->getColor().r;
1268 pixel[1] = it->getColor().g;
1269 pixel[2] = it->getColor().b;
1274 float H = (maxZ - pt.z())*299.0
f/(maxZ-minZ);
1277 pixel[0] = r*255.0f;
1278 pixel[1] =
g*255.0f;
1279 pixel[2] =
b*255.0f;
1287 volumeMapper->SetBlendModeToComposite();
1288 #if VTK_MAJOR_VERSION <= 5
1289 volumeMapper->SetInputConnection(imageData->GetProducerPort());
1291 volumeMapper->SetInputData(imageData);
1295 volumeProperty->ShadeOff();
1296 volumeProperty->IndependentComponentsOff();
1300 compositeOpacity->AddPoint(0.0,0.0);
1301 compositeOpacity->AddPoint(255.0,1.0);
1302 volumeProperty->SetScalarOpacity(0, compositeOpacity);
1306 volume->SetMapper(volumeMapper);
1307 volume->SetProperty(volumeProperty);
1308 volume->SetScale(cellSize);
1309 volume->SetPosition(minX, minY, minZ);
1311 _visualizer->getRendererCollection()->InitTraversal ();
1312 vtkRenderer* renderer =
NULL;
1313 renderer = _visualizer->getRendererCollection()->GetNextItem ();
1314 renderer = _visualizer->getRendererCollection()->GetNextItem ();
1316 renderer->AddViewProp(volume);
1319 #if !defined(VTK_LEGACY_REMOVE) && !defined(VTK_OPENGL2) && VTK_MAJOR_VERSION < 9
1320 volumeMapper->SetRequestedRenderModeToRayCastAndTexture();
1321 #endif // VTK_LEGACY_REMOVE
1325 volumeMapper->SetRequestedRenderModeToRayCast();
1326 _octomapActor = volume.GetPointer();
1338 #ifdef RTABMAP_OCTOMAP
1341 _visualizer->getRendererCollection()->InitTraversal ();
1342 vtkRenderer* renderer =
NULL;
1343 renderer = _visualizer->getRendererCollection()->GetNextItem ();
1344 renderer = _visualizer->getRendererCollection()->GetNextItem ();
1346 renderer->RemoveActor(_octomapActor);
1353 const pcl::TextureMesh &mesh,
1354 const cv::Mat & image,
1355 const std::string &
id,
1360 #if VTK_MAJOR_VERSION >= 7
1361 pcl::visualization::ShapeActorMap::iterator am_it = _visualizer->getShapeActorMap()->find (
id);
1362 if (am_it != _visualizer->getShapeActorMap()->end ())
1364 pcl::visualization::CloudActorMap::iterator am_it = _visualizer->getCloudActorMap()->find (
id);
1365 if (am_it != _visualizer->getCloudActorMap()->end ())
1368 PCL_ERROR (
"[PCLVisualizer::addTextureMesh] A shape with id <%s> already exists!"
1369 " Please choose a different id and retry.\n",
1374 if (mesh.tex_materials.size () == 0)
1376 PCL_ERROR(
"[PCLVisualizer::addTextureMesh] No textures found!\n");
1379 else if (mesh.tex_materials.size() > 1)
1381 PCL_ERROR(
"[PCLVisualizer::addTextureMesh] only one material per mesh is supported!\n");
1385 if (mesh.tex_materials.size () != mesh.tex_polygons.size ())
1387 PCL_ERROR(
"[PCLVisualizer::addTextureMesh] Materials number %lu differs from polygons number %lu!\n",
1388 mesh.tex_materials.size (), mesh.tex_polygons.size ());
1392 if (mesh.tex_materials.size () != mesh.tex_coordinates.size ())
1394 PCL_ERROR(
"[PCLVisualizer::addTextureMesh] Coordinates number %lu differs from materials number %lu!\n",
1395 mesh.tex_coordinates.size (), mesh.tex_materials.size ());
1399 std::size_t nb_vertices = 0;
1400 for (std::size_t
i = 0;
i < mesh.tex_polygons.size (); ++
i)
1401 nb_vertices+= mesh.tex_polygons[
i].size ();
1403 if (nb_vertices == 0)
1405 PCL_ERROR(
"[PCLVisualizer::addTextureMesh] No vertices found!\n");
1409 std::size_t nb_coordinates = 0;
1410 for (std::size_t
i = 0;
i < mesh.tex_coordinates.size (); ++
i)
1411 nb_coordinates+= mesh.tex_coordinates[
i].size ();
1413 if (nb_coordinates == 0)
1415 PCL_ERROR(
"[PCLVisualizer::addTextureMesh] No textures coordinates found!\n");
1422 colors->SetNumberOfComponents(3);
1423 colors->SetName (
"Colors");
1424 bool hasColors =
false;
1425 for(
unsigned int i=0;
i<mesh.cloud.fields.size(); ++
i)
1427 if(mesh.cloud.fields[
i].name.compare(
"rgb") == 0)
1435 pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (
new pcl::PointCloud<pcl::PointXYZRGB> ());
1436 pcl::fromPCLPointCloud2 (mesh.cloud, *cloud);
1438 if (cloud->points.size () == 0)
1440 PCL_ERROR(
"[PCLVisualizer::addTextureMesh] Cloud is empty!\n");
1443 pcl::visualization::PCLVisualizer::convertToVtkMatrix (cloud->sensor_origin_, cloud->sensor_orientation_,
transformation);
1444 poly_points->SetNumberOfPoints (cloud->points.size ());
1445 for (std::size_t
i = 0;
i < cloud->points.size (); ++
i)
1447 const pcl::PointXYZRGB &
p = cloud->points[
i];
1448 poly_points->InsertPoint (
i,
p.x,
p.y,
p.z);
1451 unsigned char color[3] = {
p.r,
p.g,
p.b};
1452 #if VTK_MAJOR_VERSION > 7 || (VTK_MAJOR_VERSION==7 && VTK_MINOR_VERSION >= 1)
1453 colors->InsertNextTypedTuple(color);
1455 colors->InsertNextTupleValue(color);
1462 for (std::size_t
i = 0;
i < mesh.tex_polygons.size ();
i++)
1464 for (std::size_t
j = 0;
j < mesh.tex_polygons[
i].size ();
j++)
1466 std::size_t n_points = mesh.tex_polygons[
i][
j].vertices.size ();
1467 polys->InsertNextCell (
int (n_points));
1468 for (std::size_t k = 0; k < n_points; k++)
1469 polys->InsertCellPoint (mesh.tex_polygons[
i][
j].vertices[k]);
1474 polydata->SetPolys (polys);
1475 polydata->SetPoints (poly_points);
1477 polydata->GetPointData()->SetScalars(colors);
1481 #if VTK_MAJOR_VERSION < 6
1482 mapper->SetInput (polydata);
1484 mapper->SetInputData (polydata);
1487 #if VTK_MAJOR_VERSION >= 7
1492 vtkTextureUnitManager* tex_manager = vtkOpenGLRenderWindow::SafeDownCast (_visualizer->getRenderWindow())->GetTextureUnitManager ();
1499 cvImageToVtk->SetImage(image);
1500 cvImageToVtk->Update();
1501 texture->SetInputConnection(cvImageToVtk->GetOutputPort());
1505 coordinates->SetNumberOfComponents (2);
1506 coordinates->SetNumberOfTuples (mesh.tex_coordinates[0].size ());
1507 for (std::size_t tc = 0; tc < mesh.tex_coordinates[0].size (); ++tc)
1509 const Eigen::Vector2f &uv = mesh.tex_coordinates[0][tc];
1510 coordinates->SetTuple2 (tc, (
double)uv[0], (
double)uv[1]);
1512 coordinates->SetName (
"TCoords");
1513 polydata->GetPointData ()->SetTCoords(coordinates);
1515 actor->SetTexture (texture);
1518 actor->SetMapper (mapper);
1522 _visualizer->getRendererCollection()->InitTraversal ();
1523 vtkRenderer* renderer =
NULL;
1525 while ((renderer = _visualizer->getRendererCollection()->GetNextItem ()) !=
NULL)
1530 renderer->AddActor (actor);
1532 else if (viewport ==
i)
1534 renderer->AddActor (actor);
1540 #if VTK_MAJOR_VERSION >= 7
1541 (*_visualizer->getShapeActorMap())[
id] = actor;
1543 (*_visualizer->getCloudActorMap())[
id].actor = actor;
1545 (*_visualizer->getCloudActorMap())[
id].viewpoint_transformation_ =
transformation;
1548 #if VTK_MAJOR_VERSION >= 7
1549 actor->GetProperty()->SetAmbient(0.1);
1551 actor->GetProperty()->SetAmbient(0.5);
1553 actor->GetProperty()->SetLighting(_aSetLighting->isChecked());
1554 actor->GetProperty()->SetInterpolation(_aSetFlatShading->isChecked()?VTK_FLAT:VTK_PHONG);
1555 actor->GetProperty()->SetEdgeVisibility(_aSetEdgeVisibility->isChecked());
1556 actor->GetProperty()->SetBackfaceCulling(_aBackfaceCulling->isChecked());
1557 actor->GetProperty()->SetFrontfaceCulling(_frontfaceCulling);
1558 actor->GetMapper()->SetScalarVisibility(_aSetScalarVisibility->isChecked());
1563 const cv::Mat & map8U,
1569 UASSERT(map8U.channels() == 1 && map8U.type() == CV_8U);
1571 float xSize =
float(map8U.cols) * resolution;
1572 float ySize =
float(map8U.rows) * resolution;
1574 UDEBUG(
"resolution=%f, xSize=%f, ySize=%f, xMin=%f, yMin=%f", resolution, xSize, ySize, xMin, yMin);
1575 #if VTK_MAJOR_VERSION >= 7
1576 if(_visualizer->getShapeActorMap()->find(
"map") != _visualizer->getShapeActorMap()->end())
1578 _visualizer->removeShape(
"map");
1581 if(_visualizer->getCloudActorMap()->find(
"map") != _visualizer->getCloudActorMap()->end())
1583 _visualizer->removePointCloud(
"map");
1587 if(xSize > 0.0
f && ySize > 0.0
f)
1589 pcl::TextureMeshPtr mesh(
new pcl::TextureMesh());
1590 pcl::PointCloud<pcl::PointXYZ> cloud;
1591 cloud.push_back(pcl::PointXYZ(xMin, yMin, 0));
1592 cloud.push_back(pcl::PointXYZ(xSize+xMin, yMin, 0));
1593 cloud.push_back(pcl::PointXYZ(xSize+xMin, ySize+yMin, 0));
1594 cloud.push_back(pcl::PointXYZ(xMin, ySize+yMin, 0));
1595 pcl::toPCLPointCloud2(cloud, mesh->cloud);
1597 std::vector<pcl::Vertices> polygons(1);
1598 polygons[0].vertices.push_back(0);
1599 polygons[0].vertices.push_back(1);
1600 polygons[0].vertices.push_back(2);
1601 polygons[0].vertices.push_back(3);
1602 polygons[0].vertices.push_back(0);
1603 mesh->tex_polygons.push_back(polygons);
1608 mesh->tex_materials.push_back(
material);
1610 #if PCL_VERSION_COMPARE(>=, 1, 8, 0)
1611 std::vector<Eigen::Vector2f, Eigen::aligned_allocator<Eigen::Vector2f> > coordinates;
1613 std::vector<Eigen::Vector2f> coordinates;
1615 coordinates.push_back(Eigen::Vector2f(0,1));
1616 coordinates.push_back(Eigen::Vector2f(1,1));
1617 coordinates.push_back(Eigen::Vector2f(1,0));
1618 coordinates.push_back(Eigen::Vector2f(0,0));
1619 mesh->tex_coordinates.push_back(coordinates);
1621 this->addTextureMesh(*mesh, map8U,
"map", 1);
1622 setCloudOpacity(
"map", opacity);
1629 #if VTK_MAJOR_VERSION >= 7
1630 if(_visualizer->getShapeActorMap()->find(
"map") != _visualizer->getShapeActorMap()->end())
1632 _visualizer->removeShape(
"map");
1635 if(_visualizer->getCloudActorMap()->find(
"map") != _visualizer->getCloudActorMap()->end())
1637 _visualizer->removePointCloud(
"map");
1643 const cv::Mat & map32FC1,
1649 if(_visualizer->getShapeActorMap()->find(
"elevation_map") != _visualizer->getShapeActorMap()->end())
1651 _visualizer->removeShape(
"elevation_map");
1656 for (
int y = 0;
y < map32FC1.rows; ++
y)
1658 const float * previousRow =
y>0?map32FC1.ptr<
float>(
y-1):0;
1659 const float * rowPtr = map32FC1.ptr<
float>(
y);
1660 for (
int x = 0;
x < map32FC1.cols; ++
x)
1662 gridPoints->InsertNextPoint(xMin +
x*resolution, yMin +
y*resolution, rowPtr[
x]);
1666 previousRow[
x] != 0 &&
1667 previousRow[
x-1] != 0)
1669 gridCells->InsertNextCell(4);
1670 gridCells->InsertCellPoint(
x-1+
y*map32FC1.cols);
1671 gridCells->InsertCellPoint(
x+
y*map32FC1.cols);
1672 gridCells->InsertCellPoint(
x+(
y-1)*map32FC1.cols);
1673 gridCells->InsertCellPoint(
x-1+(
y-1)*map32FC1.cols);
1679 gridPoints->GetBounds(bounds);
1682 polyData->SetPoints(gridPoints);
1683 polyData->SetPolys(gridCells);
1686 elevationFilter->SetInputData(polyData);
1687 elevationFilter->SetLowPoint(0.0, 0.0, bounds[4]);
1688 elevationFilter->SetHighPoint(0.0, 0.0, bounds[5]);
1689 elevationFilter->Update();
1692 output->ShallowCopy(
dynamic_cast<vtkPolyData*
>(elevationFilter->GetOutput()));
1694 vtkFloatArray* elevation =
dynamic_cast<vtkFloatArray*
>(
1695 output->GetPointData()->GetArray(
"Elevation"));
1699 colorLookupTable->SetTableRange(bounds[4], bounds[5]);
1700 colorLookupTable->Build();
1704 colors->SetNumberOfComponents(3);
1705 colors->SetName(
"Colors");
1707 for (vtkIdType
i = 0;
i < output->GetNumberOfPoints();
i++)
1709 double val = elevation->GetValue(
i);
1711 colorLookupTable->GetColor(val, dcolor);
1712 unsigned char color[3];
1713 for (
unsigned int j = 0;
j < 3;
j++)
1715 color[
j] = 255 * dcolor[
j] / 1.0;
1718 #if VTK_MAJOR_VERSION > 7 || (VTK_MAJOR_VERSION==7 && VTK_MINOR_VERSION >= 1)
1719 colors->InsertNextTypedTuple(color);
1721 colors->InsertNextTupleValue(color);
1726 output->GetPointData()->AddArray(colors);
1729 mapper->SetInputData(output);
1732 actor->SetMapper(mapper);
1735 _visualizer->getRendererCollection()->InitTraversal ();
1736 vtkRenderer* renderer =
NULL;
1739 while ((renderer = _visualizer->getRendererCollection()->GetNextItem ()) !=
NULL)
1744 renderer->AddActor (actor);
1746 else if (viewport ==
i)
1748 renderer->AddActor (actor);
1753 (*_visualizer->getShapeActorMap())[
"elevation_map"] = actor;
1755 setCloudOpacity(
"elevation_map", opacity);
1761 if(_visualizer->getShapeActorMap()->find(
"elevation_map") != _visualizer->getShapeActorMap()->end())
1763 _visualizer->removeShape(
"elevation_map");
1768 const std::string &
id,
1775 UERROR(
"id should not be empty!");
1779 removeCoordinate(
id);
1783 _coordinates.insert(
id);
1784 #if PCL_VERSION_COMPARE(>=, 1, 7, 2)
1785 _visualizer->addCoordinateSystem(
scale*_coordinateFrameScale,
transform.toEigen3f(),
id, foreground?3:2);
1788 _visualizer->addCoordinateSystem(
scale*_coordinateFrameScale,
transform.toEigen3f(), 0);
1794 const std::string &
id,
1797 #if PCL_VERSION_COMPARE(>=, 1, 7, 2)
1798 if(_coordinates.find(
id) != _coordinates.end() && !pose.
isNull())
1801 return _visualizer->updateCoordinateSystemPose(
id, pose.
toEigen3f());
1804 UERROR(
"CloudViewer::updateCoordinatePose() is not available on PCL < 1.7.2");
1813 UERROR(
"id should not be empty!");
1817 if(_coordinates.find(
id) != _coordinates.end())
1819 #if PCL_VERSION_COMPARE(>=, 1, 7, 2)
1820 _visualizer->removeCoordinateSystem(
id);
1823 _visualizer->removeCoordinateSystem(0);
1825 _coordinates.erase(
id);
1831 std::set<std::string> coordinates = _coordinates;
1832 for(std::set<std::string>::iterator
iter = coordinates.begin();
iter!=coordinates.end(); ++
iter)
1834 if(prefix.empty() ||
iter->find(prefix) != std::string::npos)
1836 this->removeCoordinate(*
iter);
1839 UASSERT(!prefix.empty() || _coordinates.empty());
1843 const std::string &
id,
1846 const QColor & color,
1852 UERROR(
"id should not be empty!");
1862 QColor
c = Qt::gray;
1868 pcl::PointXYZ pt1(from.
x(), from.
y(), from.
z());
1869 pcl::PointXYZ pt2(to.
x(), to.
y(), to.
z());
1873 _visualizer->addArrow(pt2, pt1,
c.redF(),
c.greenF(),
c.blueF(),
false,
id, foreground?3:2);
1877 _visualizer->addLine(pt2, pt1,
c.redF(),
c.greenF(),
c.blueF(),
id, foreground?3:2);
1879 _visualizer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_OPACITY,
c.alphaF(),
id);
1887 UERROR(
"id should not be empty!");
1891 if(_lines.find(
id) != _lines.end())
1893 _visualizer->removeShape(
id);
1900 std::set<std::string> arrows = _lines;
1901 for(std::set<std::string>::iterator
iter = arrows.begin();
iter!=arrows.end(); ++
iter)
1903 this->removeLine(*
iter);
1909 const std::string &
id,
1912 const QColor & color,
1917 UERROR(
"id should not be empty!");
1925 _spheres.insert(
id);
1927 QColor
c = Qt::gray;
1933 pcl::PointXYZ center(pose.
x(), pose.
y(), pose.
z());
1934 _visualizer->addSphere(center, radius,
c.redF(),
c.greenF(),
c.blueF(),
id, foreground?3:2);
1935 _visualizer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_OPACITY,
c.alphaF(),
id);
1943 UERROR(
"id should not be empty!");
1947 if(_spheres.find(
id) != _spheres.end())
1949 _visualizer->removeShape(
id);
1956 std::set<std::string> spheres = _spheres;
1957 for(std::set<std::string>::iterator
iter = spheres.begin();
iter!=spheres.end(); ++
iter)
1959 this->removeSphere(*
iter);
1965 const std::string &
id,
1970 const QColor & color,
1976 UERROR(
"id should not be empty!");
1986 QColor
c = Qt::gray;
1991 _visualizer->addCube(Eigen::Vector3f(pose.
x(), pose.
y(), pose.
z()), pose.
getQuaternionf(), width, height, depth,
id, foreground?3:2);
1994 _visualizer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_REPRESENTATION, pcl::visualization::PCL_VISUALIZER_REPRESENTATION_WIREFRAME,
id);
1996 _visualizer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR,
c.redF(),
c.greenF(),
c.blueF(),
id);
1997 _visualizer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_OPACITY,
c.alphaF(),
id);
2005 UERROR(
"id should not be empty!");
2009 if(_cubes.find(
id) != _cubes.end())
2011 _visualizer->removeShape(
id);
2018 std::set<std::string> cubes = _cubes;
2019 for(std::set<std::string>::iterator
iter = cubes.begin();
iter!=cubes.end(); ++
iter)
2021 this->removeCube(*
iter);
2027 const std::string &
id,
2031 const QColor & color,
2034 addOrUpdateQuad(
id, pose, width/2.0
f, width/2.0
f, height/2.0
f, height/2.0
f, color, foreground);
2038 const std::string &
id,
2044 const QColor & color,
2049 UERROR(
"id should not be empty!");
2059 QColor
c = Qt::gray;
2066 double p0[3] = {0.0, -widthLeft, heightTop};
2067 double p1[3] = {0.0, -widthLeft, -heightBottom};
2068 double p2[3] = {0.0, widthRight, -heightBottom};
2069 double p3[3] = {0.0, widthRight, heightTop};
2074 points->InsertNextPoint(
p0);
2075 points->InsertNextPoint(
p1);
2076 points->InsertNextPoint(
p2);
2077 points->InsertNextPoint(
p3);
2082 quad->GetPointIds()->SetId(0,0);
2083 quad->GetPointIds()->SetId(1,1);
2084 quad->GetPointIds()->SetId(2,2);
2085 quad->GetPointIds()->SetId(3,3);
2090 quads->InsertNextCell(quad);
2097 polydata->SetPoints(points);
2098 polydata->SetPolys(quads);
2103 #if VTK_MAJOR_VERSION <= 5
2104 mapper->SetInput(polydata);
2106 mapper->SetInputData(polydata);
2111 actor->SetMapper(mapper);
2112 actor->GetProperty()->SetColor(
c.redF(),
c.greenF(),
c.blueF());
2116 _visualizer->getRendererCollection()->InitTraversal ();
2117 vtkRenderer* renderer =
NULL;
2119 while ((renderer = _visualizer->getRendererCollection()->GetNextItem ()) !=
NULL)
2121 if ((foreground?3:2) ==
i)
2123 renderer->AddActor (actor);
2129 (*_visualizer->getCloudActorMap())[
id].actor = actor;
2134 (*_visualizer->getCloudActorMap())[
id].viewpoint_transformation_ =
transformation;
2135 (*_visualizer->getCloudActorMap())[
id].actor->SetUserMatrix (
transformation);
2136 (*_visualizer->getCloudActorMap())[
id].actor->Modified ();
2138 (*_visualizer->getCloudActorMap())[
id].actor->GetProperty()->SetLighting(
false);
2139 _visualizer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_OPACITY,
c.alphaF(),
id);
2147 UERROR(
"id should not be empty!");
2151 if(_quads.find(
id) != _quads.end())
2153 _visualizer->removeShape(
id);
2160 std::set<std::string> quads = _quads;
2161 for(std::set<std::string>::iterator
iter = quads.begin();
iter!=quads.end(); ++
iter)
2163 this->removeQuad(*
iter);
2176 1, 2, 3, 4, 1, 0, 2, 0, 3, 0, 4};
2179 const std::string &
id,
2183 const QColor & color,
2189 UERROR(
"id should not be empty!");
2193 #if PCL_VERSION_COMPARE(<, 1, 7, 2)
2194 this->removeFrustum(
id);
2199 if(_frustums.find(
id)==_frustums.end())
2204 UASSERT(frustumSize>0 && frustumSize % 3 == 0);
2206 pcl::PointCloud<pcl::PointXYZ> frustumPoints;
2207 frustumPoints.resize(frustumSize);
2208 float scaleX =
tan((fovX>0?fovX:1.1)/2.0
f) *
scale;
2209 float scaleY =
tan((fovY>0?fovY:0.85)/2.0
f) *
scale;
2210 float scaleZ =
scale;
2211 QColor
c = Qt::gray;
2216 Transform opticalRotInv(0, -1, 0, 0, 0, 0, -1, 0, 1, 0, 0, 0);
2218 #if PCL_VERSION_COMPARE(<, 1, 7, 2)
2223 for(
int i=0;
i<frustumSize; ++
i)
2231 pcl::PolygonMesh mesh;
2234 for(
unsigned int i=0;
i<
vertices.vertices.size(); ++
i)
2238 pcl::toPCLPointCloud2(frustumPoints, mesh.cloud);
2240 _visualizer->addPolylineFromPolygonMesh(mesh,
id, 2);
2241 _visualizer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR,
c.redF(),
c.greenF(),
c.blueF(),
id);
2242 _visualizer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_OPACITY,
c.alphaF(),
id);
2244 #if PCL_VERSION_COMPARE(>=, 1, 7, 2)
2245 if(!this->updateFrustumPose(
id, pose))
2247 UERROR(
"Failed updating pose of frustum %s!?",
id.
c_str());
2258 const std::string &
id,
2261 #if PCL_VERSION_COMPARE(>=, 1, 7, 2)
2262 QMap<std::string, Transform>::iterator
iter=_frustums.find(
id);
2263 if(
iter != _frustums.end() && !pose.
isNull())
2271 pcl::visualization::ShapeActorMap::iterator am_it = _visualizer->getShapeActorMap()->find (
id);
2275 if (am_it == _visualizer->getShapeActorMap()->end ())
2278 actor = vtkActor::SafeDownCast (am_it->second);
2287 actor->SetUserMatrix (
matrix);
2295 UERROR(
"updateFrustumPose() cannot be used with PCL<1.7.2. Use addOrUpdateFrustum() instead.");
2304 UERROR(
"id should not be empty!");
2308 if(_frustums.find(
id) != _frustums.end())
2310 _visualizer->removeShape(
id);
2311 _frustums.remove(
id);
2317 QMap<std::string, Transform> frustums = _frustums;
2318 for(QMap<std::string, Transform>::iterator
iter = frustums.begin();
iter!=frustums.end(); ++
iter)
2320 if(!exceptCameraReference || !
uStrContains(
iter.key(),
"reference_frustum"))
2322 this->removeFrustum(
iter.key());
2325 UASSERT(exceptCameraReference || _frustums.empty());
2329 const std::string &
id,
2330 const pcl::PointCloud<pcl::PointXYZ>::Ptr & graph,
2331 const QColor & color)
2335 UERROR(
"id should not be empty!");
2343 _graphes.insert(
id);
2345 pcl::PolygonMesh mesh;
2348 for(
unsigned int i=0;
i<
vertices.vertices.size(); ++
i)
2352 pcl::toPCLPointCloud2(*
graph, mesh.cloud);
2354 _visualizer->addPolylineFromPolygonMesh(mesh,
id, 2);
2355 _visualizer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, color.redF(), color.greenF(), color.blueF(),
id);
2356 _visualizer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_OPACITY, color.alphaF(),
id);
2358 pcl::PCLPointCloud2Ptr binaryCloud(
new pcl::PCLPointCloud2);
2359 pcl::toPCLPointCloud2(*
graph, *binaryCloud);
2361 this->setCloudPointSize(
id+
"_nodes", 5);
2369 UERROR(
"id should not be empty!");
2373 if(_graphes.find(
id) != _graphes.end())
2375 _visualizer->removeShape(
id);
2377 removeCloud(
id+
"_nodes");
2383 std::set<std::string> graphes = _graphes;
2384 for(std::set<std::string>::iterator
iter = graphes.begin();
iter!=graphes.end(); ++
iter)
2386 this->removeGraph(*
iter);
2392 const std::string &
id,
2393 const std::string & text,
2396 const QColor & color,
2401 UERROR(
"id should not be empty!");
2410 _visualizer->addText3D(
2426 UERROR(
"id should not be empty!");
2430 if(_texts.find(
id) != _texts.end())
2432 _visualizer->removeText3D(
id);
2439 std::set<std::string> texts = _texts;
2440 for(std::set<std::string>::iterator
iter = texts.begin();
iter!=texts.end(); ++
iter)
2442 this->removeText(*
iter);
2449 return _aShowTrajectory->isChecked();
2454 return _maxTrajectorySize;
2459 _aShowTrajectory->setChecked(shown);
2464 _maxTrajectorySize =
value;
2469 _trajectory->clear();
2470 _visualizer->removeShape(
"trajectory");
2471 this->refreshView();
2476 return _aShowCameraAxis->isChecked();
2483 this->removeCoordinate(
"reference");
2489 this->refreshView();
2490 _aShowCameraAxis->setChecked(shown);
2495 return _coordinateFrameScale;
2505 return _aShowFrustum->isChecked();
2510 return _frustumScale;
2515 return _frustumColor;
2522 QMap<std::string, Transform> frustumsCopy = _frustums;
2523 for(QMap<std::string, Transform>::iterator
iter=frustumsCopy.begin();
iter!=frustumsCopy.end(); ++
iter)
2527 this->removeFrustum(
iter.key());
2530 std::set<std::string> linesCopy = _lines;
2531 for(std::set<std::string>::iterator
iter=linesCopy.begin();
iter!=linesCopy.end(); ++
iter)
2535 this->removeLine(*
iter);
2538 this->refreshView();
2540 _aShowFrustum->setChecked(shown);
2545 _frustumScale =
value;
2550 if(!
value.isValid())
2554 for(QMap<std::string, Transform>::iterator
iter=_frustums.begin();
iter!=_frustums.end(); ++
iter)
2556 _visualizer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR,
value.redF(),
value.greenF(),
value.blueF(),
iter.key());
2558 this->refreshView();
2559 _frustumColor =
value;
2564 _lastCameraOrientation= _lastCameraPose = cv::Vec3f(0,0,0);
2565 if((_aFollowCamera->isChecked() || _aLockCamera->isChecked()) && !_lastPose.isNull())
2569 if(_aCameraOrtho->isChecked())
2571 this->setCameraPosition(
2572 _lastPose.x(), _lastPose.y(), _lastPose.z()+5,
2573 _lastPose.x(), _lastPose.y(), _lastPose.z(),
2576 else if(_aLockViewZ->isChecked())
2578 this->setCameraPosition(
2580 _lastPose.x(), _lastPose.y(), _lastPose.z(),
2585 this->setCameraPosition(
2587 _lastPose.x(), _lastPose.y(), _lastPose.z(),
2588 _lastPose.r31(), _lastPose.r32(), _lastPose.r33());
2591 else if(_aCameraOrtho->isChecked())
2593 this->setCameraPosition(
2600 this->setCameraPosition(
2609 QMap<std::string, Transform> addedClouds = _addedClouds;
2610 QList<std::string> ids = _addedClouds.keys();
2611 for(QList<std::string>::iterator
iter = ids.begin();
iter!=ids.end(); ++
iter)
2615 UASSERT(_addedClouds.empty());
2622 bool success = _visualizer->removePointCloud(
id);
2623 #if VTK_MAJOR_VERSION >= 7
2626 success = _visualizer->removeShape(
id);
2629 _visualizer->removePointCloud(
id+
"-normals");
2630 _addedClouds.remove(
id);
2631 _addedClouds.remove(
id+
"-normals");
2632 _locators.erase(
id);
2638 if(_addedClouds.contains(
id))
2640 pose = _addedClouds.value(
id);
2648 if(_lastPose.isNull())
2657 pcl::visualization::CloudActorMapPtr cloudActorMap = _visualizer->getCloudActorMap();
2658 for(pcl::visualization::CloudActorMap::iterator
iter=cloudActorMap->begin();
iter!=cloudActorMap->end(); ++
iter)
2660 if(
iter->second.actor.GetPointer() == actor)
2666 #if PCL_VERSION_COMPARE(>=, 1, 7, 2)
2668 pcl::visualization::ShapeActorMapPtr shapeActorMap = _visualizer->getShapeActorMap();
2669 for(pcl::visualization::ShapeActorMap::iterator
iter=shapeActorMap->begin();
iter!=shapeActorMap->end(); ++
iter)
2671 if(
iter->second.GetPointer() == actor)
2673 std::string
id =
iter->first;
2674 while(
id.
size() &&
id.at(
id.
size()-1) ==
'*')
2676 id.erase(
id.
size()-1);
2683 return std::string();
2689 pcl::visualization::CloudActorMap::iterator
iter = _visualizer->getCloudActorMap()->find(
id);
2690 if(
iter != _visualizer->getCloudActorMap()->end())
2693 iter->second.actor->GetProperty()->GetColor(r,
g,
b);
2694 a =
iter->second.actor->GetProperty()->GetOpacity();
2695 color.setRgbF(r,
g,
b,
a);
2697 #if PCL_VERSION_COMPARE(>=, 1, 7, 2)
2701 std::string idLayer1 =
id+
"*";
2702 std::string idLayer2 =
id+
"**";
2703 pcl::visualization::ShapeActorMap::iterator
iter = _visualizer->getShapeActorMap()->find(
id);
2704 if(
iter == _visualizer->getShapeActorMap()->end())
2706 iter = _visualizer->getShapeActorMap()->find(idLayer1);
2707 if(
iter == _visualizer->getShapeActorMap()->end())
2709 iter = _visualizer->getShapeActorMap()->find(idLayer2);
2712 if(
iter != _visualizer->getShapeActorMap()->end())
2714 vtkActor * actor = vtkActor::SafeDownCast(
iter->second);
2718 actor->GetProperty()->GetColor(r,
g,
b);
2719 a = actor->GetProperty()->GetOpacity();
2720 color.setRgbF(r,
g,
b,
a);
2730 pcl::visualization::CloudActorMap::iterator
iter = _visualizer->getCloudActorMap()->find(
id);
2731 if(
iter != _visualizer->getCloudActorMap()->end())
2733 iter->second.actor->GetProperty()->SetColor(color.redF(),color.greenF(),color.blueF());
2734 iter->second.actor->GetProperty()->SetOpacity(color.alphaF());
2736 #if PCL_VERSION_COMPARE(>=, 1, 7, 2)
2740 std::string idLayer1 =
id+
"*";
2741 std::string idLayer2 =
id+
"**";
2742 pcl::visualization::ShapeActorMap::iterator
iter = _visualizer->getShapeActorMap()->find(
id);
2743 if(
iter == _visualizer->getShapeActorMap()->end())
2745 iter = _visualizer->getShapeActorMap()->find(idLayer1);
2746 if(
iter == _visualizer->getShapeActorMap()->end())
2748 iter = _visualizer->getShapeActorMap()->find(idLayer2);
2751 if(
iter != _visualizer->getShapeActorMap()->end())
2753 vtkActor * actor = vtkActor::SafeDownCast(
iter->second);
2756 actor->GetProperty()->SetColor(color.redF(),color.greenF(),color.blueF());
2757 actor->GetProperty()->SetOpacity(color.alphaF());
2766 _aBackfaceCulling->setChecked(enabled);
2767 _frontfaceCulling = frontfaceCulling;
2769 pcl::visualization::CloudActorMapPtr cloudActorMap = _visualizer->getCloudActorMap();
2770 for(pcl::visualization::CloudActorMap::iterator
iter=cloudActorMap->begin();
iter!=cloudActorMap->end(); ++
iter)
2772 iter->second.actor->GetProperty()->SetBackfaceCulling(_aBackfaceCulling->isChecked());
2773 iter->second.actor->GetProperty()->SetFrontfaceCulling(_frontfaceCulling);
2775 #if VTK_MAJOR_VERSION >= 7
2776 pcl::visualization::ShapeActorMapPtr shapeActorMap = _visualizer->getShapeActorMap();
2777 for(pcl::visualization::ShapeActorMap::iterator
iter=shapeActorMap->begin();
iter!=shapeActorMap->end(); ++
iter)
2779 vtkActor* actor = vtkActor::SafeDownCast (
iter->second);
2782 actor->GetProperty()->SetBackfaceCulling(_aBackfaceCulling->isChecked());
2783 actor->GetProperty()->SetFrontfaceCulling(_frontfaceCulling);
2787 this->refreshView();
2792 _aPolygonPicking->setChecked(enabled);
2794 if(!_aPolygonPicking->isChecked())
2797 pp->SetTolerance (pp->GetTolerance());
2798 #if VTK_MAJOR_VERSION > 8
2799 this->interactor()->SetPicker (pp);
2801 this->GetInteractor()->SetPicker (pp);
2803 setMouseTracking(
false);
2808 pp->SetTolerance (pp->GetTolerance());
2809 #if VTK_MAJOR_VERSION > 8
2810 this->interactor()->SetPicker (pp);
2812 this->GetInteractor()->SetPicker (pp);
2814 setMouseTracking(
true);
2822 _renderingRate = rate;
2823 _visualizer->getInteractorStyle()->GetInteractor()->SetDesiredUpdateRate(_renderingRate);
2828 #if VTK_MAJOR_VERSION >= 7
2829 _aSetEDLShading->setChecked(on);
2830 _visualizer->getRendererCollection()->InitTraversal ();
2831 vtkRenderer* renderer =
NULL;
2832 renderer = _visualizer->getRendererCollection()->GetNextItem ();
2833 renderer = _visualizer->getRendererCollection()->GetNextItem ();
2836 vtkOpenGLRenderer* glrenderer = vtkOpenGLRenderer::SafeDownCast(renderer);
2843 edl->SetDelegatePass(basicPasses);
2844 glrenderer->SetPass(edl);
2846 else if(glrenderer->GetPass())
2848 glrenderer->GetPass()->ReleaseGraphicsResources(
NULL);
2849 glrenderer->SetPass(
NULL);
2852 this->refreshView();
2856 UERROR(
"RTAB-Map must be built with VTK>=7 to enable EDL shading!");
2863 _aSetLighting->setChecked(on);
2864 pcl::visualization::CloudActorMapPtr cloudActorMap = _visualizer->getCloudActorMap();
2865 for(pcl::visualization::CloudActorMap::iterator
iter=cloudActorMap->begin();
iter!=cloudActorMap->end(); ++
iter)
2867 iter->second.actor->GetProperty()->SetLighting(_aSetLighting->isChecked());
2869 #if VTK_MAJOR_VERSION >= 7
2870 pcl::visualization::ShapeActorMapPtr shapeActorMap = _visualizer->getShapeActorMap();
2871 for(pcl::visualization::ShapeActorMap::iterator
iter=shapeActorMap->begin();
iter!=shapeActorMap->end(); ++
iter)
2873 vtkActor* actor = vtkActor::SafeDownCast (
iter->second);
2874 if(actor && _addedClouds.contains(
iter->first))
2876 actor->GetProperty()->SetLighting(_aSetLighting->isChecked());
2880 this->refreshView();
2885 _aSetFlatShading->setChecked(on);
2886 pcl::visualization::CloudActorMapPtr cloudActorMap = _visualizer->getCloudActorMap();
2887 for(pcl::visualization::CloudActorMap::iterator
iter=cloudActorMap->begin();
iter!=cloudActorMap->end(); ++
iter)
2889 iter->second.actor->GetProperty()->SetInterpolation(_aSetFlatShading->isChecked()?VTK_FLAT:VTK_PHONG);
2891 #if VTK_MAJOR_VERSION >= 7
2892 pcl::visualization::ShapeActorMapPtr shapeActorMap = _visualizer->getShapeActorMap();
2893 for(pcl::visualization::ShapeActorMap::iterator
iter=shapeActorMap->begin();
iter!=shapeActorMap->end(); ++
iter)
2895 vtkActor* actor = vtkActor::SafeDownCast (
iter->second);
2896 if(actor && _addedClouds.contains(
iter->first))
2898 actor->GetProperty()->SetInterpolation(_aSetFlatShading->isChecked()?VTK_FLAT:VTK_PHONG);
2902 this->refreshView();
2907 _aSetEdgeVisibility->setChecked(visible);
2908 pcl::visualization::CloudActorMapPtr cloudActorMap = _visualizer->getCloudActorMap();
2909 for(pcl::visualization::CloudActorMap::iterator
iter=cloudActorMap->begin();
iter!=cloudActorMap->end(); ++
iter)
2911 iter->second.actor->GetProperty()->SetEdgeVisibility(_aSetEdgeVisibility->isChecked());
2913 #if VTK_MAJOR_VERSION >= 7
2914 pcl::visualization::ShapeActorMapPtr shapeActorMap = _visualizer->getShapeActorMap();
2915 for(pcl::visualization::ShapeActorMap::iterator
iter=shapeActorMap->begin();
iter!=shapeActorMap->end(); ++
iter)
2917 vtkActor* actor = vtkActor::SafeDownCast (
iter->second);
2918 if(actor && _addedClouds.contains(
iter->first))
2920 actor->GetProperty()->SetEdgeVisibility(_aSetEdgeVisibility->isChecked());
2924 this->refreshView();
2929 _aSetScalarVisibility->setChecked(visible);
2930 pcl::visualization::ShapeActorMapPtr shapeActorMap = _visualizer->getShapeActorMap();
2931 for(pcl::visualization::ShapeActorMap::iterator
iter=shapeActorMap->begin();
iter!=shapeActorMap->end(); ++
iter)
2933 vtkActor* actor = vtkActor::SafeDownCast (
iter->second);
2934 if(actor && _addedClouds.contains(
iter->first))
2936 actor->GetMapper()->SetScalarVisibility(_aSetScalarVisibility->isChecked());
2939 this->refreshView();
2944 _visualizer->getRendererCollection()->InitTraversal ();
2945 vtkRenderer* renderer =
NULL;
2947 while ((renderer = _visualizer->getRendererCollection()->GetNextItem ()) !=
NULL)
2951 _visualizer->getInteractorStyle()->SetDefaultRenderer(renderer);
2952 _visualizer->getInteractorStyle()->SetCurrentRenderer(renderer);
2957 UWARN(
"Could not set layer %d to interactor (layers=%d).", layer, _visualizer->getRendererCollection()->GetNumberOfItems());
2961 float & x,
float & y,
float & z,
2962 float & focalX,
float & focalY,
float & focalZ,
2963 float & upX,
float & upY,
float & upZ)
const
2965 std::vector<pcl::visualization::Camera>
cameras;
2966 _visualizer->getCameras(
cameras);
2972 focalX =
cameras.begin()->focal[0];
2973 focalY =
cameras.begin()->focal[1];
2974 focalZ =
cameras.begin()->focal[2];
2975 upX =
cameras.begin()->view[0];
2976 upY =
cameras.begin()->view[1];
2977 upZ =
cameras.begin()->view[2];
2981 UERROR(
"No camera set!?");
2986 float x,
float y,
float z,
2987 float focalX,
float focalY,
float focalZ,
2988 float upX,
float upY,
float upZ)
2990 vtkRenderer* renderer =
NULL;
2991 double boundingBox[6] = {1, -1, 1, -1, 1, -1};
2994 _visualizer->getRendererCollection()->InitTraversal ();
2995 while ((renderer = _visualizer->getRendererCollection()->GetNextItem ()) !=
NULL)
2998 cam->SetPosition (
x,
y,
z);
2999 cam->SetFocalPoint (focalX, focalY, focalZ);
3000 cam->SetViewUp (upX, upY, upZ);
3003 renderer->ComputeVisiblePropBounds(BB);
3004 for (
int i = 0;
i < 6;
i++) {
3007 if (BB[
i] < boundingBox[
i]) {
3008 boundingBox[
i] = BB[
i];
3012 if (BB[
i] > boundingBox[
i]) {
3013 boundingBox[
i] = BB[
i];
3019 _visualizer->getRendererCollection()->InitTraversal ();
3020 while ((renderer = _visualizer->getRendererCollection()->GetNextItem ()) !=
NULL)
3022 renderer->ResetCameraClippingRange(boundingBox);
3031 Eigen::Vector3f
pos =
m.translation();
3033 _trajectory->push_back(pcl::PointXYZ(
pos[0],
pos[1],
pos[2]));
3034 if(_maxTrajectorySize>0)
3036 while(_trajectory->size() > _maxTrajectorySize)
3038 _trajectory->erase(_trajectory->begin());
3041 if(_aShowTrajectory->isChecked())
3043 _visualizer->removeShape(
"trajectory");
3044 pcl::PolygonMesh mesh;
3046 vertices.vertices.resize(_trajectory->size());
3047 for(
unsigned int i=0;
i<
vertices.vertices.size(); ++
i)
3051 pcl::toPCLPointCloud2(*_trajectory, mesh.cloud);
3053 _visualizer->addPolylineFromPolygonMesh(mesh,
"trajectory", 2);
3056 if(pose != _lastPose || _lastPose.
isNull())
3058 if(_lastPose.isNull())
3060 _lastPose.setIdentity();
3063 std::vector<pcl::visualization::Camera>
cameras;
3064 _visualizer->getCameras(
cameras);
3066 if(_aLockCamera->isChecked() || _aCameraOrtho->isChecked())
3069 Eigen::Vector3f
diff =
pos - Eigen::Vector3f(_lastPose.x(), _lastPose.y(), _lastPose.z());
3077 else if(_aFollowCamera->isChecked())
3079 Eigen::Vector3f vPosToFocal = Eigen::Vector3f(
cameras.front().focal[0] -
cameras.front().pos[0],
3083 Eigen::Vector3f yAxis = zAxis.cross(vPosToFocal);
3084 Eigen::Vector3f xAxis = yAxis.cross(zAxis);
3085 Transform PR(xAxis[0], xAxis[1], xAxis[2],0,
3086 yAxis[0], yAxis[1], yAxis[2],0,
3087 zAxis[0], zAxis[1], zAxis[2],0);
3092 PR[4], PR[5], PR[6],
cameras.front().pos[1],
3093 PR[8], PR[9], PR[10],
cameras.front().pos[2]);
3095 PR[4], PR[5], PR[6],
cameras.front().focal[1],
3096 PR[8], PR[9], PR[10],
cameras.front().focal[2]);
3113 cameras.front().view[0] = _aLockViewZ->isChecked()?0:Fp[8];
3114 cameras.front().view[1] = _aLockViewZ->isChecked()?0:Fp[9];
3115 cameras.front().view[2] = _aLockViewZ->isChecked()?1:Fp[10];
3118 if(_aShowCameraAxis->isChecked())
3120 #if PCL_VERSION_COMPARE(>=, 1, 7, 2)
3121 if(_coordinates.find(
"reference") != _coordinates.end())
3123 this->updateCoordinatePose(
"reference", pose);
3128 this->addOrUpdateCoordinate(
"reference", pose, 0.2);
3132 this->setCameraPosition(
3144 std::vector<CameraModel> models;
3145 models.push_back(
model.left());
3147 if(!
model.left().localTransform().isNull())
3151 models.push_back(right);
3152 updateCameraFrustums(pose, models);
3157 std::vector<CameraModel> models;
3158 models.push_back(
model);
3159 updateCameraFrustums(pose, models);
3166 if(_aShowFrustum->isChecked())
3169 for(
unsigned int i=0;
i<models.size(); ++
i)
3174 baseToCamera = models[
i].localTransform();
3176 std::string
id =
uFormat(
"reference_frustum_%d",
i);
3177 this->removeFrustum(
id);
3178 this->addOrUpdateFrustum(
id, pose, baseToCamera, _frustumScale, _frustumColor, models[
i].fovX(), models[
i].fovY());
3181 this->addOrUpdateLine(
uFormat(
"reference_frustum_line_%d",
i), pose, pose * baseToCamera, _frustumColor);
3189 std::vector<CameraModel> models;
3190 for(
size_t i=0;
i<stereoModels.size(); ++
i)
3192 models.push_back(stereoModels[
i].left());
3194 if(!stereoModels[
i].left().localTransform().
isNull())
3198 models.push_back(right);
3199 updateCameraFrustums(pose, models);
3205 return _defaultBgColor;
3210 if(_currentBgColor == _defaultBgColor)
3212 setBackgroundColor(color);
3214 _defaultBgColor = color;
3219 return _currentBgColor;
3224 _currentBgColor = color;
3225 _visualizer->setBackgroundColor(color.redF(), color.greenF(), color.blueF());
3230 pcl::visualization::CloudActorMapPtr cloudActorMap = _visualizer->getCloudActorMap();
3231 pcl::visualization::CloudActorMap::iterator
iter = cloudActorMap->find(
id);
3232 if(
iter != cloudActorMap->end())
3234 iter->second.actor->SetVisibility(isVisible?1:0);
3236 iter = cloudActorMap->find(
id+
"-normals");
3237 if(
iter != cloudActorMap->end())
3239 iter->second.actor->SetVisibility(isVisible&&_aShowNormals->isChecked()?1:0);
3244 #if VTK_MAJOR_VERSION >= 7
3245 pcl::visualization::ShapeActorMapPtr shapeActorMap = _visualizer->getShapeActorMap();
3246 pcl::visualization::ShapeActorMap::iterator
iter = shapeActorMap->find(
id);
3247 if(
iter != shapeActorMap->end())
3249 vtkActor* actor = vtkActor::SafeDownCast (
iter->second);
3252 actor->SetVisibility(isVisible?1:0);
3257 UERROR(
"Cannot find actor named \"%s\".",
id.
c_str());
3263 pcl::visualization::CloudActorMapPtr cloudActorMap = _visualizer->getCloudActorMap();
3264 pcl::visualization::CloudActorMap::iterator
iter = cloudActorMap->find(
id);
3265 if(
iter != cloudActorMap->end())
3267 return iter->second.actor->GetVisibility() != 0;
3271 #if VTK_MAJOR_VERSION >= 7
3272 pcl::visualization::ShapeActorMapPtr shapeActorMap = _visualizer->getShapeActorMap();
3273 pcl::visualization::ShapeActorMap::iterator
iter = shapeActorMap->find(
id);
3274 if(
iter != shapeActorMap->end())
3276 vtkActor* actor = vtkActor::SafeDownCast (
iter->second);
3279 return actor->GetVisibility() != 0;
3283 UERROR(
"Cannot find actor named \"%s\".",
id.
c_str());
3292 _visualizer->updateColorHandlerIndex(
id, index-1);
3299 if(_visualizer->getPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_OPACITY, lastOpacity,
id))
3301 if(lastOpacity != opacity)
3303 _visualizer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_OPACITY, opacity,
id);
3306 #if VTK_MAJOR_VERSION >= 7
3309 pcl::visualization::ShapeActorMap::iterator am_it = _visualizer->getShapeActorMap()->find (
id);
3310 if (am_it != _visualizer->getShapeActorMap()->end ())
3312 vtkActor* actor = vtkActor::SafeDownCast (am_it->second);
3315 actor->GetProperty ()->SetOpacity (opacity);
3326 if(_visualizer->getPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, lastSize,
id))
3328 if((
int)lastSize !=
size)
3330 _visualizer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, (
double)
size,
id);
3337 _aLockCamera->setChecked(enabled);
3342 _aFollowCamera->setChecked(enabled);
3347 _aLockCamera->setChecked(
false);
3348 _aFollowCamera->setChecked(
false);
3353 _lastCameraOrientation= _lastCameraPose = cv::Vec3f(0,0,0);
3354 _aLockViewZ->setChecked(enabled);
3358 _lastCameraOrientation= _lastCameraPose = cv::Vec3f(0,0,0);
3359 #if VTK_MAJOR_VERSION > 8
3360 CloudViewerInteractorStyle * interactor = CloudViewerInteractorStyle::SafeDownCast(this->interactor()->GetInteractorStyle());
3362 CloudViewerInteractorStyle * interactor = CloudViewerInteractorStyle::SafeDownCast(this->GetInteractor()->GetInteractorStyle());
3367 this->refreshView();
3369 _aCameraOrtho->setChecked(enabled);
3373 return _aLockCamera->isChecked();
3377 return _aFollowCamera->isChecked();
3381 return !_aFollowCamera->isChecked() && !_aLockCamera->isChecked();
3385 return _aLockViewZ->isChecked();
3389 return _aCameraOrtho->isChecked();
3393 return _aBackfaceCulling->isChecked();
3397 return _frontfaceCulling;
3401 return _aPolygonPicking->isChecked();
3405 return _aSetEDLShading->isChecked();
3409 return _aSetLighting->isChecked();
3413 return _aSetFlatShading->isChecked();
3417 return _aSetEdgeVisibility->isChecked();
3421 return _renderingRate;
3426 _aShowGrid->setChecked(shown);
3438 return _aShowGrid->isChecked();
3442 return _gridCellCount;
3446 return _gridCellSize;
3452 _gridCellCount =
count;
3453 if(_aShowGrid->isChecked())
3461 UERROR(
"Cannot set grid cell count < 1, count=%d",
count);
3468 _gridCellSize =
size;
3469 if(_aShowGrid->isChecked())
3477 UERROR(
"Cannot set grid cell size <= 0, value=%f",
size);
3482 if(_gridLines.empty())
3484 float cellSize = _gridCellSize;
3485 int cellCount = _gridCellCount;
3490 float min = -
float(cellCount/2) * cellSize;
3491 float max =
float(cellCount/2) * cellSize;
3497 _visualizer->addLine(
3498 pcl::PointXYZ(
i,
min, 0.0
f),
3499 pcl::PointXYZ(
i,
max, 0.0
f),
3501 _gridLines.push_back(
name);
3504 _visualizer->addLine(
3505 pcl::PointXYZ(
min,
i, 0),
3506 pcl::PointXYZ(
max,
i, 0),
3508 _gridLines.push_back(
name);
3511 std::vector<pcl::visualization::Camera>
cameras;
3512 _visualizer->getCameras(
cameras);
3513 this->setCameraPosition(
3522 for(std::list<std::string>::iterator
iter = _gridLines.begin();
iter!=_gridLines.end(); ++
iter)
3524 _visualizer->removeShape(*
iter);
3531 _aShowNormals->setChecked(shown);
3532 QList<std::string> ids = _addedClouds.keys();
3533 for(QList<std::string>::iterator
iter = ids.begin();
iter!=ids.end(); ++
iter)
3535 std::string idNormals = *
iter +
"-normals";
3536 if(_addedClouds.find(idNormals) != _addedClouds.end())
3538 this->setCloudVisibility(idNormals, this->getCloudVisibility(*
iter) && shown);
3544 return _aShowNormals->isChecked();
3548 return _normalsStep;
3552 return _normalsScale;
3558 _normalsStep =
step;
3562 UERROR(
"Cannot set normals step <= 0, step=%d",
step);
3569 _normalsScale=
scale;
3573 UERROR(
"Cannot set normals scale <= 0, value=%f",
scale);
3579 return _aSetIntensityRedColormap->isChecked();
3583 return _aSetIntensityRainbowColormap->isChecked();
3587 return _intensityAbsMax;
3592 _aSetIntensityRedColormap->setChecked(on);
3595 _aSetIntensityRainbowColormap->setChecked(
false);
3600 _aSetIntensityRainbowColormap->setChecked(on);
3603 _aSetIntensityRedColormap->setChecked(
false);
3610 _intensityAbsMax =
value;
3614 UERROR(
"Cannot set normals scale < 0, value=%f",
value);
3620 _buildLocator = enable;
3624 const Eigen::Vector3f & point,
3625 const Eigen::Vector3f &
axis,
3628 Eigen::Vector3f direction =
point;
3629 Eigen::Vector3f zAxis =
axis;
3630 float dotProdZ = zAxis.dot(direction);
3631 Eigen::Vector3f ptOnZaxis = zAxis * dotProdZ;
3632 direction -= ptOnZaxis;
3633 Eigen::Vector3f xAxis = direction.normalized();
3634 Eigen::Vector3f yAxis = zAxis.cross(xAxis);
3636 Eigen::Matrix3f newFrame;
3637 newFrame << xAxis[0], yAxis[0], zAxis[0],
3638 xAxis[1], yAxis[1], zAxis[1],
3639 xAxis[2], yAxis[2], zAxis[2];
3643 Eigen::Vector3f newDirection = newFrame.transpose() * direction;
3648 float magnitude = newDirection.norm();
3649 newDirection[0] = ( magnitude * cosTheta );
3650 newDirection[1] = ( magnitude * sinTheta );
3653 direction = newFrame * newDirection;
3655 return direction + ptOnZaxis;
3659 if(event->key() == Qt::Key_Up ||
3660 event->key() == Qt::Key_Down ||
3661 event->key() == Qt::Key_Left ||
3662 event->key() == Qt::Key_Right)
3664 _keysPressed -= (Qt::Key)event->key();
3668 PCLQVTKWidget::keyPressEvent(event);
3674 if(event->key() == Qt::Key_Up ||
3675 event->key() == Qt::Key_Down ||
3676 event->key() == Qt::Key_Left ||
3677 event->key() == Qt::Key_Right)
3679 _keysPressed += (Qt::Key)event->key();
3681 std::vector<pcl::visualization::Camera>
cameras;
3682 _visualizer->getCameras(
cameras);
3685 Eigen::Vector3f
pos(
cameras.front().pos[0],
cameras.front().pos[1], _aLockViewZ->isChecked()?0:
cameras.front().pos[2]);
3686 Eigen::Vector3f focal(
cameras.front().focal[0],
cameras.front().focal[1], _aLockViewZ->isChecked()?0:
cameras.front().focal[2]);
3688 Eigen::Vector3f cummulatedDir(0,0,0);
3689 Eigen::Vector3f cummulatedFocalDir(0,0,0);
3691 float stepRot = 0.02f;
3692 if(_keysPressed.contains(Qt::Key_Up))
3694 Eigen::Vector3f dir;
3695 if(event->modifiers() & Qt::ShiftModifier)
3697 dir = viewUp *
step;
3701 dir = (focal-
pos).normalized() *
step;
3703 cummulatedDir += dir;
3705 if(_keysPressed.contains(Qt::Key_Down))
3707 Eigen::Vector3f dir;
3708 if(event->modifiers() & Qt::ShiftModifier)
3710 dir = viewUp * -
step;
3714 dir = (focal-
pos).normalized() * -
step;
3716 cummulatedDir += dir;
3718 if(_keysPressed.contains(Qt::Key_Right))
3720 if(event->modifiers() & Qt::ShiftModifier)
3723 Eigen::Vector3f
point = (focal-
pos);
3725 Eigen::Vector3f
diff = newPoint -
point;
3726 cummulatedFocalDir +=
diff;
3730 Eigen::Vector3f dir = ((focal-
pos).
cross(viewUp)).normalized() *
step;
3731 cummulatedDir += dir;
3734 if(_keysPressed.contains(Qt::Key_Left))
3736 if(event->modifiers() & Qt::ShiftModifier)
3739 Eigen::Vector3f
point = (focal-
pos);
3741 Eigen::Vector3f
diff = newPoint -
point;
3742 cummulatedFocalDir +=
diff;
3746 Eigen::Vector3f dir = ((focal-
pos).
cross(viewUp)).normalized() * -
step;
3747 cummulatedDir += dir;
3751 cameras.front().pos[0] += cummulatedDir[0];
3752 cameras.front().pos[1] += cummulatedDir[1];
3753 cameras.front().pos[2] += cummulatedDir[2];
3754 cameras.front().focal[0] += cummulatedDir[0] + cummulatedFocalDir[0];
3755 cameras.front().focal[1] += cummulatedDir[1] + cummulatedFocalDir[1];
3756 cameras.front().focal[2] += cummulatedDir[2] + cummulatedFocalDir[2];
3757 this->setCameraPosition(
3762 Q_EMIT configChanged();
3766 PCLQVTKWidget::keyPressEvent(event);
3772 if(event->button() == Qt::RightButton)
3778 PCLQVTKWidget::mousePressEvent(event);
3784 PCLQVTKWidget::mouseMoveEvent(event);
3786 std::vector<pcl::visualization::Camera>
cameras;
3787 _visualizer->getCameras(
cameras);
3790 if(_aLockViewZ->isChecked() && !_aCameraOrtho->isChecked())
3792 cv::Vec3d newCameraOrientation = cv::Vec3d(0,0,1).cross(cv::Vec3d(
cameras.front().pos)-cv::Vec3d(
cameras.front().focal));
3794 if( _lastCameraOrientation!=cv::Vec3d(0,0,0) &&
3795 _lastCameraPose!=cv::Vec3d(0,0,0) &&
3796 (
uSign(_lastCameraOrientation[0]) !=
uSign(newCameraOrientation[0]) &&
3797 uSign(_lastCameraOrientation[1]) !=
uSign(newCameraOrientation[1])))
3799 cameras.front().pos[0] = _lastCameraPose[0];
3800 cameras.front().pos[1] = _lastCameraPose[1];
3801 cameras.front().pos[2] = _lastCameraPose[2];
3803 else if(newCameraOrientation != cv::Vec3d(0,0,0))
3805 _lastCameraOrientation = newCameraOrientation;
3806 _lastCameraPose = cv::Vec3d(
cameras.front().pos);
3810 if(
cameras.front().view[2] == 0)
3817 cameras.front().pos[0] -= 0.00001;
3825 this->setCameraPosition(
3830 Q_EMIT configChanged();
3835 PCLQVTKWidget::wheelEvent(event);
3837 std::vector<pcl::visualization::Camera>
cameras;
3838 _visualizer->getCameras(
cameras);
3840 if(_aLockViewZ->isChecked() && !_aCameraOrtho->isChecked())
3842 _lastCameraPose = cv::Vec3d(
cameras.front().pos);
3845 this->setCameraPosition(
3850 Q_EMIT configChanged();
3855 QAction *
a = _menu->exec(event->globalPos());
3859 Q_EMIT configChanged();
3865 if(
a == _aSetTrajectorySize)
3868 int value = QInputDialog::getInt(
this, tr(
"Set trajectory size"), tr(
"Size (0=infinite)"), _maxTrajectorySize, 0, 10000, 10, &ok);
3871 _maxTrajectorySize =
value;
3874 else if(
a == _aClearTrajectory)
3876 this->clearTrajectory();
3878 else if(
a == _aShowCameraAxis)
3880 this->setCameraAxisShown(
a->isChecked());
3882 else if(
a == _aSetFrameScale)
3885 double value = QInputDialog::getDouble(
this, tr(
"Set frame scale"), tr(
"Scale"), _coordinateFrameScale, 0.1, 999.0, 1, &ok);
3888 this->setCoordinateFrameScale(
value);
3891 else if(
a == _aShowFrustum)
3893 this->setFrustumShown(
a->isChecked());
3895 else if(
a == _aSetFrustumScale)
3898 double value = QInputDialog::getDouble(
this, tr(
"Set frustum scale"), tr(
"Scale"), _frustumScale, 0.0, 999.0, 1, &ok);
3901 this->setFrustumScale(
value);
3904 else if(
a == _aSetFrustumColor)
3906 QColor
value = QColorDialog::getColor(_frustumColor,
this);
3909 this->setFrustumColor(
value);
3912 else if(
a == _aResetCamera)
3914 this->resetCamera();
3916 else if(
a == _aShowGrid)
3918 if(_aShowGrid->isChecked())
3927 this->refreshView();
3929 else if(
a == _aSetGridCellCount)
3932 int value = QInputDialog::getInt(
this, tr(
"Set grid cell count"), tr(
"Count"), _gridCellCount, 1, 10000, 10, &ok);
3935 this->setGridCellCount(
value);
3938 else if(
a == _aSetGridCellSize)
3941 double value = QInputDialog::getDouble(
this, tr(
"Set grid cell size"), tr(
"Size (m)"), _gridCellSize, 0.01, 1000, 2, &ok);
3944 this->setGridCellSize(
value);
3947 else if(
a == _aShowNormals)
3949 this->setNormalsShown(_aShowNormals->isChecked());
3950 this->refreshView();
3952 else if(
a == _aSetNormalsStep)
3955 int value = QInputDialog::getInt(
this, tr(
"Set normals step"), tr(
"Step"), _normalsStep, 1, 10000, 1, &ok);
3958 this->setNormalsStep(
value);
3961 else if(
a == _aSetNormalsScale)
3964 double value = QInputDialog::getDouble(
this, tr(
"Set normals scale"), tr(
"Scale (m)"), _normalsScale, 0.01, 10, 2, &ok);
3967 this->setNormalsScale(
value);
3970 else if(
a == _aSetIntensityMaximum)
3973 double value = QInputDialog::getDouble(
this, tr(
"Set maximum absolute intensity"), tr(
"Intensity (0=auto)"), _intensityAbsMax, 0.0, 99999, 2, &ok);
3976 this->setIntensityMax(
value);
3979 else if(
a == _aSetIntensityRedColormap)
3981 this->setIntensityRedColormap(_aSetIntensityRedColormap->isChecked());
3983 else if(
a == _aSetIntensityRainbowColormap)
3985 this->setIntensityRainbowColormap(_aSetIntensityRainbowColormap->isChecked());
3987 else if(
a == _aSetBackgroundColor)
3989 QColor color = this->getDefaultBackgroundColor();
3990 color = QColorDialog::getColor(color,
this);
3993 this->setDefaultBackgroundColor(color);
3994 this->refreshView();
3997 else if(
a == _aSetRenderingRate)
4000 double value = QInputDialog::getDouble(
this, tr(
"Rendering rate"), tr(
"Rate (hz)"), _renderingRate, 0, 60, 0, &ok);
4003 this->setRenderingRate(
value);
4006 else if(
a == _aLockViewZ)
4008 if(_aLockViewZ->isChecked())
4010 this->refreshView();
4013 else if(
a == _aCameraOrtho)
4015 this->setCameraOrtho(_aCameraOrtho->isChecked());
4017 else if(
a == _aSetEDLShading)
4019 this->setEDLShading(_aSetEDLShading->isChecked());
4021 else if(
a == _aSetLighting)
4023 this->setLighting(_aSetLighting->isChecked());
4025 else if(
a == _aSetFlatShading)
4027 this->setShading(_aSetFlatShading->isChecked());
4029 else if(
a == _aSetEdgeVisibility)
4031 this->setEdgeVisibility(_aSetEdgeVisibility->isChecked());
4033 else if(
a == _aSetScalarVisibility)
4035 this->setScalarVisibility(_aSetScalarVisibility->isChecked());
4037 else if(
a == _aBackfaceCulling)
4039 this->setBackfaceCulling(_aBackfaceCulling->isChecked(), _frontfaceCulling);
4041 else if(
a == _aPolygonPicking)
4043 this->setPolygonPicking(_aPolygonPicking->isChecked());