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
27
28#include <Inventor/SbVec3f.h>
29#include <Inventor/nodes/SoCoordinate3.h>
30#include <Inventor/nodes/SoDrawStyle.h>
31#include <Inventor/nodes/SoMaterial.h>
32#include <Inventor/nodes/SoMaterialBinding.h>
33#include <Inventor/nodes/SoPointSet.h>
34#include <Inventor/nodes/SoSphere.h>
35#include <Inventor/nodes/SoTransform.h>
36
37using namespace armarx;
38
43
45{
46 this->removeAllChildren();
47 this->unref();
48}
49
50void
51PointCloudVisualization::setVisualization(pcl::PointCloud<PointT>::ConstPtr cloud)
52{
53 // Clear all previous visualization
54 this->removeAllChildren();
55
56 //Insert color information into scene graph
57 SoMaterial* materialInfo = new SoMaterial();
58 std::vector<SbColor> colorData;
59 colorData.reserve(cloud->points.size());
60 //Add point coordinates
61 SoCoordinate3* coordinates = new SoCoordinate3();
62 std::vector<SbVec3f> pointData;
63 pointData.reserve(cloud->points.size());
64 for (const PointT& p : cloud->points)
65 {
66 if (std::isfinite(p.x) && std::isfinite(p.y) && std::isfinite(p.z))
67 {
68 SbColor colorContainer;
69 if (useOriginalColors)
70 {
71 float colorArray[3];
72
73 colorArray[0] = (float)p.r / 256.0f;
74 colorArray[1] = (float)p.g / 256.0f;
75 colorArray[2] = (float)p.b / 256.0f;
76
77 colorContainer.setValue(colorArray);
78 }
79 else
80 {
81 colorContainer = this->color;
82 }
83
84 colorData.push_back(colorContainer);
85
86 SbVec3f pointContainer;
87 //Factor 1000 to match coin unit system
88 pointContainer[0] = p.x / 1000;
89 pointContainer[1] = p.y / 1000;
90 pointContainer[2] = p.z / 1000;
91 pointData.push_back(pointContainer);
92 }
93 }
94
95
96 materialInfo->diffuseColor.setValues(0, colorData.size(), colorData.data());
97 this->addChild(materialInfo);
98
99 //Bind materials to per part
100 SoMaterialBinding* binding = new SoMaterialBinding();
101 binding->value = SoMaterialBinding::PER_PART;
102 this->addChild(binding);
103
104 coordinates->point.setValues(0, pointData.size(), pointData.data());
105 this->addChild(coordinates);
106
107 //Set point size
108 SoDrawStyle* pointSize = new SoDrawStyle();
109 pointSize->pointSize = this->pointSize;
110 this->addChild(pointSize);
111
112 //Draw a point set out of all that data
113 SoPointSet* pointSet = new SoPointSet();
114 this->addChild(pointSet);
115}
116
117void
119{
120 this->color = color;
121 this->useOriginalColors = false;
122}
123
124void
126{
127 if (size < 1)
128 {
129 size = 1;
130 }
131 this->pointSize = size;
132}
133
134void
136{
137 this->useOriginalColors = true;
138}
#define float
Definition 16_Level.h:22
void setVisualization(pcl::PointCloud< PointT >::ConstPtr cloud)
This file offers overloads of toIce() and fromIce() functions for STL container types.
pcl::PointXYZRGBL PointT
Definition Common.h:30
bool isfinite(const std::vector< T, Ts... > &v)
Definition algorithm.h:366