PointCloudVisualization.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package <PACKAGE_NAME>::<CATEGORY>::PointCloudVisualization
17  * @author Stefan Reither ( stef dot reither at web dot de )
18  * @date 2018
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
24 
25 //Coin includes
26 #include <Inventor/nodes/SoMaterial.h>
27 #include <Inventor/nodes/SoMaterialBinding.h>
28 #include <Inventor/nodes/SoCoordinate3.h>
29 #include <Inventor/nodes/SoDrawStyle.h>
30 #include <Inventor/nodes/SoPointSet.h>
31 #include <Inventor/SbVec3f.h>
32 #include <Inventor/nodes/SoTransform.h>
33 #include <Inventor/nodes/SoSphere.h>
34 
36 
37 using namespace armarx;
38 
40 {
41  this->ref();
42 }
43 
45 {
46  this->removeAllChildren();
47  this->unref();
48 }
49 
50 void PointCloudVisualization::setVisualization(pcl::PointCloud<PointT>::ConstPtr cloud)
51 {
52  // Clear all previous visualization
53  this->removeAllChildren();
54 
55  //Insert color information into scene graph
56  SoMaterial* materialInfo = new SoMaterial();
57  std::vector<SbColor> colorData;
58  colorData.reserve(cloud->points.size());
59  //Add point coordinates
60  SoCoordinate3* coordinates = new SoCoordinate3();
61  std::vector<SbVec3f> pointData;
62  pointData.reserve(cloud->points.size());
63  for (const PointT& p : cloud->points)
64  {
65  if (std::isfinite(p.x) && std::isfinite(p.y) && std::isfinite(p.z))
66  {
67  SbColor colorContainer;
68  if (useOriginalColors)
69  {
70  float colorArray[3];
71 
72  colorArray[0] = (float) p.r / 256.0f;
73  colorArray[1] = (float) p.g / 256.0f;
74  colorArray[2] = (float) p.b / 256.0f;
75 
76  colorContainer.setValue(colorArray);
77  }
78  else
79  {
80  colorContainer = this->color;
81  }
82 
83  colorData.push_back(colorContainer);
84 
85  SbVec3f pointContainer;
86  //Factor 1000 to match coin unit system
87  pointContainer[0] = p.x / 1000;
88  pointContainer[1] = p.y / 1000;
89  pointContainer[2] = p.z / 1000;
90  pointData.push_back(pointContainer);
91  }
92  }
93 
94 
95  materialInfo->diffuseColor.setValues(0, colorData.size(), colorData.data());
96  this->addChild(materialInfo);
97 
98  //Bind materials to per part
99  SoMaterialBinding* binding = new SoMaterialBinding();
100  binding->value = SoMaterialBinding::PER_PART;
101  this->addChild(binding);
102 
103  coordinates->point.setValues(0, pointData.size(), pointData.data());
104  this->addChild(coordinates);
105 
106  //Set point size
107  SoDrawStyle* pointSize = new SoDrawStyle();
108  pointSize->pointSize = this->pointSize;
109  this->addChild(pointSize);
110 
111  //Draw a point set out of all that data
112  SoPointSet* pointSet = new SoPointSet();
113  this->addChild(pointSet);
114 }
115 
117 {
118  this->color = color;
119  this->useOriginalColors = false;
120 }
121 
123 {
124  if (size < 1)
125  {
126  size = 1;
127  }
128  this->pointSize = size;
129 }
130 
132 {
133  this->useOriginalColors = true;
134 }
armarx::PointCloudVisualization::setDrawColor
void setDrawColor(SbColor color)
Definition: PointCloudVisualization.cpp:116
armarx::PointCloudVisualization::PointCloudVisualization
PointCloudVisualization()
Definition: PointCloudVisualization.cpp:39
armarx::PointCloudVisualization::resetDrawColor
void resetDrawColor()
Definition: PointCloudVisualization.cpp:131
armarx::PointCloudVisualization::~PointCloudVisualization
~PointCloudVisualization() override
Definition: PointCloudVisualization.cpp:44
std::isfinite
bool isfinite(const std::vector< T, Ts... > &v)
Definition: algorithm.h:327
armarx::PointT
pcl::PointXYZRGBL PointT
Definition: Common.h:28
armarx::PointCloudVisualization::setPointSize
void setPointSize(int size)
Definition: PointCloudVisualization.cpp:122
armarx::PointCloudVisualization::setVisualization
void setVisualization(pcl::PointCloud< PointT >::ConstPtr cloud)
Definition: PointCloudVisualization.cpp:50
PointCloudVisualization.h
float
#define float
Definition: 16_Level.h:22
Logging.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28